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
15 #if wxUSE_COLOURDLG
16
17 #include "wx/colordlg.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/intl.h"
21 #endif
22
23 #include "wx/gtk/private.h"
24 #include "wx/gtk/private/dialogcount.h"
25
26 extern "C" {
response(GtkDialog *,int response_id,wxColourDialog * win)27 static void response(GtkDialog*, int response_id, wxColourDialog* win)
28 {
29 win->EndModal(response_id == GTK_RESPONSE_OK ? wxID_OK : wxID_CANCEL);
30 }
31 }
32
33 wxIMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog);
34
wxColourDialog(wxWindow * parent,const wxColourData * data)35 wxColourDialog::wxColourDialog(wxWindow *parent, const wxColourData *data)
36 {
37 Create(parent, data);
38 }
39
Create(wxWindow * parent,const wxColourData * data)40 bool wxColourDialog::Create(wxWindow *parent, const wxColourData *data)
41 {
42 if (data)
43 m_data = *data;
44
45 m_parent = GetParentForModalDialog(parent, 0);
46 GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
47 : NULL;
48
49 wxString title(_("Choose colour"));
50 #ifdef __WXGTK4__
51 m_widget = gtk_color_chooser_dialog_new(title.utf8_str(), parentGTK);
52 g_object_ref(m_widget);
53 gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(m_widget), m_data.GetChooseAlpha());
54 #else
55 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
56 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
57
58 g_object_ref(m_widget);
59
60 if ( parentGTK )
61 {
62 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
63 }
64
65 GtkColorSelection* sel = GTK_COLOR_SELECTION(
66 gtk_color_selection_dialog_get_color_selection(
67 GTK_COLOR_SELECTION_DIALOG(m_widget)));
68 gtk_color_selection_set_has_palette(sel, true);
69 gtk_color_selection_set_has_opacity_control(sel, m_data.GetChooseAlpha());
70 wxGCC_WARNING_RESTORE()
71 #endif
72
73 return true;
74 }
75
ShowModal()76 int wxColourDialog::ShowModal()
77 {
78 ColourDataToDialog();
79
80 gulong id = g_signal_connect(m_widget, "response", G_CALLBACK(response), this);
81 int rc = wxDialog::ShowModal();
82 g_signal_handler_disconnect(m_widget, id);
83
84 if (rc == wxID_OK)
85 DialogToColourData();
86
87 return rc;
88 }
89
ColourDataToDialog()90 void wxColourDialog::ColourDataToDialog()
91 {
92 const wxColour& color = m_data.GetColour();
93 #ifdef __WXGTK4__
94 if (color.IsOk())
95 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(m_widget), color);
96 #else
97 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
98 GtkColorSelection* sel = GTK_COLOR_SELECTION(
99 gtk_color_selection_dialog_get_color_selection(
100 GTK_COLOR_SELECTION_DIALOG(m_widget)));
101
102 if (color.IsOk())
103 {
104 #ifdef __WXGTK3__
105 gtk_color_selection_set_current_rgba(sel, color);
106 #else
107 gtk_color_selection_set_current_color(sel, color.GetColor());
108 // Convert alpha range: [0,255] -> [0,65535]
109 gtk_color_selection_set_current_alpha(sel, 257*color.Alpha());
110 #endif
111 }
112
113 // setup the palette:
114
115 GdkColor colors[wxColourData::NUM_CUSTOM];
116 gint n_colors = 0;
117 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
118 {
119 wxColour c = m_data.GetCustomColour(i);
120 if (c.IsOk())
121 {
122 colors[n_colors] = *c.GetColor();
123 n_colors++;
124 }
125 }
126
127 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
128
129 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
130 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
131 wxGCC_WARNING_RESTORE()
132 #endif // !__WXGTK4__
133 }
134
DialogToColourData()135 void wxColourDialog::DialogToColourData()
136 {
137 #ifdef __WXGTK4__
138 GdkRGBA clr;
139 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(m_widget), &clr);
140 m_data.SetColour(clr);
141 #else
142 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
143 GtkColorSelection* sel = GTK_COLOR_SELECTION(
144 gtk_color_selection_dialog_get_color_selection(
145 GTK_COLOR_SELECTION_DIALOG(m_widget)));
146
147 #ifdef __WXGTK3__
148 GdkRGBA clr;
149 gtk_color_selection_get_current_rgba(sel, &clr);
150 m_data.SetColour(clr);
151 #else
152 GdkColor clr;
153 gtk_color_selection_get_current_color(sel, &clr);
154 // Set RGB colour
155 wxColour cRGB(clr);
156 guint16 alpha = gtk_color_selection_get_current_alpha(sel);
157 // Set RGBA colour (convert alpha range: [0,65535] -> [0,255]).
158 wxColour cRGBA(cRGB.Red(), cRGB.Green(), cRGB.Blue(), alpha/257);
159 m_data.SetColour(cRGBA);
160 #endif
161
162 // Extract custom palette:
163
164 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
165 gchar *pal;
166 g_object_get(settings, "gtk-color-palette", &pal, NULL);
167
168 GdkColor *colors;
169 gint n_colors;
170 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
171 {
172 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
173 {
174 m_data.SetCustomColour(i, wxColour(colors[i]));
175 }
176 g_free(colors);
177 }
178
179 g_free(pal);
180 wxGCC_WARNING_RESTORE()
181 #endif // !__WXGTK4__
182 }
183
184 #endif // wxUSE_COLOURDLG
185
186