/src/wrappers/gtk/examples/gtk-demo/editable_cells.e
Specman e | 417 lines | 21 code | 83 blank | 313 comment | 2 complexity | c4ea91858edeaf4cb90eb012b96e5d9c 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 EDITABLE_CELLS 23 24creation make 25 26feature 27-- /* Tree View/Editable Cells 28-- * 29-- * This demo demonstrates the use of editable cells in a GtkTreeView. If 30-- * you're new to the GtkTreeView widgets and associates, look into 31-- * the GtkListStore example first. It also shows how to use the 32-- * GtkCellRenderer::editing-started signal to do custom setup of the 33-- * editable widget. 34-- * 35-- * The cell renderers used in this demo are GtkCellRendererText, 36-- * GtkCellRendererCombo and GtkCellRendererProgress. 37-- */ 38 39-- #include <gtk/gtk.h> 40-- #include <string.h> 41-- #include <stdlib.h> 42 43-- static GtkWidget *window = NULL; 44 45-- typedef struct 46-- { 47-- gint number; 48-- gchar *product; 49-- gint yummy; 50-- } 51-- Item; 52 53-- enum 54-- { 55-- COLUMN_ITEM_NUMBER, 56-- COLUMN_ITEM_PRODUCT, 57-- COLUMN_ITEM_YUMMY, 58-- NUM_ITEM_COLUMNS 59-- }; 60 61-- enum 62-- { 63-- COLUMN_NUMBER_TEXT, 64-- NUM_NUMBER_COLUMNS 65-- }; 66 67-- static GArray *articles = NULL; 68 69-- static void 70-- add_items (void) 71-- { 72-- Item foo; 73 74-- g_return_if_fail (articles != NULL); 75 76-- foo.number = 3; 77-- foo.product = g_strdup ("bottles of coke"); 78-- foo.yummy = 20; 79-- g_array_append_vals (articles, &foo, 1); 80 81-- foo.number = 5; 82-- foo.product = g_strdup ("packages of noodles"); 83-- foo.yummy = 50; 84-- g_array_append_vals (articles, &foo, 1); 85 86-- foo.number = 2; 87-- foo.product = g_strdup ("packages of chocolate chip cookies"); 88-- foo.yummy = 90; 89-- g_array_append_vals (articles, &foo, 1); 90 91-- foo.number = 1; 92-- foo.product = g_strdup ("can vanilla ice cream"); 93-- foo.yummy = 60; 94-- g_array_append_vals (articles, &foo, 1); 95 96-- foo.number = 6; 97-- foo.product = g_strdup ("eggs"); 98-- foo.yummy = 10; 99-- g_array_append_vals (articles, &foo, 1); 100-- } 101 102-- static GtkTreeModel * 103-- create_items_model (void) 104-- { 105-- gint i = 0; 106-- GtkListStore *model; 107-- GtkTreeIter iter; 108 109-- /* create array */ 110-- articles = g_array_sized_new (FALSE, FALSE, sizeof (Item), 1); 111 112-- add_items (); 113 114-- /* create list store */ 115-- model = gtk_list_store_new (NUM_ITEM_COLUMNS, G_TYPE_INT, G_TYPE_STRING, 116-- G_TYPE_INT, G_TYPE_BOOLEAN); 117 118-- /* add items */ 119-- for (i = 0; i < articles->len; i++) 120-- { 121-- gtk_list_store_append (model, &iter); 122 123-- gtk_list_store_set (model, &iter, 124-- COLUMN_ITEM_NUMBER, 125-- g_array_index (articles, Item, i).number, 126-- COLUMN_ITEM_PRODUCT, 127-- g_array_index (articles, Item, i).product, 128-- COLUMN_ITEM_YUMMY, 129-- g_array_index (articles, Item, i).yummy, 130-- -1); 131-- } 132 133-- return GTK_TREE_MODEL (model); 134-- } 135 136-- static GtkTreeModel * 137-- create_numbers_model (void) 138-- { 139-- #define N_NUMBERS 10 140-- gint i = 0; 141-- GtkListStore *model; 142-- GtkTreeIter iter; 143 144-- /* create list store */ 145-- model = gtk_list_store_new (NUM_NUMBER_COLUMNS, G_TYPE_STRING, G_TYPE_INT); 146 147-- /* add numbers */ 148-- for (i = 0; i < N_NUMBERS; i++) 149-- { 150-- char str[2] = { '0' + i, '\0' }; 151 152-- gtk_list_store_append (model, &iter); 153 154-- gtk_list_store_set (model, &iter, 155-- COLUMN_NUMBER_TEXT, str, 156-- -1); 157-- } 158 159-- return GTK_TREE_MODEL (model); 160 161-- #undef N_NUMBERS 162-- } 163 164-- static void 165-- add_item (GtkWidget *button, gpointer data) 166-- { 167-- Item foo; 168-- GtkTreeIter iter; 169-- GtkTreeModel *model = (GtkTreeModel *)data; 170 171-- g_return_if_fail (articles != NULL); 172 173-- foo.number = 0; 174-- foo.product = g_strdup ("Description here"); 175-- foo.yummy = 50; 176-- g_array_append_vals (articles, &foo, 1); 177 178-- gtk_list_store_append (GTK_LIST_STORE (model), &iter); 179-- gtk_list_store_set (GTK_LIST_STORE (model), &iter, 180-- COLUMN_ITEM_NUMBER, foo.number, 181-- COLUMN_ITEM_PRODUCT, foo.product, 182-- COLUMN_ITEM_YUMMY, foo.yummy, 183-- -1); 184-- } 185 186-- static void 187-- remove_item (GtkWidget *widget, gpointer data) 188-- { 189-- GtkTreeIter iter; 190-- GtkTreeView *treeview = (GtkTreeView *)data; 191-- GtkTreeModel *model = gtk_tree_view_get_model (treeview); 192-- GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview); 193 194-- if (gtk_tree_selection_get_selected (selection, NULL, &iter)) 195-- { 196-- gint i; 197-- GtkTreePath *path; 198 199-- path = gtk_tree_model_get_path (model, &iter); 200-- i = gtk_tree_path_get_indices (path)[0]; 201-- gtk_list_store_remove (GTK_LIST_STORE (model), &iter); 202 203-- g_array_remove_index (articles, i); 204 205-- gtk_tree_path_free (path); 206-- } 207-- } 208 209-- static gboolean 210-- separator_row (GtkTreeModel *model, 211-- GtkTreeIter *iter, 212-- gpointer data) 213-- { 214-- GtkTreePath *path; 215-- gint idx; 216 217-- path = gtk_tree_model_get_path (model, iter); 218-- idx = gtk_tree_path_get_indices (path)[0]; 219 220-- gtk_tree_path_free (path); 221 222-- return idx == 5; 223-- } 224 225-- static void 226-- editing_started (GtkCellRenderer *cell, 227-- GtkCellEditable *editable, 228-- const gchar *path, 229-- gpointer data) 230-- { 231-- gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (editable), 232-- separator_row, NULL, NULL); 233-- } 234 235-- static void 236-- cell_edited (GtkCellRendererText *cell, 237-- const gchar *path_string, 238-- const gchar *new_text, 239-- gpointer data) 240-- { 241-- GtkTreeModel *model = (GtkTreeModel *)data; 242-- GtkTreePath *path = gtk_tree_path_new_from_string (path_string); 243-- GtkTreeIter iter; 244 245-- gint column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column")); 246 247-- gtk_tree_model_get_iter (model, &iter, path); 248 249-- switch (column) 250-- { 251-- case COLUMN_ITEM_NUMBER: 252-- { 253-- gint i; 254 255-- i = gtk_tree_path_get_indices (path)[0]; 256-- g_array_index (articles, Item, i).number = atoi (new_text); 257 258-- gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, 259-- g_array_index (articles, Item, i).number, -1); 260-- } 261-- break; 262 263-- case COLUMN_ITEM_PRODUCT: 264-- { 265-- gint i; 266-- gchar *old_text; 267 268-- gtk_tree_model_get (model, &iter, column, &old_text, -1); 269-- g_free (old_text); 270 271-- i = gtk_tree_path_get_indices (path)[0]; 272-- g_free (g_array_index (articles, Item, i).product); 273-- g_array_index (articles, Item, i).product = g_strdup (new_text); 274 275-- gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, 276-- g_array_index (articles, Item, i).product, -1); 277-- } 278-- break; 279-- } 280 281-- gtk_tree_path_free (path); 282-- } 283 284-- static void 285-- add_columns (GtkTreeView *treeview, 286-- GtkTreeModel *items_model, 287-- GtkTreeModel *numbers_model) 288-- { 289-- GtkCellRenderer *renderer; 290 291-- /* number column */ 292-- renderer = gtk_cell_renderer_combo_new (); 293-- g_object_set (renderer, 294-- "model", numbers_model, 295-- "text-column", COLUMN_NUMBER_TEXT, 296-- "has-entry", FALSE, 297-- "editable", TRUE, 298-- NULL); 299-- g_signal_connect (renderer, "edited", 300-- G_CALLBACK (cell_edited), items_model); 301-- g_signal_connect (renderer, "editing-started", 302-- G_CALLBACK (editing_started), NULL); 303-- g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_NUMBER)); 304 305-- gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), 306-- -1, "Number", renderer, 307-- "text", COLUMN_ITEM_NUMBER, 308-- NULL); 309 310-- /* product column */ 311-- renderer = gtk_cell_renderer_text_new (); 312-- g_object_set (renderer, 313-- "editable", TRUE, 314-- NULL); 315-- g_signal_connect (renderer, "edited", 316-- G_CALLBACK (cell_edited), items_model); 317-- g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_PRODUCT)); 318 319-- gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), 320-- -1, "Product", renderer, 321-- "text", COLUMN_ITEM_PRODUCT, 322-- NULL); 323 324-- /* yummy column */ 325-- renderer = gtk_cell_renderer_progress_new (); 326-- g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_YUMMY)); 327 328-- gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), 329-- -1, "Yummy", renderer, 330-- "value", COLUMN_ITEM_YUMMY, 331-- NULL); 332 333 334-- } 335 336-- GtkWidget * 337-- do_editable_cells (GtkWidget *do_widget) 338-- { 339-- if (!window) 340-- { 341-- GtkWidget *vbox; 342-- GtkWidget *hbox; 343-- GtkWidget *sw; 344-- GtkWidget *treeview; 345-- GtkWidget *button; 346-- GtkTreeModel *items_model; 347-- GtkTreeModel *numbers_model; 348 349-- /* create window, etc */ 350-- window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 351-- gtk_window_set_screen (GTK_WINDOW (window), 352-- gtk_widget_get_screen (do_widget)); 353-- gtk_window_set_title (GTK_WINDOW (window), "Shopping list"); 354-- gtk_container_set_border_width (GTK_CONTAINER (window), 5); 355-- g_signal_connect (window, "destroy", 356-- G_CALLBACK (gtk_widget_destroyed), &window); 357 358-- vbox = gtk_vbox_new (FALSE, 5); 359-- gtk_container_add (GTK_CONTAINER (window), vbox); 360 361-- gtk_box_pack_start (GTK_BOX (vbox), 362-- gtk_label_new ("Shopping list (you can edit the cells!)"), 363-- FALSE, FALSE, 0); 364 365-- sw = gtk_scrolled_window_new (NULL, NULL); 366-- gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), 367-- GTK_SHADOW_ETCHED_IN); 368-- gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), 369-- GTK_POLICY_AUTOMATIC, 370-- GTK_POLICY_AUTOMATIC); 371-- gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0); 372 373-- /* create models */ 374-- items_model = create_items_model (); 375-- numbers_model = create_numbers_model (); 376 377-- /* create tree view */ 378-- treeview = gtk_tree_view_new_with_model (items_model); 379-- gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE); 380-- gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)), 381-- GTK_SELECTION_SINGLE); 382 383-- add_columns (GTK_TREE_VIEW (treeview), items_model, numbers_model); 384 385-- g_object_unref (numbers_model); 386-- g_object_unref (items_model); 387 388-- gtk_container_add (GTK_CONTAINER (sw), treeview); 389 390-- /* some buttons */ 391-- hbox = gtk_hbox_new (TRUE, 4); 392-- gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); 393 394-- button = gtk_button_new_with_label ("Add item"); 395-- g_signal_connect (button, "clicked", 396-- G_CALLBACK (add_item), items_model); 397-- gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0); 398 399-- button = gtk_button_new_with_label ("Remove item"); 400-- g_signal_connect (button, "clicked", 401-- G_CALLBACK (remove_item), treeview); 402-- gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0); 403 404-- gtk_window_set_default_size (GTK_WINDOW (window), 320, 200); 405-- } 406 407-- if (!GTK_WIDGET_VISIBLE (window)) 408-- gtk_widget_show_all (window); 409-- else 410-- { 411-- gtk_widget_destroy (window); 412-- window = NULL; 413-- } 414 415-- return window; 416-- } 417end