1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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"
41 #include "wx/window.h"
46 #pragma message disable nosimpint
50 #pragma message enable nosimpint
52 #include "wx/motif/private.h"
54 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
55 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
62 wxXPalette::wxXPalette()
64 m_cmap
= (WXColormap
) 0;
66 m_pix_array
= (unsigned long*) 0;
67 m_display
= (WXDisplay
*) 0;
68 m_destroyable
= false;
71 wxPaletteRefData::wxPaletteRefData()
75 wxPaletteRefData::~wxPaletteRefData()
77 Display
*display
= (Display
*) NULL
;
79 wxList::compatibility_iterator node
, next
;
81 for (node
= m_palettes
.GetFirst(); node
; node
= next
) {
82 wxXPalette
*c
= (wxXPalette
*)node
->GetData();
83 unsigned long *pix_array
= c
->m_pix_array
;
84 Colormap cmap
= (Colormap
) c
->m_cmap
;
85 bool destroyable
= c
->m_destroyable
;
86 int pix_array_n
= c
->m_pix_array_n
;
87 display
= (Display
*) c
->m_display
;
91 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
92 // Be careful not to free '0' pixels...
94 for(i
=j
=0; i
<pix_array_n
; i
=j
) {
95 while(j
<pix_array_n
&& pix_array
[j
]!=0) j
++;
96 if(j
> i
) XFreeColors(display
, cmap
, &pix_array
[i
], j
-i
, 0);
97 while(j
<pix_array_n
&& pix_array
[j
]==0) j
++;
103 XFreeColormap(display
, cmap
);
105 next
= node
->GetNext();
106 m_palettes
.Erase(node
);
111 wxPalette::wxPalette()
115 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
117 Create(n
, red
, green
, blue
);
120 wxPalette::~wxPalette()
124 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
132 m_refData
= new wxPaletteRefData
;
135 Display
* display
= (Display
*) wxGetDisplay();
137 unsigned long *pix_array
;
141 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
143 pix_array
= new unsigned long[n
];
148 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
149 for(int i
= 0; i
< n
; i
++) {
150 xcol
.red
= (unsigned short)(red
[i
] << 8);
151 xcol
.green
= (unsigned short)(green
[i
] << 8);
152 xcol
.blue
= (unsigned short)(blue
[i
] << 8);
153 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
156 wxXPalette
*c
= new wxXPalette
;
158 c
->m_pix_array_n
= pix_array_n
;
159 c
->m_pix_array
= pix_array
;
160 c
->m_cmap
= (WXColormap
) cmap
;
161 c
->m_display
= (WXDisplay
*) display
;
162 c
->m_destroyable
= false;
163 M_PALETTEDATA
->m_palettes
.Append(c
);
168 int wxPalette::GetPixel(unsigned char WXUNUSED(red
),
169 unsigned char WXUNUSED(green
),
170 unsigned char WXUNUSED(blue
)) const
179 bool wxPalette::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
184 if (index
< 0 || index
> 255)
191 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
193 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.GetCount() == 0))
194 return wxTheApp
->GetMainColormap(display
);
196 wxList::compatibility_iterator node
= M_PALETTEDATA
->m_palettes
.GetFirst();
197 if (!display
&& node
)
199 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
204 wxXPalette
* p
= (wxXPalette
*) node
->GetData();
205 if (p
->m_display
== display
)
208 node
= node
->GetNext();
211 /* Make a new one: */
212 wxXPalette
*c
= new wxXPalette
;
214 (wxXPalette
*)M_PALETTEDATA
->m_palettes
.GetFirst()->GetData();
216 int pix_array_n
= first
->m_pix_array_n
;
218 c
->m_pix_array_n
= pix_array_n
;
219 c
->m_pix_array
= new unsigned long[pix_array_n
];
220 c
->m_display
= display
;
221 c
->m_cmap
= wxTheApp
->GetMainColormap(display
);
222 c
->m_destroyable
= false;
224 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
226 for (i
= 0; i
< pix_array_n
; i
++)
228 xcol
.pixel
= first
->m_pix_array
[i
];
229 XQueryColor((Display
*) first
->m_display
,
230 (Colormap
) first
->m_cmap
, &xcol
);
232 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0)
236 // wxPalette* nonConstThis = (wxPalette*) this;
238 M_PALETTEDATA
->m_palettes
.Append(c
);
243 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
248 unsigned char *uptr
= (unsigned char *)data
;
250 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
253 if((int)*uptr
< pix_array_n
)
254 *uptr
= (unsigned char)pix_array
[*uptr
];
265 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
266 void *dest
, unsigned int bpp
)
269 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
272 unsigned char *dptr
= (unsigned char *)dest
;
274 if((int)*data
< pix_array_n
)
275 *dptr
= (unsigned char)pix_array
[*data
];
282 unsigned short *dptr
= (unsigned short *)dest
;
284 if((int)*data
< pix_array_n
)
285 *dptr
= (unsigned short)pix_array
[*data
];
292 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
294 if((int)*data
< pix_array_n
) {
295 dptr
->r
= (unsigned char)(pix_array
[*data
] & 0xFF);
296 dptr
->g
= (unsigned char)((pix_array
[*data
] >> 8) & 0xFF);
297 dptr
->b
= (unsigned char)((pix_array
[*data
] >> 16) & 0xFF);
305 unsigned long *dptr
= (unsigned long *)dest
;
307 if((int)*data
< pix_array_n
)
308 *dptr
= pix_array
[*data
];
320 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
323 return (unsigned long*) 0;
324 wxList::compatibility_iterator node
;
326 for (node
= M_PALETTEDATA
->m_palettes
.GetFirst(); node
;
327 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
);