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