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