/src/wrappers/gtk/examples/gtk-demo/iconview.e

http://github.com/tybor/Liberty · Specman e · 386 lines · 21 code · 85 blank · 280 comment · 2 complexity · 15f8c1b7852e9cb96aab777af8496195 MD5 · raw file

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