]>
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; |
902725ee | 90 | wxASSERT_MSG( file.empty() , 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 | ||
902725ee WS |
101 | bool wxMetaFile::Ok() const |
102 | { | |
103 | return (M_METAFILEDATA && (M_METAFILEDATA->m_metafile != 0)); | |
71cc158e SC |
104 | } |
105 | ||
902725ee WS |
106 | WXHMETAFILE wxMetaFile::GetHMETAFILE() const |
107 | { | |
108 | return (WXHMETAFILE) M_METAFILEDATA->m_metafile; | |
71cc158e SC |
109 | } |
110 | ||
e9576ca5 SC |
111 | bool wxMetaFile::SetClipboard(int width, int height) |
112 | { | |
902725ee WS |
113 | bool success = true; |
114 | ||
f0822896 | 115 | #if wxUSE_DRAG_AND_DROP |
e40298d5 | 116 | //TODO finishi this port , we need the data obj first |
519cb848 | 117 | if (!m_refData) |
902725ee WS |
118 | return false; |
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) ; |
902725ee WS |
128 | success = wxTheClipboard->SetData(data); |
129 | if (!alreadyOpen) | |
e40298d5 | 130 | wxTheClipboard->Close(); |
f0822896 | 131 | #endif |
902725ee WS |
132 | |
133 | return success; | |
e9576ca5 SC |
134 | } |
135 | ||
76a5e5d2 | 136 | void wxMetafile::SetHMETAFILE(WXHMETAFILE mf) |
2f1ae414 | 137 | { |
71cc158e | 138 | UnRef() ; |
902725ee | 139 | |
71cc158e | 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 |
160 | bool wxMetaFile::Play(wxDC *dc) |
161 | { | |
e40298d5 | 162 | if (!m_refData) |
902725ee WS |
163 | return false; |
164 | ||
e40298d5 | 165 | if (!dc->Ok() ) |
902725ee WS |
166 | return false; |
167 | ||
e40298d5 | 168 | { |
20b69855 | 169 | #if wxMAC_USE_CORE_GRAPHICS |
71cc158e | 170 | QDPictRef cgPictRef = M_METAFILEDATA->m_qdPictRef ; |
626fd619 | 171 | CGContextRef cg = ((wxMacCGContext*)(dc->GetGraphicContext()))->GetNativeContext() ; |
71cc158e SC |
172 | CGRect bounds = QDPictGetBounds( cgPictRef ) ; |
173 | ||
902725ee | 174 | CGContextSaveGState(cg); |
71cc158e SC |
175 | CGContextTranslateCTM(cg, 0 , bounds.size.width ); |
176 | CGContextScaleCTM(cg, 1, -1); | |
177 | QDPictDrawToCGContext( cg , bounds , cgPictRef ) ; | |
178 | CGContextRestoreGState( cg ) ; | |
20b69855 | 179 | #else |
7fcb33e5 | 180 | PicHandle pict = (PicHandle) GetHMETAFILE() ; |
e40298d5 | 181 | wxMacPortSetter helper( dc ) ; |
e40298d5 | 182 | DrawPicture( pict , &(**pict).picFrame ) ; |
20b69855 | 183 | #endif |
e40298d5 | 184 | } |
902725ee | 185 | return true; |
e9576ca5 SC |
186 | } |
187 | ||
48de597b SC |
188 | wxSize 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 |
210 | wxMetaFileDC::wxMetaFileDC(const wxString& filename , |
211 | int width , int height , | |
212 | const wxString& WXUNUSED(description) ) | |
e9576ca5 | 213 | { |
48de597b | 214 | wxASSERT_MSG( width == 0 || height == 0 , _T("no arbitration of metafilesize supported") ) ; |
902725ee WS |
215 | wxASSERT_MSG( filename.empty() , _T("no file based metafile support yet")) ; |
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} ; |
902725ee | 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 ) ) ; |
902725ee WS |
226 | ::GetPort( (GrafPtr*) &m_macPort ) ; |
227 | m_ok = true ; | |
20b69855 | 228 | #endif |
902725ee | 229 | SetMapMode(wxMM_TEXT); |
e9576ca5 SC |
230 | } |
231 | ||
519cb848 | 232 | wxMetaFileDC::~wxMetaFileDC() |
e9576ca5 | 233 | { |
e9576ca5 SC |
234 | } |
235 | ||
48de597b SC |
236 | void 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 | 245 | wxMetaFile *wxMetaFileDC::Close() |
e9576ca5 | 246 | { |
e40298d5 JS |
247 | ClosePicture() ; |
248 | return m_metaFile; | |
e9576ca5 SC |
249 | } |
250 | ||
a07c1212 SC |
251 | #if wxUSE_DATAOBJ |
252 | size_t wxMetafileDataObject::GetDataSize() const | |
253 | { | |
e40298d5 | 254 | return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ; |
a07c1212 SC |
255 | } |
256 | ||
257 | bool 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 | ||
264 | bool 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 |