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