]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
Evil function to deal with very large (TM) amounts of text. May be moved
[~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
83 /* sortable */
84 static void     gtk_list_store_sort                    (GtkListStore           *list_store);
85 static void     gtk_list_store_sort_iter_changed       (GtkListStore           *list_store,
86                                                         GtkTreeIter            *iter,
87                                                         gint                    column);
88 static gboolean gtk_list_store_get_sort_column_id      (GtkTreeSortable        *sortable,
89                                                         gint                   *sort_column_id,
90                                                         GtkTreeSortOrder       *order);
91 static void     gtk_list_store_set_sort_column_id      (GtkTreeSortable        *sortable,
92                                                         gint                    sort_column_id,
93                                                         GtkTreeSortOrder        order);
94 static void     gtk_list_store_sort_column_id_set_func (GtkTreeSortable        *sortable,
95                                                         gint                    sort_column_id,
96                                                         GtkTreeIterCompareFunc  func,
97                                                         gpointer                data,
98                                                         GtkDestroyNotify        destroy);
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   iter->user_data = G_SLIST (iter->user_data)->next;
484
485   return (iter->user_data != NULL);
486 }
487
488 static gboolean
489 gtk_list_store_iter_children (GtkTreeModel *tree_model,
490                               GtkTreeIter  *iter,
491                               GtkTreeIter  *parent)
492 {
493   /* this is a list, nodes have no children */
494   if (parent)
495     return FALSE;
496
497   /* but if parent == NULL we return the list itself as children of the
498    * "root"
499    */
500
501   if (GTK_LIST_STORE (tree_model)->root)
502     {
503       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
504       iter->user_data = GTK_LIST_STORE (tree_model)->root;
505       return TRUE;
506     }
507   else
508     return FALSE;
509 }
510
511 static gboolean
512 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
513                                GtkTreeIter  *iter)
514 {
515   return FALSE;
516 }
517
518 static gint
519 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
520                                 GtkTreeIter  *iter)
521 {
522   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
523
524   if (iter->user_data == NULL)
525     return GTK_LIST_STORE (tree_model)->length;
526   else
527     return 0;
528 }
529
530 static gboolean
531 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
532                                GtkTreeIter  *iter,
533                                GtkTreeIter  *parent,
534                                gint          n)
535 {
536   GSList *child;
537
538   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
539
540   if (parent)
541     return FALSE;
542
543   child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
544
545   if (child)
546     {
547       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
548       iter->user_data = child;
549       return TRUE;
550     }
551   else
552     return FALSE;
553 }
554
555 static gboolean
556 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
557                             GtkTreeIter  *iter,
558                             GtkTreeIter  *child)
559 {
560   return FALSE;
561 }
562
563 /* Public accessors */
564 /* This is a somewhat inelegant function that does a lot of list
565  * manipulations on it's own.
566  */
567
568 /**
569  * gtk_list_store_set_value:
570  * @store: a #GtkListStore
571  * @iter: iterator for the row you're modifying
572  * @column: column number to modify
573  * @value: new value for the cell
574  *
575  * Sets the data in the cell specified by @iter and @column.
576  * The type of @value must be convertible to the type of the
577  * column.
578  *
579  **/
580 void
581 gtk_list_store_set_value (GtkListStore *list_store,
582                           GtkTreeIter  *iter,
583                           gint          column,
584                           GValue       *value)
585 {
586   GtkTreeDataList *list;
587   GtkTreeDataList *prev;
588   GtkTreePath *path;
589   GValue real_value = {0, };
590   gboolean converted = FALSE;
591
592   g_return_if_fail (list_store != NULL);
593   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
594   g_return_if_fail (iter != NULL);
595   g_return_if_fail (column >= 0 && column < list_store->n_columns);
596   g_return_if_fail (G_IS_VALUE (value));
597
598   if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
599     {
600       if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
601              g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
602         {
603           g_warning ("%s: Unable to convert from %s to %s\n",
604                      G_STRLOC,
605                      g_type_name (G_VALUE_TYPE (value)),
606                      g_type_name (list_store->column_headers[column]));
607           return;
608         }
609       if (!g_value_transform (value, &real_value))
610         {
611           g_warning ("%s: Unable to make conversion from %s to %s\n",
612                      G_STRLOC,
613                      g_type_name (G_VALUE_TYPE (value)),
614                      g_type_name (list_store->column_headers[column]));
615           g_value_unset (&real_value);
616           return;
617         }
618       converted = TRUE;
619     }
620
621   prev = list = G_SLIST (iter->user_data)->data;
622
623   while (list != NULL)
624     {
625       if (column == 0)
626         {
627           path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
628           if (converted)
629             _gtk_tree_data_list_value_to_node (list, &real_value);
630           else
631             _gtk_tree_data_list_value_to_node (list, value);
632           gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
633           gtk_tree_path_free (path);
634           if (converted)
635             g_value_unset (&real_value);
636           return;
637         }
638
639       column--;
640       prev = list;
641       list = list->next;
642     }
643
644   if (G_SLIST (iter->user_data)->data == NULL)
645     {
646       G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
647       list->next = NULL;
648     }
649   else
650     {
651       list = prev->next = _gtk_tree_data_list_alloc ();
652       list->next = NULL;
653     }
654
655   while (column != 0)
656     {
657       list->next = _gtk_tree_data_list_alloc ();
658       list = list->next;
659       list->next = NULL;
660       column --;
661     }
662
663   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
664   if (converted)
665     _gtk_tree_data_list_value_to_node (list, &real_value);
666   else
667     _gtk_tree_data_list_value_to_node (list, value);
668   gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
669   gtk_tree_path_free (path);
670   if (converted)
671     g_value_unset (&real_value);
672
673   if (GTK_LIST_STORE_IS_SORTED (list_store))
674     gtk_list_store_sort_iter_changed (list_store, iter, column);
675 }
676
677 /**
678  * gtk_list_store_set_valist:
679  * @list_store: a #GtkListStore
680  * @iter: row to set data for
681  * @var_args: va_list of column/value pairs
682  *
683  * See gtk_list_store_set(); this version takes a va_list for
684  * use by language bindings.
685  *
686  **/
687 void
688 gtk_list_store_set_valist (GtkListStore *list_store,
689                            GtkTreeIter  *iter,
690                            va_list       var_args)
691 {
692   gint column;
693
694   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
695
696   column = va_arg (var_args, gint);
697
698   while (column != -1)
699     {
700       GValue value = { 0, };
701       gchar *error = NULL;
702
703       if (column >= list_store->n_columns)
704         {
705           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
706           break;
707         }
708       g_value_init (&value, list_store->column_headers[column]);
709
710       G_VALUE_COLLECT (&value, var_args, 0, &error);
711       if (error)
712         {
713           g_warning ("%s: %s", G_STRLOC, error);
714           g_free (error);
715
716           /* we purposely leak the value here, it might not be
717            * in a sane state if an error condition occoured
718            */
719           break;
720         }
721
722       /* FIXME: instead of calling this n times, refactor with above */
723       gtk_list_store_set_value (list_store,
724                                 iter,
725                                 column,
726                                 &value);
727
728       g_value_unset (&value);
729
730       column = va_arg (var_args, gint);
731     }
732 }
733
734 /**
735  * gtk_list_store_set:
736  * @list_store: a #GtkListStore
737  * @iter: row iterator
738  * @Varargs: pairs of column number and value, terminated with -1
739  *
740  * Sets the value of one or more cells in the row referenced by @iter.
741  * The variable argument list should contain integer column numbers,
742  * each column number followed by the value to be set.
743  * The list is terminated by a -1. For example, to set column 0 with type
744  * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
745  * 0, "Foo", -1).
746  **/
747 void
748 gtk_list_store_set (GtkListStore *list_store,
749                     GtkTreeIter  *iter,
750                     ...)
751 {
752   va_list var_args;
753
754   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
755
756   va_start (var_args, iter);
757   gtk_list_store_set_valist (list_store, iter, var_args);
758   va_end (var_args);
759 }
760
761 static GSList*
762 remove_link_saving_prev (GSList  *list,
763                          GSList  *link,
764                          GSList **prevp)
765 {
766   GSList *tmp;
767   GSList *prev;
768
769   prev = NULL;
770   tmp = list;
771
772   while (tmp)
773     {
774       if (tmp == link)
775         {
776           if (prev)
777             prev->next = link->next;
778
779           if (list == link)
780             list = list->next;
781
782           link->next = NULL;
783           break;
784         }
785
786       prev = tmp;
787       tmp = tmp->next;
788     }
789
790   *prevp = prev;
791
792   return list;
793 }
794
795 static void
796 gtk_list_store_remove_silently (GtkListStore *list_store,
797                                 GtkTreeIter  *iter,
798                                 GtkTreePath  *path)
799 {
800   if (G_SLIST (iter->user_data)->data)
801     {
802       _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
803                                 list_store->column_headers);
804       G_SLIST (iter->user_data)->data = NULL;
805     }
806
807   {
808     GSList *prev = NULL;
809
810     list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
811                                                 G_SLIST (iter->user_data),
812                                                 &prev);
813
814     list_store->length -= 1;
815
816     if (iter->user_data == list_store->tail)
817       list_store->tail = prev;
818   }
819
820   list_store->stamp ++;
821 }
822
823 /**
824  * gtk_list_store_remove:
825  * @store: a #GtkListStore
826  * @iter: a row in @list_store
827  *
828  * Removes the given row from the list store, emitting the
829  * "deleted" signal on #GtkTreeModel.
830  *
831  **/
832 void
833 gtk_list_store_remove (GtkListStore *list_store,
834                        GtkTreeIter  *iter)
835 {
836   GtkTreePath *path;
837
838   g_return_if_fail (list_store != NULL);
839   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
840   g_return_if_fail (iter->user_data != NULL);
841
842   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
843
844   validate_list_store (list_store);
845
846   gtk_list_store_remove_silently (list_store, iter, path);
847
848   validate_list_store (list_store);
849
850   list_store->stamp ++;
851   gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
852   gtk_tree_path_free (path);
853 }
854
855 static void
856 insert_after (GtkListStore *list_store,
857               GSList       *sibling,
858               GSList       *new_list)
859 {
860   g_return_if_fail (sibling != NULL);
861   g_return_if_fail (new_list != NULL);
862
863   /* insert new node after list */
864   new_list->next = sibling->next;
865   sibling->next = new_list;
866
867   /* if list was the tail, the new node is the new tail */
868   if (sibling == ((GSList *) list_store->tail))
869     list_store->tail = new_list;
870
871   list_store->length += 1;
872 }
873
874 /**
875  * gtk_list_store_insert:
876  * @store: a #GtkListStore
877  * @iter: iterator to initialize with the new row
878  * @position: position to insert the new row
879  *
880  * Creates a new row at @position, initializing @iter to point to the
881  * new row, and emitting the "inserted" signal from the #GtkTreeModel
882  * interface.
883  *
884  **/
885 void
886 gtk_list_store_insert (GtkListStore *list_store,
887                        GtkTreeIter  *iter,
888                        gint          position)
889 {
890   GSList *list;
891   GtkTreePath *path;
892   GSList *new_list;
893
894   g_return_if_fail (list_store != NULL);
895   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
896   g_return_if_fail (iter != NULL);
897   g_return_if_fail (position >= 0);
898
899   if (position == 0 ||
900       GTK_LIST_STORE_IS_SORTED (list_store))
901     {
902       gtk_list_store_prepend (list_store, iter);
903       return;
904     }
905
906   new_list = g_slist_alloc ();
907
908   list = g_slist_nth (G_SLIST (list_store->root), position - 1);
909
910   if (list == NULL)
911     {
912       g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
913       return;
914     }
915
916   insert_after (list_store, list, new_list);
917
918   iter->stamp = list_store->stamp;
919   iter->user_data = new_list;
920
921   validate_list_store (list_store);
922
923   path = gtk_tree_path_new ();
924   gtk_tree_path_append_index (path, position);
925   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
926   gtk_tree_path_free (path);
927 }
928
929 /**
930  * gtk_list_store_insert_before:
931  * @store: a #GtkListStore
932  * @iter: iterator to initialize with the new row
933  * @sibling: an existing row
934  *
935  * Inserts a new row before @sibling, initializing @iter to point to
936  * the new row, and emitting the "inserted" signal from the
937  * #GtkTreeModel interface.
938  *
939  **/
940 void
941 gtk_list_store_insert_before (GtkListStore *list_store,
942                               GtkTreeIter  *iter,
943                               GtkTreeIter  *sibling)
944 {
945   GtkTreePath *path;
946   GSList *list, *prev, *new_list;
947   gint i = 0;
948
949   g_return_if_fail (list_store != NULL);
950   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
951   g_return_if_fail (iter != NULL);
952
953   if (GTK_LIST_STORE_IS_SORTED (list_store))
954     {
955       gtk_list_store_prepend (list_store, iter);
956       return;
957     }
958
959   if (sibling == NULL)
960     {
961       gtk_list_store_append (list_store, iter);
962       return;
963     }
964
965   new_list = g_slist_alloc ();
966
967   prev = NULL;
968   list = list_store->root;
969   while (list && list != sibling->user_data)
970     {
971       prev = list;
972       list = list->next;
973       i++;
974     }
975
976   if (list != sibling->user_data)
977     {
978       g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
979       return;
980     }
981
982   /* if there are no nodes, we become the list tail, otherwise we
983    * are inserting before any existing nodes so we can't change
984    * the tail
985    */
986
987   if (list_store->root == NULL)
988     list_store->tail = new_list;
989
990   if (prev)
991     {
992       new_list->next = prev->next;
993       prev->next = new_list;
994     }
995   else
996     {
997       new_list->next = list_store->root;
998       list_store->root = new_list;
999     }
1000
1001   iter->stamp = list_store->stamp;
1002   iter->user_data = new_list;
1003
1004   list_store->length += 1;
1005
1006   validate_list_store (list_store);
1007
1008   path = gtk_tree_path_new ();
1009   gtk_tree_path_append_index (path, i);
1010   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1011   gtk_tree_path_free (path);
1012 }
1013
1014 /**
1015  * gtk_list_store_insert_after:
1016  * @store: a #GtkListStore
1017  * @iter: iterator to initialize with the new row
1018  * @sibling: an existing row
1019  *
1020  * Inserts a new row after @sibling, initializing @iter to point to
1021  * the new row, and emitting the "inserted" signal from the
1022  * #GtkTreeModel interface.
1023  *
1024  **/
1025 void
1026 gtk_list_store_insert_after (GtkListStore *list_store,
1027                              GtkTreeIter  *iter,
1028                              GtkTreeIter  *sibling)
1029 {
1030   GtkTreePath *path;
1031   GSList *list, *new_list;
1032   gint i = 0;
1033
1034   g_return_if_fail (list_store != NULL);
1035   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1036   g_return_if_fail (iter != NULL);
1037   if (sibling)
1038     g_return_if_fail (sibling->stamp == list_store->stamp);
1039
1040   if (sibling == NULL ||
1041       GTK_LIST_STORE_IS_SORTED (list_store))
1042     {
1043       gtk_list_store_prepend (list_store, iter);
1044       return;
1045     }
1046
1047   for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1048     i++;
1049
1050   g_return_if_fail (list == sibling->user_data);
1051
1052   new_list = g_slist_alloc ();
1053
1054   insert_after (list_store, list, new_list);
1055
1056   iter->stamp = list_store->stamp;
1057   iter->user_data = new_list;
1058
1059   validate_list_store (list_store);
1060
1061   path = gtk_tree_path_new ();
1062   gtk_tree_path_append_index (path, i);
1063   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1064   gtk_tree_path_free (path);
1065 }
1066
1067 /**
1068  * gtk_list_store_prepend:
1069  * @store: a #GtkListStore
1070  * @iter: iterator to initialize with new row
1071  *
1072  * Prepends a row to @store, initializing @iter to point to the
1073  * new row, and emitting the "inserted" signal on the #GtkTreeModel
1074  * interface for the @store.
1075  *
1076  **/
1077 void
1078 gtk_list_store_prepend (GtkListStore *list_store,
1079                         GtkTreeIter  *iter)
1080 {
1081   GtkTreePath *path;
1082
1083   g_return_if_fail (list_store != NULL);
1084   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1085   g_return_if_fail (iter != NULL);
1086
1087   iter->stamp = list_store->stamp;
1088   iter->user_data = g_slist_alloc ();
1089
1090   if (list_store->root == NULL)
1091     list_store->tail = iter->user_data;
1092
1093   G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1094   list_store->root = iter->user_data;
1095
1096   list_store->length += 1;
1097
1098   validate_list_store (list_store);
1099
1100   path = gtk_tree_path_new ();
1101   gtk_tree_path_append_index (path, 0);
1102   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1103   gtk_tree_path_free (path);
1104 }
1105
1106 /**
1107  * gtk_list_store_append:
1108  * @store: a #GtkListStore
1109  * @iter: iterator to initialize with the new row
1110  *
1111  * Appends a row to @store, initializing @iter to point to the
1112  * new row, and emitting the "inserted" signal on the #GtkTreeModel
1113  * interface for the @store.
1114  *
1115  **/
1116 void
1117 gtk_list_store_append (GtkListStore *list_store,
1118                        GtkTreeIter  *iter)
1119 {
1120   GtkTreePath *path;
1121
1122   g_return_if_fail (list_store != NULL);
1123   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1124   g_return_if_fail (iter != NULL);
1125
1126   if (GTK_LIST_STORE_IS_SORTED (list_store))
1127     {
1128       gtk_list_store_prepend (list_store, iter);
1129       return;
1130     }
1131
1132   iter->stamp = list_store->stamp;
1133   iter->user_data = g_slist_alloc ();
1134
1135   if (list_store->tail)
1136     ((GSList *)list_store->tail)->next = iter->user_data;
1137   else
1138     list_store->root = iter->user_data;
1139
1140   list_store->tail = iter->user_data;
1141
1142   list_store->length += 1;
1143
1144   validate_list_store (list_store);
1145
1146   path = gtk_tree_path_new ();
1147   gtk_tree_path_append_index (path, list_store->length - 1);
1148   gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1149   gtk_tree_path_free (path);
1150 }
1151
1152 static gboolean
1153 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1154                                  GtkTreePath       *path)
1155 {
1156   GtkTreeIter iter;
1157   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1158
1159   if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1160                                &iter,
1161                                path))
1162     {
1163       gtk_list_store_remove (GTK_LIST_STORE (drag_source),
1164                              &iter);
1165       return TRUE;
1166     }
1167   else
1168     {
1169       return FALSE;
1170     }
1171 }
1172
1173 static gboolean
1174 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1175                               GtkTreePath       *path,
1176                               GtkSelectionData  *selection_data)
1177 {
1178   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1179
1180   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1181    * target, because the default handler does it for us, but
1182    * we do anyway for the convenience of someone maybe overriding the
1183    * default handler.
1184    */
1185
1186   if (gtk_selection_data_set_tree_row (selection_data,
1187                                        GTK_TREE_MODEL (drag_source),
1188                                        path))
1189     {
1190       return TRUE;
1191     }
1192   else
1193     {
1194       /* FIXME handle text targets at least. */
1195     }
1196
1197   return FALSE;
1198 }
1199
1200 static gboolean
1201 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1202                                    GtkTreePath       *dest,
1203                                    GtkSelectionData  *selection_data)
1204 {
1205   GtkTreeModel *tree_model;
1206   GtkListStore *list_store;
1207   GtkTreeModel *src_model = NULL;
1208   GtkTreePath *src_path = NULL;
1209   gboolean retval = FALSE;
1210
1211   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1212
1213   tree_model = GTK_TREE_MODEL (drag_dest);
1214   list_store = GTK_LIST_STORE (drag_dest);
1215
1216   if (gtk_selection_data_get_tree_row (selection_data,
1217                                        &src_model,
1218                                        &src_path) &&
1219       src_model == tree_model)
1220     {
1221       /* Copy the given row to a new position */
1222       GtkTreeIter src_iter;
1223       GtkTreeIter dest_iter;
1224       GtkTreePath *prev;
1225
1226       if (!gtk_tree_model_get_iter (src_model,
1227                                     &src_iter,
1228                                     src_path))
1229         {
1230           goto out;
1231         }
1232
1233       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1234       prev = gtk_tree_path_copy (dest);
1235
1236       if (!gtk_tree_path_prev (prev))
1237         {
1238           /* dest was the first spot in the list; which means we are supposed
1239            * to prepend.
1240            */
1241           gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1242                                   &dest_iter);
1243
1244           retval = TRUE;
1245         }
1246       else
1247         {
1248           if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1249                                        &dest_iter,
1250                                        prev))
1251             {
1252               GtkTreeIter tmp_iter = dest_iter;
1253               gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1254                                            &dest_iter,
1255                                            &tmp_iter);
1256               retval = TRUE;
1257             }
1258         }
1259
1260       gtk_tree_path_free (prev);
1261
1262       /* If we succeeded in creating dest_iter, copy data from src
1263        */
1264       if (retval)
1265         {
1266           GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1267           GtkTreeDataList *copy_head = NULL;
1268           GtkTreeDataList *copy_prev = NULL;
1269           GtkTreeDataList *copy_iter = NULL;
1270           GtkTreePath *path;
1271           gint col;
1272
1273           col = 0;
1274           while (dl)
1275             {
1276               copy_iter = _gtk_tree_data_list_node_copy (dl,
1277                                                          list_store->column_headers[col]);
1278
1279               if (copy_head == NULL)
1280                 copy_head = copy_iter;
1281
1282               if (copy_prev)
1283                 copy_prev->next = copy_iter;
1284
1285               copy_prev = copy_iter;
1286
1287               dl = dl->next;
1288               ++col;
1289             }
1290
1291           dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1292           G_SLIST (dest_iter.user_data)->data = copy_head;
1293
1294           path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1295           gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1296           gtk_tree_path_free (path);
1297         }
1298     }
1299   else
1300     {
1301       /* FIXME maybe add some data targets eventually, or handle text
1302        * targets in the simple case.
1303        */
1304     }
1305
1306  out:
1307
1308   if (src_path)
1309     gtk_tree_path_free (src_path);
1310
1311   return retval;
1312 }
1313
1314 static gboolean
1315 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1316                                   GtkTreeModel    *src_model,
1317                                   GtkTreePath     *src_path,
1318                                   GtkTreePath     *dest_path)
1319 {
1320   gint *indices;
1321
1322   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1323
1324   if (src_model != GTK_TREE_MODEL (drag_dest))
1325     return FALSE;
1326
1327   if (gtk_tree_path_get_depth (dest_path) != 1)
1328     return FALSE;
1329
1330   /* can drop before any existing node, or before one past any existing. */
1331
1332   indices = gtk_tree_path_get_indices (dest_path);
1333
1334   if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1335     return TRUE;
1336   else
1337     return FALSE;
1338 }
1339
1340
1341 /* Sorting */
1342 typedef struct _SortTuple
1343 {
1344   gint offset;
1345   GSList *el;
1346 } SortTuple;
1347
1348 static gint
1349 _gtk_list_store_compare_func (gconstpointer a,
1350                               gconstpointer b,
1351                               gpointer      user_data)
1352 {
1353   GtkListStore *list_store = user_data;
1354   GtkTreeDataSortHeader *header = NULL;
1355   GSList *el_a; /* Los Angeles? */
1356   GSList *el_b;
1357   GtkTreeIter iter_a;
1358   GtkTreeIter iter_b;
1359   gint retval;
1360
1361   header = _gtk_tree_data_list_get_header (list_store->sort_list,
1362                                            list_store->sort_column_id);
1363
1364   g_return_val_if_fail (header != NULL, 0);
1365   g_return_val_if_fail (header->func != NULL, 0);
1366
1367   el_a = ((SortTuple *) a)->el;
1368   el_b = ((SortTuple *) b)->el;
1369
1370   iter_a.stamp = list_store->stamp;
1371   iter_a.user_data = el_a;
1372   iter_b.stamp = list_store->stamp;
1373   iter_b.user_data = el_b;
1374
1375   retval = (* header->func) (GTK_TREE_MODEL (list_store),
1376                              &iter_a, &iter_b,
1377                              header->data);
1378
1379   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1380     {
1381       if (retval > 0)
1382         retval = -1;
1383       else if (retval < 0)
1384         retval = 1;
1385     }
1386   return retval;
1387 }
1388
1389 static void
1390 gtk_list_store_sort (GtkListStore *list_store)
1391 {
1392   GtkTreeDataSortHeader *header = NULL;
1393   GtkTreeIter iter;
1394   GArray *sort_array;
1395   gint i;
1396   GList *header_list;
1397   gint *new_order;
1398   GSList *list;
1399   GtkTreePath *path;
1400
1401   if (list_store->length <= 1)
1402     return;
1403
1404   g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1405
1406   for (header_list = list_store->sort_list; header_list; header_list = header_list->next)
1407     {
1408       header = (GtkTreeDataSortHeader*) header_list->data;
1409       if (header->sort_column_id == list_store->sort_column_id)
1410         break;
1411     }
1412
1413   /* We want to make sure that we have a function */
1414   g_return_if_fail (header != NULL);
1415   g_return_if_fail (header->func != NULL);
1416
1417   list = G_SLIST (list_store->root);
1418
1419   sort_array = g_array_sized_new (FALSE, FALSE,
1420                                   sizeof (SortTuple),
1421                                   list_store->length);
1422
1423   for (i = 0; i < list_store->length; i++)
1424     {
1425       SortTuple tuple;
1426
1427       /* If this fails, we are in an inconsistent state.  Bad */
1428       g_return_if_fail (list != NULL);
1429
1430       tuple.offset = i;
1431       tuple.el = list;
1432       g_array_append_val (sort_array, tuple);
1433
1434       list = list->next;
1435     }
1436
1437   g_array_sort_with_data (sort_array, _gtk_list_store_compare_func, list_store);
1438
1439   for (i = 0; i < list_store->length - 1; i++)
1440       g_array_index (sort_array, SortTuple, i).el->next =
1441         g_array_index (sort_array, SortTuple, i + 1).el;
1442   g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1443   list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1444
1445   /* Let the world know about our new order */
1446   new_order = g_new (gint, list_store->length);
1447   for (i = 0; i < list_store->length; i++)
1448     new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1449   path = gtk_tree_path_new ();
1450   iter.stamp = list_store->stamp;
1451   iter.user_data = NULL;
1452   gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1453                             path, &iter, new_order);
1454   gtk_tree_path_free (path);
1455   g_free (new_order);
1456   g_array_free (sort_array, TRUE);
1457 }
1458
1459 static void
1460 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1461                                   GtkTreeIter  *iter,
1462                                   gint          column)
1463
1464 {
1465   GtkTreeDataSortHeader *header;
1466   GSList *prev = NULL;
1467   GSList *next = NULL;
1468   GSList *list = G_SLIST (list_store->root);
1469   GtkTreePath *tmp_path;
1470   GtkTreeIter tmp_iter;
1471   gint cmp_a = 0;
1472   gint cmp_b = 0;
1473   gint i;
1474   gint old_location;
1475   gint new_location;
1476   gint *new_order;
1477
1478   if (list_store->length < 2)
1479     return;
1480
1481   tmp_iter.stamp = list_store->stamp;
1482   header = _gtk_tree_data_list_get_header (list_store->sort_list,
1483                                            list_store->sort_column_id);
1484   g_return_if_fail (header != NULL);
1485   g_return_if_fail (header->func != NULL);
1486
1487   /* If it's the built in function, we don't sort. */
1488   if (header->func == gtk_tree_data_list_compare_func &&
1489       list_store->sort_column_id != column)
1490     return;
1491
1492   old_location = 0;
1493   /* First we find the iter, its prev, and its next */
1494   while (list)
1495     {
1496       if (list == G_SLIST (iter->user_data))
1497         break;
1498       prev = list;
1499       list = list->next;
1500       old_location++;
1501     }
1502   g_assert (list != NULL);
1503
1504   next = list->next;
1505
1506   /* Check the common case, where we don't need to sort it moved. */
1507   if (prev != NULL)
1508     {
1509       tmp_iter.user_data = prev;
1510       cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1511                                 &tmp_iter, iter,
1512                                 header->data);
1513     }
1514
1515   if (next != NULL)
1516     {
1517       tmp_iter.user_data = next;
1518       cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1519                                 iter, &tmp_iter,
1520                                 header->data);
1521     }
1522
1523
1524   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1525     {
1526       if (cmp_a < 0)
1527         cmp_a = 1;
1528       else if (cmp_a > 0)
1529         cmp_a = -1;
1530
1531       if (cmp_b < 0)
1532         cmp_b = 1;
1533       else if (cmp_b > 0)
1534         cmp_b = -1;
1535     }
1536
1537   if (prev == NULL && cmp_b <= 0)
1538     return;
1539   else if (next == NULL && cmp_a <= 0)
1540     return;
1541   else if (prev != NULL && next != NULL &&
1542            cmp_a <= 0 && cmp_b <= 0)
1543     return;
1544
1545   /* We actually need to sort it */
1546   /* First, remove the old link. */
1547
1548   if (prev == NULL)
1549     list_store->root = next;
1550   else
1551     prev->next = next;
1552   if (next == NULL)
1553     list_store->tail = prev;
1554   list->next = NULL;
1555   
1556   /* FIXME: as an optimization, we can potentially start at next */
1557   prev = NULL;
1558   list = G_SLIST (list_store->root);
1559   new_location = 0;
1560   tmp_iter.user_data = list;
1561   if (list_store->order == GTK_TREE_SORT_DESCENDING)
1562     cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1563                               &tmp_iter, iter, header->data);
1564   else
1565     cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1566                               iter, &tmp_iter, header->data);
1567
1568   while ((list->next) && (cmp_a > 0))
1569     {
1570       prev = list;
1571       list = list->next;
1572       new_location++;
1573       tmp_iter.user_data = list;
1574       if (list_store->order == GTK_TREE_SORT_DESCENDING)
1575         cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1576                                   &tmp_iter, iter, header->data);
1577       else
1578         cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1579                                   iter, &tmp_iter, header->data);
1580     }
1581
1582   if ((!list->next) && (cmp_a > 0))
1583     {
1584       list->next = G_SLIST (iter->user_data);
1585       list_store->tail = list->next;
1586     }
1587   else if (prev)
1588     {
1589       prev->next = G_SLIST (iter->user_data);
1590       G_SLIST (iter->user_data)->next = list;
1591     }
1592   else
1593     {
1594       G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1595       list_store->root = G_SLIST (iter->user_data);
1596     }
1597
1598   /* Emit the reordered signal. */
1599   new_order = g_new (int, list_store->length);
1600   if (old_location < new_location)
1601     for (i = 0; i < list_store->length; i++)
1602       {
1603         if (i < old_location ||
1604             i > new_location)
1605           new_order[i] = i;
1606         else if (i >= old_location &&
1607                  i < new_location)
1608           new_order[i] = i + 1;
1609         else if (i == new_location)
1610           new_order[i] = old_location;
1611       }
1612   else
1613     for (i = 0; i < list_store->length; i++)
1614       {
1615         if (i < new_location ||
1616             i > old_location)
1617           new_order[i] = i;
1618         else if (i > new_location &&
1619                  i <= old_location)
1620           new_order[i] = i - 1;
1621         else if (i == new_location)
1622           new_order[i] = old_location;
1623       }
1624
1625   tmp_path = gtk_tree_path_new ();
1626   tmp_iter.user_data = NULL;
1627
1628   gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1629                             tmp_path, &tmp_iter,
1630                             new_order);
1631
1632   gtk_tree_path_free (tmp_path);
1633   g_free (new_order);
1634 }
1635
1636 static gboolean
1637 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1638                                    gint             *sort_column_id,
1639                                    GtkTreeSortOrder *order)
1640 {
1641   GtkListStore *list_store = (GtkListStore *) sortable;
1642
1643   g_return_val_if_fail (sortable != NULL, FALSE);
1644   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1645
1646   if (list_store->sort_column_id == -1)
1647     return FALSE;
1648
1649   if (sort_column_id)
1650     * sort_column_id = list_store->sort_column_id;
1651   if (order)
1652     * order = list_store->order;
1653   return TRUE;
1654 }
1655
1656 static void
1657 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1658                                    gint              sort_column_id,
1659                                    GtkTreeSortOrder  order)
1660 {
1661   GtkListStore *list_store = (GtkListStore *) sortable;
1662   GList *list;
1663
1664   g_return_if_fail (sortable != NULL);
1665   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1666
1667   for (list = list_store->sort_list; list; list = list->next)
1668     {
1669       GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1670       if (header->sort_column_id == sort_column_id)
1671         break;
1672     }
1673   g_return_if_fail (list != NULL);
1674
1675   if ((list_store->sort_column_id == sort_column_id) &&
1676       (list_store->order == order))
1677     return;
1678
1679   list_store->sort_column_id = sort_column_id;
1680   list_store->order = order;
1681
1682   if (list_store->sort_column_id >= 0)
1683     gtk_list_store_sort (list_store);
1684
1685   gtk_tree_sortable_sort_column_changed (sortable);
1686 }
1687
1688 static void
1689 gtk_list_store_sort_column_id_set_func (GtkTreeSortable  *sortable,
1690                                         gint              sort_column_id,
1691                                         GtkTreeIterCompareFunc func,
1692                                         gpointer          data,
1693                                         GtkDestroyNotify  destroy)
1694 {
1695   GtkListStore *list_store = (GtkListStore *) sortable;
1696   GtkTreeDataSortHeader *header = NULL;
1697   GList *list;
1698
1699   g_return_if_fail (sortable != NULL);
1700   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1701   g_return_if_fail (func != NULL);
1702
1703   for (list = list_store->sort_list; list; list = list->next)
1704     {
1705       header = (GtkTreeDataSortHeader*) list->data;
1706       if (header->sort_column_id == sort_column_id)
1707         break;
1708     }
1709
1710   if (header == NULL)
1711     {
1712       header = g_new0 (GtkTreeDataSortHeader, 1);
1713       header->sort_column_id = sort_column_id;
1714       list_store->sort_list = g_list_append (list_store->sort_list, header);
1715     }
1716
1717   if (header->destroy)
1718     (* header->destroy) (header->data);
1719
1720   header->func = func;
1721   header->data = data;
1722   header->destroy = destroy;
1723 }