]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
2b2a1998217179793fa8f2a5ab0f29d2736a842c
[wxWidgets.git] / src / mac / carbon / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/metafile.cpp
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"
27 #include "wx/mac/uma.h"
28 #include "wx/graphics.h"
29
30 #include <stdio.h>
31 #include <string.h>
32
33 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
34 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
35
36 #define M_METAFILEREFDATA( a ) ((wxMetafileRefData*)(a).GetRefData())
37
38 class wxMetafileRefData: public wxGDIRefData
39 {
40 public:
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);
52 virtual ~wxMetafileRefData();
53
54 void Init();
55
56 int GetWidth() const { return m_width; }
57 int GetHeight() const { return m_height; }
58
59 #if wxMAC_USE_CORE_GRAPHICS
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; }
67 #endif
68 // ends the recording
69 void Close();
70 private:
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 ;
80 };
81
82 #if !wxMAC_USE_CORE_GRAPHICS
83 wxMetafileRefData::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
94 wxMetafileRefData::wxMetafileRefData(CFDataRef data) :
95 m_data(data)
96 {
97 Init();
98 UpdateDocumentFromData();
99 }
100 #endif
101
102 wxMetafileRefData::wxMetafileRefData( const wxString& filename )
103 {
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") );
115 m_metafile = NULL;
116 #endif
117 }
118
119
120 wxMetafileRefData::wxMetafileRefData( int width, int height)
121 {
122 Init();
123
124 m_width = width;
125 m_height = height;
126 #if wxMAC_USE_CORE_GRAPHICS
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 ) ;
154 #endif
155 }
156
157 wxMetafileRefData::~wxMetafileRefData()
158 {
159 #if! wxMAC_USE_CORE_GRAPHICS
160 if (m_metafile)
161 {
162 KillPicture( (PicHandle)m_metafile );
163 m_metafile = NULL;
164 }
165 #endif
166 }
167
168 void wxMetafileRefData::Init()
169 {
170 #if wxMAC_USE_CORE_GRAPHICS
171 m_context = NULL;
172 #else
173 m_metafile = NULL;
174 #endif
175 m_width = -1;
176 m_height = -1;
177 }
178
179 void wxMetafileRefData::Close()
180 {
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);
188
189 CGContextRelease(m_context);
190 m_context = NULL;
191
192 UpdateDocumentFromData();
193 #else
194 ClosePicture();
195 #endif
196 }
197
198 #if wxMAC_USE_CORE_GRAPHICS
199 void 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 = wx_static_cast(int, rect.size.width);
208 m_height = wx_static_cast(int, rect.size.height);
209 }
210 }
211 #endif
212
213 wxMetaFile::wxMetaFile(const wxString& file)
214 {
215 m_refData = new wxMetafileRefData(file);
216 }
217
218 wxMetaFile::~wxMetaFile()
219 {
220 }
221
222 bool wxMetaFile::IsOk() const
223 {
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
229 }
230
231 WXHMETAFILE wxMetaFile::GetHMETAFILE() const
232 {
233 #if wxMAC_USE_CORE_GRAPHICS
234 return (WXHMETAFILE) (CFDataRef) M_METAFILEDATA->GetData();
235 #else
236 return (WXHMETAFILE) M_METAFILEDATA->GetHandle();
237 #endif
238 }
239
240 bool wxMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
241 {
242 bool success = true;
243
244 #if wxUSE_DRAG_AND_DROP
245 if (m_refData == NULL)
246 return false;
247
248 bool alreadyOpen = wxTheClipboard->IsOpened();
249 if (!alreadyOpen)
250 {
251 wxTheClipboard->Open();
252 wxTheClipboard->Clear();
253 }
254
255 wxDataObject *data = new wxMetafileDataObject( *this );
256 success = wxTheClipboard->SetData( data );
257 if (!alreadyOpen)
258 wxTheClipboard->Close();
259 #endif
260
261 return success;
262 }
263
264 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
265 {
266 UnRef();
267
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 }
274
275 void wxMetafile::SetPICT(void* pictHandle)
276 {
277 UnRef();
278
279 #if wxMAC_USE_CORE_GRAPHICS
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(wx_static_cast(int, rect.size.width),
287 wx_static_cast(int, rect.size.height));
288 QDPictDrawToCGContext( ((wxMetafileRefData*) m_refData)->GetContext(), rect, pictRef );
289 CFRelease( data );
290 QDPictRelease( pictRef );
291 ((wxMetafileRefData*) m_refData)->Close();
292 #else
293 m_refData = new wxMetafileRefData((PicHandle)pictHandle);
294 #endif
295 }
296
297 bool wxMetaFile::Play(wxDC *dc)
298 {
299 if (!m_refData)
300 return false;
301
302 if (!dc->Ok())
303 return false;
304
305 {
306 #if wxMAC_USE_CORE_GRAPHICS
307 CGContextRef cg = (CGContextRef) dc->GetGraphicsContext()->GetNativeContext();
308 CGPDFDocumentRef doc = M_METAFILEDATA->GetPDFDocument();
309 CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 );
310 wxMacCGContextStateSaver save(cg);
311 CGContextDrawPDFPage( cg, page );
312 // CGContextTranslateCTM( cg, 0, bounds.size.width );
313 // CGContextScaleCTM( cg, 1, -1 );
314 #else
315 PicHandle pict = (PicHandle)GetHMETAFILE();
316 wxMacPortSetter helper( dc );
317 Rect picFrame;
318 DrawPicture( pict, wxMacGetPictureBounds( pict, &picFrame ) );
319 #endif
320 }
321
322 return true;
323 }
324
325 wxSize wxMetaFile::GetSize() const
326 {
327 wxSize dataSize = wxDefaultSize;
328
329 if (Ok())
330 {
331 dataSize.x = M_METAFILEDATA->GetWidth();
332 dataSize.y = M_METAFILEDATA->GetHeight();
333 }
334
335 return dataSize;
336 }
337
338 // Metafile device context
339
340 // New constructor that takes origin and extent. If you use this, don't
341 // give origin/extent arguments to wxMakeMetaFilePlaceable.
342
343 wxMetaFileDC::wxMetaFileDC(
344 const wxString& filename,
345 int width, int height,
346 const wxString& WXUNUSED(description) )
347 {
348 wxASSERT_MSG( width != 0 || height != 0, wxT("no arbitration of metafile size supported") );
349 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
350
351 m_metaFile = new wxMetaFile( filename );
352 wxMetafileRefData* metafiledata = new wxMetafileRefData(width, height);
353 m_metaFile->UnRef();
354 m_metaFile->SetRefData( metafiledata );
355 #if wxMAC_USE_CORE_GRAPHICS
356 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata->GetContext()));
357 m_ok = (m_graphicContext != NULL) ;
358 #else
359 Rect r = { 0, 0, height, width };
360 RectRgn( (RgnHandle)m_macBoundaryClipRgn, &r );
361 CopyRgn( (RgnHandle)m_macBoundaryClipRgn, (RgnHandle)m_macCurrentClipRgn );
362 ::GetPort( (GrafPtr*)&m_macPort );
363 m_ok = true;
364 #endif
365
366 SetMapMode( wxMM_TEXT );
367 }
368
369 wxMetaFileDC::~wxMetaFileDC()
370 {
371 }
372
373 void wxMetaFileDC::DoGetSize(int *width, int *height) const
374 {
375 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
376
377 wxSize sz = m_metaFile->GetSize();
378 if (width)
379 (*width) = sz.x;
380 if (height)
381 (*height) = sz.y;
382 }
383
384 wxMetaFile *wxMetaFileDC::Close()
385 {
386 #if wxMAC_USE_CORE_GRAPHICS
387 delete m_graphicContext;
388 m_graphicContext = NULL;
389 m_ok = false;
390 #endif
391
392 M_METAFILEREFDATA(*m_metaFile)->Close();
393
394 return m_metaFile;
395 }
396
397 #if wxUSE_DATAOBJ
398 size_t wxMetafileDataObject::GetDataSize() const
399 {
400 #if wxMAC_USE_CORE_GRAPHICS
401 CFIndex length = 0;
402 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
403 if ( refData )
404 length = refData->GetData().GetLength();
405 return length;
406 #else
407 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
408 #endif
409 }
410
411 bool wxMetafileDataObject::GetDataHere(void *buf) const
412 {
413 bool result = false;
414 #if wxMAC_USE_CORE_GRAPHICS
415 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
416 if ( refData )
417 {
418 CFIndex length = refData->GetData().GetLength();
419 if ( length > 0 )
420 {
421 result = true ;
422 refData->GetData().GetBytes(CFRangeMake(0,length), (UInt8 *) buf);
423 }
424 }
425 #else
426 Handle pictH = (Handle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE();
427 result = (pictH != NULL);
428
429 if (result)
430 memcpy( buf, *pictH, GetHandleSize( pictH ) );
431
432 #endif
433 return result;
434 }
435
436 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
437 {
438 #if wxMAC_USE_CORE_GRAPHICS
439 wxMetafileRefData* metafiledata = new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8*)buf, len).get()));
440 m_metafile.UnRef();
441 m_metafile.SetRefData( metafiledata );
442 #else
443 Handle handle = NewHandle( len );
444 SetHandleSize( handle, len );
445 memcpy( *handle, buf, len );
446 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
447 #endif
448 return true;
449 }
450 #endif
451
452 #endif