]> git.saurik.com Git - wxWidgets.git/blame - src/motif/palette.cpp
debug check for infinite loop in InitializeClasses() added
[wxWidgets.git] / src / motif / palette.cpp
CommitLineData
4bb6408c
JS
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
bf6c2b35 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
f97c9854
JS
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 */
bf6c2b35 20
f97c9854
JS
21/* Wolfram Gloger <u7y22ab@sunmail.lrz-muenchen.de>
22I have implemented basic colormap support for the X11 versions of
23wxWindows, notably wxPalette::Create(). The way I did it is to
24allocate additional read-only color cells in the default colormap. In
25general you will get arbitrary pixel values assigned to these new
26cells and therefore I added a method wxColourMap::TransferBitmap()
27which maps the pixel values 0..n to the real ones obtained with
28Create(). This is only implemented for the popular case of 8-bit
29depth.
30
31Allocating read-write color cells would involve installing a private
32X11 colormap for a particular window, and AFAIK this is not
33recommended; only the window manager should do this... Also, it is
34not the functionality that wxPalette::Create() aims to provide.
35 */
36
4bb6408c
JS
37#ifdef __GNUG__
38#pragma implementation "palette.h"
39#endif
40
41#include "wx/palette.h"
f97c9854
JS
42#include "wx/window.h"
43#include "wx/app.h"
44#include "wx/utils.h"
45
338dd992
JJ
46#ifdef __VMS__
47#pragma message disable nosimpint
48#endif
f97c9854 49#include <Xm/Xm.h>
338dd992
JJ
50#ifdef __VMS__
51#pragma message enable nosimpint
52#endif
f97c9854 53#include "wx/motif/private.h"
4bb6408c
JS
54
55#if !USE_SHARED_LIBRARIES
56IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
f97c9854 57IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
4bb6408c
JS
58#endif
59
60/*
2d120f83
JS
61* Palette
62*
63*/
4bb6408c 64
f97c9854
JS
65wxXPalette::wxXPalette()
66{
67 m_cmap = (WXColormap) 0;
68 m_pix_array_n = 0;
69 m_pix_array = (unsigned long*) 0;
70 m_display = (WXDisplay*) 0;
71 m_destroyable = FALSE;
72}
73
4bb6408c
JS
74wxPaletteRefData::wxPaletteRefData()
75{
4bb6408c
JS
76}
77
78wxPaletteRefData::~wxPaletteRefData()
79{
2d120f83 80 Display *display = (Display*) NULL;
bf6c2b35 81
2d120f83 82 wxNode *node, *next;
bf6c2b35 83
2d120f83
JS
84 for (node = m_palettes.First(); node; node = next) {
85 wxXPalette *c = (wxXPalette *)node->Data();
86 unsigned long *pix_array = c->m_pix_array;
87 Colormap cmap = (Colormap) c->m_cmap;
88 bool destroyable = c->m_destroyable;
89 int pix_array_n = c->m_pix_array_n;
90 display = (Display*) c->m_display;
bf6c2b35 91
2d120f83
JS
92 if (pix_array_n > 0)
93 {
94 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
95 // Be careful not to free '0' pixels...
96 int i, j;
97 for(i=j=0; i<pix_array_n; i=j) {
98 while(j<pix_array_n && pix_array[j]!=0) j++;
99 if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0);
100 while(j<pix_array_n && pix_array[j]==0) j++;
101 }
102 delete [] pix_array;
103 }
bf6c2b35 104
2d120f83
JS
105 if (destroyable)
106 XFreeColormap(display, cmap);
bf6c2b35 107
2d120f83
JS
108 next = node->Next();
109 m_palettes.DeleteNode(node);
110 delete c;
f97c9854 111 }
4bb6408c
JS
112}
113
114wxPalette::wxPalette()
115{
116}
117
118wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
119{
120 Create(n, red, green, blue);
121}
122
123wxPalette::~wxPalette()
124{
125}
126
127bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
128{
2d120f83 129 UnRef();
bf6c2b35 130
2d120f83
JS
131 if (!n) {
132 return FALSE;
133 }
bf6c2b35 134
2d120f83 135 m_refData = new wxPaletteRefData;
bf6c2b35 136
2d120f83
JS
137 XColor xcol;
138 Display* display = (Display*) wxGetDisplay();
bf6c2b35 139
2d120f83
JS
140 unsigned long *pix_array;
141 Colormap cmap;
142 int pix_array_n;
bf6c2b35 143
2d120f83 144 cmap = (Colormap) wxTheApp->GetMainColormap(display);
bf6c2b35 145
2d120f83
JS
146 pix_array = new unsigned long[n];
147 if (!pix_array)
148 return FALSE;
bf6c2b35 149
2d120f83
JS
150 pix_array_n = n;
151 xcol.flags = DoRed | DoGreen | DoBlue;
152 for(int i = 0; i < n; i++) {
153 xcol.red = (unsigned short)red[i] << 8;
154 xcol.green = (unsigned short)green[i] << 8;
155 xcol.blue = (unsigned short)blue[i] << 8;
156 pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel;
157 }
bf6c2b35 158
2d120f83 159 wxXPalette *c = new wxXPalette;
bf6c2b35 160
2d120f83
JS
161 c->m_pix_array_n = pix_array_n;
162 c->m_pix_array = pix_array;
163 c->m_cmap = (WXColormap) cmap;
164 c->m_display = (WXDisplay*) display;
165 c->m_destroyable = FALSE;
166 M_PALETTEDATA->m_palettes.Append(c);
bf6c2b35 167
2d120f83 168 return TRUE;
4bb6408c
JS
169}
170
171int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
172{
173 if ( !m_refData )
2d120f83 174 return FALSE;
bf6c2b35 175
4bb6408c
JS
176 // TODO
177 return FALSE;
178}
179
af111fc3 180bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
4bb6408c
JS
181{
182 if ( !m_refData )
2d120f83 183 return FALSE;
bf6c2b35 184
4bb6408c
JS
185 if (index < 0 || index > 255)
186 return FALSE;
bf6c2b35 187
4bb6408c
JS
188 // TODO
189 return FALSE;
190}
191
f97c9854
JS
192WXColormap wxPalette::GetXColormap(WXDisplay* display) const
193{
194 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.Number() == 0))
195 return wxTheApp->GetMainColormap(display);
bf6c2b35 196
f97c9854
JS
197 wxNode* node = M_PALETTEDATA->m_palettes.First();
198 if (!display && node)
199 {
200 wxXPalette* p = (wxXPalette*) node->Data();
201 return p->m_cmap;
202 }
203 while (node)
204 {
205 wxXPalette* p = (wxXPalette*) node->Data();
206 if (p->m_display == display)
207 return p->m_cmap;
bf6c2b35 208
f97c9854
JS
209 node = node->Next();
210 }
bf6c2b35 211
f97c9854
JS
212 /* Make a new one: */
213 wxXPalette *c = new wxXPalette;
214 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.First()->Data();
215 XColor xcol;
216 int pix_array_n = first->m_pix_array_n;
bf6c2b35 217
f97c9854
JS
218 c->m_pix_array_n = pix_array_n;
219 c->m_pix_array = new unsigned long[pix_array_n];
220 c->m_display = display;
221 c->m_cmap = wxTheApp->GetMainColormap(display);
222 c->m_destroyable = FALSE;
bf6c2b35 223
f97c9854
JS
224 xcol.flags = DoRed | DoGreen | DoBlue;
225 int i;
226 for (i = 0; i < pix_array_n; i++)
227 {
228 xcol.pixel = first->m_pix_array[i];
229 XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol);
230 c->m_pix_array[i] =
231 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
232 }
bf6c2b35 233
f97c9854 234 // wxPalette* nonConstThis = (wxPalette*) this;
bf6c2b35 235
f97c9854 236 M_PALETTEDATA->m_palettes.Append(c);
bf6c2b35 237
f97c9854
JS
238 return c->m_cmap;
239}
240
241bool wxPalette::TransferBitmap(void *data, int depth, int size)
242{
2d120f83
JS
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 }
bf6c2b35 255
2d120f83
JS
256 return TRUE;
257 }
258 default:
259 return FALSE;
f97c9854 260 }
f97c9854
JS
261}
262
263bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
2d120f83 264 void *dest, unsigned int bpp)
f97c9854 265{
2d120f83
JS
266 int pix_array_n;
267 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
f97c9854
JS
268 switch(bpp) {
269 case 8: {
2d120f83
JS
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 }
f97c9854 279 case 16: {
2d120f83
JS
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 }
f97c9854 289 case 24: {
2d120f83
JS
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++;
f97c9854 299 }
2d120f83
JS
300 break;
301 }
f97c9854 302 case 32: {
2d120f83
JS
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 }
f97c9854 312 default:
2d120f83 313 return FALSE;
f97c9854
JS
314 }
315 return TRUE;
316}
317
318unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
319{
320 if (!M_PALETTEDATA)
321 return (unsigned long*) 0;
322 wxNode *node;
bf6c2b35 323
f97c9854
JS
324 for (node = M_PALETTEDATA->m_palettes.First(); node; node = node->Next())
325 {
326 wxXPalette *c = (wxXPalette *)node->Data();
327 if (c->m_display == display)
328 {
329 if (n)
330 *n = c->m_pix_array_n;
331 return c->m_pix_array;
332 }
333 }
bf6c2b35 334
f97c9854
JS
335 /* Not found; call GetXColormap, which will create it, then this again */
336 if (GetXColormap(display))
337 return GetXPixArray(display, n);
338 else
339 return (unsigned long*) 0;
340}
341
342void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
343{
344 UnRef();
bf6c2b35 345
f97c9854 346 m_refData = new wxPaletteRefData;
bf6c2b35 347
f97c9854 348 wxXPalette *c = new wxXPalette;
bf6c2b35 349
f97c9854
JS
350 c->m_pix_array_n = 0;
351 c->m_pix_array = (unsigned long*) NULL;
352 c->m_display = display;
353 c->m_cmap = cm;
354 c->m_destroyable = dp;
bf6c2b35 355
f97c9854
JS
356 M_PALETTEDATA->m_palettes.Append(c);
357}
4bb6408c 358