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