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/uma.h"
28 #include "wx/graphics.h"
29 #include "wx/mac/carbon/metafile.h"
34 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
35 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
36 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDCImpl
, wxGCDCImpl
)
38 #define M_METAFILEREFDATA( a ) ((wxMetafileRefData*)(a).GetRefData())
40 class wxMetafileRefData
: public wxGDIRefData
43 // creates a metafile from memory, assumes ownership
44 wxMetafileRefData(CFDataRef data
);
45 // prepares a recording metafile
46 wxMetafileRefData( int width
, int height
);
47 // prepares a metafile to be read from a file (if filename is not empty)
48 wxMetafileRefData( const wxString
& filename
);
49 virtual ~wxMetafileRefData();
53 int GetWidth() const { return m_width
; }
54 int GetHeight() const { return m_height
; }
56 CGPDFDocumentRef
GetPDFDocument() const { return m_pdfDoc
; }
57 void UpdateDocumentFromData() ;
59 const wxCFDataRef
& GetData() const { return m_data
; }
60 CGContextRef
GetContext() const { return m_context
; }
66 wxCFRef
<CGPDFDocumentRef
> m_pdfDoc
;
67 CGContextRef m_context
;
73 wxMetafileRefData::wxMetafileRefData(CFDataRef data
) :
77 UpdateDocumentFromData();
80 wxMetafileRefData::wxMetafileRefData( const wxString
& filename
)
84 if ( !filename
.empty() )
86 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxMacCFStringHolder(filename
)));
87 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
88 wxCFRef
<CFURLRef
> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
89 m_pdfDoc
.reset(CGPDFDocumentCreateWithURL(url
));
94 wxMetafileRefData::wxMetafileRefData( int width
, int height
)
101 CGRect r
= CGRectMake( 0 , 0 , width
, height
);
103 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
105 CGDataConsumerRef dataConsumer
= wxMacCGDataConsumerCreateWithCFData(data
);
106 m_context
= CGPDFContextCreate( dataConsumer
, (width
!= 0 && height
!= 0) ? &r
: NULL
, NULL
);
107 CGDataConsumerRelease( dataConsumer
);
110 CGPDFContextBeginPage(m_context
, NULL
);
112 CGColorSpaceRef genericColorSpace
= wxMacGetGenericRGBColorSpace();
114 CGContextSetFillColorSpace( m_context
, genericColorSpace
);
115 CGContextSetStrokeColorSpace( m_context
, genericColorSpace
);
117 CGContextTranslateCTM( m_context
, 0 , height
) ;
118 CGContextScaleCTM( m_context
, 1 , -1 ) ;
122 wxMetafileRefData::~wxMetafileRefData()
126 void wxMetafileRefData::Init()
133 void wxMetafileRefData::Close()
135 CGPDFContextEndPage(m_context
);
137 CGContextRelease(m_context
);
140 UpdateDocumentFromData();
143 void wxMetafileRefData::UpdateDocumentFromData()
145 wxCFRef
<CGDataProviderRef
> provider(wxMacCGDataProviderCreateWithCFData(m_data
));
146 m_pdfDoc
.reset(CGPDFDocumentCreateWithProvider(provider
));
147 if ( m_pdfDoc
!= NULL
)
149 CGPDFPageRef page
= CGPDFDocumentGetPage( m_pdfDoc
, 1 );
150 CGRect rect
= CGPDFPageGetBoxRect ( page
, kCGPDFMediaBox
);
151 m_width
= wx_static_cast(int, rect
.size
.width
);
152 m_height
= wx_static_cast(int, rect
.size
.height
);
156 wxMetaFile::wxMetaFile(const wxString
& file
)
158 m_refData
= new wxMetafileRefData(file
);
161 wxMetaFile::~wxMetaFile()
165 bool wxMetaFile::IsOk() const
167 return (M_METAFILEDATA
&& (M_METAFILEDATA
->GetData() != NULL
));
170 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
172 return (WXHMETAFILE
) (CFDataRef
) M_METAFILEDATA
->GetData();
175 bool wxMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
179 #if wxUSE_DRAG_AND_DROP
180 if (m_refData
== NULL
)
183 bool alreadyOpen
= wxTheClipboard
->IsOpened();
186 wxTheClipboard
->Open();
187 wxTheClipboard
->Clear();
190 wxDataObject
*data
= new wxMetafileDataObject( *this );
191 success
= wxTheClipboard
->SetData( data
);
193 wxTheClipboard
->Close();
199 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
203 m_refData
= new wxMetafileRefData((CFDataRef
)mf
);
207 void wxMetafile::SetPICT(void* pictHandle
)
211 Handle picHandle
= (Handle
) pictHandle
;
213 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) *picHandle
, GetHandleSize(picHandle
), kCFAllocatorNull
);
214 wxCFRef
<CGDataProviderRef
> provider(wxMacCGDataProviderCreateWithCFData(data
));
215 QDPictRef pictRef
= QDPictCreateWithProvider(provider
);
216 CGRect rect
= QDPictGetBounds(pictRef
);
217 m_refData
= new wxMetafileRefData(wx_static_cast(int, rect
.size
.width
),
218 wx_static_cast(int, rect
.size
.height
));
219 QDPictDrawToCGContext( ((wxMetafileRefData
*) m_refData
)->GetContext(), rect
, pictRef
);
221 QDPictRelease( pictRef
);
222 ((wxMetafileRefData
*) m_refData
)->Close();
226 bool wxMetaFile::Play(wxDC
*dc
)
235 wxDCImpl
*impl
= dc
->GetImpl();
236 wxGCDCImpl
*gc_impl
= wxDynamicCast(impl
, wxGCDCImpl
);
239 CGContextRef cg
= (CGContextRef
) (gc_impl
->GetGraphicsContext()->GetNativeContext());
240 CGPDFDocumentRef doc
= M_METAFILEDATA
->GetPDFDocument();
241 CGPDFPageRef page
= CGPDFDocumentGetPage( doc
, 1 );
242 wxMacCGContextStateSaver
save(cg
);
243 CGContextDrawPDFPage( cg
, page
);
245 // CGContextTranslateCTM( cg, 0, bounds.size.width );
246 // CGContextScaleCTM( cg, 1, -1 );
252 wxSize
wxMetaFile::GetSize() const
254 wxSize dataSize
= wxDefaultSize
;
258 dataSize
.x
= M_METAFILEDATA
->GetWidth();
259 dataSize
.y
= M_METAFILEDATA
->GetHeight();
265 // Metafile device context
267 // New constructor that takes origin and extent. If you use this, don't
268 // give origin/extent arguments to wxMakeMetaFilePlaceable.
270 wxMetafileDCImpl::wxMetafileDCImpl(
272 const wxString
& filename
,
273 int width
, int height
,
274 const wxString
& WXUNUSED(description
) ) :
277 wxASSERT_MSG( width
!= 0 || height
!= 0, wxT("no arbitration of metafile size supported") );
278 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
280 m_metaFile
= new wxMetaFile( filename
);
281 wxMetafileRefData
* metafiledata
= new wxMetafileRefData(width
, height
);
283 m_metaFile
->SetRefData( metafiledata
);
285 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata
->GetContext()));
286 m_ok
= (m_graphicContext
!= NULL
) ;
288 SetMapMode( wxMM_TEXT
);
291 wxMetafileDCImpl::~wxMetafileDCImpl()
295 void wxMetafileDCImpl::DoGetSize(int *width
, int *height
) const
297 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
299 wxSize sz
= m_metaFile
->GetSize();
306 wxMetaFile
*wxMetafileDCImpl::Close()
308 delete m_graphicContext
;
309 m_graphicContext
= NULL
;
312 M_METAFILEREFDATA(*m_metaFile
)->Close();
318 size_t wxMetafileDataObject::GetDataSize() const
321 wxMetafileRefData
* refData
= M_METAFILEREFDATA(m_metafile
);
323 length
= refData
->GetData().GetLength();
327 bool wxMetafileDataObject::GetDataHere(void *buf
) const
331 wxMetafileRefData
* refData
= M_METAFILEREFDATA(m_metafile
);
334 CFIndex length
= refData
->GetData().GetLength();
338 refData
->GetData().GetBytes(CFRangeMake(0,length
), (UInt8
*) buf
);
344 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
346 wxMetafileRefData
* metafiledata
= new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8
*)buf
, len
).get()));
348 m_metafile
.SetRefData( metafiledata
);