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