]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
cleanup mac
[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 = UMACGDataConsumerCreateWithCFData(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(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);
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 void wxMetafile::SetPICT(void* pictHandle)
205 {
206 UnRef();
207
208 Handle picHandle = (Handle) pictHandle;
209 HLock(picHandle);
210 CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) *picHandle, GetHandleSize(picHandle), kCFAllocatorNull);
211 wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(data));
212 QDPictRef pictRef = QDPictCreateWithProvider(provider);
213 CGRect rect = QDPictGetBounds(pictRef);
214 m_refData = new wxMetafileRefData(wx_static_cast(int, rect.size.width),
215 wx_static_cast(int, rect.size.height));
216 QDPictDrawToCGContext( ((wxMetafileRefData*) m_refData)->GetContext(), rect, pictRef );
217 CFRelease( data );
218 QDPictRelease( pictRef );
219 ((wxMetafileRefData*) m_refData)->Close();
220 }
221
222 bool wxMetaFile::Play(wxDC *dc)
223 {
224 if (!m_refData)
225 return false;
226
227 if (!dc->Ok())
228 return false;
229
230 {
231 CGContextRef cg = (CGContextRef) dc->GetGraphicsContext()->GetNativeContext();
232 CGPDFDocumentRef doc = M_METAFILEDATA->GetPDFDocument();
233 CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 );
234 wxMacCGContextStateSaver save(cg);
235 CGContextDrawPDFPage( cg, page );
236 // CGContextTranslateCTM( cg, 0, bounds.size.width );
237 // CGContextScaleCTM( cg, 1, -1 );
238 }
239
240 return true;
241 }
242
243 wxSize wxMetaFile::GetSize() const
244 {
245 wxSize dataSize = wxDefaultSize;
246
247 if (Ok())
248 {
249 dataSize.x = M_METAFILEDATA->GetWidth();
250 dataSize.y = M_METAFILEDATA->GetHeight();
251 }
252
253 return dataSize;
254 }
255
256 // Metafile device context
257
258 // New constructor that takes origin and extent. If you use this, don't
259 // give origin/extent arguments to wxMakeMetaFilePlaceable.
260
261 wxMetaFileDC::wxMetaFileDC(
262 const wxString& filename,
263 int width, int height,
264 const wxString& WXUNUSED(description) )
265 {
266 wxASSERT_MSG( width != 0 || height != 0, wxT("no arbitration of metafile size supported") );
267 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
268
269 m_metaFile = new wxMetaFile( filename );
270 wxMetafileRefData* metafiledata = new wxMetafileRefData(width, height);
271 m_metaFile->UnRef();
272 m_metaFile->SetRefData( metafiledata );
273
274 SetGraphicsContext( wxGraphicsContext::CreateFromNative(metafiledata->GetContext()));
275 m_ok = (m_graphicContext != NULL) ;
276
277 SetMapMode( wxMM_TEXT );
278 }
279
280 wxMetaFileDC::~wxMetaFileDC()
281 {
282 }
283
284 void wxMetaFileDC::DoGetSize(int *width, int *height) const
285 {
286 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
287
288 wxSize sz = m_metaFile->GetSize();
289 if (width)
290 (*width) = sz.x;
291 if (height)
292 (*height) = sz.y;
293 }
294
295 wxMetaFile *wxMetaFileDC::Close()
296 {
297 delete m_graphicContext;
298 m_graphicContext = NULL;
299 m_ok = false;
300
301 M_METAFILEREFDATA(*m_metaFile)->Close();
302
303 return m_metaFile;
304 }
305
306 #if wxUSE_DATAOBJ
307 size_t wxMetafileDataObject::GetDataSize() const
308 {
309 CFIndex length = 0;
310 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
311 if ( refData )
312 length = refData->GetData().GetLength();
313 return length;
314 }
315
316 bool wxMetafileDataObject::GetDataHere(void *buf) const
317 {
318 bool result = false;
319
320 wxMetafileRefData* refData = M_METAFILEREFDATA(m_metafile);
321 if ( refData )
322 {
323 CFIndex length = refData->GetData().GetLength();
324 if ( length > 0 )
325 {
326 result = true ;
327 refData->GetData().GetBytes(CFRangeMake(0,length), (UInt8 *) buf);
328 }
329 }
330 return result;
331 }
332
333 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
334 {
335 wxMetafileRefData* metafiledata = new wxMetafileRefData(wxCFRefFromGet(wxCFDataRef((UInt8*)buf, len).get()));
336 m_metafile.UnRef();
337 m_metafile.SetRefData( metafiledata );
338 return true;
339 }
340 #endif
341
342 #endif