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