Fixed potentially incorrect dereference in autorecode dialog
[pspp] / src / ui / gui / psppire-var-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009, 2010, 2011  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/gtk.h>
20 #include "psppire-var-view.h"
21 #include "psppire-var-ptr.h"
22 #include "psppire-select-dest.h"
23
24 #include <libpspp/str.h>
25 #include <data/variable.h>
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 static void psppire_var_view_base_finalize (PsppireVarViewClass *, gpointer);
32 static void psppire_var_view_base_init     (PsppireVarViewClass *class);
33 static void psppire_var_view_class_init    (PsppireVarViewClass *class);
34 static void psppire_var_view_init          (PsppireVarView      *var_view);
35
36 /* Returns TRUE iff VV contains the item V.
37    V must be an initialised value containing a
38    PSPPIRE_VAR_PTR_TYPE.
39 */
40 static gboolean
41 var_view_contains_var (PsppireSelectDestWidget *sdm, const GValue *v)
42 {
43   gboolean ok;
44   GtkTreeIter iter;
45   PsppireVarView *vv = PSPPIRE_VAR_VIEW (sdm);
46   g_return_val_if_fail (G_VALUE_HOLDS (v, PSPPIRE_VAR_PTR_TYPE), FALSE);
47
48   for (ok = psppire_var_view_get_iter_first (vv, &iter);
49        ok;
50        ok = psppire_var_view_get_iter_next (vv, &iter))
51     {
52       const struct variable *var = psppire_var_view_get_variable (vv, 0, &iter);
53       if (var == g_value_get_boxed (v))
54         return TRUE;
55     }
56
57   return FALSE;
58 }
59
60 static void
61 model_init (PsppireSelectDestWidgetIface *iface)
62 {
63   iface->contains_var = var_view_contains_var;
64 }
65
66 GType
67 psppire_var_view_get_type (void)
68 {
69   static GType psppire_var_view_type = 0;
70
71   if (!psppire_var_view_type)
72     {
73       static const GTypeInfo psppire_var_view_info =
74       {
75         sizeof (PsppireVarViewClass),
76         (GBaseInitFunc) psppire_var_view_base_init,
77         (GBaseFinalizeFunc) psppire_var_view_base_finalize,
78         (GClassInitFunc)psppire_var_view_class_init,
79         (GClassFinalizeFunc) NULL,
80         NULL,
81         sizeof (PsppireVarView),
82         0,
83         (GInstanceInitFunc) psppire_var_view_init,
84       };
85
86       static const GInterfaceInfo var_view_model_info = {
87         (GInterfaceInitFunc) model_init, /* Fill this in */
88         NULL,
89         NULL
90       };
91
92       psppire_var_view_type =
93         g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireVarView",
94                                 &psppire_var_view_info, 0);
95
96       g_type_add_interface_static (psppire_var_view_type,
97                                    PSPPIRE_TYPE_SELECT_DEST_WIDGET,
98                                    &var_view_model_info);
99     }
100
101   return psppire_var_view_type;
102 }
103
104 void 
105 psppire_var_view_clear (PsppireVarView *vv)
106 {
107   gint i;
108   for (i = 0; i < vv->state->n_lists; ++i)
109     g_object_unref (vv->state->list[i]);
110
111   g_free (vv->state->list);
112
113   vv->state->n_lists = 0;
114   vv->state->l_idx = -1;
115   vv->state->list = NULL;
116
117   psppire_var_view_push_model (vv);
118 }
119
120
121 static void
122 psppire_var_view_finalize (GObject *object)
123 {
124   gint i;
125   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
126   g_free (var_view->nums);
127   for (i = 0; i < var_view->state->n_lists; ++i)
128     g_object_unref (var_view->state->list[i]);
129
130   g_free (var_view->state->list);
131   g_free (var_view->cols);
132   g_free (var_view->state);
133 }
134
135
136
137 /* Properties */
138 enum
139 {
140   PROP_0,
141   PROP_N_COLS
142 };
143
144 /* A (*GtkTreeCellDataFunc) function.
145    This function expects TREEMODEL to hold PSPPIRE_VAR_PTR_TYPE.
146    It renders the name of the variable into CELL.
147 */
148 static void
149 display_cell_var_name (GtkTreeViewColumn *tree_column,
150                        GtkCellRenderer *cell,
151                        GtkTreeModel *treemodel,
152                        GtkTreeIter *iter,
153                        gpointer data)
154 {
155   struct variable *var;
156   GValue value = {0};
157   gint *col = data;
158
159   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
160
161   gtk_tree_model_get_value (treemodel, iter, *col, &value);
162
163   gtk_tree_path_free (path);
164
165   var = g_value_get_boxed (&value);
166
167   g_value_unset (&value);
168
169   g_object_set (cell, "text", var ? var_get_name (var) : "", NULL);
170 }
171
172
173 static void
174 psppire_var_view_get_property (GObject         *object,
175                                guint            prop_id,
176                                GValue          *value,
177                                GParamSpec      *pspec)
178 {
179   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
180
181   switch (prop_id)
182     {
183     case PROP_N_COLS:
184       g_value_set_int (value,  var_view->n_cols);
185       break;
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189     };
190 }
191
192 static void
193 set_renderers (PsppireVarView *var_view)
194 {
195   gint c;
196   var_view->nums = g_malloc (sizeof *var_view->nums * var_view->n_cols);
197   
198   for (c = 0 ; c < var_view->n_cols; ++c)
199     {
200       GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
201       GtkTreeViewColumn *col = gtk_tree_view_column_new ();
202       
203       gchar *label = g_strdup_printf (_("Var%d"), c + 1);
204       
205       gtk_tree_view_column_set_min_width (col, 100);
206       gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
207       gtk_tree_view_column_set_resizable (col, TRUE);
208       gtk_tree_view_column_set_title (col, label);
209       
210       g_free (label);
211       
212       var_view->nums[c] = c;
213       
214       gtk_tree_view_column_pack_start (col, renderer, TRUE);
215       gtk_tree_view_column_set_cell_data_func (col, renderer,
216                                                display_cell_var_name,
217                                                &var_view->nums[c], 0);
218       
219       gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
220     }
221 }
222
223
224
225
226 /* Set a model, which is an GtkListStore of gpointers which point to a variable */
227 void
228 psppire_var_view_push_model (PsppireVarView *vv)
229 {
230   vv->state->n_lists++;
231   vv->state->l_idx++;
232   vv->state->list = xrealloc (vv->state->list, sizeof (*vv->state->list) * vv->state->n_lists);
233   vv->state->list[vv->state->l_idx] = gtk_list_store_newv  (vv->n_cols, vv->cols);
234   g_object_ref (vv->state->list[vv->state->l_idx]);
235   gtk_tree_view_set_model (GTK_TREE_VIEW (vv), GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]));
236 }
237
238
239 GtkTreeModel *
240 psppire_var_view_get_current_model (PsppireVarView *vv)
241 {
242   if (vv->state == NULL) 
243     return NULL;
244
245   if (vv->state->list == NULL) 
246     return NULL;
247
248   if (vv->state->l_idx <= 0) 
249     return NULL;
250
251   return GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]);
252 }
253
254
255 gboolean
256 psppire_var_view_set_current_model (PsppireVarView *vv, gint n)
257 {
258   if (n < 0 || n >= vv->state->n_lists)
259     return FALSE;
260
261   vv->state->l_idx = n;
262
263   gtk_tree_view_set_model (GTK_TREE_VIEW (vv), GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]));
264
265   return TRUE;
266 }
267
268 static void
269 psppire_var_view_set_property (GObject         *object,
270                                guint            prop_id,
271                                const GValue    *value,
272                                GParamSpec      *pspec)
273 {
274   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
275
276   switch (prop_id)
277     {
278     case PROP_N_COLS:
279       {
280         gint c;
281         var_view->n_cols = g_value_get_int (value);
282
283         var_view->cols = xrealloc (var_view->cols, sizeof (GType) *  var_view->n_cols);
284
285         for (c = 0 ; c < var_view->n_cols; ++c)
286           var_view->cols[c] = PSPPIRE_VAR_PTR_TYPE;
287
288         set_renderers (var_view);
289
290         psppire_var_view_clear (var_view);
291       }
292       break;
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296     };
297 }
298
299 static void
300 psppire_var_view_class_init (PsppireVarViewClass *class)
301 {
302   GObjectClass *object_class = G_OBJECT_CLASS (class);
303
304   GParamSpec *n_cols_spec =
305     g_param_spec_int ("n-cols",
306                       "Number of columns",
307                       "The Number of Columns in the Variable View",
308                       1, 20,
309                       1,
310                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
311
312
313   object_class->set_property = psppire_var_view_set_property;
314   object_class->get_property = psppire_var_view_get_property;
315
316   g_object_class_install_property (object_class,
317                                    PROP_N_COLS,
318                                    n_cols_spec);
319 }
320
321
322 static void
323 psppire_var_view_base_init (PsppireVarViewClass *class)
324 {
325
326   GObjectClass *object_class = G_OBJECT_CLASS (class);
327
328
329
330   object_class->finalize = psppire_var_view_finalize;
331 }
332
333
334
335 static void
336 psppire_var_view_base_finalize (PsppireVarViewClass *class,
337                                  gpointer class_data)
338 {
339 }
340
341
342
343 static void
344 psppire_var_view_init (PsppireVarView *vv)
345 {
346   vv->cols = 0;
347   vv->state = g_malloc (sizeof *vv->state);
348   vv->state->n_lists = 0;
349   vv->state->l_idx = -1;
350   vv->state->list = NULL;
351 }
352
353
354 GtkWidget*
355 psppire_var_view_new (void)
356 {
357   return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
358 }
359
360
361 gboolean
362 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
363 {
364   GtkTreeIter dummy;
365   if ( vv->state->l_idx < 0)
366     return FALSE;
367
368   return gtk_tree_model_get_iter_first (GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]), iter ? iter : &dummy);
369 }
370
371 gboolean
372 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
373 {
374   if ( vv->state->l_idx < 0)
375     return FALSE;
376
377   return gtk_tree_model_iter_next (GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]), iter);
378 }
379
380 const struct variable *
381 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
382 {
383   const struct variable *var = NULL;
384   GValue value = {0};
385   gtk_tree_model_get_value (GTK_TREE_MODEL (vv->state->list[vv->state->l_idx]), iter, column, &value);
386
387   if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
388     var = g_value_get_boxed (&value);
389   else
390     g_critical ("Unsupported type `%s', in variable name treeview.",
391                 G_VALUE_TYPE_NAME (&value));
392
393   g_value_unset (&value);
394
395   return var;
396 }
397
398 /*
399   Append the names of selected variables to STRING.
400   Returns the number of variables appended.
401 */
402 gint
403 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
404 {
405   gint n_vars = 0;
406   GtkTreeIter iter;
407
408   if ( psppire_var_view_get_iter_first (vv, &iter) )
409     {
410       do
411         {
412           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
413           g_string_append (string, " ");
414           g_string_append (string, var_get_name (var));
415
416           n_vars++;
417         }
418       while (psppire_var_view_get_iter_next (vv, &iter));
419     }
420
421   return n_vars;
422 }
423
424
425 /*
426   Append the names of selected variables to STR
427   Returns the number of variables appended.
428 */
429 gint 
430 psppire_var_view_append_names_str (PsppireVarView *vv, gint column, struct string *str)
431 {
432   gint n_vars = 0;
433   GtkTreeIter iter;
434
435   if ( psppire_var_view_get_iter_first (vv, &iter) )
436     {
437       do
438         {
439           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
440           ds_put_cstr (str, " ");
441           ds_put_cstr (str, var_get_name (var));
442
443           n_vars++;
444         }
445       while (psppire_var_view_get_iter_next (vv, &iter));
446     }
447
448   return n_vars;
449 }
450
451
452