]> git.saurik.com Git - wxWidgets.git/blame - src/msw/palette.cpp
add aglUpdateContext() call (doesn't seem to change anything but should be there...
[wxWidgets.git] / src / msw / palette.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/msw/palette.cpp
2bda0e17
KB
3// Purpose: wxPalette
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
d275c7eb 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
d275c7eb
VZ
19#if wxUSE_PALETTE
20
559a723c
WS
21#include "wx/palette.h"
22
2bda0e17 23#ifndef WX_PRECOMP
2bda0e17
KB
24#endif
25
d275c7eb 26#include "wx/msw/private.h"
2bda0e17 27
2bda0e17 28IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
2bda0e17
KB
29
30/*
31 * Palette
32 *
33 */
34
35wxPaletteRefData::wxPaletteRefData(void)
36{
88ef3a57 37 m_hPalette = 0;
2bda0e17
KB
38}
39
40wxPaletteRefData::~wxPaletteRefData(void)
41{
d275c7eb
VZ
42 if ( m_hPalette )
43 ::DeleteObject((HPALETTE) m_hPalette);
2bda0e17
KB
44}
45
5633b968 46wxPalette::wxPalette()
2bda0e17
KB
47{
48}
49
debe6624 50wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
2bda0e17 51{
5633b968 52 Create(n, red, green, blue);
2bda0e17
KB
53}
54
55wxPalette::~wxPalette(void)
56{
078cf5cb 57// FreeResource(true);
2bda0e17
KB
58}
59
33ac7e6f 60bool wxPalette::FreeResource(bool WXUNUSED(force))
2bda0e17 61{
d275c7eb
VZ
62 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
63 {
5633b968 64 DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
d275c7eb 65 }
f4322df6 66
078cf5cb 67 return true;
2bda0e17
KB
68}
69
5633b968
RR
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 }
f4322df6 76
5633b968
RR
77 return 0;
78}
79
debe6624 80bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
2bda0e17 81{
88ef3a57 82 UnRef();
2bda0e17 83
b4da152e 84#if defined(__WXMICROWIN__)
5ea105e0 85
88ef3a57 86 return false;
33ac7e6f 87
5ea105e0
RR
88#else
89
88ef3a57 90 m_refData = new wxPaletteRefData;
2bda0e17 91
88ef3a57
WS
92 NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
93 (WORD)n * sizeof(PALETTEENTRY));
94 if (!npPal)
95 return false;
2bda0e17 96
88ef3a57
WS
97 npPal->palVersion = 0x300;
98 npPal->palNumEntries = (WORD)n;
99
100 int i;
101 for (i = 0; i < n; i ++)
102 {
103 npPal->palPalEntry[i].peRed = red[i];
104 npPal->palPalEntry[i].peGreen = green[i];
105 npPal->palPalEntry[i].peBlue = blue[i];
106 npPal->palPalEntry[i].peFlags = 0;
107 }
108 M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
109 LocalFree((HANDLE)npPal);
110 return true;
33ac7e6f 111
5ea105e0 112#endif
2bda0e17
KB
113}
114
88ef3a57 115int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
2bda0e17 116{
04ef50df 117#ifdef __WXMICROWIN__
88ef3a57 118 return wxNOT_FOUND;
04ef50df 119#else
88ef3a57
WS
120 if ( !m_refData )
121 return wxNOT_FOUND;
2bda0e17 122
88ef3a57 123 return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
04ef50df 124#endif
2bda0e17
KB
125}
126
debe6624 127bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
2bda0e17 128{
04ef50df 129#ifdef __WXMICROWIN__
078cf5cb 130 return false;
88ef3a57
WS
131#else
132 if ( !m_refData )
133 return false;
134
135 if (index < 0 || index > 255)
136 return false;
2bda0e17 137
88ef3a57
WS
138 PALETTEENTRY entry;
139 if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
140 {
141 *red = entry.peRed;
142 *green = entry.peGreen;
143 *blue = entry.peBlue;
144 return true;
145 }
146 else
147 return false;
04ef50df 148#endif
2bda0e17
KB
149}
150
151void wxPalette::SetHPALETTE(WXHPALETTE pal)
152{
d275c7eb
VZ
153 if ( !m_refData )
154 m_refData = new wxPaletteRefData;
2bda0e17
KB
155
156 M_PALETTEDATA->m_hPalette = pal;
157}
158
d275c7eb 159#endif // wxUSE_PALETTE