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