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