2005-06-15 [paul] 1.9.11cvs71
[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 #include <gdk/gdkkeysyms.h>
22
23 #include "colorsel.h"
24 #include "manage_window.h"
25
26 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
27 {
28         *((gint *) data) = 0;
29         gtk_main_quit();
30 }
31
32 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
33 {
34         *((gint *) data) = 1;
35         gtk_main_quit();
36 }
37
38 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
39                                                 GdkEventKey *event,
40                                                 gpointer data)
41 {
42         if (event && event->keyval == GDK_Escape) {
43                 *((gint *) data) = 1;
44                 gtk_main_quit();
45                 return TRUE;
46         } else if (event && event->keyval == GDK_Return) {
47                 *((gint *) data) = 0;
48                 gtk_main_quit();
49                 return FALSE;
50         }
51         return FALSE;
52 }
53
54 gint colorsel_select_color_rgb(gchar *title, gint rgbvalue)
55 {
56         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
57         GtkColorSelectionDialog *color_dialog;
58         gint result;
59
60         color_dialog = GTK_COLOR_SELECTION_DIALOG(gtk_color_selection_dialog_new(title));
61         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
62         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
63         manage_window_set_transient(GTK_WINDOW(color_dialog));
64
65         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
66                          "clicked", 
67                          G_CALLBACK(quote_colors_set_dialog_ok), &result);
68         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
69                          "clicked", 
70                          G_CALLBACK(quote_colors_set_dialog_cancel), &result);
71         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
72                          G_CALLBACK(quote_colors_set_dialog_key_pressed), &result);
73
74         /* preselect the previous color in the color selection dialog */
75         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
76         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
77         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
78         gtk_color_selection_set_color
79                 (GTK_COLOR_SELECTION(color_dialog->colorsel), color);
80
81         gtk_widget_show(GTK_WIDGET(color_dialog));
82         gtk_main();
83
84         if (result == 0) {
85                 gint red, green, blue, rgbvalue_new;
86
87                 gtk_color_selection_get_color(GTK_COLOR_SELECTION(color_dialog->colorsel), color);
88
89                 red          = (gint) (color[0] * 255.0);
90                 green        = (gint) (color[1] * 255.0);
91                 blue         = (gint) (color[2] * 255.0);
92                 rgbvalue_new = (gint) ((red * 0x10000) | (green * 0x100) | blue);
93                 
94 #if 0
95                 fprintf(stderr, "redc = %f, greenc = %f, bluec = %f\n", color[0], color[1], color[2]);
96                 fprintf(stderr, "red = %d, green = %d, blue = %d\n", red, green, blue);
97                 fprintf(stderr, "Color is %x\n", rgbvalue);
98 #endif
99
100                 rgbvalue = rgbvalue_new;
101         }
102
103         gtk_widget_destroy(GTK_WIDGET(color_dialog));
104
105         return rgbvalue;
106 }
107