Statistics
| Revision:

root / src / headerview.c @ 92

History | View | Annotate | Download (9.1 kB)

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