Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / sourcewindow.c
blob5e9aa835725bf1a4b98c801963a71efd318fb602
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2019 the Claws Mail team and Hiroyuki Yamamoto
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
21 #include "defs.h"
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtk.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "sourcewindow.h"
31 #include "utils.h"
32 #include "gtkutils.h"
33 #include "prefs_common.h"
34 #include "file-utils.h"
36 static void source_window_size_alloc_cb (GtkWidget *widget,
37 GtkAllocation *allocation);
38 static gint source_window_delete_cb (GtkWidget *widget,
39 GdkEventAny *event,
40 SourceWindow *sourcewin);
41 static gboolean key_pressed (GtkWidget *widget,
42 GdkEventKey *event,
43 SourceWindow *sourcewin);
44 static void source_window_append (SourceWindow *sourcewin,
45 const gchar *str);
46 static void source_window_destroy (SourceWindow *sourcewin);
48 static void source_window_init()
52 SourceWindow *source_window_create(void)
54 SourceWindow *sourcewin;
55 GtkWidget *window;
56 GtkWidget *scrolledwin;
57 GtkWidget *text;
58 static PangoFontDescription *font_desc = NULL;
60 static GdkGeometry geometry;
62 debug_print("Creating source window...\n");
63 sourcewin = g_new0(SourceWindow, 1);
65 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "sourcewindow");
66 gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
67 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
68 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
70 if (!geometry.min_height) {
71 geometry.min_width = 400;
72 geometry.min_height = 320;
74 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
75 GDK_HINT_MIN_SIZE);
76 gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.sourcewin_width,
77 prefs_common.sourcewin_height);
79 g_signal_connect(G_OBJECT(window), "size_allocate",
80 G_CALLBACK(source_window_size_alloc_cb),
81 sourcewin);
82 g_signal_connect(G_OBJECT(window), "delete_event",
83 G_CALLBACK(source_window_delete_cb), sourcewin);
84 g_signal_connect(G_OBJECT(window), "key_press_event",
85 G_CALLBACK(key_pressed), sourcewin);
86 gtk_widget_realize(window);
88 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
89 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
90 GTK_POLICY_AUTOMATIC,
91 GTK_POLICY_AUTOMATIC);
92 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
93 GTK_SHADOW_IN);
94 gtk_container_add(GTK_CONTAINER(window), scrolledwin);
95 gtk_widget_show(scrolledwin);
97 text = gtk_text_view_new();
98 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
99 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
100 if (!font_desc && prefs_common.textfont)
101 font_desc = pango_font_description_from_string
102 (prefs_common.textfont);
103 if (font_desc)
104 gtk_widget_override_font(text, font_desc);
105 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
106 gtk_widget_show(text);
108 sourcewin->window = window;
109 sourcewin->scrolledwin = scrolledwin;
110 sourcewin->text = text;
112 source_window_init();
114 return sourcewin;
117 void source_window_show(SourceWindow *sourcewin)
119 gtk_widget_show_all(sourcewin->window);
122 static void source_window_destroy(SourceWindow *sourcewin)
124 if (sourcewin->updating) {
125 debug_print("deferring destroy\n");
126 sourcewin->deferred_destroy = TRUE;
127 return;
129 gtk_widget_destroy(sourcewin->window);
130 g_free(sourcewin);
133 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
135 gchar *file;
136 gchar *title;
137 FILE *fp;
138 gchar buf[BUFFSIZE];
140 cm_return_if_fail(msginfo != NULL);
142 sourcewin->updating = TRUE;
143 file = procmsg_get_message_file(msginfo);
144 sourcewin->updating = FALSE;
146 if (sourcewin->deferred_destroy) {
147 g_free(file);
148 source_window_destroy(sourcewin);
149 return;
152 cm_return_if_fail(file != NULL);
154 if ((fp = claws_fopen(file, "rb")) == NULL) {
155 FILE_OP_ERROR(file, "claws_fopen");
156 g_free(file);
157 return;
160 debug_print("Displaying the source of %s ...\n", file);
162 title = g_strdup_printf(_("%s - Source"), file);
163 gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
164 g_free(title);
165 g_free(file);
167 while (claws_fgets(buf, sizeof(buf), fp) != NULL)
168 source_window_append(sourcewin, buf);
170 claws_fclose(fp);
173 static void source_window_append(SourceWindow *sourcewin, const gchar *str)
175 GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
176 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
177 GtkTextIter iter;
178 gchar *out;
179 gint len;
181 len = strlen(str) + 1;
182 Xalloca(out, len, return);
183 conv_utf8todisp(out, len, str);
185 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
186 gtk_text_buffer_insert(buffer, &iter, out, -1);
189 static void source_window_size_alloc_cb(GtkWidget *widget,
190 GtkAllocation *allocation)
192 cm_return_if_fail(allocation != NULL);
194 gtk_window_get_size(GTK_WINDOW(widget),
195 &prefs_common.sourcewin_width, &prefs_common.sourcewin_height);
198 static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
199 SourceWindow *sourcewin)
201 source_window_destroy(sourcewin);
202 return TRUE;
205 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
206 SourceWindow *sourcewin)
208 if (!event || !sourcewin) return FALSE;
210 switch (event->keyval) {
211 case GDK_KEY_W:
212 case GDK_KEY_w:
213 if ((event->state & GDK_CONTROL_MASK) != 0)
214 gtk_widget_destroy(sourcewin->window);
215 break;
216 case GDK_KEY_Escape:
217 source_window_destroy(sourcewin);
218 return TRUE;
219 break;
222 return FALSE;