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 static GObjectClass *parent_class = NULL;
49 #define RECIPIENT_TAG_ID "recpt-id"
53 OPEN_ADDRESSBOOK_SIGNAL,
57 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
59 struct _ModestRecptEditorPrivate
62 GtkWidget *abook_button;
63 GtkWidget *scrolled_window;
68 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o) \
69 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
71 static guint signals[LAST_SIGNAL] = {0};
73 /* static functions: GObject */
74 static void modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class);
75 static void modest_recpt_editor_finalize (GObject *object);
76 static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
79 static void modest_recpt_editor_on_abook_clicked (GtkButton *button,
80 ModestRecptEditor *editor);
81 static gboolean modest_recpt_editor_on_button_release_event (GtkWidget *widget,
82 GdkEventButton *event,
83 ModestRecptEditor *editor);
84 static void modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor);
85 static void modest_recpt_editor_on_focus_in (GtkTextView *text_view,
87 ModestRecptEditor *editor);
88 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
89 GtkTextIter *location,
92 ModestRecptEditor *editor);
93 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
95 ModestRecptEditor *editor);
96 static GtkTextTag *iter_has_recipient (GtkTextIter *iter);
97 static gunichar iter_previous_char (GtkTextIter *iter);
98 /* static gunichar iter_next_char (GtkTextIter *iter); */
99 static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
100 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
101 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
102 static gboolean quote_opened (GtkTextIter *iter);
105 * modest_recpt_editor_new:
107 * Return value: a new #ModestRecptEditor instance implemented for Gtk+
110 modest_recpt_editor_new (void)
112 ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR,
113 "homogeneous", FALSE,
117 return GTK_WIDGET (self);
121 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
123 ModestRecptEditorPrivate *priv;
124 GtkTextBuffer *buffer = NULL;
126 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
127 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
129 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
131 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
132 gtk_text_buffer_set_text (buffer, recipients, -1);
133 if (GTK_WIDGET_REALIZED (recpt_editor))
134 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
135 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
140 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
142 ModestRecptEditorPrivate *priv;
143 GtkTextBuffer *buffer = NULL;
145 gchar * string_to_add;
147 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
148 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
150 if (recipients == NULL)
153 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
155 if (gtk_text_buffer_get_char_count (buffer) > 0) {
156 string_to_add = g_strconcat (";\n", recipients, NULL);
158 string_to_add = g_strdup (recipients);
161 gtk_text_buffer_get_end_iter (buffer, &iter);
163 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
165 gtk_text_buffer_insert (buffer, &iter, recipients, -1);
167 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
169 if (GTK_WIDGET_REALIZED (recpt_editor))
170 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
175 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
177 ModestRecptEditorPrivate *priv;
178 GtkTextBuffer *buffer = NULL;
179 GtkTextIter start, end, iter;
180 GtkTextTag *tag = NULL;
182 gboolean is_first_recipient = TRUE;
184 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
185 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
187 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
189 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
190 gtk_text_buffer_get_bounds (buffer, &start, &end);
191 if (gtk_text_buffer_get_char_count (buffer) > 0) {
192 gchar * buffer_contents;
194 gtk_text_buffer_get_bounds (buffer, &start, &end);
195 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
196 if (!g_str_has_suffix (buffer_contents, "\n")) {
197 if (g_str_has_suffix (buffer_contents, ";")||(g_str_has_suffix (buffer_contents, ",")))
198 gtk_text_buffer_insert (buffer, &end, "\n", -1);
200 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
202 g_free (buffer_contents);
205 gtk_text_buffer_get_end_iter (buffer, &iter);
207 tag = gtk_text_buffer_create_tag (buffer, NULL,
208 "underline", PANGO_UNDERLINE_SINGLE,
209 "wrap-mode", GTK_WRAP_NONE,
210 "editable", TRUE, NULL);
212 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
213 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
215 for (node = email_list; node != NULL; node = g_slist_next (node)) {
216 gchar *recipient = (gchar *) node->data;
218 if ((recipient) && (strlen (recipient) != 0)) {
220 if (!is_first_recipient)
221 gtk_text_buffer_insert (buffer, &iter, "\n", -1);
223 gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
224 gtk_text_buffer_insert (buffer, &iter, ";", -1);
225 is_first_recipient = FALSE;
228 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
230 modest_recpt_editor_move_cursor_to_end (recpt_editor);
235 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor,
236 GtkTextIter *start, GtkTextIter *end,
237 GSList *email_list, const gchar * recipient_id)
239 ModestRecptEditorPrivate *priv;
240 GtkTextBuffer *buffer;
243 gboolean is_first_recipient = TRUE;
245 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
246 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
248 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
249 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
251 gtk_text_buffer_delete (buffer, start, end);
253 tag = gtk_text_buffer_create_tag (buffer, NULL,
254 "underline", PANGO_UNDERLINE_SINGLE,
255 "wrap-mode", GTK_WRAP_NONE,
256 "editable", TRUE, NULL);
258 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
259 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
261 for (node = email_list; node != NULL; node = g_slist_next (node)) {
262 gchar *recipient = (gchar *) node->data;
264 if ((recipient) && (strlen (recipient) != 0)) {
266 if (!is_first_recipient)
267 gtk_text_buffer_insert (buffer, start, "\n", -1);
269 gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
271 if (node->next != NULL)
272 gtk_text_buffer_insert (buffer, start, ";", -1);
273 is_first_recipient = FALSE;
276 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
282 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
284 ModestRecptEditorPrivate *priv;
285 GtkTextBuffer *buffer = NULL;
286 GtkTextIter start, end;
289 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
290 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
292 if (priv->recipients != NULL) {
293 g_free (priv->recipients);
294 priv->recipients = NULL;
297 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
299 gtk_text_buffer_get_start_iter (buffer, &start);
300 gtk_text_buffer_get_end_iter (buffer, &end);
302 priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
303 for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
309 return priv->recipients;
314 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
316 ModestRecptEditorPrivate *priv;
317 GtkWidget *abook_icon;
318 GtkTextBuffer *buffer;
320 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
322 priv->abook_button = gtk_button_new ();
323 gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
324 gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
325 GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
326 gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
327 abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
328 gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
330 priv->text_view = gtk_text_view_new ();
331 priv->recipients = NULL;
333 priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5);
334 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
335 GTK_POLICY_AUTOMATIC);
336 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
337 /* gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
339 gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
340 /* gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
341 gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
343 gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
344 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
345 gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
347 gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
348 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
350 gtk_widget_set_size_request (priv->text_view, 75, -1);
352 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
353 g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
354 g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
355 g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
356 g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
357 g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
359 /* gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
365 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
367 ModestRecptEditorPrivate *priv;
369 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
370 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
371 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
373 gtk_size_group_add_widget (size_group, priv->scrolled_window);
377 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
379 ModestRecptEditorPrivate *priv;
381 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
382 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
384 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
388 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
390 g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
392 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
396 modest_recpt_editor_on_button_release_event (GtkWidget *widget,
397 GdkEventButton *event,
398 ModestRecptEditor *recpt_editor)
400 ModestRecptEditorPrivate *priv;
401 GtkTextIter location, start, end;
403 GtkTextBuffer *buffer;
405 gboolean selection_changed = FALSE;
407 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
409 buffer = modest_recpt_editor_get_buffer (recpt_editor);
410 mark = gtk_text_buffer_get_insert (buffer);
411 gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
413 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
415 tag = iter_has_recipient (&start);
417 if (!gtk_text_iter_begins_tag (&start, tag)) {
418 gtk_text_iter_backward_to_tag_toggle (&start, tag);
419 selection_changed = TRUE;
422 tag = iter_has_recipient (&end);
424 if (!gtk_text_iter_ends_tag (&end, tag)) {
425 gtk_text_iter_forward_to_tag_toggle (&end, tag);
426 selection_changed = TRUE;
429 gtk_text_buffer_select_range (buffer, &start, &end);
435 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
436 GdkEventFocus *event,
437 ModestRecptEditor *editor)
439 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
440 gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
444 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
445 GtkTextIter *location,
448 ModestRecptEditor *editor)
452 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
454 if (iter_has_recipient (location)) {
455 gtk_text_buffer_get_end_iter (buffer, location);
456 gtk_text_buffer_place_cursor (buffer, location);
459 if (gtk_text_iter_is_start (location))
462 if (gtk_text_iter_is_end (location)) {
464 if (!gtk_text_iter_backward_char (&prev))
466 prev_char = gtk_text_iter_get_char (&prev);
467 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
468 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
470 gtk_text_buffer_insert (buffer, location, "\n",-1);
471 insert = gtk_text_buffer_get_insert (buffer);
472 gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
474 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
481 iter_has_recipient (GtkTextIter *iter)
484 GtkTextTag *result = NULL;
486 tags = gtk_text_iter_get_tags (iter);
488 for (node = tags; node != NULL; node = g_slist_next (node)) {
489 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
491 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
501 prev_iter_has_recipient (GtkTextIter *iter)
506 gtk_text_iter_backward_char (&prev);
507 return iter_has_recipient (&prev);
510 /* static GtkTextTag * */
511 /* next_iter_has_recipient (GtkTextIter *iter) */
513 /* GtkTextIter next; */
516 /* return iter_has_recipient (&next); */
520 iter_previous_char (GtkTextIter *iter)
525 gtk_text_iter_backward_char (&prev);
526 return gtk_text_iter_get_char (&prev);
529 /* static gunichar */
530 /* iter_next_char (GtkTextIter *iter) */
532 /* GtkTextIter next; */
535 /* gtk_text_iter_forward_char (&next); */
536 /* return gtk_text_iter_get_char (&next); */
540 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
542 GtkTextIter start, end;
545 if (!gtk_text_iter_begins_tag (&start, tag))
546 gtk_text_iter_backward_to_tag_toggle (&start, tag);
548 if (!gtk_text_iter_ends_tag (&end, tag))
549 gtk_text_iter_forward_to_tag_toggle (&end, tag);
550 gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
554 quote_opened (GtkTextIter *iter)
557 GtkTextBuffer *buffer;
558 gboolean opened = FALSE;
560 buffer = gtk_text_iter_get_buffer (iter);
561 gtk_text_buffer_get_start_iter (buffer, &start);
563 while (!gtk_text_iter_equal (&start, iter)) {
564 gunichar current_char = gtk_text_iter_get_char (&start);
565 if (current_char == '"')
567 else if (current_char == '\\')
568 gtk_text_iter_forward_char (&start);
569 if (!gtk_text_iter_equal (&start, iter))
570 gtk_text_iter_forward_char (&start);
579 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
581 ModestRecptEditor *editor)
584 GtkTextBuffer * buffer;
585 GtkTextIter location;
588 buffer = gtk_text_view_get_buffer (text_view);
589 insert = gtk_text_buffer_get_insert (buffer);
592 * * cursor is on resolved recipient:
593 * - right should go to first character after the recipient (usually ; or ,)
594 * - left should fo to the first character before the recipient
595 * - return should run check names on the recipient.
596 * * cursor is just after a recipient:
597 * - right should go to the next character. If it's a recipient, should select
599 * - left should go to the previous character. If it's a recipient, should go
600 * to the first character of the recipient, and select it.
601 * * cursor is on arbitrary text:
602 * - return should add a ; and go to the next line
603 * - left or right standard ones.
604 * * cursor is after a \n:
605 * - left should go to the character before the \n (as if \n was not a character)
606 * * cursor is before a \n:
607 * - right should go to the character after the \n
610 gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
612 switch (key->keyval) {
616 gboolean cursor_ready = FALSE;
617 while (!cursor_ready) {
618 if (iter_previous_char (&location) == '\n') {
619 gtk_text_iter_backward_char (&location);
624 tag = iter_has_recipient (&location);
625 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
626 select_tag_of_iter (&location, tag);
627 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
630 gtk_text_iter_backward_char (&location);
631 tag = iter_has_recipient (&location);
633 select_tag_of_iter (&location, tag);
635 gtk_text_buffer_place_cursor (buffer, &location);
637 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
645 gboolean cursor_moved = FALSE;
647 tag = iter_has_recipient (&location);
648 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
649 gtk_text_iter_forward_to_tag_toggle (&location, tag);
650 while (gtk_text_iter_get_char (&location) == '\n')
651 gtk_text_iter_forward_char (&location);
652 gtk_text_buffer_place_cursor (buffer, &location);
653 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
657 while (gtk_text_iter_get_char (&location) == '\n') {
658 gtk_text_iter_forward_char (&location);
662 gtk_text_iter_forward_char (&location);
663 while (gtk_text_iter_get_char (&location) == '\n') {
664 gtk_text_iter_forward_char (&location);
668 tag = iter_has_recipient (&location);
670 select_tag_of_iter (&location, tag);
672 gtk_text_buffer_place_cursor (buffer, &location);
673 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
680 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
681 tag = iter_has_recipient (&location);
683 gtk_text_buffer_get_end_iter (buffer, &location);
684 gtk_text_buffer_place_cursor (buffer, &location);
685 if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
686 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
687 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
689 gunichar prev_char = iter_previous_char (&location);
690 if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
691 ||(prev_char == ';')||(prev_char == ','))
692 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
694 if ((prev_char != ';') && (prev_char != ','))
695 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
696 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
699 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
705 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
706 if (gtk_text_buffer_get_has_selection (buffer)) {
707 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
711 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
712 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
717 tag = prev_iter_has_recipient (&location);
719 GtkTextIter iter_in_tag;
720 iter_in_tag = location;
721 gtk_text_iter_backward_char (&iter_in_tag);
722 select_tag_of_iter (&iter_in_tag, tag);
723 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
735 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
737 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
738 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
739 GtkTextIter start, end;
741 gtk_text_buffer_get_end_iter (buffer, &start);
743 gtk_text_buffer_select_range (buffer, &start, &end);
749 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
751 ModestRecptEditorPrivate *priv;
753 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
754 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
756 gtk_widget_grab_focus (priv->text_view);
760 modest_recpt_editor_finalize (GObject *object)
762 ModestRecptEditorPrivate *priv;
763 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
765 if (priv->recipients) {
766 g_free (priv->recipients);
767 priv->recipients = NULL;
770 (*parent_class->finalize) (object);
776 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
778 GObjectClass *object_class;
779 GtkWidgetClass *widget_class;
781 parent_class = g_type_class_peek_parent (klass);
782 object_class = (GObjectClass*) klass;
783 widget_class = GTK_WIDGET_CLASS (klass);
785 object_class->finalize = modest_recpt_editor_finalize;
787 g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
789 signals[OPEN_ADDRESSBOOK_SIGNAL] =
790 g_signal_new ("open-addressbook",
791 G_TYPE_FROM_CLASS (object_class),
792 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
793 G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
795 g_cclosure_marshal_VOID__VOID,
802 modest_recpt_editor_get_type (void)
804 static GType type = 0;
806 if (G_UNLIKELY(type == 0))
808 static const GTypeInfo info =
810 sizeof (ModestRecptEditorClass),
811 NULL, /* base_init */
812 NULL, /* base_finalize */
813 (GClassInitFunc) modest_recpt_editor_class_init, /* class_init */
814 NULL, /* class_finalize */
815 NULL, /* class_data */
816 sizeof (ModestRecptEditor),
818 modest_recpt_editor_instance_init /* instance_init */
821 type = g_type_register_static (GTK_TYPE_HBOX,