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 (ModestValidatingEntry, modest_validating_entry, GTK_TYPE_ENTRY);
17 #define VALIDATING_ENTRY_GET_PRIVATE(o) \
18 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_VALIDATING_ENTRY, ModestValidatingEntryPrivate))
20 typedef struct _ModestValidatingEntryPrivate ModestValidatingEntryPrivate;
22 struct _ModestValidatingEntryPrivate
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;
32 EasySetupValidatingEntryMaxFunc max_func;
33 gpointer max_func_user_data;
37 modest_validating_entry_get_property (GObject *object, guint property_id,
38 GValue *value, GParamSpec *pspec)
40 switch (property_id) {
42 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
47 modest_validating_entry_set_property (GObject *object, guint property_id,
48 const GValue *value, GParamSpec *pspec)
50 switch (property_id) {
52 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
57 modest_validating_entry_dispose (GObject *object)
59 if (G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose)
60 G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose (object);
64 modest_validating_entry_finalize (GObject *object)
66 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (object);
68 /* Free the list and its items: */
69 if (priv->list_prevent) {
70 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
71 g_list_free (priv->list_prevent);
74 G_OBJECT_CLASS (modest_validating_entry_parent_class)->finalize (object);
78 modest_validating_entry_class_init (ModestValidatingEntryClass *klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 g_type_class_add_private (klass, sizeof (ModestValidatingEntryPrivate));
84 object_class->get_property = modest_validating_entry_get_property;
85 object_class->set_property = modest_validating_entry_set_property;
86 object_class->dispose = modest_validating_entry_dispose;
87 object_class->finalize = modest_validating_entry_finalize;
91 on_list_compare(gconstpointer a, gconstpointer b)
93 gunichar* unichar_a = (gunichar*)(a);
94 gunichar* unichar_b = (gunichar*)(b);
95 if(*unichar_a == *unichar_b)
98 return -1; /* Really, we should return > and <, but we don't use this for sorting. */
102 on_insert_text(GtkEditable *editable,
103 gchar *new_text, gint new_text_length,
107 ModestValidatingEntry *self = MODEST_VALIDATING_ENTRY (user_data);
108 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
113 /* Note: new_text_length is documented as the number of bytes, not characters. */
114 if(!g_utf8_validate (new_text, new_text_length, NULL))
117 /* Look at each UTF-8 character in the text (it could be several via a drop or a paste),
119 gboolean allow = TRUE;
120 gchar *iter = new_text; /* new_text seems to be NULL-terminated, though that is not documented. */
123 if(priv->list_prevent) {
124 /* If the character is in our prevent list,
125 * then do not allow this text to be entered.
127 * This prevents entry of all text, without removing the unwanted characters.
128 * It is debatable whether that is the best thing to do.
130 gunichar one_char = g_utf8_get_char (iter);
131 GList *found = g_list_find_custom(priv->list_prevent, &one_char, &on_list_compare);
138 if(priv->prevent_whitespace) {
139 /* Check for whitespace characters: */
140 gunichar one_char = g_utf8_get_char (iter);
141 if (g_unichar_isspace (one_char)) {
147 /* Crashes. Don't know why: iter = g_utf8_next_char (iter);
148 * Maybe it doesn't check for null-termination. */
149 iter = g_utf8_find_next_char (iter, new_text + new_text_length);
152 /* Prevent more than the max characters.
153 * The regular GtkEntry does this already, but we also want to call a specified callback,
154 * so that the application can show a warning dialog. */
156 const gint max_num = gtk_entry_get_max_length (GTK_ENTRY (self));
158 const gchar *existing_text = gtk_entry_get_text (GTK_ENTRY(self));
159 const gint existing_length = existing_text ? g_utf8_strlen (existing_text, -1) : 0;
160 const gint new_length_chars = g_utf8_strlen (new_text, new_text_length);
162 if ((existing_length + new_length_chars) > max_num) {
163 priv->max_func (self, priv->max_func_user_data);
164 /* We shouldn't need to stop the signal because the underlying code will check too.
165 * Well, that would maybe be a performance optimization,
166 * but it's generally safer not to interfere too much. */
172 /* The signal documentation says
173 * "by connecting to this signal and then stopping the signal with
174 * gtk_signal_emit_stop(), it is possible to modify the inserted text,
175 * or prevent it from being inserted entirely."
177 gtk_signal_emit_stop_by_name (GTK_OBJECT (self), "insert-text");
183 modest_validating_entry_init (ModestValidatingEntry *self)
185 /* Connect to the GtkEditable::insert-text signal
186 * so we can filter out some characters:
187 * We connect _before_ so we can stop the default signal handler from running.
189 g_signal_connect (G_OBJECT (self), "insert-text", (GCallback)&on_insert_text, self);
192 ModestValidatingEntry*
193 modest_validating_entry_new (void)
195 return g_object_new (MODEST_TYPE_VALIDATING_ENTRY, NULL);
198 /** Specify characters that may not be entered into this GtkEntry.
200 * list: A list of gchar* strings. Each one identifies a UTF-8 character.
202 void modest_validating_entry_set_unallowed_characters (ModestValidatingEntry *self, GList *list)
204 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
206 /* Free the list and its items: */
207 if (priv->list_prevent) {
208 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
209 g_list_free (priv->list_prevent);
212 /* Do a deep copy of the list, converting gchar* to gunichar: */
213 priv->list_prevent = NULL;
215 for (iter = list; iter != NULL; iter = iter->next) {
216 gunichar *one_char = g_new0 (gunichar, 1);
218 *one_char = g_utf8_get_char ((gchar*)iter->data);
222 priv->list_prevent = g_list_append (priv->list_prevent, one_char);
226 /** Specify that no whitespace characters may be entered into this GtkEntry.
229 void modest_validating_entry_set_unallowed_characters_whitespace (ModestValidatingEntry *self)
231 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
232 priv->prevent_whitespace = TRUE;
235 /** Set a callback to be called when the maximum number of characters have been entered.
236 * This may be used to show an informative dialog.
238 void modest_validating_entry_set_max_func (ModestValidatingEntry *self, EasySetupValidatingEntryMaxFunc func, gpointer user_data)
240 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
241 priv->max_func = func;
242 priv->max_func_user_data = user_data;