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