* turn on autocapitalization for the address widgets; this fix the bug once autocapit...
[modest] / src / widgets / modest-recpt-editor.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <config.h>
31
32 #include <glib/gi18n-lib.h>
33
34 #include <gtk/gtkscrolledwindow.h>
35 #include <gtk/gtktextview.h>
36 #include <gtk/gtkimage.h>
37 #include <gtk/gtkbutton.h>
38
39 #include <modest-text-utils.h>
40 #include <modest-recpt-editor.h>
41 #include <modest-scroll-text.h>
42 #include <pango/pango-attributes.h>
43 #include <string.h>
44 #include <gdk/gdkkeysyms.h>
45 #include <gtk/gtk.h>
46
47 static GObjectClass *parent_class = NULL;
48
49 #define RECIPIENT_TAG_ID "recpt-id"
50
51 /* signals */
52 enum {
53         OPEN_ADDRESSBOOK_SIGNAL,
54         LAST_SIGNAL
55 };
56
57 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
58
59 struct _ModestRecptEditorPrivate
60 {
61         GtkWidget *text_view;
62         GtkWidget *abook_button;
63         GtkWidget *scrolled_window;
64         gchar *recipients;
65
66 };
67
68 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o)      \
69         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
70
71 static guint signals[LAST_SIGNAL] = {0};
72
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);
77
78 /* widget events */
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,
86                                              GdkEventFocus *event,
87                                              ModestRecptEditor *editor);
88 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
89                                                 GtkTextIter *location,
90                                                 gchar *text,
91                                                 gint len,
92                                                 ModestRecptEditor *editor);
93 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
94                                                           GdkEventKey *key,
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
104 /**
105  * modest_recpt_editor_new:
106  *
107  * Return value: a new #ModestRecptEditor instance implemented for Gtk+
108  **/
109 GtkWidget*
110 modest_recpt_editor_new (void)
111 {
112         ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR, 
113                                                 "homogeneous", FALSE,
114                                                 "spacing", 1,
115                                                 NULL);
116
117         return GTK_WIDGET (self);
118 }
119
120 void
121 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
122 {
123         ModestRecptEditorPrivate *priv;
124         GtkTextBuffer *buffer = NULL;
125
126         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
127         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
128
129         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
130
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);
136
137 }
138
139 void
140 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
141 {
142         ModestRecptEditorPrivate *priv;
143         GtkTextBuffer *buffer = NULL;
144         GtkTextIter iter;
145         gchar * string_to_add;
146
147         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
148         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
149
150         if (recipients == NULL)
151                 return;
152
153         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
154
155         if (gtk_text_buffer_get_char_count (buffer) > 0) {
156                 string_to_add = g_strconcat (";\n", recipients, NULL);
157         } else {
158                 string_to_add = g_strdup (recipients);
159         }
160
161         gtk_text_buffer_get_end_iter (buffer, &iter);
162
163         g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
164
165         gtk_text_buffer_insert (buffer, &iter, recipients, -1);
166         
167         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
168
169         if (GTK_WIDGET_REALIZED (recpt_editor))
170                 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
171
172 }
173
174 void 
175 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
176 {
177         ModestRecptEditorPrivate *priv;
178         GtkTextBuffer *buffer = NULL;
179         GtkTextIter start, end, iter;
180         GtkTextTag *tag = NULL;
181         GSList *node;
182         gboolean is_first_recipient = TRUE;
183       
184         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
185         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
186
187         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
188
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;
193
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);
199                         else 
200                                 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
201                 }
202                 g_free (buffer_contents);
203         }
204
205         gtk_text_buffer_get_end_iter (buffer, &iter);
206
207         tag = gtk_text_buffer_create_tag (buffer, NULL, 
208                                           "underline", PANGO_UNDERLINE_SINGLE,
209                                           "wrap-mode", GTK_WRAP_NONE,
210                                           "editable", TRUE, NULL);
211
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);
214
215         for (node = email_list; node != NULL; node = g_slist_next (node)) {
216                 gchar *recipient = (gchar *) node->data;
217
218                 if ((recipient) && (strlen (recipient) != 0)) {
219
220                         if (!is_first_recipient)
221                         gtk_text_buffer_insert (buffer, &iter, "\n", -1);
222
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;
226                 }
227         }
228         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
229
230         modest_recpt_editor_move_cursor_to_end (recpt_editor);
231
232 }
233
234 void 
235 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor, 
236                                                      GtkTextIter *start, GtkTextIter *end,
237                                                      GSList *email_list, const gchar * recipient_id)
238 {
239         ModestRecptEditorPrivate *priv;
240         GtkTextBuffer *buffer;
241         GtkTextTag *tag;
242         GSList *node;
243         gboolean is_first_recipient = TRUE;
244
245         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
246         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
247
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);
250
251         gtk_text_buffer_delete (buffer, start, end);
252
253         tag = gtk_text_buffer_create_tag (buffer, NULL, 
254                                           "underline", PANGO_UNDERLINE_SINGLE,
255                                           "wrap-mode", GTK_WRAP_NONE,
256                                           "editable", TRUE, NULL);
257
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);
260
261         for (node = email_list; node != NULL; node = g_slist_next (node)) {
262                 gchar *recipient = (gchar *) node->data;
263
264                 if ((recipient) && (strlen (recipient) != 0)) {
265
266                         if (!is_first_recipient)
267                         gtk_text_buffer_insert (buffer, start, "\n", -1);
268
269                         gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
270
271                         if (node->next != NULL)
272                                 gtk_text_buffer_insert (buffer, start, ";", -1);
273                         is_first_recipient = FALSE;
274                 }
275         }
276         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
277
278 }
279
280
281 const gchar *
282 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
283 {
284         ModestRecptEditorPrivate *priv;
285         GtkTextBuffer *buffer = NULL;
286         GtkTextIter start, end;
287         gchar *c;
288
289         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
290         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
291
292         if (priv->recipients != NULL) {
293                 g_free (priv->recipients);
294                 priv->recipients = NULL;
295         }
296
297         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
298
299         gtk_text_buffer_get_start_iter (buffer, &start);
300         gtk_text_buffer_get_end_iter (buffer, &end);
301
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)) {
304                 if (*c == '\n') {
305                         *c = ' ';
306                 }
307         }
308
309         return priv->recipients;
310
311 }
312
313 static void
314 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
315 {
316         ModestRecptEditorPrivate *priv;
317         GtkWidget *abook_icon;
318         GtkTextBuffer *buffer;
319
320         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
321
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);
329
330         priv->text_view = gtk_text_view_new ();
331         
332 /* on maemo, explicitly turn on autocaps */
333 #ifdef  MODEST_PLATFORM_MAEMO
334         hildon_gtk_text_view_set_input_mode (GTK_TEXT_VIEW(priv->text_view), HILDON_GTK_INPUT_MODE_AUTOCAP);
335 #endif  /*MODEST_PLATFORM_MAEMO_0*/
336         
337         priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5);
338         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
339                                         GTK_POLICY_AUTOMATIC);
340         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
341 /*      gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
342
343         gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
344 /*      gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
345         gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
346
347         gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
348         gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
349         gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
350
351         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
352         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
353
354         gtk_widget_set_size_request (priv->text_view, 75, -1);
355
356         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
357         g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
358         g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
359         g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
360         g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
361         g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
362
363 /*      gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
364
365         return;
366 }
367
368 void
369 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
370 {
371         ModestRecptEditorPrivate *priv;
372
373         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
374         g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
375         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
376
377         gtk_size_group_add_widget (size_group, priv->scrolled_window);
378 }
379
380 GtkTextBuffer *
381 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
382 {
383         ModestRecptEditorPrivate *priv;
384
385         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
386         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
387
388         return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
389 }
390
391 static void
392 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
393 {
394         g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
395
396         g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
397 }
398
399 static gboolean
400 modest_recpt_editor_on_button_release_event (GtkWidget *widget,
401                                              GdkEventButton *event,
402                                              ModestRecptEditor *recpt_editor)
403 {
404         ModestRecptEditorPrivate *priv;
405         GtkTextIter location, start, end;
406         GtkTextMark *mark;
407         GtkTextBuffer *buffer;
408         GtkTextTag *tag;
409         gboolean selection_changed = FALSE;
410         
411         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
412
413         buffer = modest_recpt_editor_get_buffer (recpt_editor);
414         mark = gtk_text_buffer_get_insert (buffer);
415         gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
416
417         gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
418
419         tag = iter_has_recipient (&start);
420         if (tag != NULL)
421                 if (!gtk_text_iter_begins_tag (&start, tag)) {
422                         gtk_text_iter_backward_to_tag_toggle (&start, tag);
423                         selection_changed = TRUE;
424                 } 
425
426         tag = iter_has_recipient (&end);
427         if (tag != NULL) 
428                 if (!gtk_text_iter_ends_tag (&end, tag)) {
429                         gtk_text_iter_forward_to_tag_toggle (&end, tag);
430                         selection_changed = TRUE;
431                 }
432
433         gtk_text_buffer_select_range (buffer, &start, &end);
434
435         return FALSE;
436 }
437
438 static void 
439 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
440                                  GdkEventFocus *event,
441                                  ModestRecptEditor *editor)
442 {
443         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
444         gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
445 }
446
447 static void 
448 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
449                                     GtkTextIter *location,
450                                     gchar *text,
451                                     gint len,
452                                     ModestRecptEditor *editor)
453 {
454         GtkTextIter prev;
455         gunichar prev_char;
456         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
457
458         if (iter_has_recipient (location)) {
459                 gtk_text_buffer_get_end_iter (buffer, location);
460                 gtk_text_buffer_place_cursor (buffer, location);
461         }
462
463         if (gtk_text_iter_is_start (location))
464                 return;
465
466         if (gtk_text_iter_is_end (location)) {
467                 prev = *location;
468                 if (!gtk_text_iter_backward_char (&prev))
469                         return;
470                 prev_char = gtk_text_iter_get_char (&prev);
471                 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
472                 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
473                         GtkTextMark *insert;
474                         gtk_text_buffer_insert (buffer, location, "\n",-1);
475                         insert = gtk_text_buffer_get_insert (buffer);
476                         gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
477                 }
478                 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
479                 
480         }
481
482 }
483
484 static GtkTextTag *
485 iter_has_recipient (GtkTextIter *iter)
486 {
487         GSList *tags, *node;
488         GtkTextTag *result = NULL;
489
490         tags = gtk_text_iter_get_tags (iter);
491
492         for (node = tags; node != NULL; node = g_slist_next (node)) {
493                 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
494
495                 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
496                         result = tag;
497                         break;
498                 }
499         }
500         g_slist_free (tags);
501         return result;
502 }
503
504 static GtkTextTag *
505 prev_iter_has_recipient (GtkTextIter *iter)
506 {
507         GtkTextIter prev;
508
509         prev = *iter;
510         gtk_text_iter_backward_char (&prev);
511         return iter_has_recipient (&prev);
512 }
513
514 /* static GtkTextTag * */
515 /* next_iter_has_recipient (GtkTextIter *iter) */
516 /* { */
517 /*      GtkTextIter next; */
518
519 /*      next = *iter; */
520 /*      return iter_has_recipient (&next); */
521 /* } */
522
523 static gunichar 
524 iter_previous_char (GtkTextIter *iter)
525 {
526         GtkTextIter prev;
527
528         prev = *iter;
529         gtk_text_iter_backward_char (&prev);
530         return gtk_text_iter_get_char (&prev);
531 }
532
533 /* static gunichar  */
534 /* iter_next_char (GtkTextIter *iter) */
535 /* { */
536 /*      GtkTextIter next; */
537
538 /*      next = *iter; */
539 /*      gtk_text_iter_forward_char (&next); */
540 /*      return gtk_text_iter_get_char (&next); */
541 /* } */
542
543 static void
544 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
545 {
546         GtkTextIter start, end;
547
548         start = *iter;
549         if (!gtk_text_iter_begins_tag (&start, tag))
550                 gtk_text_iter_backward_to_tag_toggle (&start, tag);
551         end = *iter;
552         if (!gtk_text_iter_ends_tag (&end, tag))
553                 gtk_text_iter_forward_to_tag_toggle (&end, tag);
554         gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
555 }
556
557 static gboolean 
558 quote_opened (GtkTextIter *iter)
559 {
560         GtkTextIter start;
561         GtkTextBuffer *buffer;
562         gboolean opened = FALSE;
563
564         buffer = gtk_text_iter_get_buffer (iter);
565         gtk_text_buffer_get_start_iter (buffer, &start);
566
567         while (!gtk_text_iter_equal (&start, iter)) {
568                 gunichar current_char = gtk_text_iter_get_char (&start);
569                 if (current_char == '"')
570                         opened = !opened;
571                 else if (current_char == '\\')
572                         gtk_text_iter_forward_char (&start);
573                 if (!gtk_text_iter_equal (&start, iter))
574                         gtk_text_iter_forward_char (&start);
575                         
576         }
577         return opened;
578
579 }
580
581
582 static gboolean
583 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
584                                           GdkEventKey *key,
585                                           ModestRecptEditor *editor)
586 {
587         GtkTextMark *insert;
588         GtkTextBuffer * buffer;
589         GtkTextIter location;
590         GtkTextTag *tag;
591      
592         buffer = gtk_text_view_get_buffer (text_view);
593         insert = gtk_text_buffer_get_insert (buffer);
594
595         /* cases to cover:
596          *    * cursor is on resolved recipient:
597          *        - right should go to first character after the recipient (usually ; or ,)
598          *        - left should fo to the first character before the recipient
599          *        - return should run check names on the recipient.
600          *    * cursor is just after a recipient:
601          *        - right should go to the next character. If it's a recipient, should select
602          *          it
603          *        - left should go to the previous character. If it's a recipient, should go
604          *          to the first character of the recipient, and select it.
605          *    * cursor is on arbitrary text:
606          *        - return should add a ; and go to the next line
607          *        - left or right standard ones.
608          *    * cursor is after a \n:
609          *        - left should go to the character before the \n (as if \n was not a character)
610          *    * cursor is before a \n:
611          *        - right should go to the character after the \n
612          */
613
614         gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
615
616         switch (key->keyval) {
617         case GDK_Left:
618         case GDK_KP_Left: 
619         {
620                 gboolean cursor_ready = FALSE;
621                 while (!cursor_ready) {
622                         if (iter_previous_char (&location) == '\n') {
623                                 gtk_text_iter_backward_char (&location);
624                         } else {
625                                 cursor_ready = TRUE;
626                         }
627                 }
628                 tag = iter_has_recipient (&location);
629                 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
630                         select_tag_of_iter (&location, tag);
631                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
632                         return TRUE;
633                 }
634                 gtk_text_iter_backward_char (&location);
635                 tag = iter_has_recipient (&location);
636                 if (tag != NULL)
637                         select_tag_of_iter (&location, tag);
638                 else {
639                         gtk_text_buffer_place_cursor (buffer, &location);
640                 }
641                 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
642
643                 return TRUE;
644         }
645         break;
646         case GDK_Right:
647         case GDK_KP_Right:
648         {
649                 gboolean cursor_moved = FALSE;
650
651                 tag = iter_has_recipient (&location);
652                 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
653                         gtk_text_iter_forward_to_tag_toggle (&location, tag);
654                         while (gtk_text_iter_get_char (&location) == '\n')
655                                 gtk_text_iter_forward_char (&location);
656                         gtk_text_buffer_place_cursor (buffer, &location);
657                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
658                         return TRUE;
659                 }
660
661                 while (gtk_text_iter_get_char (&location) == '\n') {
662                         gtk_text_iter_forward_char (&location);
663                         cursor_moved = TRUE;
664                 }
665                 if (!cursor_moved)
666                         gtk_text_iter_forward_char (&location);
667                 while (gtk_text_iter_get_char (&location) == '\n') {
668                         gtk_text_iter_forward_char (&location);
669                         cursor_moved = TRUE;
670                 }
671
672                 tag = iter_has_recipient (&location);
673                 if (tag != NULL)
674                         select_tag_of_iter (&location, tag);
675                 else
676                         gtk_text_buffer_place_cursor (buffer, &location);
677                 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
678                 return TRUE;
679         }
680         break;
681         case GDK_Return:
682         case GDK_KP_Enter:
683         {
684                 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
685                 tag = iter_has_recipient (&location);
686                 if (tag != NULL) {
687                         gtk_text_buffer_get_end_iter (buffer, &location);
688                         gtk_text_buffer_place_cursor (buffer, &location);
689                         if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
690                                 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
691                         gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
692                 } else {
693                         gunichar prev_char = iter_previous_char (&location);
694                         if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
695                             ||(prev_char == ';')||(prev_char == ',')) 
696                                 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
697                         else {
698                                 if ((prev_char != ';') && (prev_char != ','))
699                                         gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
700                                 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
701                         }
702                 }
703                 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
704                 return TRUE;
705         }
706         break;
707         case GDK_BackSpace:
708         {
709                 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
710                 if (gtk_text_buffer_get_has_selection (buffer)) {
711                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
712                         return TRUE;
713                 }
714                 #else
715                 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
716                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
717                         return TRUE;
718                 }
719                 #endif
720
721                 tag = prev_iter_has_recipient (&location);
722                 if (tag != NULL) {
723                         GtkTextIter iter_in_tag;
724                         iter_in_tag = location;
725                         gtk_text_iter_backward_char (&iter_in_tag);
726                         select_tag_of_iter (&iter_in_tag, tag);
727                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
728                         return TRUE;
729                 }
730                 return FALSE;
731         }
732         break;
733         default:
734                 return FALSE;
735         }
736 }
737
738 static void 
739 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
740 {
741         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
742         GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
743         GtkTextIter start, end;
744
745         gtk_text_buffer_get_end_iter (buffer, &start);
746         end = start;
747         gtk_text_buffer_select_range (buffer, &start, &end);
748
749
750 }
751
752 void
753 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
754 {
755         ModestRecptEditorPrivate *priv;
756         
757         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
758         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
759
760         gtk_widget_grab_focus (priv->text_view);
761 }
762
763 static void
764 modest_recpt_editor_finalize (GObject *object)
765 {
766         (*parent_class->finalize) (object);
767
768         return;
769 }
770
771 static void 
772 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
773 {
774         GObjectClass *object_class;
775         GtkWidgetClass *widget_class;
776
777         parent_class = g_type_class_peek_parent (klass);
778         object_class = (GObjectClass*) klass;
779         widget_class = GTK_WIDGET_CLASS (klass);
780
781         object_class->finalize = modest_recpt_editor_finalize;
782
783         g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
784
785         signals[OPEN_ADDRESSBOOK_SIGNAL] = 
786                 g_signal_new ("open-addressbook",
787                               G_TYPE_FROM_CLASS (object_class),
788                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
789                               G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
790                               NULL, NULL,
791                               g_cclosure_marshal_VOID__VOID,
792                               G_TYPE_NONE, 0);
793
794         return;
795 }
796
797 GType 
798 modest_recpt_editor_get_type (void)
799 {
800         static GType type = 0;
801
802         if (G_UNLIKELY(type == 0))
803         {
804                 static const GTypeInfo info = 
805                 {
806                   sizeof (ModestRecptEditorClass),
807                   NULL,   /* base_init */
808                   NULL,   /* base_finalize */
809                   (GClassInitFunc) modest_recpt_editor_class_init,   /* class_init */
810                   NULL,   /* class_finalize */
811                   NULL,   /* class_data */
812                   sizeof (ModestRecptEditor),
813                   0,      /* n_preallocs */
814                   modest_recpt_editor_instance_init    /* instance_init */
815                 };
816
817                 type = g_type_register_static (GTK_TYPE_HBOX,
818                         "ModestRecptEditor",
819                         &info, 0);
820
821         }
822
823         return type;
824 }