]> git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
generate key events for Space/Enter in addition to the activate events, as wxMSW...
[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 #if wxUSE_DRAG_AND_DROP
168 wxCHECK_MSG( m_hMF, FALSE, _T("can't copy invalid metafile to clipboard") );
169
170 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
171 #else // !wxUSE_DRAG_AND_DROP
172 wxFAIL_MSG(_T("not implemented"));
173 return FALSE;
174 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
175 }
176
177 // ----------------------------------------------------------------------------
178 // wxEnhMetaFileDC
179 // ----------------------------------------------------------------------------
180
181 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
182 int width, int height,
183 const wxString& description)
184 {
185 ScreenHDC hdcRef;
186 RECT rect;
187 RECT *pRect;
188 if ( width && height )
189 {
190 rect.top =
191 rect.left = 0;
192 rect.right = width;
193 rect.bottom = height;
194
195 // CreateEnhMetaFile() wants them in HIMETRIC
196 #ifdef __WXWINE__
197 LONG r, b;
198 PixelToHIMETRIC(&r, &b);
199 rect.right = r;
200 rect.bottom = b;
201 #else
202 PixelToHIMETRIC(&rect.right, &rect.bottom);
203 #endif
204
205 pRect = &rect;
206 }
207 else
208 {
209 // GDI will try to find out the size for us (not recommended)
210 pRect = (LPRECT)NULL;
211 }
212
213 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
214 pRect, description);
215 if ( !m_hDC )
216 {
217 wxLogLastError(_T("CreateEnhMetaFile"));
218 }
219 }
220
221 wxEnhMetaFile *wxEnhMetaFileDC::Close()
222 {
223 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
224
225 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
226 if ( !hMF )
227 {
228 wxLogLastError(_T("CloseEnhMetaFile"));
229
230 return NULL;
231 }
232
233 wxEnhMetaFile *mf = new wxEnhMetaFile;
234 mf->SetHENHMETAFILE((WXHANDLE)hMF);
235 return mf;
236 }
237
238 wxEnhMetaFileDC::~wxEnhMetaFileDC()
239 {
240 // avoid freeing it in the base class dtor
241 m_hDC = 0;
242 }
243
244 #if wxUSE_DRAG_AND_DROP
245
246 // ----------------------------------------------------------------------------
247 // wxEnhMetaFileDataObject
248 // ----------------------------------------------------------------------------
249
250 wxDataFormat
251 wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
252 {
253 return wxDF_ENHMETAFILE;
254 }
255
256 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
257 {
258 // wxDF_ENHMETAFILE and wxDF_METAFILE
259 return 2;
260 }
261
262 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
263 Direction WXUNUSED(dir)) const
264 {
265 formats[0] = wxDF_ENHMETAFILE;
266 formats[1] = wxDF_METAFILE;
267 }
268
269 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
270 {
271 if ( format == wxDF_ENHMETAFILE )
272 {
273 // we pass data by handle and not HGLOBAL
274 return 0u;
275 }
276 else
277 {
278 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
279
280 return sizeof(METAFILEPICT);
281 }
282 }
283
284 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
285 {
286 wxCHECK_MSG( m_metafile.Ok(), FALSE, _T("copying invalid enh metafile") );
287
288 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
289
290 if ( format == wxDF_ENHMETAFILE )
291 {
292 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
293 if ( !hEMFCopy )
294 {
295 wxLogLastError(_T("CopyEnhMetaFile"));
296
297 return FALSE;
298 }
299
300 *(HENHMETAFILE *)buf = hEMFCopy;
301 }
302 else
303 {
304 #ifdef __WXWINE__
305 wxFAIL_MSG( _T("unsupported format") );
306 return FALSE;
307 #else
308 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
309
310 // convert to WMF
311
312 ScreenHDC hdc;
313
314 // first get the buffer size and alloc memory
315 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
316 wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") );
317
318 BYTE *bits = (BYTE *)malloc(size);
319
320 // then get the enh metafile bits
321 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
322 {
323 wxLogLastError(_T("GetWinMetaFileBits"));
324
325 free(bits);
326
327 return FALSE;
328 }
329
330 // and finally convert them to the WMF
331 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
332 free(bits);
333 if ( !hMF )
334 {
335 wxLogLastError(_T("SetMetaFileBitsEx"));
336
337 return FALSE;
338 }
339
340 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
341
342 wxSize sizeMF = m_metafile.GetSize();
343 mfpict->hMF = hMF;
344 mfpict->mm = MM_ANISOTROPIC;
345 mfpict->xExt = sizeMF.x;
346 mfpict->yExt = sizeMF.y;
347
348 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
349 #endif
350 }
351
352 return TRUE;
353 }
354
355 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
356 size_t WXUNUSED(len),
357 const void *buf)
358 {
359 HENHMETAFILE hEMF;
360
361 if ( format == wxDF_ENHMETAFILE )
362 {
363 hEMF = *(HENHMETAFILE *)buf;
364
365 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
366 }
367 else
368 {
369 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
370
371 // convert from WMF
372 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
373
374 // first get the buffer size
375 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
376 wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") );
377
378 // then get metafile bits
379 BYTE *bits = (BYTE *)malloc(size);
380 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
381 {
382 wxLogLastError(_T("GetMetaFileBitsEx"));
383
384 free(bits);
385
386 return FALSE;
387 }
388
389 ScreenHDC hdcRef;
390
391 // and finally create an enhanced metafile from them
392 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
393 free(bits);
394 if ( !hEMF )
395 {
396 wxLogLastError(_T("SetWinMetaFileBits"));
397
398 return FALSE;
399 }
400 }
401
402 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
403
404 return TRUE;
405 }
406
407 // ----------------------------------------------------------------------------
408 // wxEnhMetaFileSimpleDataObject
409 // ----------------------------------------------------------------------------
410
411 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
412 {
413 // we pass data by handle and not HGLOBAL
414 return 0u;
415 }
416
417 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
418 {
419 wxCHECK_MSG( m_metafile.Ok(), FALSE, _T("copying invalid enh metafile") );
420
421 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
422
423 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
424 if ( !hEMFCopy )
425 {
426 wxLogLastError(_T("CopyEnhMetaFile"));
427
428 return FALSE;
429 }
430
431 *(HENHMETAFILE *)buf = hEMFCopy;
432 return TRUE;
433 }
434
435 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
436 const void *buf)
437 {
438 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
439
440 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
441 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
442
443 return TRUE;
444 }
445
446 #endif // wxUSE_DRAG_AND_DROP
447
448 #endif // wxUSE_ENH_METAFILE