#include "wx/palette.h"
-#ifndef WX_PRECOMP
-#endif
-
#include "wx/msw/private.h"
-IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
-
-/*
- * Palette
- *
- */
+// ============================================================================
+// wxPaletteRefData
+// ============================================================================
-wxPaletteRefData::wxPaletteRefData(void)
+class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData
{
- m_hPalette = 0;
-}
+public:
+ wxPaletteRefData() { Init(); }
-wxPaletteRefData::~wxPaletteRefData(void)
-{
- if ( m_hPalette )
- ::DeleteObject((HPALETTE) m_hPalette);
-}
+ wxPaletteRefData(int n,
+ unsigned char *red,
+ unsigned char *green,
+ 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);
+ }
-wxPalette::wxPalette()
-{
-}
+ wxPaletteRefData(const wxPaletteRefData& data)
+ : wxGDIRefData()
+ {
+ Init();
-wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
-{
- Create(n, red, green, blue);
-}
+ const UINT n = data.GetEntries();
+ if ( !n )
+ return;
-wxPalette::~wxPalette(void)
-{
-// FreeResource(true);
-}
+ LOGPALETTE *pPal = Alloc(n);
+ if ( !pPal )
+ return;
-bool wxPalette::FreeResource(bool WXUNUSED(force))
-{
- if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
+ if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) )
+ m_hPalette = ::CreatePalette(pPal);
+
+ free(pPal);
+ }
+
+ virtual ~wxPaletteRefData()
{
- DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
+ if ( m_hPalette )
+ ::DeleteObject(m_hPalette);
}
- return true;
-}
+ virtual bool IsOk() const { return m_hPalette != 0; }
-int wxPalette::GetColoursCount() const
-{
- if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
+ UINT GetEntries() const
{
- return ::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, 0, 0, NULL );
+ return ::GetPaletteEntries(m_hPalette, 0, 0, NULL);
}
- return 0;
-}
+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;
+ }
-bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
-{
- UnRef();
+ void Init() { m_hPalette = 0; }
-#if defined(__WXMICROWIN__)
+ HPALETTE m_hPalette;
- return false;
+ friend class WXDLLIMPEXP_FWD_CORE wxPalette;
+};
-#else
+// ============================================================================
+// wxPalette
+// ============================================================================
- m_refData = new wxPaletteRefData;
+IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
- NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
- (WORD)n * sizeof(PALETTEENTRY));
- if (!npPal)
- return false;
+#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
- npPal->palVersion = 0x300;
- npPal->palNumEntries = (WORD)n;
+bool wxPalette::Create(int n,
+ unsigned char *red,
+ unsigned char *green,
+ unsigned char *blue)
+{
+ m_refData = new wxPaletteRefData(n, red, green, blue);
- int i;
- for (i = 0; i < n; i ++)
- {
- npPal->palPalEntry[i].peRed = red[i];
- npPal->palPalEntry[i].peGreen = green[i];
- npPal->palPalEntry[i].peBlue = blue[i];
- npPal->palPalEntry[i].peFlags = 0;
- }
- M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
- LocalFree((HANDLE)npPal);
- return true;
+ return IsOk();
+}
-#endif
+wxGDIRefData *wxPalette::CreateGDIRefData() const
+{
+ return new wxPaletteRefData;
}
-int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
+wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
+{
+ return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
+}
+
+int wxPalette::GetColoursCount() const
+{
+ return IsOk() ? M_PALETTEDATA->GetEntries() : 0;
+}
+
+int wxPalette::GetPixel(unsigned char red,
+ unsigned char green,
+ unsigned char blue) const
{
-#ifdef __WXMICROWIN__
- return wxNOT_FOUND;
-#else
if ( !m_refData )
return wxNOT_FOUND;
- return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
-#endif
+ return ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette,
+ PALETTERGB(red, green, blue));
}
-bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
+bool wxPalette::GetRGB(int index,
+ unsigned char *red,
+ unsigned char *green,
+ unsigned char *blue) const
{
-#ifdef __WXMICROWIN__
- return false;
-#else
if ( !m_refData )
return false;
return false;
PALETTEENTRY entry;
- if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
- {
- *red = entry.peRed;
- *green = entry.peGreen;
- *blue = entry.peBlue;
- return true;
- }
- else
+ if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) )
return false;
-#endif
+
+ *red = entry.peRed;
+ *green = entry.peGreen;
+ *blue = entry.peBlue;
+
+ return true;
+}
+
+WXHPALETTE wxPalette::GetHPALETTE() const
+{
+ return M_PALETTEDATA ? (WXHPALETTE)M_PALETTEDATA->m_hPalette : 0;
}
void wxPalette::SetHPALETTE(WXHPALETTE pal)
{
- if ( !m_refData )
- m_refData = new wxPaletteRefData;
+ AllocExclusive();
- M_PALETTEDATA->m_hPalette = pal;
+ M_PALETTEDATA->m_hPalette = (HPALETTE)pal;
}
#endif // wxUSE_PALETTE