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 bool wxMetaFile::Play(wxDC
*dc
)
236 wxDCImpl
*impl
= dc
->GetImpl();
237 wxGCDCImpl
*gc_impl
= wxDynamicCast(impl
, wxGCDCImpl
);
240 CGContextRef cg
= (CGContextRef
) (gc_impl
->GetGraphicsContext()->GetNativeContext());
241 CGPDFDocumentRef doc
= M_METAFILEDATA
->GetPDFDocument();
242 CGPDFPageRef page
= CGPDFDocumentGetPage( doc
, 1 );
243 wxMacCGContextStateSaver
save(cg
);
244 CGContextDrawPDFPage( cg
, page
);
246 // CGContextTranslateCTM( cg, 0, bounds.size.width );
247 // CGContextScaleCTM( cg, 1, -1 );
253 wxSize
wxMetaFile::GetSize() const
255 wxSize dataSize
= wxDefaultSize
;
259 dataSize
.x
= M_METAFILEDATA
->GetWidth();
260 dataSize
.y
= M_METAFILEDATA
->GetHeight();
266 // Metafile device context
268 // New constructor that takes origin and extent. If you use this, don't
269 // give origin/extent arguments to wxMakeMetaFilePlaceable.
271 wxMetafileDCImpl::wxMetafileDCImpl(
273 const wxString
& filename
,
274 int width
, int height
,
275 const wxString
& WXUNUSED(description
) ) :
278 wxASSERT_MSG( width
!= 0 || height
!= 0, wxT("no arbitration of metafile size supported") );
279 wxASSERT_MSG( filename
.empty(), wxT("no file based metafile support yet"));
281 m_metaFile
= new wxMetaFile( filename
);
282 wxMetafileRefData
* metafiledata
= new wxMetafileRefData(width
, height
);
284 m_metaFile
->SetRefData( metafiledata
);
286 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata
->GetContext()));
287 m_ok
= (m_graphicContext
!= NULL
) ;
289 SetMapMode( wxMM_TEXT
);
292 wxMetafileDCImpl::~wxMetafileDCImpl()
296 void wxMetafileDCImpl::DoGetSize(int *width
, int *height
) const
298 wxCHECK_RET( m_metaFile
, wxT("GetSize() doesn't work without a metafile") );
300 wxSize sz
= m_metaFile
->GetSize();
307 wxMetaFile
*wxMetafileDCImpl::Close()
309 wxDELETE(m_graphicContext
);
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
);