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-object.h"
28 #include "psppire-dict.h"
29 #include <data/format.h>
30 #include <data/dictionary.h>
31 #include <data/missing-values.h>
32 #include <data/value-labels.h>
33 #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 signal[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_PSPPIRE_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 signal[VARIABLE_CHANGED] =
113 g_signal_new ("variable_changed",
114 G_TYPE_FROM_CLASS (class),
118 g_cclosure_marshal_VOID__INT,
125 signal[VARIABLE_INSERTED] =
126 g_signal_new ("variable_inserted",
127 G_TYPE_FROM_CLASS (class),
131 g_cclosure_marshal_VOID__INT,
137 signal[VARIABLES_DELETED] =
138 g_signal_new ("variables_deleted",
139 G_TYPE_FROM_CLASS (class),
143 gtkextra_VOID__INT_INT,
150 signal[VARIABLE_RESIZED] =
151 g_signal_new ("dict-size-changed",
152 G_TYPE_FROM_CLASS (class),
156 gtkextra_VOID__INT_INT,
163 signal [WEIGHT_CHANGED] =
164 g_signal_new ("weight-changed",
165 G_TYPE_FROM_CLASS (class),
169 g_cclosure_marshal_VOID__INT,
175 signal [FILTER_CHANGED] =
176 g_signal_new ("filter-changed",
177 G_TYPE_FROM_CLASS (class),
181 g_cclosure_marshal_VOID__INT,
187 signal [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, signal[VARIABLE_INSERTED], 0, idx);
217 delcb (struct dictionary *d, int idx, void *pd)
219 g_signal_emit (pd, signal[VARIABLES_DELETED], 0, idx, 1);
223 mutcb (struct dictionary *d, int idx, void *pd)
225 g_signal_emit (pd, signal[VARIABLE_CHANGED], 0, idx);
229 weight_changed_callback (struct dictionary *d, int idx, void *pd)
231 g_signal_emit (pd, signal [WEIGHT_CHANGED], 0, idx);
235 filter_changed_callback (struct dictionary *d, int idx, void *pd)
237 g_signal_emit (pd, signal [FILTER_CHANGED], 0, idx);
241 split_changed_callback (struct dictionary *d, void *pd)
243 g_signal_emit (pd, signal [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)
288 /* Returns a valid name for a new variable in DICT.
289 The return value is statically allocated */
291 auto_generate_var_name (PsppireDict *dict)
294 static gchar name[10];
296 while (g_snprintf (name, 10, "VAR%05d",d++),
297 psppire_dict_lookup_var (dict, name))
303 /* Insert a new variable at posn IDX, with the name NAME.
304 If NAME is null, then a name will be automatically assigned.
307 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
309 struct variable *var ;
310 g_return_if_fail (idx >= 0);
311 g_return_if_fail (d);
312 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
315 name = auto_generate_var_name (d);
317 var = dict_create_var (d->dict, name, 0);
319 dict_reorder_var (d->dict, var, idx);
322 /* Delete N variables beginning at FIRST */
324 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
327 g_return_if_fail (d);
328 g_return_if_fail (d->dict);
329 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
331 for (idx = 0 ; idx < n ; ++idx )
333 struct variable *var;
335 /* Do nothing if it's out of bounds */
336 if ( first >= dict_get_var_cnt (d->dict))
339 var = dict_get_var (d->dict, first);
340 dict_delete_var (d->dict, var);
342 dict_compact_values (d->dict);
347 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
349 struct variable *var;
351 g_assert (G_IS_PSPPIRE_DICT (d));
354 if ( idx < dict_get_var_cnt (d->dict))
356 /* This is an existing variable? */
357 var = dict_get_var (d->dict, idx);
358 dict_rename_var (d->dict, var, name);
363 dict_create_var (d->dict, name, 0);
369 /* Return the IDXth variable */
371 psppire_dict_get_variable (const PsppireDict *d, gint idx)
373 g_return_val_if_fail (d, NULL);
374 g_return_val_if_fail (d->dict, NULL);
376 if ( dict_get_var_cnt (d->dict) <= idx )
379 return dict_get_var (d->dict, idx);
383 /* Return the number of variables in the dictionary */
385 psppire_dict_get_var_cnt (const PsppireDict *d)
387 g_return_val_if_fail (d, -1);
388 g_return_val_if_fail (d->dict, -1);
390 return dict_get_var_cnt (d->dict);
394 /* Return a variable by name.
395 Return NULL if it doesn't exist
398 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
400 g_return_val_if_fail (d, NULL);
401 g_return_val_if_fail (d->dict, NULL);
403 return dict_lookup_var (d->dict, name);
406 /* Clears the contents of D */
408 psppire_dict_clear (PsppireDict *d)
410 g_return_if_fail (d);
411 g_return_if_fail (d->dict);
414 dict_clear (d->dict);
419 /* Return true is NAME would be a valid name of a variable to add to the
420 dictionary. False otherwise.
421 If REPORT is true, then invalid names will be reported as such as errors
424 psppire_dict_check_name (const PsppireDict *dict,
425 const gchar *name, gboolean report)
427 if ( ! var_is_valid_name (name, report ) )
430 if (psppire_dict_lookup_var (dict, name))
433 msg (ME,"Duplicate variable name.");
442 psppire_dict_get_next_value_idx (const PsppireDict *dict)
444 return dict_get_next_value_idx (dict->dict);
449 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
450 gint old_size, gint new_size)
453 g_return_if_fail (d);
454 g_return_if_fail (d->dict);
456 if ( old_size == new_size )
459 dict_compact_values (d->dict);
461 fv = var_get_case_index (pv);
463 g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
465 new_size - old_size );
469 /* Tree Model Stuff */
471 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
473 static gint tree_model_n_columns (GtkTreeModel *model);
475 static GType tree_model_column_type (GtkTreeModel *model, gint index);
477 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
480 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
482 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
485 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
486 gint column, GValue *value);
488 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
489 GtkTreeIter *parent, gint n);
493 dictionary_tree_model_init (GtkTreeModelIface *iface)
495 iface->get_flags = tree_model_get_flags;
496 iface->get_n_columns = tree_model_n_columns;
497 iface->get_column_type = tree_model_column_type;
498 iface->get_iter = tree_model_get_iter;
499 iface->iter_next = tree_model_iter_next;
500 iface->get_path = tree_model_get_path;
501 iface->get_value = tree_model_get_value;
503 iface->iter_children = 0;
504 iface->iter_has_child =0;
505 iface->iter_n_children =0;
506 iface->iter_nth_child = tree_model_nth_child ;
507 iface->iter_parent =0;
510 static GtkTreeModelFlags
511 tree_model_get_flags (GtkTreeModel *model)
513 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
515 return GTK_TREE_MODEL_LIST_ONLY;
520 tree_model_n_columns (GtkTreeModel *model)
526 tree_model_column_type (GtkTreeModel *model, gint index)
528 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
532 case DICT_TVM_COL_NAME:
533 return G_TYPE_STRING;
535 case DICT_TVM_COL_VAR:
536 return G_TYPE_POINTER;
539 g_return_val_if_reached ((GType)0);
543 g_assert_not_reached ();
548 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
550 gint *indices, depth;
552 struct variable *variable;
554 PsppireDict *dict = PSPPIRE_DICT (model);
556 g_return_val_if_fail (path, FALSE);
558 indices = gtk_tree_path_get_indices (path);
559 depth = gtk_tree_path_get_depth (path);
561 g_return_val_if_fail (depth == 1, FALSE);
565 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
568 variable = dict_get_var (dict->dict, n);
570 g_assert (var_get_dict_index (variable) == n);
572 iter->stamp = dict->stamp;
573 iter->user_data = variable;
580 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
582 PsppireDict *dict = PSPPIRE_DICT (model);
583 struct variable *variable;
586 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
588 if ( iter == NULL || iter->user_data == NULL)
591 variable = (struct variable *) iter->user_data;
593 idx = var_get_dict_index (variable);
595 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
598 variable = psppire_dict_get_variable (dict, idx + 1);
600 g_assert (var_get_dict_index (variable) == idx + 1);
602 iter->user_data = variable;
608 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
611 struct variable *variable;
612 PsppireDict *dict = PSPPIRE_DICT (model);
614 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
616 variable = (struct variable *) iter->user_data;
618 path = gtk_tree_path_new ();
619 gtk_tree_path_append_index (path, var_get_dict_index (variable));
626 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
627 gint column, GValue *value)
629 struct variable *variable;
630 PsppireDict *dict = PSPPIRE_DICT (model);
632 g_return_if_fail (iter->stamp == dict->stamp);
634 variable = (struct variable *) iter->user_data;
638 case DICT_TVM_COL_NAME:
639 g_value_init (value, G_TYPE_STRING);
640 g_value_set_string (value, var_get_name (variable));
642 case DICT_TVM_COL_VAR:
643 g_value_init (value, G_TYPE_POINTER);
644 g_value_set_pointer (value, variable);
647 g_return_if_reached ();
654 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
655 GtkTreeIter *parent, gint n)
658 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
660 dict = PSPPIRE_DICT (model);
665 if ( n >= psppire_dict_get_var_cnt (dict) )
668 iter->stamp = dict->stamp;
669 iter->user_data = psppire_dict_get_variable (dict, n);
671 if ( !iter->user_data)
680 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
683 dict_rename_var (dict->dict, v, text);
688 psppire_dict_set_weight_variable (PsppireDict *dict, struct variable *v)
690 g_return_if_fail (v == NULL || var_is_numeric (v));
691 dict_set_weight (dict->dict, v);
696 psppire_dict_get_weight_variable (const PsppireDict *dict)
698 return dict_get_weight (dict->dict);