1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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.
21 /* Wolfram Gloger <u7y22ab@sunmail.lrz-muenchen.de>
22 I have implemented basic colormap support for the X11 versions of
23 wxWindows, 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
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.
38 #pragma implementation "palette.h"
41 #include "wx/palette.h"
42 #include "wx/window.h"
47 #pragma message disable nosimpint
51 #pragma message enable nosimpint
53 #include "wx/motif/private.h"
55 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
56 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
63 wxXPalette::wxXPalette()
65 m_cmap
= (WXColormap
) 0;
67 m_pix_array
= (unsigned long*) 0;
68 m_display
= (WXDisplay
*) 0;
69 m_destroyable
= FALSE
;
72 wxPaletteRefData::wxPaletteRefData()
76 wxPaletteRefData::~wxPaletteRefData()
78 Display
*display
= (Display
*) NULL
;
80 wxList::compatibility_iterator node
, next
;
82 for (node
= m_palettes
.GetFirst(); node
; node
= next
) {
83 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
84 unsigned long *pix_array
= c
->m_pix_array
;
85 Colormap cmap
= (Colormap
) c
->m_cmap
;
86 bool destroyable
= c
->m_destroyable
;
87 int pix_array_n
= c
->m_pix_array_n
;
88 display
= (Display
*) c
->m_display
;
92 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
93 // Be careful not to free '0' pixels...
95 for(i
=j
=0; i
<pix_array_n
; i
=j
) {
96 while(j
<pix_array_n
&& pix_array
[j
]!=0) j
++;
97 if(j
> i
) XFreeColors(display
, cmap
, &pix_array
[i
], j
-i
, 0);
98 while(j
<pix_array_n
&& pix_array
[j
]==0) j
++;
104 XFreeColormap(display
, cmap
);
106 next
= node
->GetNext();
107 m_palettes
.Erase(node
);
112 wxPalette::wxPalette()
116 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
118 Create(n
, red
, green
, blue
);
121 wxPalette::~wxPalette()
125 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
133 m_refData
= new wxPaletteRefData
;
136 Display
* display
= (Display
*) wxGetDisplay();
138 unsigned long *pix_array
;
142 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
144 pix_array
= new unsigned long[n
];
149 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
150 for(int i
= 0; i
< n
; i
++) {
151 xcol
.red
= (unsigned short)red
[i
] << 8;
152 xcol
.green
= (unsigned short)green
[i
] << 8;
153 xcol
.blue
= (unsigned short)blue
[i
] << 8;
154 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
157 wxXPalette
*c
= new wxXPalette
;
159 c
->m_pix_array_n
= pix_array_n
;
160 c
->m_pix_array
= pix_array
;
161 c
->m_cmap
= (WXColormap
) cmap
;
162 c
->m_display
= (WXDisplay
*) display
;
163 c
->m_destroyable
= FALSE
;
164 M_PALETTEDATA
->m_palettes
.Append(c
);
169 int wxPalette::GetPixel(const unsigned char red
, const unsigned char green
, const unsigned char blue
) const
178 bool wxPalette::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
183 if (index
< 0 || index
> 255)
190 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
192 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.GetCount() == 0))
193 return wxTheApp
->GetMainColormap(display
);
195 wxList::compatibility_iterator node
= M_PALETTEDATA
->m_palettes
.GetFirst();
196 if (!display
&& node
)
198 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
203 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
204 if (p
->m_display
== display
)
207 node
= node
->GetNext();
210 /* Make a new one: */
211 wxXPalette
*c
= new wxXPalette
;
213 (wxXPalette
*)M_PALETTEDATA
->m_palettes
.GetFirst()->GetData();
215 int pix_array_n
= first
->m_pix_array_n
;
217 c
->m_pix_array_n
= pix_array_n
;
218 c
->m_pix_array
= new unsigned long[pix_array_n
];
219 c
->m_display
= display
;
220 c
->m_cmap
= wxTheApp
->GetMainColormap(display
);
221 c
->m_destroyable
= FALSE
;
223 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
225 for (i
= 0; i
< pix_array_n
; i
++)
227 xcol
.pixel
= first
->m_pix_array
[i
];
228 XQueryColor((Display
*) first
->m_display
,
229 (Colormap
) first
->m_cmap
, &xcol
);
231 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0)
235 // wxPalette* nonConstThis = (wxPalette*) this;
237 M_PALETTEDATA
->m_palettes
.Append(c
);
242 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
247 unsigned char *uptr
= (unsigned char *)data
;
249 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
252 if((int)*uptr
< pix_array_n
)
253 *uptr
= (unsigned char)pix_array
[*uptr
];
264 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
265 void *dest
, unsigned int bpp
)
268 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
271 unsigned char *dptr
= (unsigned char *)dest
;
273 if((int)*data
< pix_array_n
)
274 *dptr
= (unsigned char)pix_array
[*data
];
281 unsigned short *dptr
= (unsigned short *)dest
;
283 if((int)*data
< pix_array_n
)
284 *dptr
= (unsigned short)pix_array
[*data
];
291 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
293 if((int)*data
< pix_array_n
) {
294 dptr
->r
= pix_array
[*data
] & 0xFF;
295 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
296 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
304 unsigned long *dptr
= (unsigned long *)dest
;
306 if((int)*data
< pix_array_n
)
307 *dptr
= pix_array
[*data
];
319 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
322 return (unsigned long*) 0;
323 wxList::compatibility_iterator node
;
325 for (node
= M_PALETTEDATA
->m_palettes
.GetFirst(); node
;
326 node
= node
->GetNext())
328 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
329 if (c
->m_display
== display
)
332 *n
= c
->m_pix_array_n
;
333 return c
->m_pix_array
;
337 /* Not found; call GetXColormap, which will create it, then this again */
338 if (GetXColormap(display
))
339 return GetXPixArray(display
, n
);
341 return (unsigned long*) 0;
344 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
348 m_refData
= new wxPaletteRefData
;
350 wxXPalette
*c
= new wxXPalette
;
352 c
->m_pix_array_n
= 0;
353 c
->m_pix_array
= (unsigned long*) NULL
;
354 c
->m_display
= display
;
356 c
->m_destroyable
= dp
;
358 M_PALETTEDATA
->m_palettes
.Append(c
);