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, GdkEventButton *event, gpointer data)
517 if (event->button != 3)
520 gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
521 event->button, event->time);
527 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
529 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
531 gboolean global_setting = TRUE;
532 psppire_conf_get_boolean (psppire_conf_new (),
533 "VariableLists", "display-labels", &global_setting);
535 if (gtk_check_menu_item_get_inconsistent (checkbox))
536 gtk_check_menu_item_set_active (checkbox, !global_setting);
538 gtk_check_menu_item_set_inconsistent (checkbox, FALSE);
540 gtk_widget_queue_draw (GTK_WIDGET (dv));
545 set_sort_criteria (GtkCheckMenuItem *checkbox, PsppireDictView *dv, GtkTreeIterCompareFunc func)
547 if (!gtk_check_menu_item_get_active (checkbox))
549 gtk_widget_queue_draw (GTK_WIDGET (dv));
554 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dv->sorted_model), func, 0, 0);
557 gtk_widget_queue_draw (GTK_WIDGET (dv));
561 set_sort_criteria_default (GtkCheckMenuItem *checkbox, gpointer data)
563 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
564 set_sort_criteria (checkbox, dv, default_sort);
569 set_sort_criteria_name (GtkCheckMenuItem *checkbox, gpointer data)
571 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
572 set_sort_criteria (checkbox, dv, sort_by_name);
577 set_sort_criteria_label (GtkCheckMenuItem *checkbox, gpointer data)
579 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
580 set_sort_criteria (checkbox, dv, sort_by_label);
585 set_sort_criteria_unsorted (GtkCheckMenuItem *checkbox, gpointer data)
587 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
588 set_sort_criteria (checkbox, dv, unsorted);
594 psppire_dict_view_init (PsppireDictView *dict_view)
596 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
598 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
600 dict_view->prefer_labels_override = FALSE;
601 dict_view->sorted_model = NULL;
603 gtk_tree_view_column_set_title (col, _("Variable"));
605 gtk_tree_view_column_pack_start (col, renderer, FALSE);
607 gtk_tree_view_column_set_cell_data_func (col, renderer,
608 var_icon_cell_data_func,
611 renderer = gtk_cell_renderer_text_new ();
612 gtk_tree_view_column_pack_start (col, renderer, TRUE);
613 gtk_tree_view_column_set_cell_data_func (col, renderer,
614 var_description_cell_data_func,
617 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
618 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
620 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
622 /* FIXME: make this a value in terms of character widths */
623 gtk_tree_view_column_set_min_width (col, 150);
625 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
627 g_object_set (dict_view,
629 "headers-visible", FALSE,
632 g_signal_connect (dict_view, "query-tooltip",
633 G_CALLBACK (set_tooltip_for_variable), NULL);
635 dict_view->menu = gtk_menu_new ();
638 GSList *group = NULL;
640 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
642 dict_view->override_button = item;
643 gtk_check_menu_item_set_inconsistent (GTK_CHECK_MENU_ITEM (item),
646 g_signal_connect (item, "toggled",
647 G_CALLBACK (toggle_label_preference), dict_view);
649 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
651 item = gtk_separator_menu_item_new ();
652 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
654 item = gtk_radio_menu_item_new_with_label (group, _("Default sort order"));
655 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
656 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
657 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
658 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_default), dict_view);
660 item = gtk_radio_menu_item_new_with_label (group, _("Unsorted (dictionary order)"));
661 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
662 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
663 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_unsorted), dict_view);
665 item = gtk_radio_menu_item_new_with_label (group, _("Sort by name"));
666 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
667 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
668 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_name), dict_view);
670 item = gtk_radio_menu_item_new_with_label (group, _("Sort by label"));
671 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
672 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
673 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_label), dict_view);
676 gtk_widget_show_all (dict_view->menu);
678 g_signal_connect (dict_view, "button-press-event",
679 G_CALLBACK (show_menu), NULL);
684 psppire_dict_view_new (void)
686 return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
689 static struct variable *
690 psppire_dict_view_iter_to_var (PsppireDictView *dict_view,
691 GtkTreeIter *top_iter)
693 GtkTreeView *treeview = GTK_TREE_VIEW (dict_view);
694 GtkTreeModel *top_model = gtk_tree_view_get_model (treeview);
696 struct variable *var;
700 get_base_model (top_model, top_iter, &model, &iter);
702 g_assert (PSPPIRE_IS_DICT (model));
704 gtk_tree_model_get (model,
705 &iter, DICT_TVM_COL_VAR, &var, -1);
712 PsppireDictView *dict_view;
713 struct variable **vars;
718 get_vars_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
721 struct get_vars_aux *aux = data;
722 struct variable *var = psppire_dict_view_iter_to_var (aux->dict_view, iter);
724 g_return_if_fail (var != NULL);
725 aux->vars[aux->idx++] = var;
729 psppire_dict_view_get_selected_variables (PsppireDictView *dict_view,
730 struct variable ***vars,
733 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
734 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
735 gint n_vars = gtk_tree_selection_count_selected_rows (selection);
736 struct get_vars_aux aux;
738 *vars = g_malloc_n (n_vars, sizeof **vars);
740 aux.dict_view = dict_view;
743 gtk_tree_selection_selected_foreach (selection, get_vars_cb, &aux);
746 g_return_if_fail (aux.idx >= n_vars);
750 psppire_dict_view_get_selected_variable (PsppireDictView *dict_view)
752 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
753 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
756 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
757 return psppire_dict_view_iter_to_var (dict_view, &iter);