From a20ee7d8839cd60d6a5d0df3b6608a873efecdf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Tue, 27 Jan 2009 19:31:45 +0000 Subject: [PATCH] Add a clear icon to the toolbar search and goto text fields (will be available with GTK >= 2.16). git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3516 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 3 +++ src/geanyentryaction.c | 6 +++++- src/ui_utils.c | 28 ++++++++++++++++++++++++++++ src/ui_utils.h | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 1c8cf40d0..0c14d8333 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,9 @@ * src/document.c: Fix legacy file monitoring since I broke once more. + * src/geanyentryaction.c, src/ui_utils.c, src/ui_utils.h: + Add a clear icon to the toolbar search and goto text fields + (will be available with GTK >= 2.16). 2009-01-27 Nick Treleaven diff --git a/src/geanyentryaction.c b/src/geanyentryaction.c index df8768107..af26859c8 100644 --- a/src/geanyentryaction.c +++ b/src/geanyentryaction.c @@ -26,6 +26,7 @@ #include "geany.h" #include "support.h" +#include "ui_utils.h" #include "geanyentryaction.h" #include @@ -88,7 +89,10 @@ static GtkWidget *geany_entry_action_create_tool_item(GtkAction *action) priv->entry = gtk_entry_new(); if (priv->numeric) - gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 8); + gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 9); + + ui_entry_add_clear_icon(priv->entry); + gtk_widget_show(priv->entry); toolitem = g_object_new(GTK_TYPE_TOOL_ITEM, NULL); diff --git a/src/ui_utils.c b/src/ui_utils.c index e105633a2..476a29171 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -1178,6 +1178,34 @@ ui_image_menu_item_new(const gchar *stock_id, const gchar *label) } +static void entry_clear_icon_press_cb(GtkEntry *entry, gint icon_pos, GdkEvent *event, gpointer data) +{ + if (event->button.button == 1 && icon_pos == 1) + { + gtk_entry_set_text(entry, ""); + } +} + + +/** Convenience function to add a small clear icon to the right end of the passed @a entry. + * A callback to clear the contents of the GtkEntry is automatically added. + * + * This feature is only available with GTK 2.16 but implemented as a runtime check, + * so it is safe to just use this function, if the code is ran with older versions, + * nothing happens. If ran with GTK 2.16 or newer, the icon is displayed. + * + * @param entry The GtkEntry object to which the icon should be attached. + */ +void ui_entry_add_clear_icon(GtkWidget *entry) +{ + if (gtk_check_version(2, 15, 2) == NULL) + { + g_object_set(entry, "secondary-icon-stock", "gtk-clear", NULL); + g_signal_connect(entry, "icon-press", G_CALLBACK(entry_clear_icon_press_cb), NULL); + } +} + + static void add_to_size_group(GtkWidget *widget, gpointer size_group) { g_return_if_fail(GTK_IS_SIZE_GROUP(size_group)); diff --git a/src/ui_utils.h b/src/ui_utils.h index 3c629a2b0..c92d44f00 100644 --- a/src/ui_utils.h +++ b/src/ui_utils.h @@ -185,6 +185,8 @@ GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name); void ui_widget_set_sensitive(GtkWidget *widget, gboolean set); +void ui_entry_add_clear_icon(GtkWidget *entry); + /* End of 'generic' functions */