]>
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 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
83df96d6 JS |
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 | |
77ffb593 | 23 | wxWidgets, notably wxPalette::Create(). The way I did it is to |
83df96d6 JS |
24 | allocate additional read-only color cells in the default colormap. In |
25 | general you will get arbitrary pixel values assigned to these new | |
917be7ed | 26 | cells and therefore I added a method wxPalette::TransferBitmap() |
83df96d6 JS |
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 | ||
670f9935 WS |
37 | // for compilers that support precompilation, includes "wx.h". |
38 | #include "wx/wxprec.h" | |
39 | ||
83df96d6 | 40 | #include "wx/palette.h" |
670f9935 WS |
41 | |
42 | #ifndef WX_PRECOMP | |
43 | #include "wx/app.h" | |
de6185e2 | 44 | #include "wx/utils.h" |
cdccdfab | 45 | #include "wx/window.h" |
670f9935 WS |
46 | #endif |
47 | ||
83df96d6 JS |
48 | #ifdef __VMS__ |
49 | #pragma message disable nosimpint | |
50 | #endif | |
bc797f4c | 51 | |
83df96d6 JS |
52 | #ifdef __VMS__ |
53 | #pragma message enable nosimpint | |
54 | #endif | |
7266b672 | 55 | #include "wx/x11/private.h" |
83df96d6 JS |
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; | |
917be7ed | 71 | m_destroyable = false; |
83df96d6 JS |
72 | } |
73 | ||
74 | wxPaletteRefData::wxPaletteRefData() | |
75 | { | |
76 | } | |
77 | ||
78 | wxPaletteRefData::~wxPaletteRefData() | |
79 | { | |
d3b9f782 | 80 | Display *display = NULL; |
83df96d6 | 81 | |
ac32ba44 | 82 | wxList::compatibility_iterator node, next; |
83df96d6 | 83 | |
09a1dffa JS |
84 | for (node = m_palettes.GetFirst(); node; node = next) { |
85 | wxXPalette *c = (wxXPalette *)node->GetData(); | |
83df96d6 JS |
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 | { | |
788519c6 | 94 | #if !wxUSE_NANOX |
83df96d6 JS |
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 | } | |
788519c6 | 103 | #endif |
83df96d6 JS |
104 | delete [] pix_array; |
105 | } | |
917be7ed | 106 | |
83df96d6 JS |
107 | if (destroyable) |
108 | XFreeColormap(display, cmap); | |
109 | ||
09a1dffa | 110 | next = node->GetNext(); |
ac32ba44 | 111 | m_palettes.Erase(node); |
83df96d6 JS |
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) { | |
917be7ed | 134 | return false; |
83df96d6 JS |
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) | |
917be7ed | 150 | return false; |
83df96d6 JS |
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; | |
917be7ed | 167 | c->m_destroyable = false; |
83df96d6 JS |
168 | M_PALETTEDATA->m_palettes.Append(c); |
169 | ||
917be7ed | 170 | return true; |
83df96d6 | 171 | } |
8f884a0d VZ |
172 | |
173 | wxGDIRefData *wxPalette::CreateGDIRefData() const | |
174 | { | |
175 | return new wxPaletteRefData; | |
176 | } | |
177 | ||
178 | wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const | |
179 | { | |
5c33522f | 180 | return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data)); |
8f884a0d | 181 | } |
83df96d6 | 182 | |
89954433 VZ |
183 | int wxPalette::GetPixel(unsigned char WXUNUSED(red), |
184 | unsigned char WXUNUSED(green), | |
185 | unsigned char WXUNUSED(blue)) const | |
83df96d6 JS |
186 | { |
187 | if ( !m_refData ) | |
88ef3a57 | 188 | return wxNOT_FOUND; |
83df96d6 JS |
189 | |
190 | // TODO | |
88ef3a57 | 191 | return wxNOT_FOUND; |
83df96d6 JS |
192 | } |
193 | ||
194 | bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const | |
195 | { | |
196 | if ( !m_refData ) | |
917be7ed | 197 | return false; |
83df96d6 JS |
198 | |
199 | if (index < 0 || index > 255) | |
917be7ed | 200 | return false; |
83df96d6 JS |
201 | |
202 | // TODO | |
917be7ed | 203 | return false; |
83df96d6 JS |
204 | } |
205 | ||
206 | WXColormap wxPalette::GetXColormap(WXDisplay* display) const | |
207 | { | |
09a1dffa | 208 | if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0)) |
83df96d6 JS |
209 | return wxTheApp->GetMainColormap(display); |
210 | ||
ac32ba44 | 211 | wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst(); |
83df96d6 JS |
212 | if (!display && node) |
213 | { | |
09a1dffa | 214 | wxXPalette* p = (wxXPalette*) node->GetData(); |
83df96d6 JS |
215 | return p->m_cmap; |
216 | } | |
217 | while (node) | |
218 | { | |
09a1dffa | 219 | wxXPalette* p = (wxXPalette*) node->GetData(); |
83df96d6 JS |
220 | if (p->m_display == display) |
221 | return p->m_cmap; | |
222 | ||
09a1dffa | 223 | node = node->GetNext(); |
83df96d6 JS |
224 | } |
225 | ||
226 | /* Make a new one: */ | |
227 | wxXPalette *c = new wxXPalette; | |
09a1dffa | 228 | wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData(); |
83df96d6 JS |
229 | XColor xcol; |
230 | int pix_array_n = first->m_pix_array_n; | |
231 | ||
232 | c->m_pix_array_n = pix_array_n; | |
233 | c->m_pix_array = new unsigned long[pix_array_n]; | |
234 | c->m_display = display; | |
235 | c->m_cmap = wxTheApp->GetMainColormap(display); | |
917be7ed | 236 | c->m_destroyable = false; |
83df96d6 JS |
237 | |
238 | xcol.flags = DoRed | DoGreen | DoBlue; | |
239 | int i; | |
240 | for (i = 0; i < pix_array_n; i++) | |
241 | { | |
242 | xcol.pixel = first->m_pix_array[i]; | |
243 | XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol); | |
244 | c->m_pix_array[i] = | |
245 | (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel; | |
246 | } | |
247 | ||
248 | // wxPalette* nonConstThis = (wxPalette*) this; | |
249 | ||
250 | M_PALETTEDATA->m_palettes.Append(c); | |
251 | ||
252 | return c->m_cmap; | |
253 | } | |
254 | ||
255 | bool wxPalette::TransferBitmap(void *data, int depth, int size) | |
256 | { | |
257 | switch(depth) { | |
258 | case 8: | |
259 | { | |
260 | unsigned char *uptr = (unsigned char *)data; | |
261 | int pix_array_n; | |
262 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
263 | while(size-- > 0) | |
264 | { | |
265 | if((int)*uptr < pix_array_n) | |
266 | *uptr = (unsigned char)pix_array[*uptr]; | |
267 | uptr++; | |
268 | } | |
269 | ||
917be7ed | 270 | return true; |
83df96d6 JS |
271 | } |
272 | default: | |
917be7ed | 273 | return false; |
83df96d6 JS |
274 | } |
275 | } | |
276 | ||
277 | bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz, | |
278 | void *dest, unsigned int bpp) | |
279 | { | |
280 | int pix_array_n; | |
281 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
282 | switch(bpp) { | |
283 | case 8: { | |
284 | unsigned char *dptr = (unsigned char *)dest; | |
285 | while(sz-- > 0) { | |
286 | if((int)*data < pix_array_n) | |
287 | *dptr = (unsigned char)pix_array[*data]; | |
288 | data++; | |
289 | dptr++; | |
290 | } | |
291 | break; | |
292 | } | |
293 | case 16: { | |
294 | unsigned short *dptr = (unsigned short *)dest; | |
295 | while(sz-- > 0) { | |
296 | if((int)*data < pix_array_n) | |
297 | *dptr = (unsigned short)pix_array[*data]; | |
298 | data++; | |
299 | dptr++; | |
300 | } | |
301 | break; | |
302 | } | |
303 | case 24: { | |
304 | struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest; | |
305 | while(sz-- > 0) { | |
306 | if((int)*data < pix_array_n) { | |
307 | dptr->r = pix_array[*data] & 0xFF; | |
308 | dptr->g = (pix_array[*data] >> 8) & 0xFF; | |
309 | dptr->b = (pix_array[*data] >> 16) & 0xFF; | |
310 | } | |
311 | data++; | |
312 | dptr++; | |
313 | } | |
314 | break; | |
315 | } | |
316 | case 32: { | |
317 | unsigned long *dptr = (unsigned long *)dest; | |
318 | while(sz-- > 0) { | |
319 | if((int)*data < pix_array_n) | |
320 | *dptr = pix_array[*data]; | |
321 | data++; | |
322 | dptr++; | |
323 | } | |
324 | break; | |
325 | } | |
326 | default: | |
917be7ed | 327 | return false; |
83df96d6 | 328 | } |
917be7ed | 329 | return true; |
83df96d6 JS |
330 | } |
331 | ||
332 | unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n) | |
333 | { | |
334 | if (!M_PALETTEDATA) | |
335 | return (unsigned long*) 0; | |
ac32ba44 | 336 | wxList::compatibility_iterator node; |
83df96d6 | 337 | |
09a1dffa | 338 | for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; node = node->GetNext()) |
83df96d6 | 339 | { |
09a1dffa | 340 | wxXPalette *c = (wxXPalette *)node->GetData(); |
83df96d6 JS |
341 | if (c->m_display == display) |
342 | { | |
343 | if (n) | |
344 | *n = c->m_pix_array_n; | |
345 | return c->m_pix_array; | |
346 | } | |
347 | } | |
348 | ||
349 | /* Not found; call GetXColormap, which will create it, then this again */ | |
350 | if (GetXColormap(display)) | |
351 | return GetXPixArray(display, n); | |
352 | else | |
353 | return (unsigned long*) 0; | |
354 | } | |
355 | ||
356 | void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp) | |
357 | { | |
358 | UnRef(); | |
359 | ||
360 | m_refData = new wxPaletteRefData; | |
361 | ||
362 | wxXPalette *c = new wxXPalette; | |
363 | ||
364 | c->m_pix_array_n = 0; | |
365 | c->m_pix_array = (unsigned long*) NULL; | |
366 | c->m_display = display; | |
367 | c->m_cmap = cm; | |
368 | c->m_destroyable = dp; | |
369 | ||
370 | M_PALETTEDATA->m_palettes.Append(c); | |
371 | } |