]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
great copy ctor/assignment operators cleanup by Paul Cornett (patch 1307665)
[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 DrawPicture( pict , &(**pict).picFrame );
182 #endif
183 }
184
185 return true;
186 }
187
188 wxSize wxMetaFile::GetSize() const
189 {
190 wxSize size = wxDefaultSize ;
191
192 if ( Ok() )
193 {
194 PicHandle pict = (PicHandle) GetHMETAFILE() ;
195 Rect &r = (**pict).picFrame ;
196 size.x = r.right - r.left ;
197 size.y = r.bottom - r.top ;
198 }
199
200 return size;
201 }
202
203 // Metafile device context
204
205 // New constructor that takes origin and extent. If you use this, don't
206 // give origin/extent arguments to wxMakeMetaFilePlaceable.
207
208 wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
209 int width , int height ,
210 const wxString& WXUNUSED(description) )
211 {
212 wxASSERT_MSG( width == 0 || height == 0 , wxT("no arbitration of metafilesize supported") );
213 wxASSERT_MSG( filename.empty() , wxT("no file based metafile support yet"));
214
215 m_metaFile = new wxMetaFile( filename );
216
217 #if wxMAC_USE_CORE_GRAPHICS
218 #else
219 Rect r = { 0, 0, height, width };
220
221 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r );
222 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
223
224 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) );
225 ::GetPort( (GrafPtr*) &m_macPort );
226
227 m_ok = true ;
228 #endif
229
230 SetMapMode( wxMM_TEXT );
231 }
232
233 wxMetaFileDC::~wxMetaFileDC()
234 {
235 }
236
237 void wxMetaFileDC::DoGetSize(int *width, int *height) const
238 {
239 wxCHECK_RET( m_metaFile , wxT("GetSize() doesn't work without a metafile") );
240
241 wxSize sz = m_metaFile->GetSize() ;
242 if (width)
243 (*width) = sz.x;
244 if (height)
245 (*height) = sz.y;
246 }
247
248 wxMetaFile *wxMetaFileDC::Close()
249 {
250 ClosePicture() ;
251
252 return m_metaFile;
253 }
254
255 #if wxUSE_DATAOBJ
256 size_t wxMetafileDataObject::GetDataSize() const
257 {
258 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
259 }
260
261 bool wxMetafileDataObject::GetDataHere(void *buf) const
262 {
263 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
264 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) );
265
266 return true;
267 }
268
269 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
270 {
271 Handle handle = NewHandle( len );
272 SetHandleSize( handle, len );
273 memcpy( *handle, buf, len );
274 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
275
276 return true ;
277 }
278 #endif
279
280 #endif