X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/559a723c1dcf8b3929ab3122fb04429d4abdda8e..bb276f53fca627231b1e1f50550a314a5687650b:/src/msw/palette.cpp diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index aeb874150e..a9c9fc8056 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -20,104 +20,143 @@ #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(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, + unsigned char *red, + unsigned char *green, + unsigned char *blue) +{ + m_refData = new wxPaletteRefData(n, red, green, blue); + + return IsOk(); +} + +wxGDIRefData *wxPalette::CreateGDIRefData() const +{ + return new wxPaletteRefData; +} + +wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const +{ + return new wxPaletteRefData(*static_cast(data)); } -int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const +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; @@ -125,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