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_foreach (list->next, (GFunc) gtk_tree_path_free, NULL);
362 menu = gtk_menu_new ();
364 data = g_new0 (ItemData, 1);
365 data->icon_list = icon_view;
367 g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
369 menuitem = gtk_menu_item_new_with_label ("Activate");
370 gtk_widget_show (menuitem);
371 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
372 g_signal_connect (menuitem, "activate", G_CALLBACK (item_cb), data);
376 button = event->button;
377 event_time = event->time;
382 event_time = gtk_get_current_event_time ();
385 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
391 button_press_event_handler (GtkWidget *widget,
392 GdkEventButton *event)
394 /* Ignore double-clicks and triple-clicks */
395 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
397 do_popup_menu (widget, event);
405 popup_menu_handler (GtkWidget *widget)
407 do_popup_menu (widget, NULL);
411 static const GtkTargetEntry item_targets[] = {
412 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, 0 }
416 main (gint argc, gchar **argv)
418 GtkWidget *paned, *tv;
419 GtkWidget *window, *icon_list, *scrolled_window;
420 GtkWidget *vbox, *bbox;
422 GtkWidget *prop_editor;
424 GtkCellRenderer *cell;
425 GtkTreeViewColumn *tvc;
427 gtk_init (&argc, &argv);
429 /* to test rtl layout, set RTL=1 in the environment */
430 if (g_getenv ("RTL"))
431 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
433 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
434 gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
436 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
437 gtk_container_add (GTK_CONTAINER (window), vbox);
439 paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
440 gtk_box_pack_start (GTK_BOX (vbox), paned, TRUE, TRUE, 0);
442 icon_list = gtk_icon_view_new ();
443 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
445 tv = gtk_tree_view_new ();
446 tvc = gtk_tree_view_column_new ();
447 gtk_tree_view_append_column (GTK_TREE_VIEW (tv), tvc);
449 g_signal_connect_after (icon_list, "button_press_event",
450 G_CALLBACK (button_press_event_handler), NULL);
451 g_signal_connect (icon_list, "selection_changed",
452 G_CALLBACK (selection_changed), NULL);
453 g_signal_connect (icon_list, "popup_menu",
454 G_CALLBACK (popup_menu_handler), NULL);
456 g_signal_connect (icon_list, "item_activated",
457 G_CALLBACK (item_activated), NULL);
459 model = create_model ();
460 gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), model);
461 gtk_tree_view_set_model (GTK_TREE_VIEW (tv), model);
466 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_list), 0);
467 gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_list), 1);
471 cell = gtk_cell_renderer_toggle_new ();
472 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
473 g_object_set (cell, "activatable", TRUE, NULL);
474 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
475 cell, "active", 4, NULL);
476 g_signal_connect (cell, "toggled", G_CALLBACK (toggled), model);
478 cell = gtk_cell_renderer_pixbuf_new ();
479 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
481 "follow-state", TRUE,
483 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
484 cell, "pixbuf", 0, NULL);
486 cell = gtk_cell_renderer_text_new ();
487 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_list), cell, FALSE);
491 "wrap-mode", PANGO_WRAP_WORD_CHAR,
494 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_list),
495 cell, "text", 1, NULL);
496 g_signal_connect (cell, "edited", G_CALLBACK (edited), model);
498 /* now the tree view... */
499 cell = gtk_cell_renderer_toggle_new ();
500 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
501 g_object_set (cell, "activatable", TRUE, NULL);
502 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
503 cell, "active", 4, NULL);
504 g_signal_connect (cell, "toggled", G_CALLBACK (toggled), model);
506 cell = gtk_cell_renderer_pixbuf_new ();
507 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
509 "follow-state", TRUE,
511 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
512 cell, "pixbuf", 0, NULL);
514 cell = gtk_cell_renderer_text_new ();
515 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tvc), cell, FALSE);
516 g_object_set (cell, "editable", TRUE, NULL);
517 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tvc),
518 cell, "text", 1, NULL);
519 g_signal_connect (cell, "edited", G_CALLBACK (edited), model);
521 /* Allow DND between the icon view and the tree view */
523 gtk_icon_view_enable_model_drag_source (GTK_ICON_VIEW (icon_list),
526 G_N_ELEMENTS (item_targets),
528 gtk_icon_view_enable_model_drag_dest (GTK_ICON_VIEW (icon_list),
530 G_N_ELEMENTS (item_targets),
533 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (tv),
536 G_N_ELEMENTS (item_targets),
538 gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW (tv),
540 G_N_ELEMENTS (item_targets),
544 prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
545 gtk_widget_show_all (prop_editor);
547 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
548 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
549 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
550 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
552 gtk_paned_add1 (GTK_PANED (paned), scrolled_window);
554 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
555 gtk_container_add (GTK_CONTAINER (scrolled_window), tv);
556 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
557 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
559 gtk_paned_add2 (GTK_PANED (paned), scrolled_window);
561 bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
562 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
563 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
565 button = gtk_button_new_with_label ("Add some");
566 g_signal_connect (button, "clicked", G_CALLBACK (add_some), icon_list);
567 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
569 button = gtk_button_new_with_label ("Add many");
570 g_signal_connect (button, "clicked", G_CALLBACK (add_many), icon_list);
571 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
573 button = gtk_button_new_with_label ("Add large");
574 g_signal_connect (button, "clicked", G_CALLBACK (add_large), icon_list);
575 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
577 button = gtk_button_new_with_label ("Remove selected");
578 g_signal_connect (button, "clicked", G_CALLBACK (foreach_selected_remove), icon_list);
579 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
581 button = gtk_button_new_with_label ("Swap");
582 g_signal_connect (button, "clicked", G_CALLBACK (swap_rows), icon_list);
583 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
585 bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
586 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
587 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
589 button = gtk_button_new_with_label ("Select all");
590 g_signal_connect (button, "clicked", G_CALLBACK (select_all), icon_list);
591 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
593 button = gtk_button_new_with_label ("Unselect all");
594 g_signal_connect (button, "clicked", G_CALLBACK (unselect_all), icon_list);
595 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
597 button = gtk_button_new_with_label ("Select nonexisting");
598 g_signal_connect (button, "clicked", G_CALLBACK (select_nonexisting), icon_list);
599 gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
601 icon_list = gtk_icon_view_new ();
603 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
604 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
605 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
606 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
607 gtk_paned_add2 (GTK_PANED (paned), scrolled_window);
609 gtk_widget_show_all (window);