+WXColormap wxPalette::GetXColormap(WXDisplay* display) const
+{
+ if (!M_PALETTEDATA || (M_PALETTEDATA->m_palettes.Number() == 0))
+ return wxTheApp->GetMainColormap(display);
+
+ wxNode* node = M_PALETTEDATA->m_palettes.First();
+ if (!display && node)
+ {
+ wxXPalette* p = (wxXPalette*) node->Data();
+ return p->m_cmap;
+ }
+ while (node)
+ {
+ wxXPalette* p = (wxXPalette*) node->Data();
+ if (p->m_display == display)
+ return p->m_cmap;
+
+ node = node->Next();
+ }
+
+ /* Make a new one: */
+ wxXPalette *c = new wxXPalette;
+ wxXPalette *first = (wxXPalette *)M_PALETTEDATA->m_palettes.First()->Data();
+ XColor xcol;
+ int pix_array_n = first->m_pix_array_n;
+
+ c->m_pix_array_n = pix_array_n;
+ c->m_pix_array = new unsigned long[pix_array_n];
+ c->m_display = display;
+ c->m_cmap = wxTheApp->GetMainColormap(display);
+ c->m_destroyable = FALSE;
+
+ xcol.flags = DoRed | DoGreen | DoBlue;
+ int i;
+ for (i = 0; i < pix_array_n; i++)
+ {
+ xcol.pixel = first->m_pix_array[i];
+ XQueryColor((Display*) first->m_display, (Colormap) first->m_cmap, &xcol);
+ c->m_pix_array[i] =
+ (XAllocColor((Display*) display, (Colormap) c->m_cmap, &xcol) == 0) ? 0 : xcol.pixel;
+ }
+
+ // wxPalette* nonConstThis = (wxPalette*) this;
+
+ M_PALETTEDATA->m_palettes.Append(c);
+
+ return c->m_cmap;
+}
+
+bool wxPalette::TransferBitmap(void *data, int depth, int size)
+{
+ switch(depth) {
+ case 8:
+ {
+ unsigned char *uptr = (unsigned char *)data;
+ int pix_array_n;
+ unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
+ while(size-- > 0)
+ {
+ if((int)*uptr < pix_array_n)
+ *uptr = (unsigned char)pix_array[*uptr];
+ uptr++;
+ }
+
+ return TRUE;
+ }
+ default:
+ return FALSE;
+ }
+}
+
+bool wxPalette::TransferBitmap8(unsigned char *data, unsigned long sz,
+ void *dest, unsigned int bpp)
+{
+ int pix_array_n;
+ unsigned long *pix_array = GetXPixArray((Display*) wxGetDisplay(), &pix_array_n);
+ switch(bpp) {
+ case 8: {
+ unsigned char *dptr = (unsigned char *)dest;
+ while(sz-- > 0) {
+ if((int)*data < pix_array_n)
+ *dptr = (unsigned char)pix_array[*data];
+ data++;
+ dptr++;
+ }
+ break;
+ }
+ case 16: {
+ unsigned short *dptr = (unsigned short *)dest;
+ while(sz-- > 0) {
+ if((int)*data < pix_array_n)
+ *dptr = (unsigned short)pix_array[*data];
+ data++;
+ dptr++;
+ }
+ break;
+ }
+ case 24: {
+ struct rgb24 { unsigned char r, g, b; } *dptr = (struct rgb24 *)dest;
+ while(sz-- > 0) {
+ if((int)*data < pix_array_n) {
+ dptr->r = pix_array[*data] & 0xFF;
+ dptr->g = (pix_array[*data] >> 8) & 0xFF;
+ dptr->b = (pix_array[*data] >> 16) & 0xFF;
+ }
+ data++;
+ dptr++;
+ }
+ break;
+ }
+ case 32: {
+ unsigned long *dptr = (unsigned long *)dest;
+ while(sz-- > 0) {
+ if((int)*data < pix_array_n)
+ *dptr = pix_array[*data];
+ data++;
+ dptr++;
+ }
+ break;
+ }
+ default:
+ return FALSE;
+ }
+ return TRUE;
+}
+
+unsigned long *wxPalette::GetXPixArray(WXDisplay *display, int *n)
+{
+ if (!M_PALETTEDATA)
+ return (unsigned long*) 0;
+ wxNode *node;
+
+ for (node = M_PALETTEDATA->m_palettes.First(); node; node = node->Next())
+ {
+ wxXPalette *c = (wxXPalette *)node->Data();
+ if (c->m_display == display)
+ {
+ if (n)
+ *n = c->m_pix_array_n;
+ return c->m_pix_array;
+ }
+ }
+
+ /* Not found; call GetXColormap, which will create it, then this again */
+ if (GetXColormap(display))
+ return GetXPixArray(display, n);
+ else
+ return (unsigned long*) 0;
+}
+
+void wxPalette::PutXColormap(WXDisplay* display, WXColormap cm, bool dp)
+{
+ UnRef();
+
+ m_refData = new wxPaletteRefData;
+
+ wxXPalette *c = new wxXPalette;
+
+ c->m_pix_array_n = 0;
+ c->m_pix_array = (unsigned long*) NULL;
+ c->m_display = display;
+ c->m_cmap = cm;
+ c->m_destroyable = dp;
+
+ M_PALETTEDATA->m_palettes.Append(c);
+}