Statistics
| Revision:

root / src / colorlabel.c @ 2380

History | View | Annotate | Download (8.9 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 2001-2004 Hiroyuki Yamamoto & The Sylpheed Claws Team
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
/* (alfons) - based on a contribution by Satoshi Nagayasu; revised for colorful
21 1 hiro
 * menu and more Sylpheed integration. The idea to put the code in a separate
22 1 hiro
 * file is just that it make it easier to allow "user changeable" label colors.
23 1 hiro
 */
24 1 hiro
25 1 hiro
#include "defs.h"
26 1 hiro
27 1 hiro
#include <glib.h>
28 92 hiro
#include <glib/gi18n.h>
29 1 hiro
#include <gtk/gtkwidget.h>
30 1 hiro
#include <gtk/gtkpixmap.h>
31 1 hiro
#include <gtk/gtkmenu.h>
32 1 hiro
#include <gtk/gtkcheckmenuitem.h>
33 1 hiro
#include <gtk/gtklabel.h>
34 1 hiro
#include <gtk/gtkmenuitem.h>
35 1 hiro
#include <gtk/gtkalignment.h>
36 1 hiro
#include <gtk/gtkhbox.h>
37 198 hiro
#include <gtk/gtkvbox.h>
38 1 hiro
#include <gtk/gtkwindow.h>
39 1 hiro
#include <gtk/gtkdrawingarea.h>
40 1 hiro
41 1 hiro
#include "colorlabel.h"
42 1 hiro
#include "gtkutils.h"
43 1 hiro
#include "utils.h"
44 1 hiro
45 1 hiro
static gchar *labels[] = {
46 1 hiro
        N_("Orange"),
47 1 hiro
        N_("Red") ,
48 1 hiro
        N_("Pink"),
49 1 hiro
        N_("Sky blue"),
50 1 hiro
        N_("Blue"),
51 1 hiro
        N_("Green"),
52 1 hiro
        N_("Brown")
53 1 hiro
};
54 1 hiro
55 1 hiro
typedef enum LabelColorChangeFlags_ {
56 1 hiro
        LCCF_COLOR = 1 << 0,
57 1 hiro
        LCCF_LABEL = 1 << 1,
58 1 hiro
        LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
59 1 hiro
} LabelColorChangeFlags;
60 1 hiro
61 1 hiro
/* XXX: if you add colors, make sure you also check the procmsg.h.
62 1 hiro
 * color indices are stored as 3 bits; that explains the max. of 7 colors */
63 1 hiro
static struct
64 1 hiro
{
65 1 hiro
        LabelColorChangeFlags        changed;
66 1 hiro
        GdkColor                color;
67 1 hiro
68 1 hiro
        /* XXX: note that the label member is supposed to be dynamically
69 1 hiro
         * allocated and fffreed */
70 1 hiro
        gchar                        *label;
71 1 hiro
        GtkWidget                *widget;
72 1 hiro
} label_colors[] = {
73 1 hiro
        { LCCF_ALL, { 0, 0xffff, (0x99 << 8), 0x0 },                NULL, NULL },
74 1 hiro
        { LCCF_ALL, { 0, 0xffff, 0, 0 },                        NULL, NULL },
75 1 hiro
        { LCCF_ALL, { 0, 0xffff, (0x66 << 8), 0xffff },                NULL, NULL },
76 1 hiro
        { LCCF_ALL, { 0, 0x0, (0xcc << 8), 0xffff },                NULL, NULL },
77 1 hiro
        { LCCF_ALL, { 0, 0x0, 0x0, 0xffff },                        NULL, NULL },
78 1 hiro
        { LCCF_ALL, { 0, 0x0, 0x99 << 8, 0x0 },                        NULL, NULL },
79 1 hiro
        { LCCF_ALL, { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 },        NULL, NULL }
80 1 hiro
};
81 1 hiro
82 1 hiro
#define LABEL_COLOR_WIDTH        28
83 1 hiro
#define LABEL_COLOR_HEIGHT        16
84 1 hiro
85 1 hiro
#define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
86 1 hiro
87 1 hiro
#define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
88 1 hiro
        g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
89 1 hiro
90 1 hiro
static void colorlabel_recreate        (gint);
91 1 hiro
static void colorlabel_recreate_label  (gint);
92 1 hiro
93 1 hiro
gint colorlabel_get_color_count(void)
94 1 hiro
{
95 1 hiro
        return LABEL_COLORS_ELEMS;
96 1 hiro
}
97 1 hiro
98 1 hiro
GdkColor colorlabel_get_color(gint color_index)
99 1 hiro
{
100 1 hiro
        GdkColor invalid = { 0 };
101 1 hiro
102 1 hiro
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
103 1 hiro
104 1 hiro
        return label_colors[color_index].color;
105 1 hiro
}
106 1 hiro
107 1 hiro
gchar *colorlabel_get_color_text(gint color_index)
108 1 hiro
{
109 1 hiro
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
110 1 hiro
111 1 hiro
        colorlabel_recreate_label(color_index);
112 1 hiro
        return label_colors[color_index].label;
113 1 hiro
}
114 1 hiro
115 1 hiro
static gboolean colorlabel_drawing_area_expose_event_cb
116 1 hiro
        (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
117 1 hiro
{
118 1 hiro
        GdkDrawable *drawable = widget->window;
119 1 hiro
        gulong c = (gulong) GPOINTER_TO_INT(data);
120 1 hiro
        GdkColor color;
121 1 hiro
        GdkGC *gc;
122 1 hiro
123 1 hiro
        color.red   = ((c >> 16UL) & 0xFFUL) << 8UL;
124 1 hiro
        color.green = ((c >>  8UL) & 0xFFUL) << 8UL;
125 1 hiro
        color.blue  = ((c)         & 0xFFUL) << 8UL;
126 1 hiro
127 1 hiro
        gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
128 1 hiro
129 1 hiro
        gc = gdk_gc_new(drawable);
130 1 hiro
131 1 hiro
        gdk_gc_set_foreground(gc, &color);
132 1 hiro
        gdk_draw_rectangle(drawable, gc,
133 1 hiro
                           TRUE, 0, 0, widget->allocation.width - 1,
134 1 hiro
                           widget->allocation.height - 1);
135 1 hiro
        gdk_draw_rectangle(drawable, widget->style->black_gc,
136 1 hiro
                           FALSE, 0, 0, widget->allocation.width - 1,
137 1 hiro
                           widget->allocation.height - 1);
138 1 hiro
139 1 hiro
        gdk_gc_unref(gc);
140 1 hiro
141 1 hiro
        return FALSE;
142 1 hiro
}
143 1 hiro
144 1 hiro
static GtkWidget *colorlabel_create_color_widget(GdkColor color)
145 1 hiro
{
146 1 hiro
        GtkWidget *widget;
147 1 hiro
148 1 hiro
        widget = gtk_drawing_area_new();
149 1 hiro
        gtk_drawing_area_size(GTK_DRAWING_AREA(widget),
150 1 hiro
                              LABEL_COLOR_WIDTH - 2, LABEL_COLOR_HEIGHT - 4);
151 1 hiro
152 1 hiro
#define CL(x)                (((gulong) (x) >> (gulong) 8) & 0xFFUL)
153 1 hiro
#define CR(r, g, b)        ((CL(r) << (gulong) 16) | \
154 1 hiro
                         (CL(g) << (gulong)  8) | \
155 1 hiro
                         (CL(b)))
156 1 hiro
157 1 hiro
        g_signal_connect(G_OBJECT(widget), "expose_event",
158 1 hiro
                         G_CALLBACK(colorlabel_drawing_area_expose_event_cb),
159 1 hiro
                         GINT_TO_POINTER
160 1 hiro
                                ((gint)CR(color.red, color.green, color.blue)));
161 1 hiro
162 1 hiro
        return widget;
163 1 hiro
}
164 1 hiro
165 1 hiro
/* XXX: this function to check if menus with colors and labels should
166 1 hiro
 * be recreated */
167 1 hiro
gboolean colorlabel_changed(void)
168 1 hiro
{
169 1 hiro
        gint n;
170 1 hiro
171 1 hiro
        for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
172 1 hiro
                if (label_colors[n].changed)
173 1 hiro
                        return TRUE;
174 1 hiro
        }
175 1 hiro
176 1 hiro
        return FALSE;
177 1 hiro
}
178 1 hiro
179 1 hiro
/* XXX: colorlabel_recreate_XXX are there to make sure everything
180 1 hiro
 * is initialized ok, without having to call a global _xxx_init_
181 1 hiro
 * function */
182 1 hiro
static void colorlabel_recreate_color(gint color)
183 1 hiro
{
184 1 hiro
        GtkWidget *widget;
185 1 hiro
186 1 hiro
        if (!(label_colors[color].changed & LCCF_COLOR))
187 1 hiro
                return;
188 1 hiro
189 1 hiro
        widget = colorlabel_create_color_widget(label_colors[color].color);
190 1 hiro
        g_return_if_fail(widget);
191 1 hiro
192 1 hiro
        if (label_colors[color].widget)
193 1 hiro
                gtk_widget_destroy(label_colors[color].widget);
194 1 hiro
195 1 hiro
        label_colors[color].widget = widget;
196 1 hiro
        label_colors[color].changed &= ~LCCF_COLOR;
197 1 hiro
}
198 1 hiro
199 1 hiro
static void colorlabel_recreate_label(gint color)
200 1 hiro
{
201 1 hiro
        if (!label_colors[color].changed & LCCF_LABEL)
202 1 hiro
                return;
203 1 hiro
204 1 hiro
        if (label_colors[color].label == NULL)
205 1 hiro
                label_colors[color].label = g_strdup(gettext(labels[color]));
206 1 hiro
207 1 hiro
        label_colors[color].changed &= ~LCCF_LABEL;
208 1 hiro
}
209 1 hiro
210 1 hiro
/* XXX: call this function everytime when you're doing important
211 1 hiro
 * stuff with the label_colors[] array */
212 1 hiro
static void colorlabel_recreate(gint color)
213 1 hiro
{
214 1 hiro
        colorlabel_recreate_label(color);
215 1 hiro
        colorlabel_recreate_color(color);
216 1 hiro
}
217 1 hiro
218 1 hiro
static void colorlabel_recreate_all(void)
219 1 hiro
{
220 1 hiro
        gint n;
221 1 hiro
222 1 hiro
        for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
223 1 hiro
                colorlabel_recreate(n);
224 1 hiro
}
225 1 hiro
226 1 hiro
/* colorlabel_create_check_color_menu_item() - creates a color
227 1 hiro
 * menu item with a check box */
228 1 hiro
GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
229 1 hiro
{
230 1 hiro
        GtkWidget *label;
231 1 hiro
        GtkWidget *hbox;
232 198 hiro
        GtkWidget *vbox;
233 1 hiro
        GtkWidget *item;
234 1 hiro
235 1 hiro
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
236 1 hiro
237 1 hiro
        item = gtk_check_menu_item_new();
238 1 hiro
239 1 hiro
        colorlabel_recreate(color_index);
240 1 hiro
241 1 hiro
        /* XXX: gnome-core::panel::menu.c is a great example of
242 1 hiro
         * how to create pixmap menus */
243 1 hiro
        label = gtk_label_new(label_colors[color_index].label);
244 1 hiro
245 1 hiro
        gtk_widget_show(label);
246 1 hiro
        hbox = gtk_hbox_new(FALSE, 0);
247 1 hiro
        gtk_widget_show(hbox);
248 1 hiro
        gtk_container_add(GTK_CONTAINER(item), hbox);
249 1 hiro
250 198 hiro
        vbox = gtk_vbox_new(TRUE, 0);
251 198 hiro
        gtk_widget_show(vbox);
252 198 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
253 1 hiro
254 198 hiro
        gtk_container_add(GTK_CONTAINER(vbox),
255 198 hiro
                          label_colors[color_index].widget);
256 1 hiro
        gtk_widget_show(label_colors[color_index].widget);
257 1 hiro
258 198 hiro
        gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
259 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
260 1 hiro
261 1 hiro
        return item;
262 1 hiro
}
263 1 hiro
264 1 hiro
/* colorlabel_create_color_menu() - creates a color menu without
265 1 hiro
 * checkitems, probably for use in combo items */
266 1 hiro
GtkWidget *colorlabel_create_color_menu(void)
267 1 hiro
{
268 1 hiro
        GtkWidget *label;
269 1 hiro
        GtkWidget *item;
270 1 hiro
        GtkWidget *menu;
271 1 hiro
        gint i;
272 1 hiro
273 1 hiro
        colorlabel_recreate_all();
274 1 hiro
275 1 hiro
        /* create the menu items. each item has its color code attached */
276 1 hiro
        menu = gtk_menu_new();
277 1 hiro
        g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
278 1 hiro
279 1 hiro
#if 0
280 1 hiro
        item = gtk_menu_item_new_with_label(_("None"));
281 1 hiro
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
282 1 hiro
        g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
283 1 hiro
        gtk_widget_show(item);
284 1 hiro
#endif
285 1 hiro
286 1 hiro
        /* and the color items */
287 1 hiro
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
288 198 hiro
                GtkWidget *hbox;
289 198 hiro
                GtkWidget *vbox;
290 198 hiro
                GtkWidget *widget;
291 1 hiro
292 1 hiro
                item  = gtk_menu_item_new();
293 1 hiro
                g_object_set_data(G_OBJECT(item), "color",
294 1 hiro
                                  GUINT_TO_POINTER(i + 1));
295 1 hiro
296 1 hiro
                label = gtk_label_new(label_colors[i].label);
297 1 hiro
298 1 hiro
                gtk_widget_show(label);
299 1 hiro
                hbox = gtk_hbox_new(FALSE, 0);
300 1 hiro
                gtk_widget_show(hbox);
301 1 hiro
                gtk_container_add(GTK_CONTAINER(item), hbox);
302 1 hiro
303 198 hiro
                vbox = gtk_vbox_new(TRUE, 0);
304 198 hiro
                gtk_widget_show(vbox);
305 198 hiro
                gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
306 1 hiro
307 198 hiro
                widget = colorlabel_create_color_widget(label_colors[i].color);
308 1 hiro
                gtk_widget_show(widget);
309 198 hiro
                gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
310 1 hiro
311 198 hiro
                gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
312 1 hiro
                gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
313 1 hiro
314 1 hiro
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
315 1 hiro
                gtk_widget_show(item);
316 1 hiro
        }
317 1 hiro
318 1 hiro
        gtk_widget_show(menu);
319 1 hiro
320 1 hiro
        return menu;
321 1 hiro
}
322 1 hiro
323 1 hiro
guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
324 1 hiro
{
325 1 hiro
        GtkWidget *menuitem;
326 1 hiro
        guint color;
327 1 hiro
328 1 hiro
        g_return_val_if_fail
329 1 hiro
                (g_object_get_data(G_OBJECT(menu), "label_color_menu"), 0);
330 1 hiro
        menuitem = gtk_menu_get_active(GTK_MENU(menu));
331 1 hiro
        color = GPOINTER_TO_UINT
332 1 hiro
                (g_object_get_data(G_OBJECT(menuitem), "color"));
333 1 hiro
        return color;
334 1 hiro
}