Added a dict parameter to data_in and dealt with the consequences.
[pspp-builds.git] / src / ui / gui / psppire-dictview.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 "psppire-dictview.h"
21 #include "psppire-dict.h"
22 #include "psppire-conf.h"
23 #include <data/format.h>
24 #include <libpspp/i18n.h>
25 #include "helper.h"
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 static void psppire_dict_view_base_finalize (PsppireDictViewClass *, gpointer);
32 static void psppire_dict_view_base_init     (PsppireDictViewClass *class);
33 static void psppire_dict_view_class_init    (PsppireDictViewClass *class);
34 static void psppire_dict_view_init          (PsppireDictView      *dict_view);
35
36
37 GType
38 psppire_dict_view_get_type (void)
39 {
40   static GType psppire_dict_view_type = 0;
41
42   if (!psppire_dict_view_type)
43     {
44       static const GTypeInfo psppire_dict_view_info =
45       {
46         sizeof (PsppireDictViewClass),
47         (GBaseInitFunc) psppire_dict_view_base_init,
48         (GBaseFinalizeFunc) psppire_dict_view_base_finalize,
49         (GClassInitFunc)psppire_dict_view_class_init,
50         (GClassFinalizeFunc) NULL,
51         NULL,
52         sizeof (PsppireDictView),
53         0,
54         (GInstanceInitFunc) psppire_dict_view_init,
55       };
56
57       psppire_dict_view_type =
58         g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireDictView",
59                                 &psppire_dict_view_info, 0);
60     }
61
62   return psppire_dict_view_type;
63 }
64
65
66 static void
67 psppire_dict_view_finalize (GObject *object)
68 {
69   PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
70
71   g_object_unref (dict_view->menu);
72 }
73
74 /* Properties */
75 enum
76 {
77   PROP_0,
78   PROP_MODEL,
79   PROP_DICTIONARY,
80   PROP_PREDICATE,
81   PROP_SELECTION_MODE
82 };
83
84
85 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
86 static gboolean
87 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
88 {
89   var_predicate_func *predicate = data;
90   struct variable *var;
91   PsppireDict *dict = PSPPIRE_DICT (model);
92
93   GtkTreePath *path = gtk_tree_model_get_path (model, iter);
94
95   gint *idx = gtk_tree_path_get_indices (path);
96
97   var =  psppire_dict_get_variable (dict, *idx);
98
99   gtk_tree_path_free (path);
100
101   return predicate (var);
102 }
103
104 static void
105 set_model (PsppireDictView *dict_view)
106 {
107   GtkTreeModel *model ;
108
109   if ( dict_view->predicate )
110     {
111       model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict_view->dict),
112                                          NULL);
113
114       gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
115                                               filter_variables,
116                                               dict_view->predicate,
117                                               NULL);
118     }
119   else
120     {
121       model = GTK_TREE_MODEL (dict_view->dict);
122     }
123
124   gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
125 }
126
127 static void
128 psppire_dict_view_set_property (GObject         *object,
129                                guint            prop_id,
130                                const GValue    *value,
131                                GParamSpec      *pspec)
132 {
133   PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
134
135   switch (prop_id)
136     {
137     case PROP_DICTIONARY:
138       dict_view->dict = g_value_get_object (value);
139       break;
140     case PROP_MODEL:
141       g_critical ("Don't set the \"model\" property on %s. "
142                   "Use the \"dictionary\" property instead.",
143                   G_OBJECT_TYPE_NAME (dict_view));
144       break;
145     case PROP_PREDICATE:
146       dict_view->predicate = g_value_get_pointer (value);
147       break;
148     case PROP_SELECTION_MODE:
149       {
150         GtkTreeSelection *selection =
151           gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
152
153         GtkSelectionMode mode = g_value_get_enum (value);
154
155         gtk_tree_selection_set_mode (selection, mode);
156       }
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161     };
162
163
164   set_model (dict_view);
165 }
166
167
168 static void
169 psppire_dict_view_get_property (GObject         *object,
170                                guint            prop_id,
171                                GValue          *value,
172                                GParamSpec      *pspec)
173 {
174   PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
175
176   switch (prop_id)
177     {
178     case PROP_DICTIONARY:
179       g_value_set_object (value, dict_view->dict);
180       break;
181     case PROP_PREDICATE:
182       g_value_set_pointer (value, dict_view->predicate);
183       break;
184     case PROP_SELECTION_MODE:
185       {
186         GtkTreeSelection *selection =
187           gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
188
189         g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
190       }
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     };
195 }
196
197
198
199 static void
200 psppire_dict_view_class_init (PsppireDictViewClass *class)
201 {
202   GObjectClass *object_class = G_OBJECT_CLASS (class);
203
204   GParamSpec *dictionary_spec =
205     g_param_spec_object ("dictionary",
206                          "Dictionary",
207                          _("The dictionary to be displayed by this widget"),
208                          PSPPIRE_TYPE_DICT,
209                          G_PARAM_READABLE | G_PARAM_WRITABLE);
210
211   GParamSpec *predicate_spec =
212     g_param_spec_pointer ("predicate",
213                           "Predicate",
214                           _("A predicate function"),
215                           G_PARAM_READABLE | G_PARAM_WRITABLE);
216
217
218   GParamSpec *selection_mode_spec =
219     g_param_spec_enum ("selection-mode",
220                        "Selection Mode",
221                        _("How many things can be selected"),
222                        GTK_TYPE_SELECTION_MODE,
223                        GTK_SELECTION_MULTIPLE,
224                        G_PARAM_CONSTRUCT | G_PARAM_READABLE | G_PARAM_WRITABLE);
225
226
227   GParamSpec *dummy_spec =
228     g_param_spec_pointer ("model",
229                           "Model",
230                           "Don't set the property",
231                           G_PARAM_WRITABLE);
232
233   object_class->set_property = psppire_dict_view_set_property;
234   object_class->get_property = psppire_dict_view_get_property;
235
236   g_object_class_install_property (object_class,
237                                    PROP_MODEL,
238                                    dummy_spec);
239
240   g_object_class_install_property (object_class,
241                                    PROP_DICTIONARY,
242                                    dictionary_spec);
243
244   g_object_class_install_property (object_class,
245                                    PROP_PREDICATE,
246                                    predicate_spec);
247
248   g_object_class_install_property (object_class,
249                                    PROP_SELECTION_MODE,
250                                    selection_mode_spec);
251 }
252
253
254 static void
255 psppire_dict_view_base_init (PsppireDictViewClass *class)
256 {
257   GObjectClass *object_class = G_OBJECT_CLASS (class);
258
259   object_class->finalize = psppire_dict_view_finalize;
260 }
261
262
263
264 static void
265 psppire_dict_view_base_finalize (PsppireDictViewClass *class,
266                                  gpointer class_data)
267 {
268
269 }
270
271
272 static void
273 dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
274                 GtkTreeModel **model, GtkTreeIter *iter
275                 )
276 {
277   *model = top_model;
278
279   if ( iter)
280     *iter = *top_iter;
281
282   while ( ! PSPPIRE_IS_DICT (*model))
283     {
284       GtkTreeIter parent_iter;
285       if (iter)
286         parent_iter = *iter;
287
288       if ( GTK_IS_TREE_MODEL_FILTER (*model))
289         {
290           GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
291
292           *model = gtk_tree_model_filter_get_model (parent_model);
293
294           if (iter)
295             gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
296                                                               iter,
297                                                               &parent_iter);
298         }
299       else if (GTK_IS_TREE_MODEL_SORT (*model))
300         {
301           GtkTreeModelSort *parent_model = GTK_TREE_MODEL_SORT (*model);
302
303           *model = gtk_tree_model_sort_get_model (parent_model);
304
305           if (iter)
306             gtk_tree_model_sort_convert_iter_to_child_iter (parent_model,
307                                                             iter,
308                                                             &parent_iter);
309         }
310     }
311 }
312
313
314
315 /* A GtkTreeCellDataFunc which renders the name and/or label of the
316    variable */
317 static void
318 var_description_cell_data_func (GtkTreeViewColumn *col,
319                                 GtkCellRenderer *cell,
320                                 GtkTreeModel *top_model,
321                                 GtkTreeIter *top_iter,
322                                 gpointer data)
323 {
324   PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
325   struct variable *var;
326   GtkTreeIter iter;
327   GtkTreeModel *model;
328   PsppireDict *dict;
329
330   dv_get_base_model (top_model, top_iter, &model, &iter);
331
332   dict = PSPPIRE_DICT (model);
333
334   gtk_tree_model_get (model,
335                       &iter, DICT_TVM_COL_VAR, &var, -1);
336
337   if ( var_has_label (var) && dv->prefer_labels)
338     {
339       gchar *text = g_strdup_printf (
340                                      "<span stretch=\"condensed\">%s</span>",
341                                      var_get_label (var));
342
343       g_object_set (cell, "markup", text, NULL);
344       g_free (text);
345     }
346   else
347     {
348       g_object_set (cell, "text", var_get_name (var), NULL);
349     }
350 }
351
352
353
354 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
355    of variable */
356 static void
357 var_icon_cell_data_func (GtkTreeViewColumn *col,
358                        GtkCellRenderer *cell,
359                        GtkTreeModel *model,
360                        GtkTreeIter *iter,
361                        gpointer data)
362 {
363   struct variable *var;
364   gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
365
366   if ( var_is_alpha (var))
367     {
368       g_object_set (cell, "stock-id", "var-string", NULL);
369     }
370   else
371     {
372       const struct fmt_spec *fs = var_get_write_format (var);
373       int cat = fmt_get_category (fs->type);
374       switch ( var_get_measure (var))
375         {
376         case MEASURE_NOMINAL:
377           g_object_set (cell, "stock-id", "var-nominal", NULL);
378           break;
379         case MEASURE_ORDINAL:
380           g_object_set (cell, "stock-id", "var-ordinal", NULL);
381           break;
382         case MEASURE_SCALE:
383           if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
384             g_object_set (cell, "stock-id", "var-date-scale", NULL);
385           else
386             g_object_set (cell, "stock-id", "var-scale", NULL);
387           break;
388         default:
389           g_assert_not_reached ();
390         };
391     }
392 }
393
394
395 /* Sets the tooltip to be the name of the variable under the cursor */
396 static gboolean
397 set_tooltip_for_variable (GtkTreeView  *treeview,
398                           gint        x,
399                           gint        y,
400                           gboolean    keyboard_mode,
401                           GtkTooltip *tooltip,
402                           gpointer    user_data)
403 {
404   gint bx, by;
405   GtkTreeIter iter;
406   GtkTreePath *path;
407   GtkTreeModel *tree_model;
408   struct variable *var = NULL;
409   gboolean ok;
410
411   gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
412                                                      x, y, &bx, &by);
413
414   if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
415                                       &path, NULL, NULL, NULL))
416     return FALSE;
417
418   tree_model = gtk_tree_view_get_model (treeview);
419
420   gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
421
422   ok = gtk_tree_model_get_iter (tree_model, &iter, path);
423
424   gtk_tree_path_free (path);
425   if (!ok)
426     return FALSE;
427
428
429   gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR,  &var, -1);
430
431   if ( ! var_has_label (var))
432     return FALSE;
433
434   {
435     const gchar *tip ;
436     GtkTreeModel *m;
437     PsppireDict *dict;
438
439     dv_get_base_model (tree_model, NULL, &m, NULL);
440     dict = PSPPIRE_DICT (m);
441
442     if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
443       tip = var_get_name (var);
444     else
445       tip = var_get_label (var);
446
447     gtk_tooltip_set_text (tooltip, tip);
448   }
449
450   return TRUE;
451 }
452
453 static gboolean
454 show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
455 {
456   if (event->button != 3)
457     return FALSE;
458
459   gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
460                   event->button, event->time);
461
462   return TRUE;
463 }
464
465 static void
466 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
467 {
468   PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
469
470   dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
471
472   gtk_widget_queue_draw (GTK_WIDGET (dv));
473 }
474
475
476
477 static void
478 psppire_dict_view_init (PsppireDictView *dict_view)
479 {
480   GtkTreeViewColumn *col = gtk_tree_view_column_new ();
481
482   GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
483
484   dict_view->prefer_labels = TRUE;
485
486   psppire_conf_get_boolean (psppire_conf_new (),
487                             G_OBJECT_TYPE_NAME (dict_view),
488                             "prefer-labels",
489                             &dict_view->prefer_labels);
490
491   gtk_tree_view_column_set_title (col, _("Variable"));
492
493   gtk_tree_view_column_pack_start (col, renderer, FALSE);
494
495   gtk_tree_view_column_set_cell_data_func (col, renderer,
496                                            var_icon_cell_data_func,
497                                            NULL, NULL);
498
499   renderer = gtk_cell_renderer_text_new ();
500   gtk_tree_view_column_pack_start (col, renderer, TRUE);
501   gtk_tree_view_column_set_cell_data_func (col, renderer,
502                                            var_description_cell_data_func,
503                                            dict_view, NULL);
504
505   g_object_set (renderer, "ellipsize-set", TRUE, NULL);
506   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
507
508   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
509
510   /* FIXME: make this a value in terms of character widths */
511   gtk_tree_view_column_set_min_width (col, 150);
512
513   gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
514
515   g_object_set (dict_view,
516                 "has-tooltip", TRUE,
517                 "headers-visible", FALSE,
518                 NULL);
519
520   g_signal_connect (dict_view, "query-tooltip",
521                     G_CALLBACK (set_tooltip_for_variable), NULL);
522
523   dict_view->menu = gtk_menu_new ();
524
525
526   {
527     GtkWidget *checkbox =
528       gtk_check_menu_item_new_with_label  (_("Prefer variable labels"));
529
530     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox),
531                                     dict_view->prefer_labels);
532
533     g_signal_connect (checkbox, "toggled",
534                       G_CALLBACK (toggle_label_preference), dict_view);
535
536
537     gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
538
539   }
540
541   gtk_widget_show_all (dict_view->menu);
542
543   g_signal_connect (dict_view, "button-press-event",
544                     G_CALLBACK (show_menu), NULL);
545 }
546
547
548 GtkWidget*
549 psppire_dict_view_new (void)
550 {
551   return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
552 }
553
554
555
556 struct variable *
557 psppire_dict_view_get_selected_variable (PsppireDictView *treeview)
558 {
559   struct variable *var;
560   GtkTreeModel *top_model;
561   GtkTreeIter top_iter;
562
563   GtkTreeModel *model;
564   GtkTreeIter iter;
565
566   GtkTreeSelection *selection =
567     gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
568
569   if (! gtk_tree_selection_get_selected (selection,
570                                          &top_model, &top_iter))
571     return NULL;
572
573   dv_get_base_model (top_model, &top_iter, &model, &iter);
574
575   g_assert (PSPPIRE_IS_DICT (model));
576
577   gtk_tree_model_get (model,
578                       &iter, DICT_TVM_COL_VAR, &var, -1);
579
580   return var;
581 }
582
583