dictionary: Get rid of next_value_idx.
[pspp] / src / ui / gui / psppire-dict.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012,
3    2016, 2017  Free Software Foundation
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "ui/gui/psppire-dict.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <gtk/gtk.h>
25
26 #include "data/dictionary.h"
27 #include "data/identifier.h"
28 #include "data/missing-values.h"
29 #include "data/value-labels.h"
30 #include "data/variable.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "ui/gui/helper.h"
34 #include "ui/gui/psppire-marshal.h"
35 #include "ui/gui/psppire-var-ptr.h"
36
37 #include <gobject/genums.h>
38
39 #include <gettext.h>
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
42
43
44
45 GType align_enum_type;
46 GType measure_enum_type;
47 GType role_enum_type;
48
49
50 enum  {
51   VARIABLE_CHANGED,
52   VARIABLE_INSERTED,
53   VARIABLE_DELETED,
54
55   WEIGHT_CHANGED,
56   FILTER_CHANGED,
57   SPLIT_CHANGED,
58
59   RESIZE_ITEM,
60
61   n_SIGNALS
62 };
63
64
65 /* --- prototypes --- */
66 static void psppire_dict_dispose        (GObject                *object);
67
68 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
69
70
71
72 static guint
73 gni (GListModel *list)
74 {
75   PsppireDict *dict = PSPPIRE_DICT (list);
76
77   return psppire_dict_get_n_vars (dict);
78 }
79
80 static GType
81 git (GListModel *list)
82 {
83   return GTK_TYPE_BUTTON;
84 }
85
86 static gpointer
87 gi (GListModel *list, guint id)
88 {
89   GtkWidget *button = gtk_button_new ();
90
91   PsppireDict *dict = PSPPIRE_DICT (list);
92
93   if (id >= psppire_dict_get_n_vars (dict))
94     {
95       gtk_button_set_label (GTK_BUTTON (button),  _("Var"));
96     }
97   else
98     {
99       const struct variable *v =  psppire_dict_get_variable (dict, id);
100
101       gtk_button_set_label (GTK_BUTTON (button),  var_get_name (v));
102       gtk_widget_set_tooltip_text (button, var_get_label (v));
103
104       {
105         PangoContext *context = gtk_widget_create_pango_context (button);
106         PangoLayout *layout = pango_layout_new (context);
107         PangoRectangle rect;
108
109         pango_layout_set_text (layout, "M", 1);
110
111         pango_layout_get_extents (layout, NULL, &rect);
112
113         g_object_unref (G_OBJECT (layout));
114         g_object_unref (G_OBJECT (context));
115
116         gtk_widget_set_size_request (button,
117                                      (0.25 + var_get_display_width (v))
118                                      * rect.width / PANGO_SCALE,
119                                      -1);
120       }
121     }
122
123   return button;
124 }
125
126
127 static void
128 ssw_init_iface (GListModelInterface *iface)
129 {
130   iface->get_n_items = gni;
131   iface->get_item = gi;
132   iface->get_item_type = git;
133 }
134
135
136 /* --- variables --- */
137 static GObjectClass     *parent_class = NULL;
138
139 static guint signals [n_SIGNALS];
140
141 /* --- functions --- */
142
143 G_DEFINE_TYPE_WITH_CODE (PsppireDict, psppire_dict, G_TYPE_OBJECT,
144                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
145                                                 dictionary_tree_model_init)
146                          G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
147                                                 ssw_init_iface))
148
149 static void
150 psppire_dict_class_init (PsppireDictClass *class)
151 {
152   GObjectClass *object_class = G_OBJECT_CLASS (class);
153
154   parent_class = g_type_class_peek_parent (class);
155
156   object_class->dispose = psppire_dict_dispose;
157
158   signals [RESIZE_ITEM] =
159     g_signal_new ("resize-item",
160                   G_TYPE_FROM_CLASS (class),
161                   G_SIGNAL_RUN_LAST,
162                   0,
163                   NULL, NULL,
164                   psppire_marshal_BOOLEAN__INT_INT,
165                   G_TYPE_BOOLEAN,
166                   2,
167                   G_TYPE_INT,
168                   G_TYPE_INT);
169
170   signals [VARIABLE_CHANGED] =
171     g_signal_new ("variable-changed",
172                   G_TYPE_FROM_CLASS (class),
173                   G_SIGNAL_RUN_FIRST,
174                   0,
175                   NULL, NULL,
176                   psppire_marshal_VOID__INT_UINT_POINTER,
177                   G_TYPE_NONE,
178                   3,
179                   G_TYPE_INT,
180                   G_TYPE_UINT,
181                   G_TYPE_POINTER);
182
183   signals [VARIABLE_INSERTED] =
184     g_signal_new ("variable-inserted",
185                   G_TYPE_FROM_CLASS (class),
186                   G_SIGNAL_RUN_FIRST,
187                   0,
188                   NULL, NULL,
189                   g_cclosure_marshal_VOID__INT,
190                   G_TYPE_NONE,
191                   1,
192                   G_TYPE_INT);
193
194   signals [VARIABLE_DELETED] =
195     g_signal_new ("variable-deleted",
196                   G_TYPE_FROM_CLASS (class),
197                   G_SIGNAL_RUN_FIRST,
198                   0,
199                   NULL, NULL,
200                   psppire_marshal_VOID__POINTER_INT_INT,
201                   G_TYPE_NONE,
202                   3,
203                   G_TYPE_POINTER,
204                   G_TYPE_INT,
205                   G_TYPE_INT);
206
207   signals [WEIGHT_CHANGED] =
208     g_signal_new ("weight-changed",
209                   G_TYPE_FROM_CLASS (class),
210                   G_SIGNAL_RUN_FIRST,
211                   0,
212                   NULL, NULL,
213                   g_cclosure_marshal_VOID__INT,
214                   G_TYPE_NONE,
215                   1,
216                   G_TYPE_INT);
217
218   signals [FILTER_CHANGED] =
219     g_signal_new ("filter-changed",
220                   G_TYPE_FROM_CLASS (class),
221                   G_SIGNAL_RUN_FIRST,
222                   0,
223                   NULL, NULL,
224                   g_cclosure_marshal_VOID__INT,
225                   G_TYPE_NONE,
226                   1,
227                   G_TYPE_INT);
228
229   signals [SPLIT_CHANGED] =
230     g_signal_new ("split-changed",
231                   G_TYPE_FROM_CLASS (class),
232                   G_SIGNAL_RUN_FIRST,
233                   0,
234                   NULL, NULL,
235                   g_cclosure_marshal_VOID__VOID,
236                   G_TYPE_NONE,
237                   0);
238 }
239
240 static void
241 psppire_dict_dispose (GObject *object)
242 {
243   PsppireDict *d = PSPPIRE_DICT (object);
244
245   if (!d->dispose_has_run)
246     return;
247
248   d->dispose_has_run = TRUE;
249
250   dict_set_callbacks (d->dict, NULL, NULL);
251   dict_unref (d->dict);
252
253   G_OBJECT_CLASS (parent_class)->dispose (object);
254 }
255
256 /* Pass on callbacks from src/data/dictionary, as
257    signals in the Gtk library */
258 static void
259 addcb (struct dictionary *d, int idx, void *pd)
260 {
261   PsppireDict *dict = PSPPIRE_DICT (pd);
262
263   if (! dict->disable_insert_signal)
264     {
265       g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
266       g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
267     }
268 }
269
270 static void
271 delcb (struct dictionary *d, const struct variable *var,
272        int dict_idx, int case_idx, void *pd)
273 {
274   g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
275                  var, dict_idx, case_idx);
276   g_signal_emit_by_name (pd, "items-changed",  dict_idx, 1, 0);
277 }
278
279 static void
280 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
281 {
282   g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
283   g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
284 }
285
286 static void
287 weight_changed_callback (struct dictionary *d, int idx, void *pd)
288 {
289   g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
290 }
291
292 static void
293 filter_changed_callback (struct dictionary *d, int idx, void *pd)
294 {
295   g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
296 }
297
298 static void
299 split_changed_callback (struct dictionary *d, void *pd)
300 {
301   g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
302 }
303
304 static const struct dict_callbacks gui_callbacks =
305   {
306     addcb,
307     delcb,
308     mutcb,
309     weight_changed_callback,
310     filter_changed_callback,
311     split_changed_callback
312   };
313
314 static void
315 psppire_dict_init (PsppireDict *d)
316 {
317   d->dispose_has_run = FALSE;
318
319   d->stamp = g_random_int ();
320   d->disable_insert_signal = FALSE;
321 }
322
323 /**
324  * psppire_dict_new_from_dict:
325  * @returns: a new #PsppireDict object
326  *
327  * Creates a new #PsppireDict.
328  */
329 PsppireDict*
330 psppire_dict_new_from_dict (struct dictionary *d)
331 {
332   PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
333   new_dict->dict = dict_ref (d);
334
335   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
336
337   return new_dict;
338 }
339
340
341 void
342 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
343 {
344   const struct variable *var =  dict_get_weight (d);
345
346   struct dictionary *old_dict = dict->dict;
347
348   guint old_n = dict_get_n_vars (dict->dict);
349   guint new_n = dict_get_n_vars (d);
350
351   dict->dict = dict_ref (d);
352   dict_unref (old_dict);
353
354   weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
355
356   var = dict_get_filter (d);
357   filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
358
359   split_changed_callback (d, dict);
360
361   dict_set_callbacks (dict->dict, &gui_callbacks, dict);
362
363   g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
364 }
365
366
367 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
368    Returns true if successful, false if SIZE is insufficient. */
369 bool
370 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
371 {
372   gint d;
373
374   for (d = 1; ; d++)
375     {
376       int len;
377
378       /* TRANSLATORS: This string must be a valid variable name.  That means:
379          - The string must be at most 64 bytes (not characters) long.
380          - The string may not contain whitespace.
381          - The first character may not be '$'
382          - The first character may not be a digit
383          - The final character may not be '.' or '_'
384       */
385       len = snprintf (name, size, _("Var%04d"), d);
386       if (len + 1 >= size)
387         return false;
388
389       if (psppire_dict_lookup_var (dict, name) == NULL)
390         return true;
391     }
392
393   return name;
394 }
395
396 /* Insert a new variable at posn IDX, with the name NAME, and return the
397    new variable.
398    IDX may take the special value -1, which will be treated the same as
399    zero.   If NAME is null, then a name will be automatically assigned.
400 */
401 struct variable *
402 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
403 {
404   struct variable *var;
405   char tmpname[64];
406
407   if (idx == -1)    /* Note bug #56392. */
408     idx = 0;
409   g_return_val_if_fail (d, NULL);
410   g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
411
412   if (name == NULL)
413     {
414       if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
415         g_return_val_if_reached (NULL);
416
417       name = tmpname;
418     }
419
420   d->disable_insert_signal = TRUE;
421
422   var = dict_create_var (d->dict, name, 0);
423
424   dict_reorder_var (d->dict, var, idx);
425
426   d->disable_insert_signal = FALSE;
427
428   g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
429   g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
430
431   return var;
432 }
433
434 /* Delete N variables beginning at FIRST */
435 void
436 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
437 {
438   g_return_if_fail (d);
439   g_return_if_fail (d->dict);
440   g_return_if_fail (PSPPIRE_IS_DICT (d));
441   size_t varcnt = dict_get_n_vars (d->dict);
442   g_return_if_fail (first < varcnt);
443   g_return_if_fail (first >= 0);
444   g_return_if_fail (n > 0);
445   g_return_if_fail (first + n <= varcnt);
446
447   dict_delete_consecutive_vars (d->dict, first, n);
448 }
449
450
451 gboolean
452 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
453 {
454   g_assert (d);
455   g_assert (PSPPIRE_IS_DICT (d));
456
457   if (! dict_id_is_valid (d->dict, name))
458     return FALSE;
459
460   if (idx < dict_get_n_vars (d->dict))
461     {
462       /* This is an existing variable? */
463       struct variable * var = dict_get_var (d->dict, idx);
464       dict_rename_var (d->dict, var, name);
465     }
466   else
467     {
468       /* new variable */
469       dict_create_var (d->dict, name, 0);
470     }
471
472   return TRUE;
473 }
474
475
476
477 /* Return the IDXth variable.
478    Will return NULL if IDX  exceeds the number of variables in the dictionary.
479  */
480 struct variable *
481 psppire_dict_get_variable (const PsppireDict *d, gint idx)
482 {
483   g_return_val_if_fail (d, NULL);
484   g_return_val_if_fail (d->dict, NULL);
485
486   if (dict_get_n_vars (d->dict) <= idx)
487     return NULL;
488
489   return dict_get_var (d->dict, idx);
490 }
491
492
493 /* Return the number of variables in the dictionary */
494 gint
495 psppire_dict_get_n_vars (const PsppireDict *d)
496 {
497   g_return_val_if_fail (d, -1);
498   g_return_val_if_fail (d->dict, -1);
499
500   return dict_get_n_vars (d->dict);
501 }
502
503
504 /* Returns the prototype for the cases that match the dictionary */
505 const struct caseproto *
506 psppire_dict_get_proto (const PsppireDict *d)
507 {
508   g_return_val_if_fail (d, NULL);
509   g_return_val_if_fail (d->dict, NULL);
510
511   return dict_get_proto (d->dict);
512 }
513
514
515 /* Return a variable by name.
516    Return NULL if it doesn't exist
517 */
518 struct variable *
519 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
520 {
521   g_return_val_if_fail (d, NULL);
522   g_return_val_if_fail (d->dict, NULL);
523
524   return dict_lookup_var (d->dict, name);
525 }
526
527 /* Clears the contents of D */
528 void
529 psppire_dict_clear (PsppireDict *d)
530 {
531   g_return_if_fail (d);
532   g_return_if_fail (d->dict);
533
534   {
535     dict_clear (d->dict);
536   }
537 }
538
539
540 /* Return true if NAME would be a valid name of a variable to add to the
541    dictionary.  False otherwise.
542 */
543 gboolean
544 psppire_dict_check_name (const PsppireDict *dict,
545                          const gchar *name)
546 {
547   return (dict_id_is_valid (dict->dict, name)
548           && !psppire_dict_lookup_var (dict, name));
549 }
550
551 /* Tree Model Stuff */
552
553 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
554
555 static gint tree_model_n_columns (GtkTreeModel *model);
556
557 static GType tree_model_column_type (GtkTreeModel *model, gint index);
558
559 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
560                                      GtkTreePath *path);
561
562 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
563
564 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
565                                           GtkTreeIter *iter);
566
567 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
568                                   gint column, GValue *value);
569
570 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
571                                       GtkTreeIter *parent, gint n);
572
573 static gint tree_model_n_children (GtkTreeModel *tree_model,
574                                    GtkTreeIter  *iter);
575
576 static gboolean tree_model_iter_children (GtkTreeModel *,
577                                           GtkTreeIter *,
578                                           GtkTreeIter *);
579
580 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
581                                         GtkTreeIter *iter,
582                                         GtkTreeIter *child);
583
584 static gboolean tree_model_iter_has_child  (GtkTreeModel *tree_model,
585                                             GtkTreeIter  *iter);
586
587 static void
588 dictionary_tree_model_init (GtkTreeModelIface *iface)
589 {
590   iface->get_flags = tree_model_get_flags;
591   iface->get_n_columns = tree_model_n_columns;
592   iface->get_column_type = tree_model_column_type;
593   iface->get_iter = tree_model_get_iter;
594   iface->iter_next = tree_model_iter_next;
595   iface->get_path = tree_model_get_path;
596   iface->get_value = tree_model_get_value;
597
598   iface->iter_children = tree_model_iter_children ;
599   iface->iter_has_child = tree_model_iter_has_child ;
600   iface->iter_n_children = tree_model_n_children ;
601   iface->iter_nth_child = tree_model_nth_child ;
602   iface->iter_parent = tree_model_iter_parent ;
603 }
604
605 static gboolean
606 tree_model_iter_has_child  (GtkTreeModel *tree_model,
607                             GtkTreeIter  *iter)
608 {
609   return FALSE;
610 }
611
612 static gboolean
613 tree_model_iter_parent (GtkTreeModel *tree_model,
614                         GtkTreeIter *iter,
615                         GtkTreeIter *child)
616 {
617   return TRUE;
618 }
619
620 static GtkTreeModelFlags
621 tree_model_get_flags (GtkTreeModel *model)
622 {
623   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
624
625   return GTK_TREE_MODEL_LIST_ONLY;
626 }
627
628
629 static gint
630 tree_model_n_columns (GtkTreeModel *model)
631 {
632   return n_DICT_COLS;
633 }
634
635 static GType
636 tree_model_column_type (GtkTreeModel *model, gint index)
637 {
638   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
639
640   GType t = 0;
641
642   switch (index)
643     {
644     case DICT_TVM_COL_NAME:
645     case DICT_TVM_COL_LABEL:
646       t = G_TYPE_STRING;
647       break;
648     case DICT_TVM_COL_DECIMAL:
649     case DICT_TVM_COL_WIDTH:
650     case DICT_TVM_COL_COLUMNS:
651       t = G_TYPE_INT;
652       break;
653     case DICT_TVM_COL_VAR:
654       t = PSPPIRE_VAR_PTR_TYPE;
655       break;
656     case DICT_TVM_COL_ALIGNMENT:
657       t = align_enum_type;
658       break;
659     case DICT_TVM_COL_MEASURE:
660       t = measure_enum_type;
661       break;
662     case DICT_TVM_COL_ROLE:
663       t = role_enum_type;
664       break;
665     }
666
667   return t;
668 }
669
670 static gboolean
671 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
672 {
673   gint *indices, depth;
674   gint n;
675   struct variable *var;
676
677   PsppireDict *dict = PSPPIRE_DICT (model);
678
679   g_return_val_if_fail (path, FALSE);
680
681   indices = gtk_tree_path_get_indices (path);
682   depth = gtk_tree_path_get_depth (path);
683
684   g_return_val_if_fail (depth == 1, FALSE);
685
686   n = indices [0];
687
688   if (n < 0 || n >= psppire_dict_get_n_vars (dict))
689     {
690       iter->stamp = 0;
691       iter->user_data = NULL;
692       return FALSE;
693     }
694
695   var = psppire_dict_get_variable (dict, n);
696
697   g_assert (var_get_dict_index (var) == n);
698
699   iter->stamp = dict->stamp;
700   iter->user_data = var;
701
702   return TRUE;
703 }
704
705
706 static gboolean
707 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
708 {
709   PsppireDict *dict = PSPPIRE_DICT (model);
710   struct variable *var;
711   gint idx;
712
713   if (iter == NULL || iter->user_data == NULL)
714     return FALSE;
715
716   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
717
718   var = iter->user_data;
719
720   idx = var_get_dict_index (var);
721
722   if (idx + 1 >= psppire_dict_get_n_vars (dict))
723     {
724       iter->user_data = NULL;
725       iter->stamp = 0;
726       return FALSE;
727     }
728
729   var = psppire_dict_get_variable (dict, idx + 1);
730
731   g_assert (var_get_dict_index (var) == idx + 1);
732
733   iter->user_data = var;
734
735   return TRUE;
736 }
737
738 static GtkTreePath *
739 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
740 {
741   GtkTreePath *path;
742   struct variable *var;
743   PsppireDict *dict = PSPPIRE_DICT (model);
744
745   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
746
747   var = iter->user_data;
748
749   path = gtk_tree_path_new ();
750   gtk_tree_path_append_index (path, var_get_dict_index (var));
751
752   return path;
753 }
754
755 struct fmt_spec var_get_write_format (const struct variable *);
756
757 static void
758 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
759                       gint column, GValue *value)
760 {
761   struct variable *var;
762   PsppireDict *dict = PSPPIRE_DICT (model);
763
764   g_return_if_fail (iter->stamp == dict->stamp);
765
766   var = iter->user_data;
767
768   struct fmt_spec fs = var_get_write_format (var);
769
770   switch (column)
771     {
772     case DICT_TVM_COL_NAME:
773       g_value_init (value, G_TYPE_STRING);
774       g_value_set_string (value, var_get_name (var));
775       break;
776     case DICT_TVM_COL_WIDTH:
777       g_value_init (value, G_TYPE_INT);
778       g_value_set_int (value, fs.w);
779       break;
780     case DICT_TVM_COL_DECIMAL:
781       g_value_init (value, G_TYPE_INT);
782       g_value_set_int (value, fs.d);
783       break;
784     case DICT_TVM_COL_LABEL:
785       g_value_init (value, G_TYPE_STRING);
786       g_value_set_string (value, var_get_label (var));
787       break;
788     case DICT_TVM_COL_COLUMNS:
789       g_value_init (value, G_TYPE_INT);
790       g_value_set_int (value, var_get_display_width (var));
791       break;
792     case DICT_TVM_COL_ALIGNMENT:
793       g_value_init (value, align_enum_type);
794       g_value_set_enum (value, var_get_alignment (var));
795       break;
796     case DICT_TVM_COL_MEASURE:
797       g_value_init (value, measure_enum_type);
798       g_value_set_enum (value, var_get_measure (var));
799       break;
800     case DICT_TVM_COL_ROLE:
801       g_value_init (value, role_enum_type);
802       g_value_set_enum (value, var_get_role (var));
803       break;
804     case DICT_TVM_COL_VAR:
805       g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
806       g_value_set_boxed (value, var);
807       break;
808     default:
809       g_value_init (value, G_TYPE_STRING);
810       g_value_set_string (value, "????");
811       break;
812     }
813 }
814
815 static gboolean
816 tree_model_iter_children (GtkTreeModel *tree_model,
817                           GtkTreeIter *iter,
818                           GtkTreeIter *parent)
819 {
820   return FALSE;
821 }
822
823 static gint
824 tree_model_n_children (GtkTreeModel *model,
825                        GtkTreeIter  *iter)
826 {
827   PsppireDict *dict = PSPPIRE_DICT (model);
828
829   if (iter == NULL)
830     return psppire_dict_get_n_vars (dict);
831
832   return 0;
833 }
834
835 static gboolean
836 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
837                       GtkTreeIter *parent, gint n)
838 {
839   PsppireDict *dict;
840
841   g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
842
843   dict = PSPPIRE_DICT (model);
844
845   if (parent)
846     return FALSE;
847
848   if (n >= psppire_dict_get_n_vars (dict))
849     return FALSE;
850
851   iter->stamp = dict->stamp;
852   iter->user_data = psppire_dict_get_variable (dict, n);
853
854   if (!iter->user_data)
855     return FALSE;
856
857   return TRUE;
858 }
859
860
861 gboolean
862 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
863                          const gchar *name)
864 {
865   if (! dict_id_is_valid (dict->dict, name))
866     return FALSE;
867
868   /* Make sure no other variable has this name */
869   if (NULL != psppire_dict_lookup_var (dict, name))
870     return FALSE;
871
872   dict_rename_var (dict->dict, v, name);
873
874   return TRUE;
875 }
876
877
878 struct variable *
879 psppire_dict_get_weight_variable (const PsppireDict *dict)
880 {
881   return dict_get_weight (dict->dict);
882 }
883
884 const gchar *
885 psppire_dict_encoding (const PsppireDict *dict)
886 {
887   return dict_get_encoding (dict->dict);
888 }