]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: palette.cpp | |
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 | ||
2bda0e17 | 21 | #ifndef WX_PRECOMP |
d275c7eb | 22 | #include "wx/palette.h" |
2bda0e17 KB |
23 | #endif |
24 | ||
d275c7eb | 25 | #include "wx/msw/private.h" |
2bda0e17 | 26 | |
2bda0e17 | 27 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
2bda0e17 KB |
28 | |
29 | /* | |
30 | * Palette | |
31 | * | |
32 | */ | |
33 | ||
34 | wxPaletteRefData::wxPaletteRefData(void) | |
35 | { | |
36 | m_hPalette = 0; | |
37 | } | |
38 | ||
39 | wxPaletteRefData::~wxPaletteRefData(void) | |
40 | { | |
d275c7eb VZ |
41 | if ( m_hPalette ) |
42 | ::DeleteObject((HPALETTE) m_hPalette); | |
2bda0e17 KB |
43 | } |
44 | ||
45 | wxPalette::wxPalette(void) | |
46 | { | |
47 | } | |
48 | ||
debe6624 | 49 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
2bda0e17 KB |
50 | { |
51 | Create(n, red, green, blue); | |
52 | } | |
53 | ||
54 | wxPalette::~wxPalette(void) | |
55 | { | |
078cf5cb | 56 | // FreeResource(true); |
2bda0e17 KB |
57 | } |
58 | ||
33ac7e6f | 59 | bool wxPalette::FreeResource(bool WXUNUSED(force)) |
2bda0e17 | 60 | { |
d275c7eb VZ |
61 | if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette) |
62 | { | |
2bda0e17 | 63 | DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette); |
d275c7eb | 64 | } |
078cf5cb | 65 | return true; |
2bda0e17 KB |
66 | } |
67 | ||
debe6624 | 68 | bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
2bda0e17 KB |
69 | { |
70 | UnRef(); | |
71 | ||
b4da152e | 72 | #if defined(__WXMICROWIN__) |
5ea105e0 | 73 | |
078cf5cb | 74 | return false; |
33ac7e6f | 75 | |
5ea105e0 RR |
76 | #else |
77 | ||
2bda0e17 KB |
78 | m_refData = new wxPaletteRefData; |
79 | ||
80 | NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + | |
81 | (WORD)n * sizeof(PALETTEENTRY)); | |
82 | if (!npPal) | |
078cf5cb | 83 | return false; |
2bda0e17 KB |
84 | |
85 | npPal->palVersion = 0x300; | |
373a5fb3 | 86 | npPal->palNumEntries = (WORD)n; |
2bda0e17 KB |
87 | |
88 | int i; | |
89 | for (i = 0; i < n; i ++) | |
90 | { | |
91 | npPal->palPalEntry[i].peRed = red[i]; | |
92 | npPal->palPalEntry[i].peGreen = green[i]; | |
93 | npPal->palPalEntry[i].peBlue = blue[i]; | |
94 | npPal->palPalEntry[i].peFlags = 0; | |
95 | } | |
96 | M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal); | |
97 | LocalFree((HANDLE)npPal); | |
078cf5cb | 98 | return true; |
33ac7e6f | 99 | |
5ea105e0 | 100 | #endif |
2bda0e17 KB |
101 | } |
102 | ||
103 | int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const | |
104 | { | |
04ef50df | 105 | #ifdef __WXMICROWIN__ |
078cf5cb | 106 | return 0; |
04ef50df | 107 | #else |
2bda0e17 | 108 | if ( !m_refData ) |
078cf5cb | 109 | return 0; |
2bda0e17 KB |
110 | |
111 | return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue)); | |
04ef50df | 112 | #endif |
2bda0e17 KB |
113 | } |
114 | ||
debe6624 | 115 | bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const |
2bda0e17 | 116 | { |
04ef50df | 117 | #ifdef __WXMICROWIN__ |
078cf5cb | 118 | return false; |
04ef50df | 119 | #else |
2bda0e17 | 120 | if ( !m_refData ) |
078cf5cb | 121 | return false; |
2bda0e17 KB |
122 | |
123 | if (index < 0 || index > 255) | |
078cf5cb | 124 | return false; |
2bda0e17 KB |
125 | |
126 | PALETTEENTRY entry; | |
127 | if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry)) | |
128 | { | |
129 | *red = entry.peRed; | |
130 | *green = entry.peGreen; | |
131 | *blue = entry.peBlue; | |
078cf5cb | 132 | return true; |
2bda0e17 | 133 | } else |
078cf5cb | 134 | return false; |
04ef50df | 135 | #endif |
2bda0e17 KB |
136 | } |
137 | ||
138 | void wxPalette::SetHPALETTE(WXHPALETTE pal) | |
139 | { | |
d275c7eb VZ |
140 | if ( !m_refData ) |
141 | m_refData = new wxPaletteRefData; | |
2bda0e17 KB |
142 | |
143 | M_PALETTEDATA->m_hPalette = pal; | |
144 | } | |
145 | ||
d275c7eb VZ |
146 | #endif // wxUSE_PALETTE |
147 |