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>
47 /* FIXNE: we should have no maemo-deps in widgets/ */
48 #ifndef MODEST_TOOLKIT_GTK
49 #include "maemo/modest-hildon-includes.h"
50 #endif /*!MODEST_TOOLKIT_GTK*/
53 static GObjectClass *parent_class = NULL;
55 #define RECIPIENT_TAG_ID "recpt-id"
59 OPEN_ADDRESSBOOK_SIGNAL,
63 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
65 struct _ModestRecptEditorPrivate
68 GtkWidget *abook_button;
69 GtkWidget *scrolled_window;
74 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o) \
75 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
77 static guint signals[LAST_SIGNAL] = {0};
79 /* static functions: GObject */
80 static void modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class);
81 static void modest_recpt_editor_finalize (GObject *object);
82 static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
85 static void modest_recpt_editor_on_abook_clicked (GtkButton *button,
86 ModestRecptEditor *editor);
87 static gboolean modest_recpt_editor_on_button_release_event (GtkWidget *widget,
88 GdkEventButton *event,
89 ModestRecptEditor *editor);
90 static void modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor);
91 static void modest_recpt_editor_on_focus_in (GtkTextView *text_view,
93 ModestRecptEditor *editor);
94 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
95 GtkTextIter *location,
98 ModestRecptEditor *editor);
99 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
101 ModestRecptEditor *editor);
102 static GtkTextTag *iter_has_recipient (GtkTextIter *iter);
103 static gunichar iter_previous_char (GtkTextIter *iter);
104 /* static gunichar iter_next_char (GtkTextIter *iter); */
105 static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
106 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
107 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
108 static gboolean quote_opened (GtkTextIter *iter);
109 static gboolean is_valid_insert (const gchar *text, gint len);
110 static gchar *create_valid_text (const gchar *text, gint len);
113 * modest_recpt_editor_new:
115 * Return value: a new #ModestRecptEditor instance implemented for Gtk+
118 modest_recpt_editor_new (void)
120 ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR,
121 "homogeneous", FALSE,
125 return GTK_WIDGET (self);
129 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
131 ModestRecptEditorPrivate *priv;
132 GtkTextBuffer *buffer = NULL;
133 gchar *valid_recipients = NULL;
135 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
136 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
138 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
140 valid_recipients = create_valid_text (recipients, -1);
141 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
142 gtk_text_buffer_set_text (buffer, valid_recipients, -1);
143 g_free (valid_recipients);
144 if (GTK_WIDGET_REALIZED (recpt_editor))
145 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
146 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
151 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
153 ModestRecptEditorPrivate *priv;
154 GtkTextBuffer *buffer = NULL;
156 gchar *string_to_add;
158 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
159 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
161 if (recipients == NULL)
164 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
166 if (gtk_text_buffer_get_char_count (buffer) > 0) {
167 string_to_add = g_strconcat (";\n", recipients, NULL);
169 string_to_add = g_strdup (recipients);
172 gtk_text_buffer_get_end_iter (buffer, &iter);
174 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
176 gtk_text_buffer_insert (buffer, &iter, string_to_add, -1);
178 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
180 if (GTK_WIDGET_REALIZED (recpt_editor))
181 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
183 g_free (string_to_add);
187 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
189 ModestRecptEditorPrivate *priv;
190 GtkTextBuffer *buffer = NULL;
191 GtkTextIter start, end, iter;
192 GtkTextTag *tag = NULL;
194 gboolean is_first_recipient = TRUE;
196 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
197 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
199 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
201 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
202 gtk_text_buffer_get_bounds (buffer, &start, &end);
203 if (gtk_text_buffer_get_char_count (buffer) > 0) {
204 gchar * buffer_contents;
206 gtk_text_buffer_get_bounds (buffer, &start, &end);
207 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
208 if (!g_str_has_suffix (buffer_contents, "\n")) {
209 if (g_str_has_suffix (buffer_contents, ";")||(g_str_has_suffix (buffer_contents, ",")))
210 gtk_text_buffer_insert (buffer, &end, "\n", -1);
212 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
214 g_free (buffer_contents);
217 gtk_text_buffer_get_end_iter (buffer, &iter);
219 tag = gtk_text_buffer_create_tag (buffer, NULL,
220 "underline", PANGO_UNDERLINE_SINGLE,
221 "wrap-mode", GTK_WRAP_NONE,
222 "editable", TRUE, NULL);
224 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
225 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
227 for (node = email_list; node != NULL; node = g_slist_next (node)) {
228 gchar *recipient = (gchar *) node->data;
230 if ((recipient) && (strlen (recipient) != 0)) {
232 if (!is_first_recipient)
233 gtk_text_buffer_insert (buffer, &iter, "\n", -1);
235 gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
236 gtk_text_buffer_insert (buffer, &iter, ";", -1);
237 is_first_recipient = FALSE;
240 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
242 modest_recpt_editor_move_cursor_to_end (recpt_editor);
247 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor,
248 GtkTextIter *start, GtkTextIter *end,
249 GSList *email_list, const gchar * recipient_id)
251 ModestRecptEditorPrivate *priv;
252 GtkTextBuffer *buffer;
255 gboolean is_first_recipient = TRUE;
257 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
258 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
260 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
261 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
263 gtk_text_buffer_delete (buffer, start, end);
265 tag = gtk_text_buffer_create_tag (buffer, NULL,
266 "underline", PANGO_UNDERLINE_SINGLE,
267 "wrap-mode", GTK_WRAP_NONE,
268 "editable", TRUE, NULL);
270 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
271 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
273 for (node = email_list; node != NULL; node = g_slist_next (node)) {
274 gchar *recipient = (gchar *) node->data;
276 if ((recipient) && (strlen (recipient) != 0)) {
278 if (!is_first_recipient)
279 gtk_text_buffer_insert (buffer, start, "\n", -1);
281 gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
283 if (node->next != NULL)
284 gtk_text_buffer_insert (buffer, start, ";", -1);
285 is_first_recipient = FALSE;
288 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
294 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
296 ModestRecptEditorPrivate *priv;
297 GtkTextBuffer *buffer = NULL;
298 GtkTextIter start, end;
301 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
302 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
304 if (priv->recipients != NULL) {
305 g_free (priv->recipients);
306 priv->recipients = NULL;
309 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
311 gtk_text_buffer_get_start_iter (buffer, &start);
312 gtk_text_buffer_get_end_iter (buffer, &end);
314 priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
315 for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
321 return priv->recipients;
326 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
328 ModestRecptEditorPrivate *priv;
329 GtkWidget *abook_icon;
330 GtkTextBuffer *buffer;
332 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
334 priv->abook_button = gtk_button_new ();
335 gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
336 gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
337 GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
338 gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
339 abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
340 gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
342 priv->text_view = gtk_text_view_new ();
343 /* Auto-capitalization is the default, so let's turn it off: */
345 hildon_gtk_text_view_set_input_mode (GTK_TEXT_VIEW (priv->text_view),
346 HILDON_GTK_INPUT_MODE_FULL);
349 priv->recipients = NULL;
351 priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 1024);
352 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
353 GTK_POLICY_AUTOMATIC);
354 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
355 /* gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
357 gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
358 /* gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
359 gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
361 gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
362 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
363 gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
365 gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
366 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
368 gtk_widget_set_size_request (priv->text_view, 75, -1);
370 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
371 g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
372 g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
373 g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
374 g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
375 g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
377 /* gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
383 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
385 ModestRecptEditorPrivate *priv;
387 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
388 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
389 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
391 gtk_size_group_add_widget (size_group, priv->scrolled_window);
395 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
397 ModestRecptEditorPrivate *priv;
399 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
400 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
402 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
406 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
408 g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
410 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
414 modest_recpt_editor_on_button_release_event (GtkWidget *widget,
415 GdkEventButton *event,
416 ModestRecptEditor *recpt_editor)
418 ModestRecptEditorPrivate *priv;
419 GtkTextIter location, start, end;
421 GtkTextBuffer *buffer;
423 gboolean selection_changed = FALSE;
425 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
427 buffer = modest_recpt_editor_get_buffer (recpt_editor);
428 mark = gtk_text_buffer_get_insert (buffer);
429 gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
431 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
433 tag = iter_has_recipient (&start);
435 if (!gtk_text_iter_begins_tag (&start, tag)) {
436 gtk_text_iter_backward_to_tag_toggle (&start, tag);
437 selection_changed = TRUE;
440 tag = iter_has_recipient (&end);
442 if (!gtk_text_iter_ends_tag (&end, tag)) {
443 gtk_text_iter_forward_to_tag_toggle (&end, tag);
444 selection_changed = TRUE;
447 if (selection_changed) {
448 gtk_text_buffer_select_range (buffer, &start, &end);
450 GTK_TEXT_VIEW (priv->text_view)->pending_place_cursor_button = 0;
457 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
458 GdkEventFocus *event,
459 ModestRecptEditor *editor)
461 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
462 gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
466 is_valid_insert (const gchar *text, gint len)
471 gboolean quoted = FALSE;
472 const gchar *current, *next_current;
477 while (((len == -1)||(i < len)) && (*current != '\0')) {
478 c = g_utf8_get_char (current);
479 next_current = g_utf8_next_char (current);
480 if (next_current && *next_current != '\0')
481 next_c = g_utf8_get_char (g_utf8_next_char (current));
484 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
485 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
488 current = g_utf8_next_char (next_current);
492 if (c == 0x2022 || c == 0xfffc ||
493 c == g_utf8_get_char ("\n") ||
494 c == g_utf8_get_char ("\t"))
496 if (c == g_utf8_get_char ("\""))
498 current = g_utf8_next_char (current);
505 create_valid_text (const gchar *text, gint len)
511 gboolean quoted = FALSE;
512 const gchar *current, *next_current;
517 str = g_string_new ("");
520 while (((len == -1)||(i < len)) && (*current != '\0')) {
521 c = g_utf8_get_char (current);
522 next_current = g_utf8_next_char (current);
523 if (next_current && *next_current != '\0')
524 next_c = g_utf8_get_char (g_utf8_next_char (current));
527 if (c != 0x2022 && c != 0xfffc &&
528 c != g_utf8_get_char ("\n") &&
529 c != g_utf8_get_char ("\t"))
530 str = g_string_append_unichar (str, c);
531 if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
532 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
533 str = g_string_append_c (str, '\n');
535 if (c == g_utf8_get_char ("\""))
537 current = g_utf8_next_char (current);
540 return g_string_free (str, FALSE);
544 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
545 GtkTextIter *location,
548 ModestRecptEditor *editor)
552 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
557 if (!is_valid_insert (text, len)) {
558 gchar *new_text = create_valid_text (text, len);
559 g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
560 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text,
562 gtk_text_buffer_insert (buffer, location, new_text, -1);
563 g_signal_handlers_unblock_by_func (buffer,
564 modest_recpt_editor_on_insert_text,
570 if (iter_has_recipient (location)) {
571 gtk_text_buffer_get_end_iter (buffer, location);
572 gtk_text_buffer_place_cursor (buffer, location);
575 if (gtk_text_iter_is_start (location))
578 if (gtk_text_iter_is_end (location)) {
580 if (!gtk_text_iter_backward_char (&prev))
582 prev_char = gtk_text_iter_get_char (&prev);
583 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
584 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
586 gtk_text_buffer_insert (buffer, location, "\n",-1);
587 insert = gtk_text_buffer_get_insert (buffer);
588 gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
590 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
597 iter_has_recipient (GtkTextIter *iter)
600 GtkTextTag *result = NULL;
602 tags = gtk_text_iter_get_tags (iter);
604 for (node = tags; node != NULL; node = g_slist_next (node)) {
605 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
607 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
617 prev_iter_has_recipient (GtkTextIter *iter)
622 gtk_text_iter_backward_char (&prev);
623 return iter_has_recipient (&prev);
626 /* static GtkTextTag * */
627 /* next_iter_has_recipient (GtkTextIter *iter) */
629 /* GtkTextIter next; */
632 /* return iter_has_recipient (&next); */
636 iter_previous_char (GtkTextIter *iter)
641 gtk_text_iter_backward_char (&prev);
642 return gtk_text_iter_get_char (&prev);
645 /* static gunichar */
646 /* iter_next_char (GtkTextIter *iter) */
648 /* GtkTextIter next; */
651 /* gtk_text_iter_forward_char (&next); */
652 /* return gtk_text_iter_get_char (&next); */
656 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
658 GtkTextIter start, end;
661 if (!gtk_text_iter_begins_tag (&start, tag))
662 gtk_text_iter_backward_to_tag_toggle (&start, tag);
664 if (!gtk_text_iter_ends_tag (&end, tag))
665 gtk_text_iter_forward_to_tag_toggle (&end, tag);
666 gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
670 quote_opened (GtkTextIter *iter)
673 GtkTextBuffer *buffer;
674 gboolean opened = FALSE;
676 buffer = gtk_text_iter_get_buffer (iter);
677 gtk_text_buffer_get_start_iter (buffer, &start);
679 while (!gtk_text_iter_equal (&start, iter)) {
680 gunichar current_char = gtk_text_iter_get_char (&start);
681 if (current_char == '"')
683 else if (current_char == '\\')
684 gtk_text_iter_forward_char (&start);
685 if (!gtk_text_iter_equal (&start, iter))
686 gtk_text_iter_forward_char (&start);
695 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
697 ModestRecptEditor *editor)
700 GtkTextMark *selection;
701 GtkTextBuffer * buffer;
702 GtkTextIter location, selection_loc;
705 buffer = gtk_text_view_get_buffer (text_view);
706 insert = gtk_text_buffer_get_insert (buffer);
707 selection = gtk_text_buffer_get_selection_bound (buffer);
710 * * cursor is on resolved recipient:
711 * - right should go to first character after the recipient (usually ; or ,)
712 * - left should fo to the first character before the recipient
713 * - return should run check names on the recipient.
714 * * cursor is just after a recipient:
715 * - right should go to the next character. If it's a recipient, should select
717 * - left should go to the previous character. If it's a recipient, should go
718 * to the first character of the recipient, and select it.
719 * * cursor is on arbitrary text:
720 * - return should add a ; and go to the next line
721 * - left or right standard ones.
722 * * cursor is after a \n:
723 * - left should go to the character before the \n (as if \n was not a character)
724 * * cursor is before a \n:
725 * - right should go to the character after the \n
728 gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
729 gtk_text_buffer_get_iter_at_mark (buffer, &selection_loc, selection);
731 switch (key->keyval) {
735 gboolean cursor_ready = FALSE;
736 while (!cursor_ready) {
737 if (iter_previous_char (&location) == '\n') {
738 gtk_text_iter_backward_char (&location);
743 tag = iter_has_recipient (&location);
744 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
745 select_tag_of_iter (&location, tag);
746 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
749 gtk_text_iter_backward_char (&location);
750 tag = iter_has_recipient (&location);
752 select_tag_of_iter (&location, tag);
754 gtk_text_buffer_place_cursor (buffer, &location);
756 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
764 gboolean cursor_moved = FALSE;
766 tag = iter_has_recipient (&location);
767 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
768 gtk_text_iter_forward_to_tag_toggle (&location, tag);
769 while (gtk_text_iter_get_char (&location) == '\n')
770 gtk_text_iter_forward_char (&location);
771 gtk_text_buffer_place_cursor (buffer, &location);
772 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
776 while (gtk_text_iter_get_char (&location) == '\n') {
777 gtk_text_iter_forward_char (&location);
781 gtk_text_iter_forward_char (&location);
782 while (gtk_text_iter_get_char (&location) == '\n') {
783 gtk_text_iter_forward_char (&location);
787 tag = iter_has_recipient (&location);
789 select_tag_of_iter (&location, tag);
791 gtk_text_buffer_place_cursor (buffer, &location);
792 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
799 gint insert_offset, selection_offset;
800 insert_offset = gtk_text_iter_get_offset (&location);
801 selection_offset = gtk_text_iter_get_offset (&selection_loc);
802 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
803 if (selection_offset > insert_offset)
804 location = selection_loc;
805 tag = iter_has_recipient (&location);
807 gtk_text_buffer_get_end_iter (buffer, &location);
808 gtk_text_buffer_place_cursor (buffer, &location);
809 if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
810 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
811 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
813 gunichar prev_char = iter_previous_char (&location);
814 if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
815 ||(prev_char == ';')||(prev_char == ','))
816 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
818 if ((prev_char != ';') && (prev_char != ','))
819 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
820 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
823 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
829 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
830 if (gtk_text_buffer_get_has_selection (buffer)) {
831 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
835 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
836 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
841 tag = prev_iter_has_recipient (&location);
843 GtkTextIter iter_in_tag;
844 iter_in_tag = location;
845 gtk_text_iter_backward_char (&iter_in_tag);
846 select_tag_of_iter (&iter_in_tag, tag);
847 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
859 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
861 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
862 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
863 GtkTextIter start, end;
865 gtk_text_buffer_get_end_iter (buffer, &start);
867 gtk_text_buffer_select_range (buffer, &start, &end);
873 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
875 ModestRecptEditorPrivate *priv;
877 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
878 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
880 gtk_widget_grab_focus (priv->text_view);
884 modest_recpt_editor_has_focus (ModestRecptEditor *recpt_editor)
886 ModestRecptEditorPrivate *priv;
888 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
889 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
891 return gtk_widget_is_focus (priv->text_view);
895 modest_recpt_editor_finalize (GObject *object)
897 ModestRecptEditorPrivate *priv;
898 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
900 if (priv->recipients) {
901 g_free (priv->recipients);
902 priv->recipients = NULL;
905 (*parent_class->finalize) (object);
911 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
913 GObjectClass *object_class;
914 GtkWidgetClass *widget_class;
916 parent_class = g_type_class_peek_parent (klass);
917 object_class = (GObjectClass*) klass;
918 widget_class = GTK_WIDGET_CLASS (klass);
920 object_class->finalize = modest_recpt_editor_finalize;
922 g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
924 signals[OPEN_ADDRESSBOOK_SIGNAL] =
925 g_signal_new ("open-addressbook",
926 G_TYPE_FROM_CLASS (object_class),
927 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
928 G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
930 g_cclosure_marshal_VOID__VOID,
937 modest_recpt_editor_get_type (void)
939 static GType type = 0;
941 if (G_UNLIKELY(type == 0))
943 static const GTypeInfo info =
945 sizeof (ModestRecptEditorClass),
946 NULL, /* base_init */
947 NULL, /* base_finalize */
948 (GClassInitFunc) modest_recpt_editor_class_init, /* class_init */
949 NULL, /* class_finalize */
950 NULL, /* class_data */
951 sizeof (ModestRecptEditor),
953 modest_recpt_editor_instance_init /* instance_init */
956 type = g_type_register_static (GTK_TYPE_HBOX,