Merge 'master' into 'psppsheet'.
[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->n_lists; ++i)
109     g_object_unref (vv->list[i]);
110
111   g_free (vv->list);
112
113   vv->n_lists = 0;
114   vv->l_idx = -1;
115   vv->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->n_lists; ++i)
128     g_object_unref (var_view->list[i]);
129
130   g_free (var_view->list);
131   g_free (var_view->cols);
132 }
133
134
135
136 /* Properties */
137 enum
138 {
139   PROP_0,
140   PROP_N_COLS
141 };
142
143 /* A (*GtkTreeCellDataFunc) function.
144    This function expects TREEMODEL to hold PSPPIRE_VAR_PTR_TYPE.
145    It renders the name of the variable into CELL.
146 */
147 static void
148 display_cell_var_name (GtkTreeViewColumn *tree_column,
149                        GtkCellRenderer *cell,
150                        GtkTreeModel *treemodel,
151                        GtkTreeIter *iter,
152                        gpointer data)
153 {
154   struct variable *var;
155   GValue value = {0};
156   gint *col = data;
157
158   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
159
160   gtk_tree_model_get_value (treemodel, iter, *col, &value);
161
162   gtk_tree_path_free (path);
163
164   var = g_value_get_boxed (&value);
165
166   g_value_unset (&value);
167
168   g_object_set (cell, "text", var ? var_get_name (var) : "", NULL);
169 }
170
171
172 static void
173 psppire_var_view_get_property (GObject         *object,
174                                guint            prop_id,
175                                GValue          *value,
176                                GParamSpec      *pspec)
177 {
178   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
179
180   switch (prop_id)
181     {
182     case PROP_N_COLS:
183       g_value_set_int (value,  var_view->n_cols);
184       break;
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188     };
189 }
190
191 static void
192 set_renderers (PsppireVarView *var_view)
193 {
194   gint c;
195   var_view->nums = g_malloc (sizeof *var_view->nums * var_view->n_cols);
196   
197   for (c = 0 ; c < var_view->n_cols; ++c)
198     {
199       GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
200       GtkTreeViewColumn *col = gtk_tree_view_column_new ();
201       
202       gchar *label = g_strdup_printf (_("Var%d"), c + 1);
203       
204       gtk_tree_view_column_set_min_width (col, 100);
205       gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
206       gtk_tree_view_column_set_resizable (col, TRUE);
207       gtk_tree_view_column_set_title (col, label);
208       
209       g_free (label);
210       
211       var_view->nums[c] = c;
212       
213       gtk_tree_view_column_pack_start (col, renderer, TRUE);
214       gtk_tree_view_column_set_cell_data_func (col, renderer,
215                                                display_cell_var_name,
216                                                &var_view->nums[c], 0);
217       
218       gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
219     }
220 }
221
222
223
224
225 /* Set a model, which is an GtkListStore of gpointers which point to a variable */
226 void
227 psppire_var_view_push_model (PsppireVarView *vv)
228 {
229   vv->n_lists++;
230   vv->l_idx++;
231   vv->list = xrealloc (vv->list, sizeof (*vv->list) * vv->n_lists);
232   vv->list[vv->l_idx] = gtk_list_store_newv  (vv->n_cols, vv->cols);
233   g_object_ref (vv->list[vv->l_idx]);
234   gtk_tree_view_set_model (GTK_TREE_VIEW (vv), GTK_TREE_MODEL (vv->list[vv->l_idx]));
235 }
236
237 gboolean
238 psppire_var_view_set_current_model (PsppireVarView *vv, gint n)
239 {
240   if (n < 0 || n >= vv->n_lists)
241     return FALSE;
242
243   vv->l_idx = n;
244
245   gtk_tree_view_set_model (GTK_TREE_VIEW (vv), GTK_TREE_MODEL (vv->list[vv->l_idx]));
246
247   return TRUE;
248 }
249
250 static void
251 psppire_var_view_set_property (GObject         *object,
252                                guint            prop_id,
253                                const GValue    *value,
254                                GParamSpec      *pspec)
255 {
256   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
257
258   switch (prop_id)
259     {
260     case PROP_N_COLS:
261       {
262         gint c;
263         var_view->n_cols = g_value_get_int (value);
264
265         var_view->cols = xrealloc (var_view->cols, sizeof (GType) *  var_view->n_cols);
266
267         for (c = 0 ; c < var_view->n_cols; ++c)
268           var_view->cols[c] = PSPPIRE_VAR_PTR_TYPE;
269
270         set_renderers (var_view);
271
272         psppire_var_view_clear (var_view);
273       }
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278     };
279 }
280
281 static void
282 psppire_var_view_class_init (PsppireVarViewClass *class)
283 {
284   GObjectClass *object_class = G_OBJECT_CLASS (class);
285
286   GParamSpec *n_cols_spec =
287     g_param_spec_int ("n-cols",
288                       "Number of columns",
289                       "The Number of Columns in the Variable View",
290                       1, 20,
291                       1,
292                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
293
294
295   object_class->set_property = psppire_var_view_set_property;
296   object_class->get_property = psppire_var_view_get_property;
297
298   g_object_class_install_property (object_class,
299                                    PROP_N_COLS,
300                                    n_cols_spec);
301 }
302
303
304 static void
305 psppire_var_view_base_init (PsppireVarViewClass *class)
306 {
307
308   GObjectClass *object_class = G_OBJECT_CLASS (class);
309
310
311
312   object_class->finalize = psppire_var_view_finalize;
313 }
314
315
316
317 static void
318 psppire_var_view_base_finalize (PsppireVarViewClass *class,
319                                  gpointer class_data)
320 {
321 }
322
323
324
325 static void
326 psppire_var_view_init (PsppireVarView *vv)
327 {
328   vv->cols = 0;
329   vv->n_lists = 0;
330   vv->l_idx = -1;
331   vv->list = NULL;
332 }
333
334
335 GtkWidget*
336 psppire_var_view_new (void)
337 {
338   return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
339 }
340
341
342 gboolean
343 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
344 {
345   GtkTreeIter dummy;
346   if ( vv->l_idx < 0)
347     return FALSE;
348
349   return gtk_tree_model_get_iter_first (GTK_TREE_MODEL (vv->list[vv->l_idx]), iter ? iter : &dummy);
350 }
351
352 gboolean
353 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
354 {
355   if ( vv->l_idx < 0)
356     return FALSE;
357
358   return gtk_tree_model_iter_next (GTK_TREE_MODEL (vv->list[vv->l_idx]), iter);
359 }
360
361 const struct variable *
362 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
363 {
364   const struct variable *var = NULL;
365   GValue value = {0};
366   gtk_tree_model_get_value (GTK_TREE_MODEL (vv->list[vv->l_idx]), iter, column, &value);
367
368   if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
369     var = g_value_get_boxed (&value);
370   else
371     g_critical ("Unsupported type `%s', in variable name treeview.",
372                 G_VALUE_TYPE_NAME (&value));
373
374   g_value_unset (&value);
375
376   return var;
377 }
378
379 /*
380   Append the names of selected variables to STRING.
381   Returns the number of variables appended.
382 */
383 gint
384 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
385 {
386   gint n_vars = 0;
387   GtkTreeIter iter;
388
389   if ( psppire_var_view_get_iter_first (vv, &iter) )
390     {
391       do
392         {
393           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
394           g_string_append (string, " ");
395           g_string_append (string, var_get_name (var));
396
397           n_vars++;
398         }
399       while (psppire_var_view_get_iter_next (vv, &iter));
400     }
401
402   return n_vars;
403 }
404
405
406 /*
407   Append the names of selected variables to STR
408   Returns the number of variables appended.
409 */
410 gint 
411 psppire_var_view_append_names_str (PsppireVarView *vv, gint column, struct string *str)
412 {
413   gint n_vars = 0;
414   GtkTreeIter iter;
415
416   if ( psppire_var_view_get_iter_first (vv, &iter) )
417     {
418       do
419         {
420           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
421           ds_put_cstr (str, " ");
422           ds_put_cstr (str, var_get_name (var));
423
424           n_vars++;
425         }
426       while (psppire_var_view_get_iter_next (vv, &iter));
427     }
428
429   return n_vars;
430 }
431
432
433