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);
257 g_signal_emit (d, signal[VARIABLE_INSERTED], 0, idx );
260 /* Delete N variables beginning at FIRST */
262 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
265 g_return_if_fail (d);
266 g_return_if_fail (d->dict);
267 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
269 for (idx = 0 ; idx < n ; ++idx )
271 struct variable *var;
273 /* Do nothing if it's out of bounds */
274 if ( first >= dict_get_var_cnt (d->dict))
277 var = dict_get_var (d->dict, first);
278 dict_delete_var (d->dict, var);
280 dict_compact_values (d->dict);
282 g_signal_emit (d, signal[VARIABLES_DELETED], 0, first, idx );
287 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
289 struct variable *var;
291 g_assert (G_IS_PSPPIRE_DICT (d));
294 if ( idx < dict_get_var_cnt (d->dict))
296 /* This is an existing variable? */
297 var = dict_get_var (d->dict, idx);
298 dict_rename_var (d->dict, var, name);
299 g_signal_emit (d, signal[VARIABLE_CHANGED], 0, idx);
304 dict_create_var (d->dict, name, 0);
305 g_signal_emit (d, signal[VARIABLE_INSERTED], 0, idx);
311 /* Return the IDXth variable */
313 psppire_dict_get_variable (PsppireDict *d, gint idx)
315 g_return_val_if_fail (d, NULL);
316 g_return_val_if_fail (d->dict, NULL);
318 if ( dict_get_var_cnt (d->dict) <= idx )
321 return dict_get_var (d->dict, idx);
325 /* Return the number of variables in the dictionary */
327 psppire_dict_get_var_cnt (const PsppireDict *d)
329 g_return_val_if_fail (d, -1);
330 g_return_val_if_fail (d->dict, -1);
332 return dict_get_var_cnt (d->dict);
336 /* Return a variable by name.
337 Return NULL if it doesn't exist
340 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
342 g_return_val_if_fail (d, NULL);
343 g_return_val_if_fail (d->dict, NULL);
345 return dict_lookup_var (d->dict, name);
350 psppire_dict_var_changed (PsppireDict *d, gint idx)
352 g_return_if_fail (d);
354 g_signal_emit (d, signal[VARIABLE_CHANGED], 0, idx);
358 /* Clears the contents of D */
360 psppire_dict_clear (PsppireDict *d)
362 g_return_if_fail (d);
363 g_return_if_fail (d->dict);
366 const gint n_vars = dict_get_var_cnt (d->dict);
368 dict_clear (d->dict);
370 g_signal_emit (d, signal[VARIABLES_DELETED], 0, 0, n_vars );
376 /* Return true is NAME would be a valid name of a variable to add to the
377 dictionary. False otherwise.
378 If REPORT is true, then invalid names will be reported as such as errors
381 psppire_dict_check_name (const PsppireDict *dict,
382 const gchar *name, gboolean report)
384 if ( ! var_is_valid_name (name, report ) )
387 if (psppire_dict_lookup_var (dict, name))
390 msg (ME,"Duplicate variable name.");
399 psppire_dict_get_next_value_idx (const PsppireDict *dict)
401 return dict_get_next_value_idx (dict->dict);
406 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
407 gint old_size, gint new_size)
410 g_return_if_fail (d);
411 g_return_if_fail (d->dict);
413 if ( old_size == new_size )
416 dict_compact_values (d->dict);
418 fv = var_get_case_index (pv);
420 g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
422 new_size - old_size );
426 /* Tree Model Stuff */
428 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
430 static gint tree_model_n_columns (GtkTreeModel *model);
432 static GType tree_model_column_type (GtkTreeModel *model, gint index);
434 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
437 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
439 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
442 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
443 gint column, GValue *value);
445 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
446 GtkTreeIter *parent, gint n);
450 dictionary_tree_model_init (GtkTreeModelIface *iface)
452 iface->get_flags = tree_model_get_flags;
453 iface->get_n_columns = tree_model_n_columns;
454 iface->get_column_type = tree_model_column_type;
455 iface->get_iter = tree_model_get_iter;
456 iface->iter_next = tree_model_iter_next;
457 iface->get_path = tree_model_get_path;
458 iface->get_value = tree_model_get_value;
460 iface->iter_children = 0;
461 iface->iter_has_child =0;
462 iface->iter_n_children =0;
463 iface->iter_nth_child = tree_model_nth_child ;
464 iface->iter_parent =0;
467 static GtkTreeModelFlags
468 tree_model_get_flags (GtkTreeModel *model)
470 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
472 return GTK_TREE_MODEL_LIST_ONLY;
477 tree_model_n_columns (GtkTreeModel *model)
483 tree_model_column_type (GtkTreeModel *model, gint index)
485 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
489 case DICT_TVM_COL_NAME:
490 return G_TYPE_STRING;
492 case DICT_TVM_COL_VAR:
493 return G_TYPE_POINTER;
496 g_return_val_if_reached ((GType)0);
500 g_assert_not_reached ();
505 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
507 gint *indices, depth;
509 struct variable *variable;
511 PsppireDict *dict = PSPPIRE_DICT (model);
513 g_return_val_if_fail (path, FALSE);
515 indices = gtk_tree_path_get_indices (path);
516 depth = gtk_tree_path_get_depth (path);
518 g_return_val_if_fail (depth == 1, FALSE);
522 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
525 variable = dict_get_var (dict->dict, n);
527 g_assert (var_get_dict_index (variable) == n);
529 iter->stamp = dict->stamp;
530 iter->user_data = variable;
537 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
539 PsppireDict *dict = PSPPIRE_DICT (model);
540 struct variable *variable;
543 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
545 if ( iter == NULL || iter->user_data == NULL)
548 variable = (struct variable *) iter->user_data;
550 idx = var_get_dict_index (variable);
552 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
555 variable = psppire_dict_get_variable (dict, idx + 1);
557 g_assert (var_get_dict_index (variable) == idx + 1);
559 iter->user_data = variable;
565 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
568 struct variable *variable;
569 PsppireDict *dict = PSPPIRE_DICT (model);
571 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
573 variable = (struct variable *) iter->user_data;
575 path = gtk_tree_path_new ();
576 gtk_tree_path_append_index (path, var_get_dict_index (variable));
583 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
584 gint column, GValue *value)
586 struct variable *variable;
587 PsppireDict *dict = PSPPIRE_DICT (model);
589 g_return_if_fail (iter->stamp == dict->stamp);
591 variable = (struct variable *) iter->user_data;
595 case DICT_TVM_COL_NAME:
596 g_value_init (value, G_TYPE_STRING);
597 g_value_set_string (value, var_get_name (variable));
599 case DICT_TVM_COL_VAR:
600 g_value_init (value, G_TYPE_POINTER);
601 g_value_set_pointer (value, variable);
604 g_return_if_reached ();
611 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
612 GtkTreeIter *parent, gint n)
615 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
617 dict = PSPPIRE_DICT (model);
622 if ( n >= psppire_dict_get_var_cnt (dict) )
625 iter->stamp = dict->stamp;
626 iter->user_data = psppire_dict_get_variable (dict, n);
628 if ( !iter->user_data)
637 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
640 dict_rename_var (dict->dict, v, text);