1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/metafile.cpp
3 // Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // Currently, the only purpose for making a metafile
13 // is to put it on the clipboard.
16 #include "wx/wxprec.h"
25 #include "wx/metafile.h"
26 #include "wx/clipbrd.h"
27 #include "wx/mac/private.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
33 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
35 class wxMetafileRefData
: public wxGDIRefData
37 friend class WXDLLEXPORT wxMetafile
;
41 virtual ~wxMetafileRefData();
46 #if wxMAC_USE_CORE_GRAPHICS
47 QDPictRef m_qdPictRef
;
51 wxMetafileRefData::wxMetafileRefData()
55 #if wxMAC_USE_CORE_GRAPHICS
60 wxMetafileRefData::~wxMetafileRefData()
64 KillPicture( (PicHandle
)m_metafile
);
67 #if wxMAC_USE_CORE_GRAPHICS
68 QDPictRelease( m_qdPictRef
);
74 wxMetaFile::wxMetaFile(const wxString
& file
)
76 m_refData
= new wxMetafileRefData
;
78 M_METAFILEDATA
->m_metafile
= NULL
;
79 wxASSERT_MSG( file
.empty(), wxT("no file-based metafile support yet") );
82 if (!file
.IsNull() && (file
.Cmp("") == 0))
83 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile( file
);
87 wxMetaFile::~wxMetaFile()
91 bool wxMetaFile::IsOk() const
93 return (M_METAFILEDATA
&& (M_METAFILEDATA
->m_metafile
!= NULL
));
96 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
98 return (WXHMETAFILE
) M_METAFILEDATA
->m_metafile
;
101 bool wxMetaFile::SetClipboard(int width
, int height
)
105 #if wxUSE_DRAG_AND_DROP
106 // TODO: to finish this port, we need the data object first
107 if (m_refData
== NULL
)
110 bool alreadyOpen
= wxTheClipboard
->IsOpened();
113 wxTheClipboard
->Open();
114 wxTheClipboard
->Clear();
117 wxDataObject
*data
= new wxMetafileDataObject( *this );
118 success
= wxTheClipboard
->SetData( data
);
120 wxTheClipboard
->Close();
126 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
130 m_refData
= new wxMetafileRefData
;
132 M_METAFILEDATA
->m_metafile
= (PicHandle
)mf
;
134 #if wxMAC_USE_CORE_GRAPHICS
135 size_t sz
= GetHandleSize( (Handle
) M_METAFILEDATA
->m_metafile
);
136 wxMemoryBuffer
* membuf
= new wxMemoryBuffer( sz
);
137 void *data
= membuf
->GetWriteBuf( sz
);
139 memcpy( data
, *M_METAFILEDATA
->m_metafile
, sz
);
140 membuf
->UngetWriteBuf( sz
);
141 CGDataProviderRef provider
= CGDataProviderCreateWithData(
142 membuf
, data
, sz
, wxMacMemoryBufferReleaseProc
);
143 M_METAFILEDATA
->m_qdPictRef
= NULL
;
145 if (provider
!= NULL
)
147 M_METAFILEDATA
->m_qdPictRef
= QDPictCreateWithProvider( provider
);
148 CGDataProviderRelease( provider
);
153 bool wxMetaFile::Play(wxDC
*dc
)
162 #if wxMAC_USE_CORE_GRAPHICS
163 QDPictRef cgPictRef
= M_METAFILEDATA
->m_qdPictRef
;
164 CGContextRef cg
= ((wxMacCGContext
*)(dc
->GetGraphicContext()))->GetNativeContext();
165 CGRect bounds
= QDPictGetBounds( cgPictRef
);
167 CGContextSaveGState( cg
);
168 CGContextTranslateCTM( cg
, 0, bounds
.size
.width
);
169 CGContextScaleCTM( cg
, 1, -1 );
170 QDPictDrawToCGContext( cg
, bounds
, cgPictRef
);
171 CGContextRestoreGState( cg
);
173 PicHandle pict
= (PicHandle
)GetHMETAFILE();
174 wxMacPortSetter
helper( dc
);
176 DrawPicture( pict
, wxMacGetPictureBounds( pict
, &picFrame
) );
183 wxSize
wxMetaFile::GetSize() const
185 wxSize dataSize
= wxDefaultSize
;
189 PicHandle pict
= (PicHandle
)GetHMETAFILE();
191 wxMacGetPictureBounds( pict
, &r
);
192 dataSize
.x
= r
.right
- r
.left
;
193 dataSize
.y
= r
.bottom
- r
.top
;
199 // Metafile device context
201 // New constructor that takes origin and extent. If you use this, don't
202 // give origin/extent arguments to wxMakeMetaFilePlaceable.
204 wxMetaFileDC::wxMetaFileDC(
205 const wxString
& filename
,
206 int width
, int height
,
207 const wxString
& WXUNUSED(description
) )
209 wxASSERT_MSG( width
<= 0 || height
<= 0, wxT("no arbitration of metafile size supported") );
210 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
212 m_metaFile
= new wxMetaFile( filename
);
214 #if wxMAC_USE_CORE_GRAPHICS
216 Rect r
= { 0, 0, height
, width
};
218 RectRgn( (RgnHandle
)m_macBoundaryClipRgn
, &r
);
219 CopyRgn( (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
221 m_metaFile
->SetHMETAFILE( (WXHMETAFILE
)OpenPicture( &r
) );
222 ::GetPort( (GrafPtr
*)&m_macPort
);
227 SetMapMode( wxMM_TEXT
);
230 wxMetaFileDC::~wxMetaFileDC()
234 void wxMetaFileDC::DoGetSize(int *width
, int *height
) const
236 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
238 wxSize sz
= m_metaFile
->GetSize();
245 wxMetaFile
*wxMetaFileDC::Close()
253 size_t wxMetafileDataObject::GetDataSize() const
255 return GetHandleSize( (Handle
) (*((wxMetafile
*)&m_metafile
)).GetHMETAFILE() );
258 bool wxMetafileDataObject::GetDataHere(void *buf
) const
260 Handle pictH
= (Handle
)(*((wxMetafile
*)&m_metafile
)).GetHMETAFILE();
261 bool result
= (pictH
!= NULL
);
264 memcpy( buf
, *pictH
, GetHandleSize( pictH
) );
269 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
271 Handle handle
= NewHandle( len
);
272 SetHandleSize( handle
, len
);
273 memcpy( *handle
, buf
, len
);
274 m_metafile
.SetHMETAFILE( (WXHMETAFILE
) handle
);