Build fix after wxColourBase introduction.
[wxWidgets.git] / src / x11 / colour.cpp
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/colour.h"
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 &&
97 (wxTheApp->m_visualInfo->m_visualType == GrayScale ||
98 wxTheApp->m_visualInfo->m_visualType == PseudoColor) )
99 {
100 int idx = m_color.pixel;
101 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
102
103 if (colMapAllocCounter[ idx ] == 0)
104 {
105 unsigned long pixel = m_color.pixel;
106 XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 );
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 !wxUSE_NANOX
120 if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
121 (wxTheApp->m_visualInfo->m_visualType == PseudoColor))
122 {
123 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
124 int idx = m_color.pixel;
125 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
126 }
127 else
128 #endif
129 {
130 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
131 }
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()
145 {
146 }
147
148 bool wxColour::operator == ( const wxColour& col ) const
149 {
150 if (m_refData == col.m_refData) return true;
151
152 if (!m_refData || !col.m_refData) return false;
153
154 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
155 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
156
157 return (own->red == other->red)
158 && (own->green == other->green)
159 && (own->blue == other->blue) ;
160
161 }
162
163 wxObjectRefData *wxColour::CreateRefData() const
164 {
165 return new wxColourRefData;
166 }
167
168 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
169 {
170 return new wxColourRefData(*(wxColourRefData *)data);
171 }
172
173 void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue )
174 {
175 AllocExclusive();
176
177 #if wxUSE_NANOX
178 M_COLDATA->m_color.red = ((unsigned short)red) ;
179 M_COLDATA->m_color.green = ((unsigned short)green) ;
180 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
181 #else
182 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
183 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
184 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
185 #endif
186 M_COLDATA->m_color.pixel = 0;
187 }
188
189 unsigned char wxColour::Red() const
190 {
191 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
192
193 #if wxUSE_NANOX
194 return (unsigned char) M_COLDATA->m_color.red ;
195 #else
196 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
197 #endif
198 }
199
200 unsigned char wxColour::Green() const
201 {
202 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
203
204 #if wxUSE_NANOX
205 return (unsigned char) M_COLDATA->m_color.green ;
206 #else
207 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
208 #endif
209 }
210
211 unsigned char wxColour::Blue() const
212 {
213 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
214
215 #if wxUSE_NANOX
216 return (unsigned char) M_COLDATA->m_color.blue ;
217 #else
218 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
219 #endif
220 }
221
222 void wxColour::CalcPixel( WXColormap cmap )
223 {
224 wxCHECK_RET( Ok(), wxT("invalid colour") );
225
226 wxCHECK_RET( cmap, wxT("invalid colormap") );
227
228 M_COLDATA->AllocColour( cmap );
229 }
230
231 unsigned long wxColour::GetPixel() const
232 {
233 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
234
235 return M_COLDATA->m_color.pixel;
236 }
237
238 WXColor *wxColour::GetColor() const
239 {
240 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
241
242 return (WXColor*) &M_COLDATA->m_color;
243 }
244
245 bool wxColour::FromString(const wxChar *name)
246 {
247 Display *dpy = wxGlobalDisplay();
248 WXColormap colormap = wxTheApp->GetMainColormap( dpy );
249 XColor xcol;
250 if ( XParseColor( dpy, (Colormap)colormap, name , &xcol ) )
251 {
252 UnRef();
253
254 m_refData = new wxColourRefData;
255 M_COLDATA->m_colormap = colormap;
256 M_COLDATA->m_color = xcol;
257 return true;
258 }
259
260 return wxColourBase::FromString(name);
261 }