X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/04ef50df3a0fa3c343800c554e609f98fc7575cc..12bb29f5432174ecbd65549bda832d70d34a98ae:/src/msw/palette.cpp diff --git a/src/msw/palette.cpp b/src/msw/palette.cpp index 7d88c7fe72..2547d98851 100644 --- a/src/msw/palette.cpp +++ b/src/msw/palette.cpp @@ -1,151 +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" + +// ============================================================================ +// wxPaletteRefData +// ============================================================================ -#include "assert.h" +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 +// ============================================================================ IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) -/* - * 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(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(__WXWINE__) || defined(__WXMICROWIN__) + if ( !m_refData ) + return false; - return (FALSE); + if (index < 0 || index > 255) + return false; -#else + PALETTEENTRY entry; + if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) ) + return false; - m_refData = new wxPaletteRefData; + *red = entry.peRed; + *green = entry.peGreen; + *blue = entry.peBlue; - 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 -} - -int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const -{ -#ifdef __WXMICROWIN__ - return FALSE; -#else - if ( !m_refData ) - return FALSE; - - 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