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"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
54 /* --- prototypes --- */
55 static void psppire_dict_class_init (PsppireDictClass *class);
56 static void psppire_dict_init (PsppireDict *dict);
57 static void psppire_dict_dispose (GObject *object);
59 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
62 /* --- variables --- */
63 static GObjectClass *parent_class = NULL;
65 static guint signals [n_SIGNALS];
67 /* --- functions --- */
69 * psppire_dict_get_type:
70 * @returns: the type ID for accelerator groups.
73 psppire_dict_get_type (void)
75 static GType object_type = 0;
79 static const GTypeInfo object_info = {
80 sizeof (PsppireDictClass),
82 (GBaseFinalizeFunc) NULL,
83 (GClassInitFunc) psppire_dict_class_init,
84 NULL, /* class_finalize */
85 NULL, /* class_data */
88 (GInstanceInitFunc) psppire_dict_init,
91 static const GInterfaceInfo tree_model_info = {
92 (GInterfaceInitFunc) dictionary_tree_model_init,
97 object_type = g_type_register_static (G_TYPE_OBJECT,
101 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
110 psppire_dict_class_init (PsppireDictClass *class)
112 GObjectClass *object_class = G_OBJECT_CLASS (class);
114 parent_class = g_type_class_peek_parent (class);
116 object_class->dispose = psppire_dict_dispose;
118 signals [BACKEND_CHANGED] =
119 g_signal_new ("backend-changed",
120 G_TYPE_FROM_CLASS (class),
124 g_cclosure_marshal_VOID__VOID,
129 signals [VARIABLE_CHANGED] =
130 g_signal_new ("variable-changed",
131 G_TYPE_FROM_CLASS (class),
135 psppire_marshal_VOID__INT_UINT_POINTER,
145 signals [VARIABLE_INSERTED] =
146 g_signal_new ("variable-inserted",
147 G_TYPE_FROM_CLASS (class),
151 g_cclosure_marshal_VOID__INT,
157 signals [VARIABLE_DELETED] =
158 g_signal_new ("variable-deleted",
159 G_TYPE_FROM_CLASS (class),
163 psppire_marshal_VOID__POINTER_INT_INT,
171 signals [WEIGHT_CHANGED] =
172 g_signal_new ("weight-changed",
173 G_TYPE_FROM_CLASS (class),
177 g_cclosure_marshal_VOID__INT,
183 signals [FILTER_CHANGED] =
184 g_signal_new ("filter-changed",
185 G_TYPE_FROM_CLASS (class),
189 g_cclosure_marshal_VOID__INT,
195 signals [SPLIT_CHANGED] =
196 g_signal_new ("split-changed",
197 G_TYPE_FROM_CLASS (class),
201 g_cclosure_marshal_VOID__VOID,
207 psppire_dict_dispose (GObject *object)
209 PsppireDict *d = PSPPIRE_DICT (object);
211 dict_set_callbacks (d->dict, NULL, NULL);
213 G_OBJECT_CLASS (parent_class)->dispose (object);
216 /* Pass on callbacks from src/data/dictionary, as
217 signals in the Gtk library */
219 addcb (struct dictionary *d, int idx, void *pd)
221 PsppireDict *dict = PSPPIRE_DICT (pd);
223 if ( ! dict->disable_insert_signal)
224 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
228 delcb (struct dictionary *d, const struct variable *var,
229 int dict_idx, int case_idx, void *pd)
231 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
232 var, dict_idx, case_idx);
236 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
238 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
242 weight_changed_callback (struct dictionary *d, int idx, void *pd)
244 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
248 filter_changed_callback (struct dictionary *d, int idx, void *pd)
250 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
254 split_changed_callback (struct dictionary *d, void *pd)
256 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
259 static const struct dict_callbacks gui_callbacks =
264 weight_changed_callback,
265 filter_changed_callback,
266 split_changed_callback
270 psppire_dict_init (PsppireDict *psppire_dict)
272 psppire_dict->stamp = g_random_int ();
273 psppire_dict->disable_insert_signal = FALSE;
277 * psppire_dict_new_from_dict:
278 * @returns: a new #PsppireDict object
280 * Creates a new #PsppireDict.
283 psppire_dict_new_from_dict (struct dictionary *d)
285 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
288 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
295 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
297 struct variable *var = dict_get_weight (d);
301 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
303 var = dict_get_filter (d);
304 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
306 split_changed_callback (d, dict);
308 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
310 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
314 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
315 Returns true if successful, false if SIZE is insufficient. */
317 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
325 /* TRANSLATORS: This string must be a valid variable name. That means:
326 - The string must be at most 64 bytes (not characters) long.
327 - The string may not contain whitespace.
328 - The first character may not be '$'
329 - The first character may not be a digit
330 - The final charactor may not be '.' or '_'
332 len = snprintf (name, size, _("Var%04d"), d);
336 if (psppire_dict_lookup_var (dict, name) == NULL)
343 /* Insert a new variable at posn IDX, with the name NAME, and return the
345 If NAME is null, then a name will be automatically assigned.
348 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
350 struct variable *var;
353 g_return_val_if_fail (idx >= 0, NULL);
354 g_return_val_if_fail (d, NULL);
355 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
359 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
360 g_return_val_if_reached (NULL);
365 d->disable_insert_signal = TRUE;
367 var = dict_create_var (d->dict, name, 0);
369 dict_reorder_var (d->dict, var, idx);
371 d->disable_insert_signal = FALSE;
373 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
378 /* Delete N variables beginning at FIRST */
380 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
383 g_return_if_fail (d);
384 g_return_if_fail (d->dict);
385 g_return_if_fail (PSPPIRE_IS_DICT (d));
387 for (idx = 0 ; idx < n ; ++idx )
389 struct variable *var;
391 /* Do nothing if it's out of bounds */
392 if ( first >= dict_get_var_cnt (d->dict))
395 var = dict_get_var (d->dict, first);
396 dict_delete_var (d->dict, var);
402 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
404 struct variable *var;
406 g_assert (PSPPIRE_IS_DICT (d));
408 if ( ! dict_id_is_valid (d->dict, name, false))
411 if ( idx < dict_get_var_cnt (d->dict))
413 /* This is an existing variable? */
414 var = dict_get_var (d->dict, idx);
415 dict_rename_var (d->dict, var, name);
420 dict_create_var (d->dict, name, 0);
428 /* Return the IDXth variable.
429 Will return NULL if IDX exceeds the number of variables in the dictionary.
432 psppire_dict_get_variable (const PsppireDict *d, gint idx)
434 g_return_val_if_fail (d, NULL);
435 g_return_val_if_fail (d->dict, NULL);
437 if ( dict_get_var_cnt (d->dict) <= idx )
440 return dict_get_var (d->dict, idx);
444 /* Return the number of variables in the dictionary */
446 psppire_dict_get_var_cnt (const PsppireDict *d)
448 g_return_val_if_fail (d, -1);
449 g_return_val_if_fail (d->dict, -1);
451 return dict_get_var_cnt (d->dict);
455 /* Return the number of `union value's in the dictionary */
457 psppire_dict_get_value_cnt (const PsppireDict *d)
459 g_return_val_if_fail (d, -1);
460 g_return_val_if_fail (d->dict, -1);
462 return dict_get_next_value_idx (d->dict);
466 /* Returns the prototype for the cases that match the dictionary */
467 const struct caseproto *
468 psppire_dict_get_proto (const PsppireDict *d)
470 g_return_val_if_fail (d, NULL);
471 g_return_val_if_fail (d->dict, NULL);
473 return dict_get_proto (d->dict);
477 /* Return a variable by name.
478 Return NULL if it doesn't exist
481 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
483 g_return_val_if_fail (d, NULL);
484 g_return_val_if_fail (d->dict, NULL);
486 return dict_lookup_var (d->dict, name);
489 /* Clears the contents of D */
491 psppire_dict_clear (PsppireDict *d)
493 g_return_if_fail (d);
494 g_return_if_fail (d->dict);
497 dict_clear (d->dict);
502 /* Return true is NAME would be a valid name of a variable to add to the
503 dictionary. False otherwise.
504 If REPORT is true, then invalid names will be reported as such as errors
507 psppire_dict_check_name (const PsppireDict *dict,
508 const gchar *name, gboolean report)
510 if ( ! dict_id_is_valid (dict->dict, name, report ) )
513 if (psppire_dict_lookup_var (dict, name))
516 msg (ME, _("Duplicate variable name."));
525 psppire_dict_get_next_value_idx (const PsppireDict *dict)
527 return dict_get_next_value_idx (dict->dict);
531 /* Tree Model Stuff */
533 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
535 static gint tree_model_n_columns (GtkTreeModel *model);
537 static GType tree_model_column_type (GtkTreeModel *model, gint index);
539 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
542 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
544 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
547 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
548 gint column, GValue *value);
550 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
551 GtkTreeIter *parent, gint n);
553 static gint tree_model_n_children (GtkTreeModel *tree_model,
556 static gboolean tree_model_iter_children (GtkTreeModel *,
560 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
564 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
568 dictionary_tree_model_init (GtkTreeModelIface *iface)
570 iface->get_flags = tree_model_get_flags;
571 iface->get_n_columns = tree_model_n_columns;
572 iface->get_column_type = tree_model_column_type;
573 iface->get_iter = tree_model_get_iter;
574 iface->iter_next = tree_model_iter_next;
575 iface->get_path = tree_model_get_path;
576 iface->get_value = tree_model_get_value;
578 iface->iter_children = tree_model_iter_children ;
579 iface->iter_has_child = tree_model_iter_has_child ;
580 iface->iter_n_children = tree_model_n_children ;
581 iface->iter_nth_child = tree_model_nth_child ;
582 iface->iter_parent = tree_model_iter_parent ;
586 tree_model_iter_has_child (GtkTreeModel *tree_model,
593 tree_model_iter_parent (GtkTreeModel *tree_model,
600 static GtkTreeModelFlags
601 tree_model_get_flags (GtkTreeModel *model)
603 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
605 return GTK_TREE_MODEL_LIST_ONLY;
610 tree_model_n_columns (GtkTreeModel *model)
616 tree_model_column_type (GtkTreeModel *model, gint index)
618 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
622 case DICT_TVM_COL_NAME:
623 return G_TYPE_STRING;
625 case DICT_TVM_COL_VAR:
626 return PSPPIRE_VAR_PTR_TYPE;
629 g_return_val_if_reached ((GType)0);
633 g_assert_not_reached ();
638 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
640 gint *indices, depth;
642 struct variable *var;
644 PsppireDict *dict = PSPPIRE_DICT (model);
646 g_return_val_if_fail (path, FALSE);
648 indices = gtk_tree_path_get_indices (path);
649 depth = gtk_tree_path_get_depth (path);
651 g_return_val_if_fail (depth == 1, FALSE);
655 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
658 iter->user_data = NULL;
662 var = psppire_dict_get_variable (dict, n);
664 g_assert (var_get_dict_index (var) == n);
666 iter->stamp = dict->stamp;
667 iter->user_data = var;
674 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
676 PsppireDict *dict = PSPPIRE_DICT (model);
677 struct variable *var;
680 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
682 if ( iter == NULL || iter->user_data == NULL)
685 var = iter->user_data;
687 idx = var_get_dict_index (var);
689 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
691 iter->user_data = NULL;
696 var = psppire_dict_get_variable (dict, idx + 1);
698 g_assert (var_get_dict_index (var) == idx + 1);
700 iter->user_data = var;
706 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
709 struct variable *var;
710 PsppireDict *dict = PSPPIRE_DICT (model);
712 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
714 var = iter->user_data;
716 path = gtk_tree_path_new ();
717 gtk_tree_path_append_index (path, var_get_dict_index (var));
724 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
725 gint column, GValue *value)
727 struct variable *var;
728 PsppireDict *dict = PSPPIRE_DICT (model);
730 g_return_if_fail (iter->stamp == dict->stamp);
732 var = iter->user_data;
736 case DICT_TVM_COL_NAME:
738 g_value_init (value, G_TYPE_STRING);
739 g_value_set_string (value, var_get_name (var));
742 case DICT_TVM_COL_VAR:
743 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
744 g_value_set_boxed (value, var);
747 g_return_if_reached ();
753 tree_model_iter_children (GtkTreeModel *tree_model,
761 tree_model_n_children (GtkTreeModel *model,
764 PsppireDict *dict = PSPPIRE_DICT (model);
767 return psppire_dict_get_var_cnt (dict);
773 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
774 GtkTreeIter *parent, gint n)
778 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
780 dict = PSPPIRE_DICT (model);
785 if ( n >= psppire_dict_get_var_cnt (dict) )
788 iter->stamp = dict->stamp;
789 iter->user_data = psppire_dict_get_variable (dict, n);
791 if ( !iter->user_data)
799 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
802 if ( ! dict_id_is_valid (dict->dict, name, false))
805 /* Make sure no other variable has this name */
806 if ( NULL != psppire_dict_lookup_var (dict, name))
809 dict_rename_var (dict->dict, v, name);
816 psppire_dict_get_weight_variable (const PsppireDict *dict)
818 return dict_get_weight (dict->dict);
825 psppire_dict_dump (const PsppireDict *dict)
828 const struct dictionary *d = dict->dict;
830 for (i = 0; i < dict_get_var_cnt (d); ++i)
832 const struct variable *v = psppire_dict_get_variable (dict, i);
833 int di = var_get_dict_index (v);
834 g_print ("`%s' idx=%d, fv=%d\n",
837 var_get_case_index(v));
847 psppire_dict_encoding (const PsppireDict *dict)
849 return dict_get_encoding (dict->dict);