]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/palette.cpp
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 #if !USE_SHARED_LIBRARIES
56 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
57 IMPLEMENT_DYNAMIC_CLASS(wxXPalette
, wxObject
)
65 wxXPalette::wxXPalette()
67 m_cmap
= (WXColormap
) 0;
69 m_pix_array
= (unsigned long*) 0;
70 m_display
= (WXDisplay
*) 0;
71 m_destroyable
= FALSE
;
74 wxPaletteRefData::wxPaletteRefData()
78 wxPaletteRefData::~wxPaletteRefData()
80 Display
*display
= (Display
*) NULL
;
84 for (node
= m_palettes
.First(); node
; node
= next
) {
85 wxXPalette
*c
= (wxXPalette
*)node
->Data();
86 unsigned long *pix_array
= c
->m_pix_array
;
87 Colormap cmap
= (Colormap
) c
->m_cmap
;
88 bool destroyable
= c
->m_destroyable
;
89 int pix_array_n
= c
->m_pix_array_n
;
90 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
++;
106 XFreeColormap(display
, cmap
);
109 m_palettes
.DeleteNode(node
);
114 wxPalette::wxPalette()
118 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
120 Create(n
, red
, green
, blue
);
123 wxPalette::~wxPalette()
127 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
135 m_refData
= new wxPaletteRefData
;
138 Display
* display
= (Display
*) wxGetDisplay();
140 unsigned long *pix_array
;
144 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
146 pix_array
= new unsigned long[n
];
151 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
152 for(int i
= 0; i
< n
; i
++) {
153 xcol
.red
= (unsigned short)red
[i
] << 8;
154 xcol
.green
= (unsigned short)green
[i
] << 8;
155 xcol
.blue
= (unsigned short)blue
[i
] << 8;
156 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
159 wxXPalette
*c
= new wxXPalette
;
161 c
->m_pix_array_n
= pix_array_n
;
162 c
->m_pix_array
= pix_array
;
163 c
->m_cmap
= (WXColormap
) cmap
;
164 c
->m_display
= (WXDisplay
*) display
;
165 c
->m_destroyable
= FALSE
;
166 M_PALETTEDATA
->m_palettes
.Append(c
);
171 int wxPalette::GetPixel(const unsigned char red
, const unsigned char green
, const unsigned char blue
) const
180 bool wxPalette::GetRGB(int index
, unsigned char *WXUNUSED(red
), unsigned char *WXUNUSED(green
), unsigned char *WXUNUSED(blue
)) const
185 if (index
< 0 || index
> 255)
192 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
194 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.Number() == 0))
195 return wxTheApp
->GetMainColormap(display
);
197 wxNode
* node
= M_PALETTEDATA
->m_palettes
.First();
198 if (!display
&& node
)
200 wxXPalette
* p
= (wxXPalette
*) node
->Data();
205 wxXPalette
* p
= (wxXPalette
*) node
->Data();
206 if (p
->m_display
== display
)
212 /* Make a new one: */
213 wxXPalette
*c
= new wxXPalette
;
214 wxXPalette
*first
= (wxXPalette
*)M_PALETTEDATA
->m_palettes
.First()->Data();
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
, (Colormap
) first
->m_cmap
, &xcol
);
231 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
234 // wxPalette* nonConstThis = (wxPalette*) this;
236 M_PALETTEDATA
->m_palettes
.Append(c
);
241 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
246 unsigned char *uptr
= (unsigned char *)data
;
248 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
251 if((int)*uptr
< pix_array_n
)
252 *uptr
= (unsigned char)pix_array
[*uptr
];
263 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
264 void *dest
, unsigned int bpp
)
267 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
270 unsigned char *dptr
= (unsigned char *)dest
;
272 if((int)*data
< pix_array_n
)
273 *dptr
= (unsigned char)pix_array
[*data
];
280 unsigned short *dptr
= (unsigned short *)dest
;
282 if((int)*data
< pix_array_n
)
283 *dptr
= (unsigned short)pix_array
[*data
];
290 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
292 if((int)*data
< pix_array_n
) {
293 dptr
->r
= pix_array
[*data
] & 0xFF;
294 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
295 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
303 unsigned long *dptr
= (unsigned long *)dest
;
305 if((int)*data
< pix_array_n
)
306 *dptr
= pix_array
[*data
];
318 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
321 return (unsigned long*) 0;
324 for (node
= M_PALETTEDATA
->m_palettes
.First(); node
; node
= node
->Next())
326 wxXPalette
*c
= (wxXPalette
*)node
->Data();
327 if (c
->m_display
== display
)
330 *n
= c
->m_pix_array_n
;
331 return c
->m_pix_array
;
335 /* Not found; call GetXColormap, which will create it, then this again */
336 if (GetXColormap(display
))
337 return GetXPixArray(display
, n
);
339 return (unsigned long*) 0;
342 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
346 m_refData
= new wxPaletteRefData
;
348 wxXPalette
*c
= new wxXPalette
;
350 c
->m_pix_array_n
= 0;
351 c
->m_pix_array
= (unsigned long*) NULL
;
352 c
->m_display
= display
;
354 c
->m_destroyable
= dp
;
356 M_PALETTEDATA
->m_palettes
.Append(c
);