* src/hildon-pannable-area.c: (hildon_pannable_area_refresh), (hildon_pannable_area_m...
[hildon] / examples / hildon-pannable-area-touch-grid-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Author: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * Based on testhildoniconview.c by Kristian Rietveld <kris@imendio.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; version 2.1 of
13  * the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <gtk/gtk.h>
28 #include <hildon.h>
29
30 enum
31 {
32   MULTI_SELECT  = 1 << 0,
33   NORMAL_MODE   = 1 << 1,
34   EDIT_MODE     = 1 << 2
35 };
36
37
38 static GtkTreeModel *
39 create_model (void)
40 {
41   int i;
42   GtkListStore *store;
43
44   store = gtk_list_store_new (1, G_TYPE_STRING);
45
46   for (i = 0; i < 50; i++)
47     {
48       gchar *str;
49
50       str = g_strdup_printf ("\nRow %d\n", i);
51       gtk_list_store_insert_with_values (store, NULL, i, 0, str, -1);
52       g_free (str);
53     }
54
55   return GTK_TREE_MODEL (store);
56 }
57
58 static void
59 item_activated_callback (GtkWidget         *icon_view,
60                          GtkTreePath       *path,
61                          gpointer           user_data)
62 {
63   g_print ("item-activated emitted.\n");
64 }
65
66 static GtkWidget *
67 create_icon_view (HildonUIMode  mode,
68                   const char   *name,
69                   gboolean      multi_select)
70 {
71   GtkWidget *icon_view;
72   GtkCellRenderer *renderer;
73   GtkTreeModel *model;
74
75   if (name && g_str_equal (name, "fremantle-widget"))
76       icon_view = hildon_gtk_icon_view_new (mode);
77   else
78       icon_view = gtk_icon_view_new ();
79
80   if (name)
81     gtk_widget_set_name (icon_view, name);
82
83   model = create_model ();
84   gtk_icon_view_set_model (GTK_ICON_VIEW (icon_view), model);
85   g_object_unref (model);
86
87   if (multi_select)
88     gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
89                                       GTK_SELECTION_MULTIPLE);
90   else if (mode != HILDON_UI_MODE_NORMAL)
91     gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
92                                       GTK_SELECTION_SINGLE);
93
94   renderer = gtk_cell_renderer_pixbuf_new ();
95   g_object_set (renderer, "stock-id", GTK_STOCK_NEW, NULL);
96   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
97                               renderer,
98                               TRUE);
99
100   renderer = gtk_cell_renderer_text_new ();
101   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
102                               renderer,
103                               FALSE);
104   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
105                                   renderer,
106                                   "text", 0,
107                                   NULL);
108
109   return icon_view;
110 }
111
112 static void
113 create_icon_view_window (GtkWidget *button,
114                          gpointer   user_data)
115 {
116   const char *name;
117   GtkWidget *window;
118   GtkWidget *sw;
119   GtkWidget *icon_view;
120   HildonUIMode mode = 0;
121
122   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
123   g_signal_connect (window, "delete-event",
124                     G_CALLBACK (gtk_widget_destroy), window);
125   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
126
127   sw = hildon_pannable_area_new ();
128   gtk_container_add (GTK_CONTAINER (window), sw);
129
130   if ((GPOINTER_TO_INT (user_data) & NORMAL_MODE) == NORMAL_MODE)
131     {
132       mode = HILDON_UI_MODE_NORMAL;
133       name = "fremantle-widget";
134     }
135   else if ((GPOINTER_TO_INT (user_data) & EDIT_MODE) == EDIT_MODE)
136     {
137       mode = HILDON_UI_MODE_EDIT;
138       name = "fremantle-widget";
139     }
140   else
141     name = NULL;
142
143   icon_view = create_icon_view (mode, name,
144                                 (GPOINTER_TO_INT (user_data) & MULTI_SELECT) == MULTI_SELECT);
145
146   /* Some signals doing printfs() to see if the behavior is correct. */
147   g_signal_connect (icon_view, "item-activated",
148                     G_CALLBACK (item_activated_callback), NULL);
149
150   gtk_widget_set_size_request (icon_view, 480, 800);
151   gtk_container_add (GTK_CONTAINER (sw), icon_view);
152
153   gtk_widget_show_all (window);
154 }
155
156 int
157 main (int argc, char **argv)
158 {
159   GtkWidget *window;
160   GtkWidget *mainbox;
161   GtkWidget *label;
162   GtkWidget *vbox;
163   GtkWidget *padbox;
164   GtkWidget *button;
165
166   gtk_init (&argc, &argv);
167
168   gtk_rc_parse_string ("style \"fremantle-widget\" {\n"
169                        "  GtkWidget::hildon-mode = 1\n"
170                        "} widget \"*.fremantle-widget\" style \"fremantle-widget\"");
171
172   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
173   g_signal_connect (window, "delete-event",
174                     G_CALLBACK (gtk_main_quit), NULL);
175   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
176
177   mainbox = gtk_vbox_new (FALSE, 6);
178   gtk_container_add (GTK_CONTAINER (window), mainbox);
179
180   /* old-style */
181   label = gtk_label_new (NULL);
182   gtk_label_set_markup (GTK_LABEL (label), "<b>Old-style behavior</b>");
183   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
184   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
185
186   padbox = gtk_hbox_new (FALSE, 6);
187   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
188
189   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
190                       FALSE, FALSE, 6);
191
192   vbox = gtk_vbox_new (FALSE, 6);
193   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
194
195   button = gtk_button_new_with_label ("Single selection");
196   g_signal_connect (button, "clicked",
197                     G_CALLBACK (create_icon_view_window), 0x0);
198   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
199
200   button = gtk_button_new_with_label ("Multiple selections");
201   g_signal_connect (button, "clicked",
202                     G_CALLBACK (create_icon_view_window),
203                     GINT_TO_POINTER (MULTI_SELECT));
204   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
205
206   /* normal-mode */
207   label = gtk_label_new (NULL);
208   gtk_label_set_markup (GTK_LABEL (label), "<b>Fremantle Normal mode</b>");
209   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
210   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
211
212   padbox = gtk_hbox_new (FALSE, 6);
213   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
214
215   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
216                       FALSE, FALSE, 6);
217
218   vbox = gtk_vbox_new (FALSE, 6);
219   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
220
221   button = gtk_button_new_with_label ("Direct activation");
222   g_signal_connect (button, "clicked",
223                     G_CALLBACK (create_icon_view_window),
224                     GINT_TO_POINTER (NORMAL_MODE));
225   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
226
227   /* edit-mode */
228   label = gtk_label_new (NULL);
229   gtk_label_set_markup (GTK_LABEL (label), "<b>Fremantle Edit mode</b>");
230   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
231   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
232
233   padbox = gtk_hbox_new (FALSE, 6);
234   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
235
236   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
237                       FALSE, FALSE, 6);
238
239   vbox = gtk_vbox_new (FALSE, 6);
240   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
241
242   button = gtk_button_new_with_label ("Single selection");
243   g_signal_connect (button, "clicked",
244                     G_CALLBACK (create_icon_view_window),
245                     GINT_TO_POINTER (EDIT_MODE));
246   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
247
248   button = gtk_button_new_with_label ("Multiple selections");
249   g_signal_connect (button, "clicked",
250                     G_CALLBACK (create_icon_view_window),
251                     GINT_TO_POINTER (EDIT_MODE | MULTI_SELECT));
252   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
253
254
255   button = gtk_button_new_with_label ("Close");
256   g_signal_connect (button, "clicked",
257                     G_CALLBACK (gtk_main_quit), NULL);
258   gtk_box_pack_end (GTK_BOX (mainbox), button, FALSE, FALSE, 0);
259
260   gtk_box_pack_end (GTK_BOX (mainbox), gtk_hseparator_new (),
261                     FALSE, FALSE, 6);
262
263   gtk_widget_show_all (window);
264
265   gtk_main ();
266
267   return 0;
268 }