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