Refactor colors handling with more arrays
[claws.git] / src / prefs_msg_colors.c
1 /*
2  * Claws-Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2017 Hiroyuki Yamamoto & The Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33
34 #include "prefs_common.h"
35 #include "prefs_gtk.h"
36
37 #include "gtk/gtkutils.h"
38 #include "gtk/prefswindow.h"
39
40 #include "manage_window.h"
41 #include "mainwindow.h"
42 #include "colorlabel.h"
43
44 #define SAFE_STRING(str) \
45         (str) ? (str) : ""
46
47 static struct MessageColorButtons {
48         /* program colors */
49         GtkWidget *color[COL_LAST_COLOR_INDEX];
50         /* custom colors */
51         GtkWidget *custom_color[COLORLABELS];
52 } color_buttons;
53
54 typedef struct _MsgColorsPage
55 {
56         PrefsPage page;
57
58         GtkWidget *window;
59         
60         GtkWidget *checkbtn_enable_colors;
61         GtkWidget *checkbtn_enable_bgcolors;
62         GtkWidget *checkbtn_recycle_colors;
63
64         /* custom colors */
65         GtkWidget *entry_custom_colorlabel[COLORLABELS];
66 } MsgColorsPage;
67
68 static GtkWidget *color_dialog;
69 static const gchar *color_dialog_title[COL_LAST_COLOR_INDEX];
70 static gboolean color_dialog_title_init = FALSE;
71
72 static void quote_color_set_dialog              (GtkWidget      *widget,
73                                                  gpointer        data);
74 static void quote_colors_set_dialog_ok          (GtkWidget      *widget,
75                                                  gpointer        data);
76 static void quote_colors_set_dialog_cancel      (GtkWidget      *widget,
77                                                  gpointer        data);
78 static gboolean quote_colors_set_dialog_key_pressed     (GtkWidget      *widget,
79                                                  GdkEventKey    *event,
80                                                  gpointer        data);
81 static void set_button_bg_color                 (GtkWidget      *widget,
82                                                  gint            color);
83 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget,
84                                                  gpointer        data);
85
86 #define COLOR_BUTTON_PACK_START(gtkbox, colorid, tooltiptext) \
87         color_buttons.color[colorid] = gtk_button_new_with_label ("\x20\xE2\x80\x83\x20"); \
88         gtk_widget_show (color_buttons.color[colorid]); \
89         gtk_box_pack_start (GTK_BOX(gtkbox), color_buttons.color[colorid], FALSE, FALSE, 0); \
90         gtk_widget_set_tooltip_text (GTK_WIDGET(color_buttons.color[colorid]), tooltiptext)
91
92 #define COLOR_BUTTON_PACK_END(gtkbox, colorid, tooltiptext) \
93         color_buttons.color[colorid] = gtk_button_new_with_label ("\x20\xE2\x80\x83\x20"); \
94         gtk_widget_show (color_buttons.color[colorid]); \
95         gtk_box_pack_end (GTK_BOX(gtkbox), color_buttons.color[colorid], FALSE, FALSE, 0); \
96         gtk_widget_set_tooltip_text (GTK_WIDGET(color_buttons.color[colorid]), tooltiptext)
97
98 #define COLOR_LABEL_PACK_START(gtkbox, colorid, labeltext) \
99         label[colorid] = gtk_label_new (labeltext); \
100         gtk_widget_show (label[colorid]); \
101         gtk_box_pack_start (GTK_BOX(gtkbox), label[colorid], FALSE, FALSE, 0)
102
103 #define COLOR_LABEL_PACK_END(gtkbox, colorid, labeltext) \
104         label[colorid] = gtk_label_new (labeltext); \
105         gtk_widget_show (label[colorid]); \
106         gtk_box_pack_end (GTK_BOX(gtkbox), label[colorid], FALSE, FALSE, 0)
107
108 static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window, 
109                                     gpointer data)
110 {
111         MsgColorsPage *prefs_msg_colors = (MsgColorsPage *) _page;
112         
113         GtkWidget *notebook;
114         GtkWidget *vbox1;
115         GtkWidget *vbox2;
116         GtkWidget *checkbtn_enable_colors;
117         GtkWidget *label_quote_level1;
118         GtkWidget *label_quote_level2;
119         GtkWidget *label_quote_level3;
120         GtkWidget *checkbtn_enable_bgcolors;
121         GtkWidget *checkbtn_recycle_colors;
122         GtkWidget *hbox;
123         GtkWidget *label[COL_LAST_COLOR_INDEX];
124         GtkWidget *frame_msg;
125         GtkWidget *frame_folder;
126         GtkWidget *frame_quote;
127         GtkWidget *vbox3;
128         GtkWidget *hbox_quote;
129         GtkWidget *vbox_quotefg;
130         GtkWidget *vbox_quotebg;
131         /* custom colors */
132         GtkWidget *hbox_custom_colors;
133         GtkWidget *vbox_custom_colors;
134         GtkWidget *vbox_custom_colors1;
135         GtkWidget *vbox_custom_colors2;
136         GtkWidget *hbox_reset_custom_colors;
137         GtkWidget *btn_reset_custom_colors;
138         GtkWidget *hbox_custom_color[COLORLABELS];
139         GtkWidget *entry_custom_colorlabel[COLORLABELS];
140         gint c;
141         gchar *tooltip_btn_text = NULL;
142         gchar *tooltip_entry_text = NULL;
143
144         notebook = gtk_notebook_new();
145         gtk_widget_show(notebook);
146         
147         vbox1 = gtk_vbox_new (FALSE, VBOX_BORDER);
148         gtk_widget_show (vbox1);
149         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
150         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox1,
151                                  gtk_label_new(_("Other")));
152
153         vbox2 = gtkut_get_options_frame(vbox1, &frame_msg, _("Message view"));
154
155         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
156         gtk_widget_show (hbox);
157
158         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
159         PACK_CHECK_BUTTON (hbox, checkbtn_enable_colors,
160                            _("Enable coloration of message text"));     
161
162         hbox_quote = gtk_hbox_new(FALSE, VBOX_BORDER);
163         gtk_widget_show (hbox_quote);
164         vbox_quotefg = gtk_vbox_new(FALSE, VBOX_BORDER);
165         gtk_widget_show (vbox_quotefg);
166         vbox_quotebg = gtk_vbox_new(FALSE, VBOX_BORDER);
167         gtk_widget_show (vbox_quotebg);
168         vbox3 = gtkut_get_options_frame(vbox2, &frame_quote, _("Quote"));
169         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, frame_quote);
170
171         gtk_box_pack_start (GTK_BOX (vbox3), hbox_quote, FALSE, TRUE, 0);
172         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotefg, FALSE, TRUE, 0);
173         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotebg, FALSE, TRUE, 0);
174
175         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
176         gtk_widget_show (hbox);
177         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
178
179         PACK_CHECK_BUTTON (hbox, checkbtn_recycle_colors,
180                            _("Cycle quote colors"));
181         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, checkbtn_recycle_colors);
182
183         CLAWS_SET_TIP(checkbtn_recycle_colors,
184                              _("If there are more than 3 quote levels, the colors will be reused"));
185
186         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
187         gtk_widget_show (hbox);
188         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
189
190         label_quote_level1 = gtk_label_new (_("1st Level"));
191         gtk_widget_show(label_quote_level1);
192         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level1, 
193                             FALSE, FALSE, 0);
194         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label_quote_level1);
195
196         COLOR_LABEL_PACK_END(hbox, COL_QUOTE_LEVEL1, _("Text"));
197         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label[COL_QUOTE_LEVEL1]);
198
199         COLOR_BUTTON_PACK_END(hbox, COL_QUOTE_LEVEL1,
200                               C_("Tooltip", "Pick color for 1st level text"));
201         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors,
202                                color_buttons.color[COL_QUOTE_LEVEL1]);
203
204         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
205         gtk_widget_show (hbox);
206         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
207
208         label_quote_level2 = gtk_label_new (_("2nd Level"));
209         gtk_widget_show(label_quote_level2);
210         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level2, 
211                             FALSE, FALSE, 0);
212         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label_quote_level2);
213
214         COLOR_LABEL_PACK_END(hbox, COL_QUOTE_LEVEL2, _("Text"));
215         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label[COL_QUOTE_LEVEL2]);
216
217         COLOR_BUTTON_PACK_END(hbox, COL_QUOTE_LEVEL2,
218                               C_("Tooltip", "Pick color for 2nd level text"));
219         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors,
220                                color_buttons.color[COL_QUOTE_LEVEL2]);
221
222         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
223         gtk_widget_show (hbox);
224         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
225
226         label_quote_level3 = gtk_label_new (_("3rd Level"));
227         gtk_widget_show(label_quote_level3);
228         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level3, 
229                             FALSE, FALSE, 0);
230         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label_quote_level3);
231
232         COLOR_LABEL_PACK_END(hbox, COL_QUOTE_LEVEL3, _("Text"));
233         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label[COL_QUOTE_LEVEL3]);
234
235         COLOR_BUTTON_PACK_END(hbox, COL_QUOTE_LEVEL3,
236                               C_("Tooltip", "Pick color for 3rd level text"));
237         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors,
238                                color_buttons.color[COL_QUOTE_LEVEL3]);
239
240         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
241         gtk_widget_show (hbox);
242         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
243
244         PACK_CHECK_BUTTON (hbox, checkbtn_enable_bgcolors,
245                            _("Enable coloration of text background"));
246         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, checkbtn_enable_bgcolors);
247
248         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
249         gtk_widget_show (hbox);
250         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, hbox);
251         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
252
253         COLOR_BUTTON_PACK_START(hbox, COL_QUOTE_LEVEL1_BG,
254                                 C_("Tooltip", "Pick color for 1st level text background"));
255         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors,
256                                color_buttons.color[COL_QUOTE_LEVEL1_BG]);
257
258         COLOR_LABEL_PACK_START(hbox, COL_QUOTE_LEVEL1_BG, _("Background"));
259         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, label[COL_QUOTE_LEVEL1_BG]);
260
261         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
262         gtk_widget_show (hbox);
263         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, hbox);
264         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
265
266         COLOR_BUTTON_PACK_START(hbox, COL_QUOTE_LEVEL2_BG,
267                                 C_("Tooltip", "Pick color for 2nd level text background"));
268         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors,
269                                color_buttons.color[COL_QUOTE_LEVEL2_BG]);
270
271         COLOR_LABEL_PACK_START(hbox, COL_QUOTE_LEVEL2_BG, _("Background"));
272         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, label[COL_QUOTE_LEVEL2_BG]);
273
274         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
275         gtk_widget_show (hbox);
276         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, hbox);
277         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
278
279         COLOR_BUTTON_PACK_START(hbox, COL_QUOTE_LEVEL3_BG,
280                                 C_("Tooltip", "Pick color for 3rd level text background"));
281         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors,
282                                color_buttons.color[COL_QUOTE_LEVEL3_BG]);
283
284         COLOR_LABEL_PACK_START(hbox, COL_QUOTE_LEVEL3_BG, _("Background"));
285         SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, label[COL_QUOTE_LEVEL3_BG]);
286
287         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
288         gtk_widget_show (hbox);
289         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
290
291         COLOR_BUTTON_PACK_START(hbox, COL_URI,
292                                 C_("Tooltip", "Pick color for links"));
293         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors,
294                                color_buttons.color[COL_URI]);
295
296         COLOR_LABEL_PACK_START(hbox, COL_URI, _("URI link"));
297         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label[COL_URI]);
298
299         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
300         gtk_widget_show (hbox);
301         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
302
303         COLOR_BUTTON_PACK_START(hbox, COL_SIGNATURE,
304                                 C_("Tooltip", "Pick color for signatures"));
305         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors,
306                                color_buttons.color[COL_SIGNATURE]);
307
308         COLOR_LABEL_PACK_START(hbox, COL_SIGNATURE, _("Signatures"));
309         SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, label[COL_SIGNATURE]);
310
311         vbox2 = gtkut_get_options_frame(vbox1, &frame_folder, _("Folder list"));
312
313         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
314         gtk_widget_show (hbox);
315         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
316
317         COLOR_BUTTON_PACK_START(hbox, COL_TGT_FOLDER,
318                                 C_("Tooltip", "Pick color for Target folder. "
319                                    "Target folder is used when the option "
320                                    "'Execute immediately when moving or "
321                                    "deleting messages' is turned off"));
322
323         COLOR_LABEL_PACK_START(hbox, COL_TGT_FOLDER, _("Target folder"));
324
325         hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
326         gtk_widget_show (hbox);
327         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
328
329         COLOR_BUTTON_PACK_START(hbox, COL_NEW,
330                                 C_("Tooltip", "Pick color for folders "
331                                    "containing new messages"));
332
333         COLOR_LABEL_PACK_START(hbox, COL_NEW,
334                                _("Folder containing new messages"));
335
336         /* custom colors */
337         vbox_custom_colors = gtk_vbox_new (FALSE, VSPACING_NARROW);
338         gtk_widget_show (vbox_custom_colors);
339         gtk_container_set_border_width (GTK_CONTAINER (vbox_custom_colors), VBOX_BORDER);
340         gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook), vbox_custom_colors,
341                                  gtk_label_new(_("Color labels")));
342
343         hbox_custom_colors = gtk_hbox_new(FALSE, 8);
344         gtk_widget_show(hbox_custom_colors);
345         gtk_box_pack_start(GTK_BOX (vbox_custom_colors), hbox_custom_colors,
346                                    FALSE, TRUE, 0);
347
348         vbox_custom_colors1 = gtk_vbox_new (FALSE, VSPACING_NARROW);
349         gtk_widget_show (vbox_custom_colors1);
350         gtk_box_pack_start (GTK_BOX (hbox_custom_colors), vbox_custom_colors1, FALSE, FALSE, 0);
351
352         vbox_custom_colors2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
353         gtk_widget_show (vbox_custom_colors2);
354         gtk_box_pack_start (GTK_BOX (hbox_custom_colors), vbox_custom_colors2, FALSE, FALSE, 0);
355
356         for (c = 0; c < (COLORLABELS>>1)+(COLORLABELS&1); c++) {
357                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
358                    rule name and should not be translated */
359                 tooltip_btn_text = g_strdup_printf(C_("Tooltip", "Pick color for 'color %d'"), c+1);
360
361                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
362                    rule name and should not be translated */
363                 tooltip_entry_text = g_strdup_printf(_("Set label for 'color %d'"), c+1);
364
365                 hbox_custom_color[c] = gtk_hbox_new(FALSE, 8);
366                 gtk_widget_show(hbox_custom_color[c]);
367                 gtk_box_pack_start(GTK_BOX (vbox_custom_colors1), hbox_custom_color[c],
368                                    FALSE, TRUE, 0);
369
370                 color_buttons.custom_color[c] = GTKUT_COLOR_BUTTON();
371                 gtk_widget_show(color_buttons.custom_color[c]);
372                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), color_buttons.custom_color[c],
373                                    FALSE, FALSE, 0);
374
375                 CLAWS_SET_TIP(color_buttons.custom_color[c],
376                                      tooltip_btn_text);
377                 g_free(tooltip_btn_text);
378
379                 entry_custom_colorlabel[c] = gtk_entry_new();
380                 gtk_widget_show (entry_custom_colorlabel[c]);
381                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), entry_custom_colorlabel[c],
382                                    FALSE, FALSE, 0);
383                 CLAWS_SET_TIP(entry_custom_colorlabel[c],
384                                      tooltip_entry_text);
385                 g_free(tooltip_entry_text);
386         }
387
388         for (c = (COLORLABELS>>1)+(COLORLABELS&1); c < COLORLABELS; c++) {
389                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
390                    rule name and should not be translated */
391                 tooltip_btn_text = g_strdup_printf(C_("Tooltip", "Pick color for 'color %d'"), c+1);
392
393                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
394                    rule name and should not be translated */
395                 tooltip_entry_text = g_strdup_printf(_("Set label for 'color %d'"), c+1);
396
397                 hbox_custom_color[c] = gtk_hbox_new(FALSE, 8);
398                 gtk_widget_show(hbox_custom_color[c]);
399                 gtk_box_pack_start(GTK_BOX (vbox_custom_colors2), hbox_custom_color[c],
400                                    FALSE, TRUE, 0);
401
402                 color_buttons.custom_color[c] = GTKUT_COLOR_BUTTON();
403                 gtk_widget_show(color_buttons.custom_color[c]);
404                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), color_buttons.custom_color[c],
405                                    FALSE, FALSE, 0);
406                 CLAWS_SET_TIP(color_buttons.custom_color[c],
407                                      tooltip_btn_text);
408                 g_free(tooltip_btn_text);
409
410                 entry_custom_colorlabel[c] = gtk_entry_new();
411                 gtk_widget_show (entry_custom_colorlabel[c]);
412                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), entry_custom_colorlabel[c],
413                                    FALSE, FALSE, 0);
414                 CLAWS_SET_TIP(entry_custom_colorlabel[c],
415                                      tooltip_entry_text);
416
417                 g_free(tooltip_entry_text);
418         }
419
420         hbox_reset_custom_colors = gtk_hbox_new(FALSE, VBOX_BORDER);
421         gtk_widget_show (hbox_reset_custom_colors);
422         gtk_box_pack_start(GTK_BOX (vbox_custom_colors), hbox_reset_custom_colors,
423                            FALSE, FALSE, 0);
424
425         btn_reset_custom_colors = gtk_button_new_with_label(_(" Use default "));
426         gtk_widget_show(btn_reset_custom_colors);
427         gtk_box_pack_start(GTK_BOX(hbox_reset_custom_colors), btn_reset_custom_colors,
428                 FALSE, FALSE, 0);
429
430         /* program colors */
431         for (c = 0; c < COL_LAST_COLOR_INDEX; c++) {
432                 if (color_buttons.color[c] != NULL) {
433                         g_signal_connect(G_OBJECT(color_buttons.color[c]),
434                                          "clicked",
435                                          G_CALLBACK(quote_color_set_dialog),
436                                          GINT_TO_POINTER(COLORLABELS + c));
437                 }
438         }
439         /* custom colors */
440         for (c = 0; c < COLORLABELS; c++) {
441                 g_signal_connect(G_OBJECT(color_buttons.custom_color[c]), "clicked",
442                                  G_CALLBACK(quote_color_set_dialog), GINT_TO_POINTER(c));
443         }
444
445         g_signal_connect(G_OBJECT(btn_reset_custom_colors), "clicked",
446                          G_CALLBACK(prefs_msg_colors_reset_custom_colors), prefs_msg_colors);
447
448         /* program colors */
449         for (c = 0; c < COL_LAST_COLOR_INDEX; c++) {
450                 if (color_buttons.color[c] != NULL) {
451                         set_button_bg_color(color_buttons.color[c],
452                                             prefs_common.color[c]);
453                 }
454         }
455         /* custom colors */
456         for (c = 0; c < COLORLABELS; c++) {
457                 set_button_bg_color(color_buttons.custom_color[c],
458                                     prefs_common.custom_colorlabel[c].color);
459                 gtk_entry_set_text(GTK_ENTRY (entry_custom_colorlabel[c]), 
460                                    gettext(SAFE_STRING (prefs_common.custom_colorlabel[c].label)));
461         }
462
463         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_colors),
464                                      prefs_common.enable_color);
465         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_bgcolors),
466                                      prefs_common.enable_bgcolor);
467         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_recycle_colors),
468                                      prefs_common.recycle_quote_colors);
469
470         prefs_msg_colors->checkbtn_enable_colors        = checkbtn_enable_colors;
471         prefs_msg_colors->checkbtn_enable_bgcolors      = checkbtn_enable_bgcolors;
472         prefs_msg_colors->checkbtn_recycle_colors       = checkbtn_recycle_colors;
473         /* custom colors */
474         for (c = 0; c < COLORLABELS; c++) {
475                 prefs_msg_colors->entry_custom_colorlabel[c] = entry_custom_colorlabel[c];
476         }
477         gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
478                 
479         prefs_msg_colors->page.widget = notebook;
480 }
481
482 #undef COLOR_BUTTON_PACK_START
483 #undef COLOR_BUTTON_PACK_END
484 #undef COLOR_LABEL_PACK_START
485 #undef COLOR_LABEL_PACK_END
486
487 static void initialize_color_dialog_title()
488 {
489         color_dialog_title[COL_QUOTE_LEVEL1] =
490                 C_("Dialog title", "Pick color for 1st level text");
491         color_dialog_title[COL_QUOTE_LEVEL2] =
492                 C_("Dialog title", "Pick color for 2nd level text");
493         color_dialog_title[COL_QUOTE_LEVEL3] =
494                 C_("Dialog title", "Pick color for 3rd level text");
495         color_dialog_title[COL_QUOTE_LEVEL1_BG] =
496                 C_("Dialog title", "Pick color for 1st level text background");
497         color_dialog_title[COL_QUOTE_LEVEL2_BG] =
498                 C_("Dialog title", "Pick color for 2nd level text background");
499         color_dialog_title[COL_QUOTE_LEVEL3_BG] =
500                 C_("Dialog title", "Pick color for 3rd level text background");
501         color_dialog_title[COL_URI] =
502                 C_("Dialog title", "Pick color for links");
503         color_dialog_title[COL_TGT_FOLDER] =
504                 C_("Dialog title", "Pick color for target folder");
505         color_dialog_title[COL_SIGNATURE] =
506                 C_("Dialog title", "Pick color for signatures");
507         color_dialog_title[COL_NEW] =
508                 C_("Dialog title", "Pick color for folder");
509
510         color_dialog_title_init = TRUE;
511 }
512
513 static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
514 {
515         gchar *type = (gchar *)data;
516         gchar *title = NULL;
517         GdkColor color;
518         gint rgbvalue = 0;
519         GtkColorSelectionDialog *dialog;
520         GtkWidget *button_ok;
521         GtkWidget *button_cancel;
522         gint c, ctype;
523
524         ctype = GPOINTER_TO_INT(type);
525         /* custom colors */
526         /* leave the extra space at the end of the title, this is for translators' convenience */
527         for (c = 0; c < COLORLABELS; c++) {
528                 if (ctype == c) {
529                         /* TRANSLATORS: 'color %d' refers to the filtering/processing 
530                            rule name and should not be translated */
531                         title = g_strdup_printf(C_("Dialog title", "Pick color for 'color %d'"), c+1);
532                         rgbvalue = prefs_common.custom_colorlabel[c].color;
533                         break;
534                 }
535         }
536         /* other colors */
537         if (!color_dialog_title_init)
538                 initialize_color_dialog_title();
539
540         if (c == COLORLABELS) {
541                 ctype -= COLORLABELS;
542                 if (ctype < COL_LAST_COLOR_INDEX) {
543                         title = g_strdup(color_dialog_title[ctype]);
544                         rgbvalue = prefs_common.color[ctype];
545                 } else {
546                         g_warning("Unrecognized datatype '%s' in quote_color_set_dialog", type);
547                         return;
548                 }
549         }
550
551         color_dialog = gtk_color_selection_dialog_new(title);
552         g_free(title);
553         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
554         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
555         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
556         manage_window_set_transient(GTK_WINDOW(color_dialog));
557
558         g_object_get(color_dialog, "ok-button", &button_ok,
559                                "cancel-button", &button_cancel,
560                                NULL);
561
562         g_signal_connect(G_OBJECT(button_cancel),
563                          "clicked", G_CALLBACK(quote_colors_set_dialog_cancel), data);
564         g_signal_connect(G_OBJECT(button_ok),
565                          "clicked", G_CALLBACK(quote_colors_set_dialog_ok), data);
566         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
567                          G_CALLBACK(quote_colors_set_dialog_key_pressed),data);
568
569         /* preselect the previous color in the color selection dialog */
570
571         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
572
573         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
574         gtk_color_selection_set_current_color
575                 (GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection(dialog)), &color);
576
577         gtk_widget_show(color_dialog);
578 }
579
580 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
581 {
582         GtkColorSelection *colorsel = GTK_COLOR_SELECTION(
583                 gtk_color_selection_dialog_get_color_selection
584                                                 ((GtkColorSelectionDialog *)color_dialog));
585         GdkColor color;
586         gint rgbvalue;
587         gchar *type = (gchar *)data;
588         gint c, ctype;
589
590         gtk_color_selection_get_current_color(colorsel, &color);
591         rgbvalue = gtkut_convert_gdk_color_to_int(&color);
592
593         ctype = GPOINTER_TO_INT(type);
594         /* custom colors */
595         for (c = 0; c < COLORLABELS; c++) {
596                 if (ctype == c) {
597                         prefs_common.custom_colorlabel[c].color = rgbvalue;
598                         set_button_bg_color(color_buttons.custom_color[c], rgbvalue);
599                         break;
600                 }
601         }
602         /* other colors */
603         if (c == COLORLABELS) {
604                 ctype -= COLORLABELS;
605                 if (ctype < COL_LAST_COLOR_INDEX) {
606                         prefs_common.color[ctype] = rgbvalue;
607                         set_button_bg_color(color_buttons.color[ctype], rgbvalue);
608                         if (ctype == COL_TGT_FOLDER) {
609                                 folderview_set_target_folder_color(prefs_common.color[ctype]);
610                         }
611                 } else {
612                         g_warning("Unrecognized datatype '%s' in quote_color_set_dialog_ok", type);
613                 }
614         }
615
616         gtk_widget_destroy(color_dialog);
617 }
618
619 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
620 {
621         gtk_widget_destroy(color_dialog);
622 }
623
624 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
625                                                 GdkEventKey *event,
626                                                 gpointer data)
627 {
628         GtkColorSelectionDialog *dialog;
629         GtkWidget *button_ok;
630         GtkWidget *button_cancel;
631
632         dialog = GTK_COLOR_SELECTION_DIALOG(widget);
633         g_object_get(dialog, "ok-button", &button_ok,
634                          "cancel-button", &button_cancel,
635                           NULL);
636
637         if (event) {
638                 switch (event->keyval) {
639                         case GDK_KEY_Escape:
640                                 gtk_button_clicked(GTK_BUTTON(button_cancel));
641                                 return TRUE;
642                         case GDK_KEY_Return: 
643                         case GDK_KEY_KP_Enter:
644                                 /* NOTE: changing focus makes widget accept all currently 
645                                  * changed settings! */
646                                 gtk_widget_grab_focus(button_ok);
647                                 /* call ok handler */                                           
648                                 gtk_button_clicked(GTK_BUTTON(button_ok));
649                                 return TRUE;
650                         default:
651                                 break;
652                 }
653         }
654         return FALSE;
655 }
656
657 static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
658 {
659         GtkStyle *newstyle;
660         GdkColor color;
661
662         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
663         newstyle = gtk_style_copy(gtk_widget_get_default_style());
664         newstyle->bg[GTK_STATE_NORMAL]   = color;
665         newstyle->bg[GTK_STATE_PRELIGHT] = color;
666         newstyle->bg[GTK_STATE_ACTIVE]   = color;
667
668         gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
669         gtk_style_unref(newstyle);
670 }
671
672 static void prefs_msg_colors_save(PrefsPage *_page)
673 {
674         MsgColorsPage *page = (MsgColorsPage *) _page;
675         gint c;
676
677         prefs_common.enable_color = 
678                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_enable_colors));
679         prefs_common.enable_bgcolor = 
680                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_enable_bgcolors));
681         prefs_common.recycle_quote_colors =
682                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_recycle_colors));
683
684         /* custom colors */
685         for (c = 0; c < COLORLABELS; c++) {
686                 g_free(prefs_common.custom_colorlabel[c].label);
687                 prefs_common.custom_colorlabel[c].label =
688                         gtk_editable_get_chars(GTK_EDITABLE(page->entry_custom_colorlabel[c]), 0, -1);
689         }
690         colorlabel_update_colortable_from_prefs();
691
692         main_window_reflect_prefs_all();
693         main_window_reflect_prefs_custom_colors(mainwindow_get_mainwindow());
694 }
695
696 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget, gpointer data)
697 {
698 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
699 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
700                          (CL(g) << (gulong)  8) | \
701                          (CL(b)))
702         MsgColorsPage *page = (MsgColorsPage *) data;
703         GdkColor color;
704         gint c;
705
706         for (c = 0; c < COLORLABELS; c++) {
707                 color = colorlabel_get_default_color(c);
708                 prefs_common.custom_colorlabel[c].color =
709                                                         (gint)CR(color.red, color.green, color.blue);
710                 set_button_bg_color(color_buttons.custom_color[c],
711                                                         prefs_common.custom_colorlabel[c].color);
712                 gtk_entry_set_text(GTK_ENTRY (page->entry_custom_colorlabel[c]),
713                                                         gettext(SAFE_STRING (colorlabel_get_color_default_text(c))));
714         }
715
716 #undef CR
717 #undef CL
718 }
719
720 static void prefs_msg_colors_destroy_widget(PrefsPage *_page)
721 {
722 }
723
724 MsgColorsPage *prefs_msg_colors;
725
726 void prefs_msg_colors_init(void)
727 {
728         MsgColorsPage *page;
729         static gchar *path[3];
730
731         path[0] = _("Display");
732         path[1] = _("Colors");
733         path[2] = NULL;
734
735         page = g_new0(MsgColorsPage, 1);
736         page->page.path = path;
737         page->page.create_widget = prefs_msg_colors_create_widget;
738         page->page.destroy_widget = prefs_msg_colors_destroy_widget;
739         page->page.save_page = prefs_msg_colors_save;
740         page->page.weight = 165.0;
741         prefs_gtk_register_page((PrefsPage *) page);
742         prefs_msg_colors = page;
743 }
744
745 void prefs_msg_colors_done(void)
746 {
747         prefs_gtk_unregister_page((PrefsPage *) prefs_msg_colors);
748         g_free(prefs_msg_colors);
749 }
750