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