1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
26 #include "wx/metafile.h"
27 #include "wx/clipbrd.h"
29 #include "wx/mac/private.h"
34 extern bool wxClipboardIsOpen
;
36 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
37 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
39 class wxMetafileRefData
: public wxGDIRefData
41 friend class WXDLLEXPORT wxMetafile
;
43 wxMetafileRefData(void);
44 ~wxMetafileRefData(void);
48 #if wxMAC_USE_CORE_GRAPHICS
49 QDPictRef m_qdPictRef
;
56 * Currently, the only purpose for making a metafile is to put
57 * it on the clipboard.
60 wxMetafileRefData::wxMetafileRefData(void)
63 #if wxMAC_USE_CORE_GRAPHICS
68 wxMetafileRefData::~wxMetafileRefData(void)
72 KillPicture( (PicHandle
) m_metafile
) ;
74 #if wxMAC_USE_CORE_GRAPHICS
75 QDPictRelease( m_qdPictRef
) ;
81 wxMetaFile::wxMetaFile(const wxString
& file
)
83 m_refData
= new wxMetafileRefData
;
85 M_METAFILEDATA
->m_metafile
= 0;
86 wxASSERT_MSG( file
.empty() , wxT("no file based metafile support yet") ) ;
88 if (!file.IsNull() && (file.Cmp("") == 0))
89 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
93 wxMetaFile::~wxMetaFile()
97 bool wxMetaFile::Ok() const
99 return (M_METAFILEDATA
&& (M_METAFILEDATA
->m_metafile
!= 0));
102 WXHMETAFILE
wxMetaFile::GetHMETAFILE() const
104 return (WXHMETAFILE
) M_METAFILEDATA
->m_metafile
;
107 bool wxMetaFile::SetClipboard(int width
, int height
)
111 #if wxUSE_DRAG_AND_DROP
112 //TODO finishi this port , we need the data obj first
116 bool alreadyOpen
=wxTheClipboard
->IsOpened() ;
119 wxTheClipboard
->Open();
120 wxTheClipboard
->Clear();
123 new wxMetafileDataObject( *this) ;
124 success
= wxTheClipboard
->SetData(data
);
126 wxTheClipboard
->Close();
132 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf
)
136 m_refData
= new wxMetafileRefData
;
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
)
150 M_METAFILEDATA
->m_qdPictRef
= QDPictCreateWithProvider( provider
) ;
151 CGDataProviderRelease( provider
) ;
156 bool wxMetaFile::Play(wxDC
*dc
)
165 #if wxMAC_USE_CORE_GRAPHICS
166 QDPictRef cgPictRef
= M_METAFILEDATA
->m_qdPictRef
;
167 CGContextRef cg
= ((wxMacCGContext
*)(dc
->GetGraphicContext()))->GetNativeContext() ;
168 CGRect bounds
= QDPictGetBounds( cgPictRef
) ;
170 CGContextSaveGState(cg
);
171 CGContextTranslateCTM(cg
, 0 , bounds
.size
.width
);
172 CGContextScaleCTM(cg
, 1, -1);
173 QDPictDrawToCGContext( cg
, bounds
, cgPictRef
) ;
174 CGContextRestoreGState( cg
) ;
176 PicHandle pict
= (PicHandle
) GetHMETAFILE() ;
177 wxMacPortSetter
helper( dc
) ;
178 DrawPicture( pict
, &(**pict
).picFrame
) ;
184 wxSize
wxMetaFile::GetSize() const
186 wxSize size
= wxDefaultSize
;
189 PicHandle pict
= (PicHandle
) GetHMETAFILE() ;
190 Rect
&r
= (**pict
).picFrame
;
191 size
.x
= r
.right
- r
.left
;
192 size
.y
= r
.bottom
- r
.top
;
199 * Metafile device context
203 // New constructor that takes origin and extent. If you use this, don't
204 // give origin/extent arguments to wxMakeMetaFilePlaceable.
206 wxMetaFileDC::wxMetaFileDC(const wxString
& filename
,
207 int width
, int height
,
208 const wxString
& WXUNUSED(description
) )
210 wxASSERT_MSG( width
== 0 || height
== 0 , _T("no arbitration of metafilesize supported") ) ;
211 wxASSERT_MSG( filename
.empty() , _T("no file based metafile support yet")) ;
213 m_metaFile
= new wxMetaFile(filename
) ;
214 #if wxMAC_USE_CORE_GRAPHICS
216 Rect r
={0,0,height
,width
} ;
218 RectRgn( (RgnHandle
) m_macBoundaryClipRgn
, &r
) ;
219 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
221 m_metaFile
->SetHMETAFILE( (WXHMETAFILE
) OpenPicture( &r
) ) ;
222 ::GetPort( (GrafPtr
*) &m_macPort
) ;
225 SetMapMode(wxMM_TEXT
);
228 wxMetaFileDC::~wxMetaFileDC()
232 void wxMetaFileDC::DoGetSize(int *width
, int *height
) const
234 wxCHECK_RET( m_metaFile
, _T("GetSize() doesn't work without a metafile") );
236 wxSize sz
= m_metaFile
->GetSize() ;
237 if (width
) (*width
) = sz
.x
;
238 if (height
) (*height
) = sz
.y
;
241 wxMetaFile
*wxMetaFileDC::Close()
248 size_t wxMetafileDataObject::GetDataSize() const
250 return GetHandleSize( (Handle
) (*((wxMetafile
*)&m_metafile
)).GetHMETAFILE() ) ;
253 bool wxMetafileDataObject::GetDataHere(void *buf
) const
255 memcpy( buf
, (*(PicHandle
)(*((wxMetafile
*)&m_metafile
)).GetHMETAFILE()) ,
256 GetHandleSize( (Handle
) (*((wxMetafile
*)&m_metafile
)).GetHMETAFILE() ) ) ;
260 bool wxMetafileDataObject::SetData(size_t len
, const void *buf
)
262 Handle handle
= NewHandle( len
) ;
263 SetHandleSize( handle
, len
) ;
264 memcpy( *handle
, buf
, len
) ;
265 m_metafile
.SetHMETAFILE( (WXHMETAFILE
) handle
) ;