]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk1/colour.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/colour.h" | |
14 | ||
15 | #include "wx/gdicmn.h" | |
16 | #include "wx/gtk1/private.h" | |
17 | ||
18 | #include <gdk/gdk.h> | |
19 | #include <gdk/gdkx.h> | |
20 | #include <gdk/gdkprivate.h> | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // wxColour | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | class wxColourRefData: public wxObjectRefData | |
27 | { | |
28 | public: | |
29 | wxColourRefData() | |
30 | { | |
31 | m_color.red = 0; | |
32 | m_color.green = 0; | |
33 | m_color.blue = 0; | |
34 | m_color.pixel = 0; | |
35 | m_colormap = (GdkColormap *) NULL; | |
36 | m_hasPixel = false; | |
37 | } | |
38 | ||
39 | wxColourRefData(const wxColourRefData& data) | |
40 | : wxObjectRefData() | |
41 | { | |
42 | m_color = data.m_color; | |
43 | m_colormap = data.m_colormap; | |
44 | m_hasPixel = data.m_hasPixel; | |
45 | } | |
46 | ||
47 | ~wxColourRefData() | |
48 | { | |
49 | FreeColour(); | |
50 | } | |
51 | ||
52 | bool operator == (const wxColourRefData& data) const | |
53 | { | |
54 | return (m_colormap == data.m_colormap && | |
55 | m_hasPixel == data.m_hasPixel && | |
56 | m_color.red == data.m_color.red && | |
57 | m_color.green == data.m_color.green && | |
58 | m_color.blue == data.m_color.blue && | |
59 | m_color.pixel == data.m_color.pixel); | |
60 | } | |
61 | ||
62 | void FreeColour(); | |
63 | void AllocColour( GdkColormap* cmap ); | |
64 | ||
65 | GdkColor m_color; | |
66 | GdkColormap *m_colormap; | |
67 | bool m_hasPixel; | |
68 | ||
69 | friend class wxColour; | |
70 | ||
71 | // reference counter for systems with <= 8-Bit display | |
72 | static gushort colMapAllocCounter[ 256 ]; | |
73 | }; | |
74 | ||
75 | gushort wxColourRefData::colMapAllocCounter[ 256 ] = | |
76 | { | |
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, 0, 0, 0, 0, | |
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 | |
90 | }; | |
91 | ||
92 | void wxColourRefData::FreeColour() | |
93 | { | |
94 | if (m_colormap) | |
95 | { | |
96 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap; | |
97 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
98 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
99 | { | |
100 | int idx = m_color.pixel; | |
101 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
102 | ||
103 | if (colMapAllocCounter[ idx ] == 0) | |
104 | gdk_colormap_free_colors( m_colormap, &m_color, 1 ); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | void wxColourRefData::AllocColour( GdkColormap *cmap ) | |
110 | { | |
111 | if (m_hasPixel && (m_colormap == cmap)) | |
112 | return; | |
113 | ||
114 | FreeColour(); | |
115 | ||
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 | { | |
120 | m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE ); | |
121 | int idx = m_color.pixel; | |
122 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1; | |
123 | } | |
124 | else | |
125 | { | |
126 | m_hasPixel = gdk_color_alloc( cmap, &m_color ); | |
127 | } | |
128 | m_colormap = cmap; | |
129 | } | |
130 | ||
131 | //----------------------------------------------------------------------------- | |
132 | ||
133 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
134 | ||
135 | // GDK's values are in 0..65535 range, our are in 0..255 | |
136 | #define SHIFT 8 | |
137 | ||
138 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
139 | ||
140 | wxColour::~wxColour() | |
141 | { | |
142 | } | |
143 | ||
144 | bool wxColour::operator == ( const wxColour& col ) const | |
145 | { | |
146 | if (m_refData == col.m_refData) | |
147 | return true; | |
148 | ||
149 | if (!m_refData || !col.m_refData) | |
150 | return false; | |
151 | ||
152 | GdkColor *own = &(((wxColourRefData*)m_refData)->m_color); | |
153 | GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
154 | return own->red == other->red && | |
155 | own->blue == other->blue && | |
156 | own->green == other->green; | |
157 | } | |
158 | ||
159 | wxObjectRefData *wxColour::CreateRefData() const | |
160 | { | |
161 | return new wxColourRefData; | |
162 | } | |
163 | ||
164 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const | |
165 | { | |
166 | return new wxColourRefData(*(wxColourRefData *)data); | |
167 | } | |
168 | ||
169 | void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) | |
170 | { | |
171 | AllocExclusive(); | |
172 | ||
173 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
174 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
175 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
176 | M_COLDATA->m_color.pixel = 0; | |
177 | ||
178 | M_COLDATA->m_colormap = (GdkColormap*) NULL; | |
179 | M_COLDATA->m_hasPixel = false; | |
180 | } | |
181 | ||
182 | unsigned char wxColour::Red() const | |
183 | { | |
184 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
185 | ||
186 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); | |
187 | } | |
188 | ||
189 | unsigned char wxColour::Green() const | |
190 | { | |
191 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
192 | ||
193 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); | |
194 | } | |
195 | ||
196 | unsigned char wxColour::Blue() const | |
197 | { | |
198 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
199 | ||
200 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); | |
201 | } | |
202 | ||
203 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
204 | { | |
205 | if (!Ok()) return; | |
206 | ||
207 | M_COLDATA->AllocColour( cmap ); | |
208 | } | |
209 | ||
210 | int wxColour::GetPixel() const | |
211 | { | |
212 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
213 | ||
214 | return M_COLDATA->m_color.pixel; | |
215 | } | |
216 | ||
217 | GdkColor *wxColour::GetColor() const | |
218 | { | |
219 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); | |
220 | ||
221 | return &M_COLDATA->m_color; | |
222 | } | |
223 | ||
224 | bool wxColour::FromString(const wxChar *str) | |
225 | { | |
226 | GdkColor colGDK; | |
227 | if ( gdk_color_parse( str, &colGDK ) ) | |
228 | { | |
229 | UnRef(); | |
230 | ||
231 | m_refData = new wxColourRefData; | |
232 | M_COLDATA->m_color = colGDK; | |
233 | return true; | |
234 | } | |
235 | ||
236 | return wxColourBase::FromString(str); | |
237 | } |