X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/078cf5cb756c4f4113d7acf04ebd6a2535ef75b3..f559d1a23c71252b698badf63e4685a3abfa1712:/src/msw/palette.cpp diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index 4a73bb28b2..a9c9fc8056 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: palette.cpp +// Name: src/msw/palette.cpp // Purpose: wxPalette // Author: Julian Smart // Modified by: @@ -9,10 +9,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "palette.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -22,130 +18,172 @@ #if wxUSE_PALETTE -#ifndef WX_PRECOMP - #include "wx/palette.h" -#endif +#include "wx/palette.h" #include "wx/msw/private.h" +// ============================================================================ +// wxPaletteRefData +// ============================================================================ + +class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData +{ +public: + wxPaletteRefData() { Init(); } + + 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); + } + + wxPaletteRefData(const wxPaletteRefData& data) + : wxGDIRefData() + { + Init(); + + const UINT n = data.GetEntries(); + if ( !n ) + return; + + LOGPALETTE *pPal = Alloc(n); + if ( !pPal ) + return; + + if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) ) + m_hPalette = ::CreatePalette(pPal); + + free(pPal); + } + + virtual ~wxPaletteRefData() + { + if ( m_hPalette ) + ::DeleteObject(m_hPalette); + } + + virtual bool IsOk() const { return m_hPalette != 0; } + + UINT GetEntries() const + { + return ::GetPaletteEntries(m_hPalette, 0, 0, NULL); + } + +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; + } + + void Init() { m_hPalette = 0; } + + HPALETTE m_hPalette; + + friend class WXDLLIMPEXP_FWD_CORE wxPalette; +}; + +// ============================================================================ +// wxPalette +// ============================================================================ + IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) -/* - * Palette - * - */ +#define M_PALETTEDATA ((wxPaletteRefData *)m_refData) -wxPaletteRefData::wxPaletteRefData(void) +bool wxPalette::Create(int n, + unsigned char *red, + unsigned char *green, + unsigned char *blue) { - m_hPalette = 0; -} + m_refData = new wxPaletteRefData(n, red, green, blue); -wxPaletteRefData::~wxPaletteRefData(void) -{ - if ( m_hPalette ) - ::DeleteObject((HPALETTE) m_hPalette); + return IsOk(); } -wxPalette::wxPalette(void) +wxGDIRefData *wxPalette::CreateGDIRefData() const { + return new wxPaletteRefData; } -wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) +wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const { - Create(n, red, green, blue); + return new wxPaletteRefData(*static_cast(data)); } -wxPalette::~wxPalette(void) +int wxPalette::GetColoursCount() const { -// FreeResource(true); + return IsOk() ? M_PALETTEDATA->GetEntries() : 0; } -bool wxPalette::FreeResource(bool WXUNUSED(force)) +int wxPalette::GetPixel(unsigned char red, + unsigned char green, + unsigned char blue) const { - if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette) - { - DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette); - } - return true; + if ( !m_refData ) + return wxNOT_FOUND; + + return ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette, + PALETTERGB(red, green, blue)); } -bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) +bool wxPalette::GetRGB(int index, + unsigned char *red, + unsigned char *green, + unsigned char *blue) const { - UnRef(); - -#if defined(__WXMICROWIN__) - - return false; - -#else - - m_refData = new wxPaletteRefData; + if ( !m_refData ) + return false; - NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + - (WORD)n * sizeof(PALETTEENTRY)); - if (!npPal) - return false; + if (index < 0 || index > 255) + return false; - npPal->palVersion = 0x300; - npPal->palNumEntries = n; + PALETTEENTRY entry; + if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) ) + return false; - 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; + *red = entry.peRed; + *green = entry.peGreen; + *blue = entry.peBlue; -#endif -} - -int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const -{ -#ifdef __WXMICROWIN__ - return 0; -#else - if ( !m_refData ) - return 0; - - return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue)); -#endif + return true; } -bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const +WXHPALETTE wxPalette::GetHPALETTE() const { -#ifdef __WXMICROWIN__ - return false; -#else - if ( !m_refData ) - return false; - - if (index < 0 || index > 255) - 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 - return false; -#endif + 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 -