From: Christian Dywan Date: Wed, 9 Nov 2011 20:45:23 +0000 (+0100) Subject: sokoke_entry_set_default_text becomes gtk_entry_set_placeholder_text X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a121d24d355c7a773d19a689dd92e6ecf405c6a2;p=midori sokoke_entry_set_default_text becomes gtk_entry_set_placeholder_text GTK+ 3.2 and Hildon 2.2 have the exact same feature built-in. --- diff --git a/katze/gtk3-compat.c b/katze/gtk3-compat.c index cffff31b..0c0a4369 100644 --- a/katze/gtk3-compat.c +++ b/katze/gtk3-compat.c @@ -1,5 +1,108 @@ #include "katze/gtk3-compat.h" +#if !GTK_CHECK_VERSION (3, 2, 0) && !defined (HAVE_HILDON_2_2) +static void +sokoke_widget_set_pango_font_style (GtkWidget* widget, + PangoStyle style) +{ + /* Conveniently change the pango font style + For some reason we need to reset if we actually want the normal style */ + if (style == PANGO_STYLE_NORMAL) + gtk_widget_modify_font (widget, NULL); + else + { + PangoFontDescription* font_description = pango_font_description_new (); + pango_font_description_set_style (font_description, PANGO_STYLE_ITALIC); + gtk_widget_modify_font (widget, font_description); + pango_font_description_free (font_description); + } +} + +static gboolean +sokoke_on_entry_focus_in_event (GtkEntry* entry, + GdkEventFocus* event, + gpointer userdata) +{ + gint has_default = GPOINTER_TO_INT ( + 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)); + sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), + PANGO_STYLE_NORMAL); + } + return FALSE; +} + +static gboolean +sokoke_on_entry_focus_out_event (GtkEntry* entry, + GdkEventFocus* event, + gpointer userdata) +{ + const gchar* text = gtk_entry_get_text (entry); + if (text && !*text) + { + 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)); + sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), + PANGO_STYLE_ITALIC); + } + return FALSE; +} + +static void +sokoke_on_entry_drag_data_received (GtkEntry* entry, + GdkDragContext* drag_context, + gint x, + gint y, + guint timestamp, + gpointer user_data) +{ + sokoke_on_entry_focus_in_event (entry, NULL, NULL); +} + +void +gtk_entry_set_placeholder_text (GtkEntry* entry, + const gchar* default_text) +{ + /* Note: The default text initially overwrites any previous text */ + gchar* old_value = g_object_get_data (G_OBJECT (entry), + "sokoke_default_text"); + if (!old_value) + { + g_object_set_data (G_OBJECT (entry), "sokoke_has_default", + GINT_TO_POINTER (1)); + sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), + PANGO_STYLE_ITALIC); + gtk_entry_set_text (entry, default_text); + g_signal_connect (entry, "drag-data-received", + G_CALLBACK (sokoke_on_entry_drag_data_received), NULL); + g_signal_connect (entry, "focus-in-event", + G_CALLBACK (sokoke_on_entry_focus_in_event), NULL); + g_signal_connect (entry, "focus-out-event", + G_CALLBACK (sokoke_on_entry_focus_out_event), NULL); + } + else if (!gtk_widget_has_focus (GTK_WIDGET (entry))) + { + gint has_default = GPOINTER_TO_INT ( + g_object_get_data (G_OBJECT (entry), "sokoke_has_default")); + if (has_default) + { + gtk_entry_set_text (entry, default_text); + sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), + PANGO_STYLE_ITALIC); + } + } + g_object_set_data (G_OBJECT (entry), "sokoke_default_text", + (gpointer)default_text); +} +#endif + #if !GTK_CHECK_VERSION (2, 12, 0) void diff --git a/katze/gtk3-compat.h b/katze/gtk3-compat.h index 2f058deb..a59ff936 100644 --- a/katze/gtk3-compat.h +++ b/katze/gtk3-compat.h @@ -63,6 +63,12 @@ G_BEGIN_DECLS #define GTK_DIALOG_NO_SEPARATOR 0 #endif +#if !GTK_CHECK_VERSION (3, 2, 0) && defined (HAVE_HILDON_2_2) + #define gtk_entry_set_placeholder_text hildon_gtk_entry_set_placeholder_text +#elif !GTK_CHECK_VERSION (3, 2, 0) + #define gtk_entry_set_placeholder_text sokoke_entry_set_default_text +#endif + #if !GTK_CHECK_VERSION(2, 12, 0) void diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c index 687cb77f..00425812 100644 --- a/midori/midori-searchaction.c +++ b/midori/midori-searchaction.c @@ -536,7 +536,7 @@ midori_search_action_set_entry_icon (MidoriSearchAction* search_action, else gtk_icon_entry_set_icon_from_icon_name (GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_PRIMARY, icon_name); - sokoke_entry_set_default_text (GTK_ENTRY (entry), + gtk_entry_set_placeholder_text (GTK_ENTRY (entry), katze_item_get_name (search_action->current_item)); } else @@ -544,7 +544,7 @@ midori_search_action_set_entry_icon (MidoriSearchAction* search_action, gtk_icon_entry_set_icon_from_stock (GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_PRIMARY, GTK_STOCK_FIND); - sokoke_entry_set_default_text (GTK_ENTRY (entry), ""); + gtk_entry_set_placeholder_text (GTK_ENTRY (entry), ""); } } diff --git a/midori/sokoke.c b/midori/sokoke.c index 63874917..2c2ee9d1 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -850,107 +850,6 @@ sokoke_xfce_header_new (const gchar* icon, return NULL; } -static void -sokoke_widget_set_pango_font_style (GtkWidget* widget, - PangoStyle style) -{ - /* Conveniently change the pango font style - For some reason we need to reset if we actually want the normal style */ - if (style == PANGO_STYLE_NORMAL) - gtk_widget_modify_font (widget, NULL); - else - { - PangoFontDescription* font_description = pango_font_description_new (); - pango_font_description_set_style (font_description, PANGO_STYLE_ITALIC); - gtk_widget_modify_font (widget, font_description); - pango_font_description_free (font_description); - } -} - -static gboolean -sokoke_on_entry_focus_in_event (GtkEntry* entry, - GdkEventFocus* event, - gpointer userdata) -{ - gint has_default = GPOINTER_TO_INT ( - 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)); - sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), - PANGO_STYLE_NORMAL); - } - return FALSE; -} - -static gboolean -sokoke_on_entry_focus_out_event (GtkEntry* entry, - GdkEventFocus* event, - gpointer userdata) -{ - const gchar* text = gtk_entry_get_text (entry); - if (text && !*text) - { - 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)); - sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), - PANGO_STYLE_ITALIC); - } - return FALSE; -} - -static void -sokoke_on_entry_drag_data_received (GtkEntry* entry, - GdkDragContext* drag_context, - gint x, - gint y, - guint timestamp, - gpointer user_data) -{ - sokoke_on_entry_focus_in_event (entry, NULL, NULL); -} - -void -sokoke_entry_set_default_text (GtkEntry* entry, - const gchar* default_text) -{ - /* Note: The default text initially overwrites any previous text */ - gchar* old_value = g_object_get_data (G_OBJECT (entry), - "sokoke_default_text"); - if (!old_value) - { - g_object_set_data (G_OBJECT (entry), "sokoke_has_default", - GINT_TO_POINTER (1)); - sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), - PANGO_STYLE_ITALIC); - gtk_entry_set_text (entry, default_text); - g_signal_connect (entry, "drag-data-received", - G_CALLBACK (sokoke_on_entry_drag_data_received), NULL); - g_signal_connect (entry, "focus-in-event", - G_CALLBACK (sokoke_on_entry_focus_in_event), NULL); - g_signal_connect (entry, "focus-out-event", - G_CALLBACK (sokoke_on_entry_focus_out_event), NULL); - } - else if (!gtk_widget_has_focus (GTK_WIDGET (entry))) - { - gint has_default = GPOINTER_TO_INT ( - g_object_get_data (G_OBJECT (entry), "sokoke_has_default")); - if (has_default) - { - gtk_entry_set_text (entry, default_text); - sokoke_widget_set_pango_font_style (GTK_WIDGET (entry), - PANGO_STYLE_ITALIC); - } - } - g_object_set_data (G_OBJECT (entry), "sokoke_default_text", - (gpointer)default_text); -} - gchar* sokoke_key_file_get_string_default (GKeyFile* key_file, const gchar* group,