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()
65 KillPicture( (PicHandle
)m_metafile
);
68 #if wxMAC_USE_CORE_GRAPHICS
69 QDPictRelease( m_qdPictRef
);
76 wxMetaFile::wxMetaFile(const wxString
& file
)
78 m_refData
= new wxMetafileRefData
;
80 M_METAFILEDATA
->m_metafile
= NULL
;
81 wxASSERT_MSG( file
.empty(), wxT("no file-based metafile support yet") );
84 if (!file
.IsNull() && (file
.Cmp("") == 0))
85 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile( file
);
89 wxMetaFile::~wxMetaFile()
93 bool wxMetaFile::IsOk() const
95 return (M_METAFILEDATA
&& (M_METAFILEDATA
->m_metafile
!= NULL
));
98 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
100 return (WXHMETAFILE
) M_METAFILEDATA
->m_metafile
;
103 bool wxMetaFile::SetClipboard(int width
, int height
)
107 #if wxUSE_DRAG_AND_DROP
108 // TODO: to finish this port, we need the data object first
109 if (m_refData
== NULL
)
112 bool alreadyOpen
= wxTheClipboard
->IsOpened();
115 wxTheClipboard
->Open();
116 wxTheClipboard
->Clear();
119 wxDataObject
*data
= new wxMetafileDataObject( *this );
120 success
= wxTheClipboard
->SetData( data
);
122 wxTheClipboard
->Close();
128 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
132 m_refData
= new wxMetafileRefData
;
134 M_METAFILEDATA
->m_metafile
= (PicHandle
)mf
;
136 #if wxMAC_USE_CORE_GRAPHICS
137 size_t sz
= GetHandleSize( (Handle
) M_METAFILEDATA
->m_metafile
);
138 wxMemoryBuffer
* membuf
= new wxMemoryBuffer( sz
);
139 void *data
= membuf
->GetWriteBuf( sz
);
141 memcpy( data
, *M_METAFILEDATA
->m_metafile
, sz
);
142 membuf
->UngetWriteBuf( sz
);
143 CGDataProviderRef provider
= CGDataProviderCreateWithData(
144 membuf
, data
, sz
, wxMacMemoryBufferReleaseProc
);
145 M_METAFILEDATA
->m_qdPictRef
= NULL
;
148 if (provider
!= NULL
)
150 M_METAFILEDATA
->m_qdPictRef
= QDPictCreateWithProvider( provider
);
151 CGDataProviderRelease( provider
);
157 bool wxMetaFile::Play(wxDC
*dc
)
166 #if wxMAC_USE_CORE_GRAPHICS
168 QDPictRef cgPictRef
= M_METAFILEDATA
->m_qdPictRef
;
169 CGContextRef cg
= ((wxMacCGContext
*)(dc
->GetGraphicContext()))->GetNativeContext();
170 CGRect bounds
= QDPictGetBounds( cgPictRef
);
172 CGContextSaveGState( cg
);
173 CGContextTranslateCTM( cg
, 0, bounds
.size
.width
);
174 CGContextScaleCTM( cg
, 1, -1 );
175 QDPictDrawToCGContext( cg
, bounds
, cgPictRef
);
176 CGContextRestoreGState( cg
);
179 PicHandle pict
= (PicHandle
)GetHMETAFILE();
180 wxMacPortSetter
helper( dc
);
182 DrawPicture( pict
, wxMacGetPictureBounds( pict
, &picFrame
) );
189 wxSize
wxMetaFile::GetSize() const
191 wxSize dataSize
= wxDefaultSize
;
196 PicHandle pict
= (PicHandle
)GetHMETAFILE();
198 wxMacGetPictureBounds( pict
, &r
);
199 dataSize
.x
= r
.right
- r
.left
;
200 dataSize
.y
= r
.bottom
- r
.top
;
207 // Metafile device context
209 // New constructor that takes origin and extent. If you use this, don't
210 // give origin/extent arguments to wxMakeMetaFilePlaceable.
212 wxMetaFileDC::wxMetaFileDC(
213 const wxString
& filename
,
214 int width
, int height
,
215 const wxString
& WXUNUSED(description
) )
217 wxASSERT_MSG( width
<= 0 || height
<= 0, wxT("no arbitration of metafile size supported") );
218 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
220 m_metaFile
= new wxMetaFile( filename
);
222 #if wxMAC_USE_CORE_GRAPHICS
224 Rect r
= { 0, 0, height
, width
};
226 RectRgn( (RgnHandle
)m_macBoundaryClipRgn
, &r
);
227 CopyRgn( (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
229 m_metaFile
->SetHMETAFILE( (WXHMETAFILE
)OpenPicture( &r
) );
230 ::GetPort( (GrafPtr
*)&m_macPort
);
235 SetMapMode( wxMM_TEXT
);
238 wxMetaFileDC::~wxMetaFileDC()
242 void wxMetaFileDC::DoGetSize(int *width
, int *height
) const
244 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
246 wxSize sz
= m_metaFile
->GetSize();
253 wxMetaFile
*wxMetaFileDC::Close()
262 size_t wxMetafileDataObject::GetDataSize() const
264 return GetHandleSize( (Handle
) (*((wxMetafile
*)&m_metafile
)).GetHMETAFILE() );
267 bool wxMetafileDataObject::GetDataHere(void *buf
) const
269 Handle pictH
= (Handle
)(*((wxMetafile
*)&m_metafile
)).GetHMETAFILE();
270 bool result
= (pictH
!= NULL
);
273 memcpy( buf
, *pictH
, GetHandleSize( pictH
) );
278 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
280 Handle handle
= NewHandle( len
);
281 SetHandleSize( handle
, len
);
282 memcpy( *handle
, buf
, len
);
283 m_metafile
.SetHMETAFILE( (WXHMETAFILE
) handle
);