X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/88ef3a57a47f840adea9d38bc11cb30689a0838a..88932ec82d048d006cdc81bd309f5e59aee799ac:/src/msw/palette.cpp diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index c958a25a2a..2547d98851 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -18,105 +18,145 @@ #if wxUSE_PALETTE -#ifndef WX_PRECOMP - #include "wx/palette.h" -#endif +#include "wx/palette.h" #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, + 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); + } -wxPalette::wxPalette(void) -{ -} + 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; -} -bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) -{ - UnRef(); + virtual bool IsOk() const { return m_hPalette != 0; } -#if defined(__WXMICROWIN__) + UINT GetEntries() const + { + return ::GetPaletteEntries(m_hPalette, 0, 0, NULL); + } - return false; +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; + } -#else + void Init() { m_hPalette = 0; } - m_refData = new wxPaletteRefData; + HPALETTE m_hPalette; - NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + - (WORD)n * sizeof(PALETTEENTRY)); - if (!npPal) - return false; + friend class WXDLLIMPEXP_FWD_CORE wxPalette; +}; - npPal->palVersion = 0x300; - npPal->palNumEntries = (WORD)n; +// ============================================================================ +// wxPalette +// ============================================================================ - 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; +IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) -#endif +#define M_PALETTEDATA ((wxPaletteRefData *)m_refData) + +bool wxPalette::Create(int n, + const unsigned char *red, + const unsigned char *green, + const unsigned char *blue) +{ + m_refData = new wxPaletteRefData(n, red, green, blue); + + return IsOk(); } -int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const +wxGDIRefData *wxPalette::CreateGDIRefData() const +{ + return new wxPaletteRefData; +} + +wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const +{ + return new wxPaletteRefData(*static_cast(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; @@ -124,24 +164,26 @@ bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsi 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