]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
35 #include "wx/metafile.h"
36 #include "wx/clipbrd.h"
38 #include "wx/msw/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile
, wxObject
)
45 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #define GetEMF() ((HENHMETAFILE)m_hMF)
52 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // we must pass NULL if the string is empty to metafile functions
59 static inline const wxChar
*GetMetaFileName(const wxString
& fn
)
60 { return !fn
? (wxChar
*)NULL
: fn
.c_str(); }
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 void wxEnhMetaFile::Init()
72 if ( m_filename
.empty() )
76 else // have valid file name, load metafile from it
78 m_hMF
= GetEnhMetaFile(m_filename
);
80 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
85 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
92 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
93 GetMetaFileName(m_filename
));
96 wxLogLastError(_T("CopyEnhMetaFile"));
105 void wxEnhMetaFile::Free()
109 if ( !::DeleteEnhMetaFile(GetEMF()) )
111 wxLogLastError(_T("DeleteEnhMetaFile"));
116 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
118 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
119 wxCHECK_MSG( dc
, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
124 rect
.top
= rectBound
->y
;
125 rect
.left
= rectBound
->x
;
126 rect
.right
= rectBound
->x
+ rectBound
->width
;
127 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
131 wxSize size
= GetSize();
136 rect
.bottom
= size
.y
;
139 if ( !::PlayEnhMetaFile(GetHdcOf(*dc
), GetEMF(), &rect
) )
141 wxLogLastError(_T("PlayEnhMetaFile"));
149 wxSize
wxEnhMetaFile::GetSize() const
151 wxSize size
= wxDefaultSize
;
156 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
158 wxLogLastError(_T("GetEnhMetaFileHeader"));
162 // the width and height are in HIMETRIC (0.01mm) units, transform
164 LONG w
= hdr
.rclFrame
.right
,
165 h
= hdr
.rclFrame
.bottom
;
167 HIMETRICToPixel(&w
, &h
);
177 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
179 #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
180 wxCHECK_MSG( m_hMF
, false, _T("can't copy invalid metafile to clipboard") );
182 return wxTheClipboard
->AddData(new wxEnhMetaFileDataObject(*this));
183 #else // !wxUSE_DRAG_AND_DROP
184 wxFAIL_MSG(_T("not implemented"));
186 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
194 int width
, int height
,
195 const wxString
& description
)
200 if ( width
&& height
)
205 rect
.bottom
= height
;
207 // CreateEnhMetaFile() wants them in HIMETRIC
208 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
214 // GDI will try to find out the size for us (not recommended)
215 pRect
= (LPRECT
)NULL
;
218 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
222 wxLogLastError(_T("CreateEnhMetaFile"));
226 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
228 wxCHECK_MSG( Ok(), NULL
, _T("invalid wxEnhMetaFileDC") );
230 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
233 wxLogLastError(_T("CloseEnhMetaFile"));
238 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
239 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
243 wxEnhMetaFileDC::~wxEnhMetaFileDC()
245 // avoid freeing it in the base class dtor
249 #if wxUSE_DRAG_AND_DROP
251 // ----------------------------------------------------------------------------
252 // wxEnhMetaFileDataObject
253 // ----------------------------------------------------------------------------
256 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
258 return wxDF_ENHMETAFILE
;
261 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
263 // wxDF_ENHMETAFILE and wxDF_METAFILE
267 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
268 Direction
WXUNUSED(dir
)) const
270 formats
[0] = wxDF_ENHMETAFILE
;
271 formats
[1] = wxDF_METAFILE
;
274 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
276 if ( format
== wxDF_ENHMETAFILE
)
278 // we pass data by handle and not HGLOBAL
283 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
285 return sizeof(METAFILEPICT
);
289 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
291 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
293 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
295 if ( format
== wxDF_ENHMETAFILE
)
297 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
300 wxLogLastError(_T("CopyEnhMetaFile"));
305 *(HENHMETAFILE
*)buf
= hEMFCopy
;
309 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
315 // first get the buffer size and alloc memory
316 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
317 wxCHECK_MSG( size
, false, _T("GetWinMetaFileBits() failed") );
319 BYTE
*bits
= (BYTE
*)malloc(size
);
321 // then get the enh metafile bits
322 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
324 wxLogLastError(_T("GetWinMetaFileBits"));
331 // and finally convert them to the WMF
332 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
336 wxLogLastError(_T("SetMetaFileBitsEx"));
341 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
343 wxSize sizeMF
= m_metafile
.GetSize();
345 mfpict
->mm
= MM_ANISOTROPIC
;
346 mfpict
->xExt
= sizeMF
.x
;
347 mfpict
->yExt
= sizeMF
.y
;
349 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
355 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
356 size_t WXUNUSED(len
),
361 if ( format
== wxDF_ENHMETAFILE
)
363 hEMF
= *(HENHMETAFILE
*)buf
;
365 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
369 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
372 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
374 // first get the buffer size
375 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
376 wxCHECK_MSG( size
, false, _T("GetMetaFileBitsEx() failed") );
378 // then get metafile bits
379 BYTE
*bits
= (BYTE
*)malloc(size
);
380 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
382 wxLogLastError(_T("GetMetaFileBitsEx"));
391 // and finally create an enhanced metafile from them
392 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
396 wxLogLastError(_T("SetWinMetaFileBits"));
402 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
407 // ----------------------------------------------------------------------------
408 // wxEnhMetaFileSimpleDataObject
409 // ----------------------------------------------------------------------------
411 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
413 // we pass data by handle and not HGLOBAL
417 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
419 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
421 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
423 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
426 wxLogLastError(_T("CopyEnhMetaFile"));
431 *(HENHMETAFILE
*)buf
= hEMFCopy
;
435 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
438 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
440 wxCHECK_MSG( hEMF
, false, _T("pasting invalid enh metafile") );
441 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
447 #endif // wxUSE_DRAG_AND_DROP
449 #endif // wxUSE_ENH_METAFILE