add undo/redo patch submitted by Jens Oberender
[claws.git] / src / textview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkvbox.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtksignal.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "intl.h"
39 #include "main.h"
40 #include "summaryview.h"
41 #include "procheader.h"
42 #include "prefs_common.h"
43 #include "codeconv.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "procmime.h"
47 #include "html.h"
48 #include "compose.h"
49 #include "addressbook.h"
50 #include "displayheader.h"
51
52 #define FONT_LOAD(font, s) \
53 { \
54         gchar *fontstr, *p; \
55  \
56         Xstrdup_a(fontstr, s, ); \
57         if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
58         font = gdk_font_load(fontstr); \
59 }
60
61 typedef struct _RemoteURI       RemoteURI;
62
63 struct _RemoteURI
64 {
65         gchar *uri;
66
67         guint start;
68         guint end;
69 };
70
71 static GdkColor quote_colors[3] = {
72         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
73         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
74         {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
75 };
76
77 static GdkColor uri_color = {
78         (gulong)0,
79         (gushort)0,
80         (gushort)0,
81         (gushort)0
82 };
83
84 static GdkColor emphasis_color = {
85         (gulong)0,
86         (gushort)0,
87         (gushort)0,
88         (gushort)0xcfff
89 };
90
91 static GdkColor error_color = {
92         (gulong)0,
93         (gushort)0xefff,
94         (gushort)0,
95         (gushort)0
96 };
97
98 static GdkFont *spacingfont;
99
100 static void textview_show_html          (TextView       *textview,
101                                          FILE           *fp,
102                                          CodeConverter  *conv);
103 static void textview_write_line         (TextView       *textview,
104                                          const gchar    *str,
105                                          CodeConverter  *conv);
106 static void textview_write_link         (TextView       *textview,
107                                          const gchar    *url,
108                                          const gchar    *str,
109                                          CodeConverter  *conv);
110 static GPtrArray *textview_scan_header  (TextView       *textview,
111                                          FILE           *fp);
112 static void textview_show_header        (TextView       *textview,
113                                          GPtrArray      *headers);
114
115 static void textview_key_pressed        (GtkWidget      *widget,
116                                          GdkEventKey    *event,
117                                          TextView       *textview);
118 static void textview_button_pressed     (GtkWidget      *widget,
119                                          GdkEventButton *event,
120                                          TextView       *textview);
121
122 static void textview_uri_list_remove_all(GSList         *uri_list);
123
124 static void textview_smooth_scroll_do           (TextView       *textview,
125                                                  gfloat          old_value,
126                                                  gfloat          last_value,
127                                                  gint            step);
128 static void textview_smooth_scroll_one_line     (TextView       *textview,
129                                                  gboolean        up);
130 static gboolean textview_smooth_scroll_page     (TextView       *textview,
131                                                  gboolean        up);
132
133
134 TextView *textview_create(void)
135 {
136         TextView *textview;
137         GtkWidget *vbox;
138         GtkWidget *scrolledwin_sb;
139         GtkWidget *scrolledwin_mb;
140         GtkWidget *text_sb;
141         GtkWidget *text_mb;
142
143         debug_print(_("Creating text view...\n"));
144         textview = g_new0(TextView, 1);
145
146         scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
147         scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
148         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
149                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
150         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
151                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
152         gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
153         gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
154
155         /* create GtkText widgets for single-byte and multi-byte character */
156         text_sb = gtk_text_new(NULL, NULL);
157         text_mb = gtk_text_new(NULL, NULL);
158         GTK_TEXT(text_sb)->default_tab_width = 8;
159         GTK_TEXT(text_mb)->default_tab_width = 8;
160         gtk_widget_show(text_sb);
161         gtk_widget_show(text_mb);
162         gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
163         gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
164         gtk_widget_ensure_style(text_sb);
165         gtk_widget_ensure_style(text_mb);
166         if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
167                 GtkStyle *style;
168
169                 style = gtk_style_copy(text_sb->style);
170                 gdk_font_unref(style->font);
171                 FONT_LOAD(style->font, NORMAL_FONT);
172                 gtk_widget_set_style(text_sb, style);
173         }
174         if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
175                 GtkStyle *style;
176
177                 style = gtk_style_copy(text_mb->style);
178                 gdk_font_unref(style->font);
179                 style->font = gdk_fontset_load(NORMAL_FONT);
180                 gtk_widget_set_style(text_mb, style);
181         }
182         gtk_widget_ref(scrolledwin_sb);
183         gtk_widget_ref(scrolledwin_mb);
184
185         gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
186         gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
187         gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
188                            GTK_SIGNAL_FUNC(textview_key_pressed),
189                            textview);
190         gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_press_event",
191                                  GTK_SIGNAL_FUNC(textview_button_pressed),
192                                  textview);
193         gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
194                            GTK_SIGNAL_FUNC(textview_key_pressed),
195                            textview);
196         gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_press_event",
197                                  GTK_SIGNAL_FUNC(textview_button_pressed),
198                                  textview);
199
200         gtk_widget_show(scrolledwin_sb);
201         gtk_widget_show(scrolledwin_mb);
202
203         vbox = gtk_vbox_new(FALSE, 0);
204         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
205
206         textview->vbox           = vbox;
207         textview->scrolledwin    = scrolledwin_sb;
208         textview->scrolledwin_sb = scrolledwin_sb;
209         textview->scrolledwin_mb = scrolledwin_mb;
210         textview->text           = text_sb;
211         textview->text_sb        = text_sb;
212         textview->text_mb        = text_mb;
213         textview->text_is_mb     = FALSE;
214         textview->uri_list       = NULL;
215         textview->body_pos       = 0;
216         textview->cur_pos        = 0;
217
218         return textview;
219 }
220
221 void textview_init(TextView *textview)
222 {
223         gtkut_widget_disable_theme_engine(textview->text_sb);
224         gtkut_widget_disable_theme_engine(textview->text_mb);
225         textview_update_message_colors();
226         textview_set_font(textview, NULL);
227 }
228
229 void textview_update_message_colors(void)
230 {
231         GdkColor black = {0, 0, 0, 0};
232
233         if (prefs_common.enable_color) {
234                 /* grab the quote colors, converting from an int to a GdkColor */
235                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
236                                                &quote_colors[0]);
237                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
238                                                &quote_colors[1]);
239                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
240                                                &quote_colors[2]);
241                 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
242                                                &uri_color);
243         } else {
244                 quote_colors[0] = quote_colors[1] = quote_colors[2] = 
245                         uri_color = emphasis_color = black;
246         }
247 }
248
249 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
250                            const gchar *file)
251 {
252         FILE *fp;
253
254         if ((fp = fopen(file, "r")) == NULL) {
255                 FILE_OP_ERROR(file, "fopen");
256                 return;
257         }
258
259         textview_show_part(textview, mimeinfo, fp);
260
261         fclose(fp);
262 }
263
264 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
265 {
266         GtkText *text;
267         gchar buf[BUFFSIZE];
268         const gchar *boundary = NULL;
269         gint boundary_len = 0;
270         const gchar *charset = NULL;
271         FILE *tmpfp;
272         GPtrArray *headers = NULL;
273         CodeConverter *conv;
274
275         g_return_if_fail(mimeinfo != NULL);
276         g_return_if_fail(fp != NULL);
277
278         if (mimeinfo->mime_type == MIME_MULTIPART) {
279                 if (mimeinfo->sub) {
280                         mimeinfo = mimeinfo->sub;
281                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
282                                 perror("fseek");
283                                 return;
284                         }
285                 } else
286                         return;
287         }
288         if (mimeinfo->parent && mimeinfo->parent->boundary) {
289                 boundary = mimeinfo->parent->boundary;
290                 boundary_len = strlen(boundary);
291         }
292
293         if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
294                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
295                         perror("fseek");
296                 headers = textview_scan_header(textview, fp);
297         } else {
298                 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
299                         glong fpos;
300                         MimeInfo *parent = mimeinfo->parent;
301
302                         while (parent->parent)
303                                 parent = parent->parent;
304
305                         if ((fpos = ftell(fp)) < 0)
306                                 perror("ftell");
307                         else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
308                                 perror("fseek");
309                         else {
310                                 headers = textview_scan_header(textview, fp);
311                                 if (fseek(fp, fpos, SEEK_SET) < 0)
312                                         perror("fseek");
313                         }
314                 }
315                 while (fgets(buf, sizeof(buf), fp) != NULL)
316                         if (buf[0] == '\r' || buf[0] == '\n') break;
317         }
318
319         /* display attached RFC822 single text message */
320         if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
321                 if (headers) procheader_header_array_destroy(headers);
322                 if (!mimeinfo->sub || mimeinfo->sub->children) return;
323                 headers = textview_scan_header(textview, fp);
324                 mimeinfo = mimeinfo->sub;
325         } else if (!mimeinfo->parent &&
326                    mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
327                 if (headers) procheader_header_array_destroy(headers);
328                 if (!mimeinfo->sub) return;
329                 headers = textview_scan_header(textview, fp);
330                 mimeinfo = mimeinfo->sub;
331         }
332
333         if (prefs_common.force_charset)
334                 charset = prefs_common.force_charset;
335         else if (mimeinfo->charset)
336                 charset = mimeinfo->charset;
337         textview_set_font(textview, charset);
338
339         conv = conv_code_converter_new(charset);
340
341         textview_clear(textview);
342         text = GTK_TEXT(textview->text);
343         gtk_text_freeze(text);
344
345         textview->body_pos = 0;
346         textview->cur_pos  = 0;
347
348         if (headers) {
349                 textview_show_header(textview, headers);
350                 procheader_header_array_destroy(headers);
351         }
352
353         tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
354         if (tmpfp) {
355                 if (mimeinfo->mime_type == MIME_TEXT_HTML)
356                         textview_show_html(textview, tmpfp, conv);
357                 else
358                         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
359                                 textview_write_line(textview, buf, conv);
360                 fclose(tmpfp);
361         }
362
363         conv_code_converter_destroy(conv);
364
365         gtk_text_thaw(text);
366 }
367
368 #define TEXT_INSERT(str) \
369         gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
370
371 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
372 {
373         GtkText *text;
374
375         if (!partinfo) return;
376
377         textview_set_font(textview, NULL);
378         text = GTK_TEXT(textview->text);
379         textview_clear(textview);
380
381         gtk_text_freeze(text);
382
383         TEXT_INSERT(_("To save this part, pop up the context menu with "));
384         TEXT_INSERT(_("right click and select `Save as...', "));
385         TEXT_INSERT(_("or press `y' key.\n\n"));
386
387         TEXT_INSERT(_("To display this part as a text message, select "));
388         TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
389
390         TEXT_INSERT(_("To open this part with external program, select "));
391         TEXT_INSERT(_("`Open' or `Open with...', "));
392         TEXT_INSERT(_("or double-click, or click the center button, "));
393         TEXT_INSERT(_("or press `l' key."));
394
395         gtk_text_thaw(text);
396 }
397
398 #if USE_GPGME
399 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
400 {
401         GtkText *text;
402
403         if (!partinfo) return;
404
405         textview_set_font(textview, NULL);
406         text = GTK_TEXT(textview->text);
407         textview_clear(textview);
408
409         gtk_text_freeze(text);
410
411         if (partinfo->sigstatus_full == NULL) {
412                 TEXT_INSERT(_("This signature has not been checked yet.\n"));
413                 TEXT_INSERT(_("To check it, pop up the context menu with\n"));
414                 TEXT_INSERT(_("right click and select `Check signature'.\n"));
415         } else {
416                 TEXT_INSERT(partinfo->sigstatus_full);
417         }
418
419         gtk_text_thaw(text);
420 }
421 #endif /* USE_GPGME */
422
423 #undef TEXT_INSERT
424
425 static void textview_show_html(TextView *textview, FILE *fp,
426                                CodeConverter *conv)
427 {
428         HTMLParser *parser;
429         gchar *str;
430         gchar* url = NULL;
431
432         parser = html_parser_new(fp, conv);
433         g_return_if_fail(parser != NULL);
434
435         while ((str = html_parse(parser)) != NULL) {
436                 if (parser->state == HTML_HREF) {
437                         /* first time : get and copy the URL */
438                         if (url == NULL) {
439                                 /* ALF - the sylpheed html parser returns an empty string,
440                                  * if still inside an <a>, but already parsed past HREF */
441                                 str = strtok(str, " ");
442                                 if (str) { 
443                                         url = strdup(str);
444                                         /* the URL may (or not) be followed by the
445                                          * referenced text */
446                                         str = strtok(NULL, "");
447                                 }       
448                         }
449                         if (str != NULL) {
450                                 textview_write_link(textview, url, str, NULL);
451                         }
452                 } else {
453                         if (url != NULL) {
454                                 free(url);
455                                 url = NULL;
456                         }
457                         textview_write_line(textview, str, NULL);
458                 }
459         }
460         html_parser_destroy(parser);
461 }
462
463 /* get_uri_part() - retrieves a URI starting from scanpos.
464                     Returns TRUE if succesful */
465 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
466                              const gchar **bp, const gchar **ep)
467 {
468         const gchar *ep_;
469
470         g_return_val_if_fail(start != NULL, FALSE);
471         g_return_val_if_fail(scanpos != NULL, FALSE);
472         g_return_val_if_fail(bp != NULL, FALSE);
473         g_return_val_if_fail(ep != NULL, FALSE);
474
475         *bp = scanpos;
476
477         /* find end point of URI */
478         for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
479                 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
480                         break;
481         }
482
483         /* no punctuation at end of string */
484
485         /* FIXME: this stripping of trailing punctuations may bite with other URIs.
486          * should pass some URI type to this function and decide on that whether
487          * to perform punctuation stripping */
488
489 #define IS_REAL_PUNCT(ch)       (ispunct(ch) && ((ch) != '/')) 
490
491         for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
492                 ;
493
494 #undef IS_REAL_PUNCT
495
496         *ep = ep_;
497
498         return TRUE;            
499 }
500
501 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
502 {
503         return g_strndup(bp, ep - bp);
504 }
505
506 /* valid mail address characters */
507 #define IS_RFC822_CHAR(ch) \
508         (isascii(ch) && \
509          (ch) > 32   && \
510          (ch) != 127 && \
511          !isspace(ch) && \
512          !strchr("()<>\"", (ch)))
513
514 /* alphabet and number within 7bit ASCII */
515 #define IS_ASCII_ALNUM(ch)      (isascii(ch) && isalnum(ch))
516 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
517
518 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
519 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
520                                const gchar **bp, const gchar **ep)
521 {
522         /* more complex than the uri part because we need to scan back and forward starting from
523          * the scan position. */
524         gboolean result = FALSE;
525         const gchar *bp_ = NULL;
526         const gchar *ep_ = NULL;
527
528         /* the informative part of the email address (describing the name
529          * of the email address owner) may contain quoted parts. the
530          * closure stack stores the last encountered quotes. */
531         gchar closure_stack[128];
532         gchar *ptr = closure_stack;
533
534         g_return_val_if_fail(start != NULL, FALSE);
535         g_return_val_if_fail(scanpos != NULL, FALSE);
536         g_return_val_if_fail(bp != NULL, FALSE);
537         g_return_val_if_fail(ep != NULL, FALSE);
538
539         /* scan start of address */
540         for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
541                 ;
542
543         /* TODO: should start with an alnum? */
544         bp_++;
545         for (; bp_ < scanpos && !IS_ASCII_ALNUM(*bp_); bp_++)
546                 ;
547
548         if (bp_ != scanpos) {
549                 /* scan end of address */
550                 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
551                         ;
552
553                 /* TODO: really should terminate with an alnum? */
554                 for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
555                         ;
556                 ep_++;
557
558                 if (ep_ > scanpos + 1) {
559                         *ep = ep_;
560                         *bp = bp_;
561                         result = TRUE;
562                 }
563         }
564
565         if (!result) return FALSE;
566
567         /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
568         if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_)) 
569                 return FALSE;
570
571         /* see if this is <bracketed>; in this case we also scan for the informative part. */
572         if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
573                 return TRUE;
574
575 #define FULL_STACK()    ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
576 #define IN_STACK()      (ptr > closure_stack)
577 /* has underrun check */
578 #define POP_STACK()     if(IN_STACK()) --ptr
579 /* has overrun check */
580 #define PUSH_STACK(c)   if(!FULL_STACK()) *ptr++ = (c); else return TRUE
581 /* has underrun check */
582 #define PEEK_STACK()    (IN_STACK() ? *(ptr - 1) : 0)
583
584         ep_++;
585
586         /* scan for the informative part. */
587         for (bp_ -= 2; bp_ >= start; bp_--) {
588                 /* if closure on the stack keep scanning */
589                 if (PEEK_STACK() == *bp_) {
590                         POP_STACK();
591                         continue;
592                 }
593                 if (*bp_ == '\'' || *bp_ == '"') {
594                         PUSH_STACK(*bp_);
595                         continue;
596                 }
597
598                 /* if nothing in the closure stack, do the special conditions
599                  * the following if..else expression simply checks whether 
600                  * a token is acceptable. if not acceptable, the clause
601                  * should terminate the loop with a 'break' */
602                 if (!PEEK_STACK()) {
603                         if (*bp_ == '-'
604                         && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
605                         && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
606                                 /* hyphens are allowed, but only in
607                                    between alnums */
608                         } else if (!ispunct(*bp_)) {
609                                 /* but anything not being a punctiation
610                                    is ok */
611                         } else {
612                                 break; /* anything else is rejected */
613                         }
614                 }
615         }
616
617         bp_++;
618
619 #undef PEEK_STACK
620 #undef PUSH_STACK
621 #undef POP_STACK
622 #undef IN_STACK
623 #undef FULL_STACK
624
625         /* scan forward (should start with an alnum) */
626         for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
627                 ;
628
629         *ep = ep_;
630         *bp = bp_;
631
632         return result;
633 }
634
635 #undef IS_QUOTE
636 #undef IS_RFC822_CHAR
637
638 static gchar *make_email_string(const gchar *bp, const gchar *ep)
639 {
640         /* returns a mailto: URI; mailto: is also used to detect the
641          * uri type later on in the button_pressed signal handler */
642         gchar *tmp;
643         gchar *result;
644
645         tmp = g_strndup(bp, ep - bp);
646         result = g_strconcat("mailto:", tmp, NULL);
647         g_free(tmp);
648
649         return result;
650 }
651
652 #define ADD_TXT_POS(bp_, ep_, pti_) \
653         if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
654                 last = last->next; \
655                 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
656                 last->next = NULL; \
657         } else { \
658                 g_warning("alloc error scanning URIs\n"); \
659                 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
660                                 linebuf, -1); \
661                 return; \
662         }
663
664 /* textview_make_clickable_parts() - colorizes clickable parts */
665 static void textview_make_clickable_parts(TextView *textview,
666                                           GdkFont *font,
667                                           GdkColor *fg_color,
668                                           GdkColor *uri_color,
669                                           const gchar *linebuf)
670 {
671         /* parse table - in order of priority */
672         struct table {
673                 const gchar *needle; /* token */
674
675                 /* token search function */
676                 gchar    *(*search)     (const gchar *haystack,
677                                          const gchar *needle);
678                 /* part parsing function */
679                 gboolean  (*parse)      (const gchar *start,
680                                          const gchar *scanpos,
681                                          const gchar **bp_,
682                                          const gchar **ep_);
683                 /* part to URI function */
684                 gchar    *(*build_uri)  (const gchar *bp,
685                                          const gchar *ep);
686         };
687
688         static struct table parser[] = {
689                 {"http://",  strcasestr, get_uri_part,   make_uri_string},
690                 {"https://", strcasestr, get_uri_part,   make_uri_string},
691                 {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
692                 {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
693                 {"@",        strcasestr, get_email_part, make_email_string}
694         };
695         const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
696
697         gint  n;
698         const gchar *walk, *bp, *ep;
699
700         struct txtpos {
701                 const gchar     *bp, *ep;       /* text position */
702                 gint             pti;           /* index in parse table */
703                 struct txtpos   *next;          /* next */
704         } head = {NULL, NULL, 0,  NULL}, *last = &head;
705
706         GtkText *text = GTK_TEXT(textview->text);
707
708         /* parse for clickable parts, and build a list of begin and end positions  */
709         for (walk = linebuf, n = 0;;) {
710                 gint last_index = PARSE_ELEMS;
711                 gchar *scanpos = NULL;
712
713                 /* FIXME: this looks phony. scanning for anything in the parse table */
714                 for (n = 0; n < PARSE_ELEMS; n++) {
715                         gchar *tmp;
716
717                         tmp = parser[n].search(walk, parser[n].needle);
718                         if (tmp) {
719                                 if (scanpos == NULL || tmp < scanpos) {
720                                         scanpos = tmp;
721                                         last_index = n;
722                                 }
723                         }                                       
724                 }
725
726                 if (scanpos) {
727                         /* check if URI can be parsed */
728                         if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
729                             && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
730                                         ADD_TXT_POS(bp, ep, last_index);
731                                         walk = ep;
732                         } else
733                                 walk = scanpos +
734                                         strlen(parser[last_index].needle);
735                 } else
736                         break;
737         }
738
739         /* colorize this line */
740         if (head.next) {
741                 const gchar *normal_text = linebuf;
742
743                 /* insert URIs */
744                 for (last = head.next; last != NULL;
745                      normal_text = last->ep, last = last->next) {
746                         RemoteURI *uri;
747
748                         uri = g_new(RemoteURI, 1);
749                         if (last->bp - normal_text > 0)
750                                 gtk_text_insert(text, font,
751                                                 fg_color, NULL,
752                                                 normal_text,
753                                                 last->bp - normal_text);
754                         uri->uri = parser[last->pti].build_uri(last->bp, 
755                                                                last->ep);
756                         uri->start = gtk_text_get_point(text);
757                         gtk_text_insert(text, font, uri_color,
758                                         NULL, last->bp, last->ep - last->bp);
759                         uri->end = gtk_text_get_point(text);
760                         textview->uri_list =
761                                 g_slist_append(textview->uri_list, uri);
762                 }
763
764                 if (*normal_text)
765                         gtk_text_insert(text, font, fg_color,
766                                         NULL, normal_text, -1);
767         } else
768                 gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
769 }
770
771 #undef ADD_TXT_POS
772
773 /* This function writes str as a double-clickable link with the given url. */ 
774 static void textview_write_link(TextView *textview, const gchar *url,
775                                 const gchar *str, CodeConverter *conv)
776 {
777     GdkColor *link_color = NULL;
778     RemoteURI* uri;
779     GtkText *text = GTK_TEXT(textview->text);
780     gchar buf[BUFFSIZE];
781
782     /* this part is taken from textview_write_line. Right now the only place
783      * that calls this function passes NULL for conv, but you never know. */
784     if (!conv)
785             strncpy2(buf, str, sizeof(buf));
786     else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
787             gtk_text_insert(text, textview->msgfont,
788                             prefs_common.enable_color
789                             ? &error_color : NULL, NULL,
790                             "*** Warning: code conversion failed ***\n",
791                             -1);
792             return;
793     }
794
795     /* this part is based on the code in make_clickable_parts */
796     if (prefs_common.enable_color) {
797         link_color = &uri_color;
798     }
799     uri = g_new(RemoteURI, 1);
800     uri->uri = g_strdup(url);
801     uri->start = gtk_text_get_point(text);
802     gtk_text_insert(text, textview->msgfont, link_color, NULL, buf,
803                     strlen(buf));
804     uri->end = gtk_text_get_point(text);
805     textview->uri_list = g_slist_append(textview->uri_list, uri);
806 }
807
808 static void textview_write_line(TextView *textview, const gchar *str,
809                                 CodeConverter *conv)
810 {
811         GtkText *text = GTK_TEXT(textview->text);
812         gchar buf[BUFFSIZE];
813         GdkColor *fg_color;
814         gint quotelevel = -1;
815
816         if (!conv)
817                 strncpy2(buf, str, sizeof(buf));
818         else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
819                 gtk_text_insert(text, textview->msgfont,
820                                 prefs_common.enable_color
821                                 ? &error_color : NULL, NULL,
822                                 "*** Warning: code conversion failed ***\n",
823                                 -1);
824                 return;
825         }
826
827         strcrchomp(buf);
828         if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
829         fg_color = NULL;
830
831         /* change color of quotation
832            >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
833            Up to 3 levels of quotations are detected, and each
834            level is colored using a different color. */
835         if (prefs_common.enable_color && strchr(buf, '>')) {
836                 quotelevel = get_quote_level(buf);
837
838                 /* set up the correct foreground color */
839                 if (quotelevel > 2) {
840                         /* recycle colors */
841                         if (prefs_common.recycle_quote_colors)
842                                 quotelevel %= 3;
843                         else
844                                 quotelevel = 2;
845                 }
846         }
847
848         if (quotelevel == -1)
849                 fg_color = NULL;
850         else
851                 fg_color = &quote_colors[quotelevel];
852
853         if (prefs_common.head_space && spacingfont && buf[0] != '\n')
854                 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
855
856         if (prefs_common.enable_color)
857                 textview_make_clickable_parts(textview, textview->msgfont,
858                                               fg_color, &uri_color, buf);
859         else
860                 textview_make_clickable_parts(textview, textview->msgfont,
861                                               fg_color, NULL, buf);
862 }
863
864 void textview_clear(TextView *textview)
865 {
866         GtkText *text = GTK_TEXT(textview->text);
867
868         gtk_text_freeze(text);
869         gtk_text_set_point(text, 0);
870         gtk_text_forward_delete(text, gtk_text_get_length(text));
871         gtk_text_thaw(text);
872
873         textview_uri_list_remove_all(textview->uri_list);
874         textview->uri_list = NULL;
875 }
876
877 void textview_destroy(TextView *textview)
878 {
879         textview_uri_list_remove_all(textview->uri_list);
880         textview->uri_list = NULL;
881
882         if (!textview->scrolledwin_sb->parent)
883                 gtk_widget_destroy(textview->scrolledwin_sb);
884         if (!textview->scrolledwin_mb->parent)
885                 gtk_widget_destroy(textview->scrolledwin_mb);
886
887         if (textview->msgfont) {
888                 textview->msgfont->ascent = textview->prev_ascent;
889                 textview->msgfont->descent = textview->prev_descent;
890                 gdk_font_unref(textview->msgfont);
891         }
892         if (textview->boldfont)
893                 gdk_font_unref(textview->boldfont);
894
895         g_free(textview);
896 }
897
898 void textview_set_font(TextView *textview, const gchar *codeset)
899 {
900         gboolean use_fontset = TRUE;
901
902         /* In multi-byte mode, GtkText can't display 8bit characters
903            correctly, so it must be single-byte mode. */
904         if (MB_CUR_MAX > 1) {
905                 if (codeset) {
906                         if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
907                             !g_strcasecmp(codeset, "BALTIC"))
908                                 use_fontset = FALSE;
909                         else if (conv_get_current_charset() != C_EUC_JP &&
910                                  (!g_strncasecmp(codeset, "KOI8-", 5) ||
911                                   !g_strncasecmp(codeset, "CP", 2)    ||
912                                   !g_strncasecmp(codeset, "WINDOWS-", 8)))
913                                 use_fontset = FALSE;
914                 }
915         } else
916                 use_fontset = FALSE;
917
918         if (textview->text_is_mb && !use_fontset) {
919                 GtkWidget *parent;
920
921                 parent = textview->scrolledwin_mb->parent;
922                 gtk_editable_select_region
923                         (GTK_EDITABLE(textview->text_mb), 0, 0);
924                 gtk_container_remove(GTK_CONTAINER(parent),
925                                      textview->scrolledwin_mb);
926                 gtk_container_add(GTK_CONTAINER(parent),
927                                   textview->scrolledwin_sb);
928
929                 textview->text = textview->text_sb;
930                 textview->text_is_mb = FALSE;
931         } else if (!textview->text_is_mb && use_fontset) {
932                 GtkWidget *parent;
933
934                 parent = textview->scrolledwin_sb->parent;
935                 gtk_editable_select_region
936                         (GTK_EDITABLE(textview->text_sb), 0, 0);
937                 gtk_container_remove(GTK_CONTAINER(parent),
938                                      textview->scrolledwin_sb);
939                 gtk_container_add(GTK_CONTAINER(parent),
940                                   textview->scrolledwin_mb);
941
942                 textview->text = textview->text_mb;
943                 textview->text_is_mb = TRUE;
944         }
945
946         if (prefs_common.textfont) {
947                 if (textview->msgfont) {
948                         textview->msgfont->ascent = textview->prev_ascent;
949                         textview->msgfont->descent = textview->prev_descent;
950                         gdk_font_unref(textview->msgfont);
951                         textview->msgfont = NULL;
952                 }
953                 if (use_fontset)
954                         textview->msgfont =
955                                 gdk_fontset_load(prefs_common.textfont);
956                 else {
957                         if (MB_CUR_MAX > 1) {
958                                 FONT_LOAD(textview->msgfont,
959                                           "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
960                         } else {
961                                 FONT_LOAD(textview->msgfont,
962                                           prefs_common.textfont);
963                         }
964                 }
965
966                 if (textview->msgfont) {
967                         gint ascent, descent;
968
969                         textview->prev_ascent = textview->msgfont->ascent;
970                         textview->prev_descent = textview->msgfont->descent;
971                         descent = prefs_common.line_space / 2;
972                         ascent  = prefs_common.line_space - descent;
973                         textview->msgfont->ascent  += ascent;
974                         textview->msgfont->descent += descent;
975                 }
976         }
977
978         if (!textview->boldfont)
979                 FONT_LOAD(textview->boldfont, BOLD_FONT);
980         if (!spacingfont)
981                 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
982 }
983
984 enum
985 {
986         H_DATE          = 0,
987         H_FROM          = 1,
988         H_TO            = 2,
989         H_NEWSGROUPS    = 3,
990         H_SUBJECT       = 4,
991         H_CC            = 5,
992         H_REPLY_TO      = 6,
993         H_FOLLOWUP_TO   = 7,
994         H_X_MAILER      = 8,
995         H_X_NEWSREADER  = 9,
996         H_USER_AGENT    = 10,
997         H_ORGANIZATION  = 11,
998 };
999
1000 void textview_set_position(TextView *textview, gint pos)
1001 {
1002         if (pos < 0) {
1003                 textview->cur_pos =
1004                         gtk_text_get_length(GTK_TEXT(textview->text));
1005         } else {
1006                 textview->cur_pos = pos;
1007         }
1008 }
1009
1010 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
1011 {
1012         gchar buf[BUFFSIZE];
1013         GPtrArray *headers, *sorted_headers;
1014         GSList *disphdr_list;
1015         Header *header;
1016         gint i;
1017
1018         g_return_val_if_fail(fp != NULL, NULL);
1019
1020         if (!prefs_common.display_header) {
1021                 while (fgets(buf, sizeof(buf), fp) != NULL)
1022                         if (buf[0] == '\r' || buf[0] == '\n') break;
1023                 return NULL;
1024         }
1025
1026         headers = procheader_get_header_array_asis(fp);
1027
1028         sorted_headers = g_ptr_array_new();
1029
1030         for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
1031              disphdr_list = disphdr_list->next) {
1032                 DisplayHeaderProp *dp =
1033                         (DisplayHeaderProp *)disphdr_list->data;
1034
1035                 for (i = 0; i < headers->len; i++) {
1036                         header = g_ptr_array_index(headers, i);
1037
1038                         if (procheader_headername_equal(header->name,
1039                                                         dp->name)) {
1040                                 if (dp->hidden)
1041                                         procheader_header_free(header);
1042                                 else
1043                                         g_ptr_array_add(sorted_headers, header);
1044
1045                                 g_ptr_array_remove_index(headers, i);
1046                                 i--;
1047                         }
1048                 }
1049         }
1050
1051         if (prefs_common.show_other_header) {
1052                 for (i = 0; i < headers->len; i++) {
1053                         header = g_ptr_array_index(headers, i);
1054                         g_ptr_array_add(sorted_headers, header);
1055                 }
1056         }
1057
1058         g_ptr_array_free(headers, FALSE);
1059
1060         return sorted_headers;
1061 }
1062
1063 static void textview_show_header(TextView *textview, GPtrArray *headers)
1064 {
1065         GtkText *text = GTK_TEXT(textview->text);
1066         Header *header;
1067         gint i;
1068
1069         g_return_if_fail(headers != NULL);
1070
1071         gtk_text_freeze(text);
1072
1073         for (i = 0; i < headers->len; i++) {
1074                 header = g_ptr_array_index(headers, i);
1075                 g_return_if_fail(header->name != NULL);
1076
1077                 gtk_text_insert(text, textview->boldfont, NULL, NULL,
1078                                 header->name, -1);
1079                 if (header->name[strlen(header->name) - 1] != ' ')
1080                         gtk_text_insert(text, textview->boldfont,
1081                                         NULL, NULL, " ", 1);
1082
1083                 if (procheader_headername_equal(header->name, "Subject") ||
1084                     procheader_headername_equal(header->name, "From")    ||
1085                     procheader_headername_equal(header->name, "To")      ||
1086                     procheader_headername_equal(header->name, "Cc"))
1087                         unfold_line(header->body);
1088
1089                 if (prefs_common.enable_color &&
1090                     (procheader_headername_equal(header->name, "X-Mailer") ||
1091                      procheader_headername_equal(header->name,
1092                                                  "X-Newsreader")) &&
1093                     strstr(header->body, "Sylpheed") != NULL)
1094                         gtk_text_insert(text, NULL, &emphasis_color, NULL,
1095                                         header->body, -1);
1096                 else if (prefs_common.enable_color) {
1097                         textview_make_clickable_parts(textview,
1098                                                       NULL, NULL, &uri_color,
1099                                                       header->body);
1100                 } else {
1101                         textview_make_clickable_parts(textview,
1102                                                       NULL, NULL, NULL,
1103                                                       header->body);
1104                 }
1105                 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1106         }
1107
1108         gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1109         gtk_text_thaw(text);
1110         textview->body_pos = gtk_text_get_length(text);
1111 }
1112
1113 gboolean textview_search_string(TextView *textview, const gchar *str,
1114                                 gboolean case_sens)
1115 {
1116         GtkText *text = GTK_TEXT(textview->text);
1117         gint pos;
1118         wchar_t *wcs;
1119         gint len;
1120         gint text_len;
1121         gboolean found = FALSE;
1122
1123         g_return_val_if_fail(str != NULL, FALSE);
1124
1125         wcs = strdup_mbstowcs(str);
1126         g_return_val_if_fail(wcs != NULL, FALSE);
1127         len = wcslen(wcs);
1128         pos = textview->cur_pos;
1129         if (pos < textview->body_pos)
1130                 pos = textview->body_pos;
1131         text_len = gtk_text_get_length(text);
1132         if (text_len - pos < len) {
1133                 g_free(wcs);
1134                 return FALSE;
1135         }
1136
1137         for (; pos < text_len; pos++) {
1138                 if (text_len - pos < len) break;
1139                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1140                     == TRUE) {
1141                         gtk_editable_set_position(GTK_EDITABLE(text),
1142                                                   pos + len);
1143                         gtk_editable_select_region(GTK_EDITABLE(text),
1144                                                    pos, pos + len);
1145                         textview_set_position(textview, pos + len);
1146                         found = TRUE;
1147                         break;
1148                 }
1149                 if (text_len - pos == len) break;
1150         }
1151
1152         g_free(wcs);
1153         return found;
1154 }
1155
1156 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1157                                          gboolean case_sens)
1158 {
1159         GtkText *text = GTK_TEXT(textview->text);
1160         gint pos;
1161         wchar_t *wcs;
1162         gint len;
1163         gint text_len;
1164         gboolean found = FALSE;
1165
1166         g_return_val_if_fail(str != NULL, FALSE);
1167
1168         wcs = strdup_mbstowcs(str);
1169         g_return_val_if_fail(wcs != NULL, FALSE);
1170         len = wcslen(wcs);
1171         pos = textview->cur_pos;
1172         text_len = gtk_text_get_length(text);
1173         if (text_len - textview->body_pos < len) {
1174                 g_free(wcs);
1175                 return FALSE;
1176         }
1177         if (pos <= textview->body_pos || text_len - pos < len)
1178                 pos = text_len - len;
1179
1180         for (; pos >= textview->body_pos; pos--) {
1181                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1182                     == TRUE) {
1183                         gtk_editable_set_position(GTK_EDITABLE(text), pos);
1184                         gtk_editable_select_region(GTK_EDITABLE(text),
1185                                                    pos, pos + len);
1186                         textview_set_position(textview, pos - 1);
1187                         found = TRUE;
1188                         break;
1189                 }
1190                 if (pos == textview->body_pos) break;
1191         }
1192
1193         g_free(wcs);
1194         return found;
1195 }
1196
1197 void textview_scroll_one_line(TextView *textview, gboolean up)
1198 {
1199         GtkText *text = GTK_TEXT(textview->text);
1200         gfloat upper;
1201
1202         if (prefs_common.enable_smooth_scroll) {
1203                 textview_smooth_scroll_one_line(textview, up);
1204                 return;
1205         }
1206
1207         if (!up) {
1208                 upper = text->vadj->upper - text->vadj->page_size;
1209                 if (text->vadj->value < upper) {
1210                         text->vadj->value +=
1211                                 text->vadj->step_increment * 4;
1212                         text->vadj->value =
1213                                 MIN(text->vadj->value, upper);
1214                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1215                                                 "value_changed");
1216                 }
1217         } else {
1218                 if (text->vadj->value > 0.0) {
1219                         text->vadj->value -=
1220                                 text->vadj->step_increment * 4;
1221                         text->vadj->value =
1222                                 MAX(text->vadj->value, 0.0);
1223                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1224                                                 "value_changed");
1225                 }
1226         }
1227 }
1228
1229 gboolean textview_scroll_page(TextView *textview, gboolean up)
1230 {
1231         GtkText *text = GTK_TEXT(textview->text);
1232         gfloat upper;
1233         gfloat page_incr;
1234
1235         if (prefs_common.enable_smooth_scroll)
1236                 return textview_smooth_scroll_page(textview, up);
1237
1238         if (prefs_common.scroll_halfpage)
1239                 page_incr = text->vadj->page_increment / 2;
1240         else
1241                 page_incr = text->vadj->page_increment;
1242
1243         if (!up) {
1244                 upper = text->vadj->upper - text->vadj->page_size;
1245                 if (text->vadj->value < upper) {
1246                         text->vadj->value += page_incr;
1247                         text->vadj->value = MIN(text->vadj->value, upper);
1248                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1249                                                 "value_changed");
1250                 } else
1251                         return FALSE;
1252         } else {
1253                 if (text->vadj->value > 0.0) {
1254                         text->vadj->value -= page_incr;
1255                         text->vadj->value = MAX(text->vadj->value, 0.0);
1256                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1257                                                 "value_changed");
1258                 } else
1259                         return FALSE;
1260         }
1261
1262         return TRUE;
1263 }
1264
1265 static void textview_smooth_scroll_do(TextView *textview,
1266                                       gfloat old_value, gfloat last_value,
1267                                       gint step)
1268 {
1269         GtkText *text = GTK_TEXT(textview->text);
1270         gint change_value;
1271         gboolean up;
1272         gint i;
1273
1274         if (old_value < last_value) {
1275                 change_value = last_value - old_value;
1276                 up = FALSE;
1277         } else {
1278                 change_value = old_value - last_value;
1279                 up = TRUE;
1280         }
1281
1282         gdk_key_repeat_disable();
1283
1284         for (i = step; i <= change_value; i += step) {
1285                 text->vadj->value = old_value + (up ? -i : i);
1286                 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1287                                         "value_changed");
1288         }
1289
1290         text->vadj->value = last_value;
1291         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1292
1293         gdk_key_repeat_restore();
1294 }
1295
1296 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1297 {
1298         GtkText *text = GTK_TEXT(textview->text);
1299         gfloat upper;
1300         gfloat old_value;
1301         gfloat last_value;
1302
1303         if (!up) {
1304                 upper = text->vadj->upper - text->vadj->page_size;
1305                 if (text->vadj->value < upper) {
1306                         old_value = text->vadj->value;
1307                         last_value = text->vadj->value +
1308                                 text->vadj->step_increment * 4;
1309                         last_value = MIN(last_value, upper);
1310
1311                         textview_smooth_scroll_do(textview, old_value,
1312                                                   last_value,
1313                                                   prefs_common.scroll_step);
1314                 }
1315         } else {
1316                 if (text->vadj->value > 0.0) {
1317                         old_value = text->vadj->value;
1318                         last_value = text->vadj->value -
1319                                 text->vadj->step_increment * 4;
1320                         last_value = MAX(last_value, 0.0);
1321
1322                         textview_smooth_scroll_do(textview, old_value,
1323                                                   last_value,
1324                                                   prefs_common.scroll_step);
1325                 }
1326         }
1327 }
1328
1329 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1330 {
1331         GtkText *text = GTK_TEXT(textview->text);
1332         gfloat upper;
1333         gfloat page_incr;
1334         gfloat old_value;
1335         gfloat last_value;
1336
1337         if (prefs_common.scroll_halfpage)
1338                 page_incr = text->vadj->page_increment / 2;
1339         else
1340                 page_incr = text->vadj->page_increment;
1341
1342         if (!up) {
1343                 upper = text->vadj->upper - text->vadj->page_size;
1344                 if (text->vadj->value < upper) {
1345                         old_value = text->vadj->value;
1346                         last_value = text->vadj->value + page_incr;
1347                         last_value = MIN(last_value, upper);
1348
1349                         textview_smooth_scroll_do(textview, old_value,
1350                                                   last_value,
1351                                                   prefs_common.scroll_step);
1352                 } else
1353                         return FALSE;
1354         } else {
1355                 if (text->vadj->value > 0.0) {
1356                         old_value = text->vadj->value;
1357                         last_value = text->vadj->value - page_incr;
1358                         last_value = MAX(last_value, 0.0);
1359
1360                         textview_smooth_scroll_do(textview, old_value,
1361                                                   last_value,
1362                                                   prefs_common.scroll_step);
1363                 } else
1364                         return FALSE;
1365         }
1366
1367         return TRUE;
1368 }
1369
1370 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1371                                  TextView *textview)
1372 {
1373         SummaryView *summaryview = NULL;
1374
1375         if (!event) return;
1376         if (textview->messageview->mainwin)
1377                 summaryview = textview->messageview->mainwin->summaryview;
1378
1379         switch (event->keyval) {
1380         case GDK_Tab:
1381         case GDK_Home:
1382         case GDK_Left:
1383         case GDK_Up:
1384         case GDK_Right:
1385         case GDK_Down:
1386         case GDK_Page_Up:
1387         case GDK_Page_Down:
1388         case GDK_End:
1389         case GDK_Control_L:
1390         case GDK_Control_R:
1391                 break;
1392         case GDK_space:
1393                 if (summaryview)
1394                         summary_pass_key_press_event(summaryview, event);
1395                 else
1396                         textview_scroll_page(textview, FALSE);
1397                 break;
1398         case GDK_BackSpace:
1399                 textview_scroll_page(textview, TRUE);
1400                 break;
1401         case GDK_Return:
1402                 textview_scroll_one_line(textview,
1403                                          (event->state & GDK_MOD1_MASK) != 0);
1404                 break;
1405         default:
1406                 if (summaryview)
1407                         summary_pass_key_press_event(summaryview, event);
1408                 break;
1409         }
1410 }
1411
1412 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1413                                     TextView *textview)
1414 {
1415         textview->cur_pos = 
1416                 gtk_editable_get_position(GTK_EDITABLE(textview->text));
1417
1418         if (event &&
1419             ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1420              || event->button == 2 || event->button == 3)) {
1421                 GSList *cur;
1422
1423                 /* double click seems to set the cursor after the current
1424                  * word. The cursor position needs fixing, otherwise the
1425                  * last word of a clickable zone will not work */
1426                 if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
1427                         textview->cur_pos--;
1428                 }
1429
1430                 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1431                         RemoteURI *uri = (RemoteURI *)cur->data;
1432
1433                         if (textview->cur_pos >= uri->start &&
1434                             textview->cur_pos <  uri->end) {
1435                                 if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1436                                         if (event->button == 3) {
1437                                                 gchar *fromname, *fromaddress;
1438                                                 /* extract url */
1439                                                 fromaddress = g_strdup(uri->uri + 7);
1440                                                 /* Hiroyuki: please put this function in utils.c! */
1441                                                 fromname = procheader_get_fromname(fromaddress);
1442                                                 extract_address(fromaddress);
1443                                                 g_message("adding from textview %s <%s>", fromname, fromaddress);
1444                                                 // Add to address book - Match
1445                                                 addressbook_add_contact( fromname, fromaddress, NULL );
1446                                                 g_free(fromaddress);
1447                                                 g_free(fromname);
1448                                         } else {
1449                                                 compose_new_with_recipient
1450                                                         (NULL, uri->uri + 7);
1451                                         }
1452                                 } else {
1453                                         open_uri(uri->uri,
1454                                                  prefs_common.uri_cmd);
1455                                 }
1456                         }
1457                 }
1458         }
1459 }
1460
1461 static void textview_uri_list_remove_all(GSList *uri_list)
1462 {
1463         GSList *cur;
1464
1465         for (cur = uri_list; cur != NULL; cur = cur->next) {
1466                 if (cur->data) {
1467                         g_free(((RemoteURI *)cur->data)->uri);
1468                         g_free(cur->data);
1469                 }
1470         }
1471
1472         g_slist_free(uri_list);
1473 }