]>
Commit | Line | Data |
---|---|---|
4bb6408c | 1 | ///////////////////////////////////////////////////////////////////////////// |
355b4d3d | 2 | // Name: src/motif/palette.cpp |
4bb6408c 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 |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f97c9854 JS |
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 | */ | |
bf6c2b35 | 20 | |
f97c9854 JS |
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 |
f97c9854 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() |
f97c9854 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 | ||
1248b41f MB |
37 | // For compilers that support precompilation, includes "wx.h". |
38 | #include "wx/wxprec.h" | |
39 | ||
4bb6408c | 40 | #include "wx/palette.h" |
670f9935 WS |
41 | |
42 | #ifndef WX_PRECOMP | |
43 | #include "wx/app.h" | |
de6185e2 | 44 | #include "wx/utils.h" |
670f9935 WS |
45 | #endif |
46 | ||
f97c9854 | 47 | #include "wx/window.h" |
f97c9854 | 48 | |
338dd992 JJ |
49 | #ifdef __VMS__ |
50 | #pragma message disable nosimpint | |
51 | #endif | |
f97c9854 | 52 | #include <Xm/Xm.h> |
338dd992 JJ |
53 | #ifdef __VMS__ |
54 | #pragma message enable nosimpint | |
55 | #endif | |
f97c9854 | 56 | #include "wx/motif/private.h" |
4bb6408c | 57 | |
4bb6408c | 58 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
f97c9854 | 59 | IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject) |
4bb6408c JS |
60 | |
61 | /* | |
2d120f83 JS |
62 | * Palette |
63 | * | |
64 | */ | |
4bb6408c | 65 | |
f97c9854 JS |
66 | wxXPalette::wxXPalette() |
67 | { | |
68 | m_cmap = (WXColormap) 0; | |
69 | m_pix_array_n = 0; | |
70 | m_pix_array = (unsigned long*) 0; | |
71 | m_display = (WXDisplay*) 0; | |
917be7ed | 72 | m_destroyable = false; |
f97c9854 JS |
73 | } |
74 | ||
4bb6408c JS |
75 | wxPaletteRefData::wxPaletteRefData() |
76 | { | |
4bb6408c JS |
77 | } |
78 | ||
79 | wxPaletteRefData::~wxPaletteRefData() | |
80 | { | |
2d120f83 | 81 | Display *display = (Display*) NULL; |
bf6c2b35 | 82 | |
ac32ba44 | 83 | wxList::compatibility_iterator node, next; |
bf6c2b35 | 84 | |
1bc822df MB |
85 | for (node = m_palettes.GetFirst(); node; node = next) { |
86 | wxXPalette *c = (wxXPalette *)node->GetData(); | |
2d120f83 JS |
87 | unsigned long *pix_array = c->m_pix_array; |
88 | Colormap cmap = (Colormap) c->m_cmap; | |
89 | bool destroyable = c->m_destroyable; | |
90 | int pix_array_n = c->m_pix_array_n; | |
91 | display = (Display*) c->m_display; | |
bf6c2b35 | 92 | |
2d120f83 JS |
93 | if (pix_array_n > 0) |
94 | { | |
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 | delete [] pix_array; | |
104 | } | |
bf6c2b35 | 105 | |
2d120f83 JS |
106 | if (destroyable) |
107 | XFreeColormap(display, cmap); | |
bf6c2b35 | 108 | |
1bc822df | 109 | next = node->GetNext(); |
ac32ba44 | 110 | m_palettes.Erase(node); |
2d120f83 | 111 | delete c; |
f97c9854 | 112 | } |
4bb6408c JS |
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 | { | |
2d120f83 | 130 | UnRef(); |
bf6c2b35 | 131 | |
2d120f83 | 132 | if (!n) { |
917be7ed | 133 | return false; |
2d120f83 | 134 | } |
bf6c2b35 | 135 | |
2d120f83 | 136 | m_refData = new wxPaletteRefData; |
bf6c2b35 | 137 | |
2d120f83 JS |
138 | XColor xcol; |
139 | Display* display = (Display*) wxGetDisplay(); | |
bf6c2b35 | 140 | |
2d120f83 JS |
141 | unsigned long *pix_array; |
142 | Colormap cmap; | |
143 | int pix_array_n; | |
bf6c2b35 | 144 | |
2d120f83 | 145 | cmap = (Colormap) wxTheApp->GetMainColormap(display); |
bf6c2b35 | 146 | |
2d120f83 JS |
147 | pix_array = new unsigned long[n]; |
148 | if (!pix_array) | |
917be7ed | 149 | return false; |
bf6c2b35 | 150 | |
2d120f83 JS |
151 | pix_array_n = n; |
152 | xcol.flags = DoRed | DoGreen | DoBlue; | |
153 | for(int i = 0; i < n; i++) { | |
355b4d3d WS |
154 | xcol.red = (unsigned short)(red[i] << 8); |
155 | xcol.green = (unsigned short)(green[i] << 8); | |
156 | xcol.blue = (unsigned short)(blue[i] << 8); | |
2d120f83 JS |
157 | pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel; |
158 | } | |
bf6c2b35 | 159 | |
2d120f83 | 160 | wxXPalette *c = new wxXPalette; |
bf6c2b35 | 161 | |
2d120f83 JS |
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; |
2d120f83 | 167 | M_PALETTEDATA->m_palettes.Append(c); |
bf6c2b35 | 168 | |
917be7ed | 169 | return true; |
4bb6408c JS |
170 | } |
171 | ||
88ef3a57 WS |
172 | int wxPalette::GetPixel(unsigned char WXUNUSED(red), |
173 | unsigned char WXUNUSED(green), | |
174 | unsigned char WXUNUSED(blue)) const | |
4bb6408c JS |
175 | { |
176 | if ( !m_refData ) | |
88ef3a57 | 177 | return wxNOT_FOUND; |
bf6c2b35 | 178 | |
4bb6408c | 179 | // TODO |
88ef3a57 | 180 | return wxNOT_FOUND; |
4bb6408c JS |
181 | } |
182 | ||
af111fc3 | 183 | bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const |
4bb6408c JS |
184 | { |
185 | if ( !m_refData ) | |
917be7ed | 186 | return false; |
bf6c2b35 | 187 | |
4bb6408c | 188 | if (index < 0 || index > 255) |
917be7ed | 189 | return false; |
bf6c2b35 | 190 | |
4bb6408c | 191 | // TODO |
917be7ed | 192 | return false; |
4bb6408c JS |
193 | } |
194 | ||
f97c9854 JS |
195 | WXColormap wxPalette::GetXColormap(WXDisplay* display) const |
196 | { | |
1bc822df | 197 | if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0)) |
f97c9854 | 198 | return wxTheApp->GetMainColormap(display); |
bf6c2b35 | 199 | |
ac32ba44 | 200 | wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst(); |
f97c9854 JS |
201 | if (!display && node) |
202 | { | |
1bc822df | 203 | wxXPalette* p = (wxXPalette*) node->GetData(); |
f97c9854 JS |
204 | return p->m_cmap; |
205 | } | |
206 | while (node) | |
207 | { | |
1bc822df | 208 | wxXPalette* p = (wxXPalette*) node->GetData(); |
f97c9854 JS |
209 | if (p->m_display == display) |
210 | return p->m_cmap; | |
bf6c2b35 | 211 | |
1bc822df | 212 | node = node->GetNext(); |
f97c9854 | 213 | } |
bf6c2b35 | 214 | |
f97c9854 JS |
215 | /* Make a new one: */ |
216 | wxXPalette *c = new wxXPalette; | |
1bc822df MB |
217 | wxXPalette *first = |
218 | (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData(); | |
f97c9854 JS |
219 | XColor xcol; |
220 | int pix_array_n = first->m_pix_array_n; | |
bf6c2b35 | 221 | |
f97c9854 JS |
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); | |
917be7ed | 226 | c->m_destroyable = false; |
bf6c2b35 | 227 | |
f97c9854 JS |
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]; | |
1bc822df MB |
233 | XQueryColor((Display*) first->m_display, |
234 | (Colormap) first->m_cmap, &xcol); | |
f97c9854 | 235 | c->m_pix_array[i] = |
1bc822df MB |
236 | (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) |
237 | ? 0 : xcol.pixel; | |
f97c9854 | 238 | } |
bf6c2b35 | 239 | |
f97c9854 | 240 | // wxPalette* nonConstThis = (wxPalette*) this; |
bf6c2b35 | 241 | |
f97c9854 | 242 | M_PALETTEDATA->m_palettes.Append(c); |
bf6c2b35 | 243 | |
f97c9854 JS |
244 | return c->m_cmap; |
245 | } | |
246 | ||
247 | bool wxPalette::TransferBitmap(void *data, int depth, int size) | |
248 | { | |
2d120f83 JS |
249 | switch(depth) { |
250 | case 8: | |
251 | { | |
252 | unsigned char *uptr = (unsigned char *)data; | |
253 | int pix_array_n; | |
254 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
255 | while(size-- > 0) | |
256 | { | |
257 | if((int)*uptr < pix_array_n) | |
258 | *uptr = (unsigned char)pix_array[*uptr]; | |
259 | uptr++; | |
260 | } | |
bf6c2b35 | 261 | |
917be7ed | 262 | return true; |
2d120f83 JS |
263 | } |
264 | default: | |
917be7ed | 265 | return false; |
f97c9854 | 266 | } |
f97c9854 JS |
267 | } |
268 | ||
269 | bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz, | |
2d120f83 | 270 | void *dest, unsigned int bpp) |
f97c9854 | 271 | { |
2d120f83 JS |
272 | int pix_array_n; |
273 | unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n); | |
f97c9854 JS |
274 | switch(bpp) { |
275 | case 8: { | |
2d120f83 JS |
276 | unsigned char *dptr = (unsigned char *)dest; |
277 | while(sz-- > 0) { | |
278 | if((int)*data < pix_array_n) | |
279 | *dptr = (unsigned char)pix_array[*data]; | |
280 | data++; | |
281 | dptr++; | |
282 | } | |
283 | break; | |
284 | } | |
f97c9854 | 285 | case 16: { |
2d120f83 JS |
286 | unsigned short *dptr = (unsigned short *)dest; |
287 | while(sz-- > 0) { | |
288 | if((int)*data < pix_array_n) | |
289 | *dptr = (unsigned short)pix_array[*data]; | |
290 | data++; | |
291 | dptr++; | |
292 | } | |
293 | break; | |
294 | } | |
f97c9854 | 295 | case 24: { |
2d120f83 JS |
296 | struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest; |
297 | while(sz-- > 0) { | |
298 | if((int)*data < pix_array_n) { | |
355b4d3d WS |
299 | dptr->r = (unsigned char)(pix_array[*data] & 0xFF); |
300 | dptr->g = (unsigned char)((pix_array[*data] >> 8) & 0xFF); | |
301 | dptr->b = (unsigned char)((pix_array[*data] >> 16) & 0xFF); | |
2d120f83 JS |
302 | } |
303 | data++; | |
304 | dptr++; | |
f97c9854 | 305 | } |
2d120f83 JS |
306 | break; |
307 | } | |
f97c9854 | 308 | case 32: { |
2d120f83 JS |
309 | unsigned long *dptr = (unsigned long *)dest; |
310 | while(sz-- > 0) { | |
311 | if((int)*data < pix_array_n) | |
312 | *dptr = pix_array[*data]; | |
313 | data++; | |
314 | dptr++; | |
315 | } | |
316 | break; | |
317 | } | |
f97c9854 | 318 | default: |
917be7ed | 319 | return false; |
f97c9854 | 320 | } |
917be7ed | 321 | return true; |
f97c9854 JS |
322 | } |
323 | ||
324 | unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n) | |
325 | { | |
326 | if (!M_PALETTEDATA) | |
327 | return (unsigned long*) 0; | |
ac32ba44 | 328 | wxList::compatibility_iterator node; |
bf6c2b35 | 329 | |
1bc822df MB |
330 | for (node = M_PALETTEDATA->m_palettes.GetFirst(); node; |
331 | node = node->GetNext()) | |
f97c9854 | 332 | { |
1bc822df | 333 | wxXPalette *c = (wxXPalette *)node->GetData(); |
f97c9854 JS |
334 | if (c->m_display == display) |
335 | { | |
336 | if (n) | |
337 | *n = c->m_pix_array_n; | |
338 | return c->m_pix_array; | |
339 | } | |
340 | } | |
bf6c2b35 | 341 | |
f97c9854 JS |
342 | /* Not found; call GetXColormap, which will create it, then this again */ |
343 | if (GetXColormap(display)) | |
344 | return GetXPixArray(display, n); | |
345 | else | |
346 | return (unsigned long*) 0; | |
347 | } | |
348 | ||
349 | void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp) | |
350 | { | |
351 | UnRef(); | |
bf6c2b35 | 352 | |
f97c9854 | 353 | m_refData = new wxPaletteRefData; |
bf6c2b35 | 354 | |
f97c9854 | 355 | wxXPalette *c = new wxXPalette; |
bf6c2b35 | 356 | |
f97c9854 JS |
357 | c->m_pix_array_n = 0; |
358 | c->m_pix_array = (unsigned long*) NULL; | |
359 | c->m_display = display; | |
360 | c->m_cmap = cm; | |
361 | c->m_destroyable = dp; | |
bf6c2b35 | 362 | |
f97c9854 JS |
363 | M_PALETTEDATA->m_palettes.Append(c); |
364 | } |