]> git.saurik.com Git - wxWidgets.git/blob - src/motif/palette.cpp
Reverted a change which breaks compilation for win32 - not sure on how to fix this :-?
[wxWidgets.git] / src / motif / palette.cpp
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 wxWindows, 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 wxColourMap::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 #ifdef __GNUG__
38 #pragma implementation "palette.h"
39 #endif
40
41 #include "wx/palette.h"
42 #include "wx/window.h"
43 #include "wx/app.h"
44 #include "wx/utils.h"
45
46 #ifdef __VMS__
47 #pragma message disable nosimpint
48 #endif
49 #include <Xm/Xm.h>
50 #ifdef __VMS__
51 #pragma message enable nosimpint
52 #endif
53 #include "wx/motif/private.h"
54
55 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
56 IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
57
58 /*
59 * Palette
60 *
61 */
62
63 wxXPalette::wxXPalette()
64 {
65 m_cmap = (WXColormap) 0;
66 m_pix_array_n = 0;
67 m_pix_array = (unsigned long*) 0;
68 m_display = (WXDisplay*) 0;
69 m_destroyable = FALSE;
70 }
71
72 wxPaletteRefData::wxPaletteRefData()
73 {
74 }
75
76 wxPaletteRefData::~wxPaletteRefData()
77 {
78 Display *display = (Display*) NULL;
79
80 wxNode *node, *next;
81
82 for (node = m_palettes.First(); node; node = next) {
83 wxXPalette *c = (wxXPalette *)node->Data();
84 unsigned long *pix_array = c->m_pix_array;
85 Colormap cmap = (Colormap) c->m_cmap;
86 bool destroyable = c->m_destroyable;
87 int pix_array_n = c->m_pix_array_n;
88 display = (Display*) c->m_display;
89
90 if (pix_array_n > 0)
91 {
92 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
93 // Be careful not to free '0' pixels...
94 int i, j;
95 for(i=j=0; i<pix_array_n; i=j) {
96 while(j<pix_array_n && pix_array[j]!=0) j++;
97 if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0);
98 while(j<pix_array_n && pix_array[j]==0) j++;
99 }
100 delete [] pix_array;
101 }
102
103 if (destroyable)
104 XFreeColormap(display, cmap);
105
106 next = node->Next();
107 m_palettes.DeleteNode(node);
108 delete c;
109 }
110 }
111
112 wxPalette::wxPalette()
113 {
114 }
115
116 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
117 {
118 Create(n, red, green, blue);
119 }
120
121 wxPalette::~wxPalette()
122 {
123 }
124
125 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
126 {
127 UnRef();
128
129 if (!n) {
130 return FALSE;
131 }
132
133 m_refData = new wxPaletteRefData;
134
135 XColor xcol;
136 Display* display = (Display*) wxGetDisplay();
137
138 unsigned long *pix_array;
139 Colormap cmap;
140 int pix_array_n;
141
142 cmap = (Colormap) wxTheApp->GetMainColormap(display);
143
144 pix_array = new unsigned long[n];
145 if (!pix_array)
146 return FALSE;
147
148 pix_array_n = n;
149 xcol.flags = DoRed | DoGreen | DoBlue;
150 for(int i = 0; i < n; i++) {
151 xcol.red = (unsigned short)red[i] << 8;
152 xcol.green = (unsigned short)green[i] << 8;
153 xcol.blue = (unsigned short)blue[i] << 8;
154 pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel;
155 }
156
157 wxXPalette *c = new wxXPalette;
158
159 c->m_pix_array_n = pix_array_n;
160 c->m_pix_array = pix_array;
161 c->m_cmap = (WXColormap) cmap;
162 c->m_display = (WXDisplay*) display;
163 c->m_destroyable = FALSE;
164 M_PALETTEDATA->m_palettes.Append(c);
165
166 return TRUE;
167 }
168
169 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
170 {
171 if ( !m_refData )
172 return FALSE;
173
174 // TODO
175 return FALSE;
176 }
177
178 bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
179 {
180 if ( !m_refData )
181 return FALSE;
182
183 if (index < 0 || index > 255)
184 return FALSE;
185
186 // TODO
187 return FALSE;
188 }
189
190 WXColormap wxPalette::GetXColormap(WXDisplay* display) const
191 {
192 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.Number() == 0))
193 return wxTheApp->GetMainColormap(display);
194
195 wxNode* node = M_PALETTEDATA->m_palettes.First();
196 if (!display && node)
197 {
198 wxXPalette* p = (wxXPalette*) node->Data();
199 return p->m_cmap;
200 }
201 while (node)
202 {
203 wxXPalette* p = (wxXPalette*) node->Data();
204 if (p->m_display == display)
205 return p->m_cmap;
206
207 node = node->Next();
208 }
209
210 /* Make a new one: */
211 wxXPalette *c = new wxXPalette;
212 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.First()->Data();
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, (Colormap) first->m_cmap, &xcol);
228 c->m_pix_array[i] =
229 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
230 }
231
232 // wxPalette* nonConstThis = (wxPalette*) this;
233
234 M_PALETTEDATA->m_palettes.Append(c);
235
236 return c->m_cmap;
237 }
238
239 bool wxPalette::TransferBitmap(void *data, int depth, int size)
240 {
241 switch(depth) {
242 case 8:
243 {
244 unsigned char *uptr = (unsigned char *)data;
245 int pix_array_n;
246 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
247 while(size-- > 0)
248 {
249 if((int)*uptr < pix_array_n)
250 *uptr = (unsigned char)pix_array[*uptr];
251 uptr++;
252 }
253
254 return TRUE;
255 }
256 default:
257 return FALSE;
258 }
259 }
260
261 bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
262 void *dest, unsigned int bpp)
263 {
264 int pix_array_n;
265 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
266 switch(bpp) {
267 case 8: {
268 unsigned char *dptr = (unsigned char *)dest;
269 while(sz-- > 0) {
270 if((int)*data < pix_array_n)
271 *dptr = (unsigned char)pix_array[*data];
272 data++;
273 dptr++;
274 }
275 break;
276 }
277 case 16: {
278 unsigned short *dptr = (unsigned short *)dest;
279 while(sz-- > 0) {
280 if((int)*data < pix_array_n)
281 *dptr = (unsigned short)pix_array[*data];
282 data++;
283 dptr++;
284 }
285 break;
286 }
287 case 24: {
288 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
289 while(sz-- > 0) {
290 if((int)*data < pix_array_n) {
291 dptr->r = pix_array[*data] & 0xFF;
292 dptr->g = (pix_array[*data] >> 8) & 0xFF;
293 dptr->b = (pix_array[*data] >> 16) & 0xFF;
294 }
295 data++;
296 dptr++;
297 }
298 break;
299 }
300 case 32: {
301 unsigned long *dptr = (unsigned long *)dest;
302 while(sz-- > 0) {
303 if((int)*data < pix_array_n)
304 *dptr = pix_array[*data];
305 data++;
306 dptr++;
307 }
308 break;
309 }
310 default:
311 return FALSE;
312 }
313 return TRUE;
314 }
315
316 unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
317 {
318 if (!M_PALETTEDATA)
319 return (unsigned long*) 0;
320 wxNode *node;
321
322 for (node = M_PALETTEDATA->m_palettes.First(); node; node = node->Next())
323 {
324 wxXPalette *c = (wxXPalette *)node->Data();
325 if (c->m_display == display)
326 {
327 if (n)
328 *n = c->m_pix_array_n;
329 return c->m_pix_array;
330 }
331 }
332
333 /* Not found; call GetXColormap, which will create it, then this again */
334 if (GetXColormap(display))
335 return GetXPixArray(display, n);
336 else
337 return (unsigned long*) 0;
338 }
339
340 void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
341 {
342 UnRef();
343
344 m_refData = new wxPaletteRefData;
345
346 wxXPalette *c = new wxXPalette;
347
348 c->m_pix_array_n = 0;
349 c->m_pix_array = (unsigned long*) NULL;
350 c->m_display = display;
351 c->m_cmap = cm;
352 c->m_destroyable = dp;
353
354 M_PALETTEDATA->m_palettes.Append(c);
355 }
356