]> git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
OSX Info.plist
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if !wxUSE_NANOX
96 if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
97 (wxTheApp->m_visualInfo->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 #endif
109 }
110
111 void wxColourRefData::AllocColour( WXColormap cmap )
112 {
113 if (m_hasPixel && (m_colormap == cmap))
114 return;
115
116 FreeColour();
117
118 #if !wxUSE_NANOX
119 if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
120 (wxTheApp->m_visualInfo->m_visualType == PseudoColor))
121 {
122 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
123 int idx = m_color.pixel;
124 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
125 }
126 else
127 #endif
128 {
129 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
130 }
131
132 m_colormap = cmap;
133 }
134
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 )
144 {
145 m_refData = new wxColourRefData();
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
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;
154 #endif
155 M_COLDATA->m_color.pixel = 0;
156 }
157
158 /* static */
159 wxColour wxColour::CreateByName(const wxString& name)
160 {
161 wxColour col;
162
163 Display *dpy = wxGlobalDisplay();
164 WXColormap colormap = wxTheApp->GetMainColormap( dpy );
165 XColor xcol;
166 if ( XParseColor( dpy, (Colormap)colormap, name.mb_str(), &xcol ) )
167 {
168 wxColourRefData *refData = new wxColourRefData;
169 refData->m_colormap = colormap;
170 refData->m_color = xcol;
171 col.m_refData = refData;
172 }
173
174 return col;
175 }
176
177 void wxColour::InitFromName( const wxString &colourName )
178 {
179 // check the cache first
180 wxColour col;
181 if ( wxTheColourDatabase )
182 {
183 col = wxTheColourDatabase->Find(colourName);
184 }
185
186 if ( !col.Ok() )
187 {
188 col = CreateByName(colourName);
189 }
190
191 if ( col.Ok() )
192 {
193 *this = col;
194 }
195 else
196 {
197 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
198 }
199 }
200
201 wxColour::~wxColour()
202 {
203 }
204
205 bool wxColour::operator == ( const wxColour& col ) const
206 {
207 if (m_refData == col.m_refData) return true;
208
209 if (!m_refData || !col.m_refData) return false;
210
211 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
212 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
213
214 return (own->red == other->red)
215 && (own->green == other->green)
216 && (own->blue == other->blue) ;
217
218 }
219
220 wxObjectRefData *wxColour::CreateRefData() const
221 {
222 return new wxColourRefData;
223 }
224
225 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
226 {
227 return new wxColourRefData(*(wxColourRefData *)data);
228 }
229
230 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
231 {
232 AllocExclusive();
233
234 m_refData = new wxColourRefData();
235 #if wxUSE_NANOX
236 M_COLDATA->m_color.red = ((unsigned short)red) ;
237 M_COLDATA->m_color.green = ((unsigned short)green) ;
238 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
239 #else
240 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
241 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
242 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
243 #endif
244 M_COLDATA->m_color.pixel = 0;
245 }
246
247 unsigned char wxColour::Red() const
248 {
249 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
250
251 #if wxUSE_NANOX
252 return (unsigned char) M_COLDATA->m_color.red ;
253 #else
254 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
255 #endif
256 }
257
258 unsigned char wxColour::Green() const
259 {
260 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
261
262 #if wxUSE_NANOX
263 return (unsigned char) M_COLDATA->m_color.green ;
264 #else
265 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
266 #endif
267 }
268
269 unsigned char wxColour::Blue() const
270 {
271 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
272
273 #if wxUSE_NANOX
274 return (unsigned char) M_COLDATA->m_color.blue ;
275 #else
276 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
277 #endif
278 }
279
280 void wxColour::CalcPixel( WXColormap cmap )
281 {
282 wxCHECK_RET( Ok(), wxT("invalid colour") );
283
284 wxCHECK_RET( cmap, wxT("invalid colormap") );
285
286 M_COLDATA->AllocColour( cmap );
287 }
288
289 unsigned long wxColour::GetPixel() const
290 {
291 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
292
293 return M_COLDATA->m_color.pixel;
294 }
295
296 WXColor *wxColour::GetColor() const
297 {
298 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
299
300 return (WXColor*) &M_COLDATA->m_color;
301 }