1/////////////////////////////////////////////////////////////////////////////
2// Name:        src/msw/palette.cpp
3// Purpose:     wxPalette
4// Author:      Julian Smart
5// Modified by:
6// Created:     04/01/98
7// RCS-ID:      $Id: palette.cpp 67095 2011-03-01 00:02:52Z VZ $
8// Copyright:   (c) Julian Smart
9// Licence:     wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16    #pragma hdrstop
17#endif
18
19#if wxUSE_PALETTE
20
21#include "wx/palette.h"
22
23#ifndef WX_PRECOMP
24#endif
25
26#include "wx/msw/private.h"
27
28IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
29
30/*
31 * Palette
32 *
33 */
34
35wxPaletteRefData::wxPaletteRefData(void)
36{
37    m_hPalette = 0;
38}
39
40wxPaletteRefData::~wxPaletteRefData(void)
41{
42    if ( m_hPalette )
43        ::DeleteObject((HPALETTE) m_hPalette);
44}
45
46wxPalette::wxPalette()
47{
48}
49
50wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
51{
52    Create(n, red, green, blue);
53}
54
55wxPalette::~wxPalette(void)
56{
57//    FreeResource(true);
58}
59
60bool wxPalette::FreeResource(bool WXUNUSED(force))
61{
62    if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
63    {
64        DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
65    }
66
67    return true;
68}
69
70int wxPalette::GetColoursCount() const
71{
72    if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
73    {
74        return ::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, 0, 0, NULL );
75    }
76
77    return 0;
78}
79
80bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
81{
82    // The number of colours in LOGPALETTE is a WORD so we can't create
83    // palettes with more entries than USHRT_MAX.
84    if ( n < 0 || n > 65535 )
85        return false;
86
87    UnRef();
88
89#if defined(__WXMICROWIN__)
90
91    return false;
92
93#else
94
95    m_refData = new wxPaletteRefData;
96
97    NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
98                          (WORD)n * sizeof(PALETTEENTRY));
99    if (!npPal)
100        return false;
101
102    npPal->palVersion = 0x300;
103    npPal->palNumEntries = (WORD)n;
104
105    int i;
106    for (i = 0; i < n; i ++)
107    {
108        npPal->palPalEntry[i].peRed = red[i];
109        npPal->palPalEntry[i].peGreen = green[i];
110        npPal->palPalEntry[i].peBlue = blue[i];
111        npPal->palPalEntry[i].peFlags = 0;
112    }
113    M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
114    LocalFree((HANDLE)npPal);
115    return true;
116
117#endif
118}
119
120int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
121{
122#ifdef __WXMICROWIN__
123    return wxNOT_FOUND;
124#else
125    if ( !m_refData )
126        return wxNOT_FOUND;
127
128    return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
129#endif
130}
131
132bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
133{
134#ifdef __WXMICROWIN__
135    return false;
136#else
137    if ( !m_refData )
138        return false;
139
140    if (index < 0 || index > 255)
141        return false;
142
143    PALETTEENTRY entry;
144    if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
145    {
146        *red = entry.peRed;
147        *green = entry.peGreen;
148        *blue = entry.peBlue;
149        return true;
150    }
151    else
152        return false;
153#endif
154}
155
156void wxPalette::SetHPALETTE(WXHPALETTE pal)
157{
158    if ( !m_refData )
159        m_refData = new wxPaletteRefData;
160
161    M_PALETTEDATA->m_hPalette = pal;
162}
163
164#endif // wxUSE_PALETTE
165