]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/metafile.cpp
cleanup
[wxWidgets.git] / src / mac / carbon / metafile.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
e542ecc6 2// Name: src/mac/carbon/metafile.cpp
e9576ca5 3// Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
a31a5f85 4// Author: Stefan Csomor
e9576ca5
SC
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5 10/////////////////////////////////////////////////////////////////////////////
e542ecc6
DS
11//
12// Currently, the only purpose for making a metafile
13// is to put it on the clipboard.
e9576ca5 14
519cb848 15
e542ecc6 16#include "wx/wxprec.h"
519cb848 17
519cb848
SC
18#if wxUSE_METAFILE
19
20#ifndef WX_PRECOMP
274b7a40
DS
21 #include "wx/utils.h"
22 #include "wx/app.h"
519cb848
SC
23#endif
24
25#include "wx/metafile.h"
e9576ca5 26#include "wx/clipbrd.h"
6239ee05 27#include "wx/mac/uma.h"
8acd14d1 28#include "wx/graphics.h"
76a5e5d2 29
519cb848
SC
30#include <stdio.h>
31#include <string.h>
32
519cb848
SC
33IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
34IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
e9576ca5 35
6239ee05
SC
36#define M_METAFILEREFDATA( a ) ((wxMetafileRefData*)(a).GetRefData())
37
71cc158e
SC
38class wxMetafileRefData: public wxGDIRefData
39{
71cc158e 40public:
6239ee05
SC
41#if wxMAC_USE_CORE_GRAPHICS
42 // creates a metafile from memory, assumes ownership
43 wxMetafileRefData(CFDataRef data);
44#else
45 // creates a metafile from memory, assumes ownership
46 wxMetafileRefData(PicHandle data);
47#endif
48 // prepares a recording metafile
49 wxMetafileRefData( int width, int height);
50 // prepares a metafile to be read from a file (if filename is not empty)
51 wxMetafileRefData( const wxString& filename);
d3c7fc99 52 virtual ~wxMetafileRefData();
71cc158e 53
6239ee05
SC
54 void Init();
55
56 int GetWidth() const { return m_width; }
57 int GetHeight() const { return m_height; }
274b7a40 58
71cc158e 59#if wxMAC_USE_CORE_GRAPHICS
6239ee05
SC
60 CGPDFDocumentRef GetPDFDocument() const { return m_pdfDoc; }
61 void UpdateDocumentFromData() ;
62
63 const wxCFDataRef& GetData() const { return m_data; }
64 CGContextRef GetContext() const { return m_context; }
65#else
66 PicHandle GetHandle() const { return m_metafile; }
71cc158e 67#endif
6239ee05
SC
68 // ends the recording
69 void Close();
70private:
71#if wxMAC_USE_CORE_GRAPHICS
72 wxCFDataRef m_data;
73 wxCFRef<CGPDFDocumentRef> m_pdfDoc;
74 CGContextRef m_context;
75#else
76 PicHandle m_metafile;
77#endif
78 int m_width ;
79 int m_height ;
71cc158e
SC
80};
81
6239ee05
SC
82#if !wxMAC_USE_CORE_GRAPHICS
83wxMetafileRefData::wxMetafileRefData(PicHandle pict)
84{
85 Init();
86 m_metafile = pict;
87
88 Rect r;
89 wxMacGetPictureBounds( m_metafile, &r );
90 m_width = r.right - r.left;
91 m_height = r.bottom - r.top;
92}
93#else
94wxMetafileRefData::wxMetafileRefData(CFDataRef data) :
95 m_data(data)
96{
97 Init();
98 UpdateDocumentFromData();
99}
100#endif
101
102wxMetafileRefData::wxMetafileRefData( const wxString& filename )
519cb848 103{
6239ee05
SC
104 Init();
105#if wxMAC_USE_CORE_GRAPHICS
106 if ( !filename.empty() )
107 {
108 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxMacCFStringHolder(filename)));
109 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
110 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
111 m_pdfDoc.reset(CGPDFDocumentCreateWithURL(url));
112 }
113#else
114 wxASSERT_MSG( filename.empty(), wxT("no file-based metafile support yet") );
e542ecc6 115 m_metafile = NULL;
6239ee05
SC
116#endif
117}
118
119
120wxMetafileRefData::wxMetafileRefData( int width, int height)
121{
122 Init();
274b7a40 123
6239ee05
SC
124 m_width = width;
125 m_height = height;
71cc158e 126#if wxMAC_USE_CORE_GRAPHICS
6239ee05
SC
127 CGRect r = CGRectMake( 0 , 0 , width , height );
128
129 CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
130 m_data.reset(data);
131 CGDataConsumerRef dataConsumer = UMACGDataConsumerCreateWithCFData(data);
132 m_context = CGPDFContextCreate( dataConsumer, (width != 0 && height != 0) ? &r : NULL , NULL );
133 CGDataConsumerRelease( dataConsumer );
134 if ( m_context )
135 {
136#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
137 if ( &CGPDFContextBeginPage != NULL )
138 CGPDFContextBeginPage(m_context, NULL);
139 else
140#endif
141 CGContextBeginPage(m_context, &r);
142
143 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
144
145 CGContextSetFillColorSpace( m_context, genericColorSpace );
146 CGContextSetStrokeColorSpace( m_context, genericColorSpace );
147
148 CGContextTranslateCTM( m_context , 0 , height ) ;
149 CGContextScaleCTM( m_context , 1 , -1 ) ;
150 }
151#else
152 Rect r = { 0, 0, height, width };
153 m_metafile = OpenPicture( &r ) ;
71cc158e 154#endif
519cb848
SC
155}
156
274b7a40 157wxMetafileRefData::~wxMetafileRefData()
519cb848 158{
6239ee05 159#if! wxMAC_USE_CORE_GRAPHICS
519cb848
SC
160 if (m_metafile)
161 {
274b7a40
DS
162 KillPicture( (PicHandle)m_metafile );
163 m_metafile = NULL;
6239ee05
SC
164 }
165#endif
166}
274b7a40 167
6239ee05
SC
168void wxMetafileRefData::Init()
169{
71cc158e 170#if wxMAC_USE_CORE_GRAPHICS
6239ee05
SC
171 m_context = NULL;
172#else
173 m_metafile = NULL;
71cc158e 174#endif
6239ee05
SC
175 m_width = -1;
176 m_height = -1;
519cb848
SC
177}
178
6239ee05 179void wxMetafileRefData::Close()
e9576ca5 180{
6239ee05
SC
181#if wxMAC_USE_CORE_GRAPHICS
182#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
183 if ( &CGPDFContextEndPage != NULL )
184 CGPDFContextEndPage(m_context);
185 else
186#endif
187 CGContextEndPage(m_context);
519cb848 188
6239ee05
SC
189 CGContextRelease(m_context);
190 m_context = NULL;
191
192 UpdateDocumentFromData();
193#else
194 ClosePicture();
195#endif
196}
274b7a40 197
6239ee05
SC
198#if wxMAC_USE_CORE_GRAPHICS
199void wxMetafileRefData::UpdateDocumentFromData()
200{
201 wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(m_data));
202 m_pdfDoc.reset(CGPDFDocumentCreateWithProvider(provider));
203 if ( m_pdfDoc != NULL )
204 {
205 CGPDFPageRef page = CGPDFDocumentGetPage( m_pdfDoc, 1 );
206 CGRect rect = CGPDFPageGetBoxRect ( page, kCGPDFMediaBox);
207 m_width = rect.size.width;
208 m_height = rect.size.height;
209 }
210}
274b7a40 211#endif
6239ee05
SC
212
213wxMetaFile::wxMetaFile(const wxString& file)
214{
215 m_refData = new wxMetafileRefData(file);
e9576ca5
SC
216}
217
218wxMetaFile::~wxMetaFile()
219{
e9576ca5
SC
220}
221
b7cacb43 222bool wxMetaFile::IsOk() const
902725ee 223{
6239ee05
SC
224#if wxMAC_USE_CORE_GRAPHICS
225 return (M_METAFILEDATA && (M_METAFILEDATA->GetData() != NULL));
226#else
227 return (M_METAFILEDATA && (M_METAFILEDATA->GetHandle() != NULL));
228#endif
71cc158e
SC
229}
230
902725ee
WS
231WXHMETAFILE wxMetaFile::GetHMETAFILE() const
232{
6239ee05
SC
233#if wxMAC_USE_CORE_GRAPHICS
234 return (WXHMETAFILE) (CFDataRef) M_METAFILEDATA->GetData();
235#else
236 return (WXHMETAFILE) M_METAFILEDATA->GetHandle();
237#endif
71cc158e
SC
238}
239
89954433 240bool wxMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
e9576ca5 241{
902725ee
WS
242 bool success = true;
243
f0822896 244#if wxUSE_DRAG_AND_DROP
e542ecc6 245 if (m_refData == NULL)
902725ee
WS
246 return false;
247
274b7a40 248 bool alreadyOpen = wxTheClipboard->IsOpened();
e9576ca5
SC
249 if (!alreadyOpen)
250 {
f0822896 251 wxTheClipboard->Open();
a07c1212 252 wxTheClipboard->Clear();
e9576ca5 253 }
274b7a40
DS
254
255 wxDataObject *data = new wxMetafileDataObject( *this );
256 success = wxTheClipboard->SetData( data );
902725ee 257 if (!alreadyOpen)
e40298d5 258 wxTheClipboard->Close();
f0822896 259#endif
902725ee
WS
260
261 return success;
e9576ca5
SC
262}
263
76a5e5d2 264void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
2f1ae414 265{
274b7a40 266 UnRef();
902725ee 267
6239ee05
SC
268#if wxMAC_USE_CORE_GRAPHICS
269 m_refData = new wxMetafileRefData((CFDataRef)mf);
270#else
271 m_refData = new wxMetafileRefData((PicHandle)mf);
272#endif
273}
2f1ae414 274
6239ee05
SC
275void wxMetafile::SetPICT(void* pictHandle)
276{
277 UnRef();
274b7a40 278
71cc158e 279#if wxMAC_USE_CORE_GRAPHICS
6239ee05
SC
280 Handle picHandle = (Handle) pictHandle;
281 HLock(picHandle);
282 CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) *picHandle, GetHandleSize(picHandle), kCFAllocatorNull);
283 wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(data));
284 QDPictRef pictRef = QDPictCreateWithProvider(provider);
285 CGRect rect = QDPictGetBounds(pictRef);
286 m_refData = new wxMetafileRefData( rect.size.width, rect.size.height );
287 QDPictDrawToCGContext( ((wxMetafileRefData*) m_refData)->GetContext(), rect, pictRef );
288 CFRelease( data );
289 QDPictRelease( pictRef );
290 ((wxMetafileRefData*) m_refData)->Close();
291#else
292 m_refData = new wxMetafileRefData((PicHandle)pictHandle);
4f74e0d1 293#endif
2f1ae414
SC
294}
295
e9576ca5
SC
296bool wxMetaFile::Play(wxDC *dc)
297{
e40298d5 298 if (!m_refData)
902725ee
WS
299 return false;
300
e542ecc6 301 if (!dc->Ok())
902725ee
WS
302 return false;
303
e40298d5 304 {
20b69855 305#if wxMAC_USE_CORE_GRAPHICS
be01a403 306 CGContextRef cg = (CGContextRef) dc->GetGraphicsContext()->GetNativeContext();
6239ee05
SC
307 CGPDFDocumentRef doc = M_METAFILEDATA->GetPDFDocument();
308 CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 );
309 wxMacCGContextStateSaver save(cg);
310 CGContextDrawPDFPage( cg, page );
311// CGContextTranslateCTM( cg, 0, bounds.size.width );
312// CGContextScaleCTM( cg, 1, -1 );
20b69855 313#else
e542ecc6 314 PicHandle pict = (PicHandle)GetHMETAFILE();
274b7a40 315 wxMacPortSetter helper( dc );
e542ecc6
DS
316 Rect picFrame;
317 DrawPicture( pict, wxMacGetPictureBounds( pict, &picFrame ) );
20b69855 318#endif
e40298d5 319 }
274b7a40 320
902725ee 321 return true;
e9576ca5
SC
322}
323
48de597b
SC
324wxSize wxMetaFile::GetSize() const
325{
e542ecc6 326 wxSize dataSize = wxDefaultSize;
274b7a40 327
e542ecc6 328 if (Ok())
48de597b 329 {
6239ee05
SC
330 dataSize.x = M_METAFILEDATA->GetWidth();
331 dataSize.y = M_METAFILEDATA->GetHeight();
48de597b
SC
332 }
333
e542ecc6 334 return dataSize;
48de597b
SC
335}
336
274b7a40 337// Metafile device context
e9576ca5 338
e9576ca5
SC
339// New constructor that takes origin and extent. If you use this, don't
340// give origin/extent arguments to wxMakeMetaFilePlaceable.
519cb848 341
e542ecc6
DS
342wxMetaFileDC::wxMetaFileDC(
343 const wxString& filename,
344 int width, int height,
345 const wxString& WXUNUSED(description) )
e9576ca5 346{
c97c0054 347 wxASSERT_MSG( width != 0 || height != 0, wxT("no arbitration of metafile size supported") );
e542ecc6 348 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
274b7a40
DS
349
350 m_metaFile = new wxMetaFile( filename );
6239ee05
SC
351 wxMetafileRefData* metafiledata = new wxMetafileRefData(width, height);
352 m_metaFile->UnRef();
353 m_metaFile->SetRefData( metafiledata );
20b69855 354#if wxMAC_USE_CORE_GRAPHICS
6239ee05
SC
355 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata->GetContext()));
356 m_ok = (m_graphicContext != NULL) ;
20b69855 357#else
274b7a40 358 Rect r = { 0, 0, height, width };
e542ecc6
DS
359 RectRgn( (RgnHandle)m_macBoundaryClipRgn, &r );
360 CopyRgn( (RgnHandle)m_macBoundaryClipRgn, (RgnHandle)m_macCurrentClipRgn );
e542ecc6 361 ::GetPort( (GrafPtr*)&m_macPort );
e542ecc6 362 m_ok = true;
20b69855 363#endif
274b7a40
DS
364
365 SetMapMode( wxMM_TEXT );
e9576ca5
SC
366}
367
519cb848 368wxMetaFileDC::~wxMetaFileDC()
e9576ca5 369{
e9576ca5
SC
370}
371
48de597b
SC
372void wxMetaFileDC::DoGetSize(int *width, int *height) const
373{
e542ecc6 374 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
48de597b 375
e542ecc6 376 wxSize sz = m_metaFile->GetSize();
274b7a40
DS
377 if (width)
378 (*width) = sz.x;
379 if (height)
380 (*height) = sz.y;
48de597b
SC
381}
382
519cb848 383wxMetaFile *wxMetaFileDC::Close()
e9576ca5 384{
6239ee05
SC
385#if wxMAC_USE_CORE_GRAPHICS
386 delete m_graphicContext;
387 m_graphicContext = NULL;
388 m_ok = false;
4f74e0d1 389#endif
6239ee05
SC
390
391 M_METAFILEREFDATA(*m_metaFile)->Close();
392
e40298d5 393 return m_metaFile;
e9576ca5
SC
394}
395
a07c1212
SC
396#if wxUSE_DATAOBJ
397size_t wxMetafileDataObject::GetDataSize() const
398{
6239ee05
SC
399#if wxMAC_USE_CORE_GRAPHICS
400 CFIndex length = 0;
401 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
402 if ( refData )
403 length = refData->GetData().GetLength();
404 return length;
405#else
274b7a40 406 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
6239ee05 407#endif
a07c1212
SC
408}
409
410bool wxMetafileDataObject::GetDataHere(void *buf) const
411{
6239ee05
SC
412 bool result = false;
413#if wxMAC_USE_CORE_GRAPHICS
414 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
415 if ( refData )
416 {
417 CFIndex length = refData->GetData().GetLength();
418 if ( length > 0 )
419 {
420 result = true ;
421 refData->GetData().GetBytes(CFRangeMake(0,length), (UInt8 *) buf);
422 }
423 }
424#else
e542ecc6 425 Handle pictH = (Handle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE();
6239ee05 426 result = (pictH != NULL);
274b7a40 427
e542ecc6
DS
428 if (result)
429 memcpy( buf, *pictH, GetHandleSize( pictH ) );
430
6239ee05 431#endif
e542ecc6 432 return result;
a07c1212
SC
433}
434
435bool wxMetafileDataObject::SetData(size_t len, const void *buf)
436{
6239ee05
SC
437#if wxMAC_USE_CORE_GRAPHICS
438 wxMetafileRefData* metafiledata = new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8*)buf, len).get()));
439 m_metafile.UnRef();
440 m_metafile.SetRefData( metafiledata );
441#else
274b7a40
DS
442 Handle handle = NewHandle( len );
443 SetHandleSize( handle, len );
444 memcpy( *handle, buf, len );
445 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
6239ee05 446#endif
e542ecc6 447 return true;
a07c1212
SC
448}
449#endif
450
e9576ca5 451#endif