psppire-selector.c: Remove unused preprocessor macro
[pspp] / src / ui / gui / psppire-selector.c
index ef6e983cfe5189528b7c43eab104d83e8bc95325..027f31203b64d5ce7559e643c08b1b699fd4fd2b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007, 2009, 2010, 2012 Free Software Foundation
+   Copyright (C) 2007, 2009, 2010, 2012, 2015 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
@@ -19,7 +19,7 @@
   This module provides a widget, PsppireSelector derived from
   GtkButton.
 
-  It contains a GtkArrow, and is used for selecting objects from a
+  It contains a GtkImage (to indicate the arrow), and is used for selecting objects from a
   GtkTreeView and putting them into a destination widget (often
   another GtkTreeView).  Typically this is used in psppire for
   selecting variables, thus:
@@ -59,9 +59,9 @@
 #include <config.h>
 
 #include "psppire-dictview.h"
-#include "psppire-var-view.h"
 #include "psppire-dict.h"
 #include "psppire-select-dest.h"
+#include "psppire-means-layer.h"
 
 #include <gtk/gtk.h>
 
@@ -131,18 +131,41 @@ psppire_selector_get_type (void)
 
 static GObjectClass * parent_class = NULL;
 
+
+
 static void
-psppire_selector_finalize (GObject *obj)
+dump_hash_entry (gpointer key, gpointer value, gpointer obj)
+{
+  GList *item = NULL;
+  g_print ("Source %p; ", key);
+
+  for (item = g_list_first (value);
+       item != NULL;
+       item = g_list_next (item))
+    {
+      g_print ("%p(%p) ", item->data, item);
+    }
+  g_print ("\n");
+}
+
+/* This function is for debugging only */
+void 
+psppire_selector_show_map (PsppireSelector *obj)
 {
-   /* Chain up to the parent class */
-   G_OBJECT_CLASS (parent_class)->finalize (obj);
+  PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+
+  g_print ("%s %p\n", __FUNCTION__, obj);
+  g_hash_table_foreach (class->source_hash, dump_hash_entry, obj);
 }
 
 
+
 static void
 psppire_selector_dispose (GObject *obj)
 {
   PsppireSelector *sel = PSPPIRE_SELECTOR (obj);
+  PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+  GList *list;
 
   if (sel->dispose_has_run)
     return;
@@ -150,6 +173,21 @@ psppire_selector_dispose (GObject *obj)
   /* Make sure dispose does not run twice. */
   sel->dispose_has_run = TRUE;
 
+  /* Remove ourself from the source map. If we are the only entry for this source
+     widget, that will result in an empty list for the source.  In that case, we
+     remove the entire entry too. */
+  if ((list = g_hash_table_lookup (class->source_hash, sel->source)))
+    {
+      GList *newlist = g_list_remove_link (list, sel->source_litem);
+      g_list_free (sel->source_litem);
+      if (newlist == NULL)
+       g_hash_table_remove (class->source_hash, sel->source);
+      else
+       g_hash_table_replace (class->source_hash, sel->source, newlist);
+
+      sel->source_litem = NULL;
+    }
+  
   g_object_unref (sel->dest);
   g_object_unref (sel->source);
 
@@ -173,9 +211,6 @@ static void on_click (GtkButton *b);
 static void on_realize (GtkWidget *selector);
 
 
-static void update_subjects (PsppireSelector *selector);
-
-
 static void
 psppire_selector_set_property (GObject         *object,
                               guint            prop_id,
@@ -192,15 +227,15 @@ psppire_selector_set_property (GObject         *object,
       break;
     case PROP_PRIMARY:
       selector->primary_requested = TRUE;
-      update_subjects (selector);
+      psppire_selector_update_subjects (selector);
       break;
     case PROP_SOURCE_WIDGET:
       selector->source = g_value_dup_object (value);
-      update_subjects (selector);
+      psppire_selector_update_subjects (selector);
       break;
     case PROP_DEST_WIDGET:
       selector->dest = g_value_dup_object (value);
-      update_subjects (selector);
+      psppire_selector_update_subjects (selector);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -317,7 +352,6 @@ psppire_selector_class_init (PsppireSelectorClass *class)
                  G_TYPE_NONE,
                  0);
 
-  object_class->finalize = psppire_selector_finalize;
   object_class->dispose = psppire_selector_dispose;
 
   class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
@@ -400,8 +434,7 @@ psppire_selector_init (PsppireSelector *selector)
   selector->allow_selection = NULL;
   selector->filter = NULL;
 
-  selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
-
+  selector->arrow = gtk_image_new_from_icon_name ("pan-start-symbolic", GTK_ICON_SIZE_BUTTON);
 
   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
 
@@ -416,6 +449,8 @@ psppire_selector_init (PsppireSelector *selector)
 
   selector->row_activate_id = 0;
   selector->source_select_id  = 0;
+
+  selector->source_litem = NULL;
 }
 
 
@@ -438,16 +473,16 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
       switch (selector->orientation)
        {
        case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-end-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-start-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-down-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-up-symbolic", NULL);
          break;
        default:
          g_assert_not_reached ();
@@ -459,22 +494,21 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
       switch (selector->orientation)
        {
        case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-start-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-end-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-up-symbolic", NULL);
          break;
        case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
-         g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
+         g_object_set (selector->arrow, "icon-name", "pan-down-symbolic", NULL);
          break;
        default:
          g_assert_not_reached ();
          break;
        };
-
     }
 }
 
@@ -489,6 +523,7 @@ on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
 }
 
+
 /* Callback for source deselection, when the dest is GtkEntry */
 static void
 de_select_selection_entry (PsppireSelector *selector)
@@ -496,23 +531,47 @@ de_select_selection_entry (PsppireSelector *selector)
   gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
 }
 
+
+static void  de_select_tree_model (GtkTreeSelection *selection, GtkTreeModel *model);
+
+/* Callback for source deselection, when the dest is PsppireMeansLayer */
+static void
+de_select_selection_means_layer (PsppireSelector *selector)
+{
+  PsppireMeansLayer *ml = PSPPIRE_MEANS_LAYER (selector->dest);
+  GtkTreeView *tv = GTK_TREE_VIEW (ml->var_view);
+  GtkTreeSelection *selection = gtk_tree_view_get_selection (tv);
+
+  GtkTreeModel *model = psppire_means_layer_get_model (ml);
+
+  g_return_if_fail (selector->select_items);
+
+  de_select_tree_model (selection, model);
+}
+
 /* Callback for source deselection, when the dest is GtkTreeView */
 static void
 de_select_selection_tree_view (PsppireSelector *selector)
 {
-  GList *item;
-
   GtkTreeSelection* selection =
     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
 
   GtkTreeModel *model =
     gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
 
+  g_return_if_fail (selector->select_items);
+
+  de_select_tree_model (selection, model);
+}
+
+static void 
+de_select_tree_model (GtkTreeSelection *selection, GtkTreeModel *model)
+{
+  GList *item;
+
   GList *selected_rows =
     gtk_tree_selection_get_selected_rows (selection, NULL);
 
-  g_return_if_fail (selector->select_items);
-
   /* Convert paths to RowRefs */
   for (item = g_list_first (selected_rows);
        item != NULL;
@@ -558,7 +617,8 @@ static gboolean
 refilter (PsppireSelector *selector)
 {
   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
-  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
+  if (GTK_IS_TREE_MODEL_FILTER (model))
+    gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
   return FALSE;
 }
 
@@ -574,6 +634,9 @@ de_select_selection (PsppireSelector *selector)
   else if ( GTK_IS_ENTRY (selector->dest))
     de_select_selection_entry (selector);
 
+  else if ( PSPPIRE_IS_MEANS_LAYER (selector->dest))
+    de_select_selection_means_layer (selector);
+
   else
     g_assert_not_reached ();
 
@@ -593,11 +656,11 @@ select_selection (PsppireSelector *selector)
   GtkTreeSelection* selection =
     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
 
-  GList *selected_rows =
-    gtk_tree_selection_get_selected_rows (selection, NULL);
+  GList *selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL);
 
-  GtkTreeModel *childmodel  =
-    gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))));
+  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
+
+  GtkTreeModel *childmodel = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
 
   g_return_if_fail (selector->select_items);
 
@@ -615,14 +678,12 @@ select_selection (PsppireSelector *selector)
       GtkTreeIter iter;
       GtkTreePath *path  = item->data;
 
-      gtk_tree_model_get_iter (GTK_TREE_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))),
-                              &iter, path);
+      g_return_if_fail (model);
 
-      gtk_tree_model_filter_convert_iter_to_child_iter
-       (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))),
-        &child_iter,
-        &iter);
+      gtk_tree_model_get_iter (model, &iter, path);
 
+      gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
+                                                       &child_iter, &iter);
       selector->select_items (child_iter,
                              selector->dest,
                              childmodel,
@@ -737,8 +798,15 @@ set_tree_view_source (PsppireSelector *selector)
   
   if ( ! (list = g_hash_table_lookup (class->source_hash, selector->source)))
     {
+      /* Base case:  This widget is currently not the source of 
+        any selector.  Create a hash entry and make this selector
+        the first selector in the list */
+
       list = g_list_append (list, selector);
       g_hash_table_insert (class->source_hash, selector->source, list);
+
+      /* Save the list item so that it can be removed later */
+      selector->source_litem = list;
     }
   else
     {  /* Append this selector to the list and push the <source,list>
@@ -747,9 +815,15 @@ set_tree_view_source (PsppireSelector *selector)
       if ( NULL == g_list_find (list, selector) )
        {
          if ( selector->primary_requested )
-           list = g_list_prepend (list, selector);
+           {
+             list = g_list_prepend (list, selector);
+             selector->source_litem = list;
+           }
          else
-           list = g_list_append (list, selector);
+           {
+             list = g_list_append (list, selector);
+             selector->source_litem = g_list_last (list);
+           }
          g_hash_table_replace (class->source_hash, selector->source, list);
        }
     }
@@ -830,19 +904,31 @@ on_dest_data_delete (GtkTreeModel *tree_model,
 }
 
 
+static void
+remove_selector_handlers (PsppireSelector *selector, GObject *sel)
+{
+  g_signal_handlers_disconnect_by_data (sel, selector);
+}
+
 static void
 on_dest_model_changed (PsppireSelector *selector)
 {
   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
 
-  if ( model ) 
-    {
-      g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
-                       selector);
-      
-      g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
-                       selector);
-    }
+  if (model == NULL) 
+    return;
+
+  g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
+                   selector);
+  
+  g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
+                   selector);
+
+  g_signal_connect (selector, "destroy", G_CALLBACK (remove_selector_handlers), model);
+  
+  if ( selector->selecting ) return;
+  
+  refilter (selector);
 }
 
 /* Set the destination widget to DEST */
@@ -863,7 +949,18 @@ set_tree_view_dest (PsppireSelector *selector,
                            G_CALLBACK (on_dest_model_changed), selector);
 }
 
+static void
+set_layer_dest (PsppireSelector *selector,
+                   PsppireMeansLayer *dest)
+{
+  GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dest->var_view));
 
+  gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
+
+
+  g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
+                   selector);
+}
 
 
 /* Callback for when the DEST GtkEntry is selected (clicked) */
@@ -911,8 +1008,8 @@ set_default_filter (PsppireSelector *selector)
 }
 
 
-static void
-update_subjects (PsppireSelector *selector)
+void
+psppire_selector_update_subjects (PsppireSelector *selector)
 {
   if ( NULL == selector->dest )
     return;
@@ -940,10 +1037,14 @@ update_subjects (PsppireSelector *selector)
     {
       set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
     }
-
   else if ( GTK_IS_ENTRY (selector->dest))
-    set_entry_dest (selector, GTK_ENTRY (selector->dest));
-
+    {
+      set_entry_dest (selector, GTK_ENTRY (selector->dest));
+    }
+  else if (PSPPIRE_IS_MEANS_LAYER (selector->dest))
+    {
+      set_layer_dest (selector, PSPPIRE_MEANS_LAYER (selector->dest));
+    }
   else if (GTK_IS_TEXT_VIEW (selector->dest))
     {
       /* Nothing to be done */
@@ -1013,15 +1114,17 @@ GType
 psppire_selector_orientation_get_type (void)
 {
   static GType etype = 0;
-  if (etype == 0) {
-    static const GEnumValue values[] = {
-      { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
-      { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
-      { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
-      { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
-      { 0, NULL, NULL }
-    };
-    etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);
-  }
+  if (etype == 0)
+    {
+      static const GEnumValue values[] =
+       {
+         { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
+         { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
+         { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
+         { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
+         { 0, NULL, NULL }
+       };
+      etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);
+    }
   return etype;
 }