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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
23 24
#include <glib.h>
#include <gobject/gvaluecollector.h>
25
#include "gtktreemodel.h"
26 27
#include "gtktreeview.h"
#include "gtktreeprivate.h"
Tim Janik's avatar
Tim Janik committed
28
#include "gtksignal.h"
29

30 31 32 33 34 35 36

struct _GtkTreePath
{
  gint depth;
  gint *indices;
};

37 38 39
static void gtk_tree_model_base_init (gpointer g_class);


40 41 42 43 44
GtkType
gtk_tree_model_get_type (void)
{
  static GtkType tree_model_type = 0;

45
  if (! tree_model_type)
46 47 48
    {
      static const GTypeInfo tree_model_info =
      {
49
        sizeof (GtkTreeModelIface), /* class_size */
50
	gtk_tree_model_base_init,   /* base_init */
51
	NULL,		/* base_finalize */
52 53 54 55 56 57
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,              /* n_preallocs */
	NULL
58 59
      };

60
      tree_model_type = g_type_register_static (G_TYPE_INTERFACE, "GtkTreeModel", &tree_model_info, 0);
61
      g_type_interface_add_prerequisite (tree_model_type, G_TYPE_OBJECT);
62 63 64 65 66
    }

  return tree_model_type;
}

67 68 69
static void
gtk_tree_model_base_init (gpointer g_class)
{
70
  static gboolean initialized = FALSE;
71

72
  if (! initialized)
73
    {
74
      g_signal_newc ("range_changed",
75
		     GTK_TYPE_TREE_MODEL,
76
		     G_SIGNAL_RUN_LAST,
77
		     G_STRUCT_OFFSET (GtkTreeModelIface, range_changed),
78
		     NULL, NULL,
79 80 81 82
		     gtk_marshal_VOID__BOXED_BOXED_BOXED_BOXED,
		     G_TYPE_NONE, 4,
		     GTK_TYPE_TREE_PATH,
		     GTK_TYPE_TREE_ITER,
83 84 85 86
		     GTK_TYPE_TREE_PATH,
		     GTK_TYPE_TREE_ITER);
      g_signal_newc ("inserted",
		     GTK_TYPE_TREE_MODEL,
87
		     G_SIGNAL_RUN_LAST,
88
		     G_STRUCT_OFFSET (GtkTreeModelIface, inserted),
89
		     NULL, NULL,
90 91 92 93
		     gtk_marshal_VOID__BOXED_BOXED,
		     G_TYPE_NONE, 2,
		     GTK_TYPE_TREE_PATH,
		     GTK_TYPE_TREE_ITER);
94
      g_signal_newc ("has_child_toggled",
95
		     GTK_TYPE_TREE_MODEL,
96
		     G_SIGNAL_RUN_LAST,
97
		     G_STRUCT_OFFSET (GtkTreeModelIface, has_child_toggled),
98
		     NULL, NULL,
99 100 101 102 103 104
		     gtk_marshal_VOID__BOXED_BOXED,
		     G_TYPE_NONE, 2,
		     GTK_TYPE_TREE_PATH,
		     GTK_TYPE_TREE_ITER);
      g_signal_newc ("deleted",
		     GTK_TYPE_TREE_MODEL,
105
		     G_SIGNAL_RUN_LAST,
106
		     G_STRUCT_OFFSET (GtkTreeModelIface, deleted),
107
		     NULL, NULL,
108
		     gtk_marshal_VOID__BOXED,
109 110
		     G_TYPE_NONE, 1,
		     GTK_TYPE_TREE_PATH);
111 112 113 114 115
      g_signal_newc ("reordered",
		     GTK_TYPE_TREE_MODEL,
		     G_SIGNAL_RUN_LAST,
		     G_STRUCT_OFFSET (GtkTreeModelIface, reordered),
		     NULL, NULL,
116 117
		     gtk_marshal_VOID__BOXED_BOXED_POINTER,
		     G_TYPE_NONE, 3,
118
		     GTK_TYPE_TREE_PATH,
119
		     GTK_TYPE_TREE_ITER,
120 121
		     G_TYPE_POINTER);
      initialized = TRUE;
122 123 124
    }
}

7's avatar
7 committed
125 126
/**
 * gtk_tree_path_new:
127
 *
7's avatar
7 committed
128
 * Creates a new #GtkTreePath.
129
 *
7's avatar
7 committed
130 131
 * Return value: A newly created #GtkTreePath.
 **/
132 133 134 135 136 137 138 139 140 141 142 143
/* GtkTreePath Operations */
GtkTreePath *
gtk_tree_path_new (void)
{
  GtkTreePath *retval;
  retval = (GtkTreePath *) g_new (GtkTreePath, 1);
  retval->depth = 0;
  retval->indices = NULL;

  return retval;
}

7's avatar
7 committed
144 145 146
/**
 * gtk_tree_path_new_from_string:
 * @path: The string representation of a path.
147
 *
148 149 150 151 152
 * Creates a new #GtkTreePath initialized to @path.  @path is expected to be a
 * colon separated list of numbers.  For example, the string "10:4:0" would
 * create a path of depth 3 pointing to the 11th child of the root node, the 5th
 * child of that 11th child, and the 1st child of that 5th child.  If an invalid
 * path is past in, NULL is returned.
153
 *
154
 * Return value: A newly created #GtkTreePath, or NULL
7's avatar
7 committed
155
 **/
156 157 158 159
GtkTreePath *
gtk_tree_path_new_from_string (gchar *path)
{
  GtkTreePath *retval;
160
  gchar *orig_path = path;
161 162 163
  gchar *ptr;
  gint i;

164 165
  g_return_val_if_fail (path != NULL, NULL);
  g_return_val_if_fail (*path != '\000', NULL);
166 167 168 169 170 171 172 173

  retval = gtk_tree_path_new ();

  while (1)
    {
      i = strtol (path, &ptr, 10);
      gtk_tree_path_append_index (retval, i);

174 175 176 177 178 179
      if (i < 0)
	{
	  g_warning (G_STRLOC"Negative numbers in path %s passed to gtk_tree_path_new_from_string", orig_path);
	  gtk_tree_path_free (retval);
	  return NULL;
	}
180 181
      if (*ptr == '\000')
	break;
182 183 184 185 186 187
      if (ptr == path || *ptr != ':')
	{
	  g_warning (G_STRLOC"Invalid path %s passed to gtk_tree_path_new_from_string", orig_path);
	  gtk_tree_path_free (retval);
	  return NULL;
	}
188 189 190 191 192 193
      path = ptr + 1;
    }

  return retval;
}

7's avatar
7 committed
194 195 196
/**
 * gtk_tree_path_to_string:
 * @path: A #GtkTreePath
197
 *
7's avatar
7 committed
198 199
 * Generates a string representation of the path.  This string is a ':'
 * separated list of numbers.  For example, "4:10:0:3" would be an acceptable return value for this string.
200
 *
7's avatar
7 committed
201 202
 * Return value: A newly allocated string.  Must be freed with #g_free.
 **/
203 204 205 206 207 208
gchar *
gtk_tree_path_to_string (GtkTreePath *path)
{
  gchar *retval, *ptr;
  gint i;

209 210
  g_return_val_if_fail (path != NULL, NULL);

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  if (path->depth == 0)
    return NULL;

  ptr = retval = (gchar *) g_new0 (char *, path->depth*8);
  sprintf (retval, "%d", path->indices[0]);
  while (*ptr != '\000')
    ptr++;

  for (i = 1; i < path->depth; i++)
    {
      sprintf (ptr, ":%d", path->indices[i]);
      while (*ptr != '\000')
	ptr++;
    }

  return retval;
}

7's avatar
7 committed
229 230
/**
 * gtk_tree_path_new_root:
231
 *
7's avatar
7 committed
232 233
 * Creates a new root #GtkTreePath.  The string representation of this path is
 * "0"
234
 *
7's avatar
7 committed
235 236
 * Return value: A new #GtkTreePath.
 **/
237 238 239 240 241 242 243 244 245 246 247
GtkTreePath *
gtk_tree_path_new_root (void)
{
  GtkTreePath *retval;

  retval = gtk_tree_path_new ();
  gtk_tree_path_append_index (retval, 0);

  return retval;
}

7's avatar
7 committed
248 249 250 251
/**
 * gtk_tree_path_append_index:
 * @path: A #GtkTreePath.
 * @index: The index.
252
 *
7's avatar
7 committed
253 254 255
 * Appends a new index to a path.  As a result, the depth of the path is
 * increased.
 **/
256 257 258 259
void
gtk_tree_path_append_index (GtkTreePath *path,
			    gint         index)
{
7's avatar
7 committed
260 261 262
  g_return_if_fail (path != NULL);
  g_return_if_fail (index >= 0);

263 264
  path->depth += 1;
  path->indices = g_realloc (path->indices, path->depth * sizeof(gint));
265 266 267
  path->indices[path->depth - 1] = index;
}

7's avatar
7 committed
268 269 270 271
/**
 * gtk_tree_path_prepend_index:
 * @path: A #GtkTreePath.
 * @index: The index.
272
 *
7's avatar
7 committed
273 274 275
 * Prepends a new index to a path.  As a result, the depth of the path is
 * increased.
 **/
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
void
gtk_tree_path_prepend_index (GtkTreePath *path,
			     gint       index)
{
  gint *new_indices = g_new (gint, ++path->depth);
  if (path->indices == NULL)
    {
      path->indices = new_indices;
      path->indices[0] = index;
      return;
    }
  memcpy (new_indices + 1, path->indices, (path->depth - 1)*sizeof (gint));
  g_free (path->indices);
  path->indices = new_indices;
  path->indices[0] = index;
}

7's avatar
7 committed
293 294 295
/**
 * gtk_tree_path_get_depth:
 * @path: A #GtkTreePath.
296
 *
7's avatar
7 committed
297
 * Returns the current depth of @path.
298
 *
7's avatar
7 committed
299 300
 * Return value: The depth of @path
 **/
301 302 303
gint
gtk_tree_path_get_depth (GtkTreePath *path)
{
7's avatar
7 committed
304 305
  g_return_val_if_fail (path != NULL, 0);

306 307 308
  return path->depth;
}

7's avatar
7 committed
309 310 311
/**
 * gtk_tree_path_get_indices:
 * @path: A #GtkTreePath.
312
 *
7's avatar
7 committed
313 314
 * Returns the current indices of @path.  This is an array of integers, each
 * representing a node in a tree.
315
 *
7's avatar
7 committed
316 317
 * Return value: The current indices, or NULL.
 **/
318 319 320
gint *
gtk_tree_path_get_indices (GtkTreePath *path)
{
7's avatar
7 committed
321 322
  g_return_val_if_fail (path != NULL, NULL);

323 324 325
  return path->indices;
}

7's avatar
7 committed
326 327 328
/**
 * gtk_tree_path_free:
 * @path: A #GtkTreePath.
329
 *
7's avatar
7 committed
330 331
 * Frees @path.
 **/
332 333 334
void
gtk_tree_path_free (GtkTreePath *path)
{
335 336
  g_return_if_fail (path != NULL);

337 338 339 340
  g_free (path->indices);
  g_free (path);
}

7's avatar
7 committed
341 342 343
/**
 * gtk_tree_path_copy:
 * @path: A #GtkTreePath.
344
 *
345
 * Creates a new #GtkTreePath as a copy of @path.
346
 *
7's avatar
7 committed
347 348
 * Return value: A new #GtkTreePath.
 **/
349 350 351 352 353
GtkTreePath *
gtk_tree_path_copy (GtkTreePath *path)
{
  GtkTreePath *retval;

354 355
  g_return_val_if_fail (path != NULL, NULL);

356 357 358 359 360 361 362
  retval = g_new (GtkTreePath, 1);
  retval->depth = path->depth;
  retval->indices = g_new (gint, path->depth);
  memcpy (retval->indices, path->indices, path->depth * sizeof (gint));
  return retval;
}

7's avatar
7 committed
363 364 365 366
/**
 * gtk_tree_path_compare:
 * @a: A #GtkTreePath.
 * @b: A #GtkTreePath to compare with.
367
 *
368 369
 * Compares two paths.  If @a appears before @b in a tree, then -1, is returned.
 * If @b appears before @a, then 1 is returned.  If the two nodes are equal,
7's avatar
7 committed
370
 * then 0 is returned.
371
 *
7's avatar
7 committed
372 373
 * Return value: The relative positions of @a and @b
 **/
374
gint
7's avatar
7 committed
375 376
gtk_tree_path_compare (const GtkTreePath *a,
		       const GtkTreePath *b)
377 378 379 380 381 382 383 384 385 386 387 388
{
  gint p = 0, q = 0;

  g_return_val_if_fail (a != NULL, 0);
  g_return_val_if_fail (b != NULL, 0);
  g_return_val_if_fail (a->depth > 0, 0);
  g_return_val_if_fail (b->depth > 0, 0);

  do
    {
      if (a->indices[p] == b->indices[q])
	continue;
389
      return (a->indices[p] < b->indices[q]?-1:1);
390 391 392 393
    }
  while (++p < a->depth && ++q < b->depth);
  if (a->depth == b->depth)
    return 0;
394
  return (a->depth < b->depth?-1:1);
395 396
}

397 398 399 400
/**
 * gtk_tree_path_is_ancestor:
 * @path: a #GtkTreePath
 * @descendant: another #GtkTreePath
401 402 403
 *
 *
 *
404 405 406 407 408 409 410
 * Return value: %TRUE if @descendant is contained inside @path
 **/
gboolean
gtk_tree_path_is_ancestor (GtkTreePath *path,
                           GtkTreePath *descendant)
{
  gint i;
411

412 413 414 415 416 417
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (descendant != NULL, FALSE);

  /* can't be an ancestor if we're deeper */
  if (path->depth >= descendant->depth)
    return FALSE;
418

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  i = 0;
  while (i < path->depth)
    {
      if (path->indices[i] != descendant->indices[i])
        return FALSE;
      ++i;
    }

  return TRUE;
}

/**
 * gtk_tree_path_is_descendant:
 * @path: a #GtkTreePath
 * @ancestor: another #GtkTreePath
434 435 436
 *
 *
 *
437 438 439 440 441 442 443
 * Return value: %TRUE if @ancestor contains @path somewhere below it
 **/
gboolean
gtk_tree_path_is_descendant (GtkTreePath *path,
                             GtkTreePath *ancestor)
{
  gint i;
444

445 446
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (ancestor != NULL, FALSE);
447

448 449 450
  /* can't be a descendant if we're shallower in the tree */
  if (path->depth <= ancestor->depth)
    return FALSE;
451

452 453 454 455 456 457 458 459 460 461 462 463
  i = 0;
  while (i < ancestor->depth)
    {
      if (path->indices[i] != ancestor->indices[i])
        return FALSE;
      ++i;
    }

  return TRUE;
}


7's avatar
7 committed
464 465 466
/**
 * gtk_tree_path_next:
 * @path: A #GtkTreePath.
467
 *
7's avatar
7 committed
468 469
 * Moves the @path to point to the next node at the current depth.
 **/
470 471 472 473
void
gtk_tree_path_next (GtkTreePath *path)
{
  g_return_if_fail (path != NULL);
7's avatar
7 committed
474
  g_return_if_fail (path->depth > 0);
475 476 477 478

  path->indices[path->depth - 1] ++;
}

7's avatar
7 committed
479 480 481
/**
 * gtk_tree_path_prev:
 * @path: A #GtkTreePath.
482
 *
7's avatar
7 committed
483
 * Moves the @path to point to the previous node at the current depth, if it exists.
484
 *
Jonathan Blandford's avatar
Jonathan Blandford committed
485
 * Return value: TRUE if @path has a previous node, and the move was made.
7's avatar
7 committed
486
 **/
487
gboolean
488 489 490 491
gtk_tree_path_prev (GtkTreePath *path)
{
  g_return_val_if_fail (path != NULL, FALSE);

492
  if (path->indices[path->depth - 1] == 0)
493 494
    return FALSE;

495
  path->indices[path->depth - 1] -= 1;
496 497 498 499

  return TRUE;
}

7's avatar
7 committed
500 501 502
/**
 * gtk_tree_path_up:
 * @path: A #GtkTreePath.
503
 *
7's avatar
7 committed
504
 * Moves the @path to point to it's parent node, if it has a parent.
505
 *
7's avatar
7 committed
506 507
 * Return value: TRUE if @path has a parent, and the move was made.
 **/
508
gboolean
509 510 511 512 513 514 515 516 517 518 519 520
gtk_tree_path_up (GtkTreePath *path)
{
  g_return_val_if_fail (path != NULL, FALSE);

  if (path->depth == 1)
    return FALSE;

  path->depth--;

  return TRUE;
}

7's avatar
7 committed
521 522 523
/**
 * gtk_tree_path_down:
 * @path: A #GtkTreePath.
524
 *
7's avatar
7 committed
525 526
 * Moves @path to point to the first child of the current path.
 **/
527 528 529 530 531 532 533 534
void
gtk_tree_path_down (GtkTreePath *path)
{
  g_return_if_fail (path != NULL);

  gtk_tree_path_append_index (path, 0);
}

535 536 537
/**
 * gtk_tree_iter_copy:
 * @iter: A #GtkTreeIter.
538
 *
539 540
 * Creates a dynamically allocated tree iterator as a copy of @iter.  This
 * function is not intended for use in applications, because you can just copy
541
 * the structs by value (GtkTreeIter new_iter = iter;).  You
542
 * must free this iter with gtk_tree_iter_free ().
543
 *
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
 * Return value: a newly allocated copy of @iter.
 **/
GtkTreeIter *
gtk_tree_iter_copy (GtkTreeIter *iter)
{
  GtkTreeIter *retval;

  g_return_val_if_fail (iter != NULL, NULL);

  retval = g_new (GtkTreeIter, 1);
  *retval = *iter;

  return retval;
}

/**
 * gtk_tree_iter_free:
 * @iter: A dynamically allocated tree iterator.
562
 *
563 564 565 566 567 568 569 570 571 572 573 574 575 576
 * Free an iterator that has been allocated on the heap.  This function is
 * mainly used for language bindings.
 **/
void
gtk_tree_iter_free (GtkTreeIter *iter)
{
  g_return_if_fail (iter != NULL);

  g_free (iter);
}

/**
 * gtk_tree_model_get_flags:
 * @tree_model: A #GtkTreeModel.
577
 *
578
 * Returns a set of flags supported by this interface.  The flags are a bitwise
579 580
 * combination of #GtkTreeModelFlags.  It is expected that the flags supported
 * do not change for an interface.
581
 *
582 583
 * Return value: The flags supported by this interface.
 **/
584
GtkTreeModelFlags
585 586 587 588 589 590 591 592 593 594 595
gtk_tree_model_get_flags (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (tree_model != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);

  if (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_flags)
    return (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_flags) (tree_model);

  return 0;
}

7's avatar
7 committed
596 597 598
/**
 * gtk_tree_model_get_n_columns:
 * @tree_model: A #GtkTreeModel.
599
 *
7's avatar
7 committed
600
 * Returns the number of columns supported by the #tree_model
601
 *
7's avatar
7 committed
602 603
 * Return value: The number of columns.
 **/
604 605 606
gint
gtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
{
607 608
  g_return_val_if_fail (tree_model != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);
609
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_n_columns != NULL, 0);
610

611
  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_n_columns) (tree_model);
612 613
}

614 615 616 617
/**
 * gtk_tree_model_get_column_type:
 * @tree_model: A #GtkTreeModel.
 * @index: The column index.
618
 *
619
 * Returns the type of the column.
620
 *
621 622 623 624 625 626 627 628 629 630 631 632 633 634
 * Return value: The type of the column.
 **/
GType
gtk_tree_model_get_column_type (GtkTreeModel *tree_model,
				gint          index)
{
  g_return_val_if_fail (tree_model != NULL, G_TYPE_INVALID);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), G_TYPE_INVALID);
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_column_type != NULL, G_TYPE_INVALID);
  g_return_val_if_fail (index >= 0, G_TYPE_INVALID);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_column_type) (tree_model, index);
}

7's avatar
7 committed
635
/**
636
 * gtk_tree_model_get_iter:
7's avatar
7 committed
637
 * @tree_model: A #GtkTreeModel.
638 639
 * @iter: The uninitialized #GtkTreeIter.
 * @path: The #GtkTreePath.
640
 *
641 642 643
 * Sets @iter to a valid iterator pointing to @path.  If the model does not
 * provide an implementation of this function, it is implemented in terms of
 * @gtk_tree_model_iter_nth_child.
644
 *
645
 * Return value: TRUE, if @iter was set.
7's avatar
7 committed
646
 **/
647 648 649
gboolean
gtk_tree_model_get_iter (GtkTreeModel *tree_model,
			 GtkTreeIter  *iter,
650 651
			 GtkTreePath  *path)
{
652 653 654 655
  GtkTreeIter parent;
  gint *indices;
  gint depth, i;

656
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
657 658 659
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (path != NULL, FALSE);

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  if (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_iter != NULL)
    return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_iter) (tree_model, iter, path);

  indices = gtk_tree_path_get_indices (path);
  depth = gtk_tree_path_get_depth (path);

  g_return_val_if_fail (depth > 0, FALSE);

  if (! gtk_tree_model_iter_nth_child (tree_model, iter, NULL, 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;
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

/**
 * gtk_tree_model_get_iter_root:
 * @tree_model: A #GtkTreeModel.
 * @iter: The uninitialized #GtkTreeIter.
 * 
 * Gets the root iter, if it exists.
 * 
 * Return value: TRUE, if @iter was set.
 **/
gboolean
gtk_tree_model_get_iter_root (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter)
{
  GtkTreePath *path;
  gboolean retval;

  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);

  path = gtk_tree_path_new_root ();
  retval = gtk_tree_model_get_iter (tree_model, iter, path);
  gtk_tree_path_free (path);

  return retval;
}

708 709 710 711
/**
 * gtk_tree_model_get_first:
 * @tree_model: a #GtkTreeModel
 * @iter: iterator to initialize
712
 *
713 714 715
 * Initialized @iter with the first iterator in the tree (the one at the
 * root path) and returns %TRUE, or returns %FALSE if there are no
 * iterable locations in the model (i.e. the tree is empty).
716
 *
717 718 719 720 721 722 723 724
 * Return value: %TRUE if @iter was initialized
 **/
gboolean
gtk_tree_model_get_first (GtkTreeModel *tree_model,
                          GtkTreeIter  *iter)
{
  gboolean retval;
  GtkTreePath *path;
725

726 727 728 729 730 731 732 733 734 735 736 737
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);

  path = gtk_tree_path_new_root ();

  retval = gtk_tree_model_get_iter (tree_model, iter, path);

  gtk_tree_path_free (path);

  return retval;
}

7's avatar
7 committed
738 739 740
/**
 * gtk_tree_model_get_path:
 * @tree_model: A #GtkTreeModel.
741
 * @iter: The #GtkTreeIter.
742
 *
743
 * Returns a newly created #GtkTreePath referenced by @iter.  This path should
7's avatar
7 committed
744
 * be freed with #gtk_tree_path_free.
745
 *
7's avatar
7 committed
746 747
 * Return value: a newly created #GtkTreePath.
 **/
748 749
GtkTreePath *
gtk_tree_model_get_path (GtkTreeModel *tree_model,
750
			 GtkTreeIter  *iter)
751
{
752 753 754
  g_return_val_if_fail (tree_model != NULL, NULL);
  g_return_val_if_fail (iter != NULL, NULL);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), NULL);
755
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_path != NULL, NULL);
756 757

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_path) (tree_model, iter);
758 759
}

7's avatar
7 committed
760
/**
761
 * gtk_tree_model_get_value:
7's avatar
7 committed
762
 * @tree_model: A #GtkTreeModel.
763 764
 * @iter: The #GtkTreeIter.
 * @column: The column to lookup the value at.
7's avatar
7 committed
765
 * @value: An empty #GValue to set.
766
 *
7's avatar
7 committed
767 768 769
 * Sets initializes and sets @value to that at @column.  When done with value,
 * #g_value_unset needs to be called on it.
 **/
770
void
771 772 773 774
gtk_tree_model_get_value (GtkTreeModel *tree_model,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
775
{
776 777 778 779 780
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
  g_return_if_fail (value != NULL);
  g_return_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_value != NULL);
781

782
  (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_value) (tree_model, iter, column, value);
783 784
}

7's avatar
7 committed
785
/**
786
 * gtk_tree_model_iter_next:
7's avatar
7 committed
787
 * @tree_model: A #GtkTreeModel.
788
 * @iter: The #GtkTreeIter.
789
 *
790 791
 * Sets @iter to point to the node following it at the current level.  If there
 * is no next @iter, FALSE is returned and @iter is set to be invalid.
792
 *
793
 * Return value: TRUE if @iter has been changed to the next node.
7's avatar
7 committed
794
 **/
795
gboolean
796 797
gtk_tree_model_iter_next (GtkTreeModel  *tree_model,
			  GtkTreeIter   *iter)
798
{
799 800 801
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
802 803 804
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_next != NULL, FALSE);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_next) (tree_model, iter);
805 806
}

7's avatar
7 committed
807
/**
808
 * gtk_tree_model_iter_children:
7's avatar
7 committed
809
 * @tree_model: A #GtkTreeModel.
Jonathan Blandford's avatar
Jonathan Blandford committed
810 811
 * @iter: The new #GtkTreeIter to be set to the child.
 * @parent: The #GtkTreeIter.
812
 *
813 814 815
 * Sets @iter to point to the first child of @parent.  If @parent has no children,
 * FALSE is returned and @iter is set to be invalid.  @parent will remain a valid
 * node after this function has been called.
816
 *
817
 * Return value: TRUE, if @child has been set to the first child.
7's avatar
7 committed
818
 **/
819 820 821 822
gboolean
gtk_tree_model_iter_children (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent)
823
{
824 825 826
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
827 828 829
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_children != NULL, FALSE);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_children) (tree_model, iter, parent);
830 831
}

7's avatar
7 committed
832
/**
833
 * gtk_tree_model_iter_has_child:
7's avatar
7 committed
834
 * @tree_model: A #GtkTreeModel.
835
 * @iter: The #GtkTreeIter to test for children.
836
 *
837
 * Returns TRUE if @iter has children, FALSE otherwise.
838
 *
839
 * Return value: TRUE if @iter has children.
7's avatar
7 committed
840
 **/
841
gboolean
842 843
gtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
844
{
845 846 847
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
848 849 850
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_has_child != NULL, FALSE);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_has_child) (tree_model, iter);
851 852
}

7's avatar
7 committed
853
/**
854
 * gtk_tree_model_iter_n_children:
7's avatar
7 committed
855
 * @tree_model: A #GtkTreeModel.
856
 * @iter: The #GtkTreeIter, or NULL.
857
 *
858 859
 * Returns the number of children that @iter has.  If @iter is NULL, then the
 * number of toplevel nodes is returned.
860
 *
861
 * Return value: The number of children of @iter.
7's avatar
7 committed
862
 **/
863
gint
864 865
gtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
866
{
867 868 869
  g_return_val_if_fail (tree_model != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_n_children != NULL, 0);
870 871

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_n_children) (tree_model, iter);
872 873
}

7's avatar
7 committed
874
/**
875
 * gtk_tree_model_iter_nth_child:
7's avatar
7 committed
876
 * @tree_model: A #GtkTreeModel.
877 878 879
 * @iter: The #GtkTreeIter to set to the nth child.
 * @parent: The #GtkTreeIter to get the child from, or NULL.
 * @n: Then index of the desired child.
880
 *
881 882 883 884 885
 * Sets @iter to be the child of @parent, using the given index.  The first
 * index is 0.  If the index is too big, or @parent has no children, @iter is
 * set to an invalid iterator and FALSE is returned.  @parent will remain a
 * valid node after this function has been called.  If @parent is NULL, then the
 * root node is assumed.
886
 *
887
 * Return value: TRUE, if @parent has an nth child.
7's avatar
7 committed
888
 **/
889 890 891 892
gboolean
gtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter,
			       GtkTreeIter  *parent,
7's avatar
7 committed
893
			       gint          n)
894
{
895 896 897 898
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (n >= 0, FALSE);
899 900 901
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_nth_child != NULL, FALSE);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_nth_child) (tree_model, iter, parent, n);
902 903
}

7's avatar
7 committed
904
/**
905 906
 * gtk_tree_model_iter_parent:
 * @tree_model: A #GtkTreeModel
Jonathan Blandford's avatar
Jonathan Blandford committed
907 908
 * @iter: The new #GtkTreeIter to set to the parent.
 * @child: The #GtkTreeIter.
909
 *
910 911 912 913
 * Sets @iter to be the parent of @child.  If @child is at the toplevel, and
 * doesn't have a parent, then @iter is set to an invalid iterator and FALSE
 * is returned.  @child will remain a valid node after this function has been
 * called.
914
 *
915
 * Return value: TRUE, if @iter is set to the parent of @child.
7's avatar
7 committed
916
 **/
917 918 919 920
gboolean
gtk_tree_model_iter_parent (GtkTreeModel *tree_model,
			    GtkTreeIter  *iter,
			    GtkTreeIter  *child)
921
{
922 923 924 925
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (child != NULL, FALSE);
926 927 928
  g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_parent != NULL, FALSE);

  return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_parent) (tree_model, iter, child);
929 930
}

931
/**
932
 * gtk_tree_model_ref_node:
933 934
 * @tree_model: A #GtkTreeModel.
 * @iter: The #GtkTreeIter.
935
 *
936 937 938 939 940 941 942 943 944
 * Lets the tree ref the node.  This is an optional method for models to
 * implement.  To be more specific, models may ignore this call as it exists
 * primarily for performance reasons.
 * 
 * This function is primarily meant as a way for views to let caching model know
 * when nodes are being displayed (and hence, whether or not to cache that
 * node.)  For example, a file-system based model would not want to keep the
 * entire file-heirarchy in memory, just the sections that are currently being
 * displayed by every current view.
945 946
 **/
void
947
gtk_tree_model_ref_node (GtkTreeModel *tree_model,
948 949 950 951 952
			 GtkTreeIter  *iter)
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));

953 954
  if (GTK_TREE_MODEL_GET_IFACE (tree_model)->ref_node)
    (* GTK_TREE_MODEL_GET_IFACE (tree_model)->ref_node) (tree_model, iter);
955 956 957
}

/**
958
 * gtk_tree_model_unref_node:
959 960
 * @tree_model: A #GtkTreeModel.
 * @iter: The #GtkTreeIter.
961
 *
962 963 964 965 966 967
 * Lets the tree unref the node.  This is an optional method for models to
 * implement.  To be more specific, models may ignore this call as it exists
 * primarily for performance reasons.
 *
 * For more information on what this means, please see #gtk_tree_model_ref_node.
 * Please note that nodes that are deleted are not unreffed.
968 969
 **/
void
970
gtk_tree_model_unref_node (GtkTreeModel *tree_model,
971 972 973 974 975
			   GtkTreeIter  *iter)
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));

976 977
  if (GTK_TREE_MODEL_GET_IFACE (tree_model)->unref_node)
    (* GTK_TREE_MODEL_GET_IFACE (tree_model)->unref_node) (tree_model, iter);
978
}
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

/**
 * gtk_tree_model_get:
 * @tree_model: a #GtkTreeModel
 * @iter: a row in @tree_model
 * @Varargs: pairs of column number and value return locations, terminated by -1
 *
 * Gets the value of one or more cells in the row referenced by @iter.
 * The variable argument list should contain integer column numbers,
 * each column number followed by a place to store the value being
 * retrieved.  The list is terminated by a -1. For example, to get a
 * value from column 0 with type %G_TYPE_STRING, you would
 * write: gtk_tree_model_set (model, iter, 0, &place_string_here, -1),
 * where place_string_here is a gchar* to be filled with the string.
 * If appropriate, the returned values have to be freed or unreferenced.
994
 *
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
 **/
void
gtk_tree_model_get (GtkTreeModel *tree_model,
		    GtkTreeIter  *iter,
		    ...)
{
  va_list var_args;

  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));

  va_start (var_args, iter);
  gtk_tree_model_get_valist (tree_model, iter, var_args);
  va_end (var_args);
}

/**
 * gtk_tree_model_get_valist:
 * @tree_model: a #GtkTreeModel
 * @iter: a row in @tree_model
 * @var_args: va_list of column/return location pairs
 *
 * See gtk_tree_model_get(), this version takes a va_list for
 * language bindings to use.
1018
 *
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
 **/
void
gtk_tree_model_get_valist (GtkTreeModel *tree_model,
                           GtkTreeIter  *iter,
                           va_list	var_args)
{
  gint column;

  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));

  column = va_arg (var_args, gint);

  while (column != -1)
    {
      GValue value = { 0, };
      gchar *error = NULL;

      if (column >= gtk_tree_model_get_n_columns (tree_model))
	{
	  g_warning ("%s: Invalid column number %d accessed (remember to end your list of columns with a -1)", G_STRLOC, column);
	  break;
	}

      gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, column, &value);

1044
      G_VALUE_LCOPY (&value, var_args, 0, &error);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
      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;
	}

      g_value_unset (&value);

      column = va_arg (var_args, gint);
    }
}
1061 1062

void
1063 1064 1065 1066 1067
gtk_tree_model_range_changed (GtkTreeModel *tree_model,
			      GtkTreePath  *start_path,
			      GtkTreeIter  *start_iter,
			      GtkTreePath  *end_path,
			      GtkTreeIter  *end_iter)
1068 1069 1070
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1071 1072 1073 1074 1075 1076 1077 1078
  g_return_if_fail (start_path != NULL);
  g_return_if_fail (start_iter != NULL);
  g_return_if_fail (end_path != NULL);
  g_return_if_fail (end_iter != NULL);

  g_signal_emit_by_name (tree_model, "range_changed",
			 start_path, start_iter,
			 end_path, end_iter);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
}

void
gtk_tree_model_inserted (GtkTreeModel *tree_model,
			 GtkTreePath  *path,
			 GtkTreeIter  *iter)
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
  g_return_if_fail (path != NULL);
  g_return_if_fail (iter != NULL);

  g_signal_emit_by_name (tree_model, "inserted", path, iter);
}

void
1095 1096 1097
gtk_tree_model_has_child_toggled (GtkTreeModel *tree_model,
				  GtkTreePath  *path,
				  GtkTreeIter  *iter)
1098 1099 1100 1101 1102 1103
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
  g_return_if_fail (path != NULL);
  g_return_if_fail (iter != NULL);

1104
  g_signal_emit_by_name (tree_model, "has_child_toggled", path, iter);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
}

void
gtk_tree_model_deleted (GtkTreeModel *tree_model,
			GtkTreePath  *path)
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
  g_return_if_fail (path != NULL);

  g_signal_emit_by_name (tree_model, "deleted", path);
}
1117

1118 1119 1120
void
gtk_tree_model_reordered (GtkTreeModel *tree_model,
			  GtkTreePath  *path,
1121
			  GtkTreeIter  *iter,
1122 1123 1124 1125 1126 1127
			  gint         *new_order)
{
  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
  g_return_if_fail (new_order != NULL);

1128
  g_signal_emit_by_name (tree_model, "reordered", path, iter, new_order);
1129 1130
}

1131

Havoc Pennington's avatar
Havoc Pennington committed
1132 1133 1134
/*
 * GtkTreeRowReference
 */
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284

#define ROW_REF_DATA_STRING "gtk-tree-row-refs"

struct _GtkTreeRowReference
{
  GObject *proxy;
  GtkTreeModel *model;
  GtkTreePath *path;
};

typedef struct
{
  GSList *list;
} RowRefList;


static void
release_row_references (gpointer data)
{
  RowRefList *refs = data;
  GSList *tmp_list = NULL;

  tmp_list = refs->list;
  while (tmp_list != NULL)
    {
      GtkTreeRowReference *reference = tmp_list->data;

      if (reference->proxy == (GObject *)reference->model)
	reference->model = NULL;
      reference->proxy = NULL;

      /* we don't free the reference, users are responsible for that. */

      tmp_list = g_slist_next (tmp_list);
    }

  g_slist_free (refs->list);
  g_free (refs);
}

static void
gtk_tree_row_ref_inserted_callback (GObject     *object,
				    GtkTreePath *path,
				    GtkTreeIter *iter,
				    gpointer     data)
{
  RowRefList *refs = g_object_get_data (data, ROW_REF_DATA_STRING);

  GSList *tmp_list;

  if (refs == NULL)
    return;

  /* This function corrects the path stored in the reference to
   * account for an insertion. Note that it's called _after_ the insertion
   * with the path to the newly-inserted row. Which means that
   * the inserted path is in a different "coordinate system" than
   * the old path (e.g. if the inserted path was just before the old path,
   * then inserted path and old path will be the same, and old path must be
   * moved down one).
   */

  tmp_list = refs->list;

  while (tmp_list != NULL)
    {
      GtkTreeRowReference *reference = tmp_list->data;

      if (reference->path)
	{
	  gint depth = gtk_tree_path_get_depth (path);
	  gint ref_depth = gtk_tree_path_get_depth (reference->path);

	  if (ref_depth >= depth)
	    {
	      gint *indices = gtk_tree_path_get_indices (path);
	      gint *ref_indices = gtk_tree_path_get_indices (reference->path);
	      gint i;

	      /* This is the depth that might affect us. */
	      i = depth - 1;

	      if (indices[i] <= ref_indices[i])
		ref_indices[i] += 1;
	    }
	}

      tmp_list = g_slist_next (tmp_list);
    }
}

static void
gtk_tree_row_ref_deleted_callback (GObject     *object,
				   GtkTreePath *path,
				   gpointer     data)
{
  RowRefList *refs = g_object_get_data (data, ROW_REF_DATA_STRING);
  GSList *tmp_list;

  if (refs == NULL)
    return;

  /* This function corrects the path stored in the reference to
   * account for an deletion. Note that it's called _after_ the
   * deletion with the old path of the just-deleted row. Which means
   * that the deleted path is the same now-defunct "coordinate system"
   * as the path saved in the reference, which is what we want to fix.
   *
   * Note that this is different from the situation in "inserted," so
   * while you might think you can cut-and-paste between these
   * functions, it's not going to work. ;-)
   */

  tmp_list = refs->list;

  while (tmp_list != NULL)
    {
      GtkTreeRowReference *reference = tmp_list->data;

      if (reference->path)
	{
	  gint depth = gtk_tree_path_get_depth (path);
	  gint ref_depth = gtk_tree_path_get_depth (reference->path);

	  if (ref_depth >= depth)
	    {
	      /* Need to adjust path upward */
	      gint *indices = gtk_tree_path_get_indices (path);
	      gint *ref_indices = gtk_tree_path_get_indices (reference->path);
	      gint i;

	      i = depth - 1;
	      if (indices[i] < ref_indices[i])
		ref_indices[i] -= 1;
	      else if (indices[i] == ref_indices[i])
		{
		  /* the referenced node itself, or its parent, was
		   * deleted, mark invalid
		   */

		  gtk_tree_path_free (reference->path);
		  reference->path = NULL;
		}
	    }

	}
      tmp_list = g_slist_next (tmp_list);
    }
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
static void
gtk_tree_row_ref_reordered_callback (GObject     *object,
				     GtkTreePath *path,
				     GtkTreeIter *iter,
				     gint        *new_order,
				     gpointer     data)
{
  RowRefList *refs = g_object_get_data (data, ROW_REF_DATA_STRING);
  GSList *tmp_list;
  gint length;

  if (refs == NULL)
    return;

  tmp_list = refs->list;

  while (tmp_list != NULL)
    {
      GtkTreeRowReference *reference = tmp_list->data;

      length = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (reference->model), iter);

      if (length < 2)
	return;

      if ((reference->path) &&
	  (gtk_tree_path_is_ancestor (path, reference->path)))
	{
	  gint ref_depth = gtk_tree_path_get_depth (reference->path);
	  gint depth = gtk_tree_path_get_depth (path);

	  if (ref_depth > depth)
	    {
	      gint i;
	      gint *indices = gtk_tree_path_get_indices (reference->path);

	      for (i = 0; i < length; i++)
		{
		  if (new_order[i] == indices[depth])
		    {
		      indices[depth] = i;
		      return;
		    }
		}
	    }
	}

      tmp_list = g_slist_next (tmp_list);
    }
  
}
     
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
static void
connect_ref_callbacks (GtkTreeModel *model)
{
  g_signal_connect_data (G_OBJECT (model),
                         "inserted",
                         (GCallback) gtk_tree_row_ref_inserted_callback,
                         model,
                         NULL,
                         FALSE,
                         FALSE);

  g_signal_connect_data (G_OBJECT (model),
                         "deleted",
                         (GCallback) gtk_tree_row_ref_deleted_callback,
			 model,
                         NULL,
                         FALSE,
                         FALSE);
1355 1356 1357 1358 1359 1360 1361 1362

  g_signal_connect_data (G_OBJECT (model),
                         "reordered",
                         (GCallback) gtk_tree_row_ref_reordered_callback,
			 model,
                         NULL,
                         FALSE,
                         FALSE);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
}

static void
disconnect_ref_callbacks (GtkTreeModel *model)
{
  g_signal_handlers_disconnect_matched (G_OBJECT (model),
                                        G_SIGNAL_MATCH_FUNC,
                                        0, 0, NULL,
					gtk_tree_row_ref_inserted_callback,
					NULL);
  g_signal_handlers_disconnect_matched (G_OBJECT (model),
                                        G_SIGNAL_MATCH_FUNC,
                                        0, 0, NULL,
					gtk_tree_row_ref_deleted_callback,
					NULL);
1378 1379 1380 1381 1382
  g_signal_handlers_disconnect_matched (G_OBJECT (model),
                                        G_SIGNAL_MATCH_FUNC,
                                        0, 0, NULL,
					gtk_tree_row_ref_reordered_callback,
					NULL);
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
}

GtkTreeRowReference *
gtk_tree_row_reference_new (GtkTreeModel *model,
                            GtkTreePath  *path)
{
  g_return_val_if_fail (model != NULL, NULL);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  return gtk_tree_row_reference_new_proxy (G_OBJECT (model), model, path);
}

GtkTreeRowReference *
gtk_tree_row_reference_new_proxy (GObject      *proxy,
				  GtkTreeModel *model,
				  GtkTreePath  *path)
{
  GtkTreeRowReference *reference;
  RowRefList *refs;

  g_return_val_if_fail (G_IS_OBJECT (proxy), NULL);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  reference = g_new (GtkTreeRowReference, 1);

  reference->proxy = proxy;
  reference->model = model;
  reference->path = gtk_tree_path_copy (path);

  refs = g_object_get_data (G_OBJECT (proxy), ROW_REF_DATA_STRING);

  if (refs == NULL)
    {
      refs = g_new (RowRefList, 1);
      refs->list = NULL;

1421
      if (G_OBJECT (model) == proxy)
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	connect_ref_callbacks (model);

      g_object_set_data_full (G_OBJECT (proxy),
			      ROW_REF_DATA_STRING,
                              refs, release_row_references);
    }

  refs->list = g_slist_prepend (refs->list, reference);

  return reference;
}

GtkTreePath *
gtk_tree_row_reference_get_path (GtkTreeRowReference *reference)
{
  g_return_val_if_fail (reference != NULL, NULL);

  if (reference->proxy == NULL)
    return NULL;

  if (reference->path == NULL)
    return NULL;

  return gtk_tree_path_copy (reference->path);
}

void
gtk_tree_row_reference_free (GtkTreeRowReference *reference)
{
  RowRefList *refs;

  g_return_if_fail (reference != NULL);

  if (reference->proxy)
    {
      refs = g_object_get_data (G_OBJECT (reference->proxy), ROW_REF_DATA_STRING);

      if (refs == NULL)
        {
          g_warning (G_STRLOC": bad row reference, proxy has no outstanding row references");
          return;
        }

      refs->list = g_slist_remove (refs->list, reference);

      if (refs->list == NULL)
        {
          disconnect_ref_callbacks (reference->model);
          g_object_set_data (G_OBJECT (reference->proxy),
                             ROW_REF_DATA_STRING,
                             NULL);
        }
    }

  if (reference->path)
    gtk_tree_path_free (reference->path);

  g_free (reference);
}

void
gtk_tree_row_reference_inserted (GObject     *proxy,
				 GtkTreePath *path)
{
  g_return_if_fail (G_IS_OBJECT (proxy));

  gtk_tree_row_ref_inserted_callback (NULL, path, NULL, proxy);
  
}

void
gtk_tree_row_reference_deleted (GObject     *proxy,
				GtkTreePath *path)
{
  g_return_if_fail (G_IS_OBJECT (proxy));

  gtk_tree_row_ref_deleted_callback (NULL, path, proxy);
}
1500 1501 1502 1503 1504 1505 1506 1507