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 [BACKEND_CHANGED] =
178 g_signal_new ("backend-changed",
179 G_TYPE_FROM_CLASS (class),
183 g_cclosure_marshal_VOID__VOID,
188 signals [VARIABLE_CHANGED] =
189 g_signal_new ("variable-changed",
190 G_TYPE_FROM_CLASS (class),
194 psppire_marshal_VOID__INT_UINT_POINTER,
204 signals [VARIABLE_INSERTED] =
205 g_signal_new ("variable-inserted",
206 G_TYPE_FROM_CLASS (class),
210 g_cclosure_marshal_VOID__INT,
216 signals [VARIABLE_DELETED] =
217 g_signal_new ("variable-deleted",
218 G_TYPE_FROM_CLASS (class),
222 psppire_marshal_VOID__POINTER_INT_INT,
230 signals [WEIGHT_CHANGED] =
231 g_signal_new ("weight-changed",
232 G_TYPE_FROM_CLASS (class),
236 g_cclosure_marshal_VOID__INT,
242 signals [FILTER_CHANGED] =
243 g_signal_new ("filter-changed",
244 G_TYPE_FROM_CLASS (class),
248 g_cclosure_marshal_VOID__INT,
254 signals [SPLIT_CHANGED] =
255 g_signal_new ("split-changed",
256 G_TYPE_FROM_CLASS (class),
260 g_cclosure_marshal_VOID__VOID,
266 psppire_dict_dispose (GObject *object)
268 PsppireDict *d = PSPPIRE_DICT (object);
270 dict_set_callbacks (d->dict, NULL, NULL);
272 G_OBJECT_CLASS (parent_class)->dispose (object);
275 /* Pass on callbacks from src/data/dictionary, as
276 signals in the Gtk library */
278 addcb (struct dictionary *d, int idx, void *pd)
280 PsppireDict *dict = PSPPIRE_DICT (pd);
282 if ( ! dict->disable_insert_signal)
283 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
287 delcb (struct dictionary *d, const struct variable *var,
288 int dict_idx, int case_idx, void *pd)
290 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
291 var, dict_idx, case_idx);
295 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
297 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
301 weight_changed_callback (struct dictionary *d, int idx, void *pd)
303 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
307 filter_changed_callback (struct dictionary *d, int idx, void *pd)
309 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
313 split_changed_callback (struct dictionary *d, void *pd)
315 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
318 static const struct dict_callbacks gui_callbacks =
323 weight_changed_callback,
324 filter_changed_callback,
325 split_changed_callback
329 psppire_dict_init (PsppireDict *psppire_dict)
331 psppire_dict->stamp = g_random_int ();
332 psppire_dict->disable_insert_signal = FALSE;
336 * psppire_dict_new_from_dict:
337 * @returns: a new #PsppireDict object
339 * Creates a new #PsppireDict.
342 psppire_dict_new_from_dict (struct dictionary *d)
344 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
347 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
354 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
356 struct variable *var = dict_get_weight (d);
360 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
362 var = dict_get_filter (d);
363 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
365 split_changed_callback (d, dict);
367 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
369 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
373 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
374 Returns true if successful, false if SIZE is insufficient. */
376 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
384 /* TRANSLATORS: This string must be a valid variable name. That means:
385 - The string must be at most 64 bytes (not characters) long.
386 - The string may not contain whitespace.
387 - The first character may not be '$'
388 - The first character may not be a digit
389 - The final charactor may not be '.' or '_'
391 len = snprintf (name, size, _("Var%04d"), d);
395 if (psppire_dict_lookup_var (dict, name) == NULL)
402 /* Insert a new variable at posn IDX, with the name NAME, and return the
404 If NAME is null, then a name will be automatically assigned.
407 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
409 struct variable *var;
412 g_return_val_if_fail (idx >= 0, NULL);
413 g_return_val_if_fail (d, NULL);
414 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
418 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
419 g_return_val_if_reached (NULL);
424 d->disable_insert_signal = TRUE;
426 var = dict_create_var (d->dict, name, 0);
428 dict_reorder_var (d->dict, var, idx);
430 d->disable_insert_signal = FALSE;
432 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
437 /* Delete N variables beginning at FIRST */
439 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
442 g_return_if_fail (d);
443 g_return_if_fail (d->dict);
444 g_return_if_fail (PSPPIRE_IS_DICT (d));
446 for (idx = 0 ; idx < n ; ++idx )
448 struct variable *var;
450 /* Do nothing if it's out of bounds */
451 if ( first >= dict_get_var_cnt (d->dict))
454 var = dict_get_var (d->dict, first);
455 dict_delete_var (d->dict, var);
461 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
463 struct variable *var;
465 g_assert (PSPPIRE_IS_DICT (d));
467 if ( ! dict_id_is_valid (d->dict, name, false))
470 if ( idx < dict_get_var_cnt (d->dict))
472 /* This is an existing variable? */
473 var = dict_get_var (d->dict, idx);
474 dict_rename_var (d->dict, var, name);
479 dict_create_var (d->dict, name, 0);
487 /* Return the IDXth variable.
488 Will return NULL if IDX exceeds the number of variables in the dictionary.
491 psppire_dict_get_variable (const PsppireDict *d, gint idx)
493 g_return_val_if_fail (d, NULL);
494 g_return_val_if_fail (d->dict, NULL);
496 if ( dict_get_var_cnt (d->dict) <= idx )
499 return dict_get_var (d->dict, idx);
503 /* Return the number of variables in the dictionary */
505 psppire_dict_get_var_cnt (const PsppireDict *d)
507 g_return_val_if_fail (d, -1);
508 g_return_val_if_fail (d->dict, -1);
510 return dict_get_var_cnt (d->dict);
514 /* Return the number of `union value's in the dictionary */
516 psppire_dict_get_value_cnt (const PsppireDict *d)
518 g_return_val_if_fail (d, -1);
519 g_return_val_if_fail (d->dict, -1);
521 return dict_get_next_value_idx (d->dict);
525 /* Returns the prototype for the cases that match the dictionary */
526 const struct caseproto *
527 psppire_dict_get_proto (const PsppireDict *d)
529 g_return_val_if_fail (d, NULL);
530 g_return_val_if_fail (d->dict, NULL);
532 return dict_get_proto (d->dict);
536 /* Return a variable by name.
537 Return NULL if it doesn't exist
540 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
542 g_return_val_if_fail (d, NULL);
543 g_return_val_if_fail (d->dict, NULL);
545 return dict_lookup_var (d->dict, name);
548 /* Clears the contents of D */
550 psppire_dict_clear (PsppireDict *d)
552 g_return_if_fail (d);
553 g_return_if_fail (d->dict);
556 dict_clear (d->dict);
561 /* Return true is NAME would be a valid name of a variable to add to the
562 dictionary. False otherwise.
563 If REPORT is true, then invalid names will be reported as such as errors
566 psppire_dict_check_name (const PsppireDict *dict,
567 const gchar *name, gboolean report)
569 if ( ! dict_id_is_valid (dict->dict, name, report ) )
572 if (psppire_dict_lookup_var (dict, name))
575 msg (ME, _("Duplicate variable name."));
584 psppire_dict_get_next_value_idx (const PsppireDict *dict)
586 return dict_get_next_value_idx (dict->dict);
590 /* Tree Model Stuff */
592 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
594 static gint tree_model_n_columns (GtkTreeModel *model);
596 static GType tree_model_column_type (GtkTreeModel *model, gint index);
598 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
601 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
603 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
606 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
607 gint column, GValue *value);
609 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
610 GtkTreeIter *parent, gint n);
612 static gint tree_model_n_children (GtkTreeModel *tree_model,
615 static gboolean tree_model_iter_children (GtkTreeModel *,
619 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
623 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
627 dictionary_tree_model_init (GtkTreeModelIface *iface)
629 iface->get_flags = tree_model_get_flags;
630 iface->get_n_columns = tree_model_n_columns;
631 iface->get_column_type = tree_model_column_type;
632 iface->get_iter = tree_model_get_iter;
633 iface->iter_next = tree_model_iter_next;
634 iface->get_path = tree_model_get_path;
635 iface->get_value = tree_model_get_value;
637 iface->iter_children = tree_model_iter_children ;
638 iface->iter_has_child = tree_model_iter_has_child ;
639 iface->iter_n_children = tree_model_n_children ;
640 iface->iter_nth_child = tree_model_nth_child ;
641 iface->iter_parent = tree_model_iter_parent ;
645 tree_model_iter_has_child (GtkTreeModel *tree_model,
652 tree_model_iter_parent (GtkTreeModel *tree_model,
659 static GtkTreeModelFlags
660 tree_model_get_flags (GtkTreeModel *model)
662 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
664 return GTK_TREE_MODEL_LIST_ONLY;
669 tree_model_n_columns (GtkTreeModel *model)
675 tree_model_column_type (GtkTreeModel *model, gint index)
677 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
681 case DICT_TVM_COL_NAME:
682 return G_TYPE_STRING;
684 case DICT_TVM_COL_VAR:
685 return PSPPIRE_VAR_PTR_TYPE;
687 case DICT_TVM_COL_LABEL:
688 return G_TYPE_STRING;
691 g_return_val_if_reached ((GType)0);
695 g_assert_not_reached ();
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_var_cnt (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 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
744 if ( iter == NULL || iter->user_data == NULL)
747 var = iter->user_data;
749 idx = var_get_dict_index (var);
751 if ( idx + 1 >= psppire_dict_get_var_cnt (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));
786 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
787 gint column, GValue *value)
789 struct variable *var;
790 PsppireDict *dict = PSPPIRE_DICT (model);
792 g_return_if_fail (iter->stamp == dict->stamp);
794 var = iter->user_data;
798 case DICT_TVM_COL_NAME:
800 g_value_init (value, G_TYPE_STRING);
801 g_value_set_string (value, var_get_name (var));
804 case DICT_TVM_COL_VAR:
805 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
806 g_value_set_boxed (value, var);
809 g_return_if_reached ();
815 tree_model_iter_children (GtkTreeModel *tree_model,
823 tree_model_n_children (GtkTreeModel *model,
826 PsppireDict *dict = PSPPIRE_DICT (model);
829 return psppire_dict_get_var_cnt (dict);
835 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
836 GtkTreeIter *parent, gint n)
840 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
842 dict = PSPPIRE_DICT (model);
847 if ( n >= psppire_dict_get_var_cnt (dict) )
850 iter->stamp = dict->stamp;
851 iter->user_data = psppire_dict_get_variable (dict, n);
853 if ( !iter->user_data)
861 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
864 if ( ! dict_id_is_valid (dict->dict, name, false))
867 /* Make sure no other variable has this name */
868 if ( NULL != psppire_dict_lookup_var (dict, name))
871 dict_rename_var (dict->dict, v, name);
878 psppire_dict_get_weight_variable (const PsppireDict *dict)
880 return dict_get_weight (dict->dict);
887 psppire_dict_dump (const PsppireDict *dict)
890 const struct dictionary *d = dict->dict;
892 for (i = 0; i < dict_get_var_cnt (d); ++i)
894 const struct variable *v = psppire_dict_get_variable (dict, i);
895 int di = var_get_dict_index (v);
896 g_print ("`%s' idx=%d, fv=%d\n",
899 var_get_case_index(v));
909 psppire_dict_encoding (const PsppireDict *dict)
911 return dict_get_encoding (dict->dict);