1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011 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/>. */
20 #include "psppire-var-view.h"
21 #include "psppire-var-ptr.h"
22 #include "psppire-select-dest.h"
24 #include <libpspp/cast.h>
25 #include <libpspp/str.h>
26 #include <data/variable.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 static void psppire_var_view_base_finalize (PsppireVarViewClass *, gpointer);
33 static void psppire_var_view_base_init (PsppireVarViewClass *class);
34 static void psppire_var_view_class_init (PsppireVarViewClass *class);
35 static void psppire_var_view_init (PsppireVarView *var_view);
37 /* Returns TRUE iff VV contains the item V.
38 V must be an initialised value containing a
42 var_view_contains_var (PsppireSelectDestWidget *sdm, const GValue *v)
46 PsppireVarView *vv = PSPPIRE_VAR_VIEW (sdm);
47 g_return_val_if_fail (G_VALUE_HOLDS (v, PSPPIRE_VAR_PTR_TYPE), FALSE);
49 for (ok = psppire_var_view_get_iter_first (vv, &iter);
51 ok = psppire_var_view_get_iter_next (vv, &iter))
53 const struct variable *var = psppire_var_view_get_variable (vv, 0, &iter);
54 if (var == g_value_get_boxed (v))
62 model_init (PsppireSelectDestWidgetIface *iface)
64 iface->contains_var = var_view_contains_var;
68 psppire_var_view_get_type (void)
70 static GType psppire_var_view_type = 0;
72 if (!psppire_var_view_type)
74 static const GTypeInfo psppire_var_view_info =
76 sizeof (PsppireVarViewClass),
77 (GBaseInitFunc) psppire_var_view_base_init,
78 (GBaseFinalizeFunc) psppire_var_view_base_finalize,
79 (GClassInitFunc)psppire_var_view_class_init,
80 (GClassFinalizeFunc) NULL,
82 sizeof (PsppireVarView),
84 (GInstanceInitFunc) psppire_var_view_init,
87 static const GInterfaceInfo var_view_model_info = {
88 (GInterfaceInitFunc) model_init, /* Fill this in */
93 psppire_var_view_type =
94 g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireVarView",
95 &psppire_var_view_info, 0);
97 g_type_add_interface_static (psppire_var_view_type,
98 PSPPIRE_TYPE_SELECT_DEST_WIDGET,
99 &var_view_model_info);
102 return psppire_var_view_type;
106 psppire_var_view_clear (PsppireVarView *vv)
108 GtkListStore *l = gtk_list_store_newv (vv->n_cols, vv->cols);
110 gtk_tree_view_set_model (GTK_TREE_VIEW (vv), GTK_TREE_MODEL (l));
115 psppire_var_view_finalize (GObject *object)
117 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
118 g_free (var_view->nums);
119 g_free (var_view->cols);
131 /* A (*GtkTreeCellDataFunc) function.
132 This function expects TREEMODEL to hold PSPPIRE_VAR_PTR_TYPE.
133 It renders the name of the variable into CELL.
136 display_cell_var_name (GtkTreeViewColumn *tree_column,
137 GtkCellRenderer *cell,
138 GtkTreeModel *treemodel,
142 struct variable *var;
146 GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
148 gtk_tree_model_get_value (treemodel, iter, *col, &value);
150 gtk_tree_path_free (path);
152 var = g_value_get_boxed (&value);
154 g_value_unset (&value);
156 g_object_set (cell, "text", var ? var_get_name (var) : "", NULL);
161 psppire_var_view_get_property (GObject *object,
166 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
171 g_value_set_int (value, var_view->n_cols);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 set_renderers (PsppireVarView *var_view)
183 var_view->nums = g_malloc (sizeof *var_view->nums * var_view->n_cols);
185 for (c = 0 ; c < var_view->n_cols; ++c)
187 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
188 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
190 gchar *label = g_strdup_printf (_("Var%d"), c + 1);
192 gtk_tree_view_column_set_min_width (col, 100);
193 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
194 gtk_tree_view_column_set_resizable (col, TRUE);
195 gtk_tree_view_column_set_title (col, label);
199 var_view->nums[c] = c;
201 gtk_tree_view_column_pack_start (col, renderer, TRUE);
202 gtk_tree_view_column_set_cell_data_func (col, renderer,
203 display_cell_var_name,
204 &var_view->nums[c], 0);
206 gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
212 psppire_var_view_get_current_model (PsppireVarView *vv)
214 return gtk_tree_view_get_model (GTK_TREE_VIEW (vv));
218 psppire_var_view_set_property (GObject *object,
223 PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
230 var_view->n_cols = g_value_get_int (value);
232 var_view->cols = xrealloc (var_view->cols, sizeof (GType) * var_view->n_cols);
234 for (c = 0 ; c < var_view->n_cols; ++c)
235 var_view->cols[c] = PSPPIRE_VAR_PTR_TYPE;
237 set_renderers (var_view);
239 psppire_var_view_clear (var_view);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 psppire_var_view_class_init (PsppireVarViewClass *class)
251 GObjectClass *object_class = G_OBJECT_CLASS (class);
253 GParamSpec *n_cols_spec =
254 g_param_spec_int ("n-cols",
256 "The Number of Columns in the Variable View",
259 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
262 object_class->set_property = psppire_var_view_set_property;
263 object_class->get_property = psppire_var_view_get_property;
265 g_object_class_install_property (object_class,
272 psppire_var_view_base_init (PsppireVarViewClass *class)
275 GObjectClass *object_class = G_OBJECT_CLASS (class);
277 object_class->finalize = psppire_var_view_finalize;
283 psppire_var_view_base_finalize (PsppireVarViewClass *class,
291 psppire_var_view_init (PsppireVarView *vv)
293 GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (vv));
296 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
301 psppire_var_view_new (void)
303 return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
308 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
311 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (vv));
312 return gtk_tree_model_get_iter_first (model, iter ? iter : &dummy);
316 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
318 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (vv));
319 return gtk_tree_model_iter_next (model, iter);
322 const struct variable *
323 psppire_var_view_get_var_from_model (GtkTreeModel *model, gint column, GtkTreeIter *iter)
325 const struct variable *var = NULL;
327 gtk_tree_model_get_value (model, iter, column, &value);
329 if (G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
330 var = g_value_get_boxed (&value);
332 g_critical ("Unsupported type `%s', in variable name treeview.",
333 G_VALUE_TYPE_NAME (&value));
335 g_value_unset (&value);
340 const struct variable *
341 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
343 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (vv));
344 return psppire_var_view_get_var_from_model (model, column, iter);
350 Append the names of selected variables to STRING.
351 Returns the number of variables appended.
354 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
359 if (psppire_var_view_get_iter_first (vv, &iter))
363 const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
364 g_string_append (string, " ");
365 g_string_append (string, var_get_name (var));
369 while (psppire_var_view_get_iter_next (vv, &iter));
375 /* Return a linked list of struct variables which are
377 The caller is responsible for freeing the returned list.
378 The variables however are owned by their dictionary
379 and should not be freed.
382 psppire_var_view_list_names (PsppireVarView *vv, gint column)
387 if (psppire_var_view_get_iter_first (vv, &iter))
391 const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
392 list = g_slist_prepend (list, CONST_CAST (struct variable *, var));
394 while (psppire_var_view_get_iter_next (vv, &iter));
402 Append the names of selected variables to STR
403 Returns the number of variables appended.
406 psppire_var_view_append_names_str (PsppireVarView *vv, gint column, struct string *str)
411 if (psppire_var_view_get_iter_first (vv, &iter))
415 const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
416 ds_put_cstr (str, " ");
417 ds_put_cstr (str, var_get_name (var));
421 while (psppire_var_view_get_iter_next (vv, &iter));