1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009 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/>. */
19 #include "ui/gui/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"
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"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
46 VARIABLE_DISPLAY_WIDTH_CHANGED,
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);
60 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
63 /* --- variables --- */
64 static GObjectClass *parent_class = NULL;
66 static guint signals [n_SIGNALS];
68 /* --- functions --- */
70 * psppire_dict_get_type:
71 * @returns: the type ID for accelerator groups.
74 psppire_dict_get_type (void)
76 static GType object_type = 0;
80 static const GTypeInfo object_info = {
81 sizeof (PsppireDictClass),
83 (GBaseFinalizeFunc) NULL,
84 (GClassInitFunc) psppire_dict_class_init,
85 NULL, /* class_finalize */
86 NULL, /* class_data */
89 (GInstanceInitFunc) psppire_dict_init,
92 static const GInterfaceInfo tree_model_info = {
93 (GInterfaceInitFunc) dictionary_tree_model_init,
98 object_type = g_type_register_static (G_TYPE_OBJECT,
102 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
111 psppire_dict_class_init (PsppireDictClass *class)
113 GObjectClass *object_class = G_OBJECT_CLASS (class);
115 parent_class = g_type_class_peek_parent (class);
117 object_class->finalize = psppire_dict_finalize;
119 signals [BACKEND_CHANGED] =
120 g_signal_new ("backend-changed",
121 G_TYPE_FROM_CLASS (class),
125 g_cclosure_marshal_VOID__VOID,
130 signals [VARIABLE_CHANGED] =
131 g_signal_new ("variable_changed",
132 G_TYPE_FROM_CLASS (class),
136 g_cclosure_marshal_VOID__INT,
143 signals [VARIABLE_INSERTED] =
144 g_signal_new ("variable_inserted",
145 G_TYPE_FROM_CLASS (class),
149 g_cclosure_marshal_VOID__INT,
155 signals [VARIABLE_DELETED] =
156 g_signal_new ("variable-deleted",
157 G_TYPE_FROM_CLASS (class),
161 psppire_marshal_VOID__INT_INT_INT,
169 signals [VARIABLE_RESIZED] =
170 g_signal_new ("dict-size-changed",
171 G_TYPE_FROM_CLASS (class),
175 psppire_marshal_VOID__INT_INT,
181 signals [VARIABLE_DISPLAY_WIDTH_CHANGED] =
182 g_signal_new ("variable-display-width-changed",
183 G_TYPE_FROM_CLASS (class),
187 g_cclosure_marshal_VOID__INT,
193 signals [WEIGHT_CHANGED] =
194 g_signal_new ("weight-changed",
195 G_TYPE_FROM_CLASS (class),
199 g_cclosure_marshal_VOID__INT,
205 signals [FILTER_CHANGED] =
206 g_signal_new ("filter-changed",
207 G_TYPE_FROM_CLASS (class),
211 g_cclosure_marshal_VOID__INT,
217 signals [SPLIT_CHANGED] =
218 g_signal_new ("split-changed",
219 G_TYPE_FROM_CLASS (class),
223 g_cclosure_marshal_VOID__VOID,
229 psppire_dict_finalize (GObject *object)
231 PsppireDict *d = PSPPIRE_DICT (object);
233 dict_destroy (d->dict);
235 G_OBJECT_CLASS (parent_class)->finalize (object);
238 /* Pass on callbacks from src/data/dictionary, as
239 signals in the Gtk library */
241 addcb (struct dictionary *d, int idx, void *pd)
243 PsppireDict *dict = PSPPIRE_DICT (pd);
245 if ( ! dict->disable_insert_signal)
246 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
250 delcb (struct dictionary *d, int dict_idx, int case_idx, int width, void *pd)
252 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
253 dict_idx, case_idx, width );
257 mutcb (struct dictionary *d, int idx, void *pd)
259 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
263 resize_cb (struct dictionary *d, int idx, int old_width, void *pd)
265 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, old_width);
269 weight_changed_callback (struct dictionary *d, int idx, void *pd)
271 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
275 filter_changed_callback (struct dictionary *d, int idx, void *pd)
277 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
281 split_changed_callback (struct dictionary *d, void *pd)
283 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
287 variable_display_width_callback (struct dictionary *d, int idx, void *pd)
289 g_signal_emit (pd, signals [VARIABLE_DISPLAY_WIDTH_CHANGED], 0, idx);
294 static const struct dict_callbacks gui_callbacks =
300 weight_changed_callback,
301 filter_changed_callback,
302 split_changed_callback,
303 variable_display_width_callback
307 psppire_dict_init (PsppireDict *psppire_dict)
309 psppire_dict->stamp = g_random_int ();
310 psppire_dict->disable_insert_signal = FALSE;
314 * psppire_dict_new_from_dict:
315 * @returns: a new #PsppireDict object
317 * Creates a new #PsppireDict.
320 psppire_dict_new_from_dict (struct dictionary *d)
322 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
325 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
332 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
334 struct variable *var = dict_get_weight (d);
338 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
340 var = dict_get_filter (d);
341 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
343 split_changed_callback (d, dict);
345 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
347 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
351 /* Returns a valid name for a new variable in DICT.
352 The return value is statically allocated */
354 auto_generate_var_name (PsppireDict *dict)
357 static gchar name[10];
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 '_'
366 while (g_snprintf (name, 10, _("VAR%05d"), d++),
367 psppire_dict_lookup_var (dict, name))
373 /* Insert a new variable at posn IDX, with the name NAME.
374 If NAME is null, then a name will be automatically assigned.
377 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
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));
385 name = auto_generate_var_name (d);
387 d->disable_insert_signal = TRUE;
389 var = dict_create_var (d->dict, name, 0);
391 dict_reorder_var (d->dict, var, idx);
393 d->disable_insert_signal = FALSE;
395 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
398 /* Delete N variables beginning at FIRST */
400 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
403 g_return_if_fail (d);
404 g_return_if_fail (d->dict);
405 g_return_if_fail (PSPPIRE_IS_DICT (d));
407 for (idx = 0 ; idx < n ; ++idx )
409 struct variable *var;
411 /* Do nothing if it's out of bounds */
412 if ( first >= dict_get_var_cnt (d->dict))
415 var = dict_get_var (d->dict, first);
416 dict_delete_var (d->dict, var);
422 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
424 struct variable *var;
426 g_assert (PSPPIRE_IS_DICT (d));
428 if ( ! var_is_valid_name (name, false))
431 if ( idx < dict_get_var_cnt (d->dict))
433 /* This is an existing variable? */
434 var = dict_get_var (d->dict, idx);
435 dict_rename_var (d->dict, var, name);
440 dict_create_var (d->dict, name, 0);
448 /* Return the IDXth variable.
449 Will return NULL if IDX exceeds the number of variables in the dictionary.
452 psppire_dict_get_variable (const PsppireDict *d, gint idx)
454 g_return_val_if_fail (d, NULL);
455 g_return_val_if_fail (d->dict, NULL);
457 if ( dict_get_var_cnt (d->dict) <= idx )
460 return dict_get_var (d->dict, idx);
464 /* Return the number of variables in the dictionary */
466 psppire_dict_get_var_cnt (const PsppireDict *d)
468 g_return_val_if_fail (d, -1);
469 g_return_val_if_fail (d->dict, -1);
471 return dict_get_var_cnt (d->dict);
475 /* Return the number of `union value's in the dictionary */
477 psppire_dict_get_value_cnt (const PsppireDict *d)
479 g_return_val_if_fail (d, -1);
480 g_return_val_if_fail (d->dict, -1);
482 return dict_get_next_value_idx (d->dict);
486 /* Returns the prototype for the cases that match the dictionary */
487 const struct caseproto *
488 psppire_dict_get_proto (const PsppireDict *d)
490 g_return_val_if_fail (d, NULL);
491 g_return_val_if_fail (d->dict, NULL);
493 return dict_get_proto (d->dict);
497 /* Return a variable by name.
498 Return NULL if it doesn't exist
501 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
503 g_return_val_if_fail (d, NULL);
504 g_return_val_if_fail (d->dict, NULL);
506 return dict_lookup_var (d->dict, name);
509 /* Clears the contents of D */
511 psppire_dict_clear (PsppireDict *d)
513 g_return_if_fail (d);
514 g_return_if_fail (d->dict);
517 dict_clear (d->dict);
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
527 psppire_dict_check_name (const PsppireDict *dict,
528 const gchar *name, gboolean report)
530 if ( ! var_is_valid_name (name, report ) )
533 if (psppire_dict_lookup_var (dict, name))
536 msg (ME,"Duplicate variable name.");
545 psppire_dict_get_next_value_idx (const PsppireDict *dict)
547 return dict_get_next_value_idx (dict->dict);
552 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
553 gint old_size, gint new_size)
555 g_return_if_fail (d);
556 g_return_if_fail (d->dict);
558 if ( old_size == new_size )
561 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
562 var_get_dict_index (pv),
563 new_size - old_size );
567 /* Tree Model Stuff */
569 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
571 static gint tree_model_n_columns (GtkTreeModel *model);
573 static GType tree_model_column_type (GtkTreeModel *model, gint index);
575 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
578 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
580 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
583 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
584 gint column, GValue *value);
586 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
587 GtkTreeIter *parent, gint n);
589 static gint tree_model_n_children (GtkTreeModel *tree_model,
592 static gboolean tree_model_iter_children (GtkTreeModel *,
596 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
600 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
604 dictionary_tree_model_init (GtkTreeModelIface *iface)
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;
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 ;
622 tree_model_iter_has_child (GtkTreeModel *tree_model,
629 tree_model_iter_parent (GtkTreeModel *tree_model,
636 static GtkTreeModelFlags
637 tree_model_get_flags (GtkTreeModel *model)
639 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
641 return GTK_TREE_MODEL_LIST_ONLY;
646 tree_model_n_columns (GtkTreeModel *model)
652 tree_model_column_type (GtkTreeModel *model, gint index)
654 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
658 case DICT_TVM_COL_NAME:
659 return G_TYPE_STRING;
661 case DICT_TVM_COL_VAR:
662 return PSPPIRE_VAR_PTR_TYPE;
665 g_return_val_if_reached ((GType)0);
669 g_assert_not_reached ();
674 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
676 gint *indices, depth;
678 struct variable *var;
680 PsppireDict *dict = PSPPIRE_DICT (model);
682 g_return_val_if_fail (path, FALSE);
684 indices = gtk_tree_path_get_indices (path);
685 depth = gtk_tree_path_get_depth (path);
687 g_return_val_if_fail (depth == 1, FALSE);
691 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
694 iter->user_data = NULL;
698 var = psppire_dict_get_variable (dict, n);
700 g_assert (var_get_dict_index (var) == n);
702 iter->stamp = dict->stamp;
703 iter->user_data = var;
710 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
712 PsppireDict *dict = PSPPIRE_DICT (model);
713 struct variable *var;
716 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
718 if ( iter == NULL || iter->user_data == NULL)
721 var = iter->user_data;
723 idx = var_get_dict_index (var);
725 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
727 iter->user_data = NULL;
732 var = psppire_dict_get_variable (dict, idx + 1);
734 g_assert (var_get_dict_index (var) == idx + 1);
736 iter->user_data = var;
742 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
745 struct variable *var;
746 PsppireDict *dict = PSPPIRE_DICT (model);
748 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
750 var = iter->user_data;
752 path = gtk_tree_path_new ();
753 gtk_tree_path_append_index (path, var_get_dict_index (var));
760 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
761 gint column, GValue *value)
763 struct variable *var;
764 PsppireDict *dict = PSPPIRE_DICT (model);
766 g_return_if_fail (iter->stamp == dict->stamp);
768 var = iter->user_data;
772 case DICT_TVM_COL_NAME:
774 g_value_init (value, G_TYPE_STRING);
775 g_value_set_string (value, var_get_name (var));
778 case DICT_TVM_COL_VAR:
779 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
780 g_value_set_boxed (value, var);
783 g_return_if_reached ();
789 tree_model_iter_children (GtkTreeModel *tree_model,
797 tree_model_n_children (GtkTreeModel *model,
800 PsppireDict *dict = PSPPIRE_DICT (model);
803 return psppire_dict_get_var_cnt (dict);
809 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
810 GtkTreeIter *parent, gint n)
814 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
816 dict = PSPPIRE_DICT (model);
821 if ( n >= psppire_dict_get_var_cnt (dict) )
824 iter->stamp = dict->stamp;
825 iter->user_data = psppire_dict_get_variable (dict, n);
827 if ( !iter->user_data)
835 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
838 if ( ! var_is_valid_name (name, false))
841 /* Make sure no other variable has this name */
842 if ( NULL != psppire_dict_lookup_var (dict, name))
845 dict_rename_var (dict->dict, v, name);
852 psppire_dict_get_weight_variable (const PsppireDict *dict)
854 return dict_get_weight (dict->dict);
861 psppire_dict_dump (const PsppireDict *dict)
864 const struct dictionary *d = dict->dict;
866 for (i = 0; i < dict_get_var_cnt (d); ++i)
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",
873 var_get_case_index(v));
883 psppire_dict_encoding (const PsppireDict *dict)
885 return dict_get_encoding (dict->dict);