X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2bda0e173844e8e0f8acf4e8ad8b5c26e5c6fe5d..ee6c519a4b1c8e919c1353b977e6396a160bc4b3:/src/msw/palette.cpp?ds=sidebyside diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index 1eaaa5e074..2547d98851 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -1,137 +1,189 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: palette.cpp +// Name: src/msw/palette.cpp // Purpose: wxPalette // Author: Julian Smart // Modified by: // Created: 04/01/98 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart and Markus Holzem -// Licence: wxWindows license +// Copyright: (c) Julian Smart +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "palette.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#ifndef WX_PRECOMP -#include -#include "wx/setup.h" +#if wxUSE_PALETTE + #include "wx/palette.h" -#endif -#include +#include "wx/msw/private.h" -#include "assert.h" +// ============================================================================ +// wxPaletteRefData +// ============================================================================ + +class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData +{ +public: + wxPaletteRefData() { Init(); } + + 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); + } + + 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 +// ============================================================================ -#if !USE_SHARED_LIBRARIES IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) -#endif -/* - * Palette - * - */ +#define M_PALETTEDATA ((wxPaletteRefData *)m_refData) -wxPaletteRefData::wxPaletteRefData(void) +bool wxPalette::Create(int n, + const unsigned char *red, + const unsigned char *green, + const 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(const 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 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; -bool wxPalette::Create(const int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) -{ - UnRef(); - - m_refData = new wxPaletteRefData; - - NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + - (WORD)n * sizeof(PALETTEENTRY)); - if (!npPal) - return(FALSE); - - npPal->palVersion = 0x300; - npPal->palNumEntries = n; - - 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 ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette, + PALETTERGB(red, green, blue)); } -int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const +bool wxPalette::GetRGB(int index, + unsigned char *red, + unsigned char *green, + unsigned char *blue) const { - if ( !m_refData ) - return FALSE; + if ( !m_refData ) + return false; + + if (index < 0 || index > 255) + return false; + + PALETTEENTRY entry; + if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) ) + return false; + + *red = entry.peRed; + *green = entry.peGreen; + *blue = entry.peBlue; - return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue)); + return true; } -bool wxPalette::GetRGB(const int index, unsigned char *red, unsigned char *green, unsigned char *blue) const +WXHPALETTE wxPalette::GetHPALETTE() const { - 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; + 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