1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012, 2016 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"
38 #include <gobject/genums.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
46 GType align_enum_type;
47 GType measure_enum_type;
65 /* --- prototypes --- */
66 static void psppire_dict_class_init (PsppireDictClass *class);
67 static void psppire_dict_init (PsppireDict *dict);
68 static void psppire_dict_dispose (GObject *object);
70 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
75 gni (GListModel *list)
77 PsppireDict *dict = PSPPIRE_DICT (list);
79 return psppire_dict_get_var_cnt (dict);
83 git (GListModel *list)
85 return JMD_TYPE_DATUM;
89 gi (GListModel *list, guint id)
91 JmdDatum *gd = JMD_DATUM (g_object_new (JMD_TYPE_DATUM, NULL));
93 PsppireDict *dict = PSPPIRE_DICT (list);
95 if (id >= psppire_dict_get_var_cnt (dict))
97 gd->text = g_strdup (_("Var"));
101 const struct variable *v = psppire_dict_get_variable (dict, id);
103 gd->text = g_strdup (var_get_name (v));
104 gd->label = g_strdup (var_get_label (v));
112 jmd_init_iface (GListModelInterface *iface)
114 iface->get_n_items = gni;
115 iface->get_item = gi;
116 iface->get_item_type = git;
120 /* --- variables --- */
121 static GObjectClass *parent_class = NULL;
123 static guint signals [n_SIGNALS];
125 /* --- functions --- */
127 * psppire_dict_get_type:
128 * @returns: the type ID for accelerator groups.
131 psppire_dict_get_type (void)
133 static GType object_type = 0;
137 static const GTypeInfo object_info = {
138 sizeof (PsppireDictClass),
139 (GBaseInitFunc) NULL,
140 (GBaseFinalizeFunc) NULL,
141 (GClassInitFunc) psppire_dict_class_init,
142 NULL, /* class_finalize */
143 NULL, /* class_data */
144 sizeof (PsppireDict),
146 (GInstanceInitFunc) psppire_dict_init,
149 static const GInterfaceInfo tree_model_info = {
150 (GInterfaceInitFunc) dictionary_tree_model_init,
155 static const GInterfaceInfo list_model_info = {
156 (GInterfaceInitFunc) jmd_init_iface,
161 object_type = g_type_register_static (G_TYPE_OBJECT,
165 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
168 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
177 psppire_dict_class_init (PsppireDictClass *class)
179 GObjectClass *object_class = G_OBJECT_CLASS (class);
181 parent_class = g_type_class_peek_parent (class);
183 object_class->dispose = psppire_dict_dispose;
185 signals [ITEMS_CHANGED] =
186 g_signal_new ("changed",
187 G_TYPE_FROM_CLASS (class),
191 psppire_marshal_VOID__UINT_UINT_UINT,
199 signals [VARIABLE_CHANGED] =
200 g_signal_new ("variable-changed",
201 G_TYPE_FROM_CLASS (class),
205 psppire_marshal_VOID__INT_UINT_POINTER,
215 signals [VARIABLE_INSERTED] =
216 g_signal_new ("variable-inserted",
217 G_TYPE_FROM_CLASS (class),
221 g_cclosure_marshal_VOID__INT,
227 signals [VARIABLE_DELETED] =
228 g_signal_new ("variable-deleted",
229 G_TYPE_FROM_CLASS (class),
233 psppire_marshal_VOID__POINTER_INT_INT,
241 signals [WEIGHT_CHANGED] =
242 g_signal_new ("weight-changed",
243 G_TYPE_FROM_CLASS (class),
247 g_cclosure_marshal_VOID__INT,
253 signals [FILTER_CHANGED] =
254 g_signal_new ("filter-changed",
255 G_TYPE_FROM_CLASS (class),
259 g_cclosure_marshal_VOID__INT,
265 signals [SPLIT_CHANGED] =
266 g_signal_new ("split-changed",
267 G_TYPE_FROM_CLASS (class),
271 g_cclosure_marshal_VOID__VOID,
277 psppire_dict_dispose (GObject *object)
279 PsppireDict *d = PSPPIRE_DICT (object);
281 dict_set_callbacks (d->dict, NULL, NULL);
283 G_OBJECT_CLASS (parent_class)->dispose (object);
286 /* Pass on callbacks from src/data/dictionary, as
287 signals in the Gtk library */
289 addcb (struct dictionary *d, int idx, void *pd)
291 PsppireDict *dict = PSPPIRE_DICT (pd);
293 if ( ! dict->disable_insert_signal)
295 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
296 g_signal_emit (dict, signals [ITEMS_CHANGED], 0, idx, 1, 1);
301 delcb (struct dictionary *d, const struct variable *var,
302 int dict_idx, int case_idx, void *pd)
304 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
305 var, dict_idx, case_idx);
306 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, dict_idx, 1, 0);
310 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
312 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
313 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, idx, 1, 1);
317 weight_changed_callback (struct dictionary *d, int idx, void *pd)
319 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
323 filter_changed_callback (struct dictionary *d, int idx, void *pd)
325 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
329 split_changed_callback (struct dictionary *d, void *pd)
331 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
334 static const struct dict_callbacks gui_callbacks =
339 weight_changed_callback,
340 filter_changed_callback,
341 split_changed_callback
345 psppire_dict_init (PsppireDict *psppire_dict)
347 psppire_dict->stamp = g_random_int ();
348 psppire_dict->disable_insert_signal = FALSE;
352 * psppire_dict_new_from_dict:
353 * @returns: a new #PsppireDict object
355 * Creates a new #PsppireDict.
358 psppire_dict_new_from_dict (struct dictionary *d)
360 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
363 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
370 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
372 struct variable *var = dict_get_weight (d);
374 guint old_n = dict_get_var_cnt (dict->dict);
375 guint new_n = dict_get_var_cnt (d);
379 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
381 var = dict_get_filter (d);
382 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
384 split_changed_callback (d, dict);
386 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
388 g_signal_emit (dict, signals [ITEMS_CHANGED], 0, 0, old_n, new_n);
392 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
393 Returns true if successful, false if SIZE is insufficient. */
395 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
403 /* TRANSLATORS: This string must be a valid variable name. That means:
404 - The string must be at most 64 bytes (not characters) long.
405 - The string may not contain whitespace.
406 - The first character may not be '$'
407 - The first character may not be a digit
408 - The final charactor may not be '.' or '_'
410 len = snprintf (name, size, _("Var%04d"), d);
414 if (psppire_dict_lookup_var (dict, name) == NULL)
421 /* Insert a new variable at posn IDX, with the name NAME, and return the
423 If NAME is null, then a name will be automatically assigned.
426 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
428 struct variable *var;
431 g_return_val_if_fail (idx >= 0, NULL);
432 g_return_val_if_fail (d, NULL);
433 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
437 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
438 g_return_val_if_reached (NULL);
443 d->disable_insert_signal = TRUE;
445 var = dict_create_var (d->dict, name, 0);
447 dict_reorder_var (d->dict, var, idx);
449 d->disable_insert_signal = FALSE;
451 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
452 g_signal_emit (d, signals [ITEMS_CHANGED], 0, idx, 0, 1);
457 /* Delete N variables beginning at FIRST */
459 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
462 g_return_if_fail (d);
463 g_return_if_fail (d->dict);
464 g_return_if_fail (PSPPIRE_IS_DICT (d));
466 for (idx = 0 ; idx < n ; ++idx )
468 struct variable *var;
470 /* Do nothing if it's out of bounds */
471 if ( first >= dict_get_var_cnt (d->dict))
474 var = dict_get_var (d->dict, first);
475 dict_delete_var (d->dict, var);
481 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
483 struct variable *var;
485 g_assert (PSPPIRE_IS_DICT (d));
487 if ( ! dict_id_is_valid (d->dict, name, false))
490 if ( idx < dict_get_var_cnt (d->dict))
492 /* This is an existing variable? */
493 var = dict_get_var (d->dict, idx);
494 dict_rename_var (d->dict, var, name);
499 dict_create_var (d->dict, name, 0);
507 /* Return the IDXth variable.
508 Will return NULL if IDX exceeds the number of variables in the dictionary.
511 psppire_dict_get_variable (const PsppireDict *d, gint idx)
513 g_return_val_if_fail (d, NULL);
514 g_return_val_if_fail (d->dict, NULL);
516 if ( dict_get_var_cnt (d->dict) <= idx )
519 return dict_get_var (d->dict, idx);
523 /* Return the number of variables in the dictionary */
525 psppire_dict_get_var_cnt (const PsppireDict *d)
527 g_return_val_if_fail (d, -1);
528 g_return_val_if_fail (d->dict, -1);
530 return dict_get_var_cnt (d->dict);
534 /* Return the number of `union value's in the dictionary */
536 psppire_dict_get_value_cnt (const PsppireDict *d)
538 g_return_val_if_fail (d, -1);
539 g_return_val_if_fail (d->dict, -1);
541 return dict_get_next_value_idx (d->dict);
545 /* Returns the prototype for the cases that match the dictionary */
546 const struct caseproto *
547 psppire_dict_get_proto (const PsppireDict *d)
549 g_return_val_if_fail (d, NULL);
550 g_return_val_if_fail (d->dict, NULL);
552 return dict_get_proto (d->dict);
556 /* Return a variable by name.
557 Return NULL if it doesn't exist
560 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
562 g_return_val_if_fail (d, NULL);
563 g_return_val_if_fail (d->dict, NULL);
565 return dict_lookup_var (d->dict, name);
568 /* Clears the contents of D */
570 psppire_dict_clear (PsppireDict *d)
572 g_return_if_fail (d);
573 g_return_if_fail (d->dict);
576 dict_clear (d->dict);
581 /* Return true is NAME would be a valid name of a variable to add to the
582 dictionary. False otherwise.
583 If REPORT is true, then invalid names will be reported as such as errors
586 psppire_dict_check_name (const PsppireDict *dict,
587 const gchar *name, gboolean report)
589 if ( ! dict_id_is_valid (dict->dict, name, report ) )
592 if (psppire_dict_lookup_var (dict, name))
595 msg (ME, _("Duplicate variable name."));
604 psppire_dict_get_next_value_idx (const PsppireDict *dict)
606 return dict_get_next_value_idx (dict->dict);
610 /* Tree Model Stuff */
612 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
614 static gint tree_model_n_columns (GtkTreeModel *model);
616 static GType tree_model_column_type (GtkTreeModel *model, gint index);
618 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
621 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
623 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
626 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
627 gint column, GValue *value);
629 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
630 GtkTreeIter *parent, gint n);
632 static gint tree_model_n_children (GtkTreeModel *tree_model,
635 static gboolean tree_model_iter_children (GtkTreeModel *,
639 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
643 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
647 dictionary_tree_model_init (GtkTreeModelIface *iface)
649 iface->get_flags = tree_model_get_flags;
650 iface->get_n_columns = tree_model_n_columns;
651 iface->get_column_type = tree_model_column_type;
652 iface->get_iter = tree_model_get_iter;
653 iface->iter_next = tree_model_iter_next;
654 iface->get_path = tree_model_get_path;
655 iface->get_value = tree_model_get_value;
657 iface->iter_children = tree_model_iter_children ;
658 iface->iter_has_child = tree_model_iter_has_child ;
659 iface->iter_n_children = tree_model_n_children ;
660 iface->iter_nth_child = tree_model_nth_child ;
661 iface->iter_parent = tree_model_iter_parent ;
665 tree_model_iter_has_child (GtkTreeModel *tree_model,
672 tree_model_iter_parent (GtkTreeModel *tree_model,
679 static GtkTreeModelFlags
680 tree_model_get_flags (GtkTreeModel *model)
682 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
684 return GTK_TREE_MODEL_LIST_ONLY;
689 tree_model_n_columns (GtkTreeModel *model)
695 tree_model_column_type (GtkTreeModel *model, gint index)
697 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
703 case DICT_TVM_COL_NAME:
706 case DICT_TVM_COL_VAR:
707 t = PSPPIRE_VAR_PTR_TYPE;
709 case DICT_TVM_COL_LABEL:
712 case DICT_TVM_COL_COLUMNS:
715 case DICT_TVM_COL_ALIGNMENT:
718 case DICT_TVM_COL_MEASURE:
719 t = measure_enum_type;
721 case DICT_TVM_COL_ROLE:
730 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
732 gint *indices, depth;
734 struct variable *var;
736 PsppireDict *dict = PSPPIRE_DICT (model);
738 g_return_val_if_fail (path, FALSE);
740 indices = gtk_tree_path_get_indices (path);
741 depth = gtk_tree_path_get_depth (path);
743 g_return_val_if_fail (depth == 1, FALSE);
747 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
750 iter->user_data = NULL;
754 var = psppire_dict_get_variable (dict, n);
756 g_assert (var_get_dict_index (var) == n);
758 iter->stamp = dict->stamp;
759 iter->user_data = var;
766 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
768 PsppireDict *dict = PSPPIRE_DICT (model);
769 struct variable *var;
772 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
774 if ( iter == NULL || iter->user_data == NULL)
777 var = iter->user_data;
779 idx = var_get_dict_index (var);
781 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
783 iter->user_data = NULL;
788 var = psppire_dict_get_variable (dict, idx + 1);
790 g_assert (var_get_dict_index (var) == idx + 1);
792 iter->user_data = var;
798 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
801 struct variable *var;
802 PsppireDict *dict = PSPPIRE_DICT (model);
804 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
806 var = iter->user_data;
808 path = gtk_tree_path_new ();
809 gtk_tree_path_append_index (path, var_get_dict_index (var));
814 const struct fmt_spec *var_get_write_format (const struct variable *);
817 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
818 gint column, GValue *value)
820 struct variable *var;
821 PsppireDict *dict = PSPPIRE_DICT (model);
823 g_return_if_fail (iter->stamp == dict->stamp);
825 var = iter->user_data;
827 const struct fmt_spec *fs = var_get_write_format (var);
831 case DICT_TVM_COL_NAME:
832 g_value_init (value, G_TYPE_STRING);
833 g_value_set_string (value, var_get_name (var));
835 case DICT_TVM_COL_WIDTH:
836 g_value_init (value, G_TYPE_INT);
837 g_value_set_int (value, fs->w);
839 case DICT_TVM_COL_DECIMAL:
840 g_value_init (value, G_TYPE_INT);
841 g_value_set_int (value, fs->d);
843 case DICT_TVM_COL_LABEL:
844 g_value_init (value, G_TYPE_STRING);
845 g_value_set_string (value, var_get_label (var));
847 case DICT_TVM_COL_COLUMNS:
848 g_value_init (value, G_TYPE_INT);
849 g_value_set_int (value, var_get_display_width (var));
851 case DICT_TVM_COL_ALIGNMENT:
852 g_value_init (value, align_enum_type);
853 g_value_set_enum (value, var_get_alignment (var));
855 case DICT_TVM_COL_MEASURE:
856 g_value_init (value, measure_enum_type);
857 g_value_set_enum (value, var_get_measure (var));
859 case DICT_TVM_COL_ROLE:
860 g_value_init (value, role_enum_type);
861 g_value_set_enum (value, var_get_role (var));
863 case DICT_TVM_COL_VAR:
864 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
865 g_value_set_boxed (value, var);
868 g_value_init (value, G_TYPE_STRING);
869 g_value_set_string (value, "????");
875 tree_model_iter_children (GtkTreeModel *tree_model,
883 tree_model_n_children (GtkTreeModel *model,
886 PsppireDict *dict = PSPPIRE_DICT (model);
889 return psppire_dict_get_var_cnt (dict);
895 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
896 GtkTreeIter *parent, gint n)
900 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
902 dict = PSPPIRE_DICT (model);
907 if ( n >= psppire_dict_get_var_cnt (dict) )
910 iter->stamp = dict->stamp;
911 iter->user_data = psppire_dict_get_variable (dict, n);
913 if ( !iter->user_data)
921 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
924 if ( ! dict_id_is_valid (dict->dict, name, false))
927 /* Make sure no other variable has this name */
928 if ( NULL != psppire_dict_lookup_var (dict, name))
931 dict_rename_var (dict->dict, v, name);
938 psppire_dict_get_weight_variable (const PsppireDict *dict)
940 return dict_get_weight (dict->dict);
947 psppire_dict_dump (const PsppireDict *dict)
950 const struct dictionary *d = dict->dict;
952 for (i = 0; i < dict_get_var_cnt (d); ++i)
954 const struct variable *v = psppire_dict_get_variable (dict, i);
955 int di = var_get_dict_index (v);
956 g_print ("`%s' idx=%d, fv=%d\n",
959 var_get_case_index(v));
969 psppire_dict_encoding (const PsppireDict *dict)
971 return dict_get_encoding (dict->dict);