]>
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 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "colour.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/gdicmn.h" | |
16 | ||
071a2d78 | 17 | #include <gdk/gdk.h> |
307fc8d5 | 18 | #include <gdk/gdkx.h> |
071a2d78 | 19 | #include <gdk/gdkprivate.h> |
c801d85f KB |
20 | |
21 | //----------------------------------------------------------------------------- | |
22 | // wxColour | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | class wxColourRefData: public wxObjectRefData | |
26 | { | |
47c93b63 | 27 | public: |
68dda785 VZ |
28 | wxColourRefData(); |
29 | ~wxColourRefData(); | |
15248e43 | 30 | |
68dda785 | 31 | void FreeColour(); |
d13fd3e4 | 32 | void AllocColour( GdkColormap* cmap ); |
8bbe427f | 33 | |
47c93b63 | 34 | public: |
c801d85f KB |
35 | GdkColor m_color; |
36 | GdkColormap *m_colormap; | |
37 | bool m_hasPixel; | |
8bbe427f | 38 | |
f6bcfd97 | 39 | friend class wxColour; |
d13fd3e4 VZ |
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 | |
c801d85f KB |
60 | }; |
61 | ||
68dda785 | 62 | wxColourRefData::wxColourRefData() |
c801d85f | 63 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 70 | } |
c801d85f | 71 | |
68dda785 | 72 | wxColourRefData::~wxColourRefData() |
c801d85f | 73 | { |
1ecc4d80 | 74 | FreeColour(); |
ff7b1510 | 75 | } |
c801d85f | 76 | |
68dda785 | 77 | void wxColourRefData::FreeColour() |
c801d85f | 78 | { |
47c93b63 RR |
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 | { | |
d13fd3e4 VZ |
90 | int idx = m_color.pixel; |
91 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
92 | ||
93 | if (colMapAllocCounter[ idx ] == 0) | |
47c93b63 RR |
94 | gdk_colormap_free_colors( m_colormap, &m_color, 1 ); |
95 | } | |
96 | } | |
ff7b1510 | 97 | } |
c801d85f | 98 | |
d13fd3e4 VZ |
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 | ||
c801d85f KB |
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 | ||
68dda785 | 134 | wxColour::wxColour() |
c801d85f | 135 | { |
ff7b1510 | 136 | } |
c801d85f | 137 | |
4b5f3fe6 | 138 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 139 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 145 | } |
8bbe427f | 146 | |
68dda785 | 147 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 148 | { |
1ecc4d80 RR |
149 | wxNode *node = (wxNode *) NULL; |
150 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) | |
c801d85f | 151 | { |
1ecc4d80 RR |
152 | wxColour *col = (wxColour*)node->Data(); |
153 | UnRef(); | |
154 | if (col) Ref( *col ); | |
155 | } | |
156 | else | |
157 | { | |
158 | m_refData = new wxColourRefData(); | |
93c5dd39 | 159 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) |
1ecc4d80 | 160 | { |
15248e43 VZ |
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 | ||
1ecc4d80 RR |
170 | delete m_refData; |
171 | m_refData = (wxObjectRefData *) NULL; | |
172 | } | |
ff7b1510 | 173 | } |
ff7b1510 | 174 | } |
c801d85f KB |
175 | |
176 | wxColour::wxColour( const wxColour& col ) | |
8bbe427f | 177 | { |
1ecc4d80 | 178 | Ref( col ); |
ff7b1510 | 179 | } |
c801d85f | 180 | |
68dda785 | 181 | wxColour::~wxColour() |
c801d85f | 182 | { |
ff7b1510 | 183 | } |
c801d85f | 184 | |
8bbe427f VZ |
185 | wxColour& wxColour::operator = ( const wxColour& col ) |
186 | { | |
1ecc4d80 RR |
187 | if (*this == col) return (*this); |
188 | Ref( col ); | |
189 | return *this; | |
ff7b1510 | 190 | } |
c801d85f | 191 | |
33b64e6f | 192 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 193 | { |
2b62ab35 | 194 | if (m_refData == col.m_refData) return TRUE; |
15248e43 | 195 | |
2b62ab35 RR |
196 | if (!m_refData) return FALSE; |
197 | if (!col.m_refData) return FALSE; | |
15248e43 | 198 | |
2b62ab35 RR |
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; | |
15248e43 | 204 | |
2b62ab35 | 205 | return TRUE; |
ff7b1510 | 206 | } |
c801d85f | 207 | |
33b64e6f | 208 | bool wxColour::operator != ( const wxColour& col) const |
8bbe427f | 209 | { |
f6bcfd97 | 210 | return !(*this == col); |
ff7b1510 | 211 | } |
c801d85f | 212 | |
4b5f3fe6 | 213 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 214 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 221 | } |
c801d85f | 222 | |
68dda785 | 223 | unsigned char wxColour::Red() const |
c801d85f | 224 | { |
223d09f6 | 225 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 226 | |
1ecc4d80 | 227 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 228 | } |
c801d85f | 229 | |
68dda785 | 230 | unsigned char wxColour::Green() const |
c801d85f | 231 | { |
223d09f6 | 232 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 233 | |
1ecc4d80 | 234 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 235 | } |
c801d85f | 236 | |
68dda785 | 237 | unsigned char wxColour::Blue() const |
c801d85f | 238 | { |
223d09f6 | 239 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 240 | |
1ecc4d80 | 241 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 242 | } |
c801d85f | 243 | |
68dda785 | 244 | bool wxColour::Ok() const |
c801d85f | 245 | { |
1ecc4d80 | 246 | return (m_refData != NULL); |
ff7b1510 | 247 | } |
c801d85f KB |
248 | |
249 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
250 | { | |
1ecc4d80 | 251 | if (!Ok()) return; |
8bbe427f | 252 | |
d13fd3e4 | 253 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 254 | } |
c801d85f | 255 | |
68dda785 | 256 | int wxColour::GetPixel() const |
c801d85f | 257 | { |
223d09f6 | 258 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 259 | |
1ecc4d80 | 260 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 261 | } |
c801d85f | 262 | |
68dda785 | 263 | GdkColor *wxColour::GetColor() const |
c801d85f | 264 | { |
223d09f6 | 265 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 266 | |
1ecc4d80 | 267 | return &M_COLDATA->m_color; |
ff7b1510 | 268 | } |
c801d85f KB |
269 | |
270 |