]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: palette.cpp | |
3 | // Purpose: wxPalette | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /* | |
13 | * Colour map | |
14 | * | |
15 | * When constructed with the default constructor, we start from | |
16 | * the wxApp::GetMainColormap, allocating additional read-only cells | |
17 | * in Create(). The cells are freed on the next call to Create() | |
18 | * or when the destructor is called. | |
19 | */ | |
20 | ||
21 | /* Wolfram Gloger <u7y22ab@sunmail.lrz-muenchen.de> | |
22 | I have implemented basic colormap support for the X11 versions of | |
23 | wxWidgets, notably wxPalette::Create(). The way I did it is to | |
24 | allocate additional read-only color cells in the default colormap. In | |
25 | general you will get arbitrary pixel values assigned to these new | |
26 | cells and therefore I added a method wxPalette::TransferBitmap() | |
27 | which maps the pixel values 0..n to the real ones obtained with | |
28 | Create(). This is only implemented for the popular case of 8-bit | |
29 | depth. | |
30 | ||
31 | Allocating read-write color cells would involve installing a private | |
32 | X11 colormap for a particular window, and AFAIK this is not | |
33 | recommended; only the window manager should do this... Also, it is | |
34 | not the functionality that wxPalette::Create() aims to provide. | |
35 | */ | |
36 | ||
37 | // For compilers that support precompilation, includes "wx.h". | |
38 | #include "wx/wxprec.h" | |
39 | ||
40 | #include "wx/palette.h" | |
41 | #include "wx/window.h" | |
42 | #include "wx/app.h" | |
43 | #include "wx/utils.h" | |
44 | ||
45 | #ifdef __VMS__ | |
46 | #pragma message disable nosimpint | |
47 | #endif | |
48 | #include <Xm/Xm.h> | |
49 | #ifdef __VMS__ | |
50 | #pragma message enable nosimpint | |
51 | #endif | |
52 | #include "wx/motif/private.h" | |
53 | ||
54 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) | |
55 | IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject) | |
56 | ||
57 | /* | |
58 | * Palette | |
59 | * | |
60 | */ | |
61 | ||
62 | wxXPalette::wxXPalette() | |
63 | { | |
64 | m_cmap = (WXColormap) 0; | |
65 | m_pix_array_n = 0; | |
66 | m_pix_array = (unsigned long*) 0; | |
67 | m_display = (WXDisplay*) 0; | |
68 | m_destroyable = false; | |
69 | } | |
70 | ||
71 | wxPaletteRefData::wxPaletteRefData() | |
72 | { | |
73 | } | |
74 | ||
75 | wxPaletteRefData::~wxPaletteRefData() | |
76 | { | |
77 | Display *display = (Display*) NULL; | |
78 | ||
79 | wxList::compatibility_iterator node, next; | |
80 | ||
81 | for (node = m_palettes.GetFirst(); node; node = next) { | |
82 | wxXPalette *c = (wxXPalette *)node->GetData(); | |
83 | unsigned long *pix_array = c->m_pix_array; | |
84 | Colormap cmap = (Colormap) c->m_cmap; | |
85 | bool destroyable = c->m_destroyable; | |
86 | int pix_array_n = c->m_pix_array_n; | |
87 | display = (Display*) c->m_display; | |
88 | ||
89 | if (pix_array_n > 0) | |
90 | { | |
91 | // XFreeColors(display, cmap, pix_array, pix_array_n, 0); | |
92 | // Be careful not to free '0' pixels... | |
93 | int i, j; | |
94 | for(i=j=0; i<pix_array_n; i=j) { | |
95 | while(j<pix_array_n && pix_array[j]!=0) j++; | |
96 | if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0); | |
97 | while(j<pix_array_n && pix_array[j]==0) j++; | |
98 | } | |
99 | delete [] pix_array; | |
100 | } | |
101 | ||
102 | if (destroyable) | |
103 | XFreeColormap(display, cmap); | |
104 | ||
105 | next = node->GetNext(); | |
106 | m_palettes.Erase(node); | |
107 | delete c; | |
108 | } | |
109 | } | |
110 | ||
111 | wxPalette::wxPalette() | |
112 | { | |
113 | } | |
114 | ||
115 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) | |
116 | { | |
117 | Create(n, red, green, blue); | |
118 | } | |
119 | ||
120 | wxPalette::~wxPalette() | |
121 | { | |
122 | } | |
123 | ||
124 | bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) | |
125 | { | |
126 | UnRef(); | |
127 | ||
128 | if (!n) { | |
129 | return false; | |
130 | } | |
131 | ||
132 | m_refData = new wxPaletteRefData; | |
133 | ||
134 | XColor xcol; | |
135 | Display* display = (Display*) wxGetDisplay(); | |
136 | ||
137 | unsigned long *pix_array; | |
138 | Colormap cmap; | |
139 | int pix_array_n; | |
140 | ||
141 | cmap = (Colormap) wxTheApp->GetMainColormap(display); | |
142 | ||
143 | pix_array = new unsigned long[n]; | |
144 | if (!pix_array) | |
145 | return false; | |
146 | ||
147 | pix_array_n = n; | |
148 | xcol.flags = DoRed | DoGreen | DoBlue; | |
149 | for(int i = 0; i < n; i++) { | |
150 | xcol.red = (unsigned short)red[i] << 8; | |
151 | xcol.green = (unsigned short)green[i] << 8; | |
152 | xcol.blue = (unsigned short)blue[i] << 8; | |
153 | pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel; | |
154 | } | |
155 | ||
156 | wxXPalette *c = new wxXPalette; | |
157 | ||
158 | c->m_pix_array_n = pix_array_n; | |
159 | c->m_pix_array = pix_array; | |
160 | c->m_cmap = (WXColormap) cmap; | |
161 | c->m_display = (WXDisplay*) display; | |
162 | c->m_destroyable = false; | |
163 | M_PALETTEDATA->m_palettes.Append(c); | |
164 | ||
165 | return true; | |
166 | } | |
167 | ||
168 | int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const | |
169 | { | |
170 | if ( !m_refData ) | |
171 | return false; | |
172 | ||
173 | // TODO | |
174 | return false; | |
175 | } | |
176 | ||
177 | bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const | |
178 | { | |
179 | if ( !m_refData ) | |
180 | return false; | |
181 | ||
182 | if (index < 0 || index > 255) | |
183 | return false; | |
184 | ||
185 | // TODO | |
186 | return false; | |
187 | } | |
188 | ||
189 | WXColormap wxPalette::GetXColormap(WXDisplay* display) const | |
190 | { | |
191 | if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0)) | |
192 | return wxTheApp->GetMainColormap(display); | |
193 | ||
194 | wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst(); | |
195 | if (!display && node) | |
196 | { | |
197 | wxXPalette* p = (wxXPalette*) node->GetData(); | |
198 | return p->m_cmap; | |
199 | } | |
200 | while (node) | |
201 | { | |
202 | wxXPalette* p = (wxXPalette*) node->GetData(); | |
203 | if (p->m_display == display) | |
204 | return p->m_cmap; | |
205 | ||
206 | node = node->GetNext(); | |
207 | } | |
208 | ||
209 | /* Make a new one: */ | |
210 | wxXPalette *c = new wxXPalette; | |
211 | wxXPalette *first = | |
212 | (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData(); | |
213 | XColor xcol; | |
214 | int pix_array_n = first->m_pix_array_n; | |
215 | ||
216 | c->m_pix_array_n = pix_array_n; | |
217 | c->m_pix_array = new unsigned long[pix_array_n]; | |
218 | c->m_display = display; | |
219 | c->m_cmap = wxTheApp->GetMainColormap(display); | |
220 | c->m_destroyable = false; | |
221 | ||
222 | xcol.flags = DoRed | DoGreen | DoBlue; | |
223 | int i; | |
224 | for (i = 0; i < pix_array_n; i++) | |
225 | { | |
226 | xcol.pixel = first->m_pix_array[i]; | |
227 | XQueryColor((Display*) first->m_display, | |
228 | (Colormap) first->m_cmap, &xcol); | |
229 | c->m_pix_array[i] = | |
230 | (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) | |
231 | ? 0 : xcol.pixel; | |
232 | } | |
233 | ||
234 | // wxPalette* nonConstThis = (wxPalette*) this; | |
235 | ||
236 | M_PALETTEDATA->m_palettes.Append(c); | |
237 | ||
238 | return c->m_cmap; | |
239 | } | |
240 | ||
241 | bool wxPalette::TransferBitmap(void *data, int depth, int size) | |
242 | { | |
243 | switch(depth) { | |
244 | case 8: | |
245 | { | |
246 | unsigned char *uptr = (unsigned char *)data; | |
247 | int pix_array_n; | |
248 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
249 | while(size-- > 0) | |
250 | { | |
251 | if((int)*uptr < pix_array_n) | |
252 | *uptr = (unsigned char)pix_array[*uptr]; | |
253 | uptr++; | |
254 | } | |
255 | ||
256 | return true; | |
257 | } | |
258 | default: | |
259 | return false; | |
260 | } | |
261 | } | |
262 | ||
263 | bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz, | |
264 | void *dest, unsigned int bpp) | |
265 | { | |
266 | int pix_array_n; | |
267 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
268 | switch(bpp) { | |
269 | case 8: { | |
270 | unsigned char *dptr = (unsigned char *)dest; | |
271 | while(sz-- > 0) { | |
272 | if((int)*data < pix_array_n) | |
273 | *dptr = (unsigned char)pix_array[*data]; | |
274 | data++; | |
275 | dptr++; | |
276 | } | |
277 | break; | |
278 | } | |
279 | case 16: { | |
280 | unsigned short *dptr = (unsigned short *)dest; | |
281 | while(sz-- > 0) { | |
282 | if((int)*data < pix_array_n) | |
283 | *dptr = (unsigned short)pix_array[*data]; | |
284 | data++; | |
285 | dptr++; | |
286 | } | |
287 | break; | |
288 | } | |
289 | case 24: { | |
290 | struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest; | |
291 | while(sz-- > 0) { | |
292 | if((int)*data < pix_array_n) { | |
293 | dptr->r = pix_array[*data] & 0xFF; | |
294 | dptr->g = (pix_array[*data] >> 8) & 0xFF; | |
295 | dptr->b = (pix_array[*data] >> 16) & 0xFF; | |
296 | } | |
297 | data++; | |
298 | dptr++; | |
299 | } | |
300 | break; | |
301 | } | |
302 | case 32: { | |
303 | unsigned long *dptr = (unsigned long *)dest; | |
304 | while(sz-- > 0) { | |
305 | if((int)*data < pix_array_n) | |
306 | *dptr = pix_array[*data]; | |
307 | data++; | |
308 | dptr++; | |
309 | } | |
310 | break; | |
311 | } | |
312 | default: | |
313 | return false; | |
314 | } | |
315 | return true; | |
316 | } | |
317 | ||
318 | unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n) | |
319 | { | |
320 | if (!M_PALETTEDATA) | |
321 | return (unsigned long*) 0; | |
322 | wxList::compatibility_iterator node; | |
323 | ||
324 | for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; | |
325 | node = node->GetNext()) | |
326 | { | |
327 | wxXPalette *c = (wxXPalette *)node->GetData(); | |
328 | if (c->m_display == display) | |
329 | { | |
330 | if (n) | |
331 | *n = c->m_pix_array_n; | |
332 | return c->m_pix_array; | |
333 | } | |
334 | } | |
335 | ||
336 | /* Not found; call GetXColormap, which will create it, then this again */ | |
337 | if (GetXColormap(display)) | |
338 | return GetXPixArray(display, n); | |
339 | else | |
340 | return (unsigned long*) 0; | |
341 | } | |
342 | ||
343 | void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp) | |
344 | { | |
345 | UnRef(); | |
346 | ||
347 | m_refData = new wxPaletteRefData; | |
348 | ||
349 | wxXPalette *c = new wxXPalette; | |
350 | ||
351 | c->m_pix_array_n = 0; | |
352 | c->m_pix_array = (unsigned long*) NULL; | |
353 | c->m_display = display; | |
354 | c->m_cmap = cm; | |
355 | c->m_destroyable = dp; | |
356 | ||
357 | M_PALETTEDATA->m_palettes.Append(c); | |
358 | } | |
359 |