1 /* Icon View/Icon View Basics
3 * The GtkIconView widget is used to display and manipulate icons.
4 * It uses a GtkTreeModel for data storage, so the list store
5 * example might be helpful.
10 #include "demo-common.h"
12 static GtkWidget *window = NULL;
14 #define FOLDER_NAME "gnome-fs-directory.png"
15 #define FILE_NAME "gnome-fs-regular.png"
27 static GdkPixbuf *file_pixbuf, *folder_pixbuf;
29 GtkToolItem *up_button;
31 /* Loads the images for the demo and returns whether the operation succeeded */
33 load_pixbufs (GError **error)
38 return TRUE; /* already loaded earlier */
40 /* demo_find_file() looks in the current directory first,
41 * so you can run gtk-demo without installing GTK, then looks
42 * in the location where the file is installed.
44 filename = demo_find_file (FILE_NAME, error);
46 return FALSE; /* note that "error" was filled in and returned */
48 file_pixbuf = gdk_pixbuf_new_from_file (filename, error);
52 return FALSE; /* Note that "error" was filled with a GError */
54 filename = demo_find_file (FOLDER_NAME, error);
56 return FALSE; /* note that "error" was filled in and returned */
58 folder_pixbuf = gdk_pixbuf_new_from_file (filename, error);
65 fill_store (GtkListStore *store)
71 /* First clear the store */
72 gtk_list_store_clear (store);
74 /* Now go through the directory and extract all the file
76 dir = g_dir_open (parent, 0, NULL);
80 name = g_dir_read_name (dir);
83 gchar *path, *display_name;
86 /* We ignore hidden files that start with a '.' */
89 path = g_build_filename (parent, name, NULL);
91 is_dir = g_file_test (path, G_FILE_TEST_IS_DIR);
93 display_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
95 gtk_list_store_append (store, &iter);
96 gtk_list_store_set (store, &iter,
98 COL_DISPLAY_NAME, display_name,
99 COL_IS_DIRECTORY, is_dir,
100 COL_PIXBUF, is_dir ? folder_pixbuf : file_pixbuf,
103 g_free (display_name);
106 name = g_dir_read_name (dir);
111 sort_func (GtkTreeModel *model,
116 gboolean is_dir_a, is_dir_b;
117 gchar *name_a, *name_b;
120 /* We need this function because we want to sort
121 * folders before files.
125 gtk_tree_model_get (model, a,
126 COL_IS_DIRECTORY, &is_dir_a,
127 COL_DISPLAY_NAME, &name_a,
130 gtk_tree_model_get (model, b,
131 COL_IS_DIRECTORY, &is_dir_b,
132 COL_DISPLAY_NAME, &name_b,
135 if (!is_dir_a && is_dir_b)
137 else if (is_dir_a && !is_dir_b)
141 ret = g_utf8_collate (name_a, name_b);
150 static GtkListStore *
155 store = gtk_list_store_new (NUM_COLS,
161 /* Set sort column and function */
162 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
165 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
166 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
173 item_activated (GtkIconView *icon_view,
174 GtkTreePath *tree_path,
182 store = GTK_LIST_STORE (user_data);
184 gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
186 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
188 COL_IS_DIRECTORY, &is_dir,
197 /* Replace parent with path and re-fill the model*/
203 /* Sensitize the up button */
204 gtk_widget_set_sensitive (GTK_WIDGET (up_button), TRUE);
208 up_clicked (GtkToolItem *item,
214 store = GTK_LIST_STORE (user_data);
216 dir_name = g_path_get_dirname (parent);
223 /* Maybe de-sensitize the up button */
224 gtk_widget_set_sensitive (GTK_WIDGET (up_button),
225 strcmp (parent, "/") != 0);
229 home_clicked (GtkToolItem *item,
234 store = GTK_LIST_STORE (user_data);
237 parent = g_strdup (g_get_home_dir ());
241 /* Sensitize the up button */
242 gtk_widget_set_sensitive (GTK_WIDGET (up_button),
246 static void close_window(void)
248 gtk_widget_destroy (window);
251 g_object_unref (file_pixbuf);
254 g_object_unref (folder_pixbuf);
255 folder_pixbuf = NULL;
259 do_iconview (GtkWidget *do_widget)
265 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
266 gtk_window_set_default_size (GTK_WINDOW (window), 650, 400);
268 gtk_window_set_screen (GTK_WINDOW (window),
269 gtk_widget_get_screen (do_widget));
270 gtk_window_set_title (GTK_WINDOW (window), "GtkIconView demo");
272 g_signal_connect (window, "destroy",
273 G_CALLBACK (close_window), NULL);
276 if (!load_pixbufs (&error))
280 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
281 GTK_DIALOG_DESTROY_WITH_PARENT,
284 "Failed to load an image: %s",
287 g_error_free (error);
289 g_signal_connect (dialog, "response",
290 G_CALLBACK (gtk_widget_destroy), NULL);
292 gtk_widget_show (dialog);
297 GtkWidget *icon_view;
301 GtkToolItem *home_button;
303 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
304 gtk_container_add (GTK_CONTAINER (window), vbox);
306 tool_bar = gtk_toolbar_new ();
307 gtk_box_pack_start (GTK_BOX (vbox), tool_bar, FALSE, FALSE, 0);
309 up_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_UP);
310 gtk_tool_item_set_is_important (up_button, TRUE);
311 gtk_widget_set_sensitive (GTK_WIDGET (up_button), FALSE);
312 gtk_toolbar_insert (GTK_TOOLBAR (tool_bar), up_button, -1);
314 home_button = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
315 gtk_tool_item_set_is_important (home_button, TRUE);
316 gtk_toolbar_insert (GTK_TOOLBAR (tool_bar), home_button, -1);
319 sw = gtk_scrolled_window_new (NULL, NULL);
320 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
321 GTK_SHADOW_ETCHED_IN);
322 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
323 GTK_POLICY_AUTOMATIC,
324 GTK_POLICY_AUTOMATIC);
326 gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
328 /* Create the store and fill it with the contents of '/' */
329 parent = g_strdup ("/");
330 store = create_store ();
333 icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store));
334 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
335 GTK_SELECTION_MULTIPLE);
336 g_object_unref (store);
338 /* Connect to the "clicked" signal of the "Up" tool button */
339 g_signal_connect (up_button, "clicked",
340 G_CALLBACK (up_clicked), store);
342 /* Connect to the "clicked" signal of the "Home" tool button */
343 g_signal_connect (home_button, "clicked",
344 G_CALLBACK (home_clicked), store);
346 /* We now set which model columns that correspond to the text
347 * and pixbuf of each item
349 gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_view), COL_DISPLAY_NAME);
350 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_view), COL_PIXBUF);
352 /* Connect to the "item-activated" signal */
353 g_signal_connect (icon_view, "item-activated",
354 G_CALLBACK (item_activated), store);
355 gtk_container_add (GTK_CONTAINER (sw), icon_view);
357 gtk_widget_grab_focus (icon_view);
361 if (!gtk_widget_get_visible (window))
362 gtk_widget_show_all (window);
365 gtk_widget_destroy (window);