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