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 MANY_ITEMS 10000
29 fill_model (GtkTreeModel *model)
35 GtkListStore *store = GTK_LIST_STORE (model);
37 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
41 gtk_list_store_prepend (store, &iter);
43 gtk_list_store_set (store, &iter,
45 1, "Really really\nreally really loooooooooong item name",
47 3, "This is a <b>Test</b> of <i>markup</i>",
50 while (i < NUMBER_OF_ITEMS - 1)
52 str = g_strdup_printf ("Icon %d", i);
53 str2 = g_strdup_printf ("Icon <b>%d</b>", i);
54 gtk_list_store_prepend (store, &iter);
55 gtk_list_store_set (store, &iter,
66 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 2, GTK_SORT_ASCENDING);
74 store = gtk_list_store_new (4, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
76 return GTK_TREE_MODEL (store);
81 foreach_selected_remove (GtkWidget *button, GtkIconView *icon_list)
86 GList *list, *selected;
88 selected = gtk_icon_view_get_selected_items (icon_list);
89 model = gtk_icon_view_get_model (icon_list);
91 for (list = selected; list; list = list->next)
93 GtkTreePath *path = list->data;
95 gtk_tree_model_get_iter (model, &iter, path);
96 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
98 gtk_tree_path_free (path);
101 g_list_free (selected);
106 add_many (GtkWidget *button, GtkIconView *icon_list)
108 static gint count = NUMBER_OF_ITEMS;
116 store = GTK_LIST_STORE (gtk_icon_view_get_model (icon_list));
117 pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);
120 for (i = 0; i < MANY_ITEMS; i++)
122 str = g_strdup_printf ("Icon %d", count);
123 str2 = g_strdup_printf ("Icon <b>%d</b>", count);
124 gtk_list_store_prepend (store, &iter);
125 gtk_list_store_set (store, &iter,
138 select_all (GtkWidget *button, GtkIconView *icon_list)
140 gtk_icon_view_select_all (icon_list);
144 unselect_all (GtkWidget *button, GtkIconView *icon_list)
146 gtk_icon_view_unselect_all (icon_list);
150 selection_changed (GtkIconView *icon_list)
152 g_print ("Selection changed!\n");
156 GtkIconView *icon_list;
161 free_item_data (ItemData *data)
163 gtk_tree_path_free (data->path);
168 item_activated (GtkIconView *icon_view,
175 model = gtk_icon_view_get_model (icon_view);
176 gtk_tree_model_get_iter (model, &iter, path);
178 gtk_tree_model_get (model, &iter, 1, &text, -1);
179 g_print ("Item activated, text is %s\n", text);
185 item_cb (GtkWidget *menuitem,
188 item_activated (data->icon_list, data->path);
192 do_popup_menu (GtkWidget *icon_list,
193 GdkEventButton *event)
198 int button, event_time;
201 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (icon_list),
203 g_print ("foo: %p\n", path);
208 menu = gtk_menu_new ();
210 data = g_new0 (ItemData, 1);
211 data->icon_list = GTK_ICON_VIEW (icon_list);
213 g_object_set_data_full (G_OBJECT (menu), "item-path", data, (GDestroyNotify)free_item_data);
215 menuitem = gtk_menu_item_new_with_label ("Activate");
216 gtk_widget_show (menuitem);
217 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
218 g_signal_connect (menuitem, "activate", G_CALLBACK (item_cb), data);
222 button = event->button;
223 event_time = event->time;
228 event_time = gtk_get_current_event_time ();
231 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
237 button_press_event_handler (GtkWidget *widget,
238 GdkEventButton *event)
240 /* Ignore double-clicks and triple-clicks */
241 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
243 do_popup_menu (widget, event);
251 popup_menu_handler (GtkWidget *widget)
253 do_popup_menu (widget, NULL);
258 main (gint argc, gchar **argv)
261 GtkWidget *window, *icon_list, *scrolled_window;
262 GtkWidget *vbox, *bbox;
264 GtkWidget *prop_editor;
267 gtk_init (&argc, &argv);
269 /* to test rtl layout, set RTL=1 in the environment */
270 if (g_getenv ("RTL"))
271 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
273 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
274 gtk_window_set_default_size (GTK_WINDOW (window), 700, 400);
276 paned = gtk_hpaned_new ();
277 gtk_container_add (GTK_CONTAINER (window), paned);
279 icon_list = gtk_icon_view_new ();
280 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_list), GTK_SELECTION_MULTIPLE);
282 g_signal_connect_after (icon_list, "button_press_event",
283 G_CALLBACK (button_press_event_handler), NULL);
284 g_signal_connect (icon_list, "selection_changed",
285 G_CALLBACK (selection_changed), NULL);
286 g_signal_connect (icon_list, "popup_menu",
287 G_CALLBACK (popup_menu_handler), NULL);
289 g_signal_connect (icon_list, "item_activated",
290 G_CALLBACK (item_activated), NULL);
292 model = create_model ();
293 gtk_icon_view_set_model (GTK_ICON_VIEW (icon_list), model);
295 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_list), 0);
296 gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_list), 1);
298 prop_editor = create_prop_editor (G_OBJECT (icon_list), 0);
299 gtk_widget_show_all (prop_editor);
301 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
302 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
303 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
304 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
305 vbox = gtk_vbox_new (FALSE, 0);
306 gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
307 bbox = gtk_hbutton_box_new ();
308 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_START);
309 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
311 button = gtk_button_new_with_label ("Add many");
312 g_signal_connect (button, "clicked", G_CALLBACK (add_many), icon_list);
313 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
315 button = gtk_button_new_with_label ("Remove selected");
316 g_signal_connect (button, "clicked", G_CALLBACK (foreach_selected_remove), icon_list);
317 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
319 button = gtk_button_new_with_label ("Select all");
320 g_signal_connect (button, "clicked", G_CALLBACK (select_all), icon_list);
321 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
323 button = gtk_button_new_with_label ("Unselect all");
324 g_signal_connect (button, "clicked", G_CALLBACK (unselect_all), icon_list);
325 gtk_box_pack_start_defaults (GTK_BOX (bbox), button);
327 gtk_paned_pack1 (GTK_PANED (paned), vbox, TRUE, FALSE);
329 icon_list = gtk_icon_view_new ();
331 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
332 gtk_container_add (GTK_CONTAINER (scrolled_window), icon_list);
333 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
334 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
335 gtk_paned_pack2 (GTK_PANED (paned), scrolled_window, TRUE, FALSE);
337 gtk_widget_show_all (window);