]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
added wxUSE_PALETTE and fixed compilation with it set to 0
[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:
68dda785
VZ
28 wxColourRefData();
29 ~wxColourRefData();
15248e43 30
68dda785 31 void FreeColour();
d13fd3e4 32 void AllocColour( GdkColormap* cmap );
8bbe427f 33
47c93b63 34public:
c801d85f
KB
35 GdkColor m_color;
36 GdkColormap *m_colormap;
37 bool m_hasPixel;
8bbe427f 38
f6bcfd97 39 friend class wxColour;
d13fd3e4
VZ
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
c801d85f
KB
60};
61
68dda785 62wxColourRefData::wxColourRefData()
c801d85f 63{
1ecc4d80
RR
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;
ff7b1510 70}
c801d85f 71
68dda785 72wxColourRefData::~wxColourRefData()
c801d85f 73{
1ecc4d80 74 FreeColour();
ff7b1510 75}
c801d85f 76
68dda785 77void wxColourRefData::FreeColour()
c801d85f 78{
47c93b63
RR
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 {
d13fd3e4
VZ
90 int idx = m_color.pixel;
91 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
92
93 if (colMapAllocCounter[ idx ] == 0)
47c93b63
RR
94 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
95 }
96 }
ff7b1510 97}
c801d85f 98
d13fd3e4
VZ
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
c801d85f
KB
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
68dda785 134wxColour::wxColour()
c801d85f 135{
d13fd3e4 136 m_refData = new wxColourRefData();
ff7b1510 137}
c801d85f 138
4b5f3fe6 139wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 140{
1ecc4d80
RR
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;
ff7b1510 146}
8bbe427f 147
68dda785 148void wxColour::InitFromName( const wxString &colourName )
c801d85f 149{
1ecc4d80
RR
150 wxNode *node = (wxNode *) NULL;
151 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
c801d85f 152 {
1ecc4d80
RR
153 wxColour *col = (wxColour*)node->Data();
154 UnRef();
155 if (col) Ref( *col );
156 }
157 else
158 {
159 m_refData = new wxColourRefData();
93c5dd39 160 if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color ))
1ecc4d80 161 {
15248e43
VZ
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
1ecc4d80
RR
171 delete m_refData;
172 m_refData = (wxObjectRefData *) NULL;
173 }
ff7b1510 174 }
ff7b1510 175}
c801d85f
KB
176
177wxColour::wxColour( const wxColour& col )
8bbe427f 178{
1ecc4d80 179 Ref( col );
ff7b1510 180}
c801d85f 181
68dda785 182wxColour::~wxColour()
c801d85f 183{
ff7b1510 184}
c801d85f 185
8bbe427f
VZ
186wxColour& wxColour::operator = ( const wxColour& col )
187{
1ecc4d80
RR
188 if (*this == col) return (*this);
189 Ref( col );
190 return *this;
ff7b1510 191}
c801d85f 192
33b64e6f 193bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 194{
2b62ab35 195 if (m_refData == col.m_refData) return TRUE;
15248e43 196
2b62ab35
RR
197 if (!m_refData) return FALSE;
198 if (!col.m_refData) return FALSE;
15248e43 199
2b62ab35
RR
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;
15248e43 205
2b62ab35 206 return TRUE;
ff7b1510 207}
c801d85f 208
33b64e6f 209bool wxColour::operator != ( const wxColour& col) const
8bbe427f 210{
f6bcfd97 211 return !(*this == col);
ff7b1510 212}
c801d85f 213
4b5f3fe6 214void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 215{
1ecc4d80
RR
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;
ff7b1510 222}
c801d85f 223
68dda785 224unsigned char wxColour::Red() const
c801d85f 225{
223d09f6 226 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 227
1ecc4d80 228 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 229}
c801d85f 230
68dda785 231unsigned char wxColour::Green() const
c801d85f 232{
223d09f6 233 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 234
1ecc4d80 235 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 236}
c801d85f 237
68dda785 238unsigned char wxColour::Blue() const
c801d85f 239{
223d09f6 240 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 241
1ecc4d80 242 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 243}
c801d85f 244
68dda785 245bool wxColour::Ok() const
c801d85f 246{
1ecc4d80 247 return (m_refData != NULL);
ff7b1510 248}
c801d85f
KB
249
250void wxColour::CalcPixel( GdkColormap *cmap )
251{
1ecc4d80 252 if (!Ok()) return;
8bbe427f 253
d13fd3e4 254 M_COLDATA->AllocColour( cmap );
ff7b1510 255}
c801d85f 256
68dda785 257int wxColour::GetPixel() const
c801d85f 258{
223d09f6 259 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 260
1ecc4d80 261 return M_COLDATA->m_color.pixel;
ff7b1510 262}
c801d85f 263
68dda785 264GdkColor *wxColour::GetColor() const
c801d85f 265{
223d09f6 266 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 267
1ecc4d80 268 return &M_COLDATA->m_color;
ff7b1510 269}
c801d85f
KB
270
271