]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/enhmeta.cpp
Changed system colours for better default display of wxGrid. Please revert
[wxWidgets.git] / src / msw / enhmeta.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/enhmeta.cpp
3// Purpose: implementation of wxEnhMetaFileXXX classes
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 13.01.00
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "enhmeta.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#if wxUSE_ENH_METAFILE
32
33#ifndef WX_PRECOMP
34 #include "wx/string.h"
35 #include "wx/log.h"
36#endif //WX_PRECOMP
37
38#include "wx/metafile.h"
39
40#include "wx/msw/private.h"
41
42// ----------------------------------------------------------------------------
43// wxWin macros
44// ----------------------------------------------------------------------------
45
46IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
47IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
48
49// ----------------------------------------------------------------------------
50// macros
51// ----------------------------------------------------------------------------
52
53#define GetEMF() ((HENHMETAFILE)m_hMF)
54#define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
55
56// ----------------------------------------------------------------------------
57// private functions
58// ----------------------------------------------------------------------------
59
60// we must pass NULL if the string is empty to metafile functions
61static inline const wxChar *GetMetaFileName(const wxString& fn)
62 { return !fn ? (wxChar *)NULL : fn.c_str(); }
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxEnhMetaFile
70// ----------------------------------------------------------------------------
71
72void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
73{
74 if ( &mf == this )
75 return;
76
77 if ( mf.m_hMF )
78 {
79 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
80 GetMetaFileName(m_filename));
81 if ( !m_hMF )
82 {
83 wxLogLastError(_T("CopyEnhMetaFile"));
84 }
85 }
86 else
87 {
88 m_hMF = 0;
89 }
90}
91
92void wxEnhMetaFile::Free()
93{
94 if ( m_hMF )
95 {
96 if ( !::DeleteEnhMetaFile(GetEMF()) )
97 {
98 wxLogLastError(_T("DeleteEnhMetaFile"));
99 }
100 }
101}
102
103bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
104{
105 wxCHECK_MSG( Ok(), FALSE, _T("can't play invalid enhanced metafile") );
106 wxCHECK_MSG( dc, FALSE, _T("invalid wxDC in wxEnhMetaFile::Play") );
107
108 RECT rect;
109 if ( rectBound )
110 {
111 rect.top = rectBound->y;
112 rect.left = rectBound->x;
113 rect.right = rectBound->x + rectBound->width;
114 rect.bottom = rectBound->y + rectBound->height;
115 }
116 else
117 {
118 wxSize size = GetSize();
119
120 rect.top =
121 rect.left = 0;
122 rect.right = size.x;
123 rect.bottom = size.y;
124 }
125
126 if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) )
127 {
128 wxLogLastError(_T("PlayEnhMetaFile"));
129
130 return FALSE;
131 }
132
133 return TRUE;
134}
135
136wxSize wxEnhMetaFile::GetSize() const
137{
138 wxSize size = wxDefaultSize;
139
140 if ( Ok() )
141 {
142 ENHMETAHEADER hdr;
143 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
144 {
145 wxLogLastError(_T("GetEnhMetaFileHeader"));
146 }
147 else
148 {
149 // the width and height are in HIMETRIC (0.01mm) units, transform
150 // them to pixels
151 LONG w = hdr.rclFrame.right,
152 h = hdr.rclFrame.bottom;
153
154 HIMETRICToPixel(&w, &h);
155
156 size.x = w;
157 size.y = h;
158 }
159 }
160
161 return size;
162}
163
164// ----------------------------------------------------------------------------
165// wxEnhMetaFileDC
166// ----------------------------------------------------------------------------
167
168wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
169 int width, int height,
170 const wxString& description)
171{
172 ScreenHDC hdcRef;
173
174 RECT rect, *pRect;
175 if ( width && height )
176 {
177 rect.top =
178 rect.left = 0;
179 rect.right = width;
180 rect.bottom = height;
181
182 // CreateEnhMetaFile() wants them in HIMETRIC
183 PixelToHIMETRIC(&rect.right, &rect.bottom);
184
185 pRect = &rect;
186 }
187 else
188 {
189 // GDI will try to find out the size for us (not recommended)
190 pRect = (LPRECT)NULL;
191 }
192
193 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
194 pRect, description);
195 if ( !m_hDC )
196 {
197 wxLogLastError(_T("CreateEnhMetaFile"));
198 }
199}
200
201wxEnhMetaFile *wxEnhMetaFileDC::Close()
202{
203 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
204
205 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
206 if ( !hMF )
207 {
208 wxLogLastError(_T("CloseEnhMetaFile"));
209
210 return NULL;
211 }
212
213 wxEnhMetaFile *mf = new wxEnhMetaFile;
214 mf->SetHENHMETAFILE((WXHANDLE)hMF);
215 return mf;
216}
217
218wxEnhMetaFileDC::~wxEnhMetaFileDC()
219{
220 // avoid freeing it in the base class dtor
221 m_hDC = 0;
222}
223
224// ----------------------------------------------------------------------------
225// wxEnhMetaFileDataObject
226// ----------------------------------------------------------------------------
227
228#if wxUSE_DRAG_AND_DROP
229
230wxDataFormat
231wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
232{
233 return wxDF_ENHMETAFILE;
234}
235
236size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
237{
238 // wxDF_ENHMETAFILE and wxDF_METAFILE
239 return 2;
240}
241
242void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
243 Direction WXUNUSED(dir)) const
244{
245 formats[0] = wxDF_ENHMETAFILE;
246 formats[1] = wxDF_METAFILE;
247}
248
249size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
250{
251 if ( format == wxDF_ENHMETAFILE )
252 {
253 // we pass data by handle and not HGLOBAL
254 return 0u;
255 }
256 else
257 {
258 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
259
260 return sizeof(METAFILEPICT);
261 }
262}
263
264bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
265{
266 wxCHECK_MSG( m_metafile.Ok(), FALSE, _T("copying invalid enh metafile") );
267
268 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
269
270 if ( format == wxDF_ENHMETAFILE )
271 {
272 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
273 if ( !hEMFCopy )
274 {
275 wxLogLastError(_T("CopyEnhMetaFile"));
276
277 return FALSE;
278 }
279
280 *(HENHMETAFILE *)buf = hEMFCopy;
281 }
282 else
283 {
284 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
285
286 // convert to WMF
287
288 ScreenHDC hdc;
289
290 // first get the buffer size and alloc memory
291 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
292 wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") );
293
294 BYTE *bits = (BYTE *)malloc(size);
295
296 // then get the enh metafile bits
297 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
298 {
299 wxLogLastError(_T("GetWinMetaFileBits"));
300
301 free(bits);
302
303 return FALSE;
304 }
305
306 // and finally convert them to the WMF
307 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
308 free(bits);
309 if ( !hMF )
310 {
311 wxLogLastError(_T("SetMetaFileBitsEx"));
312
313 return FALSE;
314 }
315
316 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
317
318 wxSize sizeMF = m_metafile.GetSize();
319 mfpict->hMF = hMF;
320 mfpict->mm = MM_ANISOTROPIC;
321 mfpict->xExt = sizeMF.x;
322 mfpict->yExt = sizeMF.y;
323
324 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
325 }
326
327 return TRUE;
328}
329
330bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
331 size_t WXUNUSED(len),
332 const void *buf)
333{
334 HENHMETAFILE hEMF;
335
336 if ( format == wxDF_ENHMETAFILE )
337 {
338 hEMF = *(HENHMETAFILE *)buf;
339
340 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
341 }
342 else
343 {
344 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
345
346 // convert from WMF
347 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
348
349 // first get the buffer size
350 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
351 wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") );
352
353 // then get metafile bits
354 BYTE *bits = (BYTE *)malloc(size);
355 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
356 {
357 wxLogLastError(_T("GetMetaFileBitsEx"));
358
359 free(bits);
360
361 return FALSE;
362 }
363
364 ScreenHDC hdcRef;
365
366 // and finally create an enhanced metafile from them
367 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
368 free(bits);
369 if ( !hEMF )
370 {
371 wxLogLastError(_T("SetWinMetaFileBits"));
372
373 return FALSE;
374 }
375 }
376
377 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
378
379 return TRUE;
380}
381#endif // wxUSE_DRAG_AND_DROP
382
383#endif // wxUSE_ENH_METAFILE