]> git.saurik.com Git - wxWidgets.git/blame - src/x11/palette.cpp
docopydocs is not needed for this script.
[wxWidgets.git] / src / x11 / palette.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/x11/palette.cpp
83df96d6
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
83df96d6
JS
10/////////////////////////////////////////////////////////////////////////////
11
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 */
20
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
83df96d6
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()
83df96d6
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
670f9935
WS
37// for compilers that support precompilation, includes "wx.h".
38#include "wx/wxprec.h"
39
83df96d6 40#include "wx/palette.h"
670f9935
WS
41
42#ifndef WX_PRECOMP
43 #include "wx/app.h"
de6185e2 44 #include "wx/utils.h"
670f9935
WS
45#endif
46
83df96d6 47#include "wx/window.h"
83df96d6
JS
48
49#ifdef __VMS__
50#pragma message disable nosimpint
51#endif
bc797f4c 52
83df96d6
JS
53#ifdef __VMS__
54#pragma message enable nosimpint
55#endif
7266b672 56#include "wx/x11/private.h"
83df96d6
JS
57
58IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
59IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
60
61/*
62* Palette
63*
64*/
65
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;
917be7ed 72 m_destroyable = false;
83df96d6
JS
73}
74
75wxPaletteRefData::wxPaletteRefData()
76{
77}
78
79wxPaletteRefData::~wxPaletteRefData()
80{
81 Display *display = (Display*) NULL;
82
ac32ba44 83 wxList::compatibility_iterator node, next;
83df96d6 84
09a1dffa
JS
85 for (node = m_palettes.GetFirst(); node; node = next) {
86 wxXPalette *c = (wxXPalette *)node->GetData();
83df96d6
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;
92
93 if (pix_array_n > 0)
94 {
788519c6 95#if !wxUSE_NANOX
83df96d6
JS
96 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
97 // Be careful not to free '0' pixels...
98 int i, j;
99 for(i=j=0; i<pix_array_n; i=j) {
100 while(j<pix_array_n && pix_array[j]!=0) j++;
101 if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0);
102 while(j<pix_array_n && pix_array[j]==0) j++;
103 }
788519c6 104#endif
83df96d6
JS
105 delete [] pix_array;
106 }
917be7ed 107
83df96d6
JS
108 if (destroyable)
109 XFreeColormap(display, cmap);
110
09a1dffa 111 next = node->GetNext();
ac32ba44 112 m_palettes.Erase(node);
83df96d6
JS
113 delete c;
114 }
115}
116
117wxPalette::wxPalette()
118{
119}
120
121wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
122{
123 Create(n, red, green, blue);
124}
125
126wxPalette::~wxPalette()
127{
128}
129
130bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
131{
132 UnRef();
133
134 if (!n) {
917be7ed 135 return false;
83df96d6
JS
136 }
137
138 m_refData = new wxPaletteRefData;
139
140 XColor xcol;
141 Display* display = (Display*) wxGetDisplay();
142
143 unsigned long *pix_array;
144 Colormap cmap;
145 int pix_array_n;
146
147 cmap = (Colormap) wxTheApp->GetMainColormap(display);
148
149 pix_array = new unsigned long[n];
150 if (!pix_array)
917be7ed 151 return false;
83df96d6
JS
152
153 pix_array_n = n;
154 xcol.flags = DoRed | DoGreen | DoBlue;
155 for(int i = 0; i < n; i++) {
156 xcol.red = (unsigned short)red[i] << 8;
157 xcol.green = (unsigned short)green[i] << 8;
158 xcol.blue = (unsigned short)blue[i] << 8;
159 pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel;
160 }
161
162 wxXPalette *c = new wxXPalette;
163
164 c->m_pix_array_n = pix_array_n;
165 c->m_pix_array = pix_array;
166 c->m_cmap = (WXColormap) cmap;
167 c->m_display = (WXDisplay*) display;
917be7ed 168 c->m_destroyable = false;
83df96d6
JS
169 M_PALETTEDATA->m_palettes.Append(c);
170
917be7ed 171 return true;
83df96d6
JS
172}
173
88ef3a57 174int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
83df96d6
JS
175{
176 if ( !m_refData )
88ef3a57 177 return wxNOT_FOUND;
83df96d6
JS
178
179 // TODO
88ef3a57 180 return wxNOT_FOUND;
83df96d6
JS
181}
182
183bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
184{
185 if ( !m_refData )
917be7ed 186 return false;
83df96d6
JS
187
188 if (index < 0 || index > 255)
917be7ed 189 return false;
83df96d6
JS
190
191 // TODO
917be7ed 192 return false;
83df96d6
JS
193}
194
195WXColormap wxPalette::GetXColormap(WXDisplay* display) const
196{
09a1dffa 197 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
83df96d6
JS
198 return wxTheApp->GetMainColormap(display);
199
ac32ba44 200 wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst();
83df96d6
JS
201 if (!display && node)
202 {
09a1dffa 203 wxXPalette* p = (wxXPalette*) node->GetData();
83df96d6
JS
204 return p->m_cmap;
205 }
206 while (node)
207 {
09a1dffa 208 wxXPalette* p = (wxXPalette*) node->GetData();
83df96d6
JS
209 if (p->m_display == display)
210 return p->m_cmap;
211
09a1dffa 212 node = node->GetNext();
83df96d6
JS
213 }
214
215 /* Make a new one: */
216 wxXPalette *c = new wxXPalette;
09a1dffa 217 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
83df96d6
JS
218 XColor xcol;
219 int pix_array_n = first->m_pix_array_n;
220
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;
83df96d6
JS
226
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];
232 XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol);
233 c->m_pix_array[i] =
234 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
235 }
236
237 // wxPalette* nonConstThis = (wxPalette*) this;
238
239 M_PALETTEDATA->m_palettes.Append(c);
240
241 return c->m_cmap;
242}
243
244bool wxPalette::TransferBitmap(void *data, int depth, int size)
245{
246 switch(depth) {
247 case 8:
248 {
249 unsigned char *uptr = (unsigned char *)data;
250 int pix_array_n;
251 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
252 while(size-- > 0)
253 {
254 if((int)*uptr < pix_array_n)
255 *uptr = (unsigned char)pix_array[*uptr];
256 uptr++;
257 }
258
917be7ed 259 return true;
83df96d6
JS
260 }
261 default:
917be7ed 262 return false;
83df96d6
JS
263 }
264}
265
266bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
267 void *dest, unsigned int bpp)
268{
269 int pix_array_n;
270 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
271 switch(bpp) {
272 case 8: {
273 unsigned char *dptr = (unsigned char *)dest;
274 while(sz-- > 0) {
275 if((int)*data < pix_array_n)
276 *dptr = (unsigned char)pix_array[*data];
277 data++;
278 dptr++;
279 }
280 break;
281 }
282 case 16: {
283 unsigned short *dptr = (unsigned short *)dest;
284 while(sz-- > 0) {
285 if((int)*data < pix_array_n)
286 *dptr = (unsigned short)pix_array[*data];
287 data++;
288 dptr++;
289 }
290 break;
291 }
292 case 24: {
293 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
294 while(sz-- > 0) {
295 if((int)*data < pix_array_n) {
296 dptr->r = pix_array[*data] & 0xFF;
297 dptr->g = (pix_array[*data] >> 8) & 0xFF;
298 dptr->b = (pix_array[*data] >> 16) & 0xFF;
299 }
300 data++;
301 dptr++;
302 }
303 break;
304 }
305 case 32: {
306 unsigned long *dptr = (unsigned long *)dest;
307 while(sz-- > 0) {
308 if((int)*data < pix_array_n)
309 *dptr = pix_array[*data];
310 data++;
311 dptr++;
312 }
313 break;
314 }
315 default:
917be7ed 316 return false;
83df96d6 317 }
917be7ed 318 return true;
83df96d6
JS
319}
320
321unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
322{
323 if (!M_PALETTEDATA)
324 return (unsigned long*) 0;
ac32ba44 325 wxList::compatibility_iterator node;
83df96d6 326
09a1dffa 327 for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; node = node->GetNext())
83df96d6 328 {
09a1dffa 329 wxXPalette *c = (wxXPalette *)node->GetData();
83df96d6
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 }
337
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();
348
349 m_refData = new wxPaletteRefData;
350
351 wxXPalette *c = new wxXPalette;
352
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;
358
359 M_PALETTEDATA->m_palettes.Append(c);
360}