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.
This commit is contained in:
Matthias Clasen 2015-03-22 11:46:16 -04:00
parent b55aa154e6
commit a17d6290e4

View File

@ -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);
}
/**