Statistics
| Revision:

root / src / headerview.c @ 446

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