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"
36 #include "ui/gui/efficient-sheet/jmd-datum.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
58 /* --- prototypes --- */
59 static void psppire_dict_class_init (PsppireDictClass *class);
60 static void psppire_dict_init (PsppireDict *dict);
61 static void psppire_dict_dispose (GObject *object);
63 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
68 gni (GListModel *list)
70 PsppireDict *dict = PSPPIRE_DICT (list);
72 return psppire_dict_get_var_cnt (dict);
76 git (GListModel *list)
78 return JMD_TYPE_DATUM;
82 gi (GListModel *list, guint id)
84 JmdDatum *gd = JMD_DATUM (g_object_new (JMD_TYPE_DATUM, NULL));
86 PsppireDict *dict = PSPPIRE_DICT (list);
88 if (id >= psppire_dict_get_var_cnt (dict))
90 gd->text = g_strdup (_("Var"));
94 const struct variable *v = psppire_dict_get_variable (dict, id);
96 gd->text = g_strdup (var_get_name (v));
97 gd->label = g_strdup (var_get_label (v));
105 jmd_init_iface (GListModelInterface *iface)
107 iface->get_n_items = gni;
108 iface->get_item = gi;
109 iface->get_item_type = git;
113 /* --- variables --- */
114 static GObjectClass *parent_class = NULL;
116 static guint signals [n_SIGNALS];
118 /* --- functions --- */
120 * psppire_dict_get_type:
121 * @returns: the type ID for accelerator groups.
124 psppire_dict_get_type (void)
126 static GType object_type = 0;
130 static const GTypeInfo object_info = {
131 sizeof (PsppireDictClass),
132 (GBaseInitFunc) NULL,
133 (GBaseFinalizeFunc) NULL,
134 (GClassInitFunc) psppire_dict_class_init,
135 NULL, /* class_finalize */
136 NULL, /* class_data */
137 sizeof (PsppireDict),
139 (GInstanceInitFunc) psppire_dict_init,
142 static const GInterfaceInfo tree_model_info = {
143 (GInterfaceInitFunc) dictionary_tree_model_init,
148 static const GInterfaceInfo list_model_info = {
149 (GInterfaceInitFunc) jmd_init_iface,
154 object_type = g_type_register_static (G_TYPE_OBJECT,
158 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
161 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
170 psppire_dict_class_init (PsppireDictClass *class)
172 GObjectClass *object_class = G_OBJECT_CLASS (class);
174 parent_class = g_type_class_peek_parent (class);
176 object_class->dispose = psppire_dict_dispose;
178 signals [BACKEND_CHANGED] =
179 g_signal_new ("backend-changed",
180 G_TYPE_FROM_CLASS (class),
184 g_cclosure_marshal_VOID__VOID,
188 signals [ITEMS_CHANGED] =
189 g_signal_new ("changed",
190 G_TYPE_FROM_CLASS (class),
194 psppire_marshal_VOID__UINT_UINT_UINT,
202 signals [VARIABLE_CHANGED] =
203 g_signal_new ("variable-changed",
204 G_TYPE_FROM_CLASS (class),
208 psppire_marshal_VOID__INT_UINT_POINTER,
218 signals [VARIABLE_INSERTED] =
219 g_signal_new ("variable-inserted",
220 G_TYPE_FROM_CLASS (class),
224 g_cclosure_marshal_VOID__INT,
230 signals [VARIABLE_DELETED] =
231 g_signal_new ("variable-deleted",
232 G_TYPE_FROM_CLASS (class),
236 psppire_marshal_VOID__POINTER_INT_INT,
244 signals [WEIGHT_CHANGED] =
245 g_signal_new ("weight-changed",
246 G_TYPE_FROM_CLASS (class),
250 g_cclosure_marshal_VOID__INT,
256 signals [FILTER_CHANGED] =
257 g_signal_new ("filter-changed",
258 G_TYPE_FROM_CLASS (class),
262 g_cclosure_marshal_VOID__INT,
268 signals [SPLIT_CHANGED] =
269 g_signal_new ("split-changed",
270 G_TYPE_FROM_CLASS (class),
274 g_cclosure_marshal_VOID__VOID,
280 psppire_dict_dispose (GObject *object)
282 PsppireDict *d = PSPPIRE_DICT (object);
284 dict_set_callbacks (d->dict, NULL, NULL);
286 G_OBJECT_CLASS (parent_class)->dispose (object);
289 /* Pass on callbacks from src/data/dictionary, as
290 signals in the Gtk library */
292 addcb (struct dictionary *d, int idx, void *pd)
294 PsppireDict *dict = PSPPIRE_DICT (pd);
296 if ( ! dict->disable_insert_signal)
298 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
299 g_signal_emit (dict, signals [ITEMS_CHANGED], 0, idx, 1, 1);
304 delcb (struct dictionary *d, const struct variable *var,
305 int dict_idx, int case_idx, void *pd)
307 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
308 var, dict_idx, case_idx);
309 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, dict_idx, 1, 0);
313 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
315 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
316 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, idx, 1, 1);
320 weight_changed_callback (struct dictionary *d, int idx, void *pd)
322 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
326 filter_changed_callback (struct dictionary *d, int idx, void *pd)
328 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
332 split_changed_callback (struct dictionary *d, void *pd)
334 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
337 static const struct dict_callbacks gui_callbacks =
342 weight_changed_callback,
343 filter_changed_callback,
344 split_changed_callback
348 psppire_dict_init (PsppireDict *psppire_dict)
350 psppire_dict->stamp = g_random_int ();
351 psppire_dict->disable_insert_signal = FALSE;
355 * psppire_dict_new_from_dict:
356 * @returns: a new #PsppireDict object
358 * Creates a new #PsppireDict.
361 psppire_dict_new_from_dict (struct dictionary *d)
363 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
366 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
373 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
375 struct variable *var = dict_get_weight (d);
377 guint old_n = dict_get_var_cnt (dict->dict);
378 guint new_n = dict_get_var_cnt (d);
382 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
384 var = dict_get_filter (d);
385 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
387 split_changed_callback (d, dict);
389 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
391 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
392 g_signal_emit (dict, signals [ITEMS_CHANGED], 0, 0, old_n, new_n);
396 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
397 Returns true if successful, false if SIZE is insufficient. */
399 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
407 /* TRANSLATORS: This string must be a valid variable name. That means:
408 - The string must be at most 64 bytes (not characters) long.
409 - The string may not contain whitespace.
410 - The first character may not be '$'
411 - The first character may not be a digit
412 - The final charactor may not be '.' or '_'
414 len = snprintf (name, size, _("Var%04d"), d);
418 if (psppire_dict_lookup_var (dict, name) == NULL)
425 /* Insert a new variable at posn IDX, with the name NAME, and return the
427 If NAME is null, then a name will be automatically assigned.
430 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
432 struct variable *var;
435 g_return_val_if_fail (idx >= 0, NULL);
436 g_return_val_if_fail (d, NULL);
437 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
441 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
442 g_return_val_if_reached (NULL);
447 d->disable_insert_signal = TRUE;
449 var = dict_create_var (d->dict, name, 0);
451 dict_reorder_var (d->dict, var, idx);
453 d->disable_insert_signal = FALSE;
455 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
456 g_signal_emit (d, signals [ITEMS_CHANGED], 0, idx, 0, 1);
461 /* Delete N variables beginning at FIRST */
463 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
466 g_return_if_fail (d);
467 g_return_if_fail (d->dict);
468 g_return_if_fail (PSPPIRE_IS_DICT (d));
470 for (idx = 0 ; idx < n ; ++idx )
472 struct variable *var;
474 /* Do nothing if it's out of bounds */
475 if ( first >= dict_get_var_cnt (d->dict))
478 var = dict_get_var (d->dict, first);
479 dict_delete_var (d->dict, var);
485 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
487 struct variable *var;
489 g_assert (PSPPIRE_IS_DICT (d));
491 if ( ! dict_id_is_valid (d->dict, name, false))
494 if ( idx < dict_get_var_cnt (d->dict))
496 /* This is an existing variable? */
497 var = dict_get_var (d->dict, idx);
498 dict_rename_var (d->dict, var, name);
503 dict_create_var (d->dict, name, 0);
511 /* Return the IDXth variable.
512 Will return NULL if IDX exceeds the number of variables in the dictionary.
515 psppire_dict_get_variable (const PsppireDict *d, gint idx)
517 g_return_val_if_fail (d, NULL);
518 g_return_val_if_fail (d->dict, NULL);
520 if ( dict_get_var_cnt (d->dict) <= idx )
523 return dict_get_var (d->dict, idx);
527 /* Return the number of variables in the dictionary */
529 psppire_dict_get_var_cnt (const PsppireDict *d)
531 g_return_val_if_fail (d, -1);
532 g_return_val_if_fail (d->dict, -1);
534 return dict_get_var_cnt (d->dict);
538 /* Return the number of `union value's in the dictionary */
540 psppire_dict_get_value_cnt (const PsppireDict *d)
542 g_return_val_if_fail (d, -1);
543 g_return_val_if_fail (d->dict, -1);
545 return dict_get_next_value_idx (d->dict);
549 /* Returns the prototype for the cases that match the dictionary */
550 const struct caseproto *
551 psppire_dict_get_proto (const PsppireDict *d)
553 g_return_val_if_fail (d, NULL);
554 g_return_val_if_fail (d->dict, NULL);
556 return dict_get_proto (d->dict);
560 /* Return a variable by name.
561 Return NULL if it doesn't exist
564 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
566 g_return_val_if_fail (d, NULL);
567 g_return_val_if_fail (d->dict, NULL);
569 return dict_lookup_var (d->dict, name);
572 /* Clears the contents of D */
574 psppire_dict_clear (PsppireDict *d)
576 g_return_if_fail (d);
577 g_return_if_fail (d->dict);
580 dict_clear (d->dict);
585 /* Return true is NAME would be a valid name of a variable to add to the
586 dictionary. False otherwise.
587 If REPORT is true, then invalid names will be reported as such as errors
590 psppire_dict_check_name (const PsppireDict *dict,
591 const gchar *name, gboolean report)
593 if ( ! dict_id_is_valid (dict->dict, name, report ) )
596 if (psppire_dict_lookup_var (dict, name))
599 msg (ME, _("Duplicate variable name."));
608 psppire_dict_get_next_value_idx (const PsppireDict *dict)
610 return dict_get_next_value_idx (dict->dict);
614 /* Tree Model Stuff */
616 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
618 static gint tree_model_n_columns (GtkTreeModel *model);
620 static GType tree_model_column_type (GtkTreeModel *model, gint index);
622 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
625 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
627 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
630 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
631 gint column, GValue *value);
633 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
634 GtkTreeIter *parent, gint n);
636 static gint tree_model_n_children (GtkTreeModel *tree_model,
639 static gboolean tree_model_iter_children (GtkTreeModel *,
643 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
647 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
651 dictionary_tree_model_init (GtkTreeModelIface *iface)
653 iface->get_flags = tree_model_get_flags;
654 iface->get_n_columns = tree_model_n_columns;
655 iface->get_column_type = tree_model_column_type;
656 iface->get_iter = tree_model_get_iter;
657 iface->iter_next = tree_model_iter_next;
658 iface->get_path = tree_model_get_path;
659 iface->get_value = tree_model_get_value;
661 iface->iter_children = tree_model_iter_children ;
662 iface->iter_has_child = tree_model_iter_has_child ;
663 iface->iter_n_children = tree_model_n_children ;
664 iface->iter_nth_child = tree_model_nth_child ;
665 iface->iter_parent = tree_model_iter_parent ;
669 tree_model_iter_has_child (GtkTreeModel *tree_model,
676 tree_model_iter_parent (GtkTreeModel *tree_model,
683 static GtkTreeModelFlags
684 tree_model_get_flags (GtkTreeModel *model)
686 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
688 return GTK_TREE_MODEL_LIST_ONLY;
693 tree_model_n_columns (GtkTreeModel *model)
699 tree_model_column_type (GtkTreeModel *model, gint index)
701 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
705 case DICT_TVM_COL_NAME:
706 return G_TYPE_STRING;
708 case DICT_TVM_COL_VAR:
709 return PSPPIRE_VAR_PTR_TYPE;
711 case DICT_TVM_COL_LABEL:
712 return G_TYPE_STRING;
720 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
722 gint *indices, depth;
724 struct variable *var;
726 PsppireDict *dict = PSPPIRE_DICT (model);
728 g_return_val_if_fail (path, FALSE);
730 indices = gtk_tree_path_get_indices (path);
731 depth = gtk_tree_path_get_depth (path);
733 g_return_val_if_fail (depth == 1, FALSE);
737 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
740 iter->user_data = NULL;
744 var = psppire_dict_get_variable (dict, n);
746 g_assert (var_get_dict_index (var) == n);
748 iter->stamp = dict->stamp;
749 iter->user_data = var;
756 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
758 PsppireDict *dict = PSPPIRE_DICT (model);
759 struct variable *var;
762 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
764 if ( iter == NULL || iter->user_data == NULL)
767 var = iter->user_data;
769 idx = var_get_dict_index (var);
771 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
773 iter->user_data = NULL;
778 var = psppire_dict_get_variable (dict, idx + 1);
780 g_assert (var_get_dict_index (var) == idx + 1);
782 iter->user_data = var;
788 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
791 struct variable *var;
792 PsppireDict *dict = PSPPIRE_DICT (model);
794 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
796 var = iter->user_data;
798 path = gtk_tree_path_new ();
799 gtk_tree_path_append_index (path, var_get_dict_index (var));
804 const struct fmt_spec *var_get_write_format (const struct variable *);
807 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
808 gint column, GValue *value)
810 struct variable *var;
811 PsppireDict *dict = PSPPIRE_DICT (model);
813 g_return_if_fail (iter->stamp == dict->stamp);
815 var = iter->user_data;
817 const struct fmt_spec *fs = var_get_write_format (var);
821 case DICT_TVM_COL_NAME:
822 g_value_init (value, G_TYPE_STRING);
823 g_value_set_string (value, var_get_name (var));
825 case DICT_TVM_COL_WIDTH:
826 g_value_init (value, G_TYPE_INT);
827 g_value_set_int (value, fs->w);
829 case DICT_TVM_COL_DECIMAL:
830 g_value_init (value, G_TYPE_INT);
831 g_value_set_int (value, fs->d);
833 case DICT_TVM_COL_LABEL:
834 g_value_init (value, G_TYPE_STRING);
835 g_value_set_string (value, var_get_label (var));
837 case DICT_TVM_COL_COLUMNS:
838 g_value_init (value, G_TYPE_INT);
839 g_value_set_int (value, var_get_display_width (var));
841 case DICT_TVM_COL_ALIGNMENT:
842 g_value_init (value, G_TYPE_INT);
843 g_value_set_int (value, var_get_alignment (var));
845 case DICT_TVM_COL_MEASURE:
846 g_value_init (value, G_TYPE_INT);
847 g_value_set_int (value, var_get_measure (var));
849 case DICT_TVM_COL_ROLE:
850 g_value_init (value, G_TYPE_INT);
851 g_value_set_int (value, var_get_role (var));
853 case DICT_TVM_COL_VAR:
854 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
855 g_value_set_boxed (value, var);
858 g_value_init (value, G_TYPE_STRING);
859 g_value_set_string (value, "????");
865 tree_model_iter_children (GtkTreeModel *tree_model,
873 tree_model_n_children (GtkTreeModel *model,
876 PsppireDict *dict = PSPPIRE_DICT (model);
879 return psppire_dict_get_var_cnt (dict);
885 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
886 GtkTreeIter *parent, gint n)
890 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
892 dict = PSPPIRE_DICT (model);
897 if ( n >= psppire_dict_get_var_cnt (dict) )
900 iter->stamp = dict->stamp;
901 iter->user_data = psppire_dict_get_variable (dict, n);
903 if ( !iter->user_data)
911 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
914 if ( ! dict_id_is_valid (dict->dict, name, false))
917 /* Make sure no other variable has this name */
918 if ( NULL != psppire_dict_lookup_var (dict, name))
921 dict_rename_var (dict->dict, v, name);
928 psppire_dict_get_weight_variable (const PsppireDict *dict)
930 return dict_get_weight (dict->dict);
937 psppire_dict_dump (const PsppireDict *dict)
940 const struct dictionary *d = dict->dict;
942 for (i = 0; i < dict_get_var_cnt (d); ++i)
944 const struct variable *v = psppire_dict_get_variable (dict, i);
945 int di = var_get_dict_index (v);
946 g_print ("`%s' idx=%d, fv=%d\n",
949 var_get_case_index(v));
959 psppire_dict_encoding (const PsppireDict *dict)
961 return dict_get_encoding (dict->dict);