inital gtk2 patch
[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 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         debug_print("Creating source window...\n");
60         sourcewin = g_new0(SourceWindow, 1);
61
62         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
63         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
64         gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
65         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
66         gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
67                                     prefs_common.sourcewin_height);
68         g_signal_connect(G_OBJECT(window), "size_allocate",
69                          G_CALLBACK(source_window_size_alloc_cb),
70                          sourcewin);
71         g_signal_connect(G_OBJECT(window), "destroy",
72                          G_CALLBACK(source_window_destroy_cb),
73                          sourcewin);
74         g_signal_connect(G_OBJECT(window), "key_press_event",
75                          G_CALLBACK(key_pressed), sourcewin);
76         gtk_widget_realize(window);
77
78         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
79         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
80                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
81         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
82         gtk_widget_show(scrolledwin);
83
84         text = gtk_text_view_new();
85         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
86         if (!font_desc && prefs_common.textfont)
87                 font_desc = pango_font_description_from_string
88                                         (prefs_common.textfont);
89         if (font_desc) {
90                 gtk_widget_modify_font(text, font_desc);
91                 pango_font_description_free(font_desc);
92         }
93         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
94         gtk_widget_show(text);
95
96         sourcewin->window = window;
97         sourcewin->scrolledwin = scrolledwin;
98         sourcewin->text = text;
99
100         source_window_init();
101
102         return sourcewin;
103 }
104
105 void source_window_show(SourceWindow *sourcewin)
106 {
107         gtk_widget_show_all(sourcewin->window);
108 }
109
110 void source_window_destroy(SourceWindow *sourcewin)
111 {
112         g_free(sourcewin);
113 }
114
115 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
116 {
117         gchar *file;
118         gchar *title;
119         FILE *fp;
120         gchar buf[BUFFSIZE];
121
122         g_return_if_fail(msginfo != NULL);
123
124         file = procmsg_get_message_file(msginfo);
125         g_return_if_fail(file != NULL);
126
127         if ((fp = fopen(file, "rb")) == NULL) {
128                 FILE_OP_ERROR(file, "fopen");
129                 g_free(file);
130                 return;
131         }
132
133         debug_print("Displaying the source of %s ...\n", file);
134
135         title = g_strdup_printf(_("%s - Source"), file);
136         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
137         g_free(title);
138         g_free(file);
139
140         while (fgets(buf, sizeof(buf), fp) != NULL)
141                 source_window_append(sourcewin, buf);
142
143         fclose(fp);
144 }
145
146 void source_window_append(SourceWindow *sourcewin, const gchar *str)
147 {
148         GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
149         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
150         GtkTextIter iter;
151         gchar *out;
152         gint len;
153
154         len = strlen(str) + 1;
155         Xalloca(out, len, return);
156 #warning FIXME_GTK2
157         conv_localetodisp(out, len, str);
158
159         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
160         gtk_text_buffer_insert(buffer, &iter, out, -1);
161 }
162
163 static void source_window_size_alloc_cb(GtkWidget *widget,
164                                         GtkAllocation *allocation)
165 {
166         g_return_if_fail(allocation != NULL);
167
168         prefs_common.sourcewin_width  = allocation->width;
169         prefs_common.sourcewin_height = allocation->height;
170 }
171
172 static void source_window_destroy_cb(GtkWidget *widget,
173                                      SourceWindow *sourcewin)
174 {
175         source_window_destroy(sourcewin);
176 }
177
178 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
179                             SourceWindow *sourcewin)
180 {
181
182         if (!event || !sourcewin) return FALSE;
183         
184         switch (event->keyval) {
185         case GDK_A:
186         case GDK_a:
187                 if ((event->state & GDK_CONTROL_MASK) != 0)
188                         gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
189                 break;
190         case GDK_Escape:
191                 gtk_widget_destroy(sourcewin->window);
192                 break;
193         }
194
195         return FALSE;
196 }