]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/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 licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
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" | |
32 | #include "wx/intl.h" | |
33 | #endif //WX_PRECOMP | |
34 | ||
35 | #include "wx/dc.h" | |
36 | #include "wx/msw/dc.h" | |
37 | ||
38 | #include "wx/metafile.h" | |
39 | #include "wx/clipbrd.h" | |
40 | ||
41 | #include "wx/msw/private.h" | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // wxWin macros | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject) | |
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) | |
62 | { return !fn ? (const wxChar *)NULL : (const wxChar*)fn.wx_str(); } | |
63 | ||
64 | // ============================================================================ | |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // wxEnhMetaFile | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
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 | ||
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 | { | |
95 | m_hMF = (WXHANDLE)::GetEnhMetaFile(m_filename.fn_str()); | |
96 | if ( !m_hMF ) | |
97 | wxLogSysError(_("Failed to load metafile from file \"%s\"."), | |
98 | m_filename.c_str()); | |
99 | } | |
100 | } | |
101 | ||
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 | { | |
135 | wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") ); | |
136 | wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") ); | |
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 | ||
156 | wxDCImpl *impl = dc->GetImpl(); | |
157 | wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl ); | |
158 | if (!msw_impl) | |
159 | return false; | |
160 | ||
161 | if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl), GetEMF(), &rect) ) | |
162 | { | |
163 | wxLogLastError(_T("PlayEnhMetaFile")); | |
164 | ||
165 | return false; | |
166 | } | |
167 | ||
168 | return true; | |
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 | ||
199 | bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height)) | |
200 | { | |
201 | #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD | |
202 | wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") ); | |
203 | ||
204 | return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this)); | |
205 | #else // !wxUSE_DRAG_AND_DROP | |
206 | wxFAIL_MSG(_T("not implemented")); | |
207 | return false; | |
208 | #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP | |
209 | } | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // wxEnhMetaFileDCImpl | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | class wxEnhMetaFileDCImpl : public wxMSWDCImpl | |
216 | { | |
217 | public: | |
218 | wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner, | |
219 | const wxString& filename, int width, int height, | |
220 | const wxString& description ); | |
221 | wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner, | |
222 | const wxDC& referenceDC, | |
223 | const wxString& filename, int width, int height, | |
224 | const wxString& description ); | |
225 | virtual ~wxEnhMetaFileDCImpl(); | |
226 | ||
227 | // obtain a pointer to the new metafile (caller should delete it) | |
228 | wxEnhMetaFile *Close(); | |
229 | ||
230 | protected: | |
231 | virtual void DoGetSize(int *width, int *height) const; | |
232 | ||
233 | private: | |
234 | void Create(HDC hdcRef, | |
235 | const wxString& filename, int width, int height, | |
236 | const wxString& description); | |
237 | ||
238 | // size passed to ctor and returned by DoGetSize() | |
239 | int m_width, | |
240 | m_height; | |
241 | }; | |
242 | ||
243 | ||
244 | wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner, | |
245 | const wxString& filename, | |
246 | int width, int height, | |
247 | const wxString& description ) | |
248 | : wxMSWDCImpl( owner ) | |
249 | { | |
250 | Create(ScreenHDC(), filename, width, height, description); | |
251 | } | |
252 | ||
253 | wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner, | |
254 | const wxDC& referenceDC, | |
255 | const wxString& filename, | |
256 | int width, int height, | |
257 | const wxString& description ) | |
258 | : wxMSWDCImpl( owner ) | |
259 | { | |
260 | Create(GetHdcOf(referenceDC), filename, width, height, description); | |
261 | } | |
262 | ||
263 | void wxEnhMetaFileDCImpl::Create(HDC hdcRef, | |
264 | const wxString& filename, | |
265 | int width, int height, | |
266 | const wxString& description) | |
267 | { | |
268 | m_width = width; | |
269 | m_height = height; | |
270 | ||
271 | RECT rect; | |
272 | RECT *pRect; | |
273 | if ( width && height ) | |
274 | { | |
275 | rect.top = | |
276 | rect.left = 0; | |
277 | rect.right = width; | |
278 | rect.bottom = height; | |
279 | ||
280 | // CreateEnhMetaFile() wants them in HIMETRIC | |
281 | PixelToHIMETRIC(&rect.right, &rect.bottom, hdcRef); | |
282 | ||
283 | pRect = ▭ | |
284 | } | |
285 | else | |
286 | { | |
287 | // GDI will try to find out the size for us (not recommended) | |
288 | pRect = (LPRECT)NULL; | |
289 | } | |
290 | ||
291 | m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename), | |
292 | pRect, description.wx_str()); | |
293 | if ( !m_hDC ) | |
294 | { | |
295 | wxLogLastError(_T("CreateEnhMetaFile")); | |
296 | } | |
297 | } | |
298 | ||
299 | void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const | |
300 | { | |
301 | if ( width ) | |
302 | *width = m_width; | |
303 | if ( height ) | |
304 | *height = m_height; | |
305 | } | |
306 | ||
307 | wxEnhMetaFile *wxEnhMetaFileDCImpl::Close() | |
308 | { | |
309 | wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") ); | |
310 | ||
311 | HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc()); | |
312 | if ( !hMF ) | |
313 | { | |
314 | wxLogLastError(_T("CloseEnhMetaFile")); | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
319 | wxEnhMetaFile *mf = new wxEnhMetaFile; | |
320 | mf->SetHENHMETAFILE((WXHANDLE)hMF); | |
321 | return mf; | |
322 | } | |
323 | ||
324 | wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl() | |
325 | { | |
326 | // avoid freeing it in the base class dtor | |
327 | m_hDC = 0; | |
328 | } | |
329 | ||
330 | // ---------------------------------------------------------------------------- | |
331 | // wxEnhMetaFileDC | |
332 | // ---------------------------------------------------------------------------- | |
333 | ||
334 | IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC) | |
335 | ||
336 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename, | |
337 | int width, int height, | |
338 | const wxString& description) | |
339 | : wxDC(new wxEnhMetaFileDCImpl(this, | |
340 | filename, | |
341 | width, height, | |
342 | description)) | |
343 | { | |
344 | } | |
345 | ||
346 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxDC& referenceDC, | |
347 | const wxString& filename, | |
348 | int width, int height, | |
349 | const wxString& description) | |
350 | : wxDC(new wxEnhMetaFileDCImpl(this, | |
351 | referenceDC, | |
352 | filename, | |
353 | width, height, | |
354 | description)) | |
355 | { | |
356 | } | |
357 | ||
358 | wxEnhMetaFile *wxEnhMetaFileDC::Close() | |
359 | { | |
360 | wxEnhMetaFileDCImpl * const | |
361 | impl = static_cast<wxEnhMetaFileDCImpl *>(GetImpl()); | |
362 | wxCHECK_MSG( impl, NULL, _T("no wxEnhMetaFileDC implementation") ); | |
363 | ||
364 | return impl->Close(); | |
365 | } | |
366 | ||
367 | #if wxUSE_DRAG_AND_DROP | |
368 | ||
369 | // ---------------------------------------------------------------------------- | |
370 | // wxEnhMetaFileDataObject | |
371 | // ---------------------------------------------------------------------------- | |
372 | ||
373 | wxDataFormat | |
374 | wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
375 | { | |
376 | return wxDF_ENHMETAFILE; | |
377 | } | |
378 | ||
379 | size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const | |
380 | { | |
381 | // wxDF_ENHMETAFILE and wxDF_METAFILE | |
382 | return 2; | |
383 | } | |
384 | ||
385 | void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats, | |
386 | Direction WXUNUSED(dir)) const | |
387 | { | |
388 | formats[0] = wxDF_ENHMETAFILE; | |
389 | formats[1] = wxDF_METAFILE; | |
390 | } | |
391 | ||
392 | size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const | |
393 | { | |
394 | if ( format == wxDF_ENHMETAFILE ) | |
395 | { | |
396 | // we pass data by handle and not HGLOBAL | |
397 | return 0u; | |
398 | } | |
399 | else | |
400 | { | |
401 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
402 | ||
403 | return sizeof(METAFILEPICT); | |
404 | } | |
405 | } | |
406 | ||
407 | bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
408 | { | |
409 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); | |
410 | ||
411 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
412 | ||
413 | if ( format == wxDF_ENHMETAFILE ) | |
414 | { | |
415 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
416 | if ( !hEMFCopy ) | |
417 | { | |
418 | wxLogLastError(_T("CopyEnhMetaFile")); | |
419 | ||
420 | return false; | |
421 | } | |
422 | ||
423 | *(HENHMETAFILE *)buf = hEMFCopy; | |
424 | } | |
425 | else | |
426 | { | |
427 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
428 | ||
429 | // convert to WMF | |
430 | ||
431 | ScreenHDC hdc; | |
432 | ||
433 | // first get the buffer size and alloc memory | |
434 | size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc); | |
435 | wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") ); | |
436 | ||
437 | BYTE *bits = (BYTE *)malloc(size); | |
438 | ||
439 | // then get the enh metafile bits | |
440 | if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) ) | |
441 | { | |
442 | wxLogLastError(_T("GetWinMetaFileBits")); | |
443 | ||
444 | free(bits); | |
445 | ||
446 | return false; | |
447 | } | |
448 | ||
449 | // and finally convert them to the WMF | |
450 | HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits); | |
451 | free(bits); | |
452 | if ( !hMF ) | |
453 | { | |
454 | wxLogLastError(_T("SetMetaFileBitsEx")); | |
455 | ||
456 | return false; | |
457 | } | |
458 | ||
459 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
460 | ||
461 | wxSize sizeMF = m_metafile.GetSize(); | |
462 | mfpict->hMF = hMF; | |
463 | mfpict->mm = MM_ANISOTROPIC; | |
464 | mfpict->xExt = sizeMF.x; | |
465 | mfpict->yExt = sizeMF.y; | |
466 | ||
467 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
468 | } | |
469 | ||
470 | return true; | |
471 | } | |
472 | ||
473 | bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format, | |
474 | size_t WXUNUSED(len), | |
475 | const void *buf) | |
476 | { | |
477 | HENHMETAFILE hEMF; | |
478 | ||
479 | if ( format == wxDF_ENHMETAFILE ) | |
480 | { | |
481 | hEMF = *(HENHMETAFILE *)buf; | |
482 | ||
483 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); | |
484 | } | |
485 | else | |
486 | { | |
487 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
488 | ||
489 | // convert from WMF | |
490 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
491 | ||
492 | // first get the buffer size | |
493 | size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL); | |
494 | wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") ); | |
495 | ||
496 | // then get metafile bits | |
497 | BYTE *bits = (BYTE *)malloc(size); | |
498 | if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) ) | |
499 | { | |
500 | wxLogLastError(_T("GetMetaFileBitsEx")); | |
501 | ||
502 | free(bits); | |
503 | ||
504 | return false; | |
505 | } | |
506 | ||
507 | ScreenHDC hdcRef; | |
508 | ||
509 | // and finally create an enhanced metafile from them | |
510 | hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict); | |
511 | free(bits); | |
512 | if ( !hEMF ) | |
513 | { | |
514 | wxLogLastError(_T("SetWinMetaFileBits")); | |
515 | ||
516 | return false; | |
517 | } | |
518 | } | |
519 | ||
520 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
521 | ||
522 | return true; | |
523 | } | |
524 | ||
525 | // ---------------------------------------------------------------------------- | |
526 | // wxEnhMetaFileSimpleDataObject | |
527 | // ---------------------------------------------------------------------------- | |
528 | ||
529 | size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const | |
530 | { | |
531 | // we pass data by handle and not HGLOBAL | |
532 | return 0u; | |
533 | } | |
534 | ||
535 | bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const | |
536 | { | |
537 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); | |
538 | ||
539 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
540 | ||
541 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
542 | if ( !hEMFCopy ) | |
543 | { | |
544 | wxLogLastError(_T("CopyEnhMetaFile")); | |
545 | ||
546 | return false; | |
547 | } | |
548 | ||
549 | *(HENHMETAFILE *)buf = hEMFCopy; | |
550 | return true; | |
551 | } | |
552 | ||
553 | bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len), | |
554 | const void *buf) | |
555 | { | |
556 | HENHMETAFILE hEMF = *(HENHMETAFILE *)buf; | |
557 | ||
558 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); | |
559 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
560 | ||
561 | return true; | |
562 | } | |
563 | ||
564 | ||
565 | #endif // wxUSE_DRAG_AND_DROP | |
566 | ||
567 | #endif // wxUSE_ENH_METAFILE |