1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007 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/>. */
22 #include <gtksheet/gtkextra-marshal.h>
24 #include "psppire-dict.h"
25 #include <data/format.h>
26 #include <data/dictionary.h>
27 #include <data/missing-values.h>
28 #include <data/value-labels.h>
29 #include <data/variable.h>
32 #include "message-dialog.h"
34 /* --- prototypes --- */
35 static void psppire_dict_class_init (PsppireDictClass *class);
36 static void psppire_dict_init (PsppireDict *dict);
37 static void psppire_dict_finalize (GObject *object);
39 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
42 /* --- variables --- */
43 static GObjectClass *parent_class = NULL;
45 enum {VARIABLE_CHANGED,
54 static guint signals [n_SIGNALS];
56 /* --- functions --- */
58 * psppire_dict_get_type:
59 * @returns: the type ID for accelerator groups.
62 psppire_dict_get_type (void)
64 static GType object_type = 0;
68 static const GTypeInfo object_info = {
69 sizeof (PsppireDictClass),
71 (GBaseFinalizeFunc) NULL,
72 (GClassInitFunc) psppire_dict_class_init,
73 NULL, /* class_finalize */
74 NULL, /* class_data */
77 (GInstanceInitFunc) psppire_dict_init,
80 static const GInterfaceInfo tree_model_info = {
81 (GInterfaceInitFunc) dictionary_tree_model_init,
86 object_type = g_type_register_static (G_TYPE_OBJECT,
90 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
101 psppire_dict_class_init (PsppireDictClass *class)
103 GObjectClass *object_class = G_OBJECT_CLASS (class);
105 parent_class = g_type_class_peek_parent (class);
107 object_class->finalize = psppire_dict_finalize;
109 signals [VARIABLE_CHANGED] =
110 g_signal_new ("variable_changed",
111 G_TYPE_FROM_CLASS (class),
115 g_cclosure_marshal_VOID__INT,
122 signals [VARIABLE_INSERTED] =
123 g_signal_new ("variable_inserted",
124 G_TYPE_FROM_CLASS (class),
128 g_cclosure_marshal_VOID__INT,
134 signals [VARIABLE_DELETED] =
135 g_signal_new ("variable-deleted",
136 G_TYPE_FROM_CLASS (class),
140 marshaller_VOID__INT_INT_INT,
148 signals [VARIABLE_RESIZED] =
149 g_signal_new ("dict-size-changed",
150 G_TYPE_FROM_CLASS (class),
154 gtkextra_VOID__INT_INT,
161 signals [WEIGHT_CHANGED] =
162 g_signal_new ("weight-changed",
163 G_TYPE_FROM_CLASS (class),
167 g_cclosure_marshal_VOID__INT,
173 signals [FILTER_CHANGED] =
174 g_signal_new ("filter-changed",
175 G_TYPE_FROM_CLASS (class),
179 g_cclosure_marshal_VOID__INT,
185 signals [SPLIT_CHANGED] =
186 g_signal_new ("split-changed",
187 G_TYPE_FROM_CLASS (class),
191 g_cclosure_marshal_VOID__VOID,
197 psppire_dict_finalize (GObject *object)
199 PsppireDict *d = PSPPIRE_DICT (object);
201 dict_destroy (d->dict);
203 G_OBJECT_CLASS (parent_class)->finalize (object);
206 /* Pass on callbacks from src/data/dictionary, as
207 signals in the Gtk library */
209 addcb (struct dictionary *d, int idx, void *pd)
211 g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
215 delcb (struct dictionary *d, int dict_idx, int case_idx, int value_cnt,
218 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
219 dict_idx, case_idx, value_cnt );
223 mutcb (struct dictionary *d, int idx, void *pd)
225 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
229 resize_cb (struct dictionary *d, int idx, int delta, void *pd)
231 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, delta);
235 weight_changed_callback (struct dictionary *d, int idx, void *pd)
237 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
241 filter_changed_callback (struct dictionary *d, int idx, void *pd)
243 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
247 split_changed_callback (struct dictionary *d, void *pd)
249 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
253 static const struct dict_callbacks gui_callbacks =
259 weight_changed_callback,
260 filter_changed_callback,
261 split_changed_callback
265 psppire_dict_init (PsppireDict *psppire_dict)
267 psppire_dict->stamp = g_random_int ();
271 * psppire_dict_new_from_dict:
272 * @returns: a new #PsppireDict object
274 * Creates a new #PsppireDict.
277 psppire_dict_new_from_dict (struct dictionary *d)
279 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
282 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
289 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
291 struct variable *var = dict_get_weight (d);
294 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
296 var = dict_get_filter (d);
297 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
299 split_changed_callback (d, dict);
301 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
305 /* Returns a valid name for a new variable in DICT.
306 The return value is statically allocated */
308 auto_generate_var_name (PsppireDict *dict)
311 static gchar name[10];
313 while (g_snprintf (name, 10, "VAR%05d",d++),
314 psppire_dict_lookup_var (dict, name))
320 /* Insert a new variable at posn IDX, with the name NAME.
321 If NAME is null, then a name will be automatically assigned.
324 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
326 struct variable *var ;
327 g_return_if_fail (idx >= 0);
328 g_return_if_fail (d);
329 g_return_if_fail (PSPPIRE_IS_DICT (d));
332 name = auto_generate_var_name (d);
334 var = dict_create_var (d->dict, name, 0);
336 dict_reorder_var (d->dict, var, idx);
339 /* Delete N variables beginning at FIRST */
341 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
344 g_return_if_fail (d);
345 g_return_if_fail (d->dict);
346 g_return_if_fail (PSPPIRE_IS_DICT (d));
348 for (idx = 0 ; idx < n ; ++idx )
350 struct variable *var;
352 /* Do nothing if it's out of bounds */
353 if ( first >= dict_get_var_cnt (d->dict))
356 var = dict_get_var (d->dict, first);
357 dict_delete_var (d->dict, var);
363 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
365 struct variable *var;
367 g_assert (PSPPIRE_IS_DICT (d));
369 if ( ! var_is_valid_name (name, false))
372 if ( idx < dict_get_var_cnt (d->dict))
374 /* This is an existing variable? */
375 var = dict_get_var (d->dict, idx);
376 dict_rename_var (d->dict, var, name);
381 dict_create_var (d->dict, name, 0);
389 /* Return the IDXth variable */
391 psppire_dict_get_variable (const PsppireDict *d, gint idx)
393 g_return_val_if_fail (d, NULL);
394 g_return_val_if_fail (d->dict, NULL);
396 if ( dict_get_var_cnt (d->dict) <= idx )
399 return dict_get_var (d->dict, idx);
403 /* Return the number of variables in the dictionary */
405 psppire_dict_get_var_cnt (const PsppireDict *d)
407 g_return_val_if_fail (d, -1);
408 g_return_val_if_fail (d->dict, -1);
410 return dict_get_var_cnt (d->dict);
414 /* Return a variable by name.
415 Return NULL if it doesn't exist
418 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
420 g_return_val_if_fail (d, NULL);
421 g_return_val_if_fail (d->dict, NULL);
423 return dict_lookup_var (d->dict, name);
426 /* Clears the contents of D */
428 psppire_dict_clear (PsppireDict *d)
430 g_return_if_fail (d);
431 g_return_if_fail (d->dict);
434 dict_clear (d->dict);
439 /* Return true is NAME would be a valid name of a variable to add to the
440 dictionary. False otherwise.
441 If REPORT is true, then invalid names will be reported as such as errors
444 psppire_dict_check_name (const PsppireDict *dict,
445 const gchar *name, gboolean report)
447 if ( ! var_is_valid_name (name, report ) )
450 if (psppire_dict_lookup_var (dict, name))
453 msg (ME,"Duplicate variable name.");
462 psppire_dict_get_next_value_idx (const PsppireDict *dict)
464 return dict_get_next_value_idx (dict->dict);
469 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
470 gint old_size, gint new_size)
473 g_return_if_fail (d);
474 g_return_if_fail (d->dict);
476 if ( old_size == new_size )
479 fv = var_get_case_index (pv);
481 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
483 new_size - old_size );
487 /* Tree Model Stuff */
489 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
491 static gint tree_model_n_columns (GtkTreeModel *model);
493 static GType tree_model_column_type (GtkTreeModel *model, gint index);
495 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
498 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
500 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
503 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
504 gint column, GValue *value);
506 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
507 GtkTreeIter *parent, gint n);
509 static gint tree_model_n_children (GtkTreeModel *tree_model,
512 static gboolean tree_model_iter_children (GtkTreeModel *,
516 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
520 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
524 dictionary_tree_model_init (GtkTreeModelIface *iface)
526 iface->get_flags = tree_model_get_flags;
527 iface->get_n_columns = tree_model_n_columns;
528 iface->get_column_type = tree_model_column_type;
529 iface->get_iter = tree_model_get_iter;
530 iface->iter_next = tree_model_iter_next;
531 iface->get_path = tree_model_get_path;
532 iface->get_value = tree_model_get_value;
534 iface->iter_children = tree_model_iter_children ;
535 iface->iter_has_child = tree_model_iter_has_child ;
536 iface->iter_n_children = tree_model_n_children ;
537 iface->iter_nth_child = tree_model_nth_child ;
538 iface->iter_parent = tree_model_iter_parent ;
542 tree_model_iter_has_child (GtkTreeModel *tree_model,
549 tree_model_iter_parent (GtkTreeModel *tree_model,
556 static GtkTreeModelFlags
557 tree_model_get_flags (GtkTreeModel *model)
559 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
561 return GTK_TREE_MODEL_LIST_ONLY;
566 tree_model_n_columns (GtkTreeModel *model)
572 tree_model_column_type (GtkTreeModel *model, gint index)
574 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
578 case DICT_TVM_COL_NAME:
579 return G_TYPE_STRING;
581 case DICT_TVM_COL_VAR:
582 return G_TYPE_POINTER;
585 g_return_val_if_reached ((GType)0);
589 g_assert_not_reached ();
594 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
596 gint *indices, depth;
598 struct variable *var;
600 PsppireDict *dict = PSPPIRE_DICT (model);
602 g_return_val_if_fail (path, FALSE);
604 indices = gtk_tree_path_get_indices (path);
605 depth = gtk_tree_path_get_depth (path);
607 g_return_val_if_fail (depth == 1, FALSE);
611 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
614 iter->user_data = NULL;
618 var = psppire_dict_get_variable (dict, n);
620 g_assert (var_get_dict_index (var) == n);
622 iter->stamp = dict->stamp;
623 iter->user_data = var;
630 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
632 PsppireDict *dict = PSPPIRE_DICT (model);
633 struct variable *var;
636 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
638 if ( iter == NULL || iter->user_data == NULL)
641 var = iter->user_data;
643 idx = var_get_dict_index (var);
645 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
647 iter->user_data = NULL;
652 var = psppire_dict_get_variable (dict, idx + 1);
654 g_assert (var_get_dict_index (var) == idx + 1);
656 iter->user_data = var;
662 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
665 struct variable *var;
666 PsppireDict *dict = PSPPIRE_DICT (model);
668 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
670 var = iter->user_data;
672 path = gtk_tree_path_new ();
673 gtk_tree_path_append_index (path, var_get_dict_index (var));
680 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
681 gint column, GValue *value)
683 struct variable *var;
684 PsppireDict *dict = PSPPIRE_DICT (model);
686 g_return_if_fail (iter->stamp == dict->stamp);
688 var = iter->user_data;
692 case DICT_TVM_COL_NAME:
694 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
695 g_value_init (value, G_TYPE_STRING);
696 g_value_set_string (value, name);
700 case DICT_TVM_COL_VAR:
701 g_value_init (value, G_TYPE_POINTER);
702 g_value_set_pointer (value, var);
705 g_return_if_reached ();
711 tree_model_iter_children (GtkTreeModel *tree_model,
719 tree_model_n_children (GtkTreeModel *model,
722 PsppireDict *dict = PSPPIRE_DICT (model);
725 return psppire_dict_get_var_cnt (dict);
731 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
732 GtkTreeIter *parent, gint n)
736 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
738 dict = PSPPIRE_DICT (model);
743 if ( n >= psppire_dict_get_var_cnt (dict) )
746 iter->stamp = dict->stamp;
747 iter->user_data = psppire_dict_get_variable (dict, n);
749 if ( !iter->user_data)
757 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
760 if ( ! var_is_valid_name (name, false))
763 dict_rename_var (dict->dict, v, name);
770 psppire_dict_get_weight_variable (const PsppireDict *dict)
772 return dict_get_weight (dict->dict);
779 psppire_dict_dump (const PsppireDict *dict)
782 const struct dictionary *d = dict->dict;
784 int *map = dict_get_compacted_dict_index_to_case_index (d);
786 for (i = 0; i < dict_get_var_cnt (d); ++i)
788 const struct variable *v = psppire_dict_get_variable (dict, i);
789 int di = var_get_dict_index (v);
790 g_print ("\"%s\" idx=%d, fv=%d(%d), size=%d\n",
793 var_get_case_index(v),
795 value_cnt_from_width(var_get_width(v)));