]>
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"
39 #include "wx/clipbrd.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile
, wxObject
)
48 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC
, wxDC
)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #define GetEMF() ((HENHMETAFILE)m_hMF)
55 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // we must pass NULL if the string is empty to metafile functions
62 static inline const wxChar
*GetMetaFileName(const wxString
& fn
)
63 { return !fn
? (wxChar
*)NULL
: fn
.c_str(); }
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
80 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
81 GetMetaFileName(m_filename
));
84 wxLogLastError(_T("CopyEnhMetaFile"));
93 void wxEnhMetaFile::Free()
97 if ( !::DeleteEnhMetaFile(GetEMF()) )
99 wxLogLastError(_T("DeleteEnhMetaFile"));
104 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
106 wxCHECK_MSG( Ok(), FALSE
, _T("can't play invalid enhanced metafile") );
107 wxCHECK_MSG( dc
, FALSE
, _T("invalid wxDC in wxEnhMetaFile::Play") );
112 rect
.top
= rectBound
->y
;
113 rect
.left
= rectBound
->x
;
114 rect
.right
= rectBound
->x
+ rectBound
->width
;
115 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
119 wxSize size
= GetSize();
124 rect
.bottom
= size
.y
;
127 if ( !::PlayEnhMetaFile(GetHdcOf(*dc
), GetEMF(), &rect
) )
129 wxLogLastError(_T("PlayEnhMetaFile"));
137 wxSize
wxEnhMetaFile::GetSize() const
139 wxSize size
= wxDefaultSize
;
144 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
146 wxLogLastError(_T("GetEnhMetaFileHeader"));
150 // the width and height are in HIMETRIC (0.01mm) units, transform
152 LONG w
= hdr
.rclFrame
.right
,
153 h
= hdr
.rclFrame
.bottom
;
155 HIMETRICToPixel(&w
, &h
);
165 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
167 #if wxUSE_DRAG_AND_DROP
168 wxCHECK_MSG( m_hMF
, FALSE
, _T("can't copy invalid metafile to clipboard") );
170 return wxTheClipboard
->AddData(new wxEnhMetaFileDataObject(*this));
171 #else // !wxUSE_DRAG_AND_DROP
172 wxFAIL_MSG(_T("not implemented"));
174 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
182 int width
, int height
,
183 const wxString
& description
)
188 if ( width
&& height
)
193 rect
.bottom
= height
;
195 // CreateEnhMetaFile() wants them in HIMETRIC
198 PixelToHIMETRIC(&r
, &b
);
202 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
209 // GDI will try to find out the size for us (not recommended)
210 pRect
= (LPRECT
)NULL
;
213 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
217 wxLogLastError(_T("CreateEnhMetaFile"));
221 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
223 wxCHECK_MSG( Ok(), NULL
, _T("invalid wxEnhMetaFileDC") );
225 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
228 wxLogLastError(_T("CloseEnhMetaFile"));
233 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
234 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
238 wxEnhMetaFileDC::~wxEnhMetaFileDC()
240 // avoid freeing it in the base class dtor
244 #if wxUSE_DRAG_AND_DROP
246 // ----------------------------------------------------------------------------
247 // wxEnhMetaFileDataObject
248 // ----------------------------------------------------------------------------
251 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
253 return wxDF_ENHMETAFILE
;
256 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
258 // wxDF_ENHMETAFILE and wxDF_METAFILE
262 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
263 Direction
WXUNUSED(dir
)) const
265 formats
[0] = wxDF_ENHMETAFILE
;
266 formats
[1] = wxDF_METAFILE
;
269 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
271 if ( format
== wxDF_ENHMETAFILE
)
273 // we pass data by handle and not HGLOBAL
278 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
280 return sizeof(METAFILEPICT
);
284 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
286 wxCHECK_MSG( m_metafile
.Ok(), FALSE
, _T("copying invalid enh metafile") );
288 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
290 if ( format
== wxDF_ENHMETAFILE
)
292 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
295 wxLogLastError(_T("CopyEnhMetaFile"));
300 *(HENHMETAFILE
*)buf
= hEMFCopy
;
305 wxFAIL_MSG( _T("unsupported format") );
308 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
314 // first get the buffer size and alloc memory
315 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
316 wxCHECK_MSG( size
, FALSE
, _T("GetWinMetaFileBits() failed") );
318 BYTE
*bits
= (BYTE
*)malloc(size
);
320 // then get the enh metafile bits
321 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
323 wxLogLastError(_T("GetWinMetaFileBits"));
330 // and finally convert them to the WMF
331 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
335 wxLogLastError(_T("SetMetaFileBitsEx"));
340 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
342 wxSize sizeMF
= m_metafile
.GetSize();
344 mfpict
->mm
= MM_ANISOTROPIC
;
345 mfpict
->xExt
= sizeMF
.x
;
346 mfpict
->yExt
= sizeMF
.y
;
348 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
);
446 #endif // wxUSE_DRAG_AND_DROP
448 #endif // wxUSE_ENH_METAFILE