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