#include <libpspp/i18n.h>
#include "dialog-common.h"
+#include "dict-display.h"
#include "psppire-var-ptr.h"
#include "helper.h"
+/*
+ If m is not a base TreeModel type (ie, is a filter or sorter) then
+ convert OP to a TreePath for the base and return it.
+ The return value must be freed by the caller.
+*/
+static GtkTreePath *
+get_base_tree_path (GtkTreeModel *m, GtkTreePath *op)
+{
+ GtkTreePath *p = gtk_tree_path_copy (op);
+ while ( ! PSPPIRE_IS_DICT (m))
+ {
+ GtkTreePath *oldp = p;
+
+ if (GTK_IS_TREE_MODEL_FILTER (m))
+ {
+ p = gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (m), oldp);
+ m = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (m));
+ }
+ else if (GTK_IS_TREE_MODEL_SORT (m))
+ {
+ p = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (m), oldp);
+ m = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (m));
+ }
+ else
+ {
+ g_error ("Unexpected model type: %s", G_OBJECT_TYPE_NAME (m));
+ }
+
+ gtk_tree_path_free (oldp);
+ }
+
+ return p;
+}
+
/* Returns FALSE if the variables represented by the union of the rows
currently selected by SOURCE widget, and contents of the DEST
GtkTreeIter iter;
gboolean retval = TRUE;
- GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
+ GtkTreeModel *top_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
+ GtkTreeModel *model;
PsppireDict *dict;
GtkTreeSelection *selection;
GList *list, *l;
bool have_type;
- while (GTK_IS_TREE_MODEL_FILTER (model))
- {
- model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
- }
+
+ get_base_model (top_model, NULL, &model, NULL);
dict = PSPPIRE_DICT (model);
have_type = false;
for (l = list; l ; l = l->next)
{
- GtkTreePath *path = l->data;
-
- GtkTreePath *fpath =
- gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (model), path);
-
- gint *idx = gtk_tree_path_get_indices (fpath);
-
+ GtkTreePath *p = get_base_tree_path (top_model, l->data);
+ gint *idx = gtk_tree_path_get_indices (p);
const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
- gtk_tree_path_free (fpath);
+ gtk_tree_path_free (p);
if (have_type && var_get_type (v) != type)
{
{
gboolean retval = TRUE;
- GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
+ GtkTreeModel *top_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
+ GtkTreeModel *model = NULL;
PsppireDict *dict;
GtkTreeSelection *selection;
GList *list, *l;
- while (GTK_IS_TREE_MODEL_FILTER (model))
- {
- model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
- }
+ get_base_model (top_model, NULL, &model, NULL);
dict = PSPPIRE_DICT (model);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
- list = gtk_tree_selection_get_selected_rows (selection, &model);
+ list = gtk_tree_selection_get_selected_rows (selection, &top_model);
/* Iterate through the selection of the source treeview */
for (l = list; l ; l = l->next)
{
- GtkTreePath *path = l->data;
- GtkTreePath *fpath = gtk_tree_model_filter_convert_path_to_child_path
- (GTK_TREE_MODEL_FILTER (model), path);
-
- gint *idx = gtk_tree_path_get_indices (fpath);
-
+ GtkTreePath *p = get_base_tree_path (top_model, l->data);
+ gint *idx = gtk_tree_path_get_indices (p);
const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
-
- gtk_tree_path_free (fpath);
+ gtk_tree_path_free (p);
if ( var_is_alpha (v))
{
#include <gtk/gtk.h>
#include "psppire-dictview.h"
#include "psppire-dict.h"
+#include "dict-display.h"
#include "psppire-conf.h"
#include <data/format.h>
#include <libpspp/i18n.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 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), unsorted, 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)
-{
- *model = top_model;
-
- if ( iter)
- *iter = *top_iter;
-
- while ( ! PSPPIRE_IS_DICT (*model))
- {
- 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);
- }
- }
-}
-
/* A GtkTreeCellDataFunc which renders the name and/or label of the
GtkTreeIter iter;
GtkTreeModel *model;
- dv_get_base_model (top_model, top_iter, &model, &iter);
+ get_base_model (top_model, top_iter, &model, &iter);
gtk_tree_model_get (model,
&iter, DICT_TVM_COL_VAR, &var, -1);
const gchar *tip ;
GtkTreeModel *m;
- dv_get_base_model (tree_model, NULL, &m, NULL);
+ get_base_model (tree_model, NULL, &m, NULL);
if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
tip = var_get_name (var);
}
+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_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;
+ dict_view->sorted_model = NULL;
psppire_conf_get_boolean (psppire_conf_new (),
G_OBJECT_TYPE_NAME (dict_view),
{
- 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),
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
dict_view->prefer_labels);
- 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);
+
+ 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_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_unsorted), dict_view);
- gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
+ 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);
GtkTreeModel *model;
GtkTreeIter iter;
- 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));