]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/metafile.cpp
bug fix for MacTel builds, using the same code on PPC as well (wxMask)
[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// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
519cb848
SC
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"
e9576ca5
SC
27#include "wx/clipbrd.h"
28
76a5e5d2
SC
29#include "wx/mac/private.h"
30
519cb848
SC
31#include <stdio.h>
32#include <string.h>
33
e9576ca5
SC
34extern bool wxClipboardIsOpen;
35
519cb848
SC
36IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
37IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
e9576ca5 38
71cc158e
SC
39class wxMetafileRefData: public wxGDIRefData
40{
41 friend class WXDLLEXPORT wxMetafile;
42public:
43 wxMetafileRefData(void);
44 ~wxMetafileRefData(void);
45
46private:
47 PicHandle m_metafile;
48#if wxMAC_USE_CORE_GRAPHICS
49 QDPictRef m_qdPictRef ;
50#endif
51};
52
53
519cb848
SC
54/*
55 * Metafiles
56 * Currently, the only purpose for making a metafile is to put
57 * it on the clipboard.
58 */
59
60wxMetafileRefData::wxMetafileRefData(void)
61{
62 m_metafile = 0;
71cc158e
SC
63#if wxMAC_USE_CORE_GRAPHICS
64 m_qdPictRef = NULL ;
65#endif
519cb848
SC
66}
67
68wxMetafileRefData::~wxMetafileRefData(void)
69{
70 if (m_metafile)
71 {
e40298d5 72 KillPicture( (PicHandle) m_metafile ) ;
519cb848 73 m_metafile = 0;
71cc158e
SC
74#if wxMAC_USE_CORE_GRAPHICS
75 QDPictRelease( m_qdPictRef ) ;
76 m_qdPictRef = NULL ;
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;
902725ee 86 wxASSERT_MSG( file.empty() , wxT("no file based metafile support yet") ) ;
519cb848
SC
87/*
88 if (!file.IsNull() && (file.Cmp("") == 0))
89 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
90*/
e9576ca5
SC
91}
92
93wxMetaFile::~wxMetaFile()
94{
e9576ca5
SC
95}
96
902725ee
WS
97bool wxMetaFile::Ok() const
98{
99 return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0));
71cc158e
SC
100}
101
902725ee
WS
102WXHMETAFILE wxMetaFile::GetHMETAFILE() const
103{
104 return (WXHMETAFILE) M_METAFILEDATA->m_metafile;
71cc158e
SC
105}
106
e9576ca5
SC
107bool wxMetaFile::SetClipboard(int width, int height)
108{
902725ee
WS
109 bool success = true;
110
f0822896 111#if wxUSE_DRAG_AND_DROP
e40298d5 112 //TODO finishi this port , we need the data obj first
519cb848 113 if (!m_refData)
902725ee
WS
114 return false;
115
f0822896 116 bool alreadyOpen=wxTheClipboard->IsOpened() ;
e9576ca5
SC
117 if (!alreadyOpen)
118 {
f0822896 119 wxTheClipboard->Open();
a07c1212 120 wxTheClipboard->Clear();
e9576ca5 121 }
a07c1212 122 wxDataObject *data =
e40298d5 123 new wxMetafileDataObject( *this) ;
902725ee
WS
124 success = wxTheClipboard->SetData(data);
125 if (!alreadyOpen)
e40298d5 126 wxTheClipboard->Close();
f0822896 127#endif
902725ee
WS
128
129 return success;
e9576ca5
SC
130}
131
76a5e5d2 132void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
2f1ae414 133{
71cc158e 134 UnRef() ;
902725ee 135
71cc158e 136 m_refData = new wxMetafileRefData;
2f1ae414 137
71cc158e
SC
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
2f1ae414
SC
154}
155
e9576ca5
SC
156bool wxMetaFile::Play(wxDC *dc)
157{
e40298d5 158 if (!m_refData)
902725ee
WS
159 return false;
160
e40298d5 161 if (!dc->Ok() )
902725ee
WS
162 return false;
163
e40298d5 164 {
20b69855 165#if wxMAC_USE_CORE_GRAPHICS
71cc158e 166 QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef ;
626fd619 167 CGContextRef cg = ((wxMacCGContext*)(dc->GetGraphicContext()))->GetNativeContext() ;
71cc158e
SC
168 CGRect bounds = QDPictGetBounds( cgPictRef ) ;
169
902725ee 170 CGContextSaveGState(cg);
71cc158e
SC
171 CGContextTranslateCTM(cg, 0 , bounds.size.width );
172 CGContextScaleCTM(cg, 1, -1);
173 QDPictDrawToCGContext( cg , bounds , cgPictRef ) ;
174 CGContextRestoreGState( cg ) ;
20b69855 175#else
7fcb33e5 176 PicHandle pict = (PicHandle) GetHMETAFILE() ;
e40298d5 177 wxMacPortSetter helper( dc ) ;
e40298d5 178 DrawPicture( pict , &(**pict).picFrame ) ;
20b69855 179#endif
e40298d5 180 }
902725ee 181 return true;
e9576ca5
SC
182}
183
48de597b
SC
184wxSize 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
e9576ca5
SC
198/*
199 * Metafile device context
200 *
201 */
202
e9576ca5
SC
203// New constructor that takes origin and extent. If you use this, don't
204// give origin/extent arguments to wxMakeMetaFilePlaceable.
519cb848 205
48de597b
SC
206wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
207 int width , int height ,
208 const wxString& WXUNUSED(description) )
e9576ca5 209{
48de597b 210 wxASSERT_MSG( width == 0 || height == 0 , _T("no arbitration of metafilesize supported") ) ;
902725ee
WS
211 wxASSERT_MSG( filename.empty() , _T("no file based metafile support yet")) ;
212
48de597b 213 m_metaFile = new wxMetaFile(filename) ;
20b69855
SC
214#if wxMAC_USE_CORE_GRAPHICS
215#else
48de597b 216 Rect r={0,0,height,width} ;
902725ee 217
48de597b
SC
218 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r ) ;
219 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
220
20b69855 221 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) ) ;
902725ee
WS
222 ::GetPort( (GrafPtr*) &m_macPort ) ;
223 m_ok = true ;
20b69855 224#endif
902725ee 225 SetMapMode(wxMM_TEXT);
e9576ca5
SC
226}
227
519cb848 228wxMetaFileDC::~wxMetaFileDC()
e9576ca5 229{
e9576ca5
SC
230}
231
48de597b
SC
232void 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
519cb848 241wxMetaFile *wxMetaFileDC::Close()
e9576ca5 242{
e40298d5
JS
243 ClosePicture() ;
244 return m_metaFile;
e9576ca5
SC
245}
246
a07c1212
SC
247#if wxUSE_DATAOBJ
248size_t wxMetafileDataObject::GetDataSize() const
249{
e40298d5 250 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ;
a07c1212
SC
251}
252
253bool wxMetafileDataObject::GetDataHere(void *buf) const
254{
e40298d5
JS
255 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
256 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ) ;
257 return true ;
a07c1212
SC
258}
259
260bool wxMetafileDataObject::SetData(size_t len, const void *buf)
261{
48de597b 262 Handle handle = NewHandle( len ) ;
e40298d5
JS
263 SetHandleSize( handle , len ) ;
264 memcpy( *handle , buf , len ) ;
20b69855 265 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle ) ;
e40298d5 266 return true ;
a07c1212
SC
267}
268#endif
269
e9576ca5 270#endif