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
;
46 #if wxMAC_USE_CORE_GRAPHICS
47 QDPictRef m_qdPictRef
;
52 extern bool wxClipboardIsOpen
;
55 wxMetafileRefData::wxMetafileRefData()
59 #if wxMAC_USE_CORE_GRAPHICS
64 wxMetafileRefData::~wxMetafileRefData()
68 KillPicture( (PicHandle
)m_metafile
);
71 #if wxMAC_USE_CORE_GRAPHICS
72 QDPictRelease( m_qdPictRef
);
78 wxMetaFile::wxMetaFile(const wxString
& file
)
80 m_refData
= new wxMetafileRefData
;
82 M_METAFILEDATA
->m_metafile
= NULL
;
83 wxASSERT_MSG( file
.empty(), wxT("no file-based metafile support yet") );
86 if (!file
.IsNull() && (file
.Cmp("") == 0))
87 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile( file
);
91 wxMetaFile::~wxMetaFile()
95 bool wxMetaFile::Ok() const
97 return (M_METAFILEDATA
&& (M_METAFILEDATA
->m_metafile
!= NULL
));
100 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
102 return (WXHMETAFILE
) M_METAFILEDATA
->m_metafile
;
105 bool wxMetaFile::SetClipboard(int width
, int height
)
109 #if wxUSE_DRAG_AND_DROP
110 // TODO: to finish this port, we need the data object first
111 if (m_refData
== NULL
)
114 bool alreadyOpen
= wxTheClipboard
->IsOpened();
117 wxTheClipboard
->Open();
118 wxTheClipboard
->Clear();
121 wxDataObject
*data
= new wxMetafileDataObject( *this );
122 success
= wxTheClipboard
->SetData( data
);
124 wxTheClipboard
->Close();
130 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
134 m_refData
= new wxMetafileRefData
;
136 M_METAFILEDATA
->m_metafile
= (PicHandle
)mf
;
138 #if wxMAC_USE_CORE_GRAPHICS
139 size_t sz
= GetHandleSize( (Handle
) M_METAFILEDATA
->m_metafile
);
140 wxMemoryBuffer
* membuf
= new wxMemoryBuffer( sz
);
141 void *data
= membuf
->GetWriteBuf( sz
);
143 memcpy( data
, *M_METAFILEDATA
->m_metafile
, sz
);
144 membuf
->UngetWriteBuf( sz
);
145 CGDataProviderRef provider
= CGDataProviderCreateWithData(
146 membuf
, data
, sz
, wxMacMemoryBufferReleaseProc
);
147 M_METAFILEDATA
->m_qdPictRef
= NULL
;
149 if (provider
!= NULL
)
151 M_METAFILEDATA
->m_qdPictRef
= QDPictCreateWithProvider( provider
);
152 CGDataProviderRelease( provider
);
157 bool wxMetaFile::Play(wxDC
*dc
)
166 #if wxMAC_USE_CORE_GRAPHICS
167 QDPictRef cgPictRef
= M_METAFILEDATA
->m_qdPictRef
;
168 CGContextRef cg
= ((wxMacCGContext
*)(dc
->GetGraphicContext()))->GetNativeContext();
169 CGRect bounds
= QDPictGetBounds( cgPictRef
);
171 CGContextSaveGState( cg
);
172 CGContextTranslateCTM( cg
, 0, bounds
.size
.width
);
173 CGContextScaleCTM( cg
, 1, -1 );
174 QDPictDrawToCGContext( cg
, bounds
, cgPictRef
);
175 CGContextRestoreGState( cg
);
177 PicHandle pict
= (PicHandle
)GetHMETAFILE();
178 wxMacPortSetter
helper( dc
);
180 DrawPicture( pict
, wxMacGetPictureBounds( pict
, &picFrame
) );
187 wxSize
wxMetaFile::GetSize() const
189 wxSize dataSize
= wxDefaultSize
;
193 PicHandle pict
= (PicHandle
)GetHMETAFILE();
195 wxMacGetPictureBounds( pict
, &r
);
196 dataSize
.x
= r
.right
- r
.left
;
197 dataSize
.y
= r
.bottom
- r
.top
;
203 // Metafile device context
205 // New constructor that takes origin and extent. If you use this, don't
206 // give origin/extent arguments to wxMakeMetaFilePlaceable.
208 wxMetaFileDC::wxMetaFileDC(
209 const wxString
& filename
,
210 int width
, int height
,
211 const wxString
& WXUNUSED(description
) )
213 wxASSERT_MSG( width
<= 0 || height
<= 0, wxT("no arbitration of metafile size supported") );
214 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
216 m_metaFile
= new wxMetaFile( filename
);
218 #if wxMAC_USE_CORE_GRAPHICS
220 Rect r
= { 0, 0, height
, width
};
222 RectRgn( (RgnHandle
)m_macBoundaryClipRgn
, &r
);
223 CopyRgn( (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
225 m_metaFile
->SetHMETAFILE( (WXHMETAFILE
)OpenPicture( &r
) );
226 ::GetPort( (GrafPtr
*)&m_macPort
);
231 SetMapMode( wxMM_TEXT
);
234 wxMetaFileDC::~wxMetaFileDC()
238 void wxMetaFileDC::DoGetSize(int *width
, int *height
) const
240 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
242 wxSize sz
= m_metaFile
->GetSize();
249 wxMetaFile
*wxMetaFileDC::Close()
257 size_t wxMetafileDataObject::GetDataSize() const
259 return GetHandleSize( (Handle
) (*((wxMetafile
*)&m_metafile
)).GetHMETAFILE() );
262 bool wxMetafileDataObject::GetDataHere(void *buf
) const
264 Handle pictH
= (Handle
)(*((wxMetafile
*)&m_metafile
)).GetHMETAFILE();
265 bool result
= (pictH
!= NULL
);
268 memcpy( buf
, *pictH
, GetHandleSize( pictH
) );
273 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
275 Handle handle
= NewHandle( len
);
276 SetHandleSize( handle
, len
);
277 memcpy( *handle
, buf
, len
);
278 m_metafile
.SetHMETAFILE( (WXHMETAFILE
) handle
);