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