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