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