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);
225 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
231 /* Returns a valid name for a new variable in DICT.
232 The return value is statically allocated */
234 auto_generate_var_name (PsppireDict *dict)
237 static gchar name[10];
239 while (g_snprintf (name, 10, "VAR%05d",d++),
240 psppire_dict_lookup_var (dict, name))
246 /* Insert a new variable at posn IDX, with the name NAME.
247 If NAME is null, then a name will be automatically assigned.
250 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
252 struct variable *var ;
253 g_return_if_fail (idx >= 0);
254 g_return_if_fail (d);
255 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
258 name = auto_generate_var_name (d);
260 var = dict_create_var (d->dict, name, 0);
262 dict_reorder_var (d->dict, var, idx);
265 /* Delete N variables beginning at FIRST */
267 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
270 g_return_if_fail (d);
271 g_return_if_fail (d->dict);
272 g_return_if_fail (G_IS_PSPPIRE_DICT (d));
274 for (idx = 0 ; idx < n ; ++idx )
276 struct variable *var;
278 /* Do nothing if it's out of bounds */
279 if ( first >= dict_get_var_cnt (d->dict))
282 var = dict_get_var (d->dict, first);
283 dict_delete_var (d->dict, var);
285 dict_compact_values (d->dict);
290 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
292 struct variable *var;
294 g_assert (G_IS_PSPPIRE_DICT (d));
297 if ( idx < dict_get_var_cnt (d->dict))
299 /* This is an existing variable? */
300 var = dict_get_var (d->dict, idx);
301 dict_rename_var (d->dict, var, name);
306 dict_create_var (d->dict, name, 0);
312 /* Return the IDXth variable */
314 psppire_dict_get_variable (PsppireDict *d, gint idx)
316 g_return_val_if_fail (d, NULL);
317 g_return_val_if_fail (d->dict, NULL);
319 if ( dict_get_var_cnt (d->dict) <= idx )
322 return dict_get_var (d->dict, idx);
326 /* Return the number of variables in the dictionary */
328 psppire_dict_get_var_cnt (const PsppireDict *d)
330 g_return_val_if_fail (d, -1);
331 g_return_val_if_fail (d->dict, -1);
333 return dict_get_var_cnt (d->dict);
337 /* Return a variable by name.
338 Return NULL if it doesn't exist
341 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
343 g_return_val_if_fail (d, NULL);
344 g_return_val_if_fail (d->dict, NULL);
346 return dict_lookup_var (d->dict, name);
349 /* Clears the contents of D */
351 psppire_dict_clear (PsppireDict *d)
353 g_return_if_fail (d);
354 g_return_if_fail (d->dict);
357 dict_clear (d->dict);
362 /* Return true is NAME would be a valid name of a variable to add to the
363 dictionary. False otherwise.
364 If REPORT is true, then invalid names will be reported as such as errors
367 psppire_dict_check_name (const PsppireDict *dict,
368 const gchar *name, gboolean report)
370 if ( ! var_is_valid_name (name, report ) )
373 if (psppire_dict_lookup_var (dict, name))
376 msg (ME,"Duplicate variable name.");
385 psppire_dict_get_next_value_idx (const PsppireDict *dict)
387 return dict_get_next_value_idx (dict->dict);
392 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
393 gint old_size, gint new_size)
396 g_return_if_fail (d);
397 g_return_if_fail (d->dict);
399 if ( old_size == new_size )
402 dict_compact_values (d->dict);
404 fv = var_get_case_index (pv);
406 g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
408 new_size - old_size );
412 /* Tree Model Stuff */
414 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
416 static gint tree_model_n_columns (GtkTreeModel *model);
418 static GType tree_model_column_type (GtkTreeModel *model, gint index);
420 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
423 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
425 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
428 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
429 gint column, GValue *value);
431 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
432 GtkTreeIter *parent, gint n);
436 dictionary_tree_model_init (GtkTreeModelIface *iface)
438 iface->get_flags = tree_model_get_flags;
439 iface->get_n_columns = tree_model_n_columns;
440 iface->get_column_type = tree_model_column_type;
441 iface->get_iter = tree_model_get_iter;
442 iface->iter_next = tree_model_iter_next;
443 iface->get_path = tree_model_get_path;
444 iface->get_value = tree_model_get_value;
446 iface->iter_children = 0;
447 iface->iter_has_child =0;
448 iface->iter_n_children =0;
449 iface->iter_nth_child = tree_model_nth_child ;
450 iface->iter_parent =0;
453 static GtkTreeModelFlags
454 tree_model_get_flags (GtkTreeModel *model)
456 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
458 return GTK_TREE_MODEL_LIST_ONLY;
463 tree_model_n_columns (GtkTreeModel *model)
469 tree_model_column_type (GtkTreeModel *model, gint index)
471 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
475 case DICT_TVM_COL_NAME:
476 return G_TYPE_STRING;
478 case DICT_TVM_COL_VAR:
479 return G_TYPE_POINTER;
482 g_return_val_if_reached ((GType)0);
486 g_assert_not_reached ();
491 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
493 gint *indices, depth;
495 struct variable *variable;
497 PsppireDict *dict = PSPPIRE_DICT (model);
499 g_return_val_if_fail (path, FALSE);
501 indices = gtk_tree_path_get_indices (path);
502 depth = gtk_tree_path_get_depth (path);
504 g_return_val_if_fail (depth == 1, FALSE);
508 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
511 variable = dict_get_var (dict->dict, n);
513 g_assert (var_get_dict_index (variable) == n);
515 iter->stamp = dict->stamp;
516 iter->user_data = variable;
523 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
525 PsppireDict *dict = PSPPIRE_DICT (model);
526 struct variable *variable;
529 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
531 if ( iter == NULL || iter->user_data == NULL)
534 variable = (struct variable *) iter->user_data;
536 idx = var_get_dict_index (variable);
538 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
541 variable = psppire_dict_get_variable (dict, idx + 1);
543 g_assert (var_get_dict_index (variable) == idx + 1);
545 iter->user_data = variable;
551 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
554 struct variable *variable;
555 PsppireDict *dict = PSPPIRE_DICT (model);
557 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
559 variable = (struct variable *) iter->user_data;
561 path = gtk_tree_path_new ();
562 gtk_tree_path_append_index (path, var_get_dict_index (variable));
569 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
570 gint column, GValue *value)
572 struct variable *variable;
573 PsppireDict *dict = PSPPIRE_DICT (model);
575 g_return_if_fail (iter->stamp == dict->stamp);
577 variable = (struct variable *) iter->user_data;
581 case DICT_TVM_COL_NAME:
582 g_value_init (value, G_TYPE_STRING);
583 g_value_set_string (value, var_get_name (variable));
585 case DICT_TVM_COL_VAR:
586 g_value_init (value, G_TYPE_POINTER);
587 g_value_set_pointer (value, variable);
590 g_return_if_reached ();
597 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
598 GtkTreeIter *parent, gint n)
601 g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
603 dict = PSPPIRE_DICT (model);
608 if ( n >= psppire_dict_get_var_cnt (dict) )
611 iter->stamp = dict->stamp;
612 iter->user_data = psppire_dict_get_variable (dict, n);
614 if ( !iter->user_data)
623 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
626 dict_rename_var (dict->dict, v, text);