]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
Comment out wxHVScrolledWindow
[wxWidgets.git] / src / gtk1 / colour.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/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"
3cbab641 15#include "wx/gtk1/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 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
96 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
97 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
47c93b63 98 {
d13fd3e4
VZ
99 int idx = m_color.pixel;
100 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
4e6b8309 101
d13fd3e4 102 if (colMapAllocCounter[ idx ] == 0)
0a164d4c 103 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
47c93b63
RR
104 }
105 }
ff7b1510 106}
c801d85f 107
d13fd3e4
VZ
108void wxColourRefData::AllocColour( GdkColormap *cmap )
109{
110 if (m_hasPixel && (m_colormap == cmap))
111 return;
112
113 FreeColour();
4e6b8309 114
7e52dfd2
MW
115 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
116 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
117 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
d13fd3e4
VZ
118 {
119 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
120 int idx = m_color.pixel;
121 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
122 }
123 else
124 {
125 m_hasPixel = gdk_color_alloc( cmap, &m_color );
126 }
127 m_colormap = cmap;
128}
129
c801d85f
KB
130//-----------------------------------------------------------------------------
131
132#define M_COLDATA ((wxColourRefData *)m_refData)
133
3b9faf4c
VS
134// GDK's values are in 0..65535 range, our are in 0..255
135#define SHIFT 8
c801d85f
KB
136
137IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
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
4e6b8309
VZ
148/* static */
149wxColour wxColour::CreateByName(const wxString& name)
150{
151 wxColour col;
152
153 GdkColor colGDK;
154 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
155 {
156 wxColourRefData *refData = new wxColourRefData;
157 refData->m_color = colGDK;
158 col.m_refData = refData;
159 }
160
161 return col;
162}
222ed1d6
MB
163
164
68dda785 165void wxColour::InitFromName( const wxString &colourName )
c801d85f 166{
4e6b8309
VZ
167 // check the cache first
168 wxColour col;
169 if ( wxTheColourDatabase )
c801d85f 170 {
4e6b8309 171 col = wxTheColourDatabase->Find(colourName);
1ecc4d80 172 }
15248e43 173
4e6b8309
VZ
174 if ( !col.Ok() )
175 {
176 col = CreateByName(colourName);
177 }
15248e43 178
4e6b8309
VZ
179 if ( col.Ok() )
180 {
181 *this = col;
182 }
183 else
184 {
185 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
ff7b1510 186 }
ff7b1510 187}
c801d85f 188
68dda785 189wxColour::~wxColour()
c801d85f 190{
ff7b1510 191}
c801d85f 192
33b64e6f 193bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 194{
4e6b8309 195 if (m_refData == col.m_refData)
aad6765c 196 return true;
15248e43 197
4e6b8309 198 if (!m_refData || !col.m_refData)
aad6765c 199 return false;
15248e43 200
2b62ab35
RR
201 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
202 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
4e6b8309
VZ
203 return own->red == other->red &&
204 own->blue == other->blue &&
205 own->green == other->green;
ff7b1510 206}
c801d85f 207
c89f5c02
RR
208wxObjectRefData *wxColour::CreateRefData() const
209{
210 return new wxColourRefData;
211}
212
213wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 214{
c89f5c02 215 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 216}
c801d85f 217
4b5f3fe6 218void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 219{
c89f5c02 220 AllocExclusive();
4e6b8309 221
1ecc4d80
RR
222 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
223 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
224 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
225 M_COLDATA->m_color.pixel = 0;
4e6b8309 226
2b5f62a0 227 M_COLDATA->m_colormap = (GdkColormap*) NULL;
aad6765c 228 M_COLDATA->m_hasPixel = false;
ff7b1510 229}
c801d85f 230
68dda785 231unsigned char wxColour::Red() const
c801d85f 232{
223d09f6 233 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 234
1ecc4d80 235 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 236}
c801d85f 237
68dda785 238unsigned char wxColour::Green() const
c801d85f 239{
223d09f6 240 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 241
1ecc4d80 242 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 243}
c801d85f 244
68dda785 245unsigned char wxColour::Blue() const
c801d85f 246{
223d09f6 247 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 248
1ecc4d80 249 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 250}
c801d85f 251
c801d85f
KB
252void wxColour::CalcPixel( GdkColormap *cmap )
253{
1ecc4d80 254 if (!Ok()) return;
8bbe427f 255
d13fd3e4 256 M_COLDATA->AllocColour( cmap );
ff7b1510 257}
c801d85f 258
68dda785 259int wxColour::GetPixel() const
c801d85f 260{
223d09f6 261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 262
1ecc4d80 263 return M_COLDATA->m_color.pixel;
ff7b1510 264}
c801d85f 265
68dda785 266GdkColor *wxColour::GetColor() const
c801d85f 267{
223d09f6 268 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 269
1ecc4d80 270 return &M_COLDATA->m_color;
ff7b1510 271}