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,
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,
152 psppire_dict_finalize (GObject *object)
155 PsppireDict *d = PSPPIRE_DICT (object);
157 for (v = 0 ; v < psppire_dict_get_var_cnt(d) ; ++v )
158 g_free(d->variables[v]);
160 g_free(d->variables);
163 dict_destroy(d->dict);
165 G_OBJECT_CLASS (parent_class)->finalize (object);
169 psppire_dict_init (PsppireDict *psppire_dict)
171 psppire_dict->dict = dict_create();
173 psppire_dict->variables = 0;
174 psppire_dict->cache_size = 0;
176 psppire_dict->stamp = g_random_int();
181 * @returns: a new #PsppireDict object
183 * Creates a new #PsppireDict.
186 psppire_dict_new (void)
188 return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
193 * psppire_dict_new_from_dict:
194 * @returns: a new #PsppireDict object
196 * Creates a new #PsppireDict.
199 psppire_dict_new_from_dict (struct dictionary *d)
201 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
203 new_dict->cache_size = dict_get_var_cnt(d);
204 new_dict->variables = g_malloc0(sizeof(struct PsppireVariable *) *
205 new_dict->cache_size);
212 /* Returns a valid name for a new variable in DICT.
213 The return value is statically allocated */
215 auto_generate_var_name(PsppireDict *dict)
218 static gchar name[10];
221 while (g_snprintf(name, 10, "VAR%05d",d++),
222 psppire_dict_lookup_var(dict, name))
228 /* Insert a new variable at posn IDX, with the name NAME.
229 If NAME is null, then a name will be automatically assigned.
232 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
234 struct variable *var ;
237 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
240 /* Invalidate the cache from IDX onwards */
241 for ( i = idx ; i < d->cache_size ; ++i )
243 g_free(d->variables[i]);
247 /* Ensure that the cache is large enough */
248 if ( dict_get_var_cnt(d->dict) >= d->cache_size )
250 d->variables = g_realloc(d->variables, sizeof(struct PsppireVariable*) *
251 (d->cache_size + CACHE_CHUNK));
252 d->cache_size += CACHE_CHUNK;
255 /* Zero the new pointers */
256 for ( ; i < d->cache_size ; ++i )
262 name = auto_generate_var_name(d);
264 var = dict_create_var(d->dict, name, 0);
266 dict_reorder_var(d->dict, var, idx);
268 d->variables[idx] = g_malloc(sizeof (struct PsppireVariable));
269 d->variables[idx]->v = var;
270 d->variables[idx]->dict = d;
272 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx );
275 /* Delete N variables beginning at FIRST */
277 psppire_dict_delete_variables(PsppireDict *d, gint first, gint n)
281 g_return_if_fail(d->dict);
282 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
284 /* Invalidate all pvs from FIRST onwards */
285 for ( idx = first ; idx < d->cache_size ; ++idx )
287 g_free(d->variables[idx]);
288 d->variables[idx] = 0;
291 for (idx = 0 ; idx < n ; ++idx )
293 struct variable *var;
295 /* Do nothing if it's out of bounds */
296 if ( first >= dict_get_var_cnt (d->dict))
299 var = dict_get_var(d->dict, first);
300 dict_delete_var (d->dict, var);
303 g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );
308 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
310 struct variable *var;
312 g_assert(G_IS_PSPPIRE_DICT(d));
315 if ( idx < dict_get_var_cnt(d->dict))
317 /* This is an existing variable? */
318 var = dict_get_var(d->dict, idx);
319 dict_rename_var(d->dict, var, name);
320 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
325 dict_create_var(d->dict, name, 0);
326 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
332 /* Return the IDXth variable */
333 struct PsppireVariable *
334 psppire_dict_get_variable(PsppireDict *d, gint idx)
336 struct PsppireVariable *var ;
337 g_return_val_if_fail(d, NULL);
338 g_return_val_if_fail(d->dict, NULL);
343 if (idx < 0 || idx >= psppire_dict_get_var_cnt(d))
346 var = d->variables[idx] ;
350 var = g_malloc(sizeof (*var));
352 var->v = dict_get_var(d->dict, idx);
353 d->variables[idx] = var;
360 /* Return the number of variables in the dictionary */
362 psppire_dict_get_var_cnt(const PsppireDict *d)
364 g_return_val_if_fail(d, -1);
365 g_return_val_if_fail(d->dict, -1);
368 return dict_get_var_cnt(d->dict);
372 /* Return a variable by name.
373 Return NULL if it doesn't exist
376 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
378 g_return_val_if_fail(d, NULL);
379 g_return_val_if_fail(d->dict, NULL);
381 return dict_lookup_var(d->dict, name);
386 psppire_dict_var_changed(PsppireDict *d, gint idx)
390 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
394 /* Clears the contents of D */
396 psppire_dict_clear(PsppireDict *d)
399 g_return_if_fail(d->dict);
402 const gint n_vars = dict_get_var_cnt(d->dict);
407 /* Invalidate the entire cache */
408 for ( i = 0 ; i < d->cache_size ; ++i )
410 g_free(d->variables[i]);
414 g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
420 /* Return true is NAME would be a valid name of a variable to add to the
421 dictionary. False otherwise.
422 If REPORT is true, then invalid names will be reported as such as errors
425 psppire_dict_check_name(const PsppireDict *dict,
426 const gchar *name, gboolean report)
428 if ( ! var_is_valid_name(name, report ) )
431 if (psppire_dict_lookup_var(dict, name))
434 msg(ME,"Duplicate variable name.");
443 psppire_dict_get_next_value_idx (const PsppireDict *dict)
445 return dict_get_next_value_idx(dict->dict);
450 /* Tree Model Stuff */
452 static GtkTreeModelFlags tree_model_get_flags(GtkTreeModel *model);
454 static gint tree_model_n_columns(GtkTreeModel *model);
456 static GType tree_model_column_type(GtkTreeModel *model, gint index);
458 static gboolean tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter,
461 static gboolean tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter);
463 static GtkTreePath * tree_model_get_path(GtkTreeModel *model,
466 static void tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
467 gint column, GValue *value);
469 static gboolean tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
470 GtkTreeIter *parent, gint n);
474 dictionary_tree_model_init(GtkTreeModelIface *iface)
476 iface->get_flags = tree_model_get_flags;
477 iface->get_n_columns = tree_model_n_columns;
478 iface->get_column_type = tree_model_column_type;
479 iface->get_iter = tree_model_get_iter;
480 iface->iter_next = tree_model_iter_next;
481 iface->get_path = tree_model_get_path;
482 iface->get_value = tree_model_get_value;
484 iface->iter_children = 0;
485 iface->iter_has_child =0;
486 iface->iter_n_children =0;
487 iface->iter_nth_child = tree_model_nth_child ;
488 iface->iter_parent =0;
491 static GtkTreeModelFlags
492 tree_model_get_flags(GtkTreeModel *model)
494 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), (GtkTreeModelFlags) 0);
496 return GTK_TREE_MODEL_LIST_ONLY;
501 tree_model_n_columns(GtkTreeModel *model)
507 tree_model_column_type(GtkTreeModel *model, gint index)
509 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), (GType) 0);
513 case DICT_TVM_COL_NAME:
514 return G_TYPE_STRING;
516 case DICT_TVM_COL_VAR:
517 return G_TYPE_POINTER;
520 g_return_val_if_reached((GType)0);
524 g_assert_not_reached();
529 tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
531 gint *indices, depth;
533 struct PsppireVariable *variable;
535 PsppireDict *dict = PSPPIRE_DICT (model);
537 g_return_val_if_fail(path, FALSE);
539 indices = gtk_tree_path_get_indices(path);
540 depth = gtk_tree_path_get_depth(path);
542 g_return_val_if_fail(depth == 1, FALSE);
546 if ( n < 0 || n >= psppire_dict_get_var_cnt(dict))
549 variable = psppire_dict_get_variable(dict, n);
551 g_assert(psppire_variable_get_index(variable) == n);
553 iter->stamp = dict->stamp;
554 iter->user_data = variable;
561 tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
563 PsppireDict *dict = PSPPIRE_DICT (model);
564 struct PsppireVariable *variable;
567 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
569 if ( iter == NULL || iter->user_data == NULL)
572 variable = (struct PsppireVariable *) iter->user_data;
574 idx = psppire_variable_get_index(variable);
576 if ( idx + 1 >= psppire_dict_get_var_cnt(dict))
579 variable = psppire_dict_get_variable(dict, idx + 1);
581 g_assert(psppire_variable_get_index(variable) == idx + 1);
583 iter->user_data = variable;
589 tree_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
592 struct PsppireVariable *variable;
593 PsppireDict *dict = PSPPIRE_DICT (model);
595 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
597 variable = (struct PsppireVariable *) iter->user_data;
599 path = gtk_tree_path_new();
600 gtk_tree_path_append_index(path, psppire_variable_get_index(variable));
607 tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
608 gint column, GValue *value)
610 struct PsppireVariable *variable;
611 PsppireDict *dict = PSPPIRE_DICT (model);
613 g_return_if_fail(iter->stamp == dict->stamp);
615 variable = (struct PsppireVariable *) iter->user_data;
619 case DICT_TVM_COL_NAME:
620 g_value_init(value, G_TYPE_STRING);
621 g_value_set_string(value, psppire_variable_get_name(variable));
623 case DICT_TVM_COL_VAR:
624 g_value_init(value, G_TYPE_POINTER);
625 g_value_set_pointer(value, variable);
628 g_return_if_reached();
635 tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
636 GtkTreeIter *parent, gint n)
639 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), FALSE);
641 dict = PSPPIRE_DICT(model);
646 if ( n >= psppire_dict_get_var_cnt(dict) )
649 iter->stamp = dict->stamp;
650 iter->user_data = psppire_dict_get_variable(dict, n);
652 if ( !iter->user_data)