]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
Move code that does not need realized GdkWinow out of realize handler
[wxWidgets.git] / src / gtk / colordlg.cpp
CommitLineData
274ad000 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/colordlg.cpp
274ad000
VS
3// Purpose: Native wxColourDialog for GTK+
4// Author: Vaclav Slavik
5// Modified by:
6// Created: 2004/06/04
274ad000
VS
7// Copyright: (c) Vaclav Slavik, 2004
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
274ad000
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
ff654490 18#if wxUSE_COLOURDLG
274ad000
VS
19
20#include "wx/colordlg.h"
691745ab 21#include "wx/modalhook.h"
274ad000
VS
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25#endif
26
9dc44eff 27#include <gtk/gtk.h>
274ad000 28#include "wx/gtk/private.h"
9dc44eff 29#include "wx/gtk/private/gtk2-compat.h"
3c2f3a60 30#include "wx/gtk/private/dialogcount.h"
274ad000 31
b6dd7ed8
VZ
32#if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-color-selector.h>
34#endif // wxUSE_LIBHILDON
35
426d19f1
JS
36#if wxUSE_LIBHILDON2
37extern "C" {
38 #include <hildon/hildon.h>
39}
40#endif // wxUSE_LIBHILDON2
41
274ad000
VS
42IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
43
44wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
45{
46 Create(parent, data);
47}
48
49bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
50{
51 if (data)
52 m_data = *data;
53
cdc48273 54 m_parent = GetParentForModalDialog(parent, 0);
b6dd7ed8
VZ
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);
ce00f59b 60#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
426d19f1
JS
61 m_widget = hildon_color_chooser_dialog_new();
62#else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
63 wxString title(_("Choose colour"));
64 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
b6dd7ed8 65#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
d0ee33f5 66
9ff9d30c
PC
67 g_object_ref(m_widget);
68
b6dd7ed8 69 if ( parentGTK )
fa349e95 70 {
b6dd7ed8 71 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
fa349e95 72 }
d0ee33f5 73
426d19f1 74#if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
385e8575
PC
75 GtkColorSelection* sel = GTK_COLOR_SELECTION(
76 gtk_color_selection_dialog_get_color_selection(
77 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 78 gtk_color_selection_set_has_palette(sel, true);
426d19f1 79#endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
80
81 return true;
82}
d0ee33f5 83
274ad000
VS
84int wxColourDialog::ShowModal()
85{
691745ab 86 WX_HOOK_MODAL_DIALOG();
643e9cf9 87
274ad000 88 ColourDataToDialog();
d0ee33f5 89
3c2f3a60
VZ
90 wxOpenModalDialogLocker modalLocker;
91
274ad000 92 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 93 gtk_widget_hide(m_widget);
d0ee33f5 94
274ad000
VS
95 switch (result)
96 {
97 default:
9a83f860 98 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
274ad000
VS
99 // fall through
100
101 case GTK_RESPONSE_CANCEL:
102 case GTK_RESPONSE_DELETE_EVENT:
103 case GTK_RESPONSE_CLOSE:
104 return wxID_CANCEL;
d0ee33f5 105
274ad000
VS
106 case GTK_RESPONSE_OK:
107 DialogToColourData();
108 return wxID_OK;
d0ee33f5 109 }
274ad000
VS
110}
111
112void wxColourDialog::ColourDataToDialog()
113{
9dc44eff 114#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
b6dd7ed8 115 const GdkColor * const
a1b806b9 116 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
b6dd7ed8 117 : NULL;
9dc44eff 118#endif
b6dd7ed8
VZ
119#if wxUSE_LIBHILDON
120 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
5c33522f 121 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
426d19f1
JS
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
385e8575
PC
135 GtkColorSelection* sel = GTK_COLOR_SELECTION(
136 gtk_color_selection_dialog_get_color_selection(
137 GTK_COLOR_SELECTION_DIALOG(m_widget)));
d0ee33f5 138
7d1214cd
PC
139 const wxColour& color = m_data.GetColour();
140 if (color.IsOk())
9dc44eff
PC
141 {
142#ifdef __WXGTK3__
7d1214cd 143 gtk_color_selection_set_current_rgba(sel, color);
9dc44eff 144#else
7d1214cd 145 gtk_color_selection_set_current_color(sel, color.GetColor());
9dc44eff
PC
146#endif
147 }
274ad000
VS
148
149 // setup the palette:
150
9dc44eff 151 GdkColor colors[wxColourData::NUM_CUSTOM];
274ad000 152 gint n_colors = 0;
9dc44eff 153 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
274ad000
VS
154 {
155 wxColour c = m_data.GetCustomColour(i);
a1b806b9 156 if (c.IsOk())
274ad000
VS
157 {
158 colors[n_colors] = *c.GetColor();
159 n_colors++;
160 }
161 }
162
b976323b 163 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
164
165 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 166 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
426d19f1 167#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
168}
169
170void wxColourDialog::DialogToColourData()
171{
b6dd7ed8
VZ
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);
426d19f1
JS
177#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
178 const GdkColor * const
a1b806b9 179 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
426d19f1
JS
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
385e8575
PC
196 GtkColorSelection* sel = GTK_COLOR_SELECTION(
197 gtk_color_selection_dialog_get_color_selection(
198 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 199
9dc44eff
PC
200#ifdef __WXGTK3__
201 GdkRGBA clr;
202 gtk_color_selection_get_current_rgba(sel, &clr);
203#else
274ad000
VS
204 GdkColor clr;
205 gtk_color_selection_get_current_color(sel, &clr);
9dc44eff 206#endif
b6dd7ed8 207 m_data.SetColour(clr);
d0ee33f5 208
274ad000
VS
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 {
9dc44eff 219 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
274ad000 220 {
b91fd704 221 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
222 }
223 g_free(colors);
224 }
d0ee33f5 225
274ad000 226 g_free(pal);
426d19f1 227#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
228}
229
ff654490 230#endif // wxUSE_COLOURDLG
274ad000 231