]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
added build options sanity checks into all main libraries
[wxWidgets.git] / src / gtk1 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "colour.h"
13 #endif
14
15 #include "wx/gdicmn.h"
16 #include "wx/colour.h"
17 #include "wx/gtk/private.h"
18
19 #include <gdk/gdk.h>
20 #include <gdk/gdkx.h>
21 #include <gdk/gdkprivate.h>
22
23 //-----------------------------------------------------------------------------
24 // wxColour
25 //-----------------------------------------------------------------------------
26
27 class wxColourRefData: public wxObjectRefData
28 {
29 public:
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
40 wxColourRefData(const wxColourRefData& data)
41 : wxObjectRefData()
42 {
43 m_color = data.m_color;
44 m_colormap = data.m_colormap;
45 m_hasPixel = data.m_hasPixel;
46 }
47
48 ~wxColourRefData()
49 {
50 FreeColour();
51 }
52
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
63 void FreeColour();
64 void AllocColour( GdkColormap* cmap );
65
66 GdkColor m_color;
67 GdkColormap *m_colormap;
68 bool m_hasPixel;
69
70 friend class wxColour;
71
72 // reference counter for systems with <= 8-Bit display
73 static gushort colMapAllocCounter[ 256 ];
74 };
75
76 gushort 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
91 };
92
93 void wxColourRefData::FreeColour()
94 {
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 {
106 int idx = m_color.pixel;
107 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
108
109 if (colMapAllocCounter[ idx ] == 0)
110 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
111 }
112 }
113 }
114
115 void wxColourRefData::AllocColour( GdkColormap *cmap )
116 {
117 if (m_hasPixel && (m_colormap == cmap))
118 return;
119
120 FreeColour();
121
122 #ifdef __WXGTK20__
123 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
124 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
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
142 //-----------------------------------------------------------------------------
143
144 #define M_COLDATA ((wxColourRefData *)m_refData)
145
146 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
147
148 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
149
150 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
151 {
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;
157 }
158
159
160
161 void wxColour::InitFromName( const wxString &colourName )
162 {
163 wxColour* col = NULL;
164 if ( (wxTheColourDatabase) && (col = wxTheColourDatabase->FindColourNoAdd(colourName)) )
165 {
166 UnRef();
167 if (col) Ref( *col );
168 }
169 else
170 {
171 m_refData = new wxColourRefData();
172
173 if (!gdk_color_parse( wxGTK_CONV( colourName ), &M_COLDATA->m_color ))
174 {
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
184 delete m_refData;
185 m_refData = (wxObjectRefData *) NULL;
186 }
187 }
188 }
189
190 wxColour::~wxColour()
191 {
192 }
193
194 bool wxColour::operator == ( const wxColour& col ) const
195 {
196 if (m_refData == col.m_refData) return TRUE;
197
198 if (!m_refData || !col.m_refData) return FALSE;
199
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;
205
206 return TRUE;
207 }
208
209 wxObjectRefData *wxColour::CreateRefData() const
210 {
211 return new wxColourRefData;
212 }
213
214 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
215 {
216 return new wxColourRefData(*(wxColourRefData *)data);
217 }
218
219 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
220 {
221 AllocExclusive();
222
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;
227
228 M_COLDATA->m_colormap = (GdkColormap*) NULL;
229 M_COLDATA->m_hasPixel = FALSE;
230 }
231
232 unsigned char wxColour::Red() const
233 {
234 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
235
236 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
237 }
238
239 unsigned char wxColour::Green() const
240 {
241 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
242
243 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
244 }
245
246 unsigned char wxColour::Blue() const
247 {
248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
249
250 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
251 }
252
253 void wxColour::CalcPixel( GdkColormap *cmap )
254 {
255 if (!Ok()) return;
256
257 M_COLDATA->AllocColour( cmap );
258 }
259
260 int wxColour::GetPixel() const
261 {
262 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
263
264 return M_COLDATA->m_color.pixel;
265 }
266
267 GdkColor *wxColour::GetColor() const
268 {
269 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
270
271 return &M_COLDATA->m_color;
272 }
273
274