New interface PsppireSelectDestWidget.
[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 #include "psppire-select-dest.h"
24
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
105 static void
106 psppire_var_view_finalize (GObject *object)
107 {
108   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
109   g_free (var_view->nums);
110 }
111
112 /* Properties */
113 enum
114 {
115   PROP_0,
116   PROP_N_COLS
117 };
118
119 /* A (*GtkTreeCellDataFunc) function.
120    This function expects TREEMODEL to hold PSPPIRE_VAR_PTR_TYPE.
121    It renders the name of the variable into CELL.
122 */
123 static void
124 display_cell_var_name (GtkTreeViewColumn *tree_column,
125                        GtkCellRenderer *cell,
126                        GtkTreeModel *treemodel,
127                        GtkTreeIter *iter,
128                        gpointer data)
129 {
130   struct variable *var;
131   GValue value = {0};
132   gint *col = data;
133
134   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
135
136   gtk_tree_model_get_value (treemodel, iter, *col, &value);
137
138   gtk_tree_path_free (path);
139
140   var = g_value_get_boxed (&value);
141
142   g_value_unset (&value);
143
144   g_object_set (cell, "text", var_get_name (var), NULL);
145 }
146
147
148 static void
149 psppire_var_view_get_property (GObject         *object,
150                                guint            prop_id,
151                                GValue          *value,
152                                GParamSpec      *pspec)
153 {
154   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
155
156   switch (prop_id)
157     {
158     case PROP_N_COLS:
159       g_value_set_int (value,  gtk_tree_model_iter_n_children (GTK_TREE_MODEL (var_view->list), NULL));
160       break;
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163       break;
164     };
165 }
166
167
168 static void
169 psppire_var_view_set_property (GObject         *object,
170                                guint            prop_id,
171                                const GValue    *value,
172                                GParamSpec      *pspec)
173 {
174   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (object);
175
176   switch (prop_id)
177     {
178     case PROP_N_COLS:
179       {
180         gint n_cols = g_value_get_int (value);
181         gint c;
182
183
184         GType *array = g_alloca (sizeof (GType) *  n_cols);
185
186         var_view->nums = g_malloc (sizeof *var_view->nums * n_cols);
187
188         for (c = 0 ; c < n_cols; ++c)
189         {
190           GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
191           GtkTreeViewColumn *col = gtk_tree_view_column_new ();
192
193           gchar *label = g_strdup_printf (_("Var%d"), c + 1);
194
195           gtk_tree_view_column_set_min_width (col, 100);
196           gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
197           gtk_tree_view_column_set_resizable (col, TRUE);
198           gtk_tree_view_column_set_title (col, label);
199
200           g_free (label);
201
202           var_view->nums[c] = c;
203
204           gtk_tree_view_column_pack_start (col, renderer, TRUE);
205           gtk_tree_view_column_set_cell_data_func (col, renderer,
206                                                    display_cell_var_name,
207                                                    &var_view->nums[c], 0);
208
209           gtk_tree_view_append_column (GTK_TREE_VIEW (var_view), col);
210           array[c] = PSPPIRE_VAR_PTR_TYPE;
211         }
212
213         /* Set a model, which is an GtkListStore of gpointers which point to a variable */
214         var_view->list = gtk_list_store_newv  (n_cols, array);
215         gtk_tree_view_set_model (GTK_TREE_VIEW (var_view), GTK_TREE_MODEL (var_view->list));
216       }
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221     };
222 }
223
224 static void
225 psppire_var_view_class_init (PsppireVarViewClass *class)
226 {
227   GObjectClass *object_class = G_OBJECT_CLASS (class);
228
229   GParamSpec *n_cols_spec =
230     g_param_spec_int ("n-cols",
231                       "Number of columns",
232                       "The Number of Columns in the Variable View",
233                       1, 20,
234                       1,
235                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE);
236
237
238   object_class->set_property = psppire_var_view_set_property;
239   object_class->get_property = psppire_var_view_get_property;
240
241   g_object_class_install_property (object_class,
242                                    PROP_N_COLS,
243                                    n_cols_spec);
244 }
245
246
247 static void
248 psppire_var_view_base_init (PsppireVarViewClass *class)
249 {
250   GObjectClass *object_class = G_OBJECT_CLASS (class);
251
252   object_class->finalize = psppire_var_view_finalize;
253 }
254
255
256
257 static void
258 psppire_var_view_base_finalize (PsppireVarViewClass *class,
259                                  gpointer class_data)
260 {
261 }
262
263
264
265 static void
266 psppire_var_view_init (PsppireVarView *var_view)
267 {
268 }
269
270
271 GtkWidget*
272 psppire_var_view_new (void)
273 {
274   return GTK_WIDGET (g_object_new (psppire_var_view_get_type (), NULL));
275 }
276
277
278 gboolean
279 psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter)
280 {
281   return gtk_tree_model_get_iter_first (GTK_TREE_MODEL (vv->list), iter);
282 }
283
284 gboolean
285 psppire_var_view_get_iter_next (PsppireVarView *vv, GtkTreeIter *iter)
286 {
287   return gtk_tree_model_iter_next (GTK_TREE_MODEL (vv->list), iter);
288 }
289
290 const struct variable *
291 psppire_var_view_get_variable (PsppireVarView *vv, gint column, GtkTreeIter *iter)
292 {
293   const struct variable *var = NULL;
294   GValue value = {0};
295   gtk_tree_model_get_value (GTK_TREE_MODEL (vv->list), iter, column, &value);
296
297   if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
298     var = g_value_get_boxed (&value);
299   else
300     g_critical ("Unsupported type \"%s\", in variable name treeview.",
301                 G_VALUE_TYPE_NAME (&value));
302
303   g_value_unset (&value);
304
305   return var;
306 }
307
308 /*
309   Append the names of selected variables to STRING.
310   Returns the number of variables appended.
311 */
312 gint
313 psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string)
314 {
315   gint n_vars = 0;
316   GtkTreeIter iter;
317
318   if ( psppire_var_view_get_iter_first (vv, &iter) )
319     {
320       do
321         {
322           const struct variable *var = psppire_var_view_get_variable (vv, column, &iter);
323           g_string_append (string, " ");
324           g_string_append (string, var_get_name (var));
325
326           n_vars++;
327         }
328       while (psppire_var_view_get_iter_next (vv, &iter));
329     }
330
331   return n_vars;
332 }