gui: Properly manage ref counts of combo box and tree view models.
[pspp] / src / ui / gui / psppire-selector.c
index 83fe2d82792a04fb34425b3f0f5a221dde2238fb..08f7f0215d8fb505d1a4c8889949e22f3589f0b1 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007, 2009 Free Software Foundation
+   Copyright (C) 2007, 2009, 2010, 2012 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
 #include "psppire-dict.h"
 #include "psppire-select-dest.h"
 
-#include <gtk/gtksignal.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkentry.h>
+#include <gtk/gtk.h>
 
 #include "psppire-selector.h"
 
-#include <gtk/gtktreeview.h>
-#include <gtk/gtktreeselection.h>
-#include <gtk/gtktextview.h>
-#include <gtk/gtkwidget.h>
-
 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
 static void psppire_selector_base_init     (PsppireSelectorClass *class);
 static void psppire_selector_class_init    (PsppireSelectorClass *class);
@@ -89,6 +82,26 @@ enum  {SELECTED,    /* Emitted when an item is inserted into dest */
 
 static guint signals [n_SIGNALS];
 
+/* 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);
+}
+
 
 GType
 psppire_selector_get_type (void)
@@ -158,7 +171,9 @@ enum
 };
 
 
-static void on_activate (PsppireSelector *selector, gpointer data);
+static void on_click (GtkButton *b);
+static void on_realize (GtkWidget *selector);
+
 
 static void update_subjects (PsppireSelector *selector);
 
@@ -225,6 +240,8 @@ static void
 psppire_selector_class_init (PsppireSelectorClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
   GParamSpec *orientation_spec =
     g_param_spec_enum ("orientation",
                       "Orientation",
@@ -257,6 +274,10 @@ psppire_selector_class_init (PsppireSelectorClass *class)
                         G_PARAM_READWRITE);
 
 
+  button_class->clicked = on_click;
+  widget_class->realize = on_realize;
+
+
   object_class->set_property = psppire_selector_set_property;
   object_class->get_property = psppire_selector_get_property;
 
@@ -330,9 +351,7 @@ on_row_activate (GtkTreeView       *tree_view,
                 GtkTreeViewColumn *column,
                 gpointer           data)
 {
-  PsppireSelector *selector  = data;
-
-  gtk_action_activate (selector->action);
+  on_click (GTK_BUTTON (data));
 }
 
 /* Callback for when the source selection changes */
@@ -345,12 +364,12 @@ on_source_select (GtkTreeSelection *treeselection, gpointer data)
 
   if ( selector->allow_selection )
     {
-      gtk_action_set_sensitive (selector->action,
+      gtk_widget_set_sensitive (GTK_WIDGET (selector),
                                selector->allow_selection (selector->source, selector->dest));
     }
   else if ( GTK_IS_ENTRY (selector->dest) )
     {
-      gtk_action_set_sensitive (selector->action,
+      gtk_widget_set_sensitive (GTK_WIDGET (selector),
                                gtk_tree_selection_count_selected_rows
                                (treeselection) <= 1 );
     }
@@ -358,13 +377,17 @@ on_source_select (GtkTreeSelection *treeselection, gpointer data)
 
 
 static void
-on_realize (PsppireSelector *selector)
+on_realize (GtkWidget *w)
 {
+  PsppireSelector *selector = PSPPIRE_SELECTOR (w);
   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
   GtkTreeSelection* selection ;
 
   GList *list = g_hash_table_lookup (class->source_hash, selector->source);
 
+  if (GTK_WIDGET_CLASS (parent_class)->realize)
+    GTK_WIDGET_CLASS (parent_class)->realize (w);
+
   if ( NULL == list)
     return;
 
@@ -397,18 +420,12 @@ psppire_selector_init (PsppireSelector *selector)
   selector->filter = NULL;
 
   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);
 
-  g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
-
   selector->selecting = FALSE;
 
   selector->source = NULL;
@@ -418,10 +435,6 @@ psppire_selector_init (PsppireSelector *selector)
 
   selector->row_activate_id = 0;
   selector->source_select_id  = 0;
-
-  g_signal_connect (selector, "realize",
-                   G_CALLBACK (on_realize), NULL);
-
 }
 
 
@@ -556,6 +569,18 @@ de_select_selection_tree_view (PsppireSelector *selector)
 }
 
 
+/* 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)
+{
+  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
+  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
+  return FALSE;
+}
+
 /* Removes something from the DEST widget */
 static void
 de_select_selection (PsppireSelector *selector)
@@ -573,7 +598,7 @@ de_select_selection (PsppireSelector *selector)
 
   selector->selecting = FALSE;
 
-  gtk_tree_model_filter_refilter (selector->filtered_source);
+  refilter (selector);
 
   g_signal_emit (selector, signals [DE_SELECTED], 0);
 }
@@ -590,11 +615,15 @@ select_selection (PsppireSelector *selector)
   GList *selected_rows =
     gtk_tree_selection_get_selected_rows (selection, NULL);
 
-  GtkTreeModel *childmodel  = gtk_tree_model_filter_get_model
-    (selector->filtered_source);
+  GtkTreeModel *childmodel  =
+    gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))));
 
   g_return_if_fail (selector->select_items);
 
+  if (selector->allow_selection && 
+      ! selector->allow_selection (selector->source, selector->dest))
+    return;
+
   selector->selecting = TRUE;
 
   for (item = g_list_first (selected_rows);
@@ -605,11 +634,11 @@ select_selection (PsppireSelector *selector)
       GtkTreeIter iter;
       GtkTreePath *path  = item->data;
 
-      gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
+      gtk_tree_model_get_iter (GTK_TREE_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))),
                               &iter, path);
 
       gtk_tree_model_filter_convert_iter_to_child_iter
-       (selector->filtered_source,
+       (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))),
         &child_iter,
         &iter);
 
@@ -623,7 +652,7 @@ select_selection (PsppireSelector *selector)
   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
   g_list_free (selected_rows);
 
-  gtk_tree_model_filter_refilter (selector->filtered_source);
+  refilter (selector);
 
   g_signal_emit (selector, signals [SELECTED], 0);
 
@@ -634,8 +663,10 @@ select_selection (PsppireSelector *selector)
    or other event which causes the selector's action to occur.
  */
 static void
-on_activate (PsppireSelector *selector, gpointer data)
+on_click (GtkButton *b)
 {
+  PsppireSelector *selector = PSPPIRE_SELECTOR (b);
+
   switch (selector->direction)
     {
     case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
@@ -648,6 +679,9 @@ on_activate (PsppireSelector *selector, gpointer data)
       g_assert_not_reached ();
       break;
     }
+
+  if (GTK_BUTTON_CLASS (parent_class)->clicked)
+    GTK_BUTTON_CLASS (parent_class)->clicked (b);
 }
 
 static gboolean
@@ -714,50 +748,77 @@ is_source_item_visible (GtkTreeModel *childmodel,
 
 /* set the source widget to SOURCE */
 static void
-set_tree_view_source (PsppireSelector *selector,
-                     GtkTreeView *source)
+set_tree_view_source (PsppireSelector *selector)
 {
   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)))
+  if ( ! (list = g_hash_table_lookup (class->source_hash, selector->source)))
     {
-      selector->filtered_source =
-       GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
-
-      gtk_tree_view_set_model (source,
-                              GTK_TREE_MODEL (selector->filtered_source));
-
       list = g_list_append (list, selector);
-      g_hash_table_insert (class->source_hash, source, list);
-
-
-      gtk_tree_model_filter_set_visible_func (selector->filtered_source,
-                                             is_source_item_visible,
-                                             selector,
-                                             NULL);
+      g_hash_table_insert (class->source_hash, selector->source, list);
     }
   else
     {  /* Append this selector to the list and push the <source,list>
          pair onto the hash table */
 
-      selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
-
       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);
+         g_hash_table_replace (class->source_hash, selector->source, list);
        }
     }
 }
 
 
+
+/* This function is a callback which occurs when the
+   SOURCE's model has changed */
+static void
+update_model (
+              GtkTreeView *source,
+              GParamSpec *psp,
+              PsppireSelector *selector
+              )
+{
+  GtkTreeModel *model = gtk_tree_view_get_model (source);
+
+  g_assert (source == GTK_TREE_VIEW (selector->source));
+
+  if (model && (model == g_object_get_data (G_OBJECT (source), "model-copy")))
+    return;
+
+  if (model != NULL) 
+    {      
+      GtkTreeModel *new_model = gtk_tree_model_filter_new (model, NULL); 
+
+      g_object_set_data (G_OBJECT (source), "model-copy", new_model);  
+
+      gtk_tree_view_set_model (source, new_model);
+  
+      gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (new_model),
+                                              is_source_item_visible,
+                                              selector,
+                                              NULL);
+
+      g_signal_connect_swapped (new_model,
+                               "row-deleted",
+                               G_CALLBACK (on_row_deleted), selector);
+
+      g_signal_connect_swapped (new_model,
+                               "row-inserted",
+                               G_CALLBACK (on_row_inserted), selector);
+
+      g_object_unref (new_model);
+    }
+}
+
+
+
 /*
    Callback for when the destination treeview's data changes
  */
@@ -771,7 +832,7 @@ on_dest_data_change (GtkTreeModel *tree_model,
 
   if ( selector->selecting) return;
 
-  gtk_tree_model_filter_refilter (selector->filtered_source);
+  refilter (selector);
 }
 
 
@@ -784,7 +845,7 @@ on_dest_data_delete (GtkTreeModel *tree_model,
 
   if ( selector->selecting ) return;
 
-  gtk_tree_model_filter_refilter (selector->filtered_source);
+  refilter (selector);
 }
 
 
@@ -819,16 +880,6 @@ set_tree_view_dest (PsppireSelector *selector,
 }
 
 
-/* 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)
-{
-  gtk_tree_model_filter_refilter (selector->filtered_source);
-  return FALSE;
-}
 
 
 /* Callback for when the DEST GtkEntry is selected (clicked) */
@@ -844,27 +895,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.
- */
-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
@@ -884,11 +914,6 @@ set_entry_dest (PsppireSelector *selector,
                    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);
 }
 
 static void
@@ -901,11 +926,10 @@ set_default_filter (PsppireSelector *selector)
     }
 }
 
+
 static void
 update_subjects (PsppireSelector *selector)
 {
-  GtkTreeModel *model = NULL;
-
   if ( NULL == selector->dest )
     return;
 
@@ -914,17 +938,15 @@ update_subjects (PsppireSelector *selector)
   if ( NULL == selector->source )
     return;
 
-  g_signal_connect_swapped (selector->source, "notify::model",
-                           G_CALLBACK (update_subjects), selector);
-
-  model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
-
-  if ( NULL == model)
-    return;
+  if ( GTK_IS_TREE_VIEW (selector->source))
+    {
+      set_tree_view_source (selector);
 
+      g_signal_connect (selector->source, "notify::model", 
+                              G_CALLBACK (update_model), selector); 
 
-  if ( GTK_IS_TREE_VIEW (selector->source))
-    set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
+      update_model (GTK_TREE_VIEW (selector->source), 0, selector);
+    }
   else
     g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
 
@@ -981,8 +1003,6 @@ psppire_selector_set_filter_func (PsppireSelector *selector,
                                  FilterItemsFunc *filter_func)
 {
   selector->filter = filter_func ;
-  
-  set_default_filter (selector);
 }