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.
This commit is contained in:
Christian Dywan 2011-11-09 21:45:23 +01:00
parent 179e33d054
commit a121d24d35
4 changed files with 111 additions and 103 deletions

View file

@ -1,5 +1,108 @@
#include "katze/gtk3-compat.h" #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) #if !GTK_CHECK_VERSION (2, 12, 0)
void void

View file

@ -63,6 +63,12 @@ G_BEGIN_DECLS
#define GTK_DIALOG_NO_SEPARATOR 0 #define GTK_DIALOG_NO_SEPARATOR 0
#endif #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) #if !GTK_CHECK_VERSION(2, 12, 0)
void void

View file

@ -536,7 +536,7 @@ midori_search_action_set_entry_icon (MidoriSearchAction* search_action,
else else
gtk_icon_entry_set_icon_from_icon_name (GTK_ICON_ENTRY (entry), gtk_icon_entry_set_icon_from_icon_name (GTK_ICON_ENTRY (entry),
GTK_ICON_ENTRY_PRIMARY, icon_name); 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)); katze_item_get_name (search_action->current_item));
} }
else 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_set_icon_from_stock (GTK_ICON_ENTRY (entry),
GTK_ICON_ENTRY_PRIMARY, GTK_ICON_ENTRY_PRIMARY,
GTK_STOCK_FIND); GTK_STOCK_FIND);
sokoke_entry_set_default_text (GTK_ENTRY (entry), ""); gtk_entry_set_placeholder_text (GTK_ENTRY (entry), "");
} }
} }

View file

@ -850,107 +850,6 @@ sokoke_xfce_header_new (const gchar* icon,
return NULL; 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* gchar*
sokoke_key_file_get_string_default (GKeyFile* key_file, sokoke_key_file_get_string_default (GKeyFile* key_file,
const gchar* group, const gchar* group,