]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/colordlg.cpp
Don't connect to the same signal multiple times in wxGTK wxClipboard.
[wxWidgets.git] / src / gtk / colordlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colordlg.cpp
3 // Purpose: Native wxColourDialog for GTK+
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2004/06/04
7 // RCS-ID: $Id$
8 // Copyright: (c) Vaclav Slavik, 2004
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_COLOURDLG
20
21 #include "wx/colordlg.h"
22 #include "wx/testing.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/intl.h"
26 #endif
27
28 #include <gtk/gtk.h>
29 #include "wx/gtk/private.h"
30 #include "wx/gtk/private/gtk2-compat.h"
31
32 #if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-color-selector.h>
34 #endif // wxUSE_LIBHILDON
35
36 #if wxUSE_LIBHILDON2
37 extern "C" {
38 #include <hildon/hildon.h>
39 }
40 #endif // wxUSE_LIBHILDON2
41
42 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
43
44 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
45 {
46 Create(parent, data);
47 }
48
49 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
50 {
51 if (data)
52 m_data = *data;
53
54 m_parent = GetParentForModalDialog(parent, 0);
55 GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
56 : NULL;
57
58 #if wxUSE_LIBHILDON
59 m_widget = hildon_color_selector_new(parentGTK);
60 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
61 m_widget = hildon_color_chooser_dialog_new();
62 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
63 wxString title(_("Choose colour"));
64 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
65 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
66
67 g_object_ref(m_widget);
68
69 if ( parentGTK )
70 {
71 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
72 }
73
74 #if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
75 GtkColorSelection* sel = GTK_COLOR_SELECTION(
76 gtk_color_selection_dialog_get_color_selection(
77 GTK_COLOR_SELECTION_DIALOG(m_widget)));
78 gtk_color_selection_set_has_palette(sel, true);
79 #endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
80
81 return true;
82 }
83
84 int wxColourDialog::ShowModal()
85 {
86 WX_TESTING_SHOW_MODAL_HOOK();
87
88 ColourDataToDialog();
89
90 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
91 gtk_widget_hide(m_widget);
92
93 switch (result)
94 {
95 default:
96 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
97 // fall through
98
99 case GTK_RESPONSE_CANCEL:
100 case GTK_RESPONSE_DELETE_EVENT:
101 case GTK_RESPONSE_CLOSE:
102 return wxID_CANCEL;
103
104 case GTK_RESPONSE_OK:
105 DialogToColourData();
106 return wxID_OK;
107 }
108 }
109
110 void wxColourDialog::ColourDataToDialog()
111 {
112 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
113 const GdkColor * const
114 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
115 : NULL;
116 #endif
117 #if wxUSE_LIBHILDON
118 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
119 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
120 #elif wxUSE_LIBHILDON2
121 GdkColor clr;
122 if (col)
123 clr = *col;
124 else {
125 clr.pixel = 0;
126 clr.red = 32768;
127 clr.green = 32768;
128 clr.blue = 32768;
129 }
130
131 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog *)m_widget, &clr);
132 #else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
133 GtkColorSelection* sel = GTK_COLOR_SELECTION(
134 gtk_color_selection_dialog_get_color_selection(
135 GTK_COLOR_SELECTION_DIALOG(m_widget)));
136
137 const wxColour& color = m_data.GetColour();
138 if (color.IsOk())
139 {
140 #ifdef __WXGTK3__
141 gtk_color_selection_set_current_rgba(sel, color);
142 #else
143 gtk_color_selection_set_current_color(sel, color.GetColor());
144 #endif
145 }
146
147 // setup the palette:
148
149 GdkColor colors[wxColourData::NUM_CUSTOM];
150 gint n_colors = 0;
151 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
152 {
153 wxColour c = m_data.GetCustomColour(i);
154 if (c.IsOk())
155 {
156 colors[n_colors] = *c.GetColor();
157 n_colors++;
158 }
159 }
160
161 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
162
163 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
164 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
165 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
166 }
167
168 void wxColourDialog::DialogToColourData()
169 {
170 #if wxUSE_LIBHILDON
171 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
172 const GdkColor * const clr = hildon_color_selector_get_color(sel);
173 if ( clr )
174 m_data.SetColour(*clr);
175 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
176 const GdkColor * const
177 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
178
179 GdkColor clr;
180 if (col)
181 clr = *col;
182 else {
183 clr.pixel = 0;
184 clr.red = 32768;
185 clr.green = 32768;
186 clr.blue = 32768;
187 }
188 GdkColor new_color = clr;
189 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color);
190
191 m_data.SetColour(new_color);
192 #else // !wxUSE_LIBHILDON2
193
194 GtkColorSelection* sel = GTK_COLOR_SELECTION(
195 gtk_color_selection_dialog_get_color_selection(
196 GTK_COLOR_SELECTION_DIALOG(m_widget)));
197
198 #ifdef __WXGTK3__
199 GdkRGBA clr;
200 gtk_color_selection_get_current_rgba(sel, &clr);
201 #else
202 GdkColor clr;
203 gtk_color_selection_get_current_color(sel, &clr);
204 #endif
205 m_data.SetColour(clr);
206
207 // Extract custom palette:
208
209 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
210 gchar *pal;
211 g_object_get(settings, "gtk-color-palette", &pal, NULL);
212
213 GdkColor *colors;
214 gint n_colors;
215 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
216 {
217 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
218 {
219 m_data.SetCustomColour(i, wxColour(colors[i]));
220 }
221 g_free(colors);
222 }
223
224 g_free(pal);
225 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
226 }
227
228 #endif // wxUSE_COLOURDLG
229