gtktreestore.c 77.3 KB
Newer Older
1
/* gtktreestore.c
2
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */

Manish Singh's avatar
Manish Singh committed
20 21
#include <string.h>
#include <gobject/gvaluecollector.h>
22 23 24
#include "gtktreemodel.h"
#include "gtktreestore.h"
#include "gtktreedatalist.h"
25
#include "gtktreednd.h"
26 27

#define G_NODE(node) ((GNode *)node)
28
#define GTK_TREE_STORE_IS_SORTED(tree) (GTK_TREE_STORE (tree)->sort_column_id != -2)
29
#define VALID_ITER(iter, tree_store) (iter!= NULL && iter->user_data != NULL && tree_store->stamp == iter->stamp)
30

31 32 33
static void         gtk_tree_store_init            (GtkTreeStore      *tree_store);
static void         gtk_tree_store_class_init      (GtkTreeStoreClass *tree_store_class);
static void         gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
34 35
static void         gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
static void         gtk_tree_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
36
static void         gtk_tree_store_sortable_init   (GtkTreeSortableIface   *iface);
37
static void         gtk_tree_store_finalize        (GObject           *object);
38
static GtkTreeModelFlags gtk_tree_store_get_flags  (GtkTreeModel      *tree_model);
39
static gint         gtk_tree_store_get_n_columns   (GtkTreeModel      *tree_model);
40 41
static GType        gtk_tree_store_get_column_type (GtkTreeModel      *tree_model,
						    gint               index);
42 43 44
static gboolean     gtk_tree_store_get_iter        (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
						    GtkTreePath       *path);
45 46
static GtkTreePath *gtk_tree_store_get_path        (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
47
static void         gtk_tree_store_get_value       (GtkTreeModel      *tree_model,
48 49 50 51 52 53 54 55 56 57 58 59 60 61
						    GtkTreeIter       *iter,
						    gint               column,
						    GValue            *value);
static gboolean     gtk_tree_store_iter_next       (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gboolean     gtk_tree_store_iter_children   (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
						    GtkTreeIter       *parent);
static gboolean     gtk_tree_store_iter_has_child  (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gint         gtk_tree_store_iter_n_children (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gboolean     gtk_tree_store_iter_nth_child  (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
62
						    GtkTreeIter       *parent,
63 64 65
						    gint               n);
static gboolean     gtk_tree_store_iter_parent     (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
66
						    GtkTreeIter       *child);
67 68


69 70 71 72 73 74 75
static void gtk_tree_store_set_n_columns   (GtkTreeStore *tree_store,
					    gint          n_columns);
static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
					    gint          column,
					    GType         type);


76
/* DND interfaces */
77
static gboolean gtk_tree_store_drag_data_delete   (GtkTreeDragSource *drag_source,
78
						   GtkTreePath       *path);
79
static gboolean gtk_tree_store_drag_data_get      (GtkTreeDragSource *drag_source,
80 81
						   GtkTreePath       *path,
						   GtkSelectionData  *selection_data);
82
static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
83 84
						   GtkTreePath       *dest,
						   GtkSelectionData  *selection_data);
85
static gboolean gtk_tree_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
86 87
						   GtkTreePath       *dest_path,
						   GtkSelectionData  *selection_data);
88 89 90

/* Sortable Interfaces */

91 92 93 94 95 96
static void     gtk_tree_store_sort                    (GtkTreeStore           *tree_store);
static void     gtk_tree_store_sort_iter_changed       (GtkTreeStore           *tree_store,
							GtkTreeIter            *iter,
							gint                    column);
static gboolean gtk_tree_store_get_sort_column_id      (GtkTreeSortable        *sortable,
							gint                   *sort_column_id,
97
							GtkSortType            *order);
98 99
static void     gtk_tree_store_set_sort_column_id      (GtkTreeSortable        *sortable,
							gint                    sort_column_id,
100
							GtkSortType             order);
101
static void     gtk_tree_store_set_sort_func           (GtkTreeSortable        *sortable,
102 103 104 105
							gint                    sort_column_id,
							GtkTreeIterCompareFunc  func,
							gpointer                data,
							GtkDestroyNotify        destroy);
106 107 108 109 110
static void     gtk_tree_store_set_default_sort_func   (GtkTreeSortable        *sortable,
							GtkTreeIterCompareFunc  func,
							gpointer                data,
							GtkDestroyNotify        destroy);
static gboolean gtk_tree_store_has_default_sort_func   (GtkTreeSortable        *sortable);
111

112 113
static void     validate_gnode                         (GNode *node);

114 115 116 117 118
static void     gtk_tree_store_move                    (GtkTreeStore           *tree_store,
                                                        GtkTreeIter            *iter,
                                                        GtkTreeIter            *position,
                                                        gboolean                before);

119 120 121

static GObjectClass *parent_class = NULL;

122 123 124 125 126 127 128

static inline void
validate_tree (GtkTreeStore *tree_store)
{
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
      g_assert (G_NODE (tree_store->root)->parent == NULL);
129

130 131 132 133
      validate_gnode (G_NODE (tree_store->root));
    }
}

Manish Singh's avatar
Manish Singh committed
134
GType
135 136
gtk_tree_store_get_type (void)
{
137
  static GType tree_store_type = 0;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  if (!tree_store_type)
    {
      static const GTypeInfo tree_store_info =
      {
        sizeof (GtkTreeStoreClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_tree_store_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeStore),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_tree_store_init
      };

154 155 156 157 158 159 160
      static const GInterfaceInfo tree_model_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_tree_model_init,
	NULL,
	NULL
      };

161 162 163 164 165 166 167 168 169 170 171 172 173
      static const GInterfaceInfo drag_source_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_drag_source_init,
	NULL,
	NULL
      };

      static const GInterfaceInfo drag_dest_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_drag_dest_init,
	NULL,
	NULL
      };
174 175 176 177 178 179 180 181

      static const GInterfaceInfo sortable_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_sortable_init,
	NULL,
	NULL
      };

Manish Singh's avatar
Manish Singh committed
182 183
      tree_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeStore",
						&tree_store_info, 0);
184

185 186 187
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_MODEL,
				   &tree_model_info);
188 189 190 191 192 193
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_DRAG_SOURCE,
				   &drag_source_info);
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_DRAG_DEST,
				   &drag_dest_info);
194 195 196
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_SORTABLE,
				   &sortable_info);
197

198 199 200 201 202 203
    }

  return tree_store_type;
}

static void
204
gtk_tree_store_class_init (GtkTreeStoreClass *class)
205
{
206 207
  GObjectClass *object_class;

208 209
  parent_class = g_type_class_peek_parent (class);
  object_class = (GObjectClass *) class;
210

211
  object_class->finalize = gtk_tree_store_finalize;
212 213 214 215 216
}

static void
gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
{
217
  iface->get_flags = gtk_tree_store_get_flags;
218
  iface->get_n_columns = gtk_tree_store_get_n_columns;
219
  iface->get_column_type = gtk_tree_store_get_column_type;
220
  iface->get_iter = gtk_tree_store_get_iter;
221
  iface->get_path = gtk_tree_store_get_path;
222
  iface->get_value = gtk_tree_store_get_value;
223 224 225 226 227 228
  iface->iter_next = gtk_tree_store_iter_next;
  iface->iter_children = gtk_tree_store_iter_children;
  iface->iter_has_child = gtk_tree_store_iter_has_child;
  iface->iter_n_children = gtk_tree_store_iter_n_children;
  iface->iter_nth_child = gtk_tree_store_iter_nth_child;
  iface->iter_parent = gtk_tree_store_iter_parent;
229 230
}

231 232 233 234 235 236 237 238
static void
gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
{
  iface->drag_data_delete = gtk_tree_store_drag_data_delete;
  iface->drag_data_get = gtk_tree_store_drag_data_get;
}

static void
239
gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
240 241 242 243 244
{
  iface->drag_data_received = gtk_tree_store_drag_data_received;
  iface->row_drop_possible = gtk_tree_store_row_drop_possible;
}

245 246 247
static void
gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
{
248 249
  iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
  iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
250
  iface->set_sort_func = gtk_tree_store_set_sort_func;
251 252
  iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
  iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
253 254
}

255 256 257
static void
gtk_tree_store_init (GtkTreeStore *tree_store)
{
258
  tree_store->root = g_node_new (NULL);
259 260
  /* While the odds are against us getting 0...
   */
261 262 263 264
  do
    {
      tree_store->stamp = g_random_int ();
    }
265
  while (tree_store->stamp == 0);
266

267
  tree_store->sort_list = NULL;
268
  tree_store->sort_column_id = -2;
269
  tree_store->columns_dirty = FALSE;
270 271
}

272 273 274 275 276 277
/**
 * gtk_tree_store_new:
 * @n_columns: number of columns in the tree store
 * @Varargs: all #GType types for the columns, from first to last
 *
 * Creates a new tree store as with @n_columns columns each of the types passed
278 279 280
 * in.  As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
 * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
281 282 283
 *
 * Return value: a new #GtkTreeStore
 **/
284
GtkTreeStore *
285
gtk_tree_store_new (gint n_columns,
286
			       ...)
287
{
288
  GtkTreeStore *retval;
289 290 291 292 293
  va_list args;
  gint i;

  g_return_val_if_fail (n_columns > 0, NULL);

Manish Singh's avatar
Manish Singh committed
294
  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
295
  gtk_tree_store_set_n_columns (retval, n_columns);
296 297

  va_start (args, n_columns);
298

299
  for (i = 0; i < n_columns; i++)
300 301 302 303
    {
      GType type = va_arg (args, GType);
      if (! _gtk_tree_data_list_check_type (type))
	{
Manish Singh's avatar
Manish Singh committed
304 305 306
	  g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
		     G_STRLOC, g_type_name (type));
	  g_object_unref (retval);
307 308 309 310
	  return NULL;
	}
      gtk_tree_store_set_column_type (retval, i, type);
    }
311 312 313 314
  va_end (args);

  return retval;
}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/**
 * gtk_tree_store_newv:
 * @n_columns: number of columns in the tree store
 * @types: an array of #GType types for the columns, from first to last
 *
 * Non vararg creation function.  Used primarily by language bindings.
 *
 * Return value: a new #GtkTreeStore
 **/
GtkTreeStore *
gtk_tree_store_newv (gint   n_columns,
		     GType *types)
{
  GtkTreeStore *retval;
  gint i;

  g_return_val_if_fail (n_columns > 0, NULL);

Manish Singh's avatar
Manish Singh committed
333
  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
334 335 336 337 338 339
  gtk_tree_store_set_n_columns (retval, n_columns);

   for (i = 0; i < n_columns; i++)
    {
      if (! _gtk_tree_data_list_check_type (types[i]))
	{
Manish Singh's avatar
Manish Singh committed
340 341 342
	  g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
		     G_STRLOC, g_type_name (types[i]));
	  g_object_unref (retval);
343 344 345 346 347 348 349
	  return NULL;
	}
      gtk_tree_store_set_column_type (retval, i, types[i]);
    }

  return retval;
}
350

351 352 353 354 355

/**
 * gtk_tree_store_set_column_types:
 * @tree_store: A #GtkTreeStore
 * @n_columns: Number of columns for the tree store
Matthias Clasen's avatar
Matthias Clasen committed
356
 * @types: An array of #GType types, one for each column
357
 * 
Matthias Clasen's avatar
Matthias Clasen committed
358 359 360 361
 * This function is meant primarily for #GObjects that inherit from 
 * #GtkTreeStore, and should only be used when constructing a new 
 * #GtkTreeStore.  It will not function after a row has been added, 
 * or a method on the #GtkTreeModel interface is called.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
 **/
void
gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
				 gint          n_columns,
				 GType        *types)
{
  gint i;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (tree_store->columns_dirty == 0);

  gtk_tree_store_set_n_columns (tree_store, n_columns);
   for (i = 0; i < n_columns; i++)
    {
      if (! _gtk_tree_data_list_check_type (types[i]))
	{
	  g_warning ("%s: Invalid type %s passed to gtk_tree_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
	  continue;
	}
      gtk_tree_store_set_column_type (tree_store, i, types[i]);
    }
}

385
static void
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
			      gint          n_columns)
{
  GType *new_columns;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));

  if (tree_store->n_columns == n_columns)
    return;

  new_columns = g_new0 (GType, n_columns);
  if (tree_store->column_headers)
    {
      /* copy the old header orders over */
      if (n_columns >= tree_store->n_columns)
	memcpy (new_columns, tree_store->column_headers, tree_store->n_columns * sizeof (gchar *));
      else
	memcpy (new_columns, tree_store->column_headers, n_columns * sizeof (GType));

      g_free (tree_store->column_headers);
    }

408 409 410 411 412
  if (tree_store->sort_list)
    _gtk_tree_data_list_header_free (tree_store->sort_list);

  tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);

413 414 415 416
  tree_store->column_headers = new_columns;
  tree_store->n_columns = n_columns;
}

417 418 419 420 421
/**
 * gtk_tree_store_set_column_type:
 * @tree_store: a #GtkTreeStore
 * @column: column number
 * @type: type of the data to be stored in @column
422
 *
423 424 425 426
 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
 * subclasses of those types such as %GDK_TYPE_PIXBUF.
427
 *
428
 **/
429
static void
430 431 432 433 434 435
gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
				gint          column,
				GType         type)
{
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (column >=0 && column < tree_store->n_columns);
436 437 438 439 440
  if (!_gtk_tree_data_list_check_type (type))
    {
      g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
      return;
    }
441 442 443
  tree_store->column_headers[column] = type;
}

444
static gboolean
445 446
node_free (GNode *node, gpointer data)
{
447 448 449 450
  if (node->data)
    _gtk_tree_data_list_free (node->data, (GType*)data);
  node->data = NULL;

451
  return FALSE;
452 453 454 455 456 457 458
}

static void
gtk_tree_store_finalize (GObject *object)
{
  GtkTreeStore *tree_store = GTK_TREE_STORE (object);

459 460
  g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
		   node_free, tree_store->column_headers);
461
  g_node_destroy (tree_store->root);
462 463 464 465 466
  _gtk_tree_data_list_header_free (tree_store->sort_list);
  g_free (tree_store->column_headers);

  if (tree_store->default_sort_destroy)
    {
467 468
      GtkDestroyNotify d = tree_store->default_sort_destroy;

469
      tree_store->default_sort_destroy = NULL;
470
      d (tree_store->default_sort_data);
471 472
      tree_store->default_sort_data = NULL;
    }
473

474
  /* must chain up */
475
  (* parent_class->finalize) (object);
476 477
}

478 479
/* fulfill the GtkTreeModel requirements */
/* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node.  However,
480
 * it is not visible to the tree or to the user., and the path "0" refers to the
481 482
 * first child of GtkTreeStore::root.
 */
483 484


485
static GtkTreeModelFlags
486 487 488 489 490 491 492
gtk_tree_store_get_flags (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);

  return GTK_TREE_MODEL_ITERS_PERSIST;
}

493 494 495
static gint
gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
{
496 497
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;

498 499
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);

500 501 502
  tree_store->columns_dirty = TRUE;

  return tree_store->n_columns;
503 504
}

505 506 507 508
static GType
gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
				gint          index)
{
509 510
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;

511 512 513 514
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), G_TYPE_INVALID);
  g_return_val_if_fail (index < GTK_TREE_STORE (tree_model)->n_columns &&
			index >= 0, G_TYPE_INVALID);

515 516 517
  tree_store->columns_dirty = TRUE;

  return tree_store->column_headers[index];
518 519
}

520 521 522 523 524 525 526 527 528 529 530
static gboolean
gtk_tree_store_get_iter (GtkTreeModel *tree_model,
			 GtkTreeIter  *iter,
			 GtkTreePath  *path)
{
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
  GtkTreeIter parent;
  gint *indices;
  gint depth, i;

  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
531 532 533

  tree_store->columns_dirty = TRUE;

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  indices = gtk_tree_path_get_indices (path);
  depth = gtk_tree_path_get_depth (path);

  g_return_val_if_fail (depth > 0, FALSE);

  parent.stamp = tree_store->stamp;
  parent.user_data = tree_store->root;

  if (! gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[0]))
    return FALSE;

  for (i = 1; i < depth; i++)
    {
      parent = *iter;
      if (! gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
	return FALSE;
    }

  return TRUE;
}

555 556
static GtkTreePath *
gtk_tree_store_get_path (GtkTreeModel *tree_model,
557
			 GtkTreeIter  *iter)
558 559 560 561
{
  GtkTreePath *retval;
  GNode *tmp_node;
  gint i = 0;
562

563 564
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), NULL);
  g_return_val_if_fail (iter != NULL, NULL);
565
  g_return_val_if_fail (iter->user_data != NULL, NULL);
566
  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, NULL);
567

568
  validate_tree ((GtkTreeStore*)tree_model);
569

570 571 572
  if (G_NODE (iter->user_data)->parent == NULL &&
      G_NODE (iter->user_data) == GTK_TREE_STORE (tree_model)->root)
    return gtk_tree_path_new ();
573
  g_assert (G_NODE (iter->user_data)->parent != NULL);
574

Havoc Pennington's avatar
Havoc Pennington committed
575
  if (G_NODE (iter->user_data)->parent == G_NODE (GTK_TREE_STORE (tree_model)->root))
576 577 578 579 580 581
    {
      retval = gtk_tree_path_new ();
      tmp_node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
    }
  else
    {
582
      GtkTreeIter tmp_iter = *iter;
583

584
      tmp_iter.user_data = G_NODE (iter->user_data)->parent;
585

586
      retval = gtk_tree_store_get_path (tree_model,
587
					&tmp_iter);
Havoc Pennington's avatar
Havoc Pennington committed
588
      tmp_node = G_NODE (iter->user_data)->parent->children;
589 590 591 592
    }

  if (retval == NULL)
    return NULL;
593

594 595 596 597 598 599 600 601
  if (tmp_node == NULL)
    {
      gtk_tree_path_free (retval);
      return NULL;
    }

  for (; tmp_node; tmp_node = tmp_node->next)
    {
Havoc Pennington's avatar
Havoc Pennington committed
602
      if (tmp_node == G_NODE (iter->user_data))
603 604 605
	break;
      i++;
    }
606

607 608 609
  if (tmp_node == NULL)
    {
      /* We couldn't find node, meaning it's prolly not ours */
610
      /* Perhaps I should do a g_return_if_fail here. */
611 612 613 614 615 616 617 618 619 620 621
      gtk_tree_path_free (retval);
      return NULL;
    }

  gtk_tree_path_append_index (retval, i);

  return retval;
}


static void
622 623 624 625
gtk_tree_store_get_value (GtkTreeModel *tree_model,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
626 627 628 629 630
{
  GtkTreeDataList *list;
  gint tmp_column = column;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_model));
631
  g_return_if_fail (iter != NULL);
632
  g_return_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp);
633 634
  g_return_if_fail (column < GTK_TREE_STORE (tree_model)->n_columns);

Havoc Pennington's avatar
Havoc Pennington committed
635
  list = G_NODE (iter->user_data)->data;
636 637 638 639

  while (tmp_column-- > 0 && list)
    list = list->next;

640 641
  if (list)
    {
642 643 644
      _gtk_tree_data_list_node_to_value (list,
					 GTK_TREE_STORE (tree_model)->column_headers[column],
					 value);
645 646 647 648 649 650
    }
  else
    {
      /* We want to return an initialized but empty (default) value */
      g_value_init (value, GTK_TREE_STORE (tree_model)->column_headers[column]);
    }
651 652 653
}

static gboolean
654 655
gtk_tree_store_iter_next (GtkTreeModel  *tree_model,
			  GtkTreeIter   *iter)
656
{
657
  g_return_val_if_fail (iter != NULL, FALSE);
658
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
659
  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
660

661 662 663 664 665 666
  if (G_NODE (iter->user_data)->next)
    {
      iter->user_data = G_NODE (iter->user_data)->next;
      return TRUE;
    }
  else
667 668 669
    return FALSE;
}

670 671 672 673
static gboolean
gtk_tree_store_iter_children (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent)
674
{
675 676 677
  GNode *children;

  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
678
  g_return_val_if_fail (parent == NULL || parent->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
679

680
  if (parent)
681
    children = G_NODE (parent->user_data)->children;
682
  else
683
    children = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
684

685 686 687 688 689 690 691 692
  if (children)
    {
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      iter->user_data = children;
      return TRUE;
    }
  else
    return FALSE;
693 694 695
}

static gboolean
696 697
gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
698
{
699
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
Jonathan Blandford's avatar
Jonathan Blandford committed
700
  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
701
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
702

Havoc Pennington's avatar
Havoc Pennington committed
703
  return G_NODE (iter->user_data)->children != NULL;
704 705 706
}

static gint
707 708
gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
709
{
710
  GNode *node;
711 712
  gint i = 0;

713
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
714
  g_return_val_if_fail (iter == NULL || iter->user_data != NULL, FALSE);
715

716 717 718
  if (iter == NULL)
    node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
  else
Havoc Pennington's avatar
Havoc Pennington committed
719
    node = G_NODE (iter->user_data)->children;
720

721
  while (node)
722 723
    {
      i++;
724
      node = node->next;
725 726 727 728 729
    }

  return i;
}

730 731 732 733
static gboolean
gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter,
			       GtkTreeIter  *parent,
734 735
			       gint          n)
{
736
  GNode *parent_node;
737
  GNode *child;
738

739
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
740
  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
741

742 743 744
  if (parent == NULL)
    parent_node = GTK_TREE_STORE (tree_model)->root;
  else
Havoc Pennington's avatar
Havoc Pennington committed
745
    parent_node = parent->user_data;
746

747
  child = g_node_nth_child (parent_node, n);
748

749 750 751 752 753 754
  if (child)
    {
      iter->user_data = child;
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      return TRUE;
    }
755
  else
756
    return FALSE;
757 758
}

759 760 761 762
static gboolean
gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
			    GtkTreeIter  *iter,
			    GtkTreeIter  *child)
763
{
764
  GNode *parent;
765

766
  g_return_val_if_fail (iter != NULL, FALSE);
767 768 769
  g_return_val_if_fail (child != NULL, FALSE);
  g_return_val_if_fail (child->user_data != NULL, FALSE);
  g_return_val_if_fail (child->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
770 771 772

  parent = G_NODE (child->user_data)->parent;

773
  g_assert (parent != NULL);
774

775
  if (parent != GTK_TREE_STORE (tree_model)->root)
776
    {
777 778 779
      iter->user_data = parent;
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      return TRUE;
780
    }
781 782
  else
    return FALSE;
783 784
}

785 786

/* Does not emit a signal */
787
static gboolean
788 789 790
gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
			       GtkTreeIter  *iter,
			       gint          column,
791 792
			       GValue       *value,
			       gboolean      sort)
793 794 795
{
  GtkTreeDataList *list;
  GtkTreeDataList *prev;
796
  gint old_column = column;
797 798
  GValue real_value = {0, };
  gboolean converted = FALSE;
799
  gboolean retval = FALSE;
800

801 802 803 804 805
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
  g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
  g_return_val_if_fail (column >= 0 && column < tree_store->n_columns, FALSE);
  g_return_val_if_fail (G_IS_VALUE (value), FALSE);

806
  if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
807
    {
808 809
      if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
	     g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
810 811 812 813
	{
	  g_warning ("%s: Unable to convert from %s to %s\n",
		     G_STRLOC,
		     g_type_name (G_VALUE_TYPE (value)),
814
		     g_type_name (tree_store->column_headers[column]));
815
	  return retval;
816 817 818 819 820 821
	}
      if (!g_value_transform (value, &real_value))
	{
	  g_warning ("%s: Unable to make conversion from %s to %s\n",
		     G_STRLOC,
		     g_type_name (G_VALUE_TYPE (value)),
822
		     g_type_name (tree_store->column_headers[column]));
823
	  g_value_unset (&real_value);
824
	  return retval;
825 826 827
	}
      converted = TRUE;
    }
828

Havoc Pennington's avatar
Havoc Pennington committed
829
  prev = list = G_NODE (iter->user_data)->data;
830 831 832 833 834

  while (list != NULL)
    {
      if (column == 0)
	{
835 836 837 838
	  if (converted)
	    _gtk_tree_data_list_value_to_node (list, &real_value);
	  else
	    _gtk_tree_data_list_value_to_node (list, value);
839
	  retval = TRUE;
840 841
	  if (converted)
	    g_value_unset (&real_value);
842 843
          if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
            gtk_tree_store_sort_iter_changed (tree_store, iter, old_column);
844
	  return retval;
845 846 847 848 849 850 851
	}

      column--;
      prev = list;
      list = list->next;
    }

Havoc Pennington's avatar
Havoc Pennington committed
852
  if (G_NODE (iter->user_data)->data == NULL)
853
    {
Havoc Pennington's avatar
Havoc Pennington committed
854
      G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
855 856 857 858
      list->next = NULL;
    }
  else
    {
859
      list = prev->next = _gtk_tree_data_list_alloc ();
860 861 862 863 864
      list->next = NULL;
    }

  while (column != 0)
    {
865
      list->next = _gtk_tree_data_list_alloc ();
866 867 868 869
      list = list->next;
      list->next = NULL;
      column --;
    }
870

871
  if (converted)
872
    _gtk_tree_data_list_value_to_node (list, &real_value);
873 874
  else
    _gtk_tree_data_list_value_to_node (list, value);
875 876
  
  retval = TRUE;
877 878
  if (converted)
    g_value_unset (&real_value);
879

880 881 882
  if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
    gtk_tree_store_sort_iter_changed (tree_store, iter, old_column);

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
  return retval;
}

/**
 * gtk_tree_store_set_value:
 * @tree_store: a #GtkTreeStore
 * @iter: A valid #GtkTreeIter for the row being modified
 * @column: column number to modify
 * @value: new value for the cell
 *
 * Sets the data in the cell specified by @iter and @column.
 * The type of @value must be convertible to the type of the
 * column.
 *
 **/
void
gtk_tree_store_set_value (GtkTreeStore *tree_store,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
{
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (VALID_ITER (iter, tree_store));
  g_return_if_fail (column >= 0 && column < tree_store->n_columns);
  g_return_if_fail (G_IS_VALUE (value));

909
  if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
910 911 912 913 914 915 916
    {
      GtkTreePath *path;

      path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
      gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
      gtk_tree_path_free (path);
    }
917 918
}

919 920
/**
 * gtk_tree_store_set_valist:
921 922
 * @tree_store: A #GtkTreeStore
 * @iter: A valid #GtkTreeIter for the row being modified
Matthias Clasen's avatar
Matthias Clasen committed
923
 * @var_args: <type>va_list</type> of column/value pairs
924
 *
Matthias Clasen's avatar
Matthias Clasen committed
925
 * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
926
 * use by language bindings.
927
 *
928
 **/
929
void
Havoc Pennington's avatar
Havoc Pennington committed
930 931 932
gtk_tree_store_set_valist (GtkTreeStore *tree_store,
                           GtkTreeIter  *iter,
                           va_list	var_args)
933 934
{
  gint column;
935
  gboolean emit_signal = FALSE;
936
  gboolean maybe_need_sort = FALSE;
937
  GtkTreeIterCompareFunc func = NULL;
938 939

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
940
  g_return_if_fail (VALID_ITER (iter, tree_store));
941 942 943

  column = va_arg (var_args, gint);

944
  if (GTK_TREE_STORE_IS_SORTED (tree_store))
945
    {
946 947 948 949 950 951 952 953 954 955 956 957 958
      if (tree_store->sort_column_id != -1)
	{
	  GtkTreeDataSortHeader *header;
	  header = _gtk_tree_data_list_get_header (tree_store->sort_list,
						   tree_store->sort_column_id);
	  g_return_if_fail (header != NULL);
	  g_return_if_fail (header->func != NULL);
	  func = header->func;
	}
      else
	{
	  func = tree_store->default_sort_func;
	}
959 960 961 962 963
    }

  if (func != gtk_tree_data_list_compare_func)
    maybe_need_sort = TRUE;

Havoc Pennington's avatar
Havoc Pennington committed
964
  while (column != -1)
965 966 967 968 969 970
    {
      GValue value = { 0, };
      gchar *error = NULL;

      if (column >= tree_store->n_columns)
	{
Havoc Pennington's avatar
Havoc Pennington committed
971
	  g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
972 973 974 975
	  break;
	}
      g_value_init (&value, tree_store->column_headers[column]);

976
      G_VALUE_COLLECT (&value, var_args, 0, &error);
977 978 979 980 981 982 983 984 985 986 987
      if (error)
	{
	  g_warning ("%s: %s", G_STRLOC, error);
	  g_free (error);

 	  /* we purposely leak the value here, it might not be
	   * in a sane state if an error condition occoured
	   */
	  break;
	}

988 989 990
      emit_signal = gtk_tree_store_real_set_value (tree_store,
						   iter,
						   column,
991 992 993 994 995 996
						   &value,
						   FALSE) || emit_signal;

      if (func == gtk_tree_data_list_compare_func &&
	  column == tree_store->sort_column_id)
	maybe_need_sort = TRUE;
997 998 999 1000 1001

      g_value_unset (&value);

      column = va_arg (var_args, gint);
    }
1002 1003 1004 1005

  if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
    gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id);

1006 1007 1008 1009 1010 1011 1012 1013
  if (emit_signal)
    {
      GtkTreePath *path;

      path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
      gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
      gtk_tree_path_free (path);
    }
1014 1015
}

Havoc Pennington's avatar
Havoc Pennington committed
1016 1017
/**
 * gtk_tree_store_set:
1018 1019
 * @tree_store: A #GtkTreeStore
 * @iter: A valid #GtkTreeIter for the row being modified
Havoc Pennington's avatar
Havoc Pennington committed
1020
 * @Varargs: pairs of column number and value, terminated with -1
1021
 *
Havoc Pennington's avatar
Havoc Pennington committed
1022 1023
 * Sets the value of one or more cells in the row referenced by @iter.
 * The variable argument list should contain integer column numbers,
1024
 * each column number followed by the value to be set. 
Havoc Pennington's avatar
Havoc Pennington committed
1025
 * The list is terminated by a -1. For example, to set column 0 with type
Matthias Clasen's avatar
Matthias Clasen committed
1026 1027
 * %G_TYPE_STRING to "Foo", you would write 
 * <literal>gtk_tree_store_set (store, iter, 0, "Foo", -1)</literal>.
Havoc Pennington's avatar
Havoc Pennington committed
1028
 **/
1029
void
1030 1031 1032
gtk_tree_store_set (GtkTreeStore *tree_store,
		    GtkTreeIter  *iter,
		    ...)
1033 1034 1035 1036
{
  va_list var_args;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1037
  g_return_if_fail (VALID_ITER (iter, tree_store));
1038 1039

  va_start (var_args, iter);
Havoc Pennington's avatar
Havoc Pennington committed
1040
  gtk_tree_store_set_valist (tree_store, iter, var_args);
1041 1042 1043
  va_end (var_args);
}

1044 1045 1046 1047 1048 1049
/**
 * gtk_tree_store_remove:
 * @tree_store: A #GtkTreeStore
 * @iter: A valid #GtkTreeIter
 * 
 * Removes @iter from @tree_store.  After being removed, @iter is set to the
1050
 * next valid row at that level, or invalidated if it previously pointed to the
1051
 * last one.
Kristian Rietveld's avatar
Kristian Rietveld committed
1052 1053
 *
 * Return value: %TRUE if @iter is still valid, %FALSE if not.
1054
 **/
Kristian Rietveld's avatar
Kristian Rietveld committed
1055
gboolean
1056
gtk_tree_store_remove (GtkTreeStore *tree_store,
1057
		       GtkTreeIter  *iter)
1058 1059
{
  GtkTreePath *path;
1060
  GtkTreeIter new_iter = {0,};
1061
  GNode *parent;
1062
  GNode *next_node;
1063

Kristian Rietveld's avatar
Kristian Rietveld committed
1064 1065
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
  g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1066

Havoc Pennington's avatar
Havoc Pennington committed
1067
  parent = G_NODE (iter->user_data)->parent;
1068

1069
  g_assert (parent != NULL);
1070
  next_node = G_NODE (iter->user_data)->next;
1071

Havoc Pennington's avatar
Havoc Pennington committed
1072
  if (G_NODE (iter->user_data)->data)
1073 1074
    g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
		     -1, node_free, tree_store->column_headers);
1075

1076
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
Havoc Pennington's avatar
Havoc Pennington committed
1077
  g_node_destroy (G_NODE (iter->user_data));
1078

1079
  gtk_tree_model_row_deleted (GTK_TREE_MODEL (tree_store), path);
1080

1081
  if (parent != G_NODE (tree_store->root))
1082
    {
1083 1084 1085 1086 1087
      /* child_toggled */
      if (parent->children == NULL)
	{
	  gtk_tree_path_up (path);

1088
	  new_iter.stamp = tree_store->stamp;
1089
	  new_iter.user_data = parent;
1090
	  gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &new_iter);
1091
	}
1092 1093
    }
  gtk_tree_path_free (path);
1094 1095 1096 1097 1098 1099

  /* revalidate iter */
  if (next_node != NULL)
    {
      iter->stamp = tree_store->stamp;
      iter->user_data = next_node;
Kristian Rietveld's avatar
Kristian Rietveld committed
1100
      return TRUE;
1101 1102 1103 1104
    }
  else
    {
      iter->stamp = 0;
1105
      iter->user_data = NULL;
1106
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
1107 1108

  return FALSE;
1109 1110
}

1111 1112
/**
 * gtk_tree_store_insert:
Matt Wilson's avatar
Matt Wilson committed
1113
 * @tree_store: A #GtkTreeStore
1114 1115 1116 1117
 * @iter: An unset #GtkTreeIter to set to the new row
 * @parent: A valid #GtkTreeIter, or %NULL
 * @position: position to insert the new row
 *
1118
 * Creates a new row at @position.  If parent is non-%NULL, then the row will be
1119 1120 1121
 * made a child of @parent.  Otherwise, the row will be created at the toplevel.
 * If @position is larger than the number of rows at that level, then the new
 * row will be inserted to the end of the list.  @iter will be changed to point
1122
 * to this new row.  The row will be empty after this function is called.  To
Matt Wilson's avatar
Matt Wilson committed
1123 1124
 * fill in values, you need to call gtk_tree_store_set() or
 * gtk_tree_store_set_value().
1125 1126
 *
 **/
1127
void
1128
gtk_tree_store_insert (GtkTreeStore *tree_store,
1129 1130 1131
		       GtkTreeIter  *iter,
		       GtkTreeIter  *parent,
		       gint          position)
1132 1133
{
  GtkTreePath *path;
1134
  GNode *parent_node;
1135

1136
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1137
  if (parent)
1138
    g_return_if_fail (VALID_ITER (parent, tree_store));
1139

1140 1141 1142
  if (parent)
    parent_node = parent->user_data;
  else
1143
    parent_node = tree_store->root;
1144

1145 1146
  tree_store->columns_dirty = TRUE;

1147
  iter->stamp = tree_store->stamp;
Havoc Pennington's avatar
Havoc Pennington committed
1148
  iter->user_data = g_node_new (NULL);
1149
  g_node_insert (parent_node, position, G_NODE (iter->user_data));
1150

1151
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1152
  gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1153

1154
  gtk_tree_path_free (path);
1155

1156
  validate_tree ((GtkTreeStore*)tree_store);
1157 1158
}

1159 1160 1161 1162
/**
 * gtk_tree_store_insert_before:
 * @tree_store: A #GtkTreeStore
 * @iter: An unset #GtkTreeIter to set to the new row
1163
 * @parent: A valid #GtkTreeIter, or %NULL
1164 1165 1166
 * @sibling: A valid #GtkTreeIter, or %NULL
 *
 * Inserts a new row before @sibling.  If @sibling is %NULL, then the row will
Kristian Rietveld's avatar
Kristian Rietveld committed
1167 1168 1169 1170
 * be appended to @parent 's children.  If @parent and @sibling are %NULL, then
 * the row will be appended to the toplevel.  If both @sibling and @parent are
 * set, then @parent must be the parent of @sibling.  When @sibling is set,
 * @parent is optional.
1171 1172 1173
 *
 * @iter will be changed to point to this new row.  The row will be empty after
 * this function is called.  To fill in values, you need to call
1174
 * gtk_tree_store_set() or gtk_tree_store_set_value().
1175 1176
 *
 **/
1177
void
1178
gtk_tree_store_insert_before (GtkTreeStore *tree_store,
1179 1180 1181
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent,
			      GtkTreeIter  *sibling)
1182 1183
{
  GtkTreePath *path;
1184
  GNode *parent_node = NULL;
1185
  GNode *new_node;
1186

1187
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1188
  g_return_if_fail (iter != NULL);
1189
  if (parent != NULL)
1190
    g_return_if_fail (VALID_ITER (parent, tree_store));
1191
  if (sibling != NULL)
1192
    g_return_if_fail (VALID_ITER (sibling, tree_store));
1193

1194 1195
  tree_store->columns_dirty = TRUE;

1196
  new_node = g_node_new (NULL);
1197

1198
  if (parent == NULL && sibling == NULL)
1199
    parent_node = tree_store->root;
1200
  else if (parent == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1201
    parent_node = G_NODE (sibling->user_data)->parent;
1202
  else if (sibling == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1203
    parent_node = G_NODE (parent->user_data);
1204 1205
  else
    {
1206
      g_return_if_fail (G_NODE (sibling->user_data)->parent == G_NODE (parent->user_data));
1207 1208
      parent_node = G_NODE (parent->user_data);
    }
1209

1210
  g_node_insert_before (parent_node,
Havoc Pennington's avatar
Havoc Pennington committed
1211
			sibling ? G_NODE (sibling->user_data) : NULL,
1212
                        new_node);
1213

1214
  iter->stamp = tree_store->stamp;
1215
  iter->user_data = new_node;
1216

1217
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1218
  gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1219

1220
  gtk_tree_path_free (path);
1221

1222
  validate_tree ((GtkTreeStore*)tree_store);
1223 1224
}

1225 1226 1227 1228
/**
 * gtk_tree_store_insert_after:
 * @tree_store: A #GtkTreeStore
 * @iter: An unset #GtkTreeIter to set to the new row
1229
 * @parent: A valid #GtkTreeIter, or %NULL
1230 1231 1232
 * @sibling: A valid #GtkTreeIter, or %NULL
 *
 * Inserts a new row after @sibling.  If @sibling is %NULL, then the row will be
1233 1234 1235 1236
 * prepended to @parent 's children.  If @parent and @sibling are %NULL, then
 * the row will be prepended to the toplevel.  If both @sibling and @parent are
 * set, then @parent must be the parent of @sibling.  When @sibling is set,
 * @parent is optional.
1237 1238 1239
 *
 * @iter will be changed to point to this new row.  The row will be empty after
 * this function is called.  To fill in values, you need to call
1240
 * gtk_tree_store_set() or gtk_tree_store_set_value().
1241 1242
 *
 **/
1243
void
1244
gtk_tree_store_insert_after (GtkTreeStore *tree_store,
1245 1246 1247
			     GtkTreeIter  *iter,
			     GtkTreeIter  *parent,
			     GtkTreeIter  *sibling)
1248 1249
{
  GtkTreePath *path;
1250
  GNode *parent_node;
1251
  GNode *new_node;
1252

1253
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1254
  g_return_if_fail (iter != NULL);
1255
  if (parent != NULL)
1256
    g_return_if_fail (VALID_ITER (parent, tree_store));
1257
  if (sibling != NULL)
1258
    g_return_if_fail (VALID_ITER (sibling, tree_store));
1259

1260 1261
  tree_store->columns_dirty = TRUE;

1262
  new_node = g_node_new (NULL);
1263

1264
  if (parent == NULL && sibling == NULL)
1265
    parent_node = tree_store->root;
1266
  else if (parent == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1267
    parent_node = G_NODE (sibling->user_data)->parent;
1268
  else if (sibling == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1269
    parent_node = G_NODE (parent->user_data);
1270 1271 1272 1273 1274 1275
  else
    {
      g_return_if_fail (G_NODE (sibling->user_data)->parent ==
                        G_NODE (parent->user_data));
      parent_node = G_NODE (parent->user_data);
    }
4's avatar