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"
55 G_DEFINE_TYPE (HildonEntry, hildon_entry, GTK_TYPE_ENTRY);
57 #define HILDON_ENTRY_GET_PRIVATE(obj) \
58 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
59 HILDON_TYPE_ENTRY, HildonEntryPrivate));
61 typedef struct _HildonEntryPrivate HildonEntryPrivate;
63 struct _HildonEntryPrivate
68 static const gchar *placeholder_widget_name = "hildon-entry-placeholder";
70 /* Function used to decide whether to show the placeholder or not */
72 hildon_entry_refresh_contents (GtkWidget *entry)
74 HildonEntryPrivate *priv = HILDON_ENTRY_GET_PRIVATE (entry);
75 gboolean showing_placeholder, entry_has_focus;
77 showing_placeholder = g_str_equal (gtk_widget_get_name (entry), placeholder_widget_name);
78 entry_has_focus = GTK_WIDGET_HAS_FOCUS (entry);
80 if (showing_placeholder) {
81 if (entry_has_focus) {
82 /* Remove the placeholder when the widget obtains focus */
83 gtk_widget_set_name (entry, NULL);
84 gtk_entry_set_text (GTK_ENTRY (entry), "");
86 /* Update the placeholder (it may have been changed) */
87 gtk_entry_set_text (GTK_ENTRY (entry), priv->placeholder);
90 /* Show the placeholder when the widget is empty and has no focus */
91 const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
92 if (text[0] == '\0' && !entry_has_focus) {
93 if (priv->placeholder[0] != '\0') {
94 gtk_widget_set_name (entry, placeholder_widget_name);
95 gtk_entry_set_text (GTK_ENTRY (entry), priv->placeholder);
102 * hildon_entry_set_text:
103 * @entry: a #HildonEntry
104 * @text: the new text
106 * Sets the text in @entry to @text, replacing its current contents.
108 * Note that you must never use gtk_entry_set_text() to set the text
112 hildon_entry_set_text (HildonEntry *entry,
115 g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
117 gtk_widget_set_name (GTK_WIDGET (entry), NULL);
118 gtk_entry_set_text (GTK_ENTRY (entry), text);
120 /* If the entry is cleared show the placeholder */
122 hildon_entry_refresh_contents (GTK_WIDGET (entry));
126 * hildon_entry_get_text:
127 * @entry: a #HildonEntry
129 * Gets the current text in @entry.
131 * Note that you must never use gtk_entry_get_text() to get the text
132 * from a #HildonEntry.
134 * Also note that placeholder text (set using
135 * hildon_entry_set_placeholder()) is never returned. Only text set by
136 * hildon_entry_set_text() or typed by the user is considered.
138 * Returns: the text in @entry. This text must not be modified or
142 hildon_entry_get_text (HildonEntry *entry)
144 g_return_val_if_fail (HILDON_IS_ENTRY (entry), NULL);
146 if (g_str_equal (gtk_widget_get_name (GTK_WIDGET (entry)), placeholder_widget_name)) {
150 return gtk_entry_get_text (GTK_ENTRY (entry));
154 * hildon_entry_set_placeholder:
155 * @entry: a #HildonEntry
156 * @text: the new text
158 * Sets the placeholder text in @entry to @text.
161 hildon_entry_set_placeholder (HildonEntry *entry,
164 HildonEntryPrivate *priv;
166 g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
168 priv = HILDON_ENTRY_GET_PRIVATE (entry);
170 g_free (priv->placeholder);
171 priv->placeholder = g_strdup (text);
172 hildon_entry_refresh_contents (GTK_WIDGET (entry));
177 * @size: The size of the entry
179 * Creates a new entry.
181 * Returns: a new #HildonEntry
184 hildon_entry_new (HildonSizeType size)
186 GtkWidget *entry = g_object_new (HILDON_TYPE_ENTRY, NULL);
188 hildon_gtk_widget_set_theme_size (entry, size);
194 hildon_entry_focus_in_event (GtkWidget *widget,
195 GdkEventFocus *event)
197 hildon_entry_refresh_contents (widget);
199 if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event) {
200 return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event (widget, event);
207 hildon_entry_focus_out_event (GtkWidget *widget,
208 GdkEventFocus *event)
210 hildon_entry_refresh_contents (widget);
212 if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event) {
213 return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event (widget, event);
220 hildon_entry_finalize (GObject *object)
222 HildonEntryPrivate *priv = HILDON_ENTRY_GET_PRIVATE (object);
224 g_free (priv->placeholder);
226 if (G_OBJECT_CLASS (hildon_entry_parent_class)->finalize)
227 G_OBJECT_CLASS (hildon_entry_parent_class)->finalize (object);
231 hildon_entry_class_init (HildonEntryClass *klass)
233 GObjectClass *gobject_class = (GObjectClass *)klass;
234 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
236 gobject_class->finalize = hildon_entry_finalize;
237 widget_class->focus_in_event = hildon_entry_focus_in_event;
238 widget_class->focus_out_event = hildon_entry_focus_out_event;
240 g_type_class_add_private (klass, sizeof (HildonEntryPrivate));
244 hildon_entry_init (HildonEntry *self)
246 HildonEntryPrivate *priv = HILDON_ENTRY_GET_PRIVATE (self);
248 priv->placeholder = g_strdup ("");