0.8.10claws64
[claws.git] / src / gtk / colorlabel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2003 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 <gdk/gdkx.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkwidget.h>
31 #include <gtk/gtkpixmap.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkcheckmenuitem.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkalignment.h>
37 #include <gtk/gtkhbox.h>
38 #include <gtk/gtkwindow.h>
39 #include <gtk/gtkdrawingarea.h>
40
41 #include "intl.h"
42 #include "colorlabel.h"
43 #include "utils.h"
44
45 static gchar *labels[] = {
46         N_("Orange"),
47         N_("Red") ,
48         N_("Pink"),
49         N_("Sky blue"),
50         N_("Blue"),
51         N_("Green"),
52         N_("Brown")
53 };
54
55 typedef enum LabelColorChangeFlags_ {
56         LCCF_COLOR = 1 << 0,
57         LCCF_LABEL = 1 << 1,
58         LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
59 } LabelColorChangeFlags;
60
61 /* XXX: if you add colors, make sure you also check the procmsg.h.
62  * color indices are stored as 3 bits; that explains the max. of 7 colors */
63 static struct 
64 {
65         LabelColorChangeFlags   changed; 
66         GdkColor                color;
67
68         /* XXX: note that the label member is supposed to be dynamically 
69          * allocated and fffreed */
70         gchar                   *label;
71         GtkWidget               *widget;
72 } label_colors[] = {
73         { LCCF_ALL, { 0, 0xffff, (0x99 << 8), 0x0 },            NULL, NULL },
74         { LCCF_ALL, { 0, 0xffff, 0, 0 },                        NULL, NULL },
75         { LCCF_ALL, { 0, 0xffff, (0x66 << 8), 0xffff },         NULL, NULL },
76         { LCCF_ALL, { 0, 0x0, (0xcc << 8), 0xffff },            NULL, NULL },
77         { LCCF_ALL, { 0, 0x0, 0x0, 0xffff },                    NULL, NULL },
78         { LCCF_ALL, { 0, 0x0, 0x99 << 8, 0x0 },                 NULL, NULL },
79         { LCCF_ALL, { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 },     NULL, NULL }
80 };
81
82 #define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
83
84 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
85         g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
86
87 static void colorlabel_recreate        (gint);
88 static void colorlabel_recreate_label  (gint);
89
90 gint colorlabel_get_color_count(void)
91 {
92         return LABEL_COLORS_ELEMS;
93 }
94
95 GdkColor colorlabel_get_color(gint color_index)
96 {
97         GdkColor invalid = { 0 };
98
99         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
100
101         return label_colors[color_index].color;
102 }
103
104 gchar *colorlabel_get_color_text(gint color_index)
105 {
106         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
107
108         colorlabel_recreate_label(color_index);
109         return label_colors[color_index].label;
110 }
111
112 static gboolean colorlabel_drawing_area_expose_event_cb
113         (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
114 {
115         GdkDrawable *drawable = widget->window;
116         gulong c = (gulong) GPOINTER_TO_INT(data);
117         GdkColor color;
118         GdkGC *gc;
119
120         color.red   = ((c >> 16UL) & 0xFFUL) << 8UL;
121         color.green = ((c >>  8UL) & 0xFFUL) << 8UL;
122         color.blue  = ((c)         & 0xFFUL) << 8UL;
123
124         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
125
126         gc = gdk_gc_new(drawable);
127
128         gdk_gc_set_foreground(gc, &color);
129         gdk_draw_rectangle(drawable, gc,
130                            TRUE, 0, 0, widget->allocation.width,
131                            widget->allocation.height);
132         gdk_draw_rectangle(drawable, widget->style->black_gc,
133                            FALSE, 0, 0, widget->allocation.width,
134                            widget->allocation.height);
135
136         gdk_gc_unref(gc);                          
137
138         return FALSE;
139 }
140
141 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
142 {
143         GtkWidget *widget;
144
145         widget = gtk_drawing_area_new();
146         gtk_drawing_area_size(GTK_DRAWING_AREA(widget), 16, 16);
147
148 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
149 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
150                          (CL(g) << (gulong)  8) | \
151                          (CL(b)))
152
153         gtk_signal_connect(GTK_OBJECT(widget), "expose_event", 
154                            GTK_SIGNAL_FUNC
155                                 (colorlabel_drawing_area_expose_event_cb),
156                            GINT_TO_POINTER
157                                 ((gint)CR(color.red, color.green, color.blue)));
158
159         return widget;
160 }
161
162 /* XXX: this function to check if menus with colors and labels should
163  * be recreated */
164 gboolean colorlabel_changed(void)
165 {
166         gint n;
167
168         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
169                 if (label_colors[n].changed) 
170                         return TRUE;
171         }
172
173         return FALSE;
174 }
175
176 /* XXX: colorlabel_recreate_XXX are there to make sure everything
177  * is initialized ok, without having to call a global _xxx_init_
178  * function */
179 static void colorlabel_recreate_color(gint color)
180 {
181         GtkWidget *widget;
182
183         if (!(label_colors[color].changed & LCCF_COLOR))
184                 return;
185
186         widget = colorlabel_create_color_widget(label_colors[color].color);
187         g_return_if_fail(widget);
188
189         if (label_colors[color].widget) 
190                 gtk_widget_destroy(label_colors[color].widget);
191
192         label_colors[color].widget = widget;            
193         label_colors[color].changed &= ~LCCF_COLOR;
194 }
195
196 static void colorlabel_recreate_label(gint color)
197 {
198         if (!label_colors[color].changed & LCCF_LABEL)
199                 return;
200
201         if (label_colors[color].label == NULL) 
202                 label_colors[color].label = g_strdup(gettext(labels[color]));
203
204         label_colors[color].changed &= ~LCCF_LABEL;
205 }
206
207 /* XXX: call this function everytime when you're doing important
208  * stuff with the label_colors[] array */
209 static void colorlabel_recreate(gint color)
210 {
211         colorlabel_recreate_label(color);
212         colorlabel_recreate_color(color);
213 }
214
215 static void colorlabel_recreate_all(void)
216 {
217         gint n;
218
219         for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
220                 colorlabel_recreate(n);
221 }
222
223 /* colorlabel_create_check_color_menu_item() - creates a color
224  * menu item with a check box */
225 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
226 {
227         GtkWidget *label; 
228         GtkWidget *hbox; 
229         GtkWidget *align; 
230         GtkWidget *item;
231
232         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
233
234         item  = gtk_check_menu_item_new();
235
236         colorlabel_recreate(color_index);
237
238         /* XXX: gnome-core::panel::menu.c is a great example of
239          * how to create pixmap menus */
240         label = gtk_label_new(label_colors[color_index].label);
241
242         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
243         gtk_widget_show(label);
244         hbox = gtk_hbox_new(FALSE, 0);
245         gtk_widget_show(hbox);
246         gtk_container_add(GTK_CONTAINER(item), hbox);
247
248         align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
249         gtk_widget_show(align);
250         gtk_container_set_border_width(GTK_CONTAINER(align), 1);
251
252         gtk_container_add(GTK_CONTAINER(align), label_colors[color_index].widget);
253         gtk_widget_show(label_colors[color_index].widget);
254         gtk_widget_set_usize(align, 16, 16);
255
256         gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
257         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
258
259         return item;
260 }
261
262 /* colorlabel_create_color_menu() - creates a color menu without 
263  * checkitems, probably for use in combo items */
264 GtkWidget *colorlabel_create_color_menu(void)
265 {
266         GtkWidget *label; 
267         GtkWidget *hbox; 
268         GtkWidget *align; 
269         GtkWidget *item;
270         GtkWidget *menu;
271         gint i;
272
273         colorlabel_recreate_all();
274
275         /* create the menu items. each item has its color code attached */
276         menu = gtk_menu_new();
277         gtk_object_set_data(GTK_OBJECT(menu), "label_color_menu", menu);
278
279         item = gtk_menu_item_new_with_label(_("None"));
280         gtk_menu_append(GTK_MENU(menu), item);
281         gtk_object_set_data(GTK_OBJECT(item), "color", GUINT_TO_POINTER(0));
282         gtk_widget_show(item);
283         
284         /* and the color items */
285         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
286                 GtkWidget *widget = colorlabel_create_color_widget(label_colors[i].color);
287
288                 item  = gtk_menu_item_new();
289                 gtk_object_set_data(GTK_OBJECT(item), "color", GUINT_TO_POINTER(i + 1));
290
291                 label = gtk_label_new(label_colors[i].label);
292                 
293                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
294                 gtk_widget_show(label);
295                 hbox = gtk_hbox_new(FALSE, 0);
296                 gtk_widget_show(hbox);
297                 gtk_container_add(GTK_CONTAINER(item), hbox);
298
299                 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
300                 gtk_widget_show(align);
301                 gtk_container_set_border_width(GTK_CONTAINER(align), 1);
302
303                 gtk_container_add(GTK_CONTAINER(align), widget);
304                 gtk_widget_show(widget);
305                 gtk_widget_set_usize(align, 16, 16);
306
307                 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
308                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
309                 
310                 gtk_menu_append(GTK_MENU(menu), item);
311                 gtk_widget_show(item);
312         }
313
314         gtk_widget_show(menu);
315
316         return menu;
317 }
318
319 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
320 {
321         GtkWidget *menuitem;
322         guint      color;
323
324         g_return_val_if_fail
325                 (gtk_object_get_data(GTK_OBJECT(menu), "label_color_menu"), 0);
326         menuitem = gtk_menu_get_active(GTK_MENU(menu));
327         color = GPOINTER_TO_UINT
328                 (gtk_object_get_data(GTK_OBJECT(menuitem), "color"));
329         return color;
330 }
331
332