]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/colour.cpp
cleaned up the checkbox creation code (~70 lines of code became 3)
[wxWidgets.git] / src / gtk1 / colour.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: colour.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "colour.h"
13#endif
14
15#include "wx/gdicmn.h"
16
17#include <gdk/gdk.h>
18#include <gdk/gdkx.h>
19#include <gdk/gdkprivate.h>
20
21//-----------------------------------------------------------------------------
22// wxColour
23//-----------------------------------------------------------------------------
24
25class wxColourRefData: public wxObjectRefData
26{
27public:
28 wxColourRefData()
29 {
30 m_color.red = 0;
31 m_color.green = 0;
32 m_color.blue = 0;
33 m_color.pixel = 0;
34 m_colormap = (GdkColormap *) NULL;
35 m_hasPixel = FALSE;
36 }
37
38 wxColourRefData(const wxColourRefData& data)
39 {
40 m_color = data.m_color;
41 m_colormap = data.m_colormap;
42 m_hasPixel = data.m_hasPixel;
43 }
44
45 ~wxColourRefData()
46 {
47 FreeColour();
48 }
49
50 bool operator == (const wxColourRefData& data) const
51 {
52 return (m_colormap == data.m_colormap &&
53 m_hasPixel == data.m_hasPixel &&
54 m_color.red == data.m_color.red &&
55 m_color.green == data.m_color.green &&
56 m_color.blue == data.m_color.blue &&
57 m_color.pixel == data.m_color.pixel);
58 }
59
60 void FreeColour();
61 void AllocColour( GdkColormap* cmap );
62
63 GdkColor m_color;
64 GdkColormap *m_colormap;
65 bool m_hasPixel;
66
67 friend class wxColour;
68
69 // reference counter for systems with <= 8-Bit display
70 static gushort colMapAllocCounter[ 256 ];
71};
72
73gushort wxColourRefData::colMapAllocCounter[ 256 ] =
74{
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, 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
88};
89
90void wxColourRefData::FreeColour()
91{
92 if (m_colormap)
93 {
94#ifdef __WXGTK20__
95 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
96 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
97#else
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#endif
102 {
103 int idx = m_color.pixel;
104 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
105
106 if (colMapAllocCounter[ idx ] == 0)
107 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
108 }
109 }
110}
111
112void wxColourRefData::AllocColour( GdkColormap *cmap )
113{
114 if (m_hasPixel && (m_colormap == cmap))
115 return;
116
117 FreeColour();
118
119#ifdef __WXGTK20__
120 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
121 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
122#else
123 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
124 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
125 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
126#endif
127 {
128 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
129 int idx = m_color.pixel;
130 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
131 }
132 else
133 {
134 m_hasPixel = gdk_color_alloc( cmap, &m_color );
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
145IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
146
147wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
148{
149 m_refData = new wxColourRefData();
150 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
151 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
152 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
153 M_COLDATA->m_color.pixel = 0;
154}
155
156void wxColour::InitFromName( const wxString &colourName )
157{
158 wxNode *node = (wxNode *) NULL;
159 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
160 {
161 wxColour *col = (wxColour*)node->Data();
162 UnRef();
163 if (col) Ref( *col );
164 }
165 else
166 {
167 m_refData = new wxColourRefData();
168
169 if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color ))
170 {
171 // VZ: asserts are good in general but this one is triggered by
172 // calling wxColourDatabase::FindColour() with an
173 // unrecognized colour name and this can't be avoided from the
174 // user code, so don't give it here
175 //
176 // a better solution would be to changed code in FindColour()
177
178 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
179
180 delete m_refData;
181 m_refData = (wxObjectRefData *) NULL;
182 }
183 }
184}
185
186wxColour::~wxColour()
187{
188}
189
190bool wxColour::operator == ( const wxColour& col ) const
191{
192 if (m_refData == col.m_refData) return TRUE;
193
194 if (!m_refData || !col.m_refData) return FALSE;
195
196 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
197 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
198 if (own->red != other->red) return FALSE;
199 if (own->blue != other->blue) return FALSE;
200 if (own->green != other->green) return FALSE;
201
202 return TRUE;
203}
204
205wxObjectRefData *wxColour::CreateRefData() const
206{
207 return new wxColourRefData;
208}
209
210wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
211{
212 return new wxColourRefData(*(wxColourRefData *)data);
213}
214
215void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
216{
217 AllocExclusive();
218
219 m_refData = new wxColourRefData();
220 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
221 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
222 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
223 M_COLDATA->m_color.pixel = 0;
224}
225
226unsigned char wxColour::Red() const
227{
228 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
229
230 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
231}
232
233unsigned char wxColour::Green() const
234{
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
236
237 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
238}
239
240unsigned char wxColour::Blue() const
241{
242 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
243
244 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
245}
246
247void wxColour::CalcPixel( GdkColormap *cmap )
248{
249 if (!Ok()) return;
250
251 M_COLDATA->AllocColour( cmap );
252}
253
254int wxColour::GetPixel() const
255{
256 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
257
258 return M_COLDATA->m_color.pixel;
259}
260
261GdkColor *wxColour::GetColor() const
262{
263 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
264
265 return &M_COLDATA->m_color;
266}
267
268