New interface PsppireSelectDestWidget.
[pspp-builds.git] / src / ui / gui / psppire-selector.c
index 914c870174907e22d2b250797a3d20ba231d3e40..83fe2d82792a04fb34425b3f0f5a221dde2238fb 100644 (file)
@@ -12,7 +12,8 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 /*
   This module provides a widget, PsppireSelector derived from
 
 #include <config.h>
 
+#include "psppire-dictview.h"
+#include "psppire-var-view.h"
+#include "psppire-dict.h"
+#include "psppire-select-dest.h"
+
 #include <gtk/gtksignal.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkentry.h>
@@ -146,6 +152,7 @@ enum
 {
   PROP_0,
   PROP_ORIENTATION,
+  PROP_PRIMARY,
   PROP_SOURCE_WIDGET,
   PROP_DEST_WIDGET
 };
@@ -170,6 +177,10 @@ psppire_selector_set_property (GObject         *object,
       selector->orientation = g_value_get_enum (value);
       set_direction (selector, selector->direction);
       break;
+    case PROP_PRIMARY:
+      selector->primary_requested = TRUE;
+      update_subjects (selector);
+      break;
     case PROP_SOURCE_WIDGET:
       selector->source = g_value_dup_object (value);
       update_subjects (selector);
@@ -210,8 +221,6 @@ psppire_selector_get_property (GObject         *object,
     };
 }
 
-
-
 static void
 psppire_selector_class_init (PsppireSelectorClass *class)
 {
@@ -224,6 +233,15 @@ psppire_selector_class_init (PsppireSelectorClass *class)
                       PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
                       G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
 
+
+ /* Meaningfull only if more than one selector shares this selectors source */
+  GParamSpec *primary_spec =
+    g_param_spec_boolean ("primary",
+                         "Primary",
+                         "Whether this selector should be the primary selector for the source",
+                         FALSE,
+                         G_PARAM_READWRITE);
+
   GParamSpec *source_widget_spec = 
     g_param_spec_object ("source-widget",
                         "Source Widget",
@@ -246,6 +264,10 @@ psppire_selector_class_init (PsppireSelectorClass *class)
                                    PROP_ORIENTATION,
                                    orientation_spec);
 
+  g_object_class_install_property (object_class,
+                                   PROP_PRIMARY,
+                                   primary_spec);
+
   g_object_class_install_property (object_class,
                                    PROP_SOURCE_WIDGET,
                                    source_widget_spec);
@@ -275,6 +297,8 @@ psppire_selector_class_init (PsppireSelectorClass *class)
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);
+
+  class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal);
 }
 
 
@@ -296,13 +320,77 @@ psppire_selector_base_finalize(PsppireSelectorClass *class,
                                gpointer class_data)
 {
   g_hash_table_destroy (class->source_hash);
+  g_hash_table_destroy (class->default_selection_funcs);
 }
 
+/* Callback for when the source treeview is activated (double clicked) */
+static void
+on_row_activate (GtkTreeView       *tree_view,
+                GtkTreePath       *path,
+                GtkTreeViewColumn *column,
+                gpointer           data)
+{
+  PsppireSelector *selector  = data;
+
+  gtk_action_activate (selector->action);
+}
+
+/* Callback for when the source selection changes */
+static void
+on_source_select (GtkTreeSelection *treeselection, gpointer data)
+{
+  PsppireSelector *selector = data;
+
+  set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
+
+  if ( selector->allow_selection )
+    {
+      gtk_action_set_sensitive (selector->action,
+                               selector->allow_selection (selector->source, selector->dest));
+    }
+  else if ( GTK_IS_ENTRY (selector->dest) )
+    {
+      gtk_action_set_sensitive (selector->action,
+                               gtk_tree_selection_count_selected_rows
+                               (treeselection) <= 1 );
+    }
+}
+
+
+static void
+on_realize (PsppireSelector *selector)
+{
+  PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+  GtkTreeSelection* selection ;
+
+  GList *list = g_hash_table_lookup (class->source_hash, selector->source);
+
+  if ( NULL == list)
+    return;
+
+  if ( g_list_first (list)->data == selector)
+    {
+      if ( selector->row_activate_id )
+       g_signal_handler_disconnect (selector->source, selector->row_activate_id);
+
+      selector->row_activate_id =  
+       g_signal_connect (selector->source, "row-activated", G_CALLBACK (on_row_activate), selector);
+    }
+
+  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source));
+
+  if ( selector->source_select_id )
+    g_signal_handler_disconnect (selection, selector->source_select_id);
+
+  selector->source_select_id = 
+    g_signal_connect (selection, "changed", G_CALLBACK (on_source_select), selector);
+}
 
 
 static void
 psppire_selector_init (PsppireSelector *selector)
 {
+  selector->primary_requested = FALSE;
   selector->select_user_data = NULL;
   selector->select_items = NULL;
   selector->allow_selection = NULL;
@@ -326,6 +414,14 @@ psppire_selector_init (PsppireSelector *selector)
   selector->source = NULL;
   selector->dest = NULL;
   selector->dispose_has_run = FALSE;
+
+
+  selector->row_activate_id = 0;
+  selector->source_select_id  = 0;
+
+  g_signal_connect (selector, "realize",
+                   G_CALLBACK (on_realize), NULL);
+
 }
 
 
@@ -388,27 +484,6 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
     }
 }
 
-/* Callback for when the source selection changes */
-static void
-on_source_select (GtkTreeSelection *treeselection, gpointer data)
-{
-  PsppireSelector *selector = data;
-
-  set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
-
-  if ( selector->allow_selection )
-    {
-      gtk_action_set_sensitive (selector->action,
-                               selector->allow_selection (selector->source, selector->dest));
-    }
-  else if ( GTK_IS_ENTRY (selector->dest) )
-    {
-      gtk_action_set_sensitive (selector->action,
-                               gtk_tree_selection_count_selected_rows
-                               (treeselection) <= 1 );
-    }
-}
-
 /* Callback for when the destination treeview selection changes */
 static void
 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
@@ -555,18 +630,6 @@ select_selection (PsppireSelector *selector)
   selector->selecting = FALSE;
 }
 
-/* Callback for when the source treeview is activated (double clicked) */
-static void
-on_row_activate (GtkTreeView       *tree_view,
-                GtkTreePath       *path,
-                GtkTreeViewColumn *column,
-                gpointer           data)
-{
-  PsppireSelector *selector  = data;
-
-  gtk_action_activate (selector->action);
-}
-
 /* Callback for when the selector button is clicked,
    or other event which causes the selector's action to occur.
  */
@@ -587,25 +650,21 @@ on_activate (PsppireSelector *selector, gpointer data)
     }
 }
 
-/* Default visibility filter for GtkTreeView DEST widget */
 static gboolean
-is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
-                PsppireSelector *selector)
+is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter, PsppireSelector *selector)
 {
-  GtkTreeModel *dest_model;
-  GtkTreeIter dest_iter;
+  gboolean result = FALSE;
   GtkTreeIter source_iter;
-  gint index;
-  GtkTreePath *path ;
   GtkTreeModel *source_model;
+  GValue value = {0};
 
-  if ( GTK_IS_TREE_MODEL_FILTER (model) )
+  if (GTK_IS_TREE_MODEL_FILTER (model))
     {
       source_model = gtk_tree_model_filter_get_model
        (GTK_TREE_MODEL_FILTER (model));
 
       gtk_tree_model_filter_convert_iter_to_child_iter
-       ( GTK_TREE_MODEL_FILTER (model),  &source_iter,  iter  );
+       (GTK_TREE_MODEL_FILTER (model),  &source_iter, iter);
     }
   else
     {
@@ -613,40 +672,17 @@ is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
       source_iter = *iter;
     }
 
-  dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
-
-  path = gtk_tree_model_get_path (source_model, &source_iter);
-
-  index = *gtk_tree_path_get_indices (path);
+  gtk_tree_model_get_value (source_model, &source_iter, DICT_TVM_COL_VAR, &value);
 
-  gtk_tree_path_free (path);
+  result = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (selector->dest),
+                                                   &value);
 
-  if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
-    return FALSE;
+  g_value_unset (&value);
 
-  do
-    {
-      int x;
-      GValue value = {0};
-      GValue int_value = {0};
-      gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
-
-      g_value_init (&int_value, G_TYPE_INT);
-
-      g_value_transform (&value, &int_value);
-
-      x = g_value_get_int (&int_value);
-
-      g_value_unset (&int_value);
-      g_value_unset (&value);
+  return result;
+}
 
-      if ( x == index )
-       return TRUE;
-    }
-  while (gtk_tree_model_iter_next (dest_model, &dest_iter));
 
-  return FALSE;
-}
 
 /* Visibility function for items in the SOURCE widget.
    Returns TRUE iff *all* the selectors for which SOURCE is associated
@@ -681,18 +717,16 @@ static void
 set_tree_view_source (PsppireSelector *selector,
                      GtkTreeView *source)
 {
-  GtkTreeSelection* selection ;
   GList *list = NULL;
 
   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+  
+  GtkTreeModel *model = gtk_tree_view_get_model (source);
 
   if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
     {
       selector->filtered_source =
-       GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
-                              (gtk_tree_view_get_model (source),  NULL));
-
-      gtk_tree_view_set_model (source, NULL);
+       GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
 
       gtk_tree_view_set_model (source,
                               GTK_TREE_MODEL (selector->filtered_source));
@@ -710,20 +744,17 @@ set_tree_view_source (PsppireSelector *selector,
     {  /* Append this selector to the list and push the <source,list>
          pair onto the hash table */
 
-      selector->filtered_source = GTK_TREE_MODEL_FILTER (
-       gtk_tree_view_get_model (source));
+      selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
 
-      list = g_list_append (list, selector);
-      g_hash_table_replace (class->source_hash, source, list);
+      if ( NULL == g_list_find (list, selector) )
+       {
+         if ( selector->primary_requested )
+           list = g_list_prepend (list, selector);
+         else
+           list = g_list_append (list, selector);
+         g_hash_table_replace (class->source_hash, source, list);
+       }
     }
-
-  selection = gtk_tree_view_get_selection (source);
-
-  g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
-                   selector);
-
-  g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
-                   selector);
 }
 
 
@@ -758,7 +789,7 @@ on_dest_data_delete (GtkTreeModel *tree_model,
 
 
 static void
-xxx (PsppireSelector *selector)
+on_dest_model_changed (PsppireSelector *selector)
 {
   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
 
@@ -782,12 +813,12 @@ set_tree_view_dest (PsppireSelector *selector,
   g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
                    selector);
 
-
+  on_dest_model_changed (selector);
   g_signal_connect_swapped (dest, "notify::model",
-                           G_CALLBACK (xxx), selector);
-
+                           G_CALLBACK (on_dest_model_changed), selector);
 }
 
+
 /* Callback which causes the filter to be refiltered.
    Called when the DEST GtkEntry is activated (Enter is pressed), or when it
    looses focus.
@@ -799,6 +830,7 @@ refilter (PsppireSelector *selector)
   return FALSE;
 }
 
+
 /* Callback for when the DEST GtkEntry is selected (clicked) */
 static gboolean
 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
@@ -812,7 +844,6 @@ on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
 }
 
 
-
 /* Callback for when an item disappears from the source list.
    By implication, this means that the item has been inserted into the
    destination.
@@ -860,6 +891,16 @@ set_entry_dest (PsppireSelector *selector,
                    G_CALLBACK (on_row_inserted), selector);
 }
 
+static void
+set_default_filter (PsppireSelector *selector)
+{
+  if ( selector->filter == NULL)
+    {
+      if  (GTK_IS_TREE_VIEW (selector->dest))
+       selector->filter = is_item_in_dest;
+    }
+}
+
 static void
 update_subjects (PsppireSelector *selector)
 {
@@ -868,11 +909,7 @@ update_subjects (PsppireSelector *selector)
   if ( NULL == selector->dest )
     return;
 
-  if ( selector->filter == NULL)
-    {
-      if  (GTK_IS_TREE_VIEW (selector->dest))
-       selector->filter = is_item_in_dest;
-    }
+  set_default_filter (selector);
 
   if ( NULL == selector->source )
     return;
@@ -908,21 +945,44 @@ update_subjects (PsppireSelector *selector)
   else
     g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
 
+
+  /* FIXME: Remove this dependency */
+  if ( PSPPIRE_IS_DICT_VIEW (selector->source) )
+    {
+      GObjectClass *class = G_OBJECT_GET_CLASS (selector);
+      GType type = G_OBJECT_TYPE (selector->dest);
+
+      SelectItemsFunc *func  = 
+       g_hash_table_lookup (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type);
+
+      if ( func )
+       psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
+                                         func, NULL);
+    }
+}
+
+
+void
+psppire_selector_set_default_selection_func (GType type, SelectItemsFunc *func)
+{
+  GObjectClass *class = g_type_class_ref (PSPPIRE_SELECTOR_TYPE);
+
+  g_hash_table_insert (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type, func);
+
+  g_type_class_unref (class);
 }
 
 
+
+
 /* Set FILTER_FUNC for this selector */
 void
 psppire_selector_set_filter_func (PsppireSelector *selector,
                                  FilterItemsFunc *filter_func)
 {
   selector->filter = filter_func ;
-
-  if ( selector->filter == NULL)
-    {
-      if  (GTK_IS_TREE_VIEW (selector->dest))
-       selector->filter = is_item_in_dest;
-    }
+  
+  set_default_filter (selector);
 }
 
 
@@ -945,7 +1005,6 @@ psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow
 }
 
 
-
 GType
 psppire_selector_orientation_get_type (void)
 {