]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/palette.cpp
450c691097237a11ce36ee74c61a7720b624587f
   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() 
  75     Display 
*display 
= (Display
*) NULL
; 
  79     for (node 
= m_palettes
.First(); node
; node 
= next
) { 
  80         wxXPalette 
*c 
= (wxXPalette 
*)node
->Data(); 
  81         unsigned long *pix_array 
= c
->m_pix_array
; 
  82         Colormap cmap 
= (Colormap
) c
->m_cmap
; 
  83         bool destroyable 
= c
->m_destroyable
; 
  84         int pix_array_n 
= c
->m_pix_array_n
; 
  85         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
++; 
 101             XFreeColormap(display
, cmap
); 
 104         m_palettes
.DeleteNode(node
); 
 109 wxPalette::wxPalette() 
 113 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
) 
 115     Create(n
, red
, green
, blue
); 
 118 wxPalette::~wxPalette() 
 122 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
) 
 130     m_refData 
= new wxPaletteRefData
; 
 133     Display
* display 
= (Display
*) wxGetDisplay(); 
 135     unsigned long *pix_array
; 
 139     cmap 
= (Colormap
) wxTheApp
->GetMainColormap(display
); 
 141     pix_array 
= new unsigned long[n
]; 
 146     xcol
.flags 
= DoRed 
| DoGreen 
| DoBlue
; 
 147     for(int i 
= 0; i 
< n
; i
++) { 
 148         xcol
.red 
= (unsigned short)red
[i
] << 8; 
 149         xcol
.green 
= (unsigned short)green
[i
] << 8; 
 150         xcol
.blue 
= (unsigned short)blue
[i
] << 8; 
 151         pix_array
[i
] = (XAllocColor(display
, cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
; 
 154     wxXPalette 
*c 
= new wxXPalette
; 
 156     c
->m_pix_array_n 
= pix_array_n
; 
 157     c
->m_pix_array 
= pix_array
; 
 158     c
->m_cmap 
= (WXColormap
) cmap
; 
 159     c
->m_display 
= (WXDisplay
*) display
; 
 160     c
->m_destroyable 
= FALSE
; 
 161     M_PALETTEDATA
->m_palettes
.Append(c
); 
 166 int wxPalette::GetPixel(const unsigned char red
, const unsigned char green
, const unsigned char blue
) const 
 175 bool wxPalette::GetRGB(int index
, unsigned char *red
, unsigned char *green
, unsigned char *blue
) const 
 180     if (index 
< 0 || index 
> 255) 
 187 WXColormap 
wxPalette::GetXColormap(WXDisplay
* display
) const 
 189     if (!M_PALETTEDATA 
|| (M_PALETTEDATA
->m_palettes
.Number() == 0)) 
 190         return wxTheApp
->GetMainColormap(display
); 
 192     wxNode
* node 
= M_PALETTEDATA
->m_palettes
.First(); 
 193     if (!display 
&& node
) 
 195         wxXPalette
* p 
= (wxXPalette
*) node
->Data(); 
 200         wxXPalette
* p 
= (wxXPalette
*) node
->Data(); 
 201         if (p
->m_display 
== display
) 
 207     /* Make a new one: */ 
 208     wxXPalette 
*c 
= new wxXPalette
; 
 209     wxXPalette 
*first 
= (wxXPalette 
*)M_PALETTEDATA
->m_palettes
.First()->Data(); 
 211     int pix_array_n 
= first
->m_pix_array_n
; 
 213     c
->m_pix_array_n 
= pix_array_n
; 
 214     c
->m_pix_array 
= new unsigned long[pix_array_n
]; 
 215     c
->m_display 
= display
; 
 216     c
->m_cmap 
= wxTheApp
->GetMainColormap(display
); 
 217     c
->m_destroyable 
= FALSE
; 
 219     xcol
.flags 
= DoRed 
| DoGreen 
| DoBlue
; 
 221     for (i 
= 0; i 
< pix_array_n
; i
++) 
 223         xcol
.pixel 
= first
->m_pix_array
[i
]; 
 224         XQueryColor((Display
*) first
->m_display
, (Colormap
) first
->m_cmap
, &xcol
); 
 226             (XAllocColor((Display
*) display
, (Colormap
) c
->m_cmap
, &xcol
) == 0) ? 0 : xcol
.pixel
; 
 229     //    wxPalette* nonConstThis = (wxPalette*) this; 
 231     M_PALETTEDATA
->m_palettes
.Append(c
); 
 236 bool wxPalette::TransferBitmap(void *data
, int depth
, int size
) 
 241             unsigned char *uptr 
= (unsigned char *)data
; 
 243             unsigned long *pix_array 
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
); 
 246                 if((int)*uptr 
< pix_array_n
) 
 247                     *uptr 
= (unsigned char)pix_array
[*uptr
]; 
 258 bool wxPalette::TransferBitmap8(unsigned char *data
, unsigned long sz
, 
 259                                 void *dest
, unsigned int bpp
) 
 262     unsigned long *pix_array 
= GetXPixArray((Display
*) wxGetDisplay(), &pix_array_n
); 
 265         unsigned char *dptr 
= (unsigned char *)dest
; 
 267             if((int)*data 
< pix_array_n
) 
 268                 *dptr 
= (unsigned char)pix_array
[*data
]; 
 275         unsigned short *dptr 
= (unsigned short *)dest
; 
 277             if((int)*data 
< pix_array_n
) 
 278                 *dptr 
= (unsigned short)pix_array
[*data
]; 
 285         struct rgb24 
{ unsigned char r
, g
, b
; } *dptr 
= (struct rgb24 
*)dest
; 
 287             if((int)*data 
< pix_array_n
) { 
 288                 dptr
->r 
= pix_array
[*data
] & 0xFF; 
 289                 dptr
->g 
= (pix_array
[*data
] >> 8) & 0xFF; 
 290                 dptr
->b 
= (pix_array
[*data
] >> 16) & 0xFF; 
 298         unsigned long *dptr 
= (unsigned long *)dest
; 
 300             if((int)*data 
< pix_array_n
) 
 301                 *dptr 
= pix_array
[*data
]; 
 313 unsigned long *wxPalette::GetXPixArray(WXDisplay 
*display
, int *n
) 
 316         return (unsigned long*) 0; 
 319     for (node 
= M_PALETTEDATA
->m_palettes
.First(); node
; node 
= node
->Next()) 
 321         wxXPalette 
*c 
= (wxXPalette 
*)node
->Data(); 
 322         if (c
->m_display 
== display
) 
 325                 *n 
= c
->m_pix_array_n
; 
 326             return c
->m_pix_array
; 
 330     /* Not found; call GetXColormap, which will create it, then this again */ 
 331     if (GetXColormap(display
)) 
 332         return GetXPixArray(display
, n
); 
 334         return (unsigned long*) 0; 
 337 void wxPalette::PutXColormap(WXDisplay
* display
, WXColormap cm
, bool dp
) 
 341     m_refData 
= new wxPaletteRefData
; 
 343     wxXPalette 
*c 
= new wxXPalette
; 
 345     c
->m_pix_array_n 
= 0; 
 346     c
->m_pix_array 
= (unsigned long*) NULL
; 
 347     c
->m_display 
= display
; 
 349     c
->m_destroyable 
= dp
; 
 351     M_PALETTEDATA
->m_palettes
.Append(c
);