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);
103 static gboolean is_valid_insert (const gchar *text, gint len);
104 static gchar *create_valid_text (const gchar *text, gint len);
107 * modest_recpt_editor_new:
109 * Return value: a new #ModestRecptEditor instance implemented for Gtk+
112 modest_recpt_editor_new (void)
114 ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR,
115 "homogeneous", FALSE,
119 return GTK_WIDGET (self);
123 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
125 ModestRecptEditorPrivate *priv;
126 GtkTextBuffer *buffer = NULL;
127 gchar *valid_recipients = NULL;
129 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
130 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
132 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
134 valid_recipients = create_valid_text (recipients, -1);
135 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
136 gtk_text_buffer_set_text (buffer, valid_recipients, -1);
137 g_free (valid_recipients);
138 if (GTK_WIDGET_REALIZED (recpt_editor))
139 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
140 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
145 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
147 ModestRecptEditorPrivate *priv;
148 GtkTextBuffer *buffer = NULL;
150 gchar * string_to_add;
152 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
153 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
155 if (recipients == NULL)
158 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
160 if (gtk_text_buffer_get_char_count (buffer) > 0) {
161 string_to_add = g_strconcat (";\n", recipients, NULL);
163 string_to_add = g_strdup (recipients);
166 gtk_text_buffer_get_end_iter (buffer, &iter);
168 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
170 gtk_text_buffer_insert (buffer, &iter, recipients, -1);
172 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
174 if (GTK_WIDGET_REALIZED (recpt_editor))
175 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
180 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
182 ModestRecptEditorPrivate *priv;
183 GtkTextBuffer *buffer = NULL;
184 GtkTextIter start, end, iter;
185 GtkTextTag *tag = NULL;
187 gboolean is_first_recipient = TRUE;
189 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
190 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
192 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
194 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
195 gtk_text_buffer_get_bounds (buffer, &start, &end);
196 if (gtk_text_buffer_get_char_count (buffer) > 0) {
197 gchar * buffer_contents;
199 gtk_text_buffer_get_bounds (buffer, &start, &end);
200 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
201 if (!g_str_has_suffix (buffer_contents, "\n")) {
202 if (g_str_has_suffix (buffer_contents, ";")||(g_str_has_suffix (buffer_contents, ",")))
203 gtk_text_buffer_insert (buffer, &end, "\n", -1);
205 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
207 g_free (buffer_contents);
210 gtk_text_buffer_get_end_iter (buffer, &iter);
212 tag = gtk_text_buffer_create_tag (buffer, NULL,
213 "underline", PANGO_UNDERLINE_SINGLE,
214 "wrap-mode", GTK_WRAP_NONE,
215 "editable", TRUE, NULL);
217 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
218 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
220 for (node = email_list; node != NULL; node = g_slist_next (node)) {
221 gchar *recipient = (gchar *) node->data;
223 if ((recipient) && (strlen (recipient) != 0)) {
225 if (!is_first_recipient)
226 gtk_text_buffer_insert (buffer, &iter, "\n", -1);
228 gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
229 gtk_text_buffer_insert (buffer, &iter, ";", -1);
230 is_first_recipient = FALSE;
233 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
235 modest_recpt_editor_move_cursor_to_end (recpt_editor);
240 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor,
241 GtkTextIter *start, GtkTextIter *end,
242 GSList *email_list, const gchar * recipient_id)
244 ModestRecptEditorPrivate *priv;
245 GtkTextBuffer *buffer;
248 gboolean is_first_recipient = TRUE;
250 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
251 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
253 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
254 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
256 gtk_text_buffer_delete (buffer, start, end);
258 tag = gtk_text_buffer_create_tag (buffer, NULL,
259 "underline", PANGO_UNDERLINE_SINGLE,
260 "wrap-mode", GTK_WRAP_NONE,
261 "editable", TRUE, NULL);
263 g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
264 g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
266 for (node = email_list; node != NULL; node = g_slist_next (node)) {
267 gchar *recipient = (gchar *) node->data;
269 if ((recipient) && (strlen (recipient) != 0)) {
271 if (!is_first_recipient)
272 gtk_text_buffer_insert (buffer, start, "\n", -1);
274 gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
276 if (node->next != NULL)
277 gtk_text_buffer_insert (buffer, start, ";", -1);
278 is_first_recipient = FALSE;
281 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
287 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
289 ModestRecptEditorPrivate *priv;
290 GtkTextBuffer *buffer = NULL;
291 GtkTextIter start, end;
294 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
295 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
297 if (priv->recipients != NULL) {
298 g_free (priv->recipients);
299 priv->recipients = NULL;
302 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
304 gtk_text_buffer_get_start_iter (buffer, &start);
305 gtk_text_buffer_get_end_iter (buffer, &end);
307 priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
308 for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
314 return priv->recipients;
319 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
321 ModestRecptEditorPrivate *priv;
322 GtkWidget *abook_icon;
323 GtkTextBuffer *buffer;
325 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
327 priv->abook_button = gtk_button_new ();
328 gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
329 gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
330 GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
331 gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
332 abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
333 gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
335 priv->text_view = gtk_text_view_new ();
336 priv->recipients = NULL;
338 priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5);
339 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
340 GTK_POLICY_AUTOMATIC);
341 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
342 /* gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
344 gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
345 /* gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
346 gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
348 gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
349 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
350 gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
352 gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
353 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
355 gtk_widget_set_size_request (priv->text_view, 75, -1);
357 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
358 g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
359 g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
360 g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
361 g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
362 g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
364 /* gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
370 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
372 ModestRecptEditorPrivate *priv;
374 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
375 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
376 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
378 gtk_size_group_add_widget (size_group, priv->scrolled_window);
382 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
384 ModestRecptEditorPrivate *priv;
386 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
387 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
389 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
393 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
395 g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
397 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
401 modest_recpt_editor_on_button_release_event (GtkWidget *widget,
402 GdkEventButton *event,
403 ModestRecptEditor *recpt_editor)
405 ModestRecptEditorPrivate *priv;
406 GtkTextIter location, start, end;
408 GtkTextBuffer *buffer;
410 gboolean selection_changed = FALSE;
412 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
414 buffer = modest_recpt_editor_get_buffer (recpt_editor);
415 mark = gtk_text_buffer_get_insert (buffer);
416 gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
418 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
420 tag = iter_has_recipient (&start);
422 if (!gtk_text_iter_begins_tag (&start, tag)) {
423 gtk_text_iter_backward_to_tag_toggle (&start, tag);
424 selection_changed = TRUE;
427 tag = iter_has_recipient (&end);
429 if (!gtk_text_iter_ends_tag (&end, tag)) {
430 gtk_text_iter_forward_to_tag_toggle (&end, tag);
431 selection_changed = TRUE;
434 gtk_text_buffer_select_range (buffer, &start, &end);
440 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
441 GdkEventFocus *event,
442 ModestRecptEditor *editor)
444 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
445 gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
449 is_valid_insert (const gchar *text, gint len)
454 const gchar *current, *next_current;
459 while (((len == -1)||(i < len)) && (*current != '\0')) {
460 c = g_utf8_get_char (current);
461 next_current = g_utf8_next_char (current);
462 if (next_current && *next_current != '\0')
463 next_c = g_utf8_get_char (g_utf8_next_char (current));
466 if (c == g_utf8_get_char(",") || c == g_utf8_get_char (";")) {
467 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
470 if (c == 0x2022 || c == 0xfffc ||
471 c == g_utf8_get_char ("\n") ||
472 c == g_utf8_get_char ("\t"))
474 current = g_utf8_next_char (current);
481 create_valid_text (const gchar *text, gint len)
487 const gchar *current, *next_current;
492 str = g_string_new ("");
495 while (((len == -1)||(i < len)) && (*current != '\0')) {
496 c = g_utf8_get_char (current);
497 next_current = g_utf8_next_char (current);
498 if (next_current && *next_current != '\0')
499 next_c = g_utf8_get_char (g_utf8_next_char (current));
502 if (c != 0x2022 && c != 0xfffc &&
503 c != g_utf8_get_char ("\n") &&
504 c != g_utf8_get_char ("\t"))
505 str = g_string_append_unichar (str, c);
506 if (c == g_utf8_get_char(",") || c == g_utf8_get_char (";")) {
507 if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
508 str = g_string_append_c (str, '\n');
510 current = g_utf8_next_char (current);
513 return g_string_free (str, FALSE);
517 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
518 GtkTextIter *location,
521 ModestRecptEditor *editor)
525 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
527 if (!is_valid_insert (text, len)) {
528 gchar *new_text = create_valid_text (text, len);
529 g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
530 gtk_text_buffer_insert (buffer, location, new_text, -1);
534 if (iter_has_recipient (location)) {
535 gtk_text_buffer_get_end_iter (buffer, location);
536 gtk_text_buffer_place_cursor (buffer, location);
539 if (gtk_text_iter_is_start (location))
542 if (gtk_text_iter_is_end (location)) {
544 if (!gtk_text_iter_backward_char (&prev))
546 prev_char = gtk_text_iter_get_char (&prev);
547 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
548 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
550 gtk_text_buffer_insert (buffer, location, "\n",-1);
551 insert = gtk_text_buffer_get_insert (buffer);
552 gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
554 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
561 iter_has_recipient (GtkTextIter *iter)
564 GtkTextTag *result = NULL;
566 tags = gtk_text_iter_get_tags (iter);
568 for (node = tags; node != NULL; node = g_slist_next (node)) {
569 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
571 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
581 prev_iter_has_recipient (GtkTextIter *iter)
586 gtk_text_iter_backward_char (&prev);
587 return iter_has_recipient (&prev);
590 /* static GtkTextTag * */
591 /* next_iter_has_recipient (GtkTextIter *iter) */
593 /* GtkTextIter next; */
596 /* return iter_has_recipient (&next); */
600 iter_previous_char (GtkTextIter *iter)
605 gtk_text_iter_backward_char (&prev);
606 return gtk_text_iter_get_char (&prev);
609 /* static gunichar */
610 /* iter_next_char (GtkTextIter *iter) */
612 /* GtkTextIter next; */
615 /* gtk_text_iter_forward_char (&next); */
616 /* return gtk_text_iter_get_char (&next); */
620 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
622 GtkTextIter start, end;
625 if (!gtk_text_iter_begins_tag (&start, tag))
626 gtk_text_iter_backward_to_tag_toggle (&start, tag);
628 if (!gtk_text_iter_ends_tag (&end, tag))
629 gtk_text_iter_forward_to_tag_toggle (&end, tag);
630 gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
634 quote_opened (GtkTextIter *iter)
637 GtkTextBuffer *buffer;
638 gboolean opened = FALSE;
640 buffer = gtk_text_iter_get_buffer (iter);
641 gtk_text_buffer_get_start_iter (buffer, &start);
643 while (!gtk_text_iter_equal (&start, iter)) {
644 gunichar current_char = gtk_text_iter_get_char (&start);
645 if (current_char == '"')
647 else if (current_char == '\\')
648 gtk_text_iter_forward_char (&start);
649 if (!gtk_text_iter_equal (&start, iter))
650 gtk_text_iter_forward_char (&start);
659 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
661 ModestRecptEditor *editor)
664 GtkTextBuffer * buffer;
665 GtkTextIter location;
668 buffer = gtk_text_view_get_buffer (text_view);
669 insert = gtk_text_buffer_get_insert (buffer);
672 * * cursor is on resolved recipient:
673 * - right should go to first character after the recipient (usually ; or ,)
674 * - left should fo to the first character before the recipient
675 * - return should run check names on the recipient.
676 * * cursor is just after a recipient:
677 * - right should go to the next character. If it's a recipient, should select
679 * - left should go to the previous character. If it's a recipient, should go
680 * to the first character of the recipient, and select it.
681 * * cursor is on arbitrary text:
682 * - return should add a ; and go to the next line
683 * - left or right standard ones.
684 * * cursor is after a \n:
685 * - left should go to the character before the \n (as if \n was not a character)
686 * * cursor is before a \n:
687 * - right should go to the character after the \n
690 gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
692 switch (key->keyval) {
696 gboolean cursor_ready = FALSE;
697 while (!cursor_ready) {
698 if (iter_previous_char (&location) == '\n') {
699 gtk_text_iter_backward_char (&location);
704 tag = iter_has_recipient (&location);
705 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
706 select_tag_of_iter (&location, tag);
707 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
710 gtk_text_iter_backward_char (&location);
711 tag = iter_has_recipient (&location);
713 select_tag_of_iter (&location, tag);
715 gtk_text_buffer_place_cursor (buffer, &location);
717 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
725 gboolean cursor_moved = FALSE;
727 tag = iter_has_recipient (&location);
728 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
729 gtk_text_iter_forward_to_tag_toggle (&location, tag);
730 while (gtk_text_iter_get_char (&location) == '\n')
731 gtk_text_iter_forward_char (&location);
732 gtk_text_buffer_place_cursor (buffer, &location);
733 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
737 while (gtk_text_iter_get_char (&location) == '\n') {
738 gtk_text_iter_forward_char (&location);
742 gtk_text_iter_forward_char (&location);
743 while (gtk_text_iter_get_char (&location) == '\n') {
744 gtk_text_iter_forward_char (&location);
748 tag = iter_has_recipient (&location);
750 select_tag_of_iter (&location, tag);
752 gtk_text_buffer_place_cursor (buffer, &location);
753 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
760 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
761 tag = iter_has_recipient (&location);
763 gtk_text_buffer_get_end_iter (buffer, &location);
764 gtk_text_buffer_place_cursor (buffer, &location);
765 if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
766 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
767 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
769 gunichar prev_char = iter_previous_char (&location);
770 if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
771 ||(prev_char == ';')||(prev_char == ','))
772 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
774 if ((prev_char != ';') && (prev_char != ','))
775 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
776 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
779 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
785 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
786 if (gtk_text_buffer_get_has_selection (buffer)) {
787 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
791 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
792 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
797 tag = prev_iter_has_recipient (&location);
799 GtkTextIter iter_in_tag;
800 iter_in_tag = location;
801 gtk_text_iter_backward_char (&iter_in_tag);
802 select_tag_of_iter (&iter_in_tag, tag);
803 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
815 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
817 ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
818 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
819 GtkTextIter start, end;
821 gtk_text_buffer_get_end_iter (buffer, &start);
823 gtk_text_buffer_select_range (buffer, &start, &end);
829 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
831 ModestRecptEditorPrivate *priv;
833 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
834 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
836 gtk_widget_grab_focus (priv->text_view);
840 modest_recpt_editor_finalize (GObject *object)
842 ModestRecptEditorPrivate *priv;
843 priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
845 if (priv->recipients) {
846 g_free (priv->recipients);
847 priv->recipients = NULL;
850 (*parent_class->finalize) (object);
856 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
858 GObjectClass *object_class;
859 GtkWidgetClass *widget_class;
861 parent_class = g_type_class_peek_parent (klass);
862 object_class = (GObjectClass*) klass;
863 widget_class = GTK_WIDGET_CLASS (klass);
865 object_class->finalize = modest_recpt_editor_finalize;
867 g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
869 signals[OPEN_ADDRESSBOOK_SIGNAL] =
870 g_signal_new ("open-addressbook",
871 G_TYPE_FROM_CLASS (object_class),
872 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
873 G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
875 g_cclosure_marshal_VOID__VOID,
882 modest_recpt_editor_get_type (void)
884 static GType type = 0;
886 if (G_UNLIKELY(type == 0))
888 static const GTypeInfo info =
890 sizeof (ModestRecptEditorClass),
891 NULL, /* base_init */
892 NULL, /* base_finalize */
893 (GClassInitFunc) modest_recpt_editor_class_init, /* class_init */
894 NULL, /* class_finalize */
895 NULL, /* class_data */
896 sizeof (ModestRecptEditor),
898 modest_recpt_editor_instance_init /* instance_init */
901 type = g_type_register_static (GTK_TYPE_HBOX,