From bbbfa0d677654b032c5c590fa415008ededcb710 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sun, 19 Feb 2012 21:53:58 +0100 Subject: [PATCH] Consider focus when toggling clear button Fixes: https://bugs.launchpad.net/midori/+bug/934083 --- katze/gtk3-compat.c | 15 +++++++++++---- katze/gtk3-compat.h | 4 +++- midori/sokoke.c | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/katze/gtk3-compat.c b/katze/gtk3-compat.c index 0c0a4369..8be80914 100644 --- a/katze/gtk3-compat.c +++ b/katze/gtk3-compat.c @@ -27,9 +27,9 @@ sokoke_on_entry_focus_in_event (GtkEntry* entry, g_object_get_data (G_OBJECT (entry), "sokoke_has_default")); if (has_default) { - gtk_entry_set_text (entry, ""); g_object_set_data (G_OBJECT (entry), "sokoke_has_default", GINT_TO_POINTER (0)); + gtk_entry_set_text (entry, ""); sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), PANGO_STYLE_NORMAL); } @@ -46,9 +46,9 @@ sokoke_on_entry_focus_out_event (GtkEntry* entry, { const gchar* default_text = (const gchar*)g_object_get_data ( G_OBJECT (entry), "sokoke_default_text"); - gtk_entry_set_text (entry, default_text); g_object_set_data (G_OBJECT (entry), "sokoke_has_default", GINT_TO_POINTER (1)); + gtk_entry_set_text (entry, default_text); sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), PANGO_STYLE_ITALIC); } @@ -73,6 +73,9 @@ gtk_entry_set_placeholder_text (GtkEntry* entry, /* Note: The default text initially overwrites any previous text */ gchar* old_value = g_object_get_data (G_OBJECT (entry), "sokoke_default_text"); + g_object_set_data (G_OBJECT (entry), "sokoke_default_text", + (gpointer)default_text); + if (!old_value) { g_object_set_data (G_OBJECT (entry), "sokoke_has_default", @@ -98,8 +101,12 @@ gtk_entry_set_placeholder_text (GtkEntry* entry, PANGO_STYLE_ITALIC); } } - g_object_set_data (G_OBJECT (entry), "sokoke_default_text", - (gpointer)default_text); +} + +const gchar* +gtk_entry_get_placeholder_text (GtkEntry* entry) +{ + return g_object_get_data (G_OBJECT (entry), "sokoke_default_text"); } #endif diff --git a/katze/gtk3-compat.h b/katze/gtk3-compat.h index fddfe977..70bc5c3f 100644 --- a/katze/gtk3-compat.h +++ b/katze/gtk3-compat.h @@ -89,8 +89,10 @@ G_BEGIN_DECLS #if !GTK_CHECK_VERSION (3, 2, 0) && defined (HAVE_HILDON_2_2) #define gtk_entry_set_placeholder_text hildon_gtk_entry_set_placeholder_text + #define gtk_entry_get_placeholder_text hildon_gtk_entry_get_placeholder_text #elif !GTK_CHECK_VERSION (3, 2, 0) - #define gtk_entry_set_placeholder_text sokoke_entry_set_default_text + void gtk_entry_set_placeholder_text (GtkEntry* entry, const gchar* text); + const gchar* gtk_entry_get_placeholder_text (GtkEntry* entry); #endif #if !GTK_CHECK_VERSION(2, 12, 0) diff --git a/midori/sokoke.c b/midori/sokoke.c index e60087b5..3db12aaa 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1731,18 +1731,40 @@ midori_download_prepare_tooltip_text (WebKitDownload* download) return g_string_free (tooltip, FALSE); } +static gboolean +sokoke_entry_has_placeholder_text (GtkEntry* entry) +{ + const gchar* text = gtk_entry_get_text (entry); + const gchar* hint = gtk_entry_get_placeholder_text (entry); + if (!gtk_widget_has_focus (GTK_WIDGET (entry)) + && hint != NULL + && (text == NULL || !strcmp (text, hint))) + return TRUE; + return FALSE; +} + static void sokoke_entry_changed_cb (GtkEditable* editable, GtkEntry* entry) { - const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry)); - gboolean visible = text && *text; + const gchar* text = gtk_entry_get_text (entry); + gboolean visible = text && *text + && ! sokoke_entry_has_placeholder_text (entry); gtk_icon_entry_set_icon_from_stock ( GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_SECONDARY, visible ? GTK_STOCK_CLEAR : NULL); } +static gboolean +sokoke_entry_focus_out_event_cb (GtkEditable* editable, + GdkEventFocus* event, + GtkEntry* entry) +{ + sokoke_entry_changed_cb (editable, entry); + return FALSE; +} + static void sokoke_entry_icon_released_cb (GtkEntry* entry, GtkIconEntryPosition icon_pos, @@ -1769,17 +1791,27 @@ sokoke_entry_set_clear_button_visible (GtkEntry* entry, g_object_connect (entry, "signal::icon-release", G_CALLBACK (sokoke_entry_icon_released_cb), NULL, + "signal::focus-in-event", + G_CALLBACK (sokoke_entry_focus_out_event_cb), entry, + "signal::focus-out-event", + G_CALLBACK (sokoke_entry_focus_out_event_cb), entry, "signal::changed", G_CALLBACK (sokoke_entry_changed_cb), entry, NULL); - g_signal_emit_by_name (G_OBJECT (entry), "changed"); + sokoke_entry_changed_cb ((GtkEditable*)entry, entry); } else { g_object_disconnect (entry, "any_signal::icon-release", G_CALLBACK (sokoke_entry_icon_released_cb), NULL, + "any_signal::focus-in-event", + G_CALLBACK (sokoke_entry_focus_out_event_cb), entry, + "any_signal::focus-out-event", + G_CALLBACK (sokoke_entry_focus_out_event_cb), entry, "any_signal::changed", G_CALLBACK (sokoke_entry_changed_cb), entry, NULL); + gtk_icon_entry_set_icon_from_stock ( + GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_SECONDARY, NULL); } } -- 2.39.5