summaryrefslogtreecommitdiff
path: root/src/headerview.c
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /src/headerview.c
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/headerview.c')
-rw-r--r--src/headerview.c428
1 files changed, 428 insertions, 0 deletions
diff --git a/src/headerview.c b/src/headerview.c
new file mode 100644
index 0000000..984e8bc
--- /dev/null
+++ b/src/headerview.c
@@ -0,0 +1,428 @@
1/*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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#ifdef HAVE_CONFIG_H
21# include "config.h"
22#endif
23
24#include "defs.h"
25
26#include <glib.h>
27#include <glib/gi18n.h>
28#include <gtk/gtkversion.h>
29#include <gtk/gtkwidget.h>
30#include <gtk/gtkstyle.h>
31#include <gtk/gtkscrolledwindow.h>
32#include <gtk/gtkhbox.h>
33#include <gtk/gtkvbox.h>
34#include <gtk/gtklabel.h>
35#include <gtk/gtkpixmap.h>
36#include <gtk/gtkimage.h>
37#include <gtk/gtktooltips.h>
38#include <stdio.h>
39#include <string.h>
40#include <time.h>
41
42#if HAVE_LIBCOMPFACE
43# include <compface.h>
44#endif
45
46#include "main.h"
47#include "headerview.h"
48#include "prefs_common.h"
49#include "codeconv.h"
50#include "gtkutils.h"
51#include "utils.h"
52
53#define TR(str) (prefs_common.trans_hdr ? gettext(str) : str)
54
55#if 0
56 _("From:");
57 _("To:");
58 _("Cc:");
59 _("Newsgroups:");
60 _("Subject:");
61#endif
62
63#if HAVE_LIBCOMPFACE
64#define XPM_XFACE_HEIGHT (HEIGHT + 3) /* 3 = 1 header + 2 colors */
65
66static gchar *xpm_xface[XPM_XFACE_HEIGHT];
67
68static void headerview_show_xface (HeaderView *headerview,
69 MsgInfo *msginfo);
70static gint create_xpm_from_xface (gchar *xpm[],
71 const gchar *xface);
72#endif
73
74HeaderView *headerview_create(void)
75{
76 HeaderView *headerview;
77 GtkWidget *hbox;
78 GtkWidget *vbox;
79 GtkWidget *hbox1;
80 GtkWidget *hbox2;
81 GtkWidget *from_header_label;
82 GtkWidget *from_body_label;
83 GtkWidget *to_header_label;
84 GtkWidget *to_body_label;
85 GtkWidget *cc_header_label;
86 GtkWidget *cc_body_label;
87 GtkWidget *ng_header_label;
88 GtkWidget *ng_body_label;
89 GtkWidget *subject_header_label;
90 GtkWidget *subject_body_label;
91 GtkTooltips *tip;
92
93 debug_print(_("Creating header view...\n"));
94 headerview = g_new0(HeaderView, 1);
95
96 hbox = gtk_hbox_new(FALSE, 0);
97 gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
98 vbox = gtk_vbox_new(FALSE, 2);
99 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
100
101 hbox1 = gtk_hbox_new(FALSE, 4);
102 gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
103 hbox2 = gtk_hbox_new(FALSE, 4);
104 gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
105
106 from_header_label = gtk_label_new(TR("From:"));
107 from_body_label = gtk_label_new("");
108 to_header_label = gtk_label_new(TR("To:"));
109 to_body_label = gtk_label_new("");
110 cc_header_label = gtk_label_new(TR("Cc:"));
111 cc_body_label = gtk_label_new("");
112 ng_header_label = gtk_label_new(TR("Newsgroups:"));
113 ng_body_label = gtk_label_new("");
114 subject_header_label = gtk_label_new(TR("Subject:"));
115 subject_body_label = gtk_label_new("");
116
117 gtk_label_set_selectable(GTK_LABEL(from_body_label), TRUE);
118 gtk_label_set_selectable(GTK_LABEL(to_body_label), TRUE);
119 gtk_label_set_selectable(GTK_LABEL(cc_body_label), TRUE);
120 gtk_label_set_selectable(GTK_LABEL(ng_body_label), TRUE);
121 gtk_label_set_selectable(GTK_LABEL(subject_body_label), TRUE);
122
123 GTK_WIDGET_UNSET_FLAGS(from_body_label, GTK_CAN_FOCUS);
124 GTK_WIDGET_UNSET_FLAGS(to_body_label, GTK_CAN_FOCUS);
125 GTK_WIDGET_UNSET_FLAGS(cc_body_label, GTK_CAN_FOCUS);
126 GTK_WIDGET_UNSET_FLAGS(ng_body_label, GTK_CAN_FOCUS);
127 GTK_WIDGET_UNSET_FLAGS(subject_body_label, GTK_CAN_FOCUS);
128
129 gtk_widget_add_events(from_body_label, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
130 gtk_widget_add_events(to_body_label, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
131 gtk_widget_add_events(cc_body_label, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
132 gtk_widget_add_events(ng_body_label, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
133 gtk_widget_add_events(subject_body_label, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
134
135 tip = gtk_tooltips_new();
136 g_object_ref_sink(tip);
137
138 gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
139 gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
140 gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
141 gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, FALSE, FALSE, 0);
142 gtk_box_pack_start(GTK_BOX(hbox1), cc_header_label, FALSE, FALSE, 0);
143 gtk_box_pack_start(GTK_BOX(hbox1), cc_body_label, FALSE, FALSE, 0);
144 gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
145 gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, FALSE, FALSE, 0);
146 gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
147 gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, FALSE, FALSE, 0);
148
149 headerview->hbox = hbox;
150 headerview->from_header_label = from_header_label;
151 headerview->from_body_label = from_body_label;
152 headerview->to_header_label = to_header_label;
153 headerview->to_body_label = to_body_label;
154 headerview->cc_header_label = cc_header_label;
155 headerview->cc_body_label = cc_body_label;
156 headerview->ng_header_label = ng_header_label;
157 headerview->ng_body_label = ng_body_label;
158 headerview->subject_header_label = subject_header_label;
159 headerview->subject_body_label = subject_body_label;
160
161 headerview->image = NULL;
162
163 headerview->tip = tip;
164
165 gtk_widget_show_all(hbox);
166
167 return headerview;
168}
169
170void headerview_init(HeaderView *headerview)
171{
172 static PangoFontDescription *boldfont = NULL;
173#ifdef G_OS_WIN32
174 GtkStyle *style;
175#endif
176
177 if (!boldfont) {
178 boldfont = pango_font_description_new();
179 pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
180 }
181
182 if (boldfont) {
183 gtk_widget_modify_font(headerview->from_header_label, boldfont);
184 gtk_widget_modify_font(headerview->to_header_label, boldfont);
185 gtk_widget_modify_font(headerview->cc_header_label, boldfont);
186 gtk_widget_modify_font(headerview->ng_header_label, boldfont);
187 gtk_widget_modify_font(headerview->subject_header_label, boldfont);
188 }
189
190#ifdef G_OS_WIN32
191#define SET_LABEL_STYLE(label) \
192 style = gtk_widget_get_style(label); \
193 gtk_widget_modify_base(label, GTK_STATE_ACTIVE, \
194 &style->base[GTK_STATE_SELECTED]); \
195 gtk_widget_modify_text(label, GTK_STATE_ACTIVE, \
196 &style->text[GTK_STATE_SELECTED]);
197
198 SET_LABEL_STYLE(headerview->from_body_label);
199 SET_LABEL_STYLE(headerview->to_body_label);
200 SET_LABEL_STYLE(headerview->cc_body_label);
201 SET_LABEL_STYLE(headerview->ng_body_label);
202 SET_LABEL_STYLE(headerview->subject_body_label);
203
204#undef SET_LABEL_STYLE
205#endif
206
207 headerview_clear(headerview);
208 headerview_set_visibility(headerview, prefs_common.display_header_pane);
209
210#if HAVE_LIBCOMPFACE
211 {
212 gint i;
213
214 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
215 xpm_xface[i] = g_malloc(WIDTH + 1);
216 *xpm_xface[i] = '\0';
217 }
218 }
219#endif
220}
221
222void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
223{
224 headerview_clear(headerview);
225 gtk_tooltips_enable(headerview->tip);
226
227 gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
228 msginfo->from ? msginfo->from : _("(No From)"));
229 if (msginfo->from) {
230 gtk_tooltips_set_tip(headerview->tip, headerview->from_body_label, msginfo->from, NULL);
231 }
232
233 if (msginfo->to) {
234 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
235 msginfo->to);
236 gtk_widget_show(headerview->to_header_label);
237 gtk_widget_show(headerview->to_body_label);
238 gtk_tooltips_set_tip(headerview->tip, headerview->to_body_label, msginfo->to, NULL);
239 }
240
241 if (msginfo->cc) {
242 gtk_label_set_text(GTK_LABEL(headerview->cc_body_label),
243 msginfo->cc);
244 gtk_widget_show(headerview->cc_header_label);
245 gtk_widget_show(headerview->cc_body_label);
246 gtk_tooltips_set_tip(headerview->tip, headerview->cc_body_label, msginfo->cc, NULL);
247 }
248
249 if (msginfo->newsgroups) {
250 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
251 msginfo->newsgroups);
252 gtk_widget_show(headerview->ng_header_label);
253 gtk_widget_show(headerview->ng_body_label);
254 gtk_tooltips_set_tip(headerview->tip, headerview->ng_body_label, msginfo->newsgroups, NULL);
255 }
256
257 gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
258 msginfo->subject ? msginfo->subject
259 : _("(No Subject)"));
260 if (msginfo->subject) {
261 gtk_tooltips_set_tip(headerview->tip, headerview->subject_body_label, msginfo->subject, NULL);
262 }
263
264#if HAVE_LIBCOMPFACE
265 headerview_show_xface(headerview, msginfo);
266#endif
267}
268
269#if HAVE_LIBCOMPFACE
270static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
271{
272 gchar xface[2048];
273 GdkPixmap *pixmap;
274 GdkBitmap *mask;
275 GtkWidget *hbox = headerview->hbox;
276
277 if (!msginfo->xface || strlen(msginfo->xface) < 5) {
278 if (headerview->image &&
279 GTK_WIDGET_VISIBLE(headerview->image)) {
280 gtk_widget_hide(headerview->image);
281 gtk_widget_queue_resize(hbox);
282 }
283 return;
284 }
285 if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
286
287 strncpy2(xface, msginfo->xface, sizeof(xface));
288
289 if (uncompface(xface) < 0) {
290 g_warning("uncompface failed\n");
291 if (headerview->image)
292 gtk_widget_hide(headerview->image);
293 return;
294 }
295
296 create_xpm_from_xface(xpm_xface, xface);
297
298 pixmap = gdk_pixmap_create_from_xpm_d
299 (hbox->window, &mask, &hbox->style->white, xpm_xface);
300
301 if (!headerview->image) {
302 GtkWidget *image;
303
304 image = gtk_image_new_from_pixmap(pixmap, mask);
305 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
306 gtk_widget_show(image);
307 headerview->image = image;
308 } else {
309 gtk_image_set_from_pixmap(GTK_IMAGE(headerview->image),
310 pixmap, mask);
311 gtk_widget_show(headerview->image);
312 }
313
314 gdk_pixmap_unref(pixmap);
315}
316#endif
317
318void headerview_clear(HeaderView *headerview)
319{
320 gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
321 gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
322 gtk_label_set_text(GTK_LABEL(headerview->cc_body_label), "");
323 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
324 gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
325 gtk_widget_hide(headerview->to_header_label);
326 gtk_widget_hide(headerview->to_body_label);
327 gtk_widget_hide(headerview->cc_header_label);
328 gtk_widget_hide(headerview->cc_body_label);
329 gtk_widget_hide(headerview->ng_header_label);
330 gtk_widget_hide(headerview->ng_body_label);
331
332#if GTK_CHECK_VERSION(2, 12, 0)
333 gtk_widget_set_tooltip_text(headerview->from_body_label, NULL);
334 gtk_widget_set_tooltip_text(headerview->subject_body_label, NULL);
335#endif
336 gtk_tooltips_disable(headerview->tip);
337
338 if (headerview->image && GTK_WIDGET_VISIBLE(headerview->image)) {
339 gtk_widget_hide(headerview->image);
340 gtk_widget_queue_resize(headerview->hbox);
341 }
342}
343
344void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
345{
346 if (visibility)
347 gtk_widget_show(headerview->hbox);
348 else
349 gtk_widget_hide(headerview->hbox);
350}
351
352void headerview_destroy(HeaderView *headerview)
353{
354 g_object_unref(headerview->tip);
355 g_free(headerview);
356}
357
358#if HAVE_LIBCOMPFACE
359static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
360{
361 static gchar *bit_pattern[] = {
362 "....",
363 "...#",
364 "..#.",
365 "..##",
366 ".#..",
367 ".#.#",
368 ".##.",
369 ".###",
370 "#...",
371 "#..#",
372 "#.#.",
373 "#.##",
374 "##..",
375 "##.#",
376 "###.",
377 "####"
378 };
379
380 static gchar *xface_header = "48 48 2 1";
381 static gchar *xface_black = "# c #000000";
382 static gchar *xface_white = ". c #ffffff";
383
384 gint i, line = 0;
385 const guchar *p;
386 gchar buf[WIDTH * 4 + 1]; /* 4 = strlen("0x0000") */
387
388 p = (const guchar *)xface;
389
390 strcpy(xpm[line++], xface_header);
391 strcpy(xpm[line++], xface_black);
392 strcpy(xpm[line++], xface_white);
393
394 for (i = 0; i < HEIGHT; i++) {
395 gint col;
396
397 buf[0] = '\0';
398
399 for (col = 0; col < 3; col++) {
400 gint figure;
401
402 p += 2; /* skip '0x' */
403
404 for (figure = 0; figure < 4; figure++) {
405 gint n = 0;
406
407 if ('0' <= *p && *p <= '9') {
408 n = *p - '0';
409 } else if ('a' <= *p && *p <= 'f') {
410 n = *p - 'a' + 10;
411 } else if ('A' <= *p && *p <= 'F') {
412 n = *p - 'A' + 10;
413 }
414
415 strcat(buf, bit_pattern[n]);
416 p++; /* skip ',' */
417 }
418
419 p++; /* skip '\n' */
420 }
421
422 strcpy(xpm[line++], buf);
423 p++;
424 }
425
426 return 0;
427}
428#endif