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