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