2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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"
51 #define FONT_LOAD(font, s) \
55 Xstrdup_a(fontstr, s, ); \
56 if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
57 font = gdk_font_load(fontstr); \
60 typedef struct _RemoteURI RemoteURI;
70 static GdkColor quote_colors[3] = {
71 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
72 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
73 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
76 static GdkColor uri_color = {
83 static GdkColor emphasis_color = {
90 static GdkColor error_color = {
97 static GdkFont *spacingfont;
99 static void textview_show_html (TextView *textview,
101 CodeConverter *conv);
102 static void textview_write_line (TextView *textview,
104 CodeConverter *conv);
105 static GPtrArray *textview_scan_header (TextView *textview,
107 static void textview_show_header (TextView *textview,
110 static void textview_key_pressed (GtkWidget *widget,
113 static void textview_button_pressed (GtkWidget *widget,
114 GdkEventButton *event,
117 static void textview_uri_list_remove_all(GSList *uri_list);
119 static void textview_smooth_scroll_do (TextView *textview,
123 static void textview_smooth_scroll_one_line (TextView *textview,
125 static gboolean textview_smooth_scroll_page (TextView *textview,
129 TextView *textview_create(void)
133 GtkWidget *scrolledwin_sb;
134 GtkWidget *scrolledwin_mb;
138 debug_print(_("Creating text view...\n"));
139 textview = g_new0(TextView, 1);
141 scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
142 scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
143 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
144 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
145 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
146 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
147 gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
148 gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
150 /* create GtkText widgets for single-byte and multi-byte character */
151 text_sb = gtk_text_new(NULL, NULL);
152 text_mb = gtk_text_new(NULL, NULL);
153 gtk_widget_show(text_sb);
154 gtk_widget_show(text_mb);
155 gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
156 gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
157 gtk_widget_ensure_style(text_sb);
158 gtk_widget_ensure_style(text_mb);
159 if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
162 style = gtk_style_copy(text_sb->style);
163 gdk_font_unref(style->font);
164 FONT_LOAD(style->font, NORMAL_FONT);
165 gtk_widget_set_style(text_sb, style);
167 if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
170 style = gtk_style_copy(text_mb->style);
171 gdk_font_unref(style->font);
172 style->font = gdk_fontset_load(NORMAL_FONT);
173 gtk_widget_set_style(text_mb, style);
175 gtk_widget_ref(scrolledwin_sb);
176 gtk_widget_ref(scrolledwin_mb);
178 gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
179 gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
180 gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
181 GTK_SIGNAL_FUNC(textview_key_pressed),
183 gtk_signal_connect(GTK_OBJECT(text_sb), "button_press_event",
184 GTK_SIGNAL_FUNC(textview_button_pressed),
186 gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
187 GTK_SIGNAL_FUNC(textview_key_pressed),
189 gtk_signal_connect(GTK_OBJECT(text_mb), "button_press_event",
190 GTK_SIGNAL_FUNC(textview_button_pressed),
193 gtk_widget_show(scrolledwin_sb);
194 gtk_widget_show(scrolledwin_mb);
196 vbox = gtk_vbox_new(FALSE, 0);
197 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
199 textview->vbox = vbox;
200 textview->scrolledwin = scrolledwin_sb;
201 textview->scrolledwin_sb = scrolledwin_sb;
202 textview->scrolledwin_mb = scrolledwin_mb;
203 textview->text = text_sb;
204 textview->text_sb = text_sb;
205 textview->text_mb = text_mb;
206 textview->text_is_mb = FALSE;
207 textview->uri_list = NULL;
212 void textview_init(TextView *textview)
214 gtkut_widget_disable_theme_engine(textview->text_sb);
215 gtkut_widget_disable_theme_engine(textview->text_mb);
216 textview_update_message_colors();
217 textview_set_font(textview, NULL);
220 void textview_update_message_colors(void)
222 GdkColor black = {0, 0, 0, 0};
224 if (prefs_common.enable_color) {
225 /* grab the quote colors, converting from an int to a GdkColor */
226 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
228 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
230 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
232 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
235 quote_colors[0] = quote_colors[1] = quote_colors[2] =
236 uri_color = emphasis_color = black;
240 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
245 if ((fp = fopen(file, "r")) == NULL) {
246 FILE_OP_ERROR(file, "fopen");
250 textview_show_part(textview, mimeinfo, fp);
255 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
259 const gchar *boundary = NULL;
260 gint boundary_len = 0;
261 const gchar *charset = NULL;
263 GPtrArray *headers = NULL;
266 g_return_if_fail(mimeinfo != NULL);
267 g_return_if_fail(fp != NULL);
269 if (mimeinfo->mime_type == MIME_MULTIPART) {
271 mimeinfo = mimeinfo->sub;
272 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
279 if (mimeinfo->parent && mimeinfo->parent->boundary) {
280 boundary = mimeinfo->parent->boundary;
281 boundary_len = strlen(boundary);
284 if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
285 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
287 headers = textview_scan_header(textview, fp);
289 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
292 if ((fpos = ftell(fp)) < 0)
294 else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
298 headers = textview_scan_header(textview, fp);
299 if (fseek(fp, fpos, SEEK_SET) < 0)
303 while (fgets(buf, sizeof(buf), fp) != NULL)
304 if (buf[0] == '\r' || buf[0] == '\n') break;
307 /* display attached RFC822 single text message */
308 if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
309 if (!mimeinfo->sub || mimeinfo->sub->children) return;
310 headers = textview_scan_header(textview, fp);
311 mimeinfo = mimeinfo->sub;
312 } else if (!mimeinfo->parent &&
313 mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
314 if (!mimeinfo->sub) return;
315 headers = textview_scan_header(textview, fp);
316 mimeinfo = mimeinfo->sub;
319 if (prefs_common.force_charset)
320 charset = prefs_common.force_charset;
321 else if (mimeinfo->charset)
322 charset = mimeinfo->charset;
323 textview_set_font(textview, charset);
325 conv = conv_code_converter_new(charset);
327 textview_clear(textview);
328 text = GTK_TEXT(textview->text);
329 gtk_text_freeze(text);
334 textview_show_header(textview, headers);
335 for (i = 0; i < headers->len; i++) {
336 Header *header = g_ptr_array_index(headers, i);
337 g_free(header->body);
338 g_free(header->name);
341 g_ptr_array_free(headers, TRUE);
344 tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
346 if (mimeinfo->mime_type == MIME_TEXT_HTML)
347 textview_show_html(textview, tmpfp, conv);
349 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
350 textview_write_line(textview, buf, conv);
354 conv_code_converter_destroy(conv);
359 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
363 if (!partinfo) return;
365 textview_set_font(textview, NULL);
366 text = GTK_TEXT(textview->text);
367 textview_clear(textview);
369 gtk_text_freeze(text);
372 (text, textview->msgfont, NULL, NULL,
373 _("To save this part, pop up the context menu with\n"), -1);
375 (text, textview->msgfont, NULL, NULL,
376 _("right click and select `Save as...', or press `y' key.\n\n"), -1);
379 (text, textview->msgfont, NULL, NULL,
380 _("To display this part as a text message, select\n"), -1);
382 (text, textview->msgfont, NULL, NULL,
383 _("`Display as text', or press `t' key.\n\n"), -1);
386 (text, textview->msgfont, NULL, NULL,
387 _("To open this part with external program, select `Open',\n"), -1);
389 (text, textview->msgfont, NULL, NULL,
390 _("or double-click, or click the center button, or press `l' key."), -1);
396 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
400 if (!partinfo) return;
402 textview_set_font(textview, NULL);
403 text = GTK_TEXT(textview->text);
404 textview_clear(textview);
406 gtk_text_freeze(text);
408 if (partinfo->sigstatus_full == NULL) {
410 (text, textview->msgfont, NULL, NULL,
411 _("This signature has not been checked yet.\n"), -1);
413 (text, textview->msgfont, NULL, NULL,
414 _("To check it, pop up the context menu with\n"), -1);
416 (text, textview->msgfont, NULL, NULL,
417 _("right click and select `Check signature'.\n"), -1);
419 gtk_text_insert(text, textview->msgfont, NULL, NULL,
420 partinfo->sigstatus_full, -1);
425 #endif /* USE_GPGME */
427 static void textview_show_html(TextView *textview, FILE *fp,
433 parser = html_parser_new(fp, conv);
434 g_return_if_fail(parser != NULL);
436 while ((str = html_parse(parser)) != NULL) {
437 textview_write_line(textview, str, NULL);
439 html_parser_destroy(parser);
442 /* get_uri_part() - retrieves a URI starting from scanpos.
443 Returns TRUE if succesful */
444 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
445 const gchar **bp, const gchar **ep)
449 g_return_val_if_fail(start != NULL, FALSE);
450 g_return_val_if_fail(scanpos != NULL, FALSE);
451 g_return_val_if_fail(bp != NULL, FALSE);
452 g_return_val_if_fail(ep != NULL, FALSE);
456 /* find end point of URI */
457 for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
458 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
462 /* no punctuation at end of string */
464 /* FIXME: this stripping of trailing punctuations may bite with other URIs.
465 * should pass some URI type to this function and decide on that whether
466 * to perform punctuation stripping */
468 #define IS_REAL_PUNCT(ch) \
469 (ispunct(ch) && ((ch) != '/'))
471 for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
481 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
483 return g_strndup(bp, ep - bp);
486 /* valid mail address characters */
487 #define IS_RFC822_CHAR(ch) \
492 !strchr("()<>\"", (ch)))
494 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
495 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
496 const gchar **bp, const gchar **ep)
498 /* more complex than the uri part because we need to scan back and forward starting from
499 * the scan position. */
500 gboolean result = FALSE;
504 g_return_val_if_fail(start != NULL, FALSE);
505 g_return_val_if_fail(scanpos != NULL, FALSE);
506 g_return_val_if_fail(bp != NULL, FALSE);
507 g_return_val_if_fail(ep != NULL, FALSE);
509 /* scan start of address */
510 for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
513 /* TODO: should start with an alnum? */
515 for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
518 if (bp_ != scanpos) {
519 /* scan end of address */
520 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
523 /* TODO: really should terminate with an alnum? */
524 for (; ep_ > scanpos && !isalnum(*ep_); --ep_)
528 if (ep_ > scanpos + 1) {
536 /* we now have at least the address. now see if this is <bracketed>; in this
537 * case we also scan for the informative part. we could make this a useful
538 * function because it tries to parse out an email address backwards. :) */
539 if ( ((bp_ - 1 > start) && *(bp_ - 1) == '<') && (*ep_ == '>')) {
540 char closure_stack[128];
541 char *ptr = closure_stack;
542 #define FULL_STACK() ((ptr - closure_stack) >= sizeof closure_stack)
543 #define IN_STACK() (ptr > closure_stack)
544 /* has underrun check */
545 #define POP_STACK() if(IN_STACK()) --ptr
546 /* has overrun check */
547 #define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
548 /* has underrun check */
549 #define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
553 /* scan for the informative part */
554 for (bp_ -= 2; bp_ >= start; bp_--) {
555 /* if closure on the stack keep scanning */
556 if (PEEK_STACK() == *bp_) {
560 if (*bp_ == '\'' || *bp_ == '"') {
565 /* if nothing in the closure stack, do the special conditions
566 * the following if..else expression simply checks whether
567 * a token is acceptable. if not acceptable, the clause
568 * should terminate the loop with a 'break' */
570 && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
571 && (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1))) ) {
572 /* hyphens are allowed, but only in between alnums */
574 else if ( !ispunct(*bp_) ) {
575 /* but anything not being a punctiation is ok */
578 break; /* anything else is rejected */
593 /* scan forward (should start with an alnum) */
594 for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
604 #undef IS_RFC822_CHAR
606 static gchar *make_email_string(const gchar *bp, const gchar *ep)
608 /* returns a mailto: URI; mailto: is also used to detect the
609 * uri type later on in the button_pressed signal handler */
613 tmp = g_strndup(bp, ep - bp);
614 result = g_strconcat("mailto:", tmp, NULL);
620 #define ADD_TXT_POS(bp_, ep_, pti_) \
621 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
623 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
626 g_warning("alloc error scanning URIs\n"); \
627 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
632 /* textview_make_clickable_parts() - colorizes clickable parts */
633 static void textview_make_clickable_parts(TextView *textview, GtkText *text,
637 const gchar *linebuf)
639 /* parse table - in order of priority */
641 const gchar *needle; /* token */
643 /* token search function */
644 gchar *(*search) (const gchar *haystack,
645 const gchar *needle);
646 /* part parsing function */
647 gboolean (*parse) (const gchar *start,
648 const gchar *scanpos,
651 /* part to URI function */
652 gchar *(*build_uri) (const gchar *bp,
656 static struct table parser[] = {
657 {"http://", strcasestr, get_uri_part, make_uri_string},
658 {"https://", strcasestr, get_uri_part, make_uri_string},
659 {"ftp://", strcasestr, get_uri_part, make_uri_string},
660 {"mailto:", strcasestr, get_uri_part, make_uri_string},
661 {"@", strcasestr, get_email_part, make_email_string}
663 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
666 const gchar *walk, *bp, *ep;
669 const gchar *bp, *ep; /* text position */
670 gint pti; /* index in parse table */
671 struct txtpos *next; /* next */
672 } head = {NULL, NULL, 0, NULL}, *last = &head;
674 /* parse for clickable parts, and build a list of begin and end positions */
675 for (walk = linebuf, n = 0;;) {
676 gint last_index = PARSE_ELEMS;
677 gchar *scanpos = NULL;
679 /* FIXME: this looks phony. scanning for anything in the parse table */
680 for (n = 0; n < PARSE_ELEMS; n++) {
683 tmp = parser[n].search(walk, parser[n].needle);
685 if (scanpos == NULL || tmp < scanpos) {
693 /* check if URI can be parsed */
694 if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
695 && (ep - bp - 1) > strlen(parser[last_index].needle)) {
696 ADD_TXT_POS(bp, ep, last_index);
700 strlen(parser[last_index].needle);
705 /* colorize this line */
707 const gchar *normal_text = linebuf;
710 for (last = head.next; last != NULL;
711 normal_text = last->ep, last = last->next) {
714 uri = g_new(RemoteURI, 1);
715 if (last->bp - normal_text > 0)
716 gtk_text_insert(text, textfont,
719 last->bp - normal_text);
720 uri->uri = parser[last->pti].build_uri(last->bp, last->ep);
721 uri->start = gtk_text_get_point(text);
722 gtk_text_insert(text, textfont, uri_color,
723 NULL, last->bp, last->ep - last->bp);
724 uri->end = gtk_text_get_point(text);
726 g_slist_append(textview->uri_list, uri);
730 gtk_text_insert(text, textfont, fg_color,
731 NULL, normal_text, -1);
733 gtk_text_insert(text, textfont, fg_color, NULL,
739 static void textview_write_line(TextView *textview, const gchar *str,
742 GtkText *text = GTK_TEXT(textview->text);
746 gint quotelevel = -1;
749 strncpy2(buf, str, sizeof(buf));
750 else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
751 gtk_text_insert(text, textview->msgfont,
752 prefs_common.enable_color
753 ? &error_color : NULL, NULL,
754 "*** Warning: code conversion failed ***\n",
760 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
764 if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
767 /* change color of quotation
768 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
769 Up to 3 levels of quotations are detected, and each
770 level is colored using a different color. */
771 if (prefs_common.enable_color && strchr(buf, '>')) {
772 quotelevel = get_quote_level(buf);
774 /* set up the correct foreground color */
775 if (quotelevel > 2) {
777 if (prefs_common.recycle_quote_colors)
784 if (quotelevel == -1)
787 fg_color = "e_colors[quotelevel];
789 if (prefs_common.head_space && spacingfont && buf[0] != '\n')
790 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
792 if (prefs_common.enable_color)
793 textview_make_clickable_parts(textview, text,
795 fg_color, &uri_color, buf);
797 gtk_text_insert(text, textview->msgfont, fg_color, NULL,
801 void textview_clear(TextView *textview)
803 GtkText *text = GTK_TEXT(textview->text);
805 gtk_text_freeze(text);
806 gtk_text_backward_delete(text, gtk_text_get_length(text));
809 textview_uri_list_remove_all(textview->uri_list);
810 textview->uri_list = NULL;
813 void textview_destroy(TextView *textview)
815 textview_uri_list_remove_all(textview->uri_list);
816 textview->uri_list = NULL;
818 if (!textview->scrolledwin_sb->parent)
819 gtk_widget_destroy(textview->scrolledwin_sb);
820 if (!textview->scrolledwin_mb->parent)
821 gtk_widget_destroy(textview->scrolledwin_mb);
823 if (textview->msgfont) {
824 textview->msgfont->ascent = textview->prev_ascent;
825 textview->msgfont->descent = textview->prev_descent;
826 gdk_font_unref(textview->msgfont);
828 if (textview->boldfont)
829 gdk_font_unref(textview->boldfont);
834 void textview_set_font(TextView *textview, const gchar *codeset)
836 gboolean use_fontset = TRUE;
838 /* In multi-byte mode, GtkText can't display 8bit characters
839 correctly, so it must be single-byte mode. */
840 if (MB_CUR_MAX > 1) {
842 if (!strncasecmp(codeset, "ISO-8859-", 9) ||
843 !strncasecmp(codeset, "KOI8-", 5) ||
844 !strncasecmp(codeset, "CP", 2) ||
845 !strncasecmp(codeset, "WINDOWS-", 8) ||
846 !strcasecmp(codeset, "BALTIC"))
852 if (textview->text_is_mb && !use_fontset) {
855 parent = textview->scrolledwin_mb->parent;
856 gtk_editable_select_region
857 (GTK_EDITABLE(textview->text_mb), 0, 0);
858 gtk_container_remove(GTK_CONTAINER(parent),
859 textview->scrolledwin_mb);
860 gtk_container_add(GTK_CONTAINER(parent),
861 textview->scrolledwin_sb);
863 textview->text = textview->text_sb;
864 textview->text_is_mb = FALSE;
865 } else if (!textview->text_is_mb && use_fontset) {
868 parent = textview->scrolledwin_sb->parent;
869 gtk_editable_select_region
870 (GTK_EDITABLE(textview->text_sb), 0, 0);
871 gtk_container_remove(GTK_CONTAINER(parent),
872 textview->scrolledwin_sb);
873 gtk_container_add(GTK_CONTAINER(parent),
874 textview->scrolledwin_mb);
876 textview->text = textview->text_mb;
877 textview->text_is_mb = TRUE;
880 if (prefs_common.textfont) {
881 if (textview->msgfont) {
882 textview->msgfont->ascent = textview->prev_ascent;
883 textview->msgfont->descent = textview->prev_descent;
884 gdk_font_unref(textview->msgfont);
885 textview->msgfont = NULL;
889 gdk_fontset_load(prefs_common.textfont);
891 if (MB_CUR_MAX > 1) {
892 FONT_LOAD(textview->msgfont,
893 "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
895 FONT_LOAD(textview->msgfont,
896 prefs_common.textfont);
900 if (textview->msgfont) {
901 gint ascent, descent;
903 textview->prev_ascent = textview->msgfont->ascent;
904 textview->prev_descent = textview->msgfont->descent;
905 descent = prefs_common.line_space / 2;
906 ascent = prefs_common.line_space - descent;
907 textview->msgfont->ascent += ascent;
908 textview->msgfont->descent += descent;
912 if (!textview->boldfont)
913 FONT_LOAD(textview->boldfont, BOLD_FONT);
915 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
934 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
936 static HeaderEntry hentry[] = {{"Date:", NULL, FALSE},
937 {"From:", NULL, TRUE},
938 {"To:", NULL, FALSE},
939 {"Newsgroups:", NULL, FALSE},
940 {"Subject:", NULL, TRUE},
941 {"Cc:", NULL, FALSE},
942 {"Reply-To:", NULL, FALSE},
943 {"Followup-To:", NULL, FALSE},
944 {"X-Mailer:", NULL, TRUE},
945 {"X-Newsreader:", NULL, TRUE},
946 {"User-Agent:", NULL, TRUE},
947 {"Organization:", NULL, TRUE},
948 {NULL, NULL, FALSE}};
949 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
954 g_return_val_if_fail(fp != NULL, NULL);
956 if (!prefs_common.display_header) {
957 while (fgets(buf, sizeof(buf), fp) != NULL)
958 if (buf[0] == '\r' || buf[0] == '\n') break;
962 headers = g_ptr_array_new();
964 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
970 header = g_new(Header, 1);
971 header->name = g_strndup(buf, strlen(hp->name));
972 conv_unmime_header(tmp, sizeof(tmp), buf + strlen(hp->name),
974 header->body = g_strdup(tmp);
976 g_ptr_array_add(headers, header);
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 (prefs_common.enable_color &&
999 (strncmp(header->name, "X-Mailer", 8) == 0 ||
1000 strncmp(header->name, "X-Newsreader", 12) == 0) &&
1001 strstr(header->body, "Sylpheed") != NULL)
1002 gtk_text_insert(text, NULL, &emphasis_color, NULL,
1005 if (prefs_common.enable_color) {
1006 textview_make_clickable_parts(textview, text,
1013 gtk_text_insert(text, NULL, NULL, NULL,
1017 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1020 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1021 gtk_text_thaw(text);
1024 void textview_scroll_one_line(TextView *textview, gboolean up)
1026 GtkText *text = GTK_TEXT(textview->text);
1029 if (prefs_common.enable_smooth_scroll) {
1030 textview_smooth_scroll_one_line(textview, up);
1035 upper = text->vadj->upper - text->vadj->page_size;
1036 if (text->vadj->value < upper) {
1037 text->vadj->value +=
1038 text->vadj->step_increment * 4;
1040 MIN(text->vadj->value, upper);
1041 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1045 if (text->vadj->value > 0.0) {
1046 text->vadj->value -=
1047 text->vadj->step_increment * 4;
1049 MAX(text->vadj->value, 0.0);
1050 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1056 gboolean textview_scroll_page(TextView *textview, gboolean up)
1058 GtkText *text = GTK_TEXT(textview->text);
1062 if (prefs_common.enable_smooth_scroll)
1063 return textview_smooth_scroll_page(textview, up);
1065 if (prefs_common.scroll_halfpage)
1066 page_incr = text->vadj->page_increment / 2;
1068 page_incr = text->vadj->page_increment;
1071 upper = text->vadj->upper - text->vadj->page_size;
1072 if (text->vadj->value < upper) {
1073 text->vadj->value += page_incr;
1074 text->vadj->value = MIN(text->vadj->value, upper);
1075 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1080 if (text->vadj->value > 0.0) {
1081 text->vadj->value -= page_incr;
1082 text->vadj->value = MAX(text->vadj->value, 0.0);
1083 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1092 static void textview_smooth_scroll_do(TextView *textview,
1093 gfloat old_value, gfloat last_value,
1096 GtkText *text = GTK_TEXT(textview->text);
1101 if (old_value < last_value) {
1102 change_value = last_value - old_value;
1105 change_value = old_value - last_value;
1109 gdk_key_repeat_disable();
1111 for (i = step; i <= change_value; i += step) {
1112 text->vadj->value = old_value + (up ? -i : i);
1113 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1117 text->vadj->value = last_value;
1118 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1120 gdk_key_repeat_restore();
1123 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1125 GtkText *text = GTK_TEXT(textview->text);
1131 upper = text->vadj->upper - text->vadj->page_size;
1132 if (text->vadj->value < upper) {
1133 old_value = text->vadj->value;
1134 last_value = text->vadj->value +
1135 text->vadj->step_increment * 4;
1136 last_value = MIN(last_value, upper);
1138 textview_smooth_scroll_do(textview, old_value,
1140 prefs_common.scroll_step);
1143 if (text->vadj->value > 0.0) {
1144 old_value = text->vadj->value;
1145 last_value = text->vadj->value -
1146 text->vadj->step_increment * 4;
1147 last_value = MAX(last_value, 0.0);
1149 textview_smooth_scroll_do(textview, old_value,
1151 prefs_common.scroll_step);
1156 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1158 GtkText *text = GTK_TEXT(textview->text);
1164 if (prefs_common.scroll_halfpage)
1165 page_incr = text->vadj->page_increment / 2;
1167 page_incr = text->vadj->page_increment;
1170 upper = text->vadj->upper - text->vadj->page_size;
1171 if (text->vadj->value < upper) {
1172 old_value = text->vadj->value;
1173 last_value = text->vadj->value + page_incr;
1174 last_value = MIN(last_value, upper);
1176 textview_smooth_scroll_do(textview, old_value,
1178 prefs_common.scroll_step);
1182 if (text->vadj->value > 0.0) {
1183 old_value = text->vadj->value;
1184 last_value = text->vadj->value - page_incr;
1185 last_value = MAX(last_value, 0.0);
1187 textview_smooth_scroll_do(textview, old_value,
1189 prefs_common.scroll_step);
1197 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1200 SummaryView *summaryview = NULL;
1203 if (textview->messageview->mainwin)
1204 summaryview = textview->messageview->mainwin->summaryview;
1206 switch (event->keyval) {
1221 summary_pass_key_press_event(summaryview, event);
1223 textview_scroll_page(textview, FALSE);
1227 textview_scroll_page(textview, TRUE);
1230 textview_scroll_one_line(textview,
1231 (event->state & GDK_MOD1_MASK) != 0);
1235 summary_pass_key_press_event(summaryview, event);
1240 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1244 ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1245 || event->button == 2
1246 || event->button == 3)) {
1250 current_pos = GTK_EDITABLE(textview->text)->current_pos;
1252 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1253 RemoteURI *uri = (RemoteURI *)cur->data;
1255 if (current_pos >= uri->start &&
1256 current_pos < uri->end) {
1257 if (!strncasecmp(uri->uri, "mailto:", 7)) {
1258 compose_new_with_recipient
1259 (NULL, uri->uri + 7);
1263 prefs_common.uri_cmd);
1269 static void textview_uri_list_remove_all(GSList *uri_list)
1273 for (cur = uri_list; cur != NULL; cur = cur->next) {
1275 g_free(((RemoteURI *)cur->data)->uri);
1280 g_slist_free(uri_list);