Statistics
| Revision:

root / src / colorlabel.c @ 2441

History | View | Annotate | Download (10.9 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 2001-2010 Hiroyuki Yamamoto & The Sylpheed Claws Team
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
/* (alfons) - based on a contribution by Satoshi Nagayasu; revised for colorful 
21
 * menu and more Sylpheed integration. The idea to put the code in a separate
22
 * file is just that it make it easier to allow "user changeable" label colors.
23
 */
24
25
#include "defs.h"
26
27
#include <glib.h>
28
#include <glib/gi18n.h>
29
#include <gtk/gtkwidget.h>
30
#include <gtk/gtkpixmap.h>
31
#include <gtk/gtkmenu.h>
32
#include <gtk/gtkcheckmenuitem.h>
33
#include <gtk/gtklabel.h>
34
#include <gtk/gtkmenuitem.h>
35
#include <gtk/gtkalignment.h>
36
#include <gtk/gtkhbox.h>
37
#include <gtk/gtkvbox.h>
38
#include <gtk/gtkwindow.h>
39
#include <gtk/gtkdrawingarea.h>
40
41
#include "colorlabel.h"
42
#include "gtkutils.h"
43
#include "utils.h"
44
#include "prefs.h"
45
46
static gchar *labels[] = {
47
        N_("Orange"),
48
        N_("Red") ,
49
        N_("Pink"),
50
        N_("Sky blue"),
51
        N_("Blue"),
52
        N_("Green"),
53
        N_("Brown")
54
};
55
56
typedef enum LabelColorChangeFlags_ {
57
        LCCF_COLOR = 1 << 0,
58
        LCCF_LABEL = 1 << 1,
59
        LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
60
} LabelColorChangeFlags;
61
62
/* XXX: if you add colors, make sure you also check the procmsg.h.
63
 * color indices are stored as 3 bits; that explains the max. of 7 colors */
64
static struct 
65
{
66
        LabelColorChangeFlags        changed; 
67
        GdkColor                color;
68
69
        /* XXX: note that the label member is supposed to be dynamically 
70
         * allocated and fffreed */
71
        gchar                        *label;
72
        GtkWidget                *widget;
73
        GtkWidget                *label_widget;
74
} label_colors[] = {
75
        { LCCF_ALL, { 0, 0xffff, (0x99 << 8), 0x0 },                NULL, NULL, NULL },
76
        { LCCF_ALL, { 0, 0xffff, 0, 0 },                        NULL, NULL, NULL },
77
        { LCCF_ALL, { 0, 0xffff, (0x66 << 8), 0xffff },                NULL, NULL, NULL },
78
        { LCCF_ALL, { 0, 0x0, (0xcc << 8), 0xffff },                NULL, NULL, NULL },
79
        { LCCF_ALL, { 0, 0x0, 0x0, 0xffff },                        NULL, NULL, NULL },
80
        { LCCF_ALL, { 0, 0x0, 0x99 << 8, 0x0 },                        NULL, NULL, NULL },
81
        { LCCF_ALL, { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 },        NULL, NULL, NULL }
82
};
83
84
#define LABEL_COLOR_WIDTH        28
85
#define LABEL_COLOR_HEIGHT        16
86
87
#define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
88
89
#define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
90
        g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
91
92
static void colorlabel_recreate        (gint);
93
static void colorlabel_recreate_label  (gint);
94
95
gint colorlabel_get_color_count(void)
96
{
97
        return LABEL_COLORS_ELEMS;
98
}
99
100
GdkColor colorlabel_get_color(gint color_index)
101
{
102
        GdkColor invalid = { 0 };
103
104
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
105
106
        return label_colors[color_index].color;
107
}
108
109
const gchar *colorlabel_get_color_text(gint color_index)
110
{
111
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
112
113
        return label_colors[color_index].label ?
114
                label_colors[color_index].label : gettext(labels[color_index]);
115
}
116
117
const gchar *colorlabel_get_custom_color_text(gint color_index)
118
{
119
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
120
121
        return label_colors[color_index].label;
122
}
123
124
void colorlabel_set_color_text(gint color_index, const gchar *label)
125
{
126
        if (label_colors[color_index].label)
127
                g_free(label_colors[color_index].label);
128
129
        label_colors[color_index].label = g_strdup(label);
130
        label_colors[color_index].changed |= LCCF_LABEL;
131
}
132
133
static gboolean colorlabel_drawing_area_expose_event_cb
134
        (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
135
{
136
        GdkDrawable *drawable = widget->window;
137
        gulong c = (gulong) GPOINTER_TO_INT(data);
138
        GdkColor color;
139
        GdkGC *gc;
140
141
        color.red   = ((c >> 16UL) & 0xFFUL) << 8UL;
142
        color.green = ((c >>  8UL) & 0xFFUL) << 8UL;
143
        color.blue  = ((c)         & 0xFFUL) << 8UL;
144
145
        gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
146
147
        gc = gdk_gc_new(drawable);
148
149
        gdk_gc_set_foreground(gc, &color);
150
        gdk_draw_rectangle(drawable, gc,
151
                           TRUE, 0, 0, widget->allocation.width - 1,
152
                           widget->allocation.height - 1);
153
        gdk_draw_rectangle(drawable, widget->style->black_gc,
154
                           FALSE, 0, 0, widget->allocation.width - 1,
155
                           widget->allocation.height - 1);
156
157
        gdk_gc_unref(gc);                           
158
159
        return FALSE;
160
}
161
162
GtkWidget *colorlabel_create_color_widget(GdkColor color)
163
{
164
        GtkWidget *widget;
165
166
        widget = gtk_drawing_area_new();
167
        gtk_drawing_area_size(GTK_DRAWING_AREA(widget),
168
                              LABEL_COLOR_WIDTH - 2, LABEL_COLOR_HEIGHT - 4);
169
170
#define CL(x)                (((gulong) (x) >> (gulong) 8) & 0xFFUL)        
171
#define CR(r, g, b)        ((CL(r) << (gulong) 16) | \
172
                         (CL(g) << (gulong)  8) | \
173
                         (CL(b)))
174
175
        g_signal_connect(G_OBJECT(widget), "expose_event", 
176
                         G_CALLBACK(colorlabel_drawing_area_expose_event_cb),
177
                         GINT_TO_POINTER
178
                                ((gint)CR(color.red, color.green, color.blue)));
179
180
        return widget;
181
}
182
183
/* XXX: this function to check if menus with colors and labels should
184
 * be recreated */
185
gboolean colorlabel_changed(void)
186
{
187
        gint n;
188
189
        for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
190
                if (label_colors[n].changed) 
191
                        return TRUE;
192
        }
193
194
        return FALSE;
195
}
196
197
/* XXX: colorlabel_recreate_XXX are there to make sure everything
198
 * is initialized ok, without having to call a global _xxx_init_
199
 * function */
200
static void colorlabel_recreate_color(gint color)
201
{
202
        GtkWidget *widget;
203
204
        if (!(label_colors[color].changed & LCCF_COLOR))
205
                return;
206
207
        widget = colorlabel_create_color_widget(label_colors[color].color);
208
        g_return_if_fail(widget);
209
210
        if (label_colors[color].widget) 
211
                gtk_widget_destroy(label_colors[color].widget);
212
213
        label_colors[color].widget = widget;                
214
        label_colors[color].changed &= ~LCCF_COLOR;
215
}
216
217
static void colorlabel_recreate_label(gint color)
218
{
219
        const gchar *text;
220
221
        if (!label_colors[color].changed & LCCF_LABEL)
222
                return;
223
224
        text = colorlabel_get_color_text(color);
225
226
        if (label_colors[color].label_widget)
227
                gtk_label_set_text(GTK_LABEL(label_colors[color].label_widget),
228
                                   text);
229
        else
230
                label_colors[color].label_widget = gtk_label_new(text);
231
232
        label_colors[color].changed &= ~LCCF_LABEL;
233
}
234
235
/* XXX: call this function everytime when you're doing important
236
 * stuff with the label_colors[] array */
237
static void colorlabel_recreate(gint color)
238
{
239
        colorlabel_recreate_label(color);
240
        colorlabel_recreate_color(color);
241
}
242
243
static void colorlabel_recreate_all(void)
244
{
245
        gint n;
246
247
        for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
248
                colorlabel_recreate(n);
249
}
250
251
/* colorlabel_create_check_color_menu_item() - creates a color
252
 * menu item with a check box */
253
GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
254
{
255
        GtkWidget *hbox; 
256
        GtkWidget *vbox; 
257
        GtkWidget *item;
258
259
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
260
261
        item = gtk_check_menu_item_new();
262
263
        colorlabel_recreate(color_index);
264
265
        hbox = gtk_hbox_new(FALSE, 0);
266
        gtk_widget_show(hbox);
267
        gtk_container_add(GTK_CONTAINER(item), hbox);
268
269
        vbox = gtk_vbox_new(TRUE, 0);
270
        gtk_widget_show(vbox);
271
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
272
273
        gtk_container_add(GTK_CONTAINER(vbox),
274
                          label_colors[color_index].widget);
275
        gtk_widget_show(label_colors[color_index].widget);
276
277
        gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
278
        gtk_box_pack_start(GTK_BOX(hbox), label_colors[color_index].label_widget, FALSE, FALSE, 4);
279
        gtk_widget_show(label_colors[color_index].label_widget);
280
281
        return item;
282
}
283
284
/* colorlabel_create_color_menu() - creates a color menu without 
285
 * checkitems, probably for use in combo items */
286
GtkWidget *colorlabel_create_color_menu(void)
287
{
288
        GtkWidget *label; 
289
        GtkWidget *item;
290
        GtkWidget *menu;
291
        gint i;
292
293
        colorlabel_recreate_all();
294
295
        /* create the menu items. each item has its color code attached */
296
        menu = gtk_menu_new();
297
        g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
298
299
#if 0
300
        item = gtk_menu_item_new_with_label(_("None"));
301
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
302
        g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
303
        gtk_widget_show(item);
304
#endif
305
306
        /* and the color items */
307
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
308
                GtkWidget *hbox; 
309
                GtkWidget *vbox;
310
                GtkWidget *widget;
311
312
                item  = gtk_menu_item_new();
313
                g_object_set_data(G_OBJECT(item), "color",
314
                                  GUINT_TO_POINTER(i + 1));
315
316
                label = gtk_label_new(colorlabel_get_color_text(i));
317
318
                gtk_widget_show(label);
319
                hbox = gtk_hbox_new(FALSE, 0);
320
                gtk_widget_show(hbox);
321
                gtk_container_add(GTK_CONTAINER(item), hbox);
322
323
                vbox = gtk_vbox_new(TRUE, 0);
324
                gtk_widget_show(vbox);
325
                gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
326
327
                widget = colorlabel_create_color_widget(label_colors[i].color);
328
                gtk_widget_show(widget);
329
                gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
330
331
                gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
332
                gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
333
                
334
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
335
                gtk_widget_show(item);
336
        }
337
338
        gtk_widget_show(menu);
339
340
        return menu;
341
}
342
343
guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
344
{
345
        GtkWidget *menuitem;
346
        guint color;
347
348
        g_return_val_if_fail
349
                (g_object_get_data(G_OBJECT(menu), "label_color_menu"), 0);
350
        menuitem = gtk_menu_get_active(GTK_MENU(menu));
351
        color = GPOINTER_TO_UINT
352
                (g_object_get_data(G_OBJECT(menuitem), "color"));
353
        return color;
354
}
355
356
void colorlabel_update_menu(void)
357
{
358
        gint i;
359
360
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
361
                if (label_colors[i].widget && label_colors[i].changed) {
362
                        colorlabel_recreate(i);
363
                }
364
        }
365
}
366
367
gint colorlabel_read_config(void)
368
{
369
        gchar *path;
370
        FILE *fp;
371
        gint i;
372
        gchar buf[PREFSBUFSIZE];
373
374
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "colorlabelrc",
375
                           NULL);
376
        if ((fp = g_fopen(path, "rb")) == NULL) {
377
                g_free(path);
378
                return -1;
379
        }
380
381
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
382
                if (fgets(buf, sizeof(buf), fp) == NULL)
383
                        break;
384
                g_strstrip(buf);
385
                if (buf[0] != '\0') {
386
                        colorlabel_set_color_text(i, buf);
387
                }
388
        }
389
390
        fclose(fp);
391
        g_free(path);
392
393
        return 0;
394
}
395
396
gint colorlabel_write_config(void)
397
{
398
        gchar *path;
399
        PrefFile *pfile;
400
        gint i;
401
        gint ret;
402
        const gchar *text;
403
404
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "colorlabelrc",
405
                           NULL);
406
        if ((pfile = prefs_file_open(path)) == NULL) {
407
                g_warning("failed to write colorlabelrc");
408
                g_free(path);
409
                return -1;
410
        }
411
412
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
413
                text = colorlabel_get_custom_color_text(i);
414
                ret = 0;
415
                if (text)
416
                        ret = fputs(text, pfile->fp);
417
418
                if (ret == EOF || fputc('\n', pfile->fp) == EOF) {
419
                        FILE_OP_ERROR(path, "fputs || fputc");
420
                        prefs_file_close_revert(pfile);
421
                        g_free(path);
422
                        return -1;
423
                }
424
        }
425
426
        if (prefs_file_close(pfile) < 0) {
427
                g_warning("failed to write colorlabelrc");
428
                g_free(path);
429
                return -1;
430
        }
431
432
        g_free(path);
433
434
        return 0;
435
}