]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
edc536d3 | 2 | // Name: src/gtk/colour.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
ed39ff57 | 12 | #include "wx/colour.h" |
40989e46 | 13 | |
071a2d78 | 14 | #include <gdk/gdk.h> |
9dc44eff | 15 | #include "wx/gtk/private.h" |
c801d85f KB |
16 | |
17 | //----------------------------------------------------------------------------- | |
18 | // wxColour | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
8f884a0d | 21 | class wxColourRefData : public wxGDIRefData |
c801d85f | 22 | { |
47c93b63 | 23 | public: |
9dc44eff PC |
24 | #ifdef __WXGTK3__ |
25 | wxColourRefData(const GdkRGBA& gdkRGBA) | |
26 | : m_gdkRGBA(gdkRGBA) | |
27 | { | |
28 | m_gdkColor.red = guint16(gdkRGBA.red * 65535); | |
29 | m_gdkColor.green = guint16(gdkRGBA.green * 65535); | |
30 | m_gdkColor.blue = guint16(gdkRGBA.blue * 65535); | |
31 | m_alpha = wxByte(gdkRGBA.alpha * 255 + 0.5); | |
32 | } | |
33 | wxColourRefData(const GdkColor& gdkColor) | |
34 | : m_gdkColor(gdkColor) | |
35 | { | |
36 | m_gdkRGBA.red = gdkColor.red / 65535.0; | |
37 | m_gdkRGBA.green = gdkColor.green / 65535.0; | |
38 | m_gdkRGBA.blue = gdkColor.blue / 65535.0; | |
39 | m_gdkRGBA.alpha = 1; | |
40 | m_alpha = 255; | |
41 | } | |
42 | wxColourRefData(guchar red, guchar green, guchar blue, guchar alpha) | |
43 | { | |
44 | m_gdkRGBA.red = red / 255.0; | |
45 | m_gdkRGBA.green = green / 255.0; | |
46 | m_gdkRGBA.blue = blue / 255.0; | |
47 | m_gdkRGBA.alpha = alpha / 255.0; | |
48 | m_gdkColor.red = (guint16(red) << 8) + red; | |
49 | m_gdkColor.green = (guint16(green) << 8) + green; | |
50 | m_gdkColor.blue = (guint16(blue) << 8) + blue; | |
51 | m_alpha = alpha; | |
52 | } | |
53 | GdkRGBA m_gdkRGBA; | |
54 | GdkColor m_gdkColor; | |
55 | #else | |
3bbc1d95 | 56 | wxColourRefData(guint16 red, guint16 green, guint16 blue, wxByte alpha = 0xff) |
c89f5c02 | 57 | { |
fa21f438 | 58 | m_color.red = |
c6685317 | 59 | m_red = red; |
fa21f438 | 60 | m_color.green = |
c6685317 | 61 | m_green = green; |
fa21f438 | 62 | m_color.blue = |
c6685317 | 63 | m_blue = blue; |
dc888336 | 64 | m_alpha = alpha; |
c89f5c02 | 65 | m_color.pixel = 0; |
c4fa282c | 66 | m_colormap = NULL; |
dbb846c6 VZ |
67 | } |
68 | ||
d3c7fc99 | 69 | virtual ~wxColourRefData() |
c89f5c02 RR |
70 | { |
71 | FreeColour(); | |
72 | } | |
15248e43 | 73 | |
68dda785 | 74 | void FreeColour(); |
d13fd3e4 | 75 | void AllocColour( GdkColormap* cmap ); |
8bbe427f | 76 | |
c801d85f KB |
77 | GdkColor m_color; |
78 | GdkColormap *m_colormap; | |
c6685317 PC |
79 | // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies |
80 | guint16 m_red; | |
81 | guint16 m_green; | |
82 | guint16 m_blue; | |
9dc44eff | 83 | #endif |
3bbc1d95 | 84 | wxByte m_alpha; |
c6685317 | 85 | |
c0c133e1 | 86 | wxDECLARE_NO_COPY_CLASS(wxColourRefData); |
c801d85f KB |
87 | }; |
88 | ||
9dc44eff | 89 | #ifndef __WXGTK3__ |
68dda785 | 90 | void wxColourRefData::FreeColour() |
c801d85f | 91 | { |
c6685317 | 92 | if (m_colormap) |
47c93b63 | 93 | { |
c4fa282c | 94 | gdk_colormap_free_colors(m_colormap, &m_color, 1); |
c6685317 PC |
95 | m_colormap = NULL; |
96 | m_color.pixel = 0; | |
47c93b63 | 97 | } |
ff7b1510 | 98 | } |
c801d85f | 99 | |
d13fd3e4 VZ |
100 | void wxColourRefData::AllocColour( GdkColormap *cmap ) |
101 | { | |
c6685317 PC |
102 | if (m_colormap != cmap) |
103 | { | |
104 | FreeColour(); | |
4e6b8309 | 105 | |
c6685317 PC |
106 | m_color.red = m_red; |
107 | m_color.green = m_green; | |
108 | m_color.blue = m_blue; | |
109 | if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE)) | |
110 | { | |
111 | m_colormap = cmap; | |
112 | } | |
113 | } | |
d13fd3e4 | 114 | } |
9dc44eff | 115 | #endif |
d13fd3e4 | 116 | |
c801d85f KB |
117 | //----------------------------------------------------------------------------- |
118 | ||
5c33522f | 119 | #define M_COLDATA static_cast<wxColourRefData*>(m_refData) |
c801d85f | 120 | |
c6685317 | 121 | // GDK's values are in 0..65535 range, ours are in 0..255 |
3b9faf4c | 122 | #define SHIFT 8 |
c801d85f | 123 | |
9dc44eff PC |
124 | #ifdef __WXGTK3__ |
125 | wxColour::wxColour(const GdkRGBA& gdkRGBA) | |
126 | { | |
127 | m_refData = new wxColourRefData(gdkRGBA); | |
128 | } | |
129 | ||
130 | wxColour::wxColour(const GdkColor& gdkColor) | |
131 | { | |
132 | m_refData = new wxColourRefData(gdkColor); | |
133 | } | |
134 | #else | |
c6685317 PC |
135 | wxColour::wxColour(const GdkColor& gdkColor) |
136 | { | |
3bbc1d95 | 137 | m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue); |
c6685317 | 138 | } |
9dc44eff | 139 | #endif |
c6685317 | 140 | |
68dda785 | 141 | wxColour::~wxColour() |
c801d85f | 142 | { |
ff7b1510 | 143 | } |
c801d85f | 144 | |
33b64e6f | 145 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 146 | { |
4e6b8309 | 147 | if (m_refData == col.m_refData) |
aad6765c | 148 | return true; |
15248e43 | 149 | |
4e6b8309 | 150 | if (!m_refData || !col.m_refData) |
aad6765c | 151 | return false; |
15248e43 | 152 | |
c6685317 | 153 | wxColourRefData* refData = M_COLDATA; |
5c33522f | 154 | wxColourRefData* that_refData = static_cast<wxColourRefData*>(col.m_refData); |
9dc44eff PC |
155 | #ifdef __WXGTK3__ |
156 | return refData->m_gdkColor.red == that_refData->m_gdkColor.red && | |
157 | refData->m_gdkColor.green == that_refData->m_gdkColor.green && | |
158 | refData->m_gdkColor.blue == that_refData->m_gdkColor.blue && | |
159 | #else | |
c6685317 PC |
160 | return refData->m_red == that_refData->m_red && |
161 | refData->m_green == that_refData->m_green && | |
dc888336 | 162 | refData->m_blue == that_refData->m_blue && |
9dc44eff | 163 | #endif |
dc888336 | 164 | refData->m_alpha == that_refData->m_alpha; |
ff7b1510 | 165 | } |
c801d85f | 166 | |
aea95b1c | 167 | void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue, |
dc888336 | 168 | unsigned char alpha) |
c801d85f | 169 | { |
c6685317 | 170 | UnRef(); |
4e6b8309 | 171 | |
9dc44eff PC |
172 | #ifdef __WXGTK3__ |
173 | m_refData = new wxColourRefData(red, green, blue, alpha); | |
174 | #else | |
c6685317 PC |
175 | m_refData = new wxColourRefData( |
176 | (guint16(red) << SHIFT) + red, | |
177 | (guint16(green) << SHIFT) + green, | |
dc888336 | 178 | (guint16(blue) << SHIFT) + blue, |
3bbc1d95 | 179 | alpha); |
9dc44eff | 180 | #endif |
ff7b1510 | 181 | } |
c801d85f | 182 | |
68dda785 | 183 | unsigned char wxColour::Red() const |
c801d85f | 184 | { |
a1b806b9 | 185 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 186 | |
9dc44eff PC |
187 | #ifdef __WXGTK3__ |
188 | return wxByte(M_COLDATA->m_gdkColor.red >> 8); | |
189 | #else | |
c6685317 | 190 | return wxByte(M_COLDATA->m_red >> SHIFT); |
9dc44eff | 191 | #endif |
ff7b1510 | 192 | } |
c801d85f | 193 | |
68dda785 | 194 | unsigned char wxColour::Green() const |
c801d85f | 195 | { |
a1b806b9 | 196 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 197 | |
9dc44eff PC |
198 | #ifdef __WXGTK3__ |
199 | return wxByte(M_COLDATA->m_gdkColor.green >> 8); | |
200 | #else | |
c6685317 | 201 | return wxByte(M_COLDATA->m_green >> SHIFT); |
9dc44eff | 202 | #endif |
ff7b1510 | 203 | } |
c801d85f | 204 | |
68dda785 | 205 | unsigned char wxColour::Blue() const |
c801d85f | 206 | { |
a1b806b9 | 207 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 208 | |
9dc44eff PC |
209 | #ifdef __WXGTK3__ |
210 | return wxByte(M_COLDATA->m_gdkColor.blue >> 8); | |
211 | #else | |
c6685317 | 212 | return wxByte(M_COLDATA->m_blue >> SHIFT); |
9dc44eff | 213 | #endif |
ff7b1510 | 214 | } |
c801d85f | 215 | |
dc888336 SC |
216 | unsigned char wxColour::Alpha() const |
217 | { | |
a1b806b9 | 218 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
dc888336 | 219 | |
3bbc1d95 | 220 | return M_COLDATA->m_alpha; |
dc888336 SC |
221 | } |
222 | ||
9dc44eff | 223 | #ifndef __WXGTK3__ |
c801d85f KB |
224 | void wxColour::CalcPixel( GdkColormap *cmap ) |
225 | { | |
a1b806b9 | 226 | if (!IsOk()) return; |
8bbe427f | 227 | |
d13fd3e4 | 228 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 229 | } |
c801d85f | 230 | |
68dda785 | 231 | int wxColour::GetPixel() const |
c801d85f | 232 | { |
a1b806b9 | 233 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 234 | |
1ecc4d80 | 235 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 236 | } |
9dc44eff | 237 | #endif |
c801d85f | 238 | |
c6685317 | 239 | const GdkColor *wxColour::GetColor() const |
c801d85f | 240 | { |
a1b806b9 | 241 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") ); |
8bbe427f | 242 | |
9dc44eff PC |
243 | #ifdef __WXGTK3__ |
244 | return &M_COLDATA->m_gdkColor; | |
245 | #else | |
1ecc4d80 | 246 | return &M_COLDATA->m_color; |
9dc44eff | 247 | #endif |
ff7b1510 | 248 | } |
40989e46 | 249 | |
9dc44eff PC |
250 | #ifdef __WXGTK3__ |
251 | wxColour::operator const GdkRGBA*() const | |
252 | { | |
253 | const GdkRGBA* c = NULL; | |
254 | if (IsOk()) | |
255 | c = &M_COLDATA->m_gdkRGBA; | |
256 | return c; | |
257 | } | |
258 | #endif | |
259 | ||
e86d4e59 | 260 | bool wxColour::FromString(const wxString& str) |
40989e46 | 261 | { |
9dc44eff PC |
262 | #ifdef __WXGTK3__ |
263 | GdkRGBA gdkRGBA; | |
264 | if (gdk_rgba_parse(&gdkRGBA, wxGTK_CONV_SYS(str))) | |
265 | { | |
266 | *this = wxColour(gdkRGBA); | |
267 | return true; | |
268 | } | |
269 | #else | |
40989e46 WS |
270 | GdkColor colGDK; |
271 | if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) | |
272 | { | |
c6685317 | 273 | *this = wxColour(colGDK); |
40989e46 WS |
274 | return true; |
275 | } | |
9dc44eff | 276 | #endif |
40989e46 WS |
277 | |
278 | return wxColourBase::FromString(str); | |
279 | } |