1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation
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.
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.
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/>. */
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>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
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);
38 psppire_dict_view_get_type (void)
40 static GType psppire_dict_view_type = 0;
42 if (!psppire_dict_view_type)
44 static const GTypeInfo psppire_dict_view_info =
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,
52 sizeof (PsppireDictView),
54 (GInstanceInitFunc) psppire_dict_view_init,
57 psppire_dict_view_type =
58 g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireDictView",
59 &psppire_dict_view_info, 0);
62 return psppire_dict_view_type;
67 psppire_dict_view_finalize (GObject *object)
69 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
71 gtk_widget_destroy (dict_view->menu);
84 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
86 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
88 var_predicate_func *predicate = data;
90 PsppireDict *dict = PSPPIRE_DICT (model);
92 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
94 gint *idx = gtk_tree_path_get_indices (path);
96 var = psppire_dict_get_variable (dict, *idx);
98 gtk_tree_path_free (path);
100 return predicate (var);
104 set_model (PsppireDictView *dict_view)
106 GtkTreeModel *model ;
108 if ( dict_view->dict == NULL)
111 if ( dict_view->predicate )
113 model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict_view->dict),
116 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
118 dict_view->predicate,
123 model = GTK_TREE_MODEL (dict_view->dict);
124 g_object_ref (model);
127 gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
128 g_object_unref (model);
132 psppire_dict_view_set_property (GObject *object,
137 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
141 case PROP_DICTIONARY:
142 dict_view->dict = g_value_get_object (value);
145 dict_view->predicate = g_value_get_pointer (value);
147 case PROP_SELECTION_MODE:
149 GtkTreeSelection *selection =
150 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
152 GtkSelectionMode mode = g_value_get_enum (value);
154 gtk_tree_selection_set_mode (selection, mode);
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 set_model (dict_view);
168 psppire_dict_view_get_property (GObject *object,
173 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
177 case PROP_DICTIONARY:
178 g_value_set_object (value, dict_view->dict);
181 g_value_set_pointer (value, dict_view->predicate);
183 case PROP_SELECTION_MODE:
185 GtkTreeSelection *selection =
186 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
188 g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 psppire_dict_view_class_init (PsppireDictViewClass *class)
201 GObjectClass *object_class = G_OBJECT_CLASS (class);
203 GParamSpec *predicate_spec =
204 g_param_spec_pointer ("predicate",
206 "A predicate function",
207 G_PARAM_READABLE | G_PARAM_WRITABLE);
210 GParamSpec *selection_mode_spec =
211 g_param_spec_enum ("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);
218 object_class->set_property = psppire_dict_view_set_property;
219 object_class->get_property = psppire_dict_view_get_property;
221 g_object_class_override_property (object_class,
225 g_object_class_install_property (object_class,
229 g_object_class_install_property (object_class,
231 selection_mode_spec);
236 psppire_dict_view_base_init (PsppireDictViewClass *class)
238 GObjectClass *object_class = G_OBJECT_CLASS (class);
240 object_class->finalize = psppire_dict_view_finalize;
246 psppire_dict_view_base_finalize (PsppireDictViewClass *class,
254 dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
255 GtkTreeModel **model, GtkTreeIter *iter)
262 while ( ! PSPPIRE_IS_DICT (*model))
264 GtkTreeIter parent_iter;
268 if ( GTK_IS_TREE_MODEL_FILTER (*model))
270 GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
272 *model = gtk_tree_model_filter_get_model (parent_model);
275 gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
279 else if (GTK_IS_TREE_MODEL_SORT (*model))
281 GtkTreeModelSort *parent_model = GTK_TREE_MODEL_SORT (*model);
283 *model = gtk_tree_model_sort_get_model (parent_model);
286 gtk_tree_model_sort_convert_iter_to_child_iter (parent_model,
295 /* A GtkTreeCellDataFunc which renders the name and/or label of the
298 var_description_cell_data_func (GtkTreeViewColumn *col,
299 GtkCellRenderer *cell,
300 GtkTreeModel *top_model,
301 GtkTreeIter *top_iter,
304 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
305 struct variable *var;
310 dv_get_base_model (top_model, top_iter, &model, &iter);
312 dict = PSPPIRE_DICT (model);
314 gtk_tree_model_get (model,
315 &iter, DICT_TVM_COL_VAR, &var, -1);
317 if ( var_has_label (var) && dv->prefer_labels)
319 gchar *text = g_markup_printf_escaped (
320 "<span stretch=\"condensed\">%s</span>",
321 var_get_label (var));
323 g_object_set (cell, "markup", text, NULL);
328 g_object_set (cell, "text", var_get_name (var), NULL);
334 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
337 var_icon_cell_data_func (GtkTreeViewColumn *col,
338 GtkCellRenderer *cell,
343 struct variable *var;
344 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
346 if ( var_is_alpha (var))
348 g_object_set (cell, "stock-id", "var-string", NULL);
352 const struct fmt_spec *fs = var_get_print_format (var);
353 int cat = fmt_get_category (fs->type);
354 switch ( var_get_measure (var))
356 case MEASURE_NOMINAL:
357 g_object_set (cell, "stock-id", "var-nominal", NULL);
359 case MEASURE_ORDINAL:
360 g_object_set (cell, "stock-id", "var-ordinal", NULL);
363 if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
364 g_object_set (cell, "stock-id", "var-date-scale", NULL);
366 g_object_set (cell, "stock-id", "var-scale", NULL);
369 g_assert_not_reached ();
375 /* Sets the tooltip to be the name of the variable under the cursor */
377 set_tooltip_for_variable (GtkTreeView *treeview,
380 gboolean keyboard_mode,
387 GtkTreeModel *tree_model;
388 struct variable *var = NULL;
391 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
394 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
395 &path, NULL, NULL, NULL))
398 tree_model = gtk_tree_view_get_model (treeview);
400 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
402 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
404 gtk_tree_path_free (path);
409 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
411 if ( ! var_has_label (var))
419 dv_get_base_model (tree_model, NULL, &m, NULL);
420 dict = PSPPIRE_DICT (m);
422 if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
423 tip = var_get_name (var);
425 tip = var_get_label (var);
427 gtk_tooltip_set_text (tooltip, tip);
434 show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
436 if (event->button != 3)
439 gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
440 event->button, event->time);
446 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
448 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
450 dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
452 gtk_widget_queue_draw (GTK_WIDGET (dv));
458 psppire_dict_view_init (PsppireDictView *dict_view)
460 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
462 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
464 dict_view->prefer_labels = TRUE;
466 psppire_conf_get_boolean (psppire_conf_new (),
467 G_OBJECT_TYPE_NAME (dict_view),
469 &dict_view->prefer_labels);
471 gtk_tree_view_column_set_title (col, _("Variable"));
473 gtk_tree_view_column_pack_start (col, renderer, FALSE);
475 gtk_tree_view_column_set_cell_data_func (col, renderer,
476 var_icon_cell_data_func,
479 renderer = gtk_cell_renderer_text_new ();
480 gtk_tree_view_column_pack_start (col, renderer, TRUE);
481 gtk_tree_view_column_set_cell_data_func (col, renderer,
482 var_description_cell_data_func,
485 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
486 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
488 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
490 /* FIXME: make this a value in terms of character widths */
491 gtk_tree_view_column_set_min_width (col, 150);
493 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
495 g_object_set (dict_view,
497 "headers-visible", FALSE,
500 g_signal_connect (dict_view, "query-tooltip",
501 G_CALLBACK (set_tooltip_for_variable), NULL);
503 dict_view->menu = gtk_menu_new ();
507 GtkWidget *checkbox =
508 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
510 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox),
511 dict_view->prefer_labels);
513 g_signal_connect (checkbox, "toggled",
514 G_CALLBACK (toggle_label_preference), dict_view);
517 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
521 gtk_widget_show_all (dict_view->menu);
523 g_signal_connect (dict_view, "button-press-event",
524 G_CALLBACK (show_menu), NULL);
529 psppire_dict_view_new (void)
531 return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
537 psppire_dict_view_get_selected_variable (PsppireDictView *treeview)
539 struct variable *var;
540 GtkTreeModel *top_model;
541 GtkTreeIter top_iter;
546 GtkTreeSelection *selection =
547 gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
549 if (! gtk_tree_selection_get_selected (selection,
550 &top_model, &top_iter))
553 dv_get_base_model (top_model, &top_iter, &model, &iter);
555 g_assert (PSPPIRE_IS_DICT (model));
557 gtk_tree_model_get (model,
558 &iter, DICT_TVM_COL_VAR, &var, -1);