1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/palette.cpp
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 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
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.
37 // for compilers that support precompilation, includes "wx.h".
38 #include "wx/wxprec.h"
40 #include "wx/palette.h"
47 #include "wx/window.h"
50 #pragma message disable nosimpint
54 #pragma message enable nosimpint
56 #include "wx/x11/private.h"
58 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
59 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
66 wxXPalette
::wxXPalette()
68 m_cmap
= (WXColormap
) 0;
70 m_pix_array
= (unsigned long*) 0;
71 m_display
= (WXDisplay
*) 0;
72 m_destroyable
= false;
75 wxPaletteRefData
::wxPaletteRefData()
79 wxPaletteRefData
::~wxPaletteRefData()
81 Display
*display
= (Display
*) NULL
;
83 wxList
::compatibility_iterator node
, next
;
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
;
96 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
97 // Be careful not to free '0' pixels...
99 for(i
=j
=0; i
<pix_array_n
; i
=j
) {
100 while(j
<pix_array_n
&& pix_array
[j
]!=0) j
++;
101 if(j
> i
) XFreeColors(display
, cmap
, &pix_array
[i
], j
-i
, 0);
102 while(j
<pix_array_n
&& pix_array
[j
]==0) j
++;
109 XFreeColormap(display
, cmap
);
111 next
= node
->GetNext();
112 m_palettes
.Erase(node
);
117 wxPalette
::wxPalette()
121 wxPalette
::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
123 Create(n
, red
, green
, blue
);
126 wxPalette
::~wxPalette()
130 bool wxPalette
::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
138 m_refData
= new wxPaletteRefData
;
141 Display
* display
= (Display
*) wxGetDisplay();
143 unsigned long *pix_array
;
147 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
149 pix_array
= new unsigned long[n
];
154 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
155 for(int i
= 0; i
< n
; i
++) {
156 xcol
.red
= (unsigned short)red
[i
] << 8;
157 xcol
.green
= (unsigned short)green
[i
] << 8;
158 xcol
.blue
= (unsigned short)blue
[i
] << 8;
159 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ?
0 : xcol
.pixel
;
162 wxXPalette
*c
= new wxXPalette
;
164 c
->m_pix_array_n
= pix_array_n
;
165 c
->m_pix_array
= pix_array
;
166 c
->m_cmap
= (WXColormap
) cmap
;
167 c
->m_display
= (WXDisplay
*) display
;
168 c
->m_destroyable
= false;
169 M_PALETTEDATA
->m_palettes
.Append(c
);
174 int wxPalette
::GetPixel(unsigned char red
, unsigned char green
, unsigned char blue
) const
183 bool wxPalette
::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
188 if (index
< 0 || index
> 255)
195 WXColormap wxPalette
::GetXColormap(WXDisplay
* display
) const
197 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.GetCount() == 0))
198 return wxTheApp
->GetMainColormap(display
);
200 wxList
::compatibility_iterator node
= M_PALETTEDATA
->m_palettes
.GetFirst();
201 if (!display
&& node
)
203 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
208 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
209 if (p
->m_display
== display
)
212 node
= node
->GetNext();
215 /* Make a new one: */
216 wxXPalette
*c
= new wxXPalette
;
217 wxXPalette
*first
= (wxXPalette
*)M_PALETTEDATA
->m_palettes
.GetFirst()->GetData();
219 int pix_array_n
= first
->m_pix_array_n
;
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;
227 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
229 for (i
= 0; i
< pix_array_n
; i
++)
231 xcol
.pixel
= first
->m_pix_array
[i
];
232 XQueryColor((Display
*) first
->m_display
, (Colormap
) first
->m_cmap
, &xcol
);
234 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ?
0 : xcol
.pixel
;
237 // wxPalette* nonConstThis = (wxPalette*) this;
239 M_PALETTEDATA
->m_palettes
.Append(c
);
244 bool wxPalette
::TransferBitmap(void *data
, int depth
, int size
)
249 unsigned char *uptr
= (unsigned char *)data
;
251 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
254 if((int)*uptr
< pix_array_n
)
255 *uptr
= (unsigned char)pix_array
[*uptr
];
266 bool wxPalette
::TransferBitmap8(unsigned char *data
, unsigned long sz
,
267 void *dest
, unsigned int bpp
)
270 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
273 unsigned char *dptr
= (unsigned char *)dest
;
275 if((int)*data
< pix_array_n
)
276 *dptr
= (unsigned char)pix_array
[*data
];
283 unsigned short *dptr
= (unsigned short *)dest
;
285 if((int)*data
< pix_array_n
)
286 *dptr
= (unsigned short)pix_array
[*data
];
293 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
295 if((int)*data
< pix_array_n
) {
296 dptr
->r
= pix_array
[*data
] & 0xFF;
297 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
298 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
306 unsigned long *dptr
= (unsigned long *)dest
;
308 if((int)*data
< pix_array_n
)
309 *dptr
= pix_array
[*data
];
321 unsigned long *wxPalette
::GetXPixArray(WXDisplay
*display
, int *n
)
324 return (unsigned long*) 0;
325 wxList
::compatibility_iterator node
;
327 for (node
= M_PALETTEDATA
->m_palettes
.GetFirst(); node
; node
= node
->GetNext())
329 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
330 if (c
->m_display
== display
)
333 *n
= c
->m_pix_array_n
;
334 return c
->m_pix_array
;
338 /* Not found; call GetXColormap, which will create it, then this again */
339 if (GetXColormap(display
))
340 return GetXPixArray(display
, n
);
342 return (unsigned long*) 0;
345 void wxPalette
::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
349 m_refData
= new wxPaletteRefData
;
351 wxXPalette
*c
= new wxXPalette
;
353 c
->m_pix_array_n
= 0;
354 c
->m_pix_array
= (unsigned long*) NULL
;
355 c
->m_display
= display
;
357 c
->m_destroyable
= dp
;
359 M_PALETTEDATA
->m_palettes
.Append(c
);