]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/metafile.cpp
adding mimetype patch, closes #12072
[wxWidgets.git] / src / osx / carbon / metafile.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/metafile.cpp
489468fe
SC
3// Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11//
12// Currently, the only purpose for making a metafile
13// is to put it on the clipboard.
14
15
16#include "wx/wxprec.h"
17
18#if wxUSE_METAFILE
19
20#ifndef WX_PRECOMP
21 #include "wx/utils.h"
22 #include "wx/app.h"
23#endif
24
25#include "wx/metafile.h"
26#include "wx/clipbrd.h"
524c47aa 27#include "wx/osx/private.h"
489468fe 28#include "wx/graphics.h"
ccd5d46c 29#include "wx/osx/metafile.h"
489468fe
SC
30
31#include <stdio.h>
32#include <string.h>
33
34IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
35IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
36IMPLEMENT_ABSTRACT_CLASS(wxMetafileDCImpl, wxGCDCImpl)
37
38#define M_METAFILEREFDATA( a ) ((wxMetafileRefData*)(a).GetRefData())
39
40class wxMetafileRefData : public wxGDIRefData
41{
42public:
43 // default ctor needed for CreateGDIRefData(), must be initialized later
44 wxMetafileRefData() { Init(); }
45
46 // creates a metafile from memory, assumes ownership
47 wxMetafileRefData(CFDataRef data);
48
49 // prepares a recording metafile
50 wxMetafileRefData( int width, int height);
51
52 // prepares a metafile to be read from a file (if filename is not empty)
53 wxMetafileRefData( const wxString& filename);
54
55 virtual ~wxMetafileRefData();
56
57 virtual bool IsOk() const { return m_data != NULL; }
58
59 void Init();
60
61 int GetWidth() const { return m_width; }
62 int GetHeight() const { return m_height; }
63
64 CGPDFDocumentRef GetPDFDocument() const { return m_pdfDoc; }
65 void UpdateDocumentFromData() ;
66
67 const wxCFDataRef& GetData() const { return m_data; }
68 CGContextRef GetContext() const { return m_context; }
69
70 // ends the recording
71 void Close();
72private:
73 wxCFDataRef m_data;
74 wxCFRef<CGPDFDocumentRef> m_pdfDoc;
75 CGContextRef m_context;
76
77 int m_width ;
78 int m_height ;
79};
80
81wxMetafileRefData::wxMetafileRefData(CFDataRef data) :
82 m_data(data)
83{
84 Init();
85 UpdateDocumentFromData();
86}
87
88wxMetafileRefData::wxMetafileRefData( const wxString& filename )
89{
90 Init();
91
92 if ( !filename.empty() )
93 {
94 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(filename)));
95 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
96 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
97 m_pdfDoc.reset(CGPDFDocumentCreateWithURL(url));
98 }
99}
100
101
102wxMetafileRefData::wxMetafileRefData( int width, int height)
103{
104 Init();
105
106 m_width = width;
107 m_height = height;
108
109 CGRect r = CGRectMake( 0 , 0 , width , height );
110
111 CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
112 m_data.reset(data);
113 CGDataConsumerRef dataConsumer = wxMacCGDataConsumerCreateWithCFData(data);
114 m_context = CGPDFContextCreate( dataConsumer, (width != 0 && height != 0) ? &r : NULL , NULL );
115 CGDataConsumerRelease( dataConsumer );
116 if ( m_context )
117 {
118 CGPDFContextBeginPage(m_context, NULL);
119
120 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
121
122 CGContextSetFillColorSpace( m_context, genericColorSpace );
123 CGContextSetStrokeColorSpace( m_context, genericColorSpace );
124
125 CGContextTranslateCTM( m_context , 0 , height ) ;
126 CGContextScaleCTM( m_context , 1 , -1 ) ;
127 }
128}
129
130wxMetafileRefData::~wxMetafileRefData()
131{
132}
133
134void wxMetafileRefData::Init()
135{
136 m_context = NULL;
137 m_width = -1;
138 m_height = -1;
139}
140
141void wxMetafileRefData::Close()
142{
143 CGPDFContextEndPage(m_context);
144
145 CGContextRelease(m_context);
146 m_context = NULL;
147
148 UpdateDocumentFromData();
149}
150
151void wxMetafileRefData::UpdateDocumentFromData()
152{
153 wxCFRef<CGDataProviderRef> provider(wxMacCGDataProviderCreateWithCFData(m_data));
154 m_pdfDoc.reset(CGPDFDocumentCreateWithProvider(provider));
155 if ( m_pdfDoc != NULL )
156 {
157 CGPDFPageRef page = CGPDFDocumentGetPage( m_pdfDoc, 1 );
158 CGRect rect = CGPDFPageGetBoxRect ( page, kCGPDFMediaBox);
5c33522f
VZ
159 m_width = static_cast<int>(rect.size.width);
160 m_height = static_cast<int>(rect.size.height);
489468fe
SC
161 }
162}
163
164wxMetaFile::wxMetaFile(const wxString& file)
165{
166 m_refData = new wxMetafileRefData(file);
167}
168
169wxMetaFile::~wxMetaFile()
170{
171}
172
173wxGDIRefData *wxMetaFile::CreateGDIRefData() const
174{
175 return new wxMetafileRefData;
176}
177
178wxGDIRefData *wxMetaFile::CloneGDIRefData(const wxGDIRefData *data) const
179{
5c33522f 180 return new wxMetafileRefData(*static_cast<const wxMetafileRefData *>(data));
489468fe
SC
181}
182
183WXHMETAFILE wxMetaFile::GetHMETAFILE() const
184{
185 return (WXHMETAFILE) (CFDataRef) M_METAFILEDATA->GetData();
186}
187
188bool wxMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
189{
190 bool success = true;
191
192#if wxUSE_DRAG_AND_DROP
193 if (m_refData == NULL)
194 return false;
195
196 bool alreadyOpen = wxTheClipboard->IsOpened();
197 if (!alreadyOpen)
198 {
199 wxTheClipboard->Open();
200 wxTheClipboard->Clear();
201 }
202
203 wxDataObject *data = new wxMetafileDataObject( *this );
204 success = wxTheClipboard->SetData( data );
205 if (!alreadyOpen)
206 wxTheClipboard->Close();
207#endif
208
209 return success;
210}
211
212void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
213{
214 UnRef();
215
216 m_refData = new wxMetafileRefData((CFDataRef)mf);
217}
218
55a0f1e3 219#if wxOSX_USE_COCOA_OR_CARBON && !defined( __LP64__ )
489468fe
SC
220void wxMetafile::SetPICT(void* pictHandle)
221{
222 UnRef();
223
224 Handle picHandle = (Handle) pictHandle;
225 HLock(picHandle);
226 CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) *picHandle, GetHandleSize(picHandle), kCFAllocatorNull);
227 wxCFRef<CGDataProviderRef> provider(wxMacCGDataProviderCreateWithCFData(data));
228 QDPictRef pictRef = QDPictCreateWithProvider(provider);
229 CGRect rect = QDPictGetBounds(pictRef);
5c33522f
VZ
230 m_refData = new wxMetafileRefData(static_cast<int>(rect.size.width),
231 static_cast<int>(rect.size.height));
489468fe
SC
232 QDPictDrawToCGContext( ((wxMetafileRefData*) m_refData)->GetContext(), rect, pictRef );
233 CFRelease( data );
234 QDPictRelease( pictRef );
235 ((wxMetafileRefData*) m_refData)->Close();
236}
237#endif
238
239bool wxMetaFile::Play(wxDC *dc)
240{
241 if (!m_refData)
242 return false;
243
244 if (!dc->IsOk())
245 return false;
246
247 {
248 wxDCImpl *impl = dc->GetImpl();
249 wxGCDCImpl *gc_impl = wxDynamicCast(impl, wxGCDCImpl);
250 if (gc_impl)
251 {
252 CGContextRef cg = (CGContextRef) (gc_impl->GetGraphicsContext()->GetNativeContext());
253 CGPDFDocumentRef doc = M_METAFILEDATA->GetPDFDocument();
254 CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 );
255 wxMacCGContextStateSaver save(cg);
256 CGContextDrawPDFPage( cg, page );
257 }
258// CGContextTranslateCTM( cg, 0, bounds.size.width );
259// CGContextScaleCTM( cg, 1, -1 );
260 }
261
262 return true;
263}
264
265wxSize wxMetaFile::GetSize() const
266{
267 wxSize dataSize = wxDefaultSize;
268
269 if (Ok())
270 {
271 dataSize.x = M_METAFILEDATA->GetWidth();
272 dataSize.y = M_METAFILEDATA->GetHeight();
273 }
274
275 return dataSize;
276}
277
278// Metafile device context
279
280// New constructor that takes origin and extent. If you use this, don't
281// give origin/extent arguments to wxMakeMetaFilePlaceable.
282
283wxMetafileDCImpl::wxMetafileDCImpl(
284 wxDC *owner,
285 const wxString& filename,
286 int width, int height,
287 const wxString& WXUNUSED(description) ) :
288 wxGCDCImpl( owner )
289{
290 wxASSERT_MSG( width != 0 || height != 0, wxT("no arbitration of metafile size supported") );
291 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
292
293 m_metaFile = new wxMetaFile( filename );
294 wxMetafileRefData* metafiledata = new wxMetafileRefData(width, height);
295 m_metaFile->UnRef();
296 m_metaFile->SetRefData( metafiledata );
297
298 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata->GetContext()));
299 m_ok = (m_graphicContext != NULL) ;
300
301 SetMapMode( wxMM_TEXT );
302}
303
304wxMetafileDCImpl::~wxMetafileDCImpl()
305{
306}
307
308void wxMetafileDCImpl::DoGetSize(int *width, int *height) const
309{
310 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
311
312 wxSize sz = m_metaFile->GetSize();
313 if (width)
314 (*width) = sz.x;
315 if (height)
316 (*height) = sz.y;
317}
318
319wxMetaFile *wxMetafileDCImpl::Close()
320{
5276b0a5 321 wxDELETE(m_graphicContext);
489468fe
SC
322 m_ok = false;
323
324 M_METAFILEREFDATA(*m_metaFile)->Close();
325
326 return m_metaFile;
327}
328
329#if wxUSE_DATAOBJ
330size_t wxMetafileDataObject::GetDataSize() const
331{
332 CFIndex length = 0;
333 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
334 if ( refData )
335 length = refData->GetData().GetLength();
336 return length;
337}
338
339bool wxMetafileDataObject::GetDataHere(void *buf) const
340{
341 bool result = false;
342
343 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
344 if ( refData )
345 {
346 CFIndex length = refData->GetData().GetLength();
347 if ( length > 0 )
348 {
349 result = true ;
350 refData->GetData().GetBytes(CFRangeMake(0,length), (UInt8 *) buf);
351 }
352 }
353 return result;
354}
355
356bool wxMetafileDataObject::SetData(size_t len, const void *buf)
357{
358 wxMetafileRefData* metafiledata = new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8*)buf, len).get()));
359 m_metafile.UnRef();
360 m_metafile.SetRefData( metafiledata );
361 return true;
362}
363#endif
364
365#endif