New module psppire-var-view
[pspp-builds.git] / src / ui / gui / psppire-selector.c
index 8e0be3a3590b37015877e4b7ebb452c006f6e900..367502b6ea219dca40b1aaeb07bb65a806996de4 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 <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);
@@ -224,6 +235,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 +266,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);
@@ -298,11 +322,76 @@ psppire_selector_base_finalize(PsppireSelectorClass *class,
   g_hash_table_destroy (class->source_hash);
 }
 
+/* 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 ( 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;
+  selector->filter = NULL;
+
   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
   selector->filtered_source = NULL;
 
@@ -321,6 +410,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);
+
 }
 
 
@@ -383,27 +480,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)
@@ -550,18 +626,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.
  */
@@ -582,6 +646,13 @@ on_activate (PsppireSelector *selector, gpointer data)
     }
 }
 
+static gboolean
+permissive_filter (GtkTreeModel *model, GtkTreeIter *iter,
+                PsppireSelector *selector)
+{
+  return FALSE;
+}
+
 /* Default visibility filter for GtkTreeView DEST widget */
 static gboolean
 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
@@ -676,18 +747,17 @@ 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));
@@ -705,20 +775,19 @@ 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);
 }
 
 
@@ -855,6 +924,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 = permissive_filter;
+    }
+}
+
 static void
 update_subjects (PsppireSelector *selector)
 {
@@ -863,12 +942,11 @@ update_subjects (PsppireSelector *selector)
   if ( NULL == selector->dest )
     return;
 
+  set_default_filter (selector);
+
   if ( NULL == selector->source )
     return;
 
-  g_signal_connect_swapped (selector->source, "notify::dictionary",
-                           G_CALLBACK (update_subjects), selector);
-
   g_signal_connect_swapped (selector->source, "notify::model",
                            G_CALLBACK (update_subjects), selector);
 
@@ -900,25 +978,39 @@ 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) )
+    {
+      if ( PSPPIRE_IS_VAR_VIEW (selector->dest))
+       psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
+                                         insert_source_row_into_tree_view,
+                                         NULL);
+      else if (GTK_IS_ENTRY (selector->dest))
+       psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
+                                         insert_source_row_into_entry,
+                                         NULL);
+    }
+
+}
+
+/* Set FILTER_FUNC for this selector */
+void
+psppire_selector_set_filter_func (PsppireSelector *selector,
+                                 FilterItemsFunc *filter_func)
+{
+  selector->filter = filter_func ;
+  
+  set_default_filter (selector);
 }
 
 
-/* Set SELECT_FUNC and FILTER_FUNC for this selector */
+/* Set SELECT_FUNC for this selector */
 void
-psppire_selector_set_subjects (PsppireSelector *selector,
+psppire_selector_set_select_func (PsppireSelector *selector,
                               SelectItemsFunc *select_func,
-                              FilterItemsFunc *filter_func,
                               gpointer user_data)
 {
-  selector->filter = filter_func ;
   selector->select_user_data = user_data;
-
-  if ( filter_func == NULL)
-    {
-      if  (GTK_IS_TREE_VIEW (selector->dest))
-       selector->filter = is_item_in_dest;
-    }
-
   selector->select_items = select_func;
 }
 
@@ -931,7 +1023,6 @@ psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow
 }
 
 
-
 GType
 psppire_selector_orientation_get_type (void)
 {