]>
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 #include "wx/motif/private.h"
49 #if !USE_SHARED_LIBRARIES
50 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
51 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
;
78 for (node
= m_palettes
.First(); node
; node
= next
) {
79 wxXPalette
*c
= (wxXPalette
*)node
->Data();
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
;
88 // XFreeColors(display, cmap, pix_array, pix_array_n, 0);
89 // Be careful not to free '0' pixels...
91 for(i
=j
=0; i
<pix_array_n
; i
=j
) {
92 while(j
<pix_array_n
&& pix_array
[j
]!=0) j
++;
93 if(j
> i
) XFreeColors(display
, cmap
, &pix_array
[i
], j
-i
, 0);
94 while(j
<pix_array_n
&& pix_array
[j
]==0) j
++;
100 XFreeColormap(display
, cmap
);
103 m_palettes
.DeleteNode(node
);
108 wxPalette::wxPalette()
112 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
114 Create(n
, red
, green
, blue
);
117 wxPalette::~wxPalette()
121 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
129 m_refData
= new wxPaletteRefData
;
132 Display
* display
= (Display
*) wxGetDisplay();
134 unsigned long *pix_array
;
138 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
140 pix_array
= new unsigned long[n
];
145 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
146 for(int i
= 0; i
< n
; i
++) {
147 xcol
.red
= (unsigned short)red
[i
] << 8;
148 xcol
.green
= (unsigned short)green
[i
] << 8;
149 xcol
.blue
= (unsigned short)blue
[i
] << 8;
150 pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
153 wxXPalette
*c
= new wxXPalette
;
155 c
->m_pix_array_n
= pix_array_n
;
156 c
->m_pix_array
= pix_array
;
157 c
->m_cmap
= (WXColormap
) cmap
;
158 c
->m_display
= (WXDisplay
*) display
;
159 c
->m_destroyable
= FALSE
;
160 M_PALETTEDATA
->m_palettes
.Append(c
);
165 int wxPalette::GetPixel(const unsigned char red
, const unsigned char green
, const unsigned char blue
) const
174 bool wxPalette::GetRGB(int index
, unsigned char *red
, unsigned char *green
, unsigned char *blue
) const
179 if (index
< 0 || index
> 255)
186 WXColormap
wxPalette::GetXColormap(WXDisplay
* display
) const
188 if (!M_PALETTEDATA
|| (M_PALETTEDATA
->m_palettes
.Number() == 0))
189 return wxTheApp
->GetMainColormap(display
);
191 wxNode
* node
= M_PALETTEDATA
->m_palettes
.First();
192 if (!display
&& node
)
194 wxXPalette
* p
= (wxXPalette
*) node
->Data();
199 wxXPalette
* p
= (wxXPalette
*) node
->Data();
200 if (p
->m_display
== display
)
206 /* Make a new one: */
207 wxXPalette
*c
= new wxXPalette
;
208 wxXPalette
*first
= (wxXPalette
*)M_PALETTEDATA
->m_palettes
.First()->Data();
210 int pix_array_n
= first
->m_pix_array_n
;
212 c
->m_pix_array_n
= pix_array_n
;
213 c
->m_pix_array
= new unsigned long[pix_array_n
];
214 c
->m_display
= display
;
215 c
->m_cmap
= wxTheApp
->GetMainColormap(display
);
216 c
->m_destroyable
= FALSE
;
218 xcol
.flags
= DoRed
| DoGreen
| DoBlue
;
220 for (i
= 0; i
< pix_array_n
; i
++)
222 xcol
.pixel
= first
->m_pix_array
[i
];
223 XQueryColor((Display
*) first
->m_display
, (Colormap
) first
->m_cmap
, &xcol
);
225 (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
;
228 // wxPalette* nonConstThis = (wxPalette*) this;
230 M_PALETTEDATA
->m_palettes
.Append(c
);
235 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
)
240 unsigned char *uptr
= (unsigned char *)data
;
242 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
245 if((int)*uptr
< pix_array_n
)
246 *uptr
= (unsigned char)pix_array
[*uptr
];
257 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
,
258 void *dest
, unsigned int bpp
)
261 unsigned long *pix_array
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
);
264 unsigned char *dptr
= (unsigned char *)dest
;
266 if((int)*data
< pix_array_n
)
267 *dptr
= (unsigned char)pix_array
[*data
];
274 unsigned short *dptr
= (unsigned short *)dest
;
276 if((int)*data
< pix_array_n
)
277 *dptr
= (unsigned short)pix_array
[*data
];
284 struct rgb24
{ unsigned char r
, g
, b
; } *dptr
= (struct rgb24
*)dest
;
286 if((int)*data
< pix_array_n
) {
287 dptr
->r
= pix_array
[*data
] & 0xFF;
288 dptr
->g
= (pix_array
[*data
] >> 8) & 0xFF;
289 dptr
->b
= (pix_array
[*data
] >> 16) & 0xFF;
297 unsigned long *dptr
= (unsigned long *)dest
;
299 if((int)*data
< pix_array_n
)
300 *dptr
= pix_array
[*data
];
312 unsigned long *wxPalette::GetXPixArray(WXDisplay
*display
, int *n
)
315 return (unsigned long*) 0;
318 for (node
= M_PALETTEDATA
->m_palettes
.First(); node
; node
= node
->Next())
320 wxXPalette
*c
= (wxXPalette
*)node
->Data();
321 if (c
->m_display
== display
)
324 *n
= c
->m_pix_array_n
;
325 return c
->m_pix_array
;
329 /* Not found; call GetXColormap, which will create it, then this again */
330 if (GetXColormap(display
))
331 return GetXPixArray(display
, n
);
333 return (unsigned long*) 0;
336 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
)
340 m_refData
= new wxPaletteRefData
;
342 wxXPalette
*c
= new wxXPalette
;
344 c
->m_pix_array_n
= 0;
345 c
->m_pix_array
= (unsigned long*) NULL
;
346 c
->m_display
= display
;
348 c
->m_destroyable
= dp
;
350 M_PALETTEDATA
->m_palettes
.Append(c
);