]> Pileus Git - ~andy/gtk/blob - tests/testiconview.c
Test oversized items.
[~andy/gtk] / tests / testiconview.c
1 /* testiconview.c
2  * Copyright (C) 2002  Anders Carlsson <andersca@gnu.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21 #include <sys/types.h>
22 #include <string.h>
23 #include "prop-editor.h"
24
25 #define NUMBER_OF_ITEMS   10
26 #define SOME_ITEMS       100
27 #define MANY_ITEMS     10000
28
29 static void
30 fill_model (GtkTreeModel *model)
31 {
32   GdkPixbuf *pixbuf;
33   int i;
34   char *str, *str2;
35   GtkTreeIter iter;
36   GtkListStore *store = GTK_LIST_STORE (model);
37   
38   pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
39
40   i = 0;
41   
42   gtk_list_store_prepend (store, &iter);
43
44   gtk_list_store_set (store, &iter,
45                       0, pixbuf,
46                       1, "Really really\nreally really loooooooooong item name",
47                       2, 0,
48                       3, "This is a <b>Test</b> of <i>markup</i>",
49                       -1);
50   
51   while (i < NUMBER_OF_ITEMS - 1)
52     {
53       str = g_strdup_printf ("Icon %d", i);
54       str2 = g_strdup_printf ("Icon <b>%d</b>", i);     
55       gtk_list_store_prepend (store, &iter);
56       gtk_list_store_set (store, &iter,
57                           0, pixbuf,
58                           1, str,
59                           2, i,
60                           3, str2,
61                           -1);
62       g_free (str);
63       g_free (str2);
64       i++;
65     }
66
67   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 2, GTK_SORT_ASCENDING);
68 }
69
70 static GtkTreeModel *
71 create_model (void)
72 {
73   GtkListStore *store;
74   
75   store = gtk_list_store_new (4, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
76
77   return GTK_TREE_MODEL (store);
78 }
79
80
81 static void
82 foreach_selected_remove (GtkWidget *button, GtkIconView *icon_list)
83 {
84   GtkTreeIter iter;
85   GtkTreeModel *model;
86
87   GList *list, *selected;
88
89   selected = gtk_icon_view_get_selected_items (icon_list);
90   model = gtk_icon_view_get_model (icon_list);
91   
92   for (list = selected; list; list = list->next)
93     {
94       GtkTreePath *path = list->data;
95
96       gtk_tree_model_get_iter (model, &iter, path);
97       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
98       
99       gtk_tree_path_free (path);
100     } 
101   
102   g_list_free (selected);
103 }
104
105
106 static void
107 add_n_items (GtkIconView *icon_list, gint n)
108 {
109   static gint count = NUMBER_OF_ITEMS;
110
111   GtkTreeIter iter;
112   GtkListStore *store;
113   GdkPixbuf *pixbuf;
114   gchar *str, *str2;
115   gint i;
116
117   store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
118   pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
119
120
121   for (i = 0; i < n; i++)
122     {
123       str = g_strdup_printf ("Icon %d", count);
124       str2 = g_strdup_printf ("Icon <b>%d</b>", count); 
125       gtk_list_store_prepend (store, &iter);
126       gtk_list_store_set (store, &iter,
127                           0, pixbuf,
128                           1, str,
129                           2, i,
130                           3, str2,
131                           -1);
132       g_free (str);
133       g_free (str2);
134       count++;
135     }
136 }
137
138 static void
139 add_some (GtkWidget *button, GtkIconView *icon_list)
140 {
141   add_n_items (icon_list, SOME_ITEMS);
142 }
143
144 static void
145 add_many (GtkWidget *button, GtkIconView *icon_list)
146 {
147   add_n_items (icon_list, MANY_ITEMS);
148 }
149
150 static void
151 add_large (GtkWidget *button, GtkIconView *icon_list)
152 {
153   GtkListStore *store;
154   GtkTreeIter iter;
155
156   GdkPixbuf *pixbuf, *pb;
157   gchar *str;
158
159   store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
160   pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
161
162   pb = gdk_pixbuf_scale_simple (pixbuf, 
163                                 2 * gdk_pixbuf_get_width (pixbuf),
164                                 2 * gdk_pixbuf_get_height (pixbuf),
165                                 GDK_INTERP_BILINEAR);
166
167   str = g_strdup_printf ("Some really long text");
168   gtk_list_store_append (store, &iter);
169   gtk_list_store_set (store, &iter,
170                       0, pb,
171                       1, str,
172                       2, 0,
173                       3, str,
174                       -1);
175   g_object_unref (pb);
176   g_free (str);
177   
178   pb = gdk_pixbuf_scale_simple (pixbuf, 
179                                 3 * gdk_pixbuf_get_width (pixbuf),
180                                 3 * gdk_pixbuf_get_height (pixbuf),
181                                 GDK_INTERP_BILINEAR);
182
183   str = g_strdup ("see how long text behaves when placed underneath "
184                   "an oversized icon which would allow for long lines");
185   gtk_list_store_append (store, &iter);
186   gtk_list_store_set (store, &iter,
187                       0, pb,
188                       1, str,
189                       2, 1,
190                       3, str,
191                       -1);
192   g_object_unref (pb);
193   g_free (str);
194
195   pb = gdk_pixbuf_scale_simple (pixbuf, 
196                                 3 * gdk_pixbuf_get_width (pixbuf),
197                                 3 * gdk_pixbuf_get_height (pixbuf),
198                                 GDK_INTERP_BILINEAR);
199
200   str = g_strdup ("short text");
201   gtk_list_store_append (store, &iter);
202   gtk_list_store_set (store, &iter,
203                       0, pb,
204                       1, str,
205                       2, 2,
206                       3, str,
207                       -1);
208   g_object_unref (pb);
209   g_free (str);
210
211   g_object_unref (pixbuf);
212 }
213
214 static void
215 select_all (GtkWidget *button, GtkIconView *icon_list)
216 {
217   gtk_icon_view_select_all (icon_list);
218 }
219
220 static void
221 unselect_all (GtkWidget *button, GtkIconView *icon_list)
222 {
223   gtk_icon_view_unselect_all (icon_list);
224 }
225
226 static void
227 selection_changed (GtkIconView *icon_list)
228 {
229   g_print ("Selection changed!\n");
230 }
231
232 typedef struct {
233   GtkIconView     *icon_list;
234   GtkTreePath     *path;
235 } ItemData;
236
237 static void
238 free_item_data (ItemData *data)
239 {
240   gtk_tree_path_free (data->path);
241   g_free (data);
242 }
243
244 static void
245 item_activated (GtkIconView *icon_view,
246                 GtkTreePath *path)
247 {
248   GtkTreeIter iter;
249   GtkTreeModel *model;
250   gchar *text;
251
252   model = gtk_icon_view_get_model (icon_view);
253   gtk_tree_model_get_iter (model, &iter, path);
254
255   gtk_tree_model_get (model, &iter, 1, &text, -1);
256   g_print ("Item activated, text is %s\n", text);
257   g_free (text);
258   
259 }
260
261 static void
262 item_cb (GtkWidget *menuitem,
263          ItemData  *data)
264 {
265   item_activated (data->icon_list, data->path);
266 }
267
268 static void
269 do_popup_menu (GtkWidget      *icon_list, 
270                GdkEventButton *event)
271 {
272   GtkWidget *menu;
273   GtkWidget *menuitem;
274   GtkTreePath *path;
275   int button, event_time;
276   ItemData *data;
277
278   path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (icon_list), 
279                                         event->x, event->y);
280   g_print ("foo: %p\n", path);
281   
282   if (!path)
283     return;
284
285   menu = gtk_menu_new ();
286
287   data = g_new0 (ItemData, 1);
288   data->icon_list = GTK_ICON_VIEW (icon_list);
289   data->path = path;
290   g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
291
292   menuitem = gtk_menu_item_new_with_label ("Activate");
293   gtk_widget_show (menuitem);
294   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
295   g_signal_connect (menuitem, "activate", G_CALLBACK (item_cb), data);
296
297   if (event)
298     {
299       button = event->button;
300       event_time = event->time;
301     }
302   else
303     {
304       button = 0;
305       event_time = gtk_get_current_event_time ();
306     }
307
308   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
309                   button, event_time);
310 }
311         
312
313 static gboolean
314 button_press_event_handler (GtkWidget      *widget, 
315                             GdkEventButton *event)
316 {
317   /* Ignore double-clicks and triple-clicks */
318   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
319     {
320       do_popup_menu (widget, event);
321       return TRUE;
322     }
323
324   return FALSE;
325 }
326
327 static gboolean
328 popup_menu_handler (GtkWidget *widget)
329 {
330   do_popup_menu (widget, NULL);
331   return TRUE;
332 }
333         
334 gint
335 main (gint argc, gchar **argv)
336 {
337   GtkWidget *paned;
338   GtkWidget *window, *icon_list, *scrolled_window;
339   GtkWidget *vbox, *bbox;
340   GtkWidget *button;
341   GtkWidget *prop_editor;
342   GtkTreeModel *model;
343   
344   gtk_init (&argc, &argv);
345
346   /* to test rtl layout, set RTL=1 in the environment */
347   if (g_getenv ("RTL"))
348     gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
349
350   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
351   gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
352
353   vbox = gtk_vbox_new (FALSE, 0);
354   gtk_container_add (GTK_CONTAINER (window), vbox);
355
356   paned = gtk_hpaned_new ();
357   gtk_box_pack_start (GTK_BOX (vbox), paned, TRUE, TRUE, 0);
358
359   icon_list = gtk_icon_view_new ();
360   gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
361
362   g_signal_connect_after (icon_list, "button_press_event",
363                           G_CALLBACK (button_press_event_handler), NULL);
364   g_signal_connect (icon_list, "selection_changed",
365                     G_CALLBACK (selection_changed), NULL);
366   g_signal_connect (icon_list, "popup_menu",
367                     G_CALLBACK (popup_menu_handler), NULL);
368
369   g_signal_connect (icon_list, "item_activated",
370                     G_CALLBACK (item_activated), NULL);
371   
372   model = create_model ();
373   gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), model);
374   fill_model (model);
375   gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_list), 0);
376   gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_list), 1);
377   
378   prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
379   gtk_widget_show_all (prop_editor);
380   
381   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
382   gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
383   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
384                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
385
386   gtk_paned_add1 (GTK_PANED (paned), scrolled_window);
387   bbox = gtk_hbutton_box_new ();
388   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
389   gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
390
391   button = gtk_button_new_with_label ("Add some");
392   g_signal_connect (button, "clicked", G_CALLBACK (add_some), icon_list);
393   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
394
395   button = gtk_button_new_with_label ("Add many");
396   g_signal_connect (button, "clicked", G_CALLBACK (add_many), icon_list);
397   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
398
399   button = gtk_button_new_with_label ("Add large");
400   g_signal_connect (button, "clicked", G_CALLBACK (add_large), icon_list);
401   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
402
403   button = gtk_button_new_with_label ("Remove selected");
404   g_signal_connect (button, "clicked", G_CALLBACK (foreach_selected_remove), icon_list);
405   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
406
407   bbox = gtk_hbutton_box_new ();
408   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
409   gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
410
411   button = gtk_button_new_with_label ("Select all");
412   g_signal_connect (button, "clicked", G_CALLBACK (select_all), icon_list);
413   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
414
415   button = gtk_button_new_with_label ("Unselect all");
416   g_signal_connect (button, "clicked", G_CALLBACK (unselect_all), icon_list);
417   gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
418   
419   gtk_paned_pack1 (GTK_PANED (paned), vbox, TRUE, FALSE);
420
421   icon_list = gtk_icon_view_new ();
422   
423   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
424   gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
425   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
426                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
427   gtk_paned_pack2 (GTK_PANED (paned), scrolled_window, TRUE, FALSE);
428
429   gtk_widget_show_all (window);
430
431   gtk_main ();
432
433   return 0;
434 }