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