]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
Deprecate wxDC::{Begin,End}Drawing()
[wxWidgets.git] / src / gtk / colour.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
edc536d3 2// Name: src/gtk/colour.cpp
c801d85f
KB
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
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
ed39ff57 13#include "wx/colour.h"
e1bf3ad3 14#include "wx/gdicmn.h"
2b5f62a0 15#include "wx/gtk/private.h"
c801d85f 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;
aad6765c 35 m_hasPixel = false;
c89f5c02 36 }
4e6b8309 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 }
4e6b8309 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
4e6b8309
VZ
74gushort wxColourRefData::colMapAllocCounter[ 256 ] =
75{
d13fd3e4
VZ
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 {
47c93b63
RR
95 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
96 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
47c93b63 97 {
d13fd3e4
VZ
98 int idx = m_color.pixel;
99 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
4e6b8309 100
d13fd3e4 101 if (colMapAllocCounter[ idx ] == 0)
0a164d4c 102 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
47c93b63
RR
103 }
104 }
ff7b1510 105}
c801d85f 106
d13fd3e4
VZ
107void wxColourRefData::AllocColour( GdkColormap *cmap )
108{
109 if (m_hasPixel && (m_colormap == cmap))
110 return;
111
112 FreeColour();
4e6b8309 113
31c8af3a 114 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
0a164d4c 115 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
d13fd3e4
VZ
116 {
117 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
118 int idx = m_color.pixel;
119 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
120 }
121 else
122 {
123 m_hasPixel = gdk_color_alloc( cmap, &m_color );
124 }
125 m_colormap = cmap;
126}
127
c801d85f
KB
128//-----------------------------------------------------------------------------
129
130#define M_COLDATA ((wxColourRefData *)m_refData)
131
3b9faf4c
VS
132// GDK's values are in 0..65535 range, our are in 0..255
133#define SHIFT 8
c801d85f
KB
134
135IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
136
4b5f3fe6 137wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 138{
1ecc4d80
RR
139 m_refData = new wxColourRefData();
140 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
141 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
142 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
143 M_COLDATA->m_color.pixel = 0;
ff7b1510 144}
8bbe427f 145
4e6b8309
VZ
146/* static */
147wxColour wxColour::CreateByName(const wxString& name)
148{
149 wxColour col;
150
151 GdkColor colGDK;
152 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
153 {
154 wxColourRefData *refData = new wxColourRefData;
155 refData->m_color = colGDK;
156 col.m_refData = refData;
157 }
158
159 return col;
160}
222ed1d6
MB
161
162
68dda785 163void wxColour::InitFromName( const wxString &colourName )
c801d85f 164{
4e6b8309
VZ
165 // check the cache first
166 wxColour col;
167 if ( wxTheColourDatabase )
c801d85f 168 {
4e6b8309 169 col = wxTheColourDatabase->Find(colourName);
1ecc4d80 170 }
15248e43 171
4e6b8309
VZ
172 if ( !col.Ok() )
173 {
174 col = CreateByName(colourName);
175 }
15248e43 176
4e6b8309
VZ
177 if ( col.Ok() )
178 {
179 *this = col;
180 }
181 else
182 {
183 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
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{
4e6b8309 193 if (m_refData == col.m_refData)
aad6765c 194 return true;
15248e43 195
4e6b8309 196 if (!m_refData || !col.m_refData)
aad6765c 197 return false;
15248e43 198
2b62ab35
RR
199 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
200 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
4e6b8309
VZ
201 return own->red == other->red &&
202 own->blue == other->blue &&
203 own->green == other->green;
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 218 AllocExclusive();
4e6b8309 219
1ecc4d80
RR
220 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
221 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
222 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
223 M_COLDATA->m_color.pixel = 0;
4e6b8309 224
2b5f62a0 225 M_COLDATA->m_colormap = (GdkColormap*) NULL;
aad6765c 226 M_COLDATA->m_hasPixel = false;
ff7b1510 227}
c801d85f 228
68dda785 229unsigned char wxColour::Red() const
c801d85f 230{
223d09f6 231 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 232
1ecc4d80 233 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 234}
c801d85f 235
68dda785 236unsigned char wxColour::Green() const
c801d85f 237{
223d09f6 238 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 239
1ecc4d80 240 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 241}
c801d85f 242
68dda785 243unsigned char wxColour::Blue() const
c801d85f 244{
223d09f6 245 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 246
1ecc4d80 247 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 248}
c801d85f 249
c801d85f
KB
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}