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