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