]>
Commit | Line | Data |
---|---|---|
83df96d6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
88ef3a57 | 2 | // Name: src/x11/palette.cpp |
83df96d6 JS |
3 | // Purpose: wxPalette |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
83df96d6 | 7 | // Copyright: (c) Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
83df96d6 JS |
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 | |
77ffb593 | 22 | wxWidgets, notably wxPalette::Create(). The way I did it is to |
83df96d6 JS |
23 | allocate additional read-only color cells in the default colormap. In |
24 | general you will get arbitrary pixel values assigned to these new | |
917be7ed | 25 | cells and therefore I added a method wxPalette::TransferBitmap() |
83df96d6 JS |
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 | ||
670f9935 WS |
36 | // for compilers that support precompilation, includes "wx.h". |
37 | #include "wx/wxprec.h" | |
38 | ||
83df96d6 | 39 | #include "wx/palette.h" |
670f9935 WS |
40 | |
41 | #ifndef WX_PRECOMP | |
42 | #include "wx/app.h" | |
de6185e2 | 43 | #include "wx/utils.h" |
cdccdfab | 44 | #include "wx/window.h" |
670f9935 WS |
45 | #endif |
46 | ||
83df96d6 JS |
47 | #ifdef __VMS__ |
48 | #pragma message disable nosimpint | |
49 | #endif | |
bc797f4c | 50 | |
83df96d6 JS |
51 | #ifdef __VMS__ |
52 | #pragma message enable nosimpint | |
53 | #endif | |
7266b672 | 54 | #include "wx/x11/private.h" |
83df96d6 JS |
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; | |
917be7ed | 70 | m_destroyable = false; |
83df96d6 JS |
71 | } |
72 | ||
73 | wxPaletteRefData::wxPaletteRefData() | |
74 | { | |
75 | } | |
76 | ||
77 | wxPaletteRefData::~wxPaletteRefData() | |
78 | { | |
d3b9f782 | 79 | Display *display = NULL; |
83df96d6 | 80 | |
ac32ba44 | 81 | wxList::compatibility_iterator node, next; |
83df96d6 | 82 | |
09a1dffa JS |
83 | for (node = m_palettes.GetFirst(); node; node = next) { |
84 | wxXPalette *c = (wxXPalette *)node->GetData(); | |
83df96d6 JS |
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 | { | |
788519c6 | 93 | #if !wxUSE_NANOX |
83df96d6 JS |
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 | } | |
788519c6 | 102 | #endif |
83df96d6 JS |
103 | delete [] pix_array; |
104 | } | |
917be7ed | 105 | |
83df96d6 JS |
106 | if (destroyable) |
107 | XFreeColormap(display, cmap); | |
108 | ||
09a1dffa | 109 | next = node->GetNext(); |
ac32ba44 | 110 | m_palettes.Erase(node); |
83df96d6 JS |
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) { | |
917be7ed | 133 | return false; |
83df96d6 JS |
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) | |
917be7ed | 149 | return false; |
83df96d6 JS |
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; | |
917be7ed | 166 | c->m_destroyable = false; |
83df96d6 JS |
167 | M_PALETTEDATA->m_palettes.Append(c); |
168 | ||
917be7ed | 169 | return true; |
83df96d6 | 170 | } |
8f884a0d VZ |
171 | |
172 | wxGDIRefData *wxPalette::CreateGDIRefData() const | |
173 | { | |
174 | return new wxPaletteRefData; | |
175 | } | |
176 | ||
66f75561 VZ |
177 | wxGDIRefData * |
178 | wxPalette::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const | |
8f884a0d | 179 | { |
66f75561 VZ |
180 | wxFAIL_MSG( wxS("Cloning palettes is not implemented in wxX11.") ); |
181 | ||
182 | return new wxPaletteRefData; | |
8f884a0d | 183 | } |
83df96d6 | 184 | |
89954433 VZ |
185 | int wxPalette::GetPixel(unsigned char WXUNUSED(red), |
186 | unsigned char WXUNUSED(green), | |
187 | unsigned char WXUNUSED(blue)) const | |
83df96d6 JS |
188 | { |
189 | if ( !m_refData ) | |
88ef3a57 | 190 | return wxNOT_FOUND; |
83df96d6 JS |
191 | |
192 | // TODO | |
88ef3a57 | 193 | return wxNOT_FOUND; |
83df96d6 JS |
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 ) | |
917be7ed | 199 | return false; |
83df96d6 JS |
200 | |
201 | if (index < 0 || index > 255) | |
917be7ed | 202 | return false; |
83df96d6 JS |
203 | |
204 | // TODO | |
917be7ed | 205 | return false; |
83df96d6 JS |
206 | } |
207 | ||
208 | WXColormap wxPalette::GetXColormap(WXDisplay* display) const | |
209 | { | |
09a1dffa | 210 | if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0)) |
83df96d6 JS |
211 | return wxTheApp->GetMainColormap(display); |
212 | ||
ac32ba44 | 213 | wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst(); |
83df96d6 JS |
214 | if (!display && node) |
215 | { | |
09a1dffa | 216 | wxXPalette* p = (wxXPalette*) node->GetData(); |
83df96d6 JS |
217 | return p->m_cmap; |
218 | } | |
219 | while (node) | |
220 | { | |
09a1dffa | 221 | wxXPalette* p = (wxXPalette*) node->GetData(); |
83df96d6 JS |
222 | if (p->m_display == display) |
223 | return p->m_cmap; | |
224 | ||
09a1dffa | 225 | node = node->GetNext(); |
83df96d6 JS |
226 | } |
227 | ||
228 | /* Make a new one: */ | |
229 | wxXPalette *c = new wxXPalette; | |
09a1dffa | 230 | wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData(); |
83df96d6 JS |
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); | |
917be7ed | 238 | c->m_destroyable = false; |
83df96d6 JS |
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 | ||
917be7ed | 272 | return true; |
83df96d6 JS |
273 | } |
274 | default: | |
917be7ed | 275 | return false; |
83df96d6 JS |
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: | |
917be7ed | 329 | return false; |
83df96d6 | 330 | } |
917be7ed | 331 | return true; |
83df96d6 JS |
332 | } |
333 | ||
334 | unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n) | |
335 | { | |
336 | if (!M_PALETTEDATA) | |
337 | return (unsigned long*) 0; | |
ac32ba44 | 338 | wxList::compatibility_iterator node; |
83df96d6 | 339 | |
09a1dffa | 340 | for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; node = node->GetNext()) |
83df96d6 | 341 | { |
09a1dffa | 342 | wxXPalette *c = (wxXPalette *)node->GetData(); |
83df96d6 JS |
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 | } |