Merge commit 'origin/stable'
[pspp-builds.git] / src / ui / gui / psppire-selector.c
index a041fd5a05cae1ef23356a206f563f2a4961fc6e..d6ea3375257186d0566aa8a7706da717030db17c 100644 (file)
@@ -1,10 +1,9 @@
-/*
-   PSPPIRE --- A Graphical User Interface for PSPP
+/* PSPPIRE - a graphical user interface for PSPP.
    Copyright (C) 2007  Free Software Foundation
 
    Copyright (C) 2007  Free Software Foundation
 
-   This program is free software; you can redistribute it and/or modify
+   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
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -13,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    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, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 /*
   This module provides a widget, PsppireSelector derived from
 
 /*
   This module provides a widget, PsppireSelector derived from
@@ -129,6 +126,9 @@ enum
 };
 
 
 };
 
 
+static void on_activate (PsppireSelector *selector, gpointer data);
+
+
 static void
 psppire_selector_set_property (GObject         *object,
                               guint            prop_id,
 static void
 psppire_selector_set_property (GObject         *object,
                               guint            prop_id,
@@ -179,7 +179,7 @@ psppire_selector_class_init (PsppireSelectorClass *class)
     g_param_spec_enum ("orientation",
                       "Orientation",
                       "Where the selector is relative to its subjects",
     g_param_spec_enum ("orientation",
                       "Orientation",
                       "Where the selector is relative to its subjects",
-                      G_TYPE_PSPPIRE_SELECTOR_ORIENTATION,
+                      PSPPIRE_TYPE_SELECTOR_ORIENTATION,
                       PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
                       G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
 
                       PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
                       G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
 
@@ -233,16 +233,23 @@ psppire_selector_base_finalize(PsppireSelectorClass *class,
 }
 
 
 }
 
 
+
 static void
 psppire_selector_init (PsppireSelector *selector)
 {
   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
   selector->filtered_source = NULL;
 
 static void
 psppire_selector_init (PsppireSelector *selector)
 {
   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
   selector->filtered_source = NULL;
 
+  selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
+
+  gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
+
   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
 
   gtk_widget_show (selector->arrow);
 
   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
 
   gtk_widget_show (selector->arrow);
 
+  g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
+
   selector->selecting = FALSE;
 }
 
   selector->selecting = FALSE;
 }
 
@@ -314,9 +321,14 @@ on_source_select (GtkTreeSelection *treeselection, gpointer data)
 
   set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
 
 
   set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
 
-  if ( GTK_IS_ENTRY (selector->dest) )
+  if ( selector->allow_selection )
     {
     {
-      gtk_widget_set_sensitive (GTK_WIDGET (selector),
+      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 );
     }
                                gtk_tree_selection_count_selected_rows
                                (treeselection) <= 1 );
     }
@@ -328,6 +340,8 @@ on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
 {
   PsppireSelector *selector = data;
 
 {
   PsppireSelector *selector = data;
 
+  gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
+
   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
 }
 
   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
 }
 
@@ -451,7 +465,9 @@ select_selection (PsppireSelector *selector)
 
       selector->select_items (child_iter,
                              selector->dest,
 
       selector->select_items (child_iter,
                              selector->dest,
-                             childmodel);
+                             childmodel,
+                             selector->select_user_data
+                             );
     }
 
   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
     }
 
   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
@@ -473,12 +489,14 @@ on_row_activate (GtkTreeView       *tree_view,
 {
   PsppireSelector *selector  = data;
 
 {
   PsppireSelector *selector  = data;
 
-  select_selection (selector);
+  gtk_action_activate (selector->action);
 }
 
 }
 
-/* Callback for when the selector button is clicked */
+/* Callback for when the selector button is clicked,
+   or other event which causes the selector's action to occur.
+ */
 static void
 static void
-on_click (PsppireSelector *selector, gpointer data)
+on_activate (PsppireSelector *selector, gpointer data)
 {
   switch (selector->direction)
     {
 {
   switch (selector->direction)
     {
@@ -533,10 +551,21 @@ is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
 
   do
     {
 
   do
     {
+      int x;
       GValue value = {0};
       GValue value = {0};
+      GValue int_value = {0};
       gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
 
       gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
 
-      if ( g_value_get_int (&value) == index)
+      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);
+
+      if ( x == index )
        return TRUE;
     }
   while (gtk_tree_model_iter_next (dest_model, &dest_iter));
        return TRUE;
     }
   while (gtk_tree_model_iter_next (dest_model, &dest_iter));
@@ -676,13 +705,15 @@ set_tree_view_dest (PsppireSelector *selector,
 
 }
 
 
 }
 
-/* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
-static void
-on_entry_activate (GtkEntry *w, gpointer data)
+/* Callback which causes the filter to be refiltered.
+   Called when the DEST GtkEntry is activated (Enter is pressed), or when it
+   looses focus.
+*/
+static gboolean
+refilter (PsppireSelector *selector)
 {
 {
-  PsppireSelector * selector = data;
-
   gtk_tree_model_filter_refilter (selector->filtered_source);
   gtk_tree_model_filter_refilter (selector->filtered_source);
+  return FALSE;
 }
 
 /* Callback for when the DEST GtkEntry is selected (clicked) */
 }
 
 /* Callback for when the DEST GtkEntry is selected (clicked) */
@@ -691,21 +722,59 @@ on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
 {
   PsppireSelector * selector = data;
 
 {
   PsppireSelector * selector = data;
 
+  gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
 
   return FALSE;
 }
 
   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
 
   return FALSE;
 }
 
+
+
+/* Callback for when an item disappears from the source list.
+   By implication, this means that the item has been inserted into the
+   destination.
+ */
+static void
+on_row_deleted (PsppireSelector *selector)
+{
+  g_signal_emit (selector, signals [SELECTED], 0);
+}
+
+/* Callback for when a new item appears in the source list.
+   By implication, this means that an item has been deleted from the
+   destination.
+ */
+static void
+on_row_inserted (PsppireSelector *selector)
+{
+  g_signal_emit (selector, signals [DE_SELECTED], 0);
+}
+
+
+
 /* Set DEST to be the destination GtkEntry widget */
 static void
 set_entry_dest (PsppireSelector *selector,
                GtkEntry *dest)
 {
 /* Set DEST to be the destination GtkEntry widget */
 static void
 set_entry_dest (PsppireSelector *selector,
                GtkEntry *dest)
 {
-  g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
+  g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
+                   selector);
+
+  g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
                    selector);
 
   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
                    selector);
                    selector);
 
   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
                    selector);
+
+  g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
+                   selector);
+
+
+  g_signal_connect_swapped (selector->filtered_source, "row-deleted",
+                   G_CALLBACK (on_row_deleted), selector);
+
+  g_signal_connect_swapped (selector->filtered_source, "row-inserted",
+                   G_CALLBACK (on_row_inserted), selector);
 }
 
 
 }
 
 
@@ -716,7 +785,8 @@ psppire_selector_set_subjects (PsppireSelector *selector,
                               GtkWidget *source,
                               GtkWidget *dest,
                               SelectItemsFunc *select_func,
                               GtkWidget *source,
                               GtkWidget *dest,
                               SelectItemsFunc *select_func,
-                              FilterItemsFunc *filter_func )
+                              FilterItemsFunc *filter_func,
+                              gpointer user_data)
 {
   g_assert(selector);
 
 {
   g_assert(selector);
 
@@ -724,6 +794,7 @@ psppire_selector_set_subjects (PsppireSelector *selector,
 
   selector->source = source;
   selector->dest = dest;
 
   selector->source = source;
   selector->dest = dest;
+  selector->select_user_data = user_data;
 
   if ( filter_func == NULL)
     {
 
   if ( filter_func == NULL)
     {
@@ -731,8 +802,6 @@ psppire_selector_set_subjects (PsppireSelector *selector,
        selector->filter = is_item_in_dest;
     }
 
        selector->filter = is_item_in_dest;
     }
 
-  g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
-
   if ( GTK_IS_TREE_VIEW (source))
     set_tree_view_source (selector, GTK_TREE_VIEW (source) );
   else
   if ( GTK_IS_TREE_VIEW (source))
     set_tree_view_source (selector, GTK_TREE_VIEW (source) );
   else
@@ -740,7 +809,9 @@ psppire_selector_set_subjects (PsppireSelector *selector,
 
   g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
 
 
   g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
 
-  if  ( GTK_IS_TREE_VIEW (dest))
+  if ( NULL == dest)
+    ;
+  else if  ( GTK_IS_TREE_VIEW (dest))
     set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
 
   else if ( GTK_IS_ENTRY (dest))
     set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
 
   else if ( GTK_IS_ENTRY (dest))
@@ -759,6 +830,14 @@ psppire_selector_set_subjects (PsppireSelector *selector,
 
 
 
 
 
 
+void
+psppire_selector_set_allow        (PsppireSelector *selector , AllowSelectionFunc *allow)
+{
+  selector->allow_selection = allow;
+}
+
+
+
 GType
 psppire_selector_orientation_get_type (void)
 {
 GType
 psppire_selector_orientation_get_type (void)
 {