]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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::Init()
75 if ( m_filename
.empty() )
79 else // have valid file name, load metafile from it
81 m_hMF
= GetEnhMetaFile(m_filename
);
83 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
88 void wxEnhMetaFile::Assign(const wxEnhMetaFile
& mf
)
95 m_hMF
= (WXHANDLE
)::CopyEnhMetaFile(GetEMFOf(mf
),
96 GetMetaFileName(m_filename
));
99 wxLogLastError(_T("CopyEnhMetaFile"));
108 void wxEnhMetaFile::Free()
112 if ( !::DeleteEnhMetaFile(GetEMF()) )
114 wxLogLastError(_T("DeleteEnhMetaFile"));
119 bool wxEnhMetaFile::Play(wxDC
*dc
, wxRect
*rectBound
)
121 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
122 wxCHECK_MSG( dc
, FALSE
, _T("invalid wxDC in wxEnhMetaFile::Play") );
127 rect
.top
= rectBound
->y
;
128 rect
.left
= rectBound
->x
;
129 rect
.right
= rectBound
->x
+ rectBound
->width
;
130 rect
.bottom
= rectBound
->y
+ rectBound
->height
;
134 wxSize size
= GetSize();
139 rect
.bottom
= size
.y
;
142 if ( !::PlayEnhMetaFile(GetHdcOf(*dc
), GetEMF(), &rect
) )
144 wxLogLastError(_T("PlayEnhMetaFile"));
152 wxSize
wxEnhMetaFile::GetSize() const
154 wxSize size
= wxDefaultSize
;
159 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr
), &hdr
) )
161 wxLogLastError(_T("GetEnhMetaFileHeader"));
165 // the width and height are in HIMETRIC (0.01mm) units, transform
167 LONG w
= hdr
.rclFrame
.right
,
168 h
= hdr
.rclFrame
.bottom
;
170 HIMETRICToPixel(&w
, &h
);
180 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
182 #if wxUSE_DRAG_AND_DROP
183 wxCHECK_MSG( m_hMF
, FALSE
, _T("can't copy invalid metafile to clipboard") );
185 return wxTheClipboard
->AddData(new wxEnhMetaFileDataObject(*this));
186 #else // !wxUSE_DRAG_AND_DROP
187 wxFAIL_MSG(_T("not implemented"));
189 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString
& filename
,
197 int width
, int height
,
198 const wxString
& description
)
203 if ( width
&& height
)
208 rect
.bottom
= height
;
210 // CreateEnhMetaFile() wants them in HIMETRIC
211 PixelToHIMETRIC(&rect
.right
, &rect
.bottom
);
217 // GDI will try to find out the size for us (not recommended)
218 pRect
= (LPRECT
)NULL
;
221 m_hDC
= (WXHDC
)::CreateEnhMetaFile(hdcRef
, GetMetaFileName(filename
),
225 wxLogLastError(_T("CreateEnhMetaFile"));
229 wxEnhMetaFile
*wxEnhMetaFileDC::Close()
231 wxCHECK_MSG( Ok(), NULL
, _T("invalid wxEnhMetaFileDC") );
233 HENHMETAFILE hMF
= ::CloseEnhMetaFile(GetHdc());
236 wxLogLastError(_T("CloseEnhMetaFile"));
241 wxEnhMetaFile
*mf
= new wxEnhMetaFile
;
242 mf
->SetHENHMETAFILE((WXHANDLE
)hMF
);
246 wxEnhMetaFileDC::~wxEnhMetaFileDC()
248 // avoid freeing it in the base class dtor
252 #if wxUSE_DRAG_AND_DROP
254 // ----------------------------------------------------------------------------
255 // wxEnhMetaFileDataObject
256 // ----------------------------------------------------------------------------
259 wxEnhMetaFileDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
261 return wxDF_ENHMETAFILE
;
264 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction
WXUNUSED(dir
)) const
266 // wxDF_ENHMETAFILE and wxDF_METAFILE
270 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat
*formats
,
271 Direction
WXUNUSED(dir
)) const
273 formats
[0] = wxDF_ENHMETAFILE
;
274 formats
[1] = wxDF_METAFILE
;
277 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat
& format
) const
279 if ( format
== wxDF_ENHMETAFILE
)
281 // we pass data by handle and not HGLOBAL
286 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
288 return sizeof(METAFILEPICT
);
292 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
294 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
296 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
298 if ( format
== wxDF_ENHMETAFILE
)
300 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
303 wxLogLastError(_T("CopyEnhMetaFile"));
308 *(HENHMETAFILE
*)buf
= hEMFCopy
;
312 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
318 // first get the buffer size and alloc memory
319 size_t size
= ::GetWinMetaFileBits(hEMF
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
320 wxCHECK_MSG( size
, FALSE
, _T("GetWinMetaFileBits() failed") );
322 BYTE
*bits
= (BYTE
*)malloc(size
);
324 // then get the enh metafile bits
325 if ( !::GetWinMetaFileBits(hEMF
, size
, bits
, MM_ANISOTROPIC
, hdc
) )
327 wxLogLastError(_T("GetWinMetaFileBits"));
334 // and finally convert them to the WMF
335 HMETAFILE hMF
= ::SetMetaFileBitsEx(size
, bits
);
339 wxLogLastError(_T("SetMetaFileBitsEx"));
344 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
346 wxSize sizeMF
= m_metafile
.GetSize();
348 mfpict
->mm
= MM_ANISOTROPIC
;
349 mfpict
->xExt
= sizeMF
.x
;
350 mfpict
->yExt
= sizeMF
.y
;
352 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
358 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat
& format
,
359 size_t WXUNUSED(len
),
364 if ( format
== wxDF_ENHMETAFILE
)
366 hEMF
= *(HENHMETAFILE
*)buf
;
368 wxCHECK_MSG( hEMF
, FALSE
, _T("pasting invalid enh metafile") );
372 wxASSERT_MSG( format
== wxDF_METAFILE
, _T("unsupported format") );
375 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
377 // first get the buffer size
378 size_t size
= ::GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
379 wxCHECK_MSG( size
, FALSE
, _T("GetMetaFileBitsEx() failed") );
381 // then get metafile bits
382 BYTE
*bits
= (BYTE
*)malloc(size
);
383 if ( !::GetMetaFileBitsEx(mfpict
->hMF
, size
, bits
) )
385 wxLogLastError(_T("GetMetaFileBitsEx"));
394 // and finally create an enhanced metafile from them
395 hEMF
= ::SetWinMetaFileBits(size
, bits
, hdcRef
, mfpict
);
399 wxLogLastError(_T("SetWinMetaFileBits"));
405 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
410 // ----------------------------------------------------------------------------
411 // wxEnhMetaFileSimpleDataObject
412 // ----------------------------------------------------------------------------
414 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
416 // we pass data by handle and not HGLOBAL
420 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf
) const
422 wxCHECK_MSG( m_metafile
.Ok(), false, _T("copying invalid enh metafile") );
424 HENHMETAFILE hEMF
= (HENHMETAFILE
)m_metafile
.GetHENHMETAFILE();
426 HENHMETAFILE hEMFCopy
= ::CopyEnhMetaFile(hEMF
, NULL
);
429 wxLogLastError(_T("CopyEnhMetaFile"));
434 *(HENHMETAFILE
*)buf
= hEMFCopy
;
438 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len
),
441 HENHMETAFILE hEMF
= *(HENHMETAFILE
*)buf
;
443 wxCHECK_MSG( hEMF
, FALSE
, _T("pasting invalid enh metafile") );
444 m_metafile
.SetHENHMETAFILE((WXHANDLE
)hEMF
);
450 #endif // wxUSE_DRAG_AND_DROP
452 #endif // wxUSE_ENH_METAFILE