]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/palette.cpp
wxWindow::Fit() infinite loop bug fixed
[wxWidgets.git] / src / motif / palette.cpp
... / ...
CommitLineData
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>
22I have implemented basic colormap support for the X11 versions of
23wxWindows, notably wxPalette::Create(). The way I did it is to
24allocate additional read-only color cells in the default colormap. In
25general you will get arbitrary pixel values assigned to these new
26cells and therefore I added a method wxColourMap::TransferBitmap()
27which maps the pixel values 0..n to the real ones obtained with
28Create(). This is only implemented for the popular case of 8-bit
29depth.
30
31Allocating read-write color cells would involve installing a private
32X11 colormap for a particular window, and AFAIK this is not
33recommended; only the window manager should do this... Also, it is
34not the functionality that wxPalette::Create() aims to provide.
35 */
36
37#ifdef __GNUG__
38#pragma implementation "palette.h"
39#endif
40
41#include "wx/palette.h"
42#include "wx/window.h"
43#include "wx/app.h"
44#include "wx/utils.h"
45
46#include <Xm/Xm.h>
47#include "wx/motif/private.h"
48
49#if !USE_SHARED_LIBRARIES
50IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
51IMPLEMENT_DYNAMIC_CLASS(wxXPalette, wxObject)
52#endif
53
54/*
55* Palette
56*
57*/
58
59wxXPalette::wxXPalette()
60{
61 m_cmap = (WXColormap) 0;
62 m_pix_array_n = 0;
63 m_pix_array = (unsigned long*) 0;
64 m_display = (WXDisplay*) 0;
65 m_destroyable = FALSE;
66}
67
68wxPaletteRefData::wxPaletteRefData()
69{
70}
71
72wxPaletteRefData::~wxPaletteRefData()
73{
74 XColor xcol;
75 Display *display = (Display*) NULL;
76
77 wxNode *node, *next;
78
79 for (node = m_palettes.First(); node; node = next) {
80 wxXPalette *c = (wxXPalette *)node->Data();
81 unsigned long *pix_array = c->m_pix_array;
82 Colormap cmap = (Colormap) c->m_cmap;
83 bool destroyable = c->m_destroyable;
84 int pix_array_n = c->m_pix_array_n;
85 display = (Display*) c->m_display;
86
87 if (pix_array_n > 0)
88 {
89 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
90 // Be careful not to free '0' pixels...
91 int i, j;
92 for(i=j=0; i<pix_array_n; i=j) {
93 while(j<pix_array_n && pix_array[j]!=0) j++;
94 if(j > i) XFreeColors(display, cmap, &pix_array[i], j-i, 0);
95 while(j<pix_array_n && pix_array[j]==0) j++;
96 }
97 delete [] pix_array;
98 }
99
100 if (destroyable)
101 XFreeColormap(display, cmap);
102
103 next = node->Next();
104 m_palettes.DeleteNode(node);
105 delete c;
106 }
107}
108
109wxPalette::wxPalette()
110{
111}
112
113wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
114{
115 Create(n, red, green, blue);
116}
117
118wxPalette::~wxPalette()
119{
120}
121
122bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
123{
124 UnRef();
125
126 if (!n) {
127 return FALSE;
128 }
129
130 m_refData = new wxPaletteRefData;
131
132 XColor xcol;
133 Display* display = (Display*) wxGetDisplay();
134
135 unsigned long *pix_array;
136 Colormap cmap;
137 int pix_array_n;
138
139 cmap = (Colormap) wxTheApp->GetMainColormap(display);
140
141 pix_array = new unsigned long[n];
142 if (!pix_array)
143 return FALSE;
144
145 pix_array_n = n;
146 xcol.flags = DoRed | DoGreen | DoBlue;
147 for(int i = 0; i < n; i++) {
148 xcol.red = (unsigned short)red[i] << 8;
149 xcol.green = (unsigned short)green[i] << 8;
150 xcol.blue = (unsigned short)blue[i] << 8;
151 pix_array[i] = (XAllocColor(display, cmap, &xcol) == 0) ? 0 : xcol.pixel;
152 }
153
154 wxXPalette *c = new wxXPalette;
155
156 c->m_pix_array_n = pix_array_n;
157 c->m_pix_array = pix_array;
158 c->m_cmap = (WXColormap) cmap;
159 c->m_display = (WXDisplay*) display;
160 c->m_destroyable = FALSE;
161 M_PALETTEDATA->m_palettes.Append(c);
162
163 return TRUE;
164}
165
166int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
167{
168 if ( !m_refData )
169 return FALSE;
170
171 // TODO
172 return FALSE;
173}
174
175bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
176{
177 if ( !m_refData )
178 return FALSE;
179
180 if (index < 0 || index > 255)
181 return FALSE;
182
183 // TODO
184 return FALSE;
185}
186
187WXColormap wxPalette::GetXColormap(WXDisplay* display) const
188{
189 if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.Number() == 0))
190 return wxTheApp->GetMainColormap(display);
191
192 wxNode* node = M_PALETTEDATA->m_palettes.First();
193 if (!display && node)
194 {
195 wxXPalette* p = (wxXPalette*) node->Data();
196 return p->m_cmap;
197 }
198 while (node)
199 {
200 wxXPalette* p = (wxXPalette*) node->Data();
201 if (p->m_display == display)
202 return p->m_cmap;
203
204 node = node->Next();
205 }
206
207 /* Make a new one: */
208 wxXPalette *c = new wxXPalette;
209 wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.First()->Data();
210 XColor xcol;
211 int pix_array_n = first->m_pix_array_n;
212
213 c->m_pix_array_n = pix_array_n;
214 c->m_pix_array = new unsigned long[pix_array_n];
215 c->m_display = display;
216 c->m_cmap = wxTheApp->GetMainColormap(display);
217 c->m_destroyable = FALSE;
218
219 xcol.flags = DoRed | DoGreen | DoBlue;
220 int i;
221 for (i = 0; i < pix_array_n; i++)
222 {
223 xcol.pixel = first->m_pix_array[i];
224 XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol);
225 c->m_pix_array[i] =
226 (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
227 }
228
229 // wxPalette* nonConstThis = (wxPalette*) this;
230
231 M_PALETTEDATA->m_palettes.Append(c);
232
233 return c->m_cmap;
234}
235
236bool wxPalette::TransferBitmap(void *data, int depth, int size)
237{
238 switch(depth) {
239 case 8:
240 {
241 unsigned char *uptr = (unsigned char *)data;
242 int pix_array_n;
243 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
244 while(size-- > 0)
245 {
246 if((int)*uptr < pix_array_n)
247 *uptr = (unsigned char)pix_array[*uptr];
248 uptr++;
249 }
250
251 return TRUE;
252 }
253 default:
254 return FALSE;
255 }
256}
257
258bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
259 void *dest, unsigned int bpp)
260{
261 int pix_array_n;
262 unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
263 switch(bpp) {
264 case 8: {
265 unsigned char *dptr = (unsigned char *)dest;
266 while(sz-- > 0) {
267 if((int)*data < pix_array_n)
268 *dptr = (unsigned char)pix_array[*data];
269 data++;
270 dptr++;
271 }
272 break;
273 }
274 case 16: {
275 unsigned short *dptr = (unsigned short *)dest;
276 while(sz-- > 0) {
277 if((int)*data < pix_array_n)
278 *dptr = (unsigned short)pix_array[*data];
279 data++;
280 dptr++;
281 }
282 break;
283 }
284 case 24: {
285 struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
286 while(sz-- > 0) {
287 if((int)*data < pix_array_n) {
288 dptr->r = pix_array[*data] & 0xFF;
289 dptr->g = (pix_array[*data] >> 8) & 0xFF;
290 dptr->b = (pix_array[*data] >> 16) & 0xFF;
291 }
292 data++;
293 dptr++;
294 }
295 break;
296 }
297 case 32: {
298 unsigned long *dptr = (unsigned long *)dest;
299 while(sz-- > 0) {
300 if((int)*data < pix_array_n)
301 *dptr = pix_array[*data];
302 data++;
303 dptr++;
304 }
305 break;
306 }
307 default:
308 return FALSE;
309 }
310 return TRUE;
311}
312
313unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
314{
315 if (!M_PALETTEDATA)
316 return (unsigned long*) 0;
317 wxNode *node;
318
319 for (node = M_PALETTEDATA->m_palettes.First(); node; node = node->Next())
320 {
321 wxXPalette *c = (wxXPalette *)node->Data();
322 if (c->m_display == display)
323 {
324 if (n)
325 *n = c->m_pix_array_n;
326 return c->m_pix_array;
327 }
328 }
329
330 /* Not found; call GetXColormap, which will create it, then this again */
331 if (GetXColormap(display))
332 return GetXPixArray(display, n);
333 else
334 return (unsigned long*) 0;
335}
336
337void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
338{
339 UnRef();
340
341 m_refData = new wxPaletteRefData;
342
343 wxXPalette *c = new wxXPalette;
344
345 c->m_pix_array_n = 0;
346 c->m_pix_array = (unsigned long*) NULL;
347 c->m_display = display;
348 c->m_cmap = cm;
349 c->m_destroyable = dp;
350
351 M_PALETTEDATA->m_palettes.Append(c);
352}
353