]> git.saurik.com Git - wxWidgets.git/blob - src/x11/palette.cpp
Upgrade bundled zlib to 1.2.8.
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 /*
12 * Colour map
13 *
14 * When constructed with the default constructor, we start from
15 * the wxApp::GetMainColormap, allocating additional read-only cells
16 * in Create(). The cells are freed on the next call to Create()
17 * or when the destructor is called.
18 */
19
20 /* Wolfram Gloger <u7y22ab@sunmail.lrz-muenchen.de>
21 I have implemented basic colormap support for the X11 versions of
22 wxWidgets, notably wxPalette::Create(). The way I did it is to
23 allocate additional read-only color cells in the default colormap. In
24 general you will get arbitrary pixel values assigned to these new
25 cells and therefore I added a method wxPalette::TransferBitmap()
26 which maps the pixel values 0..n to the real ones obtained with
27 Create(). This is only implemented for the popular case of 8-bit
28 depth.
29
30 Allocating read-write color cells would involve installing a private
31 X11 colormap for a particular window, and AFAIK this is not
32 recommended; only the window manager should do this... Also, it is
33 not the functionality that wxPalette::Create() aims to provide.
34 */
35
36 // for compilers that support precompilation, includes "wx.h".
37 #include "wx/wxprec.h"
38
39 #include "wx/palette.h"
40
41 #ifndef WX_PRECOMP
42 #include "wx/app.h"
43 #include "wx/utils.h"
44 #include "wx/window.h"
45 #endif
46
47 #ifdef __VMS__
48 #pragma message disable nosimpint
49 #endif
50
51 #ifdef __VMS__
52 #pragma message enable nosimpint
53 #endif
54 #include "wx/x11/private.h"
55
56 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
57 IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
58
59 /*
60 * Palette
61 *
62 */
63
64 wxXPalette::wxXPalette()
65 {
66 m_cmap = (WXColormap) 0;
67 m_pix_array_n = 0;
68 m_pix_array = (unsigned long*) 0;
69 m_display = (WXDisplay*) 0;
70 m_destroyable = false;
71 }
72
73 wxPaletteRefData::wxPaletteRefData()
74 {
75 }
76
77 wxPaletteRefData::~wxPaletteRefData()
78 {
79 Display *display = NULL;
80
81 wxList::compatibility_iterator node, next;
82
83 for (node = m_palettes.GetFirst(); node; node = next) {
84 wxXPalette *c = (wxXPalette *)node->GetData();
85 unsigned long *pix_array = c->m_pix_array;
86 Colormap cmap = (Colormap) c->m_cmap;
87 bool destroyable = c->m_destroyable;
88 int pix_array_n = c->m_pix_array_n;
89 display = (Display*) c->m_display;
90
91 if (pix_array_n > 0)
92 {
93 #if !wxUSE_NANOX
94 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
95 // Be careful not to free '0' pixels...
96 int i, j;
97 for(i=j=0; i<pix_array_n; i=j) {
98 while(j<pix_array_n && pix_array[j]!=0) j++;
99 if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0);
100 while(j<pix_array_n && pix_array[j]==0) j++;
101 }
102 #endif
103 delete [] pix_array;
104 }
105
106 if (destroyable)
107 XFreeColormap(display, cmap);
108
109 next = node->GetNext();
110 m_palettes.Erase(node);
111 delete c;
112 }
113 }
114
115 wxPalette::wxPalette()
116 {
117 }
118
119 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
120 {
121 Create(n, red, green, blue);
122 }
123
124 wxPalette::~wxPalette()
125 {
126 }
127
128 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
129 {
130 UnRef();
131
132 if (!n) {
133 return false;
134 }
135
136 m_refData = new wxPaletteRefData;
137
138 XColor xcol;
139 Display* display = (Display*) wxGetDisplay();
140
141 unsigned long *pix_array;
142 Colormap cmap;
143 int pix_array_n;
144
145 cmap = (Colormap) wxTheApp->GetMainColormap(display);
146
147 pix_array = new unsigned long[n];
148 if (!pix_array)
149 return false;
150
151 pix_array_n = n;
152 xcol.flags = DoRed | DoGreen | DoBlue;
153 for(int i = 0; i < n; i++) {
154 xcol.red = (unsigned short)red[i] << 8;
155 xcol.green = (unsigned short)green[i] << 8;
156 xcol.blue = (unsigned short)blue[i] << 8;
157 pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel;
158 }
159
160 wxXPalette *c = new wxXPalette;
161
162 c->m_pix_array_n = pix_array_n;
163 c->m_pix_array = pix_array;
164 c->m_cmap = (WXColormap) cmap;
165 c->m_display = (WXDisplay*) display;
166 c->m_destroyable = false;
167 M_PALETTEDATA->m_palettes.Append(c);
168
169 return true;
170 }
171
172 wxGDIRefData *wxPalette::CreateGDIRefData() const
173 {
174 return new wxPaletteRefData;
175 }
176
177 wxGDIRefData *
178 wxPalette::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
179 {
180 wxFAIL_MSG( wxS("Cloning palettes is not implemented in wxX11.") );
181
182 return new wxPaletteRefData;
183 }
184
185 int wxPalette::GetPixel(unsigned char WXUNUSED(red),
186 unsigned char WXUNUSED(green),
187 unsigned char WXUNUSED(blue)) const
188 {
189 if ( !m_refData )
190 return wxNOT_FOUND;
191
192 // TODO
193 return wxNOT_FOUND;
194 }
195
196 bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
197 {
198 if ( !m_refData )
199 return false;
200
201 if (index < 0 || index > 255)
202 return false;
203
204 // TODO
205 return false;
206 }
207
208 WXColormap wxPalette::GetXColormap(WXDisplay* display) const
209 {
210 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
211 return wxTheApp->GetMainColormap(display);
212
213 wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst();
214 if (!display && node)
215 {
216 wxXPalette* p = (wxXPalette*) node->GetData();
217 return p->m_cmap;
218 }
219 while (node)
220 {
221 wxXPalette* p = (wxXPalette*) node->GetData();
222 if (p->m_display == display)
223 return p->m_cmap;
224
225 node = node->GetNext();
226 }
227
228 /* Make a new one: */
229 wxXPalette *c = new wxXPalette;
230 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
231 XColor xcol;
232 int pix_array_n = first->m_pix_array_n;
233
234 c->m_pix_array_n = pix_array_n;
235 c->m_pix_array = new unsigned long[pix_array_n];
236 c->m_display = display;
237 c->m_cmap = wxTheApp->GetMainColormap(display);
238 c->m_destroyable = false;
239
240 xcol.flags = DoRed | DoGreen | DoBlue;
241 int i;
242 for (i = 0; i < pix_array_n; i++)
243 {
244 xcol.pixel = first->m_pix_array[i];
245 XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol);
246 c->m_pix_array[i] =
247 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
248 }
249
250 // wxPalette* nonConstThis = (wxPalette*) this;
251
252 M_PALETTEDATA->m_palettes.Append(c);
253
254 return c->m_cmap;
255 }
256
257 bool wxPalette::TransferBitmap(void *data, int depth, int size)
258 {
259 switch(depth) {
260 case 8:
261 {
262 unsigned char *uptr = (unsigned char *)data;
263 int pix_array_n;
264 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
265 while(size-- > 0)
266 {
267 if((int)*uptr < pix_array_n)
268 *uptr = (unsigned char)pix_array[*uptr];
269 uptr++;
270 }
271
272 return true;
273 }
274 default:
275 return false;
276 }
277 }
278
279 bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
280 void *dest, unsigned int bpp)
281 {
282 int pix_array_n;
283 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
284 switch(bpp) {
285 case 8: {
286 unsigned char *dptr = (unsigned char *)dest;
287 while(sz-- > 0) {
288 if((int)*data < pix_array_n)
289 *dptr = (unsigned char)pix_array[*data];
290 data++;
291 dptr++;
292 }
293 break;
294 }
295 case 16: {
296 unsigned short *dptr = (unsigned short *)dest;
297 while(sz-- > 0) {
298 if((int)*data < pix_array_n)
299 *dptr = (unsigned short)pix_array[*data];
300 data++;
301 dptr++;
302 }
303 break;
304 }
305 case 24: {
306 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
307 while(sz-- > 0) {
308 if((int)*data < pix_array_n) {
309 dptr->r = pix_array[*data] & 0xFF;
310 dptr->g = (pix_array[*data] >> 8) & 0xFF;
311 dptr->b = (pix_array[*data] >> 16) & 0xFF;
312 }
313 data++;
314 dptr++;
315 }
316 break;
317 }
318 case 32: {
319 unsigned long *dptr = (unsigned long *)dest;
320 while(sz-- > 0) {
321 if((int)*data < pix_array_n)
322 *dptr = pix_array[*data];
323 data++;
324 dptr++;
325 }
326 break;
327 }
328 default:
329 return false;
330 }
331 return true;
332 }
333
334 unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
335 {
336 if (!M_PALETTEDATA)
337 return (unsigned long*) 0;
338 wxList::compatibility_iterator node;
339
340 for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; node = node->GetNext())
341 {
342 wxXPalette *c = (wxXPalette *)node->GetData();
343 if (c->m_display == display)
344 {
345 if (n)
346 *n = c->m_pix_array_n;
347 return c->m_pix_array;
348 }
349 }
350
351 /* Not found; call GetXColormap, which will create it, then this again */
352 if (GetXColormap(display))
353 return GetXPixArray(display, n);
354 else
355 return (unsigned long*) 0;
356 }
357
358 void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
359 {
360 UnRef();
361
362 m_refData = new wxPaletteRefData;
363
364 wxXPalette *c = new wxXPalette;
365
366 c->m_pix_array_n = 0;
367 c->m_pix_array = (unsigned long*) NULL;
368 c->m_display = display;
369 c->m_cmap = cm;
370 c->m_destroyable = dp;
371
372 M_PALETTEDATA->m_palettes.Append(c);
373 }