aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-hinted-entry.c
blob: 51131cedcd9c5b2e8d85a797e34ff9de06d9cbe4 (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * e-hinted-entry.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-hinted-entry.h"

#define E_HINTED_ENTRY_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_HINTED_ENTRY, EHintedEntryPrivate))

struct _EHintedEntryPrivate {
    gchar *hint;
    guint hint_shown : 1;
};

enum {
    PROP_0,
    PROP_HINT,
    PROP_HINT_SHOWN
};

static gpointer parent_class;

static void
hinted_entry_hide_hint (EHintedEntry *entry)
{
    entry->priv->hint_shown = FALSE;

    gtk_entry_set_text (GTK_ENTRY (entry), "");

    gtk_widget_modify_text (GTK_WIDGET (entry), GTK_STATE_NORMAL, NULL);

    g_object_notify (G_OBJECT (entry), "hint-shown");
}

static void
hinted_entry_show_hint (EHintedEntry *entry)
{
    GtkStyle *style;
    const GdkColor *color;
    const gchar *hint;

    entry->priv->hint_shown = TRUE;

    hint = e_hinted_entry_get_hint (entry);
    gtk_entry_set_text (GTK_ENTRY (entry), hint);

    style = gtk_widget_get_style (GTK_WIDGET (entry));
    color = &style->text[GTK_STATE_INSENSITIVE];
    gtk_widget_modify_text (GTK_WIDGET (entry), GTK_STATE_NORMAL, color);

    g_object_notify (G_OBJECT (entry), "hint-shown");
}

static void
hinted_entry_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_HINT:
            e_hinted_entry_set_hint (
                E_HINTED_ENTRY (object),
                g_value_get_string (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
hinted_entry_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_HINT:
            g_value_set_string (
                value, e_hinted_entry_get_hint (
                E_HINTED_ENTRY (object)));
            return;

        case PROP_HINT_SHOWN:
            g_value_set_boolean (
                value, e_hinted_entry_get_hint_shown (
                E_HINTED_ENTRY (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
hinted_entry_finalize (GObject *object)
{
    EHintedEntryPrivate *priv;

    priv = E_HINTED_ENTRY_GET_PRIVATE (object);

    g_free (priv->hint);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
hinted_entry_focus_in_event (GtkWidget *widget,
                             GdkEventFocus *event)
{
    EHintedEntry *entry = E_HINTED_ENTRY (widget);

    if (e_hinted_entry_get_hint_shown (entry))
        hinted_entry_hide_hint (entry);

    /* Chain up to parent's focus_in_event() method. */
    return GTK_WIDGET_CLASS (parent_class)->
        focus_in_event (widget, event);
}

static gboolean
hinted_entry_focus_out_event (GtkWidget *widget,
                              GdkEventFocus *event)
{
    EHintedEntry *entry = E_HINTED_ENTRY (widget);
    const gchar *text;

    text = e_hinted_entry_get_text (entry);

    if (text == NULL || *text == '\0')
        hinted_entry_show_hint (E_HINTED_ENTRY (widget));

    /* Chain up to parent's focus_out_event() method. */
    return GTK_WIDGET_CLASS (parent_class)->
        focus_out_event (widget, event);
}

static void
hinted_entry_class_init (EHintedEntryClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EHintedEntryPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = hinted_entry_set_property;
    object_class->get_property = hinted_entry_get_property;
    object_class->finalize = hinted_entry_finalize;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->focus_in_event = hinted_entry_focus_in_event;
    widget_class->focus_out_event = hinted_entry_focus_out_event;

    g_object_class_install_property (
        object_class,
        PROP_HINT,
        g_param_spec_string (
            "hint",
            "Hint",
            NULL,
            NULL,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_HINT_SHOWN,
        g_param_spec_boolean (
            "hint-shown",
            "Hint Shown",
            NULL,
            FALSE,
            G_PARAM_READABLE));
}

static void
hinted_entry_init (EHintedEntry *entry)
{
    entry->priv = E_HINTED_ENTRY_GET_PRIVATE (entry);
    entry->priv->hint = g_strdup ("");  /* hint must never be NULL */
    hinted_entry_show_hint (entry);
}

GType
e_hinted_entry_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EHintedEntryClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) hinted_entry_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EHintedEntry),
            0,     /* n_preallocs */
            (GInstanceInitFunc) hinted_entry_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            GTK_TYPE_ENTRY, "EHintedEntry", &type_info, 0);
    }

    return type;
}

GtkWidget *
e_hinted_entry_new (void)
{
    return g_object_new (E_TYPE_HINTED_ENTRY, NULL);
}

const gchar *
e_hinted_entry_get_hint (EHintedEntry *entry)
{
    g_return_val_if_fail (E_IS_HINTED_ENTRY (entry), NULL);

    return entry->priv->hint;
}

void
e_hinted_entry_set_hint (EHintedEntry *entry,
                         const gchar *hint)
{
    g_return_if_fail (E_IS_HINTED_ENTRY (entry));

    if (hint == NULL)
        hint = "";

    g_free (entry->priv->hint);
    entry->priv->hint = g_strdup (hint);

    if (e_hinted_entry_get_hint_shown (entry))
        gtk_entry_set_text (GTK_ENTRY (entry), hint);

    g_object_notify (G_OBJECT (entry), "hint");
}

gboolean
e_hinted_entry_get_hint_shown (EHintedEntry *entry)
{
    g_return_val_if_fail (E_IS_HINTED_ENTRY (entry), FALSE);

    return entry->priv->hint_shown;
}

const gchar *
e_hinted_entry_get_text (EHintedEntry *entry)
{
    /* XXX This clumsily overrides gtk_entry_get_text(). */

    g_return_val_if_fail (E_IS_HINTED_ENTRY (entry), NULL);

    if (e_hinted_entry_get_hint_shown (entry))
        return "";

    return gtk_entry_get_text (GTK_ENTRY (entry));
}

void
e_hinted_entry_set_text (EHintedEntry *entry,
                         const gchar *text)
{
    /* XXX This clumsily overrides gtk_entry_set_text(). */

    g_return_if_fail (E_IS_HINTED_ENTRY (entry));

    if (text == NULL)
        text = "";

    if (*text == '\0' && !gtk_widget_has_focus (GTK_WIDGET (entry)))
        hinted_entry_show_hint (entry);
    else {
        hinted_entry_hide_hint (entry);
        gtk_entry_set_text (GTK_ENTRY (entry), text);
    }
}