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 #include "wx/palette.h"
38 #include "wx/window.h"
43 #pragma message disable nosimpint
47 #pragma message enable nosimpint
49 #include "wx/x11/private.h"
51 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
52 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
59 wxXPalette::wxXPalette()
61 m_cmap
= (WXColormap
) 0;
63 m_pix_array
= (unsigned long*) 0;
64 m_display
= (WXDisplay
*) 0;
65 m_destroyable
= false;
68 wxPaletteRefData::wxPaletteRefData()
72 wxPaletteRefData::~wxPaletteRefData()
74 Display
*display
= (Display
*) NULL
;
76 wxList::compatibility_iterator node
, next
;
78 for (node
= m_palettes
.GetFirst(); node
; node
= next
) {
79 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
80 unsigned long *pix_array
= c
->m_pix_array
;
81 Colormap cmap
= (Colormap
) c
->m_cmap
;
82 bool destroyable
= c
->m_destroyable
;
83 int pix_array_n
= c
->m_pix_array_n
;
84 display
= (Display
*) c
->m_display
;
89 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
90 // Be careful not to free '0' pixels...
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
++;
102 XFreeColormap(display
, cmap
);
104 next
= node
->GetNext();
105 m_palettes
.Erase(node
);
110 wxPalette::wxPalette()
114 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
116 Create(n
, red
, green
, blue
);
119 wxPalette::~wxPalette()
123 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
131 m_refData
= new wxPaletteRefData
;
134 Display
* display
= (Display
*) wxGetDisplay();
136 unsigned long *pix_array
;
140 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
142 pix_array
= new unsigned long[n
];
147 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
148 for(int i
= 0; i
< n
; i
++) {
149 xcol
.red
= (unsigned short)red
[i
] << 8;
150 xcol
.green
= (unsigned short)green
[i
] << 8;
151 xcol
.blue
= (unsigned short)blue
[i
] << 8;
152 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
155 wxXPalette
*c
= new wxXPalette
;
157 c
->m_pix_array_n
= pix_array_n
;
158 c
->m_pix_array
= pix_array
;
159 c
->m_cmap
= (WXColormap
) cmap
;
160 c
->m_display
= (WXDisplay
*) display
;
161 c
->m_destroyable
= false;
162 M_PALETTEDATA
->m_palettes
.Append(c
);
167 int wxPalette::GetPixel(unsigned char red
, unsigned char green
, unsigned char blue
) const
176 bool wxPalette::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
181 if (index
< 0 || index
> 255)
188 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
190 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.GetCount() == 0))
191 return wxTheApp
->GetMainColormap(display
);
193 wxList::compatibility_iterator node
= M_PALETTEDATA
->m_palettes
.GetFirst();
194 if (!display
&& node
)
196 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
201 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
202 if (p
->m_display
== display
)
205 node
= node
->GetNext();
208 /* Make a new one: */
209 wxXPalette
*c
= new wxXPalette
;
210 wxXPalette
*first
= (wxXPalette
*)M_PALETTEDATA
->m_palettes
.GetFirst()->GetData();
212 int pix_array_n
= first
->m_pix_array_n
;
214 c
->m_pix_array_n
= pix_array_n
;
215 c
->m_pix_array
= new unsigned long[pix_array_n
];
216 c
->m_display
= display
;
217 c
->m_cmap
= wxTheApp
->GetMainColormap(display
);
218 c
->m_destroyable
= false;
220 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
222 for (i
= 0; i
< pix_array_n
; i
++)
224 xcol
.pixel
= first
->m_pix_array
[i
];
225 XQueryColor((Display
*) first
->m_display
, (Colormap
) first
->m_cmap
, &xcol
);
227 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
230 // wxPalette* nonConstThis = (wxPalette*) this;
232 M_PALETTEDATA
->m_palettes
.Append(c
);
237 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
242 unsigned char *uptr
= (unsigned char *)data
;
244 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
247 if((int)*uptr
< pix_array_n
)
248 *uptr
= (unsigned char)pix_array
[*uptr
];
259 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
260 void *dest
, unsigned int bpp
)
263 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
266 unsigned char *dptr
= (unsigned char *)dest
;
268 if((int)*data
< pix_array_n
)
269 *dptr
= (unsigned char)pix_array
[*data
];
276 unsigned short *dptr
= (unsigned short *)dest
;
278 if((int)*data
< pix_array_n
)
279 *dptr
= (unsigned short)pix_array
[*data
];
286 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
288 if((int)*data
< pix_array_n
) {
289 dptr
->r
= pix_array
[*data
] & 0xFF;
290 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
291 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
299 unsigned long *dptr
= (unsigned long *)dest
;
301 if((int)*data
< pix_array_n
)
302 *dptr
= pix_array
[*data
];
314 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
317 return (unsigned long*) 0;
318 wxList::compatibility_iterator node
;
320 for (node
= M_PALETTEDATA
->m_palettes
.GetFirst(); node
; node
= node
->GetNext())
322 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
323 if (c
->m_display
== display
)
326 *n
= c
->m_pix_array_n
;
327 return c
->m_pix_array
;
331 /* Not found; call GetXColormap, which will create it, then this again */
332 if (GetXColormap(display
))
333 return GetXPixArray(display
, n
);
335 return (unsigned long*) 0;
338 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
342 m_refData
= new wxPaletteRefData
;
344 wxXPalette
*c
= new wxXPalette
;
346 c
->m_pix_array_n
= 0;
347 c
->m_pix_array
= (unsigned long*) NULL
;
348 c
->m_display
= display
;
350 c
->m_destroyable
= dp
;
352 M_PALETTEDATA
->m_palettes
.Append(c
);