]>
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(); | |
93c5dd39 | 161 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) |
1ecc4d80 | 162 | { |
15248e43 VZ |
163 | // VZ: asserts are good in general but this one is triggered by |
164 | // calling wxColourDatabase::FindColour() with an | |
165 | // unrecognized colour name and this can't be avoided from the | |
166 | // user code, so don't give it here | |
167 | // | |
168 | // a better solution would be to changed code in FindColour() | |
169 | ||
170 | //wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); | |
171 | ||
1ecc4d80 RR |
172 | delete m_refData; |
173 | m_refData = (wxObjectRefData *) NULL; | |
174 | } | |
ff7b1510 | 175 | } |
ff7b1510 | 176 | } |
c801d85f | 177 | |
68dda785 | 178 | wxColour::~wxColour() |
c801d85f | 179 | { |
ff7b1510 | 180 | } |
c801d85f | 181 | |
33b64e6f | 182 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 183 | { |
2b62ab35 | 184 | if (m_refData == col.m_refData) return TRUE; |
15248e43 | 185 | |
c89f5c02 | 186 | if (!m_refData || !col.m_refData) return FALSE; |
15248e43 | 187 | |
2b62ab35 RR |
188 | GdkColor *own = &(((wxColourRefData*)m_refData)->m_color); |
189 | GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
190 | if (own->red != other->red) return FALSE; | |
191 | if (own->blue != other->blue) return FALSE; | |
192 | if (own->green != other->green) return FALSE; | |
15248e43 | 193 | |
2b62ab35 | 194 | return TRUE; |
ff7b1510 | 195 | } |
c801d85f | 196 | |
c89f5c02 RR |
197 | wxObjectRefData *wxColour::CreateRefData() const |
198 | { | |
199 | return new wxColourRefData; | |
200 | } | |
201 | ||
202 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const | |
8bbe427f | 203 | { |
c89f5c02 | 204 | return new wxColourRefData(*(wxColourRefData *)data); |
ff7b1510 | 205 | } |
c801d85f | 206 | |
4b5f3fe6 | 207 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 208 | { |
c89f5c02 RR |
209 | AllocExclusive(); |
210 | ||
1ecc4d80 RR |
211 | m_refData = new wxColourRefData(); |
212 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
213 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
214 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
215 | M_COLDATA->m_color.pixel = 0; | |
ff7b1510 | 216 | } |
c801d85f | 217 | |
68dda785 | 218 | unsigned char wxColour::Red() const |
c801d85f | 219 | { |
223d09f6 | 220 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 221 | |
1ecc4d80 | 222 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 223 | } |
c801d85f | 224 | |
68dda785 | 225 | unsigned char wxColour::Green() const |
c801d85f | 226 | { |
223d09f6 | 227 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 228 | |
1ecc4d80 | 229 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 230 | } |
c801d85f | 231 | |
68dda785 | 232 | unsigned char wxColour::Blue() const |
c801d85f | 233 | { |
223d09f6 | 234 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 235 | |
1ecc4d80 | 236 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 237 | } |
c801d85f | 238 | |
c801d85f KB |
239 | void wxColour::CalcPixel( GdkColormap *cmap ) |
240 | { | |
1ecc4d80 | 241 | if (!Ok()) return; |
8bbe427f | 242 | |
d13fd3e4 | 243 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 244 | } |
c801d85f | 245 | |
68dda785 | 246 | int wxColour::GetPixel() const |
c801d85f | 247 | { |
223d09f6 | 248 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 249 | |
1ecc4d80 | 250 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 251 | } |
c801d85f | 252 | |
68dda785 | 253 | GdkColor *wxColour::GetColor() const |
c801d85f | 254 | { |
223d09f6 | 255 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 256 | |
1ecc4d80 | 257 | return &M_COLDATA->m_color; |
ff7b1510 | 258 | } |
c801d85f KB |
259 | |
260 |