testtreeedit.c 11.7 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 34
  ICON_NAME_COLUMN,
  LAST_ICON_NAME_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 57

  model = gtk_tree_store_new (NUM_COLUMNS,
58
			      G_TYPE_STRING,
59
			      G_TYPE_BOOLEAN,
60
			      G_TYPE_BOOLEAN,
61 62
			      G_TYPE_STRING,
			      G_TYPE_STRING,
63
			      G_TYPE_INT);
64 65 66

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

69
      gtk_tree_store_set (model, &iter,
70 71
			  STRING_COLUMN, model_strings[i].string,
			  IS_EDITABLE_COLUMN, model_strings[i].is_editable,
72
			  IS_SENSITIVE_COLUMN, model_strings[i].is_sensitive,
73 74
			  ICON_NAME_COLUMN, "document-new",
			  LAST_ICON_NAME_COLUMN, "edit-delete",
75
			  PROGRESS_COLUMN, model_strings[i].progress,
76 77 78 79 80 81 82
			  -1);
    }
  
  return GTK_TREE_MODEL (model);
}

static void
83 84 85
editable_toggled (GtkCellRendererToggle *cell,
		  gchar                 *path_string,
		  gpointer               data)
86 87 88 89 90 91 92 93 94 95
{
  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;
96
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter, IS_EDITABLE_COLUMN, value, -1);
97 98 99 100

  gtk_tree_path_free (path);
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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);
}

120 121 122 123 124 125 126 127 128 129 130
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);
131
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter, STRING_COLUMN, new_text, -1);
132 133 134 135

  gtk_tree_path_free (path);
}

Anders Carlsson's avatar
Anders Carlsson committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
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;
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163
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);
}

164 165 166 167 168 169 170 171 172 173
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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
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)
190 191
{
  GtkWidget *checkbutton;
192 193
  GCallback  callback = NULL;
  gchar *name = NULL;
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  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;
    }
210 211 212

  checkbutton = gtk_check_button_new_with_label (name);
  gtk_widget_show (checkbutton);
213
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), cntl == CNTL_FIXED);
214 215
  gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);

216 217
  g_signal_connect (G_OBJECT (checkbutton), "toggled", callback, data);
  g_free (name);
218 219
}

220 221 222 223 224 225
gint
main (gint argc, gchar **argv)
{
  GtkWidget *window;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
226
  GtkWidget *vbox, *hbox, *cntl_vbox;
227 228
  GtkTreeModel *tree_model;
  GtkCellRenderer *renderer;
229
  GtkTreeViewColumn *column;
230 231
  GtkCellArea *area;
  CallbackData callback[4];
232 233 234
  
  gtk_init (&argc, &argv);

235 236 237
  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

238 239
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "GtkTreeView editing sample");
Manish Singh's avatar
Manish Singh committed
240
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
241

242
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
243 244 245
  gtk_widget_show (vbox);
  gtk_container_add (GTK_CONTAINER (window), vbox);

246 247
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_ETCHED_IN);
248 249 250
  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);
251 252 253

  tree_model = create_model ();
  tree_view = gtk_tree_view_new_with_model (tree_model);
Anders Carlsson's avatar
Anders Carlsson committed
254
  g_signal_connect (tree_view, "button_press_event", G_CALLBACK (button_press_event), NULL);
255
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), TRUE);
256

257 258
  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, "String");
259
  area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (column));
260 261

  renderer = gtk_cell_renderer_pixbuf_new ();
262
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
263
  gtk_tree_view_column_set_attributes (column, renderer,
264
				       "icon-name", ICON_NAME_COLUMN, 
265 266
				       "sensitive", IS_SENSITIVE_COLUMN,
				       NULL);
267 268
  callback[0].area = area;
  callback[0].renderer = renderer;
269

270
  renderer = gtk_cell_renderer_text_new ();
271
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
272 273 274
  gtk_tree_view_column_set_attributes (column, renderer,
				       "text", STRING_COLUMN,
				       "editable", IS_EDITABLE_COLUMN,
275
				       "sensitive", IS_SENSITIVE_COLUMN,
276
				       NULL);
277 278
  callback[1].area = area;
  callback[1].renderer = renderer;
Manish Singh's avatar
Manish Singh committed
279
  g_signal_connect (renderer, "edited",
280
		    G_CALLBACK (edited), tree_model);
281 282 283
  g_object_set (renderer,
                "placeholder-text", "Type here",
                NULL);
284

285
  renderer = gtk_cell_renderer_text_new ();
286
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
287 288 289
  gtk_tree_view_column_set_attributes (column, renderer,
		  		       "text", STRING_COLUMN,
				       "editable", IS_EDITABLE_COLUMN,
290
				       "sensitive", IS_SENSITIVE_COLUMN,
291
				       NULL);
292 293
  callback[2].area = area;
  callback[2].renderer = renderer;
Manish Singh's avatar
Manish Singh committed
294
  g_signal_connect (renderer, "edited",
295
		    G_CALLBACK (edited), tree_model);
296 297 298
  g_object_set (renderer,
                "placeholder-text", "Type here too",
                NULL);
299 300

  renderer = gtk_cell_renderer_pixbuf_new ();
301 302 303 304
  g_object_set (renderer,
		"xalign", 0.0,
		NULL);
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
305
  gtk_tree_view_column_set_attributes (column, renderer,
306
				       "icon-name", LAST_ICON_NAME_COLUMN, 
307 308
				       "sensitive", IS_SENSITIVE_COLUMN,
				       NULL);
309 310 311
  callback[3].area = area;
  callback[3].renderer = renderer;

312 313
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);

314
  renderer = gtk_cell_renderer_toggle_new ();
Manish Singh's avatar
Manish Singh committed
315
  g_signal_connect (renderer, "toggled",
316
		    G_CALLBACK (editable_toggled), tree_model);
317
  
318
  g_object_set (renderer,
319 320 321 322 323 324 325
		"xalign", 0.0,
		NULL);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       -1, "Editable",
					       renderer,
					       "active", IS_EDITABLE_COLUMN,
					       NULL);
326

327 328 329 330 331 332 333 334 335 336 337 338 339
  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);

340 341 342 343 344 345 346
  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);

347 348 349
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
  
  gtk_window_set_default_size (GTK_WINDOW (window),
350 351
			       800, 250);

352
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
353 354 355 356
  gtk_widget_show (hbox);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  /* Alignment controls */
357
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
358 359 360
  gtk_widget_show (cntl_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), cntl_vbox, FALSE, FALSE, 0);

361 362 363 364
  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]);
365 366

  /* Expand controls */
367
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
368 369 370
  gtk_widget_show (cntl_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), cntl_vbox, FALSE, FALSE, 0);

371 372 373 374 375 376
  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 */
377
  cntl_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
378 379 380 381 382 383 384
  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]);
385

386 387 388 389 390
  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}