/* PSPPIRE - a graphical user interface for PSPP.
- Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation
+ Copyright (C) 2009, 2010, 2011, 2012, 2013, 2017 Free Software Foundation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include <gtk/gtk.h>
#include "psppire-dictview.h"
#include "psppire-dict.h"
+#include "dict-display.h"
#include "psppire-conf.h"
+#include "options-dialog.h"
#include <data/format.h>
#include <libpspp/i18n.h>
#include "helper.h"
static void psppire_dict_view_class_init (PsppireDictViewClass *class);
static void psppire_dict_view_init (PsppireDictView *dict_view);
-
GType
psppire_dict_view_get_type (void)
{
};
+
/* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
static gboolean
-filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+filter_variables (GtkTreeModel *tmodel, GtkTreeIter *titer, gpointer data)
{
var_predicate_func *predicate = data;
struct variable *var;
- PsppireDict *dict = PSPPIRE_DICT (model);
-
- GtkTreePath *path = gtk_tree_model_get_path (model, iter);
+ GtkTreeModel *model = NULL;
+ GtkTreeIter iter ;
+ PsppireDict *dict ;
+ GtkTreePath *path ;
+ gint *idx;
- gint *idx = gtk_tree_path_get_indices (path);
+ get_base_model (tmodel, titer, &model, &iter);
+ dict = PSPPIRE_DICT (model);
+ path = gtk_tree_model_get_path (model, &iter);
+ idx = gtk_tree_path_get_indices (path);
var = psppire_dict_get_variable (dict, *idx);
gtk_tree_path_free (path);
return predicate (var);
}
+static gint
+unsorted (GtkTreeModel *model,
+ GtkTreeIter *a,
+ GtkTreeIter *b,
+ gpointer user_data)
+{
+ const struct variable *var_a;
+ const struct variable *var_b;
+
+
+ gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
+ gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
+
+ return compare_var_ptrs_by_dict_index (&var_a, &var_b, NULL);
+}
+
+static gint
+sort_by_name (GtkTreeModel *model,
+ GtkTreeIter *a,
+ GtkTreeIter *b,
+ gpointer user_data)
+{
+ const struct variable *var_a;
+ const struct variable *var_b;
+
+ gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
+ gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
+
+ return g_strcmp0 (var_get_name (var_a), var_get_name (var_b));
+}
+
+
+static gint
+sort_by_label (GtkTreeModel *model,
+ GtkTreeIter *a,
+ GtkTreeIter *b,
+ gpointer user_data)
+{
+ const struct variable *var_a;
+ const struct variable *var_b;
+
+ gtk_tree_model_get (model, a, DICT_TVM_COL_VAR, &var_a, -1);
+ gtk_tree_model_get (model, b, DICT_TVM_COL_VAR, &var_b, -1);
+
+ return g_strcmp0 (var_get_label (var_a), var_get_label (var_b));
+}
+
+
+static gint
+default_sort (GtkTreeModel *model,
+ GtkTreeIter *a,
+ GtkTreeIter *b,
+ gpointer user_data)
+{
+ int what = -1;
+ psppire_conf_get_enum (psppire_conf_new (), "VariableLists", "sort-order",
+ PSPP_TYPE_OPTIONS_VAR_ORDER, &what);
+
+ switch (what)
+ {
+ default:
+ return unsorted (model, a, b, user_data);
+ break;
+ case PSPP_OPTIONS_VAR_ORDER_NAME:
+ return sort_by_name (model, a, b, user_data);
+ break;
+ case PSPP_OPTIONS_VAR_ORDER_LABEL:
+ return sort_by_label (model, a, b, user_data);
+ break;
+ }
+
+ g_assert_not_reached ();
+}
+
+
+
+
static void
set_model (PsppireDictView *dict_view)
{
- GtkTreeModel *model ;
+ GtkTreeModel *model = NULL;
if ( dict_view->dict == NULL)
return;
+ dict_view->sorted_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dict_view->dict));
+ gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dict_view->sorted_model), default_sort, dict_view, 0);
+ gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dict_view->sorted_model),
+ GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
+
if ( dict_view->predicate )
{
- model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict_view->dict),
- NULL);
+ model = gtk_tree_model_filter_new (dict_view->sorted_model, NULL);
gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
filter_variables,
}
else
{
- model = GTK_TREE_MODEL (dict_view->dict);
+ model = dict_view->sorted_model;
g_object_ref (model);
}
}
-
-static void
-dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
- GtkTreeModel **model, GtkTreeIter *iter)
+static gboolean
+use_labels (PsppireDictView *dv)
{
- *model = top_model;
+ gboolean disp_labels = TRUE;
- if ( iter)
- *iter = *top_iter;
-
- while ( ! PSPPIRE_IS_DICT (*model))
+ if (gtk_check_menu_item_get_inconsistent (GTK_CHECK_MENU_ITEM
+ (dv->override_button)))
{
- GtkTreeIter parent_iter;
- if (iter)
- parent_iter = *iter;
-
- if ( GTK_IS_TREE_MODEL_FILTER (*model))
- {
- GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
-
- *model = gtk_tree_model_filter_get_model (parent_model);
-
- if (iter)
- gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
- iter,
- &parent_iter);
- }
- else if (GTK_IS_TREE_MODEL_SORT (*model))
- {
- GtkTreeModelSort *parent_model = GTK_TREE_MODEL_SORT (*model);
-
- *model = gtk_tree_model_sort_get_model (parent_model);
-
- if (iter)
- gtk_tree_model_sort_convert_iter_to_child_iter (parent_model,
- iter,
- &parent_iter);
- }
+ psppire_conf_get_boolean (psppire_conf_new (),
+ "VariableLists", "display-labels", &disp_labels);
}
+ else
+ {
+ disp_labels = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM
+ (dv->override_button));
+ }
+ return disp_labels;
}
-
/* A GtkTreeCellDataFunc which renders the name and/or label of the
variable */
static void
struct variable *var;
GtkTreeIter iter;
GtkTreeModel *model;
- PsppireDict *dict;
-
- dv_get_base_model (top_model, top_iter, &model, &iter);
- dict = PSPPIRE_DICT (model);
+ get_base_model (top_model, top_iter, &model, &iter);
gtk_tree_model_get (model,
&iter, DICT_TVM_COL_VAR, &var, -1);
- if ( var_has_label (var) && dv->prefer_labels)
+ if (var_has_label (var) && use_labels (dv))
{
gchar *text = g_markup_printf_escaped (
"<span stretch=\"condensed\">%s</span>",
gpointer data)
{
struct variable *var;
+
gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
- if ( var_is_alpha (var))
- {
- g_object_set (cell, "stock-id", "var-string", NULL);
- }
- else
+ g_object_set (cell,
+ "stock-size", GTK_ICON_SIZE_MENU,
+ "icon-name", get_var_measurement_stock_id (var_get_print_format (var)->type,
+ var_get_measure (var)),
+ NULL);
+}
+
+const char *
+get_var_measurement_stock_id (enum fmt_type type, enum measure measure)
+{
+ switch (fmt_get_category (type))
{
- const struct fmt_spec *fs = var_get_print_format (var);
- int cat = fmt_get_category (fs->type);
- switch ( var_get_measure (var))
+ case FMT_CAT_STRING:
+ switch (measure)
{
- case MEASURE_NOMINAL:
- g_object_set (cell, "stock-id", "var-nominal", NULL);
- break;
- case MEASURE_ORDINAL:
- g_object_set (cell, "stock-id", "var-ordinal", NULL);
- break;
- case MEASURE_SCALE:
- if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
- g_object_set (cell, "stock-id", "var-date-scale", NULL);
- else
- g_object_set (cell, "stock-id", "var-scale", NULL);
- break;
- default:
- g_assert_not_reached ();
- };
+ case MEASURE_NOMINAL: return "measure-string-nominal";
+ case MEASURE_ORDINAL: return "measure-string-ordinal";
+ case MEASURE_SCALE: return "role-none";
+ case n_MEASURES: break;
+ }
+ break;
+
+ case FMT_CAT_DATE:
+ case FMT_CAT_TIME:
+ switch (measure)
+ {
+ case MEASURE_NOMINAL: return "measure-date-nominal";
+ case MEASURE_ORDINAL: return "measure-date-ordinal";
+ case MEASURE_SCALE: return "measure-date-scale";
+ case n_MEASURES: break;
+ }
+ break;
+
+ default:
+ switch (measure)
+ {
+ case MEASURE_NOMINAL: return "measure-nominal";
+ case MEASURE_ORDINAL: return "measure-ordinal";
+ case MEASURE_SCALE: return "measure-scale";
+ case n_MEASURES: break;
+ }
+ break;
}
+
+ g_return_val_if_reached ("");
}
+
/* Sets the tooltip to be the name of the variable under the cursor */
static gboolean
set_tooltip_for_variable (GtkTreeView *treeview,
{
const gchar *tip ;
GtkTreeModel *m;
- PsppireDict *dict;
- dv_get_base_model (tree_model, NULL, &m, NULL);
- dict = PSPPIRE_DICT (m);
+ get_base_model (tree_model, NULL, &m, NULL);
- if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
+ if ( use_labels (PSPPIRE_DICT_VIEW (treeview)))
tip = var_get_name (var);
else
tip = var_get_label (var);
}
static gboolean
-show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
+show_menu (PsppireDictView *dv, GdkEvent *event, gpointer data)
{
- if (event->button != 3)
+ if (((GdkEventButton *) event)->button != 3)
return FALSE;
- gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
- event->button, event->time);
+ gtk_menu_popup_at_pointer (GTK_MENU (dv->menu), event);
return TRUE;
}
{
PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
- dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
+ gboolean global_setting = TRUE;
+ psppire_conf_get_boolean (psppire_conf_new (),
+ "VariableLists", "display-labels", &global_setting);
+
+ if (gtk_check_menu_item_get_inconsistent (checkbox))
+ gtk_check_menu_item_set_active (checkbox, !global_setting);
+
+ gtk_check_menu_item_set_inconsistent (checkbox, FALSE);
gtk_widget_queue_draw (GTK_WIDGET (dv));
}
+static void
+set_sort_criteria (GtkCheckMenuItem *checkbox, PsppireDictView *dv, GtkTreeIterCompareFunc func)
+{
+ if (!gtk_check_menu_item_get_active (checkbox))
+ {
+ gtk_widget_queue_draw (GTK_WIDGET (dv));
+ return;
+ }
+
+
+ gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (dv->sorted_model), func, 0, 0);
+
+
+ gtk_widget_queue_draw (GTK_WIDGET (dv));
+}
+
+static void
+set_sort_criteria_default (GtkCheckMenuItem *checkbox, gpointer data)
+{
+ PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
+ set_sort_criteria (checkbox, dv, default_sort);
+}
+
+
+static void
+set_sort_criteria_name (GtkCheckMenuItem *checkbox, gpointer data)
+{
+ PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
+ set_sort_criteria (checkbox, dv, sort_by_name);
+}
+
+
+static void
+set_sort_criteria_label (GtkCheckMenuItem *checkbox, gpointer data)
+{
+ PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
+ set_sort_criteria (checkbox, dv, sort_by_label);
+}
+
+
+static void
+set_sort_criteria_unsorted (GtkCheckMenuItem *checkbox, gpointer data)
+{
+ PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
+ set_sort_criteria (checkbox, dv, unsorted);
+}
+
+
static void
psppire_dict_view_init (PsppireDictView *dict_view)
GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
- dict_view->prefer_labels = TRUE;
-
- psppire_conf_get_boolean (psppire_conf_new (),
- G_OBJECT_TYPE_NAME (dict_view),
- "prefer-labels",
- &dict_view->prefer_labels);
+ dict_view->prefer_labels_override = FALSE;
+ dict_view->sorted_model = NULL;
gtk_tree_view_column_set_title (col, _("Variable"));
dict_view->menu = gtk_menu_new ();
-
{
- GtkWidget *checkbox =
+ GSList *group = NULL;
+ GtkWidget *item =
gtk_check_menu_item_new_with_label (_("Prefer variable labels"));
- gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox),
- dict_view->prefer_labels);
+ dict_view->override_button = item;
+ gtk_check_menu_item_set_inconsistent (GTK_CHECK_MENU_ITEM (item),
+ TRUE);
- g_signal_connect (checkbox, "toggled",
+ g_signal_connect (item, "toggled",
G_CALLBACK (toggle_label_preference), dict_view);
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
+
+ item = gtk_separator_menu_item_new ();
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
- gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
+ item = gtk_radio_menu_item_new_with_label (group, _("Default sort order"));
+ group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
+ g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_default), dict_view);
+ item = gtk_radio_menu_item_new_with_label (group, _("Unsorted (dictionary order)"));
+ group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
+ g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_unsorted), dict_view);
+
+ item = gtk_radio_menu_item_new_with_label (group, _("Sort by name"));
+ group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
+ g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_name), dict_view);
+
+ item = gtk_radio_menu_item_new_with_label (group, _("Sort by label"));
+ group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
+ gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), item);
+ g_signal_connect (item, "toggled", G_CALLBACK (set_sort_criteria_label), dict_view);
}
gtk_widget_show_all (dict_view->menu);
return GTK_WIDGET (g_object_new (psppire_dict_view_get_type (), NULL));
}
-
-
-struct variable *
-psppire_dict_view_get_selected_variable (PsppireDictView *treeview)
+static struct variable *
+psppire_dict_view_iter_to_var (PsppireDictView *dict_view,
+ GtkTreeIter *top_iter)
{
- struct variable *var;
- GtkTreeModel *top_model;
- GtkTreeIter top_iter;
+ GtkTreeView *treeview = GTK_TREE_VIEW (dict_view);
+ GtkTreeModel *top_model = gtk_tree_view_get_model (treeview);
+ struct variable *var;
GtkTreeModel *model;
GtkTreeIter iter;
- GtkTreeSelection *selection =
- gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
-
- if (! gtk_tree_selection_get_selected (selection,
- &top_model, &top_iter))
- return NULL;
-
- dv_get_base_model (top_model, &top_iter, &model, &iter);
+ get_base_model (top_model, top_iter, &model, &iter);
g_assert (PSPPIRE_IS_DICT (model));
return var;
}
+struct get_vars_aux
+ {
+ PsppireDictView *dict_view;
+ struct variable **vars;
+ size_t idx;
+ };
+
+static void
+get_vars_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
+ gpointer data)
+{
+ struct get_vars_aux *aux = data;
+ struct variable *var = psppire_dict_view_iter_to_var (aux->dict_view, iter);
+
+ g_return_if_fail (var != NULL);
+ aux->vars[aux->idx++] = var;
+}
+
+void
+psppire_dict_view_get_selected_variables (PsppireDictView *dict_view,
+ struct variable ***vars,
+ size_t *n_varsp)
+{
+ GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
+ GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
+ gint n_vars = gtk_tree_selection_count_selected_rows (selection);
+ struct get_vars_aux aux;
+
+ *vars = g_malloc_n (n_vars, sizeof **vars);
+
+ aux.dict_view = dict_view;
+ aux.vars = *vars;
+ aux.idx = 0;
+ gtk_tree_selection_selected_foreach (selection, get_vars_cb, &aux);
+
+ *n_varsp = aux.idx;
+ g_return_if_fail (aux.idx >= n_vars);
+}
+
+struct variable *
+psppire_dict_view_get_selected_variable (PsppireDictView *dict_view)
+{
+ GtkTreeView *tree_view = GTK_TREE_VIEW (dict_view);
+ GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
+ GtkTreeIter iter;
+
+ if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+ return psppire_dict_view_iter_to_var (dict_view, &iter);
+ else
+ return NULL;
+}
+