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