2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2007 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
22 #include "psppire-var-select.h"
23 #include "psppire-object.h"
25 #include "psppire-dict.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
33 /* This object is an attempt to abstract a situation commonly found in PSPP
34 dialogs, where two widgets (typically GtkTreeViews) contain a list
35 of variables, and the variables may be selected by the user and
36 transfered to between the widgets, in preparation for some
39 Currently it assumes that the first widget is GtkTreeView and the
40 second is a GtkEntry (as required for the Weight Cases dialog).
41 It needs to be generalized further to make it useful.
44 static void setup_dictionary_treeview (GtkTreeView *,
49 /* --- prototypes --- */
50 static void psppire_var_select_class_init (PsppireVarSelectClass *);
51 static void psppire_var_select_init (PsppireVarSelect *);
52 static void psppire_var_select_finalize (GObject *);
55 enum {VARIABLE_SELECTED,
59 static guint signal[n_SIGNALS];
62 /* --- variables --- */
63 static GObjectClass *parent_class = NULL;
65 /* --- functions --- */
67 * psppire_var_select_get_type:
68 * @returns: the type ID for accelerator groups.
71 psppire_var_select_get_type (void)
73 static GType object_type = 0;
77 static const GTypeInfo object_info = {
78 sizeof (PsppireVarSelectClass),
80 (GBaseFinalizeFunc) NULL,
81 (GClassInitFunc) psppire_var_select_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (PsppireVarSelect),
86 (GInstanceInitFunc) psppire_var_select_init,
89 object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT,
99 psppire_var_select_class_init (PsppireVarSelectClass *class)
101 GObjectClass *object_class = G_OBJECT_CLASS (class);
103 parent_class = g_type_class_peek_parent (class);
106 signal [VARIABLE_SELECTED] =
107 g_signal_new ("variable_selected",
108 G_TYPE_FROM_CLASS (class),
112 g_cclosure_marshal_VOID__INT,
118 signal [DESELECT_ALL] =
119 g_signal_new ("deselect_all",
120 G_TYPE_FROM_CLASS (class),
124 g_cclosure_marshal_VOID__VOID,
128 object_class->finalize = psppire_var_select_finalize;
132 psppire_var_select_finalize (GObject *object)
134 PsppireVarSelect *vs = PSPPIRE_VAR_SELECT (object);
136 g_list_free (vs->list);
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 psppire_var_select_init (PsppireVarSelect *vs)
145 vs->mode = GTK_SELECTION_SINGLE;
148 /* Return list of all the currently selected variables */
150 psppire_var_select_get_variables (PsppireVarSelect *vs)
157 add_variable_to_selection (PsppireVarSelect *vs, struct variable *var)
159 gtk_entry_set_text (GTK_ENTRY (vs->dest), var_get_name (var) );
161 if ( vs->mode == GTK_SELECTION_SINGLE)
163 g_list_free (vs->list);
166 vs->list = g_list_append (vs->list, var);
168 g_signal_emit (vs, signal [VARIABLE_SELECTED], 0, var_get_dict_index (var));
172 /* Add VAR to the list of selected variables */
174 psppire_var_select_set_variable (PsppireVarSelect *vs,
175 struct variable *var)
177 add_variable_to_selection (vs, var);
182 on_source_activate (GtkTreeView *tree_view,
184 GtkTreeViewColumn *column,
188 PsppireVarSelect *vs = user_data;
189 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
191 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
193 GList *list = gtk_tree_selection_get_selected_rows (selection, &model);
197 struct variable *var;
199 GtkTreePath *path = list->data;
201 gtk_tree_model_get_iter (model, &iter, path);
203 gtk_tree_model_get (model, &iter, DICT_TVM_COL_VAR, &var, -1);
205 add_variable_to_selection (vs, var);
213 * psppire_var_select_new:
214 * @returns: a new #PsppireVarSelect object
216 * Creates a new #PsppireVarSelect.
219 psppire_var_select_new (GtkWidget *source, GtkWidget *dest,
220 const PsppireDict *dict)
223 = g_object_new (G_TYPE_PSPPIRE_VAR_SELECT, NULL);
225 GtkTreeSelection *src_selection;
232 setup_dictionary_treeview ( GTK_TREE_VIEW (source),
236 src_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (vs->source));
238 g_signal_connect (source, "row-activated",
239 G_CALLBACK (on_source_activate), vs);
245 psppire_var_select_deselect_all (PsppireVarSelect *vs)
247 g_list_free (vs->list);
250 gtk_entry_set_text ( GTK_ENTRY(vs->dest), "");
252 g_signal_emit (vs, signal [DESELECT_ALL], 0);
257 setup_dictionary_treeview (GtkTreeView *treeview, const PsppireDict *dict,
258 GtkSelectionMode mode)
260 /* Set up the dictionary treeview */
261 GtkTreeViewColumn *col;
263 GtkTreeSelection *selection =
264 gtk_tree_view_get_selection (treeview);
266 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
269 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (dict));
271 col = gtk_tree_view_column_new_with_attributes (_("Var"),
277 /* FIXME: make this a value in terms of character widths */
278 g_object_set (col, "min-width", 100, NULL);
280 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
282 gtk_tree_view_append_column (treeview, col);
284 gtk_tree_selection_set_mode (selection, mode);