lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / ui / gui / psppire-dict.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011  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__INT_INT_INT,
163                   G_TYPE_NONE,
164                   3,
165                   G_TYPE_INT,
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, int dict_idx, int case_idx, int width, void *pd)
252 {
253   g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
254                  dict_idx, case_idx, width );
255 }
256
257 static void
258 mutcb (struct dictionary *d, int idx, void *pd)
259 {
260   g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
261 }
262
263 static void
264 resize_cb (struct dictionary *d, int idx, int old_width, void *pd)
265 {
266   g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, old_width);
267 }
268
269 static void
270 weight_changed_callback (struct dictionary *d, int idx, void *pd)
271 {
272   g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
273 }
274
275 static void
276 filter_changed_callback (struct dictionary *d, int idx, void *pd)
277 {
278   g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
279 }
280
281 static void
282 split_changed_callback (struct dictionary *d, void *pd)
283 {
284   g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
285 }
286
287 static void
288 variable_display_width_callback (struct dictionary *d, int idx, void *pd)
289 {
290   g_signal_emit (pd, signals [VARIABLE_DISPLAY_WIDTH_CHANGED], 0, idx);
291 }
292
293
294
295 static const struct dict_callbacks gui_callbacks =
296   {
297     addcb,
298     delcb,
299     mutcb,
300     resize_cb,
301     weight_changed_callback,
302     filter_changed_callback,
303     split_changed_callback,
304     variable_display_width_callback
305   };
306
307 static void
308 psppire_dict_init (PsppireDict *psppire_dict)
309 {
310   psppire_dict->stamp = g_random_int ();
311   psppire_dict->disable_insert_signal = FALSE;
312 }
313
314 /**
315  * psppire_dict_new_from_dict:
316  * @returns: a new #PsppireDict object
317  *
318  * Creates a new #PsppireDict.
319  */
320 PsppireDict*
321 psppire_dict_new_from_dict (struct dictionary *d)
322 {
323   PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
324   new_dict->dict = d;
325
326   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
327
328   return new_dict;
329 }
330
331
332 void
333 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
334 {
335   struct variable *var =  dict_get_weight (d);
336
337   dict->dict = d;
338
339   weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
340
341   var = dict_get_filter (d);
342   filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
343
344   split_changed_callback (d, dict);
345
346   dict_set_callbacks (dict->dict, &gui_callbacks, dict);
347
348   g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
349 }
350
351
352 /* Returns a valid name for a new variable in DICT.
353    The return value is statically allocated */
354 static gchar *
355 auto_generate_var_name (PsppireDict *dict)
356 {
357   gint d = 0;
358   static gchar name[10];
359
360   /* TRANSLATORS: This string must be a valid variable name.  That means:
361      - The string must be at most 64 bytes (not characters) long.
362      - The string may not contain whitespace.
363      - The first character may not be '$'
364      - The first character may not be a digit
365      - The final charactor may not be '.' or '_'
366    */
367   while (g_snprintf (name, 10, _("VAR%05d"), d++),
368          psppire_dict_lookup_var (dict, name))
369     ;
370
371   return name;
372 }
373
374 /* Insert a new variable at posn IDX, with the name NAME.
375    If NAME is null, then a name will be automatically assigned.
376 */
377 void
378 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
379 {
380   struct variable *var ;
381   g_return_if_fail (idx >= 0);
382   g_return_if_fail (d);
383   g_return_if_fail (PSPPIRE_IS_DICT (d));
384
385   if ( ! name )
386     name = auto_generate_var_name (d);
387
388   d->disable_insert_signal = TRUE;
389
390   var = dict_create_var (d->dict, name, 0);
391
392   dict_reorder_var (d->dict, var, idx);
393
394   d->disable_insert_signal = FALSE;
395
396   g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
397 }
398
399 /* Delete N variables beginning at FIRST */
400 void
401 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
402 {
403   gint idx;
404   g_return_if_fail (d);
405   g_return_if_fail (d->dict);
406   g_return_if_fail (PSPPIRE_IS_DICT (d));
407
408   for (idx = 0 ; idx < n ; ++idx )
409     {
410       struct variable *var;
411
412       /* Do nothing if it's out of bounds */
413       if ( first >= dict_get_var_cnt (d->dict))
414         break;
415
416       var = dict_get_var (d->dict, first);
417       dict_delete_var (d->dict, var);
418     }
419 }
420
421
422 gboolean
423 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
424 {
425   struct variable *var;
426   g_assert (d);
427   g_assert (PSPPIRE_IS_DICT (d));
428
429   if ( ! dict_id_is_valid (d->dict, name, false))
430     return FALSE;
431
432   if ( idx < dict_get_var_cnt (d->dict))
433     {
434       /* This is an existing variable? */
435       var = dict_get_var (d->dict, idx);
436       dict_rename_var (d->dict, var, name);
437     }
438   else
439     {
440       /* new variable */
441       dict_create_var (d->dict, name, 0);
442     }
443
444   return TRUE;
445 }
446
447
448
449 /* Return the IDXth variable.
450    Will return NULL if IDX  exceeds the number of variables in the dictionary.
451  */
452 struct variable *
453 psppire_dict_get_variable (const PsppireDict *d, gint idx)
454 {
455   g_return_val_if_fail (d, NULL);
456   g_return_val_if_fail (d->dict, NULL);
457
458   if ( dict_get_var_cnt (d->dict) <= idx )
459     return NULL;
460
461   return dict_get_var (d->dict, idx);
462 }
463
464
465 /* Return the number of variables in the dictionary */
466 gint
467 psppire_dict_get_var_cnt (const PsppireDict *d)
468 {
469   g_return_val_if_fail (d, -1);
470   g_return_val_if_fail (d->dict, -1);
471
472   return dict_get_var_cnt (d->dict);
473 }
474
475
476 /* Return the number of `union value's in the dictionary */
477 size_t
478 psppire_dict_get_value_cnt (const PsppireDict *d)
479 {
480   g_return_val_if_fail (d, -1);
481   g_return_val_if_fail (d->dict, -1);
482
483   return dict_get_next_value_idx (d->dict);
484 }
485
486
487 /* Returns the prototype for the cases that match the dictionary */
488 const struct caseproto *
489 psppire_dict_get_proto (const PsppireDict *d)
490 {
491   g_return_val_if_fail (d, NULL);
492   g_return_val_if_fail (d->dict, NULL);
493
494   return dict_get_proto (d->dict);
495 }
496
497
498 /* Return a variable by name.
499    Return NULL if it doesn't exist
500 */
501 struct variable *
502 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
503 {
504   g_return_val_if_fail (d, NULL);
505   g_return_val_if_fail (d->dict, NULL);
506
507   return dict_lookup_var (d->dict, name);
508 }
509
510 /* Clears the contents of D */
511 void
512 psppire_dict_clear (PsppireDict *d)
513 {
514   g_return_if_fail (d);
515   g_return_if_fail (d->dict);
516
517   {
518     dict_clear (d->dict);
519   }
520 }
521
522
523 /* Return true is NAME would be a valid name of a variable to add to the
524    dictionary.  False otherwise.
525    If REPORT is true, then invalid names will be reported as such as errors
526 */
527 gboolean
528 psppire_dict_check_name (const PsppireDict *dict,
529                          const gchar *name, gboolean report)
530 {
531   if ( ! dict_id_is_valid (dict->dict, name, report ) )
532     return FALSE;
533
534   if (psppire_dict_lookup_var (dict, name))
535     {
536       if ( report )
537         msg (ME,"Duplicate variable name.");
538       return FALSE;
539     }
540
541   return TRUE;
542 }
543
544
545 gint
546 psppire_dict_get_next_value_idx (const PsppireDict *dict)
547 {
548   return dict_get_next_value_idx (dict->dict);
549 }
550
551
552 void
553 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
554                               gint old_size, gint new_size)
555 {
556   g_return_if_fail (d);
557   g_return_if_fail (d->dict);
558
559   if ( old_size == new_size )
560     return ;
561
562   g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
563                  var_get_dict_index (pv),
564                  new_size - old_size );
565 }
566
567
568 /* Tree Model Stuff */
569
570 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
571
572 static gint tree_model_n_columns (GtkTreeModel *model);
573
574 static GType tree_model_column_type (GtkTreeModel *model, gint index);
575
576 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
577                                      GtkTreePath *path);
578
579 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
580
581 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
582                                           GtkTreeIter *iter);
583
584 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
585                                   gint column, GValue *value);
586
587 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
588                                       GtkTreeIter *parent, gint n);
589
590 static gint tree_model_n_children (GtkTreeModel *tree_model,
591                                    GtkTreeIter  *iter);
592
593 static gboolean tree_model_iter_children (GtkTreeModel *,
594                                           GtkTreeIter *,
595                                           GtkTreeIter *);
596
597 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
598                                         GtkTreeIter *iter,
599                                         GtkTreeIter *child);
600
601 static gboolean tree_model_iter_has_child  (GtkTreeModel *tree_model,
602                                             GtkTreeIter  *iter);
603
604 static void
605 dictionary_tree_model_init (GtkTreeModelIface *iface)
606 {
607   iface->get_flags = tree_model_get_flags;
608   iface->get_n_columns = tree_model_n_columns;
609   iface->get_column_type = tree_model_column_type;
610   iface->get_iter = tree_model_get_iter;
611   iface->iter_next = tree_model_iter_next;
612   iface->get_path = tree_model_get_path;
613   iface->get_value = tree_model_get_value;
614
615   iface->iter_children = tree_model_iter_children ;
616   iface->iter_has_child = tree_model_iter_has_child ;
617   iface->iter_n_children = tree_model_n_children ;
618   iface->iter_nth_child = tree_model_nth_child ;
619   iface->iter_parent = tree_model_iter_parent ;
620 }
621
622 static gboolean
623 tree_model_iter_has_child  (GtkTreeModel *tree_model,
624                             GtkTreeIter  *iter)
625 {
626   return FALSE;
627 }
628
629 static gboolean
630 tree_model_iter_parent (GtkTreeModel *tree_model,
631                         GtkTreeIter *iter,
632                         GtkTreeIter *child)
633 {
634   return TRUE;
635 }
636
637 static GtkTreeModelFlags
638 tree_model_get_flags (GtkTreeModel *model)
639 {
640   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
641
642   return GTK_TREE_MODEL_LIST_ONLY;
643 }
644
645
646 static gint
647 tree_model_n_columns (GtkTreeModel *model)
648 {
649   return n_DICT_COLS;
650 }
651
652 static GType
653 tree_model_column_type (GtkTreeModel *model, gint index)
654 {
655   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
656
657   switch (index)
658     {
659     case DICT_TVM_COL_NAME:
660       return G_TYPE_STRING;
661       break;
662     case DICT_TVM_COL_VAR:
663       return PSPPIRE_VAR_PTR_TYPE;
664       break;
665     default:
666       g_return_val_if_reached ((GType)0);
667       break;
668     }
669
670   g_assert_not_reached ();
671   return ((GType)0);
672 }
673
674 static gboolean
675 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
676 {
677   gint *indices, depth;
678   gint n;
679   struct variable *var;
680
681   PsppireDict *dict = PSPPIRE_DICT (model);
682
683   g_return_val_if_fail (path, FALSE);
684
685   indices = gtk_tree_path_get_indices (path);
686   depth = gtk_tree_path_get_depth (path);
687
688   g_return_val_if_fail (depth == 1, FALSE);
689
690   n = indices [0];
691
692   if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
693     {
694       iter->stamp = 0;
695       iter->user_data = NULL;
696       return FALSE;
697     }
698
699   var = psppire_dict_get_variable (dict, n);
700
701   g_assert (var_get_dict_index (var) == n);
702
703   iter->stamp = dict->stamp;
704   iter->user_data = var;
705
706   return TRUE;
707 }
708
709
710 static gboolean
711 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
712 {
713   PsppireDict *dict = PSPPIRE_DICT (model);
714   struct variable *var;
715   gint idx;
716
717   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
718
719   if ( iter == NULL || iter->user_data == NULL)
720     return FALSE;
721
722   var = iter->user_data;
723
724   idx = var_get_dict_index (var);
725
726   if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
727     {
728       iter->user_data = NULL;
729       iter->stamp = 0;
730       return FALSE;
731     }
732
733   var = psppire_dict_get_variable (dict, idx + 1);
734
735   g_assert (var_get_dict_index (var) == idx + 1);
736
737   iter->user_data = var;
738
739   return TRUE;
740 }
741
742 static GtkTreePath *
743 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
744 {
745   GtkTreePath *path;
746   struct variable *var;
747   PsppireDict *dict = PSPPIRE_DICT (model);
748
749   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
750
751   var = iter->user_data;
752
753   path = gtk_tree_path_new ();
754   gtk_tree_path_append_index (path, var_get_dict_index (var));
755
756   return path;
757 }
758
759
760 static void
761 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
762                       gint column, GValue *value)
763 {
764   struct variable *var;
765   PsppireDict *dict = PSPPIRE_DICT (model);
766
767   g_return_if_fail (iter->stamp == dict->stamp);
768
769   var =  iter->user_data;
770
771   switch (column)
772     {
773     case DICT_TVM_COL_NAME:
774       {
775         g_value_init (value, G_TYPE_STRING);
776         g_value_set_string (value, var_get_name (var));
777       }
778       break;
779     case DICT_TVM_COL_VAR:
780       g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
781       g_value_set_boxed (value, var);
782       break;
783     default:
784       g_return_if_reached ();
785       break;
786     }
787 }
788
789 static gboolean
790 tree_model_iter_children (GtkTreeModel *tree_model,
791                           GtkTreeIter *iter,
792                           GtkTreeIter *parent)
793 {
794   return FALSE;
795 }
796
797 static gint
798 tree_model_n_children (GtkTreeModel *model,
799                        GtkTreeIter  *iter)
800 {
801   PsppireDict *dict = PSPPIRE_DICT (model);
802
803   if ( iter == NULL )
804     return psppire_dict_get_var_cnt (dict);
805
806   return 0;
807 }
808
809 static gboolean
810 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
811                       GtkTreeIter *parent, gint n)
812 {
813   PsppireDict *dict;
814
815   g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
816
817   dict = PSPPIRE_DICT (model);
818
819   if ( parent )
820     return FALSE;
821
822   if ( n >= psppire_dict_get_var_cnt (dict) )
823     return FALSE;
824
825   iter->stamp = dict->stamp;
826   iter->user_data = psppire_dict_get_variable (dict, n);
827
828   if ( !iter->user_data)
829     return FALSE;
830
831   return TRUE;
832 }
833
834
835 gboolean
836 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
837                          const gchar *name)
838 {
839   if ( ! dict_id_is_valid (dict->dict, name, false))
840     return FALSE;
841
842   /* Make sure no other variable has this name */
843   if ( NULL != psppire_dict_lookup_var (dict, name))
844     return FALSE;
845
846   dict_rename_var (dict->dict, v, name);
847
848   return TRUE;
849 }
850
851
852 struct variable *
853 psppire_dict_get_weight_variable (const PsppireDict *dict)
854 {
855   return dict_get_weight (dict->dict);
856 }
857
858
859
860 #if DEBUGGING
861 void
862 psppire_dict_dump (const PsppireDict *dict)
863 {
864   gint i;
865   const struct dictionary *d = dict->dict;
866
867   for (i = 0; i < dict_get_var_cnt (d); ++i)
868     {
869       const struct variable *v = psppire_dict_get_variable (dict, i);
870       int di = var_get_dict_index (v);
871       g_print ("`%s' idx=%d, fv=%d\n",
872                var_get_name(v),
873                di,
874                var_get_case_index(v));
875
876     }
877 }
878 #endif
879
880
881
882
883 const gchar *
884 psppire_dict_encoding (const PsppireDict *dict)
885 {
886   return dict_get_encoding (dict->dict);
887 }