]> git.saurik.com Git - wxWidgets.git/blame - src/motif/palette.cpp
removed empty file which wasn't present in the makefiles anyhow
[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"
670f9935
WS
41
42#ifndef WX_PRECOMP
43 #include "wx/app.h"
de6185e2 44 #include "wx/utils.h"
cdccdfab 45 #include "wx/window.h"
670f9935
WS
46#endif
47
338dd992
JJ
48#ifdef __VMS__
49#pragma message disable nosimpint
50#endif
f97c9854 51#include <Xm/Xm.h>
338dd992
JJ
52#ifdef __VMS__
53#pragma message enable nosimpint
54#endif
f97c9854 55#include "wx/motif/private.h"
4bb6408c 56
4bb6408c 57IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
f97c9854 58IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
4bb6408c
JS
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;
917be7ed 71 m_destroyable = false;
f97c9854
JS
72}
73
4bb6408c
JS
74wxPaletteRefData::wxPaletteRefData()
75{
4bb6408c
JS
76}
77
78wxPaletteRefData::~wxPaletteRefData()
79{
2d120f83 80 Display *display = (Display*) NULL;
bf6c2b35 81
ac32ba44 82 wxList::compatibility_iterator node, next;
bf6c2b35 83
1bc822df
MB
84 for (node = m_palettes.GetFirst(); node; node = next) {
85 wxXPalette *c = (wxXPalette *)node->GetData();
2d120f83
JS
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
1bc822df 108 next = node->GetNext();
ac32ba44 109 m_palettes.Erase(node);
2d120f83 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 131 if (!n) {
917be7ed 132 return false;
2d120f83 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)
917be7ed 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++) {
355b4d3d
WS
153 xcol.red = (unsigned short)(red[i] << 8);
154 xcol.green = (unsigned short)(green[i] << 8);
155 xcol.blue = (unsigned short)(blue[i] << 8);
2d120f83
JS
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;
917be7ed 165 c->m_destroyable = false;
2d120f83 166 M_PALETTEDATA->m_palettes.Append(c);
bf6c2b35 167
917be7ed 168 return true;
4bb6408c
JS
169}
170
88ef3a57
WS
171int wxPalette::GetPixel(unsigned char WXUNUSED(red),
172 unsigned char WXUNUSED(green),
173 unsigned char WXUNUSED(blue)) const
4bb6408c
JS
174{
175 if ( !m_refData )
88ef3a57 176 return wxNOT_FOUND;
bf6c2b35 177
4bb6408c 178 // TODO
88ef3a57 179 return wxNOT_FOUND;
4bb6408c
JS
180}
181
af111fc3 182bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
4bb6408c
JS
183{
184 if ( !m_refData )
917be7ed 185 return false;
bf6c2b35 186
4bb6408c 187 if (index < 0 || index > 255)
917be7ed 188 return false;
bf6c2b35 189
4bb6408c 190 // TODO
917be7ed 191 return false;
4bb6408c
JS
192}
193
f97c9854
JS
194WXColormap wxPalette::GetXColormap(WXDisplay* display) const
195{
1bc822df 196 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
f97c9854 197 return wxTheApp->GetMainColormap(display);
bf6c2b35 198
ac32ba44 199 wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst();
f97c9854
JS
200 if (!display && node)
201 {
1bc822df 202 wxXPalette* p = (wxXPalette*) node->GetData();
f97c9854
JS
203 return p->m_cmap;
204 }
205 while (node)
206 {
1bc822df 207 wxXPalette* p = (wxXPalette*) node->GetData();
f97c9854
JS
208 if (p->m_display == display)
209 return p->m_cmap;
bf6c2b35 210
1bc822df 211 node = node->GetNext();
f97c9854 212 }
bf6c2b35 213
f97c9854
JS
214 /* Make a new one: */
215 wxXPalette *c = new wxXPalette;
1bc822df
MB
216 wxXPalette *first =
217 (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
f97c9854
JS
218 XColor xcol;
219 int pix_array_n = first->m_pix_array_n;
bf6c2b35 220
f97c9854
JS
221 c->m_pix_array_n = pix_array_n;
222 c->m_pix_array = new unsigned long[pix_array_n];
223 c->m_display = display;
224 c->m_cmap = wxTheApp->GetMainColormap(display);
917be7ed 225 c->m_destroyable = false;
bf6c2b35 226
f97c9854
JS
227 xcol.flags = DoRed | DoGreen | DoBlue;
228 int i;
229 for (i = 0; i < pix_array_n; i++)
230 {
231 xcol.pixel = first->m_pix_array[i];
1bc822df
MB
232 XQueryColor((Display*) first->m_display,
233 (Colormap) first->m_cmap, &xcol);
f97c9854 234 c->m_pix_array[i] =
1bc822df
MB
235 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0)
236 ? 0 : xcol.pixel;
f97c9854 237 }
bf6c2b35 238
f97c9854 239 // wxPalette* nonConstThis = (wxPalette*) this;
bf6c2b35 240
f97c9854 241 M_PALETTEDATA->m_palettes.Append(c);
bf6c2b35 242
f97c9854
JS
243 return c->m_cmap;
244}
245
246bool wxPalette::TransferBitmap(void *data, int depth, int size)
247{
2d120f83
JS
248 switch(depth) {
249 case 8:
250 {
251 unsigned char *uptr = (unsigned char *)data;
252 int pix_array_n;
253 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
254 while(size-- > 0)
255 {
256 if((int)*uptr < pix_array_n)
257 *uptr = (unsigned char)pix_array[*uptr];
258 uptr++;
259 }
bf6c2b35 260
917be7ed 261 return true;
2d120f83
JS
262 }
263 default:
917be7ed 264 return false;
f97c9854 265 }
f97c9854
JS
266}
267
268bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
2d120f83 269 void *dest, unsigned int bpp)
f97c9854 270{
2d120f83
JS
271 int pix_array_n;
272 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
f97c9854
JS
273 switch(bpp) {
274 case 8: {
2d120f83
JS
275 unsigned char *dptr = (unsigned char *)dest;
276 while(sz-- > 0) {
277 if((int)*data < pix_array_n)
278 *dptr = (unsigned char)pix_array[*data];
279 data++;
280 dptr++;
281 }
282 break;
283 }
f97c9854 284 case 16: {
2d120f83
JS
285 unsigned short *dptr = (unsigned short *)dest;
286 while(sz-- > 0) {
287 if((int)*data < pix_array_n)
288 *dptr = (unsigned short)pix_array[*data];
289 data++;
290 dptr++;
291 }
292 break;
293 }
f97c9854 294 case 24: {
2d120f83
JS
295 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
296 while(sz-- > 0) {
297 if((int)*data < pix_array_n) {
355b4d3d
WS
298 dptr->r = (unsigned char)(pix_array[*data] & 0xFF);
299 dptr->g = (unsigned char)((pix_array[*data] >> 8) & 0xFF);
300 dptr->b = (unsigned char)((pix_array[*data] >> 16) & 0xFF);
2d120f83
JS
301 }
302 data++;
303 dptr++;
f97c9854 304 }
2d120f83
JS
305 break;
306 }
f97c9854 307 case 32: {
2d120f83
JS
308 unsigned long *dptr = (unsigned long *)dest;
309 while(sz-- > 0) {
310 if((int)*data < pix_array_n)
311 *dptr = pix_array[*data];
312 data++;
313 dptr++;
314 }
315 break;
316 }
f97c9854 317 default:
917be7ed 318 return false;
f97c9854 319 }
917be7ed 320 return true;
f97c9854
JS
321}
322
323unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
324{
325 if (!M_PALETTEDATA)
326 return (unsigned long*) 0;
ac32ba44 327 wxList::compatibility_iterator node;
bf6c2b35 328
1bc822df
MB
329 for (node = M_PALETTEDATA->m_palettes.GetFirst(); node;
330 node = node->GetNext())
f97c9854 331 {
1bc822df 332 wxXPalette *c = (wxXPalette *)node->GetData();
f97c9854
JS
333 if (c->m_display == display)
334 {
335 if (n)
336 *n = c->m_pix_array_n;
337 return c->m_pix_array;
338 }
339 }
bf6c2b35 340
f97c9854
JS
341 /* Not found; call GetXColormap, which will create it, then this again */
342 if (GetXColormap(display))
343 return GetXPixArray(display, n);
344 else
345 return (unsigned long*) 0;
346}
347
348void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
349{
350 UnRef();
bf6c2b35 351
f97c9854 352 m_refData = new wxPaletteRefData;
bf6c2b35 353
f97c9854 354 wxXPalette *c = new wxXPalette;
bf6c2b35 355
f97c9854
JS
356 c->m_pix_array_n = 0;
357 c->m_pix_array = (unsigned long*) NULL;
358 c->m_display = display;
359 c->m_cmap = cm;
360 c->m_destroyable = dp;
bf6c2b35 361
f97c9854
JS
362 M_PALETTEDATA->m_palettes.Append(c);
363}