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

http://github.com/tybor/Liberty · Specman e · 398 lines · 21 code · 58 blank · 319 comment · 2 complexity · 7d792afc311093bd0a9cce0aa058e4d2 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 COMBOBOX
  19. creation make
  20. feature
  21. -- /* Combo boxes
  22. -- *
  23. -- * The ComboBox widget allows to select one option out of a list.
  24. -- * The ComboBoxEntry additionally allows the user to enter a value
  25. -- * that is not in the list of options.
  26. -- *
  27. -- * How the options are displayed is controlled by cell renderers.
  28. -- */
  29. -- #include <gtk/gtk.h>
  30. -- enum
  31. -- {
  32. -- PIXBUF_COL,
  33. -- TEXT_COL
  34. -- };
  35. -- static gchar *
  36. -- strip_underscore (const gchar *text)
  37. -- {
  38. -- gchar *p, *q;
  39. -- gchar *result;
  40. -- result = g_strdup (text);
  41. -- p = q = result;
  42. -- while (*p)
  43. -- {
  44. -- if (*p != '_')
  45. -- {
  46. -- *q = *p;
  47. -- q++;
  48. -- }
  49. -- p++;
  50. -- }
  51. -- *q = '\0';
  52. -- return result;
  53. -- }
  54. -- static GtkTreeModel *
  55. -- create_stock_icon_store (void)
  56. -- {
  57. -- gchar *stock_id[6] = {
  58. -- GTK_STOCK_DIALOG_WARNING,
  59. -- GTK_STOCK_STOP,
  60. -- GTK_STOCK_NEW,
  61. -- GTK_STOCK_CLEAR,
  62. -- NULL,
  63. -- GTK_STOCK_OPEN
  64. -- };
  65. -- GtkStockItem item;
  66. -- GdkPixbuf *pixbuf;
  67. -- GtkWidget *cellview;
  68. -- GtkTreeIter iter;
  69. -- GtkListStore *store;
  70. -- gchar *label;
  71. -- gint i;
  72. -- cellview = gtk_cell_view_new ();
  73. -- store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
  74. -- for (i = 0; i < G_N_ELEMENTS (stock_id); i++)
  75. -- {
  76. -- if (stock_id[i])
  77. -- {
  78. -- pixbuf = gtk_widget_render_icon (cellview, stock_id[i],
  79. -- GTK_ICON_SIZE_BUTTON, NULL);
  80. -- gtk_stock_lookup (stock_id[i], &item);
  81. -- label = strip_underscore (item.label);
  82. -- gtk_list_store_append (store, &iter);
  83. -- gtk_list_store_set (store, &iter,
  84. -- PIXBUF_COL, pixbuf,
  85. -- TEXT_COL, label,
  86. -- -1);
  87. -- g_free (label);
  88. -- }
  89. -- else
  90. -- {
  91. -- gtk_list_store_append (store, &iter);
  92. -- gtk_list_store_set (store, &iter,
  93. -- PIXBUF_COL, NULL,
  94. -- TEXT_COL, "separator",
  95. -- -1);
  96. -- }
  97. -- }
  98. -- gtk_widget_destroy (cellview);
  99. -- return GTK_TREE_MODEL (store);
  100. -- }
  101. -- /* A GtkCellLayoutDataFunc that demonstrates how one can control
  102. -- * sensitivity of rows. This particular function does nothing
  103. -- * useful and just makes the second row insensitive.
  104. -- */
  105. -- static void
  106. -- set_sensitive (GtkCellLayout *cell_layout,
  107. -- GtkCellRenderer *cell,
  108. -- GtkTreeModel *tree_model,
  109. -- GtkTreeIter *iter,
  110. -- gpointer data)
  111. -- {
  112. -- GtkTreePath *path;
  113. -- gint *indices;
  114. -- gboolean sensitive;
  115. -- path = gtk_tree_model_get_path (tree_model, iter);
  116. -- indices = gtk_tree_path_get_indices (path);
  117. -- sensitive = indices[0] != 1;
  118. -- gtk_tree_path_free (path);
  119. -- g_object_set (cell, "sensitive", sensitive, NULL);
  120. -- }
  121. -- /* A GtkTreeViewRowSeparatorFunc that demonstrates how rows can be
  122. -- * rendered as separators. This particular function does nothing
  123. -- * useful and just turns the fourth row into a separator.
  124. -- */
  125. -- static gboolean
  126. -- is_separator (GtkTreeModel *model,
  127. -- GtkTreeIter *iter,
  128. -- gpointer data)
  129. -- {
  130. -- GtkTreePath *path;
  131. -- gboolean result;
  132. -- path = gtk_tree_model_get_path (model, iter);
  133. -- result = gtk_tree_path_get_indices (path)[0] == 4;
  134. -- gtk_tree_path_free (path);
  135. -- return result;
  136. -- }
  137. -- static GtkTreeModel *
  138. -- create_capital_store (void)
  139. -- {
  140. -- struct {
  141. -- gchar *group;
  142. -- gchar *capital;
  143. -- } capitals[] = {
  144. -- { "A - B", NULL },
  145. -- { NULL, "Albany" },
  146. -- { NULL, "Annapolis" },
  147. -- { NULL, "Atlanta" },
  148. -- { NULL, "Augusta" },
  149. -- { NULL, "Austin" },
  150. -- { NULL, "Baton Rouge" },
  151. -- { NULL, "Bismarck" },
  152. -- { NULL, "Boise" },
  153. -- { NULL, "Boston" },
  154. -- { "C - D", NULL },
  155. -- { NULL, "Carson City" },
  156. -- { NULL, "Charleston" },
  157. -- { NULL, "Cheyenne" },
  158. -- { NULL, "Columbia" },
  159. -- { NULL, "Columbus" },
  160. -- { NULL, "Concord" },
  161. -- { NULL, "Denver" },
  162. -- { NULL, "Des Moines" },
  163. -- { NULL, "Dover" },
  164. -- { "E - J", NULL },
  165. -- { NULL, "Frankfort" },
  166. -- { NULL, "Harrisburg" },
  167. -- { NULL, "Hartford" },
  168. -- { NULL, "Helena" },
  169. -- { NULL, "Honolulu" },
  170. -- { NULL, "Indianapolis" },
  171. -- { NULL, "Jackson" },
  172. -- { NULL, "Jefferson City" },
  173. -- { NULL, "Juneau" },
  174. -- { "K - O" },
  175. -- { NULL, "Lansing" },
  176. -- { NULL, "Lincoln" },
  177. -- { NULL, "Little Rock" },
  178. -- { NULL, "Madison" },
  179. -- { NULL, "Montgomery" },
  180. -- { NULL, "Montpelier" },
  181. -- { NULL, "Nashville" },
  182. -- { NULL, "Oklahoma City" },
  183. -- { NULL, "Olympia" },
  184. -- { NULL, "P - S" },
  185. -- { NULL, "Phoenix" },
  186. -- { NULL, "Pierre" },
  187. -- { NULL, "Providence" },
  188. -- { NULL, "Raleigh" },
  189. -- { NULL, "Richmond" },
  190. -- { NULL, "Sacramento" },
  191. -- { NULL, "Salem" },
  192. -- { NULL, "Salt Lake City" },
  193. -- { NULL, "Santa Fe" },
  194. -- { NULL, "Springfield" },
  195. -- { NULL, "St. Paul" },
  196. -- { "T - Z", NULL },
  197. -- { NULL, "Tallahassee" },
  198. -- { NULL, "Topeka" },
  199. -- { NULL, "Trenton" },
  200. -- { NULL, NULL }
  201. -- };
  202. -- GtkTreeIter iter, iter2;
  203. -- GtkTreeStore *store;
  204. -- gint i;
  205. -- store = gtk_tree_store_new (1, G_TYPE_STRING);
  206. -- for (i = 0; capitals[i].group || capitals[i].capital; i++)
  207. -- {
  208. -- if (capitals[i].group)
  209. -- {
  210. -- gtk_tree_store_append (store, &iter, NULL);
  211. -- gtk_tree_store_set (store, &iter, 0, capitals[i].group, -1);
  212. -- }
  213. -- else if (capitals[i].capital)
  214. -- {
  215. -- gtk_tree_store_append (store, &iter2, &iter);
  216. -- gtk_tree_store_set (store, &iter2, 0, capitals[i].capital, -1);
  217. -- }
  218. -- }
  219. -- return GTK_TREE_MODEL (store);
  220. -- }
  221. -- static void
  222. -- is_capital_sensitive (GtkCellLayout *cell_layout,
  223. -- GtkCellRenderer *cell,
  224. -- GtkTreeModel *tree_model,
  225. -- GtkTreeIter *iter,
  226. -- gpointer data)
  227. -- {
  228. -- gboolean sensitive;
  229. -- sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
  230. -- g_object_set (cell, "sensitive", sensitive, NULL);
  231. -- }
  232. -- static void
  233. -- fill_combo_entry (GtkWidget *entry)
  234. -- {
  235. -- gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "One");
  236. -- gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "Two");
  237. -- gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "2\302\275");
  238. -- gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "Three");
  239. -- }
  240. -- GtkWidget *
  241. -- do_combobox (GtkWidget *do_widget)
  242. -- {
  243. -- static GtkWidget *window = NULL;
  244. -- GtkWidget *vbox, *frame, *box, *combo;
  245. -- GtkTreeModel *model;
  246. -- GtkCellRenderer *renderer;
  247. -- GtkTreePath *path;
  248. -- GtkTreeIter iter;
  249. -- if (!window)
  250. -- {
  251. -- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  252. -- gtk_window_set_screen (GTK_WINDOW (window),
  253. -- gtk_widget_get_screen (do_widget));
  254. -- gtk_window_set_title (GTK_WINDOW (window), "Combo boxes");
  255. -- g_signal_connect (window, "destroy",
  256. -- G_CALLBACK (gtk_widget_destroyed),
  257. -- &window);
  258. -- gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  259. -- vbox = gtk_vbox_new (FALSE, 2);
  260. -- gtk_container_add (GTK_CONTAINER (window), vbox);
  261. -- /* A combobox demonstrating cell renderers, separators and
  262. -- * insensitive rows
  263. -- */
  264. -- frame = gtk_frame_new ("Some stock icons");
  265. -- gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  266. -- box = gtk_vbox_new (FALSE, 0);
  267. -- gtk_container_set_border_width (GTK_CONTAINER (box), 5);
  268. -- gtk_container_add (GTK_CONTAINER (frame), box);
  269. -- model = create_stock_icon_store ();
  270. -- combo = gtk_combo_box_new_with_model (model);
  271. -- g_object_unref (model);
  272. -- gtk_container_add (GTK_CONTAINER (box), combo);
  273. -- renderer = gtk_cell_renderer_pixbuf_new ();
  274. -- gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, FALSE);
  275. -- gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
  276. -- "pixbuf", PIXBUF_COL,
  277. -- NULL);
  278. -- gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
  279. -- renderer,
  280. -- set_sensitive,
  281. -- NULL, NULL);
  282. -- renderer = gtk_cell_renderer_text_new ();
  283. -- gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
  284. -- gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
  285. -- "text", TEXT_COL,
  286. -- NULL);
  287. -- gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
  288. -- renderer,
  289. -- set_sensitive,
  290. -- NULL, NULL);
  291. -- gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
  292. -- is_separator, NULL, NULL);
  293. -- gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
  294. -- /* A combobox demonstrating trees.
  295. -- */
  296. -- frame = gtk_frame_new ("Where are we ?");
  297. -- gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  298. -- box = gtk_vbox_new (FALSE, 0);
  299. -- gtk_container_set_border_width (GTK_CONTAINER (box), 5);
  300. -- gtk_container_add (GTK_CONTAINER (frame), box);
  301. -- model = create_capital_store ();
  302. -- combo = gtk_combo_box_new_with_model (model);
  303. -- g_object_unref (model);
  304. -- gtk_container_add (GTK_CONTAINER (box), combo);
  305. -- renderer = gtk_cell_renderer_text_new ();
  306. -- gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
  307. -- gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
  308. -- "text", 0,
  309. -- NULL);
  310. -- gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
  311. -- renderer,
  312. -- is_capital_sensitive,
  313. -- NULL, NULL);
  314. -- path = gtk_tree_path_new_from_indices (0, 8, -1);
  315. -- gtk_tree_model_get_iter (model, &iter, path);
  316. -- gtk_tree_path_free (path);
  317. -- gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
  318. -- /* A GtkComboBoxEntry
  319. -- */
  320. -- frame = gtk_frame_new ("Editable");
  321. -- gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  322. -- box = gtk_vbox_new (FALSE, 0);
  323. -- gtk_container_set_border_width (GTK_CONTAINER (box), 5);
  324. -- gtk_container_add (GTK_CONTAINER (frame), box);
  325. -- combo = gtk_combo_box_entry_new_text ();
  326. -- fill_combo_entry (combo);
  327. -- gtk_container_add (GTK_CONTAINER (box), combo);
  328. -- }
  329. -- if (!GTK_WIDGET_VISIBLE (window))
  330. -- {
  331. -- gtk_widget_show_all (window);
  332. -- }
  333. -- else
  334. -- {
  335. -- gtk_widget_destroy (window);
  336. -- window = NULL;
  337. -- }
  338. -- return window;
  339. -- }
  340. end