]>
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 // ----------------------------------------------------------------------------
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 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
238 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
239 int width
, int height
,
240 const wxString
& description
)
241 : wxDC(new wxEnhMetaFileDCImpl(this,
249 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
250 const wxString
& filename
,
251 int width
, int height
,
252 const wxString
& description
)
253 : wxMSWDCImpl( owner
)
260 if ( width
&& height
)
265 rect
.bottom
= height
;
267 // CreateEnhMetaFile() wants them in HIMETRIC
268 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
274 // GDI will try to find out the size for us (not recommended)
275 pRect
= (LPRECT
)NULL
;
279 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
280 pRect
, description
.wx_str());
283 wxLogLastError(_T("CreateEnhMetaFile"));
287 void wxEnhMetaFileDCImpl::DoGetSize(int *width
, int *height
) const
295 wxEnhMetaFile
*wxEnhMetaFileDCImpl::Close()
297 wxCHECK_MSG( IsOk(), NULL
, _T("invalid wxEnhMetaFileDC") );
299 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
302 wxLogLastError(_T("CloseEnhMetaFile"));
307 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
308 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
312 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
314 // avoid freeing it in the base class dtor
318 #if wxUSE_DRAG_AND_DROP
320 // ----------------------------------------------------------------------------
321 // wxEnhMetaFileDataObject
322 // ----------------------------------------------------------------------------
325 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
327 return wxDF_ENHMETAFILE
;
330 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
332 // wxDF_ENHMETAFILE and wxDF_METAFILE
336 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
337 Direction
WXUNUSED(dir
)) const
339 formats
[0] = wxDF_ENHMETAFILE
;
340 formats
[1] = wxDF_METAFILE
;
343 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
345 if ( format
== wxDF_ENHMETAFILE
)
347 // we pass data by handle and not HGLOBAL
352 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
354 return sizeof(METAFILEPICT
);
358 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
360 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
362 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
364 if ( format
== wxDF_ENHMETAFILE
)
366 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
369 wxLogLastError(_T("CopyEnhMetaFile"));
374 *(HENHMETAFILE
*)buf
= hEMFCopy
;
378 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
384 // first get the buffer size and alloc memory
385 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
386 wxCHECK_MSG( size
, false, _T("GetWinMetaFileBits() failed") );
388 BYTE
*bits
= (BYTE
*)malloc(size
);
390 // then get the enh metafile bits
391 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
393 wxLogLastError(_T("GetWinMetaFileBits"));
400 // and finally convert them to the WMF
401 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
405 wxLogLastError(_T("SetMetaFileBitsEx"));
410 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
412 wxSize sizeMF
= m_metafile
.GetSize();
414 mfpict
->mm
= MM_ANISOTROPIC
;
415 mfpict
->xExt
= sizeMF
.x
;
416 mfpict
->yExt
= sizeMF
.y
;
418 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
424 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
425 size_t WXUNUSED(len
),
430 if ( format
== wxDF_ENHMETAFILE
)
432 hEMF
= *(HENHMETAFILE
*)buf
;
434 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
438 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
441 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
443 // first get the buffer size
444 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
445 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
447 // then get metafile bits
448 BYTE
*bits
= (BYTE
*)malloc(size
);
449 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
451 wxLogLastError(_T("GetMetaFileBitsEx"));
460 // and finally create an enhanced metafile from them
461 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
465 wxLogLastError(_T("SetWinMetaFileBits"));
471 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
476 // ----------------------------------------------------------------------------
477 // wxEnhMetaFileSimpleDataObject
478 // ----------------------------------------------------------------------------
480 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
482 // we pass data by handle and not HGLOBAL
486 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
488 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
490 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
492 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
495 wxLogLastError(_T("CopyEnhMetaFile"));
500 *(HENHMETAFILE
*)buf
= hEMFCopy
;
504 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
507 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
509 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
510 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
516 #endif // wxUSE_DRAG_AND_DROP
518 #endif // wxUSE_ENH_METAFILE