Statistics
| Revision:

root / src / sourcewindow.c @ 1963

History | View | Annotate | Download (6 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 27 hiro
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#include "defs.h"
21 1 hiro
22 1 hiro
#include <glib.h>
23 92 hiro
#include <glib/gi18n.h>
24 1 hiro
#include <gdk/gdkkeysyms.h>
25 1 hiro
#include <gtk/gtkwidget.h>
26 1 hiro
#include <gtk/gtkwindow.h>
27 1 hiro
#include <gtk/gtksignal.h>
28 1 hiro
#include <gtk/gtkscrolledwindow.h>
29 1 hiro
#include <gtk/gtktextview.h>
30 1 hiro
#include <gtk/gtkstyle.h>
31 1 hiro
#include <stdio.h>
32 1 hiro
#include <stdlib.h>
33 1 hiro
34 1 hiro
#include "sourcewindow.h"
35 1 hiro
#include "procmsg.h"
36 543 hiro
#include "codeconv.h"
37 1 hiro
#include "utils.h"
38 1 hiro
#include "gtkutils.h"
39 1 hiro
#include "prefs_common.h"
40 1 hiro
41 1 hiro
static void source_window_size_alloc_cb        (GtkWidget        *widget,
42 1 hiro
                                         GtkAllocation        *allocation);
43 316 hiro
static gint source_window_delete_cb        (GtkWidget        *widget,
44 316 hiro
                                         GdkEventAny        *event,
45 1 hiro
                                         SourceWindow        *sourcewin);
46 1 hiro
static gboolean key_pressed                (GtkWidget        *widget,
47 1 hiro
                                         GdkEventKey        *event,
48 1 hiro
                                         SourceWindow        *sourcewin);
49 1 hiro
50 1126 hiro
static void adj_value_changed                (GtkAdjustment        *adj,
51 1126 hiro
                                         SourceWindow        *sourcewin);
52 1126 hiro
53 1 hiro
static void source_window_init()
54 1 hiro
{
55 1 hiro
}
56 1 hiro
57 1 hiro
SourceWindow *source_window_create(void)
58 1 hiro
{
59 1 hiro
        SourceWindow *sourcewin;
60 1 hiro
        GtkWidget *window;
61 1 hiro
        GtkWidget *scrolledwin;
62 1 hiro
        GtkWidget *text;
63 1 hiro
        static PangoFontDescription *font_desc = NULL;
64 1 hiro
65 1 hiro
        debug_print(_("Creating source window...\n"));
66 1 hiro
        sourcewin = g_new0(SourceWindow, 1);
67 1 hiro
68 1 hiro
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
69 1 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
70 1 hiro
        gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
71 1 hiro
        gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
72 1 hiro
        gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
73 1 hiro
                                    prefs_common.sourcewin_height);
74 1 hiro
        g_signal_connect(G_OBJECT(window), "size_allocate",
75 1 hiro
                         G_CALLBACK(source_window_size_alloc_cb), sourcewin);
76 316 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
77 316 hiro
                         G_CALLBACK(source_window_delete_cb), sourcewin);
78 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
79 1 hiro
                         G_CALLBACK(key_pressed), sourcewin);
80 1 hiro
81 1 hiro
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
82 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
83 106 hiro
                                       GTK_POLICY_AUTOMATIC,
84 106 hiro
                                       GTK_POLICY_ALWAYS);
85 4 hiro
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
86 4 hiro
                                            GTK_SHADOW_IN);
87 1 hiro
        gtk_container_add(GTK_CONTAINER(window), scrolledwin);
88 1 hiro
        gtk_widget_show(scrolledwin);
89 1 hiro
90 1 hiro
        text = gtk_text_view_new();
91 1 hiro
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
92 769 hiro
        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
93 769 hiro
        gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
94 1 hiro
        if (!font_desc && prefs_common.textfont)
95 1 hiro
                font_desc = pango_font_description_from_string
96 1 hiro
                        (prefs_common.textfont);
97 1 hiro
        if (font_desc)
98 1 hiro
                gtk_widget_modify_font(text, font_desc);
99 1 hiro
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
100 1 hiro
        gtk_widget_show(text);
101 1 hiro
102 1126 hiro
        g_signal_connect(G_OBJECT(GTK_TEXT_VIEW(text)->vadjustment),
103 1126 hiro
                         "value-changed",
104 1126 hiro
                         G_CALLBACK(adj_value_changed), sourcewin);
105 1126 hiro
106 1 hiro
        sourcewin->window = window;
107 1 hiro
        sourcewin->scrolledwin = scrolledwin;
108 1 hiro
        sourcewin->text = text;
109 1 hiro
110 1 hiro
        source_window_init();
111 1 hiro
112 1 hiro
        return sourcewin;
113 1 hiro
}
114 1 hiro
115 1 hiro
void source_window_show(SourceWindow *sourcewin)
116 1 hiro
{
117 1 hiro
        gtk_widget_show_all(sourcewin->window);
118 1 hiro
}
119 1 hiro
120 1 hiro
void source_window_destroy(SourceWindow *sourcewin)
121 1 hiro
{
122 316 hiro
        gtk_widget_destroy(sourcewin->window);
123 1 hiro
        g_free(sourcewin);
124 1 hiro
}
125 1 hiro
126 1 hiro
void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
127 1 hiro
{
128 1 hiro
        gchar *file;
129 1 hiro
        gchar *title;
130 1 hiro
        FILE *fp;
131 1 hiro
        gchar buf[BUFFSIZE];
132 1126 hiro
        GtkTextBuffer *buffer;
133 1126 hiro
        GtkTextIter iter;
134 1 hiro
135 1 hiro
        g_return_if_fail(msginfo != NULL);
136 1 hiro
137 1 hiro
        file = procmsg_get_message_file(msginfo);
138 1 hiro
        g_return_if_fail(file != NULL);
139 1 hiro
140 478 hiro
        if ((fp = g_fopen(file, "rb")) == NULL) {
141 1 hiro
                FILE_OP_ERROR(file, "fopen");
142 1 hiro
                g_free(file);
143 1 hiro
                return;
144 1 hiro
        }
145 1 hiro
146 1 hiro
        debug_print(_("Displaying the source of %s ...\n"), file);
147 1 hiro
148 1 hiro
        title = g_strdup_printf(_("%s - Source"), file);
149 1 hiro
        gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
150 1 hiro
        g_free(title);
151 1 hiro
        g_free(file);
152 1 hiro
153 1 hiro
        while (fgets(buf, sizeof(buf), fp) != NULL)
154 1 hiro
                source_window_append(sourcewin, buf);
155 1 hiro
156 1 hiro
        fclose(fp);
157 1126 hiro
158 1126 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourcewin->text));
159 1126 hiro
        gtk_text_buffer_get_start_iter(buffer, &iter);
160 1126 hiro
        gtk_text_buffer_place_cursor(buffer, &iter);
161 1 hiro
}
162 1 hiro
163 1 hiro
void source_window_append(SourceWindow *sourcewin, const gchar *str)
164 1 hiro
{
165 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
166 1 hiro
        GtkTextBuffer *buffer;
167 1 hiro
        GtkTextIter iter;
168 1 hiro
        gchar *out;
169 1 hiro
170 1 hiro
        buffer = gtk_text_view_get_buffer(text);
171 1 hiro
172 185 hiro
        out = conv_utf8todisp(str, NULL);
173 1 hiro
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
174 1 hiro
        gtk_text_buffer_insert(buffer, &iter, out, -1);
175 180 hiro
        g_free(out);
176 1 hiro
}
177 1 hiro
178 1 hiro
static void source_window_size_alloc_cb(GtkWidget *widget,
179 1 hiro
                                        GtkAllocation *allocation)
180 1 hiro
{
181 1 hiro
        g_return_if_fail(allocation != NULL);
182 1 hiro
183 1 hiro
        prefs_common.sourcewin_width  = allocation->width;
184 1 hiro
        prefs_common.sourcewin_height = allocation->height;
185 1 hiro
}
186 1 hiro
187 316 hiro
static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
188 316 hiro
                                    SourceWindow *sourcewin)
189 1 hiro
{
190 1 hiro
        source_window_destroy(sourcewin);
191 316 hiro
        return TRUE;
192 1 hiro
}
193 1 hiro
194 1 hiro
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
195 1 hiro
                            SourceWindow *sourcewin)
196 1 hiro
{
197 316 hiro
        if (event && event->keyval == GDK_Escape) {
198 316 hiro
                source_window_destroy(sourcewin);
199 316 hiro
                return TRUE;
200 316 hiro
        }
201 1 hiro
        return FALSE;
202 1 hiro
}
203 1126 hiro
204 1126 hiro
static void adj_value_changed(GtkAdjustment *adj, SourceWindow *sourcewin)
205 1126 hiro
{
206 1126 hiro
        GtkTextBuffer *buffer;
207 1126 hiro
208 1126 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourcewin->text));
209 1126 hiro
        if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL))
210 1126 hiro
                return;
211 1126 hiro
        gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(sourcewin->text));
212 1126 hiro
}