]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
fix the definition of many static functions marked as 'const' or functions which...
[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
8f884a0d 29class wxColourRefData : public wxGDIRefData
c801d85f 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
VZ
42 wxColourRefData(const wxColourRefData& data)
43 {
44 m_color = data.m_color;
45 m_colormap = data.m_colormap;
46 m_hasPixel = data.m_hasPixel;
47 }
48
d3c7fc99 49 virtual ~wxColourRefData()
c89f5c02
RR
50 {
51 FreeColour();
52 }
15248e43 53
c89f5c02
RR
54 bool operator == (const wxColourRefData& data) const
55 {
56 return (m_colormap == data.m_colormap &&
57 m_hasPixel == data.m_hasPixel &&
58 m_color.red == data.m_color.red &&
59 m_color.green == data.m_color.green &&
60 m_color.blue == data.m_color.blue &&
61 m_color.pixel == data.m_color.pixel);
62 }
4e6b8309 63
68dda785 64 void FreeColour();
d13fd3e4 65 void AllocColour( GdkColormap* cmap );
8bbe427f 66
c801d85f
KB
67 GdkColor m_color;
68 GdkColormap *m_colormap;
69 bool m_hasPixel;
8bbe427f 70
f6bcfd97 71 friend class wxColour;
d13fd3e4
VZ
72
73 // reference counter for systems with <= 8-Bit display
74 static gushort colMapAllocCounter[ 256 ];
75};
76
4e6b8309
VZ
77gushort wxColourRefData::colMapAllocCounter[ 256 ] =
78{
d13fd3e4
VZ
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, 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
c801d85f
KB
92};
93
68dda785 94void wxColourRefData::FreeColour()
c801d85f 95{
47c93b63
RR
96 if (m_colormap)
97 {
47c93b63
RR
98 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
99 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
100 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
47c93b63 101 {
d13fd3e4
VZ
102 int idx = m_color.pixel;
103 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
4e6b8309 104
d13fd3e4 105 if (colMapAllocCounter[ idx ] == 0)
0a164d4c 106 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
47c93b63
RR
107 }
108 }
ff7b1510 109}
c801d85f 110
d13fd3e4
VZ
111void wxColourRefData::AllocColour( GdkColormap *cmap )
112{
113 if (m_hasPixel && (m_colormap == cmap))
114 return;
115
116 FreeColour();
4e6b8309 117
7e52dfd2
MW
118 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
119 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
120 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
d13fd3e4
VZ
121 {
122 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
123 int idx = m_color.pixel;
124 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
125 }
126 else
127 {
128 m_hasPixel = gdk_color_alloc( cmap, &m_color );
129 }
130 m_colormap = cmap;
131}
132
c801d85f
KB
133//-----------------------------------------------------------------------------
134
135#define M_COLDATA ((wxColourRefData *)m_refData)
136
3b9faf4c
VS
137// GDK's values are in 0..65535 range, our are in 0..255
138#define SHIFT 8
c801d85f
KB
139
140IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
141
68dda785 142wxColour::~wxColour()
c801d85f 143{
ff7b1510 144}
c801d85f 145
33b64e6f 146bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 147{
4e6b8309 148 if (m_refData == col.m_refData)
aad6765c 149 return true;
15248e43 150
4e6b8309 151 if (!m_refData || !col.m_refData)
aad6765c 152 return false;
15248e43 153
2b62ab35
RR
154 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
155 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
4e6b8309
VZ
156 return own->red == other->red &&
157 own->blue == other->blue &&
158 own->green == other->green;
ff7b1510 159}
c801d85f 160
8f884a0d 161wxGDIRefData *wxColour::CreateGDIRefData() const
c89f5c02
RR
162{
163 return new wxColourRefData;
164}
165
8f884a0d 166wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
8bbe427f 167{
c89f5c02 168 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 169}
c801d85f 170
aea95b1c
VZ
171void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
172 unsigned char WXUNUSED(alpha))
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 226
e86d4e59 227bool wxColour::FromString(const wxString& str)
40989e46
WS
228{
229 GdkColor colGDK;
e86d4e59 230 if ( gdk_color_parse( wxGTK_CONV(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}