]>
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
)
227 m_pimpl
= new wxEnhMetaFileDCImpl( this, filename
, width
, height
, description
);
231 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
232 const wxString
& filename
,
233 int width
, int height
,
234 const wxString
& description
)
235 : wxMSWDCImpl( owner
)
242 if ( width
&& height
)
247 rect
.bottom
= height
;
249 // CreateEnhMetaFile() wants them in HIMETRIC
250 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
256 // GDI will try to find out the size for us (not recommended)
257 pRect
= (LPRECT
)NULL
;
261 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
262 pRect
, description
.wx_str());
265 wxLogLastError(_T("CreateEnhMetaFile"));
269 void wxEnhMetaFileDCImpl::DoGetSize(int *width
, int *height
) const
277 wxEnhMetaFile
*wxEnhMetaFileDCImpl::Close()
279 wxCHECK_MSG( IsOk(), NULL
, _T("invalid wxEnhMetaFileDC") );
281 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
284 wxLogLastError(_T("CloseEnhMetaFile"));
289 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
290 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
294 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
296 // avoid freeing it in the base class dtor
300 #if wxUSE_DRAG_AND_DROP
302 // ----------------------------------------------------------------------------
303 // wxEnhMetaFileDataObject
304 // ----------------------------------------------------------------------------
307 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
309 return wxDF_ENHMETAFILE
;
312 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
314 // wxDF_ENHMETAFILE and wxDF_METAFILE
318 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
319 Direction
WXUNUSED(dir
)) const
321 formats
[0] = wxDF_ENHMETAFILE
;
322 formats
[1] = wxDF_METAFILE
;
325 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
327 if ( format
== wxDF_ENHMETAFILE
)
329 // we pass data by handle and not HGLOBAL
334 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
336 return sizeof(METAFILEPICT
);
340 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
342 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
344 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
346 if ( format
== wxDF_ENHMETAFILE
)
348 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
351 wxLogLastError(_T("CopyEnhMetaFile"));
356 *(HENHMETAFILE
*)buf
= hEMFCopy
;
360 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
366 // first get the buffer size and alloc memory
367 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
368 wxCHECK_MSG( size
, false, _T("GetWinMetaFileBits() failed") );
370 BYTE
*bits
= (BYTE
*)malloc(size
);
372 // then get the enh metafile bits
373 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
375 wxLogLastError(_T("GetWinMetaFileBits"));
382 // and finally convert them to the WMF
383 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
387 wxLogLastError(_T("SetMetaFileBitsEx"));
392 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
394 wxSize sizeMF
= m_metafile
.GetSize();
396 mfpict
->mm
= MM_ANISOTROPIC
;
397 mfpict
->xExt
= sizeMF
.x
;
398 mfpict
->yExt
= sizeMF
.y
;
400 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
406 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
407 size_t WXUNUSED(len
),
412 if ( format
== wxDF_ENHMETAFILE
)
414 hEMF
= *(HENHMETAFILE
*)buf
;
416 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
420 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
423 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
425 // first get the buffer size
426 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
427 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
429 // then get metafile bits
430 BYTE
*bits
= (BYTE
*)malloc(size
);
431 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
433 wxLogLastError(_T("GetMetaFileBitsEx"));
442 // and finally create an enhanced metafile from them
443 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
447 wxLogLastError(_T("SetWinMetaFileBits"));
453 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
458 // ----------------------------------------------------------------------------
459 // wxEnhMetaFileSimpleDataObject
460 // ----------------------------------------------------------------------------
462 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
464 // we pass data by handle and not HGLOBAL
468 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
470 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
472 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
474 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
477 wxLogLastError(_T("CopyEnhMetaFile"));
482 *(HENHMETAFILE
*)buf
= hEMFCopy
;
486 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
489 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
491 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
492 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
498 #endif // wxUSE_DRAG_AND_DROP
500 #endif // wxUSE_ENH_METAFILE