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