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 <gobject/genums.h>
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
45 GType align_enum_type;
46 GType measure_enum_type;
62 /* --- prototypes --- */
63 static void psppire_dict_class_init (PsppireDictClass *class);
64 static void psppire_dict_init (PsppireDict *dict);
65 static void psppire_dict_dispose (GObject *object);
67 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
72 gni (GListModel *list)
74 PsppireDict *dict = PSPPIRE_DICT (list);
76 return psppire_dict_get_var_cnt (dict);
80 git (GListModel *list)
82 return GTK_TYPE_BUTTON;
86 gi (GListModel *list, guint id)
88 GtkWidget *button = gtk_button_new ();
90 PsppireDict *dict = PSPPIRE_DICT (list);
92 if (id >= psppire_dict_get_var_cnt (dict))
94 gtk_button_set_label (GTK_BUTTON (button), _("Var"));
98 const struct variable *v = psppire_dict_get_variable (dict, id);
100 gtk_button_set_label (GTK_BUTTON (button), var_get_name (v));
101 gtk_widget_set_tooltip_text (button, var_get_label (v));
104 PangoContext *context = gtk_widget_create_pango_context (button);
105 PangoLayout *layout = pango_layout_new (context);
108 pango_layout_set_text (layout, "M", 1);
110 pango_layout_get_extents (layout, NULL, &rect);
112 g_object_unref (G_OBJECT (layout));
113 g_object_unref (G_OBJECT (context));
115 gtk_widget_set_size_request (button,
116 (0.25 + var_get_display_width (v))
117 * rect.width / PANGO_SCALE,
127 ssw_init_iface (GListModelInterface *iface)
129 iface->get_n_items = gni;
130 iface->get_item = gi;
131 iface->get_item_type = git;
135 /* --- variables --- */
136 static GObjectClass *parent_class = NULL;
138 static guint signals [n_SIGNALS];
140 /* --- functions --- */
142 * psppire_dict_get_type:
143 * @returns: the type ID for accelerator groups.
146 psppire_dict_get_type (void)
148 static GType object_type = 0;
152 static const GTypeInfo object_info = {
153 sizeof (PsppireDictClass),
154 (GBaseInitFunc) NULL,
155 (GBaseFinalizeFunc) NULL,
156 (GClassInitFunc) psppire_dict_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (PsppireDict),
161 (GInstanceInitFunc) psppire_dict_init,
164 static const GInterfaceInfo tree_model_info = {
165 (GInterfaceInitFunc) dictionary_tree_model_init,
170 static const GInterfaceInfo list_model_info = {
171 (GInterfaceInitFunc) ssw_init_iface,
176 object_type = g_type_register_static (G_TYPE_OBJECT,
180 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
183 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
192 psppire_dict_class_init (PsppireDictClass *class)
194 GObjectClass *object_class = G_OBJECT_CLASS (class);
196 parent_class = g_type_class_peek_parent (class);
198 object_class->dispose = psppire_dict_dispose;
200 signals [VARIABLE_CHANGED] =
201 g_signal_new ("variable-changed",
202 G_TYPE_FROM_CLASS (class),
206 psppire_marshal_VOID__INT_UINT_POINTER,
213 signals [VARIABLE_INSERTED] =
214 g_signal_new ("variable-inserted",
215 G_TYPE_FROM_CLASS (class),
219 g_cclosure_marshal_VOID__INT,
224 signals [VARIABLE_DELETED] =
225 g_signal_new ("variable-deleted",
226 G_TYPE_FROM_CLASS (class),
230 psppire_marshal_VOID__POINTER_INT_INT,
237 signals [WEIGHT_CHANGED] =
238 g_signal_new ("weight-changed",
239 G_TYPE_FROM_CLASS (class),
243 g_cclosure_marshal_VOID__INT,
248 signals [FILTER_CHANGED] =
249 g_signal_new ("filter-changed",
250 G_TYPE_FROM_CLASS (class),
254 g_cclosure_marshal_VOID__INT,
259 signals [SPLIT_CHANGED] =
260 g_signal_new ("split-changed",
261 G_TYPE_FROM_CLASS (class),
265 g_cclosure_marshal_VOID__VOID,
271 psppire_dict_dispose (GObject *object)
273 PsppireDict *d = PSPPIRE_DICT (object);
275 dict_set_callbacks (d->dict, NULL, NULL);
277 G_OBJECT_CLASS (parent_class)->dispose (object);
280 /* Pass on callbacks from src/data/dictionary, as
281 signals in the Gtk library */
283 addcb (struct dictionary *d, int idx, void *pd)
285 PsppireDict *dict = PSPPIRE_DICT (pd);
287 if ( ! dict->disable_insert_signal)
289 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
290 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
295 delcb (struct dictionary *d, const struct variable *var,
296 int dict_idx, int case_idx, void *pd)
298 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
299 var, dict_idx, case_idx);
300 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
304 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
306 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
307 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
311 weight_changed_callback (struct dictionary *d, int idx, void *pd)
313 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
317 filter_changed_callback (struct dictionary *d, int idx, void *pd)
319 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
323 split_changed_callback (struct dictionary *d, void *pd)
325 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
328 static const struct dict_callbacks gui_callbacks =
333 weight_changed_callback,
334 filter_changed_callback,
335 split_changed_callback
339 psppire_dict_init (PsppireDict *psppire_dict)
341 psppire_dict->stamp = g_random_int ();
342 psppire_dict->disable_insert_signal = FALSE;
346 * psppire_dict_new_from_dict:
347 * @returns: a new #PsppireDict object
349 * Creates a new #PsppireDict.
352 psppire_dict_new_from_dict (struct dictionary *d)
354 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
357 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
364 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
366 struct variable *var = dict_get_weight (d);
368 guint old_n = dict_get_var_cnt (dict->dict);
369 guint new_n = dict_get_var_cnt (d);
373 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
375 var = dict_get_filter (d);
376 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
378 split_changed_callback (d, dict);
380 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
382 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
386 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
387 Returns true if successful, false if SIZE is insufficient. */
389 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
397 /* TRANSLATORS: This string must be a valid variable name. That means:
398 - The string must be at most 64 bytes (not characters) long.
399 - The string may not contain whitespace.
400 - The first character may not be '$'
401 - The first character may not be a digit
402 - The final character may not be '.' or '_'
404 len = snprintf (name, size, _("Var%04d"), d);
408 if (psppire_dict_lookup_var (dict, name) == NULL)
415 /* Insert a new variable at posn IDX, with the name NAME, and return the
417 If NAME is null, then a name will be automatically assigned.
420 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
422 struct variable *var;
425 g_return_val_if_fail (idx >= 0, NULL);
426 g_return_val_if_fail (d, NULL);
427 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
431 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
432 g_return_val_if_reached (NULL);
437 d->disable_insert_signal = TRUE;
439 var = dict_create_var (d->dict, name, 0);
441 dict_reorder_var (d->dict, var, idx);
443 d->disable_insert_signal = FALSE;
445 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
446 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
451 /* Delete N variables beginning at FIRST */
453 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
456 g_return_if_fail (d);
457 g_return_if_fail (d->dict);
458 g_return_if_fail (PSPPIRE_IS_DICT (d));
460 for (idx = 0 ; idx < n ; ++idx )
462 struct variable *var;
464 /* Do nothing if it's out of bounds */
465 if ( first >= dict_get_var_cnt (d->dict))
468 var = dict_get_var (d->dict, first);
469 dict_delete_var (d->dict, var);
475 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
477 struct variable *var;
479 g_assert (PSPPIRE_IS_DICT (d));
481 if ( ! dict_id_is_valid (d->dict, name, false))
484 if ( idx < dict_get_var_cnt (d->dict))
486 /* This is an existing variable? */
487 var = dict_get_var (d->dict, idx);
488 dict_rename_var (d->dict, var, name);
493 dict_create_var (d->dict, name, 0);
501 /* Return the IDXth variable.
502 Will return NULL if IDX exceeds the number of variables in the dictionary.
505 psppire_dict_get_variable (const PsppireDict *d, gint idx)
507 g_return_val_if_fail (d, NULL);
508 g_return_val_if_fail (d->dict, NULL);
510 if ( dict_get_var_cnt (d->dict) <= idx )
513 return dict_get_var (d->dict, idx);
517 /* Return the number of variables in the dictionary */
519 psppire_dict_get_var_cnt (const PsppireDict *d)
521 g_return_val_if_fail (d, -1);
522 g_return_val_if_fail (d->dict, -1);
524 return dict_get_var_cnt (d->dict);
528 /* Return the number of `union value's in the dictionary */
530 psppire_dict_get_value_cnt (const PsppireDict *d)
532 g_return_val_if_fail (d, -1);
533 g_return_val_if_fail (d->dict, -1);
535 return dict_get_next_value_idx (d->dict);
539 /* Returns the prototype for the cases that match the dictionary */
540 const struct caseproto *
541 psppire_dict_get_proto (const PsppireDict *d)
543 g_return_val_if_fail (d, NULL);
544 g_return_val_if_fail (d->dict, NULL);
546 return dict_get_proto (d->dict);
550 /* Return a variable by name.
551 Return NULL if it doesn't exist
554 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
556 g_return_val_if_fail (d, NULL);
557 g_return_val_if_fail (d->dict, NULL);
559 return dict_lookup_var (d->dict, name);
562 /* Clears the contents of D */
564 psppire_dict_clear (PsppireDict *d)
566 g_return_if_fail (d);
567 g_return_if_fail (d->dict);
570 dict_clear (d->dict);
575 /* Return true if NAME would be a valid name of a variable to add to the
576 dictionary. False otherwise.
577 If REPORT is true, then invalid names will be reported as such as errors
580 psppire_dict_check_name (const PsppireDict *dict,
581 const gchar *name, gboolean report)
583 if ( ! dict_id_is_valid (dict->dict, name, report ) )
586 if (psppire_dict_lookup_var (dict, name))
589 msg (ME, _("Duplicate variable name."));
598 psppire_dict_get_next_value_idx (const PsppireDict *dict)
600 return dict_get_next_value_idx (dict->dict);
604 /* Tree Model Stuff */
606 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
608 static gint tree_model_n_columns (GtkTreeModel *model);
610 static GType tree_model_column_type (GtkTreeModel *model, gint index);
612 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
615 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
617 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
620 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
621 gint column, GValue *value);
623 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
624 GtkTreeIter *parent, gint n);
626 static gint tree_model_n_children (GtkTreeModel *tree_model,
629 static gboolean tree_model_iter_children (GtkTreeModel *,
633 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
637 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
641 dictionary_tree_model_init (GtkTreeModelIface *iface)
643 iface->get_flags = tree_model_get_flags;
644 iface->get_n_columns = tree_model_n_columns;
645 iface->get_column_type = tree_model_column_type;
646 iface->get_iter = tree_model_get_iter;
647 iface->iter_next = tree_model_iter_next;
648 iface->get_path = tree_model_get_path;
649 iface->get_value = tree_model_get_value;
651 iface->iter_children = tree_model_iter_children ;
652 iface->iter_has_child = tree_model_iter_has_child ;
653 iface->iter_n_children = tree_model_n_children ;
654 iface->iter_nth_child = tree_model_nth_child ;
655 iface->iter_parent = tree_model_iter_parent ;
659 tree_model_iter_has_child (GtkTreeModel *tree_model,
666 tree_model_iter_parent (GtkTreeModel *tree_model,
673 static GtkTreeModelFlags
674 tree_model_get_flags (GtkTreeModel *model)
676 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
678 return GTK_TREE_MODEL_LIST_ONLY;
683 tree_model_n_columns (GtkTreeModel *model)
689 tree_model_column_type (GtkTreeModel *model, gint index)
691 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
697 case DICT_TVM_COL_NAME:
698 case DICT_TVM_COL_LABEL:
701 case DICT_TVM_COL_DECIMAL:
702 case DICT_TVM_COL_WIDTH:
703 case DICT_TVM_COL_COLUMNS:
706 case DICT_TVM_COL_VAR:
707 t = PSPPIRE_VAR_PTR_TYPE;
709 case DICT_TVM_COL_ALIGNMENT:
712 case DICT_TVM_COL_MEASURE:
713 t = measure_enum_type;
715 case DICT_TVM_COL_ROLE:
724 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
726 gint *indices, depth;
728 struct variable *var;
730 PsppireDict *dict = PSPPIRE_DICT (model);
732 g_return_val_if_fail (path, FALSE);
734 indices = gtk_tree_path_get_indices (path);
735 depth = gtk_tree_path_get_depth (path);
737 g_return_val_if_fail (depth == 1, FALSE);
741 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
744 iter->user_data = NULL;
748 var = psppire_dict_get_variable (dict, n);
750 g_assert (var_get_dict_index (var) == n);
752 iter->stamp = dict->stamp;
753 iter->user_data = var;
760 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
762 PsppireDict *dict = PSPPIRE_DICT (model);
763 struct variable *var;
766 if ( iter == NULL || iter->user_data == NULL)
769 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
771 var = iter->user_data;
773 idx = var_get_dict_index (var);
775 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
777 iter->user_data = NULL;
782 var = psppire_dict_get_variable (dict, idx + 1);
784 g_assert (var_get_dict_index (var) == idx + 1);
786 iter->user_data = var;
792 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
795 struct variable *var;
796 PsppireDict *dict = PSPPIRE_DICT (model);
798 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
800 var = iter->user_data;
802 path = gtk_tree_path_new ();
803 gtk_tree_path_append_index (path, var_get_dict_index (var));
808 const struct fmt_spec *var_get_write_format (const struct variable *);
811 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
812 gint column, GValue *value)
814 struct variable *var;
815 PsppireDict *dict = PSPPIRE_DICT (model);
817 g_return_if_fail (iter->stamp == dict->stamp);
819 var = iter->user_data;
821 const struct fmt_spec *fs = var_get_write_format (var);
825 case DICT_TVM_COL_NAME:
826 g_value_init (value, G_TYPE_STRING);
827 g_value_set_string (value, var_get_name (var));
829 case DICT_TVM_COL_WIDTH:
830 g_value_init (value, G_TYPE_INT);
831 g_value_set_int (value, fs->w);
833 case DICT_TVM_COL_DECIMAL:
834 g_value_init (value, G_TYPE_INT);
835 g_value_set_int (value, fs->d);
837 case DICT_TVM_COL_LABEL:
838 g_value_init (value, G_TYPE_STRING);
839 g_value_set_string (value, var_get_label (var));
841 case DICT_TVM_COL_COLUMNS:
842 g_value_init (value, G_TYPE_INT);
843 g_value_set_int (value, var_get_display_width (var));
845 case DICT_TVM_COL_ALIGNMENT:
846 g_value_init (value, align_enum_type);
847 g_value_set_enum (value, var_get_alignment (var));
849 case DICT_TVM_COL_MEASURE:
850 g_value_init (value, measure_enum_type);
851 g_value_set_enum (value, var_get_measure (var));
853 case DICT_TVM_COL_ROLE:
854 g_value_init (value, role_enum_type);
855 g_value_set_enum (value, var_get_role (var));
857 case DICT_TVM_COL_VAR:
858 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
859 g_value_set_boxed (value, var);
862 g_value_init (value, G_TYPE_STRING);
863 g_value_set_string (value, "????");
869 tree_model_iter_children (GtkTreeModel *tree_model,
877 tree_model_n_children (GtkTreeModel *model,
880 PsppireDict *dict = PSPPIRE_DICT (model);
883 return psppire_dict_get_var_cnt (dict);
889 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
890 GtkTreeIter *parent, gint n)
894 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
896 dict = PSPPIRE_DICT (model);
901 if ( n >= psppire_dict_get_var_cnt (dict) )
904 iter->stamp = dict->stamp;
905 iter->user_data = psppire_dict_get_variable (dict, n);
907 if ( !iter->user_data)
915 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
918 if ( ! dict_id_is_valid (dict->dict, name, false))
921 /* Make sure no other variable has this name */
922 if ( NULL != psppire_dict_lookup_var (dict, name))
925 dict_rename_var (dict->dict, v, name);
932 psppire_dict_get_weight_variable (const PsppireDict *dict)
934 return dict_get_weight (dict->dict);
941 psppire_dict_dump (const PsppireDict *dict)
944 const struct dictionary *d = dict->dict;
946 for (i = 0; i < dict_get_var_cnt (d); ++i)
948 const struct variable *v = psppire_dict_get_variable (dict, i);
949 int di = var_get_dict_index (v);
950 g_print ("`%s' idx=%d, fv=%d\n",
953 var_get_case_index(v));
961 psppire_dict_encoding (const PsppireDict *dict)
963 return dict_get_encoding (dict->dict);