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->dict = dict_create ();
205 dict_set_callbacks (psppire_dict->dict, &gui_callbacks, psppire_dict);
207 psppire_dict->stamp = g_random_int();
212 * @returns: a new #PsppireDict object
214 * Creates a new #PsppireDict.
217 psppire_dict_new (void)
219 return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
224 * psppire_dict_new_from_dict:
225 * @returns: a new #PsppireDict object
227 * Creates a new #PsppireDict.
230 psppire_dict_new_from_dict (struct dictionary *d)
232 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
239 /* Returns a valid name for a new variable in DICT.
240 The return value is statically allocated */
242 auto_generate_var_name (PsppireDict *dict)
245 static gchar name[10];
247 while (g_snprintf(name, 10, "VAR%05d",d++),
248 psppire_dict_lookup_var(dict, name))
254 /* Insert a new variable at posn IDX, with the name NAME.
255 If NAME is null, then a name will be automatically assigned.
258 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
260 struct variable *var ;
262 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
266 name = auto_generate_var_name(d);
268 var = dict_create_var(d->dict, name, 0);
270 dict_reorder_var(d->dict, var, idx);
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 for (idx = 0 ; idx < n ; ++idx )
286 struct variable *var;
288 /* Do nothing if it's out of bounds */
289 if ( first >= dict_get_var_cnt (d->dict))
292 var = dict_get_var(d->dict, first);
293 dict_delete_var (d->dict, var);
295 dict_compact_values(d->dict);
297 g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );
302 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
304 struct variable *var;
306 g_assert(G_IS_PSPPIRE_DICT(d));
309 if ( idx < dict_get_var_cnt(d->dict))
311 /* This is an existing variable? */
312 var = dict_get_var(d->dict, idx);
313 dict_rename_var(d->dict, var, name);
314 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
319 dict_create_var(d->dict, name, 0);
320 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
326 /* Return the IDXth variable */
328 psppire_dict_get_variable(PsppireDict *d, gint idx)
330 g_return_val_if_fail(d, NULL);
331 g_return_val_if_fail(d->dict, NULL);
333 if ( dict_get_var_cnt (d->dict) <= idx )
336 return dict_get_var (d->dict, idx);
340 /* Return the number of variables in the dictionary */
342 psppire_dict_get_var_cnt (const PsppireDict *d)
344 g_return_val_if_fail (d, -1);
345 g_return_val_if_fail (d->dict, -1);
347 return dict_get_var_cnt (d->dict);
351 /* Return a variable by name.
352 Return NULL if it doesn't exist
355 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
357 g_return_val_if_fail (d, NULL);
358 g_return_val_if_fail (d->dict, NULL);
360 return dict_lookup_var (d->dict, name);
365 psppire_dict_var_changed (PsppireDict *d, gint idx)
369 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
373 /* Clears the contents of D */
375 psppire_dict_clear(PsppireDict *d)
378 g_return_if_fail(d->dict);
381 const gint n_vars = dict_get_var_cnt(d->dict);
385 g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
391 /* Return true is NAME would be a valid name of a variable to add to the
392 dictionary. False otherwise.
393 If REPORT is true, then invalid names will be reported as such as errors
396 psppire_dict_check_name(const PsppireDict *dict,
397 const gchar *name, gboolean report)
399 if ( ! var_is_valid_name(name, report ) )
402 if (psppire_dict_lookup_var(dict, name))
405 msg(ME,"Duplicate variable name.");
414 psppire_dict_get_next_value_idx (const PsppireDict *dict)
416 return dict_get_next_value_idx(dict->dict);
421 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
422 gint old_size, gint new_size)
425 g_return_if_fail (d);
426 g_return_if_fail (d->dict);
428 if ( old_size == new_size )
431 dict_compact_values (d->dict);
433 fv = var_get_case_index (pv);
435 g_signal_emit(d, signal[VARIABLE_RESIZED], 0,
437 new_size - old_size );
441 /* Tree Model Stuff */
443 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
445 static gint tree_model_n_columns (GtkTreeModel *model);
447 static GType tree_model_column_type (GtkTreeModel *model, gint index);
449 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
452 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
454 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
457 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
458 gint column, GValue *value);
460 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
461 GtkTreeIter *parent, gint n);
465 dictionary_tree_model_init (GtkTreeModelIface *iface)
467 iface->get_flags = tree_model_get_flags;
468 iface->get_n_columns = tree_model_n_columns;
469 iface->get_column_type = tree_model_column_type;
470 iface->get_iter = tree_model_get_iter;
471 iface->iter_next = tree_model_iter_next;
472 iface->get_path = tree_model_get_path;
473 iface->get_value = tree_model_get_value;
475 iface->iter_children = 0;
476 iface->iter_has_child =0;
477 iface->iter_n_children =0;
478 iface->iter_nth_child = tree_model_nth_child ;
479 iface->iter_parent =0;
482 static GtkTreeModelFlags
483 tree_model_get_flags (GtkTreeModel *model)
485 g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GtkTreeModelFlags) 0);
487 return GTK_TREE_MODEL_LIST_ONLY;
492 tree_model_n_columns (GtkTreeModel *model)
498 tree_model_column_type (GtkTreeModel *model, gint index)
500 g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GType) 0);
504 case DICT_TVM_COL_NAME:
505 return G_TYPE_STRING;
507 case DICT_TVM_COL_VAR:
508 return G_TYPE_POINTER;
511 g_return_val_if_reached((GType)0);
515 g_assert_not_reached();
520 tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
522 gint *indices, depth;
524 struct variable *variable;
526 PsppireDict *dict = PSPPIRE_DICT (model);
528 g_return_val_if_fail (path, FALSE);
530 indices = gtk_tree_path_get_indices (path);
531 depth = gtk_tree_path_get_depth (path);
533 g_return_val_if_fail(depth == 1, FALSE);
537 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
540 variable = dict_get_var (dict->dict, n);
542 g_assert (var_get_dict_index (variable) == n);
544 iter->stamp = dict->stamp;
545 iter->user_data = variable;
552 tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
554 PsppireDict *dict = PSPPIRE_DICT (model);
555 struct variable *variable;
558 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
560 if ( iter == NULL || iter->user_data == NULL)
563 variable = (struct variable *) iter->user_data;
565 idx = var_get_dict_index (variable);
567 if ( idx + 1 >= psppire_dict_get_var_cnt(dict))
570 variable = psppire_dict_get_variable (dict, idx + 1);
572 g_assert (var_get_dict_index (variable) == idx + 1);
574 iter->user_data = variable;
580 tree_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
583 struct variable *variable;
584 PsppireDict *dict = PSPPIRE_DICT (model);
586 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
588 variable = (struct variable *) iter->user_data;
590 path = gtk_tree_path_new();
591 gtk_tree_path_append_index(path, var_get_dict_index (variable));
598 tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
599 gint column, GValue *value)
601 struct variable *variable;
602 PsppireDict *dict = PSPPIRE_DICT (model);
604 g_return_if_fail(iter->stamp == dict->stamp);
606 variable = (struct variable *) iter->user_data;
610 case DICT_TVM_COL_NAME:
611 g_value_init(value, G_TYPE_STRING);
612 g_value_set_string(value, var_get_name(variable));
614 case DICT_TVM_COL_VAR:
615 g_value_init(value, G_TYPE_POINTER);
616 g_value_set_pointer(value, variable);
619 g_return_if_reached();
626 tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
627 GtkTreeIter *parent, gint n)
630 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), FALSE);
632 dict = PSPPIRE_DICT(model);
637 if ( n >= psppire_dict_get_var_cnt(dict) )
640 iter->stamp = dict->stamp;
641 iter->user_data = psppire_dict_get_variable(dict, n);
643 if ( !iter->user_data)
652 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
655 dict_rename_var (dict->dict, v, text);