2012-07-28 [ticho] 3.8.1cvs23
[claws.git] / src / gtk / colorsel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2012 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
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22
23 #include "gtkutils.h"
24 #include "colorsel.h"
25 #include "manage_window.h"
26
27 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
28 {
29         *((gint *) data) = 0;
30         gtk_main_quit();
31 }
32
33 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
34 {
35         *((gint *) data) = 1;
36         gtk_main_quit();
37 }
38
39 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
40                                                 GdkEventKey *event,
41                                                 gpointer data)
42 {
43         if (event && event->keyval == GDK_KEY_Escape) {
44                 *((gint *) data) = 1;
45                 gtk_main_quit();
46                 return TRUE;
47         } else if (event && event->keyval == GDK_KEY_Return) {
48                 *((gint *) data) = 0;
49                 gtk_main_quit();
50                 return FALSE;
51         }
52         return FALSE;
53 }
54
55 gint colorsel_select_color_rgb(gchar *title, gint rgbvalue)
56 {
57         GdkColor color;
58         GtkColorSelectionDialog *color_dialog;
59         GtkWidget *ok_button, *cancel_button;
60         gint result;
61
62         color_dialog = GTK_COLOR_SELECTION_DIALOG(gtk_color_selection_dialog_new(title));
63         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
64         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
65         manage_window_set_transient(GTK_WINDOW(color_dialog));
66
67         g_object_get(color_dialog,
68                 "ok-button", &ok_button,
69                 "cancel-button", &cancel_button,
70                 NULL);
71
72         g_signal_connect(G_OBJECT(ok_button),
73                          "clicked", 
74                          G_CALLBACK(quote_colors_set_dialog_ok), &result);
75         g_signal_connect(G_OBJECT(cancel_button),
76                          "clicked", 
77                          G_CALLBACK(quote_colors_set_dialog_cancel), &result);
78         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
79                          G_CALLBACK(quote_colors_set_dialog_key_pressed), &result);
80
81         /* preselect the previous color in the color selection dialog */
82         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
83         gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
84                 gtk_color_selection_dialog_get_color_selection(color_dialog)), &color);
85
86         gtk_widget_show(GTK_WIDGET(color_dialog));
87         gtk_main();
88
89         if (result == 0) {
90                 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(
91                         gtk_color_selection_dialog_get_color_selection(color_dialog)), &color);
92                 rgbvalue = gtkut_convert_gdk_color_to_int(&color);
93         }
94
95         gtk_widget_destroy(GTK_WIDGET(color_dialog));
96
97         return rgbvalue;
98 }
99