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