1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/palette.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/palette.h"
23 #include "wx/msw/private.h"
25 // ============================================================================
27 // ============================================================================
29 class WXDLLEXPORT wxPaletteRefData
: public wxGDIRefData
32 wxPaletteRefData() { Init(); }
34 wxPaletteRefData(int n
,
35 const unsigned char *red
,
36 const unsigned char *green
,
37 const unsigned char *blue
)
41 LOGPALETTE
*pPal
= Alloc(n
);
45 for ( int i
= 0; i
< n
; i
++ )
47 pPal
->palPalEntry
[i
].peRed
= red
[i
];
48 pPal
->palPalEntry
[i
].peGreen
= green
[i
];
49 pPal
->palPalEntry
[i
].peBlue
= blue
[i
];
50 pPal
->palPalEntry
[i
].peFlags
= 0;
53 m_hPalette
= ::CreatePalette(pPal
);
57 wxPaletteRefData(const wxPaletteRefData
& data
)
62 const UINT n
= data
.GetEntries();
66 LOGPALETTE
*pPal
= Alloc(n
);
70 if ( ::GetPaletteEntries(data
.m_hPalette
, 0, n
, pPal
->palPalEntry
) )
71 m_hPalette
= ::CreatePalette(pPal
);
76 virtual ~wxPaletteRefData()
79 ::DeleteObject(m_hPalette
);
82 virtual bool IsOk() const { return m_hPalette
!= 0; }
84 UINT
GetEntries() const
86 return ::GetPaletteEntries(m_hPalette
, 0, 0, NULL
);
90 // caller must free() the pointer
91 static LOGPALETTE
*Alloc(UINT numEntries
)
93 LOGPALETTE
*pPal
= (LOGPALETTE
*)
94 malloc(sizeof(LOGPALETTE
) + numEntries
*sizeof(PALETTEENTRY
));
97 pPal
->palVersion
= 0x300;
98 pPal
->palNumEntries
= numEntries
;
104 void Init() { m_hPalette
= 0; }
108 friend class WXDLLIMPEXP_FWD_CORE wxPalette
;
111 // ============================================================================
113 // ============================================================================
115 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
117 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
119 bool wxPalette::Create(int n
,
120 const unsigned char *red
,
121 const unsigned char *green
,
122 const unsigned char *blue
)
124 m_refData
= new wxPaletteRefData(n
, red
, green
, blue
);
129 wxGDIRefData
*wxPalette::CreateGDIRefData() const
131 return new wxPaletteRefData
;
134 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
136 return new wxPaletteRefData(*static_cast<const wxPaletteRefData
*>(data
));
139 int wxPalette::GetColoursCount() const
141 return IsOk() ? M_PALETTEDATA
->GetEntries() : 0;
144 int wxPalette::GetPixel(unsigned char red
,
146 unsigned char blue
) const
151 return ::GetNearestPaletteIndex(M_PALETTEDATA
->m_hPalette
,
152 PALETTERGB(red
, green
, blue
));
155 bool wxPalette::GetRGB(int index
,
157 unsigned char *green
,
158 unsigned char *blue
) const
163 if (index
< 0 || index
> 255)
167 if ( !::GetPaletteEntries(M_PALETTEDATA
->m_hPalette
, index
, 1, &entry
) )
171 *green
= entry
.peGreen
;
172 *blue
= entry
.peBlue
;
177 WXHPALETTE
wxPalette::GetHPALETTE() const
179 return M_PALETTEDATA
? (WXHPALETTE
)M_PALETTEDATA
->m_hPalette
: 0;
182 void wxPalette::SetHPALETTE(WXHPALETTE pal
)
186 M_PALETTEDATA
->m_hPalette
= (HPALETTE
)pal
;
189 #endif // wxUSE_PALETTE