2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2006, 2007 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <gtksheet/gtkextra-marshal.h>
27 #include "psppire-dict.h"
28 #include <data/format.h>
29 #include <data/dictionary.h>
30 #include <data/missing-values.h>
31 #include <data/value-labels.h>
32 #include <data/variable.h>
35 #include "message-dialog.h"
37 /* --- prototypes --- */
38 static void psppire_dict_class_init (PsppireDictClass *class);
39 static void psppire_dict_init (PsppireDict *dict);
40 static void psppire_dict_finalize (GObject *object);
42 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
45 /* --- variables --- */
46 static GObjectClass *parent_class = NULL;
48 enum {VARIABLE_CHANGED,
57 static guint signals [n_SIGNALS];
59 /* --- functions --- */
61 * psppire_dict_get_type:
62 * @returns: the type ID for accelerator groups.
65 psppire_dict_get_type (void)
67 static GType object_type = 0;
71 static const GTypeInfo object_info = {
72 sizeof (PsppireDictClass),
74 (GBaseFinalizeFunc) NULL,
75 (GClassInitFunc) psppire_dict_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
80 (GInstanceInitFunc) psppire_dict_init,
83 static const GInterfaceInfo tree_model_info = {
84 (GInterfaceInitFunc) dictionary_tree_model_init,
89 object_type = g_type_register_static (G_TYPE_OBJECT,
93 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
104 psppire_dict_class_init (PsppireDictClass *class)
106 GObjectClass *object_class = G_OBJECT_CLASS (class);
108 parent_class = g_type_class_peek_parent (class);
110 object_class->finalize = psppire_dict_finalize;
112 signals [VARIABLE_CHANGED] =
113 g_signal_new ("variable_changed",
114 G_TYPE_FROM_CLASS (class),
118 g_cclosure_marshal_VOID__INT,
125 signals [VARIABLE_INSERTED] =
126 g_signal_new ("variable_inserted",
127 G_TYPE_FROM_CLASS (class),
131 g_cclosure_marshal_VOID__INT,
137 signals [VARIABLES_DELETED] =
138 g_signal_new ("variables_deleted",
139 G_TYPE_FROM_CLASS (class),
143 gtkextra_VOID__INT_INT,
150 signals [VARIABLE_RESIZED] =
151 g_signal_new ("dict-size-changed",
152 G_TYPE_FROM_CLASS (class),
156 gtkextra_VOID__INT_INT,
163 signals [WEIGHT_CHANGED] =
164 g_signal_new ("weight-changed",
165 G_TYPE_FROM_CLASS (class),
169 g_cclosure_marshal_VOID__INT,
175 signals [FILTER_CHANGED] =
176 g_signal_new ("filter-changed",
177 G_TYPE_FROM_CLASS (class),
181 g_cclosure_marshal_VOID__INT,
187 signals [SPLIT_CHANGED] =
188 g_signal_new ("split-changed",
189 G_TYPE_FROM_CLASS (class),
193 g_cclosure_marshal_VOID__VOID,
199 psppire_dict_finalize (GObject *object)
201 PsppireDict *d = PSPPIRE_DICT (object);
203 dict_destroy (d->dict);
205 G_OBJECT_CLASS (parent_class)->finalize (object);
208 /* Pass on callbacks from src/data/dictionary, as
209 signals in the Gtk library */
211 addcb (struct dictionary *d, int idx, void *pd)
213 g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
217 delcb (struct dictionary *d, int idx, void *pd)
219 g_signal_emit (pd, signals [VARIABLES_DELETED], 0, idx, 1);
223 mutcb (struct dictionary *d, int idx, void *pd)
225 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
229 weight_changed_callback (struct dictionary *d, int idx, void *pd)
231 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
235 filter_changed_callback (struct dictionary *d, int idx, void *pd)
237 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
241 split_changed_callback (struct dictionary *d, void *pd)
243 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
247 static const struct dict_callbacks gui_callbacks =
252 weight_changed_callback,
253 filter_changed_callback,
254 split_changed_callback
258 psppire_dict_init (PsppireDict *psppire_dict)
260 psppire_dict->stamp = g_random_int ();
264 * psppire_dict_new_from_dict:
265 * @returns: a new #PsppireDict object
267 * Creates a new #PsppireDict.
270 psppire_dict_new_from_dict (struct dictionary *d)
272 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
275 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
282 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
284 struct variable *var = dict_get_weight (d);
287 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
289 var = dict_get_filter (d);
290 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
292 split_changed_callback (d, dict);
294 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
298 /* Returns a valid name for a new variable in DICT.
299 The return value is statically allocated */
301 auto_generate_var_name (PsppireDict *dict)
304 static gchar name[10];
306 while (g_snprintf (name, 10, "VAR%05d",d++),
307 psppire_dict_lookup_var (dict, name))
313 /* Insert a new variable at posn IDX, with the name NAME.
314 If NAME is null, then a name will be automatically assigned.
317 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
319 struct variable *var ;
320 g_return_if_fail (idx >= 0);
321 g_return_if_fail (d);
322 g_return_if_fail (PSPPIRE_IS_DICT (d));
325 name = auto_generate_var_name (d);
327 var = dict_create_var (d->dict, name, 0);
329 dict_reorder_var (d->dict, var, idx);
332 /* Delete N variables beginning at FIRST */
334 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
337 g_return_if_fail (d);
338 g_return_if_fail (d->dict);
339 g_return_if_fail (PSPPIRE_IS_DICT (d));
341 for (idx = 0 ; idx < n ; ++idx )
343 struct variable *var;
345 /* Do nothing if it's out of bounds */
346 if ( first >= dict_get_var_cnt (d->dict))
349 var = dict_get_var (d->dict, first);
350 dict_delete_var (d->dict, var);
352 dict_compact_values (d->dict);
357 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
359 struct variable *var;
361 g_assert (PSPPIRE_IS_DICT (d));
363 if ( ! var_is_valid_name (name, false))
366 if ( idx < dict_get_var_cnt (d->dict))
368 /* This is an existing variable? */
369 var = dict_get_var (d->dict, idx);
370 dict_rename_var (d->dict, var, name);
375 dict_create_var (d->dict, name, 0);
383 /* Return the IDXth variable */
385 psppire_dict_get_variable (const PsppireDict *d, gint idx)
387 g_return_val_if_fail (d, NULL);
388 g_return_val_if_fail (d->dict, NULL);
390 if ( dict_get_var_cnt (d->dict) <= idx )
393 return dict_get_var (d->dict, idx);
397 /* Return the number of variables in the dictionary */
399 psppire_dict_get_var_cnt (const PsppireDict *d)
401 g_return_val_if_fail (d, -1);
402 g_return_val_if_fail (d->dict, -1);
404 return dict_get_var_cnt (d->dict);
408 /* Return a variable by name.
409 Return NULL if it doesn't exist
412 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
414 g_return_val_if_fail (d, NULL);
415 g_return_val_if_fail (d->dict, NULL);
417 return dict_lookup_var (d->dict, name);
420 /* Clears the contents of D */
422 psppire_dict_clear (PsppireDict *d)
424 g_return_if_fail (d);
425 g_return_if_fail (d->dict);
428 dict_clear (d->dict);
433 /* Return true is NAME would be a valid name of a variable to add to the
434 dictionary. False otherwise.
435 If REPORT is true, then invalid names will be reported as such as errors
438 psppire_dict_check_name (const PsppireDict *dict,
439 const gchar *name, gboolean report)
441 if ( ! var_is_valid_name (name, report ) )
444 if (psppire_dict_lookup_var (dict, name))
447 msg (ME,"Duplicate variable name.");
456 psppire_dict_get_next_value_idx (const PsppireDict *dict)
458 return dict_get_next_value_idx (dict->dict);
463 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
464 gint old_size, gint new_size)
467 g_return_if_fail (d);
468 g_return_if_fail (d->dict);
470 if ( old_size == new_size )
473 dict_compact_values (d->dict);
475 fv = var_get_case_index (pv);
477 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
479 new_size - old_size );
483 /* Tree Model Stuff */
485 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
487 static gint tree_model_n_columns (GtkTreeModel *model);
489 static GType tree_model_column_type (GtkTreeModel *model, gint index);
491 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
494 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
496 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
499 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
500 gint column, GValue *value);
502 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
503 GtkTreeIter *parent, gint n);
505 static gint tree_model_n_children (GtkTreeModel *tree_model,
508 static gboolean tree_model_iter_children (GtkTreeModel *,
512 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
516 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
520 dictionary_tree_model_init (GtkTreeModelIface *iface)
522 iface->get_flags = tree_model_get_flags;
523 iface->get_n_columns = tree_model_n_columns;
524 iface->get_column_type = tree_model_column_type;
525 iface->get_iter = tree_model_get_iter;
526 iface->iter_next = tree_model_iter_next;
527 iface->get_path = tree_model_get_path;
528 iface->get_value = tree_model_get_value;
530 iface->iter_children = tree_model_iter_children ;
531 iface->iter_has_child = tree_model_iter_has_child ;
532 iface->iter_n_children = tree_model_n_children ;
533 iface->iter_nth_child = tree_model_nth_child ;
534 iface->iter_parent = tree_model_iter_parent ;
538 tree_model_iter_has_child (GtkTreeModel *tree_model,
545 tree_model_iter_parent (GtkTreeModel *tree_model,
552 static GtkTreeModelFlags
553 tree_model_get_flags (GtkTreeModel *model)
555 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
557 return GTK_TREE_MODEL_LIST_ONLY;
562 tree_model_n_columns (GtkTreeModel *model)
568 tree_model_column_type (GtkTreeModel *model, gint index)
570 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
574 case DICT_TVM_COL_NAME:
575 return G_TYPE_STRING;
577 case DICT_TVM_COL_VAR:
578 return G_TYPE_POINTER;
581 g_return_val_if_reached ((GType)0);
585 g_assert_not_reached ();
590 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
592 gint *indices, depth;
594 struct variable *var;
596 PsppireDict *dict = PSPPIRE_DICT (model);
598 g_return_val_if_fail (path, FALSE);
600 indices = gtk_tree_path_get_indices (path);
601 depth = gtk_tree_path_get_depth (path);
603 g_return_val_if_fail (depth == 1, FALSE);
607 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
610 iter->user_data = NULL;
614 var = psppire_dict_get_variable (dict, n);
616 g_assert (var_get_dict_index (var) == n);
618 iter->stamp = dict->stamp;
619 iter->user_data = var;
626 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
628 PsppireDict *dict = PSPPIRE_DICT (model);
629 struct variable *var;
632 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
634 if ( iter == NULL || iter->user_data == NULL)
637 var = iter->user_data;
639 idx = var_get_dict_index (var);
641 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
643 iter->user_data = NULL;
648 var = psppire_dict_get_variable (dict, idx + 1);
650 g_assert (var_get_dict_index (var) == idx + 1);
652 iter->user_data = var;
658 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
661 struct variable *var;
662 PsppireDict *dict = PSPPIRE_DICT (model);
664 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
666 var = iter->user_data;
668 path = gtk_tree_path_new ();
669 gtk_tree_path_append_index (path, var_get_dict_index (var));
676 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
677 gint column, GValue *value)
679 struct variable *var;
680 PsppireDict *dict = PSPPIRE_DICT (model);
682 g_return_if_fail (iter->stamp == dict->stamp);
684 var = iter->user_data;
688 case DICT_TVM_COL_NAME:
690 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
691 g_value_init (value, G_TYPE_STRING);
692 g_value_set_string (value, name);
696 case DICT_TVM_COL_VAR:
697 g_value_init (value, G_TYPE_POINTER);
698 g_value_set_pointer (value, var);
701 g_return_if_reached ();
707 tree_model_iter_children (GtkTreeModel *tree_model,
715 tree_model_n_children (GtkTreeModel *model,
718 PsppireDict *dict = PSPPIRE_DICT (model);
721 return psppire_dict_get_var_cnt (dict);
727 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
728 GtkTreeIter *parent, gint n)
732 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
734 dict = PSPPIRE_DICT (model);
739 if ( n >= psppire_dict_get_var_cnt (dict) )
742 iter->stamp = dict->stamp;
743 iter->user_data = psppire_dict_get_variable (dict, n);
745 if ( !iter->user_data)
753 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
756 if ( ! var_is_valid_name (name, false))
759 dict_rename_var (dict->dict, v, name);
766 psppire_dict_get_weight_variable (const PsppireDict *dict)
768 return dict_get_weight (dict->dict);