1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-validating-entry.h"
7 #include <gtk/gtksignal.h> /* For the gtk_signal_stop_emit_by_name() convenience function. */
8 #include <string.h> /* For strlen(). */
10 /* Include config.h so that _() works: */
15 G_DEFINE_TYPE (EasysetupValidatingEntry, easysetup_validating_entry, GTK_TYPE_ENTRY);
17 #define VALIDATING_ENTRY_GET_PRIVATE(o) \
18 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_VALIDATING_ENTRY, EasysetupValidatingEntryPrivate))
20 typedef struct _EasysetupValidatingEntryPrivate EasysetupValidatingEntryPrivate;
22 struct _EasysetupValidatingEntryPrivate
24 /* A list of gunichar, rather than char*,
25 * because gunichar is easier to deal with internally,
26 * but gchar* is easier to supply from the external interface.
30 gboolean prevent_whitespace;
34 easysetup_validating_entry_get_property (GObject *object, guint property_id,
35 GValue *value, GParamSpec *pspec)
37 switch (property_id) {
39 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
44 easysetup_validating_entry_set_property (GObject *object, guint property_id,
45 const GValue *value, GParamSpec *pspec)
47 switch (property_id) {
49 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
54 easysetup_validating_entry_dispose (GObject *object)
56 if (G_OBJECT_CLASS (easysetup_validating_entry_parent_class)->dispose)
57 G_OBJECT_CLASS (easysetup_validating_entry_parent_class)->dispose (object);
61 easysetup_validating_entry_finalize (GObject *object)
63 EasysetupValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (object);
65 /* Free the list and its items: */
66 if (priv->list_prevent) {
67 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
68 g_list_free (priv->list_prevent);
71 G_OBJECT_CLASS (easysetup_validating_entry_parent_class)->finalize (object);
75 easysetup_validating_entry_class_init (EasysetupValidatingEntryClass *klass)
77 GObjectClass *object_class = G_OBJECT_CLASS (klass);
79 g_type_class_add_private (klass, sizeof (EasysetupValidatingEntryPrivate));
81 object_class->get_property = easysetup_validating_entry_get_property;
82 object_class->set_property = easysetup_validating_entry_set_property;
83 object_class->dispose = easysetup_validating_entry_dispose;
84 object_class->finalize = easysetup_validating_entry_finalize;
88 on_list_compare(gconstpointer a, gconstpointer b)
90 gunichar* unichar_a = (gunichar*)(a);
91 gunichar* unichar_b = (gunichar*)(b);
92 if(*unichar_a == *unichar_b)
95 return -1; /* Really, we should return > and <, but we don't use this for sorting. */
99 on_insert_text(GtkEditable *editable,
100 gchar *new_text, gint new_text_length,
104 EasysetupValidatingEntry *self = EASYSETUP_VALIDATING_ENTRY (user_data);
105 EasysetupValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
110 /* Note: new_text_length is documented as the number of bytes, not characters. */
111 if(!g_utf8_validate (new_text, new_text_length, NULL))
114 /* Look at each UTF-8 character in the text (it could be several via a drop or a paste),
116 gboolean allow = TRUE;
117 gchar *iter = new_text; /* new_text seems to be NULL-terminated, though that is not documented. */
120 if(priv->list_prevent) {
121 /* If the character is in our prevent list,
122 * then do not allow this text to be entered.
124 * This prevents entry of all text, without removing the unwanted characters.
125 * It is debatable whether that is the best thing to do.
127 gunichar one_char = g_utf8_get_char (iter);
128 GList *found = g_list_find_custom(priv->list_prevent, &one_char, &on_list_compare);
135 if(priv->prevent_whitespace) {
136 /* Check for whitespace characters: */
137 gunichar one_char = g_utf8_get_char (iter);
138 if (g_unichar_isspace (one_char)) {
144 /* Crashes. Don't know why: iter = g_utf8_next_char (iter);
145 * Maybe it doesn't check for null-termination. */
146 iter = g_utf8_find_next_char (iter, new_text + new_text_length);
150 /* The signal documentation says
151 * "by connecting to this signal and then stopping the signal with
152 * gtk_signal_emit_stop(), it is possible to modify the inserted text,
153 * or prevent it from being inserted entirely."
155 gtk_signal_emit_stop_by_name (GTK_OBJECT (self), "insert-text");
160 easysetup_validating_entry_init (EasysetupValidatingEntry *self)
162 /* Connect to the GtkEditable::insert-text signal
163 * so we can filter out some characters:
164 * We connect _before_ so we can stop the default signal handler from running.
166 g_signal_connect (G_OBJECT (self), "insert-text", (GCallback)&on_insert_text, self);
169 EasysetupValidatingEntry*
170 easysetup_validating_entry_new (void)
172 return g_object_new (EASYSETUP_TYPE_VALIDATING_ENTRY, NULL);
175 /** Specify characters that may not be entered into this GtkEntry.
177 * list: A list of gchar* strings. Each one identifies a UTF-8 character.
179 void easysetup_validating_entry_set_unallowed_characters (EasysetupValidatingEntry *self, GList *list)
181 EasysetupValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
183 /* Free the list and its items: */
184 if (priv->list_prevent) {
185 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
186 g_list_free (priv->list_prevent);
189 /* Do a deep copy of the list, converting gchar* to gunichar: */
190 priv->list_prevent = NULL;
192 for (iter = list; iter != NULL; iter = iter->next) {
193 gunichar *one_char = g_new0 (gunichar, 1);
195 *one_char = g_utf8_get_char ((gchar*)iter->data);
199 priv->list_prevent = g_list_append (priv->list_prevent, one_char);
203 /** Specify that no whitespace characters may be entered into this GtkEntry.
206 void easysetup_validating_entry_set_unallowed_characters_whitespace (EasysetupValidatingEntry *self)
208 EasysetupValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
209 priv->prevent_whitespace = TRUE;