]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
updates from Adrián González Alba
[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
c89f5c02
RR
50 ~wxColourRefData()
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
40989e46 172void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 173{
c89f5c02 174 AllocExclusive();
4e6b8309 175
1ecc4d80
RR
176 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
177 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
178 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
179 M_COLDATA->m_color.pixel = 0;
4e6b8309 180
2b5f62a0 181 M_COLDATA->m_colormap = (GdkColormap*) NULL;
aad6765c 182 M_COLDATA->m_hasPixel = false;
ff7b1510 183}
c801d85f 184
68dda785 185unsigned char wxColour::Red() const
c801d85f 186{
223d09f6 187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 188
1ecc4d80 189 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 190}
c801d85f 191
68dda785 192unsigned char wxColour::Green() const
c801d85f 193{
223d09f6 194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 195
1ecc4d80 196 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 197}
c801d85f 198
68dda785 199unsigned char wxColour::Blue() const
c801d85f 200{
223d09f6 201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 202
1ecc4d80 203 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 204}
c801d85f 205
c801d85f
KB
206void wxColour::CalcPixel( GdkColormap *cmap )
207{
1ecc4d80 208 if (!Ok()) return;
8bbe427f 209
d13fd3e4 210 M_COLDATA->AllocColour( cmap );
ff7b1510 211}
c801d85f 212
68dda785 213int wxColour::GetPixel() const
c801d85f 214{
223d09f6 215 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 216
1ecc4d80 217 return M_COLDATA->m_color.pixel;
ff7b1510 218}
c801d85f 219
68dda785 220GdkColor *wxColour::GetColor() const
c801d85f 221{
223d09f6 222 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 223
1ecc4d80 224 return &M_COLDATA->m_color;
ff7b1510 225}
40989e46
WS
226
227bool wxColour::FromString(const wxChar *str)
228{
229 GdkColor colGDK;
9eb49b58 230 if ( gdk_color_parse( str, &colGDK ) )
40989e46
WS
231 {
232 UnRef();
233
234 m_refData = new wxColourRefData;
235 M_COLDATA->m_color = colGDK;
236 return true;
237 }
238
239 return wxColourBase::FromString(str);
240}