2008-09-24 Claudio Saavedra <csaavedra@igalia.com>
[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.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       icon_view = hildon_gtk_icon_view_new (HILDON_UI_MODE_NORMAL);
66     }
67   else
68     {
69       icon_view = hildon_gtk_icon_view_new (HILDON_UI_MODE_EDIT);
70       gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
71                                         GTK_SELECTION_MULTIPLE);
72     }
73
74   model = get_model ();
75   gtk_icon_view_set_model (GTK_ICON_VIEW (icon_view), model);
76
77   renderer = gtk_cell_renderer_pixbuf_new ();
78   g_object_set (renderer, "stock-id", GTK_STOCK_NEW, NULL);
79   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), renderer, TRUE);
80
81   renderer = gtk_cell_renderer_text_new ();
82   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), renderer, FALSE);
83   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view), renderer, "text", 0, NULL);
84
85   return icon_view;
86 }
87
88 static void
89 delete_button_clicked                           (GtkButton   *button,
90                                                  GtkIconView *iconview)
91 {
92   GtkTreeModel *model;
93   GList *items, *iter, *refs;
94   GtkWidget *window;
95
96   items = gtk_icon_view_get_selected_items (iconview);
97   model = gtk_icon_view_get_model (iconview);
98   refs = NULL;
99
100   /* Get row references for all selected items */
101   for (iter = items; iter != NULL; iter = iter->next)
102     {
103       GtkTreePath *path = (GtkTreePath *) iter->data;
104       GtkTreeRowReference *ref = gtk_tree_row_reference_new (model, path);
105       refs = g_list_prepend (refs, gtk_tree_row_reference_copy (ref));
106       gtk_tree_row_reference_free (ref);
107     }
108
109   g_list_foreach (items, (GFunc) gtk_tree_path_free, NULL);
110   g_list_free (items);
111
112   /* Remove all selected items from the model */
113   for (iter = refs; iter != NULL; iter = iter->next)
114     {
115       GtkTreeIter treeiter;
116       GtkTreeRowReference *ref = (GtkTreeRowReference *) iter->data;
117       GtkTreePath *path = gtk_tree_row_reference_get_path (ref);
118       gtk_tree_model_get_iter (model, &treeiter, path);
119       gtk_list_store_remove (GTK_LIST_STORE (model), &treeiter);
120     }
121
122   g_list_foreach (refs, (GFunc) gtk_tree_row_reference_free, NULL);
123   g_list_free (refs);
124
125   /* After removing the items, close the window */
126   window = gtk_widget_get_toplevel (GTK_WIDGET (iconview));
127   gtk_widget_destroy (window);
128 }
129
130 static void
131 edit_window                                     (void)
132 {
133   GtkWidget *window;
134   GtkWidget *iconview;
135   GtkWidget *vbox;
136   GtkWidget *toolbar;
137   GtkWidget *area;
138
139   window = hildon_stackable_window_new ();
140   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
141
142   vbox = gtk_vbox_new (FALSE, 10);
143   toolbar = hildon_edit_toolbar_new_with_text ("Choose items to delete", "Delete");
144   area = hildon_pannable_area_new ();
145   iconview = create_icon_view (EDIT_MODE);
146
147   gtk_container_add (GTK_CONTAINER (area), iconview);
148   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
149   gtk_box_pack_start (GTK_BOX (vbox), area, TRUE, TRUE, 0);
150
151   gtk_container_add (GTK_CONTAINER (window), vbox);
152
153   g_signal_connect (toolbar, "button-clicked", G_CALLBACK (delete_button_clicked), iconview);
154   g_signal_connect_swapped (toolbar, "arrow-clicked", G_CALLBACK (gtk_widget_destroy), window);
155
156   gtk_widget_show_all (window);
157   gtk_window_fullscreen (GTK_WINDOW (window));
158 }
159
160 int
161 main                                            (int    argc,
162                                                  char **argv)
163 {
164   GtkWidget *window;
165   GtkWidget *iconview;
166   GtkWidget *vbox;
167   GtkWidget *button;
168   GtkWidget *area;
169
170   gtk_init (&argc, &argv);
171
172   gtk_rc_parse_string ("style \"fremantle-widget\" {\n"
173                        "  GtkWidget::hildon-mode = 1\n"
174                        "} class \"GtkIconView\" style \"fremantle-widget\"");
175
176   window = hildon_stackable_window_new ();
177   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
178   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
179
180   vbox = gtk_vbox_new (FALSE, 10);
181   area = hildon_pannable_area_new ();
182   iconview = create_icon_view (NORMAL_MODE);
183   button = hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
184   gtk_button_set_label (GTK_BUTTON (button), "Delete some items");
185
186   gtk_container_add (GTK_CONTAINER (area), iconview);
187   gtk_box_pack_start (GTK_BOX (vbox), area, TRUE, TRUE, 0);
188   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
189
190   gtk_container_add (GTK_CONTAINER (window), vbox);
191
192   g_signal_connect (button, "clicked", G_CALLBACK (edit_window), NULL);
193
194   gtk_widget_show_all (window);
195
196   gtk_main ();
197
198   return 0;
199 }