Release 2.1.96-6
[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/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   hildon_gtk_init (&argc, &argv);
167
168   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
169   g_signal_connect (window, "delete-event",
170                     G_CALLBACK (gtk_main_quit), NULL);
171   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
172
173   mainbox = gtk_vbox_new (FALSE, 6);
174   gtk_container_add (GTK_CONTAINER (window), mainbox);
175
176   /* old-style */
177   label = gtk_label_new (NULL);
178   gtk_label_set_markup (GTK_LABEL (label), "<b>Old-style behavior</b>");
179   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
180   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
181
182   padbox = gtk_hbox_new (FALSE, 6);
183   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
184
185   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
186                       FALSE, FALSE, 6);
187
188   vbox = gtk_vbox_new (FALSE, 6);
189   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
190
191   button = gtk_button_new_with_label ("Single selection");
192   g_signal_connect (button, "clicked",
193                     G_CALLBACK (create_icon_view_window), 0x0);
194   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
195
196   button = gtk_button_new_with_label ("Multiple selections");
197   g_signal_connect (button, "clicked",
198                     G_CALLBACK (create_icon_view_window),
199                     GINT_TO_POINTER (MULTI_SELECT));
200   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
201
202   /* normal-mode */
203   label = gtk_label_new (NULL);
204   gtk_label_set_markup (GTK_LABEL (label), "<b>Fremantle Normal mode</b>");
205   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
206   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
207
208   padbox = gtk_hbox_new (FALSE, 6);
209   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
210
211   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
212                       FALSE, FALSE, 6);
213
214   vbox = gtk_vbox_new (FALSE, 6);
215   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
216
217   button = gtk_button_new_with_label ("Direct activation");
218   g_signal_connect (button, "clicked",
219                     G_CALLBACK (create_icon_view_window),
220                     GINT_TO_POINTER (NORMAL_MODE));
221   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
222
223   /* edit-mode */
224   label = gtk_label_new (NULL);
225   gtk_label_set_markup (GTK_LABEL (label), "<b>Fremantle Edit mode</b>");
226   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
227   gtk_box_pack_start (GTK_BOX (mainbox), label, FALSE, FALSE, 0);
228
229   padbox = gtk_hbox_new (FALSE, 6);
230   gtk_box_pack_start (GTK_BOX (mainbox), padbox, FALSE, FALSE, 6);
231
232   gtk_box_pack_start (GTK_BOX (padbox), gtk_label_new ("   "),
233                       FALSE, FALSE, 6);
234
235   vbox = gtk_vbox_new (FALSE, 6);
236   gtk_box_pack_start (GTK_BOX (padbox), vbox, TRUE, TRUE, 6);
237
238   button = gtk_button_new_with_label ("Single selection");
239   g_signal_connect (button, "clicked",
240                     G_CALLBACK (create_icon_view_window),
241                     GINT_TO_POINTER (EDIT_MODE));
242   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
243
244   button = gtk_button_new_with_label ("Multiple selections");
245   g_signal_connect (button, "clicked",
246                     G_CALLBACK (create_icon_view_window),
247                     GINT_TO_POINTER (EDIT_MODE | MULTI_SELECT));
248   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
249
250
251   button = gtk_button_new_with_label ("Close");
252   g_signal_connect (button, "clicked",
253                     G_CALLBACK (gtk_main_quit), NULL);
254   gtk_box_pack_end (GTK_BOX (mainbox), button, FALSE, FALSE, 0);
255
256   gtk_box_pack_end (GTK_BOX (mainbox), gtk_hseparator_new (),
257                     FALSE, FALSE, 6);
258
259   gtk_widget_show_all (window);
260
261   gtk_main ();
262
263   return 0;
264 }