From c0e0bee467561d5840430151bea760e104766992 Mon Sep 17 00:00:00 2001 From: Peter Hatina Date: Sat, 14 Jan 2012 00:34:11 +0100 Subject: [PATCH] Implement sokoke_entry_set_clear_button_visible Fixes: https://bugs.launchpad.net/midori/+bug/699954 --- midori/midori-searchaction.c | 1 + midori/sokoke.c | 52 ++++++++++++++++++++++++++++++++++++ midori/sokoke.h | 4 +++ 3 files changed, 57 insertions(+) diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c index 39ef35be..c4a97121 100644 --- a/midori/midori-searchaction.c +++ b/midori/midori-searchaction.c @@ -312,6 +312,7 @@ midori_search_action_create_tool_item (GtkAction* action) toolitem = GTK_WIDGET (gtk_tool_item_new ()); entry = gtk_icon_entry_new (); + sokoke_entry_set_clear_button_visible (GTK_ENTRY (entry), TRUE); gtk_icon_entry_set_icon_highlight (GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_PRIMARY, TRUE); alignment = gtk_alignment_new (0, 0.5, 1, 0.1); diff --git a/midori/sokoke.c b/midori/sokoke.c index 39ea28eb..e60087b5 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1731,3 +1731,55 @@ midori_download_prepare_tooltip_text (WebKitDownload* download) return g_string_free (tooltip, 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; + gtk_icon_entry_set_icon_from_stock ( + GTK_ICON_ENTRY (entry), + GTK_ICON_ENTRY_SECONDARY, + visible ? GTK_STOCK_CLEAR : NULL); +} + +static void +sokoke_entry_icon_released_cb (GtkEntry* entry, + GtkIconEntryPosition icon_pos, + GdkEvent* event, + gpointer user_data) +{ + if (icon_pos != GTK_ICON_ENTRY_SECONDARY) + return; + + gtk_entry_set_text (entry, ""); + gtk_widget_grab_focus (GTK_WIDGET (entry)); +} + +void +sokoke_entry_set_clear_button_visible (GtkEntry* entry, + gboolean visible) +{ + g_return_if_fail (GTK_IS_ENTRY (entry)); + + gtk_icon_entry_set_icon_highlight (GTK_ICON_ENTRY (entry), + GTK_ICON_ENTRY_SECONDARY, TRUE); + if (visible) + { + g_object_connect (entry, + "signal::icon-release", + G_CALLBACK (sokoke_entry_icon_released_cb), NULL, + "signal::changed", + G_CALLBACK (sokoke_entry_changed_cb), entry, NULL); + g_signal_emit_by_name (G_OBJECT (entry), "changed"); + } + else + { + g_object_disconnect (entry, + "any_signal::icon-release", + G_CALLBACK (sokoke_entry_icon_released_cb), NULL, + "any_signal::changed", + G_CALLBACK (sokoke_entry_changed_cb), entry, NULL); + } +} + diff --git a/midori/sokoke.h b/midori/sokoke.h index db7aa28a..ac8902f0 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -202,4 +202,8 @@ sokoke_build_thumbnail_path (const gchar* name); gchar* midori_download_prepare_tooltip_text (WebKitDownload* download); +void +sokoke_entry_set_clear_button_visible (GtkEntry* entry, + gboolean visible); + #endif /* !__SOKOKE_H__ */