1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007 Free Software Foundation
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.
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.
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/>. */
22 #include <ui/gui/psppire-marshal.h>
24 #include "psppire-dict.h"
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>
32 #include "message-dialog.h"
42 VARIABLE_DISPLAY_WIDTH_CHANGED,
51 /* --- prototypes --- */
52 static void psppire_dict_class_init (PsppireDictClass *class);
53 static void psppire_dict_init (PsppireDict *dict);
54 static void psppire_dict_finalize (GObject *object);
56 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
59 /* --- variables --- */
60 static GObjectClass *parent_class = NULL;
62 static guint signals [n_SIGNALS];
64 /* --- functions --- */
66 * psppire_dict_get_type:
67 * @returns: the type ID for accelerator groups.
70 psppire_dict_get_type (void)
72 static GType object_type = 0;
76 static const GTypeInfo object_info = {
77 sizeof (PsppireDictClass),
79 (GBaseFinalizeFunc) NULL,
80 (GClassInitFunc) psppire_dict_class_init,
81 NULL, /* class_finalize */
82 NULL, /* class_data */
85 (GInstanceInitFunc) psppire_dict_init,
88 static const GInterfaceInfo tree_model_info = {
89 (GInterfaceInitFunc) dictionary_tree_model_init,
94 object_type = g_type_register_static (G_TYPE_OBJECT,
98 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
107 psppire_dict_class_init (PsppireDictClass *class)
109 GObjectClass *object_class = G_OBJECT_CLASS (class);
111 parent_class = g_type_class_peek_parent (class);
113 object_class->finalize = psppire_dict_finalize;
115 signals [BACKEND_CHANGED] =
116 g_signal_new ("backend-changed",
117 G_TYPE_FROM_CLASS (class),
121 g_cclosure_marshal_VOID__VOID,
126 signals [VARIABLE_CHANGED] =
127 g_signal_new ("variable_changed",
128 G_TYPE_FROM_CLASS (class),
132 g_cclosure_marshal_VOID__INT,
139 signals [VARIABLE_INSERTED] =
140 g_signal_new ("variable_inserted",
141 G_TYPE_FROM_CLASS (class),
145 g_cclosure_marshal_VOID__INT,
151 signals [VARIABLE_DELETED] =
152 g_signal_new ("variable-deleted",
153 G_TYPE_FROM_CLASS (class),
157 psppire_marshal_VOID__INT_INT_INT,
165 signals [VARIABLE_RESIZED] =
166 g_signal_new ("dict-size-changed",
167 G_TYPE_FROM_CLASS (class),
171 psppire_marshal_VOID__INT_INT,
177 signals [VARIABLE_DISPLAY_WIDTH_CHANGED] =
178 g_signal_new ("variable-display-width-changed",
179 G_TYPE_FROM_CLASS (class),
183 g_cclosure_marshal_VOID__INT,
189 signals [WEIGHT_CHANGED] =
190 g_signal_new ("weight-changed",
191 G_TYPE_FROM_CLASS (class),
195 g_cclosure_marshal_VOID__INT,
201 signals [FILTER_CHANGED] =
202 g_signal_new ("filter-changed",
203 G_TYPE_FROM_CLASS (class),
207 g_cclosure_marshal_VOID__INT,
213 signals [SPLIT_CHANGED] =
214 g_signal_new ("split-changed",
215 G_TYPE_FROM_CLASS (class),
219 g_cclosure_marshal_VOID__VOID,
225 psppire_dict_finalize (GObject *object)
227 PsppireDict *d = PSPPIRE_DICT (object);
229 dict_destroy (d->dict);
231 G_OBJECT_CLASS (parent_class)->finalize (object);
234 /* Pass on callbacks from src/data/dictionary, as
235 signals in the Gtk library */
237 addcb (struct dictionary *d, int idx, void *pd)
239 PsppireDict *dict = PSPPIRE_DICT (pd);
241 if ( ! dict->disable_insert_signal)
242 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
246 delcb (struct dictionary *d, int dict_idx, int case_idx, int value_cnt,
249 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
250 dict_idx, case_idx, value_cnt );
254 mutcb (struct dictionary *d, int idx, void *pd)
256 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
260 resize_cb (struct dictionary *d, int idx, int delta, void *pd)
262 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, delta);
266 weight_changed_callback (struct dictionary *d, int idx, void *pd)
268 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
272 filter_changed_callback (struct dictionary *d, int idx, void *pd)
274 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
278 split_changed_callback (struct dictionary *d, void *pd)
280 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
284 variable_display_width_callback (struct dictionary *d, int idx, void *pd)
286 g_signal_emit (pd, signals [VARIABLE_DISPLAY_WIDTH_CHANGED], 0, idx);
291 static const struct dict_callbacks gui_callbacks =
297 weight_changed_callback,
298 filter_changed_callback,
299 split_changed_callback,
300 variable_display_width_callback
304 psppire_dict_init (PsppireDict *psppire_dict)
306 psppire_dict->stamp = g_random_int ();
307 psppire_dict->disable_insert_signal = FALSE;
311 * psppire_dict_new_from_dict:
312 * @returns: a new #PsppireDict object
314 * Creates a new #PsppireDict.
317 psppire_dict_new_from_dict (struct dictionary *d)
319 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
322 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
329 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
331 struct variable *var = dict_get_weight (d);
335 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
337 var = dict_get_filter (d);
338 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
340 split_changed_callback (d, dict);
342 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
344 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
348 /* Returns a valid name for a new variable in DICT.
349 The return value is statically allocated */
351 auto_generate_var_name (PsppireDict *dict)
354 static gchar name[10];
356 while (g_snprintf (name, 10, "VAR%05d",d++),
357 psppire_dict_lookup_var (dict, name))
363 /* Insert a new variable at posn IDX, with the name NAME.
364 If NAME is null, then a name will be automatically assigned.
367 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
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));
375 name = auto_generate_var_name (d);
377 d->disable_insert_signal = TRUE;
379 var = dict_create_var (d->dict, name, 0);
381 dict_reorder_var (d->dict, var, idx);
383 d->disable_insert_signal = FALSE;
385 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
388 /* Delete N variables beginning at FIRST */
390 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
393 g_return_if_fail (d);
394 g_return_if_fail (d->dict);
395 g_return_if_fail (PSPPIRE_IS_DICT (d));
397 for (idx = 0 ; idx < n ; ++idx )
399 struct variable *var;
401 /* Do nothing if it's out of bounds */
402 if ( first >= dict_get_var_cnt (d->dict))
405 var = dict_get_var (d->dict, first);
406 dict_delete_var (d->dict, var);
412 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
414 struct variable *var;
416 g_assert (PSPPIRE_IS_DICT (d));
418 if ( ! var_is_valid_name (name, false))
421 if ( idx < dict_get_var_cnt (d->dict))
423 /* This is an existing variable? */
424 var = dict_get_var (d->dict, idx);
425 dict_rename_var (d->dict, var, name);
430 dict_create_var (d->dict, name, 0);
438 /* Return the IDXth variable.
439 Will return NULL if IDX exceeds the number of variables in the dictionary.
442 psppire_dict_get_variable (const PsppireDict *d, gint idx)
444 g_return_val_if_fail (d, NULL);
445 g_return_val_if_fail (d->dict, NULL);
447 if ( dict_get_var_cnt (d->dict) <= idx )
450 return dict_get_var (d->dict, idx);
454 /* Return the number of variables in the dictionary */
456 psppire_dict_get_var_cnt (const PsppireDict *d)
458 g_return_val_if_fail (d, -1);
459 g_return_val_if_fail (d->dict, -1);
461 return dict_get_var_cnt (d->dict);
465 /* Return the number of `union value's in the dictionary */
467 psppire_dict_get_value_cnt (const PsppireDict *d)
469 g_return_val_if_fail (d, -1);
470 g_return_val_if_fail (d->dict, -1);
472 return dict_get_next_value_idx (d->dict);
476 /* Return a variable by name.
477 Return NULL if it doesn't exist
480 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
482 g_return_val_if_fail (d, NULL);
483 g_return_val_if_fail (d->dict, NULL);
485 return dict_lookup_var (d->dict, name);
488 /* Clears the contents of D */
490 psppire_dict_clear (PsppireDict *d)
492 g_return_if_fail (d);
493 g_return_if_fail (d->dict);
496 dict_clear (d->dict);
501 /* Return true is NAME would be a valid name of a variable to add to the
502 dictionary. False otherwise.
503 If REPORT is true, then invalid names will be reported as such as errors
506 psppire_dict_check_name (const PsppireDict *dict,
507 const gchar *name, gboolean report)
509 if ( ! var_is_valid_name (name, report ) )
512 if (psppire_dict_lookup_var (dict, name))
515 msg (ME,"Duplicate variable name.");
524 psppire_dict_get_next_value_idx (const PsppireDict *dict)
526 return dict_get_next_value_idx (dict->dict);
531 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
532 gint old_size, gint new_size)
535 g_return_if_fail (d);
536 g_return_if_fail (d->dict);
538 if ( old_size == new_size )
541 fv = var_get_case_index (pv);
543 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
545 new_size - old_size );
549 /* Tree Model Stuff */
551 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
553 static gint tree_model_n_columns (GtkTreeModel *model);
555 static GType tree_model_column_type (GtkTreeModel *model, gint index);
557 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
560 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
562 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
565 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
566 gint column, GValue *value);
568 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
569 GtkTreeIter *parent, gint n);
571 static gint tree_model_n_children (GtkTreeModel *tree_model,
574 static gboolean tree_model_iter_children (GtkTreeModel *,
578 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
582 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
586 dictionary_tree_model_init (GtkTreeModelIface *iface)
588 iface->get_flags = tree_model_get_flags;
589 iface->get_n_columns = tree_model_n_columns;
590 iface->get_column_type = tree_model_column_type;
591 iface->get_iter = tree_model_get_iter;
592 iface->iter_next = tree_model_iter_next;
593 iface->get_path = tree_model_get_path;
594 iface->get_value = tree_model_get_value;
596 iface->iter_children = tree_model_iter_children ;
597 iface->iter_has_child = tree_model_iter_has_child ;
598 iface->iter_n_children = tree_model_n_children ;
599 iface->iter_nth_child = tree_model_nth_child ;
600 iface->iter_parent = tree_model_iter_parent ;
604 tree_model_iter_has_child (GtkTreeModel *tree_model,
611 tree_model_iter_parent (GtkTreeModel *tree_model,
618 static GtkTreeModelFlags
619 tree_model_get_flags (GtkTreeModel *model)
621 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
623 return GTK_TREE_MODEL_LIST_ONLY;
628 tree_model_n_columns (GtkTreeModel *model)
634 tree_model_column_type (GtkTreeModel *model, gint index)
636 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
640 case DICT_TVM_COL_NAME:
641 return G_TYPE_STRING;
643 case DICT_TVM_COL_VAR:
644 return G_TYPE_POINTER;
647 g_return_val_if_reached ((GType)0);
651 g_assert_not_reached ();
656 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
658 gint *indices, depth;
660 struct variable *var;
662 PsppireDict *dict = PSPPIRE_DICT (model);
664 g_return_val_if_fail (path, FALSE);
666 indices = gtk_tree_path_get_indices (path);
667 depth = gtk_tree_path_get_depth (path);
669 g_return_val_if_fail (depth == 1, FALSE);
673 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
676 iter->user_data = NULL;
680 var = psppire_dict_get_variable (dict, n);
682 g_assert (var_get_dict_index (var) == n);
684 iter->stamp = dict->stamp;
685 iter->user_data = var;
692 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
694 PsppireDict *dict = PSPPIRE_DICT (model);
695 struct variable *var;
698 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
700 if ( iter == NULL || iter->user_data == NULL)
703 var = iter->user_data;
705 idx = var_get_dict_index (var);
707 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
709 iter->user_data = NULL;
714 var = psppire_dict_get_variable (dict, idx + 1);
716 g_assert (var_get_dict_index (var) == idx + 1);
718 iter->user_data = var;
724 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
727 struct variable *var;
728 PsppireDict *dict = PSPPIRE_DICT (model);
730 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
732 var = iter->user_data;
734 path = gtk_tree_path_new ();
735 gtk_tree_path_append_index (path, var_get_dict_index (var));
742 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
743 gint column, GValue *value)
745 struct variable *var;
746 PsppireDict *dict = PSPPIRE_DICT (model);
748 g_return_if_fail (iter->stamp == dict->stamp);
750 var = iter->user_data;
754 case DICT_TVM_COL_NAME:
756 gchar *name = recode_string (UTF8, psppire_dict_encoding (dict),
757 var_get_name (var), -1);
758 g_value_init (value, G_TYPE_STRING);
759 g_value_set_string (value, name);
763 case DICT_TVM_COL_VAR:
764 g_value_init (value, G_TYPE_POINTER);
765 g_value_set_pointer (value, var);
768 g_return_if_reached ();
774 tree_model_iter_children (GtkTreeModel *tree_model,
782 tree_model_n_children (GtkTreeModel *model,
785 PsppireDict *dict = PSPPIRE_DICT (model);
788 return psppire_dict_get_var_cnt (dict);
794 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
795 GtkTreeIter *parent, gint n)
799 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
801 dict = PSPPIRE_DICT (model);
806 if ( n >= psppire_dict_get_var_cnt (dict) )
809 iter->stamp = dict->stamp;
810 iter->user_data = psppire_dict_get_variable (dict, n);
812 if ( !iter->user_data)
820 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
823 if ( ! var_is_valid_name (name, false))
826 /* Make sure no other variable has this name */
827 if ( NULL != psppire_dict_lookup_var (dict, name))
830 dict_rename_var (dict->dict, v, name);
837 psppire_dict_get_weight_variable (const PsppireDict *dict)
839 return dict_get_weight (dict->dict);
846 psppire_dict_dump (const PsppireDict *dict)
849 const struct dictionary *d = dict->dict;
851 for (i = 0; i < dict_get_var_cnt (d); ++i)
853 const struct variable *v = psppire_dict_get_variable (dict, i);
854 int di = var_get_dict_index (v);
855 g_print ("\"%s\" idx=%d, fv=%d, size=%d\n",
858 var_get_case_index(v),
859 value_cnt_from_width(var_get_width(v)));
869 psppire_dict_encoding (const PsppireDict *dict)
871 return dict_get_encoding (dict->dict);