]> git.saurik.com Git - wxWidgets.git/blame - src/motif/palette.cpp
Compilation fixes for wxUSE_STL=1 and for
[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 54
4bb6408c 55IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
f97c9854 56IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
4bb6408c
JS
57
58/*
2d120f83
JS
59* Palette
60*
61*/
4bb6408c 62
f97c9854
JS
63wxXPalette::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
4bb6408c
JS
72wxPaletteRefData::wxPaletteRefData()
73{
4bb6408c
JS
74}
75
76wxPaletteRefData::~wxPaletteRefData()
77{
2d120f83 78 Display *display = (Display*) NULL;
bf6c2b35 79
1bc822df 80 wxList::Node *node, *next;
bf6c2b35 81
1bc822df
MB
82 for (node = m_palettes.GetFirst(); node; node = next) {
83 wxXPalette *c = (wxXPalette *)node->GetData();
2d120f83
JS
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;
bf6c2b35 89
2d120f83
JS
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 }
bf6c2b35 102
2d120f83
JS
103 if (destroyable)
104 XFreeColormap(display, cmap);
bf6c2b35 105
1bc822df 106 next = node->GetNext();
2d120f83
JS
107 m_palettes.DeleteNode(node);
108 delete c;
f97c9854 109 }
4bb6408c
JS
110}
111
112wxPalette::wxPalette()
113{
114}
115
116wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
117{
118 Create(n, red, green, blue);
119}
120
121wxPalette::~wxPalette()
122{
123}
124
125bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
126{
2d120f83 127 UnRef();
bf6c2b35 128
2d120f83
JS
129 if (!n) {
130 return FALSE;
131 }
bf6c2b35 132
2d120f83 133 m_refData = new wxPaletteRefData;
bf6c2b35 134
2d120f83
JS
135 XColor xcol;
136 Display* display = (Display*) wxGetDisplay();
bf6c2b35 137
2d120f83
JS
138 unsigned long *pix_array;
139 Colormap cmap;
140 int pix_array_n;
bf6c2b35 141
2d120f83 142 cmap = (Colormap) wxTheApp->GetMainColormap(display);
bf6c2b35 143
2d120f83
JS
144 pix_array = new unsigned long[n];
145 if (!pix_array)
146 return FALSE;
bf6c2b35 147
2d120f83
JS
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 }
bf6c2b35 156
2d120f83 157 wxXPalette *c = new wxXPalette;
bf6c2b35 158
2d120f83
JS
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);
bf6c2b35 165
2d120f83 166 return TRUE;
4bb6408c
JS
167}
168
169int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
170{
171 if ( !m_refData )
2d120f83 172 return FALSE;
bf6c2b35 173
4bb6408c
JS
174 // TODO
175 return FALSE;
176}
177
af111fc3 178bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
4bb6408c
JS
179{
180 if ( !m_refData )
2d120f83 181 return FALSE;
bf6c2b35 182
4bb6408c
JS
183 if (index < 0 || index > 255)
184 return FALSE;
bf6c2b35 185
4bb6408c
JS
186 // TODO
187 return FALSE;
188}
189
f97c9854
JS
190WXColormap wxPalette::GetXColormap(WXDisplay* display) const
191{
1bc822df 192 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
f97c9854 193 return wxTheApp->GetMainColormap(display);
bf6c2b35 194
1bc822df 195 wxList::Node* node = M_PALETTEDATA->m_palettes.GetFirst();
f97c9854
JS
196 if (!display && node)
197 {
1bc822df 198 wxXPalette* p = (wxXPalette*) node->GetData();
f97c9854
JS
199 return p->m_cmap;
200 }
201 while (node)
202 {
1bc822df 203 wxXPalette* p = (wxXPalette*) node->GetData();
f97c9854
JS
204 if (p->m_display == display)
205 return p->m_cmap;
bf6c2b35 206
1bc822df 207 node = node->GetNext();
f97c9854 208 }
bf6c2b35 209
f97c9854
JS
210 /* Make a new one: */
211 wxXPalette *c = new wxXPalette;
1bc822df
MB
212 wxXPalette *first =
213 (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
f97c9854
JS
214 XColor xcol;
215 int pix_array_n = first->m_pix_array_n;
bf6c2b35 216
f97c9854
JS
217 c->m_pix_array_n = pix_array_n;
218 c->m_pix_array = new unsigned long[pix_array_n];
219 c->m_display = display;
220 c->m_cmap = wxTheApp->GetMainColormap(display);
221 c->m_destroyable = FALSE;
bf6c2b35 222
f97c9854
JS
223 xcol.flags = DoRed | DoGreen | DoBlue;
224 int i;
225 for (i = 0; i < pix_array_n; i++)
226 {
227 xcol.pixel = first->m_pix_array[i];
1bc822df
MB
228 XQueryColor((Display*) first->m_display,
229 (Colormap) first->m_cmap, &xcol);
f97c9854 230 c->m_pix_array[i] =
1bc822df
MB
231 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0)
232 ? 0 : xcol.pixel;
f97c9854 233 }
bf6c2b35 234
f97c9854 235 // wxPalette* nonConstThis = (wxPalette*) this;
bf6c2b35 236
f97c9854 237 M_PALETTEDATA->m_palettes.Append(c);
bf6c2b35 238
f97c9854
JS
239 return c->m_cmap;
240}
241
242bool wxPalette::TransferBitmap(void *data, int depth, int size)
243{
2d120f83
JS
244 switch(depth) {
245 case 8:
246 {
247 unsigned char *uptr = (unsigned char *)data;
248 int pix_array_n;
249 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
250 while(size-- > 0)
251 {
252 if((int)*uptr < pix_array_n)
253 *uptr = (unsigned char)pix_array[*uptr];
254 uptr++;
255 }
bf6c2b35 256
2d120f83
JS
257 return TRUE;
258 }
259 default:
260 return FALSE;
f97c9854 261 }
f97c9854
JS
262}
263
264bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
2d120f83 265 void *dest, unsigned int bpp)
f97c9854 266{
2d120f83
JS
267 int pix_array_n;
268 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
f97c9854
JS
269 switch(bpp) {
270 case 8: {
2d120f83
JS
271 unsigned char *dptr = (unsigned char *)dest;
272 while(sz-- > 0) {
273 if((int)*data < pix_array_n)
274 *dptr = (unsigned char)pix_array[*data];
275 data++;
276 dptr++;
277 }
278 break;
279 }
f97c9854 280 case 16: {
2d120f83
JS
281 unsigned short *dptr = (unsigned short *)dest;
282 while(sz-- > 0) {
283 if((int)*data < pix_array_n)
284 *dptr = (unsigned short)pix_array[*data];
285 data++;
286 dptr++;
287 }
288 break;
289 }
f97c9854 290 case 24: {
2d120f83
JS
291 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
292 while(sz-- > 0) {
293 if((int)*data < pix_array_n) {
294 dptr->r = pix_array[*data] & 0xFF;
295 dptr->g = (pix_array[*data] >> 8) & 0xFF;
296 dptr->b = (pix_array[*data] >> 16) & 0xFF;
297 }
298 data++;
299 dptr++;
f97c9854 300 }
2d120f83
JS
301 break;
302 }
f97c9854 303 case 32: {
2d120f83
JS
304 unsigned long *dptr = (unsigned long *)dest;
305 while(sz-- > 0) {
306 if((int)*data < pix_array_n)
307 *dptr = pix_array[*data];
308 data++;
309 dptr++;
310 }
311 break;
312 }
f97c9854 313 default:
2d120f83 314 return FALSE;
f97c9854
JS
315 }
316 return TRUE;
317}
318
319unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
320{
321 if (!M_PALETTEDATA)
322 return (unsigned long*) 0;
1bc822df 323 wxList::Node *node;
bf6c2b35 324
1bc822df
MB
325 for (node = M_PALETTEDATA->m_palettes.GetFirst(); node;
326 node = node->GetNext())
f97c9854 327 {
1bc822df 328 wxXPalette *c = (wxXPalette *)node->GetData();
f97c9854
JS
329 if (c->m_display == display)
330 {
331 if (n)
332 *n = c->m_pix_array_n;
333 return c->m_pix_array;
334 }
335 }
bf6c2b35 336
f97c9854
JS
337 /* Not found; call GetXColormap, which will create it, then this again */
338 if (GetXColormap(display))
339 return GetXPixArray(display, n);
340 else
341 return (unsigned long*) 0;
342}
343
344void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
345{
346 UnRef();
bf6c2b35 347
f97c9854 348 m_refData = new wxPaletteRefData;
bf6c2b35 349
f97c9854 350 wxXPalette *c = new wxXPalette;
bf6c2b35 351
f97c9854
JS
352 c->m_pix_array_n = 0;
353 c->m_pix_array = (unsigned long*) NULL;
354 c->m_display = display;
355 c->m_cmap = cm;
356 c->m_destroyable = dp;
bf6c2b35 357
f97c9854
JS
358 M_PALETTEDATA->m_palettes.Append(c);
359}
4bb6408c 360