]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[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
9dc44eff 27#include <gtk/gtk.h>
274ad000 28#include "wx/gtk/private.h"
9dc44eff 29#include "wx/gtk/private/gtk2-compat.h"
274ad000 30
b6dd7ed8
VZ
31#if wxUSE_LIBHILDON
32 #include <hildon-widgets/hildon-color-selector.h>
33#endif // wxUSE_LIBHILDON
34
426d19f1
JS
35#if wxUSE_LIBHILDON2
36extern "C" {
37 #include <hildon/hildon.h>
38}
39#endif // wxUSE_LIBHILDON2
40
274ad000
VS
41IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
42
43wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
44{
45 Create(parent, data);
46}
47
48bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
49{
50 if (data)
51 m_data = *data;
52
cdc48273 53 m_parent = GetParentForModalDialog(parent, 0);
b6dd7ed8
VZ
54 GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
55 : NULL;
56
57#if wxUSE_LIBHILDON
58 m_widget = hildon_color_selector_new(parentGTK);
ce00f59b 59#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
426d19f1
JS
60 m_widget = hildon_color_chooser_dialog_new();
61#else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
62 wxString title(_("Choose colour"));
63 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
b6dd7ed8 64#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
d0ee33f5 65
9ff9d30c
PC
66 g_object_ref(m_widget);
67
b6dd7ed8 68 if ( parentGTK )
fa349e95 69 {
b6dd7ed8 70 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
fa349e95 71 }
d0ee33f5 72
426d19f1 73#if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
385e8575
PC
74 GtkColorSelection* sel = GTK_COLOR_SELECTION(
75 gtk_color_selection_dialog_get_color_selection(
76 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 77 gtk_color_selection_set_has_palette(sel, true);
426d19f1 78#endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
79
80 return true;
81}
d0ee33f5 82
274ad000
VS
83int wxColourDialog::ShowModal()
84{
85 ColourDataToDialog();
d0ee33f5 86
274ad000 87 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 88 gtk_widget_hide(m_widget);
d0ee33f5 89
274ad000
VS
90 switch (result)
91 {
92 default:
9a83f860 93 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
274ad000
VS
94 // fall through
95
96 case GTK_RESPONSE_CANCEL:
97 case GTK_RESPONSE_DELETE_EVENT:
98 case GTK_RESPONSE_CLOSE:
99 return wxID_CANCEL;
d0ee33f5 100
274ad000
VS
101 case GTK_RESPONSE_OK:
102 DialogToColourData();
103 return wxID_OK;
d0ee33f5 104 }
274ad000
VS
105}
106
107void wxColourDialog::ColourDataToDialog()
108{
9dc44eff 109#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
b6dd7ed8 110 const GdkColor * const
a1b806b9 111 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor()
b6dd7ed8 112 : NULL;
9dc44eff 113#endif
b6dd7ed8
VZ
114#if wxUSE_LIBHILDON
115 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
5c33522f 116 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
426d19f1
JS
117#elif wxUSE_LIBHILDON2
118 GdkColor clr;
119 if (col)
120 clr = *col;
121 else {
122 clr.pixel = 0;
123 clr.red = 32768;
124 clr.green = 32768;
125 clr.blue = 32768;
126 }
127
128 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog *)m_widget, &clr);
129#else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
385e8575
PC
130 GtkColorSelection* sel = GTK_COLOR_SELECTION(
131 gtk_color_selection_dialog_get_color_selection(
132 GTK_COLOR_SELECTION_DIALOG(m_widget)));
d0ee33f5 133
7d1214cd
PC
134 const wxColour& color = m_data.GetColour();
135 if (color.IsOk())
9dc44eff
PC
136 {
137#ifdef __WXGTK3__
7d1214cd 138 gtk_color_selection_set_current_rgba(sel, color);
9dc44eff 139#else
7d1214cd 140 gtk_color_selection_set_current_color(sel, color.GetColor());
9dc44eff
PC
141#endif
142 }
274ad000
VS
143
144 // setup the palette:
145
9dc44eff 146 GdkColor colors[wxColourData::NUM_CUSTOM];
274ad000 147 gint n_colors = 0;
9dc44eff 148 for (unsigned i = 0; i < WXSIZEOF(colors); i++)
274ad000
VS
149 {
150 wxColour c = m_data.GetCustomColour(i);
a1b806b9 151 if (c.IsOk())
274ad000
VS
152 {
153 colors[n_colors] = *c.GetColor();
154 n_colors++;
155 }
156 }
157
b976323b 158 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
159
160 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 161 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
426d19f1 162#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
163}
164
165void wxColourDialog::DialogToColourData()
166{
b6dd7ed8
VZ
167#if wxUSE_LIBHILDON
168 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
169 const GdkColor * const clr = hildon_color_selector_get_color(sel);
170 if ( clr )
171 m_data.SetColour(*clr);
426d19f1
JS
172#elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
173 const GdkColor * const
a1b806b9 174 col = m_data.GetColour().IsOk() ? m_data.GetColour().GetColor() : NULL;
426d19f1
JS
175
176 GdkColor clr;
177 if (col)
178 clr = *col;
179 else {
180 clr.pixel = 0;
181 clr.red = 32768;
182 clr.green = 32768;
183 clr.blue = 32768;
184 }
185 GdkColor new_color = clr;
186 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color);
187
188 m_data.SetColour(new_color);
189#else // !wxUSE_LIBHILDON2
190
385e8575
PC
191 GtkColorSelection* sel = GTK_COLOR_SELECTION(
192 gtk_color_selection_dialog_get_color_selection(
193 GTK_COLOR_SELECTION_DIALOG(m_widget)));
274ad000 194
9dc44eff
PC
195#ifdef __WXGTK3__
196 GdkRGBA clr;
197 gtk_color_selection_get_current_rgba(sel, &clr);
198#else
274ad000
VS
199 GdkColor clr;
200 gtk_color_selection_get_current_color(sel, &clr);
9dc44eff 201#endif
b6dd7ed8 202 m_data.SetColour(clr);
d0ee33f5 203
274ad000
VS
204 // Extract custom palette:
205
206 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
207 gchar *pal;
208 g_object_get(settings, "gtk-color-palette", &pal, NULL);
209
210 GdkColor *colors;
211 gint n_colors;
212 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
213 {
9dc44eff 214 for (int i = 0; i < n_colors && i < wxColourData::NUM_CUSTOM; i++)
274ad000 215 {
b91fd704 216 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
217 }
218 g_free(colors);
219 }
d0ee33f5 220
274ad000 221 g_free(pal);
426d19f1 222#endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
274ad000
VS
223}
224
ff654490 225#endif // wxUSE_COLOURDLG
274ad000 226