sync with 0.7.4cvs21
[claws.git] / src / sourcewindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkwidget.h>
25 #include <gtk/gtkwindow.h>
26 #include <gtk/gtksignal.h>
27 #include <gtk/gtkscrolledwindow.h>
28 #include <gtk/gtktext.h>
29 #include <gtk/gtkstyle.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "intl.h"
34 #include "sourcewindow.h"
35 #include "procmsg.h"
36 #include "utils.h"
37 #include "gtkutils.h"
38 #include "prefs_common.h"
39
40 static void source_window_destroy_cb    (GtkWidget      *widget,
41                                          SourceWindow   *sourcewin);
42 static void key_pressed                 (GtkWidget      *widget,
43                                          GdkEventKey    *event,
44                                          SourceWindow   *sourcewin);
45
46 static GdkFont *msgfont = NULL;
47
48 #define FONT_LOAD(font, s) \
49 { \
50         gchar *fontstr, *p; \
51  \
52         Xstrdup_a(fontstr, s, ); \
53         if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
54         font = gdk_font_load(fontstr); \
55 }
56
57 static void source_window_init()
58 {
59         if (msgfont != NULL || prefs_common.textfont == NULL)
60                 return;
61
62         if (MB_CUR_MAX == 1) {
63                 FONT_LOAD(msgfont, prefs_common.textfont);
64         } else {
65                 msgfont = gdk_fontset_load(prefs_common.textfont);
66         }
67 }
68
69 SourceWindow *source_window_create(void)
70 {
71         SourceWindow *sourcewin;
72         GtkWidget *window;
73         GtkWidget *scrolledwin;
74         GtkWidget *text;
75
76         debug_print(_("Creating source window...\n"));
77         sourcewin = g_new0(SourceWindow, 1);
78
79         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
80         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
81         gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
82         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
83         gtk_widget_set_usize(window, 600, 500);
84         gtk_signal_connect(GTK_OBJECT(window), "destroy",
85                            GTK_SIGNAL_FUNC(source_window_destroy_cb),
86                            sourcewin);
87         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
88                            GTK_SIGNAL_FUNC(key_pressed), sourcewin);
89         gtk_widget_realize(window);
90
91         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
92         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
93                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
94         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
95         gtk_widget_show(scrolledwin);
96
97         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
98                             (GTK_SCROLLED_WINDOW(scrolledwin)),
99                             gtk_scrolled_window_get_vadjustment
100                             (GTK_SCROLLED_WINDOW(scrolledwin)));
101         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
102         gtk_widget_show(text);
103
104         sourcewin->window = window;
105         sourcewin->scrolledwin = scrolledwin;
106         sourcewin->text = text;
107
108         source_window_init();
109
110         return sourcewin;
111 }
112
113 void source_window_show(SourceWindow *sourcewin)
114 {
115         gtk_widget_show_all(sourcewin->window);
116 }
117
118 void source_window_destroy(SourceWindow *sourcewin)
119 {
120         g_free(sourcewin);
121 }
122
123 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
124 {
125         gchar *file;
126         gchar *title;
127         FILE *fp;
128         gchar buf[BUFFSIZE];
129
130         g_return_if_fail(msginfo != NULL);
131
132         file = procmsg_get_message_file(msginfo);
133         g_return_if_fail(file != NULL);
134
135         if ((fp = fopen(file, "rb")) == NULL) {
136                 FILE_OP_ERROR(file, "fopen");
137                 g_free(file);
138                 return;
139         }
140
141         debug_print(_("Displaying the source of %s ...\n"), file);
142
143         title = g_strdup_printf(_("%s - Source"), file);
144         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
145         g_free(title);
146         g_free(file);
147
148         gtk_text_freeze(GTK_TEXT(sourcewin->text));
149
150         while (fgets(buf, sizeof(buf), fp) != NULL)
151                 source_window_append(sourcewin, buf);
152
153         gtk_text_thaw(GTK_TEXT(sourcewin->text));
154
155         fclose(fp);
156 }
157
158 void source_window_append(SourceWindow *sourcewin, const gchar *str)
159 {
160         gtk_text_insert(GTK_TEXT(sourcewin->text), msgfont, NULL, NULL,
161                         str, -1);
162 }
163
164 static void source_window_destroy_cb(GtkWidget *widget,
165                                      SourceWindow *sourcewin)
166 {
167         source_window_destroy(sourcewin);
168 }
169
170 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
171                         SourceWindow *sourcewin)
172 {
173         if (event && event->keyval == GDK_Escape)
174                 gtk_widget_destroy(sourcewin->window);
175 }