root / src / sourcewindow.c @ 478
History | View | Annotate | Download (5.2 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 "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 gint source_window_delete_cb (GtkWidget *widget,
|
| 43 | GdkEventAny *event, |
| 44 | SourceWindow *sourcewin); |
| 45 | static gboolean key_pressed (GtkWidget *widget,
|
| 46 | GdkEventKey *event, |
| 47 | SourceWindow *sourcewin); |
| 48 | |
| 49 | static void source_window_init() |
| 50 | {
|
| 51 | } |
| 52 | |
| 53 | SourceWindow *source_window_create(void)
|
| 54 | {
|
| 55 | SourceWindow *sourcewin; |
| 56 | GtkWidget *window; |
| 57 | GtkWidget *scrolledwin; |
| 58 | GtkWidget *text; |
| 59 | static PangoFontDescription *font_desc = NULL; |
| 60 | |
| 61 | debug_print(_("Creating source window...\n"));
|
| 62 | sourcewin = g_new0(SourceWindow, 1);
|
| 63 | |
| 64 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 65 | gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
|
| 66 | gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed"); |
| 67 | gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE); |
| 68 | gtk_widget_set_size_request(window, prefs_common.sourcewin_width, |
| 69 | prefs_common.sourcewin_height); |
| 70 | g_signal_connect(G_OBJECT(window), "size_allocate",
|
| 71 | G_CALLBACK(source_window_size_alloc_cb), sourcewin); |
| 72 | g_signal_connect(G_OBJECT(window), "delete_event",
|
| 73 | G_CALLBACK(source_window_delete_cb), 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_AUTOMATIC, |
| 81 | GTK_POLICY_ALWAYS); |
| 82 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin), |
| 83 | GTK_SHADOW_IN); |
| 84 | gtk_container_add(GTK_CONTAINER(window), scrolledwin); |
| 85 | gtk_widget_show(scrolledwin); |
| 86 | |
| 87 | text = gtk_text_view_new(); |
| 88 | gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE); |
| 89 | if (!font_desc && prefs_common.textfont)
|
| 90 | font_desc = pango_font_description_from_string |
| 91 | (prefs_common.textfont); |
| 92 | if (font_desc)
|
| 93 | gtk_widget_modify_font(text, font_desc); |
| 94 | gtk_container_add(GTK_CONTAINER(scrolledwin), text); |
| 95 | gtk_widget_show(text); |
| 96 | |
| 97 | sourcewin->window = window; |
| 98 | sourcewin->scrolledwin = scrolledwin; |
| 99 | sourcewin->text = text; |
| 100 | |
| 101 | source_window_init(); |
| 102 | |
| 103 | return sourcewin;
|
| 104 | } |
| 105 | |
| 106 | void source_window_show(SourceWindow *sourcewin)
|
| 107 | {
|
| 108 | gtk_widget_show_all(sourcewin->window); |
| 109 | } |
| 110 | |
| 111 | void source_window_destroy(SourceWindow *sourcewin)
|
| 112 | {
|
| 113 | gtk_widget_destroy(sourcewin->window); |
| 114 | g_free(sourcewin); |
| 115 | } |
| 116 | |
| 117 | void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
|
| 118 | {
|
| 119 | gchar *file; |
| 120 | gchar *title; |
| 121 | FILE *fp; |
| 122 | gchar buf[BUFFSIZE]; |
| 123 | |
| 124 | g_return_if_fail(msginfo != NULL);
|
| 125 | |
| 126 | file = procmsg_get_message_file(msginfo); |
| 127 | g_return_if_fail(file != NULL);
|
| 128 | |
| 129 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 130 | FILE_OP_ERROR(file, "fopen");
|
| 131 | g_free(file); |
| 132 | return;
|
| 133 | } |
| 134 | |
| 135 | debug_print(_("Displaying the source of %s ...\n"), file);
|
| 136 | |
| 137 | title = g_strdup_printf(_("%s - Source"), file);
|
| 138 | gtk_window_set_title(GTK_WINDOW(sourcewin->window), title); |
| 139 | g_free(title); |
| 140 | g_free(file); |
| 141 | |
| 142 | while (fgets(buf, sizeof(buf), fp) != NULL) |
| 143 | source_window_append(sourcewin, buf); |
| 144 | |
| 145 | fclose(fp); |
| 146 | } |
| 147 | |
| 148 | void source_window_append(SourceWindow *sourcewin, const gchar *str) |
| 149 | {
|
| 150 | GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text); |
| 151 | GtkTextBuffer *buffer; |
| 152 | GtkTextIter iter; |
| 153 | gchar *out; |
| 154 | |
| 155 | buffer = gtk_text_view_get_buffer(text); |
| 156 | |
| 157 | out = conv_utf8todisp(str, NULL);
|
| 158 | gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
|
| 159 | gtk_text_buffer_insert(buffer, &iter, out, -1);
|
| 160 | g_free(out); |
| 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 gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
|
| 173 | SourceWindow *sourcewin) |
| 174 | {
|
| 175 | source_window_destroy(sourcewin); |
| 176 | return TRUE;
|
| 177 | } |
| 178 | |
| 179 | static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
|
| 180 | SourceWindow *sourcewin) |
| 181 | {
|
| 182 | if (event && event->keyval == GDK_Escape) {
|
| 183 | source_window_destroy(sourcewin); |
| 184 | return TRUE;
|
| 185 | } |
| 186 | return FALSE;
|
| 187 | } |