2006-04-13 [wwp] 2.1.0cvs36
[claws.git] / src / prefs_msg_colors.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.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         GtkWidget *quote_level1_btn;
49         GtkWidget *quote_level2_btn;
50         GtkWidget *quote_level3_btn;
51         GtkWidget *quote_level1_bg_btn;
52         GtkWidget *quote_level2_bg_btn;
53         GtkWidget *quote_level3_bg_btn;
54         GtkWidget *uri_btn;
55         GtkWidget *tgt_folder_btn;
56         GtkWidget *signature_btn;
57         GtkWidget *color_new_btn;
58         /* custom colors */
59         GtkWidget *custom_color[COLORLABELS];
60 } color_buttons;
61
62 typedef struct _MsgColorsPage
63 {
64         PrefsPage page;
65
66         GtkWidget *window;
67         
68         GtkWidget *chkbtn_enablecol;
69         GtkWidget *chkbtn_enablebgcol;
70         GtkWidget *chkbtn_recycle_colors;
71
72         /* custom colors */
73         GtkWidget *custom_colorlabel_entry[COLORLABELS];
74 } MsgColorsPage;
75
76 static GtkWidget *color_dialog;
77
78 static void quote_color_set_dialog              (GtkWidget      *widget,
79                                                  gpointer        data);
80 static void quote_colors_set_dialog_ok          (GtkWidget      *widget,
81                                                  gpointer        data);
82 static void quote_colors_set_dialog_cancel      (GtkWidget      *widget,
83                                                  gpointer        data);
84 static gboolean quote_colors_set_dialog_key_pressed     (GtkWidget      *widget,
85                                                  GdkEventKey    *event,
86                                                  gpointer        data);
87 static void set_button_bg_color                 (GtkWidget      *widget,
88                                                  gint            color);
89 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget,
90                                                  gpointer        data);
91
92 void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window, 
93                                     gpointer data)
94 {
95         MsgColorsPage *prefs_msg_colors = (MsgColorsPage *) _page;
96         
97         GtkWidget *vbox1;
98         GtkWidget *vbox2;
99         GtkWidget *chkbtn_enablecol;
100         GtkWidget *quotelevel1_label;
101         GtkWidget *quotelevel2_label;
102         GtkWidget *quotelevel3_label;
103         GtkWidget *chkbtn_enablebgcol;
104         GtkWidget *quotelevel1_bglabel;
105         GtkWidget *quotelevel2_bglabel;
106         GtkWidget *quotelevel3_bglabel;
107         GtkWidget *uri_label;
108         GtkWidget *signature_label;
109         GtkWidget *tgt_folder_label;
110         GtkWidget *chkbtn_recycle_colors;
111         GtkWidget *hbox;
112         GtkWidget *color_new_label;
113         GtkWidget *msg_frame;
114         GtkWidget *folder_frame;
115         GtkWidget *hbox_quote;
116         GtkWidget *vbox_quotefg;
117         GtkWidget *vbox_quotebg;
118         /* custom colors */
119         GtkWidget *vbox_custom_colors;
120         GtkWidget *frame_custom_colors;
121         GtkWidget *hbox_reset_custom_colors;
122         GtkWidget *reset_custom_colors_btn;
123         GtkWidget *hbox_custom_color[COLORLABELS];
124         GtkWidget *custom_colorlabel_entry[COLORLABELS];
125         gint c;
126
127         vbox1 = gtk_vbox_new (FALSE, VSPACING_NARROW);
128         gtk_widget_show (vbox1);
129         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
130
131         PACK_FRAME(vbox1, msg_frame, _("Message view"));
132
133         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
134         gtk_widget_show (vbox2);
135         gtk_container_add (GTK_CONTAINER (msg_frame), vbox2);
136         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
137
138
139         hbox = gtk_hbox_new(FALSE, 8);
140         gtk_widget_show (hbox);
141         hbox_quote = gtk_hbox_new(FALSE, 8);
142         gtk_widget_show (hbox_quote);
143         vbox_quotefg = gtk_vbox_new(FALSE, 8);
144         gtk_widget_show (vbox_quotefg);
145         vbox_quotebg = gtk_vbox_new(FALSE, 8);
146 #if GTK_CHECK_VERSION(2, 8, 0)
147         gtk_widget_show (vbox_quotebg);
148 #endif  
149         
150         gtk_box_pack_start (GTK_BOX (vbox2), hbox_quote, FALSE, TRUE, 0);
151         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotefg, FALSE, TRUE, 0);
152         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotebg, FALSE, TRUE, 0);
153
154         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
155         PACK_CHECK_BUTTON (hbox, chkbtn_enablecol,
156                            _("Enable coloration of message text"));
157
158         hbox = gtk_hbox_new(FALSE, 8);
159         gtk_widget_show (hbox);
160         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
161
162         color_buttons.quote_level1_btn = gtk_button_new();
163         gtk_widget_show(color_buttons.quote_level1_btn);
164         gtk_widget_set_size_request (color_buttons.quote_level1_btn, 30, 20);
165         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level1_btn, 
166                             FALSE, FALSE, 0);
167         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level1_btn);
168
169         quotelevel1_label = gtk_label_new (_("Quoted Text - First Level"));
170         gtk_widget_show(quotelevel1_label);
171         gtk_box_pack_start (GTK_BOX(hbox), quotelevel1_label, 
172                             FALSE, FALSE, 0);
173         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel1_label);
174                 
175         hbox = gtk_hbox_new(FALSE, 8);
176         gtk_widget_show (hbox);
177         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
178
179         color_buttons.quote_level2_btn = gtk_button_new();
180         gtk_widget_show(color_buttons.quote_level2_btn);
181         gtk_widget_set_size_request (color_buttons.quote_level2_btn, 30, 20);
182         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level2_btn, 
183                             FALSE, FALSE, 0);
184         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level2_btn);
185
186         quotelevel2_label = gtk_label_new (_("Quoted Text - Second Level"));
187         gtk_widget_show(quotelevel2_label);
188         gtk_box_pack_start (GTK_BOX(hbox), quotelevel2_label, 
189                             FALSE, FALSE, 0);
190         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel2_label);
191
192         hbox = gtk_hbox_new(FALSE, 8);
193         gtk_widget_show (hbox);
194         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
195
196         color_buttons.quote_level3_btn = gtk_button_new();
197         gtk_widget_show(color_buttons.quote_level3_btn);
198         gtk_widget_set_size_request (color_buttons.quote_level3_btn, 30, 20);
199         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level3_btn, 
200                             FALSE, FALSE, 0);
201         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level3_btn);
202
203         quotelevel3_label = gtk_label_new (_("Quoted Text - Third Level"));
204         gtk_widget_show(quotelevel3_label);
205         gtk_box_pack_start (GTK_BOX(hbox), quotelevel3_label, 
206                             FALSE, FALSE, 0);
207         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel3_label);
208
209
210         hbox = gtk_hbox_new(FALSE, 8);
211         gtk_widget_show (hbox);
212         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
213
214         PACK_CHECK_BUTTON (hbox, chkbtn_enablebgcol,
215                            _("Enable coloration of background text"));
216         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, chkbtn_enablebgcol);
217
218         hbox = gtk_hbox_new(FALSE, 8);
219         gtk_widget_show (hbox);
220         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
221         
222         color_buttons.quote_level1_bg_btn = gtk_button_new();
223         gtk_widget_show(color_buttons.quote_level1_bg_btn);
224         gtk_widget_set_size_request (color_buttons.quote_level1_bg_btn, 30, 20);
225         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level1_bg_btn, 
226                             FALSE, FALSE, 0);
227         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level1_bg_btn);
228
229         quotelevel1_bglabel = gtk_label_new (_(" Background"));
230         gtk_widget_show(quotelevel1_bglabel);
231         gtk_box_pack_start (GTK_BOX(hbox), quotelevel1_bglabel, 
232                             FALSE, FALSE, 0);
233         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel1_bglabel);
234
235         hbox = gtk_hbox_new(FALSE, 8);
236         gtk_widget_show (hbox);
237         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
238         
239         color_buttons.quote_level2_bg_btn = gtk_button_new();
240         gtk_widget_show(color_buttons.quote_level2_bg_btn);
241         gtk_widget_set_size_request (color_buttons.quote_level2_bg_btn, 30, 20);
242         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level2_bg_btn, 
243                             FALSE, FALSE, 0);
244         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level2_bg_btn);
245
246         quotelevel2_bglabel = gtk_label_new (_(" Background"));
247         gtk_widget_show(quotelevel2_bglabel);
248         gtk_box_pack_start (GTK_BOX(hbox), quotelevel2_bglabel, 
249                             FALSE, FALSE, 0);
250         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel2_bglabel);
251
252         hbox = gtk_hbox_new(FALSE, 8);
253         gtk_widget_show (hbox);
254         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
255         
256         color_buttons.quote_level3_bg_btn = gtk_button_new();
257         gtk_widget_show(color_buttons.quote_level3_bg_btn);
258         gtk_widget_set_size_request (color_buttons.quote_level3_bg_btn, 30, 20);
259         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level3_bg_btn, 
260                             FALSE, FALSE, 0);
261         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.quote_level3_bg_btn);
262
263         quotelevel3_bglabel = gtk_label_new (_(" Background"));
264         gtk_widget_show(quotelevel3_bglabel);
265         gtk_box_pack_start (GTK_BOX(hbox), quotelevel3_bglabel, 
266                             FALSE, FALSE, 0);
267         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, quotelevel3_bglabel);
268
269         hbox = gtk_hbox_new(FALSE, 8);
270         gtk_widget_show (hbox);
271         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
272
273         PACK_CHECK_BUTTON (hbox, chkbtn_recycle_colors,
274                            _("Cycle quote colors"));
275         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, chkbtn_recycle_colors);
276
277         hbox = gtk_hbox_new(FALSE, 8);
278         gtk_widget_show (hbox);
279         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
280
281         color_buttons.uri_btn = gtk_button_new();
282         gtk_widget_show(color_buttons.uri_btn);
283         gtk_widget_set_size_request (color_buttons.uri_btn, 30, 20);
284         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.uri_btn, 
285                             FALSE, FALSE, 0);
286         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.uri_btn);
287
288         uri_label = gtk_label_new (_("URI link"));
289         gtk_widget_show(uri_label);
290         gtk_box_pack_start (GTK_BOX(hbox), uri_label, FALSE, FALSE, 0);
291         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, uri_label);
292
293         hbox = gtk_hbox_new(FALSE, 8);
294         gtk_widget_show (hbox);
295         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
296
297         color_buttons.signature_btn = gtk_button_new();
298         gtk_widget_show(color_buttons.signature_btn);
299         gtk_widget_set_size_request (color_buttons.signature_btn, 30, 20);
300         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.signature_btn, 
301                             FALSE, FALSE, 0);
302         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, color_buttons.signature_btn);
303
304         signature_label = gtk_label_new (_("Signatures"));
305         gtk_widget_show(signature_label);
306         gtk_box_pack_start (GTK_BOX(hbox), signature_label, FALSE, FALSE, 0);
307         SET_TOGGLE_SENSITIVITY(chkbtn_enablecol, signature_label);
308
309         PACK_FRAME(vbox1, folder_frame, _("Folder list"));
310
311         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
312         gtk_widget_show (vbox2);
313         gtk_container_add (GTK_CONTAINER (folder_frame), vbox2);
314         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
315
316         hbox = gtk_hbox_new(FALSE, 8);
317         gtk_widget_show (hbox);
318         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
319
320         color_buttons.tgt_folder_btn = gtk_button_new();
321         gtk_widget_show(color_buttons.tgt_folder_btn);
322         gtk_widget_set_size_request (color_buttons.tgt_folder_btn, 30, 20);
323         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.tgt_folder_btn, 
324                             FALSE, FALSE, 0);
325
326         tgt_folder_label = gtk_label_new (_("Target folder"));
327         gtk_widget_show(tgt_folder_label);
328         gtk_box_pack_start (GTK_BOX(hbox), tgt_folder_label, FALSE, FALSE, 0);
329
330         hbox = gtk_hbox_new(FALSE, 8);
331         gtk_widget_show (hbox);
332         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
333
334         color_buttons.color_new_btn = gtk_button_new();
335         gtk_widget_show (color_buttons.color_new_btn);
336         gtk_widget_set_size_request (color_buttons.color_new_btn, 30, 20);
337         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.color_new_btn,
338                             FALSE, FALSE, 0);
339
340         color_new_label = gtk_label_new (_("Folder containing new messages"));
341         gtk_widget_show(color_new_label);
342         gtk_box_pack_start (GTK_BOX(hbox), color_new_label, FALSE, FALSE, 0);
343
344         /* custom colors */
345         PACK_FRAME (vbox1, frame_custom_colors, _("Color labels"));
346
347         vbox_custom_colors = gtk_vbox_new (FALSE, VSPACING_NARROW);
348         gtk_widget_show (vbox_custom_colors);
349         gtk_container_add (GTK_CONTAINER (frame_custom_colors), vbox_custom_colors);
350         gtk_container_set_border_width (GTK_CONTAINER (vbox_custom_colors), VBOX_BORDER);
351
352         for (c = 0; c < COLORLABELS; c++) {
353                 hbox_custom_color[c] = gtk_hbox_new(FALSE, 8);
354                 gtk_widget_show (hbox_custom_color[c]);
355                 gtk_box_pack_start (GTK_BOX (vbox_custom_colors), hbox_custom_color[c],
356                                 FALSE, TRUE, 0);
357                 color_buttons.custom_color[c] = gtk_button_new();
358                 gtk_widget_show (color_buttons.custom_color[c]);
359                 gtk_widget_set_size_request (color_buttons.custom_color[c], 30, 20);
360                 gtk_box_pack_start (GTK_BOX (hbox_custom_color[c]), color_buttons.custom_color[c],
361                                 FALSE, FALSE, 0);
362                 custom_colorlabel_entry[c] = gtk_entry_new();
363                 gtk_widget_show (custom_colorlabel_entry[c]);
364                 gtk_box_pack_start (GTK_BOX (hbox_custom_color[c]), custom_colorlabel_entry[c],
365                                 FALSE, FALSE, 0);
366         }
367
368         hbox_reset_custom_colors = gtk_hbox_new(FALSE, 8);
369         gtk_widget_show (hbox_reset_custom_colors);
370         gtk_box_pack_start (GTK_BOX (vbox_custom_colors), hbox_reset_custom_colors,
371                 FALSE, TRUE, 0);
372
373         reset_custom_colors_btn = gtk_button_new_with_label (_(" Use default "));
374         gtk_widget_show (reset_custom_colors_btn);
375         gtk_box_pack_start (GTK_BOX (hbox_reset_custom_colors), reset_custom_colors_btn,
376                 FALSE, FALSE, 0);
377
378         g_signal_connect(G_OBJECT(color_buttons.quote_level1_btn), "clicked",
379                          G_CALLBACK(quote_color_set_dialog), "LEVEL1");
380         g_signal_connect(G_OBJECT(color_buttons.quote_level2_btn), "clicked",
381                          G_CALLBACK(quote_color_set_dialog), "LEVEL2");
382         g_signal_connect(G_OBJECT(color_buttons.quote_level3_btn), "clicked",
383                          G_CALLBACK(quote_color_set_dialog), "LEVEL3");
384         g_signal_connect(G_OBJECT(color_buttons.quote_level1_bg_btn), "clicked",
385                          G_CALLBACK(quote_color_set_dialog), "LEVEL1BG");
386         g_signal_connect(G_OBJECT(color_buttons.quote_level2_bg_btn), "clicked",
387                          G_CALLBACK(quote_color_set_dialog), "LEVEL2BG");
388         g_signal_connect(G_OBJECT(color_buttons.quote_level3_bg_btn), "clicked",
389                          G_CALLBACK(quote_color_set_dialog), "LEVEL3BG");
390         g_signal_connect(G_OBJECT(color_buttons.uri_btn), "clicked",
391                          G_CALLBACK(quote_color_set_dialog), "URI");
392         g_signal_connect(G_OBJECT(color_buttons.tgt_folder_btn), "clicked",
393                          G_CALLBACK(quote_color_set_dialog), "TGTFLD");
394         g_signal_connect(G_OBJECT(color_buttons.signature_btn), "clicked",
395                          G_CALLBACK(quote_color_set_dialog), "SIGNATURE");
396         g_signal_connect(G_OBJECT(color_buttons.color_new_btn), "clicked",
397                          G_CALLBACK(quote_color_set_dialog), "NEW");
398         /* custom colors */
399         for (c = 0; c < COLORLABELS; c++) {
400                 g_signal_connect(G_OBJECT(color_buttons.custom_color[c]), "clicked",
401                                  G_CALLBACK(quote_color_set_dialog), GINT_TO_POINTER(c));
402         }
403
404         g_signal_connect(G_OBJECT(reset_custom_colors_btn), "clicked",
405                          G_CALLBACK(prefs_msg_colors_reset_custom_colors), prefs_msg_colors);
406
407         set_button_bg_color(color_buttons.quote_level1_btn,
408                             prefs_common.quote_level1_col);
409         set_button_bg_color(color_buttons.quote_level2_btn,
410                             prefs_common.quote_level2_col);
411         set_button_bg_color(color_buttons.quote_level3_btn,
412                             prefs_common.quote_level3_col);
413         set_button_bg_color(color_buttons.quote_level1_bg_btn,
414                             prefs_common.quote_level1_bgcol);
415         set_button_bg_color(color_buttons.quote_level2_bg_btn,
416                             prefs_common.quote_level2_bgcol);
417         set_button_bg_color(color_buttons.quote_level3_bg_btn,
418                             prefs_common.quote_level3_bgcol);
419         set_button_bg_color(color_buttons.uri_btn,
420                             prefs_common.uri_col);
421         set_button_bg_color(color_buttons.tgt_folder_btn,
422                             prefs_common.tgt_folder_col);
423         set_button_bg_color(color_buttons.signature_btn,
424                             prefs_common.signature_col);
425         set_button_bg_color(color_buttons.color_new_btn,
426                             prefs_common.color_new);
427         /* custom colors */
428         for (c = 0; c < COLORLABELS; c++) {
429                 set_button_bg_color(color_buttons.custom_color[c],
430                                     prefs_common.custom_colorlabel[c].color);
431                 gtk_entry_set_text(GTK_ENTRY (custom_colorlabel_entry[c]), 
432                                    gettext(SAFE_STRING (prefs_common.custom_colorlabel[c].label)));
433         }
434
435         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enablecol),
436                                      prefs_common.enable_color);
437         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enablebgcol),
438                                      prefs_common.enable_bgcolor);
439         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_recycle_colors),
440                                      prefs_common.recycle_quote_colors);
441
442         prefs_msg_colors->chkbtn_enablecol      = chkbtn_enablecol;
443         prefs_msg_colors->chkbtn_enablebgcol    = chkbtn_enablebgcol;
444         prefs_msg_colors->chkbtn_recycle_colors = chkbtn_recycle_colors;
445         /* custom colors */
446         for (c = 0; c < COLORLABELS; c++) {
447                 prefs_msg_colors->custom_colorlabel_entry[c] = custom_colorlabel_entry[c];
448         }
449
450         prefs_msg_colors->page.widget = vbox1;
451 }
452
453 static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
454 {
455         gchar *type = (gchar *)data;
456         gchar *title = NULL;
457         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
458         gint rgbvalue = 0;
459         GtkColorSelectionDialog *dialog;
460         gint c;
461
462         /* custom colors */
463         for (c = 0; c < COLORLABELS; c++) {
464                 if (GPOINTER_TO_INT(type) == c) {
465                         title = g_strdup_printf(_("Pick color for color #%d"), c+1);
466                         rgbvalue = prefs_common.custom_colorlabel[c].color;
467                         break;
468                 }
469         }
470         /* other colors */
471         if (c == COLORLABELS) {
472                 if(g_ascii_strcasecmp(type, "LEVEL1") == 0) {
473                         title = g_strdup(_("Pick color for quotation level 1"));
474                         rgbvalue = prefs_common.quote_level1_col;
475                 } else if(g_ascii_strcasecmp(type, "LEVEL2") == 0) {
476                         title = g_strdup(_("Pick color for quotation level 2"));
477                         rgbvalue = prefs_common.quote_level2_col;
478                 } else if(g_ascii_strcasecmp(type, "LEVEL3") == 0) {
479                         title = g_strdup(_("Pick color for quotation level 3"));
480                         rgbvalue = prefs_common.quote_level3_col;
481                 } else if(g_ascii_strcasecmp(type, "LEVEL1BG") == 0) {
482                         title = g_strdup(_("Pick color for quotation level 1 background"));
483                         rgbvalue = prefs_common.quote_level1_bgcol;
484                 } else if(g_ascii_strcasecmp(type, "LEVEL2BG") == 0) {
485                         title = g_strdup(_("Pick color for quotation level 2 background"));
486                         rgbvalue = prefs_common.quote_level2_bgcol;
487                 } else if(g_ascii_strcasecmp(type, "LEVEL3BG") == 0) {
488                         title = g_strdup(_("Pick color for quotation level 3 background"));
489                         rgbvalue = prefs_common.quote_level3_bgcol;
490                 } else if(g_ascii_strcasecmp(type, "URI") == 0) {
491                         title = g_strdup(_("Pick color for URI"));
492                         rgbvalue = prefs_common.uri_col;
493                 } else if(g_ascii_strcasecmp(type, "TGTFLD") == 0) {
494                         title = g_strdup(_("Pick color for target folder"));
495                         rgbvalue = prefs_common.tgt_folder_col;
496                 } else if(g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
497                         title = g_strdup(_("Pick color for signatures"));
498                         rgbvalue = prefs_common.signature_col;
499                 } else if(g_ascii_strcasecmp(type, "NEW") == 0) {
500                         title = g_strdup(_("Pick color for folder"));
501                         rgbvalue = prefs_common.color_new;
502                 } else {
503                         /* Should never be called */
504                         g_warning("Unrecognized datatype '%s' in quote_color_set_dialog\n", type);
505                         return;
506                 }
507         }
508
509         color_dialog = gtk_color_selection_dialog_new(title);
510         g_free(title);
511         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
512         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
513         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
514         manage_window_set_transient(GTK_WINDOW(color_dialog));
515
516         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
517                          "clicked", G_CALLBACK(quote_colors_set_dialog_cancel), data);
518         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
519                          "clicked", G_CALLBACK(quote_colors_set_dialog_ok), data);
520         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
521                          G_CALLBACK(quote_colors_set_dialog_key_pressed),data);
522
523         /* preselect the previous color in the color selection dialog */
524         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
525         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
526         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
527         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
528         gtk_color_selection_set_color
529                 (GTK_COLOR_SELECTION(dialog->colorsel), color);
530
531         gtk_widget_show(color_dialog);
532 }
533
534 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
535 {
536         GtkColorSelection *colorsel = (GtkColorSelection *)
537                                                 ((GtkColorSelectionDialog *)color_dialog)->colorsel;
538         gdouble color[4];
539         gint red, green, blue, rgbvalue;
540         gchar *type = (gchar *)data;
541         gint c;
542
543         gtk_color_selection_get_color(colorsel, color);
544
545         red      = (gint) (color[0] * 255.0);
546         green    = (gint) (color[1] * 255.0);
547         blue     = (gint) (color[2] * 255.0);
548         rgbvalue = (gint) ((red * 0x10000) | (green * 0x100) | blue);
549
550
551         /* custom colors */
552         for (c = 0; c < COLORLABELS; c++) {
553                 if (GPOINTER_TO_INT(type) == c) {
554                         prefs_common.custom_colorlabel[c].color = rgbvalue;
555                         set_button_bg_color(color_buttons.custom_color[c], rgbvalue);
556                         break;
557                 }
558         }
559         /* other colors */
560         if (c == COLORLABELS) {
561                 if (g_ascii_strcasecmp(type, "LEVEL1") == 0) {
562                         prefs_common.quote_level1_col = rgbvalue;
563                         set_button_bg_color(color_buttons.quote_level1_btn, rgbvalue);
564                 } else if (g_ascii_strcasecmp(type, "LEVEL2") == 0) {
565                         prefs_common.quote_level2_col = rgbvalue;
566                         set_button_bg_color(color_buttons.quote_level2_btn, rgbvalue);
567                 } else if (g_ascii_strcasecmp(type, "LEVEL3") == 0) {
568                         prefs_common.quote_level3_col = rgbvalue;
569                         set_button_bg_color(color_buttons.quote_level3_btn, rgbvalue);
570                 } else if (g_ascii_strcasecmp(type, "LEVEL1BG") == 0) {
571                         prefs_common.quote_level1_bgcol = rgbvalue;
572                         set_button_bg_color(color_buttons.quote_level1_bg_btn, rgbvalue);
573                 } else if (g_ascii_strcasecmp(type, "LEVEL2BG") == 0) {
574                         prefs_common.quote_level2_bgcol = rgbvalue;
575                         set_button_bg_color(color_buttons.quote_level2_bg_btn, rgbvalue);
576                 } else if (g_ascii_strcasecmp(type, "LEVEL3BG") == 0) {
577                         prefs_common.quote_level3_bgcol = rgbvalue;
578                         set_button_bg_color(color_buttons.quote_level3_bg_btn, rgbvalue);
579                 } else if (g_ascii_strcasecmp(type, "URI") == 0) {
580                         prefs_common.uri_col = rgbvalue;
581                         set_button_bg_color(color_buttons.uri_btn, rgbvalue);
582                 } else if (g_ascii_strcasecmp(type, "TGTFLD") == 0) {
583                         prefs_common.tgt_folder_col = rgbvalue;
584                         set_button_bg_color(color_buttons.tgt_folder_btn, rgbvalue);
585                         folderview_set_target_folder_color(prefs_common.tgt_folder_col);
586                 } else if (g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
587                         prefs_common.signature_col = rgbvalue;
588                         set_button_bg_color(color_buttons.signature_btn, rgbvalue);
589                 } else if (g_ascii_strcasecmp(type, "NEW") == 0) {
590                         prefs_common.color_new = rgbvalue;
591                         set_button_bg_color(color_buttons.color_new_btn, rgbvalue);
592                 } else {
593                         fprintf(stderr, "Unrecognized datatype '%s' in quote_color_set_dialog_ok\n", type);
594                 }
595         }
596
597         gtk_widget_destroy(color_dialog);
598 }
599
600 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
601 {
602         gtk_widget_destroy(color_dialog);
603 }
604
605 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
606                                                 GdkEventKey *event,
607                                                 gpointer data)
608 {
609         if (event) {
610                 switch (event->keyval) {
611                         case GDK_Escape:
612                                 gtk_button_clicked(GTK_BUTTON(GTK_COLOR_SELECTION_DIALOG
613                                                         (widget)->cancel_button));
614                                 return TRUE;
615                         case GDK_Return: 
616                         case GDK_KP_Enter:
617                                 /* NOTE: changing focus makes widget accept all currently 
618                                  * changed settings! */
619                                 gtk_widget_grab_focus
620                                         (GTK_COLOR_SELECTION_DIALOG
621                                                 (widget)->ok_button);
622                                 /* call ok handler */                                           
623                                 gtk_button_clicked(GTK_BUTTON
624                                         (GTK_COLOR_SELECTION_DIALOG
625                                                 (widget)->ok_button));
626                                 return TRUE;
627                         default:
628                                 break;
629                 }
630         }
631         return FALSE;
632 }
633
634 static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
635 {
636         GtkStyle *newstyle;
637         GdkColor color;
638
639         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
640         newstyle = gtk_style_copy(gtk_widget_get_default_style());
641         newstyle->bg[GTK_STATE_NORMAL]   = color;
642         newstyle->bg[GTK_STATE_PRELIGHT] = color;
643         newstyle->bg[GTK_STATE_ACTIVE]   = color;
644
645         gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
646 }
647
648 void prefs_msg_colors_save(PrefsPage *_page)
649 {
650         MsgColorsPage *page = (MsgColorsPage *) _page;
651         gint c;
652
653         prefs_common.enable_color = 
654                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enablecol));
655         prefs_common.enable_bgcolor = 
656                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enablebgcol));
657         prefs_common.recycle_quote_colors =
658                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_recycle_colors));
659
660         /* custom colors */
661         for (c = 0; c < COLORLABELS; c++) {
662                 g_free(prefs_common.custom_colorlabel[c].label);
663                 prefs_common.custom_colorlabel[c].label =
664                         gtk_editable_get_chars(GTK_EDITABLE(page->custom_colorlabel_entry[c]), 0, -1);
665         }
666         colorlabel_update_colortable_from_prefs();
667
668         main_window_reflect_prefs_all();
669         main_window_reflect_prefs_custom_colors(mainwindow_get_mainwindow());
670 }
671
672 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget, gpointer data)
673 {
674 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
675 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
676                          (CL(g) << (gulong)  8) | \
677                          (CL(b)))
678         MsgColorsPage *page = (MsgColorsPage *) data;
679         GdkColor color;
680         gint c;
681
682         for (c = 0; c < COLORLABELS; c++) {
683                 color = colorlabel_get_default_color(c);
684                 prefs_common.custom_colorlabel[c].color =
685                                                         (gint)CR(color.red, color.green, color.blue);
686                 set_button_bg_color(color_buttons.custom_color[c],
687                                                         prefs_common.custom_colorlabel[c].color);
688                 gtk_entry_set_text(GTK_ENTRY (page->custom_colorlabel_entry[c]),
689                                                         gettext(SAFE_STRING (colorlabel_get_color_default_text(c))));
690         }
691
692 #undef CR
693 #undef CL
694 }
695
696 static void prefs_msg_colors_destroy_widget(PrefsPage *_page)
697 {
698 }
699
700 MsgColorsPage *prefs_msg_colors;
701
702 void prefs_msg_colors_init(void)
703 {
704         MsgColorsPage *page;
705         static gchar *path[3];
706
707         path[0] = _("Display");
708         path[1] = _("Colors");
709         path[2] = NULL;
710
711         page = g_new0(MsgColorsPage, 1);
712         page->page.path = path;
713         page->page.create_widget = prefs_msg_colors_create_widget;
714         page->page.destroy_widget = prefs_msg_colors_destroy_widget;
715         page->page.save_page = prefs_msg_colors_save;
716         page->page.weight = 165.0;
717         prefs_gtk_register_page((PrefsPage *) page);
718         prefs_msg_colors = page;
719 }
720
721 void prefs_msg_colors_done(void)
722 {
723         prefs_gtk_unregister_page((PrefsPage *) prefs_msg_colors);
724         g_free(prefs_msg_colors);
725 }
726