]> git.saurik.com Git - wxWidgets.git/blame - src/x11/colour.cpp
more GTK-ish toolbar drawing
[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 }
38
39 ~wxColourRefData()
40 {
41 FreeColour();
42 }
83df96d6 43
3cd0b8c5
RR
44 bool operator == (const wxColourRefData& data) const
45 {
46 return (m_colormap == data.m_colormap &&
47 m_hasPixel == data.m_hasPixel &&
48 m_color.red == data.m_color.red &&
49 m_color.green == data.m_color.green &&
50 m_color.blue == data.m_color.blue &&
51 m_color.pixel == data.m_color.pixel);
52 }
53
54 void FreeColour();
55 void AllocColour( WXColormap cmap );
56
57 XColor m_color;
58 WXColormap m_colormap;
59 bool m_hasPixel;
60
61 friend class wxColour;
62
63 // reference counter for systems with <= 8-Bit display
64 static unsigned short colMapAllocCounter[ 256 ];
65};
66
67unsigned short wxColourRefData::colMapAllocCounter[ 256 ] =
68{
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
82};
83
84void wxColourRefData::FreeColour()
83df96d6 85{
ac014163 86#if 0
3cd0b8c5
RR
87 if (m_colormap)
88 {
89 Colormap cm = (Colormap)m_colormap;
90
3cd0b8c5
RR
91 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
92 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
93 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
94 {
95 int idx = m_color.pixel;
96 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
97
98 if (colMapAllocCounter[ idx ] == 0)
99 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
100 }
3cd0b8c5 101 }
ac014163 102#endif
83df96d6
JS
103}
104
3cd0b8c5 105void wxColourRefData::AllocColour( WXColormap cmap )
83df96d6 106{
3cd0b8c5
RR
107 if (m_hasPixel && (m_colormap == cmap))
108 return;
109
110 FreeColour();
111
112#if 0
113 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
114 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
115 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
116 {
117 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
118 int idx = m_color.pixel;
119 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
120 }
121 else
122#endif
123 {
124 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
125 }
126 m_colormap = cmap;
83df96d6
JS
127}
128
3cd0b8c5
RR
129//-----------------------------------------------------------------------------
130
131#define M_COLDATA ((wxColourRefData *)m_refData)
132
133#define SHIFT (8*(sizeof(short int)-sizeof(char)))
134
135IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
136
137wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 138{
3cd0b8c5 139 m_refData = new wxColourRefData();
0b5c0e1a
JS
140#if wxUSE_NANOX
141 M_COLDATA->m_color.red = ((unsigned short)red) ;
142 M_COLDATA->m_color.green = ((unsigned short)green) ;
143 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
144#else
3cd0b8c5
RR
145 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
146 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
147 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
0b5c0e1a 148#endif
3cd0b8c5 149 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
150}
151
3cd0b8c5 152void wxColour::InitFromName( const wxString &colourName )
83df96d6 153{
3cd0b8c5
RR
154 wxNode *node = (wxNode *) NULL;
155 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
83df96d6 156 {
3cd0b8c5
RR
157 wxColour *col = (wxColour*)node->Data();
158 UnRef();
159 if (col) Ref( *col );
83df96d6
JS
160 }
161 else
162 {
3cd0b8c5 163 m_refData = new wxColourRefData();
887dd52f
RR
164
165 M_COLDATA->m_colormap = wxTheApp->GetMainColormap( wxGlobalDisplay() );
166
3cd0b8c5
RR
167 if (!XParseColor( wxGlobalDisplay(), (Colormap) M_COLDATA->m_colormap, colourName.mb_str(), &M_COLDATA->m_color ))
168 {
169 // VZ: asserts are good in general but this one is triggered by
170 // calling wxColourDatabase::FindColour() with an
171 // unrecognized colour name and this can't be avoided from the
172 // user code, so don't give it here
173 //
174 // a better solution would be to changed code in FindColour()
175
176 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
177
178 delete m_refData;
179 m_refData = (wxObjectRefData *) NULL;
180 }
83df96d6
JS
181 }
182}
183
3cd0b8c5 184wxColour::~wxColour()
83df96d6
JS
185{
186}
187
3cd0b8c5 188bool wxColour::operator == ( const wxColour& col ) const
83df96d6 189{
3cd0b8c5
RR
190 if (m_refData == col.m_refData) return TRUE;
191
192 if (!m_refData || !col.m_refData) return FALSE;
193
194 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
195 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
196 if (own->red != other->red) return FALSE;
197 if (own->blue != other->blue) return FALSE;
198 if (own->green != other->green) return FALSE;
199
200 return TRUE;
83df96d6
JS
201}
202
3cd0b8c5
RR
203wxObjectRefData *wxColour::CreateRefData() const
204{
205 return new wxColourRefData;
206}
83df96d6 207
3cd0b8c5
RR
208wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
209{
210 return new wxColourRefData(*(wxColourRefData *)data);
211}
83df96d6 212
3cd0b8c5 213void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 214{
3cd0b8c5 215 AllocExclusive();
83df96d6 216
3cd0b8c5 217 m_refData = new wxColourRefData();
0b5c0e1a
JS
218#if wxUSE_NANOX
219 M_COLDATA->m_color.red = ((unsigned short)red) ;
220 M_COLDATA->m_color.green = ((unsigned short)green) ;
221 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
222#else
3cd0b8c5
RR
223 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
224 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
225 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
0b5c0e1a 226#endif
3cd0b8c5 227 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
228}
229
3cd0b8c5
RR
230unsigned char wxColour::Red() const
231{
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
233
0b5c0e1a
JS
234#if wxUSE_NANOX
235 return (unsigned char) M_COLDATA->m_color.red ;
236#else
3cd0b8c5 237 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
0b5c0e1a 238#endif
3cd0b8c5
RR
239}
240
241unsigned char wxColour::Green() const
242{
243 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
244
0b5c0e1a
JS
245#if wxUSE_NANOX
246 return (unsigned char) M_COLDATA->m_color.green ;
247#else
3cd0b8c5 248 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
0b5c0e1a 249#endif
3cd0b8c5
RR
250}
251
252unsigned char wxColour::Blue() const
253{
254 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
255
0b5c0e1a
JS
256#if wxUSE_NANOX
257 return (unsigned char) M_COLDATA->m_color.blue ;
258#else
3cd0b8c5 259 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
0b5c0e1a 260#endif
3cd0b8c5
RR
261}
262
263void wxColour::CalcPixel( WXColormap cmap )
264{
887dd52f
RR
265 wxCHECK_RET( Ok(), wxT("invalid colour") );
266
267 wxCHECK_RET( cmap, wxT("invalid colormap") );
3cd0b8c5
RR
268
269 M_COLDATA->AllocColour( cmap );
270}
271
272unsigned long wxColour::GetPixel() const
273{
274 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
275
276 return M_COLDATA->m_color.pixel;
277}
278
279WXColor *wxColour::GetColor() const
280{
281 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
282
283 return (WXColor*) &M_COLDATA->m_color;
83df96d6 284}