]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
Corrected strange test for __WINDOWS__ or wxUSE_MFC
[wxWidgets.git] / src / gtk1 / 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
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "colour.h"
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
c801d85f 18#include "wx/gdicmn.h"
ed39ff57 19#include "wx/colour.h"
2b5f62a0 20#include "wx/gtk/private.h"
c801d85f 21
071a2d78 22#include <gdk/gdk.h>
307fc8d5 23#include <gdk/gdkx.h>
071a2d78 24#include <gdk/gdkprivate.h>
c801d85f
KB
25
26//-----------------------------------------------------------------------------
27// wxColour
28//-----------------------------------------------------------------------------
29
30class wxColourRefData: public wxObjectRefData
31{
47c93b63 32public:
c89f5c02
RR
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;
aad6765c 40 m_hasPixel = false;
c89f5c02 41 }
4e6b8309 42
dbb846c6 43 wxColourRefData(const wxColourRefData& data)
d84afea9 44 : wxObjectRefData()
dbb846c6
VZ
45 {
46 m_color = data.m_color;
47 m_colormap = data.m_colormap;
48 m_hasPixel = data.m_hasPixel;
49 }
50
c89f5c02
RR
51 ~wxColourRefData()
52 {
53 FreeColour();
54 }
15248e43 55
c89f5c02
RR
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 }
4e6b8309 65
68dda785 66 void FreeColour();
d13fd3e4 67 void AllocColour( GdkColormap* cmap );
8bbe427f 68
c801d85f
KB
69 GdkColor m_color;
70 GdkColormap *m_colormap;
71 bool m_hasPixel;
8bbe427f 72
f6bcfd97 73 friend class wxColour;
d13fd3e4
VZ
74
75 // reference counter for systems with <= 8-Bit display
76 static gushort colMapAllocCounter[ 256 ];
77};
78
4e6b8309
VZ
79gushort wxColourRefData::colMapAllocCounter[ 256 ] =
80{
d13fd3e4
VZ
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
c801d85f
KB
94};
95
68dda785 96void wxColourRefData::FreeColour()
c801d85f 97{
47c93b63
RR
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 {
d13fd3e4
VZ
109 int idx = m_color.pixel;
110 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
4e6b8309 111
d13fd3e4 112 if (colMapAllocCounter[ idx ] == 0)
47c93b63
RR
113 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
114 }
115 }
ff7b1510 116}
c801d85f 117
d13fd3e4
VZ
118void wxColourRefData::AllocColour( GdkColormap *cmap )
119{
120 if (m_hasPixel && (m_colormap == cmap))
121 return;
122
123 FreeColour();
4e6b8309 124
d13fd3e4 125#ifdef __WXGTK20__
31c8af3a
VZ
126 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
127 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
d13fd3e4
VZ
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
c801d85f
KB
145//-----------------------------------------------------------------------------
146
147#define M_COLDATA ((wxColourRefData *)m_refData)
148
149#define SHIFT (8*(sizeof(short int)-sizeof(char)))
150
151IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
152
4b5f3fe6 153wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 154{
1ecc4d80
RR
155 m_refData = new wxColourRefData();
156 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
157 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
158 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
159 M_COLDATA->m_color.pixel = 0;
ff7b1510 160}
8bbe427f 161
4e6b8309
VZ
162/* static */
163wxColour wxColour::CreateByName(const wxString& name)
164{
165 wxColour col;
166
167 GdkColor colGDK;
168 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
169 {
170 wxColourRefData *refData = new wxColourRefData;
171 refData->m_color = colGDK;
172 col.m_refData = refData;
173 }
174
175 return col;
176}
222ed1d6
MB
177
178
68dda785 179void wxColour::InitFromName( const wxString &colourName )
c801d85f 180{
4e6b8309
VZ
181 // check the cache first
182 wxColour col;
183 if ( wxTheColourDatabase )
c801d85f 184 {
4e6b8309 185 col = wxTheColourDatabase->Find(colourName);
1ecc4d80 186 }
15248e43 187
4e6b8309
VZ
188 if ( !col.Ok() )
189 {
190 col = CreateByName(colourName);
191 }
15248e43 192
4e6b8309
VZ
193 if ( col.Ok() )
194 {
195 *this = col;
196 }
197 else
198 {
199 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
ff7b1510 200 }
ff7b1510 201}
c801d85f 202
68dda785 203wxColour::~wxColour()
c801d85f 204{
ff7b1510 205}
c801d85f 206
33b64e6f 207bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 208{
4e6b8309 209 if (m_refData == col.m_refData)
aad6765c 210 return true;
15248e43 211
4e6b8309 212 if (!m_refData || !col.m_refData)
aad6765c 213 return false;
15248e43 214
2b62ab35
RR
215 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
216 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
4e6b8309
VZ
217 return own->red == other->red &&
218 own->blue == other->blue &&
219 own->green == other->green;
ff7b1510 220}
c801d85f 221
c89f5c02
RR
222wxObjectRefData *wxColour::CreateRefData() const
223{
224 return new wxColourRefData;
225}
226
227wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 228{
c89f5c02 229 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 230}
c801d85f 231
4b5f3fe6 232void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 233{
c89f5c02 234 AllocExclusive();
4e6b8309 235
1ecc4d80
RR
236 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
237 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
238 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
239 M_COLDATA->m_color.pixel = 0;
4e6b8309 240
2b5f62a0 241 M_COLDATA->m_colormap = (GdkColormap*) NULL;
aad6765c 242 M_COLDATA->m_hasPixel = false;
ff7b1510 243}
c801d85f 244
68dda785 245unsigned char wxColour::Red() const
c801d85f 246{
223d09f6 247 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 248
1ecc4d80 249 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 250}
c801d85f 251
68dda785 252unsigned char wxColour::Green() const
c801d85f 253{
223d09f6 254 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 255
1ecc4d80 256 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 257}
c801d85f 258
68dda785 259unsigned char wxColour::Blue() const
c801d85f 260{
223d09f6 261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 262
1ecc4d80 263 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 264}
c801d85f 265
c801d85f
KB
266void wxColour::CalcPixel( GdkColormap *cmap )
267{
1ecc4d80 268 if (!Ok()) return;
8bbe427f 269
d13fd3e4 270 M_COLDATA->AllocColour( cmap );
ff7b1510 271}
c801d85f 272
68dda785 273int wxColour::GetPixel() const
c801d85f 274{
223d09f6 275 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 276
1ecc4d80 277 return M_COLDATA->m_color.pixel;
ff7b1510 278}
c801d85f 279
68dda785 280GdkColor *wxColour::GetColor() const
c801d85f 281{
223d09f6 282 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 283
1ecc4d80 284 return &M_COLDATA->m_color;
ff7b1510 285}
c801d85f
KB
286
287