1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/palette.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/palette.h"
22 #include "wx/msw/private.h"
24 // ============================================================================
26 // ============================================================================
28 class WXDLLEXPORT wxPaletteRefData
: public wxGDIRefData
31 wxPaletteRefData() { Init(); }
33 wxPaletteRefData(int n
,
34 const unsigned char *red
,
35 const unsigned char *green
,
36 const unsigned char *blue
)
40 LOGPALETTE
*pPal
= Alloc(n
);
44 for ( int i
= 0; i
< n
; i
++ )
46 pPal
->palPalEntry
[i
].peRed
= red
[i
];
47 pPal
->palPalEntry
[i
].peGreen
= green
[i
];
48 pPal
->palPalEntry
[i
].peBlue
= blue
[i
];
49 pPal
->palPalEntry
[i
].peFlags
= 0;
52 m_hPalette
= ::CreatePalette(pPal
);
56 wxPaletteRefData(const wxPaletteRefData
& data
)
61 const UINT n
= data
.GetEntries();
65 LOGPALETTE
*pPal
= Alloc(n
);
69 if ( ::GetPaletteEntries(data
.m_hPalette
, 0, n
, pPal
->palPalEntry
) )
70 m_hPalette
= ::CreatePalette(pPal
);
75 virtual ~wxPaletteRefData()
78 ::DeleteObject(m_hPalette
);
81 virtual bool IsOk() const { return m_hPalette
!= 0; }
83 UINT
GetEntries() const
85 return ::GetPaletteEntries(m_hPalette
, 0, 0, NULL
);
89 // caller must free() the pointer
90 static LOGPALETTE
*Alloc(UINT numEntries
)
92 LOGPALETTE
*pPal
= (LOGPALETTE
*)
93 malloc(sizeof(LOGPALETTE
) + numEntries
*sizeof(PALETTEENTRY
));
96 pPal
->palVersion
= 0x300;
97 pPal
->palNumEntries
= numEntries
;
103 void Init() { m_hPalette
= 0; }
107 friend class WXDLLIMPEXP_FWD_CORE wxPalette
;
110 // ============================================================================
112 // ============================================================================
114 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
116 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
118 bool wxPalette::Create(int n
,
119 const unsigned char *red
,
120 const unsigned char *green
,
121 const unsigned char *blue
)
123 m_refData
= new wxPaletteRefData(n
, red
, green
, blue
);
128 wxGDIRefData
*wxPalette::CreateGDIRefData() const
130 return new wxPaletteRefData
;
133 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
135 return new wxPaletteRefData(*static_cast<const wxPaletteRefData
*>(data
));
138 int wxPalette::GetColoursCount() const
140 return IsOk() ? M_PALETTEDATA
->GetEntries() : 0;
143 int wxPalette::GetPixel(unsigned char red
,
145 unsigned char blue
) const
150 return ::GetNearestPaletteIndex(M_PALETTEDATA
->m_hPalette
,
151 PALETTERGB(red
, green
, blue
));
154 bool wxPalette::GetRGB(int index
,
156 unsigned char *green
,
157 unsigned char *blue
) const
162 if (index
< 0 || index
> 255)
166 if ( !::GetPaletteEntries(M_PALETTEDATA
->m_hPalette
, index
, 1, &entry
) )
170 *green
= entry
.peGreen
;
171 *blue
= entry
.peBlue
;
176 WXHPALETTE
wxPalette::GetHPALETTE() const
178 return M_PALETTEDATA
? (WXHPALETTE
)M_PALETTEDATA
->m_hPalette
: 0;
181 void wxPalette::SetHPALETTE(WXHPALETTE pal
)
185 M_PALETTEDATA
->m_hPalette
= (HPALETTE
)pal
;
188 #endif // wxUSE_PALETTE