]>
Commit | Line | Data |
---|---|---|
83df96d6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
edc536d3 | 2 | // Name: src/x11/colour.cpp |
83df96d6 | 3 | // Purpose: wxColour class |
3cd0b8c5 | 4 | // Author: Julian Smart, Robert Roebling |
83df96d6 JS |
5 | // Modified by: |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
3cd0b8c5 | 8 | // Copyright: (c) Julian Smart, Robert Roebling |
edc536d3 | 9 | // Licence: wxWindows licence |
83df96d6 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
40989e46 WS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/colour.h" | |
16 | ||
670f9935 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/app.h" | |
19 | #endif | |
20 | ||
83df96d6 | 21 | #include "wx/gdicmn.h" |
83df96d6 | 22 | |
7266b672 | 23 | #include "wx/x11/private.h" |
83df96d6 | 24 | |
3cd0b8c5 RR |
25 | //----------------------------------------------------------------------------- |
26 | // wxColour | |
27 | //----------------------------------------------------------------------------- | |
83df96d6 | 28 | |
3cd0b8c5 | 29 | class wxColourRefData: public wxObjectRefData |
83df96d6 | 30 | { |
3cd0b8c5 RR |
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; | |
aad6765c | 39 | m_hasPixel = false; |
3cd0b8c5 | 40 | } |
d5cf34f4 JS |
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 | } | |
aad6765c | 48 | |
3cd0b8c5 RR |
49 | ~wxColourRefData() |
50 | { | |
51 | FreeColour(); | |
52 | } | |
83df96d6 | 53 | |
3cd0b8c5 RR |
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 | } | |
aad6765c | 63 | |
3cd0b8c5 RR |
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 | ||
aad6765c JS |
77 | unsigned short wxColourRefData::colMapAllocCounter[ 256 ] = |
78 | { | |
3cd0b8c5 RR |
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() | |
83df96d6 | 95 | { |
dc4025af RR |
96 | if (!m_colormap) |
97 | return; | |
aad6765c | 98 | #if !wxUSE_NANOX |
ac7f6cf0 VZ |
99 | if ( wxTheApp && |
100 | (wxTheApp->m_visualInfo->m_visualType == GrayScale || | |
101 | wxTheApp->m_visualInfo->m_visualType == PseudoColor) ) | |
3cd0b8c5 | 102 | { |
dc4025af RR |
103 | int idx = m_color.pixel; |
104 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
aad6765c | 105 | |
dc4025af RR |
106 | if (colMapAllocCounter[ idx ] == 0) |
107 | { | |
108 | unsigned long pixel = m_color.pixel; | |
109 | XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 ); | |
3cd0b8c5 | 110 | } |
3cd0b8c5 | 111 | } |
8601b2e1 | 112 | #endif |
83df96d6 JS |
113 | } |
114 | ||
3cd0b8c5 | 115 | void wxColourRefData::AllocColour( WXColormap cmap ) |
83df96d6 | 116 | { |
3cd0b8c5 RR |
117 | if (m_hasPixel && (m_colormap == cmap)) |
118 | return; | |
119 | ||
120 | FreeColour(); | |
121 | ||
8601b2e1 | 122 | #if !wxUSE_NANOX |
9ce8d6a2 MB |
123 | if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) || |
124 | (wxTheApp->m_visualInfo->m_visualType == PseudoColor)) | |
3cd0b8c5 | 125 | { |
dc4025af | 126 | m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color ); |
3cd0b8c5 RR |
127 | int idx = m_color.pixel; |
128 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1; | |
129 | } | |
130 | else | |
8601b2e1 | 131 | #endif |
3cd0b8c5 RR |
132 | { |
133 | m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color ); | |
134 | } | |
aad6765c | 135 | |
3cd0b8c5 | 136 | m_colormap = cmap; |
83df96d6 JS |
137 | } |
138 | ||
3cd0b8c5 RR |
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 | ||
3cd0b8c5 | 147 | wxColour::~wxColour() |
83df96d6 JS |
148 | { |
149 | } | |
150 | ||
3cd0b8c5 | 151 | bool wxColour::operator == ( const wxColour& col ) const |
83df96d6 | 152 | { |
aad6765c | 153 | if (m_refData == col.m_refData) return true; |
3cd0b8c5 | 154 | |
aad6765c | 155 | if (!m_refData || !col.m_refData) return false; |
3cd0b8c5 RR |
156 | |
157 | XColor *own = &(((wxColourRefData*)m_refData)->m_color); | |
158 | XColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
3cd0b8c5 | 159 | |
aad6765c JS |
160 | return (own->red == other->red) |
161 | && (own->green == other->green) | |
d35dec9b | 162 | && (own->blue == other->blue) ; |
aad6765c | 163 | |
83df96d6 JS |
164 | } |
165 | ||
3cd0b8c5 RR |
166 | wxObjectRefData *wxColour::CreateRefData() const |
167 | { | |
168 | return new wxColourRefData; | |
169 | } | |
83df96d6 | 170 | |
3cd0b8c5 RR |
171 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const |
172 | { | |
173 | return new wxColourRefData(*(wxColourRefData *)data); | |
174 | } | |
83df96d6 | 175 | |
40989e46 | 176 | void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) |
83df96d6 | 177 | { |
3cd0b8c5 | 178 | AllocExclusive(); |
aad6765c | 179 | |
0b5c0e1a JS |
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 | |
3cd0b8c5 RR |
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; | |
0b5c0e1a | 188 | #endif |
3cd0b8c5 | 189 | M_COLDATA->m_color.pixel = 0; |
83df96d6 JS |
190 | } |
191 | ||
3cd0b8c5 RR |
192 | unsigned char wxColour::Red() const |
193 | { | |
194 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
195 | ||
0b5c0e1a JS |
196 | #if wxUSE_NANOX |
197 | return (unsigned char) M_COLDATA->m_color.red ; | |
198 | #else | |
3cd0b8c5 | 199 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
0b5c0e1a | 200 | #endif |
3cd0b8c5 RR |
201 | } |
202 | ||
203 | unsigned char wxColour::Green() const | |
204 | { | |
205 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
206 | ||
0b5c0e1a JS |
207 | #if wxUSE_NANOX |
208 | return (unsigned char) M_COLDATA->m_color.green ; | |
209 | #else | |
3cd0b8c5 | 210 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
0b5c0e1a | 211 | #endif |
3cd0b8c5 RR |
212 | } |
213 | ||
214 | unsigned char wxColour::Blue() const | |
215 | { | |
216 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
217 | ||
0b5c0e1a JS |
218 | #if wxUSE_NANOX |
219 | return (unsigned char) M_COLDATA->m_color.blue ; | |
220 | #else | |
3cd0b8c5 | 221 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
0b5c0e1a | 222 | #endif |
3cd0b8c5 RR |
223 | } |
224 | ||
225 | void wxColour::CalcPixel( WXColormap cmap ) | |
226 | { | |
887dd52f RR |
227 | wxCHECK_RET( Ok(), wxT("invalid colour") ); |
228 | ||
229 | wxCHECK_RET( cmap, wxT("invalid colormap") ); | |
3cd0b8c5 RR |
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; | |
83df96d6 | 246 | } |
40989e46 WS |
247 | |
248 | bool wxColour::FromString(const wxChar *name) | |
249 | { | |
250 | Display *dpy = wxGlobalDisplay(); | |
251 | WXColormap colormap = wxTheApp->GetMainColormap( dpy ); | |
252 | XColor xcol; | |
7760051f | 253 | if ( XParseColor( dpy, (Colormap)colormap, name , &xcol ) ) |
40989e46 WS |
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 | } |