1. cursor fixes: frame does have hand cursor in the controls sample now,
[wxWidgets.git] / src / msw / enhmeta.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/enhmeta.cpp
3 // Purpose: implementation of wxEnhMetaFileXXX classes
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.01.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "enhmeta.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_ENH_METAFILE
32
33 #ifndef WX_PRECOMP
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #endif //WX_PRECOMP
37
38 #include "wx/metafile.h"
39 #include "wx/clipbrd.h"
40
41 #include "wx/msw/private.h"
42
43 // ----------------------------------------------------------------------------
44 // wxWin macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
48 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
49
50 // ----------------------------------------------------------------------------
51 // macros
52 // ----------------------------------------------------------------------------
53
54 #define GetEMF() ((HENHMETAFILE)m_hMF)
55 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
56
57 // ----------------------------------------------------------------------------
58 // private functions
59 // ----------------------------------------------------------------------------
60
61 // we must pass NULL if the string is empty to metafile functions
62 static inline const wxChar *GetMetaFileName(const wxString& fn)
63 { return !fn ? (wxChar *)NULL : fn.c_str(); }
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // wxEnhMetaFile
71 // ----------------------------------------------------------------------------
72
73 void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
74 {
75 if ( &mf == this )
76 return;
77
78 if ( mf.m_hMF )
79 {
80 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
81 GetMetaFileName(m_filename));
82 if ( !m_hMF )
83 {
84 wxLogLastError(_T("CopyEnhMetaFile"));
85 }
86 }
87 else
88 {
89 m_hMF = 0;
90 }
91 }
92
93 void wxEnhMetaFile::Free()
94 {
95 if ( m_hMF )
96 {
97 if ( !::DeleteEnhMetaFile(GetEMF()) )
98 {
99 wxLogLastError(_T("DeleteEnhMetaFile"));
100 }
101 }
102 }
103
104 bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
105 {
106 wxCHECK_MSG( Ok(), FALSE, _T("can't play invalid enhanced metafile") );
107 wxCHECK_MSG( dc, FALSE, _T("invalid wxDC in wxEnhMetaFile::Play") );
108
109 RECT rect;
110 if ( rectBound )
111 {
112 rect.top = rectBound->y;
113 rect.left = rectBound->x;
114 rect.right = rectBound->x + rectBound->width;
115 rect.bottom = rectBound->y + rectBound->height;
116 }
117 else
118 {
119 wxSize size = GetSize();
120
121 rect.top =
122 rect.left = 0;
123 rect.right = size.x;
124 rect.bottom = size.y;
125 }
126
127 if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) )
128 {
129 wxLogLastError(_T("PlayEnhMetaFile"));
130
131 return FALSE;
132 }
133
134 return TRUE;
135 }
136
137 wxSize wxEnhMetaFile::GetSize() const
138 {
139 wxSize size = wxDefaultSize;
140
141 if ( Ok() )
142 {
143 ENHMETAHEADER hdr;
144 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
145 {
146 wxLogLastError(_T("GetEnhMetaFileHeader"));
147 }
148 else
149 {
150 // the width and height are in HIMETRIC (0.01mm) units, transform
151 // them to pixels
152 LONG w = hdr.rclFrame.right,
153 h = hdr.rclFrame.bottom;
154
155 HIMETRICToPixel(&w, &h);
156
157 size.x = w;
158 size.y = h;
159 }
160 }
161
162 return size;
163 }
164
165 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
166 {
167 wxCHECK_MSG( m_hMF, FALSE, _T("can't copy invalid metafile to clipboard") );
168
169 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
170 }
171
172 // ----------------------------------------------------------------------------
173 // wxEnhMetaFileDC
174 // ----------------------------------------------------------------------------
175
176 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
177 int width, int height,
178 const wxString& description)
179 {
180 ScreenHDC hdcRef;
181
182 RECT rect, *pRect;
183 if ( width && height )
184 {
185 rect.top =
186 rect.left = 0;
187 rect.right = width;
188 rect.bottom = height;
189
190 // CreateEnhMetaFile() wants them in HIMETRIC
191 PixelToHIMETRIC(&rect.right, &rect.bottom);
192
193 pRect = &rect;
194 }
195 else
196 {
197 // GDI will try to find out the size for us (not recommended)
198 pRect = (LPRECT)NULL;
199 }
200
201 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
202 pRect, description);
203 if ( !m_hDC )
204 {
205 wxLogLastError(_T("CreateEnhMetaFile"));
206 }
207 }
208
209 wxEnhMetaFile *wxEnhMetaFileDC::Close()
210 {
211 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
212
213 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
214 if ( !hMF )
215 {
216 wxLogLastError(_T("CloseEnhMetaFile"));
217
218 return NULL;
219 }
220
221 wxEnhMetaFile *mf = new wxEnhMetaFile;
222 mf->SetHENHMETAFILE((WXHANDLE)hMF);
223 return mf;
224 }
225
226 wxEnhMetaFileDC::~wxEnhMetaFileDC()
227 {
228 // avoid freeing it in the base class dtor
229 m_hDC = 0;
230 }
231
232 // ----------------------------------------------------------------------------
233 // wxEnhMetaFileDataObject
234 // ----------------------------------------------------------------------------
235
236 #if wxUSE_DRAG_AND_DROP
237
238 wxDataFormat
239 wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
240 {
241 return wxDF_ENHMETAFILE;
242 }
243
244 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
245 {
246 // wxDF_ENHMETAFILE and wxDF_METAFILE
247 return 2;
248 }
249
250 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
251 Direction WXUNUSED(dir)) const
252 {
253 formats[0] = wxDF_ENHMETAFILE;
254 formats[1] = wxDF_METAFILE;
255 }
256
257 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
258 {
259 if ( format == wxDF_ENHMETAFILE )
260 {
261 // we pass data by handle and not HGLOBAL
262 return 0u;
263 }
264 else
265 {
266 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
267
268 return sizeof(METAFILEPICT);
269 }
270 }
271
272 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
273 {
274 wxCHECK_MSG( m_metafile.Ok(), FALSE, _T("copying invalid enh metafile") );
275
276 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
277
278 if ( format == wxDF_ENHMETAFILE )
279 {
280 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
281 if ( !hEMFCopy )
282 {
283 wxLogLastError(_T("CopyEnhMetaFile"));
284
285 return FALSE;
286 }
287
288 *(HENHMETAFILE *)buf = hEMFCopy;
289 }
290 else
291 {
292 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
293
294 // convert to WMF
295
296 ScreenHDC hdc;
297
298 // first get the buffer size and alloc memory
299 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
300 wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") );
301
302 BYTE *bits = (BYTE *)malloc(size);
303
304 // then get the enh metafile bits
305 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
306 {
307 wxLogLastError(_T("GetWinMetaFileBits"));
308
309 free(bits);
310
311 return FALSE;
312 }
313
314 // and finally convert them to the WMF
315 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
316 free(bits);
317 if ( !hMF )
318 {
319 wxLogLastError(_T("SetMetaFileBitsEx"));
320
321 return FALSE;
322 }
323
324 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
325
326 wxSize sizeMF = m_metafile.GetSize();
327 mfpict->hMF = hMF;
328 mfpict->mm = MM_ANISOTROPIC;
329 mfpict->xExt = sizeMF.x;
330 mfpict->yExt = sizeMF.y;
331
332 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
333 }
334
335 return TRUE;
336 }
337
338 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
339 size_t WXUNUSED(len),
340 const void *buf)
341 {
342 HENHMETAFILE hEMF;
343
344 if ( format == wxDF_ENHMETAFILE )
345 {
346 hEMF = *(HENHMETAFILE *)buf;
347
348 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
349 }
350 else
351 {
352 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
353
354 // convert from WMF
355 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
356
357 // first get the buffer size
358 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
359 wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") );
360
361 // then get metafile bits
362 BYTE *bits = (BYTE *)malloc(size);
363 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
364 {
365 wxLogLastError(_T("GetMetaFileBitsEx"));
366
367 free(bits);
368
369 return FALSE;
370 }
371
372 ScreenHDC hdcRef;
373
374 // and finally create an enhanced metafile from them
375 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
376 free(bits);
377 if ( !hEMF )
378 {
379 wxLogLastError(_T("SetWinMetaFileBits"));
380
381 return FALSE;
382 }
383 }
384
385 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
386
387 return TRUE;
388 }
389 #endif // wxUSE_DRAG_AND_DROP
390
391 #endif // wxUSE_ENH_METAFILE