]> git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
revert removal of gtk1 code from common file
[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 #ifndef WX_PRECOMP
18 #include "wx/app.h"
19 #endif
20
21 #include "wx/gdicmn.h"
22
23 #include "wx/x11/private.h"
24
25 //-----------------------------------------------------------------------------
26 // wxColour
27 //-----------------------------------------------------------------------------
28
29 class wxColourRefData: public wxObjectRefData
30 {
31 public:
32 wxColourRefData()
33 {
34 m_color.red = 0;
35 m_color.green = 0;
36 m_color.blue = 0;
37 m_color.pixel = 0;
38 m_colormap = (WXColormap *) NULL;
39 m_hasPixel = false;
40 }
41 wxColourRefData(const wxColourRefData& data):
42 wxObjectRefData()
43 {
44 m_color = data.m_color;
45 m_colormap = data.m_colormap;
46 m_hasPixel = data.m_hasPixel;
47 }
48
49 ~wxColourRefData()
50 {
51 FreeColour();
52 }
53
54 bool operator == (const wxColourRefData& data) const
55 {
56 return (m_colormap == data.m_colormap &&
57 m_hasPixel == data.m_hasPixel &&
58 m_color.red == data.m_color.red &&
59 m_color.green == data.m_color.green &&
60 m_color.blue == data.m_color.blue &&
61 m_color.pixel == data.m_color.pixel);
62 }
63
64 void FreeColour();
65 void AllocColour( WXColormap cmap );
66
67 XColor m_color;
68 WXColormap m_colormap;
69 bool m_hasPixel;
70
71 friend class wxColour;
72
73 // reference counter for systems with <= 8-Bit display
74 static unsigned short colMapAllocCounter[ 256 ];
75 };
76
77 unsigned short wxColourRefData::colMapAllocCounter[ 256 ] =
78 {
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, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
92 };
93
94 void wxColourRefData::FreeColour()
95 {
96 if (!m_colormap)
97 return;
98 #if !wxUSE_NANOX
99 if ( wxTheApp &&
100 (wxTheApp->m_visualInfo->m_visualType == GrayScale ||
101 wxTheApp->m_visualInfo->m_visualType == PseudoColor) )
102 {
103 int idx = m_color.pixel;
104 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
105
106 if (colMapAllocCounter[ idx ] == 0)
107 {
108 unsigned long pixel = m_color.pixel;
109 XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 );
110 }
111 }
112 #endif
113 }
114
115 void wxColourRefData::AllocColour( WXColormap cmap )
116 {
117 if (m_hasPixel && (m_colormap == cmap))
118 return;
119
120 FreeColour();
121
122 #if !wxUSE_NANOX
123 if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
124 (wxTheApp->m_visualInfo->m_visualType == PseudoColor))
125 {
126 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
127 int idx = m_color.pixel;
128 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
129 }
130 else
131 #endif
132 {
133 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
134 }
135
136 m_colormap = cmap;
137 }
138
139 //-----------------------------------------------------------------------------
140
141 #define M_COLDATA ((wxColourRefData *)m_refData)
142
143 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
144
145 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
146
147 wxColour::~wxColour()
148 {
149 }
150
151 bool wxColour::operator == ( const wxColour& col ) const
152 {
153 if (m_refData == col.m_refData) return true;
154
155 if (!m_refData || !col.m_refData) return false;
156
157 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
158 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
159
160 return (own->red == other->red)
161 && (own->green == other->green)
162 && (own->blue == other->blue) ;
163
164 }
165
166 wxObjectRefData *wxColour::CreateRefData() const
167 {
168 return new wxColourRefData;
169 }
170
171 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
172 {
173 return new wxColourRefData(*(wxColourRefData *)data);
174 }
175
176 void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue )
177 {
178 AllocExclusive();
179
180 #if wxUSE_NANOX
181 M_COLDATA->m_color.red = ((unsigned short)red) ;
182 M_COLDATA->m_color.green = ((unsigned short)green) ;
183 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
184 #else
185 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
186 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
187 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
188 #endif
189 M_COLDATA->m_color.pixel = 0;
190 }
191
192 unsigned char wxColour::Red() const
193 {
194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
195
196 #if wxUSE_NANOX
197 return (unsigned char) M_COLDATA->m_color.red ;
198 #else
199 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
200 #endif
201 }
202
203 unsigned char wxColour::Green() const
204 {
205 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
206
207 #if wxUSE_NANOX
208 return (unsigned char) M_COLDATA->m_color.green ;
209 #else
210 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
211 #endif
212 }
213
214 unsigned char wxColour::Blue() const
215 {
216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
217
218 #if wxUSE_NANOX
219 return (unsigned char) M_COLDATA->m_color.blue ;
220 #else
221 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
222 #endif
223 }
224
225 void wxColour::CalcPixel( WXColormap cmap )
226 {
227 wxCHECK_RET( Ok(), wxT("invalid colour") );
228
229 wxCHECK_RET( cmap, wxT("invalid colormap") );
230
231 M_COLDATA->AllocColour( cmap );
232 }
233
234 unsigned long wxColour::GetPixel() const
235 {
236 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
237
238 return M_COLDATA->m_color.pixel;
239 }
240
241 WXColor *wxColour::GetColor() const
242 {
243 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
244
245 return (WXColor*) &M_COLDATA->m_color;
246 }
247
248 bool wxColour::FromString(const wxChar *name)
249 {
250 Display *dpy = wxGlobalDisplay();
251 WXColormap colormap = wxTheApp->GetMainColormap( dpy );
252 XColor xcol;
253 if ( XParseColor( dpy, (Colormap)colormap, name , &xcol ) )
254 {
255 UnRef();
256
257 m_refData = new wxColourRefData;
258 M_COLDATA->m_colormap = colormap;
259 M_COLDATA->m_color = xcol;
260 return true;
261 }
262
263 return wxColourBase::FromString(name);
264 }