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