2006-02-23 [wwp] 2.0.0cvs74
[claws.git] / src / sourcewindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
27 #include <gtk/gtksignal.h>
28 #include <gtk/gtkscrolledwindow.h>
29 #include <gtk/gtktext.h>
30 #include <gtk/gtkstyle.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
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 gint source_window_delete_cb     (GtkWidget      *widget,
42                                          GdkEventAny    *event,
43                                          SourceWindow   *sourcewin);
44 static gboolean key_pressed             (GtkWidget      *widget,
45                                          GdkEventKey    *event,
46                                          SourceWindow   *sourcewin);
47
48 static void source_window_init()
49 {
50 }
51
52 SourceWindow *source_window_create(void)
53 {
54         SourceWindow *sourcewin;
55         GtkWidget *window;
56         GtkWidget *scrolledwin;
57         GtkWidget *text;
58         static PangoFontDescription *font_desc = NULL;
59
60         static GdkGeometry geometry;
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_resizable(GTK_WINDOW(window), TRUE);
68         gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
69                                     prefs_common.sourcewin_height);
70         
71         if (!geometry.min_height) {
72                 geometry.min_width = 400;
73                 geometry.min_height = 320;
74         }
75         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
76                                       GDK_HINT_MIN_SIZE);
77
78         g_signal_connect(G_OBJECT(window), "size_allocate",
79                          G_CALLBACK(source_window_size_alloc_cb),
80                          sourcewin);
81         g_signal_connect(G_OBJECT(window), "delete_event",
82                          G_CALLBACK(source_window_delete_cb), sourcewin);
83         g_signal_connect(G_OBJECT(window), "key_press_event",
84                          G_CALLBACK(key_pressed), sourcewin);
85         gtk_widget_realize(window);
86
87         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
88         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
89                                        GTK_POLICY_AUTOMATIC,
90                                        GTK_POLICY_AUTOMATIC);
91         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
92                                             GTK_SHADOW_IN);
93         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
94         gtk_widget_show(scrolledwin);
95
96         text = gtk_text_view_new();
97         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
98         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
99         if (!font_desc && prefs_common.textfont)
100                 font_desc = pango_font_description_from_string
101                                         (prefs_common.textfont);
102         if (font_desc)
103                 gtk_widget_modify_font(text, font_desc);
104         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
105         gtk_widget_show(text);
106
107         sourcewin->window = window;
108         sourcewin->scrolledwin = scrolledwin;
109         sourcewin->text = text;
110
111         source_window_init();
112
113         return sourcewin;
114 }
115
116 void source_window_show(SourceWindow *sourcewin)
117 {
118         gtk_widget_show_all(sourcewin->window);
119 }
120
121 void source_window_destroy(SourceWindow *sourcewin)
122 {
123         gtk_widget_destroy(sourcewin->window);
124         g_free(sourcewin);
125 }
126
127 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
128 {
129         gchar *file;
130         gchar *title;
131         FILE *fp;
132         gchar buf[BUFFSIZE];
133
134         g_return_if_fail(msginfo != NULL);
135
136         file = procmsg_get_message_file(msginfo);
137         g_return_if_fail(file != NULL);
138
139         if ((fp = g_fopen(file, "rb")) == NULL) {
140                 FILE_OP_ERROR(file, "fopen");
141                 g_free(file);
142                 return;
143         }
144
145         debug_print("Displaying the source of %s ...\n", file);
146
147         title = g_strdup_printf(_("%s - Source"), file);
148         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
149         g_free(title);
150         g_free(file);
151
152         while (fgets(buf, sizeof(buf), fp) != NULL)
153                 source_window_append(sourcewin, buf);
154
155         fclose(fp);
156 }
157
158 void source_window_append(SourceWindow *sourcewin, const gchar *str)
159 {
160         GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
161         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
162         GtkTextIter iter;
163         gchar *out;
164         gint len;
165
166         len = strlen(str) + 1;
167         Xalloca(out, len, return);
168         conv_utf8todisp(out, len, str);
169
170         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
171         gtk_text_buffer_insert(buffer, &iter, out, -1);
172 }
173
174 static void source_window_size_alloc_cb(GtkWidget *widget,
175                                         GtkAllocation *allocation)
176 {
177         g_return_if_fail(allocation != NULL);
178
179         prefs_common.sourcewin_width  = allocation->width;
180         prefs_common.sourcewin_height = allocation->height;
181 }
182
183 static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
184                                     SourceWindow *sourcewin)
185 {
186         source_window_destroy(sourcewin);
187         return TRUE;
188 }
189
190 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
191                             SourceWindow *sourcewin)
192 {
193
194         if (!event || !sourcewin) return FALSE;
195         
196         switch (event->keyval) {
197         case GDK_A:
198         case GDK_a:
199                 if ((event->state & GDK_CONTROL_MASK) != 0)
200                         gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
201                 break;
202         case GDK_W:
203         case GDK_w:
204                 if ((event->state & GDK_CONTROL_MASK) != 0)
205                         gtk_widget_destroy(sourcewin->window);
206                 break;
207         case GDK_Escape:
208                 source_window_destroy(sourcewin);
209                 return TRUE;
210                 break;
211         }
212
213         return FALSE;
214 }