+class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData
+{
+public:
+ wxPaletteRefData() { Init(); }
+
+ wxPaletteRefData(int n,
+ const unsigned char *red,
+ const unsigned char *green,
+ const unsigned char *blue)
+ {
+ Init();
+
+ LOGPALETTE *pPal = Alloc(n);
+ if ( !pPal )
+ return;
+
+ for ( int i = 0; i < n; i++ )
+ {
+ pPal->palPalEntry[i].peRed = red[i];
+ pPal->palPalEntry[i].peGreen = green[i];
+ pPal->palPalEntry[i].peBlue = blue[i];
+ pPal->palPalEntry[i].peFlags = 0;
+ }
+
+ m_hPalette = ::CreatePalette(pPal);
+ free(pPal);
+ }
+
+ wxPaletteRefData(const wxPaletteRefData& data)
+ : wxGDIRefData()
+ {
+ Init();
+
+ const UINT n = data.GetEntries();
+ if ( !n )
+ return;
+
+ LOGPALETTE *pPal = Alloc(n);
+ if ( !pPal )
+ return;
+
+ if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) )
+ m_hPalette = ::CreatePalette(pPal);
+
+ free(pPal);
+ }
+
+ virtual ~wxPaletteRefData()
+ {
+ if ( m_hPalette )
+ ::DeleteObject(m_hPalette);
+ }
+
+ virtual bool IsOk() const { return m_hPalette != 0; }
+
+ UINT GetEntries() const
+ {
+ return ::GetPaletteEntries(m_hPalette, 0, 0, NULL);
+ }
+
+private:
+ // caller must free() the pointer
+ static LOGPALETTE *Alloc(UINT numEntries)
+ {
+ LOGPALETTE *pPal = (LOGPALETTE *)
+ malloc(sizeof(LOGPALETTE) + numEntries*sizeof(PALETTEENTRY));
+ if ( pPal )
+ {
+ pPal->palVersion = 0x300;
+ pPal->palNumEntries = numEntries;
+ }
+
+ return pPal;
+ }
+
+ void Init() { m_hPalette = 0; }
+
+ HPALETTE m_hPalette;
+
+ friend class WXDLLIMPEXP_FWD_CORE wxPalette;
+};
+
+// ============================================================================
+// wxPalette
+// ============================================================================