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;
59 static guint signals [n_SIGNALS];
61 /* --- functions --- */
63 * psppire_dict_get_type:
64 * @returns: the type ID for accelerator groups.
67 psppire_dict_get_type (void)
69 static GType object_type = 0;
73 static const GTypeInfo object_info = {
74 sizeof (PsppireDictClass),
76 (GBaseFinalizeFunc) NULL,
77 (GClassInitFunc) psppire_dict_class_init,
78 NULL, /* class_finalize */
79 NULL, /* class_data */
82 (GInstanceInitFunc) psppire_dict_init,
85 static const GInterfaceInfo tree_model_info = {
86 (GInterfaceInitFunc) dictionary_tree_model_init,
91 object_type = g_type_register_static (G_TYPE_OBJECT,
95 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 marshaller_VOID__INT_INT_INT,
164 signals [VARIABLE_RESIZED] =
165 g_signal_new ("dict-size-changed",
166 G_TYPE_FROM_CLASS (class),
170 gtkextra_VOID__INT_INT,
177 signals [WEIGHT_CHANGED] =
178 g_signal_new ("weight-changed",
179 G_TYPE_FROM_CLASS (class),
183 g_cclosure_marshal_VOID__INT,
189 signals [FILTER_CHANGED] =
190 g_signal_new ("filter-changed",
191 G_TYPE_FROM_CLASS (class),
195 g_cclosure_marshal_VOID__INT,
201 signals [SPLIT_CHANGED] =
202 g_signal_new ("split-changed",
203 G_TYPE_FROM_CLASS (class),
207 g_cclosure_marshal_VOID__VOID,
213 psppire_dict_finalize (GObject *object)
215 PsppireDict *d = PSPPIRE_DICT (object);
217 dict_destroy (d->dict);
219 G_OBJECT_CLASS (parent_class)->finalize (object);
222 /* Pass on callbacks from src/data/dictionary, as
223 signals in the Gtk library */
225 addcb (struct dictionary *d, int idx, void *pd)
227 g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
231 delcb (struct dictionary *d, int dict_idx, int case_idx, int value_cnt,
234 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
235 dict_idx, case_idx, value_cnt );
239 mutcb (struct dictionary *d, int idx, void *pd)
241 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
245 resize_cb (struct dictionary *d, int idx, int delta, void *pd)
247 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, delta);
251 weight_changed_callback (struct dictionary *d, int idx, void *pd)
253 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
257 filter_changed_callback (struct dictionary *d, int idx, void *pd)
259 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
263 split_changed_callback (struct dictionary *d, void *pd)
265 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
269 static const struct dict_callbacks gui_callbacks =
275 weight_changed_callback,
276 filter_changed_callback,
277 split_changed_callback
281 psppire_dict_init (PsppireDict *psppire_dict)
283 psppire_dict->stamp = g_random_int ();
287 * psppire_dict_new_from_dict:
288 * @returns: a new #PsppireDict object
290 * Creates a new #PsppireDict.
293 psppire_dict_new_from_dict (struct dictionary *d)
295 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
298 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
305 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
307 struct variable *var = dict_get_weight (d);
311 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
313 var = dict_get_filter (d);
314 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
316 split_changed_callback (d, dict);
318 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
320 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
324 /* Returns a valid name for a new variable in DICT.
325 The return value is statically allocated */
327 auto_generate_var_name (PsppireDict *dict)
330 static gchar name[10];
332 while (g_snprintf (name, 10, "VAR%05d",d++),
333 psppire_dict_lookup_var (dict, name))
339 /* Insert a new variable at posn IDX, with the name NAME.
340 If NAME is null, then a name will be automatically assigned.
343 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
345 struct variable *var ;
346 g_return_if_fail (idx >= 0);
347 g_return_if_fail (d);
348 g_return_if_fail (PSPPIRE_IS_DICT (d));
351 name = auto_generate_var_name (d);
353 var = dict_create_var (d->dict, name, 0);
355 dict_reorder_var (d->dict, var, idx);
358 /* Delete N variables beginning at FIRST */
360 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
363 g_return_if_fail (d);
364 g_return_if_fail (d->dict);
365 g_return_if_fail (PSPPIRE_IS_DICT (d));
367 for (idx = 0 ; idx < n ; ++idx )
369 struct variable *var;
371 /* Do nothing if it's out of bounds */
372 if ( first >= dict_get_var_cnt (d->dict))
375 var = dict_get_var (d->dict, first);
376 dict_delete_var (d->dict, var);
382 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
384 struct variable *var;
386 g_assert (PSPPIRE_IS_DICT (d));
388 if ( ! var_is_valid_name (name, false))
391 if ( idx < dict_get_var_cnt (d->dict))
393 /* This is an existing variable? */
394 var = dict_get_var (d->dict, idx);
395 dict_rename_var (d->dict, var, name);
400 dict_create_var (d->dict, name, 0);
408 /* Return the IDXth variable */
410 psppire_dict_get_variable (const PsppireDict *d, gint idx)
412 g_return_val_if_fail (d, NULL);
413 g_return_val_if_fail (d->dict, NULL);
415 if ( dict_get_var_cnt (d->dict) <= idx )
418 return dict_get_var (d->dict, idx);
422 /* Return the number of variables in the dictionary */
424 psppire_dict_get_var_cnt (const PsppireDict *d)
426 g_return_val_if_fail (d, -1);
427 g_return_val_if_fail (d->dict, -1);
429 return dict_get_var_cnt (d->dict);
433 /* Return the number of `union value's in the dictionary */
435 psppire_dict_get_value_cnt (const PsppireDict *d)
437 g_return_val_if_fail (d, -1);
438 g_return_val_if_fail (d->dict, -1);
440 return dict_get_next_value_idx (d->dict);
444 /* Return a variable by name.
445 Return NULL if it doesn't exist
448 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
450 g_return_val_if_fail (d, NULL);
451 g_return_val_if_fail (d->dict, NULL);
453 return dict_lookup_var (d->dict, name);
456 /* Clears the contents of D */
458 psppire_dict_clear (PsppireDict *d)
460 g_return_if_fail (d);
461 g_return_if_fail (d->dict);
464 dict_clear (d->dict);
469 /* Return true is NAME would be a valid name of a variable to add to the
470 dictionary. False otherwise.
471 If REPORT is true, then invalid names will be reported as such as errors
474 psppire_dict_check_name (const PsppireDict *dict,
475 const gchar *name, gboolean report)
477 if ( ! var_is_valid_name (name, report ) )
480 if (psppire_dict_lookup_var (dict, name))
483 msg (ME,"Duplicate variable name.");
492 psppire_dict_get_next_value_idx (const PsppireDict *dict)
494 return dict_get_next_value_idx (dict->dict);
499 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
500 gint old_size, gint new_size)
503 g_return_if_fail (d);
504 g_return_if_fail (d->dict);
506 if ( old_size == new_size )
509 fv = var_get_case_index (pv);
511 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
513 new_size - old_size );
517 /* Tree Model Stuff */
519 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
521 static gint tree_model_n_columns (GtkTreeModel *model);
523 static GType tree_model_column_type (GtkTreeModel *model, gint index);
525 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
528 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
530 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
533 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
534 gint column, GValue *value);
536 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
537 GtkTreeIter *parent, gint n);
539 static gint tree_model_n_children (GtkTreeModel *tree_model,
542 static gboolean tree_model_iter_children (GtkTreeModel *,
546 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
550 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
554 dictionary_tree_model_init (GtkTreeModelIface *iface)
556 iface->get_flags = tree_model_get_flags;
557 iface->get_n_columns = tree_model_n_columns;
558 iface->get_column_type = tree_model_column_type;
559 iface->get_iter = tree_model_get_iter;
560 iface->iter_next = tree_model_iter_next;
561 iface->get_path = tree_model_get_path;
562 iface->get_value = tree_model_get_value;
564 iface->iter_children = tree_model_iter_children ;
565 iface->iter_has_child = tree_model_iter_has_child ;
566 iface->iter_n_children = tree_model_n_children ;
567 iface->iter_nth_child = tree_model_nth_child ;
568 iface->iter_parent = tree_model_iter_parent ;
572 tree_model_iter_has_child (GtkTreeModel *tree_model,
579 tree_model_iter_parent (GtkTreeModel *tree_model,
586 static GtkTreeModelFlags
587 tree_model_get_flags (GtkTreeModel *model)
589 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
591 return GTK_TREE_MODEL_LIST_ONLY;
596 tree_model_n_columns (GtkTreeModel *model)
602 tree_model_column_type (GtkTreeModel *model, gint index)
604 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
608 case DICT_TVM_COL_NAME:
609 return G_TYPE_STRING;
611 case DICT_TVM_COL_VAR:
612 return G_TYPE_POINTER;
615 g_return_val_if_reached ((GType)0);
619 g_assert_not_reached ();
624 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
626 gint *indices, depth;
628 struct variable *var;
630 PsppireDict *dict = PSPPIRE_DICT (model);
632 g_return_val_if_fail (path, FALSE);
634 indices = gtk_tree_path_get_indices (path);
635 depth = gtk_tree_path_get_depth (path);
637 g_return_val_if_fail (depth == 1, FALSE);
641 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
644 iter->user_data = NULL;
648 var = psppire_dict_get_variable (dict, n);
650 g_assert (var_get_dict_index (var) == n);
652 iter->stamp = dict->stamp;
653 iter->user_data = var;
660 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
662 PsppireDict *dict = PSPPIRE_DICT (model);
663 struct variable *var;
666 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
668 if ( iter == NULL || iter->user_data == NULL)
671 var = iter->user_data;
673 idx = var_get_dict_index (var);
675 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
677 iter->user_data = NULL;
682 var = psppire_dict_get_variable (dict, idx + 1);
684 g_assert (var_get_dict_index (var) == idx + 1);
686 iter->user_data = var;
692 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
695 struct variable *var;
696 PsppireDict *dict = PSPPIRE_DICT (model);
698 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
700 var = iter->user_data;
702 path = gtk_tree_path_new ();
703 gtk_tree_path_append_index (path, var_get_dict_index (var));
710 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
711 gint column, GValue *value)
713 struct variable *var;
714 PsppireDict *dict = PSPPIRE_DICT (model);
716 g_return_if_fail (iter->stamp == dict->stamp);
718 var = iter->user_data;
722 case DICT_TVM_COL_NAME:
724 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
725 g_value_init (value, G_TYPE_STRING);
726 g_value_set_string (value, name);
730 case DICT_TVM_COL_VAR:
731 g_value_init (value, G_TYPE_POINTER);
732 g_value_set_pointer (value, var);
735 g_return_if_reached ();
741 tree_model_iter_children (GtkTreeModel *tree_model,
749 tree_model_n_children (GtkTreeModel *model,
752 PsppireDict *dict = PSPPIRE_DICT (model);
755 return psppire_dict_get_var_cnt (dict);
761 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
762 GtkTreeIter *parent, gint n)
766 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
768 dict = PSPPIRE_DICT (model);
773 if ( n >= psppire_dict_get_var_cnt (dict) )
776 iter->stamp = dict->stamp;
777 iter->user_data = psppire_dict_get_variable (dict, n);
779 if ( !iter->user_data)
787 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
790 if ( ! var_is_valid_name (name, false))
793 /* Make sure no other variable has this name */
794 if ( NULL != psppire_dict_lookup_var (dict, name))
797 dict_rename_var (dict->dict, v, name);
804 psppire_dict_get_weight_variable (const PsppireDict *dict)
806 return dict_get_weight (dict->dict);
813 psppire_dict_dump (const PsppireDict *dict)
816 const struct dictionary *d = dict->dict;
818 for (i = 0; i < dict_get_var_cnt (d); ++i)
820 const struct variable *v = psppire_dict_get_variable (dict, i);
821 int di = var_get_dict_index (v);
822 g_print ("\"%s\" idx=%d, fv=%d, size=%d\n",
825 var_get_case_index(v),
826 value_cnt_from_width(var_get_width(v)));