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_move_cursor_to_end (ModestRecptEditor *editor);
92 static gboolean modest_recpt_editor_on_focus_in (GtkTextView *text_view,
94 ModestRecptEditor *editor);
95 static void modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
98 ModestRecptEditor *editor);
99 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
100 GtkTextIter *location,
103 ModestRecptEditor *editor);
104 static void modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
105 GtkTextIter *location,
108 ModestRecptEditor *editor);
109 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
111 ModestRecptEditor *editor);
112 static GtkTextTag *iter_has_recipient (GtkTextIter *iter);
113 static gunichar iter_previous_char (GtkTextIter *iter);
114 /* static gunichar iter_next_char (GtkTextIter *iter); */
115 static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
116 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
117 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
118 static gboolean quote_opened (GtkTextIter *iter);
119 static gboolean is_valid_insert (const gchar *text, gint len);
120 static gchar *create_valid_text (const gchar *text, gint len);
123 * modest_recpt_editor_new:
125 * Return value: a new #ModestRecptEditor instance implemented for Gtk+
128 modest_recpt_editor_new (void)
130 ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR,
131 "homogeneous", FALSE,
132 "spacing", MODEST_MARGIN_NONE,
135 return GTK_WIDGET (self);
139 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
141 ModestRecptEditorPrivate *priv;
142 GtkTextBuffer *buffer = NULL;
143 gchar *valid_recipients = NULL;
145 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
146 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
148 #ifdef MODEST_TOOLKIT_HILDON2
149 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
151 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
154 valid_recipients = create_valid_text (recipients, -1);
155 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
156 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
157 gtk_text_buffer_set_text (buffer, valid_recipients, -1);
158 g_free (valid_recipients);
159 if (GTK_WIDGET_REALIZED (recpt_editor))
160 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
161 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
162 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
167 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
169 ModestRecptEditorPrivate *priv;
170 GtkTextBuffer *buffer = NULL;
172 gchar *string_to_add;
174 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
175 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
177 if (recipients == NULL)
180 #ifdef MODEST_TOOLKIT_HILDON2
181 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
183 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
186 if (gtk_text_buffer_get_char_count (buffer) > 0) {
187 string_to_add = g_strconcat (";\n", recipients, NULL);
189 string_to_add = g_strdup (recipients);
192 gtk_text_buffer_get_end_iter (buffer, &iter);
194 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
195 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
197 gtk_text_buffer_insert (buffer, &iter, string_to_add, -1);
199 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
200 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
202 if (GTK_WIDGET_REALIZED (recpt_editor))
203 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
205 g_free (string_to_add);
209 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
211 ModestRecptEditorPrivate *priv;
212 GtkTextBuffer *buffer = NULL;
213 GtkTextIter start, end, iter;
214 GtkTextTag *tag = NULL;
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 tag = gtk_text_buffer_create_tag (buffer, NULL,
247 "underline", PANGO_UNDERLINE_SINGLE,
248 "wrap-mode", GTK_WRAP_NONE,
249 "editable", TRUE, NULL);
251 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
252 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
254 for (node = email_list; node != NULL; node = g_slist_next (node)) {
255 gchar *recipient = (gchar *) node->data;
257 if ((recipient) && (strlen (recipient) != 0)) {
259 if (!is_first_recipient)
260 gtk_text_buffer_insert (buffer, &iter, "\n", -1);
262 gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
263 gtk_text_buffer_insert (buffer, &iter, ";", -1);
264 is_first_recipient = FALSE;
267 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
268 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
270 modest_recpt_editor_move_cursor_to_end (recpt_editor);
275 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor,
276 GtkTextIter *start, GtkTextIter *end,
277 GSList *email_list, const gchar * recipient_id)
279 GSList *email_lists_list;
280 GSList *recipient_ids_list;
282 email_lists_list = g_slist_append (NULL, email_list);
283 recipient_ids_list = g_slist_append (NULL, (gpointer) recipient_id);
285 modest_recpt_editor_replace_with_resolved_recipients (recpt_editor, start, end,
286 email_lists_list, recipient_ids_list);
288 g_slist_free (email_lists_list);
289 g_slist_free (recipient_ids_list);
294 modest_recpt_editor_replace_with_resolved_recipients (ModestRecptEditor *recpt_editor,
295 GtkTextIter *start, GtkTextIter *end,
296 GSList *email_lists_list, GSList * recipient_ids_list)
298 ModestRecptEditorPrivate *priv;
299 GtkTextBuffer *buffer;
302 gboolean is_first_recipient = TRUE;
304 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
305 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
307 #ifdef MODEST_TOOLKIT_HILDON2
308 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
310 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
312 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
313 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
315 gtk_text_buffer_delete (buffer, start, end);
317 while (email_lists_list) {
318 gchar *recipient_id = (gchar *) recipient_ids_list->data;
319 GSList *email_list = (GSList *) email_lists_list->data;
321 tag = gtk_text_buffer_create_tag (buffer, NULL,
322 "underline", PANGO_UNDERLINE_SINGLE,
323 "wrap-mode", GTK_WRAP_NONE,
324 "editable", TRUE, NULL);
326 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
327 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
329 for (node = email_list; node != NULL; node = g_slist_next (node)) {
330 gchar *recipient = (gchar *) node->data;
332 if ((recipient) && (strlen (recipient) != 0)) {
334 if (!is_first_recipient)
335 gtk_text_buffer_insert (buffer, start, "\n", -1);
337 gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
339 if (node->next != NULL)
340 gtk_text_buffer_insert (buffer, start, ";", -1);
341 is_first_recipient = FALSE;
345 email_lists_list = g_slist_next (email_lists_list);
346 recipient_ids_list = g_slist_next (recipient_ids_list);
348 /* Add a separator between lists of emails*/
349 if (recipient_ids_list)
350 gtk_text_buffer_insert (buffer, start, ";", -1);
352 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
353 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
359 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
361 ModestRecptEditorPrivate *priv;
362 GtkTextBuffer *buffer = NULL;
363 GtkTextIter start, end;
366 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
367 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
369 if (priv->recipients != NULL) {
370 g_free (priv->recipients);
371 priv->recipients = NULL;
374 #ifdef MODEST_TOOKIT_HILDON2
375 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
377 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
380 gtk_text_buffer_get_start_iter (buffer, &start);
381 gtk_text_buffer_get_end_iter (buffer, &end);
383 priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
384 for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
390 return priv->recipients;
395 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
397 ModestRecptEditorPrivate *priv;
398 GtkWidget *abook_icon;
399 GtkWidget *abook_align;
400 GtkTextBuffer *buffer;
402 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
404 priv->show_abook = TRUE;
405 priv->abook_button = gtk_button_new ();
406 gtk_widget_set_no_show_all (GTK_WIDGET (priv->abook_button), TRUE);
407 gtk_widget_show (priv->abook_button);
408 #ifdef MODEST_TOOLKIT_HILDON2
409 gtk_widget_set_size_request (priv->abook_button, RECPT_BUTTON_WIDTH_HILDON2, -1);
411 gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
413 gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
414 GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
415 gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 0.5, 0.5);
416 #ifdef MODEST_TOOLKIT_HILDON2
417 abook_icon = gtk_image_new_from_icon_name ("general_contacts", HILDON_ICON_SIZE_FINGER);
419 abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
421 gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
423 #ifdef MODEST_TOOLKIT_HILDON2
424 priv->text_view = hildon_text_view_new ();
426 priv->text_view = gtk_text_view_new ();
428 /* Auto-capitalization is the default, so let's turn it off: */
430 hildon_gtk_text_view_set_input_mode (GTK_TEXT_VIEW (priv->text_view),
431 HILDON_GTK_INPUT_MODE_FULL);
434 priv->recipients = NULL;
436 #ifdef MODEST_TOOLKIT_HILDON2
437 priv->scrolled_window = NULL;
438 gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0);
440 priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 1024);
441 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
442 GTK_POLICY_AUTOMATIC);
443 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
444 /* gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
446 gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
447 /* gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
449 abook_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
450 gtk_alignment_set_padding (GTK_ALIGNMENT (abook_align), 0, 0, MODEST_MARGIN_DEFAULT, 0);
451 gtk_container_add (GTK_CONTAINER (abook_align), priv->abook_button);
452 gtk_box_pack_end (GTK_BOX (instance), abook_align, FALSE, FALSE, 0);
454 gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
455 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
456 gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
458 gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
459 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
461 gtk_widget_set_size_request (priv->text_view, 75, -1);
463 #ifdef MODEST_TOOLKIT_HILDON2
464 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
466 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
468 g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
469 g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
470 g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
471 g_signal_connect (G_OBJECT (buffer), "insert-text",
472 G_CALLBACK (modest_recpt_editor_on_insert_text),
474 g_signal_connect_after (G_OBJECT (buffer), "insert-text",
475 G_CALLBACK (modest_recpt_editor_on_insert_text_after),
478 priv->on_mark_set_handler = g_signal_connect (G_OBJECT (buffer), "mark-set",
479 G_CALLBACK (modest_recpt_editor_on_mark_set),
486 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
488 ModestRecptEditorPrivate *priv;
490 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
491 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
492 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
494 #ifdef MODEST_TOOLKIT_HILDON2
495 gtk_size_group_add_widget (size_group, priv->text_view);
497 gtk_size_group_add_widget (size_group, priv->scrolled_window);
502 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
504 ModestRecptEditorPrivate *priv;
506 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
507 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
509 #ifdef MODEST_TOOLKIT_HILDON2
510 return hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
512 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
517 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
519 g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
521 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
525 modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
528 ModestRecptEditor *recpt_editor)
530 ModestRecptEditorPrivate *priv;
531 GtkTextIter start, end;
532 GtkTextMark *selection_bound;
534 gboolean selection_changed = FALSE;
536 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
538 buffer = modest_recpt_editor_get_buffer (recpt_editor);
539 selection_bound = gtk_text_buffer_get_selection_bound (buffer);
541 if (mark != selection_bound)
544 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
546 tag = iter_has_recipient (&start);
548 if (!gtk_text_iter_begins_tag (&start, tag)) {
549 gtk_text_iter_backward_to_tag_toggle (&start, tag);
550 selection_changed = TRUE;
553 tag = iter_has_recipient (&end);
555 if (!gtk_text_iter_ends_tag (&end, tag)) {
556 gtk_text_iter_forward_to_tag_toggle (&end, tag);
557 selection_changed = TRUE;
560 if (selection_changed) {
561 /* We block this signal handler in order to prevent a
562 stack overflow caused by recursive calls to this
563 handler as the select_range call could issue a
565 g_signal_handler_block (buffer, priv->on_mark_set_handler);
566 gtk_text_buffer_select_range (buffer, &start, &end);
567 g_signal_handler_unblock (buffer, priv->on_mark_set_handler);
572 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
573 GdkEventFocus *event,
574 ModestRecptEditor *editor)
576 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
577 gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
583 is_valid_insert (const gchar *text, gint len)
588 gboolean quoted = FALSE;
589 const gchar *current, *next_current;
594 while (((len == -1)||(i < len)) && (*current != '\0')) {
595 c = g_utf8_get_char (current);
596 next_current = g_utf8_next_char (current);
597 if (next_current && *next_current != '\0')
598 next_c = g_utf8_get_char (g_utf8_next_char (current));
601 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
602 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
605 current = g_utf8_next_char (next_current);
609 if (c == 0x2022 || c == 0xfffc ||
610 c == g_utf8_get_char ("\n") ||
611 c == g_utf8_get_char ("\t"))
613 if (c == g_utf8_get_char ("\""))
615 current = g_utf8_next_char (current);
622 create_valid_text (const gchar *text, gint len)
628 gboolean quoted = FALSE;
629 const gchar *current, *next_current;
634 str = g_string_new ("");
637 while (((len == -1)||(i < len)) && (*current != '\0')) {
638 c = g_utf8_get_char (current);
639 next_current = g_utf8_next_char (current);
640 if (next_current && *next_current != '\0')
641 next_c = g_utf8_get_char (g_utf8_next_char (current));
644 if (c != 0x2022 && c != 0xfffc &&
645 c != g_utf8_get_char ("\n") &&
646 c != g_utf8_get_char ("\t"))
647 str = g_string_append_unichar (str, c);
648 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
649 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
650 str = g_string_append_c (str, '\n');
652 if (c == g_utf8_get_char ("\""))
654 current = g_utf8_next_char (current);
657 return g_string_free (str, FALSE);
660 /* Called after the default handler, and thus after the text was
661 inserted. We use this to insert a break after a ',' or a ';'*/
663 modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
664 GtkTextIter *location,
667 ModestRecptEditor *editor)
671 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
674 if (!gtk_text_iter_backward_char (&prev))
677 prev_char = gtk_text_iter_get_char (&prev);
678 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
679 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
680 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
682 gtk_text_buffer_insert (buffer, location, "\n",-1);
683 insert = gtk_text_buffer_get_insert (buffer);
684 gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
686 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
687 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
690 /* Called before the default handler, we use it to validate the inputs */
692 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
693 GtkTextIter *location,
696 ModestRecptEditor *editor)
701 if (!is_valid_insert (text, len)) {
702 gchar *new_text = create_valid_text (text, len);
703 g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
704 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
705 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
706 gtk_text_buffer_insert (buffer, location, new_text, -1);
707 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
708 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
713 if (iter_has_recipient (location)) {
714 gtk_text_buffer_get_end_iter (buffer, location);
715 gtk_text_buffer_place_cursor (buffer, location);
720 iter_has_recipient (GtkTextIter *iter)
723 GtkTextTag *result = NULL;
725 tags = gtk_text_iter_get_tags (iter);
727 for (node = tags; node != NULL; node = g_slist_next (node)) {
728 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
730 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
740 prev_iter_has_recipient (GtkTextIter *iter)
745 gtk_text_iter_backward_char (&prev);
746 return iter_has_recipient (&prev);
749 /* static GtkTextTag * */
750 /* next_iter_has_recipient (GtkTextIter *iter) */
752 /* GtkTextIter next; */
755 /* return iter_has_recipient (&next); */
759 iter_previous_char (GtkTextIter *iter)
764 gtk_text_iter_backward_char (&prev);
765 return gtk_text_iter_get_char (&prev);
768 /* static gunichar */
769 /* iter_next_char (GtkTextIter *iter) */
771 /* GtkTextIter next; */
774 /* gtk_text_iter_forward_char (&next); */
775 /* return gtk_text_iter_get_char (&next); */
779 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
781 GtkTextIter start, end;
784 if (!gtk_text_iter_begins_tag (&start, tag))
785 gtk_text_iter_backward_to_tag_toggle (&start, tag);
787 if (!gtk_text_iter_ends_tag (&end, tag))
788 gtk_text_iter_forward_to_tag_toggle (&end, tag);
789 gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
793 quote_opened (GtkTextIter *iter)
796 GtkTextBuffer *buffer;
797 gboolean opened = FALSE;
799 buffer = gtk_text_iter_get_buffer (iter);
800 gtk_text_buffer_get_start_iter (buffer, &start);
802 while (!gtk_text_iter_equal (&start, iter)) {
803 gunichar current_char = gtk_text_iter_get_char (&start);
804 if (current_char == '"')
806 else if (current_char == '\\')
807 gtk_text_iter_forward_char (&start);
808 if (!gtk_text_iter_equal (&start, iter))
809 gtk_text_iter_forward_char (&start);
818 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
820 ModestRecptEditor *editor)
823 GtkTextMark *selection;
824 GtkTextBuffer * buffer;
825 GtkTextIter location, selection_loc;
828 #ifdef MODEST_TOOLKIT_HILDON2
829 buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (text_view));
831 buffer = gtk_text_view_get_buffer (text_view);
833 insert = gtk_text_buffer_get_insert (buffer);
834 selection = gtk_text_buffer_get_selection_bound (buffer);
837 * * cursor is on resolved recipient:
838 * - right should go to first character after the recipient (usually ; or ,)
839 * - left should fo to the first character before the recipient
840 * - return should run check names on the recipient.
841 * * cursor is just after a recipient:
842 * - right should go to the next character. If it's a recipient, should select
844 * - left should go to the previous character. If it's a recipient, should go
845 * to the first character of the recipient, and select it.
846 * * cursor is on arbitrary text:
847 * - return should add a ; and go to the next line
848 * - left or right standard ones.
849 * * cursor is after a \n:
850 * - left should go to the character before the \n (as if \n was not a character)
851 * * cursor is before a \n:
852 * - right should go to the character after the \n
855 gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
856 gtk_text_buffer_get_iter_at_mark (buffer, &selection_loc, selection);
858 switch (key->keyval) {
862 gboolean cursor_ready = FALSE;
863 while (!cursor_ready) {
864 if (iter_previous_char (&location) == '\n') {
865 gtk_text_iter_backward_char (&location);
870 tag = iter_has_recipient (&location);
871 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
872 select_tag_of_iter (&location, tag);
873 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
876 gtk_text_iter_backward_char (&location);
877 tag = iter_has_recipient (&location);
879 select_tag_of_iter (&location, tag);
881 gtk_text_buffer_place_cursor (buffer, &location);
883 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
891 gboolean cursor_moved = FALSE;
893 tag = iter_has_recipient (&location);
894 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
895 gtk_text_iter_forward_to_tag_toggle (&location, tag);
896 while (gtk_text_iter_get_char (&location) == '\n')
897 gtk_text_iter_forward_char (&location);
898 gtk_text_buffer_place_cursor (buffer, &location);
899 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
903 while (gtk_text_iter_get_char (&location) == '\n') {
904 gtk_text_iter_forward_char (&location);
908 gtk_text_iter_forward_char (&location);
909 while (gtk_text_iter_get_char (&location) == '\n') {
910 gtk_text_iter_forward_char (&location);
914 tag = iter_has_recipient (&location);
916 select_tag_of_iter (&location, tag);
918 gtk_text_buffer_place_cursor (buffer, &location);
919 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
926 gint insert_offset, selection_offset;
927 insert_offset = gtk_text_iter_get_offset (&location);
928 selection_offset = gtk_text_iter_get_offset (&selection_loc);
929 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
930 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
931 if (selection_offset > insert_offset)
932 location = selection_loc;
933 tag = iter_has_recipient (&location);
935 gtk_text_buffer_get_end_iter (buffer, &location);
936 gtk_text_buffer_place_cursor (buffer, &location);
937 if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
938 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
939 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
941 gunichar prev_char = iter_previous_char (&location);
942 if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
943 ||(prev_char == ';')||(prev_char == ','))
944 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
946 if ((prev_char != ';') && (prev_char != ','))
947 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
948 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
951 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
952 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
958 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
959 if (gtk_text_buffer_get_has_selection (buffer)) {
960 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
964 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
965 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
970 tag = prev_iter_has_recipient (&location);
972 GtkTextIter iter_in_tag;
973 iter_in_tag = location;
974 gtk_text_iter_backward_char (&iter_in_tag);
975 select_tag_of_iter (&iter_in_tag, tag);
976 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
988 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
990 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
991 #ifdef MODEST_TOOLKIT_HILDON2
992 GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
994 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
996 GtkTextIter start, end;
998 gtk_text_buffer_get_end_iter (buffer, &start);
1000 gtk_text_buffer_select_range (buffer, &start, &end);
1006 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
1008 ModestRecptEditorPrivate *priv;
1010 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
1011 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1013 if (GTK_WIDGET_VISIBLE (recpt_editor)) {
1014 gtk_widget_grab_focus (priv->text_view);
1019 modest_recpt_editor_has_focus (ModestRecptEditor *recpt_editor)
1021 ModestRecptEditorPrivate *priv;
1023 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
1024 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1026 return GTK_WIDGET_VISIBLE (priv->text_view) &&
1027 gtk_widget_is_focus (priv->text_view);
1031 modest_recpt_editor_set_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
1033 ModestRecptEditorPrivate *priv;
1035 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
1036 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1038 priv->show_abook = show;
1041 gtk_widget_show (priv->abook_button);
1043 gtk_widget_hide (priv->abook_button);
1047 modest_recpt_editor_get_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
1049 ModestRecptEditorPrivate *priv;
1051 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
1052 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
1054 return priv->show_abook;
1059 modest_recpt_editor_finalize (GObject *object)
1061 ModestRecptEditorPrivate *priv;
1062 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
1064 if (g_signal_handler_is_connected (object, priv->on_mark_set_handler))
1065 g_signal_handler_disconnect (object, priv->on_mark_set_handler);
1066 priv->on_mark_set_handler = 0;
1068 if (priv->recipients) {
1069 g_free (priv->recipients);
1070 priv->recipients = NULL;
1073 (*parent_class->finalize) (object);
1079 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
1081 GObjectClass *object_class;
1083 parent_class = g_type_class_peek_parent (klass);
1084 object_class = (GObjectClass*) klass;
1086 object_class->finalize = modest_recpt_editor_finalize;
1088 g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
1090 signals[OPEN_ADDRESSBOOK_SIGNAL] =
1091 g_signal_new ("open-addressbook",
1092 G_TYPE_FROM_CLASS (object_class),
1093 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
1094 G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
1096 g_cclosure_marshal_VOID__VOID,
1103 modest_recpt_editor_get_type (void)
1105 static GType type = 0;
1107 if (G_UNLIKELY(type == 0))
1109 static const GTypeInfo info =
1111 sizeof (ModestRecptEditorClass),
1112 NULL, /* base_init */
1113 NULL, /* base_finalize */
1114 (GClassInitFunc) modest_recpt_editor_class_init, /* class_init */
1115 NULL, /* class_finalize */
1116 NULL, /* class_data */
1117 sizeof (ModestRecptEditor),
1118 0, /* n_preallocs */
1119 modest_recpt_editor_instance_init /* instance_init */
1122 type = g_type_register_static (GTK_TYPE_HBOX,
1123 "ModestRecptEditor",