2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2006 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <gtksheet/gtkextra-marshal.h>
28 #include "psppire-object.h"
29 #include "psppire-dict.h"
30 #include <data/format.h>
31 #include <data/dictionary.h>
32 #include <data/missing-values.h>
33 #include <data/value-labels.h>
36 #include "message-dialog.h"
37 #include "psppire-variable.h"
39 /* --- prototypes --- */
40 static void psppire_dict_class_init (PsppireDictClass *class);
41 static void psppire_dict_init (PsppireDict *dict);
42 static void psppire_dict_finalize (GObject *object);
44 static void dictionary_tree_model_init(GtkTreeModelIface *iface);
47 /* --- variables --- */
48 static GObjectClass *parent_class = NULL;
50 enum {VARIABLE_CHANGED,
56 static guint signal[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_PSPPIRE_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 signal[VARIABLE_CHANGED] =
114 g_signal_new ("variable_changed",
115 G_TYPE_FROM_CLASS(class),
119 g_cclosure_marshal_VOID__INT,
126 signal[VARIABLE_INSERTED] =
127 g_signal_new ("variable_inserted",
128 G_TYPE_FROM_CLASS(class),
132 g_cclosure_marshal_VOID__INT,
138 signal[VARIABLES_DELETED] =
139 g_signal_new ("variables_deleted",
140 G_TYPE_FROM_CLASS(class),
144 gtkextra_VOID__INT_INT,
151 signal[VARIABLE_RESIZED] =
152 g_signal_new ("dict-size-changed",
153 G_TYPE_FROM_CLASS(class),
157 gtkextra_VOID__INT_INT,
166 psppire_dict_finalize (GObject *object)
169 PsppireDict *d = PSPPIRE_DICT (object);
171 for (v = 0 ; v < psppire_dict_get_var_cnt(d) ; ++v )
172 g_free(d->variables[v]);
174 g_free(d->variables);
177 dict_destroy(d->dict);
179 G_OBJECT_CLASS (parent_class)->finalize (object);
183 psppire_dict_init (PsppireDict *psppire_dict)
185 psppire_dict->dict = dict_create();
187 psppire_dict->variables = 0;
188 psppire_dict->cache_size = 0;
190 psppire_dict->stamp = g_random_int();
195 * @returns: a new #PsppireDict object
197 * Creates a new #PsppireDict.
200 psppire_dict_new (void)
202 return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
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);
217 new_dict->cache_size = dict_get_var_cnt(d);
218 new_dict->variables = g_malloc0(sizeof(struct PsppireVariable *) *
219 new_dict->cache_size);
226 /* Returns a valid name for a new variable in DICT.
227 The return value is statically allocated */
229 auto_generate_var_name(PsppireDict *dict)
232 static gchar name[10];
235 while (g_snprintf(name, 10, "VAR%05d",d++),
236 psppire_dict_lookup_var(dict, name))
242 /* Insert a new variable at posn IDX, with the name NAME.
243 If NAME is null, then a name will be automatically assigned.
246 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
248 struct variable *var ;
251 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
254 /* Invalidate the cache from IDX onwards */
255 for ( i = idx ; i < d->cache_size ; ++i )
257 g_free(d->variables[i]);
261 /* Ensure that the cache is large enough */
262 if ( dict_get_var_cnt(d->dict) >= d->cache_size )
264 d->variables = g_realloc(d->variables, sizeof(struct PsppireVariable*) *
265 (d->cache_size + CACHE_CHUNK));
266 d->cache_size += CACHE_CHUNK;
269 /* Zero the new pointers */
270 for ( ; i < d->cache_size ; ++i )
276 name = auto_generate_var_name(d);
278 var = dict_create_var(d->dict, name, 0);
280 dict_reorder_var(d->dict, var, idx);
282 d->variables[idx] = g_malloc(sizeof (struct PsppireVariable));
283 d->variables[idx]->v = var;
284 d->variables[idx]->dict = d;
286 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx );
289 /* Delete N variables beginning at FIRST */
291 psppire_dict_delete_variables(PsppireDict *d, gint first, gint n)
295 g_return_if_fail(d->dict);
296 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
298 /* Invalidate all pvs from FIRST onwards */
299 for ( idx = first ; idx < d->cache_size ; ++idx )
301 g_free(d->variables[idx]);
302 d->variables[idx] = 0;
305 for (idx = 0 ; idx < n ; ++idx )
307 struct variable *var;
309 /* Do nothing if it's out of bounds */
310 if ( first >= dict_get_var_cnt (d->dict))
313 var = dict_get_var(d->dict, first);
314 dict_delete_var (d->dict, var);
316 dict_compact_values(d->dict);
318 g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );
323 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
325 struct variable *var;
327 g_assert(G_IS_PSPPIRE_DICT(d));
330 if ( idx < dict_get_var_cnt(d->dict))
332 /* This is an existing variable? */
333 var = dict_get_var(d->dict, idx);
334 dict_rename_var(d->dict, var, name);
335 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
340 dict_create_var(d->dict, name, 0);
341 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
347 /* Return the IDXth variable */
348 struct PsppireVariable *
349 psppire_dict_get_variable(PsppireDict *d, gint idx)
351 struct PsppireVariable *var ;
352 g_return_val_if_fail(d, NULL);
353 g_return_val_if_fail(d->dict, NULL);
358 if (idx < 0 || idx >= psppire_dict_get_var_cnt(d))
361 var = d->variables[idx] ;
365 var = g_malloc(sizeof (*var));
367 var->v = dict_get_var(d->dict, idx);
368 d->variables[idx] = var;
375 /* Return the number of variables in the dictionary */
377 psppire_dict_get_var_cnt(const PsppireDict *d)
379 g_return_val_if_fail(d, -1);
380 g_return_val_if_fail(d->dict, -1);
383 return dict_get_var_cnt(d->dict);
387 /* Return a variable by name.
388 Return NULL if it doesn't exist
391 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
393 g_return_val_if_fail(d, NULL);
394 g_return_val_if_fail(d->dict, NULL);
396 return dict_lookup_var(d->dict, name);
401 psppire_dict_var_changed(PsppireDict *d, gint idx)
405 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
409 /* Clears the contents of D */
411 psppire_dict_clear(PsppireDict *d)
414 g_return_if_fail(d->dict);
417 const gint n_vars = dict_get_var_cnt(d->dict);
422 /* Invalidate the entire cache */
423 for ( i = 0 ; i < d->cache_size ; ++i )
425 g_free(d->variables[i]);
429 g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
435 /* Return true is NAME would be a valid name of a variable to add to the
436 dictionary. False otherwise.
437 If REPORT is true, then invalid names will be reported as such as errors
440 psppire_dict_check_name(const PsppireDict *dict,
441 const gchar *name, gboolean report)
443 if ( ! var_is_valid_name(name, report ) )
446 if (psppire_dict_lookup_var(dict, name))
449 msg(ME,"Duplicate variable name.");
458 psppire_dict_get_next_value_idx (const PsppireDict *dict)
460 return dict_get_next_value_idx(dict->dict);
465 psppire_dict_resize_variable(PsppireDict *d, const struct PsppireVariable *pv,
466 gint old_size, gint new_size)
469 g_return_if_fail (d);
470 g_return_if_fail (d->dict);
472 if ( old_size == new_size )
475 pv->v->nv = new_size;
477 dict_compact_values(d->dict);
479 fv = psppire_variable_get_fv(pv);
481 g_signal_emit(d, signal[VARIABLE_RESIZED], 0,
483 new_size - old_size );
490 /* Tree Model Stuff */
492 static GtkTreeModelFlags tree_model_get_flags(GtkTreeModel *model);
494 static gint tree_model_n_columns(GtkTreeModel *model);
496 static GType tree_model_column_type(GtkTreeModel *model, gint index);
498 static gboolean tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter,
501 static gboolean tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter);
503 static GtkTreePath * tree_model_get_path(GtkTreeModel *model,
506 static void tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
507 gint column, GValue *value);
509 static gboolean tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
510 GtkTreeIter *parent, gint n);
514 dictionary_tree_model_init(GtkTreeModelIface *iface)
516 iface->get_flags = tree_model_get_flags;
517 iface->get_n_columns = tree_model_n_columns;
518 iface->get_column_type = tree_model_column_type;
519 iface->get_iter = tree_model_get_iter;
520 iface->iter_next = tree_model_iter_next;
521 iface->get_path = tree_model_get_path;
522 iface->get_value = tree_model_get_value;
524 iface->iter_children = 0;
525 iface->iter_has_child =0;
526 iface->iter_n_children =0;
527 iface->iter_nth_child = tree_model_nth_child ;
528 iface->iter_parent =0;
531 static GtkTreeModelFlags
532 tree_model_get_flags(GtkTreeModel *model)
534 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), (GtkTreeModelFlags) 0);
536 return GTK_TREE_MODEL_LIST_ONLY;
541 tree_model_n_columns(GtkTreeModel *model)
547 tree_model_column_type(GtkTreeModel *model, gint index)
549 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), (GType) 0);
553 case DICT_TVM_COL_NAME:
554 return G_TYPE_STRING;
556 case DICT_TVM_COL_VAR:
557 return G_TYPE_POINTER;
560 g_return_val_if_reached((GType)0);
564 g_assert_not_reached();
569 tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
571 gint *indices, depth;
573 struct PsppireVariable *variable;
575 PsppireDict *dict = PSPPIRE_DICT (model);
577 g_return_val_if_fail(path, FALSE);
579 indices = gtk_tree_path_get_indices(path);
580 depth = gtk_tree_path_get_depth(path);
582 g_return_val_if_fail(depth == 1, FALSE);
586 if ( n < 0 || n >= psppire_dict_get_var_cnt(dict))
589 variable = psppire_dict_get_variable(dict, n);
591 g_assert(psppire_variable_get_index(variable) == n);
593 iter->stamp = dict->stamp;
594 iter->user_data = variable;
601 tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
603 PsppireDict *dict = PSPPIRE_DICT (model);
604 struct PsppireVariable *variable;
607 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
609 if ( iter == NULL || iter->user_data == NULL)
612 variable = (struct PsppireVariable *) iter->user_data;
614 idx = psppire_variable_get_index(variable);
616 if ( idx + 1 >= psppire_dict_get_var_cnt(dict))
619 variable = psppire_dict_get_variable(dict, idx + 1);
621 g_assert(psppire_variable_get_index(variable) == idx + 1);
623 iter->user_data = variable;
629 tree_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
632 struct PsppireVariable *variable;
633 PsppireDict *dict = PSPPIRE_DICT (model);
635 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
637 variable = (struct PsppireVariable *) iter->user_data;
639 path = gtk_tree_path_new();
640 gtk_tree_path_append_index(path, psppire_variable_get_index(variable));
647 tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
648 gint column, GValue *value)
650 struct PsppireVariable *variable;
651 PsppireDict *dict = PSPPIRE_DICT (model);
653 g_return_if_fail(iter->stamp == dict->stamp);
655 variable = (struct PsppireVariable *) iter->user_data;
659 case DICT_TVM_COL_NAME:
660 g_value_init(value, G_TYPE_STRING);
661 g_value_set_string(value, psppire_variable_get_name(variable));
663 case DICT_TVM_COL_VAR:
664 g_value_init(value, G_TYPE_POINTER);
665 g_value_set_pointer(value, variable);
668 g_return_if_reached();
675 tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
676 GtkTreeIter *parent, gint n)
679 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), FALSE);
681 dict = PSPPIRE_DICT(model);
686 if ( n >= psppire_dict_get_var_cnt(dict) )
689 iter->stamp = dict->stamp;
690 iter->user_data = psppire_dict_get_variable(dict, n);
692 if ( !iter->user_data)