2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2006 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,
55 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,
176 psppire_dict_finalize (GObject *object)
178 PsppireDict *d = PSPPIRE_DICT (object);
180 dict_destroy (d->dict);
182 G_OBJECT_CLASS (parent_class)->finalize (object);
185 /* Pass on callbacks from src/data/dictionary, as
186 signals in the Gtk library */
188 addcb (struct dictionary *d, int idx, void *pd)
190 g_signal_emit (pd, signal[VARIABLE_INSERTED], 0, idx);
194 delcb (struct dictionary *d, int idx, void *pd)
196 g_signal_emit (pd, signal[VARIABLES_DELETED], 0, idx, 1);
200 mutcb (struct dictionary *d, int idx, void *pd)
202 g_signal_emit (pd, signal[VARIABLE_CHANGED], 0, idx);
206 weight_changed_callback (struct dictionary *d, int idx, void *pd)
208 g_signal_emit (pd, signal [WEIGHT_CHANGED], 0, idx);
212 static const struct dict_callbacks gui_callbacks =
217 weight_changed_callback
221 psppire_dict_init (PsppireDict *psppire_dict)
223 psppire_dict->stamp = g_random_int ();
227 * psppire_dict_new_from_dict:
228 * @returns: a new #PsppireDict object
230 * Creates a new #PsppireDict.
233 psppire_dict_new_from_dict (struct dictionary *d)
235 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
238 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
245 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
251 /* Returns a valid name for a new variable in DICT.
252 The return value is statically allocated */
254 auto_generate_var_name (PsppireDict *dict)
257 static gchar name[10];
259 while (g_snprintf (name, 10, "VAR%05d",d++),
260 psppire_dict_lookup_var (dict, name))
266 /* Insert a new variable at posn IDX, with the name NAME.
267 If NAME is null, then a name will be automatically assigned.
270 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
272 struct variable *var ;
273 g_return_if_fail (idx >= 0);
274 g_return_if_fail (d);
275 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
278 name = auto_generate_var_name (d);
280 var = dict_create_var (d->dict, name, 0);
282 dict_reorder_var (d->dict, var, idx);
285 /* Delete N variables beginning at FIRST */
287 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
290 g_return_if_fail (d);
291 g_return_if_fail (d->dict);
292 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
294 for (idx = 0 ; idx < n ; ++idx )
296 struct variable *var;
298 /* Do nothing if it's out of bounds */
299 if ( first >= dict_get_var_cnt (d->dict))
302 var = dict_get_var (d->dict, first);
303 dict_delete_var (d->dict, var);
305 dict_compact_values (d->dict);
310 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
312 struct variable *var;
314 g_assert (G_IS_PSPPIRE_DICT (d));
317 if ( idx < dict_get_var_cnt (d->dict))
319 /* This is an existing variable? */
320 var = dict_get_var (d->dict, idx);
321 dict_rename_var (d->dict, var, name);
326 dict_create_var (d->dict, name, 0);
332 /* Return the IDXth variable */
334 psppire_dict_get_variable (PsppireDict *d, gint idx)
336 g_return_val_if_fail (d, NULL);
337 g_return_val_if_fail (d->dict, NULL);
339 if ( dict_get_var_cnt (d->dict) <= idx )
342 return dict_get_var (d->dict, idx);
346 /* Return the number of variables in the dictionary */
348 psppire_dict_get_var_cnt (const PsppireDict *d)
350 g_return_val_if_fail (d, -1);
351 g_return_val_if_fail (d->dict, -1);
353 return dict_get_var_cnt (d->dict);
357 /* Return a variable by name.
358 Return NULL if it doesn't exist
361 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
363 g_return_val_if_fail (d, NULL);
364 g_return_val_if_fail (d->dict, NULL);
366 return dict_lookup_var (d->dict, name);
369 /* Clears the contents of D */
371 psppire_dict_clear (PsppireDict *d)
373 g_return_if_fail (d);
374 g_return_if_fail (d->dict);
377 dict_clear (d->dict);
382 /* Return true is NAME would be a valid name of a variable to add to the
383 dictionary. False otherwise.
384 If REPORT is true, then invalid names will be reported as such as errors
387 psppire_dict_check_name (const PsppireDict *dict,
388 const gchar *name, gboolean report)
390 if ( ! var_is_valid_name (name, report ) )
393 if (psppire_dict_lookup_var (dict, name))
396 msg (ME,"Duplicate variable name.");
405 psppire_dict_get_next_value_idx (const PsppireDict *dict)
407 return dict_get_next_value_idx (dict->dict);
412 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
413 gint old_size, gint new_size)
416 g_return_if_fail (d);
417 g_return_if_fail (d->dict);
419 if ( old_size == new_size )
422 dict_compact_values (d->dict);
424 fv = var_get_case_index (pv);
426 g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
428 new_size - old_size );
432 /* Tree Model Stuff */
434 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
436 static gint tree_model_n_columns (GtkTreeModel *model);
438 static GType tree_model_column_type (GtkTreeModel *model, gint index);
440 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
443 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
445 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
448 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
449 gint column, GValue *value);
451 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
452 GtkTreeIter *parent, gint n);
456 dictionary_tree_model_init (GtkTreeModelIface *iface)
458 iface->get_flags = tree_model_get_flags;
459 iface->get_n_columns = tree_model_n_columns;
460 iface->get_column_type = tree_model_column_type;
461 iface->get_iter = tree_model_get_iter;
462 iface->iter_next = tree_model_iter_next;
463 iface->get_path = tree_model_get_path;
464 iface->get_value = tree_model_get_value;
466 iface->iter_children = 0;
467 iface->iter_has_child =0;
468 iface->iter_n_children =0;
469 iface->iter_nth_child = tree_model_nth_child ;
470 iface->iter_parent =0;
473 static GtkTreeModelFlags
474 tree_model_get_flags (GtkTreeModel *model)
476 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
478 return GTK_TREE_MODEL_LIST_ONLY;
483 tree_model_n_columns (GtkTreeModel *model)
489 tree_model_column_type (GtkTreeModel *model, gint index)
491 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
495 case DICT_TVM_COL_NAME:
496 return G_TYPE_STRING;
498 case DICT_TVM_COL_VAR:
499 return G_TYPE_POINTER;
502 g_return_val_if_reached ((GType)0);
506 g_assert_not_reached ();
511 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
513 gint *indices, depth;
515 struct variable *variable;
517 PsppireDict *dict = PSPPIRE_DICT (model);
519 g_return_val_if_fail (path, FALSE);
521 indices = gtk_tree_path_get_indices (path);
522 depth = gtk_tree_path_get_depth (path);
524 g_return_val_if_fail (depth == 1, FALSE);
528 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
531 variable = dict_get_var (dict->dict, n);
533 g_assert (var_get_dict_index (variable) == n);
535 iter->stamp = dict->stamp;
536 iter->user_data = variable;
543 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
545 PsppireDict *dict = PSPPIRE_DICT (model);
546 struct variable *variable;
549 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
551 if ( iter == NULL || iter->user_data == NULL)
554 variable = (struct variable *) iter->user_data;
556 idx = var_get_dict_index (variable);
558 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
561 variable = psppire_dict_get_variable (dict, idx + 1);
563 g_assert (var_get_dict_index (variable) == idx + 1);
565 iter->user_data = variable;
571 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
574 struct variable *variable;
575 PsppireDict *dict = PSPPIRE_DICT (model);
577 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
579 variable = (struct variable *) iter->user_data;
581 path = gtk_tree_path_new ();
582 gtk_tree_path_append_index (path, var_get_dict_index (variable));
589 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
590 gint column, GValue *value)
592 struct variable *variable;
593 PsppireDict *dict = PSPPIRE_DICT (model);
595 g_return_if_fail (iter->stamp == dict->stamp);
597 variable = (struct variable *) iter->user_data;
601 case DICT_TVM_COL_NAME:
602 g_value_init (value, G_TYPE_STRING);
603 g_value_set_string (value, var_get_name (variable));
605 case DICT_TVM_COL_VAR:
606 g_value_init (value, G_TYPE_POINTER);
607 g_value_set_pointer (value, variable);
610 g_return_if_reached ();
617 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
618 GtkTreeIter *parent, gint n)
621 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
623 dict = PSPPIRE_DICT (model);
628 if ( n >= psppire_dict_get_var_cnt (dict) )
631 iter->stamp = dict->stamp;
632 iter->user_data = psppire_dict_get_variable (dict, n);
634 if ( !iter->user_data)
643 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
646 dict_rename_var (dict->dict, v, text);