editable_cells.c 11 KB
Newer Older
1
/* Tree View/Editable Cells
2
 *
Kristian Rietveld's avatar
Kristian Rietveld committed
3
 * This demo demonstrates the use of editable cells in a GtkTreeView. If
4
 * you're new to the GtkTreeView widgets and associates, look into
5 6 7
 * the GtkListStore example first. It also shows how to use the
 * GtkCellRenderer::editing-started signal to do custom setup of the
 * editable widget.
8
 *
9 10
 * The cell renderers used in this demo are GtkCellRendererText, 
 * GtkCellRendererCombo and GtkCellRendererProgress.
11 12 13 14 15 16 17 18 19 20
 */

#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>

static GtkWidget *window = NULL;

typedef struct
{
21 22
  gint   number;
  gchar *product;
23
  gint   yummy;
24 25 26 27 28
}
Item;

enum
{
29 30
  COLUMN_ITEM_NUMBER,
  COLUMN_ITEM_PRODUCT,
31
  COLUMN_ITEM_YUMMY,
32 33 34 35 36 37 38
  NUM_ITEM_COLUMNS
};

enum
{
  COLUMN_NUMBER_TEXT,
  NUM_NUMBER_COLUMNS
39 40 41 42 43 44 45 46 47 48 49 50 51
};

static GArray *articles = NULL;

static void
add_items (void)
{
  Item foo;

  g_return_if_fail (articles != NULL);

  foo.number = 3;
  foo.product = g_strdup ("bottles of coke");
52
  foo.yummy = 20;
53 54 55 56
  g_array_append_vals (articles, &foo, 1);

  foo.number = 5;
  foo.product = g_strdup ("packages of noodles");
57
  foo.yummy = 50;
58 59 60 61
  g_array_append_vals (articles, &foo, 1);

  foo.number = 2;
  foo.product = g_strdup ("packages of chocolate chip cookies");
62
  foo.yummy = 90;
63 64 65 66
  g_array_append_vals (articles, &foo, 1);

  foo.number = 1;
  foo.product = g_strdup ("can vanilla ice cream");
67
  foo.yummy = 60;
68 69 70 71
  g_array_append_vals (articles, &foo, 1);

  foo.number = 6;
  foo.product = g_strdup ("eggs");
72
  foo.yummy = 10;
73 74 75 76
  g_array_append_vals (articles, &foo, 1);
}

static GtkTreeModel *
77
create_items_model (void)
78 79 80 81 82 83 84
{
  gint i = 0;
  GtkListStore *model;
  GtkTreeIter iter;

  /* create array */
  articles = g_array_sized_new (FALSE, FALSE, sizeof (Item), 1);
85

86 87 88
  add_items ();

  /* create list store */
89
  model = gtk_list_store_new (NUM_ITEM_COLUMNS, G_TYPE_INT, G_TYPE_STRING,
90
                              G_TYPE_INT, G_TYPE_BOOLEAN);
91 92 93 94 95 96 97

  /* add items */
  for (i = 0; i < articles->len; i++)
    {
      gtk_list_store_append (model, &iter);

      gtk_list_store_set (model, &iter,
98 99 100 101 102 103 104
                          COLUMN_ITEM_NUMBER,
                          g_array_index (articles, Item, i).number,
                          COLUMN_ITEM_PRODUCT,
                          g_array_index (articles, Item, i).product,
                          COLUMN_ITEM_YUMMY,
                          g_array_index (articles, Item, i).yummy,
                          -1);
105 106 107 108 109
    }

  return GTK_TREE_MODEL (model);
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123
static GtkTreeModel *
create_numbers_model (void)
{
#define N_NUMBERS 10
  gint i = 0;
  GtkListStore *model;
  GtkTreeIter iter;

  /* create list store */
  model = gtk_list_store_new (NUM_NUMBER_COLUMNS, G_TYPE_STRING, G_TYPE_INT);

  /* add numbers */
  for (i = 0; i < N_NUMBERS; i++)
    {
Matthias Clasen's avatar
Matthias Clasen committed
124 125 126 127
      char str[2];

      str[0] = '0' + i;
      str[1] = '\0';
128 129 130 131

      gtk_list_store_append (model, &iter);

      gtk_list_store_set (model, &iter,
132 133
                          COLUMN_NUMBER_TEXT, str,
                          -1);
134 135 136 137 138 139 140
    }

  return GTK_TREE_MODEL (model);

#undef N_NUMBERS
}

141 142 143 144 145 146 147 148 149 150 151
static void
add_item (GtkWidget *button, gpointer data)
{
  Item foo;
  GtkTreeIter iter;
  GtkTreeModel *model = (GtkTreeModel *)data;

  g_return_if_fail (articles != NULL);

  foo.number = 0;
  foo.product = g_strdup ("Description here");
152
  foo.yummy = 50;
153 154 155 156
  g_array_append_vals (articles, &foo, 1);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
157 158 159 160
                      COLUMN_ITEM_NUMBER, foo.number,
                      COLUMN_ITEM_PRODUCT, foo.product,
                      COLUMN_ITEM_YUMMY, foo.yummy,
                      -1);
161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

static void
remove_item (GtkWidget *widget, gpointer data)
{
  GtkTreeIter iter;
  GtkTreeView *treeview = (GtkTreeView *)data;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);

  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      gint i;
      GtkTreePath *path;
175

176 177 178
      path = gtk_tree_model_get_path (model, &iter);
      i = gtk_tree_path_get_indices (path)[0];
      gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
179

180 181 182 183 184 185
      g_array_remove_index (articles, i);

      gtk_tree_path_free (path);
    }
}

186 187
static gboolean
separator_row (GtkTreeModel *model,
188 189
               GtkTreeIter  *iter,
               gpointer      data)
190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
  GtkTreePath *path;
  gint idx;

  path = gtk_tree_model_get_path (model, iter);
  idx = gtk_tree_path_get_indices (path)[0];

  gtk_tree_path_free (path);

  return idx == 5;
}

static void
editing_started (GtkCellRenderer *cell,
204 205 206
                 GtkCellEditable *editable,
                 const gchar     *path,
                 gpointer         data)
207 208
{
  gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (editable), 
209
                                        separator_row, NULL, NULL);
210 211
}

212 213
static void
cell_edited (GtkCellRendererText *cell,
214 215 216
             const gchar         *path_string,
             const gchar         *new_text,
             gpointer             data)
217 218 219 220
{
  GtkTreeModel *model = (GtkTreeModel *)data;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  GtkTreeIter iter;
221

Kristian Rietveld's avatar
Kristian Rietveld committed
222
  gint column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));
223 224 225

  gtk_tree_model_get_iter (model, &iter, path);

Kristian Rietveld's avatar
Kristian Rietveld committed
226
  switch (column)
227
    {
228
    case COLUMN_ITEM_NUMBER:
229
      {
230
        gint i;
231

232 233
        i = gtk_tree_path_get_indices (path)[0];
        g_array_index (articles, Item, i).number = atoi (new_text);
234

235 236
        gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
                            g_array_index (articles, Item, i).number, -1);
237 238
      }
      break;
239

240
    case COLUMN_ITEM_PRODUCT:
241
      {
242 243
        gint i;
        gchar *old_text;
244 245

        gtk_tree_model_get (model, &iter, column, &old_text, -1);
246
        g_free (old_text);
247

248 249 250
        i = gtk_tree_path_get_indices (path)[0];
        g_free (g_array_index (articles, Item, i).product);
        g_array_index (articles, Item, i).product = g_strdup (new_text);
251

252
        gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
253
                            g_array_index (articles, Item, i).product, -1);
254 255 256 257 258 259 260 261
      }
      break;
    }

  gtk_tree_path_free (path);
}

static void
262
add_columns (GtkTreeView  *treeview, 
263 264
             GtkTreeModel *items_model,
             GtkTreeModel *numbers_model)
265 266 267 268
{
  GtkCellRenderer *renderer;

  /* number column */
269 270
  renderer = gtk_cell_renderer_combo_new ();
  g_object_set (renderer,
271 272 273 274 275
                "model", numbers_model,
                "text-column", COLUMN_NUMBER_TEXT,
                "has-entry", FALSE,
                "editable", TRUE,
                NULL);
276
  g_signal_connect (renderer, "edited",
277
                    G_CALLBACK (cell_edited), items_model);
278
  g_signal_connect (renderer, "editing-started",
279
                    G_CALLBACK (editing_started), NULL);
280
  g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_NUMBER));
281 282

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
283 284 285
                                               -1, "Number", renderer,
                                               "text", COLUMN_ITEM_NUMBER,
                                               NULL);
286 287 288

  /* product column */
  renderer = gtk_cell_renderer_text_new ();
289
  g_object_set (renderer,
290 291
                "editable", TRUE,
                NULL);
292
  g_signal_connect (renderer, "edited",
293
                    G_CALLBACK (cell_edited), items_model);
294
  g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_PRODUCT));
295

296
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
297 298 299 300 301 302 303 304 305 306 307 308 309 310
                                               -1, "Product", renderer,
                                               "text", COLUMN_ITEM_PRODUCT,
                                               NULL);

  /* yummy column */
  renderer = gtk_cell_renderer_progress_new ();
  g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_YUMMY));

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                               -1, "Yummy", renderer,
                                               "value", COLUMN_ITEM_YUMMY,
                                               NULL);
  

311 312 313
}

GtkWidget *
314
do_editable_cells (GtkWidget *do_widget)
315 316 317 318 319 320 321 322
{
  if (!window)
    {
      GtkWidget *vbox;
      GtkWidget *hbox;
      GtkWidget *sw;
      GtkWidget *treeview;
      GtkWidget *button;
323 324
      GtkTreeModel *items_model;
      GtkTreeModel *numbers_model;
325

326 327
      /* create window, etc */
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
328
      gtk_window_set_screen (GTK_WINDOW (window),
329
                             gtk_widget_get_screen (do_widget));
330 331
      gtk_window_set_title (GTK_WINDOW (window), "Shopping list");
      gtk_container_set_border_width (GTK_CONTAINER (window), 5);
332
      g_signal_connect (window, "destroy",
333
                        G_CALLBACK (gtk_widget_destroyed), &window);
334

335
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
336 337 338
      gtk_container_add (GTK_CONTAINER (window), vbox);

      gtk_box_pack_start (GTK_BOX (vbox),
339 340
                          gtk_label_new ("Shopping list (you can edit the cells!)"),
                          FALSE, FALSE, 0);
341 342 343

      sw = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
344
                                           GTK_SHADOW_ETCHED_IN);
345
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
346 347
                                      GTK_POLICY_AUTOMATIC,
                                      GTK_POLICY_AUTOMATIC);
348 349
      gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);

350 351 352
      /* create models */
      items_model = create_items_model ();
      numbers_model = create_numbers_model ();
353 354

      /* create tree view */
355
      treeview = gtk_tree_view_new_with_model (items_model);
356 357
      gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
      gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
358
                                   GTK_SELECTION_SINGLE);
359

360 361 362 363
      add_columns (GTK_TREE_VIEW (treeview), items_model, numbers_model);

      g_object_unref (numbers_model);
      g_object_unref (items_model);
364

365
      gtk_container_add (GTK_CONTAINER (sw), treeview);
366

367
      /* some buttons */
368 369
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
      gtk_box_set_homogeneous (GTK_BOX (hbox), TRUE);
370 371 372
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

      button = gtk_button_new_with_label ("Add item");
373
      g_signal_connect (button, "clicked",
374
                        G_CALLBACK (add_item), items_model);
375 376 377
      gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

      button = gtk_button_new_with_label ("Remove item");
378
      g_signal_connect (button, "clicked",
379
                        G_CALLBACK (remove_item), treeview);
380 381 382 383
      gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

      gtk_window_set_default_size (GTK_WINDOW (window), 320, 200);
    }
384

385
  if (!gtk_widget_get_visible (window))
386 387 388 389 390 391 392 393 394
    gtk_widget_show_all (window);
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}