]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/metafile.cpp
fix warnings (double to int conversions and unused variables); removed hard TABs...
[wxWidgets.git] / src / mac / carbon / metafile.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
e542ecc6 2// Name: src/mac/carbon/metafile.cpp
e9576ca5 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 10/////////////////////////////////////////////////////////////////////////////
e542ecc6
DS
11//
12// Currently, the only purpose for making a metafile
13// is to put it on the clipboard.
e9576ca5 14
519cb848 15
e542ecc6 16#include "wx/wxprec.h"
519cb848 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 40 wxMetafileRefData();
d3c7fc99 41 virtual ~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 51wxMetafileRefData::wxMetafileRefData()
519cb848 52{
e542ecc6 53 m_metafile = NULL;
274b7a40 54
71cc158e 55#if wxMAC_USE_CORE_GRAPHICS
274b7a40 56 m_qdPictRef = NULL;
71cc158e 57#endif
519cb848
SC
58}
59
274b7a40 60wxMetafileRefData::~wxMetafileRefData()
519cb848
SC
61{
62 if (m_metafile)
63 {
4f74e0d1 64#ifndef __LP64__
274b7a40
DS
65 KillPicture( (PicHandle)m_metafile );
66 m_metafile = NULL;
67
71cc158e 68#if wxMAC_USE_CORE_GRAPHICS
274b7a40
DS
69 QDPictRelease( m_qdPictRef );
70 m_qdPictRef = NULL;
4f74e0d1 71#endif
71cc158e 72#endif
519cb848
SC
73 }
74}
75
e9576ca5
SC
76wxMetaFile::wxMetaFile(const wxString& file)
77{
519cb848
SC
78 m_refData = new wxMetafileRefData;
79
e542ecc6
DS
80 M_METAFILEDATA->m_metafile = NULL;
81 wxASSERT_MSG( file.empty(), wxT("no file-based metafile support yet") );
274b7a40
DS
82
83#if 0
519cb848 84 if (!file.IsNull() && (file.Cmp("") == 0))
274b7a40
DS
85 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile( file );
86#endif
e9576ca5
SC
87}
88
89wxMetaFile::~wxMetaFile()
90{
e9576ca5
SC
91}
92
b7cacb43 93bool wxMetaFile::IsOk() const
902725ee 94{
e542ecc6 95 return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != NULL));
71cc158e
SC
96}
97
902725ee
WS
98WXHMETAFILE wxMetaFile::GetHMETAFILE() const
99{
100 return (WXHMETAFILE) M_METAFILEDATA->m_metafile;
71cc158e
SC
101}
102
e9576ca5
SC
103bool wxMetaFile::SetClipboard(int width, int height)
104{
902725ee
WS
105 bool success = true;
106
f0822896 107#if wxUSE_DRAG_AND_DROP
e542ecc6
DS
108 // TODO: to finish this port, we need the data object first
109 if (m_refData == NULL)
902725ee
WS
110 return false;
111
274b7a40 112 bool alreadyOpen = wxTheClipboard->IsOpened();
e9576ca5
SC
113 if (!alreadyOpen)
114 {
f0822896 115 wxTheClipboard->Open();
a07c1212 116 wxTheClipboard->Clear();
e9576ca5 117 }
274b7a40
DS
118
119 wxDataObject *data = new wxMetafileDataObject( *this );
120 success = wxTheClipboard->SetData( data );
902725ee 121 if (!alreadyOpen)
e40298d5 122 wxTheClipboard->Close();
f0822896 123#endif
902725ee
WS
124
125 return success;
e9576ca5
SC
126}
127
76a5e5d2 128void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
2f1ae414 129{
274b7a40 130 UnRef();
902725ee 131
71cc158e 132 m_refData = new wxMetafileRefData;
2f1ae414 133
e542ecc6 134 M_METAFILEDATA->m_metafile = (PicHandle)mf;
274b7a40 135
71cc158e 136#if wxMAC_USE_CORE_GRAPHICS
274b7a40
DS
137 size_t sz = GetHandleSize( (Handle) M_METAFILEDATA->m_metafile );
138 wxMemoryBuffer* membuf = new wxMemoryBuffer( sz );
139 void *data = membuf->GetWriteBuf( sz );
e542ecc6 140
274b7a40
DS
141 memcpy( data, *M_METAFILEDATA->m_metafile, sz );
142 membuf->UngetWriteBuf( sz );
143 CGDataProviderRef provider = CGDataProviderCreateWithData(
144 membuf, data, sz, wxMacMemoryBufferReleaseProc );
145 M_METAFILEDATA->m_qdPictRef = NULL;
146
4f74e0d1 147#ifndef __LP64__
e542ecc6 148 if (provider != NULL)
71cc158e 149 {
274b7a40
DS
150 M_METAFILEDATA->m_qdPictRef = QDPictCreateWithProvider( provider );
151 CGDataProviderRelease( provider );
71cc158e
SC
152 }
153#endif
4f74e0d1 154#endif
2f1ae414
SC
155}
156
e9576ca5
SC
157bool wxMetaFile::Play(wxDC *dc)
158{
e40298d5 159 if (!m_refData)
902725ee
WS
160 return false;
161
e542ecc6 162 if (!dc->Ok())
902725ee
WS
163 return false;
164
e40298d5 165 {
20b69855 166#if wxMAC_USE_CORE_GRAPHICS
4f74e0d1 167#ifndef __LP64__
274b7a40 168 QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef;
f7862d3e 169 CGContextRef cg = (CGContextRef) dc->GetGraphicContext()->GetNativeContext();
274b7a40
DS
170 CGRect bounds = QDPictGetBounds( cgPictRef );
171
172 CGContextSaveGState( cg );
e542ecc6 173 CGContextTranslateCTM( cg, 0, bounds.size.width );
274b7a40 174 CGContextScaleCTM( cg, 1, -1 );
e542ecc6 175 QDPictDrawToCGContext( cg, bounds, cgPictRef );
274b7a40 176 CGContextRestoreGState( cg );
4f74e0d1 177#endif
20b69855 178#else
e542ecc6 179 PicHandle pict = (PicHandle)GetHMETAFILE();
274b7a40 180 wxMacPortSetter helper( dc );
e542ecc6
DS
181 Rect picFrame;
182 DrawPicture( pict, wxMacGetPictureBounds( pict, &picFrame ) );
20b69855 183#endif
e40298d5 184 }
274b7a40 185
902725ee 186 return true;
e9576ca5
SC
187}
188
48de597b
SC
189wxSize wxMetaFile::GetSize() const
190{
e542ecc6 191 wxSize dataSize = wxDefaultSize;
274b7a40 192
e542ecc6 193 if (Ok())
48de597b 194 {
4f74e0d1
SC
195#ifndef __LP64__
196 PicHandle pict = (PicHandle)GetHMETAFILE();
e542ecc6
DS
197 Rect r;
198 wxMacGetPictureBounds( pict, &r );
199 dataSize.x = r.right - r.left;
200 dataSize.y = r.bottom - r.top;
4f74e0d1 201#endif
48de597b
SC
202 }
203
e542ecc6 204 return dataSize;
48de597b
SC
205}
206
274b7a40 207// Metafile device context
e9576ca5 208
e9576ca5
SC
209// New constructor that takes origin and extent. If you use this, don't
210// give origin/extent arguments to wxMakeMetaFilePlaceable.
519cb848 211
e542ecc6
DS
212wxMetaFileDC::wxMetaFileDC(
213 const wxString& filename,
214 int width, int height,
215 const wxString& WXUNUSED(description) )
e9576ca5 216{
e542ecc6
DS
217 wxASSERT_MSG( width <= 0 || height <= 0, wxT("no arbitration of metafile size supported") );
218 wxASSERT_MSG( filename.empty(), wxT("no file based metafile support yet"));
274b7a40
DS
219
220 m_metaFile = new wxMetaFile( filename );
902725ee 221
20b69855
SC
222#if wxMAC_USE_CORE_GRAPHICS
223#else
274b7a40
DS
224 Rect r = { 0, 0, height, width };
225
e542ecc6
DS
226 RectRgn( (RgnHandle)m_macBoundaryClipRgn, &r );
227 CopyRgn( (RgnHandle)m_macBoundaryClipRgn, (RgnHandle)m_macCurrentClipRgn );
902725ee 228
e542ecc6
DS
229 m_metaFile->SetHMETAFILE( (WXHMETAFILE)OpenPicture( &r ) );
230 ::GetPort( (GrafPtr*)&m_macPort );
48de597b 231
e542ecc6 232 m_ok = true;
20b69855 233#endif
274b7a40
DS
234
235 SetMapMode( wxMM_TEXT );
e9576ca5
SC
236}
237
519cb848 238wxMetaFileDC::~wxMetaFileDC()
e9576ca5 239{
e9576ca5
SC
240}
241
48de597b
SC
242void wxMetaFileDC::DoGetSize(int *width, int *height) const
243{
e542ecc6 244 wxCHECK_RET( m_metaFile, wxT("GetSize() doesn't work without a metafile") );
48de597b 245
e542ecc6 246 wxSize sz = m_metaFile->GetSize();
274b7a40
DS
247 if (width)
248 (*width) = sz.x;
249 if (height)
250 (*height) = sz.y;
48de597b
SC
251}
252
519cb848 253wxMetaFile *wxMetaFileDC::Close()
e9576ca5 254{
4f74e0d1 255#ifndef __LP64__
e542ecc6 256 ClosePicture();
4f74e0d1 257#endif
e40298d5 258 return m_metaFile;
e9576ca5
SC
259}
260
a07c1212
SC
261#if wxUSE_DATAOBJ
262size_t wxMetafileDataObject::GetDataSize() const
263{
274b7a40 264 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() );
a07c1212
SC
265}
266
267bool wxMetafileDataObject::GetDataHere(void *buf) const
268{
e542ecc6
DS
269 Handle pictH = (Handle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE();
270 bool result = (pictH != NULL);
274b7a40 271
e542ecc6
DS
272 if (result)
273 memcpy( buf, *pictH, GetHandleSize( pictH ) );
274
275 return result;
a07c1212
SC
276}
277
278bool wxMetafileDataObject::SetData(size_t len, const void *buf)
279{
274b7a40
DS
280 Handle handle = NewHandle( len );
281 SetHandleSize( handle, len );
282 memcpy( *handle, buf, len );
283 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle );
284
e542ecc6 285 return true;
a07c1212
SC
286}
287#endif
288
e9576ca5 289#endif