]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
Use empty check consistent with other parts.
[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 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
42 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
43 #endif
44
45 class wxMetafileRefData: public wxGDIRefData
46 {
47 friend class WXDLLEXPORT wxMetafile;
48 public:
49 wxMetafileRefData(void);
50 ~wxMetafileRefData(void);
51
52 private:
53 PicHandle m_metafile;
54 #if wxMAC_USE_CORE_GRAPHICS
55 QDPictRef m_qdPictRef ;
56 #endif
57 };
58
59
60 /*
61 * Metafiles
62 * Currently, the only purpose for making a metafile is to put
63 * it on the clipboard.
64 */
65
66 wxMetafileRefData::wxMetafileRefData(void)
67 {
68 m_metafile = 0;
69 #if wxMAC_USE_CORE_GRAPHICS
70 m_qdPictRef = NULL ;
71 #endif
72 }
73
74 wxMetafileRefData::~wxMetafileRefData(void)
75 {
76 if (m_metafile)
77 {
78 KillPicture( (PicHandle) m_metafile ) ;
79 m_metafile = 0;
80 #if wxMAC_USE_CORE_GRAPHICS
81 QDPictRelease( m_qdPictRef ) ;
82 m_qdPictRef = NULL ;
83 #endif
84 }
85 }
86
87 wxMetaFile::wxMetaFile(const wxString& file)
88 {
89 m_refData = new wxMetafileRefData;
90
91 M_METAFILEDATA->m_metafile = 0;
92 wxASSERT_MSG( file.IsEmpty() , wxT("no file based metafile support yet") ) ;
93 /*
94 if (!file.IsNull() && (file.Cmp("") == 0))
95 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
96 */
97 }
98
99 wxMetaFile::~wxMetaFile()
100 {
101 }
102
103 bool wxMetaFile::Ok() const
104 {
105 return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0));
106 }
107
108 WXHMETAFILE wxMetaFile::GetHMETAFILE() const
109 {
110 return (WXHMETAFILE) M_METAFILEDATA->m_metafile;
111 }
112
113 bool wxMetaFile::SetClipboard(int width, int height)
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 bool success = wxTheClipboard->SetData(data);
129 if (!alreadyOpen)
130 wxTheClipboard->Close();
131 return (bool) success;
132 #endif
133 return TRUE ;
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.IsEmpty() , _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