1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* This file is a part of hildon
*
* Copyright (C) 2008 Nokia Corporation, all rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; version 2 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
*/
/**
* SECTION:hildon-entry
* @short_description: Widget representing a text entry in the Hildon framework.
*
* The #HildonEntry is a GTK widget which represents a text entry. It
* is derived from the #GtkEntry widget and provides additional
* commodities specific to the Hildon framework.
*
* Besides all the features inherited from #GtkEntry, a #HildonEntry
* can also have a placeholder text. This text will be shown if the
* entry is empty and doesn't have the input focus, but it's otherwise
* ignored. Thus, calls to hildon_entry_get_text() will never return
* the placeholder text, not even when it's being displayed.
*
* Although #HildonEntry is derived from #GtkEntry,
* gtk_entry_get_text() and gtk_entry_set_text() must never be used to
* get/set the text in this widget. hildon_entry_get_text() and
* hildon_entry_set_text() must be used instead.
*
* <example>
* <title>Creating a HildonEntry with a placeholder</title>
* <programlisting>
* GtkWidget *
* create_entry (void)
* {
* GtkWidget *entry;
* <!-- -->
* entry = hildon_entry_new (HILDON_SIZE_AUTO);
* hildon_entry_set_placeholder (HILDON_ENTRY (entry), "First name");
* <!-- -->
* return entry;
* }
* </programlisting>
* </example>
*/
#include "hildon-entry.h"
#include "hildon-helper.h"
G_DEFINE_TYPE (HildonEntry, hildon_entry, GTK_TYPE_ENTRY);
enum {
PROP_SIZE = 1
};
#define HILDON_ENTRY_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
HILDON_TYPE_ENTRY, HildonEntryPrivate));
struct _HildonEntryPrivate
{
gchar *placeholder;
gboolean showing_placeholder;
};
static void
set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_SIZE:
hildon_gtk_widget_set_theme_size (GTK_WIDGET (object), g_value_get_flags (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
hildon_entry_show_placeholder (HildonEntry *entry)
{
HildonEntryPrivate *priv = HILDON_ENTRY (entry)->priv;
priv->showing_placeholder = TRUE;
gtk_entry_set_text (GTK_ENTRY (entry), priv->placeholder);
hildon_helper_set_logical_color (GTK_WIDGET (entry),
GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedSecondaryTextColor");
}
static void
hildon_entry_hide_placeholder (HildonEntry *entry, const gchar *text)
{
HildonEntryPrivate *priv = HILDON_ENTRY (entry)->priv;
priv->showing_placeholder = FALSE;
gtk_entry_set_text (GTK_ENTRY (entry), text);
hildon_helper_set_logical_color (GTK_WIDGET (entry),
GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedTextColor");
}
/**
* hildon_entry_set_text:
* @entry: a #HildonEntry
* @text: the new text
*
* Sets the text in @entry to @text, replacing its current contents.
*
* Note that you must never use gtk_entry_set_text() to set the text
* of a #HildonEntry.
*
* Since: 2.2
*/
void
hildon_entry_set_text (HildonEntry *entry,
const gchar *text)
{
g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
if (text[0] == '\0' && !GTK_WIDGET_HAS_FOCUS (entry)) {
hildon_entry_show_placeholder (entry);
} else {
hildon_entry_hide_placeholder (entry, text);
}
}
/**
* hildon_entry_get_text:
* @entry: a #HildonEntry
*
* Gets the current text in @entry.
*
* Note that you must never use gtk_entry_get_text() to get the text
* from a #HildonEntry.
*
* Also note that placeholder text (set using
* hildon_entry_set_placeholder()) is never returned. Only text set by
* hildon_entry_set_text() or typed by the user is considered.
*
* Returns: the text in @entry. This text must not be modified or
* freed.
*
* Since: 2.2
*/
const gchar *
hildon_entry_get_text (HildonEntry *entry)
{
g_return_val_if_fail (HILDON_IS_ENTRY (entry), NULL);
if (entry->priv->showing_placeholder) {
return "";
}
return gtk_entry_get_text (GTK_ENTRY (entry));
}
/**
* hildon_entry_set_placeholder:
* @entry: a #HildonEntry
* @text: the new text
*
* Sets the placeholder text in @entry to @text.
*
* Since: 2.2
*/
void
hildon_entry_set_placeholder (HildonEntry *entry,
const gchar *text)
{
g_return_if_fail (HILDON_IS_ENTRY (entry) && text != NULL);
g_free (entry->priv->placeholder);
entry->priv->placeholder = g_strdup (text);
/* Show the placeholder if it needs to be updated or if should be shown now. */
if (entry->priv->showing_placeholder ||
(!GTK_WIDGET_HAS_FOCUS (entry) && gtk_entry_get_text (GTK_ENTRY (entry)) [0] == '\0')) {
hildon_entry_show_placeholder (entry);
}
}
/**
* hildon_entry_new:
* @size: The size of the entry
*
* Creates a new entry.
*
* Returns: a new #HildonEntry
*
* Since: 2.2
*/
GtkWidget *
hildon_entry_new (HildonSizeType size)
{
return g_object_new (HILDON_TYPE_ENTRY, "size", size, NULL);
}
static gboolean
hildon_entry_focus_in_event (GtkWidget *widget,
GdkEventFocus *event)
{
if (HILDON_ENTRY (widget)->priv->showing_placeholder) {
hildon_entry_hide_placeholder (HILDON_ENTRY (widget), "");
}
if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event) {
return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event (widget, event);
} else {
return FALSE;
}
}
static gboolean
hildon_entry_focus_out_event (GtkWidget *widget,
GdkEventFocus *event)
{
if (gtk_entry_get_text (GTK_ENTRY (widget)) [0] == '\0') {
hildon_entry_show_placeholder (HILDON_ENTRY (widget));
}
if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event) {
return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_out_event (widget, event);
} else {
return FALSE;
}
}
static void
hildon_entry_finalize (GObject *object)
{
HildonEntryPrivate *priv = HILDON_ENTRY (object)->priv;
g_free (priv->placeholder);
if (G_OBJECT_CLASS (hildon_entry_parent_class)->finalize)
G_OBJECT_CLASS (hildon_entry_parent_class)->finalize (object);
}
static void
hildon_entry_class_init (HildonEntryClass *klass)
{
GObjectClass *gobject_class = (GObjectClass *)klass;
GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
gobject_class->set_property = set_property;
gobject_class->finalize = hildon_entry_finalize;
widget_class->focus_in_event = hildon_entry_focus_in_event;
widget_class->focus_out_event = hildon_entry_focus_out_event;
g_object_class_install_property (
gobject_class,
PROP_SIZE,
g_param_spec_flags (
"size",
"Size",
"Size request for the entry",
HILDON_TYPE_SIZE_TYPE,
HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE));
g_type_class_add_private (klass, sizeof (HildonEntryPrivate));
}
static void
hildon_entry_init (HildonEntry *self)
{
self->priv = HILDON_ENTRY_GET_PRIVATE (self);
self->priv->placeholder = g_strdup ("");
self->priv->showing_placeholder = FALSE;
}
|