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/psppire-marshal.h>
24 #include "psppire-dict.h"
25 #include <data/dictionary.h>
26 #include <data/missing-values.h>
27 #include <data/value-labels.h>
28 #include <data/variable.h>
31 #include "message-dialog.h"
41 VARIABLE_DISPLAY_WIDTH_CHANGED,
50 /* --- prototypes --- */
51 static void psppire_dict_class_init (PsppireDictClass *class);
52 static void psppire_dict_init (PsppireDict *dict);
53 static void psppire_dict_finalize (GObject *object);
55 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
58 /* --- variables --- */
59 static GObjectClass *parent_class = NULL;
61 static guint signals [n_SIGNALS];
63 /* --- functions --- */
65 * psppire_dict_get_type:
66 * @returns: the type ID for accelerator groups.
69 psppire_dict_get_type (void)
71 static GType object_type = 0;
75 static const GTypeInfo object_info = {
76 sizeof (PsppireDictClass),
78 (GBaseFinalizeFunc) NULL,
79 (GClassInitFunc) psppire_dict_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
84 (GInstanceInitFunc) psppire_dict_init,
87 static const GInterfaceInfo tree_model_info = {
88 (GInterfaceInitFunc) dictionary_tree_model_init,
93 object_type = g_type_register_static (G_TYPE_OBJECT,
97 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
106 psppire_dict_class_init (PsppireDictClass *class)
108 GObjectClass *object_class = G_OBJECT_CLASS (class);
110 parent_class = g_type_class_peek_parent (class);
112 object_class->finalize = psppire_dict_finalize;
114 signals [BACKEND_CHANGED] =
115 g_signal_new ("backend-changed",
116 G_TYPE_FROM_CLASS (class),
120 g_cclosure_marshal_VOID__VOID,
125 signals [VARIABLE_CHANGED] =
126 g_signal_new ("variable_changed",
127 G_TYPE_FROM_CLASS (class),
131 g_cclosure_marshal_VOID__INT,
138 signals [VARIABLE_INSERTED] =
139 g_signal_new ("variable_inserted",
140 G_TYPE_FROM_CLASS (class),
144 g_cclosure_marshal_VOID__INT,
150 signals [VARIABLE_DELETED] =
151 g_signal_new ("variable-deleted",
152 G_TYPE_FROM_CLASS (class),
156 psppire_marshal_VOID__INT_INT_INT,
164 signals [VARIABLE_RESIZED] =
165 g_signal_new ("dict-size-changed",
166 G_TYPE_FROM_CLASS (class),
170 psppire_marshal_VOID__INT_INT,
176 signals [VARIABLE_DISPLAY_WIDTH_CHANGED] =
177 g_signal_new ("variable-display-width-changed",
178 G_TYPE_FROM_CLASS (class),
182 g_cclosure_marshal_VOID__INT,
188 signals [WEIGHT_CHANGED] =
189 g_signal_new ("weight-changed",
190 G_TYPE_FROM_CLASS (class),
194 g_cclosure_marshal_VOID__INT,
200 signals [FILTER_CHANGED] =
201 g_signal_new ("filter-changed",
202 G_TYPE_FROM_CLASS (class),
206 g_cclosure_marshal_VOID__INT,
212 signals [SPLIT_CHANGED] =
213 g_signal_new ("split-changed",
214 G_TYPE_FROM_CLASS (class),
218 g_cclosure_marshal_VOID__VOID,
224 psppire_dict_finalize (GObject *object)
226 PsppireDict *d = PSPPIRE_DICT (object);
228 dict_destroy (d->dict);
230 G_OBJECT_CLASS (parent_class)->finalize (object);
233 /* Pass on callbacks from src/data/dictionary, as
234 signals in the Gtk library */
236 addcb (struct dictionary *d, int idx, void *pd)
238 PsppireDict *dict = PSPPIRE_DICT (pd);
240 if ( ! dict->disable_insert_signal)
241 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
245 delcb (struct dictionary *d, int dict_idx, int case_idx, int value_cnt,
248 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
249 dict_idx, case_idx, value_cnt );
253 mutcb (struct dictionary *d, int idx, void *pd)
255 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
259 resize_cb (struct dictionary *d, int idx, int delta, void *pd)
261 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, delta);
265 weight_changed_callback (struct dictionary *d, int idx, void *pd)
267 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
271 filter_changed_callback (struct dictionary *d, int idx, void *pd)
273 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
277 split_changed_callback (struct dictionary *d, void *pd)
279 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
283 variable_display_width_callback (struct dictionary *d, int idx, void *pd)
285 g_signal_emit (pd, signals [VARIABLE_DISPLAY_WIDTH_CHANGED], 0, idx);
290 static const struct dict_callbacks gui_callbacks =
296 weight_changed_callback,
297 filter_changed_callback,
298 split_changed_callback,
299 variable_display_width_callback
303 psppire_dict_init (PsppireDict *psppire_dict)
305 psppire_dict->stamp = g_random_int ();
306 psppire_dict->disable_insert_signal = FALSE;
310 * psppire_dict_new_from_dict:
311 * @returns: a new #PsppireDict object
313 * Creates a new #PsppireDict.
316 psppire_dict_new_from_dict (struct dictionary *d)
318 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
321 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
328 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
330 struct variable *var = dict_get_weight (d);
334 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
336 var = dict_get_filter (d);
337 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
339 split_changed_callback (d, dict);
341 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
343 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
347 /* Returns a valid name for a new variable in DICT.
348 The return value is statically allocated */
350 auto_generate_var_name (PsppireDict *dict)
353 static gchar name[10];
355 while (g_snprintf (name, 10, "VAR%05d",d++),
356 psppire_dict_lookup_var (dict, name))
362 /* Insert a new variable at posn IDX, with the name NAME.
363 If NAME is null, then a name will be automatically assigned.
366 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
368 struct variable *var ;
369 g_return_if_fail (idx >= 0);
370 g_return_if_fail (d);
371 g_return_if_fail (PSPPIRE_IS_DICT (d));
374 name = auto_generate_var_name (d);
376 d->disable_insert_signal = TRUE;
378 var = dict_create_var (d->dict, name, 0);
380 dict_reorder_var (d->dict, var, idx);
382 d->disable_insert_signal = FALSE;
384 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
387 /* Delete N variables beginning at FIRST */
389 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
392 g_return_if_fail (d);
393 g_return_if_fail (d->dict);
394 g_return_if_fail (PSPPIRE_IS_DICT (d));
396 for (idx = 0 ; idx < n ; ++idx )
398 struct variable *var;
400 /* Do nothing if it's out of bounds */
401 if ( first >= dict_get_var_cnt (d->dict))
404 var = dict_get_var (d->dict, first);
405 dict_delete_var (d->dict, var);
411 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
413 struct variable *var;
415 g_assert (PSPPIRE_IS_DICT (d));
417 if ( ! var_is_valid_name (name, false))
420 if ( idx < dict_get_var_cnt (d->dict))
422 /* This is an existing variable? */
423 var = dict_get_var (d->dict, idx);
424 dict_rename_var (d->dict, var, name);
429 dict_create_var (d->dict, name, 0);
437 /* Return the IDXth variable */
439 psppire_dict_get_variable (const PsppireDict *d, gint idx)
441 g_return_val_if_fail (d, NULL);
442 g_return_val_if_fail (d->dict, NULL);
444 if ( dict_get_var_cnt (d->dict) <= idx )
447 return dict_get_var (d->dict, idx);
451 /* Return the number of variables in the dictionary */
453 psppire_dict_get_var_cnt (const PsppireDict *d)
455 g_return_val_if_fail (d, -1);
456 g_return_val_if_fail (d->dict, -1);
458 return dict_get_var_cnt (d->dict);
462 /* Return the number of `union value's in the dictionary */
464 psppire_dict_get_value_cnt (const PsppireDict *d)
466 g_return_val_if_fail (d, -1);
467 g_return_val_if_fail (d->dict, -1);
469 return dict_get_next_value_idx (d->dict);
473 /* Return a variable by name.
474 Return NULL if it doesn't exist
477 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
479 g_return_val_if_fail (d, NULL);
480 g_return_val_if_fail (d->dict, NULL);
482 return dict_lookup_var (d->dict, name);
485 /* Clears the contents of D */
487 psppire_dict_clear (PsppireDict *d)
489 g_return_if_fail (d);
490 g_return_if_fail (d->dict);
493 dict_clear (d->dict);
498 /* Return true is NAME would be a valid name of a variable to add to the
499 dictionary. False otherwise.
500 If REPORT is true, then invalid names will be reported as such as errors
503 psppire_dict_check_name (const PsppireDict *dict,
504 const gchar *name, gboolean report)
506 if ( ! var_is_valid_name (name, report ) )
509 if (psppire_dict_lookup_var (dict, name))
512 msg (ME,"Duplicate variable name.");
521 psppire_dict_get_next_value_idx (const PsppireDict *dict)
523 return dict_get_next_value_idx (dict->dict);
528 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
529 gint old_size, gint new_size)
532 g_return_if_fail (d);
533 g_return_if_fail (d->dict);
535 if ( old_size == new_size )
538 fv = var_get_case_index (pv);
540 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
542 new_size - old_size );
546 /* Tree Model Stuff */
548 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
550 static gint tree_model_n_columns (GtkTreeModel *model);
552 static GType tree_model_column_type (GtkTreeModel *model, gint index);
554 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
557 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
559 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
562 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
563 gint column, GValue *value);
565 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
566 GtkTreeIter *parent, gint n);
568 static gint tree_model_n_children (GtkTreeModel *tree_model,
571 static gboolean tree_model_iter_children (GtkTreeModel *,
575 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
579 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
583 dictionary_tree_model_init (GtkTreeModelIface *iface)
585 iface->get_flags = tree_model_get_flags;
586 iface->get_n_columns = tree_model_n_columns;
587 iface->get_column_type = tree_model_column_type;
588 iface->get_iter = tree_model_get_iter;
589 iface->iter_next = tree_model_iter_next;
590 iface->get_path = tree_model_get_path;
591 iface->get_value = tree_model_get_value;
593 iface->iter_children = tree_model_iter_children ;
594 iface->iter_has_child = tree_model_iter_has_child ;
595 iface->iter_n_children = tree_model_n_children ;
596 iface->iter_nth_child = tree_model_nth_child ;
597 iface->iter_parent = tree_model_iter_parent ;
601 tree_model_iter_has_child (GtkTreeModel *tree_model,
608 tree_model_iter_parent (GtkTreeModel *tree_model,
615 static GtkTreeModelFlags
616 tree_model_get_flags (GtkTreeModel *model)
618 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
620 return GTK_TREE_MODEL_LIST_ONLY;
625 tree_model_n_columns (GtkTreeModel *model)
631 tree_model_column_type (GtkTreeModel *model, gint index)
633 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
637 case DICT_TVM_COL_NAME:
638 return G_TYPE_STRING;
640 case DICT_TVM_COL_VAR:
641 return G_TYPE_POINTER;
644 g_return_val_if_reached ((GType)0);
648 g_assert_not_reached ();
653 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
655 gint *indices, depth;
657 struct variable *var;
659 PsppireDict *dict = PSPPIRE_DICT (model);
661 g_return_val_if_fail (path, FALSE);
663 indices = gtk_tree_path_get_indices (path);
664 depth = gtk_tree_path_get_depth (path);
666 g_return_val_if_fail (depth == 1, FALSE);
670 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
673 iter->user_data = NULL;
677 var = psppire_dict_get_variable (dict, n);
679 g_assert (var_get_dict_index (var) == n);
681 iter->stamp = dict->stamp;
682 iter->user_data = var;
689 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
691 PsppireDict *dict = PSPPIRE_DICT (model);
692 struct variable *var;
695 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
697 if ( iter == NULL || iter->user_data == NULL)
700 var = iter->user_data;
702 idx = var_get_dict_index (var);
704 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
706 iter->user_data = NULL;
711 var = psppire_dict_get_variable (dict, idx + 1);
713 g_assert (var_get_dict_index (var) == idx + 1);
715 iter->user_data = var;
721 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
724 struct variable *var;
725 PsppireDict *dict = PSPPIRE_DICT (model);
727 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
729 var = iter->user_data;
731 path = gtk_tree_path_new ();
732 gtk_tree_path_append_index (path, var_get_dict_index (var));
739 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
740 gint column, GValue *value)
742 struct variable *var;
743 PsppireDict *dict = PSPPIRE_DICT (model);
745 g_return_if_fail (iter->stamp == dict->stamp);
747 var = iter->user_data;
751 case DICT_TVM_COL_NAME:
753 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
754 g_value_init (value, G_TYPE_STRING);
755 g_value_set_string (value, name);
759 case DICT_TVM_COL_VAR:
760 g_value_init (value, G_TYPE_POINTER);
761 g_value_set_pointer (value, var);
764 g_return_if_reached ();
770 tree_model_iter_children (GtkTreeModel *tree_model,
778 tree_model_n_children (GtkTreeModel *model,
781 PsppireDict *dict = PSPPIRE_DICT (model);
784 return psppire_dict_get_var_cnt (dict);
790 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
791 GtkTreeIter *parent, gint n)
795 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
797 dict = PSPPIRE_DICT (model);
802 if ( n >= psppire_dict_get_var_cnt (dict) )
805 iter->stamp = dict->stamp;
806 iter->user_data = psppire_dict_get_variable (dict, n);
808 if ( !iter->user_data)
816 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
819 if ( ! var_is_valid_name (name, false))
822 /* Make sure no other variable has this name */
823 if ( NULL != psppire_dict_lookup_var (dict, name))
826 dict_rename_var (dict->dict, v, name);
833 psppire_dict_get_weight_variable (const PsppireDict *dict)
835 return dict_get_weight (dict->dict);
842 psppire_dict_dump (const PsppireDict *dict)
845 const struct dictionary *d = dict->dict;
847 for (i = 0; i < dict_get_var_cnt (d); ++i)
849 const struct variable *v = psppire_dict_get_variable (dict, i);
850 int di = var_get_dict_index (v);
851 g_print ("\"%s\" idx=%d, fv=%d, size=%d\n",
854 var_get_case_index(v),
855 value_cnt_from_width(var_get_width(v)));