1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/palette.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 * When constructed with the default constructor, we start from
15 * the wxApp::GetMainColormap, allocating additional read-only cells
16 * in Create(). The cells are freed on the next call to Create()
17 * or when the destructor is called.
20 /* Wolfram Gloger <u7y22ab@sunmail.lrz-muenchen.de>
21 I have implemented basic colormap support for the X11 versions of
22 wxWidgets, notably wxPalette::Create(). The way I did it is to
23 allocate additional read-only color cells in the default colormap. In
24 general you will get arbitrary pixel values assigned to these new
25 cells and therefore I added a method wxPalette::TransferBitmap()
26 which maps the pixel values 0..n to the real ones obtained with
27 Create(). This is only implemented for the popular case of 8-bit
30 Allocating read-write color cells would involve installing a private
31 X11 colormap for a particular window, and AFAIK this is not
32 recommended; only the window manager should do this... Also, it is
33 not the functionality that wxPalette::Create() aims to provide.
36 // for compilers that support precompilation, includes "wx.h".
37 #include "wx/wxprec.h"
39 #include "wx/palette.h"
44 #include "wx/window.h"
48 #pragma message disable nosimpint
52 #pragma message enable nosimpint
54 #include "wx/x11/private.h"
56 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
57 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
64 wxXPalette::wxXPalette()
66 m_cmap
= (WXColormap
) 0;
68 m_pix_array
= (unsigned long*) 0;
69 m_display
= (WXDisplay
*) 0;
70 m_destroyable
= false;
73 wxPaletteRefData::wxPaletteRefData()
77 wxPaletteRefData::~wxPaletteRefData()
79 Display
*display
= NULL
;
81 wxList::compatibility_iterator node
, next
;
83 for (node
= m_palettes
.GetFirst(); node
; node
= next
) {
84 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
85 unsigned long *pix_array
= c
->m_pix_array
;
86 Colormap cmap
= (Colormap
) c
->m_cmap
;
87 bool destroyable
= c
->m_destroyable
;
88 int pix_array_n
= c
->m_pix_array_n
;
89 display
= (Display
*) c
->m_display
;
94 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
95 // Be careful not to free '0' pixels...
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
++;
107 XFreeColormap(display
, cmap
);
109 next
= node
->GetNext();
110 m_palettes
.Erase(node
);
115 wxPalette::wxPalette()
119 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
121 Create(n
, red
, green
, blue
);
124 wxPalette::~wxPalette()
128 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
136 m_refData
= new wxPaletteRefData
;
139 Display
* display
= (Display
*) wxGetDisplay();
141 unsigned long *pix_array
;
145 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
147 pix_array
= new unsigned long[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
;
160 wxXPalette
*c
= new wxXPalette
;
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
);
172 wxGDIRefData
*wxPalette::CreateGDIRefData() const
174 return new wxPaletteRefData
;
178 wxPalette::CloneGDIRefData(const wxGDIRefData
* WXUNUSED(data
)) const
180 wxFAIL_MSG( wxS("Cloning palettes is not implemented in wxX11.") );
182 return new wxPaletteRefData
;
185 int wxPalette::GetPixel(unsigned char WXUNUSED(red
),
186 unsigned char WXUNUSED(green
),
187 unsigned char WXUNUSED(blue
)) const
196 bool wxPalette::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
201 if (index
< 0 || index
> 255)
208 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
210 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.GetCount() == 0))
211 return wxTheApp
->GetMainColormap(display
);
213 wxList::compatibility_iterator node
= M_PALETTEDATA
->m_palettes
.GetFirst();
214 if (!display
&& node
)
216 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
221 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
222 if (p
->m_display
== display
)
225 node
= node
->GetNext();
228 /* Make a new one: */
229 wxXPalette
*c
= new wxXPalette
;
230 wxXPalette
*first
= (wxXPalette
*)M_PALETTEDATA
->m_palettes
.GetFirst()->GetData();
232 int pix_array_n
= first
->m_pix_array_n
;
234 c
->m_pix_array_n
= pix_array_n
;
235 c
->m_pix_array
= new unsigned long[pix_array_n
];
236 c
->m_display
= display
;
237 c
->m_cmap
= wxTheApp
->GetMainColormap(display
);
238 c
->m_destroyable
= false;
240 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
242 for (i
= 0; i
< pix_array_n
; i
++)
244 xcol
.pixel
= first
->m_pix_array
[i
];
245 XQueryColor((Display
*) first
->m_display
, (Colormap
) first
->m_cmap
, &xcol
);
247 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
250 // wxPalette* nonConstThis = (wxPalette*) this;
252 M_PALETTEDATA
->m_palettes
.Append(c
);
257 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
262 unsigned char *uptr
= (unsigned char *)data
;
264 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
267 if((int)*uptr
< pix_array_n
)
268 *uptr
= (unsigned char)pix_array
[*uptr
];
279 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
280 void *dest
, unsigned int bpp
)
283 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
286 unsigned char *dptr
= (unsigned char *)dest
;
288 if((int)*data
< pix_array_n
)
289 *dptr
= (unsigned char)pix_array
[*data
];
296 unsigned short *dptr
= (unsigned short *)dest
;
298 if((int)*data
< pix_array_n
)
299 *dptr
= (unsigned short)pix_array
[*data
];
306 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
308 if((int)*data
< pix_array_n
) {
309 dptr
->r
= pix_array
[*data
] & 0xFF;
310 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
311 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
319 unsigned long *dptr
= (unsigned long *)dest
;
321 if((int)*data
< pix_array_n
)
322 *dptr
= pix_array
[*data
];
334 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
337 return (unsigned long*) 0;
338 wxList::compatibility_iterator node
;
340 for (node
= M_PALETTEDATA
->m_palettes
.GetFirst(); node
; node
= node
->GetNext())
342 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
343 if (c
->m_display
== display
)
346 *n
= c
->m_pix_array_n
;
347 return c
->m_pix_array
;
351 /* Not found; call GetXColormap, which will create it, then this again */
352 if (GetXColormap(display
))
353 return GetXPixArray(display
, n
);
355 return (unsigned long*) 0;
358 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
362 m_refData
= new wxPaletteRefData
;
364 wxXPalette
*c
= new wxXPalette
;
366 c
->m_pix_array_n
= 0;
367 c
->m_pix_array
= (unsigned long*) NULL
;
368 c
->m_display
= display
;
370 c
->m_destroyable
= dp
;
372 M_PALETTEDATA
->m_palettes
.Append(c
);