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;
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 GTK_TYPE_BUTTON;
89 gi (GListModel *list, guint id)
91 GtkWidget *button = gtk_button_new ();
93 PsppireDict *dict = PSPPIRE_DICT (list);
95 if (id >= psppire_dict_get_var_cnt (dict))
97 gtk_button_set_label (GTK_BUTTON (button), _("Var"));
101 const struct variable *v = psppire_dict_get_variable (dict, id);
103 gtk_button_set_label (GTK_BUTTON (button), var_get_name (v));
104 gtk_widget_set_tooltip_text (button, var_get_label (v));
107 PangoContext *context = gtk_widget_create_pango_context (button);
108 PangoLayout *layout = pango_layout_new (context);
111 pango_layout_set_text (layout, "M", 1);
113 pango_layout_get_extents (layout, NULL, &rect);
115 g_object_unref (G_OBJECT (layout));
116 g_object_unref (G_OBJECT (context));
118 gtk_widget_set_size_request (button,
119 (0.25 + var_get_display_width (v))
120 * rect.width / PANGO_SCALE,
130 ssw_init_iface (GListModelInterface *iface)
132 iface->get_n_items = gni;
133 iface->get_item = gi;
134 iface->get_item_type = git;
138 /* --- variables --- */
139 static GObjectClass *parent_class = NULL;
141 static guint signals [n_SIGNALS];
143 /* --- functions --- */
145 * psppire_dict_get_type:
146 * @returns: the type ID for accelerator groups.
149 psppire_dict_get_type (void)
151 static GType object_type = 0;
155 static const GTypeInfo object_info = {
156 sizeof (PsppireDictClass),
157 (GBaseInitFunc) NULL,
158 (GBaseFinalizeFunc) NULL,
159 (GClassInitFunc) psppire_dict_class_init,
160 NULL, /* class_finalize */
161 NULL, /* class_data */
162 sizeof (PsppireDict),
164 (GInstanceInitFunc) psppire_dict_init,
167 static const GInterfaceInfo tree_model_info = {
168 (GInterfaceInitFunc) dictionary_tree_model_init,
173 static const GInterfaceInfo list_model_info = {
174 (GInterfaceInitFunc) ssw_init_iface,
179 object_type = g_type_register_static (G_TYPE_OBJECT,
183 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
186 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
195 psppire_dict_class_init (PsppireDictClass *class)
197 GObjectClass *object_class = G_OBJECT_CLASS (class);
199 parent_class = g_type_class_peek_parent (class);
201 object_class->dispose = psppire_dict_dispose;
203 signals [RESIZE_ITEM] =
204 g_signal_new ("resize-item",
205 G_TYPE_FROM_CLASS (class),
209 psppire_marshal_BOOLEAN__INT_INT,
215 signals [VARIABLE_CHANGED] =
216 g_signal_new ("variable-changed",
217 G_TYPE_FROM_CLASS (class),
221 psppire_marshal_VOID__INT_UINT_POINTER,
228 signals [VARIABLE_INSERTED] =
229 g_signal_new ("variable-inserted",
230 G_TYPE_FROM_CLASS (class),
234 g_cclosure_marshal_VOID__INT,
239 signals [VARIABLE_DELETED] =
240 g_signal_new ("variable-deleted",
241 G_TYPE_FROM_CLASS (class),
245 psppire_marshal_VOID__POINTER_INT_INT,
252 signals [WEIGHT_CHANGED] =
253 g_signal_new ("weight-changed",
254 G_TYPE_FROM_CLASS (class),
258 g_cclosure_marshal_VOID__INT,
263 signals [FILTER_CHANGED] =
264 g_signal_new ("filter-changed",
265 G_TYPE_FROM_CLASS (class),
269 g_cclosure_marshal_VOID__INT,
274 signals [SPLIT_CHANGED] =
275 g_signal_new ("split-changed",
276 G_TYPE_FROM_CLASS (class),
280 g_cclosure_marshal_VOID__VOID,
286 psppire_dict_dispose (GObject *object)
288 PsppireDict *d = PSPPIRE_DICT (object);
290 if (!d->dispose_has_run)
293 d->dispose_has_run = TRUE;
295 dict_set_callbacks (d->dict, NULL, NULL);
296 dict_unref (d->dict);
298 G_OBJECT_CLASS (parent_class)->dispose (object);
301 /* Pass on callbacks from src/data/dictionary, as
302 signals in the Gtk library */
304 addcb (struct dictionary *d, int idx, void *pd)
306 PsppireDict *dict = PSPPIRE_DICT (pd);
308 if (! dict->disable_insert_signal)
310 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
311 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
316 delcb (struct dictionary *d, const struct variable *var,
317 int dict_idx, int case_idx, void *pd)
319 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
320 var, dict_idx, case_idx);
321 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
325 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
327 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
328 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
332 weight_changed_callback (struct dictionary *d, int idx, void *pd)
334 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
338 filter_changed_callback (struct dictionary *d, int idx, void *pd)
340 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
344 split_changed_callback (struct dictionary *d, void *pd)
346 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
349 static const struct dict_callbacks gui_callbacks =
354 weight_changed_callback,
355 filter_changed_callback,
356 split_changed_callback
360 psppire_dict_init (PsppireDict *d)
362 d->dispose_has_run = FALSE;
364 d->stamp = g_random_int ();
365 d->disable_insert_signal = FALSE;
369 * psppire_dict_new_from_dict:
370 * @returns: a new #PsppireDict object
372 * Creates a new #PsppireDict.
375 psppire_dict_new_from_dict (struct dictionary *d)
377 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
378 new_dict->dict = dict_ref (d);
380 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
387 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
389 const struct variable *var = dict_get_weight (d);
391 struct dictionary *old_dict = dict->dict;
393 guint old_n = dict_get_var_cnt (dict->dict);
394 guint new_n = dict_get_var_cnt (d);
396 dict->dict = dict_ref (d);
397 dict_unref (old_dict);
399 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
401 var = dict_get_filter (d);
402 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
404 split_changed_callback (d, dict);
406 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
408 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
412 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
413 Returns true if successful, false if SIZE is insufficient. */
415 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
423 /* TRANSLATORS: This string must be a valid variable name. That means:
424 - The string must be at most 64 bytes (not characters) long.
425 - The string may not contain whitespace.
426 - The first character may not be '$'
427 - The first character may not be a digit
428 - The final character may not be '.' or '_'
430 len = snprintf (name, size, _("Var%04d"), d);
434 if (psppire_dict_lookup_var (dict, name) == NULL)
441 /* Insert a new variable at posn IDX, with the name NAME, and return the
443 IDX may take the special value -1, which will be treated the same as
444 zero. If NAME is null, then a name will be automatically assigned.
447 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
449 struct variable *var;
452 if (idx == -1) /* Note bug #56392. */
454 g_return_val_if_fail (d, NULL);
455 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
459 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
460 g_return_val_if_reached (NULL);
465 d->disable_insert_signal = TRUE;
467 var = dict_create_var (d->dict, name, 0);
469 dict_reorder_var (d->dict, var, idx);
471 d->disable_insert_signal = FALSE;
473 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
474 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
479 /* Delete N variables beginning at FIRST */
481 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
483 g_return_if_fail (d);
484 g_return_if_fail (d->dict);
485 g_return_if_fail (PSPPIRE_IS_DICT (d));
486 size_t varcnt = dict_get_var_cnt (d->dict);
487 g_return_if_fail (first < varcnt);
488 g_return_if_fail (first >= 0);
489 g_return_if_fail (n > 0);
490 g_return_if_fail (first + n <= varcnt);
492 dict_delete_consecutive_vars (d->dict, first, n);
497 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
499 struct variable *var;
501 g_assert (PSPPIRE_IS_DICT (d));
503 if (! dict_id_is_valid (d->dict, name, false))
506 if (idx < dict_get_var_cnt (d->dict))
508 /* This is an existing variable? */
509 var = dict_get_var (d->dict, idx);
510 dict_rename_var (d->dict, var, name);
515 dict_create_var (d->dict, name, 0);
523 /* Return the IDXth variable.
524 Will return NULL if IDX exceeds the number of variables in the dictionary.
527 psppire_dict_get_variable (const PsppireDict *d, gint idx)
529 g_return_val_if_fail (d, NULL);
530 g_return_val_if_fail (d->dict, NULL);
532 if (dict_get_var_cnt (d->dict) <= idx)
535 return dict_get_var (d->dict, idx);
539 /* Return the number of variables in the dictionary */
541 psppire_dict_get_var_cnt (const PsppireDict *d)
543 g_return_val_if_fail (d, -1);
544 g_return_val_if_fail (d->dict, -1);
546 return dict_get_var_cnt (d->dict);
550 /* Return the number of `union value's in the dictionary */
552 psppire_dict_get_value_cnt (const PsppireDict *d)
554 g_return_val_if_fail (d, -1);
555 g_return_val_if_fail (d->dict, -1);
557 return dict_get_next_value_idx (d->dict);
561 /* Returns the prototype for the cases that match the dictionary */
562 const struct caseproto *
563 psppire_dict_get_proto (const PsppireDict *d)
565 g_return_val_if_fail (d, NULL);
566 g_return_val_if_fail (d->dict, NULL);
568 return dict_get_proto (d->dict);
572 /* Return a variable by name.
573 Return NULL if it doesn't exist
576 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
578 g_return_val_if_fail (d, NULL);
579 g_return_val_if_fail (d->dict, NULL);
581 return dict_lookup_var (d->dict, name);
584 /* Clears the contents of D */
586 psppire_dict_clear (PsppireDict *d)
588 g_return_if_fail (d);
589 g_return_if_fail (d->dict);
592 dict_clear (d->dict);
597 /* Return true if NAME would be a valid name of a variable to add to the
598 dictionary. False otherwise.
599 If REPORT is true, then invalid names will be reported as such as errors
602 psppire_dict_check_name (const PsppireDict *dict,
603 const gchar *name, gboolean report)
605 if (! dict_id_is_valid (dict->dict, name, report))
608 if (psppire_dict_lookup_var (dict, name))
611 msg (ME, _("Duplicate variable name."));
620 psppire_dict_get_next_value_idx (const PsppireDict *dict)
622 return dict_get_next_value_idx (dict->dict);
626 /* Tree Model Stuff */
628 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
630 static gint tree_model_n_columns (GtkTreeModel *model);
632 static GType tree_model_column_type (GtkTreeModel *model, gint index);
634 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
637 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
639 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
642 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
643 gint column, GValue *value);
645 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
646 GtkTreeIter *parent, gint n);
648 static gint tree_model_n_children (GtkTreeModel *tree_model,
651 static gboolean tree_model_iter_children (GtkTreeModel *,
655 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
659 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
663 dictionary_tree_model_init (GtkTreeModelIface *iface)
665 iface->get_flags = tree_model_get_flags;
666 iface->get_n_columns = tree_model_n_columns;
667 iface->get_column_type = tree_model_column_type;
668 iface->get_iter = tree_model_get_iter;
669 iface->iter_next = tree_model_iter_next;
670 iface->get_path = tree_model_get_path;
671 iface->get_value = tree_model_get_value;
673 iface->iter_children = tree_model_iter_children ;
674 iface->iter_has_child = tree_model_iter_has_child ;
675 iface->iter_n_children = tree_model_n_children ;
676 iface->iter_nth_child = tree_model_nth_child ;
677 iface->iter_parent = tree_model_iter_parent ;
681 tree_model_iter_has_child (GtkTreeModel *tree_model,
688 tree_model_iter_parent (GtkTreeModel *tree_model,
695 static GtkTreeModelFlags
696 tree_model_get_flags (GtkTreeModel *model)
698 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
700 return GTK_TREE_MODEL_LIST_ONLY;
705 tree_model_n_columns (GtkTreeModel *model)
711 tree_model_column_type (GtkTreeModel *model, gint index)
713 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
719 case DICT_TVM_COL_NAME:
720 case DICT_TVM_COL_LABEL:
723 case DICT_TVM_COL_DECIMAL:
724 case DICT_TVM_COL_WIDTH:
725 case DICT_TVM_COL_COLUMNS:
728 case DICT_TVM_COL_VAR:
729 t = PSPPIRE_VAR_PTR_TYPE;
731 case DICT_TVM_COL_ALIGNMENT:
734 case DICT_TVM_COL_MEASURE:
735 t = measure_enum_type;
737 case DICT_TVM_COL_ROLE:
746 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
748 gint *indices, depth;
750 struct variable *var;
752 PsppireDict *dict = PSPPIRE_DICT (model);
754 g_return_val_if_fail (path, FALSE);
756 indices = gtk_tree_path_get_indices (path);
757 depth = gtk_tree_path_get_depth (path);
759 g_return_val_if_fail (depth == 1, FALSE);
763 if (n < 0 || n >= psppire_dict_get_var_cnt (dict))
766 iter->user_data = NULL;
770 var = psppire_dict_get_variable (dict, n);
772 g_assert (var_get_dict_index (var) == n);
774 iter->stamp = dict->stamp;
775 iter->user_data = var;
782 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
784 PsppireDict *dict = PSPPIRE_DICT (model);
785 struct variable *var;
788 if (iter == NULL || iter->user_data == NULL)
791 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
793 var = iter->user_data;
795 idx = var_get_dict_index (var);
797 if (idx + 1 >= psppire_dict_get_var_cnt (dict))
799 iter->user_data = NULL;
804 var = psppire_dict_get_variable (dict, idx + 1);
806 g_assert (var_get_dict_index (var) == idx + 1);
808 iter->user_data = var;
814 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
817 struct variable *var;
818 PsppireDict *dict = PSPPIRE_DICT (model);
820 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
822 var = iter->user_data;
824 path = gtk_tree_path_new ();
825 gtk_tree_path_append_index (path, var_get_dict_index (var));
830 const struct fmt_spec *var_get_write_format (const struct variable *);
833 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
834 gint column, GValue *value)
836 struct variable *var;
837 PsppireDict *dict = PSPPIRE_DICT (model);
839 g_return_if_fail (iter->stamp == dict->stamp);
841 var = iter->user_data;
843 const struct fmt_spec *fs = var_get_write_format (var);
847 case DICT_TVM_COL_NAME:
848 g_value_init (value, G_TYPE_STRING);
849 g_value_set_string (value, var_get_name (var));
851 case DICT_TVM_COL_WIDTH:
852 g_value_init (value, G_TYPE_INT);
853 g_value_set_int (value, fs->w);
855 case DICT_TVM_COL_DECIMAL:
856 g_value_init (value, G_TYPE_INT);
857 g_value_set_int (value, fs->d);
859 case DICT_TVM_COL_LABEL:
860 g_value_init (value, G_TYPE_STRING);
861 g_value_set_string (value, var_get_label (var));
863 case DICT_TVM_COL_COLUMNS:
864 g_value_init (value, G_TYPE_INT);
865 g_value_set_int (value, var_get_display_width (var));
867 case DICT_TVM_COL_ALIGNMENT:
868 g_value_init (value, align_enum_type);
869 g_value_set_enum (value, var_get_alignment (var));
871 case DICT_TVM_COL_MEASURE:
872 g_value_init (value, measure_enum_type);
873 g_value_set_enum (value, var_get_measure (var));
875 case DICT_TVM_COL_ROLE:
876 g_value_init (value, role_enum_type);
877 g_value_set_enum (value, var_get_role (var));
879 case DICT_TVM_COL_VAR:
880 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
881 g_value_set_boxed (value, var);
884 g_value_init (value, G_TYPE_STRING);
885 g_value_set_string (value, "????");
891 tree_model_iter_children (GtkTreeModel *tree_model,
899 tree_model_n_children (GtkTreeModel *model,
902 PsppireDict *dict = PSPPIRE_DICT (model);
905 return psppire_dict_get_var_cnt (dict);
911 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
912 GtkTreeIter *parent, gint n)
916 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
918 dict = PSPPIRE_DICT (model);
923 if (n >= psppire_dict_get_var_cnt (dict))
926 iter->stamp = dict->stamp;
927 iter->user_data = psppire_dict_get_variable (dict, n);
929 if (!iter->user_data)
937 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
940 if (! dict_id_is_valid (dict->dict, name, false))
943 /* Make sure no other variable has this name */
944 if (NULL != psppire_dict_lookup_var (dict, name))
947 dict_rename_var (dict->dict, v, name);
954 psppire_dict_get_weight_variable (const PsppireDict *dict)
956 return dict_get_weight (dict->dict);
963 psppire_dict_dump (const PsppireDict *dict)
966 const struct dictionary *d = dict->dict;
968 for (i = 0; i < dict_get_var_cnt (d); ++i)
970 const struct variable *v = psppire_dict_get_variable (dict, i);
971 int di = var_get_dict_index (v);
972 g_print ("`%s' idx=%d, fv=%d\n",
975 var_get_case_index(v));
983 psppire_dict_encoding (const PsppireDict *dict)
985 return dict_get_encoding (dict->dict);