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 [VARIABLES_DELETED] =
195 g_signal_new ("variables-deleted",
196 G_TYPE_FROM_CLASS (class),
200 psppire_marshal_VOID__INT_UINT,
206 signals [WEIGHT_CHANGED] =
207 g_signal_new ("weight-changed",
208 G_TYPE_FROM_CLASS (class),
212 g_cclosure_marshal_VOID__INT,
217 signals [FILTER_CHANGED] =
218 g_signal_new ("filter-changed",
219 G_TYPE_FROM_CLASS (class),
223 g_cclosure_marshal_VOID__INT,
228 signals [SPLIT_CHANGED] =
229 g_signal_new ("split-changed",
230 G_TYPE_FROM_CLASS (class),
234 g_cclosure_marshal_VOID__VOID,
240 psppire_dict_dispose (GObject *object)
242 PsppireDict *d = PSPPIRE_DICT (object);
244 if (!d->dispose_has_run)
247 d->dispose_has_run = TRUE;
249 dict_set_callbacks (d->dict, NULL, NULL);
250 dict_unref (d->dict);
252 G_OBJECT_CLASS (parent_class)->dispose (object);
255 /* Pass on callbacks from src/data/dictionary, as
256 signals in the Gtk library */
258 addcb (struct dictionary *d, int idx, void *pd)
260 PsppireDict *dict = PSPPIRE_DICT (pd);
262 if (! dict->disable_insert_signal)
264 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
265 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
270 delcb (struct dictionary *d, int dict_idx, unsigned int n, void *pd)
272 g_signal_emit (pd, signals [VARIABLES_DELETED], 0, dict_idx, n);
273 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
277 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
279 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
280 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
284 weight_changed_callback (struct dictionary *d, int idx, void *pd)
286 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
290 filter_changed_callback (struct dictionary *d, int idx, void *pd)
292 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
296 split_changed_callback (struct dictionary *d, void *pd)
298 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
301 static const struct dict_callbacks gui_callbacks =
306 weight_changed_callback,
307 filter_changed_callback,
308 split_changed_callback
312 psppire_dict_init (PsppireDict *d)
314 d->dispose_has_run = FALSE;
316 d->stamp = g_random_int ();
317 d->disable_insert_signal = FALSE;
321 * psppire_dict_new_from_dict:
322 * @returns: a new #PsppireDict object
324 * Creates a new #PsppireDict.
327 psppire_dict_new_from_dict (struct dictionary *d)
329 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
330 new_dict->dict = dict_ref (d);
332 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
339 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
341 const struct variable *var = dict_get_weight (d);
343 struct dictionary *old_dict = dict->dict;
345 guint old_n = dict_get_n_vars (dict->dict);
346 guint new_n = dict_get_n_vars (d);
348 dict->dict = dict_ref (d);
349 dict_unref (old_dict);
351 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
353 var = dict_get_filter (d);
354 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
356 split_changed_callback (d, dict);
358 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
360 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
364 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
365 Returns true if successful, false if SIZE is insufficient. */
367 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
375 /* TRANSLATORS: This string must be a valid variable name. That means:
376 - The string must be at most 64 bytes (not characters) long.
377 - The string may not contain whitespace.
378 - The first character may not be '$'
379 - The first character may not be a digit
380 - The final character may not be '.' or '_'
382 len = snprintf (name, size, _("Var%04d"), d);
386 if (psppire_dict_lookup_var (dict, name) == NULL)
393 /* Insert a new variable at posn IDX, with the name NAME, and return the
395 IDX may take the special value -1, which will be treated the same as
396 zero. If NAME is null, then a name will be automatically assigned.
399 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
401 struct variable *var;
404 if (idx == -1) /* Note bug #56392. */
406 g_return_val_if_fail (d, NULL);
407 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
411 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
412 g_return_val_if_reached (NULL);
417 d->disable_insert_signal = TRUE;
419 var = dict_create_var (d->dict, name, 0);
421 dict_reorder_var (d->dict, var, idx);
423 d->disable_insert_signal = FALSE;
425 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
426 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
431 /* Delete N variables beginning at FIRST */
433 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
435 g_return_if_fail (d);
436 g_return_if_fail (d->dict);
437 g_return_if_fail (PSPPIRE_IS_DICT (d));
438 size_t varcnt = dict_get_n_vars (d->dict);
439 g_return_if_fail (first < varcnt);
440 g_return_if_fail (first >= 0);
441 g_return_if_fail (n > 0);
442 g_return_if_fail (first + n <= varcnt);
444 dict_delete_consecutive_vars (d->dict, first, n);
449 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
452 g_assert (PSPPIRE_IS_DICT (d));
454 if (! dict_id_is_valid (d->dict, name))
457 if (idx < dict_get_n_vars (d->dict))
459 /* This is an existing variable? */
460 struct variable * var = dict_get_var (d->dict, idx);
461 dict_rename_var (d->dict, var, name);
466 dict_create_var (d->dict, name, 0);
474 /* Return the IDXth variable.
475 Will return NULL if IDX exceeds the number of variables in the dictionary.
478 psppire_dict_get_variable (const PsppireDict *d, gint idx)
480 g_return_val_if_fail (d, NULL);
481 g_return_val_if_fail (d->dict, NULL);
483 if (dict_get_n_vars (d->dict) <= idx)
486 return dict_get_var (d->dict, idx);
490 /* Return the number of variables in the dictionary */
492 psppire_dict_get_n_vars (const PsppireDict *d)
494 g_return_val_if_fail (d, -1);
495 g_return_val_if_fail (d->dict, -1);
497 return dict_get_n_vars (d->dict);
501 /* Returns the prototype for the cases that match the dictionary */
502 const struct caseproto *
503 psppire_dict_get_proto (const PsppireDict *d)
505 g_return_val_if_fail (d, NULL);
506 g_return_val_if_fail (d->dict, NULL);
508 return dict_get_proto (d->dict);
512 /* Return a variable by name.
513 Return NULL if it doesn't exist
516 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
518 g_return_val_if_fail (d, NULL);
519 g_return_val_if_fail (d->dict, NULL);
521 return dict_lookup_var (d->dict, name);
524 /* Clears the contents of D */
526 psppire_dict_clear (PsppireDict *d)
528 g_return_if_fail (d);
529 g_return_if_fail (d->dict);
532 dict_clear (d->dict);
537 /* Return true if NAME would be a valid name of a variable to add to the
538 dictionary. False otherwise.
541 psppire_dict_check_name (const PsppireDict *dict,
544 return (dict_id_is_valid (dict->dict, name)
545 && !psppire_dict_lookup_var (dict, name));
548 /* Tree Model Stuff */
550 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
552 static gint tree_model_n_columns (GtkTreeModel *model);
554 static GType tree_model_column_type (GtkTreeModel *model, gint index);
556 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
559 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
561 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
564 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
565 gint column, GValue *value);
567 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
568 GtkTreeIter *parent, gint n);
570 static gint tree_model_n_children (GtkTreeModel *tree_model,
573 static gboolean tree_model_iter_children (GtkTreeModel *,
577 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
581 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
585 dictionary_tree_model_init (GtkTreeModelIface *iface)
587 iface->get_flags = tree_model_get_flags;
588 iface->get_n_columns = tree_model_n_columns;
589 iface->get_column_type = tree_model_column_type;
590 iface->get_iter = tree_model_get_iter;
591 iface->iter_next = tree_model_iter_next;
592 iface->get_path = tree_model_get_path;
593 iface->get_value = tree_model_get_value;
595 iface->iter_children = tree_model_iter_children ;
596 iface->iter_has_child = tree_model_iter_has_child ;
597 iface->iter_n_children = tree_model_n_children ;
598 iface->iter_nth_child = tree_model_nth_child ;
599 iface->iter_parent = tree_model_iter_parent ;
603 tree_model_iter_has_child (GtkTreeModel *tree_model,
610 tree_model_iter_parent (GtkTreeModel *tree_model,
617 static GtkTreeModelFlags
618 tree_model_get_flags (GtkTreeModel *model)
620 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
622 return GTK_TREE_MODEL_LIST_ONLY;
627 tree_model_n_columns (GtkTreeModel *model)
633 tree_model_column_type (GtkTreeModel *model, gint index)
635 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
641 case DICT_TVM_COL_NAME:
642 case DICT_TVM_COL_LABEL:
645 case DICT_TVM_COL_DECIMAL:
646 case DICT_TVM_COL_WIDTH:
647 case DICT_TVM_COL_COLUMNS:
650 case DICT_TVM_COL_VAR:
651 t = PSPPIRE_VAR_PTR_TYPE;
653 case DICT_TVM_COL_ALIGNMENT:
656 case DICT_TVM_COL_MEASURE:
657 t = measure_enum_type;
659 case DICT_TVM_COL_ROLE:
668 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
670 gint *indices, depth;
672 struct variable *var;
674 PsppireDict *dict = PSPPIRE_DICT (model);
676 g_return_val_if_fail (path, FALSE);
678 indices = gtk_tree_path_get_indices (path);
679 depth = gtk_tree_path_get_depth (path);
681 g_return_val_if_fail (depth == 1, FALSE);
685 if (n < 0 || n >= psppire_dict_get_n_vars (dict))
688 iter->user_data = NULL;
692 var = psppire_dict_get_variable (dict, n);
694 g_assert (var_get_dict_index (var) == n);
696 iter->stamp = dict->stamp;
697 iter->user_data = var;
704 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
706 PsppireDict *dict = PSPPIRE_DICT (model);
707 struct variable *var;
710 if (iter == NULL || iter->user_data == NULL)
713 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
715 var = iter->user_data;
717 idx = var_get_dict_index (var);
719 if (idx + 1 >= psppire_dict_get_n_vars (dict))
721 iter->user_data = NULL;
726 var = psppire_dict_get_variable (dict, idx + 1);
728 g_assert (var_get_dict_index (var) == idx + 1);
730 iter->user_data = var;
736 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
739 struct variable *var;
740 PsppireDict *dict = PSPPIRE_DICT (model);
742 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
744 var = iter->user_data;
746 path = gtk_tree_path_new ();
747 gtk_tree_path_append_index (path, var_get_dict_index (var));
752 struct fmt_spec var_get_write_format (const struct variable *);
755 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
756 gint column, GValue *value)
758 struct variable *var;
759 PsppireDict *dict = PSPPIRE_DICT (model);
761 g_return_if_fail (iter->stamp == dict->stamp);
763 var = iter->user_data;
765 struct fmt_spec fs = var_get_write_format (var);
769 case DICT_TVM_COL_NAME:
770 g_value_init (value, G_TYPE_STRING);
771 g_value_set_string (value, var_get_name (var));
773 case DICT_TVM_COL_WIDTH:
774 g_value_init (value, G_TYPE_INT);
775 g_value_set_int (value, fs.w);
777 case DICT_TVM_COL_DECIMAL:
778 g_value_init (value, G_TYPE_INT);
779 g_value_set_int (value, fs.d);
781 case DICT_TVM_COL_LABEL:
782 g_value_init (value, G_TYPE_STRING);
783 g_value_set_string (value, var_get_label (var));
785 case DICT_TVM_COL_COLUMNS:
786 g_value_init (value, G_TYPE_INT);
787 g_value_set_int (value, var_get_display_width (var));
789 case DICT_TVM_COL_ALIGNMENT:
790 g_value_init (value, align_enum_type);
791 g_value_set_enum (value, var_get_alignment (var));
793 case DICT_TVM_COL_MEASURE:
794 g_value_init (value, measure_enum_type);
795 g_value_set_enum (value, var_get_measure (var));
797 case DICT_TVM_COL_ROLE:
798 g_value_init (value, role_enum_type);
799 g_value_set_enum (value, var_get_role (var));
801 case DICT_TVM_COL_VAR:
802 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
803 g_value_set_boxed (value, var);
806 g_value_init (value, G_TYPE_STRING);
807 g_value_set_string (value, "????");
813 tree_model_iter_children (GtkTreeModel *tree_model,
821 tree_model_n_children (GtkTreeModel *model,
824 PsppireDict *dict = PSPPIRE_DICT (model);
827 return psppire_dict_get_n_vars (dict);
833 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
834 GtkTreeIter *parent, gint n)
838 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
840 dict = PSPPIRE_DICT (model);
845 if (n >= psppire_dict_get_n_vars (dict))
848 iter->stamp = dict->stamp;
849 iter->user_data = psppire_dict_get_variable (dict, n);
851 if (!iter->user_data)
859 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
862 if (! dict_id_is_valid (dict->dict, name))
865 /* Make sure no other variable has this name */
866 if (NULL != psppire_dict_lookup_var (dict, name))
869 dict_rename_var (dict->dict, v, name);
876 psppire_dict_get_weight_variable (const PsppireDict *dict)
878 return dict_get_weight (dict->dict);
882 psppire_dict_encoding (const PsppireDict *dict)
884 return dict_get_encoding (dict->dict);