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