Remove GTK2 stuff from src/gtk1. Rename wx/gtk includes to wx/gtk1.
[wxWidgets.git] / src / gtk1 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/colour.h"
14 #include "wx/gdicmn.h"
15 #include "wx/gtk1/private.h"
16
17 #include <gdk/gdk.h>
18 #include <gdk/gdkx.h>
19 #include <gdk/gdkprivate.h>
20
21 //-----------------------------------------------------------------------------
22 // wxColour
23 //-----------------------------------------------------------------------------
24
25 class wxColourRefData: public wxObjectRefData
26 {
27 public:
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
38 wxColourRefData(const wxColourRefData& data)
39 : wxObjectRefData()
40 {
41 m_color = data.m_color;
42 m_colormap = data.m_colormap;
43 m_hasPixel = data.m_hasPixel;
44 }
45
46 ~wxColourRefData()
47 {
48 FreeColour();
49 }
50
51 bool operator == (const wxColourRefData& data) const
52 {
53 return (m_colormap == data.m_colormap &&
54 m_hasPixel == data.m_hasPixel &&
55 m_color.red == data.m_color.red &&
56 m_color.green == data.m_color.green &&
57 m_color.blue == data.m_color.blue &&
58 m_color.pixel == data.m_color.pixel);
59 }
60
61 void FreeColour();
62 void AllocColour( GdkColormap* cmap );
63
64 GdkColor m_color;
65 GdkColormap *m_colormap;
66 bool m_hasPixel;
67
68 friend class wxColour;
69
70 // reference counter for systems with <= 8-Bit display
71 static gushort colMapAllocCounter[ 256 ];
72 };
73
74 gushort wxColourRefData::colMapAllocCounter[ 256 ] =
75 {
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, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
89 };
90
91 void wxColourRefData::FreeColour()
92 {
93 if (m_colormap)
94 {
95 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
96 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
97 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
98 {
99 int idx = m_color.pixel;
100 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
101
102 if (colMapAllocCounter[ idx ] == 0)
103 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
104 }
105 }
106 }
107
108 void wxColourRefData::AllocColour( GdkColormap *cmap )
109 {
110 if (m_hasPixel && (m_colormap == cmap))
111 return;
112
113 FreeColour();
114
115 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
116 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
117 {
118 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
119 int idx = m_color.pixel;
120 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
121 }
122 else
123 {
124 m_hasPixel = gdk_color_alloc( cmap, &m_color );
125 }
126 m_colormap = cmap;
127 }
128
129 //-----------------------------------------------------------------------------
130
131 #define M_COLDATA ((wxColourRefData *)m_refData)
132
133 // GDK's values are in 0..65535 range, our are in 0..255
134 #define SHIFT 8
135
136 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
137
138 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
139 {
140 m_refData = new wxColourRefData();
141 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
142 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
143 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
144 M_COLDATA->m_color.pixel = 0;
145 }
146
147 /* static */
148 wxColour wxColour::CreateByName(const wxString& name)
149 {
150 wxColour col;
151
152 GdkColor colGDK;
153 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
154 {
155 wxColourRefData *refData = new wxColourRefData;
156 refData->m_color = colGDK;
157 col.m_refData = refData;
158 }
159
160 return col;
161 }
162
163
164 void wxColour::InitFromName( const wxString &colourName )
165 {
166 // check the cache first
167 wxColour col;
168 if ( wxTheColourDatabase )
169 {
170 col = wxTheColourDatabase->Find(colourName);
171 }
172
173 if ( !col.Ok() )
174 {
175 col = CreateByName(colourName);
176 }
177
178 if ( col.Ok() )
179 {
180 *this = col;
181 }
182 else
183 {
184 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
185 }
186 }
187
188 wxColour::~wxColour()
189 {
190 }
191
192 bool wxColour::operator == ( const wxColour& col ) const
193 {
194 if (m_refData == col.m_refData)
195 return true;
196
197 if (!m_refData || !col.m_refData)
198 return false;
199
200 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
201 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
202 return own->red == other->red &&
203 own->blue == other->blue &&
204 own->green == other->green;
205 }
206
207 wxObjectRefData *wxColour::CreateRefData() const
208 {
209 return new wxColourRefData;
210 }
211
212 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
213 {
214 return new wxColourRefData(*(wxColourRefData *)data);
215 }
216
217 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
218 {
219 AllocExclusive();
220
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;
225
226 M_COLDATA->m_colormap = (GdkColormap*) NULL;
227 M_COLDATA->m_hasPixel = false;
228 }
229
230 unsigned char wxColour::Red() const
231 {
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
233
234 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
235 }
236
237 unsigned char wxColour::Green() const
238 {
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
240
241 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
242 }
243
244 unsigned char wxColour::Blue() const
245 {
246 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
247
248 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
249 }
250
251 void wxColour::CalcPixel( GdkColormap *cmap )
252 {
253 if (!Ok()) return;
254
255 M_COLDATA->AllocColour( cmap );
256 }
257
258 int wxColour::GetPixel() const
259 {
260 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
261
262 return M_COLDATA->m_color.pixel;
263 }
264
265 GdkColor *wxColour::GetColor() const
266 {
267 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
268
269 return &M_COLDATA->m_color;
270 }