]>
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 void wxEnhMetaFile::Init()
74 if ( m_filename
.empty() )
78 else // have valid file name, load metafile from it
80 m_hMF
= (WXHANDLE
)::GetEnhMetaFile(m_filename
.fn_str());
82 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
87 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
94 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
95 GetMetaFileName(m_filename
));
98 wxLogLastError(_T("CopyEnhMetaFile"));
107 void wxEnhMetaFile::Free()
111 if ( !::DeleteEnhMetaFile(GetEMF()) )
113 wxLogLastError(_T("DeleteEnhMetaFile"));
118 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
120 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
121 wxCHECK_MSG( dc
, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
126 rect
.top
= rectBound
->y
;
127 rect
.left
= rectBound
->x
;
128 rect
.right
= rectBound
->x
+ rectBound
->width
;
129 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
133 wxSize size
= GetSize();
138 rect
.bottom
= size
.y
;
141 wxDCImpl
*impl
= dc
->GetImpl();
142 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
146 if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl
), GetEMF(), &rect
) )
148 wxLogLastError(_T("PlayEnhMetaFile"));
156 wxSize
wxEnhMetaFile::GetSize() const
158 wxSize size
= wxDefaultSize
;
163 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
165 wxLogLastError(_T("GetEnhMetaFileHeader"));
169 // the width and height are in HIMETRIC (0.01mm) units, transform
171 LONG w
= hdr
.rclFrame
.right
,
172 h
= hdr
.rclFrame
.bottom
;
174 HIMETRICToPixel(&w
, &h
);
184 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
186 #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
187 wxCHECK_MSG( m_hMF
, false, _T("can't copy invalid metafile to clipboard") );
189 return wxTheClipboard
->AddData(new wxEnhMetaFileDataObject(*this));
190 #else // !wxUSE_DRAG_AND_DROP
191 wxFAIL_MSG(_T("not implemented"));
193 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 class wxEnhMetaFileDCImpl
: public wxMSWDCImpl
203 wxEnhMetaFileDCImpl( wxEnhMetaFileDC
*owner
,
204 const wxString
& filename
, int width
, int height
,
205 const wxString
& description
);
206 virtual ~wxEnhMetaFileDCImpl();
208 // obtain a pointer to the new metafile (caller should delete it)
209 wxEnhMetaFile
*Close();
212 virtual void DoGetSize(int *width
, int *height
) const;
215 // size passed to ctor and returned by DoGetSize()
221 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
223 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
224 int width
, int height
,
225 const wxString
& description
)
226 : wxDC(new wxEnhMetaFileDCImpl(this,
234 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
235 const wxString
& filename
,
236 int width
, int height
,
237 const wxString
& description
)
238 : wxMSWDCImpl( owner
)
245 if ( width
&& height
)
250 rect
.bottom
= height
;
252 // CreateEnhMetaFile() wants them in HIMETRIC
253 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
259 // GDI will try to find out the size for us (not recommended)
260 pRect
= (LPRECT
)NULL
;
264 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
265 pRect
, description
.wx_str());
268 wxLogLastError(_T("CreateEnhMetaFile"));
272 void wxEnhMetaFileDCImpl::DoGetSize(int *width
, int *height
) const
280 wxEnhMetaFile
*wxEnhMetaFileDCImpl::Close()
282 wxCHECK_MSG( IsOk(), NULL
, _T("invalid wxEnhMetaFileDC") );
284 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
287 wxLogLastError(_T("CloseEnhMetaFile"));
292 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
293 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
297 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
299 // avoid freeing it in the base class dtor
303 #if wxUSE_DRAG_AND_DROP
305 // ----------------------------------------------------------------------------
306 // wxEnhMetaFileDataObject
307 // ----------------------------------------------------------------------------
310 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
312 return wxDF_ENHMETAFILE
;
315 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
317 // wxDF_ENHMETAFILE and wxDF_METAFILE
321 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
322 Direction
WXUNUSED(dir
)) const
324 formats
[0] = wxDF_ENHMETAFILE
;
325 formats
[1] = wxDF_METAFILE
;
328 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
330 if ( format
== wxDF_ENHMETAFILE
)
332 // we pass data by handle and not HGLOBAL
337 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
339 return sizeof(METAFILEPICT
);
343 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
345 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
347 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
349 if ( format
== wxDF_ENHMETAFILE
)
351 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
354 wxLogLastError(_T("CopyEnhMetaFile"));
359 *(HENHMETAFILE
*)buf
= hEMFCopy
;
363 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
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") );
373 BYTE
*bits
= (BYTE
*)malloc(size
);
375 // then get the enh metafile bits
376 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
378 wxLogLastError(_T("GetWinMetaFileBits"));
385 // and finally convert them to the WMF
386 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
390 wxLogLastError(_T("SetMetaFileBitsEx"));
395 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
397 wxSize sizeMF
= m_metafile
.GetSize();
399 mfpict
->mm
= MM_ANISOTROPIC
;
400 mfpict
->xExt
= sizeMF
.x
;
401 mfpict
->yExt
= sizeMF
.y
;
403 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
409 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
410 size_t WXUNUSED(len
),
415 if ( format
== wxDF_ENHMETAFILE
)
417 hEMF
= *(HENHMETAFILE
*)buf
;
419 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
423 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
426 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
428 // first get the buffer size
429 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
430 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
432 // then get metafile bits
433 BYTE
*bits
= (BYTE
*)malloc(size
);
434 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
436 wxLogLastError(_T("GetMetaFileBitsEx"));
445 // and finally create an enhanced metafile from them
446 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
450 wxLogLastError(_T("SetWinMetaFileBits"));
456 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
461 // ----------------------------------------------------------------------------
462 // wxEnhMetaFileSimpleDataObject
463 // ----------------------------------------------------------------------------
465 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
467 // we pass data by handle and not HGLOBAL
471 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
473 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
475 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
477 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
480 wxLogLastError(_T("CopyEnhMetaFile"));
485 *(HENHMETAFILE
*)buf
= hEMFCopy
;
489 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
492 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
494 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
495 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
501 #endif // wxUSE_DRAG_AND_DROP
503 #endif // wxUSE_ENH_METAFILE