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