1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012 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/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"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
47 VARIABLE_DISPLAY_WIDTH_CHANGED,
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);
61 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
64 /* --- variables --- */
65 static GObjectClass *parent_class = NULL;
67 static guint signals [n_SIGNALS];
69 /* --- functions --- */
71 * psppire_dict_get_type:
72 * @returns: the type ID for accelerator groups.
75 psppire_dict_get_type (void)
77 static GType object_type = 0;
81 static const GTypeInfo object_info = {
82 sizeof (PsppireDictClass),
84 (GBaseFinalizeFunc) NULL,
85 (GClassInitFunc) psppire_dict_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
90 (GInstanceInitFunc) psppire_dict_init,
93 static const GInterfaceInfo tree_model_info = {
94 (GInterfaceInitFunc) dictionary_tree_model_init,
99 object_type = g_type_register_static (G_TYPE_OBJECT,
103 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
112 psppire_dict_class_init (PsppireDictClass *class)
114 GObjectClass *object_class = G_OBJECT_CLASS (class);
116 parent_class = g_type_class_peek_parent (class);
118 object_class->finalize = psppire_dict_finalize;
120 signals [BACKEND_CHANGED] =
121 g_signal_new ("backend-changed",
122 G_TYPE_FROM_CLASS (class),
126 g_cclosure_marshal_VOID__VOID,
131 signals [VARIABLE_CHANGED] =
132 g_signal_new ("variable_changed",
133 G_TYPE_FROM_CLASS (class),
137 g_cclosure_marshal_VOID__INT,
144 signals [VARIABLE_INSERTED] =
145 g_signal_new ("variable_inserted",
146 G_TYPE_FROM_CLASS (class),
150 g_cclosure_marshal_VOID__INT,
156 signals [VARIABLE_DELETED] =
157 g_signal_new ("variable-deleted",
158 G_TYPE_FROM_CLASS (class),
162 psppire_marshal_VOID__POINTER_INT_INT,
170 signals [VARIABLE_RESIZED] =
171 g_signal_new ("dict-size-changed",
172 G_TYPE_FROM_CLASS (class),
176 psppire_marshal_VOID__INT_INT,
182 signals [VARIABLE_DISPLAY_WIDTH_CHANGED] =
183 g_signal_new ("variable-display-width-changed",
184 G_TYPE_FROM_CLASS (class),
188 g_cclosure_marshal_VOID__INT,
194 signals [WEIGHT_CHANGED] =
195 g_signal_new ("weight-changed",
196 G_TYPE_FROM_CLASS (class),
200 g_cclosure_marshal_VOID__INT,
206 signals [FILTER_CHANGED] =
207 g_signal_new ("filter-changed",
208 G_TYPE_FROM_CLASS (class),
212 g_cclosure_marshal_VOID__INT,
218 signals [SPLIT_CHANGED] =
219 g_signal_new ("split-changed",
220 G_TYPE_FROM_CLASS (class),
224 g_cclosure_marshal_VOID__VOID,
230 psppire_dict_finalize (GObject *object)
232 PsppireDict *d = PSPPIRE_DICT (object);
234 dict_destroy (d->dict);
236 G_OBJECT_CLASS (parent_class)->finalize (object);
239 /* Pass on callbacks from src/data/dictionary, as
240 signals in the Gtk library */
242 addcb (struct dictionary *d, int idx, void *pd)
244 PsppireDict *dict = PSPPIRE_DICT (pd);
246 if ( ! dict->disable_insert_signal)
247 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
251 delcb (struct dictionary *d, const struct variable *var,
252 int dict_idx, int case_idx, void *pd)
254 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
255 var, dict_idx, case_idx);
259 mutcb (struct dictionary *d, int idx, void *pd)
261 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
265 resize_cb (struct dictionary *d, int idx, int old_width, void *pd)
267 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, old_width);
271 weight_changed_callback (struct dictionary *d, int idx, void *pd)
273 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
277 filter_changed_callback (struct dictionary *d, int idx, void *pd)
279 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
283 split_changed_callback (struct dictionary *d, void *pd)
285 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
289 variable_display_width_callback (struct dictionary *d, int idx, void *pd)
291 g_signal_emit (pd, signals [VARIABLE_DISPLAY_WIDTH_CHANGED], 0, idx);
296 static const struct dict_callbacks gui_callbacks =
302 weight_changed_callback,
303 filter_changed_callback,
304 split_changed_callback,
305 variable_display_width_callback
309 psppire_dict_init (PsppireDict *psppire_dict)
311 psppire_dict->stamp = g_random_int ();
312 psppire_dict->disable_insert_signal = FALSE;
316 * psppire_dict_new_from_dict:
317 * @returns: a new #PsppireDict object
319 * Creates a new #PsppireDict.
322 psppire_dict_new_from_dict (struct dictionary *d)
324 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
327 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
334 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
336 struct variable *var = dict_get_weight (d);
340 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
342 var = dict_get_filter (d);
343 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
345 split_changed_callback (d, dict);
347 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
349 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
353 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
354 Returns true if successful, false if SIZE is insufficient. */
356 auto_generate_var_name (const PsppireDict *dict, char *name, size_t size)
364 /* TRANSLATORS: This string must be a valid variable name. That means:
365 - The string must be at most 64 bytes (not characters) long.
366 - The string may not contain whitespace.
367 - The first character may not be '$'
368 - The first character may not be a digit
369 - The final charactor may not be '.' or '_'
371 len = snprintf (name, size, _("Var%04d"), d);
375 if (psppire_dict_lookup_var (dict, name) == NULL)
382 /* Insert a new variable at posn IDX, with the name NAME.
383 If NAME is null, then a name will be automatically assigned.
386 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
388 struct variable *var ;
391 g_return_if_fail (idx >= 0);
392 g_return_if_fail (d);
393 g_return_if_fail (PSPPIRE_IS_DICT (d));
397 if (!auto_generate_var_name (d, tmpname, sizeof tmpname))
398 g_return_if_reached ();
403 d->disable_insert_signal = TRUE;
405 var = dict_create_var (d->dict, name, 0);
407 dict_reorder_var (d->dict, var, idx);
409 d->disable_insert_signal = FALSE;
411 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
414 /* Delete N variables beginning at FIRST */
416 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
419 g_return_if_fail (d);
420 g_return_if_fail (d->dict);
421 g_return_if_fail (PSPPIRE_IS_DICT (d));
423 for (idx = 0 ; idx < n ; ++idx )
425 struct variable *var;
427 /* Do nothing if it's out of bounds */
428 if ( first >= dict_get_var_cnt (d->dict))
431 var = dict_get_var (d->dict, first);
432 dict_delete_var (d->dict, var);
438 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
440 struct variable *var;
442 g_assert (PSPPIRE_IS_DICT (d));
444 if ( ! dict_id_is_valid (d->dict, name, false))
447 if ( idx < dict_get_var_cnt (d->dict))
449 /* This is an existing variable? */
450 var = dict_get_var (d->dict, idx);
451 dict_rename_var (d->dict, var, name);
456 dict_create_var (d->dict, name, 0);
464 /* Return the IDXth variable.
465 Will return NULL if IDX exceeds the number of variables in the dictionary.
468 psppire_dict_get_variable (const PsppireDict *d, gint idx)
470 g_return_val_if_fail (d, NULL);
471 g_return_val_if_fail (d->dict, NULL);
473 if ( dict_get_var_cnt (d->dict) <= idx )
476 return dict_get_var (d->dict, idx);
480 /* Return the number of variables in the dictionary */
482 psppire_dict_get_var_cnt (const PsppireDict *d)
484 g_return_val_if_fail (d, -1);
485 g_return_val_if_fail (d->dict, -1);
487 return dict_get_var_cnt (d->dict);
491 /* Return the number of `union value's in the dictionary */
493 psppire_dict_get_value_cnt (const PsppireDict *d)
495 g_return_val_if_fail (d, -1);
496 g_return_val_if_fail (d->dict, -1);
498 return dict_get_next_value_idx (d->dict);
502 /* Returns the prototype for the cases that match the dictionary */
503 const struct caseproto *
504 psppire_dict_get_proto (const PsppireDict *d)
506 g_return_val_if_fail (d, NULL);
507 g_return_val_if_fail (d->dict, NULL);
509 return dict_get_proto (d->dict);
513 /* Return a variable by name.
514 Return NULL if it doesn't exist
517 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
519 g_return_val_if_fail (d, NULL);
520 g_return_val_if_fail (d->dict, NULL);
522 return dict_lookup_var (d->dict, name);
525 /* Clears the contents of D */
527 psppire_dict_clear (PsppireDict *d)
529 g_return_if_fail (d);
530 g_return_if_fail (d->dict);
533 dict_clear (d->dict);
538 /* Return true is NAME would be a valid name of a variable to add to the
539 dictionary. False otherwise.
540 If REPORT is true, then invalid names will be reported as such as errors
543 psppire_dict_check_name (const PsppireDict *dict,
544 const gchar *name, gboolean report)
546 if ( ! dict_id_is_valid (dict->dict, name, report ) )
549 if (psppire_dict_lookup_var (dict, name))
552 msg (ME, _("Duplicate variable name."));
561 psppire_dict_get_next_value_idx (const PsppireDict *dict)
563 return dict_get_next_value_idx (dict->dict);
568 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
569 gint old_size, gint new_size)
571 g_return_if_fail (d);
572 g_return_if_fail (d->dict);
574 if ( old_size == new_size )
577 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
578 var_get_dict_index (pv),
579 new_size - old_size );
583 /* Tree Model Stuff */
585 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
587 static gint tree_model_n_columns (GtkTreeModel *model);
589 static GType tree_model_column_type (GtkTreeModel *model, gint index);
591 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
594 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
596 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
599 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
600 gint column, GValue *value);
602 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
603 GtkTreeIter *parent, gint n);
605 static gint tree_model_n_children (GtkTreeModel *tree_model,
608 static gboolean tree_model_iter_children (GtkTreeModel *,
612 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
616 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
620 dictionary_tree_model_init (GtkTreeModelIface *iface)
622 iface->get_flags = tree_model_get_flags;
623 iface->get_n_columns = tree_model_n_columns;
624 iface->get_column_type = tree_model_column_type;
625 iface->get_iter = tree_model_get_iter;
626 iface->iter_next = tree_model_iter_next;
627 iface->get_path = tree_model_get_path;
628 iface->get_value = tree_model_get_value;
630 iface->iter_children = tree_model_iter_children ;
631 iface->iter_has_child = tree_model_iter_has_child ;
632 iface->iter_n_children = tree_model_n_children ;
633 iface->iter_nth_child = tree_model_nth_child ;
634 iface->iter_parent = tree_model_iter_parent ;
638 tree_model_iter_has_child (GtkTreeModel *tree_model,
645 tree_model_iter_parent (GtkTreeModel *tree_model,
652 static GtkTreeModelFlags
653 tree_model_get_flags (GtkTreeModel *model)
655 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
657 return GTK_TREE_MODEL_LIST_ONLY;
662 tree_model_n_columns (GtkTreeModel *model)
668 tree_model_column_type (GtkTreeModel *model, gint index)
670 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
674 case DICT_TVM_COL_NAME:
675 return G_TYPE_STRING;
677 case DICT_TVM_COL_VAR:
678 return PSPPIRE_VAR_PTR_TYPE;
681 g_return_val_if_reached ((GType)0);
685 g_assert_not_reached ();
690 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
692 gint *indices, depth;
694 struct variable *var;
696 PsppireDict *dict = PSPPIRE_DICT (model);
698 g_return_val_if_fail (path, FALSE);
700 indices = gtk_tree_path_get_indices (path);
701 depth = gtk_tree_path_get_depth (path);
703 g_return_val_if_fail (depth == 1, FALSE);
707 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
710 iter->user_data = NULL;
714 var = psppire_dict_get_variable (dict, n);
716 g_assert (var_get_dict_index (var) == n);
718 iter->stamp = dict->stamp;
719 iter->user_data = var;
726 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
728 PsppireDict *dict = PSPPIRE_DICT (model);
729 struct variable *var;
732 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
734 if ( iter == NULL || iter->user_data == NULL)
737 var = iter->user_data;
739 idx = var_get_dict_index (var);
741 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
743 iter->user_data = NULL;
748 var = psppire_dict_get_variable (dict, idx + 1);
750 g_assert (var_get_dict_index (var) == idx + 1);
752 iter->user_data = var;
758 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
761 struct variable *var;
762 PsppireDict *dict = PSPPIRE_DICT (model);
764 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
766 var = iter->user_data;
768 path = gtk_tree_path_new ();
769 gtk_tree_path_append_index (path, var_get_dict_index (var));
776 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
777 gint column, GValue *value)
779 struct variable *var;
780 PsppireDict *dict = PSPPIRE_DICT (model);
782 g_return_if_fail (iter->stamp == dict->stamp);
784 var = iter->user_data;
788 case DICT_TVM_COL_NAME:
790 g_value_init (value, G_TYPE_STRING);
791 g_value_set_string (value, var_get_name (var));
794 case DICT_TVM_COL_VAR:
795 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
796 g_value_set_boxed (value, var);
799 g_return_if_reached ();
805 tree_model_iter_children (GtkTreeModel *tree_model,
813 tree_model_n_children (GtkTreeModel *model,
816 PsppireDict *dict = PSPPIRE_DICT (model);
819 return psppire_dict_get_var_cnt (dict);
825 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
826 GtkTreeIter *parent, gint n)
830 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
832 dict = PSPPIRE_DICT (model);
837 if ( n >= psppire_dict_get_var_cnt (dict) )
840 iter->stamp = dict->stamp;
841 iter->user_data = psppire_dict_get_variable (dict, n);
843 if ( !iter->user_data)
851 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
854 if ( ! dict_id_is_valid (dict->dict, name, false))
857 /* Make sure no other variable has this name */
858 if ( NULL != psppire_dict_lookup_var (dict, name))
861 dict_rename_var (dict->dict, v, name);
868 psppire_dict_get_weight_variable (const PsppireDict *dict)
870 return dict_get_weight (dict->dict);
877 psppire_dict_dump (const PsppireDict *dict)
880 const struct dictionary *d = dict->dict;
882 for (i = 0; i < dict_get_var_cnt (d); ++i)
884 const struct variable *v = psppire_dict_get_variable (dict, i);
885 int di = var_get_dict_index (v);
886 g_print ("`%s' idx=%d, fv=%d\n",
889 var_get_case_index(v));
899 psppire_dict_encoding (const PsppireDict *dict)
901 return dict_get_encoding (dict->dict);