1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <gtksheet/gtkextra-marshal.h>
24 #include "psppire-dict.h"
25 #include <data/format.h>
26 #include <data/dictionary.h>
27 #include <data/missing-values.h>
28 #include <data/value-labels.h>
29 #include <data/variable.h>
32 #include "message-dialog.h"
34 /* --- prototypes --- */
35 static void psppire_dict_class_init (PsppireDictClass *class);
36 static void psppire_dict_init (PsppireDict *dict);
37 static void psppire_dict_finalize (GObject *object);
39 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
42 /* --- variables --- */
43 static GObjectClass *parent_class = NULL;
45 enum {VARIABLE_CHANGED,
54 static guint signals [n_SIGNALS];
56 /* --- functions --- */
58 * psppire_dict_get_type:
59 * @returns: the type ID for accelerator groups.
62 psppire_dict_get_type (void)
64 static GType object_type = 0;
68 static const GTypeInfo object_info = {
69 sizeof (PsppireDictClass),
71 (GBaseFinalizeFunc) NULL,
72 (GClassInitFunc) psppire_dict_class_init,
73 NULL, /* class_finalize */
74 NULL, /* class_data */
77 (GInstanceInitFunc) psppire_dict_init,
80 static const GInterfaceInfo tree_model_info = {
81 (GInterfaceInitFunc) dictionary_tree_model_init,
86 object_type = g_type_register_static (G_TYPE_OBJECT,
90 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
101 psppire_dict_class_init (PsppireDictClass *class)
103 GObjectClass *object_class = G_OBJECT_CLASS (class);
105 parent_class = g_type_class_peek_parent (class);
107 object_class->finalize = psppire_dict_finalize;
109 signals [VARIABLE_CHANGED] =
110 g_signal_new ("variable_changed",
111 G_TYPE_FROM_CLASS (class),
115 g_cclosure_marshal_VOID__INT,
122 signals [VARIABLE_INSERTED] =
123 g_signal_new ("variable_inserted",
124 G_TYPE_FROM_CLASS (class),
128 g_cclosure_marshal_VOID__INT,
134 signals [VARIABLES_DELETED] =
135 g_signal_new ("variables_deleted",
136 G_TYPE_FROM_CLASS (class),
140 gtkextra_VOID__INT_INT,
147 signals [VARIABLE_RESIZED] =
148 g_signal_new ("dict-size-changed",
149 G_TYPE_FROM_CLASS (class),
153 gtkextra_VOID__INT_INT,
160 signals [WEIGHT_CHANGED] =
161 g_signal_new ("weight-changed",
162 G_TYPE_FROM_CLASS (class),
166 g_cclosure_marshal_VOID__INT,
172 signals [FILTER_CHANGED] =
173 g_signal_new ("filter-changed",
174 G_TYPE_FROM_CLASS (class),
178 g_cclosure_marshal_VOID__INT,
184 signals [SPLIT_CHANGED] =
185 g_signal_new ("split-changed",
186 G_TYPE_FROM_CLASS (class),
190 g_cclosure_marshal_VOID__VOID,
196 psppire_dict_finalize (GObject *object)
198 PsppireDict *d = PSPPIRE_DICT (object);
200 dict_destroy (d->dict);
202 G_OBJECT_CLASS (parent_class)->finalize (object);
205 /* Pass on callbacks from src/data/dictionary, as
206 signals in the Gtk library */
208 addcb (struct dictionary *d, int idx, void *pd)
210 g_signal_emit (pd, signals [VARIABLE_INSERTED], 0, idx);
214 delcb (struct dictionary *d, int idx, void *pd)
216 g_signal_emit (pd, signals [VARIABLES_DELETED], 0, idx, 1);
220 mutcb (struct dictionary *d, int idx, void *pd)
222 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx);
226 weight_changed_callback (struct dictionary *d, int idx, void *pd)
228 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
232 filter_changed_callback (struct dictionary *d, int idx, void *pd)
234 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
238 split_changed_callback (struct dictionary *d, void *pd)
240 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
244 static const struct dict_callbacks gui_callbacks =
249 weight_changed_callback,
250 filter_changed_callback,
251 split_changed_callback
255 psppire_dict_init (PsppireDict *psppire_dict)
257 psppire_dict->stamp = g_random_int ();
261 * psppire_dict_new_from_dict:
262 * @returns: a new #PsppireDict object
264 * Creates a new #PsppireDict.
267 psppire_dict_new_from_dict (struct dictionary *d)
269 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
272 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
279 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
281 struct variable *var = dict_get_weight (d);
284 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
286 var = dict_get_filter (d);
287 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
289 split_changed_callback (d, dict);
291 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
295 /* Returns a valid name for a new variable in DICT.
296 The return value is statically allocated */
298 auto_generate_var_name (PsppireDict *dict)
301 static gchar name[10];
303 while (g_snprintf (name, 10, "VAR%05d",d++),
304 psppire_dict_lookup_var (dict, name))
310 /* Insert a new variable at posn IDX, with the name NAME.
311 If NAME is null, then a name will be automatically assigned.
314 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
316 struct variable *var ;
317 g_return_if_fail (idx >= 0);
318 g_return_if_fail (d);
319 g_return_if_fail (PSPPIRE_IS_DICT (d));
322 name = auto_generate_var_name (d);
324 var = dict_create_var (d->dict, name, 0);
326 dict_reorder_var (d->dict, var, idx);
329 /* Delete N variables beginning at FIRST */
331 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
334 g_return_if_fail (d);
335 g_return_if_fail (d->dict);
336 g_return_if_fail (PSPPIRE_IS_DICT (d));
338 for (idx = 0 ; idx < n ; ++idx )
340 struct variable *var;
342 /* Do nothing if it's out of bounds */
343 if ( first >= dict_get_var_cnt (d->dict))
346 var = dict_get_var (d->dict, first);
347 dict_delete_var (d->dict, var);
349 dict_compact_values (d->dict);
354 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
356 struct variable *var;
358 g_assert (PSPPIRE_IS_DICT (d));
360 if ( ! var_is_valid_name (name, false))
363 if ( idx < dict_get_var_cnt (d->dict))
365 /* This is an existing variable? */
366 var = dict_get_var (d->dict, idx);
367 dict_rename_var (d->dict, var, name);
372 dict_create_var (d->dict, name, 0);
380 /* Return the IDXth variable */
382 psppire_dict_get_variable (const PsppireDict *d, gint idx)
384 g_return_val_if_fail (d, NULL);
385 g_return_val_if_fail (d->dict, NULL);
387 if ( dict_get_var_cnt (d->dict) <= idx )
390 return dict_get_var (d->dict, idx);
394 /* Return the number of variables in the dictionary */
396 psppire_dict_get_var_cnt (const PsppireDict *d)
398 g_return_val_if_fail (d, -1);
399 g_return_val_if_fail (d->dict, -1);
401 return dict_get_var_cnt (d->dict);
405 /* Return a variable by name.
406 Return NULL if it doesn't exist
409 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
411 g_return_val_if_fail (d, NULL);
412 g_return_val_if_fail (d->dict, NULL);
414 return dict_lookup_var (d->dict, name);
417 /* Clears the contents of D */
419 psppire_dict_clear (PsppireDict *d)
421 g_return_if_fail (d);
422 g_return_if_fail (d->dict);
425 dict_clear (d->dict);
430 /* Return true is NAME would be a valid name of a variable to add to the
431 dictionary. False otherwise.
432 If REPORT is true, then invalid names will be reported as such as errors
435 psppire_dict_check_name (const PsppireDict *dict,
436 const gchar *name, gboolean report)
438 if ( ! var_is_valid_name (name, report ) )
441 if (psppire_dict_lookup_var (dict, name))
444 msg (ME,"Duplicate variable name.");
453 psppire_dict_get_next_value_idx (const PsppireDict *dict)
455 return dict_get_next_value_idx (dict->dict);
460 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
461 gint old_size, gint new_size)
464 g_return_if_fail (d);
465 g_return_if_fail (d->dict);
467 if ( old_size == new_size )
470 dict_compact_values (d->dict);
472 fv = var_get_case_index (pv);
474 g_signal_emit (d, signals [VARIABLE_RESIZED], 0,
476 new_size - old_size );
480 /* Tree Model Stuff */
482 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
484 static gint tree_model_n_columns (GtkTreeModel *model);
486 static GType tree_model_column_type (GtkTreeModel *model, gint index);
488 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
491 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
493 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
496 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
497 gint column, GValue *value);
499 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
500 GtkTreeIter *parent, gint n);
502 static gint tree_model_n_children (GtkTreeModel *tree_model,
505 static gboolean tree_model_iter_children (GtkTreeModel *,
509 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
513 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
517 dictionary_tree_model_init (GtkTreeModelIface *iface)
519 iface->get_flags = tree_model_get_flags;
520 iface->get_n_columns = tree_model_n_columns;
521 iface->get_column_type = tree_model_column_type;
522 iface->get_iter = tree_model_get_iter;
523 iface->iter_next = tree_model_iter_next;
524 iface->get_path = tree_model_get_path;
525 iface->get_value = tree_model_get_value;
527 iface->iter_children = tree_model_iter_children ;
528 iface->iter_has_child = tree_model_iter_has_child ;
529 iface->iter_n_children = tree_model_n_children ;
530 iface->iter_nth_child = tree_model_nth_child ;
531 iface->iter_parent = tree_model_iter_parent ;
535 tree_model_iter_has_child (GtkTreeModel *tree_model,
542 tree_model_iter_parent (GtkTreeModel *tree_model,
549 static GtkTreeModelFlags
550 tree_model_get_flags (GtkTreeModel *model)
552 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
554 return GTK_TREE_MODEL_LIST_ONLY;
559 tree_model_n_columns (GtkTreeModel *model)
565 tree_model_column_type (GtkTreeModel *model, gint index)
567 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
571 case DICT_TVM_COL_NAME:
572 return G_TYPE_STRING;
574 case DICT_TVM_COL_VAR:
575 return G_TYPE_POINTER;
578 g_return_val_if_reached ((GType)0);
582 g_assert_not_reached ();
587 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
589 gint *indices, depth;
591 struct variable *var;
593 PsppireDict *dict = PSPPIRE_DICT (model);
595 g_return_val_if_fail (path, FALSE);
597 indices = gtk_tree_path_get_indices (path);
598 depth = gtk_tree_path_get_depth (path);
600 g_return_val_if_fail (depth == 1, FALSE);
604 if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
607 iter->user_data = NULL;
611 var = psppire_dict_get_variable (dict, n);
613 g_assert (var_get_dict_index (var) == n);
615 iter->stamp = dict->stamp;
616 iter->user_data = var;
623 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
625 PsppireDict *dict = PSPPIRE_DICT (model);
626 struct variable *var;
629 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
631 if ( iter == NULL || iter->user_data == NULL)
634 var = iter->user_data;
636 idx = var_get_dict_index (var);
638 if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
640 iter->user_data = NULL;
645 var = psppire_dict_get_variable (dict, idx + 1);
647 g_assert (var_get_dict_index (var) == idx + 1);
649 iter->user_data = var;
655 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
658 struct variable *var;
659 PsppireDict *dict = PSPPIRE_DICT (model);
661 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
663 var = iter->user_data;
665 path = gtk_tree_path_new ();
666 gtk_tree_path_append_index (path, var_get_dict_index (var));
673 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
674 gint column, GValue *value)
676 struct variable *var;
677 PsppireDict *dict = PSPPIRE_DICT (model);
679 g_return_if_fail (iter->stamp == dict->stamp);
681 var = iter->user_data;
685 case DICT_TVM_COL_NAME:
687 gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL);
688 g_value_init (value, G_TYPE_STRING);
689 g_value_set_string (value, name);
693 case DICT_TVM_COL_VAR:
694 g_value_init (value, G_TYPE_POINTER);
695 g_value_set_pointer (value, var);
698 g_return_if_reached ();
704 tree_model_iter_children (GtkTreeModel *tree_model,
712 tree_model_n_children (GtkTreeModel *model,
715 PsppireDict *dict = PSPPIRE_DICT (model);
718 return psppire_dict_get_var_cnt (dict);
724 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
725 GtkTreeIter *parent, gint n)
729 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
731 dict = PSPPIRE_DICT (model);
736 if ( n >= psppire_dict_get_var_cnt (dict) )
739 iter->stamp = dict->stamp;
740 iter->user_data = psppire_dict_get_variable (dict, n);
742 if ( !iter->user_data)
750 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
753 if ( ! var_is_valid_name (name, false))
756 dict_rename_var (dict->dict, v, name);
763 psppire_dict_get_weight_variable (const PsppireDict *dict)
765 return dict_get_weight (dict->dict);