1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2017,
3 2020 Free Software Foundation
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "psppire-dictview.h"
22 #include "psppire-dict.h"
23 #include "dict-display.h"
24 #include "psppire-conf.h"
25 #include "options-dialog.h"
26 #include <data/format.h>
27 #include <libpspp/i18n.h>
30 #include "psppire-dialog.h"
31 #include "psppire-var-info.h"
32 #include "psppire-buttonbox.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
38 static void psppire_dict_view_class_init (PsppireDictViewClass *class);
39 static void psppire_dict_view_init (PsppireDictView *dict_view);
41 G_DEFINE_TYPE (PsppireDictView, psppire_dict_view, GTK_TYPE_TREE_VIEW)
44 psppire_dict_view_finalize (GObject *object)
46 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
48 gtk_widget_destroy (dict_view->menu);
62 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
64 filter_variables (GtkTreeModel *tmodel, GtkTreeIter *titer, gpointer data)
66 var_predicate_func *predicate = data;
68 GtkTreeModel *model = NULL;
74 get_base_model (tmodel, titer, &model, &iter);
76 dict = PSPPIRE_DICT (model);
77 path = gtk_tree_model_get_path (model, &iter);
78 idx = gtk_tree_path_get_indices (path);
79 var = psppire_dict_get_variable (dict, *idx);
81 gtk_tree_path_free (path);
83 return predicate (var);
87 unsorted (GtkTreeModel *model,
92 struct variable *var_a;
93 struct variable *var_b;
95 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
96 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
98 gint rval = compare_var_ptrs_by_dict_index (&var_a, &var_b, NULL);
107 sort_by_name (GtkTreeModel *model,
112 struct variable *var_a;
113 struct variable *var_b;
115 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
116 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
118 gint rval = g_strcmp0 (var_get_name (var_a), var_get_name (var_b));
128 sort_by_label (GtkTreeModel *model,
133 struct variable *var_a;
134 struct variable *var_b;
136 gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
137 gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
139 gint rval = g_strcmp0 (var_get_label (var_a), var_get_label (var_b));
149 default_sort (GtkTreeModel *model,
155 psppire_conf_get_enum (psppire_conf_new (), "VariableLists", "sort-order",
156 PSPP_TYPE_OPTIONS_VAR_ORDER, &what);
161 return unsorted (model, a, b, user_data);
163 case PSPP_OPTIONS_VAR_ORDER_NAME:
164 return sort_by_name (model, a, b, user_data);
166 case PSPP_OPTIONS_VAR_ORDER_LABEL:
167 return sort_by_label (model, a, b, user_data);
171 g_assert_not_reached ();
178 set_model (PsppireDictView *dict_view)
180 GtkTreeModel *model = NULL;
182 if (dict_view->dict == NULL)
185 dict_view->sorted_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dict_view->dict));
186 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dict_view->sorted_model), default_sort, dict_view, 0);
187 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dict_view->sorted_model),
188 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
190 if (dict_view->predicate)
192 model = gtk_tree_model_filter_new (dict_view->sorted_model, NULL);
194 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
196 dict_view->predicate,
201 model = dict_view->sorted_model;
202 g_object_ref (model);
205 gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
206 g_object_unref (model);
210 psppire_dict_view_set_property (GObject *object,
215 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
219 case PROP_DICTIONARY:
220 dict_view->dict = g_value_get_object (value);
223 dict_view->predicate = g_value_get_pointer (value);
225 case PROP_SELECTION_MODE:
227 GtkTreeSelection *selection =
228 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
230 GtkSelectionMode mode = g_value_get_enum (value);
232 gtk_tree_selection_set_mode (selection, mode);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 set_model (dict_view);
246 psppire_dict_view_get_property (GObject *object,
251 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
255 case PROP_DICTIONARY:
256 g_value_set_object (value, dict_view->dict);
259 g_value_set_pointer (value, dict_view->predicate);
261 case PROP_SELECTION_MODE:
263 GtkTreeSelection *selection =
264 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
266 g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 psppire_dict_view_class_init (PsppireDictViewClass *class)
280 GObjectClass *object_class = G_OBJECT_CLASS (class);
282 object_class->finalize = psppire_dict_view_finalize;
284 GParamSpec *predicate_spec =
285 g_param_spec_pointer ("predicate",
287 "A predicate function",
288 G_PARAM_READABLE | G_PARAM_WRITABLE);
291 GParamSpec *selection_mode_spec =
292 g_param_spec_enum ("selection-mode",
294 "How many things can be selected",
295 GTK_TYPE_SELECTION_MODE,
296 GTK_SELECTION_MULTIPLE,
297 G_PARAM_CONSTRUCT | G_PARAM_READABLE | G_PARAM_WRITABLE);
299 object_class->set_property = psppire_dict_view_set_property;
300 object_class->get_property = psppire_dict_view_get_property;
302 g_object_class_override_property (object_class,
306 g_object_class_install_property (object_class,
310 g_object_class_install_property (object_class,
312 selection_mode_spec);
316 use_labels (PsppireDictView *dv)
318 gboolean disp_labels = TRUE;
320 if (gtk_check_menu_item_get_inconsistent (GTK_CHECK_MENU_ITEM
321 (dv->override_button)))
323 psppire_conf_get_boolean (psppire_conf_new (),
324 "VariableLists", "display-labels", &disp_labels);
328 disp_labels = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM
329 (dv->override_button));
335 /* A GtkTreeCellDataFunc which renders the name and/or label of the
338 var_description_cell_data_func (GtkTreeViewColumn *col,
339 GtkCellRenderer *cell,
340 GtkTreeModel *top_model,
341 GtkTreeIter *top_iter,
344 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
345 struct variable *var;
349 get_base_model (top_model, top_iter, &model, &iter);
351 gtk_tree_model_get (model,
352 &iter, DICT_TVM_COL_VAR, &var, -1);
354 if (var_has_label (var) && use_labels (dv))
356 gchar *text = g_markup_printf_escaped (
357 "<span stretch=\"condensed\">%s</span>",
358 var_get_label (var));
360 g_object_set (cell, "markup", text, NULL);
365 g_object_set (cell, "text", var_get_name (var), NULL);
373 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
376 var_icon_cell_data_func (GtkTreeViewColumn *col,
377 GtkCellRenderer *cell,
382 struct variable *var;
384 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
387 "stock-size", GTK_ICON_SIZE_MENU,
388 "icon-name", get_var_measurement_stock_id (var_get_print_format (var)->type,
389 var_get_measure (var)),
396 get_var_measurement_stock_id (enum fmt_type type, enum measure measure)
398 switch (fmt_get_category (type))
403 case MEASURE_UNKNOWN: return "role-none";
404 case MEASURE_NOMINAL: return "measure-string-nominal";
405 case MEASURE_ORDINAL: return "measure-string-ordinal";
406 case MEASURE_SCALE: return "role-none";
407 case n_MEASURES: break;
415 case MEASURE_UNKNOWN: return "role-none";
416 case MEASURE_NOMINAL: return "measure-date-nominal";
417 case MEASURE_ORDINAL: return "measure-date-ordinal";
418 case MEASURE_SCALE: return "measure-date-scale";
419 case n_MEASURES: break;
426 case MEASURE_UNKNOWN: return "role-none";
427 case MEASURE_NOMINAL: return "measure-nominal";
428 case MEASURE_ORDINAL: return "measure-ordinal";
429 case MEASURE_SCALE: return "measure-scale";
430 case n_MEASURES: break;
435 g_return_val_if_reached ("");
440 /* Sets the tooltip to be the name of the variable under the cursor */
442 set_tooltip_for_variable (GtkTreeView *treeview,
445 gboolean keyboard_mode,
452 GtkTreeModel *tree_model;
453 struct variable *var = NULL;
456 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
459 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
460 &path, NULL, NULL, NULL))
463 tree_model = gtk_tree_view_get_model (treeview);
465 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
467 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
469 gtk_tree_path_free (path);
473 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
475 if (! var_has_label (var))
485 get_base_model (tree_model, NULL, &m, NULL);
487 if (use_labels (PSPPIRE_DICT_VIEW (treeview)))
488 tip = var_get_name (var);
490 tip = var_get_label (var);
492 gtk_tooltip_set_text (tooltip, tip);
499 static struct variable *
500 psppire_dict_view_iter_to_var (PsppireDictView *dict_view,
501 GtkTreeIter *top_iter)
503 GtkTreeView *treeview = GTK_TREE_VIEW (dict_view);
504 GtkTreeModel *top_model = gtk_tree_view_get_model (treeview);
506 struct variable *var;
510 get_base_model (top_model, top_iter, &model, &iter);
511 g_assert (PSPPIRE_IS_DICT (model));
513 gtk_tree_model_get (model,
514 &iter, DICT_TVM_COL_VAR, &var, -1);
520 show_menu (PsppireDictView *dv, GdkEvent *event, gpointer data)
522 GdkEventButton *button_event = (GdkEventButton *) event;
523 if (button_event->button != 3)
526 dv->var_under_cursor = NULL;
527 GtkTreePath *path = NULL;
529 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (dv),
530 button_event->x, button_event->y,
531 &path, NULL, NULL, NULL);
535 GtkTreeModel *top_model = gtk_tree_view_get_model (GTK_TREE_VIEW (dv));
536 gtk_tree_model_get_iter (top_model, &iter, path);
537 dv->var_under_cursor = psppire_dict_view_iter_to_var (dv, &iter);
539 gtk_tree_path_free (path);
541 gtk_widget_set_sensitive (GTK_WIDGET (dv->var_info_check), is_row);
543 gtk_menu_popup_at_pointer (GTK_MENU (dv->menu), event);
549 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
551 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
553 gboolean global_setting = TRUE;
554 psppire_conf_get_boolean (psppire_conf_new (),
555 "VariableLists", "display-labels", &global_setting);
557 if (gtk_check_menu_item_get_inconsistent (checkbox))
558 gtk_check_menu_item_set_active (checkbox, !global_setting);
560 gtk_check_menu_item_set_inconsistent (checkbox, FALSE);
562 gtk_widget_queue_draw (GTK_WIDGET (dv));
567 set_sort_criteria (GtkCheckMenuItem *checkbox, PsppireDictView *dv, GtkTreeIterCompareFunc func)
569 if (!gtk_check_menu_item_get_active (checkbox))
571 gtk_widget_queue_draw (GTK_WIDGET (dv));
576 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dv->sorted_model), func, 0, 0);
579 gtk_widget_queue_draw (GTK_WIDGET (dv));
583 set_sort_criteria_default (GtkCheckMenuItem *checkbox, gpointer data)
585 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
586 set_sort_criteria (checkbox, dv, default_sort);
591 set_sort_criteria_name (GtkCheckMenuItem *checkbox, gpointer data)
593 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
594 set_sort_criteria (checkbox, dv, sort_by_name);
599 set_sort_criteria_label (GtkCheckMenuItem *checkbox, gpointer data)
601 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
602 set_sort_criteria (checkbox, dv, sort_by_label);
607 set_sort_criteria_unsorted (GtkCheckMenuItem *checkbox, gpointer data)
609 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
610 set_sort_criteria (checkbox, dv, unsorted);
614 check_item_set_inactive (GtkCheckMenuItem *cmi)
616 gtk_check_menu_item_set_active (cmi, FALSE);
620 pop_up_down_variable_information (GtkCheckMenuItem *cmi, PsppireDictView *dv)
623 g_object_get (cmi, "active", &active, NULL);
625 g_return_if_fail (dv->var_under_cursor);
630 GtkWidget *dialog = psppire_dialog_new ();
631 g_object_set (dialog,
632 "title", _("Variable Details"),
633 "help-page", "Manipulating-Variables",
636 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
637 gtk_container_add (GTK_CONTAINER (dialog), box);
639 GtkWidget *var_info = psppire_var_info_new ();
640 g_object_set (var_info, "variable", dv->var_under_cursor, NULL);
641 gtk_box_pack_start (GTK_BOX (box), var_info, TRUE, TRUE, 5);
643 GtkWidget *button_box = psppire_button_box_new ();
644 g_object_set (button_box,
645 "buttons", PSPPIRE_BUTTON_CLOSE_MASK | PSPPIRE_BUTTON_HELP_MASK,
647 g_object_set (button_box, "layout-style", GTK_BUTTONBOX_SPREAD, NULL);
648 gtk_box_pack_start (GTK_BOX (box), button_box, FALSE, TRUE, 5);
650 g_signal_connect_swapped (dialog, "unmap",
651 G_CALLBACK (check_item_set_inactive), cmi);
653 gtk_widget_show_all (box);
654 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
656 psppire_dialog_run (PSPPIRE_DIALOG (dialog));
660 psppire_dict_view_init (PsppireDictView *dict_view)
662 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
664 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
666 dict_view->prefer_labels_override = FALSE;
667 dict_view->sorted_model = NULL;
668 dict_view->var_under_cursor = NULL;
670 gtk_tree_view_column_set_title (col, _("Variable"));
672 gtk_tree_view_column_pack_start (col, renderer, FALSE);
674 gtk_tree_view_column_set_cell_data_func (col, renderer,
675 var_icon_cell_data_func,
678 renderer = gtk_cell_renderer_text_new ();
679 gtk_tree_view_column_pack_start (col, renderer, TRUE);
680 gtk_tree_view_column_set_cell_data_func (col, renderer,
681 var_description_cell_data_func,
684 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
685 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
687 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
689 /* FIXME: make this a value in terms of character widths */
690 gtk_tree_view_column_set_min_width (col, 150);
692 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
694 g_object_set (dict_view,
696 "headers-visible", FALSE,
699 g_signal_connect (dict_view, "query-tooltip",
700 G_CALLBACK (set_tooltip_for_variable), NULL);
702 dict_view->menu = gtk_menu_new ();
705 GSList *group = NULL;
707 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
709 dict_view->override_button = item;
710 gtk_check_menu_item_set_inconsistent (GTK_CHECK_MENU_ITEM (item),
713 g_signal_connect (item, "toggled",
714 G_CALLBACK (toggle_label_preference), dict_view);
716 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
718 item = gtk_separator_menu_item_new ();
719 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
721 item = gtk_radio_menu_item_new_with_label (group, _("Default sort order"));
722 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
723 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
724 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
725 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_default), dict_view);
727 item = gtk_radio_menu_item_new_with_label (group, _("Unsorted (dictionary order)"));
728 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
729 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
730 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_unsorted), dict_view);
732 item = gtk_radio_menu_item_new_with_label (group, _("Sort by name"));
733 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
734 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
735 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_name), dict_view);
737 item = gtk_radio_menu_item_new_with_label (group, _("Sort by label"));
738 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
739 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
740 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_label), dict_view);
742 item = gtk_separator_menu_item_new ();
743 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
745 item = gtk_check_menu_item_new_with_label (_("Variable Information"));
746 dict_view->var_info_check = item;
747 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
748 g_signal_connect (item, "toggled", G_CALLBACK (pop_up_down_variable_information), dict_view);
751 gtk_widget_show_all (dict_view->menu);
753 g_signal_connect (dict_view, "button-press-event",
754 G_CALLBACK (show_menu), NULL);
759 PsppireDictView *dict_view;
760 struct variable **vars;
765 get_vars_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
768 struct get_vars_aux *aux = data;
769 struct variable *var = psppire_dict_view_iter_to_var (aux->dict_view, iter);
771 g_return_if_fail (var != NULL);
772 aux->vars[aux->idx++] = var;
776 psppire_dict_view_get_selected_variables (PsppireDictView *dict_view,
777 struct variable ***vars,
780 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
781 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
782 gint n_vars = gtk_tree_selection_count_selected_rows (selection);
783 struct get_vars_aux aux;
785 *vars = g_malloc_n (n_vars, sizeof **vars);
787 aux.dict_view = dict_view;
790 gtk_tree_selection_selected_foreach (selection, get_vars_cb, &aux);
793 g_return_if_fail (aux.idx >= n_vars);
797 psppire_dict_view_get_selected_variable (PsppireDictView *dict_view)
799 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
800 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
803 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
804 return psppire_dict_view_iter_to_var (dict_view, &iter);