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