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