]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
ef560d445d5277095452cdf2cc82727cc68c594e
[wxWidgets.git] / src / mac / carbon / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
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/private.h"
28
29 #include <stdio.h>
30 #include <string.h>
31
32 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
33 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
34
35 class wxMetafileRefData: public wxGDIRefData
36 {
37 friend class WXDLLEXPORT wxMetafile;
38
39 public:
40 wxMetafileRefData();
41 ~wxMetafileRefData();
42
43 private:
44 PicHandle m_metafile;
45
46 #if wxMAC_USE_CORE_GRAPHICS
47 QDPictRef m_qdPictRef;
48 #endif
49 };
50
51 extern bool wxClipboardIsOpen;
52
53
54 // Metafiles:
55 // Currently, the only purpose for making a metafile
56 // is to put it on the clipboard.
57
58 wxMetafileRefData::wxMetafileRefData()
59 {
60 m_metafile = 0;
61
62 #if wxMAC_USE_CORE_GRAPHICS
63 m_qdPictRef = NULL;
64 #endif
65 }
66
67 wxMetafileRefData::~wxMetafileRefData()
68 {
69 if (m_metafile)
70 {
71 KillPicture( (PicHandle)m_metafile );
72 m_metafile = NULL;
73
74 #if wxMAC_USE_CORE_GRAPHICS
75 QDPictRelease( m_qdPictRef );
76 m_qdPictRef = NULL;
77 #endif
78 }
79 }
80
81 wxMetaFile::wxMetaFile(const wxString& file)
82 {
83 m_refData = new wxMetafileRefData;
84
85 M_METAFILEDATA->m_metafile = 0;
86 wxASSERT_MSG( file.empty(), wxT("no file based metafile support yet") );
87
88 #if 0
89 if (!file.IsNull() && (file.Cmp("") == 0))
90 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile( file );
91 #endif
92 }
93
94 wxMetaFile::~wxMetaFile()
95 {
96 }
97
98 bool wxMetaFile::Ok() const
99 {
100 return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0));
101 }
102
103 WXHMETAFILE wxMetaFile::GetHMETAFILE() const
104 {
105 return (WXHMETAFILE) M_METAFILEDATA->m_metafile;
106 }
107
108 bool wxMetaFile::SetClipboard(int width, int height)
109 {
110 bool success = true;
111
112 #if wxUSE_DRAG_AND_DROP
113 // TODO: to finish this port, we need the data obj first
114 if (!m_refData)
115 return false;
116
117 bool alreadyOpen = wxTheClipboard->IsOpened();
118 if (!alreadyOpen)
119 {
120 wxTheClipboard->Open();
121 wxTheClipboard->Clear();
122 }
123
124 wxDataObject *data = new wxMetafileDataObject( *this );
125 success = wxTheClipboard->SetData( data );
126 if (!alreadyOpen)
127 wxTheClipboard->Close();
128 #endif
129
130 return success;
131 }
132
133 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
134 {
135 UnRef();
136
137 m_refData = new wxMetafileRefData;
138
139 M_METAFILEDATA->m_metafile = (PicHandle) mf;
140
141 #if wxMAC_USE_CORE_GRAPHICS
142 size_t sz = GetHandleSize( (Handle) M_METAFILEDATA->m_metafile );
143 wxMemoryBuffer* membuf = new wxMemoryBuffer( sz );
144 void *data = membuf->GetWriteBuf( sz );
145 memcpy( data, *M_METAFILEDATA->m_metafile, sz );
146 membuf->UngetWriteBuf( sz );
147 CGDataProviderRef provider = CGDataProviderCreateWithData(
148 membuf, data, sz, wxMacMemoryBufferReleaseProc );
149 M_METAFILEDATA->m_qdPictRef = NULL;
150
151 if ( provider != NULL )
152 {
153 M_METAFILEDATA->m_qdPictRef = QDPictCreateWithProvider( provider );
154 CGDataProviderRelease( provider );
155 }
156 #endif
157 }
158
159 bool wxMetaFile::Play(wxDC *dc)
160 {
161 if (!m_refData)
162 return false;
163
164 if (!dc->Ok() )
165 return false;
166
167 {
168 #if wxMAC_USE_CORE_GRAPHICS
169 QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef;
170 CGContextRef cg = ((wxMacCGContext*)(dc->GetGraphicContext()))->GetNativeContext();
171 CGRect bounds = QDPictGetBounds( cgPictRef );
172
173 CGContextSaveGState( cg );
174 CGContextTranslateCTM( cg, 0 , bounds.size.width );
175 CGContextScaleCTM( cg, 1, -1 );
176 QDPictDrawToCGContext( cg , bounds, cgPictRef );
177 CGContextRestoreGState( cg );
178 #else
179 PicHandle pict = (PicHandle) GetHMETAFILE();
180 wxMacPortSetter helper( dc );
181 Rect picFrame ;
182 DrawPicture( pict , wxMacGetPictureBounds( pict , &picFrame ) );
183 #endif
184 }
185
186 return true;
187 }
188
189 wxSize wxMetaFile::GetSize() const
190 {
191 wxSize size = wxDefaultSize ;
192
193 if ( Ok() )
194 {
195 PicHandle pict = (PicHandle) GetHMETAFILE() ;
196 Rect r ;
197 wxMacGetPictureBounds( pict , &r ) ;
198 size.x = r.right - r.left ;
199 size.y = r.bottom - r.top ;
200 }
201
202 return size;
203 }
204
205 // Metafile device context
206
207 // New constructor that takes origin and extent. If you use this, don't
208 // give origin/extent arguments to wxMakeMetaFilePlaceable.
209
210 wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
211 int width , int height ,
212 const wxString& WXUNUSED(description) )
213 {
214 wxASSERT_MSG( width == 0 || height == 0 , wxT("no arbitration of metafilesize supported") );
215 wxASSERT_MSG( filename.empty() , wxT("no file based metafile support yet"));
216
217 m_metaFile = new wxMetaFile( filename );
218
219 #if wxMAC_USE_CORE_GRAPHICS
220 #else
221 Rect r = { 0, 0, height, width };
222
223 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r );
224 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
225
226 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) );
227 ::GetPort( (GrafPtr*) &m_macPort );
228
229 m_ok = true ;
230 #endif
231
232 SetMapMode( wxMM_TEXT );
233 }
234
235 wxMetaFileDC::~wxMetaFileDC()
236 {
237 }
238
239 void wxMetaFileDC::DoGetSize(int *width, int *height) const
240 {
241 wxCHECK_RET( m_metaFile , wxT("GetSize() doesn't work without a metafile") );
242
243 wxSize sz = m_metaFile->GetSize() ;
244 if (width)
245 (*width) = sz.x;
246 if (height)
247 (*height) = sz.y;
248 }
249
250 wxMetaFile *wxMetaFileDC::Close()
251 {
252 ClosePicture() ;
253
254 return m_metaFile;
255 }
256
257 #if wxUSE_DATAOBJ
258 size_t wxMetafileDataObject::GetDataSize() const
259 {
260 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
261 }
262
263 bool wxMetafileDataObject::GetDataHere(void *buf) const
264 {
265 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
266 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) );
267
268 return true;
269 }
270
271 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
272 {
273 Handle handle = NewHandle( len );
274 SetHandleSize( handle, len );
275 memcpy( *handle, buf, len );
276 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
277
278 return true ;
279 }
280 #endif
281
282 #endif