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