]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
fixed wxBase and GUI separation for sockets code
[wxWidgets.git] / src / gtk1 / colour.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: colour.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
8bbe427f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "colour.h"
13#endif
14
15#include "wx/gdicmn.h"
ed39ff57 16#include "wx/colour.h"
2b5f62a0 17#include "wx/gtk/private.h"
c801d85f 18
071a2d78 19#include <gdk/gdk.h>
307fc8d5 20#include <gdk/gdkx.h>
071a2d78 21#include <gdk/gdkprivate.h>
c801d85f
KB
22
23//-----------------------------------------------------------------------------
24// wxColour
25//-----------------------------------------------------------------------------
26
27class wxColourRefData: public wxObjectRefData
28{
47c93b63 29public:
c89f5c02
RR
30 wxColourRefData()
31 {
32 m_color.red = 0;
33 m_color.green = 0;
34 m_color.blue = 0;
35 m_color.pixel = 0;
36 m_colormap = (GdkColormap *) NULL;
37 m_hasPixel = FALSE;
38 }
39
dbb846c6 40 wxColourRefData(const wxColourRefData& data)
d84afea9 41 : wxObjectRefData()
dbb846c6
VZ
42 {
43 m_color = data.m_color;
44 m_colormap = data.m_colormap;
45 m_hasPixel = data.m_hasPixel;
46 }
47
c89f5c02
RR
48 ~wxColourRefData()
49 {
50 FreeColour();
51 }
15248e43 52
c89f5c02
RR
53 bool operator == (const wxColourRefData& data) const
54 {
55 return (m_colormap == data.m_colormap &&
56 m_hasPixel == data.m_hasPixel &&
57 m_color.red == data.m_color.red &&
58 m_color.green == data.m_color.green &&
59 m_color.blue == data.m_color.blue &&
60 m_color.pixel == data.m_color.pixel);
61 }
62
68dda785 63 void FreeColour();
d13fd3e4 64 void AllocColour( GdkColormap* cmap );
8bbe427f 65
c801d85f
KB
66 GdkColor m_color;
67 GdkColormap *m_colormap;
68 bool m_hasPixel;
8bbe427f 69
f6bcfd97 70 friend class wxColour;
d13fd3e4
VZ
71
72 // reference counter for systems with <= 8-Bit display
73 static gushort colMapAllocCounter[ 256 ];
74};
75
76gushort wxColourRefData::colMapAllocCounter[ 256 ] =
77{
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, 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
c801d85f
KB
91};
92
68dda785 93void wxColourRefData::FreeColour()
c801d85f 94{
47c93b63
RR
95 if (m_colormap)
96 {
97#ifdef __WXGTK20__
98 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
99 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
100#else
101 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
102 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
103 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
104#endif
105 {
d13fd3e4
VZ
106 int idx = m_color.pixel;
107 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
108
109 if (colMapAllocCounter[ idx ] == 0)
47c93b63
RR
110 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
111 }
112 }
ff7b1510 113}
c801d85f 114
d13fd3e4
VZ
115void wxColourRefData::AllocColour( GdkColormap *cmap )
116{
117 if (m_hasPixel && (m_colormap == cmap))
118 return;
119
120 FreeColour();
121
122#ifdef __WXGTK20__
31c8af3a
VZ
123 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
124 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
d13fd3e4
VZ
125#else
126 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
127 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
128 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
129#endif
130 {
131 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
132 int idx = m_color.pixel;
133 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
134 }
135 else
136 {
137 m_hasPixel = gdk_color_alloc( cmap, &m_color );
138 }
139 m_colormap = cmap;
140}
141
c801d85f
KB
142//-----------------------------------------------------------------------------
143
144#define M_COLDATA ((wxColourRefData *)m_refData)
145
146#define SHIFT (8*(sizeof(short int)-sizeof(char)))
147
148IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
149
4b5f3fe6 150wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 151{
1ecc4d80
RR
152 m_refData = new wxColourRefData();
153 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
154 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
155 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
156 M_COLDATA->m_color.pixel = 0;
ff7b1510 157}
8bbe427f 158
222ed1d6
MB
159
160
68dda785 161void wxColour::InitFromName( const wxString &colourName )
c801d85f 162{
222ed1d6
MB
163 wxColour* col = NULL;
164 if ( (wxTheColourDatabase) && (col = wxTheColourDatabase->FindColourNoAdd(colourName)) )
c801d85f 165 {
1ecc4d80
RR
166 UnRef();
167 if (col) Ref( *col );
168 }
169 else
170 {
171 m_refData = new wxColourRefData();
887dd52f 172
2b5f62a0 173 if (!gdk_color_parse( wxGTK_CONV( colourName ), &M_COLDATA->m_color ))
1ecc4d80 174 {
15248e43
VZ
175 // VZ: asserts are good in general but this one is triggered by
176 // calling wxColourDatabase::FindColour() with an
177 // unrecognized colour name and this can't be avoided from the
178 // user code, so don't give it here
179 //
180 // a better solution would be to changed code in FindColour()
181
182 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
183
1ecc4d80
RR
184 delete m_refData;
185 m_refData = (wxObjectRefData *) NULL;
186 }
ff7b1510 187 }
ff7b1510 188}
c801d85f 189
68dda785 190wxColour::~wxColour()
c801d85f 191{
ff7b1510 192}
c801d85f 193
33b64e6f 194bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 195{
2b62ab35 196 if (m_refData == col.m_refData) return TRUE;
15248e43 197
c89f5c02 198 if (!m_refData || !col.m_refData) return FALSE;
15248e43 199
2b62ab35
RR
200 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
201 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
202 if (own->red != other->red) return FALSE;
203 if (own->blue != other->blue) return FALSE;
204 if (own->green != other->green) return FALSE;
15248e43 205
2b62ab35 206 return TRUE;
ff7b1510 207}
c801d85f 208
c89f5c02
RR
209wxObjectRefData *wxColour::CreateRefData() const
210{
211 return new wxColourRefData;
212}
213
214wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 215{
c89f5c02 216 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 217}
c801d85f 218
4b5f3fe6 219void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 220{
c89f5c02
RR
221 AllocExclusive();
222
1ecc4d80
RR
223 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
224 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
225 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
226 M_COLDATA->m_color.pixel = 0;
2b5f62a0
VZ
227
228 M_COLDATA->m_colormap = (GdkColormap*) NULL;
229 M_COLDATA->m_hasPixel = FALSE;
ff7b1510 230}
c801d85f 231
68dda785 232unsigned char wxColour::Red() const
c801d85f 233{
223d09f6 234 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 235
1ecc4d80 236 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 237}
c801d85f 238
68dda785 239unsigned char wxColour::Green() const
c801d85f 240{
223d09f6 241 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 242
1ecc4d80 243 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 244}
c801d85f 245
68dda785 246unsigned char wxColour::Blue() const
c801d85f 247{
223d09f6 248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 249
1ecc4d80 250 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 251}
c801d85f 252
c801d85f
KB
253void wxColour::CalcPixel( GdkColormap *cmap )
254{
1ecc4d80 255 if (!Ok()) return;
8bbe427f 256
d13fd3e4 257 M_COLDATA->AllocColour( cmap );
ff7b1510 258}
c801d85f 259
68dda785 260int wxColour::GetPixel() const
c801d85f 261{
223d09f6 262 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 263
1ecc4d80 264 return M_COLDATA->m_color.pixel;
ff7b1510 265}
c801d85f 266
68dda785 267GdkColor *wxColour::GetColor() const
c801d85f 268{
223d09f6 269 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
8bbe427f 270
1ecc4d80 271 return &M_COLDATA->m_color;
ff7b1510 272}
c801d85f
KB
273
274