]> Pileus Git - ~andy/gtk/blob - gtk/gtktreestore.c
Fix #399071, suggestion from Benoit Dejean.
[~andy/gtk] / gtk / gtktreestore.c
1 /* gtktreestore.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 <string.h>
22 #include <gobject/gvaluecollector.h>
23 #include "gtktreemodel.h"
24 #include "gtktreestore.h"
25 #include "gtktreedatalist.h"
26 #include "gtktreednd.h"
27 #include "gtkintl.h"
28 #include "gtkalias.h"
29
30 #define G_NODE(node) ((GNode *)node)
31 #define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
32 #define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
33
34 static void         gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
35 static void         gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void         gtk_tree_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
37 static void         gtk_tree_store_sortable_init   (GtkTreeSortableIface   *iface);
38 static void         gtk_tree_store_finalize        (GObject           *object);
39 static GtkTreeModelFlags gtk_tree_store_get_flags  (GtkTreeModel      *tree_model);
40 static gint         gtk_tree_store_get_n_columns   (GtkTreeModel      *tree_model);
41 static GType        gtk_tree_store_get_column_type (GtkTreeModel      *tree_model,
42                                                     gint               index);
43 static gboolean     gtk_tree_store_get_iter        (GtkTreeModel      *tree_model,
44                                                     GtkTreeIter       *iter,
45                                                     GtkTreePath       *path);
46 static GtkTreePath *gtk_tree_store_get_path        (GtkTreeModel      *tree_model,
47                                                     GtkTreeIter       *iter);
48 static void         gtk_tree_store_get_value       (GtkTreeModel      *tree_model,
49                                                     GtkTreeIter       *iter,
50                                                     gint               column,
51                                                     GValue            *value);
52 static gboolean     gtk_tree_store_iter_next       (GtkTreeModel      *tree_model,
53                                                     GtkTreeIter       *iter);
54 static gboolean     gtk_tree_store_iter_children   (GtkTreeModel      *tree_model,
55                                                     GtkTreeIter       *iter,
56                                                     GtkTreeIter       *parent);
57 static gboolean     gtk_tree_store_iter_has_child  (GtkTreeModel      *tree_model,
58                                                     GtkTreeIter       *iter);
59 static gint         gtk_tree_store_iter_n_children (GtkTreeModel      *tree_model,
60                                                     GtkTreeIter       *iter);
61 static gboolean     gtk_tree_store_iter_nth_child  (GtkTreeModel      *tree_model,
62                                                     GtkTreeIter       *iter,
63                                                     GtkTreeIter       *parent,
64                                                     gint               n);
65 static gboolean     gtk_tree_store_iter_parent     (GtkTreeModel      *tree_model,
66                                                     GtkTreeIter       *iter,
67                                                     GtkTreeIter       *child);
68
69
70 static void gtk_tree_store_set_n_columns   (GtkTreeStore *tree_store,
71                                             gint          n_columns);
72 static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
73                                             gint          column,
74                                             GType         type);
75
76 static void gtk_tree_store_increment_stamp (GtkTreeStore  *tree_store);
77
78
79 /* DND interfaces */
80 static gboolean real_gtk_tree_store_row_draggable   (GtkTreeDragSource *drag_source,
81                                                    GtkTreePath       *path);
82 static gboolean gtk_tree_store_drag_data_delete   (GtkTreeDragSource *drag_source,
83                                                    GtkTreePath       *path);
84 static gboolean gtk_tree_store_drag_data_get      (GtkTreeDragSource *drag_source,
85                                                    GtkTreePath       *path,
86                                                    GtkSelectionData  *selection_data);
87 static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
88                                                    GtkTreePath       *dest,
89                                                    GtkSelectionData  *selection_data);
90 static gboolean gtk_tree_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
91                                                    GtkTreePath       *dest_path,
92                                                    GtkSelectionData  *selection_data);
93
94 /* Sortable Interfaces */
95
96 static void     gtk_tree_store_sort                    (GtkTreeStore           *tree_store);
97 static void     gtk_tree_store_sort_iter_changed       (GtkTreeStore           *tree_store,
98                                                         GtkTreeIter            *iter,
99                                                         gint                    column,
100                                                         gboolean                emit_signal);
101 static gboolean gtk_tree_store_get_sort_column_id      (GtkTreeSortable        *sortable,
102                                                         gint                   *sort_column_id,
103                                                         GtkSortType            *order);
104 static void     gtk_tree_store_set_sort_column_id      (GtkTreeSortable        *sortable,
105                                                         gint                    sort_column_id,
106                                                         GtkSortType             order);
107 static void     gtk_tree_store_set_sort_func           (GtkTreeSortable        *sortable,
108                                                         gint                    sort_column_id,
109                                                         GtkTreeIterCompareFunc  func,
110                                                         gpointer                data,
111                                                         GtkDestroyNotify        destroy);
112 static void     gtk_tree_store_set_default_sort_func   (GtkTreeSortable        *sortable,
113                                                         GtkTreeIterCompareFunc  func,
114                                                         gpointer                data,
115                                                         GtkDestroyNotify        destroy);
116 static gboolean gtk_tree_store_has_default_sort_func   (GtkTreeSortable        *sortable);
117
118 static void     validate_gnode                         (GNode *node);
119
120 static void     gtk_tree_store_move                    (GtkTreeStore           *tree_store,
121                                                         GtkTreeIter            *iter,
122                                                         GtkTreeIter            *position,
123                                                         gboolean                before);
124
125
126 static inline void
127 validate_tree (GtkTreeStore *tree_store)
128 {
129   if (gtk_debug_flags & GTK_DEBUG_TREE)
130     {
131       g_assert (G_NODE (tree_store->root)->parent == NULL);
132
133       validate_gnode (G_NODE (tree_store->root));
134     }
135 }
136
137 G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
138                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
139                                                 gtk_tree_store_tree_model_init)
140                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
141                                                 gtk_tree_store_drag_source_init)
142                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
143                                                 gtk_tree_store_drag_dest_init)
144                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
145                                                 gtk_tree_store_sortable_init))
146
147 static void
148 gtk_tree_store_class_init (GtkTreeStoreClass *class)
149 {
150   GObjectClass *object_class;
151
152   object_class = (GObjectClass *) class;
153
154   object_class->finalize = gtk_tree_store_finalize;
155 }
156
157 static void
158 gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
159 {
160   iface->get_flags = gtk_tree_store_get_flags;
161   iface->get_n_columns = gtk_tree_store_get_n_columns;
162   iface->get_column_type = gtk_tree_store_get_column_type;
163   iface->get_iter = gtk_tree_store_get_iter;
164   iface->get_path = gtk_tree_store_get_path;
165   iface->get_value = gtk_tree_store_get_value;
166   iface->iter_next = gtk_tree_store_iter_next;
167   iface->iter_children = gtk_tree_store_iter_children;
168   iface->iter_has_child = gtk_tree_store_iter_has_child;
169   iface->iter_n_children = gtk_tree_store_iter_n_children;
170   iface->iter_nth_child = gtk_tree_store_iter_nth_child;
171   iface->iter_parent = gtk_tree_store_iter_parent;
172 }
173
174 static void
175 gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
176 {
177   iface->row_draggable = real_gtk_tree_store_row_draggable;
178   iface->drag_data_delete = gtk_tree_store_drag_data_delete;
179   iface->drag_data_get = gtk_tree_store_drag_data_get;
180 }
181
182 static void
183 gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
184 {
185   iface->drag_data_received = gtk_tree_store_drag_data_received;
186   iface->row_drop_possible = gtk_tree_store_row_drop_possible;
187 }
188
189 static void
190 gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
191 {
192   iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
193   iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
194   iface->set_sort_func = gtk_tree_store_set_sort_func;
195   iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
196   iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
197 }
198
199 static void
200 gtk_tree_store_init (GtkTreeStore *tree_store)
201 {
202   tree_store->root = g_node_new (NULL);
203   /* While the odds are against us getting 0...
204    */
205   do
206     {
207       tree_store->stamp = g_random_int ();
208     }
209   while (tree_store->stamp == 0);
210
211   tree_store->sort_list = NULL;
212   tree_store->sort_column_id = -2;
213   tree_store->columns_dirty = FALSE;
214 }
215
216 /**
217  * gtk_tree_store_new:
218  * @n_columns: number of columns in the tree store
219  * @Varargs: all #GType types for the columns, from first to last
220  *
221  * Creates a new tree store as with @n_columns columns each of the types passed
222  * in.  Note that only types derived from standard GObject fundamental types 
223  * are supported. 
224  *
225  * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
226  * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
227  * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
228  *
229  * Return value: a new #GtkTreeStore
230  **/
231 GtkTreeStore *
232 gtk_tree_store_new (gint n_columns,
233                                ...)
234 {
235   GtkTreeStore *retval;
236   va_list args;
237   gint i;
238
239   g_return_val_if_fail (n_columns > 0, NULL);
240
241   retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
242   gtk_tree_store_set_n_columns (retval, n_columns);
243
244   va_start (args, n_columns);
245
246   for (i = 0; i < n_columns; i++)
247     {
248       GType type = va_arg (args, GType);
249       if (! _gtk_tree_data_list_check_type (type))
250         {
251           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
252           g_object_unref (retval);
253           return NULL;
254         }
255       gtk_tree_store_set_column_type (retval, i, type);
256     }
257   va_end (args);
258
259   return retval;
260 }
261 /**
262  * gtk_tree_store_newv:
263  * @n_columns: number of columns in the tree store
264  * @types: an array of #GType types for the columns, from first to last
265  *
266  * Non vararg creation function.  Used primarily by language bindings.
267  *
268  * Return value: a new #GtkTreeStore
269  **/
270 GtkTreeStore *
271 gtk_tree_store_newv (gint   n_columns,
272                      GType *types)
273 {
274   GtkTreeStore *retval;
275   gint i;
276
277   g_return_val_if_fail (n_columns > 0, NULL);
278
279   retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
280   gtk_tree_store_set_n_columns (retval, n_columns);
281
282    for (i = 0; i < n_columns; i++)
283     {
284       if (! _gtk_tree_data_list_check_type (types[i]))
285         {
286           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
287           g_object_unref (retval);
288           return NULL;
289         }
290       gtk_tree_store_set_column_type (retval, i, types[i]);
291     }
292
293   return retval;
294 }
295
296
297 /**
298  * gtk_tree_store_set_column_types:
299  * @tree_store: A #GtkTreeStore
300  * @n_columns: Number of columns for the tree store
301  * @types: An array of #GType types, one for each column
302  * 
303  * This function is meant primarily for #GObjects that inherit from 
304  * #GtkTreeStore, and should only be used when constructing a new 
305  * #GtkTreeStore.  It will not function after a row has been added, 
306  * or a method on the #GtkTreeModel interface is called.
307  **/
308 void
309 gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
310                                  gint          n_columns,
311                                  GType        *types)
312 {
313   gint i;
314
315   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
316   g_return_if_fail (tree_store->columns_dirty == 0);
317
318   gtk_tree_store_set_n_columns (tree_store, n_columns);
319    for (i = 0; i < n_columns; i++)
320     {
321       if (! _gtk_tree_data_list_check_type (types[i]))
322         {
323           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
324           continue;
325         }
326       gtk_tree_store_set_column_type (tree_store, i, types[i]);
327     }
328 }
329
330 static void
331 gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
332                               gint          n_columns)
333 {
334   GType *new_columns;
335
336   if (tree_store->n_columns == n_columns)
337     return;
338
339   new_columns = g_new0 (GType, n_columns);
340   if (tree_store->column_headers)
341     {
342       /* copy the old header orders over */
343       if (n_columns >= tree_store->n_columns)
344         memcpy (new_columns, tree_store->column_headers, tree_store->n_columns * sizeof (gchar *));
345       else
346         memcpy (new_columns, tree_store->column_headers, n_columns * sizeof (GType));
347
348       g_free (tree_store->column_headers);
349     }
350
351   if (tree_store->sort_list)
352     _gtk_tree_data_list_header_free (tree_store->sort_list);
353
354   tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);
355
356   tree_store->column_headers = new_columns;
357   tree_store->n_columns = n_columns;
358 }
359
360 /**
361  * gtk_tree_store_set_column_type:
362  * @tree_store: a #GtkTreeStore
363  * @column: column number
364  * @type: type of the data to be stored in @column
365  *
366  * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
367  * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
368  * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
369  * subclasses of those types such as %GDK_TYPE_PIXBUF.
370  *
371  **/
372 static void
373 gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
374                                 gint          column,
375                                 GType         type)
376 {
377   if (!_gtk_tree_data_list_check_type (type))
378     {
379       g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
380       return;
381     }
382   tree_store->column_headers[column] = type;
383 }
384
385 static gboolean
386 node_free (GNode *node, gpointer data)
387 {
388   if (node->data)
389     _gtk_tree_data_list_free (node->data, (GType*)data);
390   node->data = NULL;
391
392   return FALSE;
393 }
394
395 static void
396 gtk_tree_store_finalize (GObject *object)
397 {
398   GtkTreeStore *tree_store = GTK_TREE_STORE (object);
399
400   g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
401                    node_free, tree_store->column_headers);
402   g_node_destroy (tree_store->root);
403   _gtk_tree_data_list_header_free (tree_store->sort_list);
404   g_free (tree_store->column_headers);
405
406   if (tree_store->default_sort_destroy)
407     {
408       GtkDestroyNotify d = tree_store->default_sort_destroy;
409
410       tree_store->default_sort_destroy = NULL;
411       d (tree_store->default_sort_data);
412       tree_store->default_sort_data = NULL;
413     }
414
415   /* must chain up */
416   G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
417 }
418
419 /* fulfill the GtkTreeModel requirements */
420 /* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node.  However,
421  * it is not visible to the tree or to the user., and the path "0" refers to the
422  * first child of GtkTreeStore::root.
423  */
424
425
426 static GtkTreeModelFlags
427 gtk_tree_store_get_flags (GtkTreeModel *tree_model)
428 {
429   return GTK_TREE_MODEL_ITERS_PERSIST;
430 }
431
432 static gint
433 gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
434 {
435   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
436
437   tree_store->columns_dirty = TRUE;
438
439   return tree_store->n_columns;
440 }
441
442 static GType
443 gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
444                                 gint          index)
445 {
446   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
447
448   g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
449
450   tree_store->columns_dirty = TRUE;
451
452   return tree_store->column_headers[index];
453 }
454
455 static gboolean
456 gtk_tree_store_get_iter (GtkTreeModel *tree_model,
457                          GtkTreeIter  *iter,
458                          GtkTreePath  *path)
459 {
460   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
461   GtkTreeIter parent;
462   gint *indices;
463   gint depth, i;
464
465   tree_store->columns_dirty = TRUE;
466
467   indices = gtk_tree_path_get_indices (path);
468   depth = gtk_tree_path_get_depth (path);
469
470   g_return_val_if_fail (depth > 0, FALSE);
471
472   parent.stamp = tree_store->stamp;
473   parent.user_data = tree_store->root;
474
475   if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
476     return FALSE;
477
478   for (i = 1; i < depth; i++)
479     {
480       parent = *iter;
481       if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
482         return FALSE;
483     }
484
485   return TRUE;
486 }
487
488 static GtkTreePath *
489 gtk_tree_store_get_path (GtkTreeModel *tree_model,
490                          GtkTreeIter  *iter)
491 {
492   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
493   GtkTreePath *retval;
494   GNode *tmp_node;
495   gint i = 0;
496
497   g_return_val_if_fail (iter->user_data != NULL, NULL);
498   g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
499
500   validate_tree (tree_store);
501
502   if (G_NODE (iter->user_data)->parent == NULL &&
503       G_NODE (iter->user_data) == tree_store->root)
504     return gtk_tree_path_new ();
505   g_assert (G_NODE (iter->user_data)->parent != NULL);
506
507   if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
508     {
509       retval = gtk_tree_path_new ();
510       tmp_node = G_NODE (tree_store->root)->children;
511     }
512   else
513     {
514       GtkTreeIter tmp_iter = *iter;
515
516       tmp_iter.user_data = G_NODE (iter->user_data)->parent;
517
518       retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
519       tmp_node = G_NODE (iter->user_data)->parent->children;
520     }
521
522   if (retval == NULL)
523     return NULL;
524
525   if (tmp_node == NULL)
526     {
527       gtk_tree_path_free (retval);
528       return NULL;
529     }
530
531   for (; tmp_node; tmp_node = tmp_node->next)
532     {
533       if (tmp_node == G_NODE (iter->user_data))
534         break;
535       i++;
536     }
537
538   if (tmp_node == NULL)
539     {
540       /* We couldn't find node, meaning it's prolly not ours */
541       /* Perhaps I should do a g_return_if_fail here. */
542       gtk_tree_path_free (retval);
543       return NULL;
544     }
545
546   gtk_tree_path_append_index (retval, i);
547
548   return retval;
549 }
550
551
552 static void
553 gtk_tree_store_get_value (GtkTreeModel *tree_model,
554                           GtkTreeIter  *iter,
555                           gint          column,
556                           GValue       *value)
557 {
558   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
559   GtkTreeDataList *list;
560   gint tmp_column = column;
561
562   g_return_if_fail (column < tree_store->n_columns);
563   g_return_if_fail (VALID_ITER (iter, tree_store));
564
565   list = G_NODE (iter->user_data)->data;
566
567   while (tmp_column-- > 0 && list)
568     list = list->next;
569
570   if (list)
571     {
572       _gtk_tree_data_list_node_to_value (list,
573                                          tree_store->column_headers[column],
574                                          value);
575     }
576   else
577     {
578       /* We want to return an initialized but empty (default) value */
579       g_value_init (value, tree_store->column_headers[column]);
580     }
581 }
582
583 static gboolean
584 gtk_tree_store_iter_next (GtkTreeModel  *tree_model,
585                           GtkTreeIter   *iter)
586 {
587   g_return_val_if_fail (iter->user_data != NULL, FALSE);
588   g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
589
590   if (G_NODE (iter->user_data)->next)
591     {
592       iter->user_data = G_NODE (iter->user_data)->next;
593       return TRUE;
594     }
595   else
596     return FALSE;
597 }
598
599 static gboolean
600 gtk_tree_store_iter_children (GtkTreeModel *tree_model,
601                               GtkTreeIter  *iter,
602                               GtkTreeIter  *parent)
603 {
604   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
605   GNode *children;
606
607   if (parent)
608     g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
609
610   if (parent)
611     children = G_NODE (parent->user_data)->children;
612   else
613     children = G_NODE (tree_store->root)->children;
614
615   if (children)
616     {
617       iter->stamp = tree_store->stamp;
618       iter->user_data = children;
619       return TRUE;
620     }
621   else
622     return FALSE;
623 }
624
625 static gboolean
626 gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
627                                GtkTreeIter  *iter)
628 {
629   g_return_val_if_fail (iter->user_data != NULL, FALSE);
630   g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
631
632   return G_NODE (iter->user_data)->children != NULL;
633 }
634
635 static gint
636 gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
637                                 GtkTreeIter  *iter)
638 {
639   GNode *node;
640   gint i = 0;
641
642   g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
643
644   if (iter == NULL)
645     node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
646   else
647     node = G_NODE (iter->user_data)->children;
648
649   while (node)
650     {
651       i++;
652       node = node->next;
653     }
654
655   return i;
656 }
657
658 static gboolean
659 gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
660                                GtkTreeIter  *iter,
661                                GtkTreeIter  *parent,
662                                gint          n)
663 {
664   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
665   GNode *parent_node;
666   GNode *child;
667
668   g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
669
670   if (parent == NULL)
671     parent_node = tree_store->root;
672   else
673     parent_node = parent->user_data;
674
675   child = g_node_nth_child (parent_node, n);
676
677   if (child)
678     {
679       iter->user_data = child;
680       iter->stamp = tree_store->stamp;
681       return TRUE;
682     }
683   else
684     return FALSE;
685 }
686
687 static gboolean
688 gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
689                             GtkTreeIter  *iter,
690                             GtkTreeIter  *child)
691 {
692   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
693   GNode *parent;
694
695   g_return_val_if_fail (iter != NULL, FALSE);
696   g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
697
698   parent = G_NODE (child->user_data)->parent;
699
700   g_assert (parent != NULL);
701
702   if (parent != tree_store->root)
703     {
704       iter->user_data = parent;
705       iter->stamp = tree_store->stamp;
706       return TRUE;
707     }
708   else
709     return FALSE;
710 }
711
712
713 /* Does not emit a signal */
714 static gboolean
715 gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
716                                GtkTreeIter  *iter,
717                                gint          column,
718                                GValue       *value,
719                                gboolean      sort)
720 {
721   GtkTreeDataList *list;
722   GtkTreeDataList *prev;
723   gint old_column = column;
724   GValue real_value = {0, };
725   gboolean converted = FALSE;
726   gboolean retval = FALSE;
727
728   if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
729     {
730       if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
731              g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
732         {
733           g_warning ("%s: Unable to convert from %s to %s\n",
734                      G_STRLOC,
735                      g_type_name (G_VALUE_TYPE (value)),
736                      g_type_name (tree_store->column_headers[column]));
737           return retval;
738         }
739       if (!g_value_transform (value, &real_value))
740         {
741           g_warning ("%s: Unable to make conversion from %s to %s\n",
742                      G_STRLOC,
743                      g_type_name (G_VALUE_TYPE (value)),
744                      g_type_name (tree_store->column_headers[column]));
745           g_value_unset (&real_value);
746           return retval;
747         }
748       converted = TRUE;
749     }
750
751   prev = list = G_NODE (iter->user_data)->data;
752
753   while (list != NULL)
754     {
755       if (column == 0)
756         {
757           if (converted)
758             _gtk_tree_data_list_value_to_node (list, &real_value);
759           else
760             _gtk_tree_data_list_value_to_node (list, value);
761           retval = TRUE;
762           if (converted)
763             g_value_unset (&real_value);
764           if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
765             gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
766           return retval;
767         }
768
769       column--;
770       prev = list;
771       list = list->next;
772     }
773
774   if (G_NODE (iter->user_data)->data == NULL)
775     {
776       G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
777       list->next = NULL;
778     }
779   else
780     {
781       list = prev->next = _gtk_tree_data_list_alloc ();
782       list->next = NULL;
783     }
784
785   while (column != 0)
786     {
787       list->next = _gtk_tree_data_list_alloc ();
788       list = list->next;
789       list->next = NULL;
790       column --;
791     }
792
793   if (converted)
794     _gtk_tree_data_list_value_to_node (list, &real_value);
795   else
796     _gtk_tree_data_list_value_to_node (list, value);
797   
798   retval = TRUE;
799   if (converted)
800     g_value_unset (&real_value);
801
802   if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
803     gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
804
805   return retval;
806 }
807
808 /**
809  * gtk_tree_store_set_value:
810  * @tree_store: a #GtkTreeStore
811  * @iter: A valid #GtkTreeIter for the row being modified
812  * @column: column number to modify
813  * @value: new value for the cell
814  *
815  * Sets the data in the cell specified by @iter and @column.
816  * The type of @value must be convertible to the type of the
817  * column.
818  *
819  **/
820 void
821 gtk_tree_store_set_value (GtkTreeStore *tree_store,
822                           GtkTreeIter  *iter,
823                           gint          column,
824                           GValue       *value)
825 {
826   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
827   g_return_if_fail (VALID_ITER (iter, tree_store));
828   g_return_if_fail (column >= 0 && column < tree_store->n_columns);
829   g_return_if_fail (G_IS_VALUE (value));
830
831   if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
832     {
833       GtkTreePath *path;
834
835       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
836       gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
837       gtk_tree_path_free (path);
838     }
839 }
840
841 static GtkTreeIterCompareFunc
842 gtk_tree_store_get_compare_func (GtkTreeStore *tree_store)
843 {
844   GtkTreeIterCompareFunc func = NULL;
845
846   if (GTK_TREE_STORE_IS_SORTED (tree_store))
847     {
848       if (tree_store->sort_column_id != -1)
849         {
850           GtkTreeDataSortHeader *header;
851           header = _gtk_tree_data_list_get_header (tree_store->sort_list,
852                                                    tree_store->sort_column_id);
853           g_return_val_if_fail (header != NULL, NULL);
854           g_return_val_if_fail (header->func != NULL, NULL);
855           func = header->func;
856         }
857       else
858         {
859           func = tree_store->default_sort_func;
860         }
861     }
862
863   return func;
864 }
865
866 static void
867 gtk_tree_store_set_vector_internal (GtkTreeStore *tree_store,
868                                     GtkTreeIter  *iter,
869                                     gboolean     *emit_signal,
870                                     gboolean     *maybe_need_sort,
871                                     gint         *columns,
872                                     GValue       *values,
873                                     gint          n_values)
874 {
875   gint i;
876   GtkTreeIterCompareFunc func = NULL;
877
878   func = gtk_tree_store_get_compare_func (tree_store);
879   if (func != _gtk_tree_data_list_compare_func)
880     *maybe_need_sort = TRUE;
881
882   for (i = 0; i < n_values; i++)
883     {
884       *emit_signal = gtk_tree_store_real_set_value (tree_store, iter,
885                                                     columns[i], &values[i],
886                                                     FALSE) || *emit_signal;
887
888       if (func == _gtk_tree_data_list_compare_func &&
889           columns[i] == tree_store->sort_column_id)
890         *maybe_need_sort = TRUE;
891     }
892 }
893
894 static void
895 gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
896                                     GtkTreeIter  *iter,
897                                     gboolean     *emit_signal,
898                                     gboolean     *maybe_need_sort,
899                                     va_list       var_args)
900 {
901   gint column;
902   GtkTreeIterCompareFunc func = NULL;
903
904   column = va_arg (var_args, gint);
905
906   func = gtk_tree_store_get_compare_func (tree_store);
907   if (func != _gtk_tree_data_list_compare_func)
908     *maybe_need_sort = TRUE;
909
910   while (column != -1)
911     {
912       GValue value = { 0, };
913       gchar *error = NULL;
914
915       if (column < 0 || column >= tree_store->n_columns)
916         {
917           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
918           break;
919         }
920       g_value_init (&value, tree_store->column_headers[column]);
921
922       G_VALUE_COLLECT (&value, var_args, 0, &error);
923       if (error)
924         {
925           g_warning ("%s: %s", G_STRLOC, error);
926           g_free (error);
927
928           /* we purposely leak the value here, it might not be
929            * in a sane state if an error condition occoured
930            */
931           break;
932         }
933
934       *emit_signal = gtk_tree_store_real_set_value (tree_store,
935                                                     iter,
936                                                     column,
937                                                     &value,
938                                                     FALSE) || *emit_signal;
939
940       if (func == _gtk_tree_data_list_compare_func &&
941           column == tree_store->sort_column_id)
942         *maybe_need_sort = TRUE;
943
944       g_value_unset (&value);
945
946       column = va_arg (var_args, gint);
947     }
948 }
949
950 /**
951  * gtk_tree_store_set_valuesv:
952  * @tree_store: A #GtkTreeStore
953  * @iter: A valid #GtkTreeIter for the row being modified
954  * @columns: an array of column numbers
955  * @values: an array of GValues
956  * @n_values: the length of the @columns and @values arrays
957  *
958  * A variant of gtk_tree_store_set_valist() which takes
959  * the columns and values as two arrays, instead of varargs.  This
960  * function is mainly intended for language bindings or in case
961  * the number of columns to change is not known until run-time.
962  *
963  * Since: 2.12
964  **/
965 void
966 gtk_tree_store_set_valuesv (GtkTreeStore *tree_store,
967                             GtkTreeIter  *iter,
968                             gint         *columns,
969                             GValue       *values,
970                             gint          n_values)
971 {
972   gboolean emit_signal = FALSE;
973   gboolean maybe_need_sort = FALSE;
974
975   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
976   g_return_if_fail (VALID_ITER (iter, tree_store));
977
978   gtk_tree_store_set_vector_internal (tree_store, iter,
979                                       &emit_signal,
980                                       &maybe_need_sort,
981                                       columns, values, n_values);
982
983   if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
984     gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
985
986   if (emit_signal)
987     {
988       GtkTreePath *path;
989
990       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
991       gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
992       gtk_tree_path_free (path);
993     }
994 }
995
996 /**
997  * gtk_tree_store_set_valist:
998  * @tree_store: A #GtkTreeStore
999  * @iter: A valid #GtkTreeIter for the row being modified
1000  * @var_args: <type>va_list</type> of column/value pairs
1001  *
1002  * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
1003  * use by language bindings.
1004  *
1005  **/
1006 void
1007 gtk_tree_store_set_valist (GtkTreeStore *tree_store,
1008                            GtkTreeIter  *iter,
1009                            va_list       var_args)
1010 {
1011   gboolean emit_signal = FALSE;
1012   gboolean maybe_need_sort = FALSE;
1013
1014   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1015   g_return_if_fail (VALID_ITER (iter, tree_store));
1016
1017   gtk_tree_store_set_valist_internal (tree_store, iter,
1018                                       &emit_signal,
1019                                       &maybe_need_sort,
1020                                       var_args);
1021
1022   if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1023     gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
1024
1025   if (emit_signal)
1026     {
1027       GtkTreePath *path;
1028
1029       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1030       gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
1031       gtk_tree_path_free (path);
1032     }
1033 }
1034
1035 /**
1036  * gtk_tree_store_set:
1037  * @tree_store: A #GtkTreeStore
1038  * @iter: A valid #GtkTreeIter for the row being modified
1039  * @Varargs: pairs of column number and value, terminated with -1
1040  *
1041  * Sets the value of one or more cells in the row referenced by @iter.
1042  * The variable argument list should contain integer column numbers,
1043  * each column number followed by the value to be set. 
1044  * The list is terminated by a -1. For example, to set column 0 with type
1045  * %G_TYPE_STRING to "Foo", you would write 
1046  * <literal>gtk_tree_store_set (store, iter, 0, "Foo", -1)</literal>.
1047  **/
1048 void
1049 gtk_tree_store_set (GtkTreeStore *tree_store,
1050                     GtkTreeIter  *iter,
1051                     ...)
1052 {
1053   va_list var_args;
1054
1055   va_start (var_args, iter);
1056   gtk_tree_store_set_valist (tree_store, iter, var_args);
1057   va_end (var_args);
1058 }
1059
1060 /**
1061  * gtk_tree_store_remove:
1062  * @tree_store: A #GtkTreeStore
1063  * @iter: A valid #GtkTreeIter
1064  * 
1065  * Removes @iter from @tree_store.  After being removed, @iter is set to the
1066  * next valid row at that level, or invalidated if it previously pointed to the
1067  * last one.
1068  *
1069  * Return value: %TRUE if @iter is still valid, %FALSE if not.
1070  **/
1071 gboolean
1072 gtk_tree_store_remove (GtkTreeStore *tree_store,
1073                        GtkTreeIter  *iter)
1074 {
1075   GtkTreePath *path;
1076   GtkTreeIter new_iter = {0,};
1077   GNode *parent;
1078   GNode *next_node;
1079
1080   g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1081   g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1082
1083   parent = G_NODE (iter->user_data)->parent;
1084
1085   g_assert (parent != NULL);
1086   next_node = G_NODE (iter->user_data)->next;
1087
1088   if (G_NODE (iter->user_data)->data)
1089     g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
1090                      -1, node_free, tree_store->column_headers);
1091
1092   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1093   g_node_destroy (G_NODE (iter->user_data));
1094
1095   gtk_tree_model_row_deleted (GTK_TREE_MODEL (tree_store), path);
1096
1097   if (parent != G_NODE (tree_store->root))
1098     {
1099       /* child_toggled */
1100       if (parent->children == NULL)
1101         {
1102           gtk_tree_path_up (path);
1103
1104           new_iter.stamp = tree_store->stamp;
1105           new_iter.user_data = parent;
1106           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &new_iter);
1107         }
1108     }
1109   gtk_tree_path_free (path);
1110
1111   /* revalidate iter */
1112   if (next_node != NULL)
1113     {
1114       iter->stamp = tree_store->stamp;
1115       iter->user_data = next_node;
1116       return TRUE;
1117     }
1118   else
1119     {
1120       iter->stamp = 0;
1121       iter->user_data = NULL;
1122     }
1123
1124   return FALSE;
1125 }
1126
1127 /**
1128  * gtk_tree_store_insert:
1129  * @tree_store: A #GtkTreeStore
1130  * @iter: An unset #GtkTreeIter to set to the new row
1131  * @parent: A valid #GtkTreeIter, or %NULL
1132  * @position: position to insert the new row
1133  *
1134  * Creates a new row at @position.  If parent is non-%NULL, then the row will be
1135  * made a child of @parent.  Otherwise, the row will be created at the toplevel.
1136  * If @position is larger than the number of rows at that level, then the new
1137  * row will be inserted to the end of the list.  @iter will be changed to point
1138  * to this new row.  The row will be empty after this function is called.  To
1139  * fill in values, you need to call gtk_tree_store_set() or
1140  * gtk_tree_store_set_value().
1141  *
1142  **/
1143 void
1144 gtk_tree_store_insert (GtkTreeStore *tree_store,
1145                        GtkTreeIter  *iter,
1146                        GtkTreeIter  *parent,
1147                        gint          position)
1148 {
1149   GtkTreePath *path;
1150   GNode *parent_node;
1151   GNode *new_node;
1152
1153   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1154   g_return_if_fail (iter != NULL);
1155   if (parent)
1156     g_return_if_fail (VALID_ITER (parent, tree_store));
1157
1158   if (parent)
1159     parent_node = parent->user_data;
1160   else
1161     parent_node = tree_store->root;
1162
1163   tree_store->columns_dirty = TRUE;
1164
1165   new_node = g_node_new (NULL);
1166
1167   iter->stamp = tree_store->stamp;
1168   iter->user_data = new_node;
1169   g_node_insert (parent_node, position, new_node);
1170
1171   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1172   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1173
1174   if (parent_node != tree_store->root)
1175     {
1176       if (new_node->prev == NULL && new_node->next == NULL)
1177         {
1178           gtk_tree_path_up (path);
1179           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1180         }
1181     }
1182
1183   gtk_tree_path_free (path);
1184
1185   validate_tree ((GtkTreeStore*)tree_store);
1186 }
1187
1188 /**
1189  * gtk_tree_store_insert_before:
1190  * @tree_store: A #GtkTreeStore
1191  * @iter: An unset #GtkTreeIter to set to the new row
1192  * @parent: A valid #GtkTreeIter, or %NULL
1193  * @sibling: A valid #GtkTreeIter, or %NULL
1194  *
1195  * Inserts a new row before @sibling.  If @sibling is %NULL, then the row will
1196  * be appended to @parent 's children.  If @parent and @sibling are %NULL, then
1197  * the row will be appended to the toplevel.  If both @sibling and @parent are
1198  * set, then @parent must be the parent of @sibling.  When @sibling is set,
1199  * @parent is optional.
1200  *
1201  * @iter will be changed to point to this new row.  The row will be empty after
1202  * this function is called.  To fill in values, you need to call
1203  * gtk_tree_store_set() or gtk_tree_store_set_value().
1204  *
1205  **/
1206 void
1207 gtk_tree_store_insert_before (GtkTreeStore *tree_store,
1208                               GtkTreeIter  *iter,
1209                               GtkTreeIter  *parent,
1210                               GtkTreeIter  *sibling)
1211 {
1212   GtkTreePath *path;
1213   GNode *parent_node = NULL;
1214   GNode *new_node;
1215
1216   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1217   g_return_if_fail (iter != NULL);
1218   if (parent != NULL)
1219     g_return_if_fail (VALID_ITER (parent, tree_store));
1220   if (sibling != NULL)
1221     g_return_if_fail (VALID_ITER (sibling, tree_store));
1222
1223   if (parent == NULL && sibling == NULL)
1224     parent_node = tree_store->root;
1225   else if (parent == NULL)
1226     parent_node = G_NODE (sibling->user_data)->parent;
1227   else if (sibling == NULL)
1228     parent_node = G_NODE (parent->user_data);
1229   else
1230     {
1231       g_return_if_fail (G_NODE (sibling->user_data)->parent == G_NODE (parent->user_data));
1232       parent_node = G_NODE (parent->user_data);
1233     }
1234
1235   tree_store->columns_dirty = TRUE;
1236
1237   new_node = g_node_new (NULL);
1238
1239   g_node_insert_before (parent_node,
1240                         sibling ? G_NODE (sibling->user_data) : NULL,
1241                         new_node);
1242
1243   iter->stamp = tree_store->stamp;
1244   iter->user_data = new_node;
1245
1246   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1247   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1248
1249   if (parent_node != tree_store->root)
1250     {
1251       if (new_node->prev == NULL && new_node->next == NULL)
1252         {
1253           GtkTreeIter parent_iter;
1254
1255           parent_iter.stamp = tree_store->stamp;
1256           parent_iter.user_data = parent_node;
1257
1258           gtk_tree_path_up (path);
1259           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1260         }
1261     }
1262
1263   gtk_tree_path_free (path);
1264
1265   validate_tree (tree_store);
1266 }
1267
1268 /**
1269  * gtk_tree_store_insert_after:
1270  * @tree_store: A #GtkTreeStore
1271  * @iter: An unset #GtkTreeIter to set to the new row
1272  * @parent: A valid #GtkTreeIter, or %NULL
1273  * @sibling: A valid #GtkTreeIter, or %NULL
1274  *
1275  * Inserts a new row after @sibling.  If @sibling is %NULL, then the row will be
1276  * prepended to @parent 's children.  If @parent and @sibling are %NULL, then
1277  * the row will be prepended to the toplevel.  If both @sibling and @parent are
1278  * set, then @parent must be the parent of @sibling.  When @sibling is set,
1279  * @parent is optional.
1280  *
1281  * @iter will be changed to point to this new row.  The row will be empty after
1282  * this function is called.  To fill in values, you need to call
1283  * gtk_tree_store_set() or gtk_tree_store_set_value().
1284  *
1285  **/
1286 void
1287 gtk_tree_store_insert_after (GtkTreeStore *tree_store,
1288                              GtkTreeIter  *iter,
1289                              GtkTreeIter  *parent,
1290                              GtkTreeIter  *sibling)
1291 {
1292   GtkTreePath *path;
1293   GNode *parent_node;
1294   GNode *new_node;
1295
1296   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1297   g_return_if_fail (iter != NULL);
1298   if (parent != NULL)
1299     g_return_if_fail (VALID_ITER (parent, tree_store));
1300   if (sibling != NULL)
1301     g_return_if_fail (VALID_ITER (sibling, tree_store));
1302
1303   if (parent == NULL && sibling == NULL)
1304     parent_node = tree_store->root;
1305   else if (parent == NULL)
1306     parent_node = G_NODE (sibling->user_data)->parent;
1307   else if (sibling == NULL)
1308     parent_node = G_NODE (parent->user_data);
1309   else
1310     {
1311       g_return_if_fail (G_NODE (sibling->user_data)->parent ==
1312                         G_NODE (parent->user_data));
1313       parent_node = G_NODE (parent->user_data);
1314     }
1315
1316   tree_store->columns_dirty = TRUE;
1317
1318   new_node = g_node_new (NULL);
1319
1320   g_node_insert_after (parent_node,
1321                        sibling ? G_NODE (sibling->user_data) : NULL,
1322                        new_node);
1323
1324   iter->stamp = tree_store->stamp;
1325   iter->user_data = new_node;
1326
1327   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1328   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1329
1330   if (parent_node != tree_store->root)
1331     {
1332       if (new_node->prev == NULL && new_node->next == NULL)
1333         {
1334           GtkTreeIter parent_iter;
1335
1336           parent_iter.stamp = tree_store->stamp;
1337           parent_iter.user_data = parent_node;
1338
1339           gtk_tree_path_up (path);
1340           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1341         }
1342     }
1343
1344   gtk_tree_path_free (path);
1345
1346   validate_tree (tree_store);
1347 }
1348
1349 /**
1350  * gtk_tree_store_insert_with_values:
1351  * @tree_store: A #GtkTreeStore
1352  * @iter: An unset #GtkTreeIter to set the new row, or %NULL.
1353  * @parent: A valid #GtkTreeIter, or %NULL
1354  * @position: position to insert the new row
1355  * @Varargs: pairs of column number and value, terminated with -1
1356  *
1357  * Creates a new row at @position.  @iter will be changed to point to this
1358  * new row.  If @position is larger than the number of rows on the list, then
1359  * the new row will be appended to the list.  The row will be filled with
1360  * the values given to this function.
1361  *
1362  * Calling
1363  * <literal>gtk_tree_store_insert_with_values (tree_store, iter, position, ...)</literal>
1364  * has the same effect as calling
1365  * <informalexample><programlisting>
1366  * gtk_tree_store_insert (tree_store, iter, position);
1367  * gtk_tree_store_set (tree_store, iter, ...);
1368  * </programlisting></informalexample>
1369  * with the different that the former will only emit a row_inserted signal,
1370  * while the latter will emit row_inserted, row_changed and if the tree store
1371  * is sorted, rows_reordered.  Since emitting the rows_reordered signal
1372  * repeatedly can affect the performance of the program,
1373  * gtk_tree_store_insert_with_values() should generally be preferred when
1374  * inserting rows in a sorted tree store.
1375  *
1376  * Since: 2.10
1377  */
1378 void
1379 gtk_tree_store_insert_with_values (GtkTreeStore *tree_store,
1380                                    GtkTreeIter  *iter,
1381                                    GtkTreeIter  *parent,
1382                                    gint          position,
1383                                    ...)
1384 {
1385   GtkTreePath *path;
1386   GNode *parent_node;
1387   GNode *new_node;
1388   GtkTreeIter tmp_iter;
1389   va_list var_args;
1390   gboolean changed = FALSE;
1391   gboolean maybe_need_sort = FALSE;
1392
1393   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1394
1395   if (!iter)
1396     iter = &tmp_iter;
1397
1398   if (parent)
1399     g_return_if_fail (VALID_ITER (parent, tree_store));
1400
1401   if (parent)
1402     parent_node = parent->user_data;
1403   else
1404     parent_node = tree_store->root;
1405
1406   tree_store->columns_dirty = TRUE;
1407
1408   new_node = g_node_new (NULL);
1409
1410   iter->stamp = tree_store->stamp;
1411   iter->user_data = new_node;
1412   g_node_insert (parent_node, position, new_node);
1413
1414   va_start (var_args, position);
1415   gtk_tree_store_set_valist_internal (tree_store, iter,
1416                                       &changed, &maybe_need_sort,
1417                                       var_args);
1418   va_end (var_args);
1419
1420   if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1421     gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1422
1423   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1424   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1425
1426   if (parent_node != tree_store->root)
1427     {
1428       if (new_node->prev == NULL && new_node->next == NULL)
1429         {
1430           gtk_tree_path_up (path);
1431           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1432         }
1433     }
1434
1435   gtk_tree_path_free (path);
1436
1437   validate_tree ((GtkTreeStore *)tree_store);
1438 }
1439
1440 /**
1441  * gtk_tree_store_insert_with_valuesv:
1442  * @tree_store: A #GtkTreeStore
1443  * @iter: An unset #GtkTreeIter to set the new row, or %NULL.
1444  * @parent: A valid #GtkTreeIter, or %NULL
1445  * @position: position to insert the new row
1446  * @columns: an array of column numbers
1447  * @values: an array of GValues
1448  * @n_values: the length of the @columns and @values arrays
1449  *
1450  * A variant of gtk_tree_store_insert_with_values() which takes
1451  * the columns and values as two arrays, instead of varargs.  This
1452  * function is mainly intended for language bindings.
1453  *
1454  * Since: 2.10
1455  */
1456 void
1457 gtk_tree_store_insert_with_valuesv (GtkTreeStore *tree_store,
1458                                     GtkTreeIter  *iter,
1459                                     GtkTreeIter  *parent,
1460                                     gint          position,
1461                                     gint         *columns,
1462                                     GValue       *values,
1463                                     gint          n_values)
1464 {
1465   GtkTreePath *path;
1466   GNode *parent_node;
1467   GNode *new_node;
1468   GtkTreeIter tmp_iter;
1469   gboolean changed = FALSE;
1470   gboolean maybe_need_sort = FALSE;
1471
1472   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1473
1474   if (!iter)
1475     iter = &tmp_iter;
1476
1477   if (parent)
1478     g_return_if_fail (VALID_ITER (parent, tree_store));
1479
1480   if (parent)
1481     parent_node = parent->user_data;
1482   else
1483     parent_node = tree_store->root;
1484
1485   tree_store->columns_dirty = TRUE;
1486
1487   new_node = g_node_new (NULL);
1488
1489   iter->stamp = tree_store->stamp;
1490   iter->user_data = new_node;
1491   g_node_insert (parent_node, position, new_node);
1492
1493   gtk_tree_store_set_vector_internal (tree_store, iter,
1494                                       &changed, &maybe_need_sort,
1495                                       columns, values, n_values);
1496
1497   if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1498     gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1499
1500   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1501   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1502
1503   if (parent_node != tree_store->root)
1504     {
1505       if (new_node->prev == NULL && new_node->next == NULL)
1506         {
1507           gtk_tree_path_up (path);
1508           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1509         }
1510     }
1511
1512   gtk_tree_path_free (path);
1513
1514   validate_tree ((GtkTreeStore *)tree_store);
1515 }
1516
1517 /**
1518  * gtk_tree_store_prepend:
1519  * @tree_store: A #GtkTreeStore
1520  * @iter: An unset #GtkTreeIter to set to the prepended row
1521  * @parent: A valid #GtkTreeIter, or %NULL
1522  * 
1523  * Prepends a new row to @tree_store.  If @parent is non-%NULL, then it will prepend
1524  * the new row before the first child of @parent, otherwise it will prepend a row
1525  * to the top level.  @iter will be changed to point to this new row.  The row
1526  * will be empty after this function is called.  To fill in values, you need to
1527  * call gtk_tree_store_set() or gtk_tree_store_set_value().
1528  **/
1529 void
1530 gtk_tree_store_prepend (GtkTreeStore *tree_store,
1531                         GtkTreeIter  *iter,
1532                         GtkTreeIter  *parent)
1533 {
1534   GNode *parent_node;
1535
1536   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1537   g_return_if_fail (iter != NULL);
1538   if (parent != NULL)
1539     g_return_if_fail (VALID_ITER (parent, tree_store));
1540
1541   tree_store->columns_dirty = TRUE;
1542
1543   if (parent == NULL)
1544     parent_node = tree_store->root;
1545   else
1546     parent_node = parent->user_data;
1547
1548   if (parent_node->children == NULL)
1549     {
1550       GtkTreePath *path;
1551       
1552       iter->stamp = tree_store->stamp;
1553       iter->user_data = g_node_new (NULL);
1554
1555       g_node_prepend (parent_node, G_NODE (iter->user_data));
1556
1557       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1558       gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1559
1560       if (parent_node != tree_store->root)
1561         {
1562           gtk_tree_path_up (path);
1563           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1564         }
1565       gtk_tree_path_free (path);
1566     }
1567   else
1568     {
1569       gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
1570     }
1571
1572   validate_tree (tree_store);
1573 }
1574
1575 /**
1576  * gtk_tree_store_append:
1577  * @tree_store: A #GtkTreeStore
1578  * @iter: An unset #GtkTreeIter to set to the appended row
1579  * @parent: A valid #GtkTreeIter, or %NULL
1580  * 
1581  * Appends a new row to @tree_store.  If @parent is non-%NULL, then it will append the
1582  * new row after the last child of @parent, otherwise it will append a row to
1583  * the top level.  @iter will be changed to point to this new row.  The row will
1584  * be empty after this function is called.  To fill in values, you need to call
1585  * gtk_tree_store_set() or gtk_tree_store_set_value().
1586  **/
1587 void
1588 gtk_tree_store_append (GtkTreeStore *tree_store,
1589                        GtkTreeIter  *iter,
1590                        GtkTreeIter  *parent)
1591 {
1592   GNode *parent_node;
1593
1594   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1595   g_return_if_fail (iter != NULL);
1596   if (parent != NULL)
1597     g_return_if_fail (VALID_ITER (parent, tree_store));
1598
1599   if (parent == NULL)
1600     parent_node = tree_store->root;
1601   else
1602     parent_node = parent->user_data;
1603
1604   tree_store->columns_dirty = TRUE;
1605
1606   if (parent_node->children == NULL)
1607     {
1608       GtkTreePath *path;
1609
1610       iter->stamp = tree_store->stamp;
1611       iter->user_data = g_node_new (NULL);
1612
1613       g_node_append (parent_node, G_NODE (iter->user_data));
1614
1615       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1616       gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1617
1618       if (parent_node != tree_store->root)
1619         {
1620           gtk_tree_path_up (path);
1621           gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1622         }
1623       gtk_tree_path_free (path);
1624     }
1625   else
1626     {
1627       gtk_tree_store_insert_before (tree_store, iter, parent, NULL);
1628     }
1629
1630   validate_tree (tree_store);
1631 }
1632
1633 /**
1634  * gtk_tree_store_is_ancestor:
1635  * @tree_store: A #GtkTreeStore
1636  * @iter: A valid #GtkTreeIter
1637  * @descendant: A valid #GtkTreeIter
1638  * 
1639  * Returns %TRUE if @iter is an ancestor of @descendant.  That is, @iter is the
1640  * parent (or grandparent or great-grandparent) of @descendant.
1641  * 
1642  * Return value: %TRUE, if @iter is an ancestor of @descendant
1643  **/
1644 gboolean
1645 gtk_tree_store_is_ancestor (GtkTreeStore *tree_store,
1646                             GtkTreeIter  *iter,
1647                             GtkTreeIter  *descendant)
1648 {
1649   g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1650   g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1651   g_return_val_if_fail (VALID_ITER (descendant, tree_store), FALSE);
1652
1653   return g_node_is_ancestor (G_NODE (iter->user_data),
1654                              G_NODE (descendant->user_data));
1655 }
1656
1657
1658 /**
1659  * gtk_tree_store_iter_depth:
1660  * @tree_store: A #GtkTreeStore
1661  * @iter: A valid #GtkTreeIter
1662  * 
1663  * Returns the depth of @iter.  This will be 0 for anything on the root level, 1
1664  * for anything down a level, etc.
1665  * 
1666  * Return value: The depth of @iter
1667  **/
1668 gint
1669 gtk_tree_store_iter_depth (GtkTreeStore *tree_store,
1670                            GtkTreeIter  *iter)
1671 {
1672   g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), 0);
1673   g_return_val_if_fail (VALID_ITER (iter, tree_store), 0);
1674
1675   return g_node_depth (G_NODE (iter->user_data)) - 2;
1676 }
1677
1678 /* simple ripoff from g_node_traverse_post_order */
1679 static gboolean
1680 gtk_tree_store_clear_traverse (GNode        *node,
1681                                GtkTreeStore *store)
1682 {
1683   GtkTreeIter iter;
1684
1685   if (node->children)
1686     {
1687       GNode *child;
1688
1689       child = node->children;
1690       while (child)
1691         {
1692           register GNode *current;
1693
1694           current = child;
1695           child = current->next;
1696           if (gtk_tree_store_clear_traverse (current, store))
1697             return TRUE;
1698         }
1699
1700       if (node->parent)
1701         {
1702           iter.stamp = store->stamp;
1703           iter.user_data = node;
1704
1705           gtk_tree_store_remove (store, &iter);
1706         }
1707     }
1708   else if (node->parent)
1709     {
1710       iter.stamp = store->stamp;
1711       iter.user_data = node;
1712
1713       gtk_tree_store_remove (store, &iter);
1714     }
1715
1716   return FALSE;
1717 }
1718
1719 static void
1720 gtk_tree_store_increment_stamp (GtkTreeStore *tree_store)
1721 {
1722   do
1723     {
1724       tree_store->stamp++;
1725     }
1726   while (tree_store->stamp == 0);
1727 }
1728
1729 /**
1730  * gtk_tree_store_clear:
1731  * @tree_store: a #GtkTreeStore
1732  * 
1733  * Removes all rows from @tree_store
1734  **/
1735 void
1736 gtk_tree_store_clear (GtkTreeStore *tree_store)
1737 {
1738   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1739
1740   gtk_tree_store_clear_traverse (tree_store->root, tree_store);
1741   gtk_tree_store_increment_stamp (tree_store);
1742 }
1743
1744 static gboolean
1745 gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
1746                                      GNode       *first)
1747 {
1748   GNode *node;
1749
1750   node = first;
1751
1752   do
1753     {
1754       if (node == iter->user_data)
1755         return TRUE;
1756
1757       if (node->children)
1758         if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
1759           return TRUE;
1760
1761       node = node->next;
1762     }
1763   while (node);
1764
1765   return FALSE;
1766 }
1767
1768 /**
1769  * gtk_tree_store_iter_is_valid:
1770  * @tree_store: A #GtkTreeStore.
1771  * @iter: A #GtkTreeIter.
1772  *
1773  * WARNING: This function is slow. Only use it for debugging and/or testing
1774  * purposes.
1775  *
1776  * Checks if the given iter is a valid iter for this #GtkTreeStore.
1777  *
1778  * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1779  *
1780  * Since: 2.2
1781  **/
1782 gboolean
1783 gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
1784                               GtkTreeIter  *iter)
1785 {
1786   g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1787   g_return_val_if_fail (iter != NULL, FALSE);
1788
1789   if (!VALID_ITER (iter, tree_store))
1790     return FALSE;
1791
1792   return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
1793 }
1794
1795 /* DND */
1796
1797
1798 static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
1799                                                    GtkTreePath       *path)
1800 {
1801   return TRUE;
1802 }
1803                
1804 static gboolean
1805 gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
1806                                  GtkTreePath       *path)
1807 {
1808   GtkTreeIter iter;
1809
1810   if (gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_source),
1811                                &iter,
1812                                path))
1813     {
1814       gtk_tree_store_remove (GTK_TREE_STORE (drag_source),
1815                              &iter);
1816       return TRUE;
1817     }
1818   else
1819     {
1820       return FALSE;
1821     }
1822 }
1823
1824 static gboolean
1825 gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
1826                               GtkTreePath       *path,
1827                               GtkSelectionData  *selection_data)
1828 {
1829   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1830    * target, because the default handler does it for us, but
1831    * we do anyway for the convenience of someone maybe overriding the
1832    * default handler.
1833    */
1834
1835   if (gtk_tree_set_row_drag_data (selection_data,
1836                                   GTK_TREE_MODEL (drag_source),
1837                                   path))
1838     {
1839       return TRUE;
1840     }
1841   else
1842     {
1843       /* FIXME handle text targets at least. */
1844     }
1845
1846   return FALSE;
1847 }
1848
1849 static void
1850 copy_node_data (GtkTreeStore *tree_store,
1851                 GtkTreeIter  *src_iter,
1852                 GtkTreeIter  *dest_iter)
1853 {
1854   GtkTreeDataList *dl = G_NODE (src_iter->user_data)->data;
1855   GtkTreeDataList *copy_head = NULL;
1856   GtkTreeDataList *copy_prev = NULL;
1857   GtkTreeDataList *copy_iter = NULL;
1858   GtkTreePath *path;
1859   gint col;
1860
1861   col = 0;
1862   while (dl)
1863     {
1864       copy_iter = _gtk_tree_data_list_node_copy (dl, tree_store->column_headers[col]);
1865
1866       if (copy_head == NULL)
1867         copy_head = copy_iter;
1868
1869       if (copy_prev)
1870         copy_prev->next = copy_iter;
1871
1872       copy_prev = copy_iter;
1873
1874       dl = dl->next;
1875       ++col;
1876     }
1877
1878   G_NODE (dest_iter->user_data)->data = copy_head;
1879
1880   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), dest_iter);
1881   gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, dest_iter);
1882   gtk_tree_path_free (path);
1883 }
1884
1885 static void
1886 recursive_node_copy (GtkTreeStore *tree_store,
1887                      GtkTreeIter  *src_iter,
1888                      GtkTreeIter  *dest_iter)
1889 {
1890   GtkTreeIter child;
1891   GtkTreeModel *model;
1892
1893   model = GTK_TREE_MODEL (tree_store);
1894
1895   copy_node_data (tree_store, src_iter, dest_iter);
1896
1897   if (gtk_tree_store_iter_children (model,
1898                                     &child,
1899                                     src_iter))
1900     {
1901       /* Need to create children and recurse. Note our
1902        * dependence on persistent iterators here.
1903        */
1904       do
1905         {
1906           GtkTreeIter copy;
1907
1908           /* Gee, a really slow algorithm... ;-) FIXME */
1909           gtk_tree_store_append (tree_store,
1910                                  &copy,
1911                                  dest_iter);
1912
1913           recursive_node_copy (tree_store, &child, &copy);
1914         }
1915       while (gtk_tree_store_iter_next (model, &child));
1916     }
1917 }
1918
1919 static gboolean
1920 gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1921                                    GtkTreePath       *dest,
1922                                    GtkSelectionData  *selection_data)
1923 {
1924   GtkTreeModel *tree_model;
1925   GtkTreeStore *tree_store;
1926   GtkTreeModel *src_model = NULL;
1927   GtkTreePath *src_path = NULL;
1928   gboolean retval = FALSE;
1929
1930   tree_model = GTK_TREE_MODEL (drag_dest);
1931   tree_store = GTK_TREE_STORE (drag_dest);
1932
1933   validate_tree (tree_store);
1934
1935   if (gtk_tree_get_row_drag_data (selection_data,
1936                                   &src_model,
1937                                   &src_path) &&
1938       src_model == tree_model)
1939     {
1940       /* Copy the given row to a new position */
1941       GtkTreeIter src_iter;
1942       GtkTreeIter dest_iter;
1943       GtkTreePath *prev;
1944
1945       if (!gtk_tree_store_get_iter (src_model,
1946                                     &src_iter,
1947                                     src_path))
1948         {
1949           goto out;
1950         }
1951
1952       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1953       prev = gtk_tree_path_copy (dest);
1954
1955       if (!gtk_tree_path_prev (prev))
1956         {
1957           GtkTreeIter dest_parent;
1958           GtkTreePath *parent;
1959           GtkTreeIter *dest_parent_p;
1960
1961           /* dest was the first spot at the current depth; which means
1962            * we are supposed to prepend.
1963            */
1964
1965           /* Get the parent, NULL if parent is the root */
1966           dest_parent_p = NULL;
1967           parent = gtk_tree_path_copy (dest);
1968           if (gtk_tree_path_up (parent) &&
1969               gtk_tree_path_get_depth (parent) > 0)
1970             {
1971               gtk_tree_store_get_iter (tree_model,
1972                                        &dest_parent,
1973                                        parent);
1974               dest_parent_p = &dest_parent;
1975             }
1976           gtk_tree_path_free (parent);
1977           parent = NULL;
1978
1979           gtk_tree_store_prepend (tree_store,
1980                                   &dest_iter,
1981                                   dest_parent_p);
1982
1983           retval = TRUE;
1984         }
1985       else
1986         {
1987           if (gtk_tree_store_get_iter (tree_model, &dest_iter, prev))
1988             {
1989               GtkTreeIter tmp_iter = dest_iter;
1990
1991               gtk_tree_store_insert_after (tree_store, &dest_iter, NULL,
1992                                            &tmp_iter);
1993
1994               retval = TRUE;
1995             }
1996         }
1997
1998       gtk_tree_path_free (prev);
1999
2000       /* If we succeeded in creating dest_iter, walk src_iter tree branch,
2001        * duplicating it below dest_iter.
2002        */
2003
2004       if (retval)
2005         {
2006           recursive_node_copy (tree_store,
2007                                &src_iter,
2008                                &dest_iter);
2009         }
2010     }
2011   else
2012     {
2013       /* FIXME maybe add some data targets eventually, or handle text
2014        * targets in the simple case.
2015        */
2016
2017     }
2018
2019  out:
2020
2021   if (src_path)
2022     gtk_tree_path_free (src_path);
2023
2024   return retval;
2025 }
2026
2027 static gboolean
2028 gtk_tree_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
2029                                   GtkTreePath      *dest_path,
2030                                   GtkSelectionData *selection_data)
2031 {
2032   GtkTreeModel *src_model = NULL;
2033   GtkTreePath *src_path = NULL;
2034   GtkTreePath *tmp = NULL;
2035   gboolean retval = FALSE;
2036   
2037   /* don't accept drops if the tree has been sorted */
2038   if (GTK_TREE_STORE_IS_SORTED (drag_dest))
2039     return FALSE;
2040
2041   if (!gtk_tree_get_row_drag_data (selection_data,
2042                                    &src_model,
2043                                    &src_path))
2044     goto out;
2045     
2046   /* can only drag to ourselves */
2047   if (src_model != GTK_TREE_MODEL (drag_dest))
2048     goto out;
2049
2050   /* Can't drop into ourself. */
2051   if (gtk_tree_path_is_ancestor (src_path,
2052                                  dest_path))
2053     goto out;
2054
2055   /* Can't drop if dest_path's parent doesn't exist */
2056   {
2057     GtkTreeIter iter;
2058
2059     if (gtk_tree_path_get_depth (dest_path) > 1)
2060       {
2061         tmp = gtk_tree_path_copy (dest_path);
2062         gtk_tree_path_up (tmp);
2063         
2064         if (!gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_dest),
2065                                       &iter, tmp))
2066           goto out;
2067       }
2068   }
2069   
2070   /* Can otherwise drop anywhere. */
2071   retval = TRUE;
2072
2073  out:
2074
2075   if (src_path)
2076     gtk_tree_path_free (src_path);
2077   if (tmp)
2078     gtk_tree_path_free (tmp);
2079
2080   return retval;
2081 }
2082
2083 /* Sorting and reordering */
2084 typedef struct _SortTuple
2085 {
2086   gint offset;
2087   GNode *node;
2088 } SortTuple;
2089
2090 /* Reordering */
2091 static gint
2092 gtk_tree_store_reorder_func (gconstpointer a,
2093                              gconstpointer b,
2094                              gpointer      user_data)
2095 {
2096   SortTuple *a_reorder;
2097   SortTuple *b_reorder;
2098
2099   a_reorder = (SortTuple *)a;
2100   b_reorder = (SortTuple *)b;
2101
2102   if (a_reorder->offset < b_reorder->offset)
2103     return -1;
2104   if (a_reorder->offset > b_reorder->offset)
2105     return 1;
2106
2107   return 0;
2108 }
2109
2110 /**
2111  * gtk_tree_store_reorder:
2112  * @tree_store: A #GtkTreeStore.
2113  * @parent: A #GtkTreeIter.
2114  * @new_order: an array of integers mapping the new position of each child
2115  *      to its old position before the re-ordering,
2116  *      i.e. @new_order<literal>[newpos] = oldpos</literal>.
2117  *
2118  * Reorders the children of @parent in @tree_store to follow the order
2119  * indicated by @new_order. Note that this function only works with
2120  * unsorted stores.
2121  *
2122  * Since: 2.2
2123  **/
2124 void
2125 gtk_tree_store_reorder (GtkTreeStore *tree_store,
2126                         GtkTreeIter  *parent,
2127                         gint         *new_order)
2128 {
2129   gint i, length = 0;
2130   GNode *level, *node;
2131   GtkTreePath *path;
2132   SortTuple *sort_array;
2133
2134   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2135   g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2136   g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
2137   g_return_if_fail (new_order != NULL);
2138
2139   if (!parent)
2140     level = G_NODE (tree_store->root)->children;
2141   else
2142     level = G_NODE (parent->user_data)->children;
2143
2144   /* count nodes */
2145   node = level;
2146   while (node)
2147     {
2148       length++;
2149       node = node->next;
2150     }
2151
2152   /* set up sortarray */
2153   sort_array = g_new (SortTuple, length);
2154
2155   node = level;
2156   for (i = 0; i < length; i++)
2157     {
2158       sort_array[new_order[i]].offset = i;
2159       sort_array[i].node = node;
2160
2161       node = node->next;
2162     }
2163
2164   g_qsort_with_data (sort_array,
2165                      length,
2166                      sizeof (SortTuple),
2167                      gtk_tree_store_reorder_func,
2168                      NULL);
2169
2170   /* fix up level */
2171   for (i = 0; i < length - 1; i++)
2172     {
2173       sort_array[i].node->next = sort_array[i+1].node;
2174       sort_array[i+1].node->prev = sort_array[i].node;
2175     }
2176
2177   sort_array[length-1].node->next = NULL;
2178   sort_array[0].node->prev = NULL;
2179   if (parent)
2180     G_NODE (parent->user_data)->children = sort_array[0].node;
2181   else
2182     G_NODE (tree_store->root)->children = sort_array[0].node;
2183
2184   /* emit signal */
2185   if (parent)
2186     path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), parent);
2187   else
2188     path = gtk_tree_path_new ();
2189   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
2190                                  parent, new_order);
2191   gtk_tree_path_free (path);
2192   g_free (sort_array);
2193 }
2194
2195 /**
2196  * gtk_tree_store_swap:
2197  * @tree_store: A #GtkTreeStore.
2198  * @a: A #GtkTreeIter.
2199  * @b: Another #GtkTreeIter.
2200  *
2201  * Swaps @a and @b in the same level of @tree_store. Note that this function
2202  * only works with unsorted stores.
2203  *
2204  * Since: 2.2
2205  **/
2206 void
2207 gtk_tree_store_swap (GtkTreeStore *tree_store,
2208                      GtkTreeIter  *a,
2209                      GtkTreeIter  *b)
2210 {
2211   GNode *tmp, *node_a, *node_b, *parent_node;
2212   GNode *a_prev, *a_next, *b_prev, *b_next;
2213   gint i, a_count, b_count, length, *order;
2214   GtkTreePath *path_a, *path_b;
2215   GtkTreeIter parent;
2216
2217   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2218   g_return_if_fail (VALID_ITER (a, tree_store));
2219   g_return_if_fail (VALID_ITER (b, tree_store));
2220
2221   node_a = G_NODE (a->user_data);
2222   node_b = G_NODE (b->user_data);
2223
2224   /* basic sanity checking */
2225   if (node_a == node_b)
2226     return;
2227
2228   path_a = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), a);
2229   path_b = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), b);
2230
2231   g_return_if_fail (path_a && path_b);
2232
2233   gtk_tree_path_up (path_a);
2234   gtk_tree_path_up (path_b);
2235
2236   if (gtk_tree_path_get_depth (path_a) == 0
2237       || gtk_tree_path_get_depth (path_b) == 0)
2238     {
2239       if (gtk_tree_path_get_depth (path_a) != gtk_tree_path_get_depth (path_b))
2240         {
2241           gtk_tree_path_free (path_a);
2242           gtk_tree_path_free (path_b);
2243                                                                                 
2244           g_warning ("Given children are not in the same level\n");
2245           return;
2246         }
2247       parent_node = G_NODE (tree_store->root);
2248     }
2249   else
2250     {
2251       if (gtk_tree_path_compare (path_a, path_b))
2252         {
2253           gtk_tree_path_free (path_a);
2254           gtk_tree_path_free (path_b);
2255                                                                                 
2256           g_warning ("Given children don't have a common parent\n");
2257           return;
2258         }
2259       gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &parent,
2260                                path_a);
2261       parent_node = G_NODE (parent.user_data);
2262     }
2263   gtk_tree_path_free (path_b);
2264
2265   /* old links which we have to keep around */
2266   a_prev = node_a->prev;
2267   a_next = node_a->next;
2268
2269   b_prev = node_b->prev;
2270   b_next = node_b->next;
2271
2272   /* fix up links if the nodes are next to eachother */
2273   if (a_prev == node_b)
2274     a_prev = node_a;
2275   if (a_next == node_b)
2276     a_next = node_a;
2277
2278   if (b_prev == node_a)
2279     b_prev = node_b;
2280   if (b_next == node_a)
2281     b_next = node_b;
2282
2283   /* counting nodes */
2284   tmp = parent_node->children;
2285   i = a_count = b_count = 0;
2286   while (tmp)
2287     {
2288       if (tmp == node_a)
2289         a_count = i;
2290       if (tmp == node_b)
2291         b_count = i;
2292
2293       tmp = tmp->next;
2294       i++;
2295     }
2296   length = i;
2297
2298   /* hacking the tree */
2299   if (!a_prev)
2300     parent_node->children = node_b;
2301   else
2302     a_prev->next = node_b;
2303
2304   if (a_next)
2305     a_next->prev = node_b;
2306
2307   if (!b_prev)
2308     parent_node->children = node_a;
2309   else
2310     b_prev->next = node_a;
2311
2312   if (b_next)
2313     b_next->prev = node_a;
2314
2315   node_a->prev = b_prev;
2316   node_a->next = b_next;
2317
2318   node_b->prev = a_prev;
2319   node_b->next = a_next;
2320
2321   /* emit signal */
2322   order = g_new (gint, length);
2323   for (i = 0; i < length; i++)
2324     if (i == a_count)
2325       order[i] = b_count;
2326     else if (i == b_count)
2327       order[i] = a_count;
2328     else
2329       order[i] = i;
2330
2331   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
2332                                  parent_node == tree_store->root 
2333                                  ? NULL : &parent, order);
2334   gtk_tree_path_free (path_a);
2335   g_free (order);
2336 }
2337
2338 /* WARNING: this function is *incredibly* fragile. Please smashtest after
2339  * making changes here.
2340  *      -Kris
2341  */
2342 static void
2343 gtk_tree_store_move (GtkTreeStore *tree_store,
2344                      GtkTreeIter  *iter,
2345                      GtkTreeIter  *position,
2346                      gboolean      before)
2347 {
2348   GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
2349   gint old_pos, new_pos, length, i, *order;
2350   GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
2351   GtkTreeIter parent_iter, dst_a, dst_b;
2352   gint depth = 0;
2353   gboolean handle_b = TRUE;
2354
2355   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2356   g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2357   g_return_if_fail (VALID_ITER (iter, tree_store));
2358   if (position)
2359     g_return_if_fail (VALID_ITER (position, tree_store));
2360
2361   a = b = NULL;
2362
2363   /* sanity checks */
2364   if (position)
2365     {
2366       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2367       pos_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
2368                                           position);
2369
2370       /* if before:
2371        *   moving the iter before path or "path + 1" doesn't make sense
2372        * else
2373        *   moving the iter before path or "path - 1" doesn't make sense
2374        */
2375       if (!gtk_tree_path_compare (path, pos_path))
2376         goto free_paths_and_out;
2377
2378       if (before)
2379         gtk_tree_path_next (path);
2380       else
2381         gtk_tree_path_prev (path);
2382
2383       if (!gtk_tree_path_compare (path, pos_path))
2384         goto free_paths_and_out;
2385
2386       if (before)
2387         gtk_tree_path_prev (path);
2388       else
2389         gtk_tree_path_next (path);
2390
2391       if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
2392         {
2393           g_warning ("Given children are not in the same level\n");
2394
2395           goto free_paths_and_out;
2396         }
2397
2398       tmppath = gtk_tree_path_copy (pos_path);
2399       gtk_tree_path_up (path);
2400       gtk_tree_path_up (tmppath);
2401
2402       if (gtk_tree_path_get_depth (path) > 0 &&
2403           gtk_tree_path_compare (path, tmppath))
2404         {
2405           g_warning ("Given children are not in the same level\n");
2406
2407           gtk_tree_path_free (tmppath);
2408           goto free_paths_and_out;
2409         }
2410
2411       gtk_tree_path_free (tmppath);
2412     }
2413
2414   if (!path)
2415     {
2416       path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2417       gtk_tree_path_up (path);
2418     }
2419
2420   depth = gtk_tree_path_get_depth (path);
2421
2422   if (depth)
2423     {
2424       gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), 
2425                                &parent_iter, path);
2426
2427       parent = G_NODE (parent_iter.user_data);
2428     }
2429   else
2430     parent = G_NODE (tree_store->root);
2431
2432   /* yes, I know that this can be done shorter, but I'm doing it this way
2433    * so the code is also maintainable
2434    */
2435
2436   if (before && position)
2437     {
2438       b = G_NODE (position->user_data);
2439
2440       if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
2441         {
2442           gtk_tree_path_prev (pos_path);
2443           if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), 
2444                                        &dst_a, pos_path))
2445             a = G_NODE (dst_a.user_data);
2446           else
2447             a = NULL;
2448           gtk_tree_path_next (pos_path);
2449         }
2450
2451       /* if b is NULL, a is NULL too -- we are at the beginning of the list
2452        * yes and we leak memory here ...
2453        */
2454       g_return_if_fail (b);
2455     }
2456   else if (before && !position)
2457     {
2458       /* move before without position is appending */
2459       a = NULL;
2460       b = NULL;
2461     }
2462   else /* !before */
2463     {
2464       if (position)
2465         a = G_NODE (position->user_data);
2466       else
2467         a = NULL;
2468
2469       if (position)
2470         {
2471           gtk_tree_path_next (pos_path);
2472           if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
2473              b = G_NODE (dst_b.user_data);
2474           else
2475              b = NULL;
2476           gtk_tree_path_prev (pos_path);
2477         }
2478       else
2479         {
2480           /* move after without position is prepending */
2481           if (depth)
2482             gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2483                                           &parent_iter);
2484           else
2485             gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2486                                           NULL);
2487
2488           b = G_NODE (dst_b.user_data);
2489         }
2490
2491       /* if a is NULL, b is NULL too -- we are at the end of the list
2492        * yes and we leak memory here ...
2493        */
2494       if (position)
2495         g_return_if_fail (a);
2496     }
2497
2498   /* counting nodes */
2499   tmp = parent->children;
2500
2501   length = old_pos = 0;
2502   while (tmp)
2503     {
2504       if (tmp == iter->user_data)
2505         old_pos = length;
2506
2507       tmp = tmp->next;
2508       length++;
2509     }
2510
2511   /* remove node from list */
2512   node = G_NODE (iter->user_data);
2513   tmp_a = node->prev;
2514   tmp_b = node->next;
2515
2516   if (tmp_a)
2517     tmp_a->next = tmp_b;
2518   else
2519     parent->children = tmp_b;
2520
2521   if (tmp_b)
2522     tmp_b->prev = tmp_a;
2523
2524   /* and reinsert the node */
2525   if (a)
2526     {
2527       tmp = a->next;
2528
2529       a->next = node;
2530       node->next = tmp;
2531       node->prev = a;
2532     }
2533   else if (!a && !before)
2534     {
2535       tmp = parent->children;
2536
2537       node->prev = NULL;
2538       parent->children = node;
2539
2540       node->next = tmp;
2541       if (tmp) 
2542         tmp->prev = node;
2543
2544       handle_b = FALSE;
2545     }
2546   else if (!a && before)
2547     {
2548       if (!position)
2549         {
2550           node->parent = NULL;
2551           node->next = node->prev = NULL;
2552
2553           /* before with sibling = NULL appends */
2554           g_node_insert_before (parent, NULL, node);
2555         }
2556       else
2557         {
2558           node->parent = NULL;
2559           node->next = node->prev = NULL;
2560
2561           /* after with sibling = NULL prepends */
2562           g_node_insert_after (parent, NULL, node);
2563         }
2564
2565       handle_b = FALSE;
2566     }
2567
2568   if (handle_b)
2569     {
2570       if (b)
2571         {
2572           tmp = b->prev;
2573
2574           b->prev = node;
2575           node->prev = tmp;
2576           node->next = b;
2577         }
2578       else if (!(!a && before)) /* !a && before is completely handled above */
2579         node->next = NULL;
2580     }
2581
2582   /* emit signal */
2583   if (position)
2584     new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
2585   else if (before)
2586     {
2587       if (depth)
2588         new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2589                                                   &parent_iter) - 1;
2590       else
2591         new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2592                                                   NULL) - 1;
2593     }
2594   else
2595     new_pos = 0;
2596
2597   if (new_pos > old_pos)
2598     {
2599       if (before && position)
2600         new_pos--;
2601     }
2602   else
2603     {
2604       if (!before && position)
2605         new_pos++;
2606     }
2607
2608   order = g_new (gint, length);
2609   if (new_pos > old_pos)
2610     {
2611       for (i = 0; i < length; i++)
2612         if (i < old_pos)
2613           order[i] = i;
2614         else if (i >= old_pos && i < new_pos)
2615           order[i] = i + 1;
2616         else if (i == new_pos)
2617           order[i] = old_pos;
2618         else
2619           order[i] = i;
2620     }
2621   else
2622     {
2623       for (i = 0; i < length; i++)
2624         if (i == new_pos)
2625           order[i] = old_pos;
2626         else if (i > new_pos && i <= old_pos)
2627           order[i] = i - 1;
2628         else
2629           order[i] = i;
2630     }
2631
2632   if (depth)
2633     {
2634       tmppath = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), 
2635                                          &parent_iter);
2636       gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2637                                      tmppath, &parent_iter, order);
2638     }
2639   else
2640     {
2641       tmppath = gtk_tree_path_new ();
2642       gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2643                                      tmppath, NULL, order);
2644     }
2645
2646   gtk_tree_path_free (tmppath);
2647   gtk_tree_path_free (path);
2648   if (position)
2649     gtk_tree_path_free (pos_path);
2650   g_free (order);
2651
2652   return;
2653
2654 free_paths_and_out:
2655   gtk_tree_path_free (path);
2656   gtk_tree_path_free (pos_path);
2657 }
2658
2659 /**
2660  * gtk_tree_store_move_before:
2661  * @tree_store: A #GtkTreeStore.
2662  * @iter: A #GtkTreeIter.
2663  * @position: A #GtkTreeIter or %NULL.
2664  *
2665  * Moves @iter in @tree_store to the position before @position. @iter and
2666  * @position should be in the same level. Note that this function only
2667  * works with unsorted stores. If @position is %NULL, @iter will be
2668  * moved to the end of the level.
2669  *
2670  * Since: 2.2
2671  **/
2672 void
2673 gtk_tree_store_move_before (GtkTreeStore *tree_store,
2674                             GtkTreeIter  *iter,
2675                             GtkTreeIter  *position)
2676 {
2677   gtk_tree_store_move (tree_store, iter, position, TRUE);
2678 }
2679
2680 /**
2681  * gtk_tree_store_move_after:
2682  * @tree_store: A #GtkTreeStore.
2683  * @iter: A #GtkTreeIter.
2684  * @position: A #GtkTreeIter.
2685  *
2686  * Moves @iter in @tree_store to the position after @position. @iter and
2687  * @position should be in the same level. Note that this function only
2688  * works with unsorted stores. If @position is %NULL, @iter will be moved
2689  * to the start of the level.
2690  *
2691  * Since: 2.2
2692  **/
2693 void
2694 gtk_tree_store_move_after (GtkTreeStore *tree_store,
2695                            GtkTreeIter  *iter,
2696                            GtkTreeIter  *position)
2697 {
2698   gtk_tree_store_move (tree_store, iter, position, FALSE);
2699 }
2700
2701 /* Sorting */
2702 static gint
2703 gtk_tree_store_compare_func (gconstpointer a,
2704                              gconstpointer b,
2705                              gpointer      user_data)
2706 {
2707   GtkTreeStore *tree_store = user_data;
2708   GNode *node_a;
2709   GNode *node_b;
2710   GtkTreeIterCompareFunc func;
2711   gpointer data;
2712
2713   GtkTreeIter iter_a;
2714   GtkTreeIter iter_b;
2715   gint retval;
2716
2717   if (tree_store->sort_column_id != -1)
2718     {
2719       GtkTreeDataSortHeader *header;
2720
2721       header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2722                                                tree_store->sort_column_id);
2723       g_return_val_if_fail (header != NULL, 0);
2724       g_return_val_if_fail (header->func != NULL, 0);
2725
2726       func = header->func;
2727       data = header->data;
2728     }
2729   else
2730     {
2731       g_return_val_if_fail (tree_store->default_sort_func != NULL, 0);
2732       func = tree_store->default_sort_func;
2733       data = tree_store->default_sort_data;
2734     }
2735
2736   node_a = ((SortTuple *) a)->node;
2737   node_b = ((SortTuple *) b)->node;
2738
2739   iter_a.stamp = tree_store->stamp;
2740   iter_a.user_data = node_a;
2741   iter_b.stamp = tree_store->stamp;
2742   iter_b.user_data = node_b;
2743
2744   retval = (* func) (GTK_TREE_MODEL (user_data), &iter_a, &iter_b, data);
2745
2746   if (tree_store->order == GTK_SORT_DESCENDING)
2747     {
2748       if (retval > 0)
2749         retval = -1;
2750       else if (retval < 0)
2751         retval = 1;
2752     }
2753   return retval;
2754 }
2755
2756 static void
2757 gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
2758                             GNode        *parent,
2759                             gboolean      recurse)
2760 {
2761   GtkTreeIter iter;
2762   GArray *sort_array;
2763   GNode *node;
2764   GNode *tmp_node;
2765   gint list_length;
2766   gint i;
2767   gint *new_order;
2768   GtkTreePath *path;
2769
2770   node = parent->children;
2771   if (node == NULL || node->next == NULL)
2772     {
2773       if (recurse && node && node->children)
2774         gtk_tree_store_sort_helper (tree_store, node, TRUE);
2775
2776       return;
2777     }
2778
2779   list_length = 0;
2780   for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2781     list_length++;
2782
2783   sort_array = g_array_sized_new (FALSE, FALSE, sizeof (SortTuple), list_length);
2784
2785   i = 0;
2786   for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2787     {
2788       SortTuple tuple;
2789
2790       tuple.offset = i;
2791       tuple.node = tmp_node;
2792       g_array_append_val (sort_array, tuple);
2793       i++;
2794     }
2795
2796   /* Sort the array */
2797   g_array_sort_with_data (sort_array, gtk_tree_store_compare_func, tree_store);
2798
2799   for (i = 0; i < list_length - 1; i++)
2800     {
2801       g_array_index (sort_array, SortTuple, i).node->next =
2802         g_array_index (sort_array, SortTuple, i + 1).node;
2803       g_array_index (sort_array, SortTuple, i + 1).node->prev =
2804         g_array_index (sort_array, SortTuple, i).node;
2805     }
2806   g_array_index (sort_array, SortTuple, list_length - 1).node->next = NULL;
2807   g_array_index (sort_array, SortTuple, 0).node->prev = NULL;
2808   parent->children = g_array_index (sort_array, SortTuple, 0).node;
2809
2810   /* Let the world know about our new order */
2811   new_order = g_new (gint, list_length);
2812   for (i = 0; i < list_length; i++)
2813     new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2814
2815   iter.stamp = tree_store->stamp;
2816   iter.user_data = parent;
2817   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &iter);
2818   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2819                                  path, &iter, new_order);
2820   gtk_tree_path_free (path);
2821   g_free (new_order);
2822   g_array_free (sort_array, TRUE);
2823
2824   if (recurse)
2825     {
2826       for (tmp_node = parent->children; tmp_node; tmp_node = tmp_node->next)
2827         {
2828           if (tmp_node->children)
2829             gtk_tree_store_sort_helper (tree_store, tmp_node, TRUE);
2830         }
2831     }
2832 }
2833
2834 static void
2835 gtk_tree_store_sort (GtkTreeStore *tree_store)
2836 {
2837   if (!GTK_TREE_STORE_IS_SORTED (tree_store))
2838     return;
2839
2840   if (tree_store->sort_column_id != -1)
2841     {
2842       GtkTreeDataSortHeader *header = NULL;
2843
2844       header = _gtk_tree_data_list_get_header (tree_store->sort_list, 
2845                                                tree_store->sort_column_id);
2846
2847       /* We want to make sure that we have a function */
2848       g_return_if_fail (header != NULL);
2849       g_return_if_fail (header->func != NULL);
2850     }
2851   else
2852     {
2853       g_return_if_fail (tree_store->default_sort_func != NULL);
2854     }
2855
2856   gtk_tree_store_sort_helper (tree_store, G_NODE (tree_store->root), TRUE);
2857 }
2858
2859 static void
2860 gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
2861                                   GtkTreeIter  *iter,
2862                                   gint          column,
2863                                   gboolean      emit_signal)
2864 {
2865   GNode *prev = NULL;
2866   GNode *next = NULL;
2867   GNode *node;
2868   GtkTreePath *tmp_path;
2869   GtkTreeIter tmp_iter;
2870   gint cmp_a = 0;
2871   gint cmp_b = 0;
2872   gint i;
2873   gint old_location;
2874   gint new_location;
2875   gint *new_order;
2876   gint length;
2877   GtkTreeIterCompareFunc func;
2878   gpointer data;
2879
2880   g_return_if_fail (G_NODE (iter->user_data)->parent != NULL);
2881
2882   tmp_iter.stamp = tree_store->stamp;
2883   if (tree_store->sort_column_id != -1)
2884     {
2885       GtkTreeDataSortHeader *header;
2886       header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2887                                                tree_store->sort_column_id);
2888       g_return_if_fail (header != NULL);
2889       g_return_if_fail (header->func != NULL);
2890       func = header->func;
2891       data = header->data;
2892     }
2893   else
2894     {
2895       g_return_if_fail (tree_store->default_sort_func != NULL);
2896       func = tree_store->default_sort_func;
2897       data = tree_store->default_sort_data;
2898     }
2899
2900   /* If it's the built in function, we don't sort. */
2901   if (func == _gtk_tree_data_list_compare_func &&
2902       tree_store->sort_column_id != column)
2903     return;
2904
2905   old_location = 0;
2906   node = G_NODE (iter->user_data)->parent->children;
2907   /* First we find the iter, its prev, and its next */
2908   while (node)
2909     {
2910       if (node == G_NODE (iter->user_data))
2911         break;
2912       old_location++;
2913       node = node->next;
2914     }
2915   g_assert (node != NULL);
2916
2917   prev = node->prev;
2918   next = node->next;
2919
2920   /* Check the common case, where we don't need to sort it moved. */
2921   if (prev != NULL)
2922     {
2923       tmp_iter.user_data = prev;
2924       cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2925     }
2926
2927   if (next != NULL)
2928     {
2929       tmp_iter.user_data = next;
2930       cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2931     }
2932
2933   if (tree_store->order == GTK_SORT_DESCENDING)
2934     {
2935       if (cmp_a < 0)
2936         cmp_a = 1;
2937       else if (cmp_a > 0)
2938         cmp_a = -1;
2939
2940       if (cmp_b < 0)
2941         cmp_b = 1;
2942       else if (cmp_b > 0)
2943         cmp_b = -1;
2944     }
2945
2946   if (prev == NULL && cmp_b <= 0)
2947     return;
2948   else if (next == NULL && cmp_a <= 0)
2949     return;
2950   else if (prev != NULL && next != NULL &&
2951            cmp_a <= 0 && cmp_b <= 0)
2952     return;
2953
2954   /* We actually need to sort it */
2955   /* First, remove the old link. */
2956
2957   if (prev)
2958     prev->next = next;
2959   else
2960     node->parent->children = next;
2961
2962   if (next)
2963     next->prev = prev;
2964
2965   node->prev = NULL;
2966   node->next = NULL;
2967
2968   /* FIXME: as an optimization, we can potentially start at next */
2969   prev = NULL;
2970   node = node->parent->children;
2971   new_location = 0;
2972   tmp_iter.user_data = node;
2973   if (tree_store->order == GTK_SORT_DESCENDING)
2974     cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2975   else
2976     cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2977
2978   while ((node->next) && (cmp_a > 0))
2979     {
2980       prev = node;
2981       node = node->next;
2982       new_location++;
2983       tmp_iter.user_data = node;
2984       if (tree_store->order == GTK_SORT_DESCENDING)
2985         cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2986       else
2987         cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2988     }
2989
2990   if ((!node->next) && (cmp_a > 0))
2991     {
2992       new_location++;
2993       node->next = G_NODE (iter->user_data);
2994       node->next->prev = node;
2995     }
2996   else if (prev)
2997     {
2998       prev->next = G_NODE (iter->user_data);
2999       prev->next->prev = prev;
3000       G_NODE (iter->user_data)->next = node;
3001       G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
3002     }
3003   else
3004     {
3005       G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
3006       G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
3007       G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
3008     }
3009
3010   if (!emit_signal)
3011     return;
3012
3013   /* Emit the reordered signal. */
3014   length = g_node_n_children (node->parent);
3015   new_order = g_new (int, length);
3016   if (old_location < new_location)
3017     for (i = 0; i < length; i++)
3018       {
3019         if (i < old_location ||
3020             i > new_location)
3021           new_order[i] = i;
3022         else if (i >= old_location &&
3023                  i < new_location)
3024           new_order[i] = i + 1;
3025         else if (i == new_location)
3026           new_order[i] = old_location;
3027       }
3028   else
3029     for (i = 0; i < length; i++)
3030       {
3031         if (i < new_location ||
3032             i > old_location)
3033           new_order[i] = i;
3034         else if (i > new_location &&
3035                  i <= old_location)
3036           new_order[i] = i - 1;
3037         else if (i == new_location)
3038           new_order[i] = old_location;
3039       }
3040
3041   tmp_iter.user_data = node->parent;
3042   tmp_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &tmp_iter);
3043
3044   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
3045                                  tmp_path, &tmp_iter,
3046                                  new_order);
3047
3048   gtk_tree_path_free (tmp_path);
3049   g_free (new_order);
3050 }
3051
3052
3053 static gboolean
3054 gtk_tree_store_get_sort_column_id (GtkTreeSortable  *sortable,
3055                                    gint             *sort_column_id,
3056                                    GtkSortType      *order)
3057 {
3058   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3059
3060   if (sort_column_id)
3061     * sort_column_id = tree_store->sort_column_id;
3062   if (order)
3063     * order = tree_store->order;
3064
3065   if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
3066       tree_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3067     return FALSE;
3068
3069   return TRUE;
3070 }
3071
3072 static void
3073 gtk_tree_store_set_sort_column_id (GtkTreeSortable  *sortable,
3074                                    gint              sort_column_id,
3075                                    GtkSortType       order)
3076 {
3077   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3078
3079   
3080   if ((tree_store->sort_column_id == sort_column_id) &&
3081       (tree_store->order == order))
3082     return;
3083
3084   if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3085     {
3086       if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3087         {
3088           GtkTreeDataSortHeader *header = NULL;
3089
3090           header = _gtk_tree_data_list_get_header (tree_store->sort_list, 
3091                                                    sort_column_id);
3092
3093           /* We want to make sure that we have a function */
3094           g_return_if_fail (header != NULL);
3095           g_return_if_fail (header->func != NULL);
3096         }
3097       else
3098         {
3099           g_return_if_fail (tree_store->default_sort_func != NULL);
3100         }
3101     }
3102
3103   tree_store->sort_column_id = sort_column_id;
3104   tree_store->order = order;
3105
3106   gtk_tree_sortable_sort_column_changed (sortable);
3107
3108   gtk_tree_store_sort (tree_store);
3109 }
3110
3111 static void
3112 gtk_tree_store_set_sort_func (GtkTreeSortable        *sortable,
3113                               gint                    sort_column_id,
3114                               GtkTreeIterCompareFunc  func,
3115                               gpointer                data,
3116                               GtkDestroyNotify        destroy)
3117 {
3118   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3119
3120   tree_store->sort_list = _gtk_tree_data_list_set_header (tree_store->sort_list, 
3121                                                           sort_column_id, 
3122                                                           func, data, destroy);
3123
3124   if (tree_store->sort_column_id == sort_column_id)
3125     gtk_tree_store_sort (tree_store);
3126 }
3127
3128 static void
3129 gtk_tree_store_set_default_sort_func (GtkTreeSortable        *sortable,
3130                                       GtkTreeIterCompareFunc  func,
3131                                       gpointer                data,
3132                                       GtkDestroyNotify        destroy)
3133 {
3134   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3135
3136   if (tree_store->default_sort_destroy)
3137     {
3138       GtkDestroyNotify d = tree_store->default_sort_destroy;
3139
3140       tree_store->default_sort_destroy = NULL;
3141       d (tree_store->default_sort_data);
3142     }
3143
3144   tree_store->default_sort_func = func;
3145   tree_store->default_sort_data = data;
3146   tree_store->default_sort_destroy = destroy;
3147
3148   if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3149     gtk_tree_store_sort (tree_store);
3150 }
3151
3152 static gboolean
3153 gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable)
3154 {
3155   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3156
3157   return (tree_store->default_sort_func != NULL);
3158 }
3159
3160 static void
3161 validate_gnode (GNode* node)
3162 {
3163   GNode *iter;
3164
3165   iter = node->children;
3166   while (iter != NULL)
3167     {
3168       g_assert (iter->parent == node);
3169       if (iter->prev)
3170         g_assert (iter->prev->next == iter);
3171       validate_gnode (iter);
3172       iter = iter->next;
3173     }
3174 }
3175
3176 #define __GTK_TREE_STORE_C__
3177 #include "gtkaliasdef.c"