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
|
/*
* Copyright (C) 2007 OpenedHand Ltd
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <string.h>
#include <gtk/gtk.h>
#include "koto-hint-entry.h"
G_DEFINE_TYPE (KotoHintEntry, koto_hint_entry, GTK_TYPE_ENTRY);
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), KOTO_TYPE_HINT_ENTRY, KotoHintEntryPrivate))
typedef enum {
STATE_HINTING,
STATE_ENTRY,
} EntryState;
typedef struct {
EntryState state;
char *hint;
} KotoHintEntryPrivate;
/*
* Private methods.
*/
static void koto_hint_entry_style_set (GtkWidget *widget, GtkStyle *old_style, gpointer user_data);
static void
change_style (KotoHintEntry *entry)
{
KotoHintEntryPrivate *priv = GET_PRIVATE (entry);
g_signal_handlers_block_by_func (entry, koto_hint_entry_style_set, NULL);
switch (priv->state) {
case STATE_ENTRY:
gtk_widget_modify_text ((GtkWidget*) entry, GTK_STATE_NORMAL, NULL);
break;
case STATE_HINTING:
gtk_widget_modify_text ((GtkWidget*) entry,
GTK_STATE_NORMAL,
>k_widget_get_style ((GtkWidget*) entry)->text[GTK_STATE_INSENSITIVE]);
break;
}
g_signal_handlers_unblock_by_func (entry, koto_hint_entry_style_set, NULL);
}
static void
update (KotoHintEntry *entry)
{
KotoHintEntryPrivate *priv;
GtkWidget *widget;
const char *text;
g_assert (KOTO_IS_HINT_ENTRY (entry));
priv = GET_PRIVATE (entry);
widget = GTK_WIDGET (entry);
text = gtk_entry_get_text (GTK_ENTRY (entry));
if (gtk_widget_has_focus (GTK_WIDGET (entry))) {
if (priv->state == STATE_HINTING) {
priv->state = STATE_ENTRY;
gtk_entry_set_text (GTK_ENTRY (entry), "");
change_style (entry);
}
} else {
if (text == NULL || text[0] == '\0') {
priv->state = STATE_HINTING;
gtk_entry_set_text (GTK_ENTRY (entry), priv->hint);
change_style (entry);
} else {
priv->state = STATE_ENTRY;
}
}
}
/*
* Object methods.
*/
enum {
PROP_0,
PROP_HINT,
};
static void
koto_hint_entry_style_set (GtkWidget *widget, GtkStyle *old_style, gpointer user_data)
{
change_style (KOTO_HINT_ENTRY (widget));
}
static gboolean
focus_in_event (GtkWidget *widget, GdkEventFocus *event)
{
update (KOTO_HINT_ENTRY (widget));
GTK_WIDGET_CLASS (koto_hint_entry_parent_class)->focus_in_event (widget, event);
return FALSE;
}
static gboolean
focus_out_event (GtkWidget *widget, GdkEventFocus *event)
{
update (KOTO_HINT_ENTRY (widget));
GTK_WIDGET_CLASS (koto_hint_entry_parent_class)->focus_out_event (widget, event);
return FALSE;
}
static void
koto_hint_entry_constructed (GObject *object)
{
update (KOTO_HINT_ENTRY (object));
}
static void
koto_hint_entry_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
KotoHintEntryPrivate *priv = GET_PRIVATE (object);
switch (property_id) {
case PROP_HINT:
g_value_set_string (value, priv->hint);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
koto_hint_entry_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
KotoHintEntryPrivate *priv = GET_PRIVATE (object);
switch (property_id) {
case PROP_HINT:
if (priv->hint) {
g_free (priv->hint);
}
priv->hint = g_value_dup_string (value);
update (KOTO_HINT_ENTRY (object));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
koto_hint_entry_finalize (GObject *object)
{
KotoHintEntryPrivate *priv = GET_PRIVATE (object);
g_free (priv->hint);
G_OBJECT_CLASS (koto_hint_entry_parent_class)->finalize (object);
}
static void
koto_hint_entry_class_init (KotoHintEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
g_type_class_add_private (klass, sizeof (KotoHintEntryPrivate));
object_class->constructed = koto_hint_entry_constructed;
object_class->set_property = koto_hint_entry_set_property;
object_class->get_property = koto_hint_entry_get_property;
object_class->finalize = koto_hint_entry_finalize;
widget_class->focus_in_event = focus_in_event;
widget_class->focus_out_event = focus_out_event;
g_object_class_install_property (object_class, PROP_HINT, g_param_spec_string
("hint", "hint", NULL, "",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
}
static void
koto_hint_entry_init (KotoHintEntry *self)
{
/* Do this here instead of overriding the callback so we can't block ourself */
g_signal_connect (self, "style-set", G_CALLBACK (koto_hint_entry_style_set), NULL);
}
/*
* Public methods.
*/
/**
* koto_hint_entry_new:
* @hint: The hint to display when the widget is not focused
*
* Create a new #KotoHintEntry widget with the specified hint.
*/
GtkWidget *
koto_hint_entry_new (const char *hint)
{
return g_object_new (KOTO_TYPE_HINT_ENTRY,
"hint", hint,
NULL);
}
/**
* koto_hint_entry_clear:
* @entry: a #KotoHintEntry
*
* Clear the text in the entry and if the widget is not focused, display the
* hint text.
*/
void
koto_hint_entry_clear (KotoHintEntry *entry)
{
g_return_if_fail (KOTO_IS_HINT_ENTRY (entry));
gtk_entry_set_text (GTK_ENTRY (entry), "");
update (entry);
}
gboolean
koto_hint_entry_is_empty (KotoHintEntry *entry)
{
KotoHintEntryPrivate *priv;
g_return_val_if_fail (KOTO_IS_HINT_ENTRY (entry), TRUE);
priv = GET_PRIVATE (entry);
if (priv->state == STATE_HINTING)
return TRUE;
return strlen (gtk_entry_get_text (GTK_ENTRY (entry))) == 0;
}
|