]> git.saurik.com Git - wxWidgets.git/blob - src/motif/palette.cpp
Added support for most print page/selection/copies
[wxWidgets.git] / src / motif / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 wxColourMap::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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
38 #pragma implementation "palette.h"
39 #endif
40
41 // For compilers that support precompilation, includes "wx.h".
42 #include "wx/wxprec.h"
43
44 #include "wx/palette.h"
45 #include "wx/window.h"
46 #include "wx/app.h"
47 #include "wx/utils.h"
48
49 #ifdef __VMS__
50 #pragma message disable nosimpint
51 #endif
52 #include <Xm/Xm.h>
53 #ifdef __VMS__
54 #pragma message enable nosimpint
55 #endif
56 #include "wx/motif/private.h"
57
58 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
59 IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
60
61 /*
62 * Palette
63 *
64 */
65
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;
72 m_destroyable = FALSE;
73 }
74
75 wxPaletteRefData::wxPaletteRefData()
76 {
77 }
78
79 wxPaletteRefData::~wxPaletteRefData()
80 {
81 Display *display = (Display*) NULL;
82
83 wxList::compatibility_iterator node, next;
84
85 for (node = m_palettes.GetFirst(); node; node = next) {
86 wxXPalette *c = (wxXPalette *)node->GetData();
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;
92
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 }
105
106 if (destroyable)
107 XFreeColormap(display, cmap);
108
109 next = node->GetNext();
110 m_palettes.Erase(node);
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) {
133 return FALSE;
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)
149 return FALSE;
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;
166 c->m_destroyable = FALSE;
167 M_PALETTEDATA->m_palettes.Append(c);
168
169 return TRUE;
170 }
171
172 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
173 {
174 if ( !m_refData )
175 return FALSE;
176
177 // TODO
178 return FALSE;
179 }
180
181 bool wxPalette::GetRGB(int index, unsigned char *WXUNUSED(red), unsigned char *WXUNUSED(green), unsigned char *WXUNUSED(blue)) const
182 {
183 if ( !m_refData )
184 return FALSE;
185
186 if (index < 0 || index > 255)
187 return FALSE;
188
189 // TODO
190 return FALSE;
191 }
192
193 WXColormap wxPalette::GetXColormap(WXDisplay* display) const
194 {
195 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.GetCount() == 0))
196 return wxTheApp->GetMainColormap(display);
197
198 wxList::compatibility_iterator node = M_PALETTEDATA->m_palettes.GetFirst();
199 if (!display && node)
200 {
201 wxXPalette* p = (wxXPalette*) node->GetData();
202 return p->m_cmap;
203 }
204 while (node)
205 {
206 wxXPalette* p = (wxXPalette*) node->GetData();
207 if (p->m_display == display)
208 return p->m_cmap;
209
210 node = node->GetNext();
211 }
212
213 /* Make a new one: */
214 wxXPalette *c = new wxXPalette;
215 wxXPalette *first =
216 (wxXPalette *)M_PALETTEDATA->m_palettes.GetFirst()->GetData();
217 XColor xcol;
218 int pix_array_n = first->m_pix_array_n;
219
220 c->m_pix_array_n = pix_array_n;
221 c->m_pix_array = new unsigned long[pix_array_n];
222 c->m_display = display;
223 c->m_cmap = wxTheApp->GetMainColormap(display);
224 c->m_destroyable = FALSE;
225
226 xcol.flags = DoRed | DoGreen | DoBlue;
227 int i;
228 for (i = 0; i < pix_array_n; i++)
229 {
230 xcol.pixel = first->m_pix_array[i];
231 XQueryColor((Display*) first->m_display,
232 (Colormap) first->m_cmap, &xcol);
233 c->m_pix_array[i] =
234 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0)
235 ? 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;
329 node = node->GetNext())
330 {
331 wxXPalette *c = (wxXPalette *)node->GetData();
332 if (c->m_display == display)
333 {
334 if (n)
335 *n = c->m_pix_array_n;
336 return c->m_pix_array;
337 }
338 }
339
340 /* Not found; call GetXColormap, which will create it, then this again */
341 if (GetXColormap(display))
342 return GetXPixArray(display, n);
343 else
344 return (unsigned long*) 0;
345 }
346
347 void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
348 {
349 UnRef();
350
351 m_refData = new wxPaletteRefData;
352
353 wxXPalette *c = new wxXPalette;
354
355 c->m_pix_array_n = 0;
356 c->m_pix_array = (unsigned long*) NULL;
357 c->m_display = display;
358 c->m_cmap = cm;
359 c->m_destroyable = dp;
360
361 M_PALETTEDATA->m_palettes.Append(c);
362 }
363