]> git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
Docstring fixes
[wxWidgets.git] / src / msw / enhmeta.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/intl.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/metafile.h"
40 #include "wx/clipbrd.h"
41
42 #include "wx/msw/private.h"
43
44 // ----------------------------------------------------------------------------
45 // wxWin macros
46 // ----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
49 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
50
51 // ----------------------------------------------------------------------------
52 // macros
53 // ----------------------------------------------------------------------------
54
55 #define GetEMF() ((HENHMETAFILE)m_hMF)
56 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
57
58 // ----------------------------------------------------------------------------
59 // private functions
60 // ----------------------------------------------------------------------------
61
62 // we must pass NULL if the string is empty to metafile functions
63 static inline const wxChar *GetMetaFileName(const wxString& fn)
64 { return !fn ? (wxChar *)NULL : fn.c_str(); }
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // wxEnhMetaFile
72 // ----------------------------------------------------------------------------
73
74 void wxEnhMetaFile::Init()
75 {
76 if ( m_filename.empty() )
77 {
78 m_hMF = 0;
79 }
80 else // have valid file name, load metafile from it
81 {
82 m_hMF = GetEnhMetaFile(m_filename);
83 if ( !m_hMF )
84 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
85 m_filename.c_str());
86 }
87 }
88
89 void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
90 {
91 if ( &mf == this )
92 return;
93
94 if ( mf.m_hMF )
95 {
96 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
97 GetMetaFileName(m_filename));
98 if ( !m_hMF )
99 {
100 wxLogLastError(_T("CopyEnhMetaFile"));
101 }
102 }
103 else
104 {
105 m_hMF = 0;
106 }
107 }
108
109 void wxEnhMetaFile::Free()
110 {
111 if ( m_hMF )
112 {
113 if ( !::DeleteEnhMetaFile(GetEMF()) )
114 {
115 wxLogLastError(_T("DeleteEnhMetaFile"));
116 }
117 }
118 }
119
120 bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
121 {
122 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
123 wxCHECK_MSG( dc, FALSE, _T("invalid wxDC in wxEnhMetaFile::Play") );
124
125 RECT rect;
126 if ( rectBound )
127 {
128 rect.top = rectBound->y;
129 rect.left = rectBound->x;
130 rect.right = rectBound->x + rectBound->width;
131 rect.bottom = rectBound->y + rectBound->height;
132 }
133 else
134 {
135 wxSize size = GetSize();
136
137 rect.top =
138 rect.left = 0;
139 rect.right = size.x;
140 rect.bottom = size.y;
141 }
142
143 if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) )
144 {
145 wxLogLastError(_T("PlayEnhMetaFile"));
146
147 return false;
148 }
149
150 return true;
151 }
152
153 wxSize wxEnhMetaFile::GetSize() const
154 {
155 wxSize size = wxDefaultSize;
156
157 if ( Ok() )
158 {
159 ENHMETAHEADER hdr;
160 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
161 {
162 wxLogLastError(_T("GetEnhMetaFileHeader"));
163 }
164 else
165 {
166 // the width and height are in HIMETRIC (0.01mm) units, transform
167 // them to pixels
168 LONG w = hdr.rclFrame.right,
169 h = hdr.rclFrame.bottom;
170
171 HIMETRICToPixel(&w, &h);
172
173 size.x = w;
174 size.y = h;
175 }
176 }
177
178 return size;
179 }
180
181 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
182 {
183 #if wxUSE_DRAG_AND_DROP
184 wxCHECK_MSG( m_hMF, FALSE, _T("can't copy invalid metafile to clipboard") );
185
186 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
187 #else // !wxUSE_DRAG_AND_DROP
188 wxFAIL_MSG(_T("not implemented"));
189 return false;
190 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxEnhMetaFileDC
195 // ----------------------------------------------------------------------------
196
197 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
198 int width, int height,
199 const wxString& description)
200 {
201 ScreenHDC hdcRef;
202 RECT rect;
203 RECT *pRect;
204 if ( width && height )
205 {
206 rect.top =
207 rect.left = 0;
208 rect.right = width;
209 rect.bottom = height;
210
211 // CreateEnhMetaFile() wants them in HIMETRIC
212 PixelToHIMETRIC(&rect.right, &rect.bottom);
213
214 pRect = &rect;
215 }
216 else
217 {
218 // GDI will try to find out the size for us (not recommended)
219 pRect = (LPRECT)NULL;
220 }
221
222 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
223 pRect, description);
224 if ( !m_hDC )
225 {
226 wxLogLastError(_T("CreateEnhMetaFile"));
227 }
228 }
229
230 wxEnhMetaFile *wxEnhMetaFileDC::Close()
231 {
232 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
233
234 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
235 if ( !hMF )
236 {
237 wxLogLastError(_T("CloseEnhMetaFile"));
238
239 return NULL;
240 }
241
242 wxEnhMetaFile *mf = new wxEnhMetaFile;
243 mf->SetHENHMETAFILE((WXHANDLE)hMF);
244 return mf;
245 }
246
247 wxEnhMetaFileDC::~wxEnhMetaFileDC()
248 {
249 // avoid freeing it in the base class dtor
250 m_hDC = 0;
251 }
252
253 #if wxUSE_DRAG_AND_DROP
254
255 // ----------------------------------------------------------------------------
256 // wxEnhMetaFileDataObject
257 // ----------------------------------------------------------------------------
258
259 wxDataFormat
260 wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
261 {
262 return wxDF_ENHMETAFILE;
263 }
264
265 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
266 {
267 // wxDF_ENHMETAFILE and wxDF_METAFILE
268 return 2;
269 }
270
271 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
272 Direction WXUNUSED(dir)) const
273 {
274 formats[0] = wxDF_ENHMETAFILE;
275 formats[1] = wxDF_METAFILE;
276 }
277
278 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
279 {
280 if ( format == wxDF_ENHMETAFILE )
281 {
282 // we pass data by handle and not HGLOBAL
283 return 0u;
284 }
285 else
286 {
287 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
288
289 return sizeof(METAFILEPICT);
290 }
291 }
292
293 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
294 {
295 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
296
297 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
298
299 if ( format == wxDF_ENHMETAFILE )
300 {
301 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
302 if ( !hEMFCopy )
303 {
304 wxLogLastError(_T("CopyEnhMetaFile"));
305
306 return false;
307 }
308
309 *(HENHMETAFILE *)buf = hEMFCopy;
310 }
311 else
312 {
313 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
314
315 // convert to WMF
316
317 ScreenHDC hdc;
318
319 // first get the buffer size and alloc memory
320 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
321 wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") );
322
323 BYTE *bits = (BYTE *)malloc(size);
324
325 // then get the enh metafile bits
326 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
327 {
328 wxLogLastError(_T("GetWinMetaFileBits"));
329
330 free(bits);
331
332 return false;
333 }
334
335 // and finally convert them to the WMF
336 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
337 free(bits);
338 if ( !hMF )
339 {
340 wxLogLastError(_T("SetMetaFileBitsEx"));
341
342 return false;
343 }
344
345 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
346
347 wxSize sizeMF = m_metafile.GetSize();
348 mfpict->hMF = hMF;
349 mfpict->mm = MM_ANISOTROPIC;
350 mfpict->xExt = sizeMF.x;
351 mfpict->yExt = sizeMF.y;
352
353 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
354 }
355
356 return true;
357 }
358
359 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
360 size_t WXUNUSED(len),
361 const void *buf)
362 {
363 HENHMETAFILE hEMF;
364
365 if ( format == wxDF_ENHMETAFILE )
366 {
367 hEMF = *(HENHMETAFILE *)buf;
368
369 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
370 }
371 else
372 {
373 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
374
375 // convert from WMF
376 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
377
378 // first get the buffer size
379 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
380 wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") );
381
382 // then get metafile bits
383 BYTE *bits = (BYTE *)malloc(size);
384 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
385 {
386 wxLogLastError(_T("GetMetaFileBitsEx"));
387
388 free(bits);
389
390 return false;
391 }
392
393 ScreenHDC hdcRef;
394
395 // and finally create an enhanced metafile from them
396 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
397 free(bits);
398 if ( !hEMF )
399 {
400 wxLogLastError(_T("SetWinMetaFileBits"));
401
402 return false;
403 }
404 }
405
406 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
407
408 return true;
409 }
410
411 // ----------------------------------------------------------------------------
412 // wxEnhMetaFileSimpleDataObject
413 // ----------------------------------------------------------------------------
414
415 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
416 {
417 // we pass data by handle and not HGLOBAL
418 return 0u;
419 }
420
421 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
422 {
423 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
424
425 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
426
427 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
428 if ( !hEMFCopy )
429 {
430 wxLogLastError(_T("CopyEnhMetaFile"));
431
432 return false;
433 }
434
435 *(HENHMETAFILE *)buf = hEMFCopy;
436 return true;
437 }
438
439 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
440 const void *buf)
441 {
442 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
443
444 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
445 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
446
447 return true;
448 }
449
450
451 #endif // wxUSE_DRAG_AND_DROP
452
453 #endif // wxUSE_ENH_METAFILE