1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2017 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 "dict-display.h"
23 #include "psppire-conf.h"
24 #include "options-dialog.h"
25 #include <data/format.h>
26 #include <libpspp/i18n.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
33 static void psppire_dict_view_base_finalize (PsppireDictViewClass *, gpointer);
34 static void psppire_dict_view_base_init (PsppireDictViewClass *class);
35 static void psppire_dict_view_class_init (PsppireDictViewClass *class);
36 static void psppire_dict_view_init (PsppireDictView *dict_view);
39 psppire_dict_view_get_type (void)
41 static GType psppire_dict_view_type = 0;
43 if (!psppire_dict_view_type)
45 static const GTypeInfo psppire_dict_view_info =
47 sizeof (PsppireDictViewClass),
48 (GBaseInitFunc) psppire_dict_view_base_init,
49 (GBaseFinalizeFunc) psppire_dict_view_base_finalize,
50 (GClassInitFunc)psppire_dict_view_class_init,
51 (GClassFinalizeFunc) NULL,
53 sizeof (PsppireDictView),
55 (GInstanceInitFunc) psppire_dict_view_init,
58 psppire_dict_view_type =
59 g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireDictView",
60 &psppire_dict_view_info, 0);
63 return psppire_dict_view_type;
68 psppire_dict_view_finalize (GObject *object)
70 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
72 gtk_widget_destroy (dict_view->menu);
86 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
88 filter_variables (GtkTreeModel *tmodel, GtkTreeIter *titer, gpointer data)
90 var_predicate_func *predicate = data;
92 GtkTreeModel *model = NULL;
98 get_base_model (tmodel, titer, &model, &iter);
100 dict = PSPPIRE_DICT (model);
101 path = gtk_tree_model_get_path (model, &iter);
102 idx = gtk_tree_path_get_indices (path);
103 var = psppire_dict_get_variable (dict, *idx);
105 gtk_tree_path_free (path);
107 return predicate (var);
111 unsorted (GtkTreeModel *model,
116 const struct variable *var_a;
117 const struct variable *var_b;
120 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
121 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
123 return compare_var_ptrs_by_dict_index (&var_a, &var_b, NULL);
127 sort_by_name (GtkTreeModel *model,
132 const struct variable *var_a;
133 const struct variable *var_b;
135 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
136 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
138 return g_strcmp0 (var_get_name (var_a), var_get_name (var_b));
143 sort_by_label (GtkTreeModel *model,
148 const struct variable *var_a;
149 const struct variable *var_b;
151 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
152 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
154 return g_strcmp0 (var_get_label (var_a), var_get_label (var_b));
159 default_sort (GtkTreeModel *model,
165 psppire_conf_get_enum (psppire_conf_new (), "VariableLists", "sort-order",
166 PSPP_TYPE_OPTIONS_VAR_ORDER, &what);
171 return unsorted (model, a, b, user_data);
173 case PSPP_OPTIONS_VAR_ORDER_NAME:
174 return sort_by_name (model, a, b, user_data);
176 case PSPP_OPTIONS_VAR_ORDER_LABEL:
177 return sort_by_label (model, a, b, user_data);
181 g_assert_not_reached ();
188 set_model (PsppireDictView *dict_view)
190 GtkTreeModel *model = NULL;
192 if ( dict_view->dict == NULL)
195 dict_view->sorted_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dict_view->dict));
196 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dict_view->sorted_model), default_sort, dict_view, 0);
197 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dict_view->sorted_model),
198 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
200 if ( dict_view->predicate )
202 model = gtk_tree_model_filter_new (dict_view->sorted_model, NULL);
204 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
206 dict_view->predicate,
211 model = dict_view->sorted_model;
212 g_object_ref (model);
215 gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
216 g_object_unref (model);
220 psppire_dict_view_set_property (GObject *object,
225 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
229 case PROP_DICTIONARY:
230 dict_view->dict = g_value_get_object (value);
233 dict_view->predicate = g_value_get_pointer (value);
235 case PROP_SELECTION_MODE:
237 GtkTreeSelection *selection =
238 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
240 GtkSelectionMode mode = g_value_get_enum (value);
242 gtk_tree_selection_set_mode (selection, mode);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 set_model (dict_view);
256 psppire_dict_view_get_property (GObject *object,
261 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
265 case PROP_DICTIONARY:
266 g_value_set_object (value, dict_view->dict);
269 g_value_set_pointer (value, dict_view->predicate);
271 case PROP_SELECTION_MODE:
273 GtkTreeSelection *selection =
274 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
276 g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 psppire_dict_view_class_init (PsppireDictViewClass *class)
289 GObjectClass *object_class = G_OBJECT_CLASS (class);
291 GParamSpec *predicate_spec =
292 g_param_spec_pointer ("predicate",
294 "A predicate function",
295 G_PARAM_READABLE | G_PARAM_WRITABLE);
298 GParamSpec *selection_mode_spec =
299 g_param_spec_enum ("selection-mode",
301 "How many things can be selected",
302 GTK_TYPE_SELECTION_MODE,
303 GTK_SELECTION_MULTIPLE,
304 G_PARAM_CONSTRUCT | G_PARAM_READABLE | G_PARAM_WRITABLE);
306 object_class->set_property = psppire_dict_view_set_property;
307 object_class->get_property = psppire_dict_view_get_property;
309 g_object_class_override_property (object_class,
313 g_object_class_install_property (object_class,
317 g_object_class_install_property (object_class,
319 selection_mode_spec);
324 psppire_dict_view_base_init (PsppireDictViewClass *class)
326 GObjectClass *object_class = G_OBJECT_CLASS (class);
328 object_class->finalize = psppire_dict_view_finalize;
334 psppire_dict_view_base_finalize (PsppireDictViewClass *class,
341 use_labels (PsppireDictView *dv)
343 gboolean disp_labels = TRUE;
345 if (gtk_check_menu_item_get_inconsistent (GTK_CHECK_MENU_ITEM
346 (dv->override_button)))
348 psppire_conf_get_boolean (psppire_conf_new (),
349 "VariableLists", "display-labels", &disp_labels);
353 disp_labels = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM
354 (dv->override_button));
360 /* A GtkTreeCellDataFunc which renders the name and/or label of the
363 var_description_cell_data_func (GtkTreeViewColumn *col,
364 GtkCellRenderer *cell,
365 GtkTreeModel *top_model,
366 GtkTreeIter *top_iter,
369 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
370 struct variable *var;
374 get_base_model (top_model, top_iter, &model, &iter);
376 gtk_tree_model_get (model,
377 &iter, DICT_TVM_COL_VAR, &var, -1);
379 if (var_has_label (var) && use_labels (dv))
381 gchar *text = g_markup_printf_escaped (
382 "<span stretch=\"condensed\">%s</span>",
383 var_get_label (var));
385 g_object_set (cell, "markup", text, NULL);
390 g_object_set (cell, "text", var_get_name (var), NULL);
396 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
399 var_icon_cell_data_func (GtkTreeViewColumn *col,
400 GtkCellRenderer *cell,
405 struct variable *var;
407 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
410 "stock-size", GTK_ICON_SIZE_MENU,
411 "icon-name", get_var_measurement_stock_id (var_get_print_format (var)->type,
412 var_get_measure (var)),
417 get_var_measurement_stock_id (enum fmt_type type, enum measure measure)
419 switch (fmt_get_category (type))
424 case MEASURE_NOMINAL: return "measure-string-nominal";
425 case MEASURE_ORDINAL: return "measure-string-ordinal";
426 case MEASURE_SCALE: return "role-none";
427 case n_MEASURES: break;
435 case MEASURE_NOMINAL: return "measure-date-nominal";
436 case MEASURE_ORDINAL: return "measure-date-ordinal";
437 case MEASURE_SCALE: return "measure-date-scale";
438 case n_MEASURES: break;
445 case MEASURE_NOMINAL: return "measure-nominal";
446 case MEASURE_ORDINAL: return "measure-ordinal";
447 case MEASURE_SCALE: return "measure-scale";
448 case n_MEASURES: break;
453 g_return_val_if_reached ("");
458 /* Sets the tooltip to be the name of the variable under the cursor */
460 set_tooltip_for_variable (GtkTreeView *treeview,
463 gboolean keyboard_mode,
470 GtkTreeModel *tree_model;
471 struct variable *var = NULL;
474 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
477 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
478 &path, NULL, NULL, NULL))
481 tree_model = gtk_tree_view_get_model (treeview);
483 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
485 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
487 gtk_tree_path_free (path);
492 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
494 if ( ! var_has_label (var))
501 get_base_model (tree_model, NULL, &m, NULL);
503 if ( use_labels (PSPPIRE_DICT_VIEW (treeview)))
504 tip = var_get_name (var);
506 tip = var_get_label (var);
508 gtk_tooltip_set_text (tooltip, tip);
515 show_menu (PsppireDictView *dv, GdkEvent *event, gpointer data)
517 if (((GdkEventButton *) event)->button != 3)
520 gtk_menu_popup_at_pointer (GTK_MENU (dv->menu), event);
526 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
528 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
530 gboolean global_setting = TRUE;
531 psppire_conf_get_boolean (psppire_conf_new (),
532 "VariableLists", "display-labels", &global_setting);
534 if (gtk_check_menu_item_get_inconsistent (checkbox))
535 gtk_check_menu_item_set_active (checkbox, !global_setting);
537 gtk_check_menu_item_set_inconsistent (checkbox, FALSE);
539 gtk_widget_queue_draw (GTK_WIDGET (dv));
544 set_sort_criteria (GtkCheckMenuItem *checkbox, PsppireDictView *dv, GtkTreeIterCompareFunc func)
546 if (!gtk_check_menu_item_get_active (checkbox))
548 gtk_widget_queue_draw (GTK_WIDGET (dv));
553 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dv->sorted_model), func, 0, 0);
556 gtk_widget_queue_draw (GTK_WIDGET (dv));
560 set_sort_criteria_default (GtkCheckMenuItem *checkbox, gpointer data)
562 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
563 set_sort_criteria (checkbox, dv, default_sort);
568 set_sort_criteria_name (GtkCheckMenuItem *checkbox, gpointer data)
570 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
571 set_sort_criteria (checkbox, dv, sort_by_name);
576 set_sort_criteria_label (GtkCheckMenuItem *checkbox, gpointer data)
578 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
579 set_sort_criteria (checkbox, dv, sort_by_label);
584 set_sort_criteria_unsorted (GtkCheckMenuItem *checkbox, gpointer data)
586 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
587 set_sort_criteria (checkbox, dv, unsorted);
593 psppire_dict_view_init (PsppireDictView *dict_view)
595 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
597 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
599 dict_view->prefer_labels_override = FALSE;
600 dict_view->sorted_model = NULL;
602 gtk_tree_view_column_set_title (col, _("Variable"));
604 gtk_tree_view_column_pack_start (col, renderer, FALSE);
606 gtk_tree_view_column_set_cell_data_func (col, renderer,
607 var_icon_cell_data_func,
610 renderer = gtk_cell_renderer_text_new ();
611 gtk_tree_view_column_pack_start (col, renderer, TRUE);
612 gtk_tree_view_column_set_cell_data_func (col, renderer,
613 var_description_cell_data_func,
616 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
617 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
619 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
621 /* FIXME: make this a value in terms of character widths */
622 gtk_tree_view_column_set_min_width (col, 150);
624 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
626 g_object_set (dict_view,
628 "headers-visible", FALSE,
631 g_signal_connect (dict_view, "query-tooltip",
632 G_CALLBACK (set_tooltip_for_variable), NULL);
634 dict_view->menu = gtk_menu_new ();
637 GSList *group = NULL;
639 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
641 dict_view->override_button = item;
642 gtk_check_menu_item_set_inconsistent (GTK_CHECK_MENU_ITEM (item),
645 g_signal_connect (item, "toggled",
646 G_CALLBACK (toggle_label_preference), dict_view);
648 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
650 item = gtk_separator_menu_item_new ();
651 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
653 item = gtk_radio_menu_item_new_with_label (group, _("Default sort order"));
654 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
655 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
656 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
657 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_default), dict_view);
659 item = gtk_radio_menu_item_new_with_label (group, _("Unsorted (dictionary order)"));
660 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
661 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
662 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_unsorted), dict_view);
664 item = gtk_radio_menu_item_new_with_label (group, _("Sort by name"));
665 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
666 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
667 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_name), dict_view);
669 item = gtk_radio_menu_item_new_with_label (group, _("Sort by label"));
670 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
671 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
672 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_label), dict_view);
675 gtk_widget_show_all (dict_view->menu);
677 g_signal_connect (dict_view, "button-press-event",
678 G_CALLBACK (show_menu), NULL);
683 psppire_dict_view_new (void)
685 return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
688 static struct variable *
689 psppire_dict_view_iter_to_var (PsppireDictView *dict_view,
690 GtkTreeIter *top_iter)
692 GtkTreeView *treeview = GTK_TREE_VIEW (dict_view);
693 GtkTreeModel *top_model = gtk_tree_view_get_model (treeview);
695 struct variable *var;
699 get_base_model (top_model, top_iter, &model, &iter);
701 g_assert (PSPPIRE_IS_DICT (model));
703 gtk_tree_model_get (model,
704 &iter, DICT_TVM_COL_VAR, &var, -1);
711 PsppireDictView *dict_view;
712 struct variable **vars;
717 get_vars_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
720 struct get_vars_aux *aux = data;
721 struct variable *var = psppire_dict_view_iter_to_var (aux->dict_view, iter);
723 g_return_if_fail (var != NULL);
724 aux->vars[aux->idx++] = var;
728 psppire_dict_view_get_selected_variables (PsppireDictView *dict_view,
729 struct variable ***vars,
732 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
733 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
734 gint n_vars = gtk_tree_selection_count_selected_rows (selection);
735 struct get_vars_aux aux;
737 *vars = g_malloc_n (n_vars, sizeof **vars);
739 aux.dict_view = dict_view;
742 gtk_tree_selection_selected_foreach (selection, get_vars_cb, &aux);
745 g_return_if_fail (aux.idx >= n_vars);
749 psppire_dict_view_get_selected_variable (PsppireDictView *dict_view)
751 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
752 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
755 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
756 return psppire_dict_view_iter_to_var (dict_view, &iter);