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 the number of `union value's in the dictionary */
416 psppire_dict_get_value_cnt (const PsppireDict *d)
418 g_return_val_if_fail (d, -1);
419 g_return_val_if_fail (d->dict, -1);
421 return dict_get_next_value_idx (d->dict);
425 /* Return a variable by name.
426 Return NULL if it doesn't exist
429 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
431 g_return_val_if_fail (d, NULL);
432 g_return_val_if_fail (d->dict, NULL);
434 return dict_lookup_var (d->dict, name);
437 /* Clears the contents of D */
439 psppire_dict_clear (PsppireDict *d)
441 g_return_if_fail (d);
442 g_return_if_fail (d->dict);
445 dict_clear (d->dict);
450 /* Return true is NAME would be a valid name of a variable to add to the
451 dictionary. False otherwise.
452 If REPORT is true, then invalid names will be reported as such as errors
455 psppire_dict_check_name (const PsppireDict *dict,
456 const gchar *name, gboolean report)
458 if ( ! var_is_valid_name (name, report ) )
461 if (psppire_dict_lookup_var (dict, name))
464 msg (ME,"Duplicate variable name.");
473 psppire_dict_get_next_value_idx (const PsppireDict *dict)
475 return dict_get_next_value_idx (dict->dict);
480 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
481 gint old_size, gint new_size)
484 g_return_if_fail (d);
485 g_return_if_fail (d->dict);
487 if ( old_size == new_size )
490 fv = var_get_case_index (pv);
492 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
494 new_size - old_size );
498 /* Tree Model Stuff */
500 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
502 static gint tree_model_n_columns (GtkTreeModel *model);
504 static GType tree_model_column_type (GtkTreeModel *model, gint index);
506 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
509 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
511 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
514 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
515 gint column, GValue *value);
517 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
518 GtkTreeIter *parent, gint n);
520 static gint tree_model_n_children (GtkTreeModel *tree_model,
523 static gboolean tree_model_iter_children (GtkTreeModel *,
527 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
531 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
535 dictionary_tree_model_init (GtkTreeModelIface *iface)
537 iface->get_flags = tree_model_get_flags;
538 iface->get_n_columns = tree_model_n_columns;
539 iface->get_column_type = tree_model_column_type;
540 iface->get_iter = tree_model_get_iter;
541 iface->iter_next = tree_model_iter_next;
542 iface->get_path = tree_model_get_path;
543 iface->get_value = tree_model_get_value;
545 iface->iter_children = tree_model_iter_children ;
546 iface->iter_has_child = tree_model_iter_has_child ;
547 iface->iter_n_children = tree_model_n_children ;
548 iface->iter_nth_child = tree_model_nth_child ;
549 iface->iter_parent = tree_model_iter_parent ;
553 tree_model_iter_has_child (GtkTreeModel *tree_model,
560 tree_model_iter_parent (GtkTreeModel *tree_model,
567 static GtkTreeModelFlags
568 tree_model_get_flags (GtkTreeModel *model)
570 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
572 return GTK_TREE_MODEL_LIST_ONLY;
577 tree_model_n_columns (GtkTreeModel *model)
583 tree_model_column_type (GtkTreeModel *model, gint index)
585 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
589 case DICT_TVM_COL_NAME:
590 return G_TYPE_STRING;
592 case DICT_TVM_COL_VAR:
593 return G_TYPE_POINTER;
596 g_return_val_if_reached ((GType)0);
600 g_assert_not_reached ();
605 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
607 gint *indices, depth;
609 struct variable *var;
611 PsppireDict *dict = PSPPIRE_DICT (model);
613 g_return_val_if_fail (path, FALSE);
615 indices = gtk_tree_path_get_indices (path);
616 depth = gtk_tree_path_get_depth (path);
618 g_return_val_if_fail (depth == 1, FALSE);
622 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
625 iter->user_data = NULL;
629 var = psppire_dict_get_variable (dict, n);
631 g_assert (var_get_dict_index (var) == n);
633 iter->stamp = dict->stamp;
634 iter->user_data = var;
641 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
643 PsppireDict *dict = PSPPIRE_DICT (model);
644 struct variable *var;
647 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
649 if ( iter == NULL || iter->user_data == NULL)
652 var = iter->user_data;
654 idx = var_get_dict_index (var);
656 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
658 iter->user_data = NULL;
663 var = psppire_dict_get_variable (dict, idx + 1);
665 g_assert (var_get_dict_index (var) == idx + 1);
667 iter->user_data = var;
673 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
676 struct variable *var;
677 PsppireDict *dict = PSPPIRE_DICT (model);
679 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
681 var = iter->user_data;
683 path = gtk_tree_path_new ();
684 gtk_tree_path_append_index (path, var_get_dict_index (var));
691 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
692 gint column, GValue *value)
694 struct variable *var;
695 PsppireDict *dict = PSPPIRE_DICT (model);
697 g_return_if_fail (iter->stamp == dict->stamp);
699 var = iter->user_data;
703 case DICT_TVM_COL_NAME:
705 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
706 g_value_init (value, G_TYPE_STRING);
707 g_value_set_string (value, name);
711 case DICT_TVM_COL_VAR:
712 g_value_init (value, G_TYPE_POINTER);
713 g_value_set_pointer (value, var);
716 g_return_if_reached ();
722 tree_model_iter_children (GtkTreeModel *tree_model,
730 tree_model_n_children (GtkTreeModel *model,
733 PsppireDict *dict = PSPPIRE_DICT (model);
736 return psppire_dict_get_var_cnt (dict);
742 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
743 GtkTreeIter *parent, gint n)
747 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
749 dict = PSPPIRE_DICT (model);
754 if ( n >= psppire_dict_get_var_cnt (dict) )
757 iter->stamp = dict->stamp;
758 iter->user_data = psppire_dict_get_variable (dict, n);
760 if ( !iter->user_data)
768 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
771 if ( ! var_is_valid_name (name, false))
774 dict_rename_var (dict->dict, v, name);
781 psppire_dict_get_weight_variable (const PsppireDict *dict)
783 return dict_get_weight (dict->dict);
790 psppire_dict_dump (const PsppireDict *dict)
793 const struct dictionary *d = dict->dict;
795 for (i = 0; i < dict_get_var_cnt (d); ++i)
797 const struct variable *v = psppire_dict_get_variable (dict, i);
798 int di = var_get_dict_index (v);
799 g_print ("\"%s\" idx=%d, fv=%d, size=%d\n",
802 var_get_case_index(v),
803 value_cnt_from_width(var_get_width(v)));