]>
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: |
c89f5c02 RR |
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 | } | |
15248e43 | 42 | |
c89f5c02 RR |
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 | ||
68dda785 | 53 | void FreeColour(); |
d13fd3e4 | 54 | void AllocColour( GdkColormap* cmap ); |
8bbe427f | 55 | |
c801d85f KB |
56 | GdkColor m_color; |
57 | GdkColormap *m_colormap; | |
58 | bool m_hasPixel; | |
8bbe427f | 59 | |
f6bcfd97 | 60 | friend class wxColour; |
d13fd3e4 VZ |
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 | |
c801d85f KB |
81 | }; |
82 | ||
68dda785 | 83 | void wxColourRefData::FreeColour() |
c801d85f | 84 | { |
47c93b63 RR |
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 | { | |
d13fd3e4 VZ |
96 | int idx = m_color.pixel; |
97 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
98 | ||
99 | if (colMapAllocCounter[ idx ] == 0) | |
47c93b63 RR |
100 | gdk_colormap_free_colors( m_colormap, &m_color, 1 ); |
101 | } | |
102 | } | |
ff7b1510 | 103 | } |
c801d85f | 104 | |
d13fd3e4 VZ |
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 | ||
c801d85f KB |
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 | ||
4b5f3fe6 | 140 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 141 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 147 | } |
8bbe427f | 148 | |
68dda785 | 149 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 150 | { |
1ecc4d80 RR |
151 | wxNode *node = (wxNode *) NULL; |
152 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) | |
c801d85f | 153 | { |
1ecc4d80 RR |
154 | wxColour *col = (wxColour*)node->Data(); |
155 | UnRef(); | |
156 | if (col) Ref( *col ); | |
157 | } | |
158 | else | |
159 | { | |
160 | m_refData = new wxColourRefData(); | |
887dd52f | 161 | |
93c5dd39 | 162 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) |
1ecc4d80 | 163 | { |
15248e43 VZ |
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 | ||
1ecc4d80 RR |
173 | delete m_refData; |
174 | m_refData = (wxObjectRefData *) NULL; | |
175 | } | |
ff7b1510 | 176 | } |
ff7b1510 | 177 | } |
c801d85f | 178 | |
68dda785 | 179 | wxColour::~wxColour() |
c801d85f | 180 | { |
ff7b1510 | 181 | } |
c801d85f | 182 | |
33b64e6f | 183 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 184 | { |
2b62ab35 | 185 | if (m_refData == col.m_refData) return TRUE; |
15248e43 | 186 | |
c89f5c02 | 187 | if (!m_refData || !col.m_refData) return FALSE; |
15248e43 | 188 | |
2b62ab35 RR |
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; | |
15248e43 | 194 | |
2b62ab35 | 195 | return TRUE; |
ff7b1510 | 196 | } |
c801d85f | 197 | |
c89f5c02 RR |
198 | wxObjectRefData *wxColour::CreateRefData() const |
199 | { | |
200 | return new wxColourRefData; | |
201 | } | |
202 | ||
203 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const | |
8bbe427f | 204 | { |
c89f5c02 | 205 | return new wxColourRefData(*(wxColourRefData *)data); |
ff7b1510 | 206 | } |
c801d85f | 207 | |
4b5f3fe6 | 208 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 209 | { |
c89f5c02 RR |
210 | AllocExclusive(); |
211 | ||
1ecc4d80 RR |
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; | |
ff7b1510 | 217 | } |
c801d85f | 218 | |
68dda785 | 219 | unsigned char wxColour::Red() const |
c801d85f | 220 | { |
223d09f6 | 221 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 222 | |
1ecc4d80 | 223 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 224 | } |
c801d85f | 225 | |
68dda785 | 226 | unsigned char wxColour::Green() const |
c801d85f | 227 | { |
223d09f6 | 228 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 229 | |
1ecc4d80 | 230 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 231 | } |
c801d85f | 232 | |
68dda785 | 233 | unsigned char wxColour::Blue() const |
c801d85f | 234 | { |
223d09f6 | 235 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 236 | |
1ecc4d80 | 237 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 238 | } |
c801d85f | 239 | |
c801d85f KB |
240 | void wxColour::CalcPixel( GdkColormap *cmap ) |
241 | { | |
1ecc4d80 | 242 | if (!Ok()) return; |
8bbe427f | 243 | |
d13fd3e4 | 244 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 245 | } |
c801d85f | 246 | |
68dda785 | 247 | int wxColour::GetPixel() const |
c801d85f | 248 | { |
223d09f6 | 249 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 250 | |
1ecc4d80 | 251 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 252 | } |
c801d85f | 253 | |
68dda785 | 254 | GdkColor *wxColour::GetColor() const |
c801d85f | 255 | { |
223d09f6 | 256 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 257 | |
1ecc4d80 | 258 | return &M_COLDATA->m_color; |
ff7b1510 | 259 | } |
c801d85f KB |
260 | |
261 |