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