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,
54 static guint signal[n_SIGNALS];
58 /* --- functions --- */
60 * psppire_dict_get_type:
61 * @returns: the type ID for accelerator groups.
64 psppire_dict_get_type (void)
66 static GType object_type = 0;
70 static const GTypeInfo object_info = {
71 sizeof (PsppireDictClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) psppire_dict_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) psppire_dict_init,
82 static const GInterfaceInfo tree_model_info = {
83 (GInterfaceInitFunc) dictionary_tree_model_init,
88 object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT,
92 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
103 psppire_dict_class_init (PsppireDictClass *class)
105 GObjectClass *object_class = G_OBJECT_CLASS (class);
107 parent_class = g_type_class_peek_parent (class);
109 object_class->finalize = psppire_dict_finalize;
111 signal[VARIABLE_CHANGED] =
112 g_signal_new ("variable_changed",
113 G_TYPE_FROM_CLASS (class),
117 g_cclosure_marshal_VOID__INT,
124 signal[VARIABLE_INSERTED] =
125 g_signal_new ("variable_inserted",
126 G_TYPE_FROM_CLASS (class),
130 g_cclosure_marshal_VOID__INT,
136 signal[VARIABLES_DELETED] =
137 g_signal_new ("variables_deleted",
138 G_TYPE_FROM_CLASS (class),
142 gtkextra_VOID__INT_INT,
149 signal[VARIABLE_RESIZED] =
150 g_signal_new ("dict-size-changed",
151 G_TYPE_FROM_CLASS (class),
155 gtkextra_VOID__INT_INT,
164 psppire_dict_finalize (GObject *object)
166 PsppireDict *d = PSPPIRE_DICT (object);
168 dict_destroy (d->dict);
170 G_OBJECT_CLASS (parent_class)->finalize (object);
173 /* Pass on callbacks from src/data/dictionary, as
174 signals in the Gtk library */
176 addcb (struct dictionary *d, int idx, void *pd)
178 g_signal_emit (pd, signal[VARIABLE_INSERTED], 0, idx);
182 delcb (struct dictionary *d, int idx, void *pd)
184 g_signal_emit (pd, signal[VARIABLES_DELETED], 0, idx, 1);
188 mutcb (struct dictionary *d, int idx, void *pd)
190 g_signal_emit (pd, signal[VARIABLE_CHANGED], 0, idx);
193 static const struct dict_callbacks gui_callbacks =
201 psppire_dict_init (PsppireDict *psppire_dict)
203 psppire_dict->stamp = g_random_int ();
207 * psppire_dict_new_from_dict:
208 * @returns: a new #PsppireDict object
210 * Creates a new #PsppireDict.
213 psppire_dict_new_from_dict (struct dictionary *d)
215 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
218 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
224 /* Returns a valid name for a new variable in DICT.
225 The return value is statically allocated */
227 auto_generate_var_name (PsppireDict *dict)
230 static gchar name[10];
232 while (g_snprintf (name, 10, "VAR%05d",d++),
233 psppire_dict_lookup_var (dict, name))
239 /* Insert a new variable at posn IDX, with the name NAME.
240 If NAME is null, then a name will be automatically assigned.
243 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
245 struct variable *var ;
246 g_return_if_fail (d);
247 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
251 name = auto_generate_var_name (d);
253 var = dict_create_var (d->dict, name, 0);
255 dict_reorder_var (d->dict, var, idx);
258 /* Delete N variables beginning at FIRST */
260 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
263 g_return_if_fail (d);
264 g_return_if_fail (d->dict);
265 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
267 for (idx = 0 ; idx < n ; ++idx )
269 struct variable *var;
271 /* Do nothing if it's out of bounds */
272 if ( first >= dict_get_var_cnt (d->dict))
275 var = dict_get_var (d->dict, first);
276 dict_delete_var (d->dict, var);
278 dict_compact_values (d->dict);
283 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
285 struct variable *var;
287 g_assert (G_IS_PSPPIRE_DICT (d));
290 if ( idx < dict_get_var_cnt (d->dict))
292 /* This is an existing variable? */
293 var = dict_get_var (d->dict, idx);
294 dict_rename_var (d->dict, var, name);
299 dict_create_var (d->dict, name, 0);
305 /* Return the IDXth variable */
307 psppire_dict_get_variable (PsppireDict *d, gint idx)
309 g_return_val_if_fail (d, NULL);
310 g_return_val_if_fail (d->dict, NULL);
312 if ( dict_get_var_cnt (d->dict) <= idx )
315 return dict_get_var (d->dict, idx);
319 /* Return the number of variables in the dictionary */
321 psppire_dict_get_var_cnt (const PsppireDict *d)
323 g_return_val_if_fail (d, -1);
324 g_return_val_if_fail (d->dict, -1);
326 return dict_get_var_cnt (d->dict);
330 /* Return a variable by name.
331 Return NULL if it doesn't exist
334 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
336 g_return_val_if_fail (d, NULL);
337 g_return_val_if_fail (d->dict, NULL);
339 return dict_lookup_var (d->dict, name);
343 /* Clears the contents of D */
345 psppire_dict_clear (PsppireDict *d)
347 g_return_if_fail (d);
348 g_return_if_fail (d->dict);
351 const gint n_vars = dict_get_var_cnt (d->dict);
353 dict_clear (d->dict);
359 /* Return true is NAME would be a valid name of a variable to add to the
360 dictionary. False otherwise.
361 If REPORT is true, then invalid names will be reported as such as errors
364 psppire_dict_check_name (const PsppireDict *dict,
365 const gchar *name, gboolean report)
367 if ( ! var_is_valid_name (name, report ) )
370 if (psppire_dict_lookup_var (dict, name))
373 msg (ME,"Duplicate variable name.");
382 psppire_dict_get_next_value_idx (const PsppireDict *dict)
384 return dict_get_next_value_idx (dict->dict);
389 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
390 gint old_size, gint new_size)
393 g_return_if_fail (d);
394 g_return_if_fail (d->dict);
396 if ( old_size == new_size )
399 dict_compact_values (d->dict);
401 fv = var_get_case_index (pv);
403 g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
405 new_size - old_size );
409 /* Tree Model Stuff */
411 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
413 static gint tree_model_n_columns (GtkTreeModel *model);
415 static GType tree_model_column_type (GtkTreeModel *model, gint index);
417 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
420 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
422 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
425 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
426 gint column, GValue *value);
428 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
429 GtkTreeIter *parent, gint n);
433 dictionary_tree_model_init (GtkTreeModelIface *iface)
435 iface->get_flags = tree_model_get_flags;
436 iface->get_n_columns = tree_model_n_columns;
437 iface->get_column_type = tree_model_column_type;
438 iface->get_iter = tree_model_get_iter;
439 iface->iter_next = tree_model_iter_next;
440 iface->get_path = tree_model_get_path;
441 iface->get_value = tree_model_get_value;
443 iface->iter_children = 0;
444 iface->iter_has_child =0;
445 iface->iter_n_children =0;
446 iface->iter_nth_child = tree_model_nth_child ;
447 iface->iter_parent =0;
450 static GtkTreeModelFlags
451 tree_model_get_flags (GtkTreeModel *model)
453 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
455 return GTK_TREE_MODEL_LIST_ONLY;
460 tree_model_n_columns (GtkTreeModel *model)
466 tree_model_column_type (GtkTreeModel *model, gint index)
468 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
472 case DICT_TVM_COL_NAME:
473 return G_TYPE_STRING;
475 case DICT_TVM_COL_VAR:
476 return G_TYPE_POINTER;
479 g_return_val_if_reached ((GType)0);
483 g_assert_not_reached ();
488 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
490 gint *indices, depth;
492 struct variable *variable;
494 PsppireDict *dict = PSPPIRE_DICT (model);
496 g_return_val_if_fail (path, FALSE);
498 indices = gtk_tree_path_get_indices (path);
499 depth = gtk_tree_path_get_depth (path);
501 g_return_val_if_fail (depth == 1, FALSE);
505 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
508 variable = dict_get_var (dict->dict, n);
510 g_assert (var_get_dict_index (variable) == n);
512 iter->stamp = dict->stamp;
513 iter->user_data = variable;
520 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
522 PsppireDict *dict = PSPPIRE_DICT (model);
523 struct variable *variable;
526 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
528 if ( iter == NULL || iter->user_data == NULL)
531 variable = (struct variable *) iter->user_data;
533 idx = var_get_dict_index (variable);
535 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
538 variable = psppire_dict_get_variable (dict, idx + 1);
540 g_assert (var_get_dict_index (variable) == idx + 1);
542 iter->user_data = variable;
548 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
551 struct variable *variable;
552 PsppireDict *dict = PSPPIRE_DICT (model);
554 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
556 variable = (struct variable *) iter->user_data;
558 path = gtk_tree_path_new ();
559 gtk_tree_path_append_index (path, var_get_dict_index (variable));
566 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
567 gint column, GValue *value)
569 struct variable *variable;
570 PsppireDict *dict = PSPPIRE_DICT (model);
572 g_return_if_fail (iter->stamp == dict->stamp);
574 variable = (struct variable *) iter->user_data;
578 case DICT_TVM_COL_NAME:
579 g_value_init (value, G_TYPE_STRING);
580 g_value_set_string (value, var_get_name (variable));
582 case DICT_TVM_COL_VAR:
583 g_value_init (value, G_TYPE_POINTER);
584 g_value_set_pointer (value, variable);
587 g_return_if_reached ();
594 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
595 GtkTreeIter *parent, gint n)
598 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
600 dict = PSPPIRE_DICT (model);
605 if ( n >= psppire_dict_get_var_cnt (dict) )
608 iter->stamp = dict->stamp;
609 iter->user_data = psppire_dict_get_variable (dict, n);
611 if ( !iter->user_data)
620 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
623 dict_rename_var (dict->dict, v, text);