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