]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
Remove code that has no effect
[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"
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{
643e9cf9
VS
86 WX_TESTING_SHOW_MODAL_HOOK();
87
274ad000 88 ColourDataToDialog();
d0ee33f5 89
274ad000 90 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 91 gtk_widget_hide(m_widget);
d0ee33f5 92
274ad000
VS
93 switch (result)
94 {
95 default:
9a83f860 96 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
274ad000
VS
97 // fall through
98
99 case GTK_RESPONSE_CANCEL:
100 case GTK_RESPONSE_DELETE_EVENT:
101 case GTK_RESPONSE_CLOSE:
102 return wxID_CANCEL;
d0ee33f5 103
274ad000
VS
104 case GTK_RESPONSE_OK:
105 DialogToColourData();
106 return wxID_OK;
d0ee33f5 107 }
274ad000
VS
108}
109
110void wxColourDialog::ColourDataToDialog()
111{
9dc44eff 112#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
b6dd7ed8 113 const GdkColor * const
a1b806b9 114 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
b6dd7ed8 115 : NULL;
9dc44eff 116#endif
b6dd7ed8
VZ
117#if wxUSE_LIBHILDON
118 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
5c33522f 119 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
426d19f1
JS
120#elif wxUSE_LIBHILDON2
121 GdkColor clr;
122 if (col)
123 clr = *col;
124 else {
125 clr.pixel = 0;
126 clr.red = 32768;
127 clr.green = 32768;
128 clr.blue = 32768;
129 }
130
131 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog *)m_widget, &clr);
132#else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
385e8575
PC
133 GtkColorSelection* sel = GTK_COLOR_SELECTION(
134 gtk_color_selection_dialog_get_color_selection(
135 GTK_COLOR_SELECTION_DIALOG(m_widget)));
d0ee33f5 136
7d1214cd
PC
137 const wxColour& color = m_data.GetColour();
138 if (color.IsOk())
9dc44eff
PC
139 {
140#ifdef __WXGTK3__
7d1214cd 141 gtk_color_selection_set_current_rgba(sel, color);
9dc44eff 142#else
7d1214cd 143 gtk_color_selection_set_current_color(sel, color.GetColor());
9dc44eff
PC
144#endif
145 }
274ad000
VS
146
147 // setup the palette:
148
9dc44eff 149 GdkColor colors[wxColourData::NUM_CUSTOM];
274ad000 150 gint n_colors = 0;
9dc44eff 151 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
274ad000
VS
152 {
153 wxColour c = m_data.GetCustomColour(i);
a1b806b9 154 if (c.IsOk())
274ad000
VS
155 {
156 colors[n_colors] = *c.GetColor();
157 n_colors++;
158 }
159 }
160
b976323b 161 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
162
163 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 164 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
426d19f1 165#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
166}
167
168void wxColourDialog::DialogToColourData()
169{
b6dd7ed8
VZ
170#if wxUSE_LIBHILDON
171 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
172 const GdkColor * const clr = hildon_color_selector_get_color(sel);
173 if ( clr )
174 m_data.SetColour(*clr);
426d19f1
JS
175#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
176 const GdkColor * const
a1b806b9 177 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
426d19f1
JS
178
179 GdkColor clr;
180 if (col)
181 clr = *col;
182 else {
183 clr.pixel = 0;
184 clr.red = 32768;
185 clr.green = 32768;
186 clr.blue = 32768;
187 }
188 GdkColor new_color = clr;
189 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color);
190
191 m_data.SetColour(new_color);
192#else // !wxUSE_LIBHILDON2
193
385e8575
PC
194 GtkColorSelection* sel = GTK_COLOR_SELECTION(
195 gtk_color_selection_dialog_get_color_selection(
196 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 197
9dc44eff
PC
198#ifdef __WXGTK3__
199 GdkRGBA clr;
200 gtk_color_selection_get_current_rgba(sel, &clr);
201#else
274ad000
VS
202 GdkColor clr;
203 gtk_color_selection_get_current_color(sel, &clr);
9dc44eff 204#endif
b6dd7ed8 205 m_data.SetColour(clr);
d0ee33f5 206
274ad000
VS
207 // Extract custom palette:
208
209 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
210 gchar *pal;
211 g_object_get(settings, "gtk-color-palette", &pal, NULL);
212
213 GdkColor *colors;
214 gint n_colors;
215 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
216 {
9dc44eff 217 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
274ad000 218 {
b91fd704 219 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
220 }
221 g_free(colors);
222 }
d0ee33f5 223
274ad000 224 g_free(pal);
426d19f1 225#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
226}
227
ff654490 228#endif // wxUSE_COLOURDLG
274ad000 229