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