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