dictionary: Always compact immediately upon deletion of a variable.
[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   VARIABLES_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 [VARIABLES_DELETED] =
195     g_signal_new ("variables-deleted",
196                   G_TYPE_FROM_CLASS (class),
197                   G_SIGNAL_RUN_FIRST,
198                   0,
199                   NULL, NULL,
200                   psppire_marshal_VOID__INT_UINT,
201                   G_TYPE_NONE,
202                   2,
203                   G_TYPE_INT,
204                   G_TYPE_UINT);
205
206   signals [WEIGHT_CHANGED] =
207     g_signal_new ("weight-changed",
208                   G_TYPE_FROM_CLASS (class),
209                   G_SIGNAL_RUN_FIRST,
210                   0,
211                   NULL, NULL,
212                   g_cclosure_marshal_VOID__INT,
213                   G_TYPE_NONE,
214                   1,
215                   G_TYPE_INT);
216
217   signals [FILTER_CHANGED] =
218     g_signal_new ("filter-changed",
219                   G_TYPE_FROM_CLASS (class),
220                   G_SIGNAL_RUN_FIRST,
221                   0,
222                   NULL, NULL,
223                   g_cclosure_marshal_VOID__INT,
224                   G_TYPE_NONE,
225                   1,
226                   G_TYPE_INT);
227
228   signals [SPLIT_CHANGED] =
229     g_signal_new ("split-changed",
230                   G_TYPE_FROM_CLASS (class),
231                   G_SIGNAL_RUN_FIRST,
232                   0,
233                   NULL, NULL,
234                   g_cclosure_marshal_VOID__VOID,
235                   G_TYPE_NONE,
236                   0);
237 }
238
239 static void
240 psppire_dict_dispose (GObject *object)
241 {
242   PsppireDict *d = PSPPIRE_DICT (object);
243
244   if (!d->dispose_has_run)
245     return;
246
247   d->dispose_has_run = TRUE;
248
249   dict_set_callbacks (d->dict, NULL, NULL);
250   dict_unref (d->dict);
251
252   G_OBJECT_CLASS (parent_class)->dispose (object);
253 }
254
255 /* Pass on callbacks from src/data/dictionary, as
256    signals in the Gtk library */
257 static void
258 addcb (struct dictionary *d, int idx, void *pd)
259 {
260   PsppireDict *dict = PSPPIRE_DICT (pd);
261
262   if (! dict->disable_insert_signal)
263     {
264       g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
265       g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
266     }
267 }
268
269 static void
270 delcb (struct dictionary *d, int dict_idx, unsigned int n, void *pd)
271 {
272   g_signal_emit (pd, signals [VARIABLES_DELETED], 0, dict_idx, n);
273   g_signal_emit_by_name (pd, "items-changed",  dict_idx, 1, 0);
274 }
275
276 static void
277 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
278 {
279   g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
280   g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
281 }
282
283 static void
284 weight_changed_callback (struct dictionary *d, int idx, void *pd)
285 {
286   g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
287 }
288
289 static void
290 filter_changed_callback (struct dictionary *d, int idx, void *pd)
291 {
292   g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
293 }
294
295 static void
296 split_changed_callback (struct dictionary *d, void *pd)
297 {
298   g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
299 }
300
301 static const struct dict_callbacks gui_callbacks =
302   {
303     addcb,
304     delcb,
305     mutcb,
306     weight_changed_callback,
307     filter_changed_callback,
308     split_changed_callback
309   };
310
311 static void
312 psppire_dict_init (PsppireDict *d)
313 {
314   d->dispose_has_run = FALSE;
315
316   d->stamp = g_random_int ();
317   d->disable_insert_signal = FALSE;
318 }
319
320 /**
321  * psppire_dict_new_from_dict:
322  * @returns: a new #PsppireDict object
323  *
324  * Creates a new #PsppireDict.
325  */
326 PsppireDict*
327 psppire_dict_new_from_dict (struct dictionary *d)
328 {
329   PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
330   new_dict->dict = dict_ref (d);
331
332   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
333
334   return new_dict;
335 }
336
337
338 void
339 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
340 {
341   const struct variable *var =  dict_get_weight (d);
342
343   struct dictionary *old_dict = dict->dict;
344
345   guint old_n = dict_get_n_vars (dict->dict);
346   guint new_n = dict_get_n_vars (d);
347
348   dict->dict = dict_ref (d);
349   dict_unref (old_dict);
350
351   weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
352
353   var = dict_get_filter (d);
354   filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
355
356   split_changed_callback (d, dict);
357
358   dict_set_callbacks (dict->dict, &gui_callbacks, dict);
359
360   g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
361 }
362
363
364 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
365    Returns true if successful, false if SIZE is insufficient. */
366 bool
367 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
368 {
369   gint d;
370
371   for (d = 1; ; d++)
372     {
373       int len;
374
375       /* TRANSLATORS: This string must be a valid variable name.  That means:
376          - The string must be at most 64 bytes (not characters) long.
377          - The string may not contain whitespace.
378          - The first character may not be '$'
379          - The first character may not be a digit
380          - The final character may not be '.' or '_'
381       */
382       len = snprintf (name, size, _("Var%04d"), d);
383       if (len + 1 >= size)
384         return false;
385
386       if (psppire_dict_lookup_var (dict, name) == NULL)
387         return true;
388     }
389
390   return name;
391 }
392
393 /* Insert a new variable at posn IDX, with the name NAME, and return the
394    new variable.
395    IDX may take the special value -1, which will be treated the same as
396    zero.   If NAME is null, then a name will be automatically assigned.
397 */
398 struct variable *
399 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
400 {
401   struct variable *var;
402   char tmpname[64];
403
404   if (idx == -1)    /* Note bug #56392. */
405     idx = 0;
406   g_return_val_if_fail (d, NULL);
407   g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
408
409   if (name == NULL)
410     {
411       if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
412         g_return_val_if_reached (NULL);
413
414       name = tmpname;
415     }
416
417   d->disable_insert_signal = TRUE;
418
419   var = dict_create_var (d->dict, name, 0);
420
421   dict_reorder_var (d->dict, var, idx);
422
423   d->disable_insert_signal = FALSE;
424
425   g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
426   g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
427
428   return var;
429 }
430
431 /* Delete N variables beginning at FIRST */
432 void
433 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
434 {
435   g_return_if_fail (d);
436   g_return_if_fail (d->dict);
437   g_return_if_fail (PSPPIRE_IS_DICT (d));
438   size_t varcnt = dict_get_n_vars (d->dict);
439   g_return_if_fail (first < varcnt);
440   g_return_if_fail (first >= 0);
441   g_return_if_fail (n > 0);
442   g_return_if_fail (first + n <= varcnt);
443
444   dict_delete_consecutive_vars (d->dict, first, n);
445 }
446
447
448 gboolean
449 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
450 {
451   g_assert (d);
452   g_assert (PSPPIRE_IS_DICT (d));
453
454   if (! dict_id_is_valid (d->dict, name))
455     return FALSE;
456
457   if (idx < dict_get_n_vars (d->dict))
458     {
459       /* This is an existing variable? */
460       struct variable * var = dict_get_var (d->dict, idx);
461       dict_rename_var (d->dict, var, name);
462     }
463   else
464     {
465       /* new variable */
466       dict_create_var (d->dict, name, 0);
467     }
468
469   return TRUE;
470 }
471
472
473
474 /* Return the IDXth variable.
475    Will return NULL if IDX  exceeds the number of variables in the dictionary.
476  */
477 struct variable *
478 psppire_dict_get_variable (const PsppireDict *d, gint idx)
479 {
480   g_return_val_if_fail (d, NULL);
481   g_return_val_if_fail (d->dict, NULL);
482
483   if (dict_get_n_vars (d->dict) <= idx)
484     return NULL;
485
486   return dict_get_var (d->dict, idx);
487 }
488
489
490 /* Return the number of variables in the dictionary */
491 gint
492 psppire_dict_get_n_vars (const PsppireDict *d)
493 {
494   g_return_val_if_fail (d, -1);
495   g_return_val_if_fail (d->dict, -1);
496
497   return dict_get_n_vars (d->dict);
498 }
499
500
501 /* Returns the prototype for the cases that match the dictionary */
502 const struct caseproto *
503 psppire_dict_get_proto (const PsppireDict *d)
504 {
505   g_return_val_if_fail (d, NULL);
506   g_return_val_if_fail (d->dict, NULL);
507
508   return dict_get_proto (d->dict);
509 }
510
511
512 /* Return a variable by name.
513    Return NULL if it doesn't exist
514 */
515 struct variable *
516 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
517 {
518   g_return_val_if_fail (d, NULL);
519   g_return_val_if_fail (d->dict, NULL);
520
521   return dict_lookup_var (d->dict, name);
522 }
523
524 /* Clears the contents of D */
525 void
526 psppire_dict_clear (PsppireDict *d)
527 {
528   g_return_if_fail (d);
529   g_return_if_fail (d->dict);
530
531   {
532     dict_clear (d->dict);
533   }
534 }
535
536
537 /* Return true if NAME would be a valid name of a variable to add to the
538    dictionary.  False otherwise.
539 */
540 gboolean
541 psppire_dict_check_name (const PsppireDict *dict,
542                          const gchar *name)
543 {
544   return (dict_id_is_valid (dict->dict, name)
545           && !psppire_dict_lookup_var (dict, name));
546 }
547
548 /* Tree Model Stuff */
549
550 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
551
552 static gint tree_model_n_columns (GtkTreeModel *model);
553
554 static GType tree_model_column_type (GtkTreeModel *model, gint index);
555
556 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
557                                      GtkTreePath *path);
558
559 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
560
561 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
562                                           GtkTreeIter *iter);
563
564 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
565                                   gint column, GValue *value);
566
567 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
568                                       GtkTreeIter *parent, gint n);
569
570 static gint tree_model_n_children (GtkTreeModel *tree_model,
571                                    GtkTreeIter  *iter);
572
573 static gboolean tree_model_iter_children (GtkTreeModel *,
574                                           GtkTreeIter *,
575                                           GtkTreeIter *);
576
577 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
578                                         GtkTreeIter *iter,
579                                         GtkTreeIter *child);
580
581 static gboolean tree_model_iter_has_child  (GtkTreeModel *tree_model,
582                                             GtkTreeIter  *iter);
583
584 static void
585 dictionary_tree_model_init (GtkTreeModelIface *iface)
586 {
587   iface->get_flags = tree_model_get_flags;
588   iface->get_n_columns = tree_model_n_columns;
589   iface->get_column_type = tree_model_column_type;
590   iface->get_iter = tree_model_get_iter;
591   iface->iter_next = tree_model_iter_next;
592   iface->get_path = tree_model_get_path;
593   iface->get_value = tree_model_get_value;
594
595   iface->iter_children = tree_model_iter_children ;
596   iface->iter_has_child = tree_model_iter_has_child ;
597   iface->iter_n_children = tree_model_n_children ;
598   iface->iter_nth_child = tree_model_nth_child ;
599   iface->iter_parent = tree_model_iter_parent ;
600 }
601
602 static gboolean
603 tree_model_iter_has_child  (GtkTreeModel *tree_model,
604                             GtkTreeIter  *iter)
605 {
606   return FALSE;
607 }
608
609 static gboolean
610 tree_model_iter_parent (GtkTreeModel *tree_model,
611                         GtkTreeIter *iter,
612                         GtkTreeIter *child)
613 {
614   return TRUE;
615 }
616
617 static GtkTreeModelFlags
618 tree_model_get_flags (GtkTreeModel *model)
619 {
620   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
621
622   return GTK_TREE_MODEL_LIST_ONLY;
623 }
624
625
626 static gint
627 tree_model_n_columns (GtkTreeModel *model)
628 {
629   return n_DICT_COLS;
630 }
631
632 static GType
633 tree_model_column_type (GtkTreeModel *model, gint index)
634 {
635   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
636
637   GType t = 0;
638
639   switch (index)
640     {
641     case DICT_TVM_COL_NAME:
642     case DICT_TVM_COL_LABEL:
643       t = G_TYPE_STRING;
644       break;
645     case DICT_TVM_COL_DECIMAL:
646     case DICT_TVM_COL_WIDTH:
647     case DICT_TVM_COL_COLUMNS:
648       t = G_TYPE_INT;
649       break;
650     case DICT_TVM_COL_VAR:
651       t = PSPPIRE_VAR_PTR_TYPE;
652       break;
653     case DICT_TVM_COL_ALIGNMENT:
654       t = align_enum_type;
655       break;
656     case DICT_TVM_COL_MEASURE:
657       t = measure_enum_type;
658       break;
659     case DICT_TVM_COL_ROLE:
660       t = role_enum_type;
661       break;
662     }
663
664   return t;
665 }
666
667 static gboolean
668 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
669 {
670   gint *indices, depth;
671   gint n;
672   struct variable *var;
673
674   PsppireDict *dict = PSPPIRE_DICT (model);
675
676   g_return_val_if_fail (path, FALSE);
677
678   indices = gtk_tree_path_get_indices (path);
679   depth = gtk_tree_path_get_depth (path);
680
681   g_return_val_if_fail (depth == 1, FALSE);
682
683   n = indices [0];
684
685   if (n < 0 || n >= psppire_dict_get_n_vars (dict))
686     {
687       iter->stamp = 0;
688       iter->user_data = NULL;
689       return FALSE;
690     }
691
692   var = psppire_dict_get_variable (dict, n);
693
694   g_assert (var_get_dict_index (var) == n);
695
696   iter->stamp = dict->stamp;
697   iter->user_data = var;
698
699   return TRUE;
700 }
701
702
703 static gboolean
704 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
705 {
706   PsppireDict *dict = PSPPIRE_DICT (model);
707   struct variable *var;
708   gint idx;
709
710   if (iter == NULL || iter->user_data == NULL)
711     return FALSE;
712
713   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
714
715   var = iter->user_data;
716
717   idx = var_get_dict_index (var);
718
719   if (idx + 1 >= psppire_dict_get_n_vars (dict))
720     {
721       iter->user_data = NULL;
722       iter->stamp = 0;
723       return FALSE;
724     }
725
726   var = psppire_dict_get_variable (dict, idx + 1);
727
728   g_assert (var_get_dict_index (var) == idx + 1);
729
730   iter->user_data = var;
731
732   return TRUE;
733 }
734
735 static GtkTreePath *
736 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
737 {
738   GtkTreePath *path;
739   struct variable *var;
740   PsppireDict *dict = PSPPIRE_DICT (model);
741
742   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
743
744   var = iter->user_data;
745
746   path = gtk_tree_path_new ();
747   gtk_tree_path_append_index (path, var_get_dict_index (var));
748
749   return path;
750 }
751
752 struct fmt_spec var_get_write_format (const struct variable *);
753
754 static void
755 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
756                       gint column, GValue *value)
757 {
758   struct variable *var;
759   PsppireDict *dict = PSPPIRE_DICT (model);
760
761   g_return_if_fail (iter->stamp == dict->stamp);
762
763   var = iter->user_data;
764
765   struct fmt_spec fs = var_get_write_format (var);
766
767   switch (column)
768     {
769     case DICT_TVM_COL_NAME:
770       g_value_init (value, G_TYPE_STRING);
771       g_value_set_string (value, var_get_name (var));
772       break;
773     case DICT_TVM_COL_WIDTH:
774       g_value_init (value, G_TYPE_INT);
775       g_value_set_int (value, fs.w);
776       break;
777     case DICT_TVM_COL_DECIMAL:
778       g_value_init (value, G_TYPE_INT);
779       g_value_set_int (value, fs.d);
780       break;
781     case DICT_TVM_COL_LABEL:
782       g_value_init (value, G_TYPE_STRING);
783       g_value_set_string (value, var_get_label (var));
784       break;
785     case DICT_TVM_COL_COLUMNS:
786       g_value_init (value, G_TYPE_INT);
787       g_value_set_int (value, var_get_display_width (var));
788       break;
789     case DICT_TVM_COL_ALIGNMENT:
790       g_value_init (value, align_enum_type);
791       g_value_set_enum (value, var_get_alignment (var));
792       break;
793     case DICT_TVM_COL_MEASURE:
794       g_value_init (value, measure_enum_type);
795       g_value_set_enum (value, var_get_measure (var));
796       break;
797     case DICT_TVM_COL_ROLE:
798       g_value_init (value, role_enum_type);
799       g_value_set_enum (value, var_get_role (var));
800       break;
801     case DICT_TVM_COL_VAR:
802       g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
803       g_value_set_boxed (value, var);
804       break;
805     default:
806       g_value_init (value, G_TYPE_STRING);
807       g_value_set_string (value, "????");
808       break;
809     }
810 }
811
812 static gboolean
813 tree_model_iter_children (GtkTreeModel *tree_model,
814                           GtkTreeIter *iter,
815                           GtkTreeIter *parent)
816 {
817   return FALSE;
818 }
819
820 static gint
821 tree_model_n_children (GtkTreeModel *model,
822                        GtkTreeIter  *iter)
823 {
824   PsppireDict *dict = PSPPIRE_DICT (model);
825
826   if (iter == NULL)
827     return psppire_dict_get_n_vars (dict);
828
829   return 0;
830 }
831
832 static gboolean
833 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
834                       GtkTreeIter *parent, gint n)
835 {
836   PsppireDict *dict;
837
838   g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
839
840   dict = PSPPIRE_DICT (model);
841
842   if (parent)
843     return FALSE;
844
845   if (n >= psppire_dict_get_n_vars (dict))
846     return FALSE;
847
848   iter->stamp = dict->stamp;
849   iter->user_data = psppire_dict_get_variable (dict, n);
850
851   if (!iter->user_data)
852     return FALSE;
853
854   return TRUE;
855 }
856
857
858 gboolean
859 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
860                          const gchar *name)
861 {
862   if (! dict_id_is_valid (dict->dict, name))
863     return FALSE;
864
865   /* Make sure no other variable has this name */
866   if (NULL != psppire_dict_lookup_var (dict, name))
867     return FALSE;
868
869   dict_rename_var (dict->dict, v, name);
870
871   return TRUE;
872 }
873
874
875 struct variable *
876 psppire_dict_get_weight_variable (const PsppireDict *dict)
877 {
878   return dict_get_weight (dict->dict);
879 }
880
881 const gchar *
882 psppire_dict_encoding (const PsppireDict *dict)
883 {
884   return dict_get_encoding (dict->dict);
885 }