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