/src/wrappers/gtk/examples/gtk-demo/iconview.e
Specman e | 386 lines | 21 code | 85 blank | 280 comment | 2 complexity | 15f8c1b7852e9cb96aab777af8496195 MD5 | raw file
1indexing 2 description: "." 3 copyright: "[ 4 Copyright (C) 2006 Paolo Redaelli, GTK+ team 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public License 8 as published by the Free Software Foundation; either version 2.1 of 9 the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA 20 ]" 21 22class ICONVIEW 23 24creation make 25 26feature 27-- /* Icon View/Icon View Basics 28-- * 29-- * The GtkIconView widget is used to display and manipulate icons. 30-- * It uses a GtkTreeModel for data storage, so the list store 31-- * example might be helpful. 32-- */ 33 34-- #include <gtk/gtk.h> 35-- #include <string.h> 36-- #include "demo-common.h" 37 38-- static GtkWidget *window = NULL; 39 40-- #define FOLDER_NAME "gnome-fs-directory.png" 41-- #define FILE_NAME "gnome-fs-regular.png" 42 43-- enum 44-- { 45-- COL_PATH, 46-- COL_DISPLAY_NAME, 47-- COL_PIXBUF, 48-- COL_IS_DIRECTORY, 49-- NUM_COLS 50-- }; 51 52 53-- static GdkPixbuf *file_pixbuf, *folder_pixbuf; 54-- gchar *parent; 55-- GtkToolItem *up_button; 56 57-- /* Loads the images for the demo and returns whether the operation succeeded */ 58-- static gboolean 59-- load_pixbufs (GError **error) 60-- { 61-- char *filename; 62 63-- if (file_pixbuf) 64-- return TRUE; /* already loaded earlier */ 65 66-- /* demo_find_file() looks in the the current directory first, 67-- * so you can run gtk-demo without installing GTK, then looks 68-- * in the location where the file is installed. 69-- */ 70-- filename = demo_find_file (FILE_NAME, error); 71-- if (!filename) 72-- return FALSE; /* note that "error" was filled in and returned */ 73 74-- file_pixbuf = gdk_pixbuf_new_from_file (filename, error); 75-- g_free (filename); 76 77-- if (!file_pixbuf) 78-- return FALSE; /* Note that "error" was filled with a GError */ 79 80-- filename = demo_find_file (FOLDER_NAME, error); 81-- if (!filename) 82-- return FALSE; /* note that "error" was filled in and returned */ 83 84-- folder_pixbuf = gdk_pixbuf_new_from_file (filename, error); 85-- g_free (filename); 86 87-- return TRUE; 88-- } 89 90-- static void 91-- fill_store (GtkListStore *store) 92-- { 93-- GDir *dir; 94-- const gchar *name; 95-- GtkTreeIter iter; 96 97-- /* First clear the store */ 98-- gtk_list_store_clear (store); 99 100-- /* Now go through the directory and extract all the file 101-- * information */ 102-- dir = g_dir_open (parent, 0, NULL); 103-- if (!dir) 104-- return; 105 106-- name = g_dir_read_name (dir); 107-- while (name != NULL) 108-- { 109-- gchar *path, *display_name; 110-- gboolean is_dir; 111 112-- /* We ignore hidden files that start with a '.' */ 113-- if (name[0] != '.') 114-- { 115-- path = g_build_filename (parent, name, NULL); 116 117-- is_dir = g_file_test (path, G_FILE_TEST_IS_DIR); 118 119-- display_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL); 120 121-- gtk_list_store_append (store, &iter); 122-- gtk_list_store_set (store, &iter, 123-- COL_PATH, path, 124-- COL_DISPLAY_NAME, display_name, 125-- COL_IS_DIRECTORY, is_dir, 126-- COL_PIXBUF, is_dir ? folder_pixbuf : file_pixbuf, 127-- -1); 128-- g_free (path); 129-- g_free (display_name); 130-- } 131 132-- name = g_dir_read_name (dir); 133-- } 134-- } 135 136-- static gint 137-- sort_func (GtkTreeModel *model, 138-- GtkTreeIter *a, 139-- GtkTreeIter *b, 140-- gpointer user_data) 141-- { 142-- gboolean is_dir_a, is_dir_b; 143-- gchar *name_a, *name_b; 144-- int ret; 145 146-- /* We need this function because we want to sort 147-- * folders before files. 148-- */ 149 150 151-- gtk_tree_model_get (model, a, 152-- COL_IS_DIRECTORY, &is_dir_a, 153-- COL_DISPLAY_NAME, &name_a, 154-- -1); 155 156-- gtk_tree_model_get (model, b, 157-- COL_IS_DIRECTORY, &is_dir_b, 158-- COL_DISPLAY_NAME, &name_b, 159-- -1); 160 161-- if (!is_dir_a && is_dir_b) 162-- ret = 1; 163-- else if (is_dir_a && !is_dir_b) 164-- ret = -1; 165-- else 166-- { 167-- ret = g_utf8_collate (name_a, name_b); 168-- } 169 170-- g_free (name_a); 171-- g_free (name_b); 172 173-- return ret; 174-- } 175 176-- static GtkListStore * 177-- create_store (void) 178-- { 179-- GtkListStore *store; 180 181-- store = gtk_list_store_new (NUM_COLS, 182-- G_TYPE_STRING, 183-- G_TYPE_STRING, 184-- GDK_TYPE_PIXBUF, 185-- G_TYPE_BOOLEAN); 186 187-- /* Set sort column and function */ 188-- gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), 189-- sort_func, 190-- NULL, NULL); 191-- gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 192-- GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, 193-- GTK_SORT_ASCENDING); 194 195-- return store; 196-- } 197 198-- static void 199-- item_activated (GtkIconView *icon_view, 200-- GtkTreePath *tree_path, 201-- gpointer user_data) 202-- { 203-- GtkListStore *store; 204-- gchar *path; 205-- GtkTreeIter iter; 206-- gboolean is_dir; 207 208-- store = GTK_LIST_STORE (user_data); 209 210-- gtk_tree_model_get_iter (GTK_TREE_MODEL (store), 211-- &iter, tree_path); 212-- gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 213-- COL_PATH, &path, 214-- COL_IS_DIRECTORY, &is_dir, 215-- -1); 216 217-- if (!is_dir) 218-- { 219-- g_free (path); 220-- return; 221-- } 222 223-- /* Replace parent with path and re-fill the model*/ 224-- g_free (parent); 225-- parent = path; 226 227-- fill_store (store); 228 229-- /* Sensitize the up button */ 230-- gtk_widget_set_sensitive (GTK_WIDGET (up_button), TRUE); 231-- } 232 233-- static void 234-- up_clicked (GtkToolItem *item, 235-- gpointer user_data) 236-- { 237-- GtkListStore *store; 238-- gchar *dir_name; 239 240-- store = GTK_LIST_STORE (user_data); 241 242-- dir_name = g_path_get_dirname (parent); 243-- g_free (parent); 244 245-- parent = dir_name; 246 247-- fill_store (store); 248 249-- /* Maybe de-sensitize the up button */ 250-- gtk_widget_set_sensitive (GTK_WIDGET (up_button), 251-- strcmp (parent, "/") != 0); 252-- } 253 254-- static void 255-- home_clicked (GtkToolItem *item, 256-- gpointer user_data) 257-- { 258-- GtkListStore *store; 259 260-- store = GTK_LIST_STORE (user_data); 261 262-- g_free (parent); 263-- parent = g_strdup (g_get_home_dir ()); 264 265-- fill_store (store); 266 267-- /* Sensitize the up button */ 268-- gtk_widget_set_sensitive (GTK_WIDGET (up_button), 269-- TRUE); 270-- } 271 272-- GtkWidget * 273-- do_iconview (GtkWidget *do_widget) 274-- { 275-- if (!window) 276-- { 277-- GError *error; 278 279-- window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 280-- gtk_window_set_default_size (GTK_WINDOW (window), 650, 400); 281 282-- gtk_window_set_screen (GTK_WINDOW (window), 283-- gtk_widget_get_screen (do_widget)); 284-- gtk_window_set_title (GTK_WINDOW (window), "GtkIconView demo"); 285 286-- g_signal_connect (window, "destroy", 287-- G_CALLBACK (gtk_widget_destroyed), &window); 288 289-- error = NULL; 290-- if (!load_pixbufs (&error)) 291-- { 292-- GtkWidget *dialog; 293 294-- dialog = gtk_message_dialog_new (GTK_WINDOW (window), 295-- GTK_DIALOG_DESTROY_WITH_PARENT, 296-- GTK_MESSAGE_ERROR, 297-- GTK_BUTTONS_CLOSE, 298-- "Failed to load an image: %s", 299-- error->message); 300 301-- g_error_free (error); 302 303-- g_signal_connect (dialog, "response", 304-- G_CALLBACK (gtk_widget_destroy), NULL); 305 306-- gtk_widget_show (dialog); 307-- } 308-- else 309-- { 310-- GtkWidget *sw; 311-- GtkWidget *icon_view; 312-- GtkListStore *store; 313-- GtkWidget *vbox; 314-- GtkWidget *tool_bar; 315-- GtkToolItem *home_button; 316 317-- vbox = gtk_vbox_new (FALSE, 0); 318-- gtk_container_add (GTK_CONTAINER (window), vbox); 319 320-- tool_bar = gtk_toolbar_new (); 321-- gtk_box_pack_start (GTK_BOX (vbox), tool_bar, FALSE, FALSE, 0); 322 323-- up_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_UP); 324-- gtk_tool_item_set_is_important (up_button, TRUE); 325-- gtk_widget_set_sensitive (GTK_WIDGET (up_button), FALSE); 326-- gtk_toolbar_insert (GTK_TOOLBAR (tool_bar), up_button, -1); 327 328-- home_button = gtk_tool_button_new_from_stock (GTK_STOCK_HOME); 329-- gtk_tool_item_set_is_important (home_button, TRUE); 330-- gtk_toolbar_insert (GTK_TOOLBAR (tool_bar), home_button, -1); 331 332 333-- sw = gtk_scrolled_window_new (NULL, NULL); 334-- gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), 335-- GTK_SHADOW_ETCHED_IN); 336-- gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), 337-- GTK_POLICY_AUTOMATIC, 338-- GTK_POLICY_AUTOMATIC); 339 340-- gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0); 341 342-- /* Create the store and fill it with the contents of '/' */ 343-- parent = g_strdup ("/"); 344-- store = create_store (); 345-- fill_store (store); 346 347-- icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store)); 348-- gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view), 349-- GTK_SELECTION_MULTIPLE); 350-- g_object_unref (store); 351 352-- /* Connect to the "clicked" signal of the "Up" tool button */ 353-- g_signal_connect (up_button, "clicked", 354-- G_CALLBACK (up_clicked), store); 355 356-- /* Connect to the "clicked" signal of the "Home" tool button */ 357-- g_signal_connect (home_button, "clicked", 358-- G_CALLBACK (home_clicked), store); 359 360-- /* We now set which model columns that correspond to the text 361-- * and pixbuf of each item 362-- */ 363-- gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_view), COL_DISPLAY_NAME); 364-- gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_view), COL_PIXBUF); 365 366-- /* Connect to the "item_activated" signal */ 367-- g_signal_connect (icon_view, "item_activated", 368-- G_CALLBACK (item_activated), store); 369-- gtk_container_add (GTK_CONTAINER (sw), icon_view); 370 371-- gtk_widget_grab_focus (icon_view); 372-- } 373-- } 374 375-- if (!GTK_WIDGET_VISIBLE (window)) 376-- gtk_widget_show_all (window); 377-- else 378-- { 379-- gtk_widget_destroy (window); 380-- window = NULL; 381-- } 382 383-- return window; 384-- } 385 386end