treestoretest.c 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* treestoretest.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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
20
#include "config.h"
21 22
#include <gtk/gtk.h>
#include <stdlib.h>
23
#include <string.h>
24

25
GtkTreeStore *base_model;
26
static gint node_count = 0;
27 28

static void
Jonathan Blandford's avatar
Jonathan Blandford committed
29
selection_changed (GtkTreeSelection *selection,
30
		   GtkWidget        *button)
31
{
32
  if (gtk_tree_selection_get_selected (selection, NULL, NULL))
Jonathan Blandford's avatar
Jonathan Blandford committed
33 34 35
    gtk_widget_set_sensitive (button, TRUE);
  else
    gtk_widget_set_sensitive (button, FALSE);
36 37
}

38 39
static void
node_set (GtkTreeIter *iter)
40
{
41
  gint n;
42 43
  gchar *str;

44
  str = g_strdup_printf ("Row (<span color=\"red\">%d</span>)", node_count++);
45
  gtk_tree_store_set (base_model, iter, 0, str, -1);
46 47
  g_free (str);

48
  n = g_random_int_range (10000,99999);
49 50 51 52
  if (n < 0)
    n *= -1;
  str = g_strdup_printf ("%d", n);

53 54
  gtk_tree_store_set (base_model, iter, 1, str, -1);
  g_free (str);
55 56 57
}

static void
58
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
59
{
60
  GtkTreeIter selected;
61 62 63 64 65
  GtkTreeModel *model;

  model = gtk_tree_view_get_model (tree_view);

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view),
66
				       NULL,
67 68
				       &selected))
    {
69 70 71 72
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_remove (GTK_TREE_STORE (model), &selected);
	}
73
    }
74 75 76
}

static void
77
iter_insert (GtkWidget *button, GtkTreeView *tree_view)
78 79
{
  GtkWidget *entry;
80 81
  GtkTreeIter iter;
  GtkTreeIter selected;
82
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
83

Manish Singh's avatar
Manish Singh committed
84
  entry = g_object_get_data (G_OBJECT (button), "user_data");
85
  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
86
				       NULL,
87 88
				       &selected))
    {
89
      gtk_tree_store_insert (GTK_TREE_STORE (model),
90 91 92
			     &iter,
			     &selected,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
93 94 95
    }
  else
    {
96
      gtk_tree_store_insert (GTK_TREE_STORE (model),
97 98 99
			     &iter,
			     NULL,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
100 101 102
    }

  node_set (&iter);
103 104
}

105 106 107 108 109 110 111
static void
iter_change (GtkWidget *button, GtkTreeView *tree_view)
{
  GtkWidget *entry;
  GtkTreeIter selected;
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);

Manish Singh's avatar
Manish Singh committed
112
  entry = g_object_get_data (G_OBJECT (button), "user_data");
113 114 115 116 117 118 119 120 121 122 123
  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
				       NULL, &selected))
    {
      gtk_tree_store_set (GTK_TREE_STORE (model),
			  &selected,
			  1,
			  gtk_entry_get_text (GTK_ENTRY (entry)),
			  -1);
    }
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static void
iter_insert_with_values (GtkWidget *button, GtkTreeView *tree_view)
{
  GtkWidget *entry;
  GtkTreeIter iter;
  GtkTreeIter selected;
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  gchar *str1, *str2;

  entry = g_object_get_data (G_OBJECT (button), "user_data");
  str1 = g_strdup_printf ("Row (<span color=\"red\">%d</span>)", node_count++);
  str2 = g_strdup_printf ("%d", atoi (gtk_entry_get_text (GTK_ENTRY (entry))));

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
				       NULL,
				       &selected))
    {
      gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
					 &iter,
					 &selected,
					 -1,
					 0, str1,
					 1, str2,
					 -1);
    }
  else
    {
      gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
					 &iter,
					 NULL,
					 -1,
					 0, str1,
					 1, str2,
					 -1);
    }

  g_free (str1);
  g_free (str2);
}

164
static void
165
iter_insert_before  (GtkWidget *button, GtkTreeView *tree_view)
166
{
167 168
  GtkTreeIter iter;
  GtkTreeIter selected;
169
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
170 171

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
172
				       NULL,
173 174
				       &selected))
    {
175
      gtk_tree_store_insert_before (GTK_TREE_STORE (model),
176 177 178
				    &iter,
				    NULL,
				    &selected);
179 180 181
    }
  else
    {
182
      gtk_tree_store_insert_before (GTK_TREE_STORE (model),
183 184 185
				    &iter,
				    NULL,
				    NULL);
186 187 188
    }

  node_set (&iter);
189 190 191
}

static void
192
iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
193
{
194 195
  GtkTreeIter iter;
  GtkTreeIter selected;
196
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
197 198

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
199
				       NULL,
200 201
				       &selected))
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
202 203
      if (GTK_IS_TREE_STORE (model))
	{
204
	  gtk_tree_store_insert_after (GTK_TREE_STORE (model),
Jonathan Blandford's avatar
Jonathan Blandford committed
205 206 207 208 209
				       &iter,
				       NULL,
				       &selected);
	  node_set (&iter);
	}
210 211 212
    }
  else
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
213 214
      if (GTK_IS_TREE_STORE (model))
	{
215
	  gtk_tree_store_insert_after (GTK_TREE_STORE (model),
Jonathan Blandford's avatar
Jonathan Blandford committed
216 217
				       &iter,
				       NULL,
218
				       NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
219 220
	  node_set (&iter);
	}
221
    }
222 223 224
}

static void
225
iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
226
{
227 228
  GtkTreeIter iter;
  GtkTreeIter selected;
229 230
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
231

232
  if (gtk_tree_selection_get_selected (selection, NULL, &selected))
233
    {
234 235 236 237 238 239 240
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  &selected);
	  node_set (&iter);
	}
241 242 243
    }
  else
    {
244 245 246 247 248 249 250
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  NULL);
	  node_set (&iter);
	}
251
    }
252 253 254
}

static void
255
iter_append (GtkWidget *button, GtkTreeView *tree_view)
256
{
257 258
  GtkTreeIter iter;
  GtkTreeIter selected;
259
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
260 261

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
262
				       NULL,
263 264
				       &selected))
    {
265 266
      if (GTK_IS_TREE_STORE (model))
	{
267
	  gtk_tree_store_append (GTK_TREE_STORE (model), &iter, &selected);
268 269
	  node_set (&iter);
	}
270 271 272
    }
  else
    {
273 274
      if (GTK_IS_TREE_STORE (model))
	{
275
	  gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
276 277
	  node_set (&iter);
	}
278
    }
279 280
}

281 282
static void
make_window (gint view_type)
283 284 285 286 287 288 289
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *hbox, *entry;
  GtkWidget *button;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
290
  GtkTreeViewColumn *column;
291
  GtkCellRenderer *cell;
292
  GObject *selection;
293 294 295

  /* Make the Widgets/Objects */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
296 297 298 299 300 301 302 303 304
  switch (view_type)
    {
    case 0:
      gtk_window_set_title (GTK_WINDOW (window), "Unsorted list");
      break;
    case 1:
      gtk_window_set_title (GTK_WINDOW (window), "Sorted list");
      break;
    }
305

306 307 308 309
  vbox = gtk_vbox_new (FALSE, 8);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 350);
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
310
  switch (view_type)
311
    {
312
    case 0:
313
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
314 315 316 317 318
      break;
    case 1:
      {
	GtkTreeModel *sort_model;
	
319
	sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model));
320 321 322
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
      }
      break;
323 324
    default:
      g_assert_not_reached ();
325
      tree_view = NULL; /* Quiet compiler */
326
      break;
327 328
    }

329
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
330
  selection = G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
331
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
332 333 334 335 336 337 338 339

  /* Put them together */
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
Manish Singh's avatar
Manish Singh committed
340
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
341 342

  /* buttons */
343
  button = gtk_button_new_with_label ("gtk_tree_store_remove");
344
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
345
  g_signal_connect (selection, "changed",
346
                    G_CALLBACK (selection_changed),
347
                    button);
Manish Singh's avatar
Manish Singh committed
348 349 350
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_remove), 
                    tree_view);
351 352
  gtk_widget_set_sensitive (button, FALSE);

353
  button = gtk_button_new_with_label ("gtk_tree_store_insert");
354 355 356 357 358
  hbox = gtk_hbox_new (FALSE, 8);
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
359 360 361 362
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert), 
                    tree_view);
363 364 365 366 367 368 369
  
  button = gtk_button_new_with_label ("gtk_tree_store_set");
  hbox = gtk_hbox_new (FALSE, 8);
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
370 371 372 373
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked",
		    G_CALLBACK (iter_change),
		    tree_view);
374 375 376 377 378 379 380 381 382 383 384

  button = gtk_button_new_with_label ("gtk_tree_store_insert_with_values");
  hbox = gtk_hbox_new (FALSE, 8);
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked",
		    G_CALLBACK (iter_insert_with_values),
		    tree_view);
385
  
386
  button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
387
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
388 389 390 391
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert_before), 
                    tree_view);
  g_signal_connect (selection, "changed",
392
                    G_CALLBACK (selection_changed),
393
                    button);
394 395
  gtk_widget_set_sensitive (button, FALSE);

396
  button = gtk_button_new_with_label ("gtk_tree_store_insert_after");
397
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
398 399 400 401
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert_after), 
                    tree_view);
  g_signal_connect (selection, "changed",
402
                    G_CALLBACK (selection_changed),
403
                    button);
404 405
  gtk_widget_set_sensitive (button, FALSE);

406
  button = gtk_button_new_with_label ("gtk_tree_store_prepend");
407
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
408 409 410
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_prepend), 
                    tree_view);
411

412
  button = gtk_button_new_with_label ("gtk_tree_store_append");
413
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
414 415 416
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_append), 
                    tree_view);
417 418 419

  /* The selected column */
  cell = gtk_cell_renderer_text_new ();
Alexander Larsson's avatar
Alexander Larsson committed
420
  column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "markup", 0, NULL);
421
  gtk_tree_view_column_set_sort_column_id (column, 0);
422
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
423

424 425
  cell = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
426
  gtk_tree_view_column_set_sort_column_id (column, 1);
427
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
428

429
  /* A few to start */
430
  if (view_type == 0)
431
    {
432 433 434 435 436 437
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
      iter_append (NULL, GTK_TREE_VIEW (tree_view));
438
    }
439
  /* Show it all */
440
  gtk_widget_show_all (window);
441 442 443 444 445 446 447
}

int
main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

448
  base_model = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
449 450

  /* FIXME: reverse this */
451
  make_window (0);
452
  make_window (1);
453 454 455 456 457

  gtk_main ();

  return 0;
}
458