]> Pileus Git - ~andy/gtk/blob - gtk/gtktreemodel.c
ccc774f456bcd3896bc48477a86b336cbb1e6f4f
[~andy/gtk] / gtk / gtktreemodel.c
1 /* gtktreemodel.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
25 #include <gobject/gvaluecollector.h>
26 #include "gtkalias.h"
27 #include "gtktreemodel.h"
28 #include "gtktreeview.h"
29 #include "gtktreeprivate.h"
30 #include "gtkmarshalers.h"
31
32
33 #define INITIALIZE_TREE_ITER(Iter) \
34     G_STMT_START{ \
35       (Iter)->stamp = 0; \
36       (Iter)->user_data  = NULL; \
37       (Iter)->user_data2 = NULL; \
38       (Iter)->user_data3 = NULL; \
39     }G_STMT_END
40
41 #define ROW_REF_DATA_STRING "gtk-tree-row-refs"
42
43 enum {
44   ROW_CHANGED,
45   ROW_INSERTED,
46   ROW_HAS_CHILD_TOGGLED,
47   ROW_DELETED,
48   ROWS_REORDERED,
49   LAST_SIGNAL
50 };
51
52 static guint tree_model_signals[LAST_SIGNAL] = { 0 };
53
54 struct _GtkTreePath
55 {
56   gint depth;
57   gint *indices;
58 };
59
60 typedef struct
61 {
62   GSList *list;
63 } RowRefList;
64
65 static void      gtk_tree_model_base_init   (gpointer           g_class);
66
67 /* custom closures */
68 static void      row_inserted_marshal       (GClosure          *closure,
69                                              GValue /* out */  *return_value,
70                                              guint              n_param_value,
71                                              const GValue      *param_values,
72                                              gpointer           invocation_hint,
73                                              gpointer           marshal_data);
74 static void      row_deleted_marshal        (GClosure          *closure,
75                                              GValue /* out */  *return_value,
76                                              guint              n_param_value,
77                                              const GValue      *param_values,
78                                              gpointer           invocation_hint,
79                                              gpointer           marshal_data);
80 static void      rows_reordered_marshal     (GClosure          *closure,
81                                              GValue /* out */  *return_value,
82                                              guint              n_param_value,
83                                              const GValue      *param_values,
84                                              gpointer           invocation_hint,
85                                              gpointer           marshal_data);
86
87 static void      gtk_tree_row_ref_inserted  (RowRefList        *refs,
88                                              GtkTreePath       *path,
89                                              GtkTreeIter       *iter);
90 static void      gtk_tree_row_ref_deleted   (RowRefList        *refs,
91                                              GtkTreePath       *path);
92 static void      gtk_tree_row_ref_reordered (RowRefList        *refs,
93                                              GtkTreePath       *path,
94                                              GtkTreeIter       *iter,
95                                              gint              *new_order);
96
97 GType
98 gtk_tree_model_get_type (void)
99 {
100   static GType tree_model_type = 0;
101
102   if (! tree_model_type)
103     {
104       static const GTypeInfo tree_model_info =
105       {
106         sizeof (GtkTreeModelIface), /* class_size */
107         gtk_tree_model_base_init,   /* base_init */
108         NULL,           /* base_finalize */
109         NULL,
110         NULL,           /* class_finalize */
111         NULL,           /* class_data */
112         0,
113         0,              /* n_preallocs */
114         NULL
115       };
116
117       tree_model_type =
118         g_type_register_static (G_TYPE_INTERFACE, "GtkTreeModel",
119                                 &tree_model_info, 0);
120
121       g_type_interface_add_prerequisite (tree_model_type, G_TYPE_OBJECT);
122     }
123
124   return tree_model_type;
125 }
126
127 static void
128 gtk_tree_model_base_init (gpointer g_class)
129 {
130   static gboolean initialized = FALSE;
131   GClosure *closure;
132
133   if (! initialized)
134     {
135       GType row_inserted_params[2];
136       GType row_deleted_params[1];
137       GType rows_reordered_params[3];
138
139       row_inserted_params[0] = GTK_TYPE_TREE_PATH;
140       row_inserted_params[1] = GTK_TYPE_TREE_ITER;
141
142       row_deleted_params[0] = GTK_TYPE_TREE_PATH;
143
144       rows_reordered_params[0] = GTK_TYPE_TREE_PATH;
145       rows_reordered_params[1] = GTK_TYPE_TREE_ITER;
146       rows_reordered_params[2] = G_TYPE_POINTER;
147
148       tree_model_signals[ROW_CHANGED] =
149         g_signal_new ("row_changed",
150                       GTK_TYPE_TREE_MODEL,
151                       G_SIGNAL_RUN_LAST,
152                       G_STRUCT_OFFSET (GtkTreeModelIface, row_changed),
153                       NULL, NULL,
154                       _gtk_marshal_VOID__BOXED_BOXED,
155                       G_TYPE_NONE, 2,
156                       GTK_TYPE_TREE_PATH,
157                       GTK_TYPE_TREE_ITER);
158
159       /* We need to get notification about structure changes
160        * to update row references., so instead of using the
161        * standard g_signal_new() with an offset into our interface
162        * structure, we use a customs closures for the class
163        * closures (default handlers) that first update row references
164        * and then calls the function from the interface structure.
165        *
166        * The reason we don't simply update the row references from
167        * the wrapper functions (gtk_tree_model_row_inserted(), etc.)
168        * is to keep proper ordering with respect to signal handlers
169        * connected normally and after.
170        */
171       closure = g_closure_new_simple (sizeof (GClosure), NULL);
172       g_closure_set_marshal (closure, row_inserted_marshal);
173       tree_model_signals[ROW_INSERTED] =
174         g_signal_newv ("row_inserted",
175                        GTK_TYPE_TREE_MODEL,
176                        G_SIGNAL_RUN_FIRST,
177                        closure,
178                        NULL, NULL,
179                        _gtk_marshal_VOID__BOXED_BOXED,
180                        G_TYPE_NONE, 2,
181                        row_inserted_params);
182
183       tree_model_signals[ROW_HAS_CHILD_TOGGLED] =
184         g_signal_new ("row_has_child_toggled",
185                       GTK_TYPE_TREE_MODEL,
186                       G_SIGNAL_RUN_LAST,
187                       G_STRUCT_OFFSET (GtkTreeModelIface, row_has_child_toggled),
188                       NULL, NULL,
189                       _gtk_marshal_VOID__BOXED_BOXED,
190                       G_TYPE_NONE, 2,
191                       GTK_TYPE_TREE_PATH,
192                       GTK_TYPE_TREE_ITER);
193
194       closure = g_closure_new_simple (sizeof (GClosure), NULL);
195       g_closure_set_marshal (closure, row_deleted_marshal);
196       tree_model_signals[ROW_DELETED] =
197         g_signal_newv ("row_deleted",
198                        GTK_TYPE_TREE_MODEL,
199                        G_SIGNAL_RUN_FIRST,
200                        closure,
201                        NULL, NULL,
202                        _gtk_marshal_VOID__BOXED,
203                        G_TYPE_NONE, 1,
204                        row_deleted_params);
205
206       closure = g_closure_new_simple (sizeof (GClosure), NULL);
207       g_closure_set_marshal (closure, rows_reordered_marshal);
208       tree_model_signals[ROWS_REORDERED] =
209         g_signal_newv ("rows_reordered",
210                        GTK_TYPE_TREE_MODEL,
211                        G_SIGNAL_RUN_FIRST,
212                        closure,
213                        NULL, NULL,
214                        _gtk_marshal_VOID__BOXED_BOXED_POINTER,
215                        G_TYPE_NONE, 3,
216                        rows_reordered_params);
217       initialized = TRUE;
218     }
219 }
220
221 static void
222 row_inserted_marshal (GClosure          *closure,
223                       GValue /* out */  *return_value,
224                       guint              n_param_values,
225                       const GValue      *param_values,
226                       gpointer           invocation_hint,
227                       gpointer           marshal_data)
228 {
229   GtkTreeModelIface *iface;
230
231   void (* row_inserted_callback) (GtkTreeModel *tree_model,
232                                   GtkTreePath *path,
233                                   GtkTreeIter *iter) = 0;
234             
235   GObject *model = g_value_get_object (param_values + 0);
236   GtkTreePath *path = (GtkTreePath *)g_value_get_boxed (param_values + 1);
237   GtkTreeIter *iter = (GtkTreeIter *)g_value_get_boxed (param_values + 2);
238
239   /* first, we need to update internal row references */
240   gtk_tree_row_ref_inserted ((RowRefList *)g_object_get_data (model, ROW_REF_DATA_STRING),
241                              path, iter);
242                                
243   /* fetch the interface ->row_inserted implementation */
244   iface = GTK_TREE_MODEL_GET_IFACE (model);
245   row_inserted_callback = G_STRUCT_MEMBER (gpointer, iface,
246                               G_STRUCT_OFFSET (GtkTreeModelIface,
247                                                row_inserted));
248
249   /* Call that default signal handler, it if has been set */                                                         
250   if (row_inserted_callback)
251     row_inserted_callback (GTK_TREE_MODEL (model), path, iter);
252 }
253
254 static void
255 row_deleted_marshal (GClosure          *closure,
256                      GValue /* out */  *return_value,
257                      guint              n_param_values,
258                      const GValue      *param_values,
259                      gpointer           invocation_hint,
260                      gpointer           marshal_data)
261 {
262   GtkTreeModelIface *iface;
263   void (* row_deleted_callback) (GtkTreeModel *tree_model,
264                                  GtkTreePath  *path) = 0;                                 
265   GObject *model = g_value_get_object (param_values + 0);
266   GtkTreePath *path = (GtkTreePath *)g_value_get_boxed (param_values + 1);
267  
268
269   /* first, we need to update internal row references */
270   gtk_tree_row_ref_deleted ((RowRefList *)g_object_get_data (model, ROW_REF_DATA_STRING),
271                             path);
272
273   /* fetch the interface ->row_deleted implementation */
274   iface = GTK_TREE_MODEL_GET_IFACE (model);
275   row_deleted_callback = G_STRUCT_MEMBER (gpointer, iface,
276                               G_STRUCT_OFFSET (GtkTreeModelIface,
277                                                row_deleted));
278                               
279   /* Call that default signal handler, it if has been set */
280   if (row_deleted_callback)
281     row_deleted_callback (GTK_TREE_MODEL (model), path);
282 }
283
284 static void
285 rows_reordered_marshal (GClosure          *closure,
286                         GValue /* out */  *return_value,
287                         guint              n_param_values,
288                         const GValue      *param_values,
289                         gpointer           invocation_hint,
290                         gpointer           marshal_data)
291 {
292   GtkTreeModelIface *iface;
293   void (* rows_reordered_callback) (GtkTreeModel *tree_model,
294                                     GtkTreePath  *path,
295                                     GtkTreeIter  *iter,
296                                     gint         *new_order);
297             
298   GObject *model = g_value_get_object (param_values + 0);
299   GtkTreePath *path = (GtkTreePath *)g_value_get_boxed (param_values + 1);
300   GtkTreeIter *iter = (GtkTreeIter *)g_value_get_boxed (param_values + 2);
301   gint *new_order = (gint *)g_value_get_pointer (param_values + 3);
302   
303   /* first, we need to update internal row references */
304   gtk_tree_row_ref_reordered ((RowRefList *)g_object_get_data (model, ROW_REF_DATA_STRING),
305                               path, iter, new_order);
306
307   /* fetch the interface ->rows_reordered implementation */
308   iface = GTK_TREE_MODEL_GET_IFACE (model);
309   rows_reordered_callback = G_STRUCT_MEMBER (gpointer, iface,
310                               G_STRUCT_OFFSET (GtkTreeModelIface,
311                                                rows_reordered));
312
313   /* Call that default signal handler, it if has been set */
314   if (rows_reordered_callback)
315     rows_reordered_callback (GTK_TREE_MODEL (model), path, iter, new_order);
316 }
317
318 /**
319  * gtk_tree_path_new:
320  *
321  * Creates a new #GtkTreePath.  This structure refers to a row.
322  *
323  * Return value: A newly created #GtkTreePath.
324  **/
325 /* GtkTreePath Operations */
326 GtkTreePath *
327 gtk_tree_path_new (void)
328 {
329   GtkTreePath *retval;
330   retval = (GtkTreePath *) g_new (GtkTreePath, 1);
331   retval->depth = 0;
332   retval->indices = NULL;
333
334   return retval;
335 }
336
337 /**
338  * gtk_tree_path_new_from_string:
339  * @path: The string representation of a path.
340  *
341  * Creates a new #GtkTreePath initialized to @path.  @path is expected to be a
342  * colon separated list of numbers.  For example, the string "10:4:0" would
343  * create a path of depth 3 pointing to the 11th child of the root node, the 5th
344  * child of that 11th child, and the 1st child of that 5th child.  If an invalid
345  * path string is passed in, %NULL is returned.
346  *
347  * Return value: A newly-created #GtkTreePath, or %NULL
348  **/
349 GtkTreePath *
350 gtk_tree_path_new_from_string (const gchar *path)
351 {
352   GtkTreePath *retval;
353   const gchar *orig_path = path;
354   gchar *ptr;
355   gint i;
356
357   g_return_val_if_fail (path != NULL, NULL);
358   g_return_val_if_fail (*path != '\000', NULL);
359
360   retval = gtk_tree_path_new ();
361
362   while (1)
363     {
364       i = strtol (path, &ptr, 10);
365       gtk_tree_path_append_index (retval, i);
366
367       if (i < 0)
368         {
369           g_warning (G_STRLOC ": Negative numbers in path %s passed to gtk_tree_path_new_from_string", orig_path);
370           gtk_tree_path_free (retval);
371           return NULL;
372         }
373       if (*ptr == '\000')
374         break;
375       if (ptr == path || *ptr != ':')
376         {
377           g_warning (G_STRLOC ": Invalid path %s passed to gtk_tree_path_new_from_string", orig_path);
378           gtk_tree_path_free (retval);
379           return NULL;
380         }
381       path = ptr + 1;
382     }
383
384   return retval;
385 }
386
387 /**
388  * gtk_tree_path_new_from_indices:
389  * @first_index: first integer
390  * @varargs: list of integers terminated by -1
391  *
392  * Creates a new path with @first_index and @varargs as indices.
393  *
394  * Return value: A newly created GtkTreePath.
395  *
396  * Since: 2.2
397  **/
398 GtkTreePath *
399 gtk_tree_path_new_from_indices (gint first_index,
400                                 ...)
401 {
402   int arg;
403   va_list args;
404   GtkTreePath *path;
405
406   path = gtk_tree_path_new ();
407
408   va_start (args, first_index);
409   arg = first_index;
410
411   while (arg != -1)
412     {
413       gtk_tree_path_append_index (path, arg);
414       arg = va_arg (args, gint);
415     }
416
417   va_end (args);
418
419   return path;
420 }
421
422 /**
423  * gtk_tree_path_to_string:
424  * @path: A #GtkTreePath
425  *
426  * Generates a string representation of the path.  This string is a ':'
427  * separated list of numbers.  For example, "4:10:0:3" would be an acceptable return value for this string.
428  *
429  * Return value: A newly-allocated string.  Must be freed with g_free().
430  **/
431 gchar *
432 gtk_tree_path_to_string (GtkTreePath *path)
433 {
434   gchar *retval, *ptr;
435   gint i;
436
437   g_return_val_if_fail (path != NULL, NULL);
438
439   if (path->depth == 0)
440     return NULL;
441
442   ptr = retval = (gchar *) g_new0 (char *, path->depth*8);
443   g_sprintf (retval, "%d", path->indices[0]);
444   while (*ptr != '\000')
445     ptr++;
446
447   for (i = 1; i < path->depth; i++)
448     {
449       g_sprintf (ptr, ":%d", path->indices[i]);
450       while (*ptr != '\000')
451         ptr++;
452     }
453
454   return retval;
455 }
456
457 /**
458  * gtk_tree_path_new_first:
459  *
460  * Creates a new #GtkTreePath.  The string representation of this path is "0"
461  *
462  * Return value: A new #GtkTreePath.
463  **/
464 GtkTreePath *
465 gtk_tree_path_new_first (void)
466 {
467   GtkTreePath *retval;
468
469   retval = gtk_tree_path_new ();
470   gtk_tree_path_append_index (retval, 0);
471
472   return retval;
473 }
474
475 /**
476  * gtk_tree_path_append_index:
477  * @path: A #GtkTreePath.
478  * @index_: The index.
479  *
480  * Appends a new index to a path.  As a result, the depth of the path is
481  * increased.
482  **/
483 void
484 gtk_tree_path_append_index (GtkTreePath *path,
485                             gint         index)
486 {
487   g_return_if_fail (path != NULL);
488   g_return_if_fail (index >= 0);
489
490   path->depth += 1;
491   path->indices = g_realloc (path->indices, path->depth * sizeof(gint));
492   path->indices[path->depth - 1] = index;
493 }
494
495 /**
496  * gtk_tree_path_prepend_index:
497  * @path: A #GtkTreePath.
498  * @index_: The index.
499  *
500  * Prepends a new index to a path.  As a result, the depth of the path is
501  * increased.
502  **/
503 void
504 gtk_tree_path_prepend_index (GtkTreePath *path,
505                              gint       index)
506 {
507   gint *new_indices;
508
509   (path->depth)++;
510   new_indices = g_new (gint, path->depth);
511
512   if (path->indices == NULL)
513     {
514       path->indices = new_indices;
515       path->indices[0] = index;
516       return;
517     }
518   memcpy (new_indices + 1, path->indices, (path->depth - 1)*sizeof (gint));
519   g_free (path->indices);
520   path->indices = new_indices;
521   path->indices[0] = index;
522 }
523
524 /**
525  * gtk_tree_path_get_depth:
526  * @path: A #GtkTreePath.
527  *
528  * Returns the current depth of @path.
529  *
530  * Return value: The depth of @path
531  **/
532 gint
533 gtk_tree_path_get_depth (GtkTreePath *path)
534 {
535   g_return_val_if_fail (path != NULL, 0);
536
537   return path->depth;
538 }
539
540 /**
541  * gtk_tree_path_get_indices:
542  * @path: A #GtkTreePath.
543  *
544  * Returns the current indices of @path.  This is an array of integers, each
545  * representing a node in a tree.  This value should not be freed.
546  *
547  * Return value: The current indices, or %NULL.
548  **/
549 gint *
550 gtk_tree_path_get_indices (GtkTreePath *path)
551 {
552   g_return_val_if_fail (path != NULL, NULL);
553
554   return path->indices;
555 }
556
557 /**
558  * gtk_tree_path_free:
559  * @path: A #GtkTreePath.
560  *
561  * Frees @path.
562  **/
563 void
564 gtk_tree_path_free (GtkTreePath *path)
565 {
566   if (!path)
567     return;
568
569   g_free (path->indices);
570   g_free (path);
571 }
572
573 /**
574  * gtk_tree_path_copy:
575  * @path: A #GtkTreePath.
576  *
577  * Creates a new #GtkTreePath as a copy of @path.
578  *
579  * Return value: A new #GtkTreePath.
580  **/
581 GtkTreePath *
582 gtk_tree_path_copy (const GtkTreePath *path)
583 {
584   GtkTreePath *retval;
585
586   g_return_val_if_fail (path != NULL, NULL);
587
588   retval = g_new (GtkTreePath, 1);
589   retval->depth = path->depth;
590   retval->indices = g_new (gint, path->depth);
591   memcpy (retval->indices, path->indices, path->depth * sizeof (gint));
592   return retval;
593 }
594
595 GType
596 gtk_tree_path_get_type (void)
597 {
598   static GType our_type = 0;
599   
600   if (our_type == 0)
601     our_type = g_boxed_type_register_static ("GtkTreePath",
602                                              (GBoxedCopyFunc) gtk_tree_path_copy,
603                                              (GBoxedFreeFunc) gtk_tree_path_free);
604
605   return our_type;
606 }
607
608 /**
609  * gtk_tree_path_compare:
610  * @a: A #GtkTreePath.
611  * @b: A #GtkTreePath to compare with.
612  *
613  * Compares two paths.  If @a appears before @b in a tree, then -1 is returned.
614  * If @b appears before @a, then 1 is returned.  If the two nodes are equal,
615  * then 0 is returned.
616  *
617  * Return value: The relative positions of @a and @b
618  **/
619 gint
620 gtk_tree_path_compare (const GtkTreePath *a,
621                        const GtkTreePath *b)
622 {
623   gint p = 0, q = 0;
624
625   g_return_val_if_fail (a != NULL, 0);
626   g_return_val_if_fail (b != NULL, 0);
627   g_return_val_if_fail (a->depth > 0, 0);
628   g_return_val_if_fail (b->depth > 0, 0);
629
630   do
631     {
632       if (a->indices[p] == b->indices[q])
633         continue;
634       return (a->indices[p] < b->indices[q]?-1:1);
635     }
636   while (++p < a->depth && ++q < b->depth);
637   if (a->depth == b->depth)
638     return 0;
639   return (a->depth < b->depth?-1:1);
640 }
641
642 /**
643  * gtk_tree_path_is_ancestor:
644  * @path: a #GtkTreePath
645  * @descendant: another #GtkTreePath
646  *
647  * Returns %TRUE if @descendant is a descendant of @path.
648  *
649  * Return value: %TRUE if @descendant is contained inside @path
650  **/
651 gboolean
652 gtk_tree_path_is_ancestor (GtkTreePath *path,
653                            GtkTreePath *descendant)
654 {
655   gint i;
656
657   g_return_val_if_fail (path != NULL, FALSE);
658   g_return_val_if_fail (descendant != NULL, FALSE);
659
660   /* can't be an ancestor if we're deeper */
661   if (path->depth >= descendant->depth)
662     return FALSE;
663
664   i = 0;
665   while (i < path->depth)
666     {
667       if (path->indices[i] != descendant->indices[i])
668         return FALSE;
669       ++i;
670     }
671
672   return TRUE;
673 }
674
675 /**
676  * gtk_tree_path_is_descendant:
677  * @path: a #GtkTreePath
678  * @ancestor: another #GtkTreePath
679  *
680  * Returns %TRUE if @path is a descendant of @ancestor.
681  *
682  * Return value: %TRUE if @ancestor contains @path somewhere below it
683  **/
684 gboolean
685 gtk_tree_path_is_descendant (GtkTreePath *path,
686                              GtkTreePath *ancestor)
687 {
688   gint i;
689
690   g_return_val_if_fail (path != NULL, FALSE);
691   g_return_val_if_fail (ancestor != NULL, FALSE);
692
693   /* can't be a descendant if we're shallower in the tree */
694   if (path->depth <= ancestor->depth)
695     return FALSE;
696
697   i = 0;
698   while (i < ancestor->depth)
699     {
700       if (path->indices[i] != ancestor->indices[i])
701         return FALSE;
702       ++i;
703     }
704
705   return TRUE;
706 }
707
708
709 /**
710  * gtk_tree_path_next:
711  * @path: A #GtkTreePath.
712  *
713  * Moves the @path to point to the next node at the current depth.
714  **/
715 void
716 gtk_tree_path_next (GtkTreePath *path)
717 {
718   g_return_if_fail (path != NULL);
719   g_return_if_fail (path->depth > 0);
720
721   path->indices[path->depth - 1] ++;
722 }
723
724 /**
725  * gtk_tree_path_prev:
726  * @path: A #GtkTreePath.
727  *
728  * Moves the @path to point to the previous node at the current depth, if it exists.
729  *
730  * Return value: %TRUE if @path has a previous node, and the move was made.
731  **/
732 gboolean
733 gtk_tree_path_prev (GtkTreePath *path)
734 {
735   g_return_val_if_fail (path != NULL, FALSE);
736
737   if (path->indices[path->depth - 1] == 0)
738     return FALSE;
739
740   path->indices[path->depth - 1] -= 1;
741
742   return TRUE;
743 }
744
745 /**
746  * gtk_tree_path_up:
747  * @path: A #GtkTreePath.
748  *
749  * Moves the @path to point to its parent node, if it has a parent.
750  *
751  * Return value: %TRUE if @path has a parent, and the move was made.
752  **/
753 gboolean
754 gtk_tree_path_up (GtkTreePath *path)
755 {
756   g_return_val_if_fail (path != NULL, FALSE);
757
758   if (path->depth == 0)
759     return FALSE;
760
761   path->depth--;
762
763   return TRUE;
764 }
765
766 /**
767  * gtk_tree_path_down:
768  * @path: A #GtkTreePath.
769  *
770  * Moves @path to point to the first child of the current path.
771  **/
772 void
773 gtk_tree_path_down (GtkTreePath *path)
774 {
775   g_return_if_fail (path != NULL);
776
777   gtk_tree_path_append_index (path, 0);
778 }
779
780 /**
781  * gtk_tree_iter_copy:
782  * @iter: A #GtkTreeIter.
783  *
784  * Creates a dynamically allocated tree iterator as a copy of @iter.  This
785  * function is not intended for use in applications, because you can just copy
786  * the structs by value (<literal>GtkTreeIter new_iter = iter;</literal>).  You
787  * must free this iter with gtk_tree_iter_free ().
788  *
789  * Return value: a newly-allocated copy of @iter.
790  **/
791 GtkTreeIter *
792 gtk_tree_iter_copy (GtkTreeIter *iter)
793 {
794   GtkTreeIter *retval;
795
796   g_return_val_if_fail (iter != NULL, NULL);
797
798   retval = g_new (GtkTreeIter, 1);
799   *retval = *iter;
800
801   return retval;
802 }
803
804 /**
805  * gtk_tree_iter_free:
806  * @iter: A dynamically allocated tree iterator.
807  *
808  * Frees an iterator that has been allocated on the heap.  This function is
809  * mainly used for language bindings.
810  **/
811 void
812 gtk_tree_iter_free (GtkTreeIter *iter)
813 {
814   g_return_if_fail (iter != NULL);
815
816   g_free (iter);
817 }
818
819 GType
820 gtk_tree_iter_get_type (void)
821 {
822   static GType our_type = 0;
823   
824   if (our_type == 0)
825     our_type = g_boxed_type_register_static ("GtkTreeIter",
826                                              (GBoxedCopyFunc) gtk_tree_iter_copy,
827                                              (GBoxedFreeFunc) gtk_tree_iter_free);
828
829   return our_type;
830 }
831
832 /**
833  * gtk_tree_model_get_flags:
834  * @tree_model: A #GtkTreeModel.
835  *
836  * Returns a set of flags supported by this interface.  The flags are a bitwise
837  * combination of #GtkTreeModelFlags.  The flags supported should not change
838  * during the lifecycle of the tree_model.
839  *
840  * Return value: The flags supported by this interface.
841  **/
842 GtkTreeModelFlags
843 gtk_tree_model_get_flags (GtkTreeModel *tree_model)
844 {
845   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);
846
847   if (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_flags)
848     return (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_flags) (tree_model);
849
850   return 0;
851 }
852
853 /**
854  * gtk_tree_model_get_n_columns:
855  * @tree_model: A #GtkTreeModel.
856  *
857  * Returns the number of columns supported by @tree_model.
858  *
859  * Return value: The number of columns.
860  **/
861 gint
862 gtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
863 {
864   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);
865   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_n_columns != NULL, 0);
866
867   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_n_columns) (tree_model);
868 }
869
870 /**
871  * gtk_tree_model_get_column_type:
872  * @tree_model: A #GtkTreeModel.
873  * @index_: The column index.
874  *
875  * Returns the type of the column.
876  *
877  * Return value: The type of the column.
878  **/
879 GType
880 gtk_tree_model_get_column_type (GtkTreeModel *tree_model,
881                                 gint          index)
882 {
883   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), G_TYPE_INVALID);
884   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_column_type != NULL, G_TYPE_INVALID);
885   g_return_val_if_fail (index >= 0, G_TYPE_INVALID);
886
887   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_column_type) (tree_model, index);
888 }
889
890 /**
891  * gtk_tree_model_get_iter:
892  * @tree_model: A #GtkTreeModel.
893  * @iter: The uninitialized #GtkTreeIter.
894  * @path: The #GtkTreePath.
895  *
896  * Sets @iter to a valid iterator pointing to @path.
897  *
898  * Return value: %TRUE, if @iter was set.
899  **/
900 gboolean
901 gtk_tree_model_get_iter (GtkTreeModel *tree_model,
902                          GtkTreeIter  *iter,
903                          GtkTreePath  *path)
904 {
905   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
906   g_return_val_if_fail (iter != NULL, FALSE);
907   g_return_val_if_fail (path != NULL, FALSE);
908   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_iter != NULL, FALSE);
909   g_return_val_if_fail (path->depth > 0, FALSE);
910
911   INITIALIZE_TREE_ITER (iter);
912
913   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_iter) (tree_model, iter, path);
914 }
915
916 /**
917  * gtk_tree_model_get_iter_from_string:
918  * @tree_model: A #GtkTreeModel.
919  * @iter: An uninitialized #GtkTreeIter.
920  * @path_string: A string representation of a #GtkTreePath.
921  *
922  * Sets @iter to a valid iterator pointing to @path_string, if it
923  * exists. Otherwise, @iter is left invalid and %FALSE is returned.
924  *
925  * Return value: %TRUE, if @iter was set.
926  **/
927 gboolean
928 gtk_tree_model_get_iter_from_string (GtkTreeModel *tree_model,
929                                      GtkTreeIter  *iter,
930                                      const gchar  *path_string)
931 {
932   gboolean retval;
933   GtkTreePath *path;
934
935   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
936   g_return_val_if_fail (iter != NULL, FALSE);
937   g_return_val_if_fail (path_string != NULL, FALSE);
938   
939   path = gtk_tree_path_new_from_string (path_string);
940   
941   g_return_val_if_fail (path != NULL, FALSE);
942
943   retval = gtk_tree_model_get_iter (tree_model, iter, path);
944   gtk_tree_path_free (path);
945   
946   return retval;
947 }
948
949 /**
950  * gtk_tree_model_get_string_from_iter:
951  * @tree_model: A #GtkTreeModel.
952  * @iter: An #GtkTreeIter.
953  *
954  * Generates a string representation of the iter. This string is a ':'
955  * separated list of numbers. For example, "4:10:0:3" would be an
956  * acceptable return value for this string.
957  *
958  * Return value: A newly-allocated string. Must be freed with g_free().
959  *
960  * Since: 2.2
961  **/
962 gchar *
963 gtk_tree_model_get_string_from_iter (GtkTreeModel *tree_model,
964                                      GtkTreeIter  *iter)
965 {
966   GtkTreePath *path;
967   gchar *ret;
968
969   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), NULL);
970   g_return_val_if_fail (iter != NULL, NULL);
971
972   path = gtk_tree_model_get_path (tree_model, iter);
973
974   g_return_val_if_fail (path != NULL, NULL);
975
976   ret = gtk_tree_path_to_string (path);
977   gtk_tree_path_free (path);
978
979   return ret;
980 }
981
982 /**
983  * gtk_tree_model_get_iter_first:
984  * @tree_model: A #GtkTreeModel.
985  * @iter: The uninitialized #GtkTreeIter.
986  * 
987  * Initializes @iter with the first iterator in the tree (the one at the path
988  * "0") and returns %TRUE.  Returns %FALSE if the tree is empty.
989  * 
990  * Return value: %TRUE, if @iter was set.
991  **/
992 gboolean
993 gtk_tree_model_get_iter_first (GtkTreeModel *tree_model,
994                                GtkTreeIter  *iter)
995 {
996   GtkTreePath *path;
997   gboolean retval;
998
999   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1000   g_return_val_if_fail (iter != NULL, FALSE);
1001
1002   path = gtk_tree_path_new_first ();
1003   retval = gtk_tree_model_get_iter (tree_model, iter, path);
1004   gtk_tree_path_free (path);
1005
1006   return retval;
1007 }
1008
1009 /**
1010  * gtk_tree_model_get_path:
1011  * @tree_model: A #GtkTreeModel.
1012  * @iter: The #GtkTreeIter.
1013  *
1014  * Returns a newly-created #GtkTreePath referenced by @iter.  This path should
1015  * be freed with gtk_tree_path_free().
1016  *
1017  * Return value: a newly-created #GtkTreePath.
1018  **/
1019 GtkTreePath *
1020 gtk_tree_model_get_path (GtkTreeModel *tree_model,
1021                          GtkTreeIter  *iter)
1022 {
1023   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), NULL);
1024   g_return_val_if_fail (iter != NULL, NULL);
1025   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_path != NULL, NULL);
1026
1027   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_path) (tree_model, iter);
1028 }
1029
1030 /**
1031  * gtk_tree_model_get_value:
1032  * @tree_model: A #GtkTreeModel.
1033  * @iter: The #GtkTreeIter.
1034  * @column: The column to lookup the value at.
1035  * @value: An empty #GValue to set.
1036  *
1037  * Sets initializes and sets @value to that at @column.  When done with @value,
1038  * g_value_unset() needs to be called to free any allocated memory.
1039  **/
1040 void
1041 gtk_tree_model_get_value (GtkTreeModel *tree_model,
1042                           GtkTreeIter  *iter,
1043                           gint          column,
1044                           GValue       *value)
1045 {
1046   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1047   g_return_if_fail (iter != NULL);
1048   g_return_if_fail (value != NULL);
1049   g_return_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->get_value != NULL);
1050
1051   (* GTK_TREE_MODEL_GET_IFACE (tree_model)->get_value) (tree_model, iter, column, value);
1052 }
1053
1054 /**
1055  * gtk_tree_model_iter_next:
1056  * @tree_model: A #GtkTreeModel.
1057  * @iter: The #GtkTreeIter.
1058  *
1059  * Sets @iter to point to the node following it at the current level.  If there
1060  * is no next @iter, %FALSE is returned and @iter is set to be invalid.
1061  *
1062  * Return value: %TRUE if @iter has been changed to the next node.
1063  **/
1064 gboolean
1065 gtk_tree_model_iter_next (GtkTreeModel  *tree_model,
1066                           GtkTreeIter   *iter)
1067 {
1068   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1069   g_return_val_if_fail (iter != NULL, FALSE);
1070   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_next != NULL, FALSE);
1071
1072   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_next) (tree_model, iter);
1073 }
1074
1075 /**
1076  * gtk_tree_model_iter_children:
1077  * @tree_model: A #GtkTreeModel.
1078  * @iter: The new #GtkTreeIter to be set to the child.
1079  * @parent: The #GtkTreeIter, or %NULL
1080  *
1081  * Sets @iter to point to the first child of @parent.  If @parent has no children,
1082  * %FALSE is returned and @iter is set to be invalid.  @parent will remain a valid
1083  * node after this function has been called.
1084  *
1085  * If @parent is %NULL returns the first node, equivalent to
1086  * <literal>gtk_tree_model_get_iter_first (tree_model, iter);</literal>
1087  *
1088  * Return value: %TRUE, if @child has been set to the first child.
1089  **/
1090 gboolean
1091 gtk_tree_model_iter_children (GtkTreeModel *tree_model,
1092                               GtkTreeIter  *iter,
1093                               GtkTreeIter  *parent)
1094 {
1095   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1096   g_return_val_if_fail (iter != NULL, FALSE);
1097   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_children != NULL, FALSE);
1098
1099   INITIALIZE_TREE_ITER (iter);
1100
1101   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_children) (tree_model, iter, parent);
1102 }
1103
1104 /**
1105  * gtk_tree_model_iter_has_child:
1106  * @tree_model: A #GtkTreeModel.
1107  * @iter: The #GtkTreeIter to test for children.
1108  *
1109  * Returns %TRUE if @iter has children, %FALSE otherwise.
1110  *
1111  * Return value: %TRUE if @iter has children.
1112  **/
1113 gboolean
1114 gtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
1115                                GtkTreeIter  *iter)
1116 {
1117   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1118   g_return_val_if_fail (iter != NULL, FALSE);
1119   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_has_child != NULL, FALSE);
1120
1121   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_has_child) (tree_model, iter);
1122 }
1123
1124 /**
1125  * gtk_tree_model_iter_n_children:
1126  * @tree_model: A #GtkTreeModel.
1127  * @iter: The #GtkTreeIter, or %NULL.
1128  *
1129  * Returns the number of children that @iter has.  As a special case, if @iter
1130  * is %NULL, then the number of toplevel nodes is returned.
1131  *
1132  * Return value: The number of children of @iter.
1133  **/
1134 gint
1135 gtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
1136                                 GtkTreeIter  *iter)
1137 {
1138   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), 0);
1139   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_n_children != NULL, 0);
1140
1141   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_n_children) (tree_model, iter);
1142 }
1143
1144 /**
1145  * gtk_tree_model_iter_nth_child:
1146  * @tree_model: A #GtkTreeModel.
1147  * @iter: The #GtkTreeIter to set to the nth child.
1148  * @parent: The #GtkTreeIter to get the child from, or %NULL.
1149  * @n: Then index of the desired child.
1150  *
1151  * Sets @iter to be the child of @parent, using the given index.  The first
1152  * index is 0.  If @n is too big, or @parent has no children, @iter is set
1153  * to an invalid iterator and %FALSE is returned.  @parent will remain a valid
1154  * node after this function has been called.  As a special case, if @parent is
1155  * %NULL, then the @n<!-- -->th root node is set.
1156  *
1157  * Return value: %TRUE, if @parent has an @n<!-- -->th child.
1158  **/
1159 gboolean
1160 gtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
1161                                GtkTreeIter  *iter,
1162                                GtkTreeIter  *parent,
1163                                gint          n)
1164 {
1165   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1166   g_return_val_if_fail (iter != NULL, FALSE);
1167   g_return_val_if_fail (n >= 0, FALSE);
1168   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_nth_child != NULL, FALSE);
1169
1170   INITIALIZE_TREE_ITER (iter);
1171
1172   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_nth_child) (tree_model, iter, parent, n);
1173 }
1174
1175 /**
1176  * gtk_tree_model_iter_parent:
1177  * @tree_model: A #GtkTreeModel
1178  * @iter: The new #GtkTreeIter to set to the parent.
1179  * @child: The #GtkTreeIter.
1180  *
1181  * Sets @iter to be the parent of @child.  If @child is at the toplevel, and
1182  * doesn't have a parent, then @iter is set to an invalid iterator and %FALSE
1183  * is returned.  @child will remain a valid node after this function has been
1184  * called.
1185  *
1186  * Return value: %TRUE, if @iter is set to the parent of @child.
1187  **/
1188 gboolean
1189 gtk_tree_model_iter_parent (GtkTreeModel *tree_model,
1190                             GtkTreeIter  *iter,
1191                             GtkTreeIter  *child)
1192 {
1193   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
1194   g_return_val_if_fail (iter != NULL, FALSE);
1195   g_return_val_if_fail (child != NULL, FALSE);
1196   g_return_val_if_fail (GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_parent != NULL, FALSE);
1197
1198   INITIALIZE_TREE_ITER (iter);
1199
1200   return (* GTK_TREE_MODEL_GET_IFACE (tree_model)->iter_parent) (tree_model, iter, child);
1201 }
1202
1203 /**
1204  * gtk_tree_model_ref_node:
1205  * @tree_model: A #GtkTreeModel.
1206  * @iter: The #GtkTreeIter.
1207  *
1208  * Lets the tree ref the node.  This is an optional method for models to
1209  * implement.  To be more specific, models may ignore this call as it exists
1210  * primarily for performance reasons.
1211  * 
1212  * This function is primarily meant as a way for views to let caching model know
1213  * when nodes are being displayed (and hence, whether or not to cache that
1214  * node.)  For example, a file-system based model would not want to keep the
1215  * entire file-hierarchy in memory, just the sections that are currently being
1216  * displayed by every current view.
1217  *
1218  * A model should be expected to be able to get an iter independent of its
1219  * reffed state.
1220  **/
1221 void
1222 gtk_tree_model_ref_node (GtkTreeModel *tree_model,
1223                          GtkTreeIter  *iter)
1224 {
1225   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1226
1227   if (GTK_TREE_MODEL_GET_IFACE (tree_model)->ref_node)
1228     (* GTK_TREE_MODEL_GET_IFACE (tree_model)->ref_node) (tree_model, iter);
1229 }
1230
1231 /**
1232  * gtk_tree_model_unref_node:
1233  * @tree_model: A #GtkTreeModel.
1234  * @iter: The #GtkTreeIter.
1235  *
1236  * Lets the tree unref the node.  This is an optional method for models to
1237  * implement.  To be more specific, models may ignore this call as it exists
1238  * primarily for performance reasons.
1239  *
1240  * For more information on what this means, see gtk_tree_model_ref_node().
1241  * Please note that nodes that are deleted are not unreffed.
1242  **/
1243 void
1244 gtk_tree_model_unref_node (GtkTreeModel *tree_model,
1245                            GtkTreeIter  *iter)
1246 {
1247   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1248   g_return_if_fail (iter != NULL);
1249
1250   if (GTK_TREE_MODEL_GET_IFACE (tree_model)->unref_node)
1251     (* GTK_TREE_MODEL_GET_IFACE (tree_model)->unref_node) (tree_model, iter);
1252 }
1253
1254 /**
1255  * gtk_tree_model_get:
1256  * @tree_model: a #GtkTreeModel
1257  * @iter: a row in @tree_model
1258  * @Varargs: pairs of column number and value return locations, terminated by -1
1259  *
1260  * Gets the value of one or more cells in the row referenced by @iter.
1261  * The variable argument list should contain integer column numbers,
1262  * each column number followed by a place to store the value being
1263  * retrieved.  The list is terminated by a -1. For example, to get a
1264  * value from column 0 with type %G_TYPE_STRING, you would
1265  * write: <literal>gtk_tree_model_get (model, iter, 0, &amp;place_string_here, -1)</literal>,
1266  * where <literal>place_string_here</literal> is a <type>gchar*</type> to be 
1267  * filled with the string.
1268  * If appropriate, the returned values have to be freed or unreferenced.
1269  *
1270  **/
1271 void
1272 gtk_tree_model_get (GtkTreeModel *tree_model,
1273                     GtkTreeIter  *iter,
1274                     ...)
1275 {
1276   va_list var_args;
1277
1278   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1279   g_return_if_fail (iter != NULL);
1280
1281   va_start (var_args, iter);
1282   gtk_tree_model_get_valist (tree_model, iter, var_args);
1283   va_end (var_args);
1284 }
1285
1286 /**
1287  * gtk_tree_model_get_valist:
1288  * @tree_model: a #GtkTreeModel
1289  * @iter: a row in @tree_model
1290  * @var_args: <type>va_list</type> of column/return location pairs
1291  *
1292  * See gtk_tree_model_get(), this version takes a <type>va_list</type> 
1293  * for language bindings to use.
1294  **/
1295 void
1296 gtk_tree_model_get_valist (GtkTreeModel *tree_model,
1297                            GtkTreeIter  *iter,
1298                            va_list      var_args)
1299 {
1300   gint column;
1301
1302   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1303   g_return_if_fail (iter != NULL);
1304
1305   column = va_arg (var_args, gint);
1306
1307   while (column != -1)
1308     {
1309       GValue value = { 0, };
1310       gchar *error = NULL;
1311
1312       if (column >= gtk_tree_model_get_n_columns (tree_model))
1313         {
1314           g_warning ("%s: Invalid column number %d accessed (remember to end your list of columns with a -1)", G_STRLOC, column);
1315           break;
1316         }
1317
1318       gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, column, &value);
1319
1320       G_VALUE_LCOPY (&value, var_args, 0, &error);
1321       if (error)
1322         {
1323           g_warning ("%s: %s", G_STRLOC, error);
1324           g_free (error);
1325
1326           /* we purposely leak the value here, it might not be
1327            * in a sane state if an error condition occoured
1328            */
1329           break;
1330         }
1331
1332       g_value_unset (&value);
1333
1334       column = va_arg (var_args, gint);
1335     }
1336 }
1337
1338 /**
1339  * gtk_tree_model_row_changed:
1340  * @tree_model: A #GtkTreeModel
1341  * @path: A #GtkTreePath pointing to the changed row
1342  * @iter: A valid #GtkTreeIter pointing to the changed row
1343  * 
1344  * Emits the "row_changed" signal on @tree_model.
1345  **/
1346 void
1347 gtk_tree_model_row_changed (GtkTreeModel *tree_model,
1348                             GtkTreePath  *path,
1349                             GtkTreeIter  *iter)
1350 {
1351   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1352   g_return_if_fail (path != NULL);
1353   g_return_if_fail (iter != NULL);
1354
1355   g_signal_emit (tree_model, tree_model_signals[ROW_CHANGED], 0, path, iter);
1356 }
1357
1358 /**
1359  * gtk_tree_model_row_inserted:
1360  * @tree_model: A #GtkTreeModel
1361  * @path: A #GtkTreePath pointing to the inserted row
1362  * @iter: A valid #GtkTreeIter pointing to the inserted row
1363  * 
1364  * Emits the "row_inserted" signal on @tree_model
1365  **/
1366 void
1367 gtk_tree_model_row_inserted (GtkTreeModel *tree_model,
1368                              GtkTreePath  *path,
1369                              GtkTreeIter  *iter)
1370 {
1371   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1372   g_return_if_fail (path != NULL);
1373   g_return_if_fail (iter != NULL);
1374
1375   g_signal_emit (tree_model, tree_model_signals[ROW_INSERTED], 0, path, iter);
1376 }
1377
1378 /**
1379  * gtk_tree_model_row_has_child_toggled:
1380  * @tree_model: A #GtkTreeModel
1381  * @path: A #GtkTreePath pointing to the changed row
1382  * @iter: A valid #GtkTreeIter pointing to the changed row
1383  * 
1384  * Emits the "row_has_child_toggled" signal on @tree_model.  This should be
1385  * called by models after the child state of a node changes.
1386  **/
1387 void
1388 gtk_tree_model_row_has_child_toggled (GtkTreeModel *tree_model,
1389                                       GtkTreePath  *path,
1390                                       GtkTreeIter  *iter)
1391 {
1392   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1393   g_return_if_fail (path != NULL);
1394   g_return_if_fail (iter != NULL);
1395
1396   g_signal_emit (tree_model, tree_model_signals[ROW_HAS_CHILD_TOGGLED], 0, path, iter);
1397 }
1398
1399 /**
1400  * gtk_tree_model_row_deleted:
1401  * @tree_model: A #GtkTreeModel
1402  * @path: A #GtkTreePath pointing to the previous location of the deleted row.
1403  * 
1404  * Emits the "row_deleted" signal on @tree_model.  This should be called by
1405  * models after a row has been removed.  The location pointed to by @path should
1406  * be the location that the row previously was at.  It may not be a valid
1407  * location anymore.
1408  **/
1409 void
1410 gtk_tree_model_row_deleted (GtkTreeModel *tree_model,
1411                             GtkTreePath  *path)
1412 {
1413   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1414   g_return_if_fail (path != NULL);
1415
1416   g_signal_emit (tree_model, tree_model_signals[ROW_DELETED], 0, path);
1417 }
1418
1419 /**
1420  * gtk_tree_model_rows_reordered:
1421  * @tree_model: A #GtkTreeModel
1422  * @path: A #GtkTreePath pointing to the tree node whose children have been reordered
1423  * @iter: A valid #GtkTreeIter pointing to the node whose children have been reordered
1424  * @new_order: an array of integers mapping the current position of each child
1425  *      to its old position before the re-ordering,
1426  *      i.e. @new_order<literal>[newpos] = oldpos</literal>.
1427  * 
1428  * Emits the "rows_reordered" signal on @tree_model.  This should be called by
1429  * models when their rows have been reordered.  
1430  **/
1431 void
1432 gtk_tree_model_rows_reordered (GtkTreeModel *tree_model,
1433                                GtkTreePath  *path,
1434                                GtkTreeIter  *iter,
1435                                gint         *new_order)
1436 {
1437   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
1438   g_return_if_fail (new_order != NULL);
1439
1440   g_signal_emit (tree_model, tree_model_signals[ROWS_REORDERED], 0, path, iter, new_order);
1441 }
1442
1443
1444 static gboolean
1445 gtk_tree_model_foreach_helper (GtkTreeModel            *model,
1446                                GtkTreeIter             *iter,
1447                                GtkTreePath             *path,
1448                                GtkTreeModelForeachFunc  func,
1449                                gpointer                 user_data)
1450 {
1451   do
1452     {
1453       GtkTreeIter child;
1454
1455       if ((* func) (model, path, iter, user_data))
1456         return TRUE;
1457
1458       if (gtk_tree_model_iter_children (model, &child, iter))
1459         {
1460           gtk_tree_path_down (path);
1461           if (gtk_tree_model_foreach_helper (model, &child, path, func, user_data))
1462             return TRUE;
1463           gtk_tree_path_up (path);
1464         }
1465
1466       gtk_tree_path_next (path);
1467     }
1468   while (gtk_tree_model_iter_next (model, iter));
1469
1470   return FALSE;
1471 }
1472
1473 /**
1474  * gtk_tree_model_foreach:
1475  * @model: A #GtkTreeModel
1476  * @func: A function to be called on each row
1477  * @user_data: User data to passed to func.
1478  * 
1479  * Calls func on each node in model in a depth-first fashion.  If func returns
1480  * %TRUE, then the tree ceases to be walked, and gtk_tree_model_foreach() returns.
1481  **/
1482
1483 void
1484 gtk_tree_model_foreach (GtkTreeModel            *model,
1485                         GtkTreeModelForeachFunc  func,
1486                         gpointer                 user_data)
1487 {
1488   GtkTreePath *path;
1489   GtkTreeIter iter;
1490
1491   g_return_if_fail (GTK_IS_TREE_MODEL (model));
1492   g_return_if_fail (func != NULL);
1493
1494   path = gtk_tree_path_new_first ();
1495   if (gtk_tree_model_get_iter (model, &iter, path) == FALSE)
1496     {
1497       gtk_tree_path_free (path);
1498       return;
1499     }
1500
1501   gtk_tree_model_foreach_helper (model, &iter, path, func, user_data);
1502   gtk_tree_path_free (path);
1503 }
1504
1505
1506 /*
1507  * GtkTreeRowReference
1508  */
1509
1510 static void gtk_tree_row_reference_unref_path (GtkTreePath  *path,
1511                                                GtkTreeModel *model,
1512                                                gint          depth);
1513
1514
1515 GType
1516 gtk_tree_row_reference_get_type (void)
1517 {
1518   static GType our_type = 0;
1519   
1520   if (our_type == 0)
1521     our_type = g_boxed_type_register_static ("GtkTreeRowReference",
1522                                              (GBoxedCopyFunc) gtk_tree_row_reference_copy,
1523                                              (GBoxedFreeFunc) gtk_tree_row_reference_free);
1524
1525   return our_type;
1526 }
1527
1528
1529 struct _GtkTreeRowReference
1530 {
1531   GObject *proxy;
1532   GtkTreeModel *model;
1533   GtkTreePath *path;
1534 };
1535
1536
1537 static void
1538 release_row_references (gpointer data)
1539 {
1540   RowRefList *refs = data;
1541   GSList *tmp_list = NULL;
1542
1543   tmp_list = refs->list;
1544   while (tmp_list != NULL)
1545     {
1546       GtkTreeRowReference *reference = tmp_list->data;
1547
1548       if (reference->proxy == (GObject *)reference->model)
1549         reference->model = NULL;
1550       reference->proxy = NULL;
1551
1552       /* we don't free the reference, users are responsible for that. */
1553
1554       tmp_list = g_slist_next (tmp_list);
1555     }
1556
1557   g_slist_free (refs->list);
1558   g_free (refs);
1559 }
1560
1561 static void
1562 gtk_tree_row_ref_inserted (RowRefList  *refs,
1563                            GtkTreePath *path,
1564                            GtkTreeIter *iter)
1565 {
1566   GSList *tmp_list;
1567
1568   if (refs == NULL)
1569     return;
1570
1571   /* This function corrects the path stored in the reference to
1572    * account for an insertion. Note that it's called _after_ the insertion
1573    * with the path to the newly-inserted row. Which means that
1574    * the inserted path is in a different "coordinate system" than
1575    * the old path (e.g. if the inserted path was just before the old path,
1576    * then inserted path and old path will be the same, and old path must be
1577    * moved down one).
1578    */
1579
1580   tmp_list = refs->list;
1581
1582   while (tmp_list != NULL)
1583     {
1584       GtkTreeRowReference *reference = tmp_list->data;
1585
1586       if (reference->path == NULL)
1587         goto done;
1588
1589       if (reference->path->depth >= path->depth)
1590         {
1591           gint i;
1592           gboolean ancestor = TRUE;
1593
1594           for (i = 0; i < path->depth - 1; i ++)
1595             {
1596               if (path->indices[i] != reference->path->indices[i])
1597                 {
1598                   ancestor = FALSE;
1599                   break;
1600                 }
1601             }
1602           if (ancestor == FALSE)
1603             goto done;
1604
1605           if (path->indices[path->depth-1] <= reference->path->indices[path->depth-1])
1606             reference->path->indices[path->depth-1] += 1;
1607         }
1608     done:
1609       tmp_list = g_slist_next (tmp_list);
1610     }
1611 }
1612
1613 static void
1614 gtk_tree_row_ref_deleted (RowRefList  *refs,
1615                           GtkTreePath *path)
1616 {
1617   GSList *tmp_list;
1618
1619   if (refs == NULL)
1620     return;
1621
1622   /* This function corrects the path stored in the reference to
1623    * account for an deletion. Note that it's called _after_ the
1624    * deletion with the old path of the just-deleted row. Which means
1625    * that the deleted path is the same now-defunct "coordinate system"
1626    * as the path saved in the reference, which is what we want to fix.
1627    */
1628
1629   tmp_list = refs->list;
1630
1631   while (tmp_list != NULL)
1632     {
1633       GtkTreeRowReference *reference = tmp_list->data;
1634
1635       if (reference->path)
1636         {
1637           gint i;
1638
1639           if (path->depth > reference->path->depth)
1640             goto next;
1641           for (i = 0; i < path->depth - 1; i++)
1642             {
1643               if (path->indices[i] != reference->path->indices[i])
1644                 goto next;
1645             }
1646
1647           /* We know it affects us. */
1648           if (path->indices[i] == reference->path->indices[i])
1649             {
1650               if (reference->path->depth > path->depth)
1651                 /* some parent was deleted, trying to unref any node
1652                  * between the deleted parent and the node the reference
1653                  * is pointing to is bad, as those nodes are already gone.
1654                  */
1655                 gtk_tree_row_reference_unref_path (reference->path, reference->model, path->depth - 1);
1656               else
1657                 gtk_tree_row_reference_unref_path (reference->path, reference->model, reference->path->depth - 1);
1658               gtk_tree_path_free (reference->path);
1659               reference->path = NULL;
1660             }
1661           else if (path->indices[i] < reference->path->indices[i])
1662             {
1663               reference->path->indices[path->depth-1]-=1;
1664             }
1665         }
1666
1667 next:
1668       tmp_list = g_slist_next (tmp_list);
1669     }
1670 }
1671
1672 static void
1673 gtk_tree_row_ref_reordered (RowRefList  *refs,
1674                             GtkTreePath *path,
1675                             GtkTreeIter *iter,
1676                             gint        *new_order)
1677 {
1678   GSList *tmp_list;
1679   gint length;
1680
1681   if (refs == NULL)
1682     return;
1683
1684   tmp_list = refs->list;
1685
1686   while (tmp_list != NULL)
1687     {
1688       GtkTreeRowReference *reference = tmp_list->data;
1689
1690       length = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (reference->model), iter);
1691
1692       if (length < 2)
1693         return;
1694
1695       if ((reference->path) &&
1696           (gtk_tree_path_is_ancestor (path, reference->path)))
1697         {
1698           gint ref_depth = gtk_tree_path_get_depth (reference->path);
1699           gint depth = gtk_tree_path_get_depth (path);
1700
1701           if (ref_depth > depth)
1702             {
1703               gint i;
1704               gint *indices = gtk_tree_path_get_indices (reference->path);
1705
1706               for (i = 0; i < length; i++)
1707                 {
1708                   if (new_order[i] == indices[depth])
1709                     {
1710                       indices[depth] = i;
1711                       break;
1712                     }
1713                 }
1714             }
1715         }
1716
1717       tmp_list = g_slist_next (tmp_list);
1718     }
1719 }
1720
1721 /* We do this recursively so that we can unref children nodes before their parent */
1722 static void
1723 gtk_tree_row_reference_unref_path_helper (GtkTreePath  *path,
1724                                           GtkTreeModel *model,
1725                                           GtkTreeIter  *parent_iter,
1726                                           gint          depth,
1727                                           gint          current_depth)
1728 {
1729   GtkTreeIter iter;
1730
1731   if (depth == current_depth)
1732     return;
1733
1734   gtk_tree_model_iter_nth_child (model, &iter, parent_iter, path->indices[current_depth]);
1735   gtk_tree_row_reference_unref_path_helper (path, model, &iter, depth, current_depth + 1);
1736   gtk_tree_model_unref_node (model, &iter);
1737 }
1738
1739 static void
1740 gtk_tree_row_reference_unref_path (GtkTreePath  *path,
1741                                    GtkTreeModel *model,
1742                                    gint          depth)
1743 {
1744   GtkTreeIter iter;
1745
1746   if (depth <= 0)
1747     return;
1748   
1749   gtk_tree_model_iter_nth_child (model, &iter, NULL, path->indices[0]);
1750   gtk_tree_row_reference_unref_path_helper (path, model, &iter, depth, 1);
1751   gtk_tree_model_unref_node (model, &iter);
1752 }
1753
1754 /**
1755  * gtk_tree_row_reference_new:
1756  * @model: A #GtkTreeModel
1757  * @path: A valid #GtkTreePath to monitor
1758  * 
1759  * Creates a row reference based on @path.  This reference will keep pointing to
1760  * the node pointed to by @path, so long as it exists.  It listens to all
1761  * signals emitted by @model, and updates its path appropriately.  If @path
1762  * isn't a valid path in @model, then %NULL is returned.
1763  * 
1764  * Return value: A newly allocated #GtkTreeRowReference, or %NULL
1765  **/
1766 GtkTreeRowReference *
1767 gtk_tree_row_reference_new (GtkTreeModel *model,
1768                             GtkTreePath  *path)
1769 {
1770   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1771   g_return_val_if_fail (path != NULL, NULL);
1772
1773   /* We use the model itself as the proxy object; and call
1774    * gtk_tree_row_reference_inserted(), etc, in the
1775    * class closure (default handler) marshalers for the signal.
1776    */  
1777   return gtk_tree_row_reference_new_proxy (G_OBJECT (model), model, path);
1778 }
1779
1780 /**
1781  * gtk_tree_row_reference_new_proxy:
1782  * @proxy: A proxy #GObject
1783  * @model: A #GtkTreeModel
1784  * @path: A valid #GtkTreePath to monitor
1785  * 
1786  * You do not need to use this function.  Creates a row reference based on
1787  * @path.  This reference will keep pointing to the node pointed to by @path, so
1788  * long as it exists.  If @path isn't a valid path in @model, then %NULL is
1789  * returned.  However, unlike references created with
1790  * gtk_tree_row_reference_new(), it does not listen to the model for changes.
1791  * The creator of the row reference must do this explicitly using
1792  * gtk_tree_row_reference_inserted(), gtk_tree_row_reference_deleted(),
1793  * gtk_tree_row_reference_reordered().
1794  * 
1795  * These functions must be called exactly once per proxy when the
1796  * corresponding signal on the model is emitted. This single call
1797  * updates all row references for that proxy. Since built-in GTK+
1798  * objects like #GtkTreeView already use this mechanism internally,
1799  * using them as the proxy object will produce unpredictable results.
1800  * Further more, passing the same object as @model and @proxy
1801  * doesn't work for reasons of internal implementation.
1802  *
1803  * This type of row reference is primarily meant by structures that need to
1804  * carefully monitor exactly when a row_reference updates itself, and is not
1805  * generally needed by most applications.
1806  *
1807  * Return value: A newly allocated #GtkTreeRowReference, or %NULL
1808  **/
1809 GtkTreeRowReference *
1810 gtk_tree_row_reference_new_proxy (GObject      *proxy,
1811                                   GtkTreeModel *model,
1812                                   GtkTreePath  *path)
1813 {
1814   GtkTreeRowReference *reference;
1815   RowRefList *refs;
1816   GtkTreeIter parent_iter;
1817   gint i;
1818
1819   g_return_val_if_fail (G_IS_OBJECT (proxy), NULL);
1820   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1821   g_return_val_if_fail (path != NULL, NULL);
1822   g_return_val_if_fail (path->depth > 0, NULL);
1823
1824   /* check that the path is valid */
1825   if (gtk_tree_model_get_iter (model, &parent_iter, path) == FALSE)
1826     return NULL;
1827
1828   /* Now we want to ref every node */
1829   gtk_tree_model_iter_nth_child (model, &parent_iter, NULL, path->indices[0]);
1830   gtk_tree_model_ref_node (model, &parent_iter);
1831
1832   for (i = 1; i < path->depth; i++)
1833     {
1834       GtkTreeIter iter;
1835       gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, path->indices[i]);
1836       gtk_tree_model_ref_node (model, &iter);
1837       parent_iter = iter;
1838     }
1839
1840   /* Make the row reference */
1841   reference = g_new (GtkTreeRowReference, 1);
1842
1843   g_object_ref (proxy);
1844   g_object_ref (model);
1845   reference->proxy = proxy;
1846   reference->model = model;
1847   reference->path = gtk_tree_path_copy (path);
1848
1849   refs = g_object_get_data (G_OBJECT (proxy), ROW_REF_DATA_STRING);
1850
1851   if (refs == NULL)
1852     {
1853       refs = g_new (RowRefList, 1);
1854       refs->list = NULL;
1855
1856       g_object_set_data_full (G_OBJECT (proxy),
1857                               ROW_REF_DATA_STRING,
1858                               refs, release_row_references);
1859     }
1860
1861   refs->list = g_slist_prepend (refs->list, reference);
1862
1863   return reference;
1864 }
1865
1866 /**
1867  * gtk_tree_row_reference_get_path:
1868  * @reference: A #GtkTreeRowReference
1869  * 
1870  * Returns a path that the row reference currently points to, or %NULL if the
1871  * path pointed to is no longer valid.
1872  * 
1873  * Return value: A current path, or %NULL.
1874  **/
1875 GtkTreePath *
1876 gtk_tree_row_reference_get_path (GtkTreeRowReference *reference)
1877 {
1878   g_return_val_if_fail (reference != NULL, NULL);
1879
1880   if (reference->proxy == NULL)
1881     return NULL;
1882
1883   if (reference->path == NULL)
1884     return NULL;
1885
1886   return gtk_tree_path_copy (reference->path);
1887 }
1888
1889 /**
1890  * gtk_tree_row_reference_valid:
1891  * @reference: A #GtkTreeRowReference, or NULL
1892  * 
1893  * Returns TRUE if the %reference is non-NULL and refers to a current valid
1894  * path.
1895  * 
1896  * Return value: TRUE if %reference points to a valid path.
1897  **/
1898 gboolean
1899 gtk_tree_row_reference_valid (GtkTreeRowReference *reference)
1900 {
1901   if (reference == NULL || reference->path == NULL)
1902     return FALSE;
1903
1904   return TRUE;
1905 }
1906
1907
1908 /**
1909  * gtk_tree_row_reference_copy:
1910  * @reference: a #GtkTreeRowReference
1911  * 
1912  * Copies a #GtkTreeRowReference.
1913  * 
1914  * Return value: a copy of @reference.
1915  *
1916  * Since: 2.2
1917  **/
1918 GtkTreeRowReference *
1919 gtk_tree_row_reference_copy (GtkTreeRowReference *reference)
1920 {
1921   return gtk_tree_row_reference_new_proxy (reference->proxy,
1922                                            reference->model,
1923                                            reference->path);
1924 }
1925
1926 /**
1927  * gtk_tree_row_reference_free:
1928  * @reference: A #GtkTreeRowReference, or NULL
1929  * 
1930  * Free's %reference.  %reference may be NULL.
1931  **/
1932 void
1933 gtk_tree_row_reference_free (GtkTreeRowReference *reference)
1934 {
1935   RowRefList *refs;
1936
1937   if (reference == NULL)
1938     return;
1939
1940   refs = g_object_get_data (G_OBJECT (reference->proxy), ROW_REF_DATA_STRING);
1941
1942   if (refs == NULL)
1943     {
1944       g_warning (G_STRLOC": bad row reference, proxy has no outstanding row references");
1945       return;
1946     }
1947
1948   refs->list = g_slist_remove (refs->list, reference);
1949
1950   if (refs->list == NULL)
1951     {
1952       g_object_set_data (G_OBJECT (reference->proxy),
1953                          ROW_REF_DATA_STRING,
1954                          NULL);
1955     }
1956
1957   if (reference->path)
1958     {
1959       gtk_tree_row_reference_unref_path (reference->path, reference->model, reference->path->depth);
1960       gtk_tree_path_free (reference->path);
1961     }
1962
1963   g_object_unref (reference->proxy);
1964   g_object_unref (reference->model);
1965   g_free (reference);
1966 }
1967
1968 /**
1969  * gtk_tree_row_reference_inserted:
1970  * @proxy: A #GObject
1971  * @path: The row position that was inserted
1972  * 
1973  * Lets a set of row reference created by gtk_tree_row_reference_new_proxy()
1974  * know that the model emitted the "row_inserted" signal.
1975  **/
1976 void
1977 gtk_tree_row_reference_inserted (GObject     *proxy,
1978                                  GtkTreePath *path)
1979 {
1980   g_return_if_fail (G_IS_OBJECT (proxy));
1981
1982   gtk_tree_row_ref_inserted ((RowRefList *)g_object_get_data (proxy, ROW_REF_DATA_STRING), path, NULL);
1983 }
1984
1985 /**
1986  * gtk_tree_row_reference_deleted:
1987  * @proxy: A #GObject
1988  * @path: The path position that was deleted
1989  * 
1990  * Lets a set of row reference created by gtk_tree_row_reference_new_proxy()
1991  * know that the model emitted the "row_deleted" signal.
1992  **/
1993 void
1994 gtk_tree_row_reference_deleted (GObject     *proxy,
1995                                 GtkTreePath *path)
1996 {
1997   g_return_if_fail (G_IS_OBJECT (proxy));
1998
1999   gtk_tree_row_ref_deleted ((RowRefList *)g_object_get_data (proxy, ROW_REF_DATA_STRING), path);
2000 }
2001
2002 /**
2003  * gtk_tree_row_reference_reordered:
2004  * @proxy: A #GObject
2005  * @path: The parent path of the reordered signal
2006  * @iter: The iter pointing to the parent of the reordered
2007  * @new_order: The new order of rows
2008  * 
2009  * Lets a set of row reference created by gtk_tree_row_reference_new_proxy()
2010  * know that the model emitted the "rows_reordered" signal.
2011  **/
2012 void
2013 gtk_tree_row_reference_reordered (GObject     *proxy,
2014                                   GtkTreePath *path,
2015                                   GtkTreeIter *iter,
2016                                   gint        *new_order)
2017 {
2018   g_return_if_fail (G_IS_OBJECT (proxy));
2019
2020   gtk_tree_row_ref_reordered ((RowRefList *)g_object_get_data (proxy, ROW_REF_DATA_STRING), path, iter, new_order);
2021 }