treestoretest.c 10.5 KB
Newer Older
1
#include <config.h>
2 3
#include <gtk/gtk.h>
#include <stdlib.h>
4
#include <string.h>
5

6
GtkTreeStore *base_model;
7 8

static void
Jonathan Blandford's avatar
Jonathan Blandford committed
9
selection_changed (GtkTreeSelection *selection,
10
		   GtkWidget        *button)
11
{
12
  if (gtk_tree_selection_get_selected (selection, NULL, NULL))
Jonathan Blandford's avatar
Jonathan Blandford committed
13 14 15
    gtk_widget_set_sensitive (button, TRUE);
  else
    gtk_widget_set_sensitive (button, FALSE);
16 17
}

18 19
static void
node_set (GtkTreeIter *iter)
20 21
{
  static gint i = 0;
22
  gint n;
23 24
  gchar *str;

25
  str = g_strdup_printf ("Row (<span color=\"red\">%d</span>)", i++);
26
  gtk_tree_store_set (base_model, iter, 0, str, -1);
27 28
  g_free (str);

29
  n = g_random_int_range (10000,99999);
30 31 32 33
  if (n < 0)
    n *= -1;
  str = g_strdup_printf ("%d", n);

34 35
  gtk_tree_store_set (base_model, iter, 1, str, -1);
  g_free (str);
36 37 38
}

static void
39
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
40
{
41
  GtkTreeIter selected;
42 43 44 45 46
  GtkTreeModel *model;

  model = gtk_tree_view_get_model (tree_view);

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view),
47
				       NULL,
48 49
				       &selected))
    {
50 51 52 53
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_remove (GTK_TREE_STORE (model), &selected);
	}
54
    }
55 56 57
}

static void
58
iter_insert (GtkWidget *button, GtkTreeView *tree_view)
59 60
{
  GtkWidget *entry;
61 62
  GtkTreeIter iter;
  GtkTreeIter selected;
63
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
64

Manish Singh's avatar
Manish Singh committed
65
  entry = g_object_get_data (G_OBJECT (button), "user_data");
66
  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
67
				       NULL,
68 69
				       &selected))
    {
70
      gtk_tree_store_insert (GTK_TREE_STORE (model),
71 72 73
			     &iter,
			     &selected,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
74 75 76
    }
  else
    {
77
      gtk_tree_store_insert (GTK_TREE_STORE (model),
78 79 80
			     &iter,
			     NULL,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
81 82 83
    }

  node_set (&iter);
84 85
}

86 87 88 89 90 91 92
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
93
  entry = g_object_get_data (G_OBJECT (button), "user_data");
94 95 96 97 98 99 100 101 102 103 104
  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);
    }
}

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

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
113
				       NULL,
114 115
				       &selected))
    {
116
      gtk_tree_store_insert_before (GTK_TREE_STORE (model),
117 118 119
				    &iter,
				    NULL,
				    &selected);
120 121 122
    }
  else
    {
123
      gtk_tree_store_insert_before (GTK_TREE_STORE (model),
124 125 126
				    &iter,
				    NULL,
				    NULL);
127 128 129
    }

  node_set (&iter);
130 131 132
}

static void
133
iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
134
{
135 136
  GtkTreeIter iter;
  GtkTreeIter selected;
137
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
138 139

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
140
				       NULL,
141 142
				       &selected))
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
143 144
      if (GTK_IS_TREE_STORE (model))
	{
145
	  gtk_tree_store_insert_after (GTK_TREE_STORE (model),
Jonathan Blandford's avatar
Jonathan Blandford committed
146 147 148 149 150
				       &iter,
				       NULL,
				       &selected);
	  node_set (&iter);
	}
151 152 153
    }
  else
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
154 155
      if (GTK_IS_TREE_STORE (model))
	{
156
	  gtk_tree_store_insert_after (GTK_TREE_STORE (model),
Jonathan Blandford's avatar
Jonathan Blandford committed
157 158
				       &iter,
				       NULL,
159
				       NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
160 161
	  node_set (&iter);
	}
162
    }
163 164 165
}

static void
166
iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
167
{
168 169
  GtkTreeIter iter;
  GtkTreeIter selected;
170 171
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
172

173
  if (gtk_tree_selection_get_selected (selection, NULL, &selected))
174
    {
175 176 177 178 179 180 181
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  &selected);
	  node_set (&iter);
	}
182 183 184
    }
  else
    {
185 186 187 188 189 190 191
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  NULL);
	  node_set (&iter);
	}
192
    }
193 194 195
}

static void
196
iter_append (GtkWidget *button, GtkTreeView *tree_view)
197
{
198 199
  GtkTreeIter iter;
  GtkTreeIter selected;
200
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
201 202

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
203
				       NULL,
204 205
				       &selected))
    {
206 207
      if (GTK_IS_TREE_STORE (model))
	{
208
	  gtk_tree_store_append (GTK_TREE_STORE (model), &iter, &selected);
209 210
	  node_set (&iter);
	}
211 212 213
    }
  else
    {
214 215
      if (GTK_IS_TREE_STORE (model))
	{
216
	  gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
217 218
	  node_set (&iter);
	}
219
    }
220 221
}

222 223
static void
make_window (gint view_type)
224 225 226 227 228 229 230
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *hbox, *entry;
  GtkWidget *button;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
231
  GtkTreeViewColumn *column;
232
  GtkCellRenderer *cell;
233
  GObject *selection;
234 235 236

  /* Make the Widgets/Objects */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
237 238 239 240 241 242 243 244 245
  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;
    }
246

247 248 249 250
  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);
251
  switch (view_type)
252
    {
253
    case 0:
254
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
255 256 257 258 259
      break;
    case 1:
      {
	GtkTreeModel *sort_model;
	
260
	sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model));
261 262 263
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
      }
      break;
264 265
    default:
      g_assert_not_reached ();
266
      tree_view = NULL; /* Quiet compiler */
267
      break;
268 269
    }

270
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
271
  selection = G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
272
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
273 274 275 276 277 278 279 280

  /* 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
281
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
282 283

  /* buttons */
284
  button = gtk_button_new_with_label ("gtk_tree_store_remove");
285
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
286
  g_signal_connect (selection, "changed",
287
                    G_CALLBACK (selection_changed),
288
                    button);
Manish Singh's avatar
Manish Singh committed
289 290 291
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_remove), 
                    tree_view);
292 293
  gtk_widget_set_sensitive (button, FALSE);

294
  button = gtk_button_new_with_label ("gtk_tree_store_insert");
295 296 297 298 299
  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
300 301 302 303
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert), 
                    tree_view);
304 305 306 307 308 309 310
  
  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
311 312 313 314
  g_object_set_data (G_OBJECT (button), "user_data", entry);
  g_signal_connect (button, "clicked",
		    G_CALLBACK (iter_change),
		    tree_view);
315
  
316
  button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
317
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
318 319 320 321
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert_before), 
                    tree_view);
  g_signal_connect (selection, "changed",
322
                    G_CALLBACK (selection_changed),
323
                    button);
324 325
  gtk_widget_set_sensitive (button, FALSE);

326
  button = gtk_button_new_with_label ("gtk_tree_store_insert_after");
327
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
328 329 330 331
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_insert_after), 
                    tree_view);
  g_signal_connect (selection, "changed",
332
                    G_CALLBACK (selection_changed),
333
                    button);
334 335
  gtk_widget_set_sensitive (button, FALSE);

336
  button = gtk_button_new_with_label ("gtk_tree_store_prepend");
337
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
338 339 340
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_prepend), 
                    tree_view);
341

342
  button = gtk_button_new_with_label ("gtk_tree_store_append");
343
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
Manish Singh's avatar
Manish Singh committed
344 345 346
  g_signal_connect (button, "clicked", 
                    G_CALLBACK (iter_append), 
                    tree_view);
347 348 349

  /* The selected column */
  cell = gtk_cell_renderer_text_new ();
350
  column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "markup", 0, NULL);
351
  gtk_tree_view_column_set_sort_column_id (column, 0);
352
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
353

354 355
  cell = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
356
  gtk_tree_view_column_set_sort_column_id (column, 1);
357
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
358

359
  /* A few to start */
360
  if (view_type == 0)
361
    {
362 363 364 365 366 367
      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));
368
    }
369
  /* Show it all */
370
  gtk_widget_show_all (window);
371 372 373 374 375 376 377
}

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

378
  base_model = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
379 380

  /* FIXME: reverse this */
381
  make_window (0);
382
  make_window (1);
383 384 385 386 387

  gtk_main ();

  return 0;
}
388