]>
Commit | Line | Data |
---|---|---|
83df96d6 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: colour.cpp | |
3 | // Purpose: wxColour class | |
3cd0b8c5 | 4 | // Author: Julian Smart, Robert Roebling |
83df96d6 JS |
5 | // Modified by: |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
3cd0b8c5 | 8 | // Copyright: (c) Julian Smart, Robert Roebling |
83df96d6 JS |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
83df96d6 | 12 | |
14f355c2 | 13 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
83df96d6 JS |
14 | #pragma implementation "colour.h" |
15 | #endif | |
16 | ||
17 | #include "wx/gdicmn.h" | |
887dd52f | 18 | #include "wx/app.h" |
83df96d6 | 19 | |
7266b672 | 20 | #include "wx/x11/private.h" |
83df96d6 | 21 | |
3cd0b8c5 RR |
22 | //----------------------------------------------------------------------------- |
23 | // wxColour | |
24 | //----------------------------------------------------------------------------- | |
83df96d6 | 25 | |
3cd0b8c5 | 26 | class wxColourRefData: public wxObjectRefData |
83df96d6 | 27 | { |
3cd0b8c5 RR |
28 | public: |
29 | wxColourRefData() | |
30 | { | |
31 | m_color.red = 0; | |
32 | m_color.green = 0; | |
33 | m_color.blue = 0; | |
34 | m_color.pixel = 0; | |
35 | m_colormap = (WXColormap *) NULL; | |
36 | m_hasPixel = FALSE; | |
37 | } | |
d5cf34f4 JS |
38 | wxColourRefData(const wxColourRefData& data): |
39 | wxObjectRefData() | |
40 | { | |
41 | m_color = data.m_color; | |
42 | m_colormap = data.m_colormap; | |
43 | m_hasPixel = data.m_hasPixel; | |
44 | } | |
3cd0b8c5 RR |
45 | |
46 | ~wxColourRefData() | |
47 | { | |
48 | FreeColour(); | |
49 | } | |
83df96d6 | 50 | |
3cd0b8c5 RR |
51 | bool operator == (const wxColourRefData& data) const |
52 | { | |
53 | return (m_colormap == data.m_colormap && | |
54 | m_hasPixel == data.m_hasPixel && | |
55 | m_color.red == data.m_color.red && | |
56 | m_color.green == data.m_color.green && | |
57 | m_color.blue == data.m_color.blue && | |
58 | m_color.pixel == data.m_color.pixel); | |
59 | } | |
60 | ||
61 | void FreeColour(); | |
62 | void AllocColour( WXColormap cmap ); | |
63 | ||
64 | XColor m_color; | |
65 | WXColormap m_colormap; | |
66 | bool m_hasPixel; | |
67 | ||
68 | friend class wxColour; | |
69 | ||
70 | // reference counter for systems with <= 8-Bit display | |
71 | static unsigned short colMapAllocCounter[ 256 ]; | |
72 | }; | |
73 | ||
74 | unsigned short wxColourRefData::colMapAllocCounter[ 256 ] = | |
75 | { | |
76 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
77 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
78 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
80 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
81 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
82 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
83 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
84 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
85 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
88 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
89 | }; | |
90 | ||
91 | void wxColourRefData::FreeColour() | |
83df96d6 | 92 | { |
dc4025af RR |
93 | if (!m_colormap) |
94 | return; | |
8601b2e1 | 95 | #if !wxUSE_NANOX |
9ce8d6a2 MB |
96 | if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) || |
97 | (wxTheApp->m_visualInfo->m_visualType == PseudoColor)) | |
3cd0b8c5 | 98 | { |
dc4025af RR |
99 | int idx = m_color.pixel; |
100 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
3cd0b8c5 | 101 | |
dc4025af RR |
102 | if (colMapAllocCounter[ idx ] == 0) |
103 | { | |
104 | unsigned long pixel = m_color.pixel; | |
105 | XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 ); | |
3cd0b8c5 | 106 | } |
3cd0b8c5 | 107 | } |
8601b2e1 | 108 | #endif |
83df96d6 JS |
109 | } |
110 | ||
3cd0b8c5 | 111 | void wxColourRefData::AllocColour( WXColormap cmap ) |
83df96d6 | 112 | { |
3cd0b8c5 RR |
113 | if (m_hasPixel && (m_colormap == cmap)) |
114 | return; | |
115 | ||
116 | FreeColour(); | |
117 | ||
8601b2e1 | 118 | #if !wxUSE_NANOX |
9ce8d6a2 MB |
119 | if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) || |
120 | (wxTheApp->m_visualInfo->m_visualType == PseudoColor)) | |
3cd0b8c5 | 121 | { |
dc4025af | 122 | m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color ); |
3cd0b8c5 RR |
123 | int idx = m_color.pixel; |
124 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1; | |
125 | } | |
126 | else | |
8601b2e1 | 127 | #endif |
3cd0b8c5 RR |
128 | { |
129 | m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color ); | |
130 | } | |
dc4025af | 131 | |
3cd0b8c5 | 132 | m_colormap = cmap; |
83df96d6 JS |
133 | } |
134 | ||
3cd0b8c5 RR |
135 | //----------------------------------------------------------------------------- |
136 | ||
137 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
138 | ||
139 | #define SHIFT (8*(sizeof(short int)-sizeof(char))) | |
140 | ||
141 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
142 | ||
143 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) | |
83df96d6 | 144 | { |
3cd0b8c5 | 145 | m_refData = new wxColourRefData(); |
0b5c0e1a JS |
146 | #if wxUSE_NANOX |
147 | M_COLDATA->m_color.red = ((unsigned short)red) ; | |
148 | M_COLDATA->m_color.green = ((unsigned short)green) ; | |
149 | M_COLDATA->m_color.blue = ((unsigned short)blue) ; | |
150 | #else | |
3cd0b8c5 RR |
151 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; |
152 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
153 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
0b5c0e1a | 154 | #endif |
3cd0b8c5 | 155 | M_COLDATA->m_color.pixel = 0; |
83df96d6 JS |
156 | } |
157 | ||
3cd0b8c5 | 158 | void wxColour::InitFromName( const wxString &colourName ) |
83df96d6 | 159 | { |
222ed1d6 MB |
160 | wxColour* col; |
161 | if ( (wxTheColourDatabase) && (col = wxTheColourDatabase->FindColourNoAdd(colourName)) ) | |
83df96d6 | 162 | { |
3cd0b8c5 RR |
163 | UnRef(); |
164 | if (col) Ref( *col ); | |
83df96d6 JS |
165 | } |
166 | else | |
167 | { | |
3cd0b8c5 | 168 | m_refData = new wxColourRefData(); |
887dd52f RR |
169 | |
170 | M_COLDATA->m_colormap = wxTheApp->GetMainColormap( wxGlobalDisplay() ); | |
171 | ||
3cd0b8c5 RR |
172 | if (!XParseColor( wxGlobalDisplay(), (Colormap) M_COLDATA->m_colormap, colourName.mb_str(), &M_COLDATA->m_color )) |
173 | { | |
174 | // VZ: asserts are good in general but this one is triggered by | |
175 | // calling wxColourDatabase::FindColour() with an | |
176 | // unrecognized colour name and this can't be avoided from the | |
177 | // user code, so don't give it here | |
178 | // | |
179 | // a better solution would be to changed code in FindColour() | |
180 | ||
181 | //wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); | |
182 | ||
183 | delete m_refData; | |
184 | m_refData = (wxObjectRefData *) NULL; | |
185 | } | |
83df96d6 JS |
186 | } |
187 | } | |
188 | ||
3cd0b8c5 | 189 | wxColour::~wxColour() |
83df96d6 JS |
190 | { |
191 | } | |
192 | ||
3cd0b8c5 | 193 | bool wxColour::operator == ( const wxColour& col ) const |
83df96d6 | 194 | { |
3cd0b8c5 RR |
195 | if (m_refData == col.m_refData) return TRUE; |
196 | ||
197 | if (!m_refData || !col.m_refData) return FALSE; | |
198 | ||
199 | XColor *own = &(((wxColourRefData*)m_refData)->m_color); | |
200 | XColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
201 | if (own->red != other->red) return FALSE; | |
202 | if (own->blue != other->blue) return FALSE; | |
203 | if (own->green != other->green) return FALSE; | |
204 | ||
205 | return TRUE; | |
83df96d6 JS |
206 | } |
207 | ||
3cd0b8c5 RR |
208 | wxObjectRefData *wxColour::CreateRefData() const |
209 | { | |
210 | return new wxColourRefData; | |
211 | } | |
83df96d6 | 212 | |
3cd0b8c5 RR |
213 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const |
214 | { | |
215 | return new wxColourRefData(*(wxColourRefData *)data); | |
216 | } | |
83df96d6 | 217 | |
3cd0b8c5 | 218 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
83df96d6 | 219 | { |
3cd0b8c5 | 220 | AllocExclusive(); |
83df96d6 | 221 | |
3cd0b8c5 | 222 | m_refData = new wxColourRefData(); |
0b5c0e1a JS |
223 | #if wxUSE_NANOX |
224 | M_COLDATA->m_color.red = ((unsigned short)red) ; | |
225 | M_COLDATA->m_color.green = ((unsigned short)green) ; | |
226 | M_COLDATA->m_color.blue = ((unsigned short)blue) ; | |
227 | #else | |
3cd0b8c5 RR |
228 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; |
229 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
230 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
0b5c0e1a | 231 | #endif |
3cd0b8c5 | 232 | M_COLDATA->m_color.pixel = 0; |
83df96d6 JS |
233 | } |
234 | ||
3cd0b8c5 RR |
235 | unsigned char wxColour::Red() const |
236 | { | |
237 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
238 | ||
0b5c0e1a JS |
239 | #if wxUSE_NANOX |
240 | return (unsigned char) M_COLDATA->m_color.red ; | |
241 | #else | |
3cd0b8c5 | 242 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
0b5c0e1a | 243 | #endif |
3cd0b8c5 RR |
244 | } |
245 | ||
246 | unsigned char wxColour::Green() const | |
247 | { | |
248 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
249 | ||
0b5c0e1a JS |
250 | #if wxUSE_NANOX |
251 | return (unsigned char) M_COLDATA->m_color.green ; | |
252 | #else | |
3cd0b8c5 | 253 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
0b5c0e1a | 254 | #endif |
3cd0b8c5 RR |
255 | } |
256 | ||
257 | unsigned char wxColour::Blue() const | |
258 | { | |
259 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
260 | ||
0b5c0e1a JS |
261 | #if wxUSE_NANOX |
262 | return (unsigned char) M_COLDATA->m_color.blue ; | |
263 | #else | |
3cd0b8c5 | 264 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
0b5c0e1a | 265 | #endif |
3cd0b8c5 RR |
266 | } |
267 | ||
268 | void wxColour::CalcPixel( WXColormap cmap ) | |
269 | { | |
887dd52f RR |
270 | wxCHECK_RET( Ok(), wxT("invalid colour") ); |
271 | ||
272 | wxCHECK_RET( cmap, wxT("invalid colormap") ); | |
3cd0b8c5 RR |
273 | |
274 | M_COLDATA->AllocColour( cmap ); | |
275 | } | |
276 | ||
277 | unsigned long wxColour::GetPixel() const | |
278 | { | |
279 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
280 | ||
281 | return M_COLDATA->m_color.pixel; | |
282 | } | |
283 | ||
284 | WXColor *wxColour::GetColor() const | |
285 | { | |
286 | wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") ); | |
287 | ||
288 | return (WXColor*) &M_COLDATA->m_color; | |
83df96d6 | 289 | } |