]>
Commit | Line | Data |
---|---|---|
d9317fd4 | 1 | /////////////////////////////////////////////////////////////////////////////// |
658ff7f1 | 2 | // Name: src/msw/enhmeta.cpp |
d9317fd4 VZ |
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 | ||
888dde65 RR |
35 | #include "wx/dc.h" |
36 | #include "wx/msw/dc.h" | |
37 | ||
d9317fd4 | 38 | #include "wx/metafile.h" |
bfbd6dc1 | 39 | #include "wx/clipbrd.h" |
d9317fd4 VZ |
40 | |
41 | #include "wx/msw/private.h" | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // wxWin macros | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject) | |
d9317fd4 VZ |
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 | |
61 | static inline const wxChar *GetMetaFileName(const wxString& fn) | |
e0a050e3 | 62 | { return !fn ? (const wxChar *)NULL : (const wxChar*)fn.wx_str(); } |
d9317fd4 VZ |
63 | |
64 | // ============================================================================ | |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // wxEnhMetaFile | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
8f884a0d VZ |
72 | wxGDIRefData *wxEnhMetaFile::CreateGDIRefData() const |
73 | { | |
74 | wxFAIL_MSG( _T("must be implemented if used") ); | |
75 | ||
76 | return NULL; | |
77 | } | |
78 | ||
79 | wxGDIRefData * | |
80 | wxEnhMetaFile::CloneGDIRefData(const wxGDIRefData *WXUNUSED(data)) const | |
81 | { | |
82 | wxFAIL_MSG( _T("must be implemented if used") ); | |
83 | ||
84 | return NULL; | |
85 | } | |
86 | ||
7a295dfa VZ |
87 | void wxEnhMetaFile::Init() |
88 | { | |
89 | if ( m_filename.empty() ) | |
90 | { | |
91 | m_hMF = 0; | |
92 | } | |
93 | else // have valid file name, load metafile from it | |
94 | { | |
e0a050e3 | 95 | m_hMF = (WXHANDLE)::GetEnhMetaFile(m_filename.fn_str()); |
7a295dfa VZ |
96 | if ( !m_hMF ) |
97 | wxLogSysError(_("Failed to load metafile from file \"%s\"."), | |
98 | m_filename.c_str()); | |
99 | } | |
100 | } | |
101 | ||
d9317fd4 VZ |
102 | void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf) |
103 | { | |
104 | if ( &mf == this ) | |
105 | return; | |
106 | ||
107 | if ( mf.m_hMF ) | |
108 | { | |
109 | m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf), | |
110 | GetMetaFileName(m_filename)); | |
111 | if ( !m_hMF ) | |
112 | { | |
113 | wxLogLastError(_T("CopyEnhMetaFile")); | |
114 | } | |
115 | } | |
116 | else | |
117 | { | |
118 | m_hMF = 0; | |
119 | } | |
120 | } | |
121 | ||
122 | void wxEnhMetaFile::Free() | |
123 | { | |
124 | if ( m_hMF ) | |
125 | { | |
126 | if ( !::DeleteEnhMetaFile(GetEMF()) ) | |
127 | { | |
128 | wxLogLastError(_T("DeleteEnhMetaFile")); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound) | |
134 | { | |
cbe874bd | 135 | wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") ); |
caf448e3 | 136 | wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") ); |
d9317fd4 VZ |
137 | |
138 | RECT rect; | |
139 | if ( rectBound ) | |
140 | { | |
141 | rect.top = rectBound->y; | |
142 | rect.left = rectBound->x; | |
143 | rect.right = rectBound->x + rectBound->width; | |
144 | rect.bottom = rectBound->y + rectBound->height; | |
145 | } | |
146 | else | |
147 | { | |
148 | wxSize size = GetSize(); | |
149 | ||
150 | rect.top = | |
151 | rect.left = 0; | |
152 | rect.right = size.x; | |
153 | rect.bottom = size.y; | |
154 | } | |
155 | ||
888dde65 RR |
156 | wxDCImpl *impl = dc->GetImpl(); |
157 | wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl ); | |
158 | if (!msw_impl) | |
159 | return false; | |
f0875501 | 160 | |
888dde65 | 161 | if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl), GetEMF(), &rect) ) |
d9317fd4 VZ |
162 | { |
163 | wxLogLastError(_T("PlayEnhMetaFile")); | |
164 | ||
cbe874bd | 165 | return false; |
d9317fd4 VZ |
166 | } |
167 | ||
cbe874bd | 168 | return true; |
d9317fd4 VZ |
169 | } |
170 | ||
171 | wxSize wxEnhMetaFile::GetSize() const | |
172 | { | |
173 | wxSize size = wxDefaultSize; | |
174 | ||
175 | if ( Ok() ) | |
176 | { | |
177 | ENHMETAHEADER hdr; | |
178 | if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) ) | |
179 | { | |
180 | wxLogLastError(_T("GetEnhMetaFileHeader")); | |
181 | } | |
182 | else | |
183 | { | |
184 | // the width and height are in HIMETRIC (0.01mm) units, transform | |
185 | // them to pixels | |
186 | LONG w = hdr.rclFrame.right, | |
187 | h = hdr.rclFrame.bottom; | |
188 | ||
189 | HIMETRICToPixel(&w, &h); | |
190 | ||
191 | size.x = w; | |
192 | size.y = h; | |
193 | } | |
194 | } | |
195 | ||
196 | return size; | |
197 | } | |
198 | ||
bfbd6dc1 VZ |
199 | bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height)) |
200 | { | |
caf448e3 WS |
201 | #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD |
202 | wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") ); | |
bfbd6dc1 VZ |
203 | |
204 | return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this)); | |
a95e38c0 VZ |
205 | #else // !wxUSE_DRAG_AND_DROP |
206 | wxFAIL_MSG(_T("not implemented")); | |
cbe874bd | 207 | return false; |
a95e38c0 | 208 | #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP |
bfbd6dc1 VZ |
209 | } |
210 | ||
d9317fd4 VZ |
211 | // ---------------------------------------------------------------------------- |
212 | // wxEnhMetaFileDC | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
888dde65 RR |
215 | class wxEnhMetaFileDCImpl : public wxMSWDCImpl |
216 | { | |
217 | public: | |
f0875501 | 218 | wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner, |
888dde65 RR |
219 | const wxString& filename, int width, int height, |
220 | const wxString& description ); | |
221 | virtual ~wxEnhMetaFileDCImpl(); | |
222 | ||
223 | // obtain a pointer to the new metafile (caller should delete it) | |
224 | wxEnhMetaFile *Close(); | |
225 | ||
226 | protected: | |
227 | virtual void DoGetSize(int *width, int *height) const; | |
228 | ||
229 | private: | |
230 | // size passed to ctor and returned by DoGetSize() | |
231 | int m_width, | |
232 | m_height; | |
233 | }; | |
234 | ||
235 | ||
236 | IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC) | |
237 | ||
d9317fd4 VZ |
238 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename, |
239 | int width, int height, | |
240 | const wxString& description) | |
f0875501 VZ |
241 | : wxDC(new wxEnhMetaFileDCImpl(this, |
242 | filename, | |
243 | width, height, | |
244 | description)) | |
888dde65 | 245 | { |
888dde65 RR |
246 | } |
247 | ||
248 | ||
249 | wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner, | |
250 | const wxString& filename, | |
251 | int width, int height, | |
252 | const wxString& description ) | |
253 | : wxMSWDCImpl( owner ) | |
d9317fd4 | 254 | { |
024026be VZ |
255 | m_width = width; |
256 | m_height = height; | |
257 | ||
33ac7e6f | 258 | RECT rect; |
cbe874bd | 259 | RECT *pRect; |
d9317fd4 VZ |
260 | if ( width && height ) |
261 | { | |
262 | rect.top = | |
263 | rect.left = 0; | |
264 | rect.right = width; | |
265 | rect.bottom = height; | |
266 | ||
267 | // CreateEnhMetaFile() wants them in HIMETRIC | |
268 | PixelToHIMETRIC(&rect.right, &rect.bottom); | |
cbe874bd | 269 | |
d9317fd4 VZ |
270 | pRect = ▭ |
271 | } | |
272 | else | |
273 | { | |
274 | // GDI will try to find out the size for us (not recommended) | |
275 | pRect = (LPRECT)NULL; | |
276 | } | |
277 | ||
024026be | 278 | ScreenHDC hdcRef; |
d9317fd4 | 279 | m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename), |
e0a050e3 | 280 | pRect, description.wx_str()); |
d9317fd4 VZ |
281 | if ( !m_hDC ) |
282 | { | |
283 | wxLogLastError(_T("CreateEnhMetaFile")); | |
284 | } | |
285 | } | |
286 | ||
888dde65 | 287 | void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const |
024026be VZ |
288 | { |
289 | if ( width ) | |
290 | *width = m_width; | |
291 | if ( height ) | |
292 | *height = m_height; | |
293 | } | |
294 | ||
888dde65 | 295 | wxEnhMetaFile *wxEnhMetaFileDCImpl::Close() |
d9317fd4 | 296 | { |
888dde65 | 297 | wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") ); |
d9317fd4 VZ |
298 | |
299 | HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc()); | |
300 | if ( !hMF ) | |
301 | { | |
302 | wxLogLastError(_T("CloseEnhMetaFile")); | |
303 | ||
304 | return NULL; | |
305 | } | |
306 | ||
307 | wxEnhMetaFile *mf = new wxEnhMetaFile; | |
308 | mf->SetHENHMETAFILE((WXHANDLE)hMF); | |
309 | return mf; | |
310 | } | |
311 | ||
888dde65 | 312 | wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl() |
d9317fd4 VZ |
313 | { |
314 | // avoid freeing it in the base class dtor | |
315 | m_hDC = 0; | |
316 | } | |
317 | ||
7ba4fbeb VZ |
318 | #if wxUSE_DRAG_AND_DROP |
319 | ||
d9317fd4 VZ |
320 | // ---------------------------------------------------------------------------- |
321 | // wxEnhMetaFileDataObject | |
322 | // ---------------------------------------------------------------------------- | |
323 | ||
324 | wxDataFormat | |
325 | wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
326 | { | |
327 | return wxDF_ENHMETAFILE; | |
328 | } | |
329 | ||
330 | size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const | |
331 | { | |
332 | // wxDF_ENHMETAFILE and wxDF_METAFILE | |
333 | return 2; | |
334 | } | |
335 | ||
336 | void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats, | |
337 | Direction WXUNUSED(dir)) const | |
338 | { | |
339 | formats[0] = wxDF_ENHMETAFILE; | |
340 | formats[1] = wxDF_METAFILE; | |
341 | } | |
342 | ||
343 | size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const | |
344 | { | |
345 | if ( format == wxDF_ENHMETAFILE ) | |
346 | { | |
347 | // we pass data by handle and not HGLOBAL | |
348 | return 0u; | |
349 | } | |
350 | else | |
351 | { | |
352 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
353 | ||
354 | return sizeof(METAFILEPICT); | |
355 | } | |
356 | } | |
357 | ||
358 | bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
359 | { | |
cbe874bd | 360 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
d9317fd4 VZ |
361 | |
362 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
363 | ||
364 | if ( format == wxDF_ENHMETAFILE ) | |
365 | { | |
366 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
367 | if ( !hEMFCopy ) | |
368 | { | |
369 | wxLogLastError(_T("CopyEnhMetaFile")); | |
370 | ||
cbe874bd | 371 | return false; |
d9317fd4 VZ |
372 | } |
373 | ||
374 | *(HENHMETAFILE *)buf = hEMFCopy; | |
375 | } | |
376 | else | |
377 | { | |
378 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
379 | ||
380 | // convert to WMF | |
381 | ||
382 | ScreenHDC hdc; | |
383 | ||
384 | // first get the buffer size and alloc memory | |
385 | size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc); | |
caf448e3 | 386 | wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") ); |
d9317fd4 VZ |
387 | |
388 | BYTE *bits = (BYTE *)malloc(size); | |
389 | ||
390 | // then get the enh metafile bits | |
391 | if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) ) | |
392 | { | |
393 | wxLogLastError(_T("GetWinMetaFileBits")); | |
394 | ||
395 | free(bits); | |
396 | ||
cbe874bd | 397 | return false; |
d9317fd4 VZ |
398 | } |
399 | ||
400 | // and finally convert them to the WMF | |
401 | HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits); | |
402 | free(bits); | |
403 | if ( !hMF ) | |
404 | { | |
405 | wxLogLastError(_T("SetMetaFileBitsEx")); | |
406 | ||
cbe874bd | 407 | return false; |
d9317fd4 VZ |
408 | } |
409 | ||
410 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
411 | ||
412 | wxSize sizeMF = m_metafile.GetSize(); | |
413 | mfpict->hMF = hMF; | |
414 | mfpict->mm = MM_ANISOTROPIC; | |
415 | mfpict->xExt = sizeMF.x; | |
416 | mfpict->yExt = sizeMF.y; | |
417 | ||
418 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
419 | } | |
420 | ||
cbe874bd | 421 | return true; |
d9317fd4 VZ |
422 | } |
423 | ||
424 | bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format, | |
425 | size_t WXUNUSED(len), | |
426 | const void *buf) | |
427 | { | |
428 | HENHMETAFILE hEMF; | |
429 | ||
430 | if ( format == wxDF_ENHMETAFILE ) | |
431 | { | |
432 | hEMF = *(HENHMETAFILE *)buf; | |
433 | ||
caf448e3 | 434 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
d9317fd4 VZ |
435 | } |
436 | else | |
437 | { | |
438 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
439 | ||
440 | // convert from WMF | |
441 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
442 | ||
443 | // first get the buffer size | |
444 | size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL); | |
caf448e3 | 445 | wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") ); |
d9317fd4 VZ |
446 | |
447 | // then get metafile bits | |
448 | BYTE *bits = (BYTE *)malloc(size); | |
449 | if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) ) | |
450 | { | |
451 | wxLogLastError(_T("GetMetaFileBitsEx")); | |
452 | ||
453 | free(bits); | |
454 | ||
cbe874bd | 455 | return false; |
d9317fd4 VZ |
456 | } |
457 | ||
458 | ScreenHDC hdcRef; | |
459 | ||
460 | // and finally create an enhanced metafile from them | |
461 | hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict); | |
462 | free(bits); | |
463 | if ( !hEMF ) | |
464 | { | |
465 | wxLogLastError(_T("SetWinMetaFileBits")); | |
466 | ||
cbe874bd | 467 | return false; |
d9317fd4 VZ |
468 | } |
469 | } | |
470 | ||
471 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
472 | ||
cbe874bd | 473 | return true; |
d9317fd4 | 474 | } |
7ba4fbeb VZ |
475 | |
476 | // ---------------------------------------------------------------------------- | |
477 | // wxEnhMetaFileSimpleDataObject | |
478 | // ---------------------------------------------------------------------------- | |
479 | ||
480 | size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const | |
481 | { | |
482 | // we pass data by handle and not HGLOBAL | |
483 | return 0u; | |
484 | } | |
485 | ||
486 | bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const | |
487 | { | |
cbe874bd | 488 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
7ba4fbeb VZ |
489 | |
490 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
491 | ||
492 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
493 | if ( !hEMFCopy ) | |
494 | { | |
495 | wxLogLastError(_T("CopyEnhMetaFile")); | |
496 | ||
cbe874bd | 497 | return false; |
7ba4fbeb VZ |
498 | } |
499 | ||
500 | *(HENHMETAFILE *)buf = hEMFCopy; | |
cbe874bd | 501 | return true; |
7ba4fbeb VZ |
502 | } |
503 | ||
504 | bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len), | |
505 | const void *buf) | |
506 | { | |
507 | HENHMETAFILE hEMF = *(HENHMETAFILE *)buf; | |
508 | ||
caf448e3 | 509 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
7ba4fbeb VZ |
510 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); |
511 | ||
cbe874bd | 512 | return true; |
7ba4fbeb VZ |
513 | } |
514 | ||
4ce1efe1 | 515 | |
a2327a9f | 516 | #endif // wxUSE_DRAG_AND_DROP |
d9317fd4 VZ |
517 | |
518 | #endif // wxUSE_ENH_METAFILE |