]> git.saurik.com Git - wxWidgets.git/blob - src/x11/palette.cpp
Darwin dlopen functions is needed both for DYNLIB_CLASS and DYNAMIC_LOADER
[wxWidgets.git] / src / x11 / palette.cpp
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
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 wxWindows, 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 wxColourMap::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 #ifdef __GNUG__
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
49
50 #ifdef __VMS__
51 #pragma message enable nosimpint
52 #endif
53 #include "wx/x11/private.h"
54
55 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
56 IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
57
58 /*
59 * Palette
60 *
61 */
62
63 wxXPalette::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;
69 m_destroyable = FALSE;
70 }
71
72 wxPaletteRefData::wxPaletteRefData()
73 {
74 }
75
76 wxPaletteRefData::~wxPaletteRefData()
77 {
78 Display *display = (Display*) NULL;
79
80 wxNode *node, *next;
81
82 for (node = m_palettes.First(); node; node = next) {
83 wxXPalette *c = (wxXPalette *)node->Data();
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 {
92 #if !wxUSE_NANOX
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 }
101 #endif
102 delete [] pix_array;
103 }
104
105 if (destroyable)
106 XFreeColormap(display, cmap);
107
108 next = node->Next();
109 m_palettes.DeleteNode(node);
110 delete c;
111 }
112 }
113
114 wxPalette::wxPalette()
115 {
116 }
117
118 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
119 {
120 Create(n, red, green, blue);
121 }
122
123 wxPalette::~wxPalette()
124 {
125 }
126
127 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
128 {
129 UnRef();
130
131 if (!n) {
132 return FALSE;
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)
148 return FALSE;
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;
165 c->m_destroyable = FALSE;
166 M_PALETTEDATA->m_palettes.Append(c);
167
168 return TRUE;
169 }
170
171 int 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
180 bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
181 {
182 if ( !m_refData )
183 return FALSE;
184
185 if (index < 0 || index > 255)
186 return FALSE;
187
188 // TODO
189 return FALSE;
190 }
191
192 WXColormap wxPalette::GetXColormap(WXDisplay* display) const
193 {
194 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.Number() == 0))
195 return wxTheApp->GetMainColormap(display);
196
197 wxNode* node = M_PALETTEDATA->m_palettes.First();
198 if (!display && node)
199 {
200 wxXPalette* p = (wxXPalette*) node->Data();
201 return p->m_cmap;
202 }
203 while (node)
204 {
205 wxXPalette* p = (wxXPalette*) node->Data();
206 if (p->m_display == display)
207 return p->m_cmap;
208
209 node = node->Next();
210 }
211
212 /* Make a new one: */
213 wxXPalette *c = new wxXPalette;
214 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.First()->Data();
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);
222 c->m_destroyable = FALSE;
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
241 bool 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
256 return TRUE;
257 }
258 default:
259 return FALSE;
260 }
261 }
262
263 bool 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:
313 return FALSE;
314 }
315 return TRUE;
316 }
317
318 unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
319 {
320 if (!M_PALETTEDATA)
321 return (unsigned long*) 0;
322 wxNode *node;
323
324 for (node = M_PALETTEDATA->m_palettes.First(); node; node = node->Next())
325 {
326 wxXPalette *c = (wxXPalette *)node->Data();
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
342 void 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