sync with 0.8.11cvs31
[claws.git] / src / sourcewindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 "utils.h"
36 #include "gtkutils.h"
37 #include "prefs_common.h"
38
39 static void source_window_size_alloc_cb (GtkWidget      *widget,
40                                          GtkAllocation  *allocation);
41 static void source_window_destroy_cb    (GtkWidget      *widget,
42                                          SourceWindow   *sourcewin);
43 static void key_pressed                 (GtkWidget      *widget,
44                                          GdkEventKey    *event,
45                                          SourceWindow   *sourcewin);
46
47 static GdkFont *msgfont = NULL;
48
49 static void source_window_init()
50 {
51         if (!msgfont && prefs_common.textfont)
52                 msgfont = gtkut_font_load(prefs_common.textfont);
53 }
54
55 SourceWindow *source_window_create(void)
56 {
57         SourceWindow *sourcewin;
58         GtkWidget *window;
59         GtkWidget *scrolledwin;
60         GtkWidget *text;
61
62         debug_print("Creating source window...\n");
63         sourcewin = g_new0(SourceWindow, 1);
64
65         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
66         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
67         gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
68         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
69         gtk_widget_set_usize(window, prefs_common.sourcewin_width,
70                              prefs_common.sourcewin_height);
71         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
72                            GTK_SIGNAL_FUNC(source_window_size_alloc_cb),
73                            sourcewin);
74         gtk_signal_connect(GTK_OBJECT(window), "destroy",
75                            GTK_SIGNAL_FUNC(source_window_destroy_cb),
76                            sourcewin);
77         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
78                            GTK_SIGNAL_FUNC(key_pressed), sourcewin);
79         gtk_widget_realize(window);
80
81         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
82         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
83                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
84         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
85         gtk_widget_show(scrolledwin);
86
87         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
88                             (GTK_SCROLLED_WINDOW(scrolledwin)),
89                             gtk_scrolled_window_get_vadjustment
90                             (GTK_SCROLLED_WINDOW(scrolledwin)));
91         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
92         gtk_widget_show(text);
93
94         sourcewin->window = window;
95         sourcewin->scrolledwin = scrolledwin;
96         sourcewin->text = text;
97
98         source_window_init();
99
100         return sourcewin;
101 }
102
103 void source_window_show(SourceWindow *sourcewin)
104 {
105         gtk_widget_show_all(sourcewin->window);
106 }
107
108 void source_window_destroy(SourceWindow *sourcewin)
109 {
110         g_free(sourcewin);
111 }
112
113 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
114 {
115         gchar *file;
116         gchar *title;
117         FILE *fp;
118         gchar buf[BUFFSIZE];
119
120         g_return_if_fail(msginfo != NULL);
121
122         file = procmsg_get_message_file(msginfo);
123         g_return_if_fail(file != NULL);
124
125         if ((fp = fopen(file, "rb")) == NULL) {
126                 FILE_OP_ERROR(file, "fopen");
127                 g_free(file);
128                 return;
129         }
130
131         debug_print("Displaying the source of %s ...\n", file);
132
133         title = g_strdup_printf(_("%s - Source"), file);
134         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
135         g_free(title);
136         g_free(file);
137
138         gtk_text_freeze(GTK_TEXT(sourcewin->text));
139
140         while (fgets(buf, sizeof(buf), fp) != NULL)
141                 source_window_append(sourcewin, buf);
142
143         gtk_text_thaw(GTK_TEXT(sourcewin->text));
144
145         fclose(fp);
146 }
147
148 void source_window_append(SourceWindow *sourcewin, const gchar *str)
149 {
150         gchar *out;
151         gint len;
152
153         len = strlen(str) + 1;
154         Xalloca(out, len, return);
155         conv_localetodisp(out, len, str);
156         gtk_text_insert(GTK_TEXT(sourcewin->text), msgfont, NULL, NULL,
157                         out, -1);
158 }
159
160 static void source_window_size_alloc_cb(GtkWidget *widget,
161                                         GtkAllocation *allocation)
162 {
163         g_return_if_fail(allocation != NULL);
164
165         prefs_common.sourcewin_width  = allocation->width;
166         prefs_common.sourcewin_height = allocation->height;
167 }
168
169 static void source_window_destroy_cb(GtkWidget *widget,
170                                      SourceWindow *sourcewin)
171 {
172         source_window_destroy(sourcewin);
173 }
174
175 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
176                         SourceWindow *sourcewin)
177 {
178         if (event && event->keyval == GDK_Escape)
179                 gtk_widget_destroy(sourcewin->window);
180 }