Means Dialog: Fix issue where the wrong syntax was generated.
[pspp] / src / ui / gui / psppire-selector.c
index 194caf1b80ae7546476d3e0dd75f3f18e418f8b5..17e173b5a59117d17ff7e7021aff66a0f7d62f93 100644 (file)
@@ -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,43 @@ psppire_selector_get_type (void)
 
 static GObjectClass * parent_class = NULL;
 
+
+
+#define SELECTOR_DEBUGGING 0
+
 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 +175,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);
 
@@ -317,7 +357,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);
@@ -416,6 +455,8 @@ psppire_selector_init (PsppireSelector *selector)
 
   selector->row_activate_id = 0;
   selector->source_select_id  = 0;
+
+  selector->source_litem = NULL;
 }
 
 
@@ -489,6 +530,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 +538,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 +624,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 +641,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 ();
 
@@ -735,8 +805,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>
@@ -745,9 +822,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);
        }
     }
@@ -828,19 +911,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 */
@@ -861,7 +956,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) */
@@ -938,10 +1044,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 */
@@ -1011,15 +1121,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;
 }