]>
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 | 211 | // ---------------------------------------------------------------------------- |
0273787a | 212 | // wxEnhMetaFileDCImpl |
d9317fd4 VZ |
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 | ||
888dde65 RR |
236 | wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner, |
237 | const wxString& filename, | |
238 | int width, int height, | |
239 | const wxString& description ) | |
0273787a | 240 | : wxMSWDCImpl( owner ) |
d9317fd4 | 241 | { |
024026be VZ |
242 | m_width = width; |
243 | m_height = height; | |
244 | ||
33ac7e6f | 245 | RECT rect; |
cbe874bd | 246 | RECT *pRect; |
d9317fd4 VZ |
247 | if ( width && height ) |
248 | { | |
249 | rect.top = | |
250 | rect.left = 0; | |
251 | rect.right = width; | |
252 | rect.bottom = height; | |
253 | ||
254 | // CreateEnhMetaFile() wants them in HIMETRIC | |
255 | PixelToHIMETRIC(&rect.right, &rect.bottom); | |
cbe874bd | 256 | |
d9317fd4 VZ |
257 | pRect = ▭ |
258 | } | |
259 | else | |
260 | { | |
261 | // GDI will try to find out the size for us (not recommended) | |
262 | pRect = (LPRECT)NULL; | |
263 | } | |
264 | ||
024026be | 265 | ScreenHDC hdcRef; |
d9317fd4 | 266 | m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename), |
e0a050e3 | 267 | pRect, description.wx_str()); |
d9317fd4 VZ |
268 | if ( !m_hDC ) |
269 | { | |
270 | wxLogLastError(_T("CreateEnhMetaFile")); | |
271 | } | |
272 | } | |
273 | ||
888dde65 | 274 | void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const |
024026be VZ |
275 | { |
276 | if ( width ) | |
277 | *width = m_width; | |
278 | if ( height ) | |
279 | *height = m_height; | |
280 | } | |
281 | ||
888dde65 | 282 | wxEnhMetaFile *wxEnhMetaFileDCImpl::Close() |
d9317fd4 | 283 | { |
888dde65 | 284 | wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") ); |
d9317fd4 VZ |
285 | |
286 | HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc()); | |
287 | if ( !hMF ) | |
288 | { | |
289 | wxLogLastError(_T("CloseEnhMetaFile")); | |
290 | ||
291 | return NULL; | |
292 | } | |
293 | ||
294 | wxEnhMetaFile *mf = new wxEnhMetaFile; | |
295 | mf->SetHENHMETAFILE((WXHANDLE)hMF); | |
296 | return mf; | |
297 | } | |
298 | ||
888dde65 | 299 | wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl() |
d9317fd4 VZ |
300 | { |
301 | // avoid freeing it in the base class dtor | |
302 | m_hDC = 0; | |
303 | } | |
304 | ||
0273787a VZ |
305 | // ---------------------------------------------------------------------------- |
306 | // wxEnhMetaFileDC | |
307 | // ---------------------------------------------------------------------------- | |
308 | ||
309 | IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC) | |
310 | ||
311 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename, | |
312 | int width, int height, | |
313 | const wxString& description) | |
314 | : wxDC(new wxEnhMetaFileDCImpl(this, | |
315 | filename, | |
316 | width, height, | |
317 | description)) | |
318 | { | |
319 | } | |
320 | ||
321 | wxEnhMetaFile *wxEnhMetaFileDC::Close() | |
322 | { | |
323 | wxEnhMetaFileDCImpl * const | |
5c33522f | 324 | impl = static_cast<wxEnhMetaFileDCImpl *>(GetImpl()); |
0273787a VZ |
325 | wxCHECK_MSG( impl, NULL, _T("no wxEnhMetaFileDC implementation") ); |
326 | ||
327 | return impl->Close(); | |
328 | } | |
329 | ||
7ba4fbeb VZ |
330 | #if wxUSE_DRAG_AND_DROP |
331 | ||
d9317fd4 VZ |
332 | // ---------------------------------------------------------------------------- |
333 | // wxEnhMetaFileDataObject | |
334 | // ---------------------------------------------------------------------------- | |
335 | ||
336 | wxDataFormat | |
337 | wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
338 | { | |
339 | return wxDF_ENHMETAFILE; | |
340 | } | |
341 | ||
342 | size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const | |
343 | { | |
344 | // wxDF_ENHMETAFILE and wxDF_METAFILE | |
345 | return 2; | |
346 | } | |
347 | ||
348 | void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats, | |
349 | Direction WXUNUSED(dir)) const | |
350 | { | |
351 | formats[0] = wxDF_ENHMETAFILE; | |
352 | formats[1] = wxDF_METAFILE; | |
353 | } | |
354 | ||
355 | size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const | |
356 | { | |
357 | if ( format == wxDF_ENHMETAFILE ) | |
358 | { | |
359 | // we pass data by handle and not HGLOBAL | |
360 | return 0u; | |
361 | } | |
362 | else | |
363 | { | |
364 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
365 | ||
366 | return sizeof(METAFILEPICT); | |
367 | } | |
368 | } | |
369 | ||
370 | bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
371 | { | |
cbe874bd | 372 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
d9317fd4 VZ |
373 | |
374 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
375 | ||
376 | if ( format == wxDF_ENHMETAFILE ) | |
377 | { | |
378 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
379 | if ( !hEMFCopy ) | |
380 | { | |
381 | wxLogLastError(_T("CopyEnhMetaFile")); | |
382 | ||
cbe874bd | 383 | return false; |
d9317fd4 VZ |
384 | } |
385 | ||
386 | *(HENHMETAFILE *)buf = hEMFCopy; | |
387 | } | |
388 | else | |
389 | { | |
390 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
391 | ||
392 | // convert to WMF | |
393 | ||
394 | ScreenHDC hdc; | |
395 | ||
396 | // first get the buffer size and alloc memory | |
397 | size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc); | |
caf448e3 | 398 | wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") ); |
d9317fd4 VZ |
399 | |
400 | BYTE *bits = (BYTE *)malloc(size); | |
401 | ||
402 | // then get the enh metafile bits | |
403 | if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) ) | |
404 | { | |
405 | wxLogLastError(_T("GetWinMetaFileBits")); | |
406 | ||
407 | free(bits); | |
408 | ||
cbe874bd | 409 | return false; |
d9317fd4 VZ |
410 | } |
411 | ||
412 | // and finally convert them to the WMF | |
413 | HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits); | |
414 | free(bits); | |
415 | if ( !hMF ) | |
416 | { | |
417 | wxLogLastError(_T("SetMetaFileBitsEx")); | |
418 | ||
cbe874bd | 419 | return false; |
d9317fd4 VZ |
420 | } |
421 | ||
422 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
423 | ||
424 | wxSize sizeMF = m_metafile.GetSize(); | |
425 | mfpict->hMF = hMF; | |
426 | mfpict->mm = MM_ANISOTROPIC; | |
427 | mfpict->xExt = sizeMF.x; | |
428 | mfpict->yExt = sizeMF.y; | |
429 | ||
430 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
431 | } | |
432 | ||
cbe874bd | 433 | return true; |
d9317fd4 VZ |
434 | } |
435 | ||
436 | bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format, | |
437 | size_t WXUNUSED(len), | |
438 | const void *buf) | |
439 | { | |
440 | HENHMETAFILE hEMF; | |
441 | ||
442 | if ( format == wxDF_ENHMETAFILE ) | |
443 | { | |
444 | hEMF = *(HENHMETAFILE *)buf; | |
445 | ||
caf448e3 | 446 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
d9317fd4 VZ |
447 | } |
448 | else | |
449 | { | |
450 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
451 | ||
452 | // convert from WMF | |
453 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
454 | ||
455 | // first get the buffer size | |
456 | size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL); | |
caf448e3 | 457 | wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") ); |
d9317fd4 VZ |
458 | |
459 | // then get metafile bits | |
460 | BYTE *bits = (BYTE *)malloc(size); | |
461 | if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) ) | |
462 | { | |
463 | wxLogLastError(_T("GetMetaFileBitsEx")); | |
464 | ||
465 | free(bits); | |
466 | ||
cbe874bd | 467 | return false; |
d9317fd4 VZ |
468 | } |
469 | ||
470 | ScreenHDC hdcRef; | |
471 | ||
472 | // and finally create an enhanced metafile from them | |
473 | hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict); | |
474 | free(bits); | |
475 | if ( !hEMF ) | |
476 | { | |
477 | wxLogLastError(_T("SetWinMetaFileBits")); | |
478 | ||
cbe874bd | 479 | return false; |
d9317fd4 VZ |
480 | } |
481 | } | |
482 | ||
483 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
484 | ||
cbe874bd | 485 | return true; |
d9317fd4 | 486 | } |
7ba4fbeb VZ |
487 | |
488 | // ---------------------------------------------------------------------------- | |
489 | // wxEnhMetaFileSimpleDataObject | |
490 | // ---------------------------------------------------------------------------- | |
491 | ||
492 | size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const | |
493 | { | |
494 | // we pass data by handle and not HGLOBAL | |
495 | return 0u; | |
496 | } | |
497 | ||
498 | bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const | |
499 | { | |
cbe874bd | 500 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
7ba4fbeb VZ |
501 | |
502 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
503 | ||
504 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
505 | if ( !hEMFCopy ) | |
506 | { | |
507 | wxLogLastError(_T("CopyEnhMetaFile")); | |
508 | ||
cbe874bd | 509 | return false; |
7ba4fbeb VZ |
510 | } |
511 | ||
512 | *(HENHMETAFILE *)buf = hEMFCopy; | |
cbe874bd | 513 | return true; |
7ba4fbeb VZ |
514 | } |
515 | ||
516 | bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len), | |
517 | const void *buf) | |
518 | { | |
519 | HENHMETAFILE hEMF = *(HENHMETAFILE *)buf; | |
520 | ||
caf448e3 | 521 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
7ba4fbeb VZ |
522 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); |
523 | ||
cbe874bd | 524 | return true; |
7ba4fbeb VZ |
525 | } |
526 | ||
4ce1efe1 | 527 | |
a2327a9f | 528 | #endif // wxUSE_DRAG_AND_DROP |
d9317fd4 VZ |
529 | |
530 | #endif // wxUSE_ENH_METAFILE |