]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/metafile.cpp
unload msimg32.dll earlier (before static cleanup time) to avoid lockups when wx...
[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 41 // creates a metafile from memory, assumes ownership
31c1cbc7 42 wxMetafileRefData(CFDataRef data);
6239ee05 43 // prepares a recording metafile
31c1cbc7 44 wxMetafileRefData( int width, int height);
6239ee05 45 // prepares a metafile to be read from a file (if filename is not empty)
31c1cbc7 46 wxMetafileRefData( const wxString& filename);
d3c7fc99 47 virtual ~wxMetafileRefData();
71cc158e 48
6239ee05
SC
49 void Init();
50
51 int GetWidth() const { return m_width; }
52 int GetHeight() const { return m_height; }
274b7a40 53
6239ee05
SC
54 CGPDFDocumentRef GetPDFDocument() const { return m_pdfDoc; }
55 void UpdateDocumentFromData() ;
56
57 const wxCFDataRef& GetData() const { return m_data; }
58 CGContextRef GetContext() const { return m_context; }
315e9e5a 59
6239ee05
SC
60 // ends the recording
61 void Close();
62private:
6239ee05
SC
63 wxCFDataRef m_data;
64 wxCFRef<CGPDFDocumentRef> m_pdfDoc;
65 CGContextRef m_context;
315e9e5a 66
6239ee05
SC
67 int m_width ;
68 int m_height ;
71cc158e
SC
69};
70
6239ee05
SC
71wxMetafileRefData::wxMetafileRefData(CFDataRef data) :
72 m_data(data)
73{
74 Init();
75 UpdateDocumentFromData();
76}
6239ee05
SC
77
78wxMetafileRefData::wxMetafileRefData( const wxString& filename )
519cb848 79{
6239ee05 80 Init();
315e9e5a 81
6239ee05
SC
82 if ( !filename.empty() )
83 {
84 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxMacCFStringHolder(filename)));
85 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
86 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
87 m_pdfDoc.reset(CGPDFDocumentCreateWithURL(url));
88 }
6239ee05
SC
89}
90
91
92wxMetafileRefData::wxMetafileRefData( int width, int height)
93{
94 Init();
274b7a40 95
6239ee05
SC
96 m_width = width;
97 m_height = height;
315e9e5a 98
6239ee05
SC
99 CGRect r = CGRectMake( 0 , 0 , width , height );
100
101 CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
102 m_data.reset(data);
103 CGDataConsumerRef dataConsumer = UMACGDataConsumerCreateWithCFData(data);
104 m_context = CGPDFContextCreate( dataConsumer, (width != 0 && height != 0) ? &r : NULL , NULL );
105 CGDataConsumerRelease( dataConsumer );
106 if ( m_context )
107 {
315e9e5a 108 CGPDFContextBeginPage(m_context, NULL);
6239ee05
SC
109
110 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
111
112 CGContextSetFillColorSpace( m_context, genericColorSpace );
113 CGContextSetStrokeColorSpace( m_context, genericColorSpace );
114
115 CGContextTranslateCTM( m_context , 0 , height ) ;
116 CGContextScaleCTM( m_context , 1 , -1 ) ;
117 }
519cb848
SC
118}
119
274b7a40 120wxMetafileRefData::~wxMetafileRefData()
519cb848 121{
6239ee05 122}
274b7a40 123
6239ee05
SC
124void wxMetafileRefData::Init()
125{
6239ee05 126 m_context = NULL;
6239ee05
SC
127 m_width = -1;
128 m_height = -1;
519cb848
SC
129}
130
6239ee05 131void wxMetafileRefData::Close()
e9576ca5 132{
315e9e5a 133 CGPDFContextEndPage(m_context);
519cb848 134
6239ee05
SC
135 CGContextRelease(m_context);
136 m_context = NULL;
31c1cbc7 137
6239ee05 138 UpdateDocumentFromData();
6239ee05 139}
274b7a40 140
31c1cbc7 141void wxMetafileRefData::UpdateDocumentFromData()
6239ee05
SC
142{
143 wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(m_data));
144 m_pdfDoc.reset(CGPDFDocumentCreateWithProvider(provider));
145 if ( m_pdfDoc != NULL )
146 {
147 CGPDFPageRef page = CGPDFDocumentGetPage( m_pdfDoc, 1 );
148 CGRect rect = CGPDFPageGetBoxRect ( page, kCGPDFMediaBox);
31c1cbc7
VZ
149 m_width = wx_static_cast(int, rect.size.width);
150 m_height = wx_static_cast(int, rect.size.height);
6239ee05
SC
151 }
152}
6239ee05
SC
153
154wxMetaFile::wxMetaFile(const wxString& file)
155{
156 m_refData = new wxMetafileRefData(file);
e9576ca5
SC
157}
158
159wxMetaFile::~wxMetaFile()
160{
e9576ca5
SC
161}
162
b7cacb43 163bool wxMetaFile::IsOk() const
902725ee 164{
6239ee05 165 return (M_METAFILEDATA && (M_METAFILEDATA->GetData() != NULL));
71cc158e
SC
166}
167
902725ee
WS
168WXHMETAFILE wxMetaFile::GetHMETAFILE() const
169{
6239ee05 170 return (WXHMETAFILE) (CFDataRef) M_METAFILEDATA->GetData();
71cc158e
SC
171}
172
89954433 173bool wxMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
e9576ca5 174{
902725ee
WS
175 bool success = true;
176
f0822896 177#if wxUSE_DRAG_AND_DROP
e542ecc6 178 if (m_refData == NULL)
902725ee
WS
179 return false;
180
274b7a40 181 bool alreadyOpen = wxTheClipboard->IsOpened();
e9576ca5
SC
182 if (!alreadyOpen)
183 {
f0822896 184 wxTheClipboard->Open();
a07c1212 185 wxTheClipboard->Clear();
e9576ca5 186 }
274b7a40
DS
187
188 wxDataObject *data = new wxMetafileDataObject( *this );
189 success = wxTheClipboard->SetData( data );
902725ee 190 if (!alreadyOpen)
e40298d5 191 wxTheClipboard->Close();
f0822896 192#endif
902725ee
WS
193
194 return success;
e9576ca5
SC
195}
196
76a5e5d2 197void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
2f1ae414 198{
274b7a40 199 UnRef();
902725ee 200
6239ee05 201 m_refData = new wxMetafileRefData((CFDataRef)mf);
6239ee05 202}
2f1ae414 203
276ee533 204#ifndef __LP64__
6239ee05
SC
205void wxMetafile::SetPICT(void* pictHandle)
206{
207 UnRef();
274b7a40 208
6239ee05
SC
209 Handle picHandle = (Handle) pictHandle;
210 HLock(picHandle);
211 CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) *picHandle, GetHandleSize(picHandle), kCFAllocatorNull);
212 wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(data));
213 QDPictRef pictRef = QDPictCreateWithProvider(provider);
214 CGRect rect = QDPictGetBounds(pictRef);
31c1cbc7
VZ
215 m_refData = new wxMetafileRefData(wx_static_cast(int, rect.size.width),
216 wx_static_cast(int, rect.size.height));
6239ee05
SC
217 QDPictDrawToCGContext( ((wxMetafileRefData*) m_refData)->GetContext(), rect, pictRef );
218 CFRelease( data );
219 QDPictRelease( pictRef );
220 ((wxMetafileRefData*) m_refData)->Close();
2f1ae414 221}
276ee533 222#endif
2f1ae414 223
e9576ca5
SC
224bool wxMetaFile::Play(wxDC *dc)
225{
e40298d5 226 if (!m_refData)
902725ee
WS
227 return false;
228
e542ecc6 229 if (!dc->Ok())
902725ee
WS
230 return false;
231
e40298d5 232 {
be01a403 233 CGContextRef cg = (CGContextRef) dc->GetGraphicsContext()->GetNativeContext();
6239ee05
SC
234 CGPDFDocumentRef doc = M_METAFILEDATA->GetPDFDocument();
235 CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 );
236 wxMacCGContextStateSaver save(cg);
237 CGContextDrawPDFPage( cg, page );
238// CGContextTranslateCTM( cg, 0, bounds.size.width );
239// CGContextScaleCTM( cg, 1, -1 );
e40298d5 240 }
274b7a40 241
902725ee 242 return true;
e9576ca5
SC
243}
244
48de597b
SC
245wxSize wxMetaFile::GetSize() const
246{
e542ecc6 247 wxSize dataSize = wxDefaultSize;
274b7a40 248
e542ecc6 249 if (Ok())
48de597b 250 {
6239ee05
SC
251 dataSize.x = M_METAFILEDATA->GetWidth();
252 dataSize.y = M_METAFILEDATA->GetHeight();
48de597b
SC
253 }
254
e542ecc6 255 return dataSize;
48de597b
SC
256}
257
274b7a40 258// Metafile device context
e9576ca5 259
e9576ca5
SC
260// New constructor that takes origin and extent. If you use this, don't
261// give origin/extent arguments to wxMakeMetaFilePlaceable.
519cb848 262
e542ecc6
DS
263wxMetaFileDC::wxMetaFileDC(
264 const wxString& filename,
265 int width, int height,
266 const wxString& WXUNUSED(description) )
e9576ca5 267{
c97c0054 268 wxASSERT_MSG( width != 0 || height != 0, wxT("no arbitration of metafile size supported") );
e542ecc6 269 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
274b7a40
DS
270
271 m_metaFile = new wxMetaFile( filename );
6239ee05
SC
272 wxMetafileRefData* metafiledata = new wxMetafileRefData(width, height);
273 m_metaFile->UnRef();
274 m_metaFile->SetRefData( metafiledata );
315e9e5a 275
6239ee05
SC
276 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata->GetContext()));
277 m_ok = (m_graphicContext != NULL) ;
274b7a40
DS
278
279 SetMapMode( wxMM_TEXT );
e9576ca5
SC
280}
281
519cb848 282wxMetaFileDC::~wxMetaFileDC()
e9576ca5 283{
e9576ca5
SC
284}
285
48de597b
SC
286void wxMetaFileDC::DoGetSize(int *width, int *height) const
287{
e542ecc6 288 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
48de597b 289
e542ecc6 290 wxSize sz = m_metaFile->GetSize();
274b7a40
DS
291 if (width)
292 (*width) = sz.x;
293 if (height)
294 (*height) = sz.y;
48de597b
SC
295}
296
519cb848 297wxMetaFile *wxMetaFileDC::Close()
e9576ca5 298{
6239ee05
SC
299 delete m_graphicContext;
300 m_graphicContext = NULL;
301 m_ok = false;
6239ee05
SC
302
303 M_METAFILEREFDATA(*m_metaFile)->Close();
304
e40298d5 305 return m_metaFile;
e9576ca5
SC
306}
307
a07c1212
SC
308#if wxUSE_DATAOBJ
309size_t wxMetafileDataObject::GetDataSize() const
310{
6239ee05
SC
311 CFIndex length = 0;
312 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
313 if ( refData )
314 length = refData->GetData().GetLength();
315 return length;
a07c1212
SC
316}
317
318bool wxMetafileDataObject::GetDataHere(void *buf) const
319{
6239ee05 320 bool result = false;
315e9e5a 321
6239ee05
SC
322 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
323 if ( refData )
31c1cbc7 324 {
6239ee05
SC
325 CFIndex length = refData->GetData().GetLength();
326 if ( length > 0 )
327 {
328 result = true ;
329 refData->GetData().GetBytes(CFRangeMake(0,length), (UInt8 *) buf);
330 }
31c1cbc7 331 }
e542ecc6 332 return result;
a07c1212
SC
333}
334
335bool wxMetafileDataObject::SetData(size_t len, const void *buf)
336{
6239ee05
SC
337 wxMetafileRefData* metafiledata = new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8*)buf, len).get()));
338 m_metafile.UnRef();
339 m_metafile.SetRefData( metafiledata );
e542ecc6 340 return true;
a07c1212
SC
341}
342#endif
343
e9576ca5 344#endif