Added display sort feature to PsppireDictView
[pspp] / src / ui / gui / dialog-common.c
index c6832df4d05d4b783200f6a5e33bdd33fae463f2..0d32904d141434e55488b5c9ee1c59e6a79adf5a 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007  Free Software Foundation
+   Copyright (C) 2007, 2014  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
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
+
+#include <libpspp/i18n.h>
 #include "dialog-common.h"
+#include "dict-display.h"
 
-#include "helper.h"
+#include "psppire-var-ptr.h"
 
+#include "helper.h"
 
-/* Append the names of selected variables to STRING.
-   TREEVIEW is the treeview containing the variables.
-   DICT is the dictionary for those variables.
+/* 
+   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.
 */
-gint
-append_variable_names (GString *string,
-                      PsppireDict *dict, GtkTreeView *treeview)
+static GtkTreePath *
+get_base_tree_path (GtkTreeModel *m, GtkTreePath *op)
 {
-  gint n_vars = 0;
-  GtkTreeIter iter;
-
-  GtkTreeModel *list_store =
-    gtk_tree_view_get_model (treeview);
-
-  if ( gtk_tree_model_get_iter_first (list_store, &iter) )
+  GtkTreePath *p = gtk_tree_path_copy (op);
+  while ( ! PSPPIRE_IS_DICT (m))
     {
-      do
+      GtkTreePath *oldp = p;
+      
+      if (GTK_IS_TREE_MODEL_FILTER (m))
        {
-         GValue value = {0};
-         struct variable *var;
-         GtkTreePath *path = gtk_tree_model_get_path (list_store, &iter);
-
-         gtk_tree_model_get_value (list_store, &iter, 0, &value);
-
-         var = psppire_dict_get_variable (dict, g_value_get_int (&value));
-         g_value_unset (&value);
-
-         g_string_append (string, " ");
-         g_string_append (string, var_get_name (var));
-
-         gtk_tree_path_free (path);
-         n_vars++;
+         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));
        }
-      while (gtk_tree_model_iter_next (list_store, &iter));
+      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 n_vars;
-}
-
-
-
-struct variable *
-get_selected_variable (GtkTreeModel *treemodel,
-                      GtkTreeIter *iter,
-                      PsppireDict *dict)
-{
-  struct variable *var;
-  GValue value = {0};
-
-  GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
-
-  gtk_tree_model_get_value (treemodel, iter, 0, &value);
-
-  gtk_tree_path_free (path);
-
-  var =  psppire_dict_get_variable (dict, g_value_get_int (&value));
-
-  g_value_unset (&value);
-
-  return var;
-}
-
-
-
-
-/* A (*GtkTreeCellDataFunc) function.
-   This function expects TREEMODEL to hold G_TYPE_INT.  The ints it holds
-   are the indices of the variables in the dictionary, which DATA points to.
-   It renders the name of the variable into CELL.
-*/
-void
-cell_var_name (GtkTreeViewColumn *tree_column,
-              GtkCellRenderer *cell,
-              GtkTreeModel *tree_model,
-              GtkTreeIter *iter,
-              gpointer data)
-{
-  PsppireDict *dict = data;
-  struct variable *var;
-  gchar *name;
-
-  var = get_selected_variable (tree_model, iter, dict);
-
-  name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
-  g_object_set (cell, "text", name, NULL);
-  g_free (name);
-}
-
-
-
-/* Set a model for DEST, which is an GtkListStore of g_int's
-   whose values are the indices into DICT */
-void
-set_dest_model (GtkTreeView *dest, PsppireDict *dict)
-{
-  GtkTreeViewColumn *col;
-  GtkListStore *dest_list = gtk_list_store_new (1, G_TYPE_INT);
-  GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
-
-  gtk_tree_view_set_model (GTK_TREE_VIEW (dest), GTK_TREE_MODEL (dest_list));
-
-  col = gtk_tree_view_column_new_with_attributes ("Var",
-                                                 renderer,
-                                                 "text",
-                                                 0,
-                                                 NULL);
-
-  gtk_tree_view_column_set_cell_data_func (col, renderer,
-                                          cell_var_name,
-                                          dict, 0);
-
-  /* FIXME: make this a value in terms of character widths */
-  g_object_set (col, "min-width",  100, NULL);
-
-  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
-
-  gtk_tree_view_append_column (GTK_TREE_VIEW (dest), col);
+  return p;
 }
 
 
-
 /* Returns FALSE if the variables represented by the union of the rows
    currently selected by SOURCE widget, and contents of the DEST
    widget, are of different types.
@@ -156,17 +74,17 @@ homogeneous_types (GtkWidget *source, GtkWidget *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;
-  enum val_type type = -1;
+  enum val_type type;
   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);
 
@@ -175,30 +93,23 @@ homogeneous_types (GtkWidget *source, GtkWidget *dest)
   list = gtk_tree_selection_get_selected_rows (selection, &model);
 
   /* Iterate through the selection of the source treeview */
+  have_type = false;
   for (l = list; l ; l = l->next)
     {
-      GtkTreePath *path = l->data;
-      GtkTreePath *fpath;
-      gint *idx;
-
-      const struct variable *v;
+      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]);
 
-      fpath = gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (model), path);
+      gtk_tree_path_free (p);
 
-      idx = gtk_tree_path_get_indices (fpath);
-
-      v = psppire_dict_get_variable (dict, idx[0]);
-
-      if ( type != -1 )
-       {
-         if ( var_get_type (v) != type )
-           {
-             retval = FALSE;
-             break;
-           }
-       }
+      if (have_type && var_get_type (v) != type)
+        {
+          retval = FALSE;
+          break;
+        }
 
       type = var_get_type (v);
+      have_type = true;
     }
 
   g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
@@ -214,25 +125,19 @@ homogeneous_types (GtkWidget *source, GtkWidget *dest)
        ok;
        ok = gtk_tree_model_iter_next (model, &iter))
     {
-      gint idx;
       const struct variable *v;
-      gtk_tree_model_get (model, &iter, 0, &idx, -1);
-
-      v = psppire_dict_get_variable (dict, idx);
+      gtk_tree_model_get (model, &iter, 0, &v, -1);
 
-      if ( type != -1 )
-       {
-         if ( var_get_type (v) != type )
-           {
-             retval = FALSE;
-             break;
-           }
-       }
+      if ( have_type && var_get_type (v) != type )
+        {
+          retval = FALSE;
+          break;
+        }
 
       type = var_get_type (v);
+      have_type = true;
     }
 
-
   return retval;
 }
 
@@ -242,42 +147,30 @@ homogeneous_types (GtkWidget *source, GtkWidget *dest)
 gboolean
 numeric_only (GtkWidget *source, GtkWidget *dest)
 {
-  gboolean ok;
-  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 = 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;
-      gint *idx;
-
-      const struct variable *v;
-
-      fpath = gtk_tree_model_filter_convert_path_to_child_path
-       (GTK_TREE_MODEL_FILTER (model), path);
-
-      idx = gtk_tree_path_get_indices (fpath);
-
-      v = psppire_dict_get_variable (dict, idx[0]);
+      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 (p);
 
       if ( var_is_alpha (v))
        {
@@ -292,3 +185,30 @@ numeric_only (GtkWidget *source, GtkWidget *dest)
   return retval;
 }
 
+/*
+  A pair of functions intended to be used as callbacks for the "toggled" signal
+  of a GtkToggleButton widget.  They make the sensitivity of W follow the status
+  of the togglebutton.
+*/
+void
+set_sensitivity_from_toggle (GtkToggleButton *togglebutton,  GtkWidget *w)
+{
+  gboolean active = gtk_toggle_button_get_active (togglebutton);
+
+  gtk_widget_set_sensitive (w, active);
+  if (active)
+    gtk_widget_grab_focus (w);
+}
+
+/* */
+void
+set_sensitivity_from_toggle_invert (GtkToggleButton *togglebutton,
+                                   GtkWidget *w)
+{
+  gboolean active = gtk_toggle_button_get_active (togglebutton);
+
+  gtk_widget_set_sensitive (w, !active);
+}
+
+
+