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