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>
34 #include <data/variable.h>
36 #include "message-dialog.h"
38 /* --- prototypes --- */
39 static void psppire_dict_class_init (PsppireDictClass *class);
40 static void psppire_dict_init (PsppireDict *dict);
41 static void psppire_dict_finalize (GObject *object);
43 static void dictionary_tree_model_init(GtkTreeModelIface *iface);
46 /* --- variables --- */
47 static GObjectClass *parent_class = NULL;
49 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,
165 psppire_dict_finalize (GObject *object)
167 PsppireDict *d = PSPPIRE_DICT (object);
169 dict_destroy(d->dict);
171 G_OBJECT_CLASS (parent_class)->finalize (object);
174 /* Pass on callbacks from src/data/dictionary, as
175 signals in the Gtk library */
177 addcb (struct dictionary *d, int idx, void *pd)
179 g_signal_emit(pd, signal[VARIABLE_INSERTED], 0, idx);
183 delcb (struct dictionary *d, int idx, void *pd)
185 g_signal_emit(pd, signal[VARIABLES_DELETED], 0, idx, 1);
189 mutcb (struct dictionary *d, int idx, void *pd)
191 g_signal_emit(pd, signal[VARIABLE_CHANGED], 0, idx);
194 static const struct dict_callbacks gui_callbacks =
202 psppire_dict_init (PsppireDict *psppire_dict)
204 psppire_dict->dict = dict_create ();
206 dict_set_callbacks (psppire_dict->dict, &gui_callbacks, psppire_dict);
208 psppire_dict->stamp = g_random_int();
213 * @returns: a new #PsppireDict object
215 * Creates a new #PsppireDict.
218 psppire_dict_new (void)
220 return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
225 * psppire_dict_new_from_dict:
226 * @returns: a new #PsppireDict object
228 * Creates a new #PsppireDict.
231 psppire_dict_new_from_dict (struct dictionary *d)
233 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
240 /* Returns a valid name for a new variable in DICT.
241 The return value is statically allocated */
243 auto_generate_var_name (PsppireDict *dict)
246 static gchar name[10];
248 while (g_snprintf(name, 10, "VAR%05d",d++),
249 psppire_dict_lookup_var(dict, name))
255 /* Insert a new variable at posn IDX, with the name NAME.
256 If NAME is null, then a name will be automatically assigned.
259 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
261 struct variable *var ;
263 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
267 name = auto_generate_var_name(d);
269 var = dict_create_var(d->dict, name, 0);
271 dict_reorder_var(d->dict, var, idx);
273 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx );
276 /* Delete N variables beginning at FIRST */
278 psppire_dict_delete_variables(PsppireDict *d, gint first, gint n)
282 g_return_if_fail(d->dict);
283 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
285 for (idx = 0 ; idx < n ; ++idx )
287 struct variable *var;
289 /* Do nothing if it's out of bounds */
290 if ( first >= dict_get_var_cnt (d->dict))
293 var = dict_get_var(d->dict, first);
294 dict_delete_var (d->dict, var);
296 dict_compact_values(d->dict);
298 g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );
303 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
305 struct variable *var;
307 g_assert(G_IS_PSPPIRE_DICT(d));
310 if ( idx < dict_get_var_cnt(d->dict))
312 /* This is an existing variable? */
313 var = dict_get_var(d->dict, idx);
314 dict_rename_var(d->dict, var, name);
315 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
320 dict_create_var(d->dict, name, 0);
321 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
327 /* Return the IDXth variable */
329 psppire_dict_get_variable(PsppireDict *d, gint idx)
331 g_return_val_if_fail(d, NULL);
332 g_return_val_if_fail(d->dict, NULL);
334 if ( dict_get_var_cnt (d->dict) <= idx )
337 return dict_get_var (d->dict, idx);
341 /* Return the number of variables in the dictionary */
343 psppire_dict_get_var_cnt (const PsppireDict *d)
345 g_return_val_if_fail (d, -1);
346 g_return_val_if_fail (d->dict, -1);
348 return dict_get_var_cnt (d->dict);
352 /* Return a variable by name.
353 Return NULL if it doesn't exist
356 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
358 g_return_val_if_fail (d, NULL);
359 g_return_val_if_fail (d->dict, NULL);
361 return dict_lookup_var (d->dict, name);
366 psppire_dict_var_changed (PsppireDict *d, gint idx)
370 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
374 /* Clears the contents of D */
376 psppire_dict_clear(PsppireDict *d)
379 g_return_if_fail(d->dict);
382 const gint n_vars = dict_get_var_cnt(d->dict);
386 g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
392 /* Return true is NAME would be a valid name of a variable to add to the
393 dictionary. False otherwise.
394 If REPORT is true, then invalid names will be reported as such as errors
397 psppire_dict_check_name(const PsppireDict *dict,
398 const gchar *name, gboolean report)
400 if ( ! var_is_valid_name(name, report ) )
403 if (psppire_dict_lookup_var(dict, name))
406 msg(ME,"Duplicate variable name.");
415 psppire_dict_get_next_value_idx (const PsppireDict *dict)
417 return dict_get_next_value_idx(dict->dict);
422 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
423 gint old_size, gint new_size)
426 g_return_if_fail (d);
427 g_return_if_fail (d->dict);
429 if ( old_size == new_size )
432 dict_compact_values (d->dict);
434 fv = var_get_case_index (pv);
436 g_signal_emit(d, signal[VARIABLE_RESIZED], 0,
438 new_size - old_size );
442 /* Tree Model Stuff */
444 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
446 static gint tree_model_n_columns (GtkTreeModel *model);
448 static GType tree_model_column_type (GtkTreeModel *model, gint index);
450 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
453 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
455 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
458 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
459 gint column, GValue *value);
461 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
462 GtkTreeIter *parent, gint n);
466 dictionary_tree_model_init (GtkTreeModelIface *iface)
468 iface->get_flags = tree_model_get_flags;
469 iface->get_n_columns = tree_model_n_columns;
470 iface->get_column_type = tree_model_column_type;
471 iface->get_iter = tree_model_get_iter;
472 iface->iter_next = tree_model_iter_next;
473 iface->get_path = tree_model_get_path;
474 iface->get_value = tree_model_get_value;
476 iface->iter_children = 0;
477 iface->iter_has_child =0;
478 iface->iter_n_children =0;
479 iface->iter_nth_child = tree_model_nth_child ;
480 iface->iter_parent =0;
483 static GtkTreeModelFlags
484 tree_model_get_flags (GtkTreeModel *model)
486 g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GtkTreeModelFlags) 0);
488 return GTK_TREE_MODEL_LIST_ONLY;
493 tree_model_n_columns (GtkTreeModel *model)
499 tree_model_column_type (GtkTreeModel *model, gint index)
501 g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GType) 0);
505 case DICT_TVM_COL_NAME:
506 return G_TYPE_STRING;
508 case DICT_TVM_COL_VAR:
509 return G_TYPE_POINTER;
512 g_return_val_if_reached((GType)0);
516 g_assert_not_reached();
521 tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
523 gint *indices, depth;
525 struct variable *variable;
527 PsppireDict *dict = PSPPIRE_DICT (model);
529 g_return_val_if_fail (path, FALSE);
531 indices = gtk_tree_path_get_indices (path);
532 depth = gtk_tree_path_get_depth (path);
534 g_return_val_if_fail(depth == 1, FALSE);
538 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
541 variable = dict_get_var (dict->dict, n);
543 g_assert (var_get_dict_index (variable) == n);
545 iter->stamp = dict->stamp;
546 iter->user_data = variable;
553 tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
555 PsppireDict *dict = PSPPIRE_DICT (model);
556 struct variable *variable;
559 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
561 if ( iter == NULL || iter->user_data == NULL)
564 variable = (struct variable *) iter->user_data;
566 idx = var_get_dict_index (variable);
568 if ( idx + 1 >= psppire_dict_get_var_cnt(dict))
571 variable = psppire_dict_get_variable (dict, idx + 1);
573 g_assert (var_get_dict_index (variable) == idx + 1);
575 iter->user_data = variable;
581 tree_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
584 struct variable *variable;
585 PsppireDict *dict = PSPPIRE_DICT (model);
587 g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
589 variable = (struct variable *) iter->user_data;
591 path = gtk_tree_path_new();
592 gtk_tree_path_append_index(path, var_get_dict_index (variable));
599 tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
600 gint column, GValue *value)
602 struct variable *variable;
603 PsppireDict *dict = PSPPIRE_DICT (model);
605 g_return_if_fail(iter->stamp == dict->stamp);
607 variable = (struct variable *) iter->user_data;
611 case DICT_TVM_COL_NAME:
612 g_value_init(value, G_TYPE_STRING);
613 g_value_set_string(value, var_get_name(variable));
615 case DICT_TVM_COL_VAR:
616 g_value_init(value, G_TYPE_POINTER);
617 g_value_set_pointer(value, variable);
620 g_return_if_reached();
627 tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
628 GtkTreeIter *parent, gint n)
631 g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), FALSE);
633 dict = PSPPIRE_DICT(model);
638 if ( n >= psppire_dict_get_var_cnt(dict) )
641 iter->stamp = dict->stamp;
642 iter->user_data = psppire_dict_get_variable(dict, n);
644 if ( !iter->user_data)
653 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
656 dict_rename_var (dict->dict, v, text);