]>
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 wxEnhMetaFileDCImpl( wxEnhMetaFileDC
*owner
,
222 const wxDC
& referenceDC
,
223 const wxString
& filename
, int width
, int height
,
224 const wxString
& description
);
225 virtual ~wxEnhMetaFileDCImpl();
227 // obtain a pointer to the new metafile (caller should delete it)
228 wxEnhMetaFile
*Close();
231 virtual void DoGetSize(int *width
, int *height
) const;
234 void Create(HDC hdcRef
,
235 const wxString
& filename
, int width
, int height
,
236 const wxString
& description
);
238 // size passed to ctor and returned by DoGetSize()
244 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
245 const wxString
& filename
,
246 int width
, int height
,
247 const wxString
& description
)
248 : wxMSWDCImpl( owner
)
250 Create(ScreenHDC(), filename
, width
, height
, description
);
253 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC
* owner
,
254 const wxDC
& referenceDC
,
255 const wxString
& filename
,
256 int width
, int height
,
257 const wxString
& description
)
258 : wxMSWDCImpl( owner
)
260 Create(GetHdcOf(referenceDC
), filename
, width
, height
, description
);
263 void wxEnhMetaFileDCImpl::Create(HDC hdcRef
,
264 const wxString
& filename
,
265 int width
, int height
,
266 const wxString
& description
)
273 if ( width
&& height
)
278 rect
.bottom
= height
;
280 // CreateEnhMetaFile() wants them in HIMETRIC
281 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
, hdcRef
);
287 // GDI will try to find out the size for us (not recommended)
288 pRect
= (LPRECT
)NULL
;
291 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
292 pRect
, description
.wx_str());
295 wxLogLastError(_T("CreateEnhMetaFile"));
299 void wxEnhMetaFileDCImpl::DoGetSize(int *width
, int *height
) const
307 wxEnhMetaFile
*wxEnhMetaFileDCImpl::Close()
309 wxCHECK_MSG( IsOk(), NULL
, _T("invalid wxEnhMetaFileDC") );
311 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
314 wxLogLastError(_T("CloseEnhMetaFile"));
319 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
320 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
324 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
326 // avoid freeing it in the base class dtor
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
336 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
337 int width
, int height
,
338 const wxString
& description
)
339 : wxDC(new wxEnhMetaFileDCImpl(this,
346 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxDC
& referenceDC
,
347 const wxString
& filename
,
348 int width
, int height
,
349 const wxString
& description
)
350 : wxDC(new wxEnhMetaFileDCImpl(this,
358 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
360 wxEnhMetaFileDCImpl
* const
361 impl
= static_cast<wxEnhMetaFileDCImpl
*>(GetImpl());
362 wxCHECK_MSG( impl
, NULL
, _T("no wxEnhMetaFileDC implementation") );
364 return impl
->Close();
367 #if wxUSE_DRAG_AND_DROP
369 // ----------------------------------------------------------------------------
370 // wxEnhMetaFileDataObject
371 // ----------------------------------------------------------------------------
374 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
376 return wxDF_ENHMETAFILE
;
379 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
381 // wxDF_ENHMETAFILE and wxDF_METAFILE
385 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
386 Direction
WXUNUSED(dir
)) const
388 formats
[0] = wxDF_ENHMETAFILE
;
389 formats
[1] = wxDF_METAFILE
;
392 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
394 if ( format
== wxDF_ENHMETAFILE
)
396 // we pass data by handle and not HGLOBAL
401 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
403 return sizeof(METAFILEPICT
);
407 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
409 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
411 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
413 if ( format
== wxDF_ENHMETAFILE
)
415 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
418 wxLogLastError(_T("CopyEnhMetaFile"));
423 *(HENHMETAFILE
*)buf
= hEMFCopy
;
427 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
433 // first get the buffer size and alloc memory
434 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
435 wxCHECK_MSG( size
, false, _T("GetWinMetaFileBits() failed") );
437 BYTE
*bits
= (BYTE
*)malloc(size
);
439 // then get the enh metafile bits
440 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
442 wxLogLastError(_T("GetWinMetaFileBits"));
449 // and finally convert them to the WMF
450 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
454 wxLogLastError(_T("SetMetaFileBitsEx"));
459 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
461 wxSize sizeMF
= m_metafile
.GetSize();
463 mfpict
->mm
= MM_ANISOTROPIC
;
464 mfpict
->xExt
= sizeMF
.x
;
465 mfpict
->yExt
= sizeMF
.y
;
467 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
473 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
474 size_t WXUNUSED(len
),
479 if ( format
== wxDF_ENHMETAFILE
)
481 hEMF
= *(HENHMETAFILE
*)buf
;
483 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
487 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
490 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
492 // first get the buffer size
493 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
494 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
496 // then get metafile bits
497 BYTE
*bits
= (BYTE
*)malloc(size
);
498 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
500 wxLogLastError(_T("GetMetaFileBitsEx"));
509 // and finally create an enhanced metafile from them
510 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
514 wxLogLastError(_T("SetWinMetaFileBits"));
520 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
525 // ----------------------------------------------------------------------------
526 // wxEnhMetaFileSimpleDataObject
527 // ----------------------------------------------------------------------------
529 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
531 // we pass data by handle and not HGLOBAL
535 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
537 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
539 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
541 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
544 wxLogLastError(_T("CopyEnhMetaFile"));
549 *(HENHMETAFILE
*)buf
= hEMFCopy
;
553 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
556 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
558 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
559 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
565 #endif // wxUSE_DRAG_AND_DROP
567 #endif // wxUSE_ENH_METAFILE