Release 2.2.0 RC 5
[hildon] / examples / hildon-edit-toolbar-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include                                        <gtk/gtk.h>
24 #include                                        <hildon/hildon.h>
25
26 typedef enum
27 {
28   NORMAL_MODE,
29   EDIT_MODE
30 } TreeViewMode;
31
32
33 static GtkTreeModel *
34 get_model                                       (void)
35 {
36   int i;
37   static GtkListStore *store = NULL;
38
39   if (store != NULL)
40     return GTK_TREE_MODEL (store);
41
42   store = gtk_list_store_new (1, G_TYPE_STRING);
43
44   for (i = 0; i < 50; i++)
45     {
46       gchar *str;
47
48       str = g_strdup_printf ("\nRow %d\n", i);
49       gtk_list_store_insert_with_values (store, NULL, i, 0, str, -1);
50       g_free (str);
51     }
52
53   return GTK_TREE_MODEL (store);
54 }
55
56 static GtkWidget *
57 create_icon_view                                (TreeViewMode  tvmode)
58 {
59   GtkWidget *icon_view;
60   GtkTreeModel *model;
61   GtkCellRenderer *renderer;
62
63   if (tvmode == NORMAL_MODE)
64     {
65 #ifdef MAEMO_GTK
66       icon_view = hildon_gtk_icon_view_new (HILDON_UI_MODE_NORMAL);
67 #else
68       icon_view = gtk_icon_view_new ();
69 #endif /* MAEMO_GTK */
70     }
71   else
72     {
73 #ifdef MAEMO_GTK
74       icon_view = hildon_gtk_icon_view_new (HILDON_UI_MODE_EDIT);
75 #else
76       icon_view = gtk_icon_view_new ();
77 #endif /* MAEMO_GTK */
78
79       gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
80                                         GTK_SELECTION_MULTIPLE);
81     }
82
83   model = get_model ();
84   gtk_icon_view_set_model (GTK_ICON_VIEW (icon_view), model);
85
86   renderer = gtk_cell_renderer_pixbuf_new ();
87   g_object_set (renderer, "stock-id", GTK_STOCK_NEW, NULL);
88   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), renderer, TRUE);
89
90   renderer = gtk_cell_renderer_text_new ();
91   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), renderer, FALSE);
92   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view), renderer, "text", 0, NULL);
93
94   return icon_view;
95 }
96
97 static void
98 delete_button_clicked                           (GtkButton   *button,
99                                                  GtkIconView *iconview)
100 {
101   GtkTreeModel *model;
102   GList *items, *iter, *refs;
103   GtkWidget *window;
104
105   items = gtk_icon_view_get_selected_items (iconview);
106   model = gtk_icon_view_get_model (iconview);
107   refs = NULL;
108
109   /* Get row references for all selected items */
110   for (iter = items; iter != NULL; iter = iter->next)
111     {
112       GtkTreePath *path = (GtkTreePath *) iter->data;
113       GtkTreeRowReference *ref = gtk_tree_row_reference_new (model, path);
114       refs = g_list_prepend (refs, gtk_tree_row_reference_copy (ref));
115       gtk_tree_row_reference_free (ref);
116     }
117
118   g_list_foreach (items, (GFunc) gtk_tree_path_free, NULL);
119   g_list_free (items);
120
121   /* Remove all selected items from the model */
122   for (iter = refs; iter != NULL; iter = iter->next)
123     {
124       GtkTreeIter treeiter;
125       GtkTreeRowReference *ref = (GtkTreeRowReference *) iter->data;
126       GtkTreePath *path = gtk_tree_row_reference_get_path (ref);
127       gtk_tree_model_get_iter (model, &treeiter, path);
128       gtk_list_store_remove (GTK_LIST_STORE (model), &treeiter);
129     }
130
131   g_list_foreach (refs, (GFunc) gtk_tree_row_reference_free, NULL);
132   g_list_free (refs);
133
134   /* After removing the items, close the window */
135   window = gtk_widget_get_toplevel (GTK_WIDGET (iconview));
136   gtk_widget_destroy (window);
137 }
138
139 static void
140 edit_window                                     (void)
141 {
142   GtkWidget *window;
143   GtkWidget *iconview;
144   GtkWidget *toolbar;
145   GtkWidget *area;
146
147   window = hildon_stackable_window_new ();
148   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
149
150   toolbar = hildon_edit_toolbar_new_with_text ("Choose items to delete", "Delete");
151   area = hildon_pannable_area_new ();
152   iconview = create_icon_view (EDIT_MODE);
153
154   hildon_window_set_edit_toolbar (HILDON_WINDOW (window), HILDON_EDIT_TOOLBAR (toolbar));
155
156   gtk_container_add (GTK_CONTAINER (area), iconview);
157   gtk_container_add (GTK_CONTAINER (window), area);
158
159   g_signal_connect (toolbar, "button-clicked", G_CALLBACK (delete_button_clicked), iconview);
160   g_signal_connect_swapped (toolbar, "arrow-clicked", G_CALLBACK (gtk_widget_destroy), window);
161
162   gtk_widget_show_all (window);
163   gtk_window_fullscreen (GTK_WINDOW (window));
164 }
165
166 int
167 main                                            (int    argc,
168                                                  char **argv)
169 {
170   GtkWidget *window;
171   GtkWidget *iconview;
172   GtkWidget *vbox;
173   GtkWidget *button;
174   GtkWidget *area;
175
176   hildon_gtk_init (&argc, &argv);
177
178   window = hildon_stackable_window_new ();
179   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
180   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
181
182   vbox = gtk_vbox_new (FALSE, 10);
183   area = hildon_pannable_area_new ();
184   iconview = create_icon_view (NORMAL_MODE);
185   button = hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
186   gtk_button_set_label (GTK_BUTTON (button), "Delete some items");
187
188   gtk_container_add (GTK_CONTAINER (area), iconview);
189   gtk_box_pack_start (GTK_BOX (vbox), area, TRUE, TRUE, 0);
190   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
191
192   gtk_container_add (GTK_CONTAINER (window), vbox);
193
194   g_signal_connect (button, "clicked", G_CALLBACK (edit_window), NULL);
195
196   gtk_widget_show_all (window);
197
198   gtk_main ();
199
200   return 0;
201 }