Call dictionary callbacks on replaced dictionary.
[pspp-builds.git] / src / ui / gui / psppire-dict.c
1 /*
2   PSPPIRE --- A Graphical User Interface for PSPP
3   Copyright (C) 2004, 2006, 2007  Free Software Foundation
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18   02110-1301, USA. */
19
20 #include <config.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <gtk/gtk.h>
25 #include <gtksheet/gtkextra-marshal.h>
26
27 #include "psppire-dict.h"
28 #include <data/format.h>
29 #include <data/dictionary.h>
30 #include <data/missing-values.h>
31 #include <data/value-labels.h>
32 #include <data/variable.h>
33
34 #include "helper.h"
35 #include "message-dialog.h"
36
37 /* --- prototypes --- */
38 static void psppire_dict_class_init     (PsppireDictClass       *class);
39 static void psppire_dict_init   (PsppireDict            *dict);
40 static void psppire_dict_finalize       (GObject                *object);
41
42 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
43
44
45 /* --- variables --- */
46 static GObjectClass     *parent_class = NULL;
47
48 enum  {VARIABLE_CHANGED,
49        VARIABLE_RESIZED,
50        VARIABLE_INSERTED,
51        VARIABLES_DELETED,
52        WEIGHT_CHANGED,
53        FILTER_CHANGED,
54        SPLIT_CHANGED,
55        n_SIGNALS};
56
57 static guint signals [n_SIGNALS];
58
59 /* --- functions --- */
60 /**
61  * psppire_dict_get_type:
62  * @returns: the type ID for accelerator groups.
63  */
64 GType
65 psppire_dict_get_type (void)
66 {
67   static GType object_type = 0;
68
69   if (!object_type)
70     {
71       static const GTypeInfo object_info = {
72         sizeof (PsppireDictClass),
73         (GBaseInitFunc) NULL,
74         (GBaseFinalizeFunc) NULL,
75         (GClassInitFunc) psppire_dict_class_init,
76         NULL,   /* class_finalize */
77         NULL,   /* class_data */
78         sizeof (PsppireDict),
79         0,      /* n_preallocs */
80         (GInstanceInitFunc) psppire_dict_init,
81       };
82
83       static const GInterfaceInfo tree_model_info = {
84         (GInterfaceInitFunc) dictionary_tree_model_init,
85         NULL,
86         NULL
87       };
88
89       object_type = g_type_register_static (G_TYPE_OBJECT,
90                                             "PsppireDict",
91                                             &object_info, 0);
92
93       g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
94                                    &tree_model_info);
95
96
97     }
98
99   return object_type;
100 }
101
102
103 static void
104 psppire_dict_class_init (PsppireDictClass *class)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (class);
107
108   parent_class = g_type_class_peek_parent (class);
109
110   object_class->finalize = psppire_dict_finalize;
111
112   signals [VARIABLE_CHANGED] =
113     g_signal_new ("variable_changed",
114                   G_TYPE_FROM_CLASS (class),
115                   G_SIGNAL_RUN_FIRST,
116                   0,
117                   NULL, NULL,
118                   g_cclosure_marshal_VOID__INT,
119                   G_TYPE_NONE,
120                   1,
121                   G_TYPE_INT);
122
123
124
125   signals [VARIABLE_INSERTED] =
126     g_signal_new ("variable_inserted",
127                   G_TYPE_FROM_CLASS (class),
128                   G_SIGNAL_RUN_FIRST,
129                   0,
130                   NULL, NULL,
131                   g_cclosure_marshal_VOID__INT,
132                   G_TYPE_NONE,
133                   1,
134                   G_TYPE_INT);
135
136
137   signals [VARIABLES_DELETED] =
138     g_signal_new ("variables_deleted",
139                   G_TYPE_FROM_CLASS (class),
140                   G_SIGNAL_RUN_FIRST,
141                   0,
142                   NULL, NULL,
143                   gtkextra_VOID__INT_INT,
144                   G_TYPE_NONE,
145                   2,
146                   G_TYPE_INT,
147                   G_TYPE_INT);
148
149
150   signals [VARIABLE_RESIZED] =
151     g_signal_new ("dict-size-changed",
152                   G_TYPE_FROM_CLASS (class),
153                   G_SIGNAL_RUN_FIRST,
154                   0,
155                   NULL, NULL,
156                   gtkextra_VOID__INT_INT,
157                   G_TYPE_NONE,
158                   2,
159                   G_TYPE_INT,
160                   G_TYPE_INT);
161
162
163   signals [WEIGHT_CHANGED] =
164     g_signal_new ("weight-changed",
165                   G_TYPE_FROM_CLASS (class),
166                   G_SIGNAL_RUN_FIRST,
167                   0,
168                   NULL, NULL,
169                   g_cclosure_marshal_VOID__INT,
170                   G_TYPE_NONE,
171                   1,
172                   G_TYPE_INT);
173
174
175   signals [FILTER_CHANGED] =
176     g_signal_new ("filter-changed",
177                   G_TYPE_FROM_CLASS (class),
178                   G_SIGNAL_RUN_FIRST,
179                   0,
180                   NULL, NULL,
181                   g_cclosure_marshal_VOID__INT,
182                   G_TYPE_NONE,
183                   1,
184                   G_TYPE_INT);
185
186
187   signals [SPLIT_CHANGED] =
188     g_signal_new ("split-changed",
189                   G_TYPE_FROM_CLASS (class),
190                   G_SIGNAL_RUN_FIRST,
191                   0,
192                   NULL, NULL,
193                   g_cclosure_marshal_VOID__VOID,
194                   G_TYPE_NONE,
195                   0);
196 }
197
198 static void
199 psppire_dict_finalize (GObject *object)
200 {
201   PsppireDict *d = PSPPIRE_DICT (object);
202
203   dict_destroy (d->dict);
204
205   G_OBJECT_CLASS (parent_class)->finalize (object);
206 }
207
208 /* Pass on callbacks from src/data/dictionary, as
209    signals in the Gtk library */
210 static void
211 addcb (struct dictionary *d, int idx, void *pd)
212 {
213   g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
214 }
215
216 static void
217 delcb (struct dictionary *d, int idx, void *pd)
218 {
219   g_signal_emit (pd, signals [VARIABLES_DELETED], 0, idx, 1);
220 }
221
222 static void
223 mutcb (struct dictionary *d, int idx, void *pd)
224 {
225   g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
226 }
227
228 static void
229 weight_changed_callback (struct dictionary *d, int idx, void *pd)
230 {
231   g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
232 }
233
234 static void
235 filter_changed_callback (struct dictionary *d, int idx, void *pd)
236 {
237   g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
238 }
239
240 static void
241 split_changed_callback (struct dictionary *d, void *pd)
242 {
243   g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
244 }
245
246
247 static const struct dict_callbacks gui_callbacks =
248   {
249     addcb,
250     delcb,
251     mutcb,
252     weight_changed_callback,
253     filter_changed_callback,
254     split_changed_callback
255   };
256
257 static void
258 psppire_dict_init (PsppireDict *psppire_dict)
259 {
260   psppire_dict->stamp = g_random_int ();
261 }
262
263 /**
264  * psppire_dict_new_from_dict:
265  * @returns: a new #PsppireDict object
266  *
267  * Creates a new #PsppireDict.
268  */
269 PsppireDict*
270 psppire_dict_new_from_dict (struct dictionary *d)
271 {
272   PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
273   new_dict->dict = d;
274
275   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
276
277   return new_dict;
278 }
279
280
281 void
282 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
283 {
284   struct variable *var =  dict_get_weight (d);
285   dict->dict = d;
286
287   weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
288
289   var = dict_get_filter (d);
290   filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
291
292   split_changed_callback (d, dict);
293 }
294
295
296 /* Returns a valid name for a new variable in DICT.
297    The return value is statically allocated */
298 static gchar *
299 auto_generate_var_name (PsppireDict *dict)
300 {
301   gint d = 0;
302   static gchar name[10];
303
304   while (g_snprintf (name, 10, "VAR%05d",d++),
305          psppire_dict_lookup_var (dict, name))
306     ;
307
308   return name;
309 }
310
311 /* Insert a new variable at posn IDX, with the name NAME.
312    If NAME is null, then a name will be automatically assigned.
313 */
314 void
315 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
316 {
317   struct variable *var ;
318   g_return_if_fail (idx >= 0);
319   g_return_if_fail (d);
320   g_return_if_fail (PSPPIRE_IS_DICT (d));
321
322   if ( ! name )
323     name = auto_generate_var_name (d);
324
325   var = dict_create_var (d->dict, name, 0);
326
327   dict_reorder_var (d->dict, var, idx);
328 }
329
330 /* Delete N variables beginning at FIRST */
331 void
332 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
333 {
334   gint idx;
335   g_return_if_fail (d);
336   g_return_if_fail (d->dict);
337   g_return_if_fail (PSPPIRE_IS_DICT (d));
338
339   for (idx = 0 ; idx < n ; ++idx )
340     {
341       struct variable *var;
342
343       /* Do nothing if it's out of bounds */
344       if ( first >= dict_get_var_cnt (d->dict))
345         break;
346
347       var = dict_get_var (d->dict, first);
348       dict_delete_var (d->dict, var);
349     }
350   dict_compact_values (d->dict);
351 }
352
353
354 gboolean
355 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
356 {
357   struct variable *var;
358   g_assert (d);
359   g_assert (PSPPIRE_IS_DICT (d));
360
361   if ( ! var_is_valid_name (name, false))
362     return FALSE;
363
364   if ( idx < dict_get_var_cnt (d->dict))
365     {
366       /* This is an existing variable? */
367       var = dict_get_var (d->dict, idx);
368       dict_rename_var (d->dict, var, name);
369     }
370   else
371     {
372       /* new variable */
373       dict_create_var (d->dict, name, 0);
374     }
375
376   return TRUE;
377 }
378
379
380
381 /* Return the IDXth variable */
382 struct variable *
383 psppire_dict_get_variable (const PsppireDict *d, gint idx)
384 {
385   g_return_val_if_fail (d, NULL);
386   g_return_val_if_fail (d->dict, NULL);
387
388   if ( dict_get_var_cnt (d->dict) <= idx )
389     return NULL;
390
391   return dict_get_var (d->dict, idx);
392 }
393
394
395 /* Return the number of variables in the dictionary */
396 gint
397 psppire_dict_get_var_cnt (const PsppireDict *d)
398 {
399   g_return_val_if_fail (d, -1);
400   g_return_val_if_fail (d->dict, -1);
401
402   return dict_get_var_cnt (d->dict);
403 }
404
405
406 /* Return a variable by name.
407    Return NULL if it doesn't exist
408 */
409 struct variable *
410 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
411 {
412   g_return_val_if_fail (d, NULL);
413   g_return_val_if_fail (d->dict, NULL);
414
415   return dict_lookup_var (d->dict, name);
416 }
417
418 /* Clears the contents of D */
419 void
420 psppire_dict_clear (PsppireDict *d)
421 {
422   g_return_if_fail (d);
423   g_return_if_fail (d->dict);
424
425   {
426     dict_clear (d->dict);
427   }
428 }
429
430
431 /* Return true is NAME would be a valid name of a variable to add to the
432    dictionary.  False otherwise.
433    If REPORT is true, then invalid names will be reported as such as errors
434 */
435 gboolean
436 psppire_dict_check_name (const PsppireDict *dict,
437                          const gchar *name, gboolean report)
438 {
439   if ( ! var_is_valid_name (name, report ) )
440     return FALSE;
441
442   if (psppire_dict_lookup_var (dict, name))
443     {
444       if ( report )
445         msg (ME,"Duplicate variable name.");
446       return FALSE;
447     }
448
449   return TRUE;
450 }
451
452
453 inline gint
454 psppire_dict_get_next_value_idx (const PsppireDict *dict)
455 {
456   return dict_get_next_value_idx (dict->dict);
457 }
458
459
460 void
461 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
462                               gint old_size, gint new_size)
463 {
464   gint fv;
465   g_return_if_fail (d);
466   g_return_if_fail (d->dict);
467
468   if ( old_size == new_size )
469     return ;
470
471   dict_compact_values (d->dict);
472
473   fv = var_get_case_index (pv);
474
475   g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
476                  fv + old_size,
477                  new_size - old_size );
478 }
479
480
481 /* Tree Model Stuff */
482
483 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
484
485 static gint tree_model_n_columns (GtkTreeModel *model);
486
487 static GType tree_model_column_type (GtkTreeModel *model, gint index);
488
489 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
490                                      GtkTreePath *path);
491
492 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
493
494 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
495                                           GtkTreeIter *iter);
496
497 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
498                                   gint column, GValue *value);
499
500 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
501                                       GtkTreeIter *parent, gint n);
502
503 static gint tree_model_n_children (GtkTreeModel *tree_model,
504                                    GtkTreeIter  *iter);
505
506 static gboolean tree_model_iter_children (GtkTreeModel *,
507                                           GtkTreeIter *,
508                                           GtkTreeIter *);
509
510 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
511                                         GtkTreeIter *iter,
512                                         GtkTreeIter *child);
513
514 static gboolean tree_model_iter_has_child  (GtkTreeModel *tree_model,
515                                             GtkTreeIter  *iter);
516
517 static void
518 dictionary_tree_model_init (GtkTreeModelIface *iface)
519 {
520   iface->get_flags = tree_model_get_flags;
521   iface->get_n_columns = tree_model_n_columns;
522   iface->get_column_type = tree_model_column_type;
523   iface->get_iter = tree_model_get_iter;
524   iface->iter_next = tree_model_iter_next;
525   iface->get_path = tree_model_get_path;
526   iface->get_value = tree_model_get_value;
527
528   iface->iter_children = tree_model_iter_children ;
529   iface->iter_has_child = tree_model_iter_has_child ;
530   iface->iter_n_children = tree_model_n_children ;
531   iface->iter_nth_child = tree_model_nth_child ;
532   iface->iter_parent = tree_model_iter_parent ;
533 }
534
535 static gboolean
536 tree_model_iter_has_child  (GtkTreeModel *tree_model,
537                             GtkTreeIter  *iter)
538 {
539   return FALSE;
540 }
541
542 static gboolean
543 tree_model_iter_parent (GtkTreeModel *tree_model,
544                         GtkTreeIter *iter,
545                         GtkTreeIter *child)
546 {
547   return TRUE;
548 }
549
550 static GtkTreeModelFlags
551 tree_model_get_flags (GtkTreeModel *model)
552 {
553   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
554
555   return GTK_TREE_MODEL_LIST_ONLY;
556 }
557
558
559 static gint
560 tree_model_n_columns (GtkTreeModel *model)
561 {
562   return n_DICT_COLS;
563 }
564
565 static GType
566 tree_model_column_type (GtkTreeModel *model, gint index)
567 {
568   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
569
570   switch (index)
571     {
572     case DICT_TVM_COL_NAME:
573       return G_TYPE_STRING;
574       break;
575     case DICT_TVM_COL_VAR:
576       return G_TYPE_POINTER;
577       break;
578     default:
579       g_return_val_if_reached ((GType)0);
580       break;
581     }
582
583   g_assert_not_reached ();
584   return ((GType)0);
585 }
586
587 static gboolean
588 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
589 {
590   gint *indices, depth;
591   gint n;
592   struct variable *var;
593
594   PsppireDict *dict = PSPPIRE_DICT (model);
595
596   g_return_val_if_fail (path, FALSE);
597
598   indices = gtk_tree_path_get_indices (path);
599   depth = gtk_tree_path_get_depth (path);
600
601   g_return_val_if_fail (depth == 1, FALSE);
602
603   n = indices [0];
604
605   if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
606     {
607       iter->stamp = 0;
608       iter->user_data = NULL;
609       return FALSE;
610     }
611
612   var = psppire_dict_get_variable (dict, n);
613
614   g_assert (var_get_dict_index (var) == n);
615
616   iter->stamp = dict->stamp;
617   iter->user_data = var;
618
619   return TRUE;
620 }
621
622
623 static gboolean
624 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
625 {
626   PsppireDict *dict = PSPPIRE_DICT (model);
627   struct variable *var;
628   gint idx;
629
630   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
631
632   if ( iter == NULL || iter->user_data == NULL)
633     return FALSE;
634
635   var = iter->user_data;
636
637   idx = var_get_dict_index (var);
638
639   if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
640     {
641       iter->user_data = NULL;
642       iter->stamp = 0;
643       return FALSE;
644     }
645
646   var = psppire_dict_get_variable (dict, idx + 1);
647
648   g_assert (var_get_dict_index (var) == idx + 1);
649
650   iter->user_data = var;
651
652   return TRUE;
653 }
654
655 static GtkTreePath *
656 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
657 {
658   GtkTreePath *path;
659   struct variable *var;
660   PsppireDict *dict = PSPPIRE_DICT (model);
661
662   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
663
664   var = iter->user_data;
665
666   path = gtk_tree_path_new ();
667   gtk_tree_path_append_index (path, var_get_dict_index (var));
668
669   return path;
670 }
671
672
673 static void
674 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
675                       gint column, GValue *value)
676 {
677   struct variable *var;
678   PsppireDict *dict = PSPPIRE_DICT (model);
679
680   g_return_if_fail (iter->stamp == dict->stamp);
681
682   var =  iter->user_data;
683
684   switch (column)
685     {
686     case DICT_TVM_COL_NAME:
687       {
688       gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
689       g_value_init (value, G_TYPE_STRING);
690       g_value_set_string (value, name);
691       g_free (name);
692       }
693       break;
694     case DICT_TVM_COL_VAR:
695       g_value_init (value, G_TYPE_POINTER);
696       g_value_set_pointer (value, var);
697       break;
698     default:
699       g_return_if_reached ();
700       break;
701     }
702 }
703
704 static gboolean
705 tree_model_iter_children (GtkTreeModel *tree_model,
706                           GtkTreeIter *iter,
707                           GtkTreeIter *parent)
708 {
709   return FALSE;
710 }
711
712 static gint
713 tree_model_n_children (GtkTreeModel *model,
714                        GtkTreeIter  *iter)
715 {
716   PsppireDict *dict = PSPPIRE_DICT (model);
717
718   if ( iter == NULL )
719     return psppire_dict_get_var_cnt (dict);
720
721   return 0;
722 }
723
724 static gboolean
725 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
726                       GtkTreeIter *parent, gint n)
727 {
728   PsppireDict *dict;
729
730   g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
731
732   dict = PSPPIRE_DICT (model);
733
734   if ( parent )
735     return FALSE;
736
737   if ( n >= psppire_dict_get_var_cnt (dict) )
738     return FALSE;
739
740   iter->stamp = dict->stamp;
741   iter->user_data = psppire_dict_get_variable (dict, n);
742
743   if ( !iter->user_data)
744     return FALSE;
745
746   return TRUE;
747 }
748
749
750 gboolean
751 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
752                          const gchar *name)
753 {
754   if ( ! var_is_valid_name (name, false))
755     return FALSE;
756
757   dict_rename_var (dict->dict, v, name);
758
759   return TRUE;
760 }
761
762
763 struct variable *
764 psppire_dict_get_weight_variable (const PsppireDict *dict)
765 {
766   return dict_get_weight (dict->dict);
767 }