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