]>
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> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "enhmeta.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #if wxUSE_ENH_METAFILE | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/string.h" | |
35 | #include "wx/log.h" | |
36 | #endif //WX_PRECOMP | |
37 | ||
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) | |
48 | IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC) | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // macros | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | #define GetEMF() ((HENHMETAFILE)m_hMF) | |
55 | #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF)) | |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // private functions | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // we must pass NULL if the string is empty to metafile functions | |
62 | static inline const wxChar *GetMetaFileName(const wxString& fn) | |
63 | { return !fn ? (wxChar *)NULL : fn.c_str(); } | |
64 | ||
65 | // ============================================================================ | |
66 | // implementation | |
67 | // ============================================================================ | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // wxEnhMetaFile | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf) | |
74 | { | |
75 | if ( &mf == this ) | |
76 | return; | |
77 | ||
78 | if ( mf.m_hMF ) | |
79 | { | |
80 | m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf), | |
81 | GetMetaFileName(m_filename)); | |
82 | if ( !m_hMF ) | |
83 | { | |
84 | wxLogLastError(_T("CopyEnhMetaFile")); | |
85 | } | |
86 | } | |
87 | else | |
88 | { | |
89 | m_hMF = 0; | |
90 | } | |
91 | } | |
92 | ||
93 | void wxEnhMetaFile::Free() | |
94 | { | |
95 | if ( m_hMF ) | |
96 | { | |
97 | if ( !::DeleteEnhMetaFile(GetEMF()) ) | |
98 | { | |
99 | wxLogLastError(_T("DeleteEnhMetaFile")); | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound) | |
105 | { | |
106 | wxCHECK_MSG( Ok(), FALSE, _T("can't play invalid enhanced metafile") ); | |
107 | wxCHECK_MSG( dc, FALSE, _T("invalid wxDC in wxEnhMetaFile::Play") ); | |
108 | ||
109 | RECT rect; | |
110 | if ( rectBound ) | |
111 | { | |
112 | rect.top = rectBound->y; | |
113 | rect.left = rectBound->x; | |
114 | rect.right = rectBound->x + rectBound->width; | |
115 | rect.bottom = rectBound->y + rectBound->height; | |
116 | } | |
117 | else | |
118 | { | |
119 | wxSize size = GetSize(); | |
120 | ||
121 | rect.top = | |
122 | rect.left = 0; | |
123 | rect.right = size.x; | |
124 | rect.bottom = size.y; | |
125 | } | |
126 | ||
127 | if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) ) | |
128 | { | |
129 | wxLogLastError(_T("PlayEnhMetaFile")); | |
130 | ||
131 | return FALSE; | |
132 | } | |
133 | ||
134 | return TRUE; | |
135 | } | |
136 | ||
137 | wxSize wxEnhMetaFile::GetSize() const | |
138 | { | |
139 | wxSize size = wxDefaultSize; | |
140 | ||
141 | if ( Ok() ) | |
142 | { | |
143 | ENHMETAHEADER hdr; | |
144 | if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) ) | |
145 | { | |
146 | wxLogLastError(_T("GetEnhMetaFileHeader")); | |
147 | } | |
148 | else | |
149 | { | |
150 | // the width and height are in HIMETRIC (0.01mm) units, transform | |
151 | // them to pixels | |
152 | LONG w = hdr.rclFrame.right, | |
153 | h = hdr.rclFrame.bottom; | |
154 | ||
155 | HIMETRICToPixel(&w, &h); | |
156 | ||
157 | size.x = w; | |
158 | size.y = h; | |
159 | } | |
160 | } | |
161 | ||
162 | return size; | |
163 | } | |
164 | ||
bfbd6dc1 VZ |
165 | bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height)) |
166 | { | |
a95e38c0 | 167 | #if wxUSE_DRAG_AND_DROP |
bfbd6dc1 VZ |
168 | wxCHECK_MSG( m_hMF, FALSE, _T("can't copy invalid metafile to clipboard") ); |
169 | ||
170 | return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this)); | |
a95e38c0 VZ |
171 | #else // !wxUSE_DRAG_AND_DROP |
172 | wxFAIL_MSG(_T("not implemented")); | |
173 | #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP | |
bfbd6dc1 VZ |
174 | } |
175 | ||
d9317fd4 VZ |
176 | // ---------------------------------------------------------------------------- |
177 | // wxEnhMetaFileDC | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
180 | wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename, | |
181 | int width, int height, | |
182 | const wxString& description) | |
183 | { | |
184 | ScreenHDC hdcRef; | |
185 | ||
186 | RECT rect, *pRect; | |
187 | if ( width && height ) | |
188 | { | |
189 | rect.top = | |
190 | rect.left = 0; | |
191 | rect.right = width; | |
192 | rect.bottom = height; | |
193 | ||
194 | // CreateEnhMetaFile() wants them in HIMETRIC | |
195 | PixelToHIMETRIC(&rect.right, &rect.bottom); | |
196 | ||
197 | pRect = ▭ | |
198 | } | |
199 | else | |
200 | { | |
201 | // GDI will try to find out the size for us (not recommended) | |
202 | pRect = (LPRECT)NULL; | |
203 | } | |
204 | ||
205 | m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename), | |
206 | pRect, description); | |
207 | if ( !m_hDC ) | |
208 | { | |
209 | wxLogLastError(_T("CreateEnhMetaFile")); | |
210 | } | |
211 | } | |
212 | ||
213 | wxEnhMetaFile *wxEnhMetaFileDC::Close() | |
214 | { | |
215 | wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") ); | |
216 | ||
217 | HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc()); | |
218 | if ( !hMF ) | |
219 | { | |
220 | wxLogLastError(_T("CloseEnhMetaFile")); | |
221 | ||
222 | return NULL; | |
223 | } | |
224 | ||
225 | wxEnhMetaFile *mf = new wxEnhMetaFile; | |
226 | mf->SetHENHMETAFILE((WXHANDLE)hMF); | |
227 | return mf; | |
228 | } | |
229 | ||
230 | wxEnhMetaFileDC::~wxEnhMetaFileDC() | |
231 | { | |
232 | // avoid freeing it in the base class dtor | |
233 | m_hDC = 0; | |
234 | } | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // wxEnhMetaFileDataObject | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
a2327a9f JS |
240 | #if wxUSE_DRAG_AND_DROP |
241 | ||
d9317fd4 VZ |
242 | wxDataFormat |
243 | wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
244 | { | |
245 | return wxDF_ENHMETAFILE; | |
246 | } | |
247 | ||
248 | size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const | |
249 | { | |
250 | // wxDF_ENHMETAFILE and wxDF_METAFILE | |
251 | return 2; | |
252 | } | |
253 | ||
254 | void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats, | |
255 | Direction WXUNUSED(dir)) const | |
256 | { | |
257 | formats[0] = wxDF_ENHMETAFILE; | |
258 | formats[1] = wxDF_METAFILE; | |
259 | } | |
260 | ||
261 | size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const | |
262 | { | |
263 | if ( format == wxDF_ENHMETAFILE ) | |
264 | { | |
265 | // we pass data by handle and not HGLOBAL | |
266 | return 0u; | |
267 | } | |
268 | else | |
269 | { | |
270 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
271 | ||
272 | return sizeof(METAFILEPICT); | |
273 | } | |
274 | } | |
275 | ||
276 | bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
277 | { | |
278 | wxCHECK_MSG( m_metafile.Ok(), FALSE, _T("copying invalid enh metafile") ); | |
279 | ||
280 | HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE(); | |
281 | ||
282 | if ( format == wxDF_ENHMETAFILE ) | |
283 | { | |
284 | HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL); | |
285 | if ( !hEMFCopy ) | |
286 | { | |
287 | wxLogLastError(_T("CopyEnhMetaFile")); | |
288 | ||
289 | return FALSE; | |
290 | } | |
291 | ||
292 | *(HENHMETAFILE *)buf = hEMFCopy; | |
293 | } | |
294 | else | |
295 | { | |
296 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
297 | ||
298 | // convert to WMF | |
299 | ||
300 | ScreenHDC hdc; | |
301 | ||
302 | // first get the buffer size and alloc memory | |
303 | size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc); | |
304 | wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") ); | |
305 | ||
306 | BYTE *bits = (BYTE *)malloc(size); | |
307 | ||
308 | // then get the enh metafile bits | |
309 | if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) ) | |
310 | { | |
311 | wxLogLastError(_T("GetWinMetaFileBits")); | |
312 | ||
313 | free(bits); | |
314 | ||
315 | return FALSE; | |
316 | } | |
317 | ||
318 | // and finally convert them to the WMF | |
319 | HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits); | |
320 | free(bits); | |
321 | if ( !hMF ) | |
322 | { | |
323 | wxLogLastError(_T("SetMetaFileBitsEx")); | |
324 | ||
325 | return FALSE; | |
326 | } | |
327 | ||
328 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
329 | ||
330 | wxSize sizeMF = m_metafile.GetSize(); | |
331 | mfpict->hMF = hMF; | |
332 | mfpict->mm = MM_ANISOTROPIC; | |
333 | mfpict->xExt = sizeMF.x; | |
334 | mfpict->yExt = sizeMF.y; | |
335 | ||
336 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
337 | } | |
338 | ||
339 | return TRUE; | |
340 | } | |
341 | ||
342 | bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format, | |
343 | size_t WXUNUSED(len), | |
344 | const void *buf) | |
345 | { | |
346 | HENHMETAFILE hEMF; | |
347 | ||
348 | if ( format == wxDF_ENHMETAFILE ) | |
349 | { | |
350 | hEMF = *(HENHMETAFILE *)buf; | |
351 | ||
352 | wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") ); | |
353 | } | |
354 | else | |
355 | { | |
356 | wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") ); | |
357 | ||
358 | // convert from WMF | |
359 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
360 | ||
361 | // first get the buffer size | |
362 | size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL); | |
363 | wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") ); | |
364 | ||
365 | // then get metafile bits | |
366 | BYTE *bits = (BYTE *)malloc(size); | |
367 | if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) ) | |
368 | { | |
369 | wxLogLastError(_T("GetMetaFileBitsEx")); | |
370 | ||
371 | free(bits); | |
372 | ||
373 | return FALSE; | |
374 | } | |
375 | ||
376 | ScreenHDC hdcRef; | |
377 | ||
378 | // and finally create an enhanced metafile from them | |
379 | hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict); | |
380 | free(bits); | |
381 | if ( !hEMF ) | |
382 | { | |
383 | wxLogLastError(_T("SetWinMetaFileBits")); | |
384 | ||
385 | return FALSE; | |
386 | } | |
387 | } | |
388 | ||
389 | m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF); | |
390 | ||
391 | return TRUE; | |
392 | } | |
a2327a9f | 393 | #endif // wxUSE_DRAG_AND_DROP |
d9317fd4 VZ |
394 | |
395 | #endif // wxUSE_ENH_METAFILE |