]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
Copy recently added wxGenericValidator fields in Copy().
[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
7// RCS-ID: $Id$
8// Copyright: (c) Vaclav Slavik, 2004
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
274ad000
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
ff654490 19#if wxUSE_COLOURDLG
274ad000
VS
20
21#include "wx/colordlg.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25#endif
26
27#include "wx/gtk/private.h"
28
b6dd7ed8
VZ
29#if wxUSE_LIBHILDON
30 #include <hildon-widgets/hildon-color-selector.h>
31#endif // wxUSE_LIBHILDON
32
426d19f1
JS
33#if wxUSE_LIBHILDON2
34extern "C" {
35 #include <hildon/hildon.h>
36}
37#endif // wxUSE_LIBHILDON2
38
274ad000
VS
39IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
40
41wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
42{
43 Create(parent, data);
44}
45
46bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
47{
48 if (data)
49 m_data = *data;
50
cdc48273 51 m_parent = GetParentForModalDialog(parent, 0);
b6dd7ed8
VZ
52 GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
53 : NULL;
54
55#if wxUSE_LIBHILDON
56 m_widget = hildon_color_selector_new(parentGTK);
ce00f59b 57#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
426d19f1
JS
58 m_widget = hildon_color_chooser_dialog_new();
59#else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
60 wxString title(_("Choose colour"));
61 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
b6dd7ed8 62#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
d0ee33f5 63
9ff9d30c
PC
64 g_object_ref(m_widget);
65
b6dd7ed8 66 if ( parentGTK )
fa349e95 67 {
b6dd7ed8 68 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
fa349e95 69 }
d0ee33f5 70
426d19f1 71#if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
385e8575
PC
72 GtkColorSelection* sel = GTK_COLOR_SELECTION(
73 gtk_color_selection_dialog_get_color_selection(
74 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 75 gtk_color_selection_set_has_palette(sel, true);
426d19f1 76#endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
77
78 return true;
79}
d0ee33f5 80
274ad000
VS
81int wxColourDialog::ShowModal()
82{
83 ColourDataToDialog();
d0ee33f5 84
274ad000 85 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 86 gtk_widget_hide(m_widget);
d0ee33f5 87
274ad000
VS
88 switch (result)
89 {
90 default:
9a83f860 91 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
274ad000
VS
92 // fall through
93
94 case GTK_RESPONSE_CANCEL:
95 case GTK_RESPONSE_DELETE_EVENT:
96 case GTK_RESPONSE_CLOSE:
97 return wxID_CANCEL;
d0ee33f5 98
274ad000
VS
99 case GTK_RESPONSE_OK:
100 DialogToColourData();
101 return wxID_OK;
d0ee33f5 102 }
274ad000
VS
103}
104
105void wxColourDialog::ColourDataToDialog()
106{
b6dd7ed8 107 const GdkColor * const
a1b806b9 108 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
b6dd7ed8
VZ
109 : NULL;
110
111#if wxUSE_LIBHILDON
112 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
5c33522f 113 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
426d19f1
JS
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
385e8575
PC
127 GtkColorSelection* sel = GTK_COLOR_SELECTION(
128 gtk_color_selection_dialog_get_color_selection(
129 GTK_COLOR_SELECTION_DIALOG(m_widget)));
d0ee33f5 130
b6dd7ed8
VZ
131 if ( col )
132 gtk_color_selection_set_current_color(sel, col);
274ad000
VS
133
134 // setup the palette:
135
136 GdkColor colors[16];
137 gint n_colors = 0;
138 for (unsigned i = 0; i < 16; i++)
139 {
140 wxColour c = m_data.GetCustomColour(i);
a1b806b9 141 if (c.IsOk())
274ad000
VS
142 {
143 colors[n_colors] = *c.GetColor();
144 n_colors++;
145 }
146 }
147
b976323b 148 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
149
150 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 151 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
426d19f1 152#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
153}
154
155void wxColourDialog::DialogToColourData()
156{
b6dd7ed8
VZ
157#if wxUSE_LIBHILDON
158 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
159 const GdkColor * const clr = hildon_color_selector_get_color(sel);
160 if ( clr )
161 m_data.SetColour(*clr);
426d19f1
JS
162#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
163 const GdkColor * const
a1b806b9 164 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
426d19f1
JS
165
166 GdkColor clr;
167 if (col)
168 clr = *col;
169 else {
170 clr.pixel = 0;
171 clr.red = 32768;
172 clr.green = 32768;
173 clr.blue = 32768;
174 }
175 GdkColor new_color = clr;
176 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color);
177
178 m_data.SetColour(new_color);
179#else // !wxUSE_LIBHILDON2
180
385e8575
PC
181 GtkColorSelection* sel = GTK_COLOR_SELECTION(
182 gtk_color_selection_dialog_get_color_selection(
183 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000
VS
184
185 GdkColor clr;
186 gtk_color_selection_get_current_color(sel, &clr);
b6dd7ed8 187 m_data.SetColour(clr);
d0ee33f5 188
274ad000
VS
189 // Extract custom palette:
190
191 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
192 gchar *pal;
193 g_object_get(settings, "gtk-color-palette", &pal, NULL);
194
195 GdkColor *colors;
196 gint n_colors;
197 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
198 {
199 for (int i = 0; i < wxMin(n_colors, 16); i++)
200 {
b91fd704 201 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
202 }
203 g_free(colors);
204 }
d0ee33f5 205
274ad000 206 g_free(pal);
426d19f1 207#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
208}
209
ff654490 210#endif // wxUSE_COLOURDLG
274ad000 211