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;
66 /* --- prototypes --- */
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_n_vars (dict);
82 git (GListModel *list)
84 return GTK_TYPE_BUTTON;
88 gi (GListModel *list, guint id)
90 GtkWidget *button = gtk_button_new ();
92 PsppireDict *dict = PSPPIRE_DICT (list);
94 if (id >= psppire_dict_get_n_vars (dict))
96 gtk_button_set_label (GTK_BUTTON (button), _("Var"));
100 const struct variable *v = psppire_dict_get_variable (dict, id);
102 gtk_button_set_label (GTK_BUTTON (button), var_get_name (v));
103 gtk_widget_set_tooltip_text (button, var_get_label (v));
106 PangoContext *context = gtk_widget_create_pango_context (button);
107 PangoLayout *layout = pango_layout_new (context);
110 pango_layout_set_text (layout, "M", 1);
112 pango_layout_get_extents (layout, NULL, &rect);
114 g_object_unref (G_OBJECT (layout));
115 g_object_unref (G_OBJECT (context));
117 gtk_widget_set_size_request (button,
118 (0.25 + var_get_display_width (v))
119 * rect.width / PANGO_SCALE,
129 ssw_init_iface (GListModelInterface *iface)
131 iface->get_n_items = gni;
132 iface->get_item = gi;
133 iface->get_item_type = git;
137 /* --- variables --- */
138 static GObjectClass *parent_class = NULL;
140 static guint signals [n_SIGNALS];
142 /* --- functions --- */
144 G_DEFINE_TYPE_WITH_CODE (PsppireDict, psppire_dict, G_TYPE_OBJECT,
145 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
146 dictionary_tree_model_init)
147 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
151 psppire_dict_class_init (PsppireDictClass *class)
153 GObjectClass *object_class = G_OBJECT_CLASS (class);
155 parent_class = g_type_class_peek_parent (class);
157 object_class->dispose = psppire_dict_dispose;
159 signals [RESIZE_ITEM] =
160 g_signal_new ("resize-item",
161 G_TYPE_FROM_CLASS (class),
165 psppire_marshal_BOOLEAN__INT_INT,
171 signals [VARIABLE_CHANGED] =
172 g_signal_new ("variable-changed",
173 G_TYPE_FROM_CLASS (class),
177 psppire_marshal_VOID__INT_UINT_POINTER,
184 signals [VARIABLE_INSERTED] =
185 g_signal_new ("variable-inserted",
186 G_TYPE_FROM_CLASS (class),
190 g_cclosure_marshal_VOID__INT,
195 signals [VARIABLES_DELETED] =
196 g_signal_new ("variables-deleted",
197 G_TYPE_FROM_CLASS (class),
201 psppire_marshal_VOID__INT_UINT,
207 signals [VARIABLE_MOVED] =
208 g_signal_new ("variable-moved",
209 G_TYPE_FROM_CLASS (class),
213 psppire_marshal_VOID__INT_INT,
219 signals [WEIGHT_CHANGED] =
220 g_signal_new ("weight-changed",
221 G_TYPE_FROM_CLASS (class),
225 g_cclosure_marshal_VOID__INT,
230 signals [FILTER_CHANGED] =
231 g_signal_new ("filter-changed",
232 G_TYPE_FROM_CLASS (class),
236 g_cclosure_marshal_VOID__INT,
241 signals [SPLIT_CHANGED] =
242 g_signal_new ("split-changed",
243 G_TYPE_FROM_CLASS (class),
247 g_cclosure_marshal_VOID__VOID,
253 psppire_dict_dispose (GObject *object)
255 PsppireDict *d = PSPPIRE_DICT (object);
257 if (!d->dispose_has_run)
260 d->dispose_has_run = TRUE;
262 dict_set_callbacks (d->dict, NULL, NULL);
263 dict_unref (d->dict);
265 G_OBJECT_CLASS (parent_class)->dispose (object);
268 /* Pass on callbacks from src/data/dictionary, as
269 signals in the Gtk library */
271 var_added_callback (struct dictionary *d, int idx, void *pd)
273 PsppireDict *dict = PSPPIRE_DICT (pd);
275 if (! dict->disable_insert_signal)
277 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
278 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
283 vars_deleted_callback (struct dictionary *d, int dict_idx, unsigned int n, void *pd)
285 g_signal_emit (pd, signals [VARIABLES_DELETED], 0, dict_idx, n);
286 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
290 var_moved_callback (struct dictionary *d, int new_dict_index, int old_dict_index, void *pd)
292 g_signal_emit (pd, signals [VARIABLE_MOVED], 0, new_dict_index, old_dict_index);
296 var_changed_callback (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
298 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
299 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
303 weight_changed_callback (struct dictionary *d, int idx, void *pd)
305 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
309 filter_changed_callback (struct dictionary *d, int idx, void *pd)
311 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
315 split_changed_callback (struct dictionary *d, void *pd)
317 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
320 static const struct dict_callbacks gui_callbacks =
322 .var_added = var_added_callback,
323 .vars_deleted = vars_deleted_callback,
324 .var_moved = var_moved_callback,
325 .var_changed = var_changed_callback,
326 .weight_changed = weight_changed_callback,
327 .filter_changed = filter_changed_callback,
328 .split_changed = split_changed_callback
332 psppire_dict_init (PsppireDict *d)
334 d->dispose_has_run = FALSE;
336 d->stamp = g_random_int ();
337 d->disable_insert_signal = FALSE;
341 * psppire_dict_new_from_dict:
342 * @returns: a new #PsppireDict object
344 * Creates a new #PsppireDict.
347 psppire_dict_new_from_dict (struct dictionary *d)
349 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
350 new_dict->dict = dict_ref (d);
352 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
359 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
361 const struct variable *var = dict_get_weight (d);
363 struct dictionary *old_dict = dict->dict;
365 guint old_n = dict_get_n_vars (dict->dict);
366 guint new_n = dict_get_n_vars (d);
368 dict->dict = dict_ref (d);
369 dict_unref (old_dict);
371 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
373 var = dict_get_filter (d);
374 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
376 split_changed_callback (d, dict);
378 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
380 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
384 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
385 Returns true if successful, false if SIZE is insufficient. */
387 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
395 /* TRANSLATORS: This string must be a valid variable name. That means:
396 - The string must be at most 64 bytes (not characters) long.
397 - The string may not contain whitespace.
398 - The first character may not be '$'
399 - The first character may not be a digit
400 - The final character may not be '.' or '_'
402 len = snprintf (name, size, _("Var%04d"), d);
406 if (psppire_dict_lookup_var (dict, name) == NULL)
413 /* Insert a new variable at posn IDX, with the name NAME, and return the
415 IDX may take the special value -1, which will be treated the same as
416 zero. If NAME is null, then a name will be automatically assigned.
419 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
421 struct variable *var;
424 if (idx == -1) /* Note bug #56392. */
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)
455 g_return_if_fail (d);
456 g_return_if_fail (d->dict);
457 g_return_if_fail (PSPPIRE_IS_DICT (d));
458 size_t varcnt = dict_get_n_vars (d->dict);
459 g_return_if_fail (first < varcnt);
460 g_return_if_fail (first >= 0);
461 g_return_if_fail (n > 0);
462 g_return_if_fail (first + n <= varcnt);
464 dict_delete_consecutive_vars (d->dict, first, n);
469 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
472 g_assert (PSPPIRE_IS_DICT (d));
474 if (! dict_id_is_valid (d->dict, name))
477 if (idx < dict_get_n_vars (d->dict))
479 /* This is an existing variable? */
480 struct variable * var = dict_get_var (d->dict, idx);
481 dict_rename_var (d->dict, var, name);
486 dict_create_var (d->dict, name, 0);
494 /* Return the IDXth variable.
495 Will return NULL if IDX exceeds the number of variables in the dictionary.
498 psppire_dict_get_variable (const PsppireDict *d, gint idx)
500 g_return_val_if_fail (d, NULL);
501 g_return_val_if_fail (d->dict, NULL);
503 if (dict_get_n_vars (d->dict) <= idx)
506 return dict_get_var (d->dict, idx);
510 /* Return the number of variables in the dictionary */
512 psppire_dict_get_n_vars (const PsppireDict *d)
514 g_return_val_if_fail (d, -1);
515 g_return_val_if_fail (d->dict, -1);
517 return dict_get_n_vars (d->dict);
521 /* Returns the prototype for the cases that match the dictionary */
522 const struct caseproto *
523 psppire_dict_get_proto (const PsppireDict *d)
525 g_return_val_if_fail (d, NULL);
526 g_return_val_if_fail (d->dict, NULL);
528 return dict_get_proto (d->dict);
532 /* Return a variable by name.
533 Return NULL if it doesn't exist
536 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
538 g_return_val_if_fail (d, NULL);
539 g_return_val_if_fail (d->dict, NULL);
541 return dict_lookup_var (d->dict, name);
544 /* Clears the contents of D */
546 psppire_dict_clear (PsppireDict *d)
548 g_return_if_fail (d);
549 g_return_if_fail (d->dict);
552 dict_clear (d->dict);
557 /* Return true if NAME would be a valid name of a variable to add to the
558 dictionary. False otherwise.
561 psppire_dict_check_name (const PsppireDict *dict,
564 return (dict_id_is_valid (dict->dict, name)
565 && !psppire_dict_lookup_var (dict, name));
568 /* Tree Model Stuff */
570 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
572 static gint tree_model_n_columns (GtkTreeModel *model);
574 static GType tree_model_column_type (GtkTreeModel *model, gint index);
576 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
579 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
581 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
584 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
585 gint column, GValue *value);
587 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
588 GtkTreeIter *parent, gint n);
590 static gint tree_model_n_children (GtkTreeModel *tree_model,
593 static gboolean tree_model_iter_children (GtkTreeModel *,
597 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
601 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
605 dictionary_tree_model_init (GtkTreeModelIface *iface)
607 iface->get_flags = tree_model_get_flags;
608 iface->get_n_columns = tree_model_n_columns;
609 iface->get_column_type = tree_model_column_type;
610 iface->get_iter = tree_model_get_iter;
611 iface->iter_next = tree_model_iter_next;
612 iface->get_path = tree_model_get_path;
613 iface->get_value = tree_model_get_value;
615 iface->iter_children = tree_model_iter_children ;
616 iface->iter_has_child = tree_model_iter_has_child ;
617 iface->iter_n_children = tree_model_n_children ;
618 iface->iter_nth_child = tree_model_nth_child ;
619 iface->iter_parent = tree_model_iter_parent ;
623 tree_model_iter_has_child (GtkTreeModel *tree_model,
630 tree_model_iter_parent (GtkTreeModel *tree_model,
637 static GtkTreeModelFlags
638 tree_model_get_flags (GtkTreeModel *model)
640 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
642 return GTK_TREE_MODEL_LIST_ONLY;
647 tree_model_n_columns (GtkTreeModel *model)
653 tree_model_column_type (GtkTreeModel *model, gint index)
655 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
661 case DICT_TVM_COL_NAME:
662 case DICT_TVM_COL_LABEL:
665 case DICT_TVM_COL_DECIMAL:
666 case DICT_TVM_COL_WIDTH:
667 case DICT_TVM_COL_COLUMNS:
670 case DICT_TVM_COL_VAR:
671 t = PSPPIRE_VAR_PTR_TYPE;
673 case DICT_TVM_COL_ALIGNMENT:
676 case DICT_TVM_COL_MEASURE:
677 t = measure_enum_type;
679 case DICT_TVM_COL_ROLE:
688 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
690 gint *indices, depth;
692 struct variable *var;
694 PsppireDict *dict = PSPPIRE_DICT (model);
696 g_return_val_if_fail (path, FALSE);
698 indices = gtk_tree_path_get_indices (path);
699 depth = gtk_tree_path_get_depth (path);
701 g_return_val_if_fail (depth == 1, FALSE);
705 if (n < 0 || n >= psppire_dict_get_n_vars (dict))
708 iter->user_data = NULL;
712 var = psppire_dict_get_variable (dict, n);
714 g_assert (var_get_dict_index (var) == n);
716 iter->stamp = dict->stamp;
717 iter->user_data = var;
724 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
726 PsppireDict *dict = PSPPIRE_DICT (model);
727 struct variable *var;
730 if (iter == NULL || iter->user_data == NULL)
733 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
735 var = iter->user_data;
737 idx = var_get_dict_index (var);
739 if (idx + 1 >= psppire_dict_get_n_vars (dict))
741 iter->user_data = NULL;
746 var = psppire_dict_get_variable (dict, idx + 1);
748 g_assert (var_get_dict_index (var) == idx + 1);
750 iter->user_data = var;
756 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
759 struct variable *var;
760 PsppireDict *dict = PSPPIRE_DICT (model);
762 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
764 var = iter->user_data;
766 path = gtk_tree_path_new ();
767 gtk_tree_path_append_index (path, var_get_dict_index (var));
772 struct fmt_spec var_get_write_format (const struct variable *);
775 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
776 gint column, GValue *value)
778 struct variable *var;
779 PsppireDict *dict = PSPPIRE_DICT (model);
781 g_return_if_fail (iter->stamp == dict->stamp);
783 var = iter->user_data;
785 struct fmt_spec fs = var_get_write_format (var);
789 case DICT_TVM_COL_NAME:
790 g_value_init (value, G_TYPE_STRING);
791 g_value_set_string (value, var_get_name (var));
793 case DICT_TVM_COL_WIDTH:
794 g_value_init (value, G_TYPE_INT);
795 g_value_set_int (value, fs.w);
797 case DICT_TVM_COL_DECIMAL:
798 g_value_init (value, G_TYPE_INT);
799 g_value_set_int (value, fs.d);
801 case DICT_TVM_COL_LABEL:
802 g_value_init (value, G_TYPE_STRING);
803 g_value_set_string (value, var_get_label (var));
805 case DICT_TVM_COL_COLUMNS:
806 g_value_init (value, G_TYPE_INT);
807 g_value_set_int (value, var_get_display_width (var));
809 case DICT_TVM_COL_ALIGNMENT:
810 g_value_init (value, align_enum_type);
811 g_value_set_enum (value, var_get_alignment (var));
813 case DICT_TVM_COL_MEASURE:
814 g_value_init (value, measure_enum_type);
815 g_value_set_enum (value, var_get_measure (var));
817 case DICT_TVM_COL_ROLE:
818 g_value_init (value, role_enum_type);
819 g_value_set_enum (value, var_get_role (var));
821 case DICT_TVM_COL_VAR:
822 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
823 g_value_set_boxed (value, var);
826 g_value_init (value, G_TYPE_STRING);
827 g_value_set_string (value, "????");
833 tree_model_iter_children (GtkTreeModel *tree_model,
841 tree_model_n_children (GtkTreeModel *model,
844 PsppireDict *dict = PSPPIRE_DICT (model);
847 return psppire_dict_get_n_vars (dict);
853 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
854 GtkTreeIter *parent, gint n)
858 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
860 dict = PSPPIRE_DICT (model);
865 if (n >= psppire_dict_get_n_vars (dict))
868 iter->stamp = dict->stamp;
869 iter->user_data = psppire_dict_get_variable (dict, n);
871 if (!iter->user_data)
879 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
882 if (! dict_id_is_valid (dict->dict, name))
885 /* Make sure no other variable has this name */
886 if (NULL != psppire_dict_lookup_var (dict, name))
889 dict_rename_var (dict->dict, v, name);
896 psppire_dict_get_weight_variable (const PsppireDict *dict)
898 return dict_get_weight (dict->dict);
902 psppire_dict_encoding (const PsppireDict *dict)
904 return dict_get_encoding (dict->dict);