Statistics
| Revision:

root / src / sourcewindow.c @ 1963

History | View | Annotate | Download (6 kB)

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 <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/gtktextview.h>
30
#include <gtk/gtkstyle.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
34
#include "sourcewindow.h"
35
#include "procmsg.h"
36
#include "codeconv.h"
37
#include "utils.h"
38
#include "gtkutils.h"
39
#include "prefs_common.h"
40
41
static void source_window_size_alloc_cb        (GtkWidget        *widget,
42
                                         GtkAllocation        *allocation);
43
static gint source_window_delete_cb        (GtkWidget        *widget,
44
                                         GdkEventAny        *event,
45
                                         SourceWindow        *sourcewin);
46
static gboolean key_pressed                (GtkWidget        *widget,
47
                                         GdkEventKey        *event,
48
                                         SourceWindow        *sourcewin);
49
50
static void adj_value_changed                (GtkAdjustment        *adj,
51
                                         SourceWindow        *sourcewin);
52
53
static void source_window_init()
54
{
55
}
56
57
SourceWindow *source_window_create(void)
58
{
59
        SourceWindow *sourcewin;
60
        GtkWidget *window;
61
        GtkWidget *scrolledwin;
62
        GtkWidget *text;
63
        static PangoFontDescription *font_desc = NULL;
64
65
        debug_print(_("Creating source window...\n"));
66
        sourcewin = g_new0(SourceWindow, 1);
67
68
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
69
        gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
70
        gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
71
        gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
72
        gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
73
                                    prefs_common.sourcewin_height);
74
        g_signal_connect(G_OBJECT(window), "size_allocate",
75
                         G_CALLBACK(source_window_size_alloc_cb), sourcewin);
76
        g_signal_connect(G_OBJECT(window), "delete_event",
77
                         G_CALLBACK(source_window_delete_cb), sourcewin);
78
        g_signal_connect(G_OBJECT(window), "key_press_event",
79
                         G_CALLBACK(key_pressed), sourcewin);
80
81
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
82
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
83
                                       GTK_POLICY_AUTOMATIC,
84
                                       GTK_POLICY_ALWAYS);
85
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
86
                                            GTK_SHADOW_IN);
87
        gtk_container_add(GTK_CONTAINER(window), scrolledwin);
88
        gtk_widget_show(scrolledwin);
89
90
        text = gtk_text_view_new();
91
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
92
        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
93
        gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
94
        if (!font_desc && prefs_common.textfont)
95
                font_desc = pango_font_description_from_string
96
                        (prefs_common.textfont);
97
        if (font_desc)
98
                gtk_widget_modify_font(text, font_desc);
99
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
100
        gtk_widget_show(text);
101
102
        g_signal_connect(G_OBJECT(GTK_TEXT_VIEW(text)->vadjustment),
103
                         "value-changed",
104
                         G_CALLBACK(adj_value_changed), sourcewin);
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
        gtk_widget_destroy(sourcewin->window);
123
        g_free(sourcewin);
124
}
125
126
void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
127
{
128
        gchar *file;
129
        gchar *title;
130
        FILE *fp;
131
        gchar buf[BUFFSIZE];
132
        GtkTextBuffer *buffer;
133
        GtkTextIter iter;
134
135
        g_return_if_fail(msginfo != NULL);
136
137
        file = procmsg_get_message_file(msginfo);
138
        g_return_if_fail(file != NULL);
139
140
        if ((fp = g_fopen(file, "rb")) == NULL) {
141
                FILE_OP_ERROR(file, "fopen");
142
                g_free(file);
143
                return;
144
        }
145
146
        debug_print(_("Displaying the source of %s ...\n"), file);
147
148
        title = g_strdup_printf(_("%s - Source"), file);
149
        gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
150
        g_free(title);
151
        g_free(file);
152
153
        while (fgets(buf, sizeof(buf), fp) != NULL)
154
                source_window_append(sourcewin, buf);
155
156
        fclose(fp);
157
158
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourcewin->text));
159
        gtk_text_buffer_get_start_iter(buffer, &iter);
160
        gtk_text_buffer_place_cursor(buffer, &iter);
161
}
162
163
void source_window_append(SourceWindow *sourcewin, const gchar *str)
164
{
165
        GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
166
        GtkTextBuffer *buffer;
167
        GtkTextIter iter;
168
        gchar *out;
169
170
        buffer = gtk_text_view_get_buffer(text);
171
172
        out = conv_utf8todisp(str, NULL);
173
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
174
        gtk_text_buffer_insert(buffer, &iter, out, -1);
175
        g_free(out);
176
}
177
178
static void source_window_size_alloc_cb(GtkWidget *widget,
179
                                        GtkAllocation *allocation)
180
{
181
        g_return_if_fail(allocation != NULL);
182
183
        prefs_common.sourcewin_width  = allocation->width;
184
        prefs_common.sourcewin_height = allocation->height;
185
}
186
187
static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
188
                                    SourceWindow *sourcewin)
189
{
190
        source_window_destroy(sourcewin);
191
        return TRUE;
192
}
193
194
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
195
                            SourceWindow *sourcewin)
196
{
197
        if (event && event->keyval == GDK_Escape) {
198
                source_window_destroy(sourcewin);
199
                return TRUE;
200
        }
201
        return FALSE;
202
}
203
204
static void adj_value_changed(GtkAdjustment *adj, SourceWindow *sourcewin)
205
{
206
        GtkTextBuffer *buffer;
207
208
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourcewin->text));
209
        if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL))
210
                return;
211
        gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(sourcewin->text));
212
}