]>
Commit | Line | Data |
---|---|---|
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 | ||
44 | IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject) | |
45 | IMPLEMENT_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 | |
59 | static 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 |
70 | void 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 |
85 | void 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 | ||
105 | void wxEnhMetaFile::Free() | |
106 | { | |
107 | if ( m_hMF ) | |
108 | { | |
109 | if ( !::DeleteEnhMetaFile(GetEMF()) ) | |
110 | { | |
111 | wxLogLastError(_T("DeleteEnhMetaFile")); | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | bool 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 | ||
149 | wxSize 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 |
177 | bool 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 | ||
193 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename, | |
194 | int width, int height, | |
195 | const wxString& description) | |
196 | { | |
024026be VZ |
197 | m_width = width; |
198 | m_height = height; | |
199 | ||
33ac7e6f | 200 | RECT rect; |
cbe874bd | 201 | RECT *pRect; |
d9317fd4 VZ |
202 | if ( width && height ) |
203 | { | |
204 | rect.top = | |
205 | rect.left = 0; | |
206 | rect.right = width; | |
207 | rect.bottom = height; | |
208 | ||
209 | // CreateEnhMetaFile() wants them in HIMETRIC | |
210 | PixelToHIMETRIC(&rect.right, &rect.bottom); | |
cbe874bd | 211 | |
d9317fd4 VZ |
212 | pRect = ▭ |
213 | } | |
214 | else | |
215 | { | |
216 | // GDI will try to find out the size for us (not recommended) | |
217 | pRect = (LPRECT)NULL; | |
218 | } | |
219 | ||
024026be | 220 | ScreenHDC hdcRef; |
d9317fd4 VZ |
221 | m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename), |
222 | pRect, description); | |
223 | if ( !m_hDC ) | |
224 | { | |
225 | wxLogLastError(_T("CreateEnhMetaFile")); | |
226 | } | |
227 | } | |
228 | ||
024026be VZ |
229 | void wxEnhMetaFileDC::DoGetSize(int *width, int *height) const |
230 | { | |
231 | if ( width ) | |
232 | *width = m_width; | |
233 | if ( height ) | |
234 | *height = m_height; | |
235 | } | |
236 | ||
d9317fd4 VZ |
237 | wxEnhMetaFile *wxEnhMetaFileDC::Close() |
238 | { | |
239 | wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") ); | |
240 | ||
241 | HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc()); | |
242 | if ( !hMF ) | |
243 | { | |
244 | wxLogLastError(_T("CloseEnhMetaFile")); | |
245 | ||
246 | return NULL; | |
247 | } | |
248 | ||
249 | wxEnhMetaFile *mf = new wxEnhMetaFile; | |
250 | mf->SetHENHMETAFILE((WXHANDLE)hMF); | |
251 | return mf; | |
252 | } | |
253 | ||
254 | wxEnhMetaFileDC::~wxEnhMetaFileDC() | |
255 | { | |
256 | // avoid freeing it in the base class dtor | |
257 | m_hDC = 0; | |
258 | } | |
259 | ||
7ba4fbeb VZ |
260 | #if wxUSE_DRAG_AND_DROP |
261 | ||
d9317fd4 VZ |
262 | // ---------------------------------------------------------------------------- |
263 | // wxEnhMetaFileDataObject | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | wxDataFormat | |
267 | wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
268 | { | |
269 | return wxDF_ENHMETAFILE; | |
270 | } | |
271 | ||
272 | size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const | |
273 | { | |
274 | // wxDF_ENHMETAFILE and wxDF_METAFILE | |
275 | return 2; | |
276 | } | |
277 | ||
278 | void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats, | |
279 | Direction WXUNUSED(dir)) const | |
280 | { | |
281 | formats[0] = wxDF_ENHMETAFILE; | |
282 | formats[1] = wxDF_METAFILE; | |
283 | } | |
284 | ||
285 | size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const | |
286 | { | |
287 | if ( format == wxDF_ENHMETAFILE ) | |
288 | { | |
289 | // we pass data by handle and not HGLOBAL | |
290 | return 0u; | |
291 | } | |
292 | else | |
293 | { | |
294 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
295 | ||
296 | return sizeof(METAFILEPICT); | |
297 | } | |
298 | } | |
299 | ||
300 | bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
301 | { | |
cbe874bd | 302 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
d9317fd4 VZ |
303 | |
304 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
305 | ||
306 | if ( format == wxDF_ENHMETAFILE ) | |
307 | { | |
308 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
309 | if ( !hEMFCopy ) | |
310 | { | |
311 | wxLogLastError(_T("CopyEnhMetaFile")); | |
312 | ||
cbe874bd | 313 | return false; |
d9317fd4 VZ |
314 | } |
315 | ||
316 | *(HENHMETAFILE *)buf = hEMFCopy; | |
317 | } | |
318 | else | |
319 | { | |
320 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
321 | ||
322 | // convert to WMF | |
323 | ||
324 | ScreenHDC hdc; | |
325 | ||
326 | // first get the buffer size and alloc memory | |
327 | size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc); | |
caf448e3 | 328 | wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") ); |
d9317fd4 VZ |
329 | |
330 | BYTE *bits = (BYTE *)malloc(size); | |
331 | ||
332 | // then get the enh metafile bits | |
333 | if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) ) | |
334 | { | |
335 | wxLogLastError(_T("GetWinMetaFileBits")); | |
336 | ||
337 | free(bits); | |
338 | ||
cbe874bd | 339 | return false; |
d9317fd4 VZ |
340 | } |
341 | ||
342 | // and finally convert them to the WMF | |
343 | HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits); | |
344 | free(bits); | |
345 | if ( !hMF ) | |
346 | { | |
347 | wxLogLastError(_T("SetMetaFileBitsEx")); | |
348 | ||
cbe874bd | 349 | return false; |
d9317fd4 VZ |
350 | } |
351 | ||
352 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
353 | ||
354 | wxSize sizeMF = m_metafile.GetSize(); | |
355 | mfpict->hMF = hMF; | |
356 | mfpict->mm = MM_ANISOTROPIC; | |
357 | mfpict->xExt = sizeMF.x; | |
358 | mfpict->yExt = sizeMF.y; | |
359 | ||
360 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
361 | } | |
362 | ||
cbe874bd | 363 | return true; |
d9317fd4 VZ |
364 | } |
365 | ||
366 | bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format, | |
367 | size_t WXUNUSED(len), | |
368 | const void *buf) | |
369 | { | |
370 | HENHMETAFILE hEMF; | |
371 | ||
372 | if ( format == wxDF_ENHMETAFILE ) | |
373 | { | |
374 | hEMF = *(HENHMETAFILE *)buf; | |
375 | ||
caf448e3 | 376 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
d9317fd4 VZ |
377 | } |
378 | else | |
379 | { | |
380 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
381 | ||
382 | // convert from WMF | |
383 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
384 | ||
385 | // first get the buffer size | |
386 | size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL); | |
caf448e3 | 387 | wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") ); |
d9317fd4 VZ |
388 | |
389 | // then get metafile bits | |
390 | BYTE *bits = (BYTE *)malloc(size); | |
391 | if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) ) | |
392 | { | |
393 | wxLogLastError(_T("GetMetaFileBitsEx")); | |
394 | ||
395 | free(bits); | |
396 | ||
cbe874bd | 397 | return false; |
d9317fd4 VZ |
398 | } |
399 | ||
400 | ScreenHDC hdcRef; | |
401 | ||
402 | // and finally create an enhanced metafile from them | |
403 | hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict); | |
404 | free(bits); | |
405 | if ( !hEMF ) | |
406 | { | |
407 | wxLogLastError(_T("SetWinMetaFileBits")); | |
408 | ||
cbe874bd | 409 | return false; |
d9317fd4 VZ |
410 | } |
411 | } | |
412 | ||
413 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
414 | ||
cbe874bd | 415 | return true; |
d9317fd4 | 416 | } |
7ba4fbeb VZ |
417 | |
418 | // ---------------------------------------------------------------------------- | |
419 | // wxEnhMetaFileSimpleDataObject | |
420 | // ---------------------------------------------------------------------------- | |
421 | ||
422 | size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const | |
423 | { | |
424 | // we pass data by handle and not HGLOBAL | |
425 | return 0u; | |
426 | } | |
427 | ||
428 | bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const | |
429 | { | |
cbe874bd | 430 | wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") ); |
7ba4fbeb VZ |
431 | |
432 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
433 | ||
434 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
435 | if ( !hEMFCopy ) | |
436 | { | |
437 | wxLogLastError(_T("CopyEnhMetaFile")); | |
438 | ||
cbe874bd | 439 | return false; |
7ba4fbeb VZ |
440 | } |
441 | ||
442 | *(HENHMETAFILE *)buf = hEMFCopy; | |
cbe874bd | 443 | return true; |
7ba4fbeb VZ |
444 | } |
445 | ||
446 | bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len), | |
447 | const void *buf) | |
448 | { | |
449 | HENHMETAFILE hEMF = *(HENHMETAFILE *)buf; | |
450 | ||
caf448e3 | 451 | wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") ); |
7ba4fbeb VZ |
452 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); |
453 | ||
cbe874bd | 454 | return true; |
7ba4fbeb VZ |
455 | } |
456 | ||
4ce1efe1 | 457 | |
a2327a9f | 458 | #endif // wxUSE_DRAG_AND_DROP |
d9317fd4 VZ |
459 | |
460 | #endif // wxUSE_ENH_METAFILE |