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>",
52 while (i < NUMBER_OF_ITEMS - 1)
54 str = g_strdup_printf ("Icon %d", i);
55 str2 = g_strdup_printf ("Icon <b>%d</b>", i);
56 gtk_list_store_prepend (store, &iter);
57 gtk_list_store_set (store, &iter,
69 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 2, GTK_SORT_ASCENDING);
77 store = gtk_list_store_new (5, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN);
79 return GTK_TREE_MODEL (store);
84 foreach_selected_remove (GtkWidget *button, GtkIconView *icon_list)
89 GList *list, *selected;
91 selected = gtk_icon_view_get_selected_items (icon_list);
92 model = gtk_icon_view_get_model (icon_list);
94 for (list = selected; list; list = list->next)
96 GtkTreePath *path = list->data;
98 gtk_tree_model_get_iter (model, &iter, path);
99 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
101 gtk_tree_path_free (path);
104 g_list_free (selected);
109 swap_rows (GtkWidget *button, GtkIconView *icon_list)
111 GtkTreeIter iter, iter2;
114 model = gtk_icon_view_get_model (icon_list);
115 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), -2, GTK_SORT_ASCENDING);
117 gtk_tree_model_get_iter_first (model, &iter);
119 gtk_tree_model_iter_next (model, &iter2);
120 gtk_list_store_swap (GTK_LIST_STORE (model), &iter, &iter2);
124 add_n_items (GtkIconView *icon_list, gint n)
126 static gint count = NUMBER_OF_ITEMS;
134 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
135 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
138 for (i = 0; i < n; i++)
140 str = g_strdup_printf ("Icon %d", count);
141 str2 = g_strdup_printf ("Icon <b>%d</b>", count);
142 gtk_list_store_prepend (store, &iter);
143 gtk_list_store_set (store, &iter,
156 add_some (GtkWidget *button, GtkIconView *icon_list)
158 add_n_items (icon_list, SOME_ITEMS);
162 add_many (GtkWidget *button, GtkIconView *icon_list)
164 add_n_items (icon_list, MANY_ITEMS);
168 add_large (GtkWidget *button, GtkIconView *icon_list)
173 GdkPixbuf *pixbuf, *pb;
176 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
177 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
179 pb = gdk_pixbuf_scale_simple (pixbuf,
180 2 * gdk_pixbuf_get_width (pixbuf),
181 2 * gdk_pixbuf_get_height (pixbuf),
182 GDK_INTERP_BILINEAR);
184 str = g_strdup_printf ("Some really long text");
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 ("see how long text behaves when placed underneath "
201 "an oversized icon which would allow for long lines");
202 gtk_list_store_append (store, &iter);
203 gtk_list_store_set (store, &iter,
212 pb = gdk_pixbuf_scale_simple (pixbuf,
213 3 * gdk_pixbuf_get_width (pixbuf),
214 3 * gdk_pixbuf_get_height (pixbuf),
215 GDK_INTERP_BILINEAR);
217 str = g_strdup ("short text");
218 gtk_list_store_append (store, &iter);
219 gtk_list_store_set (store, &iter,
228 g_object_unref (pixbuf);
232 select_all (GtkWidget *button, GtkIconView *icon_list)
234 gtk_icon_view_select_all (icon_list);
238 select_nonexisting (GtkWidget *button, GtkIconView *icon_list)
240 GtkTreePath *path = gtk_tree_path_new_from_indices (999999, -1);
241 gtk_icon_view_select_path (icon_list, path);
242 gtk_tree_path_free (path);
246 unselect_all (GtkWidget *button, GtkIconView *icon_list)
248 gtk_icon_view_unselect_all (icon_list);
252 selection_changed (GtkIconView *icon_list)
254 g_print ("Selection changed!\n");
258 GtkIconView *icon_list;
263 free_item_data (ItemData *data)
265 gtk_tree_path_free (data->path);
270 item_activated (GtkIconView *icon_view,
277 model = gtk_icon_view_get_model (icon_view);
278 gtk_tree_model_get_iter (model, &iter, path);
280 gtk_tree_model_get (model, &iter, 1, &text, -1);
281 g_print ("Item activated, text is %s\n", text);
287 toggled (GtkCellRendererToggle *cell,
291 GtkTreeModel *model = GTK_TREE_MODEL (data);
293 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
296 gtk_tree_model_get_iter (model, &iter, path);
297 gtk_tree_model_get (model, &iter, 4, &value, -1);
300 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 4, value, -1);
302 gtk_tree_path_free (path);
306 edited (GtkCellRendererText *cell,
311 GtkTreeModel *model = GTK_TREE_MODEL (data);
313 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
315 gtk_tree_model_get_iter (model, &iter, path);
316 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 1, new_text, -1);
318 gtk_tree_path_free (path);
322 item_cb (GtkWidget *menuitem,
325 item_activated (data->icon_list, data->path);
329 do_popup_menu (GtkWidget *icon_list,
330 GdkEventButton *event)
335 int button, event_time;
338 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (icon_list),
344 menu = gtk_menu_new ();
346 data = g_new0 (ItemData, 1);
347 data->icon_list = GTK_ICON_VIEW (icon_list);
349 g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
351 menuitem = gtk_menu_item_new_with_label ("Activate");
352 gtk_widget_show (menuitem);
353 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
354 g_signal_connect (menuitem, "activate", G_CALLBACK (item_cb), data);
358 button = event->button;
359 event_time = event->time;
364 event_time = gtk_get_current_event_time ();
367 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
373 button_press_event_handler (GtkWidget *widget,
374 GdkEventButton *event)
376 /* Ignore double-clicks and triple-clicks */
377 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
379 do_popup_menu (widget, event);
387 popup_menu_handler (GtkWidget *widget)
389 do_popup_menu (widget, NULL);
394 main (gint argc, gchar **argv)
397 GtkWidget *window, *icon_list, *scrolled_window;
398 GtkWidget *vbox, *bbox;
400 GtkWidget *prop_editor;
402 GtkCellRenderer *cell;
404 gtk_init (&argc, &argv);
406 /* to test rtl layout, set RTL=1 in the environment */
407 if (g_getenv ("RTL"))
408 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
410 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
411 gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
413 vbox = gtk_vbox_new (FALSE, 0);
414 gtk_container_add (GTK_CONTAINER (window), vbox);
416 paned = gtk_hpaned_new ();
417 gtk_box_pack_start (GTK_BOX (vbox), paned, TRUE, TRUE, 0);
419 icon_list = gtk_icon_view_new ();
420 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
422 g_signal_connect_after (icon_list, "button_press_event",
423 G_CALLBACK (button_press_event_handler), NULL);
424 g_signal_connect (icon_list, "selection_changed",
425 G_CALLBACK (selection_changed), NULL);
426 g_signal_connect (icon_list, "popup_menu",
427 G_CALLBACK (popup_menu_handler), NULL);
429 g_signal_connect (icon_list, "item_activated",
430 G_CALLBACK (item_activated), NULL);
432 model = create_model ();
433 gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), model);
438 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_list), 0);
439 gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_list), 1);
443 cell = gtk_cell_renderer_toggle_new ();
444 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
445 g_object_set (cell, "activatable", TRUE, NULL);
446 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
447 cell, "active", 4, NULL);
448 g_signal_connect (cell, "toggled", G_CALLBACK (toggled), model);
450 cell = gtk_cell_renderer_pixbuf_new ();
451 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
453 "follow-state", TRUE,
455 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
456 cell, "pixbuf", 0, NULL);
458 cell = gtk_cell_renderer_text_new ();
459 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
463 "wrap-mode", PANGO_WRAP_WORD_CHAR,
466 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
467 cell, "text", 1, NULL);
468 g_signal_connect (cell, "edited", G_CALLBACK (edited), model);
471 prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
472 gtk_widget_show_all (prop_editor);
474 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
475 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
476 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
477 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
479 gtk_paned_add1 (GTK_PANED (paned), scrolled_window);
481 bbox = gtk_hbutton_box_new ();
482 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
483 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
485 button = gtk_button_new_with_label ("Add some");
486 g_signal_connect (button, "clicked", G_CALLBACK (add_some), icon_list);
487 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
489 button = gtk_button_new_with_label ("Add many");
490 g_signal_connect (button, "clicked", G_CALLBACK (add_many), icon_list);
491 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
493 button = gtk_button_new_with_label ("Add large");
494 g_signal_connect (button, "clicked", G_CALLBACK (add_large), icon_list);
495 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
497 button = gtk_button_new_with_label ("Remove selected");
498 g_signal_connect (button, "clicked", G_CALLBACK (foreach_selected_remove), icon_list);
499 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
501 button = gtk_button_new_with_label ("Swap");
502 g_signal_connect (button, "clicked", G_CALLBACK (swap_rows), icon_list);
503 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
505 bbox = gtk_hbutton_box_new ();
506 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
507 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
509 button = gtk_button_new_with_label ("Select all");
510 g_signal_connect (button, "clicked", G_CALLBACK (select_all), icon_list);
511 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
513 button = gtk_button_new_with_label ("Unselect all");
514 g_signal_connect (button, "clicked", G_CALLBACK (unselect_all), icon_list);
515 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
517 button = gtk_button_new_with_label ("Select nonexisting");
518 g_signal_connect (button, "clicked", G_CALLBACK (select_nonexisting), icon_list);
519 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
521 icon_list = gtk_icon_view_new ();
523 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
524 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
525 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
526 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
527 gtk_paned_add2 (GTK_PANED (paned), scrolled_window);
529 gtk_widget_show_all (window);