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