Remove obsolete VisualAge-related files.
[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 // Copyright: (c) Vaclav Slavik, 2004
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_COLOURDLG
19
20 #include "wx/colordlg.h"
21 #include "wx/modalhook.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 #include "wx/gtk/private/dialogcount.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_HOOK_MODAL_DIALOG();
87
88 ColourDataToDialog();
89
90 wxOpenModalDialogLocker modalLocker;
91
92 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
93 gtk_widget_hide(m_widget);
94
95 switch (result)
96 {
97 default:
98 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
99 // fall through
100
101 case GTK_RESPONSE_CANCEL:
102 case GTK_RESPONSE_DELETE_EVENT:
103 case GTK_RESPONSE_CLOSE:
104 return wxID_CANCEL;
105
106 case GTK_RESPONSE_OK:
107 DialogToColourData();
108 return wxID_OK;
109 }
110 }
111
112 void wxColourDialog::ColourDataToDialog()
113 {
114 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
115 const GdkColor * const
116 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
117 : NULL;
118 #endif
119 #if wxUSE_LIBHILDON
120 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
121 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
122 #elif wxUSE_LIBHILDON2
123 GdkColor clr;
124 if (col)
125 clr = *col;
126 else {
127 clr.pixel = 0;
128 clr.red = 32768;
129 clr.green = 32768;
130 clr.blue = 32768;
131 }
132
133 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog *)m_widget, &clr);
134 #else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
135 GtkColorSelection* sel = GTK_COLOR_SELECTION(
136 gtk_color_selection_dialog_get_color_selection(
137 GTK_COLOR_SELECTION_DIALOG(m_widget)));
138
139 const wxColour& color = m_data.GetColour();
140 if (color.IsOk())
141 {
142 #ifdef __WXGTK3__
143 gtk_color_selection_set_current_rgba(sel, color);
144 #else
145 gtk_color_selection_set_current_color(sel, color.GetColor());
146 #endif
147 }
148
149 // setup the palette:
150
151 GdkColor colors[wxColourData::NUM_CUSTOM];
152 gint n_colors = 0;
153 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
154 {
155 wxColour c = m_data.GetCustomColour(i);
156 if (c.IsOk())
157 {
158 colors[n_colors] = *c.GetColor();
159 n_colors++;
160 }
161 }
162
163 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
164
165 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
166 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
167 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
168 }
169
170 void wxColourDialog::DialogToColourData()
171 {
172 #if wxUSE_LIBHILDON
173 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
174 const GdkColor * const clr = hildon_color_selector_get_color(sel);
175 if ( clr )
176 m_data.SetColour(*clr);
177 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
178 const GdkColor * const
179 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
180
181 GdkColor clr;
182 if (col)
183 clr = *col;
184 else {
185 clr.pixel = 0;
186 clr.red = 32768;
187 clr.green = 32768;
188 clr.blue = 32768;
189 }
190 GdkColor new_color = clr;
191 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color);
192
193 m_data.SetColour(new_color);
194 #else // !wxUSE_LIBHILDON2
195
196 GtkColorSelection* sel = GTK_COLOR_SELECTION(
197 gtk_color_selection_dialog_get_color_selection(
198 GTK_COLOR_SELECTION_DIALOG(m_widget)));
199
200 #ifdef __WXGTK3__
201 GdkRGBA clr;
202 gtk_color_selection_get_current_rgba(sel, &clr);
203 #else
204 GdkColor clr;
205 gtk_color_selection_get_current_color(sel, &clr);
206 #endif
207 m_data.SetColour(clr);
208
209 // Extract custom palette:
210
211 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
212 gchar *pal;
213 g_object_get(settings, "gtk-color-palette", &pal, NULL);
214
215 GdkColor *colors;
216 gint n_colors;
217 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
218 {
219 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
220 {
221 m_data.SetCustomColour(i, wxColour(colors[i]));
222 }
223 g_free(colors);
224 }
225
226 g_free(pal);
227 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
228 }
229
230 #endif // wxUSE_COLOURDLG
231