treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[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, false))
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 /* Return the number of `union value's in the dictionary */
505 size_t
506 psppire_dict_get_n_values (const PsppireDict *d)
507 {
508   g_return_val_if_fail (d, -1);
509   g_return_val_if_fail (d->dict, -1);
510
511   return dict_get_next_value_idx (d->dict);
512 }
513
514
515 /* Returns the prototype for the cases that match the dictionary */
516 const struct caseproto *
517 psppire_dict_get_proto (const PsppireDict *d)
518 {
519   g_return_val_if_fail (d, NULL);
520   g_return_val_if_fail (d->dict, NULL);
521
522   return dict_get_proto (d->dict);
523 }
524
525
526 /* Return a variable by name.
527    Return NULL if it doesn't exist
528 */
529 struct variable *
530 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
531 {
532   g_return_val_if_fail (d, NULL);
533   g_return_val_if_fail (d->dict, NULL);
534
535   return dict_lookup_var (d->dict, name);
536 }
537
538 /* Clears the contents of D */
539 void
540 psppire_dict_clear (PsppireDict *d)
541 {
542   g_return_if_fail (d);
543   g_return_if_fail (d->dict);
544
545   {
546     dict_clear (d->dict);
547   }
548 }
549
550
551 /* Return true if NAME would be a valid name of a variable to add to the
552    dictionary.  False otherwise.
553    If REPORT is true, then invalid names will be reported as such as errors
554 */
555 gboolean
556 psppire_dict_check_name (const PsppireDict *dict,
557                          const gchar *name, gboolean report)
558 {
559   if (! dict_id_is_valid (dict->dict, name, report))
560     return FALSE;
561
562   if (psppire_dict_lookup_var (dict, name))
563     {
564       if (report)
565         msg (ME, _("Duplicate variable name."));
566       return FALSE;
567     }
568
569   return TRUE;
570 }
571
572
573 gint
574 psppire_dict_get_next_value_idx (const PsppireDict *dict)
575 {
576   return dict_get_next_value_idx (dict->dict);
577 }
578
579
580 /* Tree Model Stuff */
581
582 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
583
584 static gint tree_model_n_columns (GtkTreeModel *model);
585
586 static GType tree_model_column_type (GtkTreeModel *model, gint index);
587
588 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
589                                      GtkTreePath *path);
590
591 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
592
593 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
594                                           GtkTreeIter *iter);
595
596 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
597                                   gint column, GValue *value);
598
599 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
600                                       GtkTreeIter *parent, gint n);
601
602 static gint tree_model_n_children (GtkTreeModel *tree_model,
603                                    GtkTreeIter  *iter);
604
605 static gboolean tree_model_iter_children (GtkTreeModel *,
606                                           GtkTreeIter *,
607                                           GtkTreeIter *);
608
609 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
610                                         GtkTreeIter *iter,
611                                         GtkTreeIter *child);
612
613 static gboolean tree_model_iter_has_child  (GtkTreeModel *tree_model,
614                                             GtkTreeIter  *iter);
615
616 static void
617 dictionary_tree_model_init (GtkTreeModelIface *iface)
618 {
619   iface->get_flags = tree_model_get_flags;
620   iface->get_n_columns = tree_model_n_columns;
621   iface->get_column_type = tree_model_column_type;
622   iface->get_iter = tree_model_get_iter;
623   iface->iter_next = tree_model_iter_next;
624   iface->get_path = tree_model_get_path;
625   iface->get_value = tree_model_get_value;
626
627   iface->iter_children = tree_model_iter_children ;
628   iface->iter_has_child = tree_model_iter_has_child ;
629   iface->iter_n_children = tree_model_n_children ;
630   iface->iter_nth_child = tree_model_nth_child ;
631   iface->iter_parent = tree_model_iter_parent ;
632 }
633
634 static gboolean
635 tree_model_iter_has_child  (GtkTreeModel *tree_model,
636                             GtkTreeIter  *iter)
637 {
638   return FALSE;
639 }
640
641 static gboolean
642 tree_model_iter_parent (GtkTreeModel *tree_model,
643                         GtkTreeIter *iter,
644                         GtkTreeIter *child)
645 {
646   return TRUE;
647 }
648
649 static GtkTreeModelFlags
650 tree_model_get_flags (GtkTreeModel *model)
651 {
652   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
653
654   return GTK_TREE_MODEL_LIST_ONLY;
655 }
656
657
658 static gint
659 tree_model_n_columns (GtkTreeModel *model)
660 {
661   return n_DICT_COLS;
662 }
663
664 static GType
665 tree_model_column_type (GtkTreeModel *model, gint index)
666 {
667   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
668
669   GType t = 0;
670
671   switch (index)
672     {
673     case DICT_TVM_COL_NAME:
674     case DICT_TVM_COL_LABEL:
675       t = G_TYPE_STRING;
676       break;
677     case DICT_TVM_COL_DECIMAL:
678     case DICT_TVM_COL_WIDTH:
679     case DICT_TVM_COL_COLUMNS:
680       t = G_TYPE_INT;
681       break;
682     case DICT_TVM_COL_VAR:
683       t = PSPPIRE_VAR_PTR_TYPE;
684       break;
685     case DICT_TVM_COL_ALIGNMENT:
686       t = align_enum_type;
687       break;
688     case DICT_TVM_COL_MEASURE:
689       t = measure_enum_type;
690       break;
691     case DICT_TVM_COL_ROLE:
692       t = role_enum_type;
693       break;
694     }
695
696   return t;
697 }
698
699 static gboolean
700 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
701 {
702   gint *indices, depth;
703   gint n;
704   struct variable *var;
705
706   PsppireDict *dict = PSPPIRE_DICT (model);
707
708   g_return_val_if_fail (path, FALSE);
709
710   indices = gtk_tree_path_get_indices (path);
711   depth = gtk_tree_path_get_depth (path);
712
713   g_return_val_if_fail (depth == 1, FALSE);
714
715   n = indices [0];
716
717   if (n < 0 || n >= psppire_dict_get_n_vars (dict))
718     {
719       iter->stamp = 0;
720       iter->user_data = NULL;
721       return FALSE;
722     }
723
724   var = psppire_dict_get_variable (dict, n);
725
726   g_assert (var_get_dict_index (var) == n);
727
728   iter->stamp = dict->stamp;
729   iter->user_data = var;
730
731   return TRUE;
732 }
733
734
735 static gboolean
736 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
737 {
738   PsppireDict *dict = PSPPIRE_DICT (model);
739   struct variable *var;
740   gint idx;
741
742   if (iter == NULL || iter->user_data == NULL)
743     return FALSE;
744
745   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
746
747   var = iter->user_data;
748
749   idx = var_get_dict_index (var);
750
751   if (idx + 1 >= psppire_dict_get_n_vars (dict))
752     {
753       iter->user_data = NULL;
754       iter->stamp = 0;
755       return FALSE;
756     }
757
758   var = psppire_dict_get_variable (dict, idx + 1);
759
760   g_assert (var_get_dict_index (var) == idx + 1);
761
762   iter->user_data = var;
763
764   return TRUE;
765 }
766
767 static GtkTreePath *
768 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
769 {
770   GtkTreePath *path;
771   struct variable *var;
772   PsppireDict *dict = PSPPIRE_DICT (model);
773
774   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
775
776   var = iter->user_data;
777
778   path = gtk_tree_path_new ();
779   gtk_tree_path_append_index (path, var_get_dict_index (var));
780
781   return path;
782 }
783
784 const struct fmt_spec *var_get_write_format (const struct variable *);
785
786 static void
787 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
788                       gint column, GValue *value)
789 {
790   struct variable *var;
791   PsppireDict *dict = PSPPIRE_DICT (model);
792
793   g_return_if_fail (iter->stamp == dict->stamp);
794
795   var = iter->user_data;
796
797   const struct fmt_spec *fs = var_get_write_format (var);
798
799   switch (column)
800     {
801     case DICT_TVM_COL_NAME:
802       g_value_init (value, G_TYPE_STRING);
803       g_value_set_string (value, var_get_name (var));
804       break;
805     case DICT_TVM_COL_WIDTH:
806       g_value_init (value, G_TYPE_INT);
807       g_value_set_int (value, fs->w);
808       break;
809     case DICT_TVM_COL_DECIMAL:
810       g_value_init (value, G_TYPE_INT);
811       g_value_set_int (value, fs->d);
812       break;
813     case DICT_TVM_COL_LABEL:
814       g_value_init (value, G_TYPE_STRING);
815       g_value_set_string (value, var_get_label (var));
816       break;
817     case DICT_TVM_COL_COLUMNS:
818       g_value_init (value, G_TYPE_INT);
819       g_value_set_int (value, var_get_display_width (var));
820       break;
821     case DICT_TVM_COL_ALIGNMENT:
822       g_value_init (value, align_enum_type);
823       g_value_set_enum (value, var_get_alignment (var));
824       break;
825     case DICT_TVM_COL_MEASURE:
826       g_value_init (value, measure_enum_type);
827       g_value_set_enum (value, var_get_measure (var));
828       break;
829     case DICT_TVM_COL_ROLE:
830       g_value_init (value, role_enum_type);
831       g_value_set_enum (value, var_get_role (var));
832       break;
833     case DICT_TVM_COL_VAR:
834       g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
835       g_value_set_boxed (value, var);
836       break;
837     default:
838       g_value_init (value, G_TYPE_STRING);
839       g_value_set_string (value, "????");
840       break;
841     }
842 }
843
844 static gboolean
845 tree_model_iter_children (GtkTreeModel *tree_model,
846                           GtkTreeIter *iter,
847                           GtkTreeIter *parent)
848 {
849   return FALSE;
850 }
851
852 static gint
853 tree_model_n_children (GtkTreeModel *model,
854                        GtkTreeIter  *iter)
855 {
856   PsppireDict *dict = PSPPIRE_DICT (model);
857
858   if (iter == NULL)
859     return psppire_dict_get_n_vars (dict);
860
861   return 0;
862 }
863
864 static gboolean
865 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
866                       GtkTreeIter *parent, gint n)
867 {
868   PsppireDict *dict;
869
870   g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
871
872   dict = PSPPIRE_DICT (model);
873
874   if (parent)
875     return FALSE;
876
877   if (n >= psppire_dict_get_n_vars (dict))
878     return FALSE;
879
880   iter->stamp = dict->stamp;
881   iter->user_data = psppire_dict_get_variable (dict, n);
882
883   if (!iter->user_data)
884     return FALSE;
885
886   return TRUE;
887 }
888
889
890 gboolean
891 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
892                          const gchar *name)
893 {
894   if (! dict_id_is_valid (dict->dict, name, false))
895     return FALSE;
896
897   /* Make sure no other variable has this name */
898   if (NULL != psppire_dict_lookup_var (dict, name))
899     return FALSE;
900
901   dict_rename_var (dict->dict, v, name);
902
903   return TRUE;
904 }
905
906
907 struct variable *
908 psppire_dict_get_weight_variable (const PsppireDict *dict)
909 {
910   return dict_get_weight (dict->dict);
911 }
912
913 const gchar *
914 psppire_dict_encoding (const PsppireDict *dict)
915 {
916   return dict_get_encoding (dict->dict);
917 }