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