1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/private.h"
28 #include "wx/graphics.h"
29 #include "wx/osx/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 // default ctor needed for CreateGDIRefData(), must be initialized later
44 wxMetafileRefData() { Init(); }
46 // creates a metafile from memory, assumes ownership
47 wxMetafileRefData(CFDataRef data
);
49 // prepares a recording metafile
50 wxMetafileRefData( int width
, int height
);
52 // prepares a metafile to be read from a file (if filename is not empty)
53 wxMetafileRefData( const wxString
& filename
);
55 virtual ~wxMetafileRefData();
57 virtual bool IsOk() const { return m_data
!= NULL
; }
61 int GetWidth() const { return m_width
; }
62 int GetHeight() const { return m_height
; }
64 CGPDFDocumentRef
GetPDFDocument() const { return m_pdfDoc
; }
65 void UpdateDocumentFromData() ;
67 const wxCFDataRef
& GetData() const { return m_data
; }
68 CGContextRef
GetContext() const { return m_context
; }
74 wxCFRef
<CGPDFDocumentRef
> m_pdfDoc
;
75 CGContextRef m_context
;
81 // Our m_pdfDoc field can't be easily (deep) copied and so we don't define a
83 wxDECLARE_NO_COPY_CLASS(wxMetafileRefData
);
86 wxMetafileRefData::wxMetafileRefData(CFDataRef data
) :
90 UpdateDocumentFromData();
93 wxMetafileRefData::wxMetafileRefData( const wxString
& filename
)
97 if ( !filename
.empty() )
99 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(filename
)));
100 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
101 wxCFRef
<CFURLRef
> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
102 m_pdfDoc
.reset(CGPDFDocumentCreateWithURL(url
));
107 wxMetafileRefData::wxMetafileRefData( int width
, int height
)
114 CGRect r
= CGRectMake( 0 , 0 , width
, height
);
116 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
118 CGDataConsumerRef dataConsumer
= wxMacCGDataConsumerCreateWithCFData(data
);
119 m_context
= CGPDFContextCreate( dataConsumer
, (width
!= 0 && height
!= 0) ? &r
: NULL
, NULL
);
120 CGDataConsumerRelease( dataConsumer
);
123 CGPDFContextBeginPage(m_context
, NULL
);
125 CGColorSpaceRef genericColorSpace
= wxMacGetGenericRGBColorSpace();
127 CGContextSetFillColorSpace( m_context
, genericColorSpace
);
128 CGContextSetStrokeColorSpace( m_context
, genericColorSpace
);
130 CGContextTranslateCTM( m_context
, 0 , height
) ;
131 CGContextScaleCTM( m_context
, 1 , -1 ) ;
135 wxMetafileRefData::~wxMetafileRefData()
139 void wxMetafileRefData::Init()
146 void wxMetafileRefData::Close()
148 CGPDFContextEndPage(m_context
);
150 CGContextRelease(m_context
);
153 UpdateDocumentFromData();
156 void wxMetafileRefData::UpdateDocumentFromData()
158 wxCFRef
<CGDataProviderRef
> provider(wxMacCGDataProviderCreateWithCFData(m_data
));
159 m_pdfDoc
.reset(CGPDFDocumentCreateWithProvider(provider
));
160 if ( m_pdfDoc
!= NULL
)
162 CGPDFPageRef page
= CGPDFDocumentGetPage( m_pdfDoc
, 1 );
163 CGRect rect
= CGPDFPageGetBoxRect ( page
, kCGPDFMediaBox
);
164 m_width
= static_cast<int>(rect
.size
.width
);
165 m_height
= static_cast<int>(rect
.size
.height
);
169 wxMetaFile::wxMetaFile(const wxString
& file
)
171 m_refData
= new wxMetafileRefData(file
);
174 wxMetaFile::~wxMetaFile()
178 wxGDIRefData
*wxMetaFile::CreateGDIRefData() const
180 return new wxMetafileRefData
;
184 wxMetaFile::CloneGDIRefData(const wxGDIRefData
* WXUNUSED(data
)) const
186 wxFAIL_MSG( wxS("Cloning metafiles is not implemented in wxCarbon.") );
188 return new wxMetafileRefData
;
191 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
193 return (WXHMETAFILE
) (CFDataRef
) M_METAFILEDATA
->GetData();
196 bool wxMetaFile::SetClipboard(int WXUNUSED(width
), int WXUNUSED(height
))
200 #if wxUSE_DRAG_AND_DROP
201 if (m_refData
== NULL
)
204 bool alreadyOpen
= wxTheClipboard
->IsOpened();
207 wxTheClipboard
->Open();
208 wxTheClipboard
->Clear();
211 wxDataObject
*data
= new wxMetafileDataObject( *this );
212 success
= wxTheClipboard
->SetData( data
);
214 wxTheClipboard
->Close();
220 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
224 m_refData
= new wxMetafileRefData((CFDataRef
)mf
);
227 #if wxOSX_USE_COCOA_OR_CARBON && !defined( __LP64__ )
228 void wxMetafile::SetPICT(void* pictHandle
)
232 Handle picHandle
= (Handle
) pictHandle
;
234 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) *picHandle
, GetHandleSize(picHandle
), kCFAllocatorNull
);
235 wxCFRef
<CGDataProviderRef
> provider(wxMacCGDataProviderCreateWithCFData(data
));
236 QDPictRef pictRef
= QDPictCreateWithProvider(provider
);
237 CGRect rect
= QDPictGetBounds(pictRef
);
238 m_refData
= new wxMetafileRefData(static_cast<int>(rect
.size
.width
),
239 static_cast<int>(rect
.size
.height
));
240 QDPictDrawToCGContext( ((wxMetafileRefData
*) m_refData
)->GetContext(), rect
, pictRef
);
242 QDPictRelease( pictRef
);
243 ((wxMetafileRefData
*) m_refData
)->Close();
247 bool wxMetaFile::Play(wxDC
*dc
)
256 wxDCImpl
*impl
= dc
->GetImpl();
257 wxGCDCImpl
*gc_impl
= wxDynamicCast(impl
, wxGCDCImpl
);
260 CGContextRef cg
= (CGContextRef
) (gc_impl
->GetGraphicsContext()->GetNativeContext());
261 CGPDFDocumentRef doc
= M_METAFILEDATA
->GetPDFDocument();
262 CGPDFPageRef page
= CGPDFDocumentGetPage( doc
, 1 );
263 wxMacCGContextStateSaver
save(cg
);
264 CGContextDrawPDFPage( cg
, page
);
266 // CGContextTranslateCTM( cg, 0, bounds.size.width );
267 // CGContextScaleCTM( cg, 1, -1 );
273 wxSize
wxMetaFile::GetSize() const
275 wxSize dataSize
= wxDefaultSize
;
279 dataSize
.x
= M_METAFILEDATA
->GetWidth();
280 dataSize
.y
= M_METAFILEDATA
->GetHeight();
286 // Metafile device context
288 // New constructor that takes origin and extent. If you use this, don't
289 // give origin/extent arguments to wxMakeMetaFilePlaceable.
291 wxMetafileDCImpl::wxMetafileDCImpl(
293 const wxString
& filename
,
294 int width
, int height
,
295 const wxString
& WXUNUSED(description
) ) :
298 wxASSERT_MSG( width
!= 0 || height
!= 0, wxT("no arbitration of metafile size supported") );
299 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
301 m_metaFile
= new wxMetaFile( filename
);
302 wxMetafileRefData
* metafiledata
= new wxMetafileRefData(width
, height
);
304 m_metaFile
->SetRefData( metafiledata
);
306 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata
->GetContext()));
307 m_ok
= (m_graphicContext
!= NULL
) ;
309 SetMapMode( wxMM_TEXT
);
312 wxMetafileDCImpl::~wxMetafileDCImpl()
316 void wxMetafileDCImpl::DoGetSize(int *width
, int *height
) const
318 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
320 wxSize sz
= m_metaFile
->GetSize();
327 wxMetaFile
*wxMetafileDCImpl::Close()
329 wxDELETE(m_graphicContext
);
332 M_METAFILEREFDATA(*m_metaFile
)->Close();
338 size_t wxMetafileDataObject::GetDataSize() const
341 wxMetafileRefData
* refData
= M_METAFILEREFDATA(m_metafile
);
343 length
= refData
->GetData().GetLength();
347 bool wxMetafileDataObject::GetDataHere(void *buf
) const
351 wxMetafileRefData
* refData
= M_METAFILEREFDATA(m_metafile
);
354 CFIndex length
= refData
->GetData().GetLength();
358 refData
->GetData().GetBytes(CFRangeMake(0,length
), (UInt8
*) buf
);
364 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
366 wxMetafileRefData
* metafiledata
= new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8
*)buf
, len
).get()));
368 m_metafile
.SetRefData( metafiledata
);