]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/metafile.cpp
fixed crash introduced in v1.41
[wxWidgets.git] / src / mac / carbon / metafile.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: metafile.cpp
3// Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
a31a5f85 4// Author: Stefan Csomor
e9576ca5
SC
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
519cb848
SC
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15#pragma hdrstop
16#endif
17
519cb848
SC
18#if wxUSE_METAFILE
19
20#ifndef WX_PRECOMP
274b7a40
DS
21 #include "wx/utils.h"
22 #include "wx/app.h"
519cb848
SC
23#endif
24
25#include "wx/metafile.h"
e9576ca5 26#include "wx/clipbrd.h"
76a5e5d2
SC
27#include "wx/mac/private.h"
28
519cb848
SC
29#include <stdio.h>
30#include <string.h>
31
519cb848
SC
32IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
33IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
e9576ca5 34
71cc158e
SC
35class wxMetafileRefData: public wxGDIRefData
36{
37 friend class WXDLLEXPORT wxMetafile;
274b7a40 38
71cc158e 39public:
274b7a40
DS
40 wxMetafileRefData();
41 ~wxMetafileRefData();
71cc158e
SC
42
43private:
44 PicHandle m_metafile;
274b7a40 45
71cc158e 46#if wxMAC_USE_CORE_GRAPHICS
274b7a40 47 QDPictRef m_qdPictRef;
71cc158e
SC
48#endif
49};
50
274b7a40
DS
51extern bool wxClipboardIsOpen;
52
71cc158e 53
274b7a40
DS
54// Metafiles:
55// Currently, the only purpose for making a metafile
56// is to put it on the clipboard.
519cb848 57
274b7a40 58wxMetafileRefData::wxMetafileRefData()
519cb848
SC
59{
60 m_metafile = 0;
274b7a40 61
71cc158e 62#if wxMAC_USE_CORE_GRAPHICS
274b7a40 63 m_qdPictRef = NULL;
71cc158e 64#endif
519cb848
SC
65}
66
274b7a40 67wxMetafileRefData::~wxMetafileRefData()
519cb848
SC
68{
69 if (m_metafile)
70 {
274b7a40
DS
71 KillPicture( (PicHandle)m_metafile );
72 m_metafile = NULL;
73
71cc158e 74#if wxMAC_USE_CORE_GRAPHICS
274b7a40
DS
75 QDPictRelease( m_qdPictRef );
76 m_qdPictRef = NULL;
71cc158e 77#endif
519cb848
SC
78 }
79}
80
e9576ca5
SC
81wxMetaFile::wxMetaFile(const wxString& file)
82{
519cb848
SC
83 m_refData = new wxMetafileRefData;
84
519cb848 85 M_METAFILEDATA->m_metafile = 0;
274b7a40
DS
86 wxASSERT_MSG( file.empty(), wxT("no file based metafile support yet") );
87
88#if 0
519cb848 89 if (!file.IsNull() && (file.Cmp("") == 0))
274b7a40
DS
90 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile( file );
91#endif
e9576ca5
SC
92}
93
94wxMetaFile::~wxMetaFile()
95{
e9576ca5
SC
96}
97
902725ee
WS
98bool wxMetaFile::Ok() const
99{
100 return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0));
71cc158e
SC
101}
102
902725ee
WS
103WXHMETAFILE wxMetaFile::GetHMETAFILE() const
104{
105 return (WXHMETAFILE) M_METAFILEDATA->m_metafile;
71cc158e
SC
106}
107
e9576ca5
SC
108bool wxMetaFile::SetClipboard(int width, int height)
109{
902725ee
WS
110 bool success = true;
111
f0822896 112#if wxUSE_DRAG_AND_DROP
274b7a40 113 // TODO: to finish this port, we need the data obj first
519cb848 114 if (!m_refData)
902725ee
WS
115 return false;
116
274b7a40 117 bool alreadyOpen = wxTheClipboard->IsOpened();
e9576ca5
SC
118 if (!alreadyOpen)
119 {
f0822896 120 wxTheClipboard->Open();
a07c1212 121 wxTheClipboard->Clear();
e9576ca5 122 }
274b7a40
DS
123
124 wxDataObject *data = new wxMetafileDataObject( *this );
125 success = wxTheClipboard->SetData( data );
902725ee 126 if (!alreadyOpen)
e40298d5 127 wxTheClipboard->Close();
f0822896 128#endif
902725ee
WS
129
130 return success;
e9576ca5
SC
131}
132
76a5e5d2 133void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
2f1ae414 134{
274b7a40 135 UnRef();
902725ee 136
71cc158e 137 m_refData = new wxMetafileRefData;
2f1ae414 138
71cc158e 139 M_METAFILEDATA->m_metafile = (PicHandle) mf;
274b7a40 140
71cc158e 141#if wxMAC_USE_CORE_GRAPHICS
274b7a40
DS
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
71cc158e
SC
151 if ( provider != NULL )
152 {
274b7a40
DS
153 M_METAFILEDATA->m_qdPictRef = QDPictCreateWithProvider( provider );
154 CGDataProviderRelease( provider );
71cc158e
SC
155 }
156#endif
2f1ae414
SC
157}
158
e9576ca5
SC
159bool wxMetaFile::Play(wxDC *dc)
160{
e40298d5 161 if (!m_refData)
902725ee
WS
162 return false;
163
e40298d5 164 if (!dc->Ok() )
902725ee
WS
165 return false;
166
e40298d5 167 {
20b69855 168#if wxMAC_USE_CORE_GRAPHICS
274b7a40
DS
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 );
20b69855 178#else
274b7a40
DS
179 PicHandle pict = (PicHandle) GetHMETAFILE();
180 wxMacPortSetter helper( dc );
181 DrawPicture( pict , &(**pict).picFrame );
20b69855 182#endif
e40298d5 183 }
274b7a40 184
902725ee 185 return true;
e9576ca5
SC
186}
187
48de597b
SC
188wxSize wxMetaFile::GetSize() const
189{
190 wxSize size = wxDefaultSize ;
274b7a40 191
48de597b
SC
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
274b7a40 203// Metafile device context
e9576ca5 204
e9576ca5
SC
205// New constructor that takes origin and extent. If you use this, don't
206// give origin/extent arguments to wxMakeMetaFilePlaceable.
519cb848 207
48de597b
SC
208wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
209 int width , int height ,
210 const wxString& WXUNUSED(description) )
e9576ca5 211{
274b7a40
DS
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 );
902725ee 216
20b69855
SC
217#if wxMAC_USE_CORE_GRAPHICS
218#else
274b7a40
DS
219 Rect r = { 0, 0, height, width };
220
221 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r );
222 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
902725ee 223
274b7a40
DS
224 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) );
225 ::GetPort( (GrafPtr*) &m_macPort );
48de597b 226
902725ee 227 m_ok = true ;
20b69855 228#endif
274b7a40
DS
229
230 SetMapMode( wxMM_TEXT );
e9576ca5
SC
231}
232
519cb848 233wxMetaFileDC::~wxMetaFileDC()
e9576ca5 234{
e9576ca5
SC
235}
236
48de597b
SC
237void wxMetaFileDC::DoGetSize(int *width, int *height) const
238{
274b7a40 239 wxCHECK_RET( m_metaFile , wxT("GetSize() doesn't work without a metafile") );
48de597b
SC
240
241 wxSize sz = m_metaFile->GetSize() ;
274b7a40
DS
242 if (width)
243 (*width) = sz.x;
244 if (height)
245 (*height) = sz.y;
48de597b
SC
246}
247
519cb848 248wxMetaFile *wxMetaFileDC::Close()
e9576ca5 249{
e40298d5 250 ClosePicture() ;
274b7a40 251
e40298d5 252 return m_metaFile;
e9576ca5
SC
253}
254
a07c1212
SC
255#if wxUSE_DATAOBJ
256size_t wxMetafileDataObject::GetDataSize() const
257{
274b7a40 258 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
a07c1212
SC
259}
260
261bool wxMetafileDataObject::GetDataHere(void *buf) const
262{
e40298d5 263 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
274b7a40
DS
264 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) );
265
266 return true;
a07c1212
SC
267}
268
269bool wxMetafileDataObject::SetData(size_t len, const void *buf)
270{
274b7a40
DS
271 Handle handle = NewHandle( len );
272 SetHandleSize( handle, len );
273 memcpy( *handle, buf, len );
274 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
275
e40298d5 276 return true ;
a07c1212
SC
277}
278#endif
279
e9576ca5 280#endif