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 "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 (!mimeinfo->sub || mimeinfo->sub->children) return;
311 headers = textview_scan_header(textview, fp);
312 mimeinfo = mimeinfo->sub;
313 } else if (!mimeinfo->parent &&
314 mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
315 if (!mimeinfo->sub) return;
316 headers = textview_scan_header(textview, fp);
317 mimeinfo = mimeinfo->sub;
320 if (prefs_common.force_charset)
321 charset = prefs_common.force_charset;
322 else if (mimeinfo->charset)
323 charset = mimeinfo->charset;
324 textview_set_font(textview, charset);
326 conv = conv_code_converter_new(charset);
328 textview_clear(textview);
329 text = GTK_TEXT(textview->text);
330 gtk_text_freeze(text);
333 textview_show_header(textview, headers);
334 procheader_header_array_destroy(headers);
337 tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
339 if (mimeinfo->mime_type == MIME_TEXT_HTML)
340 textview_show_html(textview, tmpfp, conv);
342 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
343 textview_write_line(textview, buf, conv);
347 conv_code_converter_destroy(conv);
352 #define TEXT_INSERT(str) \
353 gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
355 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
359 if (!partinfo) return;
361 textview_set_font(textview, NULL);
362 text = GTK_TEXT(textview->text);
363 textview_clear(textview);
365 gtk_text_freeze(text);
367 TEXT_INSERT(_("To save this part, pop up the context menu with "));
368 TEXT_INSERT(_("right click and select `Save as...', "));
369 TEXT_INSERT(_("or press `y' key.\n\n"));
371 TEXT_INSERT(_("To display this part as a text message, select "));
372 TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
374 TEXT_INSERT(_("To open this part with external program, select "));
375 TEXT_INSERT(_("`Open' or `Open with...', "));
376 TEXT_INSERT(_("or double-click, or click the center button, "));
377 TEXT_INSERT(_("or press `l' key."));
383 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
387 if (!partinfo) return;
389 textview_set_font(textview, NULL);
390 text = GTK_TEXT(textview->text);
391 textview_clear(textview);
393 gtk_text_freeze(text);
395 if (partinfo->sigstatus_full == NULL) {
396 TEXT_INSERT(_("This signature has not been checked yet.\n"));
397 TEXT_INSERT(_("To check it, pop up the context menu with\n"));
398 TEXT_INSERT(_("right click and select `Check signature'.\n"));
400 TEXT_INSERT(partinfo->sigstatus_full);
405 #endif /* USE_GPGME */
409 static void textview_show_html(TextView *textview, FILE *fp,
415 parser = html_parser_new(fp, conv);
416 g_return_if_fail(parser != NULL);
418 while ((str = html_parse(parser)) != NULL) {
419 textview_write_line(textview, str, NULL);
421 html_parser_destroy(parser);
424 /* get_uri_part() - retrieves a URI starting from scanpos.
425 Returns TRUE if succesful */
426 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
427 const gchar **bp, const gchar **ep)
431 g_return_val_if_fail(start != NULL, FALSE);
432 g_return_val_if_fail(scanpos != NULL, FALSE);
433 g_return_val_if_fail(bp != NULL, FALSE);
434 g_return_val_if_fail(ep != NULL, FALSE);
438 /* find end point of URI */
439 for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
440 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
444 /* no punctuation at end of string */
446 /* FIXME: this stripping of trailing punctuations may bite with other URIs.
447 * should pass some URI type to this function and decide on that whether
448 * to perform punctuation stripping */
450 #define IS_REAL_PUNCT(ch) \
451 (ispunct(ch) && ((ch) != '/'))
453 for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
463 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
465 return g_strndup(bp, ep - bp);
468 /* valid mail address characters */
469 #define IS_RFC822_CHAR(ch) \
474 !strchr("()<>\"", (ch)))
476 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
477 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
478 const gchar **bp, const gchar **ep)
480 /* more complex than the uri part because we need to scan back and forward starting from
481 * the scan position. */
482 gboolean result = FALSE;
483 const gchar *bp_ = NULL;
484 const gchar *ep_ = NULL;
486 /* the informative part of the email address (describing the name
487 * of the email address owner) may contain quoted parts. the
488 * closure stack stores the last encountered quotes. */
489 gchar closure_stack[128];
490 gchar *ptr = closure_stack;
492 g_return_val_if_fail(start != NULL, FALSE);
493 g_return_val_if_fail(scanpos != NULL, FALSE);
494 g_return_val_if_fail(bp != NULL, FALSE);
495 g_return_val_if_fail(ep != NULL, FALSE);
497 /* scan start of address */
498 for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
501 /* TODO: should start with an alnum? */
503 for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
506 if (bp_ != scanpos) {
507 /* scan end of address */
508 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
511 /* TODO: really should terminate with an alnum? */
512 for (; ep_ > scanpos && !isalnum(*ep_); --ep_)
516 if (ep_ > scanpos + 1) {
523 if (!result) return FALSE;
525 /* we now have at least the address. now see if this is <bracketed>; in this
526 * case we also scan for the informative part. we could make this a useful
527 * function because it tries to parse out an email address backwards. :) */
528 if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
531 #define FULL_STACK() ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
532 #define IN_STACK() (ptr > closure_stack)
533 /* has underrun check */
534 #define POP_STACK() if(IN_STACK()) --ptr
535 /* has overrun check */
536 #define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
537 /* has underrun check */
538 #define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
542 /* scan for the informative part. */
543 for (bp_ -= 2; bp_ >= start; bp_--) {
544 /* if closure on the stack keep scanning */
545 if (PEEK_STACK() == *bp_) {
549 if (*bp_ == '\'' || *bp_ == '"') {
554 /* if nothing in the closure stack, do the special conditions
555 * the following if..else expression simply checks whether
556 * a token is acceptable. if not acceptable, the clause
557 * should terminate the loop with a 'break' */
560 && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
561 && (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1)))) {
562 /* hyphens are allowed, but only in
564 } else if (!ispunct(*bp_)) {
565 /* but anything not being a punctiation
568 break; /* anything else is rejected */
581 /* scan forward (should start with an alnum) */
582 for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
591 #undef IS_RFC822_CHAR
593 static gchar *make_email_string(const gchar *bp, const gchar *ep)
595 /* returns a mailto: URI; mailto: is also used to detect the
596 * uri type later on in the button_pressed signal handler */
600 tmp = g_strndup(bp, ep - bp);
601 result = g_strconcat("mailto:", tmp, NULL);
607 #define ADD_TXT_POS(bp_, ep_, pti_) \
608 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
610 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
613 g_warning("alloc error scanning URIs\n"); \
614 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
619 /* textview_make_clickable_parts() - colorizes clickable parts */
620 static void textview_make_clickable_parts(TextView *textview,
624 const gchar *linebuf)
626 /* parse table - in order of priority */
628 const gchar *needle; /* token */
630 /* token search function */
631 gchar *(*search) (const gchar *haystack,
632 const gchar *needle);
633 /* part parsing function */
634 gboolean (*parse) (const gchar *start,
635 const gchar *scanpos,
638 /* part to URI function */
639 gchar *(*build_uri) (const gchar *bp,
643 static struct table parser[] = {
644 {"http://", strcasestr, get_uri_part, make_uri_string},
645 {"https://", strcasestr, get_uri_part, make_uri_string},
646 {"ftp://", strcasestr, get_uri_part, make_uri_string},
647 {"mailto:", strcasestr, get_uri_part, make_uri_string},
648 {"@", strcasestr, get_email_part, make_email_string}
650 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
653 const gchar *walk, *bp, *ep;
656 const gchar *bp, *ep; /* text position */
657 gint pti; /* index in parse table */
658 struct txtpos *next; /* next */
659 } head = {NULL, NULL, 0, NULL}, *last = &head;
661 GtkText *text = GTK_TEXT(textview->text);
663 /* parse for clickable parts, and build a list of begin and end positions */
664 for (walk = linebuf, n = 0;;) {
665 gint last_index = PARSE_ELEMS;
666 gchar *scanpos = NULL;
668 /* FIXME: this looks phony. scanning for anything in the parse table */
669 for (n = 0; n < PARSE_ELEMS; n++) {
672 tmp = parser[n].search(walk, parser[n].needle);
674 if (scanpos == NULL || tmp < scanpos) {
682 /* check if URI can be parsed */
683 if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
684 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
685 ADD_TXT_POS(bp, ep, last_index);
689 strlen(parser[last_index].needle);
694 /* colorize this line */
696 const gchar *normal_text = linebuf;
699 for (last = head.next; last != NULL;
700 normal_text = last->ep, last = last->next) {
703 uri = g_new(RemoteURI, 1);
704 if (last->bp - normal_text > 0)
705 gtk_text_insert(text, font,
708 last->bp - normal_text);
709 uri->uri = parser[last->pti].build_uri(last->bp,
711 uri->start = gtk_text_get_point(text);
712 gtk_text_insert(text, font, uri_color,
713 NULL, last->bp, last->ep - last->bp);
714 uri->end = gtk_text_get_point(text);
716 g_slist_append(textview->uri_list, uri);
720 gtk_text_insert(text, font, fg_color,
721 NULL, normal_text, -1);
723 gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
728 static void textview_write_line(TextView *textview, const gchar *str,
731 GtkText *text = GTK_TEXT(textview->text);
735 gint quotelevel = -1;
738 strncpy2(buf, str, sizeof(buf));
739 else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
740 gtk_text_insert(text, textview->msgfont,
741 prefs_common.enable_color
742 ? &error_color : NULL, NULL,
743 "*** Warning: code conversion failed ***\n",
749 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
753 if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
756 /* change color of quotation
757 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
758 Up to 3 levels of quotations are detected, and each
759 level is colored using a different color. */
760 if (prefs_common.enable_color && strchr(buf, '>')) {
761 quotelevel = get_quote_level(buf);
763 /* set up the correct foreground color */
764 if (quotelevel > 2) {
766 if (prefs_common.recycle_quote_colors)
773 if (quotelevel == -1)
776 fg_color = "e_colors[quotelevel];
778 if (prefs_common.head_space && spacingfont && buf[0] != '\n')
779 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
781 if (prefs_common.enable_color)
782 textview_make_clickable_parts(textview, textview->msgfont,
783 fg_color, &uri_color, buf);
785 gtk_text_insert(text, textview->msgfont, fg_color, NULL,
789 void textview_clear(TextView *textview)
791 GtkText *text = GTK_TEXT(textview->text);
793 gtk_text_freeze(text);
794 gtk_text_backward_delete(text, gtk_text_get_length(text));
797 textview_uri_list_remove_all(textview->uri_list);
798 textview->uri_list = NULL;
801 void textview_destroy(TextView *textview)
803 textview_uri_list_remove_all(textview->uri_list);
804 textview->uri_list = NULL;
806 if (!textview->scrolledwin_sb->parent)
807 gtk_widget_destroy(textview->scrolledwin_sb);
808 if (!textview->scrolledwin_mb->parent)
809 gtk_widget_destroy(textview->scrolledwin_mb);
811 if (textview->msgfont) {
812 textview->msgfont->ascent = textview->prev_ascent;
813 textview->msgfont->descent = textview->prev_descent;
814 gdk_font_unref(textview->msgfont);
816 if (textview->boldfont)
817 gdk_font_unref(textview->boldfont);
822 void textview_set_font(TextView *textview, const gchar *codeset)
824 gboolean use_fontset = TRUE;
826 /* In multi-byte mode, GtkText can't display 8bit characters
827 correctly, so it must be single-byte mode. */
828 if (MB_CUR_MAX > 1) {
830 if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
831 !g_strncasecmp(codeset, "KOI8-", 5) ||
832 !g_strncasecmp(codeset, "CP", 2) ||
833 !g_strncasecmp(codeset, "WINDOWS-", 8) ||
834 !g_strcasecmp(codeset, "BALTIC"))
840 if (textview->text_is_mb && !use_fontset) {
843 parent = textview->scrolledwin_mb->parent;
844 gtk_editable_select_region
845 (GTK_EDITABLE(textview->text_mb), 0, 0);
846 gtk_container_remove(GTK_CONTAINER(parent),
847 textview->scrolledwin_mb);
848 gtk_container_add(GTK_CONTAINER(parent),
849 textview->scrolledwin_sb);
851 textview->text = textview->text_sb;
852 textview->text_is_mb = FALSE;
853 } else if (!textview->text_is_mb && use_fontset) {
856 parent = textview->scrolledwin_sb->parent;
857 gtk_editable_select_region
858 (GTK_EDITABLE(textview->text_sb), 0, 0);
859 gtk_container_remove(GTK_CONTAINER(parent),
860 textview->scrolledwin_sb);
861 gtk_container_add(GTK_CONTAINER(parent),
862 textview->scrolledwin_mb);
864 textview->text = textview->text_mb;
865 textview->text_is_mb = TRUE;
868 if (prefs_common.textfont) {
869 if (textview->msgfont) {
870 textview->msgfont->ascent = textview->prev_ascent;
871 textview->msgfont->descent = textview->prev_descent;
872 gdk_font_unref(textview->msgfont);
873 textview->msgfont = NULL;
877 gdk_fontset_load(prefs_common.textfont);
879 if (MB_CUR_MAX > 1) {
880 FONT_LOAD(textview->msgfont,
881 "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
883 FONT_LOAD(textview->msgfont,
884 prefs_common.textfont);
888 if (textview->msgfont) {
889 gint ascent, descent;
891 textview->prev_ascent = textview->msgfont->ascent;
892 textview->prev_descent = textview->msgfont->descent;
893 descent = prefs_common.line_space / 2;
894 ascent = prefs_common.line_space - descent;
895 textview->msgfont->ascent += ascent;
896 textview->msgfont->descent += descent;
900 if (!textview->boldfont)
901 FONT_LOAD(textview->boldfont, BOLD_FONT);
903 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
922 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
925 GPtrArray *headers, *sorted_headers;
926 GSList *disphdr_list;
932 g_return_val_if_fail(fp != NULL, NULL);
934 if (!prefs_common.display_header) {
935 while (fgets(buf, sizeof(buf), fp) != NULL)
936 if (buf[0] == '\r' || buf[0] == '\n') break;
940 headers = procheader_get_header_array_asis(fp);
942 sorted_headers = g_ptr_array_new();
944 for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
945 disphdr_list = disphdr_list->next) {
946 DisplayHeaderProp *dp =
947 (DisplayHeaderProp *)disphdr_list->data;
949 for (i = 0; i < headers->len; i++) {
950 header = g_ptr_array_index(headers, i);
952 if (procheader_headername_equal(header->name,
955 procheader_header_free(header);
957 g_ptr_array_add(sorted_headers, header);
959 g_ptr_array_remove_index(headers, i);
965 if (prefs_common.show_other_header) {
966 for (i = 0; i < headers->len; i++) {
967 header = g_ptr_array_index(headers, i);
968 g_ptr_array_add(sorted_headers, header);
972 g_ptr_array_free(headers, TRUE);
973 return sorted_headers;
976 static void textview_show_header(TextView *textview, GPtrArray *headers)
978 GtkText *text = GTK_TEXT(textview->text);
982 g_return_if_fail(headers != NULL);
984 gtk_text_freeze(text);
986 for (i = 0; i < headers->len; i++) {
987 header = g_ptr_array_index(headers, i);
988 g_return_if_fail(header->name != NULL);
990 gtk_text_insert(text, textview->boldfont, NULL, NULL,
992 gtk_text_insert(text, textview->boldfont, NULL, NULL, " ", 1);
994 if (procheader_headername_equal(header->name, "Subject") ||
995 procheader_headername_equal(header->name, "From") ||
996 procheader_headername_equal(header->name, "To") ||
997 procheader_headername_equal(header->name, "Cc"))
998 unfold_line(header->body);
1000 if (prefs_common.enable_color &&
1001 (procheader_headername_equal(header->name, "X-Mailer") ||
1002 procheader_headername_equal(header->name,
1004 strstr(header->body, "Sylpheed") != NULL)
1005 gtk_text_insert(text, NULL, &emphasis_color, NULL,
1007 else if (prefs_common.enable_color) {
1008 textview_make_clickable_parts(textview,
1009 NULL, NULL, &uri_color,
1012 gtk_text_insert(text, NULL, NULL, NULL,
1015 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1018 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1019 gtk_text_thaw(text);
1022 void textview_scroll_one_line(TextView *textview, gboolean up)
1024 GtkText *text = GTK_TEXT(textview->text);
1027 if (prefs_common.enable_smooth_scroll) {
1028 textview_smooth_scroll_one_line(textview, up);
1033 upper = text->vadj->upper - text->vadj->page_size;
1034 if (text->vadj->value < upper) {
1035 text->vadj->value +=
1036 text->vadj->step_increment * 4;
1038 MIN(text->vadj->value, upper);
1039 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1043 if (text->vadj->value > 0.0) {
1044 text->vadj->value -=
1045 text->vadj->step_increment * 4;
1047 MAX(text->vadj->value, 0.0);
1048 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1054 gboolean textview_scroll_page(TextView *textview, gboolean up)
1056 GtkText *text = GTK_TEXT(textview->text);
1060 if (prefs_common.enable_smooth_scroll)
1061 return textview_smooth_scroll_page(textview, up);
1063 if (prefs_common.scroll_halfpage)
1064 page_incr = text->vadj->page_increment / 2;
1066 page_incr = text->vadj->page_increment;
1069 upper = text->vadj->upper - text->vadj->page_size;
1070 if (text->vadj->value < upper) {
1071 text->vadj->value += page_incr;
1072 text->vadj->value = MIN(text->vadj->value, upper);
1073 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1078 if (text->vadj->value > 0.0) {
1079 text->vadj->value -= page_incr;
1080 text->vadj->value = MAX(text->vadj->value, 0.0);
1081 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1090 static void textview_smooth_scroll_do(TextView *textview,
1091 gfloat old_value, gfloat last_value,
1094 GtkText *text = GTK_TEXT(textview->text);
1099 if (old_value < last_value) {
1100 change_value = last_value - old_value;
1103 change_value = old_value - last_value;
1107 gdk_key_repeat_disable();
1109 for (i = step; i <= change_value; i += step) {
1110 text->vadj->value = old_value + (up ? -i : i);
1111 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1115 text->vadj->value = last_value;
1116 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1118 gdk_key_repeat_restore();
1121 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1123 GtkText *text = GTK_TEXT(textview->text);
1129 upper = text->vadj->upper - text->vadj->page_size;
1130 if (text->vadj->value < upper) {
1131 old_value = text->vadj->value;
1132 last_value = text->vadj->value +
1133 text->vadj->step_increment * 4;
1134 last_value = MIN(last_value, upper);
1136 textview_smooth_scroll_do(textview, old_value,
1138 prefs_common.scroll_step);
1141 if (text->vadj->value > 0.0) {
1142 old_value = text->vadj->value;
1143 last_value = text->vadj->value -
1144 text->vadj->step_increment * 4;
1145 last_value = MAX(last_value, 0.0);
1147 textview_smooth_scroll_do(textview, old_value,
1149 prefs_common.scroll_step);
1154 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1156 GtkText *text = GTK_TEXT(textview->text);
1162 if (prefs_common.scroll_halfpage)
1163 page_incr = text->vadj->page_increment / 2;
1165 page_incr = text->vadj->page_increment;
1168 upper = text->vadj->upper - text->vadj->page_size;
1169 if (text->vadj->value < upper) {
1170 old_value = text->vadj->value;
1171 last_value = text->vadj->value + page_incr;
1172 last_value = MIN(last_value, upper);
1174 textview_smooth_scroll_do(textview, old_value,
1176 prefs_common.scroll_step);
1180 if (text->vadj->value > 0.0) {
1181 old_value = text->vadj->value;
1182 last_value = text->vadj->value - page_incr;
1183 last_value = MAX(last_value, 0.0);
1185 textview_smooth_scroll_do(textview, old_value,
1187 prefs_common.scroll_step);
1195 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1198 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,
1245 ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1246 || event->button == 2
1247 || event->button == 3)) {
1251 current_pos = GTK_EDITABLE(textview->text)->current_pos;
1253 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1254 RemoteURI *uri = (RemoteURI *)cur->data;
1256 if (current_pos >= uri->start &&
1257 current_pos < uri->end) {
1258 if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1259 compose_new_with_recipient
1260 (NULL, uri->uri + 7);
1264 prefs_common.uri_cmd);
1270 static void textview_uri_list_remove_all(GSList *uri_list)
1274 for (cur = uri_list; cur != NULL; cur = cur->next) {
1276 g_free(((RemoteURI *)cur->data)->uri);
1281 g_slist_free(uri_list);