]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
remove gtk_tree_model_get_first in favor of gtk_tree_model_get_iter_root.
[~andy/gtk] / gtk / gtkliststore.c
1 /* gtkliststore.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 <string.h>
21 #include "gtktreemodel.h"
22 #include "gtkliststore.h"
23 #include "gtktreedatalist.h"
24 #include "gtksignal.h"
25 #include "gtktreednd.h"
26 #include <gobject/gvaluecollector.h>
27
28 #define G_SLIST(x) ((GSList *) x)
29 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -1)
30 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
31
32 static void         gtk_list_store_init            (GtkListStore      *list_store);
33 static void         gtk_list_store_class_init      (GtkListStoreClass *class);
34 static void         gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
35 static void         gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void         gtk_list_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
37 static void         gtk_list_store_sortable_init   (GtkTreeSortableIface   *iface);
38 static guint        gtk_list_store_get_flags       (GtkTreeModel      *tree_model);
39 static gint         gtk_list_store_get_n_columns   (GtkTreeModel      *tree_model);
40 static GType        gtk_list_store_get_column_type (GtkTreeModel      *tree_model,
41                                                     gint               index);
42 static gboolean     gtk_list_store_get_iter        (GtkTreeModel      *tree_model,
43                                                     GtkTreeIter       *iter,
44                                                     GtkTreePath       *path);
45 static GtkTreePath *gtk_list_store_get_path        (GtkTreeModel      *tree_model,
46                                                     GtkTreeIter       *iter);
47 static void         gtk_list_store_get_value       (GtkTreeModel      *tree_model,
48                                                     GtkTreeIter       *iter,
49                                                     gint               column,
50                                                     GValue            *value);
51 static gboolean     gtk_list_store_iter_next       (GtkTreeModel      *tree_model,
52                                                     GtkTreeIter       *iter);
53 static gboolean     gtk_list_store_iter_children   (GtkTreeModel      *tree_model,
54                                                     GtkTreeIter       *iter,
55                                                     GtkTreeIter       *parent);
56 static gboolean     gtk_list_store_iter_has_child  (GtkTreeModel      *tree_model,
57                                                     GtkTreeIter       *iter);
58 static gint         gtk_list_store_iter_n_children (GtkTreeModel      *tree_model,
59                                                     GtkTreeIter       *iter);
60 static gboolean     gtk_list_store_iter_nth_child  (GtkTreeModel      *tree_model,
61                                                     GtkTreeIter       *iter,
62                                                     GtkTreeIter       *parent,
63                                                     gint               n);
64 static gboolean     gtk_list_store_iter_parent     (GtkTreeModel      *tree_model,
65                                                     GtkTreeIter       *iter,
66                                                     GtkTreeIter       *child);
67
68
69 static void gtk_list_store_set_n_columns   (GtkListStore *list_store,
70                                             gint          n_columns);
71 static void gtk_list_store_set_column_type (GtkListStore *list_store,
72                                             gint          column,
73                                             GType         type);
74
75
76 /* Drag and Drop */
77 static gboolean gtk_list_store_drag_data_delete   (GtkTreeDragSource *drag_source,
78                                                    GtkTreePath       *path);
79 static gboolean gtk_list_store_drag_data_get      (GtkTreeDragSource *drag_source,
80                                                    GtkTreePath       *path,
81                                                    GtkSelectionData  *selection_data);
82 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
83                                                    GtkTreePath       *dest,
84                                                    GtkSelectionData  *selection_data);
85 static gboolean gtk_list_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
86                                                    GtkTreeModel      *src_model,
87                                                    GtkTreePath       *src_path,
88                                                    GtkTreePath       *dest_path);
89
90
91 /* sortable */
92 static void     gtk_list_store_sort               (GtkListStore           *list_store);
93 static void     gtk_list_store_sort_iter_changed  (GtkListStore           *list_store,
94                                                    GtkTreeIter            *iter,
95                                                    gint                    column);
96 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable        *sortable,
97                                                    gint                   *sort_column_id,
98                                                    GtkTreeSortOrder       *order);
99 static void     gtk_list_store_set_sort_column_id (GtkTreeSortable        *sortable,
100                                                    gint                    sort_column_id,
101                                                    GtkTreeSortOrder        order);
102 static void     gtk_list_store_set_sort_func      (GtkTreeSortable        *sortable,
103                                                    gint                    sort_column_id,
104                                                    GtkTreeIterCompareFunc  func,
105                                                    gpointer                data,
106                                                    GtkDestroyNotify        destroy);
107
108
109 static void
110 validate_list_store (GtkListStore *list_store)
111 {
112   if (gtk_debug_flags & GTK_DEBUG_TREE)
113     {
114       g_assert (g_slist_length (list_store->root) == list_store->length);
115
116       g_assert (g_slist_last (list_store->root) == list_store->tail);
117     }
118 }
119
120 GtkType
121 gtk_list_store_get_type (void)
122 {
123   static GType list_store_type = 0;
124
125   if (!list_store_type)
126     {
127       static const GTypeInfo list_store_info =
128       {
129         sizeof (GtkListStoreClass),
130         NULL,           /* base_init */
131         NULL,           /* base_finalize */
132         (GClassInitFunc) gtk_list_store_class_init,
133         NULL,           /* class_finalize */
134         NULL,           /* class_data */
135         sizeof (GtkListStore),
136         0,
137         (GInstanceInitFunc) gtk_list_store_init,
138       };
139
140       static const GInterfaceInfo tree_model_info =
141       {
142         (GInterfaceInitFunc) gtk_list_store_tree_model_init,
143         NULL,
144         NULL
145       };
146
147       static const GInterfaceInfo drag_source_info =
148       {
149         (GInterfaceInitFunc) gtk_list_store_drag_source_init,
150         NULL,
151         NULL
152       };
153
154       static const GInterfaceInfo drag_dest_info =
155       {
156         (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
157         NULL,
158         NULL
159       };
160
161       static const GInterfaceInfo sortable_info =
162       {
163         (GInterfaceInitFunc) gtk_list_store_sortable_init,
164         NULL,
165         NULL
166       };
167
168       list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
169       g_type_add_interface_static (list_store_type,
170                                    GTK_TYPE_TREE_MODEL,
171                                    &tree_model_info);
172       g_type_add_interface_static (list_store_type,
173                                    GTK_TYPE_TREE_DRAG_SOURCE,
174                                    &drag_source_info);
175       g_type_add_interface_static (list_store_type,
176                                    GTK_TYPE_TREE_DRAG_DEST,
177                                    &drag_dest_info);
178       g_type_add_interface_static (list_store_type,
179                                    GTK_TYPE_TREE_SORTABLE,
180                                    &sortable_info);
181     }
182
183   return list_store_type;
184 }
185
186 static void
187 gtk_list_store_class_init (GtkListStoreClass *class)
188 {
189   GObjectClass *object_class;
190
191   object_class = (GObjectClass*) class;
192 }
193
194 static void
195 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
196 {
197   iface->get_flags = gtk_list_store_get_flags;
198   iface->get_n_columns = gtk_list_store_get_n_columns;
199   iface->get_column_type = gtk_list_store_get_column_type;
200   iface->get_iter = gtk_list_store_get_iter;
201   iface->get_path = gtk_list_store_get_path;
202   iface->get_value = gtk_list_store_get_value;
203   iface->iter_next = gtk_list_store_iter_next;
204   iface->iter_children = gtk_list_store_iter_children;
205   iface->iter_has_child = gtk_list_store_iter_has_child;
206   iface->iter_n_children = gtk_list_store_iter_n_children;
207   iface->iter_nth_child = gtk_list_store_iter_nth_child;
208   iface->iter_parent = gtk_list_store_iter_parent;
209 }
210
211 static void
212 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
213 {
214   iface->drag_data_delete = gtk_list_store_drag_data_delete;
215   iface->drag_data_get = gtk_list_store_drag_data_get;
216 }
217
218 static void
219 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
220 {
221   iface->drag_data_received = gtk_list_store_drag_data_received;
222   iface->row_drop_possible = gtk_list_store_row_drop_possible;
223 }
224
225 static void
226 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
227 {
228   iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
229   iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
230   iface->set_sort_func = gtk_list_store_set_sort_func;
231 }
232
233 static void
234 gtk_list_store_init (GtkListStore *list_store)
235 {
236   list_store->root = NULL;
237   list_store->tail = NULL;
238   list_store->sort_list = NULL;
239   list_store->stamp = g_random_int ();
240   list_store->length = 0;
241   list_store->sort_column_id = -1;
242 }
243
244 /**
245  * gtk_list_store_new:
246  * @n_columns: number of columns in the list store
247  * @Varargs: all #GType types for the columns, from first to last
248  *
249  * Creates a new list store as with @n_columns columns each of the types passed
250  * in.  As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
251  * GDK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
252  * int, string and GDkPixbuf respectively.
253  *
254  * Return value: a new #GtkListStore
255  **/
256 GtkListStore *
257 gtk_list_store_new (gint n_columns,
258                                ...)
259 {
260   GtkListStore *retval;
261   va_list args;
262   gint i;
263
264   g_return_val_if_fail (n_columns > 0, NULL);
265
266   retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
267   gtk_list_store_set_n_columns (retval, n_columns);
268
269   va_start (args, n_columns);
270
271   for (i = 0; i < n_columns; i++)
272     {
273       GType type = va_arg (args, GType);
274       if (! _gtk_tree_data_list_check_type (type))
275         {
276           g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
277           g_object_unref (G_OBJECT (retval));
278           return NULL;
279         }
280
281       gtk_list_store_set_column_type (retval, i, type);
282     }
283
284   va_end (args);
285
286   return retval;
287 }
288
289
290 /**
291  * gtk_list_store_newv:
292  * @n_columns: number of columns in the list store
293  * @types: an array of #GType types for the columns, from first to last
294  *
295  * Non vararg creation function.  Used primarily by language bindings.
296  *
297  * Return value: a new #GtkListStore
298  **/
299 GtkListStore *
300 gtk_list_store_newv (gint   n_columns,
301                      GType *types)
302 {
303   GtkListStore *retval;
304   gint i;
305
306   g_return_val_if_fail (n_columns > 0, NULL);
307
308   retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
309   gtk_list_store_set_n_columns (retval, n_columns);
310
311   for (i = 0; i < n_columns; i++)
312     {
313       if (! _gtk_tree_data_list_check_type (types[i]))
314         {
315           g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (types[i]));
316           g_object_unref (G_OBJECT (retval));
317           return NULL;
318         }
319
320       gtk_list_store_set_column_type (retval, i, types[i]);
321     }
322
323   return retval;
324 }
325
326 static void
327 gtk_list_store_set_n_columns (GtkListStore *list_store,
328                               gint          n_columns)
329 {
330   GType *new_columns;
331
332   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
333   g_return_if_fail (n_columns > 0);
334
335   if (list_store->n_columns == n_columns)
336     return;
337
338   new_columns = g_new0 (GType, n_columns);
339   if (list_store->column_headers)
340     {
341       /* copy the old header orders over */
342       if (n_columns >= list_store->n_columns)
343         memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
344       else
345         memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
346
347       g_free (list_store->column_headers);
348     }
349
350   if (list_store->sort_list)
351     _gtk_tree_data_list_header_free (list_store->sort_list);
352
353   list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
354
355   list_store->column_headers = new_columns;
356   list_store->n_columns = n_columns;
357 }
358
359 static void
360 gtk_list_store_set_column_type (GtkListStore *list_store,
361                                 gint          column,
362                                 GType         type)
363 {
364   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
365   g_return_if_fail (column >=0 && column < list_store->n_columns);
366   if (!_gtk_tree_data_list_check_type (type))
367     {
368       g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
369       return;
370     }
371
372   list_store->column_headers[column] = type;
373 }
374
375 /* Fulfill the GtkTreeModel requirements */
376 static guint
377 gtk_list_store_get_flags (GtkTreeModel *tree_model)
378 {
379   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
380
381   return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
382 }
383
384 static gint
385 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
386 {
387   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
388
389   return GTK_LIST_STORE (tree_model)->n_columns;
390 }
391
392 static GType
393 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
394                                 gint          index)
395 {
396   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
397   g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
398                         index >= 0, G_TYPE_INVALID);
399
400   return GTK_LIST_STORE (tree_model)->column_headers[index];
401 }
402
403 static gboolean
404 gtk_list_store_get_iter (GtkTreeModel *tree_model,
405                          GtkTreeIter  *iter,
406                          GtkTreePath  *path)
407 {
408   GSList *list;
409   gint i;
410
411   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
412   g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
413
414   i = gtk_tree_path_get_indices (path)[0];
415
416   if (i >= GTK_LIST_STORE (tree_model)->length)
417     return FALSE;
418
419   list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
420
421   /* If this fails, list_store->length has gotten mangled. */
422   g_assert (list);
423
424   iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
425   iter->user_data = list;
426   return TRUE;
427 }
428
429 static GtkTreePath *
430 gtk_list_store_get_path (GtkTreeModel *tree_model,
431                          GtkTreeIter  *iter)
432 {
433   GtkTreePath *retval;
434   GSList *list;
435   gint i = 0;
436
437   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
438   g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
439
440   for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
441     {
442       if (list == G_SLIST (iter->user_data))
443         break;
444       i++;
445     }
446   if (list == NULL)
447     return NULL;
448
449   retval = gtk_tree_path_new ();
450   gtk_tree_path_append_index (retval, i);
451   return retval;
452 }
453
454 static void
455 gtk_list_store_get_value (GtkTreeModel *tree_model,
456                           GtkTreeIter  *iter,
457                           gint          column,
458                           GValue       *value)
459 {
460   GtkTreeDataList *list;
461   gint tmp_column = column;
462
463   g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
464   g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
465   g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
466
467   list = G_SLIST (iter->user_data)->data;
468
469   while (tmp_column-- > 0 && list)
470     list = list->next;
471
472   if (list == NULL)
473     g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
474   else
475     _gtk_tree_data_list_node_to_value (list,
476                                        GTK_LIST_STORE (tree_model)->column_headers[column],
477                                        value);
478 }
479
480 static gboolean
481 gtk_list_store_iter_next (GtkTreeModel  *tree_model,
482                           GtkTreeIter   *iter)
483 {
484   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
485   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
486
487   iter->user_data = G_SLIST (iter->user_data)->next;
488
489   return (iter->user_data != NULL);
490 }
491
492 static gboolean
493 gtk_list_store_iter_children (GtkTreeModel *tree_model,
494                               GtkTreeIter  *iter,
495                               GtkTreeIter  *parent)
496 {
497   /* this is a list, nodes have no children */
498   if (parent)
499     return FALSE;
500
501   /* but if parent == NULL we return the list itself as children of the
502    * "root"
503    */
504
505   if (GTK_LIST_STORE (tree_model)->root)
506     {
507       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
508       iter->user_data = GTK_LIST_STORE (tree_model)->root;
509       return TRUE;
510     }
511   else
512     return FALSE;
513 }
514
515 static gboolean
516 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
517                                GtkTreeIter  *iter)
518 {
519   return FALSE;
520 }
521
522 static gint
523 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
524                                 GtkTreeIter  *iter)
525 {
526   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
527   if (iter == NULL)
528     return GTK_LIST_STORE (tree_model)->length;
529
530   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
531   return 0;
532 }
533
534 static gboolean
535 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
536                                GtkTreeIter  *iter,
537                                GtkTreeIter  *parent,
538                                gint          n)
539 {
540   GSList *child;
541
542   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
543
544   if (parent)
545     return FALSE;
546
547   child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
548
549   if (child)
550     {
551       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
552       iter->user_data = child;
553       return TRUE;
554     }
555   else
556     return FALSE;
557 }
558
559 static gboolean
560 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
561                             GtkTreeIter  *iter,
562                             GtkTreeIter  *child)
563 {
564   return FALSE;
565 }
566
567 /* Public accessors */
568 /* This is a somewhat inelegant function that does a lot of list
569  * manipulations on it's own.
570  */
571
572 /**
573  * gtk_list_store_set_value:
574  * @store: a #GtkListStore
575  * @iter: iterator for the row you're modifying
576  * @column: column number to modify
577  * @value: new value for the cell
578  *
579  * Sets the data in the cell specified by @iter and @column.
580  * The type of @value must be convertible to the type of the
581  * column.
582  *
583  **/
584 void
585 gtk_list_store_set_value (GtkListStore *list_store,
586                           GtkTreeIter  *iter,
587                           gint          column,
588                           GValue       *value)
589 {
590   GtkTreeDataList *list;
591   GtkTreeDataList *prev;
592   GtkTreePath *path;
593   GValue real_value = {0, };
594   gboolean converted = FALSE;
595   gint orig_column = column;
596
597   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
598   g_return_if_fail (VALID_ITER (iter, list_store));
599   g_return_if_fail (column >= 0 && column < list_store->n_columns);
600   g_return_if_fail (G_IS_VALUE (value));
601
602   if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
603     {
604       if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
605              g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
606         {
607           g_warning ("%s: Unable to convert from %s to %s\n",
608                      G_STRLOC,
609                      g_type_name (G_VALUE_TYPE (value)),
610                      g_type_name (list_store->column_headers[column]));
611           return;
612         }
613       if (!g_value_transform (value, &real_value))
614         {
615           g_warning ("%s: Unable to make conversion from %s to %s\n",
616                      G_STRLOC,
617                      g_type_name (G_VALUE_TYPE (value)),
618                      g_type_name (list_store->column_headers[column]));
619           g_value_unset (&real_value);
620           return;
621         }
622       converted = TRUE;
623     }
624
625   prev = list = G_SLIST (iter->user_data)->data;
626
627   while (list != NULL)
628     {
629       if (column == 0)
630         {
631           path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
632           if (converted)
633             _gtk_tree_data_list_value_to_node (list, &real_value);
634           else
635             _gtk_tree_data_list_value_to_node (list, value);
636           gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
637           gtk_tree_path_free (path);
638           if (converted)
639             g_value_unset (&real_value);
640           return;
641         }
642
643       column--;
644       prev = list;
645       list = list->next;
646     }
647
648   if (G_SLIST (iter->user_data)->data == NULL)
649     {
650       G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
651       list->next = NULL;
652     }
653   else
654     {
655       list = prev->next = _gtk_tree_data_list_alloc ();
656       list->next = NULL;
657     }
658
659   while (column != 0)
660     {
661       list->next = _gtk_tree_data_list_alloc ();
662       list = list->next;
663       list->next = NULL;
664       column --;
665     }
666
667   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
668   if (converted)
669     _gtk_tree_data_list_value_to_node (list, &real_value);
670   else
671     _gtk_tree_data_list_value_to_node (list, value);
672   gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
673   gtk_tree_path_free (path);
674   if (converted)
675     g_value_unset (&real_value);
676
677   if (GTK_LIST_STORE_IS_SORTED (list_store))
678     gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
679 }
680
681 /**
682  * gtk_list_store_set_valist:
683  * @list_store: a #GtkListStore
684  * @iter: row to set data for
685  * @var_args: va_list of column/value pairs
686  *
687  * See gtk_list_store_set(); this version takes a va_list for
688  * use by language bindings.
689  *
690  **/
691 void
692 gtk_list_store_set_valist (GtkListStore *list_store,
693                            GtkTreeIter  *iter,
694                            va_list       var_args)
695 {
696   gint column;
697
698   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
699   g_return_if_fail (VALID_ITER (iter, list_store));
700
701   column = va_arg (var_args, gint);
702
703   while (column != -1)
704     {
705       GValue value = { 0, };
706       gchar *error = NULL;
707
708       if (column >= list_store->n_columns)
709         {
710           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
711           break;
712         }
713       g_value_init (&value, list_store->column_headers[column]);
714
715       G_VALUE_COLLECT (&value, var_args, 0, &error);
716       if (error)
717         {
718           g_warning ("%s: %s", G_STRLOC, error);
719           g_free (error);
720
721           /* we purposely leak the value here, it might not be
722            * in a sane state if an error condition occoured
723            */
724           break;
725         }
726
727       /* FIXME: instead of calling this n times, refactor with above */
728       gtk_list_store_set_value (list_store,
729                                 iter,
730                                 column,
731                                 &value);
732
733       g_value_unset (&value);
734
735       column = va_arg (var_args, gint);
736     }
737 }
738
739 /**
740  * gtk_list_store_set:
741  * @list_store: a #GtkListStore
742  * @iter: row iterator
743  * @Varargs: pairs of column number and value, terminated with -1
744  *
745  * Sets the value of one or more cells in the row referenced by @iter.
746  * The variable argument list should contain integer column numbers,
747  * each column number followed by the value to be set.
748  * The list is terminated by a -1. For example, to set column 0 with type
749  * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
750  * 0, "Foo", -1).
751  **/
752 void
753 gtk_list_store_set (GtkListStore *list_store,
754                     GtkTreeIter  *iter,
755                     ...)
756 {
757   va_list var_args;
758
759   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
760   g_return_if_fail (iter != NULL);
761   g_return_if_fail (iter->stamp == list_store->stamp);
762
763   va_start (var_args, iter);
764   gtk_list_store_set_valist (list_store, iter, var_args);
765   va_end (var_args);
766 }
767
768 static GSList*
769 remove_link_saving_prev (GSList  *list,
770                          GSList  *link,
771                          GSList **prevp)
772 {
773   GSList *tmp;
774   GSList *prev;
775
776   prev = NULL;
777   tmp = list;
778
779   while (tmp)
780     {
781       if (tmp == link)
782         {
783           if (prev)
784             prev->next = link->next;
785
786           if (list == link)
787             list = list->next;
788
789           link->next = NULL;
790           break;
791         }
792
793       prev = tmp;
794       tmp = tmp->next;
795     }
796
797   *prevp = prev;
798
799   return list;
800 }
801
802 static void
803 gtk_list_store_remove_silently (GtkListStore *list_store,
804                                 GtkTreeIter  *iter,
805                                 GtkTreePath  *path)
806 {
807   if (G_SLIST (iter->user_data)->data)
808     {
809       _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
810                                 list_store->column_headers);
811       G_SLIST (iter->user_data)->data = NULL;
812     }
813
814   {
815     GSList *prev = NULL;
816
817     list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
818                                                 G_SLIST (iter->user_data),
819                                                 &prev);
820
821     list_store->length -= 1;
822
823     if (iter->user_data == list_store->tail)
824       list_store->tail = prev;
825   }
826
827   list_store->stamp ++;
828 }
829
830 /**
831  * gtk_list_store_remove:
832  * @store: a #GtkListStore
833  * @iter: a row in @list_store
834  *
835  * Removes the given row from the list store, emitting the
836  * "deleted" signal on #GtkTreeModel.
837  *
838  **/
839 void
840 gtk_list_store_remove (GtkListStore *list_store,
841                        GtkTreeIter  *iter)
842 {
843   GtkTreePath *path;
844
845   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
846   g_return_if_fail (VALID_ITER (iter, list_store));
847
848   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
849
850   validate_list_store (list_store);
851
852   gtk_list_store_remove_silently (list_store, iter, path);
853
854   validate_list_store (list_store);
855
856   list_store->stamp ++;
857   gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
858   gtk_tree_path_free (path);
859 }
860
861 static void
862 insert_after (GtkListStore *list_store,
863               GSList       *sibling,
864               GSList       *new_list)
865 {
866   g_return_if_fail (sibling != NULL);
867   g_return_if_fail (new_list != NULL);
868
869   /* insert new node after list */
870   new_list->next = sibling->next;
871   sibling->next = new_list;
872
873   /* if list was the tail, the new node is the new tail */
874   if (sibling == ((GSList *) list_store->tail))
875     list_store->tail = new_list;
876
877   list_store->length += 1;
878 }
879
880 /**
881  * gtk_list_store_insert:
882  * @store: a #GtkListStore
883  * @iter: iterator to initialize with the new row
884  * @position: position to insert the new row
885  *
886  * Creates a new row at @position, initializing @iter to point to the
887  * new row, and emitting the "inserted" signal from the #GtkTreeModel
888  * interface.
889  *
890  **/
891 void
892 gtk_list_store_insert (GtkListStore *list_store,
893                        GtkTreeIter  *iter,
894                        gint          position)
895 {
896   GSList *list;
897   GtkTreePath *path;
898   GSList *new_list;
899
900   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
901   g_return_if_fail (iter != NULL);
902   g_return_if_fail (position >= 0);
903
904   if (position == 0 ||
905       GTK_LIST_STORE_IS_SORTED (list_store))
906     {
907       gtk_list_store_prepend (list_store, iter);
908       return;
909     }
910
911   new_list = g_slist_alloc ();
912
913   list = g_slist_nth (G_SLIST (list_store->root), position - 1);
914
915   if (list == NULL)
916     {
917       g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
918       return;
919     }
920
921   insert_after (list_store, list, new_list);
922
923   iter->stamp = list_store->stamp;
924   iter->user_data = new_list;
925
926   validate_list_store (list_store);
927
928   path = gtk_tree_path_new ();
929   gtk_tree_path_append_index (path, position);
930   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
931   gtk_tree_path_free (path);
932 }
933
934 /**
935  * gtk_list_store_insert_before:
936  * @store: a #GtkListStore
937  * @iter: iterator to initialize with the new row
938  * @sibling: an existing row
939  *
940  * Inserts a new row before @sibling, initializing @iter to point to
941  * the new row, and emitting the "inserted" signal from the
942  * #GtkTreeModel interface.
943  *
944  **/
945 void
946 gtk_list_store_insert_before (GtkListStore *list_store,
947                               GtkTreeIter  *iter,
948                               GtkTreeIter  *sibling)
949 {
950   GtkTreePath *path;
951   GSList *list, *prev, *new_list;
952   gint i = 0;
953
954   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
955   g_return_if_fail (iter != NULL);
956   if (sibling)
957     g_return_if_fail (VALID_ITER (sibling, list_store));
958
959
960   if (GTK_LIST_STORE_IS_SORTED (list_store))
961     {
962       gtk_list_store_prepend (list_store, iter);
963       return;
964     }
965
966   if (sibling == NULL)
967     {
968       gtk_list_store_append (list_store, iter);
969       return;
970     }
971
972   new_list = g_slist_alloc ();
973
974   prev = NULL;
975   list = list_store->root;
976   while (list && list != sibling->user_data)
977     {
978       prev = list;
979       list = list->next;
980       i++;
981     }
982
983   if (list != sibling->user_data)
984     {
985       g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
986       return;
987     }
988
989   /* if there are no nodes, we become the list tail, otherwise we
990    * are inserting before any existing nodes so we can't change
991    * the tail
992    */
993
994   if (list_store->root == NULL)
995     list_store->tail = new_list;
996
997   if (prev)
998     {
999       new_list->next = prev->next;
1000       prev->next = new_list;
1001     }
1002   else
1003     {
1004       new_list->next = list_store->root;
1005       list_store->root = new_list;
1006     }
1007
1008   iter->stamp = list_store->stamp;
1009   iter->user_data = new_list;
1010
1011   list_store->length += 1;
1012
1013   validate_list_store (list_store);
1014
1015   path = gtk_tree_path_new ();
1016   gtk_tree_path_append_index (path, i);
1017   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1018   gtk_tree_path_free (path);
1019 }
1020
1021 /**
1022  * gtk_list_store_insert_after:
1023  * @store: a #GtkListStore
1024  * @iter: iterator to initialize with the new row
1025  * @sibling: an existing row
1026  *
1027  * Inserts a new row after @sibling, initializing @iter to point to
1028  * the new row, and emitting the "inserted" signal from the
1029  * #GtkTreeModel interface.
1030  *
1031  **/
1032 void
1033 gtk_list_store_insert_after (GtkListStore *list_store,
1034                              GtkTreeIter  *iter,
1035                              GtkTreeIter  *sibling)
1036 {
1037   GtkTreePath *path;
1038   GSList *list, *new_list;
1039   gint i = 0;
1040
1041   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1042   g_return_if_fail (iter != NULL);
1043   if (sibling)
1044     g_return_if_fail (VALID_ITER (sibling, list_store));
1045
1046   if (sibling == NULL ||
1047       GTK_LIST_STORE_IS_SORTED (list_store))
1048     {
1049       gtk_list_store_prepend (list_store, iter);
1050       return;
1051     }
1052
1053   for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1054     i++;
1055
1056   g_return_if_fail (list == sibling->user_data);
1057
1058   new_list = g_slist_alloc ();
1059
1060   insert_after (list_store, list, new_list);
1061
1062   iter->stamp = list_store->stamp;
1063   iter->user_data = new_list;
1064
1065   validate_list_store (list_store);
1066
1067   path = gtk_tree_path_new ();
1068   gtk_tree_path_append_index (path, i);
1069   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1070   gtk_tree_path_free (path);
1071 }
1072
1073 /**
1074  * gtk_list_store_prepend:
1075  * @store: a #GtkListStore
1076  * @iter: iterator to initialize with new row
1077  *
1078  * Prepends a row to @store, initializing @iter to point to the
1079  * new row, and emitting the "inserted" signal on the #GtkTreeModel
1080  * interface for the @store.
1081  *
1082  **/
1083 void
1084 gtk_list_store_prepend (GtkListStore *list_store,
1085                         GtkTreeIter  *iter)
1086 {
1087   GtkTreePath *path;
1088
1089   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1090   g_return_if_fail (iter != NULL);
1091
1092   iter->stamp = list_store->stamp;
1093   iter->user_data = g_slist_alloc ();
1094
1095   if (list_store->root == NULL)
1096     list_store->tail = iter->user_data;
1097
1098   G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1099   list_store->root = iter->user_data;
1100
1101   list_store->length += 1;
1102
1103   validate_list_store (list_store);
1104
1105   path = gtk_tree_path_new ();
1106   gtk_tree_path_append_index (path, 0);
1107   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1108   gtk_tree_path_free (path);
1109 }
1110
1111 /**
1112  * gtk_list_store_append:
1113  * @store: a #GtkListStore
1114  * @iter: iterator to initialize with the new row
1115  *
1116  * Appends a row to @store, initializing @iter to point to the
1117  * new row, and emitting the "inserted" signal on the #GtkTreeModel
1118  * interface for the @store.
1119  *
1120  **/
1121 void
1122 gtk_list_store_append (GtkListStore *list_store,
1123                        GtkTreeIter  *iter)
1124 {
1125   GtkTreePath *path;
1126
1127   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1128   g_return_if_fail (iter != NULL);
1129
1130   if (GTK_LIST_STORE_IS_SORTED (list_store))
1131     {
1132       gtk_list_store_prepend (list_store, iter);
1133       return;
1134     }
1135
1136   iter->stamp = list_store->stamp;
1137   iter->user_data = g_slist_alloc ();
1138
1139   if (list_store->tail)
1140     ((GSList *)list_store->tail)->next = iter->user_data;
1141   else
1142     list_store->root = iter->user_data;
1143
1144   list_store->tail = iter->user_data;
1145
1146   list_store->length += 1;
1147
1148   validate_list_store (list_store);
1149
1150   path = gtk_tree_path_new ();
1151   gtk_tree_path_append_index (path, list_store->length - 1);
1152   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1153   gtk_tree_path_free (path);
1154 }
1155
1156 void
1157 gtk_list_store_clear (GtkListStore *list_store)
1158 {
1159   GtkTreeIter iter;
1160   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1161
1162   while (list_store->root)
1163     {
1164       iter.stamp = list_store->stamp;
1165       iter.user_data = list_store->root;
1166       gtk_list_store_remove (list_store, &iter);
1167     }
1168 }
1169
1170
1171 static gboolean
1172 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1173                                  GtkTreePath       *path)
1174 {
1175   GtkTreeIter iter;
1176   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1177
1178   if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1179                                &iter,
1180                                path))
1181     {
1182       gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1183       return TRUE;
1184     }
1185   return FALSE;
1186 }
1187
1188 static gboolean
1189 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1190                               GtkTreePath       *path,
1191                               GtkSelectionData  *selection_data)
1192 {
1193   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1194
1195   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1196    * target, because the default handler does it for us, but
1197    * we do anyway for the convenience of someone maybe overriding the
1198    * default handler.
1199    */
1200
1201   if (gtk_selection_data_set_tree_row (selection_data,
1202                                        GTK_TREE_MODEL (drag_source),
1203                                        path))
1204     {
1205       return TRUE;
1206     }
1207   else
1208     {
1209       /* FIXME handle text targets at least. */
1210     }
1211
1212   return FALSE;
1213 }
1214
1215 static gboolean
1216 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1217                                    GtkTreePath       *dest,
1218                                    GtkSelectionData  *selection_data)
1219 {
1220   GtkTreeModel *tree_model;
1221   GtkListStore *list_store;
1222   GtkTreeModel *src_model = NULL;
1223   GtkTreePath *src_path = NULL;
1224   gboolean retval = FALSE;
1225
1226   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1227
1228   tree_model = GTK_TREE_MODEL (drag_dest);
1229   list_store = GTK_LIST_STORE (drag_dest);
1230
1231   if (gtk_selection_data_get_tree_row (selection_data,
1232                                        &src_model,
1233                                        &src_path) &&
1234       src_model == tree_model)
1235     {
1236       /* Copy the given row to a new position */
1237       GtkTreeIter src_iter;
1238       GtkTreeIter dest_iter;
1239       GtkTreePath *prev;
1240
1241       if (!gtk_tree_model_get_iter (src_model,
1242                                     &src_iter,
1243                                     src_path))
1244         {
1245           goto out;
1246         }
1247
1248       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1249       prev = gtk_tree_path_copy (dest);
1250
1251       if (!gtk_tree_path_prev (prev))
1252         {
1253           /* dest was the first spot in the list; which means we are supposed
1254            * to prepend.
1255            */
1256           gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1257                                   &dest_iter);
1258
1259           retval = TRUE;
1260         }
1261       else
1262         {
1263           if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1264                                        &dest_iter,
1265                                        prev))
1266             {
1267               GtkTreeIter tmp_iter = dest_iter;
1268               gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1269                                            &dest_iter,
1270                                            &tmp_iter);
1271               retval = TRUE;
1272             }
1273         }
1274
1275       gtk_tree_path_free (prev);
1276
1277       /* If we succeeded in creating dest_iter, copy data from src
1278        */
1279       if (retval)
1280         {
1281           GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1282           GtkTreeDataList *copy_head = NULL;
1283           GtkTreeDataList *copy_prev = NULL;
1284           GtkTreeDataList *copy_iter = NULL;
1285           GtkTreePath *path;
1286           gint col;
1287
1288           col = 0;
1289           while (dl)
1290             {
1291               copy_iter = _gtk_tree_data_list_node_copy (dl,
1292                                                          list_store->column_headers[col]);
1293
1294               if (copy_head == NULL)
1295                 copy_head = copy_iter;
1296
1297               if (copy_prev)
1298                 copy_prev->next = copy_iter;
1299
1300               copy_prev = copy_iter;
1301
1302               dl = dl->next;
1303               ++col;
1304             }
1305
1306           dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1307           G_SLIST (dest_iter.user_data)->data = copy_head;
1308
1309           path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1310           gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1311           gtk_tree_path_free (path);
1312         }
1313     }
1314   else
1315     {
1316       /* FIXME maybe add some data targets eventually, or handle text
1317        * targets in the simple case.
1318        */
1319     }
1320
1321  out:
1322
1323   if (src_path)
1324     gtk_tree_path_free (src_path);
1325
1326   return retval;
1327 }
1328
1329 static gboolean
1330 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1331                                   GtkTreeModel    *src_model,
1332                                   GtkTreePath     *src_path,
1333                                   GtkTreePath     *dest_path)
1334 {
1335   gint *indices;
1336
1337   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1338
1339   if (src_model != GTK_TREE_MODEL (drag_dest))
1340     return FALSE;
1341
1342   if (gtk_tree_path_get_depth (dest_path) != 1)
1343     return FALSE;
1344
1345   /* can drop before any existing node, or before one past any existing. */
1346
1347   indices = gtk_tree_path_get_indices (dest_path);
1348
1349   if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1350     return TRUE;
1351   else
1352     return FALSE;
1353 }
1354
1355
1356 /* Sorting */
1357 typedef struct _SortTuple
1358 {
1359   gint offset;
1360   GSList *el;
1361 } SortTuple;
1362
1363 static gint
1364 gtk_list_store_compare_func (gconstpointer a,
1365                              gconstpointer b,
1366                              gpointer      user_data)
1367 {
1368   GtkListStore *list_store = user_data;
1369   GtkTreeDataSortHeader *header = NULL;
1370   GSList *el_a; /* Los Angeles? */
1371   GSList *el_b;
1372   GtkTreeIter iter_a;
1373   GtkTreeIter iter_b;
1374   gint retval;
1375
1376   header = _gtk_tree_data_list_get_header (list_store->sort_list,
1377                                            list_store->sort_column_id);
1378
1379   g_return_val_if_fail (header != NULL, 0);
1380   g_return_val_if_fail (header->func != NULL, 0);
1381
1382   el_a = ((SortTuple *) a)->el;
1383   el_b = ((SortTuple *) b)->el;
1384
1385   iter_a.stamp = list_store->stamp;
1386   iter_a.user_data = el_a;
1387   iter_b.stamp = list_store->stamp;
1388   iter_b.user_data = el_b;
1389
1390   retval = (* header->func) (GTK_TREE_MODEL (list_store),
1391                              &iter_a, &iter_b,
1392                              header->data);
1393
1394   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1395     {
1396       if (retval > 0)
1397         retval = -1;
1398       else if (retval < 0)
1399         retval = 1;
1400     }
1401   return retval;
1402 }
1403
1404 static void
1405 gtk_list_store_sort (GtkListStore *list_store)
1406 {
1407   GtkTreeDataSortHeader *header = NULL;
1408   GtkTreeIter iter;
1409   GArray *sort_array;
1410   gint i;
1411   gint *new_order;
1412   GSList *list;
1413   GtkTreePath *path;
1414
1415   if (list_store->length <= 1)
1416     return;
1417
1418   g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1419
1420   header = _gtk_tree_data_list_get_header (list_store->sort_list, list_store->sort_column_id);
1421
1422   /* We want to make sure that we have a function */
1423   g_return_if_fail (header != NULL);
1424   g_return_if_fail (header->func != NULL);
1425
1426   list = G_SLIST (list_store->root);
1427
1428   sort_array = g_array_sized_new (FALSE, FALSE,
1429                                   sizeof (SortTuple),
1430                                   list_store->length);
1431
1432   for (i = 0; i < list_store->length; i++)
1433     {
1434       SortTuple tuple;
1435
1436       /* If this fails, we are in an inconsistent state.  Bad */
1437       g_return_if_fail (list != NULL);
1438
1439       tuple.offset = i;
1440       tuple.el = list;
1441       g_array_append_val (sort_array, tuple);
1442
1443       list = list->next;
1444     }
1445
1446   g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1447
1448   for (i = 0; i < list_store->length - 1; i++)
1449       g_array_index (sort_array, SortTuple, i).el->next =
1450         g_array_index (sort_array, SortTuple, i + 1).el;
1451   g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1452   list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1453
1454   /* Let the world know about our new order */
1455   new_order = g_new (gint, list_store->length);
1456   for (i = 0; i < list_store->length; i++)
1457     new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1458   path = gtk_tree_path_new ();
1459   iter.stamp = list_store->stamp;
1460   iter.user_data = NULL;
1461   gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1462                             path, &iter, new_order);
1463   gtk_tree_path_free (path);
1464   g_free (new_order);
1465   g_array_free (sort_array, TRUE);
1466 }
1467
1468 static void
1469 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1470                                   GtkTreeIter  *iter,
1471                                   gint          column)
1472
1473 {
1474   GtkTreeDataSortHeader *header;
1475   GSList *prev = NULL;
1476   GSList *next = NULL;
1477   GSList *list = G_SLIST (list_store->root);
1478   GtkTreePath *tmp_path;
1479   GtkTreeIter tmp_iter;
1480   gint cmp_a = 0;
1481   gint cmp_b = 0;
1482   gint i;
1483   gint old_location;
1484   gint new_location;
1485   gint *new_order;
1486
1487   if (list_store->length < 2)
1488     return;
1489
1490   tmp_iter.stamp = list_store->stamp;
1491   header = _gtk_tree_data_list_get_header (list_store->sort_list,
1492                                            list_store->sort_column_id);
1493   g_return_if_fail (header != NULL);
1494   g_return_if_fail (header->func != NULL);
1495
1496   /* If it's the built in function, we don't sort. */
1497   if (header->func == gtk_tree_data_list_compare_func &&
1498       list_store->sort_column_id != column)
1499     return;
1500
1501   old_location = 0;
1502   /* First we find the iter, its prev, and its next */
1503   while (list)
1504     {
1505       if (list == G_SLIST (iter->user_data))
1506         break;
1507       prev = list;
1508       list = list->next;
1509       old_location++;
1510     }
1511   g_assert (list != NULL);
1512
1513   next = list->next;
1514
1515   /* Check the common case, where we don't need to sort it moved. */
1516   if (prev != NULL)
1517     {
1518       tmp_iter.user_data = prev;
1519       cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1520                                 &tmp_iter, iter,
1521                                 header->data);
1522     }
1523
1524   if (next != NULL)
1525     {
1526       tmp_iter.user_data = next;
1527       cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1528                                 iter, &tmp_iter,
1529                                 header->data);
1530     }
1531
1532
1533   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1534     {
1535       if (cmp_a < 0)
1536         cmp_a = 1;
1537       else if (cmp_a > 0)
1538         cmp_a = -1;
1539
1540       if (cmp_b < 0)
1541         cmp_b = 1;
1542       else if (cmp_b > 0)
1543         cmp_b = -1;
1544     }
1545
1546   if (prev == NULL && cmp_b <= 0)
1547     return;
1548   else if (next == NULL && cmp_a <= 0)
1549     return;
1550   else if (prev != NULL && next != NULL &&
1551            cmp_a <= 0 && cmp_b <= 0)
1552     return;
1553
1554   /* We actually need to sort it */
1555   /* First, remove the old link. */
1556
1557   if (prev == NULL)
1558     list_store->root = next;
1559   else
1560     prev->next = next;
1561   if (next == NULL)
1562     list_store->tail = prev;
1563   list->next = NULL;
1564   
1565   /* FIXME: as an optimization, we can potentially start at next */
1566   prev = NULL;
1567   list = G_SLIST (list_store->root);
1568   new_location = 0;
1569   tmp_iter.user_data = list;
1570   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1571     cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1572                               &tmp_iter, iter, header->data);
1573   else
1574     cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1575                               iter, &tmp_iter, header->data);
1576
1577   while ((list->next) && (cmp_a > 0))
1578     {
1579       prev = list;
1580       list = list->next;
1581       new_location++;
1582       tmp_iter.user_data = list;
1583       if (list_store->order == GTK_TREE_SORT_DESCENDING)
1584         cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1585                                   &tmp_iter, iter, header->data);
1586       else
1587         cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1588                                   iter, &tmp_iter, header->data);
1589     }
1590
1591   if ((!list->next) && (cmp_a > 0))
1592     {
1593       list->next = G_SLIST (iter->user_data);
1594       list_store->tail = list->next;
1595     }
1596   else if (prev)
1597     {
1598       prev->next = G_SLIST (iter->user_data);
1599       G_SLIST (iter->user_data)->next = list;
1600     }
1601   else
1602     {
1603       G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1604       list_store->root = G_SLIST (iter->user_data);
1605     }
1606
1607   /* Emit the reordered signal. */
1608   new_order = g_new (int, list_store->length);
1609   if (old_location < new_location)
1610     for (i = 0; i < list_store->length; i++)
1611       {
1612         if (i < old_location ||
1613             i > new_location)
1614           new_order[i] = i;
1615         else if (i >= old_location &&
1616                  i < new_location)
1617           new_order[i] = i + 1;
1618         else if (i == new_location)
1619           new_order[i] = old_location;
1620       }
1621   else
1622     for (i = 0; i < list_store->length; i++)
1623       {
1624         if (i < new_location ||
1625             i > old_location)
1626           new_order[i] = i;
1627         else if (i > new_location &&
1628                  i <= old_location)
1629           new_order[i] = i - 1;
1630         else if (i == new_location)
1631           new_order[i] = old_location;
1632       }
1633
1634   tmp_path = gtk_tree_path_new ();
1635   tmp_iter.user_data = NULL;
1636
1637   gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1638                             tmp_path, &tmp_iter,
1639                             new_order);
1640
1641   gtk_tree_path_free (tmp_path);
1642   g_free (new_order);
1643 }
1644
1645 static gboolean
1646 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1647                                    gint             *sort_column_id,
1648                                    GtkTreeSortOrder *order)
1649 {
1650   GtkListStore *list_store = (GtkListStore *) sortable;
1651
1652   g_return_val_if_fail (sortable != NULL, FALSE);
1653   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1654
1655   if (list_store->sort_column_id == -1)
1656     return FALSE;
1657
1658   if (sort_column_id)
1659     * sort_column_id = list_store->sort_column_id;
1660   if (order)
1661     * order = list_store->order;
1662   return TRUE;
1663 }
1664
1665 static void
1666 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1667                                    gint              sort_column_id,
1668                                    GtkTreeSortOrder  order)
1669 {
1670   GtkListStore *list_store = (GtkListStore *) sortable;
1671   GList *list;
1672
1673   g_return_if_fail (sortable != NULL);
1674   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1675
1676   for (list = list_store->sort_list; list; list = list->next)
1677     {
1678       GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1679       if (header->sort_column_id == sort_column_id)
1680         break;
1681     }
1682   g_return_if_fail (list != NULL);
1683
1684   if ((list_store->sort_column_id == sort_column_id) &&
1685       (list_store->order == order))
1686     return;
1687
1688   list_store->sort_column_id = sort_column_id;
1689   list_store->order = order;
1690
1691   if (list_store->sort_column_id >= 0)
1692     gtk_list_store_sort (list_store);
1693
1694   gtk_tree_sortable_sort_column_changed (sortable);
1695 }
1696
1697 static void
1698 gtk_list_store_set_sort_func (GtkTreeSortable        *sortable,
1699                               gint                    sort_column_id,
1700                               GtkTreeIterCompareFunc  func,
1701                               gpointer                data,
1702                               GtkDestroyNotify        destroy)
1703 {
1704   GtkListStore *list_store = (GtkListStore *) sortable;
1705   GtkTreeDataSortHeader *header = NULL;
1706   GList *list;
1707
1708   g_return_if_fail (sortable != NULL);
1709   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1710   g_return_if_fail (func != NULL);
1711
1712   for (list = list_store->sort_list; list; list = list->next)
1713     {
1714       header = (GtkTreeDataSortHeader*) list->data;
1715       if (header->sort_column_id == sort_column_id)
1716         break;
1717     }
1718
1719   if (header == NULL)
1720     {
1721       header = g_new0 (GtkTreeDataSortHeader, 1);
1722       header->sort_column_id = sort_column_id;
1723       list_store->sort_list = g_list_append (list_store->sort_list, header);
1724     }
1725
1726   if (header->destroy)
1727     (* header->destroy) (header->data);
1728
1729   header->func = func;
1730   header->data = data;
1731   header->destroy = destroy;
1732 }