treestoretest.c 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
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
#include "config.h"
19 20
#include <gtk/gtk.h>
#include <stdlib.h>
21
#include <string.h>
22

23
GtkTreeStore *base_model;
24
static gint node_count = 0;
25 26

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

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

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

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

51 52
  gtk_tree_store_set (base_model, iter, 1, str, -1);
  g_free (str);
53 54 55
}

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

  model = gtk_tree_view_get_model (tree_view);

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

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

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

  node_set (&iter);
101 102
}

103 104 105 106 107 108 109
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
110
  entry = g_object_get_data (G_OBJECT (button), "user_data");
111 112 113 114 115 116 117 118 119 120 121
  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);
    }
}

122 123 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
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);
}

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

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

  node_set (&iter);
187 188 189
}

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

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

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

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

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

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

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

  /* Make the Widgets/Objects */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
294 295 296 297 298 299 300 301 302
  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;
    }
303

304
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
305 306 307
  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);
308
  switch (view_type)
309
    {
310
    case 0:
311
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
312 313 314 315 316
      break;
    case 1:
      {
	GtkTreeModel *sort_model;
	
317
	sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model));
318 319 320
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
      }
      break;
321 322
    default:
      g_assert_not_reached ();
323
      tree_view = NULL; /* Quiet compiler */
324
      break;
325 326
    }

327
  selection = G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
328
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
329 330 331 332 333 334 335 336

  /* 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
337
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
338 339

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

350
  button = gtk_button_new_with_label ("gtk_tree_store_insert");
351
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
352 353 354 355
  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
356 357 358 359
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert), 
                    tree_view);
360 361
  
  button = gtk_button_new_with_label ("gtk_tree_store_set");
362
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
363 364 365 366
  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
367 368 369 370
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked",
		    G_CALLBACK (iter_change),
		    tree_view);
371 372

  button = gtk_button_new_with_label ("gtk_tree_store_insert_with_values");
373
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
374 375 376 377 378 379 380 381
  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);
382
  
383
  button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
384
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
385 386 387 388
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert_before), 
                    tree_view);
  g_signal_connect (selection, "changed",
389
                    G_CALLBACK (selection_changed),
390
                    button);
391 392
  gtk_widget_set_sensitive (button, FALSE);

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

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

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

  /* The selected column */
  cell = gtk_cell_renderer_text_new ();
417
  column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "markup", 0, NULL);
418
  gtk_tree_view_column_set_sort_column_id (column, 0);
419
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
420

421 422
  cell = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
423
  gtk_tree_view_column_set_sort_column_id (column, 1);
424
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
425

426
  /* A few to start */
427
  if (view_type == 0)
428
    {
429 430 431 432 433 434
      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));
435
    }
436
  /* Show it all */
437
  gtk_widget_show_all (window);
438 439 440 441 442 443 444
}

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

445
  base_model = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
446 447

  /* FIXME: reverse this */
448
  make_window (0);
449
  make_window (1);
450 451 452 453 454

  gtk_main ();

  return 0;
}
455