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