2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
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>
40 #include "summaryview.h"
41 #include "procheader.h"
42 #include "prefs_common.h"
49 #include "addressbook.h"
50 #include "displayheader.h"
52 #define FONT_LOAD(font, s) \
56 Xstrdup_a(fontstr, s, ); \
57 if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
58 font = gdk_font_load(fontstr); \
61 typedef struct _RemoteURI RemoteURI;
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}
77 static GdkColor uri_color = {
84 static GdkColor emphasis_color = {
91 static GdkColor error_color = {
98 static GdkFont *spacingfont;
100 static void textview_show_html (TextView *textview,
102 CodeConverter *conv);
103 static void textview_write_line (TextView *textview,
105 CodeConverter *conv);
106 static GPtrArray *textview_scan_header (TextView *textview,
108 static void textview_show_header (TextView *textview,
111 static void textview_key_pressed (GtkWidget *widget,
114 static void textview_button_pressed (GtkWidget *widget,
115 GdkEventButton *event,
118 static void textview_uri_list_remove_all(GSList *uri_list);
120 static void textview_smooth_scroll_do (TextView *textview,
124 static void textview_smooth_scroll_one_line (TextView *textview,
126 static gboolean textview_smooth_scroll_page (TextView *textview,
130 TextView *textview_create(void)
134 GtkWidget *scrolledwin_sb;
135 GtkWidget *scrolledwin_mb;
139 debug_print(_("Creating text view...\n"));
140 textview = g_new0(TextView, 1);
142 scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
143 scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
144 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
145 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
146 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
147 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
148 gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
149 gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
151 /* create GtkText widgets for single-byte and multi-byte character */
152 text_sb = gtk_text_new(NULL, NULL);
153 text_mb = gtk_text_new(NULL, NULL);
154 gtk_widget_show(text_sb);
155 gtk_widget_show(text_mb);
156 gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
157 gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
158 gtk_widget_ensure_style(text_sb);
159 gtk_widget_ensure_style(text_mb);
160 if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
163 style = gtk_style_copy(text_sb->style);
164 gdk_font_unref(style->font);
165 FONT_LOAD(style->font, NORMAL_FONT);
166 gtk_widget_set_style(text_sb, style);
168 if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
171 style = gtk_style_copy(text_mb->style);
172 gdk_font_unref(style->font);
173 style->font = gdk_fontset_load(NORMAL_FONT);
174 gtk_widget_set_style(text_mb, style);
176 gtk_widget_ref(scrolledwin_sb);
177 gtk_widget_ref(scrolledwin_mb);
179 gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
180 gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
181 gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
182 GTK_SIGNAL_FUNC(textview_key_pressed),
184 gtk_signal_connect(GTK_OBJECT(text_sb), "button_press_event",
185 GTK_SIGNAL_FUNC(textview_button_pressed),
187 gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
188 GTK_SIGNAL_FUNC(textview_key_pressed),
190 gtk_signal_connect(GTK_OBJECT(text_mb), "button_press_event",
191 GTK_SIGNAL_FUNC(textview_button_pressed),
194 gtk_widget_show(scrolledwin_sb);
195 gtk_widget_show(scrolledwin_mb);
197 vbox = gtk_vbox_new(FALSE, 0);
198 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
200 textview->vbox = vbox;
201 textview->scrolledwin = scrolledwin_sb;
202 textview->scrolledwin_sb = scrolledwin_sb;
203 textview->scrolledwin_mb = scrolledwin_mb;
204 textview->text = text_sb;
205 textview->text_sb = text_sb;
206 textview->text_mb = text_mb;
207 textview->text_is_mb = FALSE;
208 textview->uri_list = NULL;
213 void textview_init(TextView *textview)
215 gtkut_widget_disable_theme_engine(textview->text_sb);
216 gtkut_widget_disable_theme_engine(textview->text_mb);
217 textview_update_message_colors();
218 textview_set_font(textview, NULL);
221 void textview_update_message_colors(void)
223 GdkColor black = {0, 0, 0, 0};
225 if (prefs_common.enable_color) {
226 /* grab the quote colors, converting from an int to a GdkColor */
227 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
229 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
231 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
233 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
236 quote_colors[0] = quote_colors[1] = quote_colors[2] =
237 uri_color = emphasis_color = black;
241 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
246 if ((fp = fopen(file, "r")) == NULL) {
247 FILE_OP_ERROR(file, "fopen");
251 textview_show_part(textview, mimeinfo, fp);
256 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
260 const gchar *boundary = NULL;
261 gint boundary_len = 0;
262 const gchar *charset = NULL;
264 GPtrArray *headers = NULL;
267 g_return_if_fail(mimeinfo != NULL);
268 g_return_if_fail(fp != NULL);
270 if (mimeinfo->mime_type == MIME_MULTIPART) {
272 mimeinfo = mimeinfo->sub;
273 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
280 if (mimeinfo->parent && mimeinfo->parent->boundary) {
281 boundary = mimeinfo->parent->boundary;
282 boundary_len = strlen(boundary);
285 if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
286 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
288 headers = textview_scan_header(textview, fp);
290 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
293 if ((fpos = ftell(fp)) < 0)
295 else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
299 headers = textview_scan_header(textview, fp);
300 if (fseek(fp, fpos, SEEK_SET) < 0)
304 while (fgets(buf, sizeof(buf), fp) != NULL)
305 if (buf[0] == '\r' || buf[0] == '\n') break;
308 /* display attached RFC822 single text message */
309 if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
310 if (headers) procheader_header_array_destroy(headers);
311 if (!mimeinfo->sub || mimeinfo->sub->children) return;
312 headers = textview_scan_header(textview, fp);
313 mimeinfo = mimeinfo->sub;
314 } else if (!mimeinfo->parent &&
315 mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
316 if (headers) procheader_header_array_destroy(headers);
317 if (!mimeinfo->sub) return;
318 headers = textview_scan_header(textview, fp);
319 mimeinfo = mimeinfo->sub;
322 if (prefs_common.force_charset)
323 charset = prefs_common.force_charset;
324 else if (mimeinfo->charset)
325 charset = mimeinfo->charset;
326 textview_set_font(textview, charset);
328 conv = conv_code_converter_new(charset);
330 textview_clear(textview);
331 text = GTK_TEXT(textview->text);
332 gtk_text_freeze(text);
335 textview_show_header(textview, headers);
336 procheader_header_array_destroy(headers);
339 tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
341 if (mimeinfo->mime_type == MIME_TEXT_HTML)
342 textview_show_html(textview, tmpfp, conv);
344 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
345 textview_write_line(textview, buf, conv);
349 conv_code_converter_destroy(conv);
354 #define TEXT_INSERT(str) \
355 gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
357 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
361 if (!partinfo) return;
363 textview_set_font(textview, NULL);
364 text = GTK_TEXT(textview->text);
365 textview_clear(textview);
367 gtk_text_freeze(text);
369 TEXT_INSERT(_("To save this part, pop up the context menu with "));
370 TEXT_INSERT(_("right click and select `Save as...', "));
371 TEXT_INSERT(_("or press `y' key.\n\n"));
373 TEXT_INSERT(_("To display this part as a text message, select "));
374 TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
376 TEXT_INSERT(_("To open this part with external program, select "));
377 TEXT_INSERT(_("`Open' or `Open with...', "));
378 TEXT_INSERT(_("or double-click, or click the center button, "));
379 TEXT_INSERT(_("or press `l' key."));
385 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
389 if (!partinfo) return;
391 textview_set_font(textview, NULL);
392 text = GTK_TEXT(textview->text);
393 textview_clear(textview);
395 gtk_text_freeze(text);
397 if (partinfo->sigstatus_full == NULL) {
398 TEXT_INSERT(_("This signature has not been checked yet.\n"));
399 TEXT_INSERT(_("To check it, pop up the context menu with\n"));
400 TEXT_INSERT(_("right click and select `Check signature'.\n"));
402 TEXT_INSERT(partinfo->sigstatus_full);
407 #endif /* USE_GPGME */
411 static void textview_show_html(TextView *textview, FILE *fp,
417 parser = html_parser_new(fp, conv);
418 g_return_if_fail(parser != NULL);
420 while ((str = html_parse(parser)) != NULL) {
421 textview_write_line(textview, str, NULL);
423 html_parser_destroy(parser);
426 /* get_uri_part() - retrieves a URI starting from scanpos.
427 Returns TRUE if succesful */
428 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
429 const gchar **bp, const gchar **ep)
433 g_return_val_if_fail(start != NULL, FALSE);
434 g_return_val_if_fail(scanpos != NULL, FALSE);
435 g_return_val_if_fail(bp != NULL, FALSE);
436 g_return_val_if_fail(ep != NULL, FALSE);
440 /* find end point of URI */
441 for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
442 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
446 /* no punctuation at end of string */
448 /* FIXME: this stripping of trailing punctuations may bite with other URIs.
449 * should pass some URI type to this function and decide on that whether
450 * to perform punctuation stripping */
452 #define IS_REAL_PUNCT(ch) \
453 (ispunct(ch) && ((ch) != '/'))
455 for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
465 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
467 return g_strndup(bp, ep - bp);
470 /* valid mail address characters */
471 #define IS_RFC822_CHAR(ch) \
476 !strchr("()<>\"", (ch)))
478 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
480 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
481 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
482 const gchar **bp, const gchar **ep)
484 /* more complex than the uri part because we need to scan back and forward starting from
485 * the scan position. */
486 gboolean result = FALSE;
487 const gchar *bp_ = NULL;
488 const gchar *ep_ = NULL;
490 /* the informative part of the email address (describing the name
491 * of the email address owner) may contain quoted parts. the
492 * closure stack stores the last encountered quotes. */
493 gchar closure_stack[128];
494 gchar *ptr = closure_stack;
496 g_return_val_if_fail(start != NULL, FALSE);
497 g_return_val_if_fail(scanpos != NULL, FALSE);
498 g_return_val_if_fail(bp != NULL, FALSE);
499 g_return_val_if_fail(ep != NULL, FALSE);
501 /* scan start of address */
502 for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
505 /* TODO: should start with an alnum? */
507 for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
510 if (bp_ != scanpos) {
511 /* scan end of address */
512 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
515 /* TODO: really should terminate with an alnum? */
516 for (; ep_ > scanpos && !isalnum(*ep_); --ep_)
520 if (ep_ > scanpos + 1) {
527 if (!result) return FALSE;
529 /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
530 if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
533 /* see if this is <bracketed>; in this case we also scan for the informative part. */
534 if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
537 #define FULL_STACK() ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
538 #define IN_STACK() (ptr > closure_stack)
539 /* has underrun check */
540 #define POP_STACK() if(IN_STACK()) --ptr
541 /* has overrun check */
542 #define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
543 /* has underrun check */
544 #define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
548 /* scan for the informative part. */
549 for (bp_ -= 2; bp_ >= start; bp_--) {
550 /* if closure on the stack keep scanning */
551 if (PEEK_STACK() == *bp_) {
555 if (*bp_ == '\'' || *bp_ == '"') {
560 /* if nothing in the closure stack, do the special conditions
561 * the following if..else expression simply checks whether
562 * a token is acceptable. if not acceptable, the clause
563 * should terminate the loop with a 'break' */
566 && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
567 && (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1)))) {
568 /* hyphens are allowed, but only in
570 } else if (!ispunct(*bp_)) {
571 /* but anything not being a punctiation
574 break; /* anything else is rejected */
587 /* scan forward (should start with an alnum) */
588 for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
598 #undef IS_RFC822_CHAR
600 static gchar *make_email_string(const gchar *bp, const gchar *ep)
602 /* returns a mailto: URI; mailto: is also used to detect the
603 * uri type later on in the button_pressed signal handler */
607 tmp = g_strndup(bp, ep - bp);
608 result = g_strconcat("mailto:", tmp, NULL);
614 #define ADD_TXT_POS(bp_, ep_, pti_) \
615 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
617 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
620 g_warning("alloc error scanning URIs\n"); \
621 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
626 /* textview_make_clickable_parts() - colorizes clickable parts */
627 static void textview_make_clickable_parts(TextView *textview,
631 const gchar *linebuf)
633 /* parse table - in order of priority */
635 const gchar *needle; /* token */
637 /* token search function */
638 gchar *(*search) (const gchar *haystack,
639 const gchar *needle);
640 /* part parsing function */
641 gboolean (*parse) (const gchar *start,
642 const gchar *scanpos,
645 /* part to URI function */
646 gchar *(*build_uri) (const gchar *bp,
650 static struct table parser[] = {
651 {"http://", strcasestr, get_uri_part, make_uri_string},
652 {"https://", strcasestr, get_uri_part, make_uri_string},
653 {"ftp://", strcasestr, get_uri_part, make_uri_string},
654 {"mailto:", strcasestr, get_uri_part, make_uri_string},
655 {"@", strcasestr, get_email_part, make_email_string}
657 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
660 const gchar *walk, *bp, *ep;
663 const gchar *bp, *ep; /* text position */
664 gint pti; /* index in parse table */
665 struct txtpos *next; /* next */
666 } head = {NULL, NULL, 0, NULL}, *last = &head;
668 GtkText *text = GTK_TEXT(textview->text);
670 /* parse for clickable parts, and build a list of begin and end positions */
671 for (walk = linebuf, n = 0;;) {
672 gint last_index = PARSE_ELEMS;
673 gchar *scanpos = NULL;
675 /* FIXME: this looks phony. scanning for anything in the parse table */
676 for (n = 0; n < PARSE_ELEMS; n++) {
679 tmp = parser[n].search(walk, parser[n].needle);
681 if (scanpos == NULL || tmp < scanpos) {
689 /* check if URI can be parsed */
690 if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
691 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
692 ADD_TXT_POS(bp, ep, last_index);
696 strlen(parser[last_index].needle);
701 /* colorize this line */
703 const gchar *normal_text = linebuf;
706 for (last = head.next; last != NULL;
707 normal_text = last->ep, last = last->next) {
710 uri = g_new(RemoteURI, 1);
711 if (last->bp - normal_text > 0)
712 gtk_text_insert(text, font,
715 last->bp - normal_text);
716 uri->uri = parser[last->pti].build_uri(last->bp,
718 uri->start = gtk_text_get_point(text);
719 gtk_text_insert(text, font, uri_color,
720 NULL, last->bp, last->ep - last->bp);
721 uri->end = gtk_text_get_point(text);
723 g_slist_append(textview->uri_list, uri);
727 gtk_text_insert(text, font, fg_color,
728 NULL, normal_text, -1);
730 gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
735 static void textview_write_line(TextView *textview, const gchar *str,
738 GtkText *text = GTK_TEXT(textview->text);
742 gint quotelevel = -1;
745 strncpy2(buf, str, sizeof(buf));
746 else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
747 gtk_text_insert(text, textview->msgfont,
748 prefs_common.enable_color
749 ? &error_color : NULL, NULL,
750 "*** Warning: code conversion failed ***\n",
756 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
760 if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
763 /* change color of quotation
764 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
765 Up to 3 levels of quotations are detected, and each
766 level is colored using a different color. */
767 if (prefs_common.enable_color && strchr(buf, '>')) {
768 quotelevel = get_quote_level(buf);
770 /* set up the correct foreground color */
771 if (quotelevel > 2) {
773 if (prefs_common.recycle_quote_colors)
780 if (quotelevel == -1)
783 fg_color = "e_colors[quotelevel];
785 if (prefs_common.head_space && spacingfont && buf[0] != '\n')
786 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
788 if (prefs_common.enable_color)
789 textview_make_clickable_parts(textview, textview->msgfont,
790 fg_color, &uri_color, buf);
792 textview_make_clickable_parts(textview, textview->msgfont,
793 fg_color, NULL, buf);
796 void textview_clear(TextView *textview)
798 GtkText *text = GTK_TEXT(textview->text);
800 gtk_text_freeze(text);
801 gtk_text_backward_delete(text, gtk_text_get_length(text));
804 textview_uri_list_remove_all(textview->uri_list);
805 textview->uri_list = NULL;
808 void textview_destroy(TextView *textview)
810 textview_uri_list_remove_all(textview->uri_list);
811 textview->uri_list = NULL;
813 if (!textview->scrolledwin_sb->parent)
814 gtk_widget_destroy(textview->scrolledwin_sb);
815 if (!textview->scrolledwin_mb->parent)
816 gtk_widget_destroy(textview->scrolledwin_mb);
818 if (textview->msgfont) {
819 textview->msgfont->ascent = textview->prev_ascent;
820 textview->msgfont->descent = textview->prev_descent;
821 gdk_font_unref(textview->msgfont);
823 if (textview->boldfont)
824 gdk_font_unref(textview->boldfont);
829 void textview_set_font(TextView *textview, const gchar *codeset)
831 gboolean use_fontset = TRUE;
833 /* In multi-byte mode, GtkText can't display 8bit characters
834 correctly, so it must be single-byte mode. */
835 if (MB_CUR_MAX > 1) {
837 if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
838 !g_strncasecmp(codeset, "KOI8-", 5) ||
839 !g_strncasecmp(codeset, "CP", 2) ||
840 !g_strncasecmp(codeset, "WINDOWS-", 8) ||
841 !g_strcasecmp(codeset, "BALTIC"))
847 if (textview->text_is_mb && !use_fontset) {
850 parent = textview->scrolledwin_mb->parent;
851 gtk_editable_select_region
852 (GTK_EDITABLE(textview->text_mb), 0, 0);
853 gtk_container_remove(GTK_CONTAINER(parent),
854 textview->scrolledwin_mb);
855 gtk_container_add(GTK_CONTAINER(parent),
856 textview->scrolledwin_sb);
858 textview->text = textview->text_sb;
859 textview->text_is_mb = FALSE;
860 } else if (!textview->text_is_mb && use_fontset) {
863 parent = textview->scrolledwin_sb->parent;
864 gtk_editable_select_region
865 (GTK_EDITABLE(textview->text_sb), 0, 0);
866 gtk_container_remove(GTK_CONTAINER(parent),
867 textview->scrolledwin_sb);
868 gtk_container_add(GTK_CONTAINER(parent),
869 textview->scrolledwin_mb);
871 textview->text = textview->text_mb;
872 textview->text_is_mb = TRUE;
875 if (prefs_common.textfont) {
876 if (textview->msgfont) {
877 textview->msgfont->ascent = textview->prev_ascent;
878 textview->msgfont->descent = textview->prev_descent;
879 gdk_font_unref(textview->msgfont);
880 textview->msgfont = NULL;
884 gdk_fontset_load(prefs_common.textfont);
886 if (MB_CUR_MAX > 1) {
887 FONT_LOAD(textview->msgfont,
888 "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
890 FONT_LOAD(textview->msgfont,
891 prefs_common.textfont);
895 if (textview->msgfont) {
896 gint ascent, descent;
898 textview->prev_ascent = textview->msgfont->ascent;
899 textview->prev_descent = textview->msgfont->descent;
900 descent = prefs_common.line_space / 2;
901 ascent = prefs_common.line_space - descent;
902 textview->msgfont->ascent += ascent;
903 textview->msgfont->descent += descent;
907 if (!textview->boldfont)
908 FONT_LOAD(textview->boldfont, BOLD_FONT);
910 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
929 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
932 GPtrArray *headers, *sorted_headers;
933 GSList *disphdr_list;
937 g_return_val_if_fail(fp != NULL, NULL);
939 if (!prefs_common.display_header) {
940 while (fgets(buf, sizeof(buf), fp) != NULL)
941 if (buf[0] == '\r' || buf[0] == '\n') break;
945 headers = procheader_get_header_array_asis(fp);
947 sorted_headers = g_ptr_array_new();
949 for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
950 disphdr_list = disphdr_list->next) {
951 DisplayHeaderProp *dp =
952 (DisplayHeaderProp *)disphdr_list->data;
954 for (i = 0; i < headers->len; i++) {
955 header = g_ptr_array_index(headers, i);
957 if (procheader_headername_equal(header->name,
960 procheader_header_free(header);
962 g_ptr_array_add(sorted_headers, header);
964 g_ptr_array_remove_index(headers, i);
970 if (prefs_common.show_other_header) {
971 for (i = 0; i < headers->len; i++) {
972 header = g_ptr_array_index(headers, i);
973 g_ptr_array_add(sorted_headers, header);
977 g_ptr_array_free(headers, FALSE);
979 return sorted_headers;
982 static void textview_show_header(TextView *textview, GPtrArray *headers)
984 GtkText *text = GTK_TEXT(textview->text);
988 g_return_if_fail(headers != NULL);
990 gtk_text_freeze(text);
992 for (i = 0; i < headers->len; i++) {
993 header = g_ptr_array_index(headers, i);
994 g_return_if_fail(header->name != NULL);
996 gtk_text_insert(text, textview->boldfont, NULL, NULL,
998 if (header->name[strlen(header->name) - 1] != ' ')
999 gtk_text_insert(text, textview->boldfont,
1000 NULL, NULL, " ", 1);
1002 if (procheader_headername_equal(header->name, "Subject") ||
1003 procheader_headername_equal(header->name, "From") ||
1004 procheader_headername_equal(header->name, "To") ||
1005 procheader_headername_equal(header->name, "Cc"))
1006 unfold_line(header->body);
1008 if (prefs_common.enable_color &&
1009 (procheader_headername_equal(header->name, "X-Mailer") ||
1010 procheader_headername_equal(header->name,
1012 strstr(header->body, "Sylpheed") != NULL)
1013 gtk_text_insert(text, NULL, &emphasis_color, NULL,
1015 else if (prefs_common.enable_color) {
1016 textview_make_clickable_parts(textview,
1017 NULL, NULL, &uri_color,
1020 textview_make_clickable_parts(textview,
1024 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1027 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1028 gtk_text_thaw(text);
1031 void textview_scroll_one_line(TextView *textview, gboolean up)
1033 GtkText *text = GTK_TEXT(textview->text);
1036 if (prefs_common.enable_smooth_scroll) {
1037 textview_smooth_scroll_one_line(textview, up);
1042 upper = text->vadj->upper - text->vadj->page_size;
1043 if (text->vadj->value < upper) {
1044 text->vadj->value +=
1045 text->vadj->step_increment * 4;
1047 MIN(text->vadj->value, upper);
1048 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1052 if (text->vadj->value > 0.0) {
1053 text->vadj->value -=
1054 text->vadj->step_increment * 4;
1056 MAX(text->vadj->value, 0.0);
1057 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1063 gboolean textview_scroll_page(TextView *textview, gboolean up)
1065 GtkText *text = GTK_TEXT(textview->text);
1069 if (prefs_common.enable_smooth_scroll)
1070 return textview_smooth_scroll_page(textview, up);
1072 if (prefs_common.scroll_halfpage)
1073 page_incr = text->vadj->page_increment / 2;
1075 page_incr = text->vadj->page_increment;
1078 upper = text->vadj->upper - text->vadj->page_size;
1079 if (text->vadj->value < upper) {
1080 text->vadj->value += page_incr;
1081 text->vadj->value = MIN(text->vadj->value, upper);
1082 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1087 if (text->vadj->value > 0.0) {
1088 text->vadj->value -= page_incr;
1089 text->vadj->value = MAX(text->vadj->value, 0.0);
1090 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1099 static void textview_smooth_scroll_do(TextView *textview,
1100 gfloat old_value, gfloat last_value,
1103 GtkText *text = GTK_TEXT(textview->text);
1108 if (old_value < last_value) {
1109 change_value = last_value - old_value;
1112 change_value = old_value - last_value;
1116 gdk_key_repeat_disable();
1118 for (i = step; i <= change_value; i += step) {
1119 text->vadj->value = old_value + (up ? -i : i);
1120 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1124 text->vadj->value = last_value;
1125 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1127 gdk_key_repeat_restore();
1130 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1132 GtkText *text = GTK_TEXT(textview->text);
1138 upper = text->vadj->upper - text->vadj->page_size;
1139 if (text->vadj->value < upper) {
1140 old_value = text->vadj->value;
1141 last_value = text->vadj->value +
1142 text->vadj->step_increment * 4;
1143 last_value = MIN(last_value, upper);
1145 textview_smooth_scroll_do(textview, old_value,
1147 prefs_common.scroll_step);
1150 if (text->vadj->value > 0.0) {
1151 old_value = text->vadj->value;
1152 last_value = text->vadj->value -
1153 text->vadj->step_increment * 4;
1154 last_value = MAX(last_value, 0.0);
1156 textview_smooth_scroll_do(textview, old_value,
1158 prefs_common.scroll_step);
1163 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1165 GtkText *text = GTK_TEXT(textview->text);
1171 if (prefs_common.scroll_halfpage)
1172 page_incr = text->vadj->page_increment / 2;
1174 page_incr = text->vadj->page_increment;
1177 upper = text->vadj->upper - text->vadj->page_size;
1178 if (text->vadj->value < upper) {
1179 old_value = text->vadj->value;
1180 last_value = text->vadj->value + page_incr;
1181 last_value = MIN(last_value, upper);
1183 textview_smooth_scroll_do(textview, old_value,
1185 prefs_common.scroll_step);
1189 if (text->vadj->value > 0.0) {
1190 old_value = text->vadj->value;
1191 last_value = text->vadj->value - page_incr;
1192 last_value = MAX(last_value, 0.0);
1194 textview_smooth_scroll_do(textview, old_value,
1196 prefs_common.scroll_step);
1204 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1207 SummaryView *summaryview = NULL;
1210 if (textview->messageview->mainwin)
1211 summaryview = textview->messageview->mainwin->summaryview;
1213 switch (event->keyval) {
1228 summary_pass_key_press_event(summaryview, event);
1230 textview_scroll_page(textview, FALSE);
1234 textview_scroll_page(textview, TRUE);
1237 textview_scroll_one_line(textview,
1238 (event->state & GDK_MOD1_MASK) != 0);
1242 summary_pass_key_press_event(summaryview, event);
1247 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1251 ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1252 || event->button == 2 || event->button == 3)) {
1256 current_pos = GTK_EDITABLE(textview->text)->current_pos;
1258 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1259 RemoteURI *uri = (RemoteURI *)cur->data;
1261 if (current_pos >= uri->start &&
1262 current_pos < uri->end) {
1263 if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1264 if (event->button == 3) {
1265 gchar *fromname, *fromaddress;
1267 fromaddress = g_strdup(uri->uri + 7);
1268 /* Hiroyuki: please put this function in utils.c! */
1269 fromname = procheader_get_fromname(fromaddress);
1270 extract_address(fromaddress);
1271 g_message("adding from textview %s <%s>", fromname, fromaddress);
1272 addressbook_add_contact_by_menu(NULL, fromname, fromaddress, NULL);
1273 g_free(fromaddress);
1276 compose_new_with_recipient
1277 (NULL, uri->uri + 7);
1281 prefs_common.uri_cmd);
1288 static void textview_uri_list_remove_all(GSList *uri_list)
1292 for (cur = uri_list; cur != NULL; cur = cur->next) {
1294 g_free(((RemoteURI *)cur->data)->uri);
1299 g_slist_free(uri_list);