2 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
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.
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.
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.
21 #include <sys/types.h>
23 #include "prop-editor.h"
25 #define NUMBER_OF_ITEMS 10
26 #define SOME_ITEMS 100
27 #define MANY_ITEMS 10000
30 fill_model (GtkTreeModel *model)
36 GtkListStore *store = GTK_LIST_STORE (model);
38 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
42 gtk_list_store_prepend (store, &iter);
44 gtk_list_store_set (store, &iter,
46 1, "Really really\nreally really loooooooooong item name",
48 3, "This is a <b>Test</b> of <i>markup</i>",
51 while (i < NUMBER_OF_ITEMS - 1)
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,
67 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 2, GTK_SORT_ASCENDING);
75 store = gtk_list_store_new (4, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
77 return GTK_TREE_MODEL (store);
82 foreach_selected_remove (GtkWidget *button, GtkIconView *icon_list)
87 GList *list, *selected;
89 selected = gtk_icon_view_get_selected_items (icon_list);
90 model = gtk_icon_view_get_model (icon_list);
92 for (list = selected; list; list = list->next)
94 GtkTreePath *path = list->data;
96 gtk_tree_model_get_iter (model, &iter, path);
97 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
99 gtk_tree_path_free (path);
102 g_list_free (selected);
107 add_n_items (GtkIconView *icon_list, gint n)
109 static gint count = NUMBER_OF_ITEMS;
117 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
118 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
121 for (i = 0; i < n; i++)
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,
139 add_some (GtkWidget *button, GtkIconView *icon_list)
141 add_n_items (icon_list, SOME_ITEMS);
145 add_many (GtkWidget *button, GtkIconView *icon_list)
147 add_n_items (icon_list, MANY_ITEMS);
151 add_large (GtkWidget *button, GtkIconView *icon_list)
156 GdkPixbuf *pixbuf, *pb;
159 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
160 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
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);
167 str = g_strdup_printf ("Some really long text");
168 gtk_list_store_append (store, &iter);
169 gtk_list_store_set (store, &iter,
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);
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,
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);
200 str = g_strdup ("short text");
201 gtk_list_store_append (store, &iter);
202 gtk_list_store_set (store, &iter,
211 g_object_unref (pixbuf);
215 select_all (GtkWidget *button, GtkIconView *icon_list)
217 gtk_icon_view_select_all (icon_list);
221 unselect_all (GtkWidget *button, GtkIconView *icon_list)
223 gtk_icon_view_unselect_all (icon_list);
227 selection_changed (GtkIconView *icon_list)
229 g_print ("Selection changed!\n");
233 GtkIconView *icon_list;
238 free_item_data (ItemData *data)
240 gtk_tree_path_free (data->path);
245 item_activated (GtkIconView *icon_view,
252 model = gtk_icon_view_get_model (icon_view);
253 gtk_tree_model_get_iter (model, &iter, path);
255 gtk_tree_model_get (model, &iter, 1, &text, -1);
256 g_print ("Item activated, text is %s\n", text);
262 item_cb (GtkWidget *menuitem,
265 item_activated (data->icon_list, data->path);
269 do_popup_menu (GtkWidget *icon_list,
270 GdkEventButton *event)
275 int button, event_time;
278 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (icon_list),
280 g_print ("foo: %p\n", path);
285 menu = gtk_menu_new ();
287 data = g_new0 (ItemData, 1);
288 data->icon_list = GTK_ICON_VIEW (icon_list);
290 g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
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);
299 button = event->button;
300 event_time = event->time;
305 event_time = gtk_get_current_event_time ();
308 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
314 button_press_event_handler (GtkWidget *widget,
315 GdkEventButton *event)
317 /* Ignore double-clicks and triple-clicks */
318 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
320 do_popup_menu (widget, event);
328 popup_menu_handler (GtkWidget *widget)
330 do_popup_menu (widget, NULL);
335 main (gint argc, gchar **argv)
338 GtkWidget *window, *icon_list, *scrolled_window;
339 GtkWidget *vbox, *bbox;
341 GtkWidget *prop_editor;
344 gtk_init (&argc, &argv);
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);
350 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
351 gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
353 vbox = gtk_vbox_new (FALSE, 0);
354 gtk_container_add (GTK_CONTAINER (window), vbox);
356 paned = gtk_hpaned_new ();
357 gtk_box_pack_start (GTK_BOX (vbox), paned, TRUE, TRUE, 0);
359 icon_list = gtk_icon_view_new ();
360 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
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);
369 g_signal_connect (icon_list, "item_activated",
370 G_CALLBACK (item_activated), NULL);
372 model = create_model ();
373 gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), 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);
378 prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
379 gtk_widget_show_all (prop_editor);
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);
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);
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);
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);
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);
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);
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);
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);
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);
419 gtk_paned_pack1 (GTK_PANED (paned), vbox, TRUE, FALSE);
421 icon_list = gtk_icon_view_new ();
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);
429 gtk_widget_show_all (window);