0.8.9claws5
[claws.git] / src / headerview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 <gtk/gtkwidget.h>
28 #include <gtk/gtkstyle.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkhbox.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkpixmap.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37
38 #if HAVE_LIBCOMPFACE
39 #  include <compface.h>
40 #endif
41
42 #include "intl.h"
43 #include "headerview.h"
44 #include "prefs_common.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47
48 static GdkFont *boldfont;
49
50 #define TR(str) (prefs_common.trans_hdr ? gettext(str) : str)
51
52 #if 0
53         _("From:");
54         _("To:");
55         _("Newsgroups:");
56         _("Subject:");
57 #endif
58
59 #if HAVE_LIBCOMPFACE
60 #define XPM_XFACE_HEIGHT        (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
61
62 static gchar *xpm_xface[XPM_XFACE_HEIGHT];
63
64 static void headerview_show_xface       (HeaderView     *headerview,
65                                          MsgInfo        *msginfo);
66 static gint create_xpm_from_xface       (gchar          *xpm[],
67                                          const gchar    *xface);
68 #endif
69
70 HeaderView *headerview_create(void)
71 {
72         HeaderView *headerview;
73         GtkWidget *hbox;
74         GtkWidget *vbox;
75         GtkWidget *hbox1;
76         GtkWidget *hbox2;
77         GtkWidget *from_header_label;
78         GtkWidget *from_body_label;
79         GtkWidget *to_header_label;
80         GtkWidget *to_body_label;
81         GtkWidget *ng_header_label;
82         GtkWidget *ng_body_label;
83         GtkWidget *subject_header_label;
84         GtkWidget *subject_body_label;
85
86         debug_print("Creating header view...\n");
87         headerview = g_new0(HeaderView, 1);
88
89         hbox = gtk_hbox_new(FALSE, 0);
90         gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
91         vbox = gtk_vbox_new(FALSE, 0);
92         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
93
94         hbox1 = gtk_hbox_new(FALSE, 4);
95         gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
96         hbox2 = gtk_hbox_new(FALSE, 4);
97         gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
98
99         from_header_label    = gtk_label_new(TR("From:"));
100         from_body_label      = gtk_label_new("");
101         to_header_label      = gtk_label_new(TR("To:"));
102         to_body_label        = gtk_label_new("");
103         ng_header_label      = gtk_label_new(TR("Newsgroups:"));
104         ng_body_label        = gtk_label_new("");
105         subject_header_label = gtk_label_new(TR("Subject:"));
106         subject_body_label   = gtk_label_new("");
107
108         gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
109         gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
110         gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
111         gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, FALSE, FALSE, 0);
112         gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
113         gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
115         gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, FALSE, FALSE, 0);
116
117         headerview->hbox = hbox;
118         headerview->from_header_label    = from_header_label;
119         headerview->from_body_label      = from_body_label;
120         headerview->to_header_label      = to_header_label;
121         headerview->to_body_label        = to_body_label;
122         headerview->ng_header_label      = ng_header_label;
123         headerview->ng_body_label        = ng_body_label;
124         headerview->subject_header_label = subject_header_label;
125         headerview->subject_body_label   = subject_body_label;
126         headerview->image = NULL;
127
128         gtk_widget_show_all(hbox);
129
130         return headerview;
131 }
132
133 void headerview_init(HeaderView *headerview)
134 {
135         if (!boldfont)
136                 boldfont = gtkut_font_load(BOLD_FONT);
137
138 #define SET_FONT_STYLE(wid) \
139 { \
140         GtkStyle *style; \
141  \
142         style = gtk_style_copy(gtk_widget_get_style(headerview->wid)); \
143         if (boldfont) \
144                 style->font = boldfont; \
145         gtk_widget_set_style(headerview->wid, style); \
146 }
147
148         SET_FONT_STYLE(from_header_label);
149         SET_FONT_STYLE(to_header_label);
150         SET_FONT_STYLE(ng_header_label);
151         SET_FONT_STYLE(subject_header_label);
152
153         headerview_clear(headerview);
154         headerview_set_visibility(headerview, prefs_common.display_header_pane);
155
156 #if HAVE_LIBCOMPFACE
157         {
158                 gint i;
159
160                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
161                         xpm_xface[i] = g_malloc(WIDTH + 1);
162                         *xpm_xface[i] = '\0';
163                 }
164         }
165 #endif
166 }
167
168 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
169 {
170         headerview_clear(headerview);
171
172         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
173                            msginfo->from ? msginfo->from : _("(No From)"));
174         if (msginfo->to) {
175                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
176                                    msginfo->to);
177                 gtk_widget_show(headerview->to_header_label);
178                 gtk_widget_show(headerview->to_body_label);
179         }
180         if (msginfo->newsgroups) {
181                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
182                                    msginfo->newsgroups);
183                 gtk_widget_show(headerview->ng_header_label);
184                 gtk_widget_show(headerview->ng_body_label);
185         }
186         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
187                            msginfo->subject ? msginfo->subject :
188                            _("(No Subject)"));
189
190 #if HAVE_LIBCOMPFACE
191         headerview_show_xface(headerview, msginfo);
192 #endif
193 }
194
195 #if HAVE_LIBCOMPFACE
196 static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
197 {
198         gchar xface[2048];
199         GdkPixmap *pixmap;
200         GdkBitmap *mask;
201         GtkWidget *hbox = headerview->hbox;
202
203         if (!msginfo->xface || strlen(msginfo->xface) < 5) {
204                 if (headerview->image &&
205                     GTK_WIDGET_VISIBLE(headerview->image)) {
206                         gtk_widget_hide(headerview->image);
207                         gtk_widget_queue_resize(hbox);
208                 }
209                 return;
210         }
211         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
212
213         strncpy(xface, msginfo->xface, sizeof(xface));
214
215         if (uncompface(xface) < 0) {
216                 g_warning("uncompface failed\n");
217                 if (headerview->image)
218                         gtk_widget_hide(headerview->image);
219                 return;
220         }
221
222         create_xpm_from_xface(xpm_xface, xface);
223
224         pixmap = gdk_pixmap_create_from_xpm_d
225                 (hbox->window, &mask, &hbox->style->white, xpm_xface);
226
227         if (!headerview->image) {
228                 GtkWidget *image;
229
230                 image = gtk_pixmap_new(pixmap, mask);
231                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
232                 gtk_widget_show(image);
233                 headerview->image = image;
234         } else {
235                 gtk_pixmap_set(GTK_PIXMAP(headerview->image), pixmap, mask);
236                 gtk_widget_show(headerview->image);
237         }
238
239         gdk_pixmap_unref(pixmap);
240 }
241 #endif
242
243 void headerview_clear(HeaderView *headerview)
244 {
245         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
246         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
247         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
248         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
249         gtk_widget_hide(headerview->to_header_label);
250         gtk_widget_hide(headerview->to_body_label);
251         gtk_widget_hide(headerview->ng_header_label);
252         gtk_widget_hide(headerview->ng_body_label);
253
254         if (headerview->image && GTK_WIDGET_VISIBLE(headerview->image)) {
255                 gtk_widget_hide(headerview->image);
256                 gtk_widget_queue_resize(headerview->hbox);
257         }
258 }
259
260 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
261 {
262         if (visibility)
263                 gtk_widget_show(headerview->hbox);
264         else
265                 gtk_widget_hide(headerview->hbox);
266 }
267
268 void headerview_destroy(HeaderView *headerview)
269 {
270         g_free(headerview);
271 }
272
273 #if HAVE_LIBCOMPFACE
274 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
275 {
276         static gchar *bit_pattern[] = {
277                 "....",
278                 "...#",
279                 "..#.",
280                 "..##",
281                 ".#..",
282                 ".#.#",
283                 ".##.",
284                 ".###",
285                 "#...",
286                 "#..#",
287                 "#.#.",
288                 "#.##",
289                 "##..",
290                 "##.#",
291                 "###.",
292                 "####"
293         };
294
295         static gchar *xface_header = "48 48 2 1";
296         static gchar *xface_black  = "# c #000000";
297         static gchar *xface_white  = ". c #ffffff";
298
299         gint i, line = 0;
300         const guchar *p;
301         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
302
303         p = xface;
304
305         strcpy(xpm[line++], xface_header);
306         strcpy(xpm[line++], xface_black);
307         strcpy(xpm[line++], xface_white);
308
309         for (i = 0; i < HEIGHT; i++) {
310                 gint col;
311
312                 buf[0] = '\0';
313      
314                 for (col = 0; col < 3; col++) {
315                         gint figure;
316
317                         p += 2;  /* skip '0x' */
318
319                         for (figure = 0; figure < 4; figure++) {
320                                 gint n = 0;
321
322                                 if ('0' <= *p && *p <= '9') {
323                                         n = *p - '0';
324                                 } else if ('a' <= *p && *p <= 'f') {
325                                         n = *p - 'a' + 10;
326                                 } else if ('A' <= *p && *p <= 'F') {
327                                         n = *p - 'A' + 10;
328                                 }
329
330                                 strcat(buf, bit_pattern[n]);
331                                 p++;  /* skip ',' */
332                         }
333
334                         p++;  /* skip '\n' */
335                 }
336
337                 strcpy(xpm[line++], buf);
338                 p++;
339         }
340
341         return 0;
342 }
343 #endif