]> git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[wxWidgets.git] / src / msw / enhmeta.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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/dc.h"
36 #include "wx/msw/dc.h"
37
38 #include "wx/metafile.h"
39 #include "wx/clipbrd.h"
40
41 #include "wx/msw/private.h"
42
43 // ----------------------------------------------------------------------------
44 // wxWin macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
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)
62 { return !fn ? (const wxChar *)NULL : (const wxChar*)fn.wx_str(); }
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // wxEnhMetaFile
70 // ----------------------------------------------------------------------------
71
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 {
80 m_hMF = (WXHANDLE)::GetEnhMetaFile(m_filename.fn_str());
81 if ( !m_hMF )
82 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
83 m_filename.c_str());
84 }
85 }
86
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 {
120 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
121 wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
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
141 wxDCImpl *impl = dc->GetImpl();
142 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
143 if (!msw_impl)
144 return false;
145
146 if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl), GetEMF(), &rect) )
147 {
148 wxLogLastError(_T("PlayEnhMetaFile"));
149
150 return false;
151 }
152
153 return true;
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
184 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
185 {
186 #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
187 wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") );
188
189 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
190 #else // !wxUSE_DRAG_AND_DROP
191 wxFAIL_MSG(_T("not implemented"));
192 return false;
193 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
194 }
195
196 // ----------------------------------------------------------------------------
197 // wxEnhMetaFileDC
198 // ----------------------------------------------------------------------------
199
200 class wxEnhMetaFileDCImpl : public wxMSWDCImpl
201 {
202 public:
203 wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner,
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
223 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
224 int width, int height,
225 const wxString& description)
226 : wxDC(new wxEnhMetaFileDCImpl(this,
227 filename,
228 width, height,
229 description))
230 {
231 }
232
233
234 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner,
235 const wxString& filename,
236 int width, int height,
237 const wxString& description )
238 : wxMSWDCImpl( owner )
239 {
240 m_width = width;
241 m_height = height;
242
243 RECT rect;
244 RECT *pRect;
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);
254
255 pRect = &rect;
256 }
257 else
258 {
259 // GDI will try to find out the size for us (not recommended)
260 pRect = (LPRECT)NULL;
261 }
262
263 ScreenHDC hdcRef;
264 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
265 pRect, description.wx_str());
266 if ( !m_hDC )
267 {
268 wxLogLastError(_T("CreateEnhMetaFile"));
269 }
270 }
271
272 void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const
273 {
274 if ( width )
275 *width = m_width;
276 if ( height )
277 *height = m_height;
278 }
279
280 wxEnhMetaFile *wxEnhMetaFileDCImpl::Close()
281 {
282 wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") );
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
297 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
298 {
299 // avoid freeing it in the base class dtor
300 m_hDC = 0;
301 }
302
303 #if wxUSE_DRAG_AND_DROP
304
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 {
345 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
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
356 return false;
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);
371 wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") );
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
382 return false;
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
392 return false;
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
406 return true;
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
419 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
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);
430 wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") );
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
440 return false;
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
452 return false;
453 }
454 }
455
456 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
457
458 return true;
459 }
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 {
473 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
474
475 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
476
477 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
478 if ( !hEMFCopy )
479 {
480 wxLogLastError(_T("CopyEnhMetaFile"));
481
482 return false;
483 }
484
485 *(HENHMETAFILE *)buf = hEMFCopy;
486 return true;
487 }
488
489 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
490 const void *buf)
491 {
492 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
493
494 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
495 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
496
497 return true;
498 }
499
500
501 #endif // wxUSE_DRAG_AND_DROP
502
503 #endif // wxUSE_ENH_METAFILE