treestoretest.c 8.52 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

22 23 24
  gchar *str;

  str = g_strdup_printf ("FOO: %d", i++);
25
  gtk_tree_store_set (base_model, iter, 0, str, -1);
26 27
  g_free (str);

28 29 30
  str = g_strdup_printf ("%d", g_random_int ());
  gtk_tree_store_set (base_model, iter, 1, str, -1);
  g_free (str);
31 32 33
}

static void
34
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
35
{
36
  GtkTreeIter selected;
37 38 39 40 41 42
  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),
43
				       NULL,
44 45
				       &selected))
    {
46 47 48 49
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_remove (GTK_TREE_STORE (model), &selected);
	}
50
    }
51 52 53
}

static void
54
iter_insert (GtkWidget *button, GtkTreeView *tree_view)
55 56
{
  GtkWidget *entry;
57 58
  GtkTreeIter iter;
  GtkTreeIter selected;
59
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
60 61

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

  node_set (&iter);
80 81 82
}

static void
83
iter_insert_before  (GtkWidget *button, GtkTreeView *tree_view)
84
{
85 86
  GtkTreeIter iter;
  GtkTreeIter selected;
87
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
88 89

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
90
				       NULL,
91 92
				       &selected))
    {
93 94 95 96
      gtk_tree_store_insert_before (model,
				    &iter,
				    NULL,
				    &selected);
97 98 99
    }
  else
    {
100 101 102 103
      gtk_tree_store_insert_before (model,
				    &iter,
				    NULL,
				    NULL);
104 105 106
    }

  node_set (&iter);
107 108 109
}

static void
110
iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
111
{
112 113
  GtkTreeIter iter;
  GtkTreeIter selected;
114
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
115 116

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
117
				       NULL,
118 119
				       &selected))
    {
120 121 122 123
      gtk_tree_store_insert_after (model,
				   &iter,
				   NULL,
				   &selected);
124 125 126
    }
  else
    {
127 128 129 130
      gtk_tree_store_insert_after (model,
				   &iter,
				   NULL,
				   &selected);
131 132 133
    }

  node_set (&iter);
134 135 136
}

static void
137
iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
138
{
139 140
  GtkTreeIter iter;
  GtkTreeIter selected;
141 142
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
143

144
  if (gtk_tree_selection_get_selected (selection, NULL, &selected))
145
    {
146 147 148 149 150 151 152
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  &selected);
	  node_set (&iter);
	}
153 154 155
    }
  else
    {
156 157 158 159 160 161 162
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_prepend (GTK_TREE_STORE (model),
				  &iter,
				  NULL);
	  node_set (&iter);
	}
163
    }
164 165 166
}

static void
167
iter_append (GtkWidget *button, GtkTreeView *tree_view)
168
{
169 170
  GtkTreeIter iter;
  GtkTreeIter selected;
171
  GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
172 173

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
174
				       NULL,
175 176
				       &selected))
    {
177 178 179 180 181
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_append (model, &iter, &selected);
	  node_set (&iter);
	}
182 183 184
    }
  else
    {
185 186 187 188 189
      if (GTK_IS_TREE_STORE (model))
	{
	  gtk_tree_store_append (model, &iter, NULL);
	  node_set (&iter);
	}
190
    }
191 192 193
}

static void
194
make_window (gboolean use_sort)
195 196 197 198 199 200 201
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *hbox, *entry;
  GtkWidget *button;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
202
  GtkTreeViewColumn *column;
203 204 205 206 207
  GtkCellRenderer *cell;
  GtkObject *selection;

  /* Make the Widgets/Objects */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
208 209 210 211 212
  if (use_sort)
    gtk_window_set_title (GTK_WINDOW (window), "Sorted list");
  else
    gtk_window_set_title (GTK_WINDOW (window), "Unsorted list");

213 214 215 216
  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);
217 218 219 220 221 222 223 224 225 226 227 228 229
  if (use_sort)
    {
      GtkTreeModel *sort_model;

      sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model),
						       NULL, 0);
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
    }
  else
    {
      tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
    }

230 231 232 233 234 235 236 237 238 239 240 241 242
  selection = GTK_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
  gtk_tree_selection_set_type (GTK_TREE_SELECTION (selection), GTK_TREE_SELECTION_SINGLE);

  /* 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 */
243
  button = gtk_button_new_with_label ("gtk_tree_store_remove");
244 245
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (selection),
Jonathan Blandford's avatar
Jonathan Blandford committed
246 247 248
		      "selection_changed",
		      selection_changed,
		      button);
249
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_remove, tree_view);
250 251
  gtk_widget_set_sensitive (button, FALSE);

252
  button = gtk_button_new_with_label ("gtk_tree_store_insert");
253 254 255 256 257 258
  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);
259
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert, tree_view);
260 261

  
262
  button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
263
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
264
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_before, tree_view);
Jonathan Blandford's avatar
Jonathan Blandford committed
265 266 267 268
  gtk_signal_connect (GTK_OBJECT (selection),
		      "selection_changed",
		      selection_changed,
		      button);
269 270
  gtk_widget_set_sensitive (button, FALSE);

271
  button = gtk_button_new_with_label ("gtk_tree_store_insert_after");
272
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
273
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_after, tree_view);
Jonathan Blandford's avatar
Jonathan Blandford committed
274 275 276 277
  gtk_signal_connect (GTK_OBJECT (selection),
		      "selection_changed",
		      selection_changed,
		      button);
278 279
  gtk_widget_set_sensitive (button, FALSE);

280
  button = gtk_button_new_with_label ("gtk_tree_store_prepend");
281
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
282
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_prepend, tree_view);
283

284
  button = gtk_button_new_with_label ("gtk_tree_store_append");
285
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
286
  gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_append, tree_view);
287 288 289

  /* The selected column */
  cell = gtk_cell_renderer_text_new ();
290
  column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "text", 0, NULL);
291
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
292

293 294 295
  cell = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
296

297 298 299 300 301 302 303
  /* A few to start */
  if (!use_sort)
    {
      iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
      iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
      iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
    }
304 305 306 307 308 309 310 311 312
  /* Show it all */
  gtk_widget_show_all (window);
}

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

313 314 315 316 317
  base_model = gtk_tree_store_new_with_types (2, G_TYPE_STRING, G_TYPE_STRING);

  /* FIXME: reverse this */
  make_window (FALSE);
  make_window (TRUE);
318 319 320 321 322

  gtk_main ();

  return 0;
}