1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <glib/gi18n-lib.h>
34 #include <gtk/gtkscrolledwindow.h>
35 #include <gtk/gtktextview.h>
36 #include <gtk/gtkimage.h>
37 #include <gtk/gtkbutton.h>
39 #include <modest-text-utils.h>
40 #include <modest-recpt-editor.h>
41 #include <modest-scroll-text.h>
42 #include <pango/pango-attributes.h>
44 #include <gdk/gdkkeysyms.h>
46 #include <modest-ui-constants.h>
48 /* FIXNE: we should have no maemo-deps in widgets/ */
49 #ifndef MODEST_TOOLKIT_GTK
50 #include "modest-hildon-includes.h"
51 #endif /*!MODEST_TOOLKIT_GTK*/
53 #define RECPT_BUTTON_WIDTH_HILDON2 118
56 static GObjectClass *parent_class = NULL;
58 #define RECIPIENT_TAG_ID "recpt-id"
62 OPEN_ADDRESSBOOK_SIGNAL,
66 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
68 struct _ModestRecptEditorPrivate
71 GtkWidget *abook_button;
72 GtkWidget *scrolled_window;
74 gulong on_mark_set_handler;
78 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o) \
79 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
81 static guint signals[LAST_SIGNAL] = {0};
83 /* static functions: GObject */
84 static void modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class);
85 static void modest_recpt_editor_finalize (GObject *object);
86 static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
89 static void modest_recpt_editor_on_abook_clicked (GtkButton *button,
90 ModestRecptEditor *editor);
91 static void modest_recpt_editor_add_tags (ModestRecptEditor *editor,
92 const gchar * recipient_id);
93 static gboolean modest_recpt_editor_on_focus_in (GtkTextView *text_view,
95 ModestRecptEditor *editor);
96 static void modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
99 ModestRecptEditor *editor);
100 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
101 GtkTextIter *location,
104 ModestRecptEditor *editor);
105 static void modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
106 GtkTextIter *location,
109 ModestRecptEditor *editor);
110 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
112 ModestRecptEditor *editor);
113 static GtkTextTag *iter_has_recipient (GtkTextIter *iter);
114 static gunichar iter_previous_char (GtkTextIter *iter);
115 /* static gunichar iter_next_char (GtkTextIter *iter); */
116 static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
117 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
118 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
119 static gboolean quote_opened (GtkTextIter *iter);
120 static gboolean is_valid_insert (const gchar *text, gint len);
121 static gchar *create_valid_text (const gchar *text, gint len);
124 * modest_recpt_editor_new:
126 * Return value: a new #ModestRecptEditor instance implemented for Gtk+
129 modest_recpt_editor_new (void)
131 ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR,
132 "homogeneous", FALSE,
133 "spacing", MODEST_MARGIN_NONE,
136 return GTK_WIDGET (self);
140 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
142 ModestRecptEditorPrivate *priv;
143 GtkTextBuffer *buffer = NULL;
144 gchar *valid_recipients = NULL;
146 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
147 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
149 #ifdef MODEST_TOOLKIT_HILDON2
150 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
152 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
155 valid_recipients = create_valid_text (recipients, -1);
156 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
157 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
158 gtk_text_buffer_set_text (buffer, valid_recipients, -1);
159 g_free (valid_recipients);
160 if (GTK_WIDGET_REALIZED (recpt_editor))
161 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
162 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
163 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
168 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
170 ModestRecptEditorPrivate *priv;
171 GtkTextBuffer *buffer = NULL;
173 gchar *string_to_add;
175 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
176 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
178 if (recipients == NULL)
181 #ifdef MODEST_TOOLKIT_HILDON2
182 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
184 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
187 if (gtk_text_buffer_get_char_count (buffer) > 0) {
188 string_to_add = g_strconcat (";\n", recipients, NULL);
190 string_to_add = g_strdup (recipients);
193 gtk_text_buffer_get_end_iter (buffer, &iter);
195 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
196 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
198 gtk_text_buffer_insert (buffer, &iter, string_to_add, -1);
200 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
201 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
203 if (GTK_WIDGET_REALIZED (recpt_editor))
204 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
206 g_free (string_to_add);
210 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
212 ModestRecptEditorPrivate *priv;
213 GtkTextBuffer *buffer = NULL;
214 GtkTextIter start, end, iter;
216 gboolean is_first_recipient = TRUE;
218 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
219 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
221 #ifdef MODEST_TOOLKIT_HILDON2
222 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
224 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
227 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
228 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
229 gtk_text_buffer_get_bounds (buffer, &start, &end);
230 if (gtk_text_buffer_get_char_count (buffer) > 0) {
231 gchar * buffer_contents;
233 gtk_text_buffer_get_bounds (buffer, &start, &end);
234 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
235 if (!g_str_has_suffix (buffer_contents, "\n")) {
236 if (g_str_has_suffix (buffer_contents, ";")||(g_str_has_suffix (buffer_contents, ",")))
237 gtk_text_buffer_insert (buffer, &end, "\n", -1);
239 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
241 g_free (buffer_contents);
244 gtk_text_buffer_get_end_iter (buffer, &iter);
246 for (node = email_list; node != NULL; node = g_slist_next (node)) {
247 gchar *recipient = (gchar *) node->data;
249 if ((recipient) && (strlen (recipient) != 0)) {
251 if (!is_first_recipient)
252 gtk_text_buffer_insert (buffer, &iter, "\n", -1);
254 gtk_text_buffer_insert (buffer, &iter, recipient, -1);
255 gtk_text_buffer_insert (buffer, &iter, ";", -1);
256 is_first_recipient = FALSE;
259 modest_recpt_editor_add_tags (recpt_editor, recipient_id);
261 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
262 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
266 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor,
267 GtkTextIter *start, GtkTextIter *end,
268 GSList *email_list, const gchar * recipient_id)
270 GSList *email_lists_list;
271 GSList *recipient_ids_list;
273 email_lists_list = g_slist_append (NULL, email_list);
274 recipient_ids_list = g_slist_append (NULL, (gpointer) recipient_id);
276 modest_recpt_editor_replace_with_resolved_recipients (recpt_editor, start, end,
277 email_lists_list, recipient_ids_list);
279 g_slist_free (email_lists_list);
280 g_slist_free (recipient_ids_list);
285 modest_recpt_editor_replace_with_resolved_recipients (ModestRecptEditor *recpt_editor,
286 GtkTextIter *start, GtkTextIter *end,
287 GSList *email_lists_list, GSList * recipient_ids_list)
289 ModestRecptEditorPrivate *priv;
290 GtkTextBuffer *buffer;
293 gboolean is_first_recipient = TRUE;
295 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
296 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
298 #ifdef MODEST_TOOLKIT_HILDON2
299 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
301 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
303 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
304 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
306 gtk_text_buffer_delete (buffer, start, end);
308 while (email_lists_list && recipient_ids_list) {
309 gchar *recipient_id = (gchar *) recipient_ids_list->data;
310 GSList *email_list = (GSList *) email_lists_list->data;
312 tag = gtk_text_buffer_create_tag (buffer, NULL,
313 "underline", PANGO_UNDERLINE_SINGLE,
314 "wrap-mode", GTK_WRAP_NONE,
315 "editable", TRUE, NULL);
317 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
318 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
320 for (node = email_list; node != NULL; node = g_slist_next (node)) {
321 gchar *recipient = (gchar *) node->data;
323 if ((recipient) && (strlen (recipient) != 0)) {
325 if (!is_first_recipient)
326 gtk_text_buffer_insert (buffer, start, "\n", -1);
328 gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
330 if (node->next != NULL)
331 gtk_text_buffer_insert (buffer, start, ";", -1);
332 is_first_recipient = FALSE;
336 email_lists_list = g_slist_next (email_lists_list);
337 recipient_ids_list = g_slist_next (recipient_ids_list);
339 /* Add a separator between lists of emails*/
340 if (recipient_ids_list)
341 gtk_text_buffer_insert (buffer, start, ";", -1);
343 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
344 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
350 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
352 ModestRecptEditorPrivate *priv;
353 GtkTextBuffer *buffer = NULL;
354 GtkTextIter start, end;
357 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
358 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
360 if (priv->recipients != NULL) {
361 g_free (priv->recipients);
362 priv->recipients = NULL;
365 #ifdef MODEST_TOOKIT_HILDON2
366 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
368 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
371 gtk_text_buffer_get_start_iter (buffer, &start);
372 gtk_text_buffer_get_end_iter (buffer, &end);
374 priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
375 for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
381 return priv->recipients;
386 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
388 ModestRecptEditorPrivate *priv;
389 GtkWidget *abook_icon;
390 GtkWidget *abook_align;
391 GtkTextBuffer *buffer;
393 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
395 priv->show_abook = TRUE;
396 priv->abook_button = gtk_button_new ();
397 gtk_widget_set_no_show_all (GTK_WIDGET (priv->abook_button), TRUE);
398 gtk_widget_show (priv->abook_button);
399 #ifdef MODEST_TOOLKIT_HILDON2
400 gtk_widget_set_size_request (priv->abook_button, RECPT_BUTTON_WIDTH_HILDON2, -1);
402 gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
404 gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
405 GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
406 gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 0.5, 0.5);
407 #ifdef MODEST_TOOLKIT_HILDON2
408 abook_icon = gtk_image_new_from_icon_name ("general_contacts", HILDON_ICON_SIZE_FINGER);
410 abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
412 gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
414 #ifdef MODEST_TOOLKIT_HILDON2
415 priv->text_view = hildon_text_view_new ();
417 priv->text_view = gtk_text_view_new ();
419 /* Auto-capitalization is the default, so let's turn it off: */
421 hildon_gtk_text_view_set_input_mode (GTK_TEXT_VIEW (priv->text_view),
422 HILDON_GTK_INPUT_MODE_FULL);
425 priv->recipients = NULL;
427 #ifdef MODEST_TOOLKIT_HILDON2
428 priv->scrolled_window = NULL;
429 gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0);
431 priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 1024);
432 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
433 GTK_POLICY_AUTOMATIC);
434 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
435 /* gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
437 gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
438 /* gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
440 abook_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
441 gtk_alignment_set_padding (GTK_ALIGNMENT (abook_align), 0, 0, MODEST_MARGIN_DEFAULT, 0);
442 gtk_container_add (GTK_CONTAINER (abook_align), priv->abook_button);
443 gtk_box_pack_end (GTK_BOX (instance), abook_align, FALSE, FALSE, 0);
445 gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
446 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
447 gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
449 gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
450 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
452 gtk_widget_set_size_request (priv->text_view, 75, -1);
454 #ifdef MODEST_TOOLKIT_HILDON2
455 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
457 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
459 g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
460 g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
461 g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
462 g_signal_connect (G_OBJECT (buffer), "insert-text",
463 G_CALLBACK (modest_recpt_editor_on_insert_text),
465 g_signal_connect_after (G_OBJECT (buffer), "insert-text",
466 G_CALLBACK (modest_recpt_editor_on_insert_text_after),
469 priv->on_mark_set_handler = g_signal_connect (G_OBJECT (buffer), "mark-set",
470 G_CALLBACK (modest_recpt_editor_on_mark_set),
477 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
479 ModestRecptEditorPrivate *priv;
481 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
482 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
483 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
485 #ifdef MODEST_TOOLKIT_HILDON2
486 gtk_size_group_add_widget (size_group, priv->text_view);
488 gtk_size_group_add_widget (size_group, priv->scrolled_window);
493 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
495 ModestRecptEditorPrivate *priv;
497 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
498 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
500 #ifdef MODEST_TOOLKIT_HILDON2
501 return hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
503 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
508 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
510 g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
512 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
516 modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
519 ModestRecptEditor *recpt_editor)
521 ModestRecptEditorPrivate *priv;
522 GtkTextIter start, end;
523 GtkTextMark *selection_bound;
525 gboolean selection_changed = FALSE;
527 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
529 buffer = modest_recpt_editor_get_buffer (recpt_editor);
530 selection_bound = gtk_text_buffer_get_selection_bound (buffer);
532 if (mark != selection_bound)
535 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
537 tag = iter_has_recipient (&start);
539 if (!gtk_text_iter_begins_tag (&start, tag)) {
540 gtk_text_iter_backward_to_tag_toggle (&start, tag);
541 selection_changed = TRUE;
544 tag = iter_has_recipient (&end);
546 if (!gtk_text_iter_ends_tag (&end, tag)) {
547 gtk_text_iter_forward_to_tag_toggle (&end, tag);
548 selection_changed = TRUE;
551 if (selection_changed) {
552 /* We block this signal handler in order to prevent a
553 stack overflow caused by recursive calls to this
554 handler as the select_range call could issue a
556 g_signal_handler_block (buffer, priv->on_mark_set_handler);
557 gtk_text_buffer_select_range (buffer, &start, &end);
558 g_signal_handler_unblock (buffer, priv->on_mark_set_handler);
563 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
564 GdkEventFocus *event,
565 ModestRecptEditor *editor)
567 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
568 gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
574 is_valid_insert (const gchar *text, gint len)
579 gboolean quoted = FALSE;
580 const gchar *current, *next_current;
585 while (((len == -1)||(i < len)) && (*current != '\0')) {
586 c = g_utf8_get_char (current);
587 next_current = g_utf8_next_char (current);
588 if (next_current && *next_current != '\0')
589 next_c = g_utf8_get_char (g_utf8_next_char (current));
592 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
593 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
596 current = g_utf8_next_char (next_current);
600 if (c == 0x2022 || c == 0xfffc ||
601 c == g_utf8_get_char ("\n") ||
602 c == g_utf8_get_char ("\t"))
604 if (c == g_utf8_get_char ("\""))
606 current = g_utf8_next_char (current);
613 create_valid_text (const gchar *text, gint len)
619 gboolean quoted = FALSE;
620 const gchar *current, *next_current;
625 str = g_string_new ("");
628 while (((len == -1)||(i < len)) && (*current != '\0')) {
629 c = g_utf8_get_char (current);
630 next_current = g_utf8_next_char (current);
631 if (next_current && *next_current != '\0')
632 next_c = g_utf8_get_char (g_utf8_next_char (current));
635 if (c != 0x2022 && c != 0xfffc &&
636 c != g_utf8_get_char ("\n") &&
637 c != g_utf8_get_char ("\t"))
638 str = g_string_append_unichar (str, c);
639 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
640 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
641 str = g_string_append_c (str, '\n');
643 if (c == g_utf8_get_char ("\""))
645 current = g_utf8_next_char (current);
648 return g_string_free (str, FALSE);
651 /* Called after the default handler, and thus after the text was
652 inserted. We use this to insert a break after a ',' or a ';'*/
654 modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
655 GtkTextIter *location,
658 ModestRecptEditor *editor)
662 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
665 if (!gtk_text_iter_backward_char (&prev))
668 prev_char = gtk_text_iter_get_char (&prev);
669 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
670 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
671 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
673 gtk_text_buffer_insert (buffer, location, "\n",-1);
674 insert = gtk_text_buffer_get_insert (buffer);
675 gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
677 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
678 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
681 /* Called before the default handler, we use it to validate the inputs */
683 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
684 GtkTextIter *location,
687 ModestRecptEditor *editor)
692 if (!is_valid_insert (text, len)) {
693 gchar *new_text = create_valid_text (text, len);
694 g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
695 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
696 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
697 gtk_text_buffer_insert (buffer, location, new_text, -1);
698 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
699 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
704 if (iter_has_recipient (location)) {
705 gtk_text_buffer_get_end_iter (buffer, location);
706 gtk_text_buffer_place_cursor (buffer, location);
711 iter_has_recipient (GtkTextIter *iter)
714 GtkTextTag *result = NULL;
716 tags = gtk_text_iter_get_tags (iter);
718 for (node = tags; node != NULL; node = g_slist_next (node)) {
719 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
721 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
731 prev_iter_has_recipient (GtkTextIter *iter)
736 gtk_text_iter_backward_char (&prev);
737 return iter_has_recipient (&prev);
740 /* static GtkTextTag * */
741 /* next_iter_has_recipient (GtkTextIter *iter) */
743 /* GtkTextIter next; */
746 /* return iter_has_recipient (&next); */
750 iter_previous_char (GtkTextIter *iter)
755 gtk_text_iter_backward_char (&prev);
756 return gtk_text_iter_get_char (&prev);
759 /* static gunichar */
760 /* iter_next_char (GtkTextIter *iter) */
762 /* GtkTextIter next; */
765 /* gtk_text_iter_forward_char (&next); */
766 /* return gtk_text_iter_get_char (&next); */
770 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
772 GtkTextIter start, end;
775 if (!gtk_text_iter_begins_tag (&start, tag))
776 gtk_text_iter_backward_to_tag_toggle (&start, tag);
778 if (!gtk_text_iter_ends_tag (&end, tag))
779 gtk_text_iter_forward_to_tag_toggle (&end, tag);
780 gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
784 quote_opened (GtkTextIter *iter)
787 GtkTextBuffer *buffer;
788 gboolean opened = FALSE;
790 buffer = gtk_text_iter_get_buffer (iter);
791 gtk_text_buffer_get_start_iter (buffer, &start);
793 while (!gtk_text_iter_equal (&start, iter)) {
794 gunichar current_char = gtk_text_iter_get_char (&start);
795 if (current_char == '"')
797 else if (current_char == '\\')
798 gtk_text_iter_forward_char (&start);
799 if (!gtk_text_iter_equal (&start, iter))
800 gtk_text_iter_forward_char (&start);
809 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
811 ModestRecptEditor *editor)
814 GtkTextMark *selection;
815 GtkTextBuffer * buffer;
816 GtkTextIter location, selection_loc;
819 #ifdef MODEST_TOOLKIT_HILDON2
820 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (text_view));
822 buffer = gtk_text_view_get_buffer (text_view);
824 insert = gtk_text_buffer_get_insert (buffer);
825 selection = gtk_text_buffer_get_selection_bound (buffer);
828 * * cursor is on resolved recipient:
829 * - right should go to first character after the recipient (usually ; or ,)
830 * - left should fo to the first character before the recipient
831 * - return should run check names on the recipient.
832 * * cursor is just after a recipient:
833 * - right should go to the next character. If it's a recipient, should select
835 * - left should go to the previous character. If it's a recipient, should go
836 * to the first character of the recipient, and select it.
837 * * cursor is on arbitrary text:
838 * - return should add a ; and go to the next line
839 * - left or right standard ones.
840 * * cursor is after a \n:
841 * - left should go to the character before the \n (as if \n was not a character)
842 * * cursor is before a \n:
843 * - right should go to the character after the \n
846 gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
847 gtk_text_buffer_get_iter_at_mark (buffer, &selection_loc, selection);
849 switch (key->keyval) {
853 gboolean cursor_ready = FALSE;
854 while (!cursor_ready) {
855 if (iter_previous_char (&location) == '\n') {
856 gtk_text_iter_backward_char (&location);
861 tag = iter_has_recipient (&location);
862 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
863 select_tag_of_iter (&location, tag);
864 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
867 gtk_text_iter_backward_char (&location);
868 tag = iter_has_recipient (&location);
870 select_tag_of_iter (&location, tag);
872 gtk_text_buffer_place_cursor (buffer, &location);
874 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
882 gboolean cursor_moved = FALSE;
884 tag = iter_has_recipient (&location);
885 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
886 gtk_text_iter_forward_to_tag_toggle (&location, tag);
887 while (gtk_text_iter_get_char (&location) == '\n')
888 gtk_text_iter_forward_char (&location);
889 gtk_text_buffer_place_cursor (buffer, &location);
890 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
894 while (gtk_text_iter_get_char (&location) == '\n') {
895 gtk_text_iter_forward_char (&location);
899 gtk_text_iter_forward_char (&location);
900 while (gtk_text_iter_get_char (&location) == '\n') {
901 gtk_text_iter_forward_char (&location);
905 tag = iter_has_recipient (&location);
907 select_tag_of_iter (&location, tag);
909 gtk_text_buffer_place_cursor (buffer, &location);
910 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
917 gint insert_offset, selection_offset;
918 insert_offset = gtk_text_iter_get_offset (&location);
919 selection_offset = gtk_text_iter_get_offset (&selection_loc);
920 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
921 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
922 if (selection_offset > insert_offset)
923 location = selection_loc;
924 tag = iter_has_recipient (&location);
926 gtk_text_buffer_get_end_iter (buffer, &location);
927 gtk_text_buffer_place_cursor (buffer, &location);
928 if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
929 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
930 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
932 gunichar prev_char = iter_previous_char (&location);
933 if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
934 ||(prev_char == ';')||(prev_char == ','))
935 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
937 if ((prev_char != ';') && (prev_char != ','))
938 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
939 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
942 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
943 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
949 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
950 if (gtk_text_buffer_get_has_selection (buffer)) {
951 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
955 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
956 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
961 tag = prev_iter_has_recipient (&location);
963 GtkTextIter iter_in_tag;
964 iter_in_tag = location;
965 gtk_text_iter_backward_char (&iter_in_tag);
966 select_tag_of_iter (&iter_in_tag, tag);
967 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
978 /* NOTE: before calling this function be sure that both
979 modest_recpt_editor_on_insert_text and
980 modest_recpt_editor_on_insert_text_after won't be triggered during
981 the execution of the procedure. You'll have to block both signal
982 handlers otherwise you'll get an infinite loop and most likely a
983 SIGSEV caused by a stack overflow */
985 modest_recpt_editor_add_tags (ModestRecptEditor *editor,
986 const gchar * recipient_id)
989 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
990 #ifdef MODEST_TOOLKIT_HILDON2
991 GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
993 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
996 GtkTextIter start, end;
997 gchar * buffer_contents;
998 GtkTextIter start_match, end_match;
1000 /* This would move the cursor to the end of the buffer
1001 containing new line character. */
1002 gtk_text_buffer_get_bounds (buffer, &start, &end);
1003 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1004 gtk_text_buffer_set_text(buffer,buffer_contents,strlen(buffer_contents));
1006 tag = gtk_text_buffer_create_tag (buffer, NULL,
1007 "underline", PANGO_UNDERLINE_SINGLE,
1008 "wrap-mode", GTK_WRAP_NONE,
1009 "editable", TRUE, NULL);
1011 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
1012 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
1014 /* Formatting the buffer content by applying tag */
1015 gtk_text_buffer_get_bounds (buffer, &start, &end);
1016 while (gtk_text_iter_forward_search(&start, ";",
1017 GTK_TEXT_SEARCH_TEXT_ONLY |
1018 GTK_TEXT_SEARCH_VISIBLE_ONLY,
1019 &start_match, &end_match, &end )) {
1022 gtk_text_buffer_apply_tag(buffer, tag, &start, &start_match);
1023 offset = gtk_text_iter_get_offset (&end_match);
1024 gtk_text_buffer_get_iter_at_offset(buffer, &start, offset);
1026 g_free (buffer_contents);
1030 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
1032 ModestRecptEditorPrivate *priv;
1034 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
1035 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1037 if (GTK_WIDGET_VISIBLE (recpt_editor)) {
1038 gtk_widget_grab_focus (priv->text_view);
1043 modest_recpt_editor_has_focus (ModestRecptEditor *recpt_editor)
1045 ModestRecptEditorPrivate *priv;
1047 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
1048 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1050 return GTK_WIDGET_VISIBLE (priv->text_view) &&
1051 gtk_widget_is_focus (priv->text_view);
1055 modest_recpt_editor_set_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
1057 ModestRecptEditorPrivate *priv;
1059 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
1060 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1062 priv->show_abook = show;
1065 gtk_widget_show (priv->abook_button);
1067 gtk_widget_hide (priv->abook_button);
1071 modest_recpt_editor_get_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
1073 ModestRecptEditorPrivate *priv;
1075 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
1076 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1078 return priv->show_abook;
1083 modest_recpt_editor_finalize (GObject *object)
1085 ModestRecptEditorPrivate *priv;
1086 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
1088 if (g_signal_handler_is_connected (object, priv->on_mark_set_handler))
1089 g_signal_handler_disconnect (object, priv->on_mark_set_handler);
1090 priv->on_mark_set_handler = 0;
1092 if (priv->recipients) {
1093 g_free (priv->recipients);
1094 priv->recipients = NULL;
1097 (*parent_class->finalize) (object);
1103 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
1105 GObjectClass *object_class;
1107 parent_class = g_type_class_peek_parent (klass);
1108 object_class = (GObjectClass*) klass;
1110 object_class->finalize = modest_recpt_editor_finalize;
1112 g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
1114 signals[OPEN_ADDRESSBOOK_SIGNAL] =
1115 g_signal_new ("open-addressbook",
1116 G_TYPE_FROM_CLASS (object_class),
1117 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
1118 G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
1120 g_cclosure_marshal_VOID__VOID,
1127 modest_recpt_editor_get_type (void)
1129 static GType type = 0;
1131 if (G_UNLIKELY(type == 0))
1133 static const GTypeInfo info =
1135 sizeof (ModestRecptEditorClass),
1136 NULL, /* base_init */
1137 NULL, /* base_finalize */
1138 (GClassInitFunc) modest_recpt_editor_class_init, /* class_init */
1139 NULL, /* class_finalize */
1140 NULL, /* class_data */
1141 sizeof (ModestRecptEditor),
1142 0, /* n_preallocs */
1143 modest_recpt_editor_instance_init /* instance_init */
1146 type = g_type_register_static (GTK_TYPE_HBOX,
1147 "ModestRecptEditor",