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))
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.
555 psppire_dict_check_name (const PsppireDict *dict,
558 return (dict_id_is_valid (dict->dict, name)
559 && !psppire_dict_lookup_var (dict, name));
563 psppire_dict_get_next_value_idx (const PsppireDict *dict)
565 return dict_get_next_value_idx (dict->dict);
569 /* Tree Model Stuff */
571 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
573 static gint tree_model_n_columns (GtkTreeModel *model);
575 static GType tree_model_column_type (GtkTreeModel *model, gint index);
577 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
580 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
582 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
585 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
586 gint column, GValue *value);
588 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
589 GtkTreeIter *parent, gint n);
591 static gint tree_model_n_children (GtkTreeModel *tree_model,
594 static gboolean tree_model_iter_children (GtkTreeModel *,
598 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
602 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
606 dictionary_tree_model_init (GtkTreeModelIface *iface)
608 iface->get_flags = tree_model_get_flags;
609 iface->get_n_columns = tree_model_n_columns;
610 iface->get_column_type = tree_model_column_type;
611 iface->get_iter = tree_model_get_iter;
612 iface->iter_next = tree_model_iter_next;
613 iface->get_path = tree_model_get_path;
614 iface->get_value = tree_model_get_value;
616 iface->iter_children = tree_model_iter_children ;
617 iface->iter_has_child = tree_model_iter_has_child ;
618 iface->iter_n_children = tree_model_n_children ;
619 iface->iter_nth_child = tree_model_nth_child ;
620 iface->iter_parent = tree_model_iter_parent ;
624 tree_model_iter_has_child (GtkTreeModel *tree_model,
631 tree_model_iter_parent (GtkTreeModel *tree_model,
638 static GtkTreeModelFlags
639 tree_model_get_flags (GtkTreeModel *model)
641 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
643 return GTK_TREE_MODEL_LIST_ONLY;
648 tree_model_n_columns (GtkTreeModel *model)
654 tree_model_column_type (GtkTreeModel *model, gint index)
656 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
662 case DICT_TVM_COL_NAME:
663 case DICT_TVM_COL_LABEL:
666 case DICT_TVM_COL_DECIMAL:
667 case DICT_TVM_COL_WIDTH:
668 case DICT_TVM_COL_COLUMNS:
671 case DICT_TVM_COL_VAR:
672 t = PSPPIRE_VAR_PTR_TYPE;
674 case DICT_TVM_COL_ALIGNMENT:
677 case DICT_TVM_COL_MEASURE:
678 t = measure_enum_type;
680 case DICT_TVM_COL_ROLE:
689 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
691 gint *indices, depth;
693 struct variable *var;
695 PsppireDict *dict = PSPPIRE_DICT (model);
697 g_return_val_if_fail (path, FALSE);
699 indices = gtk_tree_path_get_indices (path);
700 depth = gtk_tree_path_get_depth (path);
702 g_return_val_if_fail (depth == 1, FALSE);
706 if (n < 0 || n >= psppire_dict_get_n_vars (dict))
709 iter->user_data = NULL;
713 var = psppire_dict_get_variable (dict, n);
715 g_assert (var_get_dict_index (var) == n);
717 iter->stamp = dict->stamp;
718 iter->user_data = var;
725 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
727 PsppireDict *dict = PSPPIRE_DICT (model);
728 struct variable *var;
731 if (iter == NULL || iter->user_data == NULL)
734 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
736 var = iter->user_data;
738 idx = var_get_dict_index (var);
740 if (idx + 1 >= psppire_dict_get_n_vars (dict))
742 iter->user_data = NULL;
747 var = psppire_dict_get_variable (dict, idx + 1);
749 g_assert (var_get_dict_index (var) == idx + 1);
751 iter->user_data = var;
757 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
760 struct variable *var;
761 PsppireDict *dict = PSPPIRE_DICT (model);
763 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
765 var = iter->user_data;
767 path = gtk_tree_path_new ();
768 gtk_tree_path_append_index (path, var_get_dict_index (var));
773 const struct fmt_spec *var_get_write_format (const struct variable *);
776 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
777 gint column, GValue *value)
779 struct variable *var;
780 PsppireDict *dict = PSPPIRE_DICT (model);
782 g_return_if_fail (iter->stamp == dict->stamp);
784 var = iter->user_data;
786 const struct fmt_spec *fs = var_get_write_format (var);
790 case DICT_TVM_COL_NAME:
791 g_value_init (value, G_TYPE_STRING);
792 g_value_set_string (value, var_get_name (var));
794 case DICT_TVM_COL_WIDTH:
795 g_value_init (value, G_TYPE_INT);
796 g_value_set_int (value, fs->w);
798 case DICT_TVM_COL_DECIMAL:
799 g_value_init (value, G_TYPE_INT);
800 g_value_set_int (value, fs->d);
802 case DICT_TVM_COL_LABEL:
803 g_value_init (value, G_TYPE_STRING);
804 g_value_set_string (value, var_get_label (var));
806 case DICT_TVM_COL_COLUMNS:
807 g_value_init (value, G_TYPE_INT);
808 g_value_set_int (value, var_get_display_width (var));
810 case DICT_TVM_COL_ALIGNMENT:
811 g_value_init (value, align_enum_type);
812 g_value_set_enum (value, var_get_alignment (var));
814 case DICT_TVM_COL_MEASURE:
815 g_value_init (value, measure_enum_type);
816 g_value_set_enum (value, var_get_measure (var));
818 case DICT_TVM_COL_ROLE:
819 g_value_init (value, role_enum_type);
820 g_value_set_enum (value, var_get_role (var));
822 case DICT_TVM_COL_VAR:
823 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
824 g_value_set_boxed (value, var);
827 g_value_init (value, G_TYPE_STRING);
828 g_value_set_string (value, "????");
834 tree_model_iter_children (GtkTreeModel *tree_model,
842 tree_model_n_children (GtkTreeModel *model,
845 PsppireDict *dict = PSPPIRE_DICT (model);
848 return psppire_dict_get_n_vars (dict);
854 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
855 GtkTreeIter *parent, gint n)
859 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
861 dict = PSPPIRE_DICT (model);
866 if (n >= psppire_dict_get_n_vars (dict))
869 iter->stamp = dict->stamp;
870 iter->user_data = psppire_dict_get_variable (dict, n);
872 if (!iter->user_data)
880 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
883 if (! dict_id_is_valid (dict->dict, name))
886 /* Make sure no other variable has this name */
887 if (NULL != psppire_dict_lookup_var (dict, name))
890 dict_rename_var (dict->dict, v, name);
897 psppire_dict_get_weight_variable (const PsppireDict *dict)
899 return dict_get_weight (dict->dict);
903 psppire_dict_encoding (const PsppireDict *dict)
905 return dict_get_encoding (dict->dict);