2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser Public License as published by
8 * the Free Software Foundation; version 2 of the license.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser Public License for more details.
18 * SECTION:hildon-entry
19 * @short_description: Widget representing a text entry in the Hildon framework.
21 * The #HildonEntry is a GTK widget which represents a text entry. It
22 * is derived from the #GtkEntry widget and provides additional
23 * commodities specific to the Hildon framework.
25 * Besides all the features inherited from #GtkEntry, a #HildonEntry
26 * can also have a placeholder text. This text will be shown if the
27 * entry is empty and doesn't have the input focus, but it's otherwise
28 * ignored. Thus, calls to hildon_entry_get_text() will never return
29 * the placeholder text, not even when it's being displayed.
31 * Although #HildonEntry is derived from #GtkEntry,
32 * gtk_entry_get_text() and gtk_entry_set_text() must never be used to
33 * get/set the text in this widget. hildon_entry_get_text() and
34 * hildon_entry_set_text() must be used instead.
37 * <title>Creating a HildonEntry with a placeholder</title>
44 * entry = hildon_entry_new (HILDON_SIZE_AUTO);
45 * hildon_entry_set_placeholder (HILDON_ENTRY (entry), "First name");
53 #include "hildon-entry.h"
54 #include "hildon-helper.h"
56 G_DEFINE_TYPE (HildonEntry, hildon_entry, GTK_TYPE_ENTRY);
58 #define HILDON_ENTRY_GET_PRIVATE(obj) \
59 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
60 HILDON_TYPE_ENTRY, HildonEntryPrivate));
62 struct _HildonEntryPrivate
65 gboolean showing_placeholder;
69 hildon_entry_show_placeholder (HildonEntry *entry)
71 HildonEntryPrivate *priv = HILDON_ENTRY (entry)->priv;
73 priv->showing_placeholder = TRUE;
74 gtk_entry_set_text (GTK_ENTRY (entry), priv->placeholder);
75 hildon_helper_set_logical_color (GTK_WIDGET (entry),
76 GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedSecondaryTextColor");
80 hildon_entry_hide_placeholder (HildonEntry *entry, const gchar *text)
82 HildonEntryPrivate *priv = HILDON_ENTRY (entry)->priv;
84 priv->showing_placeholder = FALSE;
85 gtk_entry_set_text (GTK_ENTRY (entry), text);
86 hildon_helper_set_logical_color (GTK_WIDGET (entry),
87 GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedTextColor");
91 * hildon_entry_set_text:
92 * @entry: a #HildonEntry
95 * Sets the text in @entry to @text, replacing its current contents.
97 * Note that you must never use gtk_entry_set_text() to set the text
103 hildon_entry_set_text (HildonEntry *entry,
106 g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
108 if (text[0] == '\0' && !GTK_WIDGET_HAS_FOCUS (entry)) {
109 hildon_entry_show_placeholder (entry);
111 hildon_entry_hide_placeholder (entry, text);
116 * hildon_entry_get_text:
117 * @entry: a #HildonEntry
119 * Gets the current text in @entry.
121 * Note that you must never use gtk_entry_get_text() to get the text
122 * from a #HildonEntry.
124 * Also note that placeholder text (set using
125 * hildon_entry_set_placeholder()) is never returned. Only text set by
126 * hildon_entry_set_text() or typed by the user is considered.
128 * Returns: the text in @entry. This text must not be modified or
134 hildon_entry_get_text (HildonEntry *entry)
136 g_return_val_if_fail (HILDON_IS_ENTRY (entry), NULL);
138 if (entry->priv->showing_placeholder) {
142 return gtk_entry_get_text (GTK_ENTRY (entry));
146 * hildon_entry_set_placeholder:
147 * @entry: a #HildonEntry
148 * @text: the new text
150 * Sets the placeholder text in @entry to @text.
155 hildon_entry_set_placeholder (HildonEntry *entry,
158 g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
160 g_free (entry->priv->placeholder);
161 entry->priv->placeholder = g_strdup (text);
163 /* Show the placeholder if it needs to be updated or if should be shown now. */
164 if (entry->priv->showing_placeholder ||
165 (!GTK_WIDGET_HAS_FOCUS (entry) && gtk_entry_get_text (GTK_ENTRY (entry)) [0] == '\0')) {
166 hildon_entry_show_placeholder (entry);
172 * @size: The size of the entry
174 * Creates a new entry.
176 * Returns: a new #HildonEntry
181 hildon_entry_new (HildonSizeType size)
183 GtkWidget *entry = g_object_new (HILDON_TYPE_ENTRY, NULL);
185 hildon_gtk_widget_set_theme_size (entry, size);
191 hildon_entry_focus_in_event (GtkWidget *widget,
192 GdkEventFocus *event)
194 if (HILDON_ENTRY (widget)->priv->showing_placeholder) {
195 hildon_entry_hide_placeholder (HILDON_ENTRY (widget), "");
198 if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event) {
199 return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event (widget, event);
206 hildon_entry_focus_out_event (GtkWidget *widget,
207 GdkEventFocus *event)
209 if (gtk_entry_get_text (GTK_ENTRY (widget)) [0] == '\0') {
210 hildon_entry_show_placeholder (HILDON_ENTRY (widget));
213 if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event) {
214 return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event (widget, event);
221 hildon_entry_finalize (GObject *object)
223 HildonEntryPrivate *priv = HILDON_ENTRY (object)->priv;
225 g_free (priv->placeholder);
227 if (G_OBJECT_CLASS (hildon_entry_parent_class)->finalize)
228 G_OBJECT_CLASS (hildon_entry_parent_class)->finalize (object);
232 hildon_entry_class_init (HildonEntryClass *klass)
234 GObjectClass *gobject_class = (GObjectClass *)klass;
235 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
237 gobject_class->finalize = hildon_entry_finalize;
238 widget_class->focus_in_event = hildon_entry_focus_in_event;
239 widget_class->focus_out_event = hildon_entry_focus_out_event;
241 g_type_class_add_private (klass, sizeof (HildonEntryPrivate));
245 hildon_entry_init (HildonEntry *self)
247 self->priv = HILDON_ENTRY_GET_PRIVATE (self);
248 self->priv->placeholder = g_strdup ("");
249 self->priv->showing_placeholder = FALSE;