]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
cleanup
[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.IsEmpty() , 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 #if wxUSE_DRAG_AND_DROP
114 //TODO finishi this port , we need the data obj first
115 if (!m_refData)
116 return FALSE;
117
118 bool alreadyOpen=wxTheClipboard->IsOpened() ;
119 if (!alreadyOpen)
120 {
121 wxTheClipboard->Open();
122 wxTheClipboard->Clear();
123 }
124 wxDataObject *data =
125 new wxMetafileDataObject( *this) ;
126 bool success = wxTheClipboard->SetData(data);
127 if (!alreadyOpen)
128 wxTheClipboard->Close();
129 return (bool) success;
130 #endif
131 return TRUE ;
132 }
133
134 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
135 {
136 UnRef() ;
137
138 m_refData = new wxMetafileRefData;
139
140 M_METAFILEDATA->m_metafile = (PicHandle) mf;
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( membuf , data , sz ,
148 wxMacMemoryBufferReleaseProc ) ;
149 M_METAFILEDATA->m_qdPictRef = NULL ;
150 if ( provider != NULL )
151 {
152 M_METAFILEDATA->m_qdPictRef = QDPictCreateWithProvider( provider ) ;
153 CGDataProviderRelease( provider ) ;
154 }
155 #endif
156 }
157
158 bool wxMetaFile::Play(wxDC *dc)
159 {
160 if (!m_refData)
161 return FALSE;
162
163 if (!dc->Ok() )
164 return FALSE;
165
166 {
167 #if wxMAC_USE_CORE_GRAPHICS
168 QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef ;
169 CGContextRef cg = ((wxMacCGContext*)(dc->GetGraphicContext()))->GetNativeContext() ;
170 CGRect bounds = QDPictGetBounds( cgPictRef ) ;
171
172 CGContextSaveGState(cg);
173 CGContextTranslateCTM(cg, 0 , bounds.size.width );
174 CGContextScaleCTM(cg, 1, -1);
175 QDPictDrawToCGContext( cg , bounds , cgPictRef ) ;
176 CGContextRestoreGState( cg ) ;
177 #else
178 PicHandle pict = (PicHandle) GetHMETAFILE() ;
179 wxMacPortSetter helper( dc ) ;
180 DrawPicture( pict , &(**pict).picFrame ) ;
181 #endif
182 }
183 return TRUE;
184 }
185
186 wxSize wxMetaFile::GetSize() const
187 {
188 wxSize size = wxDefaultSize ;
189 if ( Ok() )
190 {
191 PicHandle pict = (PicHandle) GetHMETAFILE() ;
192 Rect &r = (**pict).picFrame ;
193 size.x = r.right - r.left ;
194 size.y = r.bottom - r.top ;
195 }
196
197 return size;
198 }
199
200 /*
201 * Metafile device context
202 *
203 */
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 , _T("no arbitration of metafilesize supported") ) ;
213 wxASSERT_MSG( filename.IsEmpty() , _T("no file based metafile support yet")) ;
214
215 m_metaFile = new wxMetaFile(filename) ;
216 #if wxMAC_USE_CORE_GRAPHICS
217 #else
218 Rect r={0,0,height,width} ;
219
220 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r ) ;
221 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
222
223 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) ) ;
224 ::GetPort( (GrafPtr*) &m_macPort ) ;
225 m_ok = TRUE ;
226 #endif
227 SetMapMode(wxMM_TEXT);
228 }
229
230 wxMetaFileDC::~wxMetaFileDC()
231 {
232 }
233
234 void wxMetaFileDC::DoGetSize(int *width, int *height) const
235 {
236 wxCHECK_RET( m_metaFile , _T("GetSize() doesn't work without a metafile") );
237
238 wxSize sz = m_metaFile->GetSize() ;
239 if (width) (*width) = sz.x;
240 if (height) (*height) = sz.y;
241 }
242
243 wxMetaFile *wxMetaFileDC::Close()
244 {
245 ClosePicture() ;
246 return m_metaFile;
247 }
248
249 #if wxUSE_DATAOBJ
250 size_t wxMetafileDataObject::GetDataSize() const
251 {
252 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ;
253 }
254
255 bool wxMetafileDataObject::GetDataHere(void *buf) const
256 {
257 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
258 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ) ;
259 return true ;
260 }
261
262 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
263 {
264 Handle handle = NewHandle( len ) ;
265 SetHandleSize( handle , len ) ;
266 memcpy( *handle , buf , len ) ;
267 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle ) ;
268 return true ;
269 }
270 #endif
271
272 #endif