From a17d6290e404857a1f4bf021d8635a3a1eb9ee19 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 22 Mar 2015 11:46:16 -0400 Subject: [PATCH] search bar: Clean up weak pointer handling Break out a setter that manages the weak pointer, and use it in finalize. This also fixes a bug where we were forgetting to disconnect the right signal handler in some cases. --- gtk/gtksearchbar.c | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/gtk/gtksearchbar.c b/gtk/gtksearchbar.c index d1b3f3c73e..d098a486d7 100644 --- a/gtk/gtksearchbar.c +++ b/gtk/gtksearchbar.c @@ -350,12 +350,7 @@ gtk_search_bar_dispose (GObject *object) GtkSearchBar *bar = GTK_SEARCH_BAR (object); GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar); - if (priv->entry) - { - g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar); - g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry); - priv->entry = NULL; - } + gtk_search_bar_set_entry (bar, NULL); G_OBJECT_CLASS (gtk_search_bar_parent_class)->dispose (object); } @@ -471,6 +466,35 @@ gtk_search_bar_new (void) return g_object_new (GTK_TYPE_SEARCH_BAR, NULL); } +static void +gtk_search_bar_set_entry (GtkSearchBar *bar, + GtkEntry *entry) +{ + GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar); + + if (priv->entry != NULL) + { + if (GTK_IS_SEARCH_ENTRY (priv->entry)) + g_signal_handlers_disconnect_by_func (priv->entry, stop_search_cb, bar); + else + g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar); + g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry); + } + + priv->entry = GTK_WIDGET (entry); + + if (priv->entry != NULL) + { + g_object_add_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry); + if (GTK_IS_SEARCH_ENTRY (priv->entry)) + g_signal_connect (priv->entry, "stop-search", + G_CALLBACK (stop_search_cb), bar); + else + g_signal_connect (priv->entry, "key-press-event", + G_CALLBACK (entry_key_pressed_event_cb), bar); + } +} + /** * gtk_search_bar_connect_entry: * @bar: a #GtkSearchBar @@ -487,32 +511,10 @@ void gtk_search_bar_connect_entry (GtkSearchBar *bar, GtkEntry *entry) { - GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar); - g_return_if_fail (GTK_IS_SEARCH_BAR (bar)); g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry)); - if (priv->entry != NULL) - { - if (GTK_IS_SEARCH_ENTRY (priv->entry)) - g_signal_handlers_disconnect_by_func (priv->entry, stop_search_cb, bar); - else - g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar); - g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry); - priv->entry = NULL; - } - - if (entry != NULL) - { - priv->entry = GTK_WIDGET (entry); - g_object_add_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry); - if (GTK_IS_SEARCH_ENTRY (priv->entry)) - g_signal_connect (priv->entry, "stop-search", - G_CALLBACK (stop_search_cb), bar); - else - g_signal_connect (priv->entry, "key-press-event", - G_CALLBACK (entry_key_pressed_event_cb), bar); - } + gtk_search_bar_set_entry (bar, entry); } /**