1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2012, 2013 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 <data/format.h>
25 #include <libpspp/i18n.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 static void psppire_dict_view_base_finalize (PsppireDictViewClass *, gpointer);
33 static void psppire_dict_view_base_init (PsppireDictViewClass *class);
34 static void psppire_dict_view_class_init (PsppireDictViewClass *class);
35 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);
85 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
87 filter_variables (GtkTreeModel *tmodel, GtkTreeIter *titer, gpointer data)
89 var_predicate_func *predicate = data;
91 GtkTreeModel *model = NULL;
97 get_base_model (tmodel, titer, &model, &iter);
99 dict = PSPPIRE_DICT (model);
100 path = gtk_tree_model_get_path (model, &iter);
101 idx = gtk_tree_path_get_indices (path);
102 var = psppire_dict_get_variable (dict, *idx);
104 gtk_tree_path_free (path);
106 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));
158 set_model (PsppireDictView *dict_view)
160 GtkTreeModel *model = NULL;
162 if ( dict_view->dict == NULL)
165 dict_view->sorted_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dict_view->dict));
166 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dict_view->sorted_model), unsorted, dict_view, 0);
167 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dict_view->sorted_model),
168 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
170 if ( dict_view->predicate )
172 model = gtk_tree_model_filter_new (dict_view->sorted_model, NULL);
174 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
176 dict_view->predicate,
181 model = dict_view->sorted_model;
182 g_object_ref (model);
185 gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
186 g_object_unref (model);
190 psppire_dict_view_set_property (GObject *object,
195 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
199 case PROP_DICTIONARY:
200 dict_view->dict = g_value_get_object (value);
203 dict_view->predicate = g_value_get_pointer (value);
205 case PROP_SELECTION_MODE:
207 GtkTreeSelection *selection =
208 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
210 GtkSelectionMode mode = g_value_get_enum (value);
212 gtk_tree_selection_set_mode (selection, mode);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 set_model (dict_view);
226 psppire_dict_view_get_property (GObject *object,
231 PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
235 case PROP_DICTIONARY:
236 g_value_set_object (value, dict_view->dict);
239 g_value_set_pointer (value, dict_view->predicate);
241 case PROP_SELECTION_MODE:
243 GtkTreeSelection *selection =
244 gtk_tree_view_get_selection (GTK_TREE_VIEW (dict_view));
246 g_value_set_enum (value, gtk_tree_selection_get_mode (selection));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 psppire_dict_view_class_init (PsppireDictViewClass *class)
259 GObjectClass *object_class = G_OBJECT_CLASS (class);
261 GParamSpec *predicate_spec =
262 g_param_spec_pointer ("predicate",
264 "A predicate function",
265 G_PARAM_READABLE | G_PARAM_WRITABLE);
268 GParamSpec *selection_mode_spec =
269 g_param_spec_enum ("selection-mode",
271 "How many things can be selected",
272 GTK_TYPE_SELECTION_MODE,
273 GTK_SELECTION_MULTIPLE,
274 G_PARAM_CONSTRUCT | G_PARAM_READABLE | G_PARAM_WRITABLE);
276 object_class->set_property = psppire_dict_view_set_property;
277 object_class->get_property = psppire_dict_view_get_property;
279 g_object_class_override_property (object_class,
283 g_object_class_install_property (object_class,
287 g_object_class_install_property (object_class,
289 selection_mode_spec);
294 psppire_dict_view_base_init (PsppireDictViewClass *class)
296 GObjectClass *object_class = G_OBJECT_CLASS (class);
298 object_class->finalize = psppire_dict_view_finalize;
304 psppire_dict_view_base_finalize (PsppireDictViewClass *class,
313 /* A GtkTreeCellDataFunc which renders the name and/or label of the
316 var_description_cell_data_func (GtkTreeViewColumn *col,
317 GtkCellRenderer *cell,
318 GtkTreeModel *top_model,
319 GtkTreeIter *top_iter,
322 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
323 struct variable *var;
327 get_base_model (top_model, top_iter, &model, &iter);
329 gtk_tree_model_get (model,
330 &iter, DICT_TVM_COL_VAR, &var, -1);
332 if ( var_has_label (var) && dv->prefer_labels)
334 gchar *text = g_markup_printf_escaped (
335 "<span stretch=\"condensed\">%s</span>",
336 var_get_label (var));
338 g_object_set (cell, "markup", text, NULL);
343 g_object_set (cell, "text", var_get_name (var), NULL);
349 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
352 var_icon_cell_data_func (GtkTreeViewColumn *col,
353 GtkCellRenderer *cell,
358 struct variable *var;
360 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
362 g_object_set (cell, "stock_id",
363 get_var_measurement_stock_id (var_get_print_format (var)->type,
364 var_get_measure (var)),
369 get_var_measurement_stock_id (enum fmt_type type, enum measure measure)
371 switch (fmt_get_category (type))
376 case MEASURE_NOMINAL: return "measure-string-nominal";
377 case MEASURE_ORDINAL: return "measure-string-ordinal";
378 case MEASURE_SCALE: return "role-none";
379 case n_MEASURES: break;
387 case MEASURE_NOMINAL: return "measure-date-nominal";
388 case MEASURE_ORDINAL: return "measure-date-ordinal";
389 case MEASURE_SCALE: return "measure-date-scale";
390 case n_MEASURES: break;
397 case MEASURE_NOMINAL: return "measure-nominal";
398 case MEASURE_ORDINAL: return "measure-ordinal";
399 case MEASURE_SCALE: return "measure-scale";
400 case n_MEASURES: break;
405 g_return_val_if_reached ("");
410 /* Sets the tooltip to be the name of the variable under the cursor */
412 set_tooltip_for_variable (GtkTreeView *treeview,
415 gboolean keyboard_mode,
422 GtkTreeModel *tree_model;
423 struct variable *var = NULL;
426 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
429 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
430 &path, NULL, NULL, NULL))
433 tree_model = gtk_tree_view_get_model (treeview);
435 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
437 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
439 gtk_tree_path_free (path);
444 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
446 if ( ! var_has_label (var))
453 get_base_model (tree_model, NULL, &m, NULL);
455 if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
456 tip = var_get_name (var);
458 tip = var_get_label (var);
460 gtk_tooltip_set_text (tooltip, tip);
467 show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
469 if (event->button != 3)
472 gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
473 event->button, event->time);
479 toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
481 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
483 dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
485 gtk_widget_queue_draw (GTK_WIDGET (dv));
490 set_sort_criteria (GtkCheckMenuItem *checkbox, PsppireDictView *dv, GtkTreeIterCompareFunc func)
492 if (!gtk_check_menu_item_get_active (checkbox))
494 gtk_widget_queue_draw (GTK_WIDGET (dv));
499 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dv->sorted_model), func, 0, 0);
502 gtk_widget_queue_draw (GTK_WIDGET (dv));
506 set_sort_criteria_name (GtkCheckMenuItem *checkbox, gpointer data)
508 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
509 set_sort_criteria (checkbox, dv, sort_by_name);
514 set_sort_criteria_label (GtkCheckMenuItem *checkbox, gpointer data)
516 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
517 set_sort_criteria (checkbox, dv, sort_by_label);
522 set_sort_criteria_unsorted (GtkCheckMenuItem *checkbox, gpointer data)
524 PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
525 set_sort_criteria (checkbox, dv, unsorted);
531 psppire_dict_view_init (PsppireDictView *dict_view)
533 GtkTreeViewColumn *col = gtk_tree_view_column_new ();
535 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
537 dict_view->prefer_labels = TRUE;
538 dict_view->sorted_model = NULL;
540 psppire_conf_get_boolean (psppire_conf_new (),
541 G_OBJECT_TYPE_NAME (dict_view),
543 &dict_view->prefer_labels);
545 gtk_tree_view_column_set_title (col, _("Variable"));
547 gtk_tree_view_column_pack_start (col, renderer, FALSE);
549 gtk_tree_view_column_set_cell_data_func (col, renderer,
550 var_icon_cell_data_func,
553 renderer = gtk_cell_renderer_text_new ();
554 gtk_tree_view_column_pack_start (col, renderer, TRUE);
555 gtk_tree_view_column_set_cell_data_func (col, renderer,
556 var_description_cell_data_func,
559 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
560 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
562 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
564 /* FIXME: make this a value in terms of character widths */
565 gtk_tree_view_column_set_min_width (col, 150);
567 gtk_tree_view_append_column (GTK_TREE_VIEW (dict_view), col);
569 g_object_set (dict_view,
571 "headers-visible", FALSE,
574 g_signal_connect (dict_view, "query-tooltip",
575 G_CALLBACK (set_tooltip_for_variable), NULL);
577 dict_view->menu = gtk_menu_new ();
581 GSList *group = NULL;
583 gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
585 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
586 dict_view->prefer_labels);
588 g_signal_connect (item, "toggled",
589 G_CALLBACK (toggle_label_preference), dict_view);
591 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
593 item = gtk_separator_menu_item_new ();
594 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
596 item = gtk_radio_menu_item_new_with_label (group, _("Unsorted (dictionary order)"));
597 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
598 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
599 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
600 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_unsorted), dict_view);
602 item = gtk_radio_menu_item_new_with_label (group, _("Sort by name"));
603 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
604 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
605 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_name), dict_view);
607 item = gtk_radio_menu_item_new_with_label (group, _("Sort by label"));
608 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
609 gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
610 g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_label), dict_view);
613 gtk_widget_show_all (dict_view->menu);
615 g_signal_connect (dict_view, "button-press-event",
616 G_CALLBACK (show_menu), NULL);
621 psppire_dict_view_new (void)
623 return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
626 static struct variable *
627 psppire_dict_view_iter_to_var (PsppireDictView *dict_view,
628 GtkTreeIter *top_iter)
630 GtkTreeView *treeview = GTK_TREE_VIEW (dict_view);
631 GtkTreeModel *top_model = gtk_tree_view_get_model (treeview);
633 struct variable *var;
637 get_base_model (top_model, top_iter, &model, &iter);
639 g_assert (PSPPIRE_IS_DICT (model));
641 gtk_tree_model_get (model,
642 &iter, DICT_TVM_COL_VAR, &var, -1);
649 PsppireDictView *dict_view;
650 struct variable **vars;
655 get_vars_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
658 struct get_vars_aux *aux = data;
659 struct variable *var = psppire_dict_view_iter_to_var (aux->dict_view, iter);
661 g_return_if_fail (var != NULL);
662 aux->vars[aux->idx++] = var;
666 psppire_dict_view_get_selected_variables (PsppireDictView *dict_view,
667 struct variable ***vars,
670 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
671 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
672 gint n_vars = gtk_tree_selection_count_selected_rows (selection);
673 struct get_vars_aux aux;
675 *vars = g_malloc_n (n_vars, sizeof **vars);
677 aux.dict_view = dict_view;
680 gtk_tree_selection_selected_foreach (selection, get_vars_cb, &aux);
683 g_return_if_fail (aux.idx >= n_vars);
687 psppire_dict_view_get_selected_variable (PsppireDictView *dict_view)
689 GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
690 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
693 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
694 return psppire_dict_view_iter_to_var (dict_view, &iter);