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