keypress fixes
[claws.git] / src / gtk / colorsel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 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 #include <gtk/gtk.h>
21
22 #include "colorsel.h"
23 #include "manage_window.h"
24
25 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
26 {
27         *((gint *) data) = 0;
28         gtk_main_quit();
29 }
30
31 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
32 {
33         *((gint *) data) = 1;
34         gtk_main_quit();
35 }
36
37 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
38                                                 GdkEventKey *event,
39                                                 gpointer data)
40 {
41         *((gint *) data) = 1;
42         gtk_main_quit();
43 }
44
45 gint colorsel_select_color_rgb(gchar *title, gint rgbvalue)
46 {
47         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
48         GtkColorSelectionDialog *color_dialog;
49         gint result;
50
51         color_dialog = GTK_COLOR_SELECTION_DIALOG(gtk_color_selection_dialog_new(title));
52         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
53         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
54         gtk_window_set_policy(GTK_WINDOW(color_dialog), FALSE, FALSE, FALSE);
55         manage_window_set_transient(GTK_WINDOW(color_dialog));
56
57         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
58                            "clicked", GTK_SIGNAL_FUNC(quote_colors_set_dialog_ok), &result);
59         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
60                            "clicked", GTK_SIGNAL_FUNC(quote_colors_set_dialog_cancel), &result);
61         gtk_signal_connect(GTK_OBJECT(color_dialog), "key_press_event",
62                            GTK_SIGNAL_FUNC(quote_colors_set_dialog_key_pressed),
63                            &result);
64
65         /* preselect the previous color in the color selection dialog */
66         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
67         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
68         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
69         gtk_color_selection_set_color
70                 (GTK_COLOR_SELECTION(color_dialog->colorsel), color);
71
72         gtk_widget_show(GTK_WIDGET(color_dialog));
73         gtk_main();
74
75         if (result == 0) {
76                 gint red, green, blue, rgbvalue_new;
77
78                 gtk_color_selection_get_color(GTK_COLOR_SELECTION(color_dialog->colorsel), color);
79
80                 red          = (gint) (color[0] * 255.0);
81                 green        = (gint) (color[1] * 255.0);
82                 blue         = (gint) (color[2] * 255.0);
83                 rgbvalue_new = (gint) ((red * 0x10000) | (green * 0x100) | blue);
84                 
85 #if 0
86                 fprintf(stderr, "redc = %f, greenc = %f, bluec = %f\n", color[0], color[1], color[2]);
87                 fprintf(stderr, "red = %d, green = %d, blue = %d\n", red, green, blue);
88                 fprintf(stderr, "Color is %x\n", rgbvalue);
89 #endif
90
91                 rgbvalue = rgbvalue_new;
92         }
93
94         gtk_widget_destroy(GTK_WIDGET(color_dialog));
95
96         return rgbvalue;
97 }
98