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"
50 #include "headers_display.h"
51 #include "prefs_display_headers.h"
53 #define FONT_LOAD(font, s) \
57 Xstrdup_a(fontstr, s, ); \
58 if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
59 font = gdk_font_load(fontstr); \
62 typedef struct _RemoteURI RemoteURI;
72 static GdkColor quote_colors[3] = {
73 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
74 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
75 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
78 static GdkColor uri_color = {
85 static GdkColor emphasis_color = {
92 static GdkColor error_color = {
99 static GdkFont *spacingfont;
101 static void textview_show_html (TextView *textview,
103 CodeConverter *conv);
104 static void textview_write_line (TextView *textview,
106 CodeConverter *conv);
107 static GPtrArray *textview_scan_header (TextView *textview,
109 static void textview_show_header (TextView *textview,
112 static void textview_key_pressed (GtkWidget *widget,
115 static void textview_button_pressed (GtkWidget *widget,
116 GdkEventButton *event,
119 static void textview_uri_list_remove_all(GSList *uri_list);
121 static void textview_smooth_scroll_do (TextView *textview,
125 static void textview_smooth_scroll_one_line (TextView *textview,
127 static gboolean textview_smooth_scroll_page (TextView *textview,
131 TextView *textview_create(void)
135 GtkWidget *scrolledwin_sb;
136 GtkWidget *scrolledwin_mb;
140 debug_print(_("Creating text view...\n"));
141 textview = g_new0(TextView, 1);
143 scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
144 scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
145 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
146 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
147 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
148 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
149 gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
150 gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
152 /* create GtkText widgets for single-byte and multi-byte character */
153 text_sb = gtk_text_new(NULL, NULL);
154 text_mb = gtk_text_new(NULL, NULL);
155 gtk_widget_show(text_sb);
156 gtk_widget_show(text_mb);
157 gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
158 gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
159 gtk_widget_ensure_style(text_sb);
160 gtk_widget_ensure_style(text_mb);
161 if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
164 style = gtk_style_copy(text_sb->style);
165 gdk_font_unref(style->font);
166 FONT_LOAD(style->font, NORMAL_FONT);
167 gtk_widget_set_style(text_sb, style);
169 if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
172 style = gtk_style_copy(text_mb->style);
173 gdk_font_unref(style->font);
174 style->font = gdk_fontset_load(NORMAL_FONT);
175 gtk_widget_set_style(text_mb, style);
177 gtk_widget_ref(scrolledwin_sb);
178 gtk_widget_ref(scrolledwin_mb);
180 gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
181 gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
182 gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
183 GTK_SIGNAL_FUNC(textview_key_pressed),
185 gtk_signal_connect(GTK_OBJECT(text_sb), "button_press_event",
186 GTK_SIGNAL_FUNC(textview_button_pressed),
188 gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
189 GTK_SIGNAL_FUNC(textview_key_pressed),
191 gtk_signal_connect(GTK_OBJECT(text_mb), "button_press_event",
192 GTK_SIGNAL_FUNC(textview_button_pressed),
195 gtk_widget_show(scrolledwin_sb);
196 gtk_widget_show(scrolledwin_mb);
198 vbox = gtk_vbox_new(FALSE, 0);
199 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
201 textview->vbox = vbox;
202 textview->scrolledwin = scrolledwin_sb;
203 textview->scrolledwin_sb = scrolledwin_sb;
204 textview->scrolledwin_mb = scrolledwin_mb;
205 textview->text = text_sb;
206 textview->text_sb = text_sb;
207 textview->text_mb = text_mb;
208 textview->text_is_mb = FALSE;
209 textview->uri_list = NULL;
214 void textview_init(TextView *textview)
216 gtkut_widget_disable_theme_engine(textview->text_sb);
217 gtkut_widget_disable_theme_engine(textview->text_mb);
218 textview_update_message_colors();
219 textview_set_font(textview, NULL);
222 void textview_update_message_colors(void)
224 GdkColor black = {0, 0, 0, 0};
226 if (prefs_common.enable_color) {
227 /* grab the quote colors, converting from an int to a GdkColor */
228 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
230 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
232 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
234 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
237 quote_colors[0] = quote_colors[1] = quote_colors[2] =
238 uri_color = emphasis_color = black;
242 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
247 if ((fp = fopen(file, "r")) == NULL) {
248 FILE_OP_ERROR(file, "fopen");
252 textview_show_part(textview, mimeinfo, fp);
257 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
261 const gchar *boundary = NULL;
262 gint boundary_len = 0;
263 const gchar *charset = NULL;
265 GPtrArray *headers = NULL;
268 g_return_if_fail(mimeinfo != NULL);
269 g_return_if_fail(fp != NULL);
271 if (mimeinfo->mime_type == MIME_MULTIPART) {
273 mimeinfo = mimeinfo->sub;
274 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
281 if (mimeinfo->parent && mimeinfo->parent->boundary) {
282 boundary = mimeinfo->parent->boundary;
283 boundary_len = strlen(boundary);
286 if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
287 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
289 headers = textview_scan_header(textview, fp);
291 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
294 if ((fpos = ftell(fp)) < 0)
296 else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
300 headers = textview_scan_header(textview, fp);
301 if (fseek(fp, fpos, SEEK_SET) < 0)
305 while (fgets(buf, sizeof(buf), fp) != NULL)
306 if (buf[0] == '\r' || buf[0] == '\n') break;
309 /* display attached RFC822 single text message */
310 if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
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 (!mimeinfo->sub) return;
317 headers = textview_scan_header(textview, fp);
318 mimeinfo = mimeinfo->sub;
321 if (prefs_common.force_charset)
322 charset = prefs_common.force_charset;
323 else if (mimeinfo->charset)
324 charset = mimeinfo->charset;
325 textview_set_font(textview, charset);
327 conv = conv_code_converter_new(charset);
329 textview_clear(textview);
330 text = GTK_TEXT(textview->text);
331 gtk_text_freeze(text);
336 textview_show_header(textview, headers);
337 for (i = 0; i < headers->len; i++) {
338 Header *header = g_ptr_array_index(headers, i);
339 g_free(header->body);
340 g_free(header->name);
343 g_ptr_array_free(headers, TRUE);
346 tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
348 if (mimeinfo->mime_type == MIME_TEXT_HTML)
349 textview_show_html(textview, tmpfp, conv);
351 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
352 textview_write_line(textview, buf, conv);
356 conv_code_converter_destroy(conv);
361 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
365 if (!partinfo) return;
367 textview_set_font(textview, NULL);
368 text = GTK_TEXT(textview->text);
369 textview_clear(textview);
371 gtk_text_freeze(text);
374 (text, textview->msgfont, NULL, NULL,
375 _("To save this part, pop up the context menu with\n"), -1);
377 (text, textview->msgfont, NULL, NULL,
378 _("right click and select `Save as...', or press `y' key.\n\n"), -1);
381 (text, textview->msgfont, NULL, NULL,
382 _("To display this part as a text message, select\n"), -1);
384 (text, textview->msgfont, NULL, NULL,
385 _("`Display as text', or press `t' key.\n\n"), -1);
388 (text, textview->msgfont, NULL, NULL,
389 _("To open this part with external program, select `Open',\n"), -1);
391 (text, textview->msgfont, NULL, NULL,
392 _("or double-click, or click the center button, or press `l' key."), -1);
398 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
402 if (!partinfo) return;
404 textview_set_font(textview, NULL);
405 text = GTK_TEXT(textview->text);
406 textview_clear(textview);
408 gtk_text_freeze(text);
410 if (partinfo->sigstatus_full == NULL) {
412 (text, textview->msgfont, NULL, NULL,
413 _("This signature has not been checked yet.\n"), -1);
415 (text, textview->msgfont, NULL, NULL,
416 _("To check it, pop up the context menu with\n"), -1);
418 (text, textview->msgfont, NULL, NULL,
419 _("right click and select `Check signature'.\n"), -1);
421 gtk_text_insert(text, textview->msgfont, NULL, NULL,
422 partinfo->sigstatus_full, -1);
427 #endif /* USE_GPGME */
429 static void textview_show_html(TextView *textview, FILE *fp,
435 parser = html_parser_new(fp, conv);
436 g_return_if_fail(parser != NULL);
438 while ((str = html_parse(parser)) != NULL) {
439 textview_write_line(textview, str, NULL);
441 html_parser_destroy(parser);
444 /* get_uri_part() - retrieves a URI starting from scanpos.
445 Returns TRUE if succesful */
446 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
447 const gchar **bp, const gchar **ep)
451 g_return_val_if_fail(start != NULL, FALSE);
452 g_return_val_if_fail(scanpos != NULL, FALSE);
453 g_return_val_if_fail(bp != NULL, FALSE);
454 g_return_val_if_fail(ep != NULL, FALSE);
458 /* find end point of URI */
459 for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
460 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
464 /* no punctuation at end of string */
466 /* FIXME: this stripping of trailing punctuations may bite with other URIs.
467 * should pass some URI type to this function and decide on that whether
468 * to perform punctuation stripping */
470 #define IS_REAL_PUNCT(ch) \
471 (ispunct(ch) && ((ch) != '/'))
473 for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
483 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
485 return g_strndup(bp, ep - bp);
488 /* valid mail address characters */
489 #define IS_RFC822_CHAR(ch) \
494 !strchr("()<>\"", (ch)))
496 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
497 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
498 const gchar **bp, const gchar **ep)
500 /* more complex than the uri part because we need to scan back and forward starting from
501 * the scan position. */
502 gboolean result = FALSE;
506 g_return_val_if_fail(start != NULL, FALSE);
507 g_return_val_if_fail(scanpos != NULL, FALSE);
508 g_return_val_if_fail(bp != NULL, FALSE);
509 g_return_val_if_fail(ep != NULL, FALSE);
511 /* scan start of address */
512 for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
515 /* TODO: should start with an alnum? */
517 for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
520 if (bp_ != scanpos) {
521 /* scan end of address */
522 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
525 /* TODO: really should terminate with an alnum? */
526 for (; ep_ > scanpos && !isalnum(*ep_); --ep_)
530 if (ep_ > scanpos + 1) {
538 /* we now have at least the address. now see if this is <bracketed>; in this
539 * case we also scan for the informative part. we could make this a useful
540 * function because it tries to parse out an email address backwards. :) */
541 if ( ((bp_ - 1 > start) && *(bp_ - 1) == '<') && (*ep_ == '>')) {
542 char closure_stack[128];
543 char *ptr = closure_stack;
544 #define FULL_STACK() ((ptr - closure_stack) >= sizeof closure_stack)
545 #define IN_STACK() (ptr > closure_stack)
546 /* has underrun check */
547 #define POP_STACK() if(IN_STACK()) --ptr
548 /* has overrun check */
549 #define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
550 /* has underrun check */
551 #define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
555 /* scan for the informative part */
556 for (bp_ -= 2; bp_ >= start; bp_--) {
557 /* if closure on the stack keep scanning */
558 if (PEEK_STACK() == *bp_) {
562 if (*bp_ == '\'' || *bp_ == '"') {
567 /* if nothing in the closure stack, do the special conditions
568 * the following if..else expression simply checks whether
569 * a token is acceptable. if not acceptable, the clause
570 * should terminate the loop with a 'break' */
572 && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
573 && (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1))) ) {
574 /* hyphens are allowed, but only in between alnums */
576 else if ( !ispunct(*bp_) ) {
577 /* but anything not being a punctiation is ok */
580 break; /* anything else is rejected */
595 /* scan forward (should start with an alnum) */
596 for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
606 #undef IS_RFC822_CHAR
608 static gchar *make_email_string(const gchar *bp, const gchar *ep)
610 /* returns a mailto: URI; mailto: is also used to detect the
611 * uri type later on in the button_pressed signal handler */
615 tmp = g_strndup(bp, ep - bp);
616 result = g_strconcat("mailto:", tmp, NULL);
622 #define ADD_TXT_POS(bp_, ep_, pti_) \
623 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
625 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
628 g_warning("alloc error scanning URIs\n"); \
629 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
634 /* textview_make_clickable_parts() - colorizes clickable parts */
635 static void textview_make_clickable_parts(TextView *textview, GtkText *text,
639 const gchar *linebuf)
641 /* parse table - in order of priority */
643 const gchar *needle; /* token */
645 /* token search function */
646 gchar *(*search) (const gchar *haystack,
647 const gchar *needle);
648 /* part parsing function */
649 gboolean (*parse) (const gchar *start,
650 const gchar *scanpos,
653 /* part to URI function */
654 gchar *(*build_uri) (const gchar *bp,
658 static struct table parser[] = {
659 {"http://", strcasestr, get_uri_part, make_uri_string},
660 {"https://", strcasestr, get_uri_part, make_uri_string},
661 {"ftp://", strcasestr, get_uri_part, make_uri_string},
662 {"mailto:", strcasestr, get_uri_part, make_uri_string},
663 {"@", strcasestr, get_email_part, make_email_string}
665 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
668 const gchar *walk, *bp, *ep;
671 const gchar *bp, *ep; /* text position */
672 gint pti; /* index in parse table */
673 struct txtpos *next; /* next */
674 } head = {NULL, NULL, 0, NULL}, *last = &head;
676 /* parse for clickable parts, and build a list of begin and end positions */
677 for (walk = linebuf, n = 0;;) {
678 gint last_index = PARSE_ELEMS;
679 gchar *scanpos = NULL;
681 /* FIXME: this looks phony. scanning for anything in the parse table */
682 for (n = 0; n < PARSE_ELEMS; n++) {
685 tmp = parser[n].search(walk, parser[n].needle);
687 if (scanpos == NULL || tmp < scanpos) {
695 /* check if URI can be parsed */
696 if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
697 && (ep - bp - 1) > strlen(parser[last_index].needle)) {
698 ADD_TXT_POS(bp, ep, last_index);
702 strlen(parser[last_index].needle);
707 /* colorize this line */
709 const gchar *normal_text = linebuf;
712 for (last = head.next; last != NULL;
713 normal_text = last->ep, last = last->next) {
716 uri = g_new(RemoteURI, 1);
717 if (last->bp - normal_text > 0)
718 gtk_text_insert(text, textfont,
721 last->bp - normal_text);
722 uri->uri = parser[last->pti].build_uri(last->bp, last->ep);
723 uri->start = gtk_text_get_point(text);
724 gtk_text_insert(text, textfont, uri_color,
725 NULL, last->bp, last->ep - last->bp);
726 uri->end = gtk_text_get_point(text);
728 g_slist_append(textview->uri_list, uri);
732 gtk_text_insert(text, textfont, fg_color,
733 NULL, normal_text, -1);
735 gtk_text_insert(text, textfont, fg_color, NULL,
741 static void textview_write_line(TextView *textview, const gchar *str,
744 GtkText *text = GTK_TEXT(textview->text);
748 gint quotelevel = -1;
751 strncpy2(buf, str, sizeof(buf));
752 else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
753 gtk_text_insert(text, textview->msgfont,
754 prefs_common.enable_color
755 ? &error_color : NULL, NULL,
756 "*** Warning: code conversion failed ***\n",
762 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
766 if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
769 /* change color of quotation
770 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
771 Up to 3 levels of quotations are detected, and each
772 level is colored using a different color. */
773 if (prefs_common.enable_color && strchr(buf, '>')) {
774 quotelevel = get_quote_level(buf);
776 /* set up the correct foreground color */
777 if (quotelevel > 2) {
779 if (prefs_common.recycle_quote_colors)
786 if (quotelevel == -1)
789 fg_color = "e_colors[quotelevel];
791 if (prefs_common.head_space && spacingfont && buf[0] != '\n')
792 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
794 if (prefs_common.enable_color)
795 textview_make_clickable_parts(textview, text,
797 fg_color, &uri_color, buf);
799 gtk_text_insert(text, textview->msgfont, fg_color, NULL,
803 void textview_clear(TextView *textview)
805 GtkText *text = GTK_TEXT(textview->text);
807 gtk_text_freeze(text);
808 gtk_text_backward_delete(text, gtk_text_get_length(text));
811 textview_uri_list_remove_all(textview->uri_list);
812 textview->uri_list = NULL;
815 void textview_destroy(TextView *textview)
817 textview_uri_list_remove_all(textview->uri_list);
818 textview->uri_list = NULL;
820 if (!textview->scrolledwin_sb->parent)
821 gtk_widget_destroy(textview->scrolledwin_sb);
822 if (!textview->scrolledwin_mb->parent)
823 gtk_widget_destroy(textview->scrolledwin_mb);
825 if (textview->msgfont) {
826 textview->msgfont->ascent = textview->prev_ascent;
827 textview->msgfont->descent = textview->prev_descent;
828 gdk_font_unref(textview->msgfont);
830 if (textview->boldfont)
831 gdk_font_unref(textview->boldfont);
836 void textview_set_font(TextView *textview, const gchar *codeset)
838 gboolean use_fontset = TRUE;
840 /* In multi-byte mode, GtkText can't display 8bit characters
841 correctly, so it must be single-byte mode. */
842 if (MB_CUR_MAX > 1) {
844 if (!strncasecmp(codeset, "ISO-8859-", 9) ||
845 !strncasecmp(codeset, "KOI8-", 5) ||
846 !strncasecmp(codeset, "CP", 2) ||
847 !strncasecmp(codeset, "WINDOWS-", 8) ||
848 !strcasecmp(codeset, "BALTIC"))
854 if (textview->text_is_mb && !use_fontset) {
857 parent = textview->scrolledwin_mb->parent;
858 gtk_editable_select_region
859 (GTK_EDITABLE(textview->text_mb), 0, 0);
860 gtk_container_remove(GTK_CONTAINER(parent),
861 textview->scrolledwin_mb);
862 gtk_container_add(GTK_CONTAINER(parent),
863 textview->scrolledwin_sb);
865 textview->text = textview->text_sb;
866 textview->text_is_mb = FALSE;
867 } else if (!textview->text_is_mb && use_fontset) {
870 parent = textview->scrolledwin_sb->parent;
871 gtk_editable_select_region
872 (GTK_EDITABLE(textview->text_sb), 0, 0);
873 gtk_container_remove(GTK_CONTAINER(parent),
874 textview->scrolledwin_sb);
875 gtk_container_add(GTK_CONTAINER(parent),
876 textview->scrolledwin_mb);
878 textview->text = textview->text_mb;
879 textview->text_is_mb = TRUE;
882 if (prefs_common.textfont) {
883 if (textview->msgfont) {
884 textview->msgfont->ascent = textview->prev_ascent;
885 textview->msgfont->descent = textview->prev_descent;
886 gdk_font_unref(textview->msgfont);
887 textview->msgfont = NULL;
891 gdk_fontset_load(prefs_common.textfont);
893 if (MB_CUR_MAX > 1) {
894 FONT_LOAD(textview->msgfont,
895 "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
897 FONT_LOAD(textview->msgfont,
898 prefs_common.textfont);
902 if (textview->msgfont) {
903 gint ascent, descent;
905 textview->prev_ascent = textview->msgfont->ascent;
906 textview->prev_descent = textview->msgfont->descent;
907 descent = prefs_common.line_space / 2;
908 ascent = prefs_common.line_space - descent;
909 textview->msgfont->ascent += ascent;
910 textview->msgfont->descent += descent;
914 if (!textview->boldfont)
915 FONT_LOAD(textview->boldfont, BOLD_FONT);
917 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
936 static gboolean hdrequal(char * hdr1, char * hdr2)
943 if (hdr1[len1 - 1] == ':')
945 if (hdr2[len2 - 1] == ':')
949 return (strncasecmp(hdr1, hdr2, len1) == 0);
952 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
955 static HeaderEntry hentry[] = {{"Date:", NULL, FALSE},
956 {"From:", NULL, TRUE},
957 {"To:", NULL, FALSE},
958 {"Newsgroups:", NULL, FALSE},
959 {"Subject:", NULL, TRUE},
960 {"Cc:", NULL, FALSE},
961 {"Reply-To:", NULL, FALSE},
962 {"Followup-To:", NULL, FALSE},
963 {"X-Mailer:", NULL, TRUE},
964 {"X-Newsreader:", NULL, TRUE},
965 {"User-Agent:", NULL, TRUE},
966 {"Organization:", NULL, TRUE},
967 {NULL, NULL, FALSE}};
969 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
976 GPtrArray *sorted_headers;
978 g_return_val_if_fail(fp != NULL, NULL);
980 if (!prefs_common.display_header) {
981 while (fgets(buf, sizeof(buf), fp) != NULL)
982 if (buf[0] == '\r' || buf[0] == '\n') break;
986 headers = g_ptr_array_new();
989 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
995 header = g_new(Header, 1);
996 header->name = g_strndup(buf, strlen(hp->name));
997 conv_unmime_header(tmp, sizeof(tmp), buf + strlen(hp->name),
999 header->body = g_strdup(tmp);
1001 g_ptr_array_add(headers, header);
1004 // while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
1005 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1009 if (*buf == ':') continue;
1010 for (p = buf; *p && *p != ' '; p++) {
1012 header = g_new(Header, 1);
1013 header->name = g_strndup(buf, p - buf + 1);
1015 /* while (*p == ' ' || *p == '\t') p++; */
1016 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
1017 header->body = g_strdup(tmp);
1019 g_ptr_array_add(headers, header);
1025 sorted_headers = g_ptr_array_new();
1026 for(l = prefs_display_headers ; l != NULL ; l = g_slist_next(l)) {
1027 HeaderDisplayProp * dp = (HeaderDisplayProp *) l->data;
1028 for(i = 0 ; i < headers->len ; i++) {
1029 Header * header = g_ptr_array_index(headers, i);
1030 if (hdrequal(header->name, dp->name)) {
1032 g_ptr_array_remove_index(headers, i);
1033 g_free(header->body);
1034 g_free(header->name);
1039 g_ptr_array_add(sorted_headers,
1041 g_ptr_array_remove_index(headers, i);
1048 for(i = 0 ; i < headers->len ; i++) {
1049 Header * header = g_ptr_array_index(headers, i);
1050 g_ptr_array_add(sorted_headers, header);
1053 g_ptr_array_free(headers, TRUE);
1055 return sorted_headers;
1058 static void textview_show_header(TextView *textview, GPtrArray *headers)
1060 GtkText *text = GTK_TEXT(textview->text);
1064 g_return_if_fail(headers != NULL);
1066 gtk_text_freeze(text);
1068 for (i = 0; i < headers->len; i++) {
1069 header = g_ptr_array_index(headers, i);
1070 g_return_if_fail(header->name != NULL);
1072 gtk_text_insert(text, textview->boldfont, NULL, NULL,
1074 if (prefs_common.enable_color &&
1075 (strncmp(header->name, "X-Mailer", 8) == 0 ||
1076 strncmp(header->name, "X-Newsreader", 12) == 0) &&
1077 strstr(header->body, "Sylpheed") != NULL)
1078 gtk_text_insert(text, NULL, &emphasis_color, NULL,
1081 if (prefs_common.enable_color) {
1082 textview_make_clickable_parts(textview, text,
1089 gtk_text_insert(text, NULL, NULL, NULL,
1093 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1096 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1097 gtk_text_thaw(text);
1100 void textview_scroll_one_line(TextView *textview, gboolean up)
1102 GtkText *text = GTK_TEXT(textview->text);
1105 if (prefs_common.enable_smooth_scroll) {
1106 textview_smooth_scroll_one_line(textview, up);
1111 upper = text->vadj->upper - text->vadj->page_size;
1112 if (text->vadj->value < upper) {
1113 text->vadj->value +=
1114 text->vadj->step_increment * 4;
1116 MIN(text->vadj->value, upper);
1117 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1121 if (text->vadj->value > 0.0) {
1122 text->vadj->value -=
1123 text->vadj->step_increment * 4;
1125 MAX(text->vadj->value, 0.0);
1126 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1132 gboolean textview_scroll_page(TextView *textview, gboolean up)
1134 GtkText *text = GTK_TEXT(textview->text);
1138 if (prefs_common.enable_smooth_scroll)
1139 return textview_smooth_scroll_page(textview, up);
1141 if (prefs_common.scroll_halfpage)
1142 page_incr = text->vadj->page_increment / 2;
1144 page_incr = text->vadj->page_increment;
1147 upper = text->vadj->upper - text->vadj->page_size;
1148 if (text->vadj->value < upper) {
1149 text->vadj->value += page_incr;
1150 text->vadj->value = MIN(text->vadj->value, upper);
1151 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1156 if (text->vadj->value > 0.0) {
1157 text->vadj->value -= page_incr;
1158 text->vadj->value = MAX(text->vadj->value, 0.0);
1159 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1168 static void textview_smooth_scroll_do(TextView *textview,
1169 gfloat old_value, gfloat last_value,
1172 GtkText *text = GTK_TEXT(textview->text);
1177 if (old_value < last_value) {
1178 change_value = last_value - old_value;
1181 change_value = old_value - last_value;
1185 gdk_key_repeat_disable();
1187 for (i = step; i <= change_value; i += step) {
1188 text->vadj->value = old_value + (up ? -i : i);
1189 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1193 text->vadj->value = last_value;
1194 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1196 gdk_key_repeat_restore();
1199 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1201 GtkText *text = GTK_TEXT(textview->text);
1207 upper = text->vadj->upper - text->vadj->page_size;
1208 if (text->vadj->value < upper) {
1209 old_value = text->vadj->value;
1210 last_value = text->vadj->value +
1211 text->vadj->step_increment * 4;
1212 last_value = MIN(last_value, upper);
1214 textview_smooth_scroll_do(textview, old_value,
1216 prefs_common.scroll_step);
1219 if (text->vadj->value > 0.0) {
1220 old_value = text->vadj->value;
1221 last_value = text->vadj->value -
1222 text->vadj->step_increment * 4;
1223 last_value = MAX(last_value, 0.0);
1225 textview_smooth_scroll_do(textview, old_value,
1227 prefs_common.scroll_step);
1232 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1234 GtkText *text = GTK_TEXT(textview->text);
1240 if (prefs_common.scroll_halfpage)
1241 page_incr = text->vadj->page_increment / 2;
1243 page_incr = text->vadj->page_increment;
1246 upper = text->vadj->upper - text->vadj->page_size;
1247 if (text->vadj->value < upper) {
1248 old_value = text->vadj->value;
1249 last_value = text->vadj->value + page_incr;
1250 last_value = MIN(last_value, upper);
1252 textview_smooth_scroll_do(textview, old_value,
1254 prefs_common.scroll_step);
1258 if (text->vadj->value > 0.0) {
1259 old_value = text->vadj->value;
1260 last_value = text->vadj->value - page_incr;
1261 last_value = MAX(last_value, 0.0);
1263 textview_smooth_scroll_do(textview, old_value,
1265 prefs_common.scroll_step);
1273 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1276 SummaryView *summaryview = NULL;
1279 if (textview->messageview->mainwin)
1280 summaryview = textview->messageview->mainwin->summaryview;
1282 switch (event->keyval) {
1297 summary_pass_key_press_event(summaryview, event);
1299 textview_scroll_page(textview, FALSE);
1303 textview_scroll_page(textview, TRUE);
1306 textview_scroll_one_line(textview,
1307 (event->state & GDK_MOD1_MASK) != 0);
1311 summary_pass_key_press_event(summaryview, event);
1316 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1320 ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1321 || event->button == 2
1322 || event->button == 3)) {
1326 current_pos = GTK_EDITABLE(textview->text)->current_pos;
1328 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1329 RemoteURI *uri = (RemoteURI *)cur->data;
1331 if (current_pos >= uri->start &&
1332 current_pos < uri->end) {
1333 if (!strncasecmp(uri->uri, "mailto:", 7)) {
1334 compose_new_with_recipient
1335 (NULL, uri->uri + 7);
1339 prefs_common.uri_cmd);
1345 static void textview_uri_list_remove_all(GSList *uri_list)
1349 for (cur = uri_list; cur != NULL; cur = cur->next) {
1351 g_free(((RemoteURI *)cur->data)->uri);
1356 g_slist_free(uri_list);