]> git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
Compile fix for MinGW32
[wxWidgets.git] / src / x11 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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
13 #ifdef __GNUG__
14 #pragma implementation "colour.h"
15 #endif
16
17 #include "wx/gdicmn.h"
18 #include "wx/app.h"
19
20 #include "wx/x11/private.h"
21
22 //-----------------------------------------------------------------------------
23 // wxColour
24 //-----------------------------------------------------------------------------
25
26 class wxColourRefData: public wxObjectRefData
27 {
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 }
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 }
45
46 ~wxColourRefData()
47 {
48 FreeColour();
49 }
50
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()
92 {
93 if (!m_colormap)
94 return;
95
96 if ((wxTheApp->m_visualType == GrayScale) ||
97 (wxTheApp->m_visualType == PseudoColor))
98 {
99 int idx = m_color.pixel;
100 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
101
102 if (colMapAllocCounter[ idx ] == 0)
103 {
104 unsigned long pixel = m_color.pixel;
105 XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 );
106 }
107 }
108 }
109
110 void wxColourRefData::AllocColour( WXColormap cmap )
111 {
112 if (m_hasPixel && (m_colormap == cmap))
113 return;
114
115 FreeColour();
116
117 if ((wxTheApp->m_visualType == GrayScale) ||
118 (wxTheApp->m_visualType == PseudoColor))
119 {
120 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
121 int idx = m_color.pixel;
122 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
123 }
124 else
125 {
126 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
127 }
128
129 m_colormap = cmap;
130 }
131
132 //-----------------------------------------------------------------------------
133
134 #define M_COLDATA ((wxColourRefData *)m_refData)
135
136 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
137
138 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
139
140 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
141 {
142 m_refData = new wxColourRefData();
143 #if wxUSE_NANOX
144 M_COLDATA->m_color.red = ((unsigned short)red) ;
145 M_COLDATA->m_color.green = ((unsigned short)green) ;
146 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
147 #else
148 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
149 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
150 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
151 #endif
152 M_COLDATA->m_color.pixel = 0;
153 }
154
155 void wxColour::InitFromName( const wxString &colourName )
156 {
157 wxNode *node = (wxNode *) NULL;
158 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
159 {
160 wxColour *col = (wxColour*)node->Data();
161 UnRef();
162 if (col) Ref( *col );
163 }
164 else
165 {
166 m_refData = new wxColourRefData();
167
168 M_COLDATA->m_colormap = wxTheApp->GetMainColormap( wxGlobalDisplay() );
169
170 if (!XParseColor( wxGlobalDisplay(), (Colormap) M_COLDATA->m_colormap, colourName.mb_str(), &M_COLDATA->m_color ))
171 {
172 // VZ: asserts are good in general but this one is triggered by
173 // calling wxColourDatabase::FindColour() with an
174 // unrecognized colour name and this can't be avoided from the
175 // user code, so don't give it here
176 //
177 // a better solution would be to changed code in FindColour()
178
179 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
180
181 delete m_refData;
182 m_refData = (wxObjectRefData *) NULL;
183 }
184 }
185 }
186
187 wxColour::~wxColour()
188 {
189 }
190
191 bool wxColour::operator == ( const wxColour& col ) const
192 {
193 if (m_refData == col.m_refData) return TRUE;
194
195 if (!m_refData || !col.m_refData) return FALSE;
196
197 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
198 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
199 if (own->red != other->red) return FALSE;
200 if (own->blue != other->blue) return FALSE;
201 if (own->green != other->green) return FALSE;
202
203 return TRUE;
204 }
205
206 wxObjectRefData *wxColour::CreateRefData() const
207 {
208 return new wxColourRefData;
209 }
210
211 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
212 {
213 return new wxColourRefData(*(wxColourRefData *)data);
214 }
215
216 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
217 {
218 AllocExclusive();
219
220 m_refData = new wxColourRefData();
221 #if wxUSE_NANOX
222 M_COLDATA->m_color.red = ((unsigned short)red) ;
223 M_COLDATA->m_color.green = ((unsigned short)green) ;
224 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
225 #else
226 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
227 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
228 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
229 #endif
230 M_COLDATA->m_color.pixel = 0;
231 }
232
233 unsigned char wxColour::Red() const
234 {
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
236
237 #if wxUSE_NANOX
238 return (unsigned char) M_COLDATA->m_color.red ;
239 #else
240 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
241 #endif
242 }
243
244 unsigned char wxColour::Green() const
245 {
246 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
247
248 #if wxUSE_NANOX
249 return (unsigned char) M_COLDATA->m_color.green ;
250 #else
251 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
252 #endif
253 }
254
255 unsigned char wxColour::Blue() const
256 {
257 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
258
259 #if wxUSE_NANOX
260 return (unsigned char) M_COLDATA->m_color.blue ;
261 #else
262 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
263 #endif
264 }
265
266 void wxColour::CalcPixel( WXColormap cmap )
267 {
268 wxCHECK_RET( Ok(), wxT("invalid colour") );
269
270 wxCHECK_RET( cmap, wxT("invalid colormap") );
271
272 M_COLDATA->AllocColour( cmap );
273 }
274
275 unsigned long wxColour::GetPixel() const
276 {
277 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
278
279 return M_COLDATA->m_color.pixel;
280 }
281
282 WXColor *wxColour::GetColor() const
283 {
284 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
285
286 return (WXColor*) &M_COLDATA->m_color;
287 }