X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f4322df68b099e5e983acc0a3dad4ad5841b57b5..4e15d1caa03346c126015019c1fdf093033ef40b:/src/msw/palette.cpp?ds=sidebyside diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index 989ff48e22..2547d98851 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -20,115 +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, + 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() -{ -} + 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, + const unsigned char *red, + const unsigned char *green, + const 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(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; @@ -136,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