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

5
GtkTreeStore *base_model;
6 7

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

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

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

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

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

static void
38
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
39
{
40
  GtkTreeIter selected;
41 42 43 44 45 46
  GtkTreeIter s_selected;
  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
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
64 65

  entry = gtk_object_get_user_data (GTK_OBJECT (button));
66
  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
67
				       NULL,
68 69
				       &selected))
    {
70 71 72 73
      gtk_tree_store_insert (model,
			     &iter,
			     &selected,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
74 75 76
    }
  else
    {
77 78 79 80
      gtk_tree_store_insert (model,
			     &iter,
			     NULL,
			     atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
81 82 83
    }

  node_set (&iter);
84 85 86
}

static void
87
iter_insert_before  (GtkWidget *button, GtkTreeView *tree_view)
88
{
89 90
  GtkTreeIter iter;
  GtkTreeIter selected;
91
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
92 93

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
94
				       NULL,
95 96
				       &selected))
    {
97 98 99 100
      gtk_tree_store_insert_before (model,
				    &iter,
				    NULL,
				    &selected);
101 102 103
    }
  else
    {
104 105 106 107
      gtk_tree_store_insert_before (model,
				    &iter,
				    NULL,
				    NULL);
108 109 110
    }

  node_set (&iter);
111 112 113
}

static void
114
iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
115
{
116 117
  GtkTreeIter iter;
  GtkTreeIter selected;
118
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
119 120

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
121
				       NULL,
122 123
				       &selected))
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
124 125 126 127 128 129 130 131
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_insert_after (model,
				       &iter,
				       NULL,
				       &selected);
	  node_set (&iter);
	}
132 133 134
    }
  else
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
135 136 137 138 139
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_insert_after (model,
				       &iter,
				       NULL,
140
				       NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
141 142
	  node_set (&iter);
	}
143
    }
144 145 146
}

static void
147
iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
148
{
149 150
  GtkTreeIter iter;
  GtkTreeIter selected;
151 152
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
153

154
  if (gtk_tree_selection_get_selected (selection, NULL, &selected))
155
    {
156 157 158 159 160 161 162
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  &selected);
	  node_set (&iter);
	}
163 164 165
    }
  else
    {
166 167 168 169 170 171 172
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  NULL);
	  node_set (&iter);
	}
173
    }
174 175 176
}

static void
177
iter_append (GtkWidget *button, GtkTreeView *tree_view)
178
{
179 180
  GtkTreeIter iter;
  GtkTreeIter selected;
181
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
182 183

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
184
				       NULL,
185 186
				       &selected))
    {
187 188 189 190 191
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_append (model, &iter, &selected);
	  node_set (&iter);
	}
192 193 194
    }
  else
    {
195 196 197 198 199
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_append (model, &iter, NULL);
	  node_set (&iter);
	}
200
    }
201 202 203
}

static void
204 205 206 207 208 209 210 211 212 213 214 215 216 217
uppercase_value (const GValue *src, GValue *dest, gpointer data)
{
  gchar *str;
  
  g_value_init (dest, G_TYPE_STRING);
  str = g_strdup (g_value_get_string (src));
  if (str)
    g_strup (str);
  g_value_set_string (dest, str);
  g_free (str);
}

static void
make_window (gint view_type)
218 219 220 221 222 223 224
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *hbox, *entry;
  GtkWidget *button;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
225
  GtkTreeViewColumn *column;
226
  GtkCellRenderer *cell;
227
  GObject *selection;
228 229 230

  /* Make the Widgets/Objects */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
231 232 233 234 235 236 237 238 239
  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;
    }
240

241 242 243 244
  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);
245
  switch (view_type)
246
    {
247
    case 0:
248
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
249 250 251 252 253
      break;
    case 1:
      {
	GtkTreeModel *sort_model;
	
254
	sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model));
255 256 257
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
      }
      break;
258 259 260
    default:
      g_assert_not_reached ();
      break;
261 262
    }

263
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
264
  selection = G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
Havoc Pennington's avatar
Havoc Pennington committed
265
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection), GTK_TREE_SELECTION_SINGLE);
266 267 268 269 270 271 272 273 274 275 276

  /* 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);
  gtk_signal_connect (GTK_OBJECT (window), "destroy", gtk_main_quit, NULL);

  /* buttons */
277
  button = gtk_button_new_with_label ("gtk_tree_store_remove");
278
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
279 280 281 282
  g_signal_connect (G_OBJECT (selection),
                    "changed",
                    selection_changed,
                    button);
283
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_remove, tree_view);
284 285
  gtk_widget_set_sensitive (button, FALSE);

286
  button = gtk_button_new_with_label ("gtk_tree_store_insert");
287 288 289 290 291 292
  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);
  gtk_object_set_user_data (GTK_OBJECT (button), entry);
293
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert, tree_view);
294 295

  
296
  button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
297
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
298
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_before, tree_view);
299 300 301 302
  g_signal_connect (G_OBJECT (selection),
                    "changed",
                    selection_changed,
                    button);
303 304
  gtk_widget_set_sensitive (button, FALSE);

305
  button = gtk_button_new_with_label ("gtk_tree_store_insert_after");
306
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
307
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_after, tree_view);
308 309 310 311
  g_signal_connect (G_OBJECT (selection),
                    "changed",
                    selection_changed,
                    button);
312 313
  gtk_widget_set_sensitive (button, FALSE);

314
  button = gtk_button_new_with_label ("gtk_tree_store_prepend");
315
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
316
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_prepend, tree_view);
317

318
  button = gtk_button_new_with_label ("gtk_tree_store_append");
319
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
320
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_append, tree_view);
321 322 323

  /* The selected column */
  cell = gtk_cell_renderer_text_new ();
324
  column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "markup", 0, NULL);
325
  gtk_tree_view_column_set_sort_column_id (column, 0);
326
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
327 328
  g_object_unref (G_OBJECT (cell));
  g_object_unref (G_OBJECT (column));
329

330 331
  cell = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
332
  gtk_tree_view_column_set_sort_column_id (column, 1);
333
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
334 335
  g_object_unref (G_OBJECT (cell));
  g_object_unref (G_OBJECT (column));
336

337
  /* A few to start */
338
  if (view_type == 0)
339
    {
340 341 342 343 344 345
      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));
346
    }
347
  /* Show it all */
348
  gtk_widget_show_all (window);
349 350 351 352 353 354 355
}

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

356 357 358
  base_model = gtk_tree_store_new_with_types (2, G_TYPE_STRING, G_TYPE_STRING);

  /* FIXME: reverse this */
359
  make_window (0);
360
  make_window (1);
361 362 363 364 365

  gtk_main ();

  return 0;
}
366