1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009 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/>. */
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>
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 g_object_unref (dict_view->menu);
85 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
87 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
89 var_predicate_func *predicate = data;
91 PsppireDict *dict = PSPPIRE_DICT (model);
93 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
95 gint *idx = gtk_tree_path_get_indices (path);
97 var = psppire_dict_get_variable (dict, *idx);
99 gtk_tree_path_free (path);
101 return predicate (var);
105 set_model (PsppireDictView *dict_view)
107 GtkTreeModel *model ;
109 if ( dict_view->predicate )
111 model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict_view->dict),
114 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
116 dict_view->predicate,
121 model = GTK_TREE_MODEL (dict_view->dict);
124 gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
128 psppire_dict_view_set_property (GObject *object,
133 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
137 case PROP_DICTIONARY:
138 dict_view->dict = g_value_get_object (value);
141 g_critical ("Don't set the \"model\" property on %s. "
142 "Use the \"dictionary\" property instead.",
143 G_OBJECT_TYPE_NAME (dict_view));
146 dict_view->predicate = g_value_get_pointer (value);
148 case PROP_SELECTION_MODE:
150 GtkTreeSelection *selection =
151 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
153 GtkSelectionMode mode = g_value_get_enum (value);
155 gtk_tree_selection_set_mode (selection, mode);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 set_model (dict_view);
169 psppire_dict_view_get_property (GObject *object,
174 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
178 case PROP_DICTIONARY:
179 g_value_set_object (value, dict_view->dict);
182 g_value_set_pointer (value, dict_view->predicate);
184 case PROP_SELECTION_MODE:
186 GtkTreeSelection *selection =
187 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
189 g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 psppire_dict_view_class_init (PsppireDictViewClass *class)
202 GObjectClass *object_class = G_OBJECT_CLASS (class);
204 GParamSpec *dictionary_spec =
205 g_param_spec_object ("dictionary",
207 _("The dictionary to be displayed by this widget"),
209 G_PARAM_READABLE | G_PARAM_WRITABLE);
211 GParamSpec *predicate_spec =
212 g_param_spec_pointer ("predicate",
214 _("A predicate function"),
215 G_PARAM_READABLE | G_PARAM_WRITABLE);
218 GParamSpec *selection_mode_spec =
219 g_param_spec_enum ("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);
227 GParamSpec *dummy_spec =
228 g_param_spec_pointer ("model",
230 "Don't set the property",
233 object_class->set_property = psppire_dict_view_set_property;
234 object_class->get_property = psppire_dict_view_get_property;
236 g_object_class_install_property (object_class,
240 g_object_class_install_property (object_class,
244 g_object_class_install_property (object_class,
248 g_object_class_install_property (object_class,
250 selection_mode_spec);
255 psppire_dict_view_base_init (PsppireDictViewClass *class)
257 GObjectClass *object_class = G_OBJECT_CLASS (class);
259 object_class->finalize = psppire_dict_view_finalize;
265 psppire_dict_view_base_finalize (PsppireDictViewClass *class,
273 dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
274 GtkTreeModel **model, GtkTreeIter *iter
282 while ( ! PSPPIRE_IS_DICT (*model))
284 GtkTreeIter parent_iter;
288 if ( GTK_IS_TREE_MODEL_FILTER (*model))
290 GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
292 *model = gtk_tree_model_filter_get_model (parent_model);
295 gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
299 else if (GTK_IS_TREE_MODEL_SORT (*model))
301 GtkTreeModelSort *parent_model = GTK_TREE_MODEL_SORT (*model);
303 *model = gtk_tree_model_sort_get_model (parent_model);
306 gtk_tree_model_sort_convert_iter_to_child_iter (parent_model,
315 /* A GtkTreeCellDataFunc which renders the name and/or label of the
318 var_description_cell_data_func (GtkTreeViewColumn *col,
319 GtkCellRenderer *cell,
320 GtkTreeModel *top_model,
321 GtkTreeIter *top_iter,
324 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
325 struct variable *var;
330 dv_get_base_model (top_model, top_iter, &model, &iter);
332 dict = PSPPIRE_DICT (model);
334 gtk_tree_model_get (model,
335 &iter, DICT_TVM_COL_VAR, &var, -1);
337 if ( var_has_label (var) && dv->prefer_labels)
339 gchar *text = g_strdup_printf (
340 "<span stretch=\"condensed\">%s</span>",
341 var_get_label (var));
343 g_object_set (cell, "markup", text, NULL);
348 g_object_set (cell, "text", var_get_name (var), NULL);
354 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
357 var_icon_cell_data_func (GtkTreeViewColumn *col,
358 GtkCellRenderer *cell,
363 struct variable *var;
364 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
366 if ( var_is_alpha (var))
368 g_object_set (cell, "stock-id", "var-string", NULL);
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))
376 case MEASURE_NOMINAL:
377 g_object_set (cell, "stock-id", "var-nominal", NULL);
379 case MEASURE_ORDINAL:
380 g_object_set (cell, "stock-id", "var-ordinal", NULL);
383 if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
384 g_object_set (cell, "stock-id", "var-date-scale", NULL);
386 g_object_set (cell, "stock-id", "var-scale", NULL);
389 g_assert_not_reached ();
395 /* Sets the tooltip to be the name of the variable under the cursor */
397 set_tooltip_for_variable (GtkTreeView *treeview,
400 gboolean keyboard_mode,
407 GtkTreeModel *tree_model;
408 struct variable *var = NULL;
411 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
414 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
415 &path, NULL, NULL, NULL))
418 tree_model = gtk_tree_view_get_model (treeview);
420 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
422 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
424 gtk_tree_path_free (path);
429 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
431 if ( ! var_has_label (var))
439 dv_get_base_model (tree_model, NULL, &m, NULL);
440 dict = PSPPIRE_DICT (m);
442 if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
443 tip = var_get_name (var);
445 tip = var_get_label (var);
447 gtk_tooltip_set_text (tooltip, tip);
454 show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
456 if (event->button != 3)
459 gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
460 event->button, event->time);
466 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
468 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
470 dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
472 gtk_widget_queue_draw (GTK_WIDGET (dv));
478 psppire_dict_view_init (PsppireDictView *dict_view)
480 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
482 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
484 dict_view->prefer_labels = TRUE;
486 psppire_conf_get_boolean (psppire_conf_new (),
487 G_OBJECT_TYPE_NAME (dict_view),
489 &dict_view->prefer_labels);
491 gtk_tree_view_column_set_title (col, _("Variable"));
493 gtk_tree_view_column_pack_start (col, renderer, FALSE);
495 gtk_tree_view_column_set_cell_data_func (col, renderer,
496 var_icon_cell_data_func,
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,
505 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
506 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
508 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
510 /* FIXME: make this a value in terms of character widths */
511 gtk_tree_view_column_set_min_width (col, 150);
513 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
515 g_object_set (dict_view,
517 "headers-visible", FALSE,
520 g_signal_connect (dict_view, "query-tooltip",
521 G_CALLBACK (set_tooltip_for_variable), NULL);
523 dict_view->menu = gtk_menu_new ();
527 GtkWidget *checkbox =
528 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
530 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox),
531 dict_view->prefer_labels);
533 g_signal_connect (checkbox, "toggled",
534 G_CALLBACK (toggle_label_preference), dict_view);
537 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
541 gtk_widget_show_all (dict_view->menu);
543 g_signal_connect (dict_view, "button-press-event",
544 G_CALLBACK (show_menu), NULL);
549 psppire_dict_view_new (void)
551 return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
557 psppire_dict_view_get_selected_variable (PsppireDictView *treeview)
559 struct variable *var;
560 GtkTreeModel *top_model;
561 GtkTreeIter top_iter;
566 GtkTreeSelection *selection =
567 gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
569 if (! gtk_tree_selection_get_selected (selection,
570 &top_model, &top_iter))
573 dv_get_base_model (top_model, &top_iter, &model, &iter);
575 g_assert (PSPPIRE_IS_DICT (model));
577 gtk_tree_model_get (model,
578 &iter, DICT_TVM_COL_VAR, &var, -1);