]>
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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "enhmeta.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_ENH_METAFILE
34 #include "wx/string.h"
38 #include "wx/metafile.h"
40 #include "wx/msw/private.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile
, wxObject
)
47 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
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
? (wxChar
*)NULL
: fn
.c_str(); }
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
79 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
80 GetMetaFileName(m_filename
));
83 wxLogLastError(_T("CopyEnhMetaFile"));
92 void wxEnhMetaFile::Free()
96 if ( !::DeleteEnhMetaFile(GetEMF()) )
98 wxLogLastError(_T("DeleteEnhMetaFile"));
103 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
105 wxCHECK_MSG( Ok(), FALSE
, _T("can't play invalid enhanced metafile") );
106 wxCHECK_MSG( dc
, FALSE
, _T("invalid wxDC in wxEnhMetaFile::Play") );
111 rect
.top
= rectBound
->y
;
112 rect
.left
= rectBound
->x
;
113 rect
.right
= rectBound
->x
+ rectBound
->width
;
114 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
118 wxSize size
= GetSize();
123 rect
.bottom
= size
.y
;
126 if ( !::PlayEnhMetaFile(GetHdcOf(*dc
), GetEMF(), &rect
) )
128 wxLogLastError(_T("PlayEnhMetaFile"));
136 wxSize
wxEnhMetaFile::GetSize() const
138 wxSize size
= wxDefaultSize
;
143 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
145 wxLogLastError(_T("GetEnhMetaFileHeader"));
149 // the width and height are in HIMETRIC (0.01mm) units, transform
151 LONG w
= hdr
.rclFrame
.right
,
152 h
= hdr
.rclFrame
.bottom
;
154 HIMETRICToPixel(&w
, &h
);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
169 int width
, int height
,
170 const wxString
& description
)
175 if ( width
&& height
)
180 rect
.bottom
= height
;
182 // CreateEnhMetaFile() wants them in HIMETRIC
183 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
189 // GDI will try to find out the size for us (not recommended)
190 pRect
= (LPRECT
)NULL
;
193 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
197 wxLogLastError(_T("CreateEnhMetaFile"));
201 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
203 wxCHECK_MSG( Ok(), NULL
, _T("invalid wxEnhMetaFileDC") );
205 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
208 wxLogLastError(_T("CloseEnhMetaFile"));
213 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
214 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
218 wxEnhMetaFileDC::~wxEnhMetaFileDC()
220 // avoid freeing it in the base class dtor
224 // ----------------------------------------------------------------------------
225 // wxEnhMetaFileDataObject
226 // ----------------------------------------------------------------------------
229 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
231 return wxDF_ENHMETAFILE
;
234 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
236 // wxDF_ENHMETAFILE and wxDF_METAFILE
240 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
241 Direction
WXUNUSED(dir
)) const
243 formats
[0] = wxDF_ENHMETAFILE
;
244 formats
[1] = wxDF_METAFILE
;
247 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
249 if ( format
== wxDF_ENHMETAFILE
)
251 // we pass data by handle and not HGLOBAL
256 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
258 return sizeof(METAFILEPICT
);
262 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
264 wxCHECK_MSG( m_metafile
.Ok(), FALSE
, _T("copying invalid enh metafile") );
266 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
268 if ( format
== wxDF_ENHMETAFILE
)
270 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
273 wxLogLastError(_T("CopyEnhMetaFile"));
278 *(HENHMETAFILE
*)buf
= hEMFCopy
;
282 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
288 // first get the buffer size and alloc memory
289 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
290 wxCHECK_MSG( size
, FALSE
, _T("GetWinMetaFileBits() failed") );
292 BYTE
*bits
= (BYTE
*)malloc(size
);
294 // then get the enh metafile bits
295 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
297 wxLogLastError(_T("GetWinMetaFileBits"));
304 // and finally convert them to the WMF
305 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
309 wxLogLastError(_T("SetMetaFileBitsEx"));
314 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
316 wxSize sizeMF
= m_metafile
.GetSize();
318 mfpict
->mm
= MM_ANISOTROPIC
;
319 mfpict
->xExt
= sizeMF
.x
;
320 mfpict
->yExt
= sizeMF
.y
;
322 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
328 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
329 size_t WXUNUSED(len
),
334 if ( format
== wxDF_ENHMETAFILE
)
336 hEMF
= *(HENHMETAFILE
*)buf
;
338 wxCHECK_MSG( hEMF
, FALSE
, _T("pasting invalid enh metafile") );
342 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
345 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
347 // first get the buffer size
348 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
349 wxCHECK_MSG( size
, FALSE
, _T("GetMetaFileBitsEx() failed") );
351 // then get metafile bits
352 BYTE
*bits
= (BYTE
*)malloc(size
);
353 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
355 wxLogLastError(_T("GetMetaFileBitsEx"));
364 // and finally create an enhanced metafile from them
365 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
369 wxLogLastError(_T("SetWinMetaFileBits"));
375 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
380 #endif // wxUSE_ENH_METAFILE