]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
examples/gtkdial/gtkdial.c gdk/gdkapplaunchcontext.c gdk/gdkpango.c
[~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 "config.h"
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <gobject/gvaluecollector.h>
25 #include "gtktreemodel.h"
26 #include "gtkliststore.h"
27 #include "gtktreedatalist.h"
28 #include "gtktreednd.h"
29 #include "gtkintl.h"
30 #include "gtkbuildable.h"
31 #include "gtkbuilderprivate.h"
32 #include "gtkalias.h"
33
34 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
35 #define VALID_ITER(iter, list_store) ((iter)!= NULL && (iter)->user_data != NULL && list_store->stamp == (iter)->stamp && !g_sequence_iter_is_end ((iter)->user_data) && g_sequence_iter_get_sequence ((iter)->user_data) == list_store->seq)
36
37 static void         gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
38 static void         gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
39 static void         gtk_list_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
40 static void         gtk_list_store_sortable_init   (GtkTreeSortableIface   *iface);
41 static void         gtk_list_store_buildable_init  (GtkBuildableIface      *iface);
42 static void         gtk_list_store_finalize        (GObject           *object);
43 static GtkTreeModelFlags gtk_list_store_get_flags  (GtkTreeModel      *tree_model);
44 static gint         gtk_list_store_get_n_columns   (GtkTreeModel      *tree_model);
45 static GType        gtk_list_store_get_column_type (GtkTreeModel      *tree_model,
46                                                     gint               index);
47 static gboolean     gtk_list_store_get_iter        (GtkTreeModel      *tree_model,
48                                                     GtkTreeIter       *iter,
49                                                     GtkTreePath       *path);
50 static GtkTreePath *gtk_list_store_get_path        (GtkTreeModel      *tree_model,
51                                                     GtkTreeIter       *iter);
52 static void         gtk_list_store_get_value       (GtkTreeModel      *tree_model,
53                                                     GtkTreeIter       *iter,
54                                                     gint               column,
55                                                     GValue            *value);
56 static gboolean     gtk_list_store_iter_next       (GtkTreeModel      *tree_model,
57                                                     GtkTreeIter       *iter);
58 static gboolean     gtk_list_store_iter_children   (GtkTreeModel      *tree_model,
59                                                     GtkTreeIter       *iter,
60                                                     GtkTreeIter       *parent);
61 static gboolean     gtk_list_store_iter_has_child  (GtkTreeModel      *tree_model,
62                                                     GtkTreeIter       *iter);
63 static gint         gtk_list_store_iter_n_children (GtkTreeModel      *tree_model,
64                                                     GtkTreeIter       *iter);
65 static gboolean     gtk_list_store_iter_nth_child  (GtkTreeModel      *tree_model,
66                                                     GtkTreeIter       *iter,
67                                                     GtkTreeIter       *parent,
68                                                     gint               n);
69 static gboolean     gtk_list_store_iter_parent     (GtkTreeModel      *tree_model,
70                                                     GtkTreeIter       *iter,
71                                                     GtkTreeIter       *child);
72
73
74 static void gtk_list_store_set_n_columns   (GtkListStore *list_store,
75                                             gint          n_columns);
76 static void gtk_list_store_set_column_type (GtkListStore *list_store,
77                                             gint          column,
78                                             GType         type);
79
80 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
81
82
83 /* Drag and Drop */
84 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
85                                                    GtkTreePath       *path);
86 static gboolean gtk_list_store_drag_data_delete   (GtkTreeDragSource *drag_source,
87                                                    GtkTreePath       *path);
88 static gboolean gtk_list_store_drag_data_get      (GtkTreeDragSource *drag_source,
89                                                    GtkTreePath       *path,
90                                                    GtkSelectionData  *selection_data);
91 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
92                                                    GtkTreePath       *dest,
93                                                    GtkSelectionData  *selection_data);
94 static gboolean gtk_list_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
95                                                    GtkTreePath       *dest_path,
96                                                    GtkSelectionData  *selection_data);
97
98
99 /* sortable */
100 static void     gtk_list_store_sort                  (GtkListStore           *list_store);
101 static void     gtk_list_store_sort_iter_changed     (GtkListStore           *list_store,
102                                                       GtkTreeIter            *iter,
103                                                       gint                    column);
104 static gboolean gtk_list_store_get_sort_column_id    (GtkTreeSortable        *sortable,
105                                                       gint                   *sort_column_id,
106                                                       GtkSortType            *order);
107 static void     gtk_list_store_set_sort_column_id    (GtkTreeSortable        *sortable,
108                                                       gint                    sort_column_id,
109                                                       GtkSortType             order);
110 static void     gtk_list_store_set_sort_func         (GtkTreeSortable        *sortable,
111                                                       gint                    sort_column_id,
112                                                       GtkTreeIterCompareFunc  func,
113                                                       gpointer                data,
114                                                       GDestroyNotify          destroy);
115 static void     gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
116                                                       GtkTreeIterCompareFunc  func,
117                                                       gpointer                data,
118                                                       GDestroyNotify          destroy);
119 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable        *sortable);
120
121
122 /* buildable */
123 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable  *buildable,
124                                                            GtkBuilder    *builder,
125                                                            GObject       *child,
126                                                            const gchar   *tagname,
127                                                            GMarkupParser *parser,
128                                                            gpointer      *data);
129 static void     gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
130                                                          GtkBuilder   *builder,
131                                                          GObject      *child,
132                                                          const gchar  *tagname,
133                                                          gpointer     *data);
134
135 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
136                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
137                                                 gtk_list_store_tree_model_init)
138                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
139                                                 gtk_list_store_drag_source_init)
140                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
141                                                 gtk_list_store_drag_dest_init)
142                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
143                                                 gtk_list_store_sortable_init)
144                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
145                                                 gtk_list_store_buildable_init))
146
147
148 static void
149 gtk_list_store_class_init (GtkListStoreClass *class)
150 {
151   GObjectClass *object_class;
152
153   object_class = (GObjectClass*) class;
154
155   object_class->finalize = gtk_list_store_finalize;
156 }
157
158 static void
159 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
160 {
161   iface->get_flags = gtk_list_store_get_flags;
162   iface->get_n_columns = gtk_list_store_get_n_columns;
163   iface->get_column_type = gtk_list_store_get_column_type;
164   iface->get_iter = gtk_list_store_get_iter;
165   iface->get_path = gtk_list_store_get_path;
166   iface->get_value = gtk_list_store_get_value;
167   iface->iter_next = gtk_list_store_iter_next;
168   iface->iter_children = gtk_list_store_iter_children;
169   iface->iter_has_child = gtk_list_store_iter_has_child;
170   iface->iter_n_children = gtk_list_store_iter_n_children;
171   iface->iter_nth_child = gtk_list_store_iter_nth_child;
172   iface->iter_parent = gtk_list_store_iter_parent;
173 }
174
175 static void
176 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
177 {
178   iface->row_draggable = real_gtk_list_store_row_draggable;
179   iface->drag_data_delete = gtk_list_store_drag_data_delete;
180   iface->drag_data_get = gtk_list_store_drag_data_get;
181 }
182
183 static void
184 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
185 {
186   iface->drag_data_received = gtk_list_store_drag_data_received;
187   iface->row_drop_possible = gtk_list_store_row_drop_possible;
188 }
189
190 static void
191 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
192 {
193   iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
194   iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
195   iface->set_sort_func = gtk_list_store_set_sort_func;
196   iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
197   iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
198 }
199
200 void
201 gtk_list_store_buildable_init (GtkBuildableIface *iface)
202 {
203   iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
204   iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
205 }
206
207 static void
208 gtk_list_store_init (GtkListStore *list_store)
209 {
210   list_store->seq = g_sequence_new (NULL);
211   list_store->sort_list = NULL;
212   list_store->stamp = g_random_int ();
213   list_store->sort_column_id = -2;
214   list_store->columns_dirty = FALSE;
215   list_store->length = 0;
216 }
217
218 /**
219  * gtk_list_store_new:
220  * @n_columns: number of columns in the list store
221  * @Varargs: all #GType types for the columns, from first to last
222  *
223  * Creates a new list store as with @n_columns columns each of the types passed
224  * in.  Note that only types derived from standard GObject fundamental types 
225  * are supported. 
226  *
227  * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
228  * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
229  * int, string and #GdkPixbuf respectively.
230  *
231  * Return value: a new #GtkListStore
232  **/
233 GtkListStore *
234 gtk_list_store_new (gint n_columns,
235                     ...)
236 {
237   GtkListStore *retval;
238   va_list args;
239   gint i;
240
241   g_return_val_if_fail (n_columns > 0, NULL);
242
243   retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
244   gtk_list_store_set_n_columns (retval, n_columns);
245
246   va_start (args, n_columns);
247
248   for (i = 0; i < n_columns; i++)
249     {
250       GType type = va_arg (args, GType);
251       if (! _gtk_tree_data_list_check_type (type))
252         {
253           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
254           g_object_unref (retval);
255           return NULL;
256         }
257
258       gtk_list_store_set_column_type (retval, i, type);
259     }
260
261   va_end (args);
262
263   return retval;
264 }
265
266
267 /**
268  * gtk_list_store_newv:
269  * @n_columns: number of columns in the list store
270  * @types: an array of #GType types for the columns, from first to last
271  *
272  * Non-vararg creation function.  Used primarily by language bindings.
273  *
274  * Return value: a new #GtkListStore
275  **/
276 GtkListStore *
277 gtk_list_store_newv (gint   n_columns,
278                      GType *types)
279 {
280   GtkListStore *retval;
281   gint i;
282
283   g_return_val_if_fail (n_columns > 0, NULL);
284
285   retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
286   gtk_list_store_set_n_columns (retval, n_columns);
287
288   for (i = 0; i < n_columns; i++)
289     {
290       if (! _gtk_tree_data_list_check_type (types[i]))
291         {
292           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
293           g_object_unref (retval);
294           return NULL;
295         }
296
297       gtk_list_store_set_column_type (retval, i, types[i]);
298     }
299
300   return retval;
301 }
302
303 /**
304  * gtk_list_store_set_column_types:
305  * @list_store: A #GtkListStore
306  * @n_columns: Number of columns for the list store
307  * @types: An array length n of #GTypes
308  * 
309  * This function is meant primarily for #GObjects that inherit from #GtkListStore,
310  * and should only be used when constructing a new #GtkListStore.  It will not
311  * function after a row has been added, or a method on the #GtkTreeModel
312  * interface is called.
313  **/
314 void
315 gtk_list_store_set_column_types (GtkListStore *list_store,
316                                  gint          n_columns,
317                                  GType        *types)
318 {
319   gint i;
320
321   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
322   g_return_if_fail (list_store->columns_dirty == 0);
323
324   gtk_list_store_set_n_columns (list_store, n_columns);
325   for (i = 0; i < n_columns; i++)
326     {
327       if (! _gtk_tree_data_list_check_type (types[i]))
328         {
329           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
330           continue;
331         }
332       gtk_list_store_set_column_type (list_store, i, types[i]);
333     }
334 }
335
336 static void
337 gtk_list_store_set_n_columns (GtkListStore *list_store,
338                               gint          n_columns)
339 {
340   GType *new_columns;
341
342   if (list_store->n_columns == n_columns)
343     return;
344
345   new_columns = g_new0 (GType, n_columns);
346   if (list_store->column_headers)
347     {
348       /* copy the old header orders over */
349       if (n_columns >= list_store->n_columns)
350         memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
351       else
352         memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
353
354       g_free (list_store->column_headers);
355     }
356
357   if (list_store->sort_list)
358     _gtk_tree_data_list_header_free (list_store->sort_list);
359
360   list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
361
362   list_store->column_headers = new_columns;
363   list_store->n_columns = n_columns;
364 }
365
366 static void
367 gtk_list_store_set_column_type (GtkListStore *list_store,
368                                 gint          column,
369                                 GType         type)
370 {
371   if (!_gtk_tree_data_list_check_type (type))
372     {
373       g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
374       return;
375     }
376
377   list_store->column_headers[column] = type;
378 }
379
380 static void
381 gtk_list_store_finalize (GObject *object)
382 {
383   GtkListStore *list_store = GTK_LIST_STORE (object);
384
385   g_sequence_foreach (list_store->seq,
386                       (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
387
388   g_sequence_free (list_store->seq);
389
390   _gtk_tree_data_list_header_free (list_store->sort_list);
391   g_free (list_store->column_headers);
392   
393   if (list_store->default_sort_destroy)
394     {
395       GDestroyNotify d = list_store->default_sort_destroy;
396
397       list_store->default_sort_destroy = NULL;
398       d (list_store->default_sort_data);
399       list_store->default_sort_data = NULL;
400     }
401
402   G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
403 }
404
405 /* Fulfill the GtkTreeModel requirements */
406 static GtkTreeModelFlags
407 gtk_list_store_get_flags (GtkTreeModel *tree_model)
408 {
409   return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
410 }
411
412 static gint
413 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
414 {
415   GtkListStore *list_store = (GtkListStore *) tree_model;
416
417   list_store->columns_dirty = TRUE;
418
419   return list_store->n_columns;
420 }
421
422 static GType
423 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
424                                 gint          index)
425 {
426   GtkListStore *list_store = (GtkListStore *) tree_model;
427
428   g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns, 
429                         G_TYPE_INVALID);
430
431   list_store->columns_dirty = TRUE;
432
433   return list_store->column_headers[index];
434 }
435
436 static gboolean
437 gtk_list_store_get_iter (GtkTreeModel *tree_model,
438                          GtkTreeIter  *iter,
439                          GtkTreePath  *path)
440 {
441   GtkListStore *list_store = (GtkListStore *) tree_model;
442   GSequence *seq;
443   gint i;
444
445   list_store->columns_dirty = TRUE;
446
447   seq = list_store->seq;
448   
449   i = gtk_tree_path_get_indices (path)[0];
450
451   if (i >= g_sequence_get_length (seq))
452     return FALSE;
453
454   iter->stamp = list_store->stamp;
455   iter->user_data = g_sequence_get_iter_at_pos (seq, i);
456
457   return TRUE;
458 }
459
460 static GtkTreePath *
461 gtk_list_store_get_path (GtkTreeModel *tree_model,
462                          GtkTreeIter  *iter)
463 {
464   GtkTreePath *path;
465
466   g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
467
468   if (g_sequence_iter_is_end (iter->user_data))
469     return NULL;
470         
471   path = gtk_tree_path_new ();
472   gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
473   
474   return path;
475 }
476
477 static void
478 gtk_list_store_get_value (GtkTreeModel *tree_model,
479                           GtkTreeIter  *iter,
480                           gint          column,
481                           GValue       *value)
482 {
483   GtkListStore *list_store = (GtkListStore *) tree_model;
484   GtkTreeDataList *list;
485   gint tmp_column = column;
486
487   g_return_if_fail (column < list_store->n_columns);
488   g_return_if_fail (VALID_ITER (iter, list_store));
489                     
490   list = g_sequence_get (iter->user_data);
491
492   while (tmp_column-- > 0 && list)
493     list = list->next;
494
495   if (list == NULL)
496     g_value_init (value, list_store->column_headers[column]);
497   else
498     _gtk_tree_data_list_node_to_value (list,
499                                        list_store->column_headers[column],
500                                        value);
501 }
502
503 static gboolean
504 gtk_list_store_iter_next (GtkTreeModel  *tree_model,
505                           GtkTreeIter   *iter)
506 {
507   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
508   iter->user_data = g_sequence_iter_next (iter->user_data);
509
510   return !g_sequence_iter_is_end (iter->user_data);
511 }
512
513 static gboolean
514 gtk_list_store_iter_children (GtkTreeModel *tree_model,
515                               GtkTreeIter  *iter,
516                               GtkTreeIter  *parent)
517 {
518   GtkListStore *list_store = (GtkListStore *) tree_model;
519   
520   /* this is a list, nodes have no children */
521   if (parent)
522     return FALSE;
523
524   if (g_sequence_get_length (list_store->seq) > 0)
525     {
526       iter->stamp = list_store->stamp;
527       iter->user_data = g_sequence_get_begin_iter (list_store->seq);
528       return TRUE;
529     }
530   else
531     return FALSE;
532 }
533
534 static gboolean
535 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
536                                GtkTreeIter  *iter)
537 {
538   return FALSE;
539 }
540
541 static gint
542 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
543                                 GtkTreeIter  *iter)
544 {
545   GtkListStore *list_store = (GtkListStore *) tree_model;
546
547   if (iter == NULL)
548     return g_sequence_get_length (list_store->seq);
549
550   g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
551
552   return 0;
553 }
554
555 static gboolean
556 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
557                                GtkTreeIter  *iter,
558                                GtkTreeIter  *parent,
559                                gint          n)
560 {
561   GtkListStore *list_store = (GtkListStore *) tree_model;
562   GSequenceIter *child;
563
564   if (parent)
565     return FALSE;
566
567   child = g_sequence_get_iter_at_pos (list_store->seq, n);
568
569   if (g_sequence_iter_is_end (child))
570     return FALSE;
571
572   iter->stamp = list_store->stamp;
573   iter->user_data = child;
574
575   return TRUE;
576 }
577
578 static gboolean
579 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
580                             GtkTreeIter  *iter,
581                             GtkTreeIter  *child)
582 {
583   return FALSE;
584 }
585
586 static gboolean
587 gtk_list_store_real_set_value (GtkListStore *list_store,
588                                GtkTreeIter  *iter,
589                                gint          column,
590                                GValue       *value,
591                                gboolean      sort)
592 {
593   GtkTreeDataList *list;
594   GtkTreeDataList *prev;
595   gint old_column = column;
596   GValue real_value = {0, };
597   gboolean converted = FALSE;
598   gboolean retval = FALSE;
599
600   if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
601     {
602       if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
603              g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
604         {
605           g_warning ("%s: Unable to convert from %s to %s\n",
606                      G_STRLOC,
607                      g_type_name (G_VALUE_TYPE (value)),
608                      g_type_name (list_store->column_headers[column]));
609           return retval;
610         }
611       if (!g_value_transform (value, &real_value))
612         {
613           g_warning ("%s: Unable to make conversion from %s to %s\n",
614                      G_STRLOC,
615                      g_type_name (G_VALUE_TYPE (value)),
616                      g_type_name (list_store->column_headers[column]));
617           g_value_unset (&real_value);
618           return retval;
619         }
620       converted = TRUE;
621     }
622
623   prev = list = g_sequence_get (iter->user_data);
624
625   while (list != NULL)
626     {
627       if (column == 0)
628         {
629           if (converted)
630             _gtk_tree_data_list_value_to_node (list, &real_value);
631           else
632             _gtk_tree_data_list_value_to_node (list, value);
633           retval = TRUE;
634           if (converted)
635             g_value_unset (&real_value);
636          if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
637             gtk_list_store_sort_iter_changed (list_store, iter, old_column);
638           return retval;
639         }
640
641       column--;
642       prev = list;
643       list = list->next;
644     }
645
646   if (g_sequence_get (iter->user_data) == NULL)
647     {
648       list = _gtk_tree_data_list_alloc();
649       g_sequence_set (iter->user_data, list);
650       list->next = NULL;
651     }
652   else
653     {
654       list = prev->next = _gtk_tree_data_list_alloc ();
655       list->next = NULL;
656     }
657
658   while (column != 0)
659     {
660       list->next = _gtk_tree_data_list_alloc ();
661       list = list->next;
662       list->next = NULL;
663       column --;
664     }
665
666   if (converted)
667     _gtk_tree_data_list_value_to_node (list, &real_value);
668   else
669     _gtk_tree_data_list_value_to_node (list, value);
670
671   retval = TRUE;
672   if (converted)
673     g_value_unset (&real_value);
674
675   if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
676     gtk_list_store_sort_iter_changed (list_store, iter, old_column);
677
678   return retval;
679 }
680
681
682 /**
683  * gtk_list_store_set_value:
684  * @list_store: A #GtkListStore
685  * @iter: A valid #GtkTreeIter for the row being modified
686  * @column: column number to modify
687  * @value: new value for the cell
688  *
689  * Sets the data in the cell specified by @iter and @column.
690  * The type of @value must be convertible to the type of the
691  * column.
692  *
693  **/
694 void
695 gtk_list_store_set_value (GtkListStore *list_store,
696                           GtkTreeIter  *iter,
697                           gint          column,
698                           GValue       *value)
699 {
700   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
701   g_return_if_fail (VALID_ITER (iter, list_store));
702   g_return_if_fail (column >= 0 && column < list_store->n_columns);
703   g_return_if_fail (G_IS_VALUE (value));
704
705   if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
706     {
707       GtkTreePath *path;
708
709       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
710       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
711       gtk_tree_path_free (path);
712     }
713 }
714
715 static GtkTreeIterCompareFunc
716 gtk_list_store_get_compare_func (GtkListStore *list_store)
717 {
718   GtkTreeIterCompareFunc func = NULL;
719
720   if (GTK_LIST_STORE_IS_SORTED (list_store))
721     {
722       if (list_store->sort_column_id != -1)
723         {
724           GtkTreeDataSortHeader *header;
725           header = _gtk_tree_data_list_get_header (list_store->sort_list,
726                                                    list_store->sort_column_id);
727           g_return_val_if_fail (header != NULL, NULL);
728           g_return_val_if_fail (header->func != NULL, NULL);
729           func = header->func;
730         }
731       else
732         {
733           func = list_store->default_sort_func;
734         }
735     }
736
737   return func;
738 }
739
740 static void
741 gtk_list_store_set_vector_internal (GtkListStore *list_store,
742                                     GtkTreeIter  *iter,
743                                     gboolean     *emit_signal,
744                                     gboolean     *maybe_need_sort,
745                                     gint         *columns,
746                                     GValue       *values,
747                                     gint          n_values)
748 {
749   gint i;
750   GtkTreeIterCompareFunc func = NULL;
751
752   func = gtk_list_store_get_compare_func (list_store);
753   if (func != _gtk_tree_data_list_compare_func)
754     *maybe_need_sort = TRUE;
755
756   for (i = 0; i < n_values; i++)
757     {
758       *emit_signal = gtk_list_store_real_set_value (list_store, 
759                                                iter, 
760                                                columns[i],
761                                                &values[i],
762                                                FALSE) || *emit_signal;
763
764       if (func == _gtk_tree_data_list_compare_func &&
765           columns[i] == list_store->sort_column_id)
766         *maybe_need_sort = TRUE;
767     }
768 }
769
770 static void
771 gtk_list_store_set_valist_internal (GtkListStore *list_store,
772                                     GtkTreeIter  *iter,
773                                     gboolean     *emit_signal,
774                                     gboolean     *maybe_need_sort,
775                                     va_list       var_args)
776 {
777   gint column;
778   GtkTreeIterCompareFunc func = NULL;
779
780   column = va_arg (var_args, gint);
781
782   func = gtk_list_store_get_compare_func (list_store);
783   if (func != _gtk_tree_data_list_compare_func)
784     *maybe_need_sort = TRUE;
785
786   while (column != -1)
787     {
788       GValue value = { 0, };
789       gchar *error = NULL;
790
791       if (column < 0 || column >= list_store->n_columns)
792         {
793           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
794           break;
795         }
796       g_value_init (&value, list_store->column_headers[column]);
797
798       G_VALUE_COLLECT (&value, var_args, 0, &error);
799       if (error)
800         {
801           g_warning ("%s: %s", G_STRLOC, error);
802           g_free (error);
803
804           /* we purposely leak the value here, it might not be
805            * in a sane state if an error condition occoured
806            */
807           break;
808         }
809
810       /* FIXME: instead of calling this n times, refactor with above */
811       *emit_signal = gtk_list_store_real_set_value (list_store,
812                                                     iter,
813                                                     column,
814                                                     &value,
815                                                     FALSE) || *emit_signal;
816       
817       if (func == _gtk_tree_data_list_compare_func &&
818           column == list_store->sort_column_id)
819         *maybe_need_sort = TRUE;
820
821       g_value_unset (&value);
822
823       column = va_arg (var_args, gint);
824     }
825 }
826
827 /**
828  * gtk_list_store_set_valuesv:
829  * @list_store: A #GtkListStore
830  * @iter: A valid #GtkTreeIter for the row being modified
831  * @columns: an array of column numbers
832  * @values: an array of GValues 
833  * @n_values: the length of the @columns and @values arrays
834  * 
835  * A variant of gtk_list_store_set_valist() which
836  * takes the columns and values as two arrays, instead of
837  * varargs. This function is mainly intended for 
838  * language-bindings and in case the number of columns to
839  * change is not known until run-time.
840  *
841  * Since: 2.12
842  */
843 void
844 gtk_list_store_set_valuesv (GtkListStore *list_store,
845                             GtkTreeIter  *iter,
846                             gint         *columns,
847                             GValue       *values,
848                             gint          n_values)
849 {
850   gboolean emit_signal = FALSE;
851   gboolean maybe_need_sort = FALSE;
852
853   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
854   g_return_if_fail (VALID_ITER (iter, list_store));
855
856   gtk_list_store_set_vector_internal (list_store, iter,
857                                       &emit_signal,
858                                       &maybe_need_sort,
859                                       columns, values, n_values);
860
861   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
862     gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
863
864   if (emit_signal)
865     {
866       GtkTreePath *path;
867
868       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
869       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
870       gtk_tree_path_free (path);
871     }
872 }
873
874 /**
875  * gtk_list_store_set_valist:
876  * @list_store: A #GtkListStore
877  * @iter: A valid #GtkTreeIter for the row being modified
878  * @var_args: va_list of column/value pairs
879  *
880  * See gtk_list_store_set(); this version takes a va_list for use by language
881  * bindings.
882  *
883  **/
884 void
885 gtk_list_store_set_valist (GtkListStore *list_store,
886                            GtkTreeIter  *iter,
887                            va_list       var_args)
888 {
889   gboolean emit_signal = FALSE;
890   gboolean maybe_need_sort = FALSE;
891
892   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
893   g_return_if_fail (VALID_ITER (iter, list_store));
894
895   gtk_list_store_set_valist_internal (list_store, iter, 
896                                       &emit_signal, 
897                                       &maybe_need_sort,
898                                       var_args);
899
900   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
901     gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
902
903   if (emit_signal)
904     {
905       GtkTreePath *path;
906
907       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
908       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
909       gtk_tree_path_free (path);
910     }
911 }
912
913 /**
914  * gtk_list_store_set:
915  * @list_store: a #GtkListStore
916  * @iter: row iterator
917  * @Varargs: pairs of column number and value, terminated with -1
918  *
919  * Sets the value of one or more cells in the row referenced by @iter.
920  * The variable argument list should contain integer column numbers,
921  * each column number followed by the value to be set.
922  * The list is terminated by a -1. For example, to set column 0 with type
923  * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
924  * 0, "Foo", -1)</literal>.
925  **/
926 void
927 gtk_list_store_set (GtkListStore *list_store,
928                     GtkTreeIter  *iter,
929                     ...)
930 {
931   va_list var_args;
932
933   va_start (var_args, iter);
934   gtk_list_store_set_valist (list_store, iter, var_args);
935   va_end (var_args);
936 }
937
938 /**
939  * gtk_list_store_remove:
940  * @list_store: A #GtkListStore
941  * @iter: A valid #GtkTreeIter
942  *
943  * Removes the given row from the list store.  After being removed, 
944  * @iter is set to be the next valid row, or invalidated if it pointed 
945  * to the last row in @list_store.
946  *
947  * Return value: %TRUE if @iter is valid, %FALSE if not.
948  **/
949 gboolean
950 gtk_list_store_remove (GtkListStore *list_store,
951                        GtkTreeIter  *iter)
952 {
953   GtkTreePath *path;
954   GSequenceIter *ptr, *next;
955
956   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
957   g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
958
959   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
960
961   ptr = iter->user_data;
962   next = g_sequence_iter_next (ptr);
963   
964   _gtk_tree_data_list_free (g_sequence_get (ptr), list_store->column_headers);
965   g_sequence_remove (iter->user_data);
966
967   list_store->length--;
968   
969   gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
970   gtk_tree_path_free (path);
971
972   if (g_sequence_iter_is_end (next))
973     {
974       iter->stamp = 0;
975       return FALSE;
976     }
977   else
978     {
979       iter->stamp = list_store->stamp;
980       iter->user_data = next;
981       return TRUE;
982     }
983 }
984
985 /**
986  * gtk_list_store_insert:
987  * @list_store: A #GtkListStore
988  * @iter: An unset #GtkTreeIter to set to the new row
989  * @position: position to insert the new row
990  *
991  * Creates a new row at @position.  @iter will be changed to point to this new
992  * row.  If @position is larger than the number of rows on the list, then the
993  * new row will be appended to the list. The row will be empty after this
994  * function is called.  To fill in values, you need to call 
995  * gtk_list_store_set() or gtk_list_store_set_value().
996  *
997  **/
998 void
999 gtk_list_store_insert (GtkListStore *list_store,
1000                        GtkTreeIter  *iter,
1001                        gint          position)
1002 {
1003   GtkTreePath *path;
1004   GSequence *seq;
1005   GSequenceIter *ptr;
1006   gint length;
1007
1008   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1009   g_return_if_fail (iter != NULL);
1010   g_return_if_fail (position >= 0);
1011
1012   list_store->columns_dirty = TRUE;
1013
1014   seq = list_store->seq;
1015
1016   length = g_sequence_get_length (seq);
1017   if (position > length)
1018     position = length;
1019
1020   ptr = g_sequence_get_iter_at_pos (seq, position);
1021   ptr = g_sequence_insert_before (ptr, NULL);
1022
1023   iter->stamp = list_store->stamp;
1024   iter->user_data = ptr;
1025
1026   g_assert (VALID_ITER (iter, list_store));
1027
1028   list_store->length++;
1029   
1030   path = gtk_tree_path_new ();
1031   gtk_tree_path_append_index (path, position);
1032   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1033   gtk_tree_path_free (path);
1034 }
1035
1036 /**
1037  * gtk_list_store_insert_before:
1038  * @list_store: A #GtkListStore
1039  * @iter: An unset #GtkTreeIter to set to the new row
1040  * @sibling: A valid #GtkTreeIter, or %NULL
1041  *
1042  * Inserts a new row before @sibling. If @sibling is %NULL, then the row will 
1043  * be appended to the end of the list. @iter will be changed to point to this 
1044  * new row. The row will be empty after this function is called. To fill in 
1045  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1046  *
1047  **/
1048 void
1049 gtk_list_store_insert_before (GtkListStore *list_store,
1050                               GtkTreeIter  *iter,
1051                               GtkTreeIter  *sibling)
1052 {
1053   GSequenceIter *after;
1054   
1055   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1056   g_return_if_fail (iter != NULL);
1057   if (sibling)
1058     g_return_if_fail (VALID_ITER (sibling, list_store));
1059
1060   if (!sibling)
1061     after = g_sequence_get_end_iter (list_store->seq);
1062   else
1063     after = sibling->user_data;
1064
1065   gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1066 }
1067
1068 /**
1069  * gtk_list_store_insert_after:
1070  * @list_store: A #GtkListStore
1071  * @iter: An unset #GtkTreeIter to set to the new row
1072  * @sibling: A valid #GtkTreeIter, or %NULL
1073  *
1074  * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1075  * prepended to the beginning of the list. @iter will be changed to point to
1076  * this new row. The row will be empty after this function is called. To fill
1077  * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1078  *
1079  **/
1080 void
1081 gtk_list_store_insert_after (GtkListStore *list_store,
1082                              GtkTreeIter  *iter,
1083                              GtkTreeIter  *sibling)
1084 {
1085   GSequenceIter *after;
1086
1087   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1088   g_return_if_fail (iter != NULL);
1089   if (sibling)
1090     g_return_if_fail (VALID_ITER (sibling, list_store));
1091
1092   if (!sibling)
1093     after = g_sequence_get_begin_iter (list_store->seq);
1094   else
1095     after = g_sequence_iter_next (sibling->user_data);
1096
1097   gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1098 }
1099
1100 /**
1101  * gtk_list_store_prepend:
1102  * @list_store: A #GtkListStore
1103  * @iter: An unset #GtkTreeIter to set to the prepend row
1104  *
1105  * Prepends a new row to @list_store. @iter will be changed to point to this new
1106  * row. The row will be empty after this function is called. To fill in
1107  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1108  *
1109  **/
1110 void
1111 gtk_list_store_prepend (GtkListStore *list_store,
1112                         GtkTreeIter  *iter)
1113 {
1114   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1115   g_return_if_fail (iter != NULL);
1116
1117   gtk_list_store_insert (list_store, iter, 0);
1118 }
1119
1120 /**
1121  * gtk_list_store_append:
1122  * @list_store: A #GtkListStore
1123  * @iter: An unset #GtkTreeIter to set to the appended row
1124  *
1125  * Appends a new row to @list_store.  @iter will be changed to point to this new
1126  * row.  The row will be empty after this function is called.  To fill in
1127  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1128  *
1129  **/
1130 void
1131 gtk_list_store_append (GtkListStore *list_store,
1132                        GtkTreeIter  *iter)
1133 {
1134   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1135   g_return_if_fail (iter != NULL);
1136
1137   gtk_list_store_insert (list_store, iter, g_sequence_get_length (list_store->seq));
1138 }
1139
1140 static void
1141 gtk_list_store_increment_stamp (GtkListStore *list_store)
1142 {
1143   do
1144     {
1145       list_store->stamp++;
1146     }
1147   while (list_store->stamp == 0);
1148 }
1149
1150 /**
1151  * gtk_list_store_clear:
1152  * @list_store: a #GtkListStore.
1153  *
1154  * Removes all rows from the list store.  
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 (g_sequence_get_length (list_store->seq) > 0)
1164     {
1165       iter.stamp = list_store->stamp;
1166       iter.user_data = g_sequence_get_begin_iter (list_store->seq);
1167       gtk_list_store_remove (list_store, &iter);
1168     }
1169
1170   gtk_list_store_increment_stamp (list_store);
1171 }
1172
1173 /**
1174  * gtk_list_store_iter_is_valid:
1175  * @list_store: A #GtkListStore.
1176  * @iter: A #GtkTreeIter.
1177  *
1178  * <warning>This function is slow. Only use it for debugging and/or testing
1179  * purposes.</warning>
1180  *
1181  * Checks if the given iter is a valid iter for this #GtkListStore.
1182  *
1183  * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1184  *
1185  * Since: 2.2
1186  **/
1187 gboolean
1188 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1189                               GtkTreeIter  *iter)
1190 {
1191   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1192   g_return_val_if_fail (iter != NULL, FALSE);
1193
1194   if (!VALID_ITER (iter, list_store))
1195     return FALSE;
1196
1197   if (g_sequence_iter_get_sequence (iter->user_data) != list_store->seq)
1198     return FALSE;
1199
1200   return TRUE;
1201 }
1202
1203 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1204                                                    GtkTreePath       *path)
1205 {
1206   return TRUE;
1207 }
1208   
1209 static gboolean
1210 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1211                                  GtkTreePath       *path)
1212 {
1213   GtkTreeIter iter;
1214
1215   if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1216                                &iter,
1217                                path))
1218     {
1219       gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1220       return TRUE;
1221     }
1222   return FALSE;
1223 }
1224
1225 static gboolean
1226 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1227                               GtkTreePath       *path,
1228                               GtkSelectionData  *selection_data)
1229 {
1230   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1231    * target, because the default handler does it for us, but
1232    * we do anyway for the convenience of someone maybe overriding the
1233    * default handler.
1234    */
1235
1236   if (gtk_tree_set_row_drag_data (selection_data,
1237                                   GTK_TREE_MODEL (drag_source),
1238                                   path))
1239     {
1240       return TRUE;
1241     }
1242   else
1243     {
1244       /* FIXME handle text targets at least. */
1245     }
1246
1247   return FALSE;
1248 }
1249
1250 static gboolean
1251 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1252                                    GtkTreePath       *dest,
1253                                    GtkSelectionData  *selection_data)
1254 {
1255   GtkTreeModel *tree_model;
1256   GtkListStore *list_store;
1257   GtkTreeModel *src_model = NULL;
1258   GtkTreePath *src_path = NULL;
1259   gboolean retval = FALSE;
1260
1261   tree_model = GTK_TREE_MODEL (drag_dest);
1262   list_store = GTK_LIST_STORE (drag_dest);
1263
1264   if (gtk_tree_get_row_drag_data (selection_data,
1265                                   &src_model,
1266                                   &src_path) &&
1267       src_model == tree_model)
1268     {
1269       /* Copy the given row to a new position */
1270       GtkTreeIter src_iter;
1271       GtkTreeIter dest_iter;
1272       GtkTreePath *prev;
1273
1274       if (!gtk_list_store_get_iter (src_model,
1275                                     &src_iter,
1276                                     src_path))
1277         {
1278           goto out;
1279         }
1280
1281       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1282       prev = gtk_tree_path_copy (dest);
1283
1284       if (!gtk_tree_path_prev (prev))
1285         {
1286           /* dest was the first spot in the list; which means we are supposed
1287            * to prepend.
1288            */
1289           gtk_list_store_prepend (list_store, &dest_iter);
1290
1291           retval = TRUE;
1292         }
1293       else
1294         {
1295           if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1296             {
1297               GtkTreeIter tmp_iter = dest_iter;
1298
1299               gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1300
1301               retval = TRUE;
1302             }
1303         }
1304
1305       gtk_tree_path_free (prev);
1306
1307       /* If we succeeded in creating dest_iter, copy data from src
1308        */
1309       if (retval)
1310         {
1311           GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1312           GtkTreeDataList *copy_head = NULL;
1313           GtkTreeDataList *copy_prev = NULL;
1314           GtkTreeDataList *copy_iter = NULL;
1315           GtkTreePath *path;
1316           gint col;
1317
1318           col = 0;
1319           while (dl)
1320             {
1321               copy_iter = _gtk_tree_data_list_node_copy (dl,
1322                                                          list_store->column_headers[col]);
1323
1324               if (copy_head == NULL)
1325                 copy_head = copy_iter;
1326
1327               if (copy_prev)
1328                 copy_prev->next = copy_iter;
1329
1330               copy_prev = copy_iter;
1331
1332               dl = dl->next;
1333               ++col;
1334             }
1335
1336           dest_iter.stamp = list_store->stamp;
1337           g_sequence_set (dest_iter.user_data, copy_head);
1338
1339           path = gtk_list_store_get_path (tree_model, &dest_iter);
1340           gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1341           gtk_tree_path_free (path);
1342         }
1343     }
1344   else
1345     {
1346       /* FIXME maybe add some data targets eventually, or handle text
1347        * targets in the simple case.
1348        */
1349     }
1350
1351  out:
1352
1353   if (src_path)
1354     gtk_tree_path_free (src_path);
1355
1356   return retval;
1357 }
1358
1359 static gboolean
1360 gtk_list_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
1361                                   GtkTreePath      *dest_path,
1362                                   GtkSelectionData *selection_data)
1363 {
1364   gint *indices;
1365   GtkTreeModel *src_model = NULL;
1366   GtkTreePath *src_path = NULL;
1367   gboolean retval = FALSE;
1368
1369   /* don't accept drops if the list has been sorted */
1370   if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1371     return FALSE;
1372
1373   if (!gtk_tree_get_row_drag_data (selection_data,
1374                                    &src_model,
1375                                    &src_path))
1376     goto out;
1377
1378   if (src_model != GTK_TREE_MODEL (drag_dest))
1379     goto out;
1380
1381   if (gtk_tree_path_get_depth (dest_path) != 1)
1382     goto out;
1383
1384   /* can drop before any existing node, or before one past any existing. */
1385
1386   indices = gtk_tree_path_get_indices (dest_path);
1387
1388   if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->seq))
1389     retval = TRUE;
1390
1391  out:
1392   if (src_path)
1393     gtk_tree_path_free (src_path);
1394   
1395   return retval;
1396 }
1397
1398 /* Sorting and reordering */
1399
1400 /* Reordering */
1401 static gint
1402 gtk_list_store_reorder_func (GSequenceIter *a,
1403                              GSequenceIter *b,
1404                              gpointer       user_data)
1405 {
1406   GHashTable *new_positions = user_data;
1407   gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1408   gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1409
1410   if (apos < bpos)
1411     return -1;
1412   if (apos > bpos)
1413     return 1;
1414   return 0;
1415 }
1416   
1417 /**
1418  * gtk_list_store_reorder:
1419  * @store: A #GtkListStore.
1420  * @new_order: an array of integers mapping the new position of each child
1421  *      to its old position before the re-ordering,
1422  *      i.e. @new_order<literal>[newpos] = oldpos</literal>.
1423  *
1424  * Reorders @store to follow the order indicated by @new_order. Note that
1425  * this function only works with unsorted stores.
1426  *
1427  * Since: 2.2
1428  **/
1429 void
1430 gtk_list_store_reorder (GtkListStore *store,
1431                         gint         *new_order)
1432 {
1433   gint i;
1434   GtkTreePath *path;
1435   GHashTable *new_positions;
1436   GSequenceIter *ptr;
1437   gint *order;
1438   
1439   g_return_if_fail (GTK_IS_LIST_STORE (store));
1440   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1441   g_return_if_fail (new_order != NULL);
1442
1443   order = g_new (gint, g_sequence_get_length (store->seq));
1444   for (i = 0; i < g_sequence_get_length (store->seq); i++)
1445     order[new_order[i]] = i;
1446   
1447   new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1448
1449   ptr = g_sequence_get_begin_iter (store->seq);
1450   i = 0;
1451   while (!g_sequence_iter_is_end (ptr))
1452     {
1453       g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1454
1455       ptr = g_sequence_iter_next (ptr);
1456     }
1457   g_free (order);
1458   
1459   g_sequence_sort_iter (store->seq, gtk_list_store_reorder_func, new_positions);
1460
1461   g_hash_table_destroy (new_positions);
1462   
1463   /* emit signal */
1464   path = gtk_tree_path_new ();
1465   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1466                                  path, NULL, new_order);
1467   gtk_tree_path_free (path);
1468 }
1469
1470 static GHashTable *
1471 save_positions (GSequence *seq)
1472 {
1473   GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1474   GSequenceIter *ptr;
1475
1476   ptr = g_sequence_get_begin_iter (seq);
1477   while (!g_sequence_iter_is_end (ptr))
1478     {
1479       g_hash_table_insert (positions, ptr,
1480                            GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1481       ptr = g_sequence_iter_next (ptr);
1482     }
1483
1484   return positions;
1485 }
1486
1487 static int *
1488 generate_order (GSequence *seq,
1489                 GHashTable *old_positions)
1490 {
1491   GSequenceIter *ptr;
1492   int *order = g_new (int, g_sequence_get_length (seq));
1493   int i;
1494
1495   i = 0;
1496   ptr = g_sequence_get_begin_iter (seq);
1497   while (!g_sequence_iter_is_end (ptr))
1498     {
1499       int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1500       order[i++] = old_pos;
1501       ptr = g_sequence_iter_next (ptr);
1502     }
1503
1504   g_hash_table_destroy (old_positions);
1505
1506   return order;
1507 }
1508
1509 /**
1510  * gtk_list_store_swap:
1511  * @store: A #GtkListStore.
1512  * @a: A #GtkTreeIter.
1513  * @b: Another #GtkTreeIter.
1514  *
1515  * Swaps @a and @b in @store. Note that this function only works with
1516  * unsorted stores.
1517  *
1518  * Since: 2.2
1519  **/
1520 void
1521 gtk_list_store_swap (GtkListStore *store,
1522                      GtkTreeIter  *a,
1523                      GtkTreeIter  *b)
1524 {
1525   GHashTable *old_positions;
1526   gint *order;
1527   GtkTreePath *path;
1528
1529   g_return_if_fail (GTK_IS_LIST_STORE (store));
1530   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1531   g_return_if_fail (VALID_ITER (a, store));
1532   g_return_if_fail (VALID_ITER (b, store));
1533
1534   if (a->user_data == b->user_data)
1535     return;
1536
1537   old_positions = save_positions (store->seq);
1538   
1539   g_sequence_swap (a->user_data, b->user_data);
1540
1541   order = generate_order (store->seq, old_positions);
1542   path = gtk_tree_path_new ();
1543   
1544   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1545                                  path, NULL, order);
1546
1547   gtk_tree_path_free (path);
1548   g_free (order);
1549 }
1550
1551 static void
1552 gtk_list_store_move_to (GtkListStore *store,
1553                         GtkTreeIter  *iter,
1554                         gint          new_pos)
1555 {
1556   GHashTable *old_positions;
1557   GtkTreePath *path;
1558   gint *order;
1559   
1560   old_positions = save_positions (store->seq);
1561   
1562   g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (store->seq, new_pos));
1563
1564   order = generate_order (store->seq, old_positions);
1565   
1566   path = gtk_tree_path_new ();
1567   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1568                                  path, NULL, order);
1569   gtk_tree_path_free (path);
1570   g_free (order);
1571 }
1572
1573 /**
1574  * gtk_list_store_move_before:
1575  * @store: A #GtkListStore.
1576  * @iter: A #GtkTreeIter.
1577  * @position: A #GtkTreeIter, or %NULL.
1578  *
1579  * Moves @iter in @store to the position before @position. Note that this
1580  * function only works with unsorted stores. If @position is %NULL, @iter
1581  * will be moved to the end of the list.
1582  *
1583  * Since: 2.2
1584  **/
1585 void
1586 gtk_list_store_move_before (GtkListStore *store,
1587                             GtkTreeIter  *iter,
1588                             GtkTreeIter  *position)
1589 {
1590   gint pos;
1591   
1592   g_return_if_fail (GTK_IS_LIST_STORE (store));
1593   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1594   g_return_if_fail (VALID_ITER (iter, store));
1595   if (position)
1596     g_return_if_fail (VALID_ITER (position, store));
1597
1598   if (position)
1599     pos = g_sequence_iter_get_position (position->user_data);
1600   else
1601     pos = -1;
1602   
1603   gtk_list_store_move_to (store, iter, pos);
1604 }
1605
1606 /**
1607  * gtk_list_store_move_after:
1608  * @store: A #GtkListStore.
1609  * @iter: A #GtkTreeIter.
1610  * @position: A #GtkTreeIter or %NULL.
1611  *
1612  * Moves @iter in @store to the position after @position. Note that this
1613  * function only works with unsorted stores. If @position is %NULL, @iter
1614  * will be moved to the start of the list.
1615  *
1616  * Since: 2.2
1617  **/
1618 void
1619 gtk_list_store_move_after (GtkListStore *store,
1620                            GtkTreeIter  *iter,
1621                            GtkTreeIter  *position)
1622 {
1623   gint pos;
1624   
1625   g_return_if_fail (GTK_IS_LIST_STORE (store));
1626   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1627   g_return_if_fail (VALID_ITER (iter, store));
1628   if (position)
1629     g_return_if_fail (VALID_ITER (position, store));
1630
1631   if (position)
1632     pos = g_sequence_iter_get_position (position->user_data) + 1;
1633   else
1634     pos = 0;
1635   
1636   gtk_list_store_move_to (store, iter, pos);
1637 }
1638     
1639 /* Sorting */
1640 static gint
1641 gtk_list_store_compare_func (GSequenceIter *a,
1642                              GSequenceIter *b,
1643                              gpointer      user_data)
1644 {
1645   GtkListStore *list_store = user_data;
1646   GtkTreeIter iter_a;
1647   GtkTreeIter iter_b;
1648   gint retval;
1649   GtkTreeIterCompareFunc func;
1650   gpointer data;
1651
1652   if (list_store->sort_column_id != -1)
1653     {
1654       GtkTreeDataSortHeader *header;
1655
1656       header = _gtk_tree_data_list_get_header (list_store->sort_list,
1657                                                list_store->sort_column_id);
1658       g_return_val_if_fail (header != NULL, 0);
1659       g_return_val_if_fail (header->func != NULL, 0);
1660
1661       func = header->func;
1662       data = header->data;
1663     }
1664   else
1665     {
1666       g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1667       func = list_store->default_sort_func;
1668       data = list_store->default_sort_data;
1669     }
1670
1671   iter_a.stamp = list_store->stamp;
1672   iter_a.user_data = (gpointer)a;
1673   iter_b.stamp = list_store->stamp;
1674   iter_b.user_data = (gpointer)b;
1675
1676   g_assert (VALID_ITER (&iter_a, list_store));
1677   g_assert (VALID_ITER (&iter_b, list_store));
1678   
1679   retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1680
1681   if (list_store->order == GTK_SORT_DESCENDING)
1682     {
1683       if (retval > 0)
1684         retval = -1;
1685       else if (retval < 0)
1686         retval = 1;
1687     }
1688
1689   return retval;
1690 }
1691
1692 static void
1693 gtk_list_store_sort (GtkListStore *list_store)
1694 {
1695   gint *new_order;
1696   GtkTreePath *path;
1697   GHashTable *old_positions;
1698
1699   if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1700       g_sequence_get_length (list_store->seq) <= 1)
1701     return;
1702
1703   old_positions = save_positions (list_store->seq);
1704
1705   g_sequence_sort_iter (list_store->seq, gtk_list_store_compare_func, list_store);
1706
1707   /* Let the world know about our new order */
1708   new_order = generate_order (list_store->seq, old_positions);
1709
1710   path = gtk_tree_path_new ();
1711   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1712                                  path, NULL, new_order);
1713   gtk_tree_path_free (path);
1714   g_free (new_order);
1715 }
1716
1717 static gboolean
1718 iter_is_sorted (GtkListStore *list_store,
1719                 GtkTreeIter  *iter)
1720 {
1721   GSequenceIter *cmp;
1722
1723   if (!g_sequence_iter_is_begin (iter->user_data))
1724     {
1725       cmp = g_sequence_iter_prev (iter->user_data);
1726       if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
1727         return FALSE;
1728     }
1729
1730   cmp = g_sequence_iter_next (iter->user_data);
1731   if (!g_sequence_iter_is_end (cmp))
1732     {
1733       if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
1734         return FALSE;
1735     }
1736   
1737   return TRUE;
1738 }
1739
1740 static void
1741 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1742                                   GtkTreeIter  *iter,
1743                                   gint          column)
1744
1745 {
1746   GtkTreePath *path;
1747
1748   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1749   gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1750   gtk_tree_path_free (path);
1751
1752   if (!iter_is_sorted (list_store, iter))
1753     {
1754       GHashTable *old_positions;
1755       gint *order;
1756
1757       old_positions = save_positions (list_store->seq);
1758       g_sequence_sort_changed_iter (iter->user_data,
1759                                     gtk_list_store_compare_func,
1760                                     list_store);
1761       order = generate_order (list_store->seq, old_positions);
1762       path = gtk_tree_path_new ();
1763       gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1764                                      path, NULL, order);
1765       gtk_tree_path_free (path);
1766       g_free (order);
1767     }
1768 }
1769
1770 static gboolean
1771 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1772                                    gint             *sort_column_id,
1773                                    GtkSortType      *order)
1774 {
1775   GtkListStore *list_store = (GtkListStore *) sortable;
1776
1777   if (sort_column_id)
1778     * sort_column_id = list_store->sort_column_id;
1779   if (order)
1780     * order = list_store->order;
1781
1782   if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
1783       list_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1784     return FALSE;
1785
1786   return TRUE;
1787 }
1788
1789 static void
1790 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1791                                    gint              sort_column_id,
1792                                    GtkSortType       order)
1793 {
1794   GtkListStore *list_store = (GtkListStore *) sortable;
1795
1796   if ((list_store->sort_column_id == sort_column_id) &&
1797       (list_store->order == order))
1798     return;
1799
1800   if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1801     {
1802       if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1803         {
1804           GtkTreeDataSortHeader *header = NULL;
1805
1806           header = _gtk_tree_data_list_get_header (list_store->sort_list, 
1807                                                    sort_column_id);
1808
1809           /* We want to make sure that we have a function */
1810           g_return_if_fail (header != NULL);
1811           g_return_if_fail (header->func != NULL);
1812         }
1813       else
1814         {
1815           g_return_if_fail (list_store->default_sort_func != NULL);
1816         }
1817     }
1818
1819
1820   list_store->sort_column_id = sort_column_id;
1821   list_store->order = order;
1822
1823   gtk_tree_sortable_sort_column_changed (sortable);
1824
1825   gtk_list_store_sort (list_store);
1826 }
1827
1828 static void
1829 gtk_list_store_set_sort_func (GtkTreeSortable        *sortable,
1830                               gint                    sort_column_id,
1831                               GtkTreeIterCompareFunc  func,
1832                               gpointer                data,
1833                               GDestroyNotify          destroy)
1834 {
1835   GtkListStore *list_store = (GtkListStore *) sortable;
1836
1837   list_store->sort_list = _gtk_tree_data_list_set_header (list_store->sort_list, 
1838                                                           sort_column_id, 
1839                                                           func, data, destroy);
1840
1841   if (list_store->sort_column_id == sort_column_id)
1842     gtk_list_store_sort (list_store);
1843 }
1844
1845 static void
1846 gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
1847                                       GtkTreeIterCompareFunc  func,
1848                                       gpointer                data,
1849                                       GDestroyNotify          destroy)
1850 {
1851   GtkListStore *list_store = (GtkListStore *) sortable;
1852
1853   if (list_store->default_sort_destroy)
1854     {
1855       GDestroyNotify d = list_store->default_sort_destroy;
1856
1857       list_store->default_sort_destroy = NULL;
1858       d (list_store->default_sort_data);
1859     }
1860
1861   list_store->default_sort_func = func;
1862   list_store->default_sort_data = data;
1863   list_store->default_sort_destroy = destroy;
1864
1865   if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1866     gtk_list_store_sort (list_store);
1867 }
1868
1869 static gboolean
1870 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1871 {
1872   GtkListStore *list_store = (GtkListStore *) sortable;
1873
1874   return (list_store->default_sort_func != NULL);
1875 }
1876
1877
1878 /**
1879  * gtk_list_store_insert_with_values:
1880  * @list_store: A #GtkListStore
1881  * @iter: An unset #GtkTreeIter to set to the new row, or %NULL.
1882  * @position: position to insert the new row
1883  * @Varargs: pairs of column number and value, terminated with -1
1884  *
1885  * Creates a new row at @position.  @iter will be changed to point to this new
1886  * row.  If @position is larger than the number of rows on the list, then the
1887  * new row will be appended to the list. The row will be filled with the 
1888  * values given to this function. 
1889  * 
1890  * Calling
1891  * <literal>gtk_list_store_insert_with_values(list_store, iter, position...)</literal> 
1892  * has the same effect as calling 
1893  * |[
1894  * gtk_list_store_insert (list_store, iter, position);
1895  * gtk_list_store_set (list_store, iter, ...);
1896  * ]|
1897  * with the difference that the former will only emit a row_inserted signal,
1898  * while the latter will emit row_inserted, row_changed and, if the list store
1899  * is sorted, rows_reordered. Since emitting the rows_reordered signal
1900  * repeatedly can affect the performance of the program, 
1901  * gtk_list_store_insert_with_values() should generally be preferred when
1902  * inserting rows in a sorted list store.
1903  *
1904  * Since: 2.6
1905  */
1906 void
1907 gtk_list_store_insert_with_values (GtkListStore *list_store,
1908                                    GtkTreeIter  *iter,
1909                                    gint          position,
1910                                    ...)
1911 {
1912   GtkTreePath *path;
1913   GSequence *seq;
1914   GSequenceIter *ptr;
1915   GtkTreeIter tmp_iter;
1916   gint length;
1917   gboolean changed = FALSE;
1918   gboolean maybe_need_sort = FALSE;
1919   va_list var_args;
1920
1921   /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
1922   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1923
1924   if (!iter)
1925     iter = &tmp_iter;
1926
1927   list_store->columns_dirty = TRUE;
1928
1929   seq = list_store->seq;
1930
1931   length = g_sequence_get_length (seq);
1932   if (position > length)
1933     position = length;
1934
1935   ptr = g_sequence_get_iter_at_pos (seq, position);
1936   ptr = g_sequence_insert_before (ptr, NULL);
1937
1938   iter->stamp = list_store->stamp;
1939   iter->user_data = ptr;
1940
1941   g_assert (VALID_ITER (iter, list_store));
1942
1943   list_store->length++;  
1944
1945   va_start (var_args, position);
1946   gtk_list_store_set_valist_internal (list_store, iter, 
1947                                       &changed, &maybe_need_sort,
1948                                       var_args);
1949   va_end (var_args);
1950
1951   /* Don't emit rows_reordered here */
1952   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1953     g_sequence_sort_changed_iter (iter->user_data,
1954                                   gtk_list_store_compare_func,
1955                                   list_store);
1956
1957   /* Just emit row_inserted */
1958   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1959   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1960   gtk_tree_path_free (path);
1961 }
1962
1963
1964 /**
1965  * gtk_list_store_insert_with_valuesv:
1966  * @list_store: A #GtkListStore
1967  * @iter: An unset #GtkTreeIter to set to the new row, or %NULL.
1968  * @position: position to insert the new row
1969  * @columns: an array of column numbers
1970  * @values: an array of GValues 
1971  * @n_values: the length of the @columns and @values arrays
1972  * 
1973  * A variant of gtk_list_store_insert_with_values() which
1974  * takes the columns and values as two arrays, instead of
1975  * varargs. This function is mainly intended for 
1976  * language-bindings.
1977  *
1978  * Since: 2.6
1979  */
1980 void
1981 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
1982                                     GtkTreeIter  *iter,
1983                                     gint          position,
1984                                     gint         *columns, 
1985                                     GValue       *values,
1986                                     gint          n_values)
1987 {
1988   GtkTreePath *path;
1989   GSequence *seq;
1990   GSequenceIter *ptr;
1991   GtkTreeIter tmp_iter;
1992   gint length;
1993   gboolean changed = FALSE;
1994   gboolean maybe_need_sort = FALSE;
1995
1996   /* FIXME refactor to reduce overlap with 
1997    * gtk_list_store_insert_with_values() 
1998    */
1999   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2000
2001   if (!iter)
2002     iter = &tmp_iter;
2003
2004   list_store->columns_dirty = TRUE;
2005
2006   seq = list_store->seq;
2007
2008   length = g_sequence_get_length (seq);
2009   if (position > length)
2010     position = length;
2011
2012   ptr = g_sequence_get_iter_at_pos (seq, position);
2013   ptr = g_sequence_insert_before (ptr, NULL);
2014
2015   iter->stamp = list_store->stamp;
2016   iter->user_data = ptr;
2017
2018   g_assert (VALID_ITER (iter, list_store));
2019
2020   list_store->length++;  
2021
2022   gtk_list_store_set_vector_internal (list_store, iter,
2023                                       &changed, &maybe_need_sort,
2024                                       columns, values, n_values);
2025
2026   /* Don't emit rows_reordered here */
2027   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2028     g_sequence_sort_changed_iter (iter->user_data,
2029                                   gtk_list_store_compare_func,
2030                                   list_store);
2031
2032   /* Just emit row_inserted */
2033   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2034   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2035   gtk_tree_path_free (path);
2036 }
2037
2038 /* GtkBuildable custom tag implementation
2039  *
2040  * <columns>
2041  *   <column type="..."/>
2042  *   <column type="..."/>
2043  * </columns>
2044  */
2045 typedef struct {
2046   gboolean translatable;
2047   gchar *context;
2048   int id;
2049 } ColInfo;
2050
2051 typedef struct {
2052   GtkBuilder *builder;
2053   GObject *object;
2054   GSList *column_type_names;
2055   GType *column_types;
2056   GValue *values;
2057   gint *colids;
2058   ColInfo **columns;
2059   gint last_row;
2060   gint n_columns;
2061   gint row_column;
2062   GQuark error_quark;
2063   gboolean is_data;
2064   const gchar *domain;
2065 } SubParserData;
2066
2067 static void
2068 list_store_start_element (GMarkupParseContext *context,
2069                           const gchar         *element_name,
2070                           const gchar        **names,
2071                           const gchar        **values,
2072                           gpointer             user_data,
2073                           GError             **error)
2074 {
2075   guint i;
2076   SubParserData *data = (SubParserData*)user_data;
2077
2078   if (strcmp (element_name, "col") == 0)
2079     {
2080       int i, id = -1;
2081       gchar *context = NULL;
2082       gboolean translatable = FALSE;
2083       ColInfo *info;
2084
2085       if (data->row_column >= data->n_columns)
2086         {
2087           g_set_error (error, data->error_quark, 0,
2088                        "Too many columns, maximum is %d\n", data->n_columns - 1);
2089           return;
2090         }
2091
2092       for (i = 0; names[i]; i++)
2093         if (strcmp (names[i], "id") == 0)
2094           {
2095             errno = 0;
2096             id = atoi (values[i]);
2097             if (errno)
2098               {
2099                 g_set_error (error, data->error_quark, 0,
2100                              "the id tag %s could not be converted to an integer",
2101                              values[i]);
2102                 return;
2103               }
2104             if (id < 0 || id >= data->n_columns)
2105               {
2106                 g_set_error (error, data->error_quark, 0,
2107                              "id value %d out of range", id);
2108                 return;
2109               }
2110           }
2111         else if (strcmp (names[i], "translatable") == 0)
2112           {
2113             if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2114                                                    error))
2115               return;
2116           }
2117         else if (strcmp (names[i], "comments") == 0)
2118           {
2119             /* do nothing, comments are for translators */
2120           }
2121         else if (strcmp (names[i], "context") == 0) 
2122           {
2123             context = g_strdup (values[i]);
2124           }
2125
2126       if (id == -1)
2127         {
2128           g_set_error (error, data->error_quark, 0,
2129                        "<col> needs an id attribute");
2130           return;
2131         }
2132       
2133       info = g_slice_new0 (ColInfo);
2134       info->translatable = translatable;
2135       info->context = context;
2136       info->id = id;
2137
2138       data->colids[data->row_column] = id;
2139       data->columns[data->row_column] = info;
2140       data->row_column++;
2141       data->is_data = TRUE;
2142     }
2143   else if (strcmp (element_name, "row") == 0)
2144     ;
2145   else if (strcmp (element_name, "column") == 0)
2146     for (i = 0; names[i]; i++)
2147       if (strcmp (names[i], "type") == 0)
2148         data->column_type_names = g_slist_prepend (data->column_type_names,
2149                                                    g_strdup (values[i]));
2150   else if (strcmp (element_name, "columns") == 0)
2151     ;
2152   else if (strcmp (element_name, "data") == 0)
2153     ;
2154   else
2155     g_set_error (error, data->error_quark, 0,
2156                  "Unknown start tag: %s", element_name);
2157 }
2158
2159 static void
2160 list_store_end_element (GMarkupParseContext *context,
2161                         const gchar         *element_name,
2162                         gpointer             user_data,
2163                         GError             **error)
2164 {
2165   SubParserData *data = (SubParserData*)user_data;
2166
2167   g_assert (data->builder);
2168   
2169   if (strcmp (element_name, "row") == 0)
2170     {
2171       GtkTreeIter iter;
2172       int i;
2173
2174       gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2175                                           &iter,
2176                                           data->last_row,
2177                                           data->colids,
2178                                           data->values,
2179                                           data->row_column);
2180       for (i = 0; i < data->row_column; i++)
2181         {
2182           ColInfo *info = data->columns[i];
2183           g_free (info->context);
2184           g_slice_free (ColInfo, info);
2185           data->columns[i] = NULL;
2186           g_value_unset (&data->values[i]);
2187         }
2188       g_free (data->values);
2189       data->values = g_new0 (GValue, data->n_columns);
2190       data->last_row++;
2191       data->row_column = 0;
2192     }
2193   else if (strcmp (element_name, "columns") == 0)
2194     {
2195       GType *column_types;
2196       GSList *l;
2197       int i;
2198       GType type;
2199
2200       data->column_type_names = g_slist_reverse (data->column_type_names);
2201       column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2202
2203       for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2204         {
2205           type = gtk_builder_get_type_from_name (data->builder, l->data);
2206           if (type == G_TYPE_INVALID)
2207             {
2208               g_warning ("Unknown type %s specified in treemodel %s",
2209                          (const gchar*)l->data,
2210                          gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2211               continue;
2212             }
2213           column_types[i] = type;
2214
2215           g_free (l->data);
2216         }
2217
2218       gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2219                                        column_types);
2220
2221       g_free (column_types);
2222     }
2223   else if (strcmp (element_name, "col") == 0)
2224     data->is_data = FALSE;
2225   else if (strcmp (element_name, "data") == 0)
2226     ;
2227   else if (strcmp (element_name, "column") == 0)
2228     ;
2229   else
2230     g_set_error (error, data->error_quark, 0,
2231                  "Unknown end tag: %s", element_name);
2232 }
2233
2234 static void
2235 list_store_text (GMarkupParseContext *context,
2236                  const gchar         *text,
2237                  gsize                text_len,
2238                  gpointer             user_data,
2239                  GError             **error)
2240 {
2241   SubParserData *data = (SubParserData*)user_data;
2242   gint i;
2243   GError *tmp_error = NULL;
2244   gchar *string;
2245   ColInfo *info;
2246   
2247   if (!data->is_data)
2248     return;
2249
2250   i = data->row_column - 1;
2251   info = data->columns[i];
2252
2253   string = g_strndup (text, text_len);
2254   if (info->translatable && text_len)
2255     {
2256       gchar *translated;
2257
2258       /* FIXME: This will not use the domain set in the .ui file,
2259        * since the parser is not telling the builder about the domain.
2260        * However, it will work for gtk_builder_set_translation_domain() calls.
2261        */
2262       translated = _gtk_builder_parser_translate (data->domain,
2263                                                   info->context,
2264                                                   string);
2265       g_free (string);
2266       string = translated;
2267     }
2268
2269   if (!gtk_builder_value_from_string_type (data->builder,
2270                                            data->column_types[info->id],
2271                                            string,
2272                                            &data->values[i],
2273                                            &tmp_error))
2274     {
2275       g_set_error (error,
2276                    tmp_error->domain,
2277                    tmp_error->code,
2278                    "Could not convert '%s' to type %s: %s\n",
2279                    text, g_type_name (data->column_types[info->id]),
2280                    tmp_error->message);
2281       g_error_free (tmp_error);
2282     }
2283   g_free (string);
2284 }
2285
2286 static const GMarkupParser list_store_parser =
2287   {
2288     list_store_start_element,
2289     list_store_end_element,
2290     list_store_text
2291   };
2292
2293 static gboolean
2294 gtk_list_store_buildable_custom_tag_start (GtkBuildable  *buildable,
2295                                            GtkBuilder    *builder,
2296                                            GObject       *child,
2297                                            const gchar   *tagname,
2298                                            GMarkupParser *parser,
2299                                            gpointer      *data)
2300 {
2301   SubParserData *parser_data;
2302
2303   if (child)
2304     return FALSE;
2305
2306   if (strcmp (tagname, "columns") == 0)
2307     {
2308
2309       parser_data = g_slice_new0 (SubParserData);
2310       parser_data->builder = builder;
2311       parser_data->object = G_OBJECT (buildable);
2312       parser_data->column_type_names = NULL;
2313
2314       *parser = list_store_parser;
2315       *data = parser_data;
2316       return TRUE;
2317     }
2318   else if (strcmp (tagname, "data") == 0)
2319     {
2320       gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2321       if (n_columns == 0)
2322         g_error ("Cannot append data to an empty model");
2323
2324       parser_data = g_slice_new0 (SubParserData);
2325       parser_data->builder = builder;
2326       parser_data->object = G_OBJECT (buildable);
2327       parser_data->values = g_new0 (GValue, n_columns);
2328       parser_data->colids = g_new0 (gint, n_columns);
2329       parser_data->columns = g_new0 (ColInfo*, n_columns);
2330       parser_data->column_types = GTK_LIST_STORE (buildable)->column_headers;
2331       parser_data->n_columns = n_columns;
2332       parser_data->last_row = 0;
2333       parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2334       parser_data->domain = gtk_builder_get_translation_domain (builder);
2335       
2336       *parser = list_store_parser;
2337       *data = parser_data;
2338       return TRUE;
2339     }
2340   else
2341     g_warning ("Unknown custom list store tag: %s", tagname);
2342   
2343   return FALSE;
2344 }
2345
2346 static void
2347 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2348                                          GtkBuilder   *builder,
2349                                          GObject      *child,
2350                                          const gchar  *tagname,
2351                                          gpointer     *data)
2352 {
2353   SubParserData *sub = (SubParserData*)data;
2354   
2355   if (strcmp (tagname, "columns") == 0)
2356     {
2357       g_slist_free (sub->column_type_names);
2358       g_slice_free (SubParserData, sub);
2359     }
2360   else if (strcmp (tagname, "data") == 0)
2361     {
2362       int i;
2363       for (i = 0; i < sub->n_columns; i++)
2364         {
2365           ColInfo *info = sub->columns[i];
2366           if (info)
2367             {
2368               g_free (info->context);
2369               g_slice_free (ColInfo, info);
2370             }
2371         }
2372       g_free (sub->colids);
2373       g_free (sub->columns);
2374       g_free (sub->values);
2375       g_slice_free (SubParserData, sub);
2376     }
2377   else
2378     g_warning ("Unknown custom list store tag: %s", tagname);
2379 }
2380
2381 #define __GTK_LIST_STORE_C__
2382 #include "gtkaliasdef.c"