1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "ui/gui/psppire-dict.h"
25 #include "data/dictionary.h"
26 #include "data/identifier.h"
27 #include "data/missing-values.h"
28 #include "data/value-labels.h"
29 #include "data/variable.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "ui/gui/helper.h"
33 #include "ui/gui/psppire-marshal.h"
34 #include "ui/gui/psppire-var-ptr.h"
36 #include "ui/gui/efficient-sheet/jmd-datum.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
57 /* --- prototypes --- */
58 static void psppire_dict_class_init (PsppireDictClass *class);
59 static void psppire_dict_init (PsppireDict *dict);
60 static void psppire_dict_dispose (GObject *object);
62 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
67 gni (GListModel *list)
69 PsppireDict *dict = PSPPIRE_DICT (list);
71 return psppire_dict_get_var_cnt (dict);
75 git (GListModel *list)
77 return JMD_TYPE_DATUM;
81 gi (GListModel *list, guint id)
83 JmdDatum *gd = JMD_DATUM (g_object_new (JMD_TYPE_DATUM, NULL));
85 PsppireDict *dict = PSPPIRE_DICT (list);
87 if (id >= psppire_dict_get_var_cnt (dict))
89 gd->text = g_strdup (_("Var"));
93 const struct variable *v = psppire_dict_get_variable (dict, id);
95 gd->text = g_strdup (var_get_name (v));
96 gd->label = g_strdup (var_get_label (v));
104 jmd_init_iface (GListModelInterface *iface)
106 iface->get_n_items = gni;
107 iface->get_item = gi;
108 iface->get_item_type = git;
112 /* --- variables --- */
113 static GObjectClass *parent_class = NULL;
115 static guint signals [n_SIGNALS];
117 /* --- functions --- */
119 * psppire_dict_get_type:
120 * @returns: the type ID for accelerator groups.
123 psppire_dict_get_type (void)
125 static GType object_type = 0;
129 static const GTypeInfo object_info = {
130 sizeof (PsppireDictClass),
131 (GBaseInitFunc) NULL,
132 (GBaseFinalizeFunc) NULL,
133 (GClassInitFunc) psppire_dict_class_init,
134 NULL, /* class_finalize */
135 NULL, /* class_data */
136 sizeof (PsppireDict),
138 (GInstanceInitFunc) psppire_dict_init,
141 static const GInterfaceInfo tree_model_info = {
142 (GInterfaceInitFunc) dictionary_tree_model_init,
147 static const GInterfaceInfo list_model_info = {
148 (GInterfaceInitFunc) jmd_init_iface,
153 object_type = g_type_register_static (G_TYPE_OBJECT,
157 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
160 g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
169 psppire_dict_class_init (PsppireDictClass *class)
171 GObjectClass *object_class = G_OBJECT_CLASS (class);
173 parent_class = g_type_class_peek_parent (class);
175 object_class->dispose = psppire_dict_dispose;
177 signals [ITEMS_CHANGED] =
178 g_signal_new ("changed",
179 G_TYPE_FROM_CLASS (class),
183 psppire_marshal_VOID__UINT_UINT_UINT,
191 signals [VARIABLE_CHANGED] =
192 g_signal_new ("variable-changed",
193 G_TYPE_FROM_CLASS (class),
197 psppire_marshal_VOID__INT_UINT_POINTER,
207 signals [VARIABLE_INSERTED] =
208 g_signal_new ("variable-inserted",
209 G_TYPE_FROM_CLASS (class),
213 g_cclosure_marshal_VOID__INT,
219 signals [VARIABLE_DELETED] =
220 g_signal_new ("variable-deleted",
221 G_TYPE_FROM_CLASS (class),
225 psppire_marshal_VOID__POINTER_INT_INT,
233 signals [WEIGHT_CHANGED] =
234 g_signal_new ("weight-changed",
235 G_TYPE_FROM_CLASS (class),
239 g_cclosure_marshal_VOID__INT,
245 signals [FILTER_CHANGED] =
246 g_signal_new ("filter-changed",
247 G_TYPE_FROM_CLASS (class),
251 g_cclosure_marshal_VOID__INT,
257 signals [SPLIT_CHANGED] =
258 g_signal_new ("split-changed",
259 G_TYPE_FROM_CLASS (class),
263 g_cclosure_marshal_VOID__VOID,
269 psppire_dict_dispose (GObject *object)
271 PsppireDict *d = PSPPIRE_DICT (object);
273 dict_set_callbacks (d->dict, NULL, NULL);
275 G_OBJECT_CLASS (parent_class)->dispose (object);
278 /* Pass on callbacks from src/data/dictionary, as
279 signals in the Gtk library */
281 addcb (struct dictionary *d, int idx, void *pd)
283 PsppireDict *dict = PSPPIRE_DICT (pd);
285 if ( ! dict->disable_insert_signal)
287 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
288 g_signal_emit (dict, signals [ITEMS_CHANGED], 0, idx, 1, 1);
293 delcb (struct dictionary *d, const struct variable *var,
294 int dict_idx, int case_idx, void *pd)
296 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
297 var, dict_idx, case_idx);
298 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, dict_idx, 1, 0);
302 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
304 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
305 g_signal_emit (pd, signals [ITEMS_CHANGED], 0, idx, 1, 1);
309 weight_changed_callback (struct dictionary *d, int idx, void *pd)
311 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
315 filter_changed_callback (struct dictionary *d, int idx, void *pd)
317 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
321 split_changed_callback (struct dictionary *d, void *pd)
323 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
326 static const struct dict_callbacks gui_callbacks =
331 weight_changed_callback,
332 filter_changed_callback,
333 split_changed_callback
337 psppire_dict_init (PsppireDict *psppire_dict)
339 psppire_dict->stamp = g_random_int ();
340 psppire_dict->disable_insert_signal = FALSE;
344 * psppire_dict_new_from_dict:
345 * @returns: a new #PsppireDict object
347 * Creates a new #PsppireDict.
350 psppire_dict_new_from_dict (struct dictionary *d)
352 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
355 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
362 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
364 struct variable *var = dict_get_weight (d);
366 guint old_n = dict_get_var_cnt (dict->dict);
367 guint new_n = dict_get_var_cnt (d);
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 (dict, signals [ITEMS_CHANGED], 0, 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 charactor 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 If NAME is null, then a name will be automatically assigned.
418 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
420 struct variable *var;
423 g_return_val_if_fail (idx >= 0, NULL);
424 g_return_val_if_fail (d, NULL);
425 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
429 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
430 g_return_val_if_reached (NULL);
435 d->disable_insert_signal = TRUE;
437 var = dict_create_var (d->dict, name, 0);
439 dict_reorder_var (d->dict, var, idx);
441 d->disable_insert_signal = FALSE;
443 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
444 g_signal_emit (d, signals [ITEMS_CHANGED], 0, idx, 0, 1);
449 /* Delete N variables beginning at FIRST */
451 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
454 g_return_if_fail (d);
455 g_return_if_fail (d->dict);
456 g_return_if_fail (PSPPIRE_IS_DICT (d));
458 for (idx = 0 ; idx < n ; ++idx )
460 struct variable *var;
462 /* Do nothing if it's out of bounds */
463 if ( first >= dict_get_var_cnt (d->dict))
466 var = dict_get_var (d->dict, first);
467 dict_delete_var (d->dict, var);
473 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
475 struct variable *var;
477 g_assert (PSPPIRE_IS_DICT (d));
479 if ( ! dict_id_is_valid (d->dict, name, false))
482 if ( idx < dict_get_var_cnt (d->dict))
484 /* This is an existing variable? */
485 var = dict_get_var (d->dict, idx);
486 dict_rename_var (d->dict, var, name);
491 dict_create_var (d->dict, name, 0);
499 /* Return the IDXth variable.
500 Will return NULL if IDX exceeds the number of variables in the dictionary.
503 psppire_dict_get_variable (const PsppireDict *d, gint idx)
505 g_return_val_if_fail (d, NULL);
506 g_return_val_if_fail (d->dict, NULL);
508 if ( dict_get_var_cnt (d->dict) <= idx )
511 return dict_get_var (d->dict, idx);
515 /* Return the number of variables in the dictionary */
517 psppire_dict_get_var_cnt (const PsppireDict *d)
519 g_return_val_if_fail (d, -1);
520 g_return_val_if_fail (d->dict, -1);
522 return dict_get_var_cnt (d->dict);
526 /* Return the number of `union value's in the dictionary */
528 psppire_dict_get_value_cnt (const PsppireDict *d)
530 g_return_val_if_fail (d, -1);
531 g_return_val_if_fail (d->dict, -1);
533 return dict_get_next_value_idx (d->dict);
537 /* Returns the prototype for the cases that match the dictionary */
538 const struct caseproto *
539 psppire_dict_get_proto (const PsppireDict *d)
541 g_return_val_if_fail (d, NULL);
542 g_return_val_if_fail (d->dict, NULL);
544 return dict_get_proto (d->dict);
548 /* Return a variable by name.
549 Return NULL if it doesn't exist
552 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
554 g_return_val_if_fail (d, NULL);
555 g_return_val_if_fail (d->dict, NULL);
557 return dict_lookup_var (d->dict, name);
560 /* Clears the contents of D */
562 psppire_dict_clear (PsppireDict *d)
564 g_return_if_fail (d);
565 g_return_if_fail (d->dict);
568 dict_clear (d->dict);
573 /* Return true is NAME would be a valid name of a variable to add to the
574 dictionary. False otherwise.
575 If REPORT is true, then invalid names will be reported as such as errors
578 psppire_dict_check_name (const PsppireDict *dict,
579 const gchar *name, gboolean report)
581 if ( ! dict_id_is_valid (dict->dict, name, report ) )
584 if (psppire_dict_lookup_var (dict, name))
587 msg (ME, _("Duplicate variable name."));
596 psppire_dict_get_next_value_idx (const PsppireDict *dict)
598 return dict_get_next_value_idx (dict->dict);
602 /* Tree Model Stuff */
604 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
606 static gint tree_model_n_columns (GtkTreeModel *model);
608 static GType tree_model_column_type (GtkTreeModel *model, gint index);
610 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
613 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
615 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
618 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
619 gint column, GValue *value);
621 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
622 GtkTreeIter *parent, gint n);
624 static gint tree_model_n_children (GtkTreeModel *tree_model,
627 static gboolean tree_model_iter_children (GtkTreeModel *,
631 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
635 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
639 dictionary_tree_model_init (GtkTreeModelIface *iface)
641 iface->get_flags = tree_model_get_flags;
642 iface->get_n_columns = tree_model_n_columns;
643 iface->get_column_type = tree_model_column_type;
644 iface->get_iter = tree_model_get_iter;
645 iface->iter_next = tree_model_iter_next;
646 iface->get_path = tree_model_get_path;
647 iface->get_value = tree_model_get_value;
649 iface->iter_children = tree_model_iter_children ;
650 iface->iter_has_child = tree_model_iter_has_child ;
651 iface->iter_n_children = tree_model_n_children ;
652 iface->iter_nth_child = tree_model_nth_child ;
653 iface->iter_parent = tree_model_iter_parent ;
657 tree_model_iter_has_child (GtkTreeModel *tree_model,
664 tree_model_iter_parent (GtkTreeModel *tree_model,
671 static GtkTreeModelFlags
672 tree_model_get_flags (GtkTreeModel *model)
674 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
676 return GTK_TREE_MODEL_LIST_ONLY;
681 tree_model_n_columns (GtkTreeModel *model)
687 tree_model_column_type (GtkTreeModel *model, gint index)
689 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
693 case DICT_TVM_COL_NAME:
694 return G_TYPE_STRING;
696 case DICT_TVM_COL_VAR:
697 return PSPPIRE_VAR_PTR_TYPE;
699 case DICT_TVM_COL_LABEL:
700 return G_TYPE_STRING;
708 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
710 gint *indices, depth;
712 struct variable *var;
714 PsppireDict *dict = PSPPIRE_DICT (model);
716 g_return_val_if_fail (path, FALSE);
718 indices = gtk_tree_path_get_indices (path);
719 depth = gtk_tree_path_get_depth (path);
721 g_return_val_if_fail (depth == 1, FALSE);
725 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
728 iter->user_data = NULL;
732 var = psppire_dict_get_variable (dict, n);
734 g_assert (var_get_dict_index (var) == n);
736 iter->stamp = dict->stamp;
737 iter->user_data = var;
744 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
746 PsppireDict *dict = PSPPIRE_DICT (model);
747 struct variable *var;
750 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
752 if ( iter == NULL || iter->user_data == NULL)
755 var = iter->user_data;
757 idx = var_get_dict_index (var);
759 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
761 iter->user_data = NULL;
766 var = psppire_dict_get_variable (dict, idx + 1);
768 g_assert (var_get_dict_index (var) == idx + 1);
770 iter->user_data = var;
776 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
779 struct variable *var;
780 PsppireDict *dict = PSPPIRE_DICT (model);
782 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
784 var = iter->user_data;
786 path = gtk_tree_path_new ();
787 gtk_tree_path_append_index (path, var_get_dict_index (var));
792 const struct fmt_spec *var_get_write_format (const struct variable *);
795 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
796 gint column, GValue *value)
798 struct variable *var;
799 PsppireDict *dict = PSPPIRE_DICT (model);
801 g_return_if_fail (iter->stamp == dict->stamp);
803 var = iter->user_data;
805 const struct fmt_spec *fs = var_get_write_format (var);
809 case DICT_TVM_COL_NAME:
810 g_value_init (value, G_TYPE_STRING);
811 g_value_set_string (value, var_get_name (var));
813 case DICT_TVM_COL_WIDTH:
814 g_value_init (value, G_TYPE_INT);
815 g_value_set_int (value, fs->w);
817 case DICT_TVM_COL_DECIMAL:
818 g_value_init (value, G_TYPE_INT);
819 g_value_set_int (value, fs->d);
821 case DICT_TVM_COL_LABEL:
822 g_value_init (value, G_TYPE_STRING);
823 g_value_set_string (value, var_get_label (var));
825 case DICT_TVM_COL_COLUMNS:
826 g_value_init (value, G_TYPE_INT);
827 g_value_set_int (value, var_get_display_width (var));
829 case DICT_TVM_COL_ALIGNMENT:
830 g_value_init (value, G_TYPE_INT);
831 g_value_set_int (value, var_get_alignment (var));
833 case DICT_TVM_COL_MEASURE:
834 g_value_init (value, G_TYPE_INT);
835 g_value_set_int (value, var_get_measure (var));
837 case DICT_TVM_COL_ROLE:
838 g_value_init (value, G_TYPE_INT);
839 g_value_set_int (value, var_get_role (var));
841 case DICT_TVM_COL_VAR:
842 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
843 g_value_set_boxed (value, var);
846 g_value_init (value, G_TYPE_STRING);
847 g_value_set_string (value, "????");
853 tree_model_iter_children (GtkTreeModel *tree_model,
861 tree_model_n_children (GtkTreeModel *model,
864 PsppireDict *dict = PSPPIRE_DICT (model);
867 return psppire_dict_get_var_cnt (dict);
873 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
874 GtkTreeIter *parent, gint n)
878 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
880 dict = PSPPIRE_DICT (model);
885 if ( n >= psppire_dict_get_var_cnt (dict) )
888 iter->stamp = dict->stamp;
889 iter->user_data = psppire_dict_get_variable (dict, n);
891 if ( !iter->user_data)
899 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
902 if ( ! dict_id_is_valid (dict->dict, name, false))
905 /* Make sure no other variable has this name */
906 if ( NULL != psppire_dict_lookup_var (dict, name))
909 dict_rename_var (dict->dict, v, name);
916 psppire_dict_get_weight_variable (const PsppireDict *dict)
918 return dict_get_weight (dict->dict);
925 psppire_dict_dump (const PsppireDict *dict)
928 const struct dictionary *d = dict->dict;
930 for (i = 0; i < dict_get_var_cnt (d); ++i)
932 const struct variable *v = psppire_dict_get_variable (dict, i);
933 int di = var_get_dict_index (v);
934 g_print ("`%s' idx=%d, fv=%d\n",
937 var_get_case_index(v));
947 psppire_dict_encoding (const PsppireDict *dict)
949 return dict_get_encoding (dict->dict);