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