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