X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ad81651f00edc6f489d9b6a0839d316a964fd521..6c99dbd55e7a8ee6d2978bf7ef1b31e59b326768:/src/msw/palette.cpp diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index 891815bf74..a9c9fc8056 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -1,143 +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, + 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 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(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) -{ - UnRef(); - -#ifdef __WXWINE__ - - return (FALSE); - -#else - - 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; - -#endif + 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(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