]>
Commit | Line | Data |
---|---|---|
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 |
38 | extern bool wxClipboardIsOpen; |
39 | ||
519cb848 SC |
40 | IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject) |
41 | IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC) | |
e9576ca5 | 42 | |
71cc158e SC |
43 | class wxMetafileRefData: public wxGDIRefData |
44 | { | |
45 | friend class WXDLLEXPORT wxMetafile; | |
46 | public: | |
47 | wxMetafileRefData(void); | |
48 | ~wxMetafileRefData(void); | |
49 | ||
50 | private: | |
51 | PicHandle m_metafile; | |
52 | #if wxMAC_USE_CORE_GRAPHICS | |
53 | QDPictRef m_qdPictRef ; | |
54 | #endif | |
55 | }; | |
56 | ||
57 | ||
519cb848 SC |
58 | /* |
59 | * Metafiles | |
60 | * Currently, the only purpose for making a metafile is to put | |
61 | * it on the clipboard. | |
62 | */ | |
63 | ||
64 | wxMetafileRefData::wxMetafileRefData(void) | |
65 | { | |
66 | m_metafile = 0; | |
71cc158e SC |
67 | #if wxMAC_USE_CORE_GRAPHICS |
68 | m_qdPictRef = NULL ; | |
69 | #endif | |
519cb848 SC |
70 | } |
71 | ||
72 | wxMetafileRefData::~wxMetafileRefData(void) | |
73 | { | |
74 | if (m_metafile) | |
75 | { | |
e40298d5 | 76 | KillPicture( (PicHandle) m_metafile ) ; |
519cb848 | 77 | m_metafile = 0; |
71cc158e SC |
78 | #if wxMAC_USE_CORE_GRAPHICS |
79 | QDPictRelease( m_qdPictRef ) ; | |
80 | m_qdPictRef = NULL ; | |
81 | #endif | |
519cb848 SC |
82 | } |
83 | } | |
84 | ||
e9576ca5 SC |
85 | wxMetaFile::wxMetaFile(const wxString& file) |
86 | { | |
519cb848 SC |
87 | m_refData = new wxMetafileRefData; |
88 | ||
519cb848 | 89 | M_METAFILEDATA->m_metafile = 0; |
427ff662 | 90 | wxASSERT_MSG( file.IsEmpty() , wxT("no file based metafile support yet") ) ; |
519cb848 SC |
91 | /* |
92 | if (!file.IsNull() && (file.Cmp("") == 0)) | |
93 | M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file); | |
94 | */ | |
e9576ca5 SC |
95 | } |
96 | ||
97 | wxMetaFile::~wxMetaFile() | |
98 | { | |
e9576ca5 SC |
99 | } |
100 | ||
71cc158e SC |
101 | bool wxMetaFile::Ok() const |
102 | { | |
103 | return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0)); | |
104 | } | |
105 | ||
106 | WXHMETAFILE wxMetaFile::GetHMETAFILE() const | |
107 | { | |
108 | return (WXHMETAFILE) M_METAFILEDATA->m_metafile; | |
109 | } | |
110 | ||
e9576ca5 SC |
111 | bool wxMetaFile::SetClipboard(int width, int height) |
112 | { | |
f0822896 | 113 | #if wxUSE_DRAG_AND_DROP |
e40298d5 | 114 | //TODO finishi this port , we need the data obj first |
519cb848 SC |
115 | if (!m_refData) |
116 | return FALSE; | |
e40298d5 | 117 | |
f0822896 | 118 | bool alreadyOpen=wxTheClipboard->IsOpened() ; |
e9576ca5 SC |
119 | if (!alreadyOpen) |
120 | { | |
f0822896 | 121 | wxTheClipboard->Open(); |
a07c1212 | 122 | wxTheClipboard->Clear(); |
e9576ca5 | 123 | } |
a07c1212 | 124 | wxDataObject *data = |
e40298d5 | 125 | new wxMetafileDataObject( *this) ; |
a07c1212 SC |
126 | bool success = wxTheClipboard->SetData(data); |
127 | if (!alreadyOpen) | |
e40298d5 JS |
128 | wxTheClipboard->Close(); |
129 | return (bool) success; | |
f0822896 | 130 | #endif |
519cb848 | 131 | return TRUE ; |
e9576ca5 SC |
132 | } |
133 | ||
76a5e5d2 | 134 | void wxMetafile::SetHMETAFILE(WXHMETAFILE mf) |
2f1ae414 | 135 | { |
71cc158e SC |
136 | UnRef() ; |
137 | ||
138 | m_refData = new wxMetafileRefData; | |
2f1ae414 | 139 | |
71cc158e SC |
140 | M_METAFILEDATA->m_metafile = (PicHandle) mf; |
141 | #if wxMAC_USE_CORE_GRAPHICS | |
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( membuf , data , sz , | |
148 | wxMacMemoryBufferReleaseProc ) ; | |
149 | M_METAFILEDATA->m_qdPictRef = NULL ; | |
150 | if ( provider != NULL ) | |
151 | { | |
152 | M_METAFILEDATA->m_qdPictRef = QDPictCreateWithProvider( provider ) ; | |
153 | CGDataProviderRelease( provider ) ; | |
154 | } | |
155 | #endif | |
2f1ae414 SC |
156 | } |
157 | ||
e9576ca5 SC |
158 | bool wxMetaFile::Play(wxDC *dc) |
159 | { | |
e40298d5 JS |
160 | if (!m_refData) |
161 | return FALSE; | |
162 | ||
163 | if (!dc->Ok() ) | |
164 | return FALSE; | |
165 | ||
166 | { | |
20b69855 | 167 | #if wxMAC_USE_CORE_GRAPHICS |
71cc158e | 168 | QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef ; |
626fd619 | 169 | CGContextRef cg = ((wxMacCGContext*)(dc->GetGraphicContext()))->GetNativeContext() ; |
71cc158e SC |
170 | CGRect bounds = QDPictGetBounds( cgPictRef ) ; |
171 | ||
172 | CGContextSaveGState(cg); | |
173 | CGContextTranslateCTM(cg, 0 , bounds.size.width ); | |
174 | CGContextScaleCTM(cg, 1, -1); | |
175 | QDPictDrawToCGContext( cg , bounds , cgPictRef ) ; | |
176 | CGContextRestoreGState( cg ) ; | |
20b69855 | 177 | #else |
7fcb33e5 | 178 | PicHandle pict = (PicHandle) GetHMETAFILE() ; |
e40298d5 | 179 | wxMacPortSetter helper( dc ) ; |
e40298d5 | 180 | DrawPicture( pict , &(**pict).picFrame ) ; |
20b69855 | 181 | #endif |
e40298d5 | 182 | } |
519cb848 | 183 | return TRUE; |
e9576ca5 SC |
184 | } |
185 | ||
48de597b SC |
186 | wxSize wxMetaFile::GetSize() const |
187 | { | |
188 | wxSize size = wxDefaultSize ; | |
189 | if ( Ok() ) | |
190 | { | |
191 | PicHandle pict = (PicHandle) GetHMETAFILE() ; | |
192 | Rect &r = (**pict).picFrame ; | |
193 | size.x = r.right - r.left ; | |
194 | size.y = r.bottom - r.top ; | |
195 | } | |
196 | ||
197 | return size; | |
198 | } | |
199 | ||
e9576ca5 SC |
200 | /* |
201 | * Metafile device context | |
202 | * | |
203 | */ | |
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 |
208 | wxMetaFileDC::wxMetaFileDC(const wxString& filename , |
209 | int width , int height , | |
210 | const wxString& WXUNUSED(description) ) | |
e9576ca5 | 211 | { |
48de597b SC |
212 | wxASSERT_MSG( width == 0 || height == 0 , _T("no arbitration of metafilesize supported") ) ; |
213 | wxASSERT_MSG( filename.IsEmpty() , _T("no file based metafile support yet")) ; | |
e40298d5 | 214 | |
48de597b | 215 | m_metaFile = new wxMetaFile(filename) ; |
20b69855 SC |
216 | #if wxMAC_USE_CORE_GRAPHICS |
217 | #else | |
48de597b | 218 | Rect r={0,0,height,width} ; |
e40298d5 | 219 | |
48de597b SC |
220 | RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r ) ; |
221 | CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; | |
222 | ||
20b69855 | 223 | m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) ) ; |
e40298d5 JS |
224 | ::GetPort( (GrafPtr*) &m_macPort ) ; |
225 | m_ok = TRUE ; | |
20b69855 | 226 | #endif |
e40298d5 | 227 | SetMapMode(wxMM_TEXT); |
e9576ca5 SC |
228 | } |
229 | ||
519cb848 | 230 | wxMetaFileDC::~wxMetaFileDC() |
e9576ca5 | 231 | { |
e9576ca5 SC |
232 | } |
233 | ||
48de597b SC |
234 | void wxMetaFileDC::DoGetSize(int *width, int *height) const |
235 | { | |
236 | wxCHECK_RET( m_metaFile , _T("GetSize() doesn't work without a metafile") ); | |
237 | ||
238 | wxSize sz = m_metaFile->GetSize() ; | |
239 | if (width) (*width) = sz.x; | |
240 | if (height) (*height) = sz.y; | |
241 | } | |
242 | ||
519cb848 | 243 | wxMetaFile *wxMetaFileDC::Close() |
e9576ca5 | 244 | { |
e40298d5 JS |
245 | ClosePicture() ; |
246 | return m_metaFile; | |
e9576ca5 SC |
247 | } |
248 | ||
a07c1212 SC |
249 | #if wxUSE_DATAOBJ |
250 | size_t wxMetafileDataObject::GetDataSize() const | |
251 | { | |
e40298d5 | 252 | return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ; |
a07c1212 SC |
253 | } |
254 | ||
255 | bool wxMetafileDataObject::GetDataHere(void *buf) const | |
256 | { | |
e40298d5 JS |
257 | memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) , |
258 | GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ) ; | |
259 | return true ; | |
a07c1212 SC |
260 | } |
261 | ||
262 | bool wxMetafileDataObject::SetData(size_t len, const void *buf) | |
263 | { | |
48de597b | 264 | Handle handle = NewHandle( len ) ; |
e40298d5 JS |
265 | SetHandleSize( handle , len ) ; |
266 | memcpy( *handle , buf , len ) ; | |
20b69855 | 267 | m_metafile.SetHMETAFILE( (WXHMETAFILE) handle ) ; |
e40298d5 | 268 | return true ; |
a07c1212 SC |
269 | } |
270 | #endif | |
271 | ||
e9576ca5 | 272 | #endif |