testtreeedit.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testtreeedit.c
 * Copyright (C) 2001 Red Hat, Inc
 * Author: Jonathan Blandford
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20 21 22 23 24
#include <gtk/gtk.h>

typedef struct {
  const gchar *string;
  gboolean is_editable;
25
  gboolean is_sensitive;
26
  gint progress;
27 28 29 30 31
} ListEntry;

enum {
  STRING_COLUMN,
  IS_EDITABLE_COLUMN,
32
  IS_SENSITIVE_COLUMN,
33
  PIXBUF_COLUMN,
34
  LAST_PIXBUF_COLUMN,
35
  PROGRESS_COLUMN,
36 37 38 39 40
  NUM_COLUMNS
};

static ListEntry model_strings[] =
{
41 42
  {"A simple string", TRUE, TRUE, 0 },
  {"Another string!", TRUE, TRUE, 10 },
43
  {"", TRUE, TRUE, 0 },
44 45 46
  {"Guess what, a third string. This one can't be edited", FALSE, TRUE, 47 },
  {"And then a fourth string. Neither can this", FALSE, TRUE, 48 },
  {"Multiline\nFun!", TRUE, FALSE, 75 },
47 48 49 50 51 52
  { NULL }
};

static GtkTreeModel *
create_model (void)
{
53
  GtkTreeStore *model;
54 55
  GtkTreeIter iter;
  gint i;
56
  GdkPixbuf *foo, *bar;
57 58 59
  GtkWidget *blah;

  blah = gtk_window_new (GTK_WINDOW_TOPLEVEL);
60
  foo = gtk_widget_render_icon_pixbuf (blah, GTK_STOCK_NEW, GTK_ICON_SIZE_MENU);
61
  bar = gtk_widget_render_icon_pixbuf (blah, GTK_STOCK_DELETE, GTK_ICON_SIZE_MENU);
62
  gtk_widget_destroy (blah);
63
  
64
  model = gtk_tree_store_new (NUM_COLUMNS,
65
			      G_TYPE_STRING,
66
			      G_TYPE_BOOLEAN,
67
			      G_TYPE_BOOLEAN,
68
			      GDK_TYPE_PIXBUF,
69
			      GDK_TYPE_PIXBUF,
70
			      G_TYPE_INT);
71 72 73

  for (i = 0; model_strings[i].string != NULL; i++)
    {
74
      gtk_tree_store_append (model, &iter, NULL);
75

76
      gtk_tree_store_set (model, &iter,
77 78
			  STRING_COLUMN, model_strings[i].string,
			  IS_EDITABLE_COLUMN, model_strings[i].is_editable,
79
			  IS_SENSITIVE_COLUMN, model_strings[i].is_sensitive,
80
			  PIXBUF_COLUMN, foo,
81
			  LAST_PIXBUF_COLUMN, bar,
82
			  PROGRESS_COLUMN, model_strings[i].progress,
83 84 85 86 87 88 89
			  -1);
    }
  
  return GTK_TREE_MODEL (model);
}

static void
90 91 92
editable_toggled (GtkCellRendererToggle *cell,
		  gchar                 *path_string,
		  gpointer               data)
93 94 95 96 97 98 99 100 101 102
{
  GtkTreeModel *model = GTK_TREE_MODEL (data);
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  gboolean value;

  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter, IS_EDITABLE_COLUMN, &value, -1);

  value = !value;
103
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter, IS_EDITABLE_COLUMN, value, -1);
104 105 106 107

  gtk_tree_path_free (path);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
static void
sensitive_toggled (GtkCellRendererToggle *cell,
		   gchar                 *path_string,
		   gpointer               data)
{
  GtkTreeModel *model = GTK_TREE_MODEL (data);
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  gboolean value;

  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter, IS_SENSITIVE_COLUMN, &value, -1);

  value = !value;
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter, IS_SENSITIVE_COLUMN, value, -1);

  gtk_tree_path_free (path);
}

127 128 129 130 131 132 133 134 135 136 137
static void
edited (GtkCellRendererText *cell,
	gchar               *path_string,
	gchar               *new_text,
	gpointer             data)
{
  GtkTreeModel *model = GTK_TREE_MODEL (data);
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);

  gtk_tree_model_get_iter (model, &iter, path);
138
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter, STRING_COLUMN, new_text, -1);
139 140 141 142

  gtk_tree_path_free (path);
}

Anders Carlsson's avatar
Anders Carlsson committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156
static gboolean
button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer callback_data)
{
	/* Deselect if people click outside any row. */
	if (event->window == gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget))
	    && !gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
					       event->x, event->y, NULL, NULL, NULL, NULL)) {
		gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)));
	}

	/* Let the default code run in any case; it won't reselect anything. */
	return FALSE;
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170
typedef struct {
  GtkCellArea     *area;
  GtkCellRenderer *renderer;
} CallbackData;

static void
align_cell_toggled (GtkToggleButton  *toggle,
		    CallbackData     *data)
{
  gboolean active = gtk_toggle_button_get_active (toggle);

  gtk_cell_area_cell_set (data->area, data->renderer, "align", active, NULL);
}

171 172 173 174 175 176 177 178 179 180
static void
expand_cell_toggled (GtkToggleButton  *toggle,
		     CallbackData     *data)
{
  gboolean active = gtk_toggle_button_get_active (toggle);

  gtk_cell_area_cell_set (data->area, data->renderer, "expand", active, NULL);
}

static void
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
fixed_cell_toggled (GtkToggleButton  *toggle,
		    CallbackData     *data)
{
  gboolean active = gtk_toggle_button_get_active (toggle);

  gtk_cell_area_cell_set (data->area, data->renderer, "fixed-size", active, NULL);
}

enum {
  CNTL_EXPAND,
  CNTL_ALIGN,
  CNTL_FIXED
};

static void
create_control (GtkWidget *box, gint number, gint cntl, CallbackData *data)
197 198
{
  GtkWidget *checkbutton;
199 200
  GCallback  callback = NULL;
  gchar *name = NULL;
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  switch (cntl)
    {
    case CNTL_EXPAND: 
      name = g_strdup_printf ("Expand Cell #%d", number); 
      callback = G_CALLBACK (expand_cell_toggled);
      break;
    case CNTL_ALIGN: 
      name = g_strdup_printf ("Align Cell #%d", number); 
      callback = G_CALLBACK (align_cell_toggled);
      break;
    case CNTL_FIXED: 
      name = g_strdup_printf ("Fix size Cell #%d", number); 
      callback = G_CALLBACK (fixed_cell_toggled);
      break;
    }
217 218 219

  checkbutton = gtk_check_button_new_with_label (name);
  gtk_widget_show (checkbutton);
220
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), cntl == CNTL_FIXED);
221 222
  gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);

223 224
  g_signal_connect (G_OBJECT (checkbutton), "toggled", callback, data);
  g_free (name);
225 226
}

227 228 229 230 231 232
gint
main (gint argc, gchar **argv)
{
  GtkWidget *window;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
233
  GtkWidget *vbox, *hbox, *cntl_vbox;
234 235
  GtkTreeModel *tree_model;
  GtkCellRenderer *renderer;
236
  GtkTreeViewColumn *column;
237 238
  GtkCellArea *area;
  CallbackData callback[4];
239 240 241
  
  gtk_init (&argc, &argv);

242 243 244
  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

245 246
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "GtkTreeView editing sample");
Manish Singh's avatar
Manish Singh committed
247
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
248

249
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
250 251 252
  gtk_widget_show (vbox);
  gtk_container_add (GTK_CONTAINER (window), vbox);

253 254
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_ETCHED_IN);
255 256 257
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), 
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
258 259 260

  tree_model = create_model ();
  tree_view = gtk_tree_view_new_with_model (tree_model);
Anders Carlsson's avatar
Anders Carlsson committed
261
  g_signal_connect (tree_view, "button_press_event", G_CALLBACK (button_press_event), NULL);
262
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
263
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), TRUE);
264

265 266
  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, "String");
267
  area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (column));
268 269

  renderer = gtk_cell_renderer_pixbuf_new ();
270
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
271
  gtk_tree_view_column_set_attributes (column, renderer,
272 273 274
				       "pixbuf", PIXBUF_COLUMN, 
				       "sensitive", IS_SENSITIVE_COLUMN,
				       NULL);
275 276
  callback[0].area = area;
  callback[0].renderer = renderer;
277

278
  renderer = gtk_cell_renderer_text_new ();
279
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
280 281 282
  gtk_tree_view_column_set_attributes (column, renderer,
				       "text", STRING_COLUMN,
				       "editable", IS_EDITABLE_COLUMN,
283
				       "sensitive", IS_SENSITIVE_COLUMN,
284
				       NULL);
285 286
  callback[1].area = area;
  callback[1].renderer = renderer;
Manish Singh's avatar
Manish Singh committed
287
  g_signal_connect (renderer, "edited",
288
		    G_CALLBACK (edited), tree_model);
289 290 291
  g_object_set (renderer,
                "placeholder-text", "Type here",
                NULL);
292

293
  renderer = gtk_cell_renderer_text_new ();
294
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
295 296 297
  gtk_tree_view_column_set_attributes (column, renderer,
		  		       "text", STRING_COLUMN,
				       "editable", IS_EDITABLE_COLUMN,
298
				       "sensitive", IS_SENSITIVE_COLUMN,
299
				       NULL);
300 301
  callback[2].area = area;
  callback[2].renderer = renderer;
Manish Singh's avatar
Manish Singh committed
302
  g_signal_connect (renderer, "edited",
303
		    G_CALLBACK (edited), tree_model);
304 305 306
  g_object_set (renderer,
                "placeholder-text", "Type here too",
                NULL);
307 308

  renderer = gtk_cell_renderer_pixbuf_new ();
309 310 311 312
  g_object_set (renderer,
		"xalign", 0.0,
		NULL);
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
313
  gtk_tree_view_column_set_attributes (column, renderer,
314 315 316
				       "pixbuf", LAST_PIXBUF_COLUMN, 
				       "sensitive", IS_SENSITIVE_COLUMN,
				       NULL);
317 318 319
  callback[3].area = area;
  callback[3].renderer = renderer;

320 321
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);

322
  renderer = gtk_cell_renderer_toggle_new ();
Manish Singh's avatar
Manish Singh committed
323
  g_signal_connect (renderer, "toggled",
324
		    G_CALLBACK (editable_toggled), tree_model);
325
  
326
  g_object_set (renderer,
327 328 329 330 331 332 333
		"xalign", 0.0,
		NULL);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       -1, "Editable",
					       renderer,
					       "active", IS_EDITABLE_COLUMN,
					       NULL);
334

335 336 337 338 339 340 341 342 343 344 345 346 347
  renderer = gtk_cell_renderer_toggle_new ();
  g_signal_connect (renderer, "toggled",
		    G_CALLBACK (sensitive_toggled), tree_model);
  
  g_object_set (renderer,
		"xalign", 0.0,
		NULL);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       -1, "Sensitive",
					       renderer,
					       "active", IS_SENSITIVE_COLUMN,
					       NULL);

348 349 350 351 352 353 354
  renderer = gtk_cell_renderer_progress_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       -1, "Progress",
					       renderer,
					       "value", PROGRESS_COLUMN,
					       NULL);

355 356 357
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
  
  gtk_window_set_default_size (GTK_WINDOW (window),
358 359
			       800, 250);

360
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
361 362 363 364
  gtk_widget_show (hbox);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  /* Alignment controls */
365
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
366 367 368
  gtk_widget_show (cntl_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), cntl_vbox, FALSE, FALSE, 0);

369 370 371 372
  create_control (cntl_vbox, 1, CNTL_ALIGN, &callback[0]);
  create_control (cntl_vbox, 2, CNTL_ALIGN, &callback[1]);
  create_control (cntl_vbox, 3, CNTL_ALIGN, &callback[2]);
  create_control (cntl_vbox, 4, CNTL_ALIGN, &callback[3]);
373 374

  /* Expand controls */
375
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
376 377 378
  gtk_widget_show (cntl_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), cntl_vbox, FALSE, FALSE, 0);

379 380 381 382 383 384
  create_control (cntl_vbox, 1, CNTL_EXPAND, &callback[0]);
  create_control (cntl_vbox, 2, CNTL_EXPAND, &callback[1]);
  create_control (cntl_vbox, 3, CNTL_EXPAND, &callback[2]);
  create_control (cntl_vbox, 4, CNTL_EXPAND, &callback[3]);

  /* Fixed controls */
385
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
386 387 388 389 390 391 392
  gtk_widget_show (cntl_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), cntl_vbox, FALSE, FALSE, 0);

  create_control (cntl_vbox, 1, CNTL_FIXED, &callback[0]);
  create_control (cntl_vbox, 2, CNTL_FIXED, &callback[1]);
  create_control (cntl_vbox, 3, CNTL_FIXED, &callback[2]);
  create_control (cntl_vbox, 4, CNTL_FIXED, &callback[3]);
393

394 395 396 397 398
  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}