New function psppire_var_view_contains_var
[pspp-builds.git] / src / ui / gui / psppire-var-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gtk/gtktreeview.h>
20 #include <gtk/gtkcellrenderertext.h>
21 #include "psppire-var-view.h"
22 #include "psppire-var-ptr.h"
23
24 #include <data/variable.h>
25
26 #include <gettext.h>
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
29
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);
34
35
36 GType
37 psppire_var_view_get_type (void)
38 {
39   static GType psppire_var_view_type = 0;
40
41   if (!psppire_var_view_type)
42     {
43       static const GTypeInfo psppire_var_view_info =
44       {
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,
50         NULL,
51         sizeof (PsppireVarView),
52         0,
53         (GInstanceInitFunc) psppire_var_view_init,
54       };
55
56       psppire_var_view_type =
57         g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireVarView",
58                                 &psppire_var_view_info, 0);
59     }
60
61   return psppire_var_view_type;
62 }
63
64
65 static void
66 psppire_var_view_finalize (GObject *object)
67 {
68   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
69   g_free (var_view->nums);
70 }
71
72 /* Properties */
73 enum
74 {
75   PROP_0,
76   PROP_N_COLS
77 };
78
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.
82 */
83 static void
84 display_cell_var_name (GtkTreeViewColumn *tree_column,
85                        GtkCellRenderer *cell,
86                        GtkTreeModel *treemodel,
87                        GtkTreeIter *iter,
88                        gpointer data)
89 {
90   struct variable *var;
91   GValue value = {0};
92   gint *col = data;
93
94   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
95
96   gtk_tree_model_get_value (treemodel, iter, *col, &value);
97
98   gtk_tree_path_free (path);
99
100   var = g_value_get_boxed (&value);
101
102   g_value_unset (&value);
103
104   g_object_set (cell, "text", var_get_name (var), NULL);
105 }
106
107
108 static void
109 psppire_var_view_get_property (GObject         *object,
110                                guint            prop_id,
111                                GValue          *value,
112                                GParamSpec      *pspec)
113 {
114   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
115
116   switch (prop_id)
117     {
118     case PROP_N_COLS:
119       g_value_set_int (value,  gtk_tree_model_iter_n_children (GTK_TREE_MODEL (var_view->list), NULL));
120       break;
121     default:
122       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123       break;
124     };
125 }
126
127
128 static void
129 psppire_var_view_set_property (GObject         *object,
130                                guint            prop_id,
131                                const GValue    *value,
132                                GParamSpec      *pspec)
133 {
134   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
135
136   switch (prop_id)
137     {
138     case PROP_N_COLS:
139       {
140         gint n_cols = g_value_get_int (value);
141         gint c;
142
143
144         GType *array = g_alloca (sizeof (GType) *  n_cols);
145
146         var_view->nums = g_malloc (sizeof *var_view->nums * n_cols);
147
148         for (c = 0 ; c < n_cols; ++c)
149         {
150           GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
151           GtkTreeViewColumn *col = gtk_tree_view_column_new ();
152
153           gchar *label = g_strdup_printf (_("Var%d"), c + 1);
154
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);
159
160           g_free (label);
161
162           var_view->nums[c] = c;
163
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);
168
169           gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
170           array[c] = PSPPIRE_VAR_PTR_TYPE;
171         }
172
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));
176       }
177       break;
178     default:
179       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180       break;
181     };
182 }
183
184 static void
185 psppire_var_view_class_init (PsppireVarViewClass *class)
186 {
187   GObjectClass *object_class = G_OBJECT_CLASS (class);
188
189   GParamSpec *n_cols_spec =
190     g_param_spec_int ("n-cols",
191                       "Number of columns",
192                       "The Number of Columns in the Variable View",
193                       1, 20,
194                       1,
195                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
196
197
198   object_class->set_property = psppire_var_view_set_property;
199   object_class->get_property = psppire_var_view_get_property;
200
201   g_object_class_install_property (object_class,
202                                    PROP_N_COLS,
203                                    n_cols_spec);
204 }
205
206
207 static void
208 psppire_var_view_base_init (PsppireVarViewClass *class)
209 {
210   GObjectClass *object_class = G_OBJECT_CLASS (class);
211
212   object_class->finalize = psppire_var_view_finalize;
213 }
214
215
216
217 static void
218 psppire_var_view_base_finalize (PsppireVarViewClass *class,
219                                  gpointer class_data)
220 {
221 }
222
223
224
225 static void
226 psppire_var_view_init (PsppireVarView *var_view)
227 {
228 }
229
230
231 GtkWidget*
232 psppire_var_view_new (void)
233 {
234   return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
235 }
236
237
238 gboolean
239 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
240 {
241   return gtk_tree_model_get_iter_first (GTK_TREE_MODEL (vv->list), iter);
242 }
243
244 gboolean
245 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
246 {
247   return gtk_tree_model_iter_next (GTK_TREE_MODEL (vv->list), iter);
248 }
249
250 const struct variable *
251 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
252 {
253   const struct variable *var = NULL;
254   GValue value = {0};
255   gtk_tree_model_get_value (GTK_TREE_MODEL (vv->list), iter, column, &value);
256
257   if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
258     var = g_value_get_boxed (&value);
259   else
260     g_critical ("Unsupported type \"%s\", in variable name treeview.",
261                 G_VALUE_TYPE_NAME (&value));
262
263   g_value_unset (&value);
264
265   return var;
266 }
267
268 /*
269   Append the names of selected variables to STRING.
270   Returns the number of variables appended.
271 */
272 gint
273 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
274 {
275   gint n_vars = 0;
276   GtkTreeIter iter;
277
278   if ( psppire_var_view_get_iter_first (vv, &iter) )
279     {
280       do
281         {
282           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
283           g_string_append (string, " ");
284           g_string_append (string, var_get_name (var));
285
286           n_vars++;
287         }
288       while (psppire_var_view_get_iter_next (vv, &iter));
289     }
290
291   return n_vars;
292 }
293
294 /* Returns TRUE iff VV contains the item V.
295    V must be an initialised value containing a
296    PSPPIRE_VAR_PTR_TYPE.
297 */
298 gboolean
299 psppire_var_view_contains_var (PsppireVarView *vv, const GValue *v)
300 {
301   gboolean ok;
302   GtkTreeIter iter;
303   g_return_val_if_fail (G_VALUE_HOLDS (v, PSPPIRE_VAR_PTR_TYPE), FALSE);
304
305   for (ok = psppire_var_view_get_iter_first (vv, &iter);
306        ok;
307        ok = psppire_var_view_get_iter_next (vv, &iter))
308     {
309       const struct variable *var = psppire_var_view_get_variable (vv, 0, &iter);
310       if (var == g_value_get_boxed (v))
311         return TRUE;
312     }
313
314   return FALSE;
315 }
316