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_dispose (GObject *object);
68 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
73 gni (GListModel *list)
75 PsppireDict *dict = PSPPIRE_DICT (list);
77 return psppire_dict_get_n_vars (dict);
81 git (GListModel *list)
83 return GTK_TYPE_BUTTON;
87 gi (GListModel *list, guint id)
89 GtkWidget *button = gtk_button_new ();
91 PsppireDict *dict = PSPPIRE_DICT (list);
93 if (id >= psppire_dict_get_n_vars (dict))
95 gtk_button_set_label (GTK_BUTTON (button), _("Var"));
99 const struct variable *v = psppire_dict_get_variable (dict, id);
101 gtk_button_set_label (GTK_BUTTON (button), var_get_name (v));
102 gtk_widget_set_tooltip_text (button, var_get_label (v));
105 PangoContext *context = gtk_widget_create_pango_context (button);
106 PangoLayout *layout = pango_layout_new (context);
109 pango_layout_set_text (layout, "M", 1);
111 pango_layout_get_extents (layout, NULL, &rect);
113 g_object_unref (G_OBJECT (layout));
114 g_object_unref (G_OBJECT (context));
116 gtk_widget_set_size_request (button,
117 (0.25 + var_get_display_width (v))
118 * rect.width / PANGO_SCALE,
128 ssw_init_iface (GListModelInterface *iface)
130 iface->get_n_items = gni;
131 iface->get_item = gi;
132 iface->get_item_type = git;
136 /* --- variables --- */
137 static GObjectClass *parent_class = NULL;
139 static guint signals [n_SIGNALS];
141 /* --- functions --- */
143 G_DEFINE_TYPE_WITH_CODE (PsppireDict, psppire_dict, G_TYPE_OBJECT,
144 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
145 dictionary_tree_model_init)
146 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
150 psppire_dict_class_init (PsppireDictClass *class)
152 GObjectClass *object_class = G_OBJECT_CLASS (class);
154 parent_class = g_type_class_peek_parent (class);
156 object_class->dispose = psppire_dict_dispose;
158 signals [RESIZE_ITEM] =
159 g_signal_new ("resize-item",
160 G_TYPE_FROM_CLASS (class),
164 psppire_marshal_BOOLEAN__INT_INT,
170 signals [VARIABLE_CHANGED] =
171 g_signal_new ("variable-changed",
172 G_TYPE_FROM_CLASS (class),
176 psppire_marshal_VOID__INT_UINT_POINTER,
183 signals [VARIABLE_INSERTED] =
184 g_signal_new ("variable-inserted",
185 G_TYPE_FROM_CLASS (class),
189 g_cclosure_marshal_VOID__INT,
194 signals [VARIABLE_DELETED] =
195 g_signal_new ("variable-deleted",
196 G_TYPE_FROM_CLASS (class),
200 psppire_marshal_VOID__POINTER_INT_INT,
207 signals [WEIGHT_CHANGED] =
208 g_signal_new ("weight-changed",
209 G_TYPE_FROM_CLASS (class),
213 g_cclosure_marshal_VOID__INT,
218 signals [FILTER_CHANGED] =
219 g_signal_new ("filter-changed",
220 G_TYPE_FROM_CLASS (class),
224 g_cclosure_marshal_VOID__INT,
229 signals [SPLIT_CHANGED] =
230 g_signal_new ("split-changed",
231 G_TYPE_FROM_CLASS (class),
235 g_cclosure_marshal_VOID__VOID,
241 psppire_dict_dispose (GObject *object)
243 PsppireDict *d = PSPPIRE_DICT (object);
245 if (!d->dispose_has_run)
248 d->dispose_has_run = TRUE;
250 dict_set_callbacks (d->dict, NULL, NULL);
251 dict_unref (d->dict);
253 G_OBJECT_CLASS (parent_class)->dispose (object);
256 /* Pass on callbacks from src/data/dictionary, as
257 signals in the Gtk library */
259 addcb (struct dictionary *d, int idx, void *pd)
261 PsppireDict *dict = PSPPIRE_DICT (pd);
263 if (! dict->disable_insert_signal)
265 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
266 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
271 delcb (struct dictionary *d, const struct variable *var,
272 int dict_idx, int case_idx, void *pd)
274 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
275 var, dict_idx, case_idx);
276 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
280 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
282 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
283 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
287 weight_changed_callback (struct dictionary *d, int idx, void *pd)
289 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
293 filter_changed_callback (struct dictionary *d, int idx, void *pd)
295 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
299 split_changed_callback (struct dictionary *d, void *pd)
301 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
304 static const struct dict_callbacks gui_callbacks =
309 weight_changed_callback,
310 filter_changed_callback,
311 split_changed_callback
315 psppire_dict_init (PsppireDict *d)
317 d->dispose_has_run = FALSE;
319 d->stamp = g_random_int ();
320 d->disable_insert_signal = FALSE;
324 * psppire_dict_new_from_dict:
325 * @returns: a new #PsppireDict object
327 * Creates a new #PsppireDict.
330 psppire_dict_new_from_dict (struct dictionary *d)
332 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
333 new_dict->dict = dict_ref (d);
335 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
342 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
344 const struct variable *var = dict_get_weight (d);
346 struct dictionary *old_dict = dict->dict;
348 guint old_n = dict_get_n_vars (dict->dict);
349 guint new_n = dict_get_n_vars (d);
351 dict->dict = dict_ref (d);
352 dict_unref (old_dict);
354 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
356 var = dict_get_filter (d);
357 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
359 split_changed_callback (d, dict);
361 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
363 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
367 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
368 Returns true if successful, false if SIZE is insufficient. */
370 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
378 /* TRANSLATORS: This string must be a valid variable name. That means:
379 - The string must be at most 64 bytes (not characters) long.
380 - The string may not contain whitespace.
381 - The first character may not be '$'
382 - The first character may not be a digit
383 - The final character may not be '.' or '_'
385 len = snprintf (name, size, _("Var%04d"), d);
389 if (psppire_dict_lookup_var (dict, name) == NULL)
396 /* Insert a new variable at posn IDX, with the name NAME, and return the
398 IDX may take the special value -1, which will be treated the same as
399 zero. If NAME is null, then a name will be automatically assigned.
402 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
404 struct variable *var;
407 if (idx == -1) /* Note bug #56392. */
409 g_return_val_if_fail (d, NULL);
410 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
414 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
415 g_return_val_if_reached (NULL);
420 d->disable_insert_signal = TRUE;
422 var = dict_create_var (d->dict, name, 0);
424 dict_reorder_var (d->dict, var, idx);
426 d->disable_insert_signal = FALSE;
428 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
429 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
434 /* Delete N variables beginning at FIRST */
436 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
438 g_return_if_fail (d);
439 g_return_if_fail (d->dict);
440 g_return_if_fail (PSPPIRE_IS_DICT (d));
441 size_t varcnt = dict_get_n_vars (d->dict);
442 g_return_if_fail (first < varcnt);
443 g_return_if_fail (first >= 0);
444 g_return_if_fail (n > 0);
445 g_return_if_fail (first + n <= varcnt);
447 dict_delete_consecutive_vars (d->dict, first, n);
452 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
455 g_assert (PSPPIRE_IS_DICT (d));
457 if (! dict_id_is_valid (d->dict, name, false))
460 if (idx < dict_get_n_vars (d->dict))
462 /* This is an existing variable? */
463 struct variable * var = dict_get_var (d->dict, idx);
464 dict_rename_var (d->dict, var, name);
469 dict_create_var (d->dict, name, 0);
477 /* Return the IDXth variable.
478 Will return NULL if IDX exceeds the number of variables in the dictionary.
481 psppire_dict_get_variable (const PsppireDict *d, gint idx)
483 g_return_val_if_fail (d, NULL);
484 g_return_val_if_fail (d->dict, NULL);
486 if (dict_get_n_vars (d->dict) <= idx)
489 return dict_get_var (d->dict, idx);
493 /* Return the number of variables in the dictionary */
495 psppire_dict_get_n_vars (const PsppireDict *d)
497 g_return_val_if_fail (d, -1);
498 g_return_val_if_fail (d->dict, -1);
500 return dict_get_n_vars (d->dict);
504 /* Return the number of `union value's in the dictionary */
506 psppire_dict_get_n_values (const PsppireDict *d)
508 g_return_val_if_fail (d, -1);
509 g_return_val_if_fail (d->dict, -1);
511 return dict_get_next_value_idx (d->dict);
515 /* Returns the prototype for the cases that match the dictionary */
516 const struct caseproto *
517 psppire_dict_get_proto (const PsppireDict *d)
519 g_return_val_if_fail (d, NULL);
520 g_return_val_if_fail (d->dict, NULL);
522 return dict_get_proto (d->dict);
526 /* Return a variable by name.
527 Return NULL if it doesn't exist
530 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
532 g_return_val_if_fail (d, NULL);
533 g_return_val_if_fail (d->dict, NULL);
535 return dict_lookup_var (d->dict, name);
538 /* Clears the contents of D */
540 psppire_dict_clear (PsppireDict *d)
542 g_return_if_fail (d);
543 g_return_if_fail (d->dict);
546 dict_clear (d->dict);
551 /* Return true if NAME would be a valid name of a variable to add to the
552 dictionary. False otherwise.
553 If REPORT is true, then invalid names will be reported as such as errors
556 psppire_dict_check_name (const PsppireDict *dict,
557 const gchar *name, gboolean report)
559 if (! dict_id_is_valid (dict->dict, name, report))
562 if (psppire_dict_lookup_var (dict, name))
565 msg (ME, _("Duplicate variable name."));
574 psppire_dict_get_next_value_idx (const PsppireDict *dict)
576 return dict_get_next_value_idx (dict->dict);
580 /* Tree Model Stuff */
582 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
584 static gint tree_model_n_columns (GtkTreeModel *model);
586 static GType tree_model_column_type (GtkTreeModel *model, gint index);
588 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
591 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
593 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
596 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
597 gint column, GValue *value);
599 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
600 GtkTreeIter *parent, gint n);
602 static gint tree_model_n_children (GtkTreeModel *tree_model,
605 static gboolean tree_model_iter_children (GtkTreeModel *,
609 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
613 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
617 dictionary_tree_model_init (GtkTreeModelIface *iface)
619 iface->get_flags = tree_model_get_flags;
620 iface->get_n_columns = tree_model_n_columns;
621 iface->get_column_type = tree_model_column_type;
622 iface->get_iter = tree_model_get_iter;
623 iface->iter_next = tree_model_iter_next;
624 iface->get_path = tree_model_get_path;
625 iface->get_value = tree_model_get_value;
627 iface->iter_children = tree_model_iter_children ;
628 iface->iter_has_child = tree_model_iter_has_child ;
629 iface->iter_n_children = tree_model_n_children ;
630 iface->iter_nth_child = tree_model_nth_child ;
631 iface->iter_parent = tree_model_iter_parent ;
635 tree_model_iter_has_child (GtkTreeModel *tree_model,
642 tree_model_iter_parent (GtkTreeModel *tree_model,
649 static GtkTreeModelFlags
650 tree_model_get_flags (GtkTreeModel *model)
652 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
654 return GTK_TREE_MODEL_LIST_ONLY;
659 tree_model_n_columns (GtkTreeModel *model)
665 tree_model_column_type (GtkTreeModel *model, gint index)
667 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
673 case DICT_TVM_COL_NAME:
674 case DICT_TVM_COL_LABEL:
677 case DICT_TVM_COL_DECIMAL:
678 case DICT_TVM_COL_WIDTH:
679 case DICT_TVM_COL_COLUMNS:
682 case DICT_TVM_COL_VAR:
683 t = PSPPIRE_VAR_PTR_TYPE;
685 case DICT_TVM_COL_ALIGNMENT:
688 case DICT_TVM_COL_MEASURE:
689 t = measure_enum_type;
691 case DICT_TVM_COL_ROLE:
700 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
702 gint *indices, depth;
704 struct variable *var;
706 PsppireDict *dict = PSPPIRE_DICT (model);
708 g_return_val_if_fail (path, FALSE);
710 indices = gtk_tree_path_get_indices (path);
711 depth = gtk_tree_path_get_depth (path);
713 g_return_val_if_fail (depth == 1, FALSE);
717 if (n < 0 || n >= psppire_dict_get_n_vars (dict))
720 iter->user_data = NULL;
724 var = psppire_dict_get_variable (dict, n);
726 g_assert (var_get_dict_index (var) == n);
728 iter->stamp = dict->stamp;
729 iter->user_data = var;
736 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
738 PsppireDict *dict = PSPPIRE_DICT (model);
739 struct variable *var;
742 if (iter == NULL || iter->user_data == NULL)
745 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
747 var = iter->user_data;
749 idx = var_get_dict_index (var);
751 if (idx + 1 >= psppire_dict_get_n_vars (dict))
753 iter->user_data = NULL;
758 var = psppire_dict_get_variable (dict, idx + 1);
760 g_assert (var_get_dict_index (var) == idx + 1);
762 iter->user_data = var;
768 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
771 struct variable *var;
772 PsppireDict *dict = PSPPIRE_DICT (model);
774 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
776 var = iter->user_data;
778 path = gtk_tree_path_new ();
779 gtk_tree_path_append_index (path, var_get_dict_index (var));
784 const struct fmt_spec *var_get_write_format (const struct variable *);
787 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
788 gint column, GValue *value)
790 struct variable *var;
791 PsppireDict *dict = PSPPIRE_DICT (model);
793 g_return_if_fail (iter->stamp == dict->stamp);
795 var = iter->user_data;
797 const struct fmt_spec *fs = var_get_write_format (var);
801 case DICT_TVM_COL_NAME:
802 g_value_init (value, G_TYPE_STRING);
803 g_value_set_string (value, var_get_name (var));
805 case DICT_TVM_COL_WIDTH:
806 g_value_init (value, G_TYPE_INT);
807 g_value_set_int (value, fs->w);
809 case DICT_TVM_COL_DECIMAL:
810 g_value_init (value, G_TYPE_INT);
811 g_value_set_int (value, fs->d);
813 case DICT_TVM_COL_LABEL:
814 g_value_init (value, G_TYPE_STRING);
815 g_value_set_string (value, var_get_label (var));
817 case DICT_TVM_COL_COLUMNS:
818 g_value_init (value, G_TYPE_INT);
819 g_value_set_int (value, var_get_display_width (var));
821 case DICT_TVM_COL_ALIGNMENT:
822 g_value_init (value, align_enum_type);
823 g_value_set_enum (value, var_get_alignment (var));
825 case DICT_TVM_COL_MEASURE:
826 g_value_init (value, measure_enum_type);
827 g_value_set_enum (value, var_get_measure (var));
829 case DICT_TVM_COL_ROLE:
830 g_value_init (value, role_enum_type);
831 g_value_set_enum (value, var_get_role (var));
833 case DICT_TVM_COL_VAR:
834 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
835 g_value_set_boxed (value, var);
838 g_value_init (value, G_TYPE_STRING);
839 g_value_set_string (value, "????");
845 tree_model_iter_children (GtkTreeModel *tree_model,
853 tree_model_n_children (GtkTreeModel *model,
856 PsppireDict *dict = PSPPIRE_DICT (model);
859 return psppire_dict_get_n_vars (dict);
865 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
866 GtkTreeIter *parent, gint n)
870 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
872 dict = PSPPIRE_DICT (model);
877 if (n >= psppire_dict_get_n_vars (dict))
880 iter->stamp = dict->stamp;
881 iter->user_data = psppire_dict_get_variable (dict, n);
883 if (!iter->user_data)
891 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
894 if (! dict_id_is_valid (dict->dict, name, false))
897 /* Make sure no other variable has this name */
898 if (NULL != psppire_dict_lookup_var (dict, name))
901 dict_rename_var (dict->dict, v, name);
908 psppire_dict_get_weight_variable (const PsppireDict *dict)
910 return dict_get_weight (dict->dict);
914 psppire_dict_encoding (const PsppireDict *dict)
916 return dict_get_encoding (dict->dict);