Include wx/utils.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / x11 / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/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
9 // Licence: wxWindows licence
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>
22 I have implemented basic colormap support for the X11 versions of
23 wxWidgets, notably wxPalette::Create(). The way I did it is to
24 allocate additional read-only color cells in the default colormap. In
25 general you will get arbitrary pixel values assigned to these new
26 cells and therefore I added a method wxPalette::TransferBitmap()
27 which maps the pixel values 0..n to the real ones obtained with
28 Create(). This is only implemented for the popular case of 8-bit
29 depth.
30
31 Allocating read-write color cells would involve installing a private
32 X11 colormap for a particular window, and AFAIK this is not
33 recommended; only the window manager should do this... Also, it is
34 not the functionality that wxPalette::Create() aims to provide.
35 */
36
37 // for compilers that support precompilation, includes "wx.h".
38 #include "wx/wxprec.h"
39
40 #include "wx/palette.h"
41
42 #ifndef WX_PRECOMP
43 #include "wx/app.h"
44 #include "wx/utils.h"
45 #endif
46
47 #include "wx/window.h"
48
49 #ifdef __VMS__
50 #pragma message disable nosimpint
51 #endif
52
53 #ifdef __VMS__
54 #pragma message enable nosimpint
55 #endif
56 #include "wx/x11/private.h"
57
58 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
59 IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
60
61 /*
62 * Palette
63 *
64 */
65
66 wxXPalette::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;
72 m_destroyable = false;
73 }
74
75 wxPaletteRefData::wxPaletteRefData()
76 {
77 }
78
79 wxPaletteRefData::~wxPaletteRefData()
80 {
81 Display *display = (Display*) NULL;
82
83 wxList::compatibility_iterator node, next;
84
85 for (node = m_palettes.GetFirst(); node; node = next) {
86 wxXPalette *c = (wxXPalette *)node->GetData();
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 {
95 #if !wxUSE_NANOX
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 }
104 #endif
105 delete [] pix_array;
106 }
107
108 if (destroyable)
109 XFreeColormap(display, cmap);
110
111 next = node->GetNext();
112 m_palettes.Erase(node);
113 delete c;
114 }
115 }
116
117 wxPalette::wxPalette()
118 {
119 }
120
121 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
122 {
123 Create(n, red, green, blue);
124 }
125
126 wxPalette::~wxPalette()
127 {
128 }
129
130 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
131 {
132 UnRef();
133
134 if (!n) {
135 return false;
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)
151 return false;
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;
168 c->m_destroyable = false;
169 M_PALETTEDATA->m_palettes.Append(c);
170
171 return true;
172 }
173
174 int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
175 {
176 if ( !m_refData )
177 return wxNOT_FOUND;
178
179 // TODO
180 return wxNOT_FOUND;
181 }
182
183 bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
184 {
185 if ( !m_refData )
186 return false;
187
188 if (index < 0 || index > 255)
189 return false;
190
191 // TODO
192 return false;
193 }
194
195 WXColormap wxPalette::GetXColormap(WXDisplay* display) const
196 {
197 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
198 return wxTheApp->GetMainColormap(display);
199
200 wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst();
201 if (!display && node)
202 {
203 wxXPalette* p = (wxXPalette*) node->GetData();
204 return p->m_cmap;
205 }
206 while (node)
207 {
208 wxXPalette* p = (wxXPalette*) node->GetData();
209 if (p->m_display == display)
210 return p->m_cmap;
211
212 node = node->GetNext();
213 }
214
215 /* Make a new one: */
216 wxXPalette *c = new wxXPalette;
217 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
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);
225 c->m_destroyable = false;
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
244 bool 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
259 return true;
260 }
261 default:
262 return false;
263 }
264 }
265
266 bool 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:
316 return false;
317 }
318 return true;
319 }
320
321 unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
322 {
323 if (!M_PALETTEDATA)
324 return (unsigned long*) 0;
325 wxList::compatibility_iterator node;
326
327 for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; node = node->GetNext())
328 {
329 wxXPalette *c = (wxXPalette *)node->GetData();
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
345 void 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 }