2005-01-29 [paul] 1.0.0cvs23.2
[claws.git] / src / sourcewindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 gboolean key_pressed             (GtkWidget      *widget,
44                                          GdkEventKey    *event,
45                                          SourceWindow   *sourcewin);
46
47 static void source_window_init()
48 {
49 }
50
51 SourceWindow *source_window_create(void)
52 {
53         SourceWindow *sourcewin;
54         GtkWidget *window;
55         GtkWidget *scrolledwin;
56         GtkWidget *text;
57         static PangoFontDescription *font_desc = NULL;
58
59         static GdkGeometry geometry;
60         
61         debug_print("Creating source window...\n");
62         sourcewin = g_new0(SourceWindow, 1);
63
64         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
65         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
66         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
67         gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
68                                     prefs_common.sourcewin_height);
69         
70         if (!geometry.min_height) {
71                 geometry.min_width = 400;
72                 geometry.min_height = 320;
73         }
74         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
75                                       GDK_HINT_MIN_SIZE);
76
77         g_signal_connect(G_OBJECT(window), "size_allocate",
78                          G_CALLBACK(source_window_size_alloc_cb),
79                          sourcewin);
80         g_signal_connect(G_OBJECT(window), "destroy",
81                          G_CALLBACK(source_window_destroy_cb),
82                          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_NEVER, GTK_POLICY_AUTOMATIC);
90         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
91                                             GTK_SHADOW_IN);
92         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
93         gtk_widget_show(scrolledwin);
94
95         text = gtk_text_view_new();
96         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
97         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
98         if (!font_desc && prefs_common.textfont)
99                 font_desc = pango_font_description_from_string
100                                         (prefs_common.textfont);
101         if (font_desc)
102                 gtk_widget_modify_font(text, font_desc);
103         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
104         gtk_widget_show(text);
105
106         sourcewin->window = window;
107         sourcewin->scrolledwin = scrolledwin;
108         sourcewin->text = text;
109
110         source_window_init();
111
112         return sourcewin;
113 }
114
115 void source_window_show(SourceWindow *sourcewin)
116 {
117         gtk_widget_show_all(sourcewin->window);
118 }
119
120 void source_window_destroy(SourceWindow *sourcewin)
121 {
122         g_free(sourcewin);
123 }
124
125 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
126 {
127         gchar *file;
128         gchar *title;
129         FILE *fp;
130         gchar buf[BUFFSIZE];
131
132         g_return_if_fail(msginfo != NULL);
133
134         file = procmsg_get_message_file(msginfo);
135         g_return_if_fail(file != NULL);
136
137         if ((fp = fopen(file, "rb")) == NULL) {
138                 FILE_OP_ERROR(file, "fopen");
139                 g_free(file);
140                 return;
141         }
142
143         debug_print("Displaying the source of %s ...\n", file);
144
145         title = g_strdup_printf(_("%s - Source"), file);
146         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
147         g_free(title);
148         g_free(file);
149
150         while (fgets(buf, sizeof(buf), fp) != NULL)
151                 source_window_append(sourcewin, buf);
152
153         fclose(fp);
154 }
155
156 void source_window_append(SourceWindow *sourcewin, const gchar *str)
157 {
158         GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
159         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
160         GtkTextIter iter;
161         gchar *out;
162         gint len;
163
164         len = strlen(str) + 1;
165         Xalloca(out, len, return);
166         
167         conv_localetodisp(out, len, str);
168         if (!g_utf8_validate(out, -1, NULL)) {
169                 gchar *buf;
170                 gint buflen;
171                 const gchar *src_codeset, *dest_codeset;
172                 src_codeset = conv_get_current_charset_str();
173                 dest_codeset = CS_UTF_8;
174                 buf = conv_codeset_strdup(out, src_codeset, dest_codeset);
175                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
176                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
177                 g_free(buf);
178         } else {
179                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
180                 gtk_text_buffer_insert(buffer, &iter, out, -1);
181         }
182 }
183
184 static void source_window_size_alloc_cb(GtkWidget *widget,
185                                         GtkAllocation *allocation)
186 {
187         g_return_if_fail(allocation != NULL);
188
189         prefs_common.sourcewin_width  = allocation->width;
190         prefs_common.sourcewin_height = allocation->height;
191 }
192
193 static void source_window_destroy_cb(GtkWidget *widget,
194                                      SourceWindow *sourcewin)
195 {
196         source_window_destroy(sourcewin);
197 }
198
199 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
200                             SourceWindow *sourcewin)
201 {
202
203         if (!event || !sourcewin) return FALSE;
204         
205         switch (event->keyval) {
206         case GDK_A:
207         case GDK_a:
208                 if ((event->state & GDK_CONTROL_MASK) != 0)
209                         gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
210                 break;
211         case GDK_W:
212         case GDK_w:
213                 if ((event->state & GDK_CONTROL_MASK) != 0)
214                         gtk_widget_destroy(sourcewin->window);
215                 break;
216         case GDK_Escape:
217                 gtk_widget_destroy(sourcewin->window);
218                 break;
219         }
220
221         return FALSE;
222 }