1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012,
3 2016, 2017 Free Software Foundation
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "ui/gui/psppire-dict.h"
26 #include "data/dictionary.h"
27 #include "data/identifier.h"
28 #include "data/missing-values.h"
29 #include "data/value-labels.h"
30 #include "data/variable.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "ui/gui/helper.h"
34 #include "ui/gui/psppire-marshal.h"
35 #include "ui/gui/psppire-var-ptr.h"
37 #include "ui/gui/efficient-sheet/jmd-datum.h"
39 #include <gobject/genums.h>
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
47 GType align_enum_type;
48 GType measure_enum_type;
64 /* --- prototypes --- */
65 static void psppire_dict_class_init (PsppireDictClass *class);
66 static void psppire_dict_init (PsppireDict *dict);
67 static void psppire_dict_dispose (GObject *object);
69 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
74 gni (GListModel *list)
76 PsppireDict *dict = PSPPIRE_DICT (list);
78 return psppire_dict_get_var_cnt (dict);
82 git (GListModel *list)
84 return JMD_TYPE_DATUM;
88 gi (GListModel *list, guint id)
90 JmdDatum *gd = JMD_DATUM (g_object_new (JMD_TYPE_DATUM, NULL));
92 PsppireDict *dict = PSPPIRE_DICT (list);
94 if (id >= psppire_dict_get_var_cnt (dict))
96 gd->text = g_strdup (_("Var"));
100 const struct variable *v = psppire_dict_get_variable (dict, id);
102 gd->text = g_strdup (var_get_name (v));
103 gd->label = g_strdup (var_get_label (v));
111 jmd_init_iface (GListModelInterface *iface)
113 iface->get_n_items = gni;
114 iface->get_item = gi;
115 iface->get_item_type = git;
119 /* --- variables --- */
120 static GObjectClass *parent_class = NULL;
122 static guint signals [n_SIGNALS];
124 /* --- functions --- */
126 * psppire_dict_get_type:
127 * @returns: the type ID for accelerator groups.
130 psppire_dict_get_type (void)
132 static GType object_type = 0;
136 static const GTypeInfo object_info = {
137 sizeof (PsppireDictClass),
138 (GBaseInitFunc) NULL,
139 (GBaseFinalizeFunc) NULL,
140 (GClassInitFunc) psppire_dict_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
143 sizeof (PsppireDict),
145 (GInstanceInitFunc) psppire_dict_init,
148 static const GInterfaceInfo tree_model_info = {
149 (GInterfaceInitFunc) dictionary_tree_model_init,
154 static const GInterfaceInfo list_model_info = {
155 (GInterfaceInitFunc) jmd_init_iface,
160 object_type = g_type_register_static (G_TYPE_OBJECT,
164 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
167 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
176 psppire_dict_class_init (PsppireDictClass *class)
178 GObjectClass *object_class = G_OBJECT_CLASS (class);
180 parent_class = g_type_class_peek_parent (class);
182 object_class->dispose = psppire_dict_dispose;
184 signals [VARIABLE_CHANGED] =
185 g_signal_new ("variable-changed",
186 G_TYPE_FROM_CLASS (class),
190 psppire_marshal_VOID__INT_UINT_POINTER,
200 signals [VARIABLE_INSERTED] =
201 g_signal_new ("variable-inserted",
202 G_TYPE_FROM_CLASS (class),
206 g_cclosure_marshal_VOID__INT,
212 signals [VARIABLE_DELETED] =
213 g_signal_new ("variable-deleted",
214 G_TYPE_FROM_CLASS (class),
218 psppire_marshal_VOID__POINTER_INT_INT,
226 signals [WEIGHT_CHANGED] =
227 g_signal_new ("weight-changed",
228 G_TYPE_FROM_CLASS (class),
232 g_cclosure_marshal_VOID__INT,
238 signals [FILTER_CHANGED] =
239 g_signal_new ("filter-changed",
240 G_TYPE_FROM_CLASS (class),
244 g_cclosure_marshal_VOID__INT,
250 signals [SPLIT_CHANGED] =
251 g_signal_new ("split-changed",
252 G_TYPE_FROM_CLASS (class),
256 g_cclosure_marshal_VOID__VOID,
262 psppire_dict_dispose (GObject *object)
264 PsppireDict *d = PSPPIRE_DICT (object);
266 dict_set_callbacks (d->dict, NULL, NULL);
268 G_OBJECT_CLASS (parent_class)->dispose (object);
271 /* Pass on callbacks from src/data/dictionary, as
272 signals in the Gtk library */
274 addcb (struct dictionary *d, int idx, void *pd)
276 PsppireDict *dict = PSPPIRE_DICT (pd);
278 if ( ! dict->disable_insert_signal)
280 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
281 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
286 delcb (struct dictionary *d, const struct variable *var,
287 int dict_idx, int case_idx, void *pd)
289 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
290 var, dict_idx, case_idx);
291 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
295 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
297 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
298 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
302 weight_changed_callback (struct dictionary *d, int idx, void *pd)
304 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
308 filter_changed_callback (struct dictionary *d, int idx, void *pd)
310 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
314 split_changed_callback (struct dictionary *d, void *pd)
316 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
319 static const struct dict_callbacks gui_callbacks =
324 weight_changed_callback,
325 filter_changed_callback,
326 split_changed_callback
330 psppire_dict_init (PsppireDict *psppire_dict)
332 psppire_dict->stamp = g_random_int ();
333 psppire_dict->disable_insert_signal = FALSE;
337 * psppire_dict_new_from_dict:
338 * @returns: a new #PsppireDict object
340 * Creates a new #PsppireDict.
343 psppire_dict_new_from_dict (struct dictionary *d)
345 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
348 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
355 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
357 struct variable *var = dict_get_weight (d);
359 guint old_n = dict_get_var_cnt (dict->dict);
360 guint new_n = dict_get_var_cnt (d);
364 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
366 var = dict_get_filter (d);
367 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
369 split_changed_callback (d, dict);
371 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
373 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
377 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
378 Returns true if successful, false if SIZE is insufficient. */
380 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
388 /* TRANSLATORS: This string must be a valid variable name. That means:
389 - The string must be at most 64 bytes (not characters) long.
390 - The string may not contain whitespace.
391 - The first character may not be '$'
392 - The first character may not be a digit
393 - The final charactor may not be '.' or '_'
395 len = snprintf (name, size, _("Var%04d"), d);
399 if (psppire_dict_lookup_var (dict, name) == NULL)
406 /* Insert a new variable at posn IDX, with the name NAME, and return the
408 If NAME is null, then a name will be automatically assigned.
411 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
413 struct variable *var;
416 g_return_val_if_fail (idx >= 0, NULL);
417 g_return_val_if_fail (d, NULL);
418 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
422 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
423 g_return_val_if_reached (NULL);
428 d->disable_insert_signal = TRUE;
430 var = dict_create_var (d->dict, name, 0);
432 dict_reorder_var (d->dict, var, idx);
434 d->disable_insert_signal = FALSE;
436 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
437 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
442 /* Delete N variables beginning at FIRST */
444 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
447 g_return_if_fail (d);
448 g_return_if_fail (d->dict);
449 g_return_if_fail (PSPPIRE_IS_DICT (d));
451 for (idx = 0 ; idx < n ; ++idx )
453 struct variable *var;
455 /* Do nothing if it's out of bounds */
456 if ( first >= dict_get_var_cnt (d->dict))
459 var = dict_get_var (d->dict, first);
460 dict_delete_var (d->dict, var);
466 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
468 struct variable *var;
470 g_assert (PSPPIRE_IS_DICT (d));
472 if ( ! dict_id_is_valid (d->dict, name, false))
475 if ( idx < dict_get_var_cnt (d->dict))
477 /* This is an existing variable? */
478 var = dict_get_var (d->dict, idx);
479 dict_rename_var (d->dict, var, name);
484 dict_create_var (d->dict, name, 0);
492 /* Return the IDXth variable.
493 Will return NULL if IDX exceeds the number of variables in the dictionary.
496 psppire_dict_get_variable (const PsppireDict *d, gint idx)
498 g_return_val_if_fail (d, NULL);
499 g_return_val_if_fail (d->dict, NULL);
501 if ( dict_get_var_cnt (d->dict) <= idx )
504 return dict_get_var (d->dict, idx);
508 /* Return the number of variables in the dictionary */
510 psppire_dict_get_var_cnt (const PsppireDict *d)
512 g_return_val_if_fail (d, -1);
513 g_return_val_if_fail (d->dict, -1);
515 return dict_get_var_cnt (d->dict);
519 /* Return the number of `union value's in the dictionary */
521 psppire_dict_get_value_cnt (const PsppireDict *d)
523 g_return_val_if_fail (d, -1);
524 g_return_val_if_fail (d->dict, -1);
526 return dict_get_next_value_idx (d->dict);
530 /* Returns the prototype for the cases that match the dictionary */
531 const struct caseproto *
532 psppire_dict_get_proto (const PsppireDict *d)
534 g_return_val_if_fail (d, NULL);
535 g_return_val_if_fail (d->dict, NULL);
537 return dict_get_proto (d->dict);
541 /* Return a variable by name.
542 Return NULL if it doesn't exist
545 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
547 g_return_val_if_fail (d, NULL);
548 g_return_val_if_fail (d->dict, NULL);
550 return dict_lookup_var (d->dict, name);
553 /* Clears the contents of D */
555 psppire_dict_clear (PsppireDict *d)
557 g_return_if_fail (d);
558 g_return_if_fail (d->dict);
561 dict_clear (d->dict);
566 /* Return true if NAME would be a valid name of a variable to add to the
567 dictionary. False otherwise.
568 If REPORT is true, then invalid names will be reported as such as errors
571 psppire_dict_check_name (const PsppireDict *dict,
572 const gchar *name, gboolean report)
574 if ( ! dict_id_is_valid (dict->dict, name, report ) )
577 if (psppire_dict_lookup_var (dict, name))
580 msg (ME, _("Duplicate variable name."));
589 psppire_dict_get_next_value_idx (const PsppireDict *dict)
591 return dict_get_next_value_idx (dict->dict);
595 /* Tree Model Stuff */
597 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
599 static gint tree_model_n_columns (GtkTreeModel *model);
601 static GType tree_model_column_type (GtkTreeModel *model, gint index);
603 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
606 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
608 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
611 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
612 gint column, GValue *value);
614 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
615 GtkTreeIter *parent, gint n);
617 static gint tree_model_n_children (GtkTreeModel *tree_model,
620 static gboolean tree_model_iter_children (GtkTreeModel *,
624 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
628 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
632 dictionary_tree_model_init (GtkTreeModelIface *iface)
634 iface->get_flags = tree_model_get_flags;
635 iface->get_n_columns = tree_model_n_columns;
636 iface->get_column_type = tree_model_column_type;
637 iface->get_iter = tree_model_get_iter;
638 iface->iter_next = tree_model_iter_next;
639 iface->get_path = tree_model_get_path;
640 iface->get_value = tree_model_get_value;
642 iface->iter_children = tree_model_iter_children ;
643 iface->iter_has_child = tree_model_iter_has_child ;
644 iface->iter_n_children = tree_model_n_children ;
645 iface->iter_nth_child = tree_model_nth_child ;
646 iface->iter_parent = tree_model_iter_parent ;
650 tree_model_iter_has_child (GtkTreeModel *tree_model,
657 tree_model_iter_parent (GtkTreeModel *tree_model,
664 static GtkTreeModelFlags
665 tree_model_get_flags (GtkTreeModel *model)
667 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
669 return GTK_TREE_MODEL_LIST_ONLY;
674 tree_model_n_columns (GtkTreeModel *model)
680 tree_model_column_type (GtkTreeModel *model, gint index)
682 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
688 case DICT_TVM_COL_NAME:
689 case DICT_TVM_COL_LABEL:
692 case DICT_TVM_COL_DECIMAL:
693 case DICT_TVM_COL_WIDTH:
694 case DICT_TVM_COL_COLUMNS:
697 case DICT_TVM_COL_VAR:
698 t = PSPPIRE_VAR_PTR_TYPE;
700 case DICT_TVM_COL_ALIGNMENT:
703 case DICT_TVM_COL_MEASURE:
704 t = measure_enum_type;
706 case DICT_TVM_COL_ROLE:
715 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
717 gint *indices, depth;
719 struct variable *var;
721 PsppireDict *dict = PSPPIRE_DICT (model);
723 g_return_val_if_fail (path, FALSE);
725 indices = gtk_tree_path_get_indices (path);
726 depth = gtk_tree_path_get_depth (path);
728 g_return_val_if_fail (depth == 1, FALSE);
732 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
735 iter->user_data = NULL;
739 var = psppire_dict_get_variable (dict, n);
741 g_assert (var_get_dict_index (var) == n);
743 iter->stamp = dict->stamp;
744 iter->user_data = var;
751 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
753 PsppireDict *dict = PSPPIRE_DICT (model);
754 struct variable *var;
757 if ( iter == NULL || iter->user_data == NULL)
760 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
762 var = iter->user_data;
764 idx = var_get_dict_index (var);
766 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
768 iter->user_data = NULL;
773 var = psppire_dict_get_variable (dict, idx + 1);
775 g_assert (var_get_dict_index (var) == idx + 1);
777 iter->user_data = var;
783 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
786 struct variable *var;
787 PsppireDict *dict = PSPPIRE_DICT (model);
789 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
791 var = iter->user_data;
793 path = gtk_tree_path_new ();
794 gtk_tree_path_append_index (path, var_get_dict_index (var));
799 const struct fmt_spec *var_get_write_format (const struct variable *);
802 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
803 gint column, GValue *value)
805 struct variable *var;
806 PsppireDict *dict = PSPPIRE_DICT (model);
808 g_return_if_fail (iter->stamp == dict->stamp);
810 var = iter->user_data;
812 const struct fmt_spec *fs = var_get_write_format (var);
816 case DICT_TVM_COL_NAME:
817 g_value_init (value, G_TYPE_STRING);
818 g_value_set_string (value, var_get_name (var));
820 case DICT_TVM_COL_WIDTH:
821 g_value_init (value, G_TYPE_INT);
822 g_value_set_int (value, fs->w);
824 case DICT_TVM_COL_DECIMAL:
825 g_value_init (value, G_TYPE_INT);
826 g_value_set_int (value, fs->d);
828 case DICT_TVM_COL_LABEL:
829 g_value_init (value, G_TYPE_STRING);
830 g_value_set_string (value, var_get_label (var));
832 case DICT_TVM_COL_COLUMNS:
833 g_value_init (value, G_TYPE_INT);
834 g_value_set_int (value, var_get_display_width (var));
836 case DICT_TVM_COL_ALIGNMENT:
837 g_value_init (value, align_enum_type);
838 g_value_set_enum (value, var_get_alignment (var));
840 case DICT_TVM_COL_MEASURE:
841 g_value_init (value, measure_enum_type);
842 g_value_set_enum (value, var_get_measure (var));
844 case DICT_TVM_COL_ROLE:
845 g_value_init (value, role_enum_type);
846 g_value_set_enum (value, var_get_role (var));
848 case DICT_TVM_COL_VAR:
849 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
850 g_value_set_boxed (value, var);
853 g_value_init (value, G_TYPE_STRING);
854 g_value_set_string (value, "????");
860 tree_model_iter_children (GtkTreeModel *tree_model,
868 tree_model_n_children (GtkTreeModel *model,
871 PsppireDict *dict = PSPPIRE_DICT (model);
874 return psppire_dict_get_var_cnt (dict);
880 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
881 GtkTreeIter *parent, gint n)
885 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
887 dict = PSPPIRE_DICT (model);
892 if ( n >= psppire_dict_get_var_cnt (dict) )
895 iter->stamp = dict->stamp;
896 iter->user_data = psppire_dict_get_variable (dict, n);
898 if ( !iter->user_data)
906 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
909 if ( ! dict_id_is_valid (dict->dict, name, false))
912 /* Make sure no other variable has this name */
913 if ( NULL != psppire_dict_lookup_var (dict, name))
916 dict_rename_var (dict->dict, v, name);
923 psppire_dict_get_weight_variable (const PsppireDict *dict)
925 return dict_get_weight (dict->dict);
932 psppire_dict_dump (const PsppireDict *dict)
935 const struct dictionary *d = dict->dict;
937 for (i = 0; i < dict_get_var_cnt (d); ++i)
939 const struct variable *v = psppire_dict_get_variable (dict, i);
940 int di = var_get_dict_index (v);
941 g_print ("`%s' idx=%d, fv=%d\n",
944 var_get_case_index(v));
954 psppire_dict_encoding (const PsppireDict *dict)
956 return dict_get_encoding (dict->dict);