Statistics
| Revision:

root / src / sourcewindow.c @ 1

History | View | Annotate | Download (5.01 KB)

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/gtktextview.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 "procmsg.h"
36
#include "utils.h"
37
#include "gtkutils.h"
38
#include "prefs_common.h"
39

    
40
static void source_window_size_alloc_cb        (GtkWidget        *widget,
41
                                         GtkAllocation        *allocation);
42
static void source_window_destroy_cb        (GtkWidget        *widget,
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
        debug_print(_("Creating source window...\n"));
61
        sourcewin = g_new0(SourceWindow, 1);
62

    
63
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
64
        gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
65
        gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
66
        gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
67
        gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
68
                                    prefs_common.sourcewin_height);
69
        g_signal_connect(G_OBJECT(window), "size_allocate",
70
                         G_CALLBACK(source_window_size_alloc_cb), sourcewin);
71
        g_signal_connect(G_OBJECT(window), "destroy",
72
                         G_CALLBACK(source_window_destroy_cb), sourcewin);
73
        g_signal_connect(G_OBJECT(window), "key_press_event",
74
                         G_CALLBACK(key_pressed), sourcewin);
75
        gtk_widget_realize(window);
76

    
77
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
78
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
79
                                       GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
80
        gtk_container_add(GTK_CONTAINER(window), scrolledwin);
81
        gtk_widget_show(scrolledwin);
82

    
83
        text = gtk_text_view_new();
84
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
85
        if (!font_desc && prefs_common.textfont)
86
                font_desc = pango_font_description_from_string
87
                        (prefs_common.textfont);
88
        if (font_desc)
89
                gtk_widget_modify_font(text, font_desc);
90
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
91
        gtk_widget_show(text);
92

    
93
        sourcewin->window = window;
94
        sourcewin->scrolledwin = scrolledwin;
95
        sourcewin->text = text;
96

    
97
        source_window_init();
98

    
99
        return sourcewin;
100
}
101

    
102
void source_window_show(SourceWindow *sourcewin)
103
{
104
        gtk_widget_show_all(sourcewin->window);
105
}
106

    
107
void source_window_destroy(SourceWindow *sourcewin)
108
{
109
        g_free(sourcewin);
110
}
111

    
112
void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
113
{
114
        gchar *file;
115
        gchar *title;
116
        FILE *fp;
117
        gchar buf[BUFFSIZE];
118

    
119
        g_return_if_fail(msginfo != NULL);
120

    
121
        file = procmsg_get_message_file(msginfo);
122
        g_return_if_fail(file != NULL);
123

    
124
        if ((fp = fopen(file, "rb")) == NULL) {
125
                FILE_OP_ERROR(file, "fopen");
126
                g_free(file);
127
                return;
128
        }
129

    
130
        debug_print(_("Displaying the source of %s ...\n"), file);
131

    
132
        title = g_strdup_printf(_("%s - Source"), file);
133
        gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
134
        g_free(title);
135
        g_free(file);
136

    
137
        while (fgets(buf, sizeof(buf), fp) != NULL)
138
                source_window_append(sourcewin, buf);
139

    
140
        fclose(fp);
141
}
142

    
143
void source_window_append(SourceWindow *sourcewin, const gchar *str)
144
{
145
        GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
146
        GtkTextBuffer *buffer;
147
        GtkTextIter iter;
148
        gchar *out;
149
        gint len;
150

    
151
        buffer = gtk_text_view_get_buffer(text);
152

    
153
        len = strlen(str) + 1;
154
        Xalloca(out, len, return);
155
#warning FIXME_GTK2
156
        conv_localetodisp(out, len, str);
157

    
158
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
159
        gtk_text_buffer_insert(buffer, &iter, out, -1);
160
}
161

    
162
static void source_window_size_alloc_cb(GtkWidget *widget,
163
                                        GtkAllocation *allocation)
164
{
165
        g_return_if_fail(allocation != NULL);
166

    
167
        prefs_common.sourcewin_width  = allocation->width;
168
        prefs_common.sourcewin_height = allocation->height;
169
}
170

    
171
static void source_window_destroy_cb(GtkWidget *widget,
172
                                     SourceWindow *sourcewin)
173
{
174
        source_window_destroy(sourcewin);
175
}
176

    
177
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
178
                            SourceWindow *sourcewin)
179
{
180
        if (event && event->keyval == GDK_Escape)
181
                gtk_widget_destroy(sourcewin->window);
182
        return FALSE;
183
}