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