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 EasySetupValidatingEntryFunc func;
33 gpointer func_user_data;
35 EasySetupValidatingEntryMaxFunc max_func;
36 gpointer max_func_user_data;
40 modest_validating_entry_get_property (GObject *object, guint property_id,
41 GValue *value, GParamSpec *pspec)
43 switch (property_id) {
45 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
50 modest_validating_entry_set_property (GObject *object, guint property_id,
51 const GValue *value, GParamSpec *pspec)
53 switch (property_id) {
55 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
60 modest_validating_entry_dispose (GObject *object)
62 if (G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose)
63 G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose (object);
67 modest_validating_entry_finalize (GObject *object)
69 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (object);
71 /* Free the list and its items: */
72 if (priv->list_prevent) {
73 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
74 g_list_free (priv->list_prevent);
77 G_OBJECT_CLASS (modest_validating_entry_parent_class)->finalize (object);
81 modest_validating_entry_class_init (ModestValidatingEntryClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 g_type_class_add_private (klass, sizeof (ModestValidatingEntryPrivate));
87 object_class->get_property = modest_validating_entry_get_property;
88 object_class->set_property = modest_validating_entry_set_property;
89 object_class->dispose = modest_validating_entry_dispose;
90 object_class->finalize = modest_validating_entry_finalize;
94 on_list_compare(gconstpointer a, gconstpointer b)
96 gunichar* unichar_a = (gunichar*)(a);
97 gunichar* unichar_b = (gunichar*)(b);
98 if(*unichar_a == *unichar_b)
101 return -1; /* Really, we should return > and <, but we don't use this for sorting. */
105 on_insert_text(GtkEditable *editable,
106 gchar *new_text, gint new_text_length,
110 ModestValidatingEntry *self = MODEST_VALIDATING_ENTRY (user_data);
111 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
116 /* Note: new_text_length is documented as the number of bytes, not characters. */
117 if(!g_utf8_validate (new_text, new_text_length, NULL))
120 /* Look at each UTF-8 character in the text (it could be several via a drop or a paste),
122 gboolean allow = TRUE;
123 gchar *iter = new_text; /* new_text seems to be NULL-terminated, though that is not documented. */
126 if(priv->list_prevent) {
127 /* If the character is in our prevent list,
128 * then do not allow this text to be entered.
130 * This prevents entry of all text, without removing the unwanted characters.
131 * It is debatable whether that is the best thing to do.
133 gunichar one_char = g_utf8_get_char (iter);
134 GList *found = g_list_find_custom(priv->list_prevent, &one_char, &on_list_compare);
139 priv->func(self, iter, priv->func_user_data);
145 if(priv->prevent_whitespace) {
146 /* Check for whitespace characters: */
147 gunichar one_char = g_utf8_get_char (iter);
148 if (g_unichar_isspace (one_char)) {
152 priv->func(self, NULL, priv->func_user_data);
158 /* Crashes. Don't know why: iter = g_utf8_next_char (iter);
159 * Maybe it doesn't check for null-termination. */
160 iter = g_utf8_find_next_char (iter, new_text + new_text_length);
163 /* Prevent more than the max characters.
164 * The regular GtkEntry does this already, but we also want to call a specified callback,
165 * so that the application can show a warning dialog. */
167 const gint max_num = gtk_entry_get_max_length (GTK_ENTRY (self));
169 const gchar *existing_text = gtk_entry_get_text (GTK_ENTRY(self));
170 const gint existing_length = existing_text ? g_utf8_strlen (existing_text, -1) : 0;
171 const gint new_length_chars = g_utf8_strlen (new_text, new_text_length);
173 if ((existing_length + new_length_chars) > max_num) {
174 priv->max_func (self, priv->max_func_user_data);
175 /* We shouldn't need to stop the signal because the underlying code will check too.
176 * Well, that would maybe be a performance optimization,
177 * but it's generally safer not to interfere too much. */
183 /* The signal documentation says
184 * "by connecting to this signal and then stopping the signal with
185 * gtk_signal_emit_stop(), it is possible to modify the inserted text,
186 * or prevent it from being inserted entirely."
188 gtk_signal_emit_stop_by_name (GTK_OBJECT (self), "insert-text");
194 modest_validating_entry_init (ModestValidatingEntry *self)
196 /* Connect to the GtkEditable::insert-text signal
197 * so we can filter out some characters:
198 * We connect _before_ so we can stop the default signal handler from running.
200 g_signal_connect (G_OBJECT (self), "insert-text", (GCallback)&on_insert_text, self);
203 ModestValidatingEntry*
204 modest_validating_entry_new (void)
206 return g_object_new (MODEST_TYPE_VALIDATING_ENTRY, NULL);
209 /** Specify characters that may not be entered into this GtkEntry.
211 * list: A list of gchar* strings. Each one identifies a UTF-8 character.
213 void modest_validating_entry_set_unallowed_characters (ModestValidatingEntry *self, GList *list)
215 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
217 /* Free the list and its items: */
218 if (priv->list_prevent) {
219 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
220 g_list_free (priv->list_prevent);
223 /* Do a deep copy of the list, converting gchar* to gunichar: */
224 priv->list_prevent = NULL;
226 for (iter = list; iter != NULL; iter = iter->next) {
227 gunichar *one_char = g_new0 (gunichar, 1);
229 *one_char = g_utf8_get_char ((gchar*)iter->data);
233 priv->list_prevent = g_list_append (priv->list_prevent, one_char);
237 /** Specify that no whitespace characters may be entered into this GtkEntry.
240 void modest_validating_entry_set_unallowed_characters_whitespace (ModestValidatingEntry *self)
242 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
243 priv->prevent_whitespace = TRUE;
246 /** Set a callback to be called when the maximum number of characters have been entered.
247 * This may be used to show an informative dialog.
249 void modest_validating_entry_set_max_func (ModestValidatingEntry *self, EasySetupValidatingEntryMaxFunc func, gpointer user_data)
251 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
252 priv->max_func = func;
253 priv->max_func_user_data = user_data;
256 /** Set a callback to be called when a character was prevented so that a
257 * note can be shown by the application to inform the user. For whitespaces,
258 * character will be NULL
260 void modest_validating_entry_set_func (ModestValidatingEntry *self, EasySetupValidatingEntryFunc func, gpointer user_data)
262 ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
264 priv->func_user_data = user_data;