]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/enhmeta.cpp
3 // Purpose: implementation of wxEnhMetaFileXXX classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_ENH_METAFILE
30 #include "wx/string.h"
36 #include "wx/msw/dc.h"
38 #include "wx/metafile.h"
39 #include "wx/clipbrd.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile
, wxObject
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define GetEMF() ((HENHMETAFILE)m_hMF)
54 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
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(); }
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxGDIRefData
*wxEnhMetaFile::CreateGDIRefData() const
74 wxFAIL_MSG( _T("must be implemented if used") );
80 wxEnhMetaFile::CloneGDIRefData(const wxGDIRefData
*WXUNUSED(data
)) const
82 wxFAIL_MSG( _T("must be implemented if used") );
87 void wxEnhMetaFile::Init()
89 if ( m_filename
.empty() )
93 else // have valid file name, load metafile from it
95 m_hMF
= (WXHANDLE
)::GetEnhMetaFile(m_filename
.fn_str());
97 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
102 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
109 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
110 GetMetaFileName(m_filename
));
113 wxLogLastError(_T("CopyEnhMetaFile"));
122 void wxEnhMetaFile::Free()
126 if ( !::DeleteEnhMetaFile(GetEMF()) )
128 wxLogLastError(_T("DeleteEnhMetaFile"));
133 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
135 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
136 wxCHECK_MSG( dc
, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
141 rect
.top
= rectBound
->y
;
142 rect
.left
= rectBound
->x
;
143 rect
.right
= rectBound
->x
+ rectBound
->width
;
144 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
148 wxSize size
= GetSize();
153 rect
.bottom
= size
.y
;
156 wxDCImpl
*impl
= dc
->GetImpl();
157 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
161 if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl
), GetEMF(), &rect
) )
163 wxLogLastError(_T("PlayEnhMetaFile"));
171 wxSize
wxEnhMetaFile::GetSize() const
173 wxSize size
= wxDefaultSize
;
178 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
180 wxLogLastError(_T("GetEnhMetaFileHeader"));
184 // the width and height are in HIMETRIC (0.01mm) units, transform
186 LONG w
= hdr
.rclFrame
.right
,
187 h
= hdr
.rclFrame
.bottom
;
189 HIMETRICToPixel(&w
, &h
);
199 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
201 #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
202 wxCHECK_MSG( m_hMF
, false, _T("can't copy invalid metafile to clipboard") );
204 return wxTheClipboard
->AddData(new wxEnhMetaFileDataObject(*this));
205 #else // !wxUSE_DRAG_AND_DROP
206 wxFAIL_MSG(_T("not implemented"));
208 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
211 // ----------------------------------------------------------------------------
212 // wxEnhMetaFileDCImpl
213 // ----------------------------------------------------------------------------
215 class wxEnhMetaFileDCImpl
: public wxMSWDCImpl
218 wxEnhMetaFileDCImpl( wxEnhMetaFileDC
*owner
,
219 const wxString
& filename
, int width
, int height
,
220 const wxString
& description
);
221 virtual ~wxEnhMetaFileDCImpl();
223 // obtain a pointer to the new metafile (caller should delete it)
224 wxEnhMetaFile
*Close();
227 virtual void DoGetSize(int *width
, int *height
) const;
230 // size passed to ctor and returned by DoGetSize()
236 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
237 const wxString
& filename
,
238 int width
, int height
,
239 const wxString
& description
)
240 : wxMSWDCImpl( owner
)
247 if ( width
&& height
)
252 rect
.bottom
= height
;
254 // CreateEnhMetaFile() wants them in HIMETRIC
255 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
261 // GDI will try to find out the size for us (not recommended)
262 pRect
= (LPRECT
)NULL
;
266 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
267 pRect
, description
.wx_str());
270 wxLogLastError(_T("CreateEnhMetaFile"));
274 void wxEnhMetaFileDCImpl::DoGetSize(int *width
, int *height
) const
282 wxEnhMetaFile
*wxEnhMetaFileDCImpl::Close()
284 wxCHECK_MSG( IsOk(), NULL
, _T("invalid wxEnhMetaFileDC") );
286 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
289 wxLogLastError(_T("CloseEnhMetaFile"));
294 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
295 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
299 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
301 // avoid freeing it in the base class dtor
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
311 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
312 int width
, int height
,
313 const wxString
& description
)
314 : wxDC(new wxEnhMetaFileDCImpl(this,
321 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
323 wxEnhMetaFileDCImpl
* const
324 impl
= wx_static_cast(wxEnhMetaFileDCImpl
*, GetImpl());
325 wxCHECK_MSG( impl
, NULL
, _T("no wxEnhMetaFileDC implementation") );
327 return impl
->Close();
330 #if wxUSE_DRAG_AND_DROP
332 // ----------------------------------------------------------------------------
333 // wxEnhMetaFileDataObject
334 // ----------------------------------------------------------------------------
337 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
339 return wxDF_ENHMETAFILE
;
342 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
344 // wxDF_ENHMETAFILE and wxDF_METAFILE
348 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
349 Direction
WXUNUSED(dir
)) const
351 formats
[0] = wxDF_ENHMETAFILE
;
352 formats
[1] = wxDF_METAFILE
;
355 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
357 if ( format
== wxDF_ENHMETAFILE
)
359 // we pass data by handle and not HGLOBAL
364 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
366 return sizeof(METAFILEPICT
);
370 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
372 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
374 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
376 if ( format
== wxDF_ENHMETAFILE
)
378 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
381 wxLogLastError(_T("CopyEnhMetaFile"));
386 *(HENHMETAFILE
*)buf
= hEMFCopy
;
390 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
396 // first get the buffer size and alloc memory
397 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
398 wxCHECK_MSG( size
, false, _T("GetWinMetaFileBits() failed") );
400 BYTE
*bits
= (BYTE
*)malloc(size
);
402 // then get the enh metafile bits
403 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
405 wxLogLastError(_T("GetWinMetaFileBits"));
412 // and finally convert them to the WMF
413 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
417 wxLogLastError(_T("SetMetaFileBitsEx"));
422 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
424 wxSize sizeMF
= m_metafile
.GetSize();
426 mfpict
->mm
= MM_ANISOTROPIC
;
427 mfpict
->xExt
= sizeMF
.x
;
428 mfpict
->yExt
= sizeMF
.y
;
430 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
436 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
437 size_t WXUNUSED(len
),
442 if ( format
== wxDF_ENHMETAFILE
)
444 hEMF
= *(HENHMETAFILE
*)buf
;
446 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
450 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
453 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
455 // first get the buffer size
456 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
457 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
459 // then get metafile bits
460 BYTE
*bits
= (BYTE
*)malloc(size
);
461 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
463 wxLogLastError(_T("GetMetaFileBitsEx"));
472 // and finally create an enhanced metafile from them
473 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
477 wxLogLastError(_T("SetWinMetaFileBits"));
483 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
488 // ----------------------------------------------------------------------------
489 // wxEnhMetaFileSimpleDataObject
490 // ----------------------------------------------------------------------------
492 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
494 // we pass data by handle and not HGLOBAL
498 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
500 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
502 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
504 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
507 wxLogLastError(_T("CopyEnhMetaFile"));
512 *(HENHMETAFILE
*)buf
= hEMFCopy
;
516 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
519 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
521 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
522 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
528 #endif // wxUSE_DRAG_AND_DROP
530 #endif // wxUSE_ENH_METAFILE