#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;
-}
-int wxPalette::GetColoursCount() const
-{
- if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
+ virtual bool IsOk() const { return m_hPalette != 0; }
+
+ UINT GetEntries() const
{
- return ::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, 0, 0, NULL );
+ return ::GetPaletteEntries(m_hPalette, 0, 0, NULL);
}
-
- return 0;
-}
-bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
-{
- UnRef();
+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;
+ }
-#if defined(__WXMICROWIN__)
+ void Init() { m_hPalette = 0; }
- return false;
+ HPALETTE m_hPalette;
-#else
+ friend class WXDLLIMPEXP_FWD_CORE wxPalette;
+};
- m_refData = new wxPaletteRefData;
+// ============================================================================
+// wxPalette
+// ============================================================================
- NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
- (WORD)n * sizeof(PALETTEENTRY));
- if (!npPal)
- return false;
+IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
- npPal->palVersion = 0x300;
- npPal->palNumEntries = (WORD)n;
+#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
- 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;
+bool wxPalette::Create(int n,
+ unsigned char *red,
+ unsigned char *green,
+ unsigned char *blue)
+{
+ m_refData = new wxPaletteRefData(n, red, green, blue);
-#endif
+ return IsOk();
+}
+
+wxGDIRefData *wxPalette::CreateGDIRefData() const
+{
+ return new wxPaletteRefData;
+}
+
+wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
+{
+ return new wxPaletteRefData(*wx_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
+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