]> git.saurik.com Git - wxWidgets.git/blame - src/msw/enhmeta.cpp
update from liou xiao <liouxiao@hotmail.com>
[wxWidgets.git] / src / msw / enhmeta.cpp
CommitLineData
d9317fd4
VZ
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>
65571936 9// Licence: wxWindows licence
d9317fd4
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
d9317fd4
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_ENH_METAFILE
28
29#ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/log.h"
7dca9b44 32 #include "wx/intl.h"
d9317fd4
VZ
33#endif //WX_PRECOMP
34
35#include "wx/metafile.h"
bfbd6dc1 36#include "wx/clipbrd.h"
d9317fd4
VZ
37
38#include "wx/msw/private.h"
39
40// ----------------------------------------------------------------------------
41// wxWin macros
42// ----------------------------------------------------------------------------
43
44IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
45IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
46
47// ----------------------------------------------------------------------------
48// macros
49// ----------------------------------------------------------------------------
50
51#define GetEMF() ((HENHMETAFILE)m_hMF)
52#define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
53
54// ----------------------------------------------------------------------------
55// private functions
56// ----------------------------------------------------------------------------
57
58// we must pass NULL if the string is empty to metafile functions
59static inline const wxChar *GetMetaFileName(const wxString& fn)
60 { return !fn ? (wxChar *)NULL : fn.c_str(); }
61
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// wxEnhMetaFile
68// ----------------------------------------------------------------------------
69
7a295dfa
VZ
70void wxEnhMetaFile::Init()
71{
72 if ( m_filename.empty() )
73 {
74 m_hMF = 0;
75 }
76 else // have valid file name, load metafile from it
77 {
78 m_hMF = GetEnhMetaFile(m_filename);
79 if ( !m_hMF )
80 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
81 m_filename.c_str());
82 }
83}
84
d9317fd4
VZ
85void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
86{
87 if ( &mf == this )
88 return;
89
90 if ( mf.m_hMF )
91 {
92 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
93 GetMetaFileName(m_filename));
94 if ( !m_hMF )
95 {
96 wxLogLastError(_T("CopyEnhMetaFile"));
97 }
98 }
99 else
100 {
101 m_hMF = 0;
102 }
103}
104
105void wxEnhMetaFile::Free()
106{
107 if ( m_hMF )
108 {
109 if ( !::DeleteEnhMetaFile(GetEMF()) )
110 {
111 wxLogLastError(_T("DeleteEnhMetaFile"));
112 }
113 }
114}
115
116bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
117{
cbe874bd 118 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
caf448e3 119 wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
d9317fd4
VZ
120
121 RECT rect;
122 if ( rectBound )
123 {
124 rect.top = rectBound->y;
125 rect.left = rectBound->x;
126 rect.right = rectBound->x + rectBound->width;
127 rect.bottom = rectBound->y + rectBound->height;
128 }
129 else
130 {
131 wxSize size = GetSize();
132
133 rect.top =
134 rect.left = 0;
135 rect.right = size.x;
136 rect.bottom = size.y;
137 }
138
139 if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) )
140 {
141 wxLogLastError(_T("PlayEnhMetaFile"));
142
cbe874bd 143 return false;
d9317fd4
VZ
144 }
145
cbe874bd 146 return true;
d9317fd4
VZ
147}
148
149wxSize wxEnhMetaFile::GetSize() const
150{
151 wxSize size = wxDefaultSize;
152
153 if ( Ok() )
154 {
155 ENHMETAHEADER hdr;
156 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
157 {
158 wxLogLastError(_T("GetEnhMetaFileHeader"));
159 }
160 else
161 {
162 // the width and height are in HIMETRIC (0.01mm) units, transform
163 // them to pixels
164 LONG w = hdr.rclFrame.right,
165 h = hdr.rclFrame.bottom;
166
167 HIMETRICToPixel(&w, &h);
168
169 size.x = w;
170 size.y = h;
171 }
172 }
173
174 return size;
175}
176
bfbd6dc1
VZ
177bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
178{
caf448e3
WS
179#if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
180 wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") );
bfbd6dc1
VZ
181
182 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
a95e38c0
VZ
183#else // !wxUSE_DRAG_AND_DROP
184 wxFAIL_MSG(_T("not implemented"));
cbe874bd 185 return false;
a95e38c0 186#endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
bfbd6dc1
VZ
187}
188
d9317fd4
VZ
189// ----------------------------------------------------------------------------
190// wxEnhMetaFileDC
191// ----------------------------------------------------------------------------
192
193wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
194 int width, int height,
195 const wxString& description)
196{
197 ScreenHDC hdcRef;
33ac7e6f 198 RECT rect;
cbe874bd 199 RECT *pRect;
d9317fd4
VZ
200 if ( width && height )
201 {
202 rect.top =
203 rect.left = 0;
204 rect.right = width;
205 rect.bottom = height;
206
207 // CreateEnhMetaFile() wants them in HIMETRIC
208 PixelToHIMETRIC(&rect.right, &rect.bottom);
cbe874bd 209
d9317fd4
VZ
210 pRect = &rect;
211 }
212 else
213 {
214 // GDI will try to find out the size for us (not recommended)
215 pRect = (LPRECT)NULL;
216 }
217
218 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
219 pRect, description);
220 if ( !m_hDC )
221 {
222 wxLogLastError(_T("CreateEnhMetaFile"));
223 }
224}
225
226wxEnhMetaFile *wxEnhMetaFileDC::Close()
227{
228 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
229
230 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
231 if ( !hMF )
232 {
233 wxLogLastError(_T("CloseEnhMetaFile"));
234
235 return NULL;
236 }
237
238 wxEnhMetaFile *mf = new wxEnhMetaFile;
239 mf->SetHENHMETAFILE((WXHANDLE)hMF);
240 return mf;
241}
242
243wxEnhMetaFileDC::~wxEnhMetaFileDC()
244{
245 // avoid freeing it in the base class dtor
246 m_hDC = 0;
247}
248
7ba4fbeb
VZ
249#if wxUSE_DRAG_AND_DROP
250
d9317fd4
VZ
251// ----------------------------------------------------------------------------
252// wxEnhMetaFileDataObject
253// ----------------------------------------------------------------------------
254
255wxDataFormat
256wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
257{
258 return wxDF_ENHMETAFILE;
259}
260
261size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
262{
263 // wxDF_ENHMETAFILE and wxDF_METAFILE
264 return 2;
265}
266
267void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
268 Direction WXUNUSED(dir)) const
269{
270 formats[0] = wxDF_ENHMETAFILE;
271 formats[1] = wxDF_METAFILE;
272}
273
274size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
275{
276 if ( format == wxDF_ENHMETAFILE )
277 {
278 // we pass data by handle and not HGLOBAL
279 return 0u;
280 }
281 else
282 {
283 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
284
285 return sizeof(METAFILEPICT);
286 }
287}
288
289bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
290{
cbe874bd 291 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
d9317fd4
VZ
292
293 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
294
295 if ( format == wxDF_ENHMETAFILE )
296 {
297 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
298 if ( !hEMFCopy )
299 {
300 wxLogLastError(_T("CopyEnhMetaFile"));
301
cbe874bd 302 return false;
d9317fd4
VZ
303 }
304
305 *(HENHMETAFILE *)buf = hEMFCopy;
306 }
307 else
308 {
309 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
310
311 // convert to WMF
312
313 ScreenHDC hdc;
314
315 // first get the buffer size and alloc memory
316 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
caf448e3 317 wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") );
d9317fd4
VZ
318
319 BYTE *bits = (BYTE *)malloc(size);
320
321 // then get the enh metafile bits
322 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
323 {
324 wxLogLastError(_T("GetWinMetaFileBits"));
325
326 free(bits);
327
cbe874bd 328 return false;
d9317fd4
VZ
329 }
330
331 // and finally convert them to the WMF
332 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
333 free(bits);
334 if ( !hMF )
335 {
336 wxLogLastError(_T("SetMetaFileBitsEx"));
337
cbe874bd 338 return false;
d9317fd4
VZ
339 }
340
341 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
342
343 wxSize sizeMF = m_metafile.GetSize();
344 mfpict->hMF = hMF;
345 mfpict->mm = MM_ANISOTROPIC;
346 mfpict->xExt = sizeMF.x;
347 mfpict->yExt = sizeMF.y;
348
349 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
350 }
351
cbe874bd 352 return true;
d9317fd4
VZ
353}
354
355bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
356 size_t WXUNUSED(len),
357 const void *buf)
358{
359 HENHMETAFILE hEMF;
360
361 if ( format == wxDF_ENHMETAFILE )
362 {
363 hEMF = *(HENHMETAFILE *)buf;
364
caf448e3 365 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
d9317fd4
VZ
366 }
367 else
368 {
369 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
370
371 // convert from WMF
372 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
373
374 // first get the buffer size
375 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
caf448e3 376 wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") );
d9317fd4
VZ
377
378 // then get metafile bits
379 BYTE *bits = (BYTE *)malloc(size);
380 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
381 {
382 wxLogLastError(_T("GetMetaFileBitsEx"));
383
384 free(bits);
385
cbe874bd 386 return false;
d9317fd4
VZ
387 }
388
389 ScreenHDC hdcRef;
390
391 // and finally create an enhanced metafile from them
392 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
393 free(bits);
394 if ( !hEMF )
395 {
396 wxLogLastError(_T("SetWinMetaFileBits"));
397
cbe874bd 398 return false;
d9317fd4
VZ
399 }
400 }
401
402 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
403
cbe874bd 404 return true;
d9317fd4 405}
7ba4fbeb
VZ
406
407// ----------------------------------------------------------------------------
408// wxEnhMetaFileSimpleDataObject
409// ----------------------------------------------------------------------------
410
411size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
412{
413 // we pass data by handle and not HGLOBAL
414 return 0u;
415}
416
417bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
418{
cbe874bd 419 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
7ba4fbeb
VZ
420
421 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
422
423 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
424 if ( !hEMFCopy )
425 {
426 wxLogLastError(_T("CopyEnhMetaFile"));
427
cbe874bd 428 return false;
7ba4fbeb
VZ
429 }
430
431 *(HENHMETAFILE *)buf = hEMFCopy;
cbe874bd 432 return true;
7ba4fbeb
VZ
433}
434
435bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
436 const void *buf)
437{
438 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
439
caf448e3 440 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
7ba4fbeb
VZ
441 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
442
cbe874bd 443 return true;
7ba4fbeb
VZ
444}
445
4ce1efe1 446
a2327a9f 447#endif // wxUSE_DRAG_AND_DROP
d9317fd4
VZ
448
449#endif // wxUSE_ENH_METAFILE