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