1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009 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/>. */
19 #include <gtk/gtktreeview.h>
20 #include <gtk/gtkcellrenderertext.h>
21 #include "psppire-var-view.h"
22 #include "psppire-var-ptr.h"
24 #include <data/variable.h>
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
30 static void psppire_var_view_base_finalize (PsppireVarViewClass *, gpointer);
31 static void psppire_var_view_base_init (PsppireVarViewClass *class);
32 static void psppire_var_view_class_init (PsppireVarViewClass *class);
33 static void psppire_var_view_init (PsppireVarView *var_view);
37 psppire_var_view_get_type (void)
39 static GType psppire_var_view_type = 0;
41 if (!psppire_var_view_type)
43 static const GTypeInfo psppire_var_view_info =
45 sizeof (PsppireVarViewClass),
46 (GBaseInitFunc) psppire_var_view_base_init,
47 (GBaseFinalizeFunc) psppire_var_view_base_finalize,
48 (GClassInitFunc)psppire_var_view_class_init,
49 (GClassFinalizeFunc) NULL,
51 sizeof (PsppireVarView),
53 (GInstanceInitFunc) psppire_var_view_init,
56 psppire_var_view_type =
57 g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireVarView",
58 &psppire_var_view_info, 0);
61 return psppire_var_view_type;
66 psppire_var_view_finalize (GObject *object)
68 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
69 g_free (var_view->nums);
79 /* A (*GtkTreeCellDataFunc) function.
80 This function expects TREEMODEL to hold PSPPIRE_VAR_PTR_TYPE.
81 It renders the name of the variable into CELL.
84 display_cell_var_name (GtkTreeViewColumn *tree_column,
85 GtkCellRenderer *cell,
86 GtkTreeModel *treemodel,
94 GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
96 gtk_tree_model_get_value (treemodel, iter, *col, &value);
98 gtk_tree_path_free (path);
100 var = g_value_get_boxed (&value);
102 g_value_unset (&value);
104 g_object_set (cell, "text", var_get_name (var), NULL);
109 psppire_var_view_get_property (GObject *object,
114 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
119 g_value_set_int (value, gtk_tree_model_iter_n_children (GTK_TREE_MODEL (var_view->list), NULL));
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 psppire_var_view_set_property (GObject *object,
134 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
140 gint n_cols = g_value_get_int (value);
144 GType *array = g_alloca (sizeof (GType) * n_cols);
146 var_view->nums = g_malloc (sizeof *var_view->nums * n_cols);
148 for (c = 0 ; c < n_cols; ++c)
150 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
151 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
153 gchar *label = g_strdup_printf (_("Var%d"), c + 1);
155 gtk_tree_view_column_set_min_width (col, 100);
156 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
157 gtk_tree_view_column_set_resizable (col, TRUE);
158 gtk_tree_view_column_set_title (col, label);
162 var_view->nums[c] = c;
164 gtk_tree_view_column_pack_start (col, renderer, TRUE);
165 gtk_tree_view_column_set_cell_data_func (col, renderer,
166 display_cell_var_name,
167 &var_view->nums[c], 0);
169 gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
170 array[c] = PSPPIRE_VAR_PTR_TYPE;
173 /* Set a model, which is an GtkListStore of gpointers which point to a variable */
174 var_view->list = gtk_list_store_newv (n_cols, array);
175 gtk_tree_view_set_model (GTK_TREE_VIEW (var_view), GTK_TREE_MODEL (var_view->list));
179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 psppire_var_view_class_init (PsppireVarViewClass *class)
187 GObjectClass *object_class = G_OBJECT_CLASS (class);
189 GParamSpec *n_cols_spec =
190 g_param_spec_int ("n-cols",
192 "The Number of Columns in the Variable View",
195 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
198 object_class->set_property = psppire_var_view_set_property;
199 object_class->get_property = psppire_var_view_get_property;
201 g_object_class_install_property (object_class,
209 psppire_var_view_base_init (PsppireVarViewClass *class)
211 GObjectClass *object_class = G_OBJECT_CLASS (class);
213 object_class->finalize = psppire_var_view_finalize;
219 psppire_var_view_base_finalize (PsppireVarViewClass *class,
227 psppire_var_view_init (PsppireVarView *var_view)
233 psppire_var_view_new (void)
235 return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
240 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
242 return gtk_tree_model_get_iter_first (GTK_TREE_MODEL (vv->list), iter);
246 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
248 return gtk_tree_model_iter_next (GTK_TREE_MODEL (vv->list), iter);
251 const struct variable *
252 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
254 const struct variable *var = NULL;
256 gtk_tree_model_get_value (GTK_TREE_MODEL (vv->list), iter, column, &value);
258 if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
259 var = g_value_get_boxed (&value);
261 g_critical ("Unsupported type \"%s\", in variable name treeview.",
262 G_VALUE_TYPE_NAME (&value));
264 g_value_unset (&value);
270 Append the names of selected variables to STRING.
271 Returns the number of variables appended.
274 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
279 if ( psppire_var_view_get_iter_first (vv, &iter) )
283 const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
284 g_string_append (string, " ");
285 g_string_append (string, var_get_name (var));
289 while (psppire_var_view_get_iter_next (vv, &iter));