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);
39 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
43 gtk_list_store_prepend (store, &iter);
45 gtk_list_store_set (store, &iter,
47 1, "Really really\nreally really loooooooooong item name",
49 3, "This is a <b>Test</b> of <i>markup</i>",
53 while (i < NUMBER_OF_ITEMS - 1)
56 size = g_random_int_range (20, 70);
57 pb = gdk_pixbuf_scale_simple (pixbuf, size, size, GDK_INTERP_NEAREST);
59 str = g_strdup_printf ("Icon %d", i);
60 str2 = g_strdup_printf ("Icon <b>%d</b>", i);
61 gtk_list_store_prepend (store, &iter);
62 gtk_list_store_set (store, &iter,
74 // gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 2, GTK_SORT_ASCENDING);
82 store = gtk_list_store_new (5, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN);
84 return GTK_TREE_MODEL (store);
89 foreach_selected_remove (GtkWidget *button, GtkIconView *icon_list)
94 GList *list, *selected;
96 selected = gtk_icon_view_get_selected_items (icon_list);
97 model = gtk_icon_view_get_model (icon_list);
99 for (list = selected; list; list = list->next)
101 GtkTreePath *path = list->data;
103 gtk_tree_model_get_iter (model, &iter, path);
104 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
106 gtk_tree_path_free (path);
109 g_list_free (selected);
114 swap_rows (GtkWidget *button, GtkIconView *icon_list)
116 GtkTreeIter iter, iter2;
119 model = gtk_icon_view_get_model (icon_list);
120 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), -2, GTK_SORT_ASCENDING);
122 gtk_tree_model_get_iter_first (model, &iter);
124 gtk_tree_model_iter_next (model, &iter2);
125 gtk_list_store_swap (GTK_LIST_STORE (model), &iter, &iter2);
129 add_n_items (GtkIconView *icon_list, gint n)
131 static gint count = NUMBER_OF_ITEMS;
139 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
140 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
143 for (i = 0; i < n; i++)
145 str = g_strdup_printf ("Icon %d", count);
146 str2 = g_strdup_printf ("Icon <b>%d</b>", count);
147 gtk_list_store_prepend (store, &iter);
148 gtk_list_store_set (store, &iter,
161 add_some (GtkWidget *button, GtkIconView *icon_list)
163 add_n_items (icon_list, SOME_ITEMS);
167 add_many (GtkWidget *button, GtkIconView *icon_list)
169 add_n_items (icon_list, MANY_ITEMS);
173 add_large (GtkWidget *button, GtkIconView *icon_list)
178 GdkPixbuf *pixbuf, *pb;
181 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
182 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
184 pb = gdk_pixbuf_scale_simple (pixbuf,
185 2 * gdk_pixbuf_get_width (pixbuf),
186 2 * gdk_pixbuf_get_height (pixbuf),
187 GDK_INTERP_BILINEAR);
189 str = g_strdup_printf ("Some really long text");
190 gtk_list_store_append (store, &iter);
191 gtk_list_store_set (store, &iter,
200 pb = gdk_pixbuf_scale_simple (pixbuf,
201 3 * gdk_pixbuf_get_width (pixbuf),
202 3 * gdk_pixbuf_get_height (pixbuf),
203 GDK_INTERP_BILINEAR);
205 str = g_strdup ("see how long text behaves when placed underneath "
206 "an oversized icon which would allow for long lines");
207 gtk_list_store_append (store, &iter);
208 gtk_list_store_set (store, &iter,
217 pb = gdk_pixbuf_scale_simple (pixbuf,
218 3 * gdk_pixbuf_get_width (pixbuf),
219 3 * gdk_pixbuf_get_height (pixbuf),
220 GDK_INTERP_BILINEAR);
222 str = g_strdup ("short text");
223 gtk_list_store_append (store, &iter);
224 gtk_list_store_set (store, &iter,
233 g_object_unref (pixbuf);
237 select_all (GtkWidget *button, GtkIconView *icon_list)
239 gtk_icon_view_select_all (icon_list);
243 select_nonexisting (GtkWidget *button, GtkIconView *icon_list)
245 GtkTreePath *path = gtk_tree_path_new_from_indices (999999, -1);
246 gtk_icon_view_select_path (icon_list, path);
247 gtk_tree_path_free (path);
251 unselect_all (GtkWidget *button, GtkIconView *icon_list)
253 gtk_icon_view_unselect_all (icon_list);
257 selection_changed (GtkIconView *icon_list)
259 g_print ("Selection changed!\n");
263 GtkIconView *icon_list;
268 free_item_data (ItemData *data)
270 gtk_tree_path_free (data->path);
275 item_activated (GtkIconView *icon_view,
282 model = gtk_icon_view_get_model (icon_view);
283 gtk_tree_model_get_iter (model, &iter, path);
285 gtk_tree_model_get (model, &iter, 1, &text, -1);
286 g_print ("Item activated, text is %s\n", text);
292 toggled (GtkCellRendererToggle *cell,
296 GtkTreeModel *model = GTK_TREE_MODEL (data);
298 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
301 gtk_tree_model_get_iter (model, &iter, path);
302 gtk_tree_model_get (model, &iter, 4, &value, -1);
305 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 4, value, -1);
307 gtk_tree_path_free (path);
311 edited (GtkCellRendererText *cell,
316 GtkTreeModel *model = GTK_TREE_MODEL (data);
318 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
320 gtk_tree_model_get_iter (model, &iter, path);
321 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 1, new_text, -1);
323 gtk_tree_path_free (path);
327 item_cb (GtkWidget *menuitem,
330 item_activated (data->icon_list, data->path);
334 do_popup_menu (GtkWidget *icon_list,
335 GdkEventButton *event)
337 GtkIconView *icon_view = GTK_ICON_VIEW (icon_list);
340 GtkTreePath *path = NULL;
341 int button, event_time;
346 path = gtk_icon_view_get_path_at_pos (icon_view, event->x, event->y);
349 list = gtk_icon_view_get_selected_items (icon_view);
353 path = (GtkTreePath*)list->data;
354 g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
361 menu = gtk_menu_new ();
363 data = g_new0 (ItemData, 1);
364 data->icon_list = icon_view;
366 g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
368 menuitem = gtk_menu_item_new_with_label ("Activate");
369 gtk_widget_show (menuitem);
370 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
371 g_signal_connect (menuitem, "activate", G_CALLBACK (item_cb), data);
375 button = event->button;
376 event_time = event->time;
381 event_time = gtk_get_current_event_time ();
384 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
390 button_press_event_handler (GtkWidget *widget,
391 GdkEventButton *event)
393 /* Ignore double-clicks and triple-clicks */
394 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
396 do_popup_menu (widget, event);
404 popup_menu_handler (GtkWidget *widget)
406 do_popup_menu (widget, NULL);
410 static const GtkTargetEntry item_targets[] = {
411 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, 0 }
415 main (gint argc, gchar **argv)
417 GtkWidget *paned, *tv;
418 GtkWidget *window, *icon_list, *scrolled_window;
419 GtkWidget *vbox, *bbox;
421 GtkWidget *prop_editor;
423 GtkCellRenderer *cell;
424 GtkTreeViewColumn *tvc;
426 gtk_init (&argc, &argv);
428 /* to test rtl layout, set RTL=1 in the environment */
429 if (g_getenv ("RTL"))
430 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
432 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
433 gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
435 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
436 gtk_container_add (GTK_CONTAINER (window), vbox);
438 paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
439 gtk_box_pack_start (GTK_BOX (vbox), paned, TRUE, TRUE, 0);
441 icon_list = gtk_icon_view_new ();
442 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
444 tv = gtk_tree_view_new ();
445 tvc = gtk_tree_view_column_new ();
446 gtk_tree_view_append_column (GTK_TREE_VIEW (tv), tvc);
448 g_signal_connect_after (icon_list, "button_press_event",
449 G_CALLBACK (button_press_event_handler), NULL);
450 g_signal_connect (icon_list, "selection_changed",
451 G_CALLBACK (selection_changed), NULL);
452 g_signal_connect (icon_list, "popup_menu",
453 G_CALLBACK (popup_menu_handler), NULL);
455 g_signal_connect (icon_list, "item_activated",
456 G_CALLBACK (item_activated), NULL);
458 model = create_model ();
459 gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), model);
460 gtk_tree_view_set_model (GTK_TREE_VIEW (tv), model);
465 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_list), 0);
466 gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_list), 1);
470 cell = gtk_cell_renderer_toggle_new ();
471 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
472 g_object_set (cell, "activatable", TRUE, NULL);
473 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
474 cell, "active", 4, NULL);
475 g_signal_connect (cell, "toggled", G_CALLBACK (toggled), model);
477 cell = gtk_cell_renderer_pixbuf_new ();
478 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
480 "follow-state", TRUE,
482 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
483 cell, "pixbuf", 0, NULL);
485 cell = gtk_cell_renderer_text_new ();
486 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
490 "wrap-mode", PANGO_WRAP_WORD_CHAR,
493 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
494 cell, "text", 1, NULL);
495 g_signal_connect (cell, "edited", G_CALLBACK (edited), model);
497 /* now the tree view... */
498 cell = gtk_cell_renderer_toggle_new ();
499 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
500 g_object_set (cell, "activatable", TRUE, NULL);
501 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
502 cell, "active", 4, NULL);
503 g_signal_connect (cell, "toggled", G_CALLBACK (toggled), model);
505 cell = gtk_cell_renderer_pixbuf_new ();
506 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
508 "follow-state", TRUE,
510 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
511 cell, "pixbuf", 0, NULL);
513 cell = gtk_cell_renderer_text_new ();
514 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
515 g_object_set (cell, "editable", TRUE, NULL);
516 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
517 cell, "text", 1, NULL);
518 g_signal_connect (cell, "edited", G_CALLBACK (edited), model);
520 /* Allow DND between the icon view and the tree view */
522 gtk_icon_view_enable_model_drag_source (GTK_ICON_VIEW (icon_list),
525 G_N_ELEMENTS (item_targets),
527 gtk_icon_view_enable_model_drag_dest (GTK_ICON_VIEW (icon_list),
529 G_N_ELEMENTS (item_targets),
532 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (tv),
535 G_N_ELEMENTS (item_targets),
537 gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW (tv),
539 G_N_ELEMENTS (item_targets),
543 prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
544 gtk_widget_show_all (prop_editor);
546 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
547 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
548 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
549 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
551 gtk_paned_add1 (GTK_PANED (paned), scrolled_window);
553 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
554 gtk_container_add (GTK_CONTAINER (scrolled_window), tv);
555 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
556 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
558 gtk_paned_add2 (GTK_PANED (paned), scrolled_window);
560 bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
561 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
562 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
564 button = gtk_button_new_with_label ("Add some");
565 g_signal_connect (button, "clicked", G_CALLBACK (add_some), icon_list);
566 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
568 button = gtk_button_new_with_label ("Add many");
569 g_signal_connect (button, "clicked", G_CALLBACK (add_many), icon_list);
570 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
572 button = gtk_button_new_with_label ("Add large");
573 g_signal_connect (button, "clicked", G_CALLBACK (add_large), icon_list);
574 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
576 button = gtk_button_new_with_label ("Remove selected");
577 g_signal_connect (button, "clicked", G_CALLBACK (foreach_selected_remove), icon_list);
578 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
580 button = gtk_button_new_with_label ("Swap");
581 g_signal_connect (button, "clicked", G_CALLBACK (swap_rows), icon_list);
582 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
584 bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
585 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
586 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
588 button = gtk_button_new_with_label ("Select all");
589 g_signal_connect (button, "clicked", G_CALLBACK (select_all), icon_list);
590 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
592 button = gtk_button_new_with_label ("Unselect all");
593 g_signal_connect (button, "clicked", G_CALLBACK (unselect_all), icon_list);
594 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
596 button = gtk_button_new_with_label ("Select nonexisting");
597 g_signal_connect (button, "clicked", G_CALLBACK (select_nonexisting), icon_list);
598 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
600 icon_list = gtk_icon_view_new ();
602 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
603 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
604 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
605 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
606 gtk_paned_add2 (GTK_PANED (paned), scrolled_window);
608 gtk_widget_show_all (window);