]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
Back to previous call with -1 replacement.
[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"
40989e46 14
dd05139a
WS
15#ifndef WX_PRECOMP
16 #include "wx/gdicmn.h"
17#endif
18
3cbab641 19#include "wx/gtk1/private.h"
c801d85f 20
071a2d78 21#include <gdk/gdk.h>
307fc8d5 22#include <gdk/gdkx.h>
071a2d78 23#include <gdk/gdkprivate.h>
c801d85f
KB
24
25//-----------------------------------------------------------------------------
26// wxColour
27//-----------------------------------------------------------------------------
28
29class wxColourRefData: public wxObjectRefData
30{
47c93b63 31public:
c89f5c02
RR
32 wxColourRefData()
33 {
34 m_color.red = 0;
35 m_color.green = 0;
36 m_color.blue = 0;
37 m_color.pixel = 0;
38 m_colormap = (GdkColormap *) NULL;
aad6765c 39 m_hasPixel = false;
c89f5c02 40 }
4e6b8309 41
dbb846c6 42 wxColourRefData(const wxColourRefData& data)
d84afea9 43 : wxObjectRefData()
dbb846c6
VZ
44 {
45 m_color = data.m_color;
46 m_colormap = data.m_colormap;
47 m_hasPixel = data.m_hasPixel;
48 }
49
d3c7fc99 50 virtual ~wxColourRefData()
c89f5c02
RR
51 {
52 FreeColour();
53 }
15248e43 54
c89f5c02
RR
55 bool operator == (const wxColourRefData& data) const
56 {
57 return (m_colormap == data.m_colormap &&
58 m_hasPixel == data.m_hasPixel &&
59 m_color.red == data.m_color.red &&
60 m_color.green == data.m_color.green &&
61 m_color.blue == data.m_color.blue &&
62 m_color.pixel == data.m_color.pixel);
63 }
4e6b8309 64
68dda785 65 void FreeColour();
d13fd3e4 66 void AllocColour( GdkColormap* cmap );
8bbe427f 67
c801d85f
KB
68 GdkColor m_color;
69 GdkColormap *m_colormap;
70 bool m_hasPixel;
8bbe427f 71
f6bcfd97 72 friend class wxColour;
d13fd3e4
VZ
73
74 // reference counter for systems with <= 8-Bit display
75 static gushort colMapAllocCounter[ 256 ];
76};
77
4e6b8309
VZ
78gushort wxColourRefData::colMapAllocCounter[ 256 ] =
79{
d13fd3e4
VZ
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, 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
c801d85f
KB
93};
94
68dda785 95void wxColourRefData::FreeColour()
c801d85f 96{
47c93b63
RR
97 if (m_colormap)
98 {
47c93b63
RR
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))
47c93b63 102 {
d13fd3e4
VZ
103 int idx = m_color.pixel;
104 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
4e6b8309 105
d13fd3e4 106 if (colMapAllocCounter[ idx ] == 0)
0a164d4c 107 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
47c93b63
RR
108 }
109 }
ff7b1510 110}
c801d85f 111
d13fd3e4
VZ
112void wxColourRefData::AllocColour( GdkColormap *cmap )
113{
114 if (m_hasPixel && (m_colormap == cmap))
115 return;
116
117 FreeColour();
4e6b8309 118
7e52dfd2
MW
119 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
120 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
121 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
d13fd3e4
VZ
122 {
123 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
124 int idx = m_color.pixel;
125 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
126 }
127 else
128 {
129 m_hasPixel = gdk_color_alloc( cmap, &m_color );
130 }
131 m_colormap = cmap;
132}
133
c801d85f
KB
134//-----------------------------------------------------------------------------
135
136#define M_COLDATA ((wxColourRefData *)m_refData)
137
3b9faf4c
VS
138// GDK's values are in 0..65535 range, our are in 0..255
139#define SHIFT 8
c801d85f
KB
140
141IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
142
68dda785 143wxColour::~wxColour()
c801d85f 144{
ff7b1510 145}
c801d85f 146
33b64e6f 147bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 148{
4e6b8309 149 if (m_refData == col.m_refData)
aad6765c 150 return true;
15248e43 151
4e6b8309 152 if (!m_refData || !col.m_refData)
aad6765c 153 return false;
15248e43 154
2b62ab35
RR
155 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
156 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
4e6b8309
VZ
157 return own->red == other->red &&
158 own->blue == other->blue &&
159 own->green == other->green;
ff7b1510 160}
c801d85f 161
c89f5c02
RR
162wxObjectRefData *wxColour::CreateRefData() const
163{
164 return new wxColourRefData;
165}
166
167wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 168{
c89f5c02 169 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 170}
c801d85f 171
aea95b1c
VZ
172void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
173 unsigned char WXUNUSED(alpha))
c801d85f 174{
c89f5c02 175 AllocExclusive();
4e6b8309 176
1ecc4d80
RR
177 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
178 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
179 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
180 M_COLDATA->m_color.pixel = 0;
4e6b8309 181
2b5f62a0 182 M_COLDATA->m_colormap = (GdkColormap*) NULL;
aad6765c 183 M_COLDATA->m_hasPixel = false;
ff7b1510 184}
c801d85f 185
68dda785 186unsigned char wxColour::Red() const
c801d85f 187{
223d09f6 188 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 189
1ecc4d80 190 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 191}
c801d85f 192
68dda785 193unsigned char wxColour::Green() const
c801d85f 194{
223d09f6 195 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 196
1ecc4d80 197 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 198}
c801d85f 199
68dda785 200unsigned char wxColour::Blue() const
c801d85f 201{
223d09f6 202 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 203
1ecc4d80 204 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 205}
c801d85f 206
c801d85f
KB
207void wxColour::CalcPixel( GdkColormap *cmap )
208{
1ecc4d80 209 if (!Ok()) return;
8bbe427f 210
d13fd3e4 211 M_COLDATA->AllocColour( cmap );
ff7b1510 212}
c801d85f 213
68dda785 214int wxColour::GetPixel() const
c801d85f 215{
223d09f6 216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 217
1ecc4d80 218 return M_COLDATA->m_color.pixel;
ff7b1510 219}
c801d85f 220
68dda785 221GdkColor *wxColour::GetColor() const
c801d85f 222{
223d09f6 223 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 224
1ecc4d80 225 return &M_COLDATA->m_color;
ff7b1510 226}
40989e46
WS
227
228bool wxColour::FromString(const wxChar *str)
229{
230 GdkColor colGDK;
9eb49b58 231 if ( gdk_color_parse( str, &colGDK ) )
40989e46
WS
232 {
233 UnRef();
234
235 m_refData = new wxColourRefData;
236 M_COLDATA->m_color = colGDK;
237 return true;
238 }
239
240 return wxColourBase::FromString(str);
241}