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