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