]> git.saurik.com Git - wxWidgets.git/blame - src/x11/colour.cpp
Unicode compilation fixes (patch from Dimitri)
[wxWidgets.git] / src / x11 / colour.cpp
CommitLineData
83df96d6
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: colour.cpp
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
83df96d6
JS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
83df96d6
JS
12
13#ifdef __GNUG__
14#pragma implementation "colour.h"
15#endif
16
17#include "wx/gdicmn.h"
887dd52f 18#include "wx/app.h"
83df96d6 19
7266b672 20#include "wx/x11/private.h"
83df96d6 21
3cd0b8c5
RR
22//-----------------------------------------------------------------------------
23// wxColour
24//-----------------------------------------------------------------------------
83df96d6 25
3cd0b8c5 26class wxColourRefData: public wxObjectRefData
83df96d6 27{
3cd0b8c5
RR
28public:
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 }
d5cf34f4
JS
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 }
3cd0b8c5
RR
45
46 ~wxColourRefData()
47 {
48 FreeColour();
49 }
83df96d6 50
3cd0b8c5
RR
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
74unsigned 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
91void wxColourRefData::FreeColour()
83df96d6 92{
ac014163 93#if 0
3cd0b8c5
RR
94 if (m_colormap)
95 {
96 Colormap cm = (Colormap)m_colormap;
97
3cd0b8c5
RR
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 }
3cd0b8c5 108 }
ac014163 109#endif
83df96d6
JS
110}
111
3cd0b8c5 112void wxColourRefData::AllocColour( WXColormap cmap )
83df96d6 113{
3cd0b8c5
RR
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;
83df96d6
JS
134}
135
3cd0b8c5
RR
136//-----------------------------------------------------------------------------
137
138#define M_COLDATA ((wxColourRefData *)m_refData)
139
140#define SHIFT (8*(sizeof(short int)-sizeof(char)))
141
142IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
143
144wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 145{
3cd0b8c5 146 m_refData = new wxColourRefData();
0b5c0e1a
JS
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
3cd0b8c5
RR
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;
0b5c0e1a 155#endif
3cd0b8c5 156 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
157}
158
3cd0b8c5 159void wxColour::InitFromName( const wxString &colourName )
83df96d6 160{
3cd0b8c5
RR
161 wxNode *node = (wxNode *) NULL;
162 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
83df96d6 163 {
3cd0b8c5
RR
164 wxColour *col = (wxColour*)node->Data();
165 UnRef();
166 if (col) Ref( *col );
83df96d6
JS
167 }
168 else
169 {
3cd0b8c5 170 m_refData = new wxColourRefData();
887dd52f
RR
171
172 M_COLDATA->m_colormap = wxTheApp->GetMainColormap( wxGlobalDisplay() );
173
3cd0b8c5
RR
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 }
83df96d6
JS
188 }
189}
190
3cd0b8c5 191wxColour::~wxColour()
83df96d6
JS
192{
193}
194
3cd0b8c5 195bool wxColour::operator == ( const wxColour& col ) const
83df96d6 196{
3cd0b8c5
RR
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;
83df96d6
JS
208}
209
3cd0b8c5
RR
210wxObjectRefData *wxColour::CreateRefData() const
211{
212 return new wxColourRefData;
213}
83df96d6 214
3cd0b8c5
RR
215wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
216{
217 return new wxColourRefData(*(wxColourRefData *)data);
218}
83df96d6 219
3cd0b8c5 220void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 221{
3cd0b8c5 222 AllocExclusive();
83df96d6 223
3cd0b8c5 224 m_refData = new wxColourRefData();
0b5c0e1a
JS
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
3cd0b8c5
RR
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;
0b5c0e1a 233#endif
3cd0b8c5 234 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
235}
236
3cd0b8c5
RR
237unsigned char wxColour::Red() const
238{
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
240
0b5c0e1a
JS
241#if wxUSE_NANOX
242 return (unsigned char) M_COLDATA->m_color.red ;
243#else
3cd0b8c5 244 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
0b5c0e1a 245#endif
3cd0b8c5
RR
246}
247
248unsigned char wxColour::Green() const
249{
250 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
251
0b5c0e1a
JS
252#if wxUSE_NANOX
253 return (unsigned char) M_COLDATA->m_color.green ;
254#else
3cd0b8c5 255 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
0b5c0e1a 256#endif
3cd0b8c5
RR
257}
258
259unsigned char wxColour::Blue() const
260{
261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
262
0b5c0e1a
JS
263#if wxUSE_NANOX
264 return (unsigned char) M_COLDATA->m_color.blue ;
265#else
3cd0b8c5 266 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
0b5c0e1a 267#endif
3cd0b8c5
RR
268}
269
270void wxColour::CalcPixel( WXColormap cmap )
271{
887dd52f
RR
272 wxCHECK_RET( Ok(), wxT("invalid colour") );
273
274 wxCHECK_RET( cmap, wxT("invalid colormap") );
3cd0b8c5
RR
275
276 M_COLDATA->AllocColour( cmap );
277}
278
279unsigned long wxColour::GetPixel() const
280{
281 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
282
283 return M_COLDATA->m_color.pixel;
284}
285
286WXColor *wxColour::GetColor() const
287{
288 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
289
290 return (WXColor*) &M_COLDATA->m_color;
83df96d6 291}