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/dictionary.h>
26 #include <data/missing-values.h>
27 #include <data/value-labels.h>
28 #include <data/variable.h>
31 #include "message-dialog.h"
33 /* --- prototypes --- */
34 static void psppire_dict_class_init (PsppireDictClass *class);
35 static void psppire_dict_init (PsppireDict *dict);
36 static void psppire_dict_finalize (GObject *object);
38 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
41 /* --- variables --- */
42 static GObjectClass *parent_class = NULL;
58 static guint signals [n_SIGNALS];
60 /* --- functions --- */
62 * psppire_dict_get_type:
63 * @returns: the type ID for accelerator groups.
66 psppire_dict_get_type (void)
68 static GType object_type = 0;
72 static const GTypeInfo object_info = {
73 sizeof (PsppireDictClass),
75 (GBaseFinalizeFunc) NULL,
76 (GClassInitFunc) psppire_dict_class_init,
77 NULL, /* class_finalize */
78 NULL, /* class_data */
81 (GInstanceInitFunc) psppire_dict_init,
84 static const GInterfaceInfo tree_model_info = {
85 (GInterfaceInitFunc) dictionary_tree_model_init,
90 object_type = g_type_register_static (G_TYPE_OBJECT,
94 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
105 psppire_dict_class_init (PsppireDictClass *class)
107 GObjectClass *object_class = G_OBJECT_CLASS (class);
109 parent_class = g_type_class_peek_parent (class);
111 object_class->finalize = psppire_dict_finalize;
113 signals [BACKEND_CHANGED] =
114 g_signal_new ("backend-changed",
115 G_TYPE_FROM_CLASS (class),
119 g_cclosure_marshal_VOID__VOID,
124 signals [VARIABLE_CHANGED] =
125 g_signal_new ("variable_changed",
126 G_TYPE_FROM_CLASS (class),
130 g_cclosure_marshal_VOID__INT,
137 signals [VARIABLE_INSERTED] =
138 g_signal_new ("variable_inserted",
139 G_TYPE_FROM_CLASS (class),
143 g_cclosure_marshal_VOID__INT,
149 signals [VARIABLE_DELETED] =
150 g_signal_new ("variable-deleted",
151 G_TYPE_FROM_CLASS (class),
155 marshaller_VOID__INT_INT_INT,
163 signals [VARIABLE_RESIZED] =
164 g_signal_new ("dict-size-changed",
165 G_TYPE_FROM_CLASS (class),
169 gtkextra_VOID__INT_INT,
176 signals [WEIGHT_CHANGED] =
177 g_signal_new ("weight-changed",
178 G_TYPE_FROM_CLASS (class),
182 g_cclosure_marshal_VOID__INT,
188 signals [FILTER_CHANGED] =
189 g_signal_new ("filter-changed",
190 G_TYPE_FROM_CLASS (class),
194 g_cclosure_marshal_VOID__INT,
200 signals [SPLIT_CHANGED] =
201 g_signal_new ("split-changed",
202 G_TYPE_FROM_CLASS (class),
206 g_cclosure_marshal_VOID__VOID,
212 psppire_dict_finalize (GObject *object)
214 PsppireDict *d = PSPPIRE_DICT (object);
216 dict_destroy (d->dict);
218 G_OBJECT_CLASS (parent_class)->finalize (object);
221 /* Pass on callbacks from src/data/dictionary, as
222 signals in the Gtk library */
224 addcb (struct dictionary *d, int idx, void *pd)
226 g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
230 delcb (struct dictionary *d, int dict_idx, int case_idx, int value_cnt,
233 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
234 dict_idx, case_idx, value_cnt );
238 mutcb (struct dictionary *d, int idx, void *pd)
240 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
244 resize_cb (struct dictionary *d, int idx, int delta, void *pd)
246 g_signal_emit (pd, signals [VARIABLE_RESIZED], 0, idx, delta);
250 weight_changed_callback (struct dictionary *d, int idx, void *pd)
252 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
256 filter_changed_callback (struct dictionary *d, int idx, void *pd)
258 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
262 split_changed_callback (struct dictionary *d, void *pd)
264 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
268 static const struct dict_callbacks gui_callbacks =
274 weight_changed_callback,
275 filter_changed_callback,
276 split_changed_callback
280 psppire_dict_init (PsppireDict *psppire_dict)
282 psppire_dict->stamp = g_random_int ();
286 * psppire_dict_new_from_dict:
287 * @returns: a new #PsppireDict object
289 * Creates a new #PsppireDict.
292 psppire_dict_new_from_dict (struct dictionary *d)
294 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
297 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
304 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
306 struct variable *var = dict_get_weight (d);
310 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
312 var = dict_get_filter (d);
313 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
315 split_changed_callback (d, dict);
317 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
319 g_signal_emit (dict, signals [BACKEND_CHANGED], 0);
323 /* Returns a valid name for a new variable in DICT.
324 The return value is statically allocated */
326 auto_generate_var_name (PsppireDict *dict)
329 static gchar name[10];
331 while (g_snprintf (name, 10, "VAR%05d",d++),
332 psppire_dict_lookup_var (dict, name))
338 /* Insert a new variable at posn IDX, with the name NAME.
339 If NAME is null, then a name will be automatically assigned.
342 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
344 struct variable *var ;
345 g_return_if_fail (idx >= 0);
346 g_return_if_fail (d);
347 g_return_if_fail (PSPPIRE_IS_DICT (d));
350 name = auto_generate_var_name (d);
352 var = dict_create_var (d->dict, name, 0);
354 dict_reorder_var (d->dict, var, idx);
357 /* Delete N variables beginning at FIRST */
359 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
362 g_return_if_fail (d);
363 g_return_if_fail (d->dict);
364 g_return_if_fail (PSPPIRE_IS_DICT (d));
366 for (idx = 0 ; idx < n ; ++idx )
368 struct variable *var;
370 /* Do nothing if it's out of bounds */
371 if ( first >= dict_get_var_cnt (d->dict))
374 var = dict_get_var (d->dict, first);
375 dict_delete_var (d->dict, var);
381 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
383 struct variable *var;
385 g_assert (PSPPIRE_IS_DICT (d));
387 if ( ! var_is_valid_name (name, false))
390 if ( idx < dict_get_var_cnt (d->dict))
392 /* This is an existing variable? */
393 var = dict_get_var (d->dict, idx);
394 dict_rename_var (d->dict, var, name);
399 dict_create_var (d->dict, name, 0);
407 /* Return the IDXth variable */
409 psppire_dict_get_variable (const PsppireDict *d, gint idx)
411 g_return_val_if_fail (d, NULL);
412 g_return_val_if_fail (d->dict, NULL);
414 if ( dict_get_var_cnt (d->dict) <= idx )
417 return dict_get_var (d->dict, idx);
421 /* Return the number of variables in the dictionary */
423 psppire_dict_get_var_cnt (const PsppireDict *d)
425 g_return_val_if_fail (d, -1);
426 g_return_val_if_fail (d->dict, -1);
428 return dict_get_var_cnt (d->dict);
432 /* Return the number of `union value's in the dictionary */
434 psppire_dict_get_value_cnt (const PsppireDict *d)
436 g_return_val_if_fail (d, -1);
437 g_return_val_if_fail (d->dict, -1);
439 return dict_get_next_value_idx (d->dict);
443 /* Return a variable by name.
444 Return NULL if it doesn't exist
447 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
449 g_return_val_if_fail (d, NULL);
450 g_return_val_if_fail (d->dict, NULL);
452 return dict_lookup_var (d->dict, name);
455 /* Clears the contents of D */
457 psppire_dict_clear (PsppireDict *d)
459 g_return_if_fail (d);
460 g_return_if_fail (d->dict);
463 dict_clear (d->dict);
468 /* Return true is NAME would be a valid name of a variable to add to the
469 dictionary. False otherwise.
470 If REPORT is true, then invalid names will be reported as such as errors
473 psppire_dict_check_name (const PsppireDict *dict,
474 const gchar *name, gboolean report)
476 if ( ! var_is_valid_name (name, report ) )
479 if (psppire_dict_lookup_var (dict, name))
482 msg (ME,"Duplicate variable name.");
491 psppire_dict_get_next_value_idx (const PsppireDict *dict)
493 return dict_get_next_value_idx (dict->dict);
498 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
499 gint old_size, gint new_size)
502 g_return_if_fail (d);
503 g_return_if_fail (d->dict);
505 if ( old_size == new_size )
508 fv = var_get_case_index (pv);
510 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
512 new_size - old_size );
516 /* Tree Model Stuff */
518 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
520 static gint tree_model_n_columns (GtkTreeModel *model);
522 static GType tree_model_column_type (GtkTreeModel *model, gint index);
524 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
527 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
529 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
532 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
533 gint column, GValue *value);
535 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
536 GtkTreeIter *parent, gint n);
538 static gint tree_model_n_children (GtkTreeModel *tree_model,
541 static gboolean tree_model_iter_children (GtkTreeModel *,
545 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
549 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
553 dictionary_tree_model_init (GtkTreeModelIface *iface)
555 iface->get_flags = tree_model_get_flags;
556 iface->get_n_columns = tree_model_n_columns;
557 iface->get_column_type = tree_model_column_type;
558 iface->get_iter = tree_model_get_iter;
559 iface->iter_next = tree_model_iter_next;
560 iface->get_path = tree_model_get_path;
561 iface->get_value = tree_model_get_value;
563 iface->iter_children = tree_model_iter_children ;
564 iface->iter_has_child = tree_model_iter_has_child ;
565 iface->iter_n_children = tree_model_n_children ;
566 iface->iter_nth_child = tree_model_nth_child ;
567 iface->iter_parent = tree_model_iter_parent ;
571 tree_model_iter_has_child (GtkTreeModel *tree_model,
578 tree_model_iter_parent (GtkTreeModel *tree_model,
585 static GtkTreeModelFlags
586 tree_model_get_flags (GtkTreeModel *model)
588 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
590 return GTK_TREE_MODEL_LIST_ONLY;
595 tree_model_n_columns (GtkTreeModel *model)
601 tree_model_column_type (GtkTreeModel *model, gint index)
603 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
607 case DICT_TVM_COL_NAME:
608 return G_TYPE_STRING;
610 case DICT_TVM_COL_VAR:
611 return G_TYPE_POINTER;
614 g_return_val_if_reached ((GType)0);
618 g_assert_not_reached ();
623 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
625 gint *indices, depth;
627 struct variable *var;
629 PsppireDict *dict = PSPPIRE_DICT (model);
631 g_return_val_if_fail (path, FALSE);
633 indices = gtk_tree_path_get_indices (path);
634 depth = gtk_tree_path_get_depth (path);
636 g_return_val_if_fail (depth == 1, FALSE);
640 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
643 iter->user_data = NULL;
647 var = psppire_dict_get_variable (dict, n);
649 g_assert (var_get_dict_index (var) == n);
651 iter->stamp = dict->stamp;
652 iter->user_data = var;
659 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
661 PsppireDict *dict = PSPPIRE_DICT (model);
662 struct variable *var;
665 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
667 if ( iter == NULL || iter->user_data == NULL)
670 var = iter->user_data;
672 idx = var_get_dict_index (var);
674 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
676 iter->user_data = NULL;
681 var = psppire_dict_get_variable (dict, idx + 1);
683 g_assert (var_get_dict_index (var) == idx + 1);
685 iter->user_data = var;
691 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
694 struct variable *var;
695 PsppireDict *dict = PSPPIRE_DICT (model);
697 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
699 var = iter->user_data;
701 path = gtk_tree_path_new ();
702 gtk_tree_path_append_index (path, var_get_dict_index (var));
709 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
710 gint column, GValue *value)
712 struct variable *var;
713 PsppireDict *dict = PSPPIRE_DICT (model);
715 g_return_if_fail (iter->stamp == dict->stamp);
717 var = iter->user_data;
721 case DICT_TVM_COL_NAME:
723 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
724 g_value_init (value, G_TYPE_STRING);
725 g_value_set_string (value, name);
729 case DICT_TVM_COL_VAR:
730 g_value_init (value, G_TYPE_POINTER);
731 g_value_set_pointer (value, var);
734 g_return_if_reached ();
740 tree_model_iter_children (GtkTreeModel *tree_model,
748 tree_model_n_children (GtkTreeModel *model,
751 PsppireDict *dict = PSPPIRE_DICT (model);
754 return psppire_dict_get_var_cnt (dict);
760 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
761 GtkTreeIter *parent, gint n)
765 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
767 dict = PSPPIRE_DICT (model);
772 if ( n >= psppire_dict_get_var_cnt (dict) )
775 iter->stamp = dict->stamp;
776 iter->user_data = psppire_dict_get_variable (dict, n);
778 if ( !iter->user_data)
786 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
789 if ( ! var_is_valid_name (name, false))
792 /* Make sure no other variable has this name */
793 if ( NULL != psppire_dict_lookup_var (dict, name))
796 dict_rename_var (dict->dict, v, name);
803 psppire_dict_get_weight_variable (const PsppireDict *dict)
805 return dict_get_weight (dict->dict);
812 psppire_dict_dump (const PsppireDict *dict)
815 const struct dictionary *d = dict->dict;
817 for (i = 0; i < dict_get_var_cnt (d); ++i)
819 const struct variable *v = psppire_dict_get_variable (dict, i);
820 int di = var_get_dict_index (v);
821 g_print ("\"%s\" idx=%d, fv=%d, size=%d\n",
824 var_get_case_index(v),
825 value_cnt_from_width(var_get_width(v)));