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