]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/colour.cpp
Fix memory leak when a spacer is added, and crash when a window is added before wxSiz...
[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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12#pragma implementation "colour.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#include "wx/colour.h"
19#include "wx/gdicmn.h"
20#include "wx/gtk/private.h"
21
22#include <gdk/gdk.h>
23#include <gdk/gdkx.h>
24#include <gdk/gdkprivate.h>
25
26//-----------------------------------------------------------------------------
27// wxColour
28//-----------------------------------------------------------------------------
29
30class wxColourRefData: public wxObjectRefData
31{
32public:
33 wxColourRefData()
34 {
35 m_color.red = 0;
36 m_color.green = 0;
37 m_color.blue = 0;
38 m_color.pixel = 0;
39 m_colormap = (GdkColormap *) NULL;
40 m_hasPixel = false;
41 }
42
43 wxColourRefData(const wxColourRefData& data)
44 : wxObjectRefData()
45 {
46 m_color = data.m_color;
47 m_colormap = data.m_colormap;
48 m_hasPixel = data.m_hasPixel;
49 }
50
51 ~wxColourRefData()
52 {
53 FreeColour();
54 }
55
56 bool operator == (const wxColourRefData& data) const
57 {
58 return (m_colormap == data.m_colormap &&
59 m_hasPixel == data.m_hasPixel &&
60 m_color.red == data.m_color.red &&
61 m_color.green == data.m_color.green &&
62 m_color.blue == data.m_color.blue &&
63 m_color.pixel == data.m_color.pixel);
64 }
65
66 void FreeColour();
67 void AllocColour( GdkColormap* cmap );
68
69 GdkColor m_color;
70 GdkColormap *m_colormap;
71 bool m_hasPixel;
72
73 friend class wxColour;
74
75 // reference counter for systems with <= 8-Bit display
76 static gushort colMapAllocCounter[ 256 ];
77};
78
79gushort wxColourRefData::colMapAllocCounter[ 256 ] =
80{
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, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94};
95
96void wxColourRefData::FreeColour()
97{
98 if (m_colormap)
99 {
100#ifdef __WXGTK20__
101 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
102 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
103#else
104 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
105 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
106 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
107#endif
108 {
109 int idx = m_color.pixel;
110 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
111
112 if (colMapAllocCounter[ idx ] == 0)
113 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
114 }
115 }
116}
117
118void wxColourRefData::AllocColour( GdkColormap *cmap )
119{
120 if (m_hasPixel && (m_colormap == cmap))
121 return;
122
123 FreeColour();
124
125#ifdef __WXGTK20__
126 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
127 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
128#else
129 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
130 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
131 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
132#endif
133 {
134 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
135 int idx = m_color.pixel;
136 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
137 }
138 else
139 {
140 m_hasPixel = gdk_color_alloc( cmap, &m_color );
141 }
142 m_colormap = cmap;
143}
144
145//-----------------------------------------------------------------------------
146
147#define M_COLDATA ((wxColourRefData *)m_refData)
148
149// GDK's values are in 0..65535 range, our are in 0..255
150#define SHIFT 8
151
152IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
153
154wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
155{
156 m_refData = new wxColourRefData();
157 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
158 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
159 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
160 M_COLDATA->m_color.pixel = 0;
161}
162
163/* static */
164wxColour wxColour::CreateByName(const wxString& name)
165{
166 wxColour col;
167
168 GdkColor colGDK;
169 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
170 {
171 wxColourRefData *refData = new wxColourRefData;
172 refData->m_color = colGDK;
173 col.m_refData = refData;
174 }
175
176 return col;
177}
178
179
180void wxColour::InitFromName( const wxString &colourName )
181{
182 // check the cache first
183 wxColour col;
184 if ( wxTheColourDatabase )
185 {
186 col = wxTheColourDatabase->Find(colourName);
187 }
188
189 if ( !col.Ok() )
190 {
191 col = CreateByName(colourName);
192 }
193
194 if ( col.Ok() )
195 {
196 *this = col;
197 }
198 else
199 {
200 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
201 }
202}
203
204wxColour::~wxColour()
205{
206}
207
208bool wxColour::operator == ( const wxColour& col ) const
209{
210 if (m_refData == col.m_refData)
211 return true;
212
213 if (!m_refData || !col.m_refData)
214 return false;
215
216 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
217 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
218 return own->red == other->red &&
219 own->blue == other->blue &&
220 own->green == other->green;
221}
222
223wxObjectRefData *wxColour::CreateRefData() const
224{
225 return new wxColourRefData;
226}
227
228wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
229{
230 return new wxColourRefData(*(wxColourRefData *)data);
231}
232
233void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
234{
235 AllocExclusive();
236
237 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
238 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
239 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
240 M_COLDATA->m_color.pixel = 0;
241
242 M_COLDATA->m_colormap = (GdkColormap*) NULL;
243 M_COLDATA->m_hasPixel = false;
244}
245
246unsigned char wxColour::Red() const
247{
248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
249
250 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
251}
252
253unsigned char wxColour::Green() const
254{
255 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
256
257 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
258}
259
260unsigned char wxColour::Blue() const
261{
262 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
263
264 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
265}
266
267void wxColour::CalcPixel( GdkColormap *cmap )
268{
269 if (!Ok()) return;
270
271 M_COLDATA->AllocColour( cmap );
272}
273
274int wxColour::GetPixel() const
275{
276 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
277
278 return M_COLDATA->m_color.pixel;
279}
280
281GdkColor *wxColour::GetColor() const
282{
283 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
284
285 return &M_COLDATA->m_color;
286}
287
288