]> git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
clean up wxEvtHandler::m_eventsLocker weirdness: there is no need to allocate it...
[wxWidgets.git] / src / msw / enhmeta.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_ENH_METAFILE
28
29 #ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/log.h"
32 #include "wx/intl.h"
33 #endif //WX_PRECOMP
34
35 #include "wx/dc.h"
36 #include "wx/msw/dc.h"
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
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 #define GetEMF() ((HENHMETAFILE)m_hMF)
54 #define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
55
56 // ----------------------------------------------------------------------------
57 // private functions
58 // ----------------------------------------------------------------------------
59
60 // we must pass NULL if the string is empty to metafile functions
61 static inline const wxChar *GetMetaFileName(const wxString& fn)
62 { return !fn ? (const wxChar *)NULL : (const wxChar*)fn.wx_str(); }
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // wxEnhMetaFile
70 // ----------------------------------------------------------------------------
71
72 wxGDIRefData *wxEnhMetaFile::CreateGDIRefData() const
73 {
74 wxFAIL_MSG( _T("must be implemented if used") );
75
76 return NULL;
77 }
78
79 wxGDIRefData *
80 wxEnhMetaFile::CloneGDIRefData(const wxGDIRefData *WXUNUSED(data)) const
81 {
82 wxFAIL_MSG( _T("must be implemented if used") );
83
84 return NULL;
85 }
86
87 void wxEnhMetaFile::Init()
88 {
89 if ( m_filename.empty() )
90 {
91 m_hMF = 0;
92 }
93 else // have valid file name, load metafile from it
94 {
95 m_hMF = (WXHANDLE)::GetEnhMetaFile(m_filename.fn_str());
96 if ( !m_hMF )
97 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
98 m_filename.c_str());
99 }
100 }
101
102 void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
103 {
104 if ( &mf == this )
105 return;
106
107 if ( mf.m_hMF )
108 {
109 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
110 GetMetaFileName(m_filename));
111 if ( !m_hMF )
112 {
113 wxLogLastError(_T("CopyEnhMetaFile"));
114 }
115 }
116 else
117 {
118 m_hMF = 0;
119 }
120 }
121
122 void wxEnhMetaFile::Free()
123 {
124 if ( m_hMF )
125 {
126 if ( !::DeleteEnhMetaFile(GetEMF()) )
127 {
128 wxLogLastError(_T("DeleteEnhMetaFile"));
129 }
130 }
131 }
132
133 bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
134 {
135 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
136 wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
137
138 RECT rect;
139 if ( rectBound )
140 {
141 rect.top = rectBound->y;
142 rect.left = rectBound->x;
143 rect.right = rectBound->x + rectBound->width;
144 rect.bottom = rectBound->y + rectBound->height;
145 }
146 else
147 {
148 wxSize size = GetSize();
149
150 rect.top =
151 rect.left = 0;
152 rect.right = size.x;
153 rect.bottom = size.y;
154 }
155
156 wxDCImpl *impl = dc->GetImpl();
157 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
158 if (!msw_impl)
159 return false;
160
161 if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl), GetEMF(), &rect) )
162 {
163 wxLogLastError(_T("PlayEnhMetaFile"));
164
165 return false;
166 }
167
168 return true;
169 }
170
171 wxSize wxEnhMetaFile::GetSize() const
172 {
173 wxSize size = wxDefaultSize;
174
175 if ( Ok() )
176 {
177 ENHMETAHEADER hdr;
178 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
179 {
180 wxLogLastError(_T("GetEnhMetaFileHeader"));
181 }
182 else
183 {
184 // the width and height are in HIMETRIC (0.01mm) units, transform
185 // them to pixels
186 LONG w = hdr.rclFrame.right,
187 h = hdr.rclFrame.bottom;
188
189 HIMETRICToPixel(&w, &h);
190
191 size.x = w;
192 size.y = h;
193 }
194 }
195
196 return size;
197 }
198
199 bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
200 {
201 #if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
202 wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") );
203
204 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
205 #else // !wxUSE_DRAG_AND_DROP
206 wxFAIL_MSG(_T("not implemented"));
207 return false;
208 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
209 }
210
211 // ----------------------------------------------------------------------------
212 // wxEnhMetaFileDC
213 // ----------------------------------------------------------------------------
214
215 class wxEnhMetaFileDCImpl : public wxMSWDCImpl
216 {
217 public:
218 wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner,
219 const wxString& filename, int width, int height,
220 const wxString& description );
221 virtual ~wxEnhMetaFileDCImpl();
222
223 // obtain a pointer to the new metafile (caller should delete it)
224 wxEnhMetaFile *Close();
225
226 protected:
227 virtual void DoGetSize(int *width, int *height) const;
228
229 private:
230 // size passed to ctor and returned by DoGetSize()
231 int m_width,
232 m_height;
233 };
234
235
236 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
237
238 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
239 int width, int height,
240 const wxString& description)
241 : wxDC(new wxEnhMetaFileDCImpl(this,
242 filename,
243 width, height,
244 description))
245 {
246 }
247
248
249 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner,
250 const wxString& filename,
251 int width, int height,
252 const wxString& description )
253 : wxMSWDCImpl( owner )
254 {
255 m_width = width;
256 m_height = height;
257
258 RECT rect;
259 RECT *pRect;
260 if ( width && height )
261 {
262 rect.top =
263 rect.left = 0;
264 rect.right = width;
265 rect.bottom = height;
266
267 // CreateEnhMetaFile() wants them in HIMETRIC
268 PixelToHIMETRIC(&rect.right, &rect.bottom);
269
270 pRect = &rect;
271 }
272 else
273 {
274 // GDI will try to find out the size for us (not recommended)
275 pRect = (LPRECT)NULL;
276 }
277
278 ScreenHDC hdcRef;
279 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
280 pRect, description.wx_str());
281 if ( !m_hDC )
282 {
283 wxLogLastError(_T("CreateEnhMetaFile"));
284 }
285 }
286
287 void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const
288 {
289 if ( width )
290 *width = m_width;
291 if ( height )
292 *height = m_height;
293 }
294
295 wxEnhMetaFile *wxEnhMetaFileDCImpl::Close()
296 {
297 wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") );
298
299 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
300 if ( !hMF )
301 {
302 wxLogLastError(_T("CloseEnhMetaFile"));
303
304 return NULL;
305 }
306
307 wxEnhMetaFile *mf = new wxEnhMetaFile;
308 mf->SetHENHMETAFILE((WXHANDLE)hMF);
309 return mf;
310 }
311
312 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
313 {
314 // avoid freeing it in the base class dtor
315 m_hDC = 0;
316 }
317
318 #if wxUSE_DRAG_AND_DROP
319
320 // ----------------------------------------------------------------------------
321 // wxEnhMetaFileDataObject
322 // ----------------------------------------------------------------------------
323
324 wxDataFormat
325 wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
326 {
327 return wxDF_ENHMETAFILE;
328 }
329
330 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
331 {
332 // wxDF_ENHMETAFILE and wxDF_METAFILE
333 return 2;
334 }
335
336 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
337 Direction WXUNUSED(dir)) const
338 {
339 formats[0] = wxDF_ENHMETAFILE;
340 formats[1] = wxDF_METAFILE;
341 }
342
343 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
344 {
345 if ( format == wxDF_ENHMETAFILE )
346 {
347 // we pass data by handle and not HGLOBAL
348 return 0u;
349 }
350 else
351 {
352 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
353
354 return sizeof(METAFILEPICT);
355 }
356 }
357
358 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
359 {
360 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
361
362 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
363
364 if ( format == wxDF_ENHMETAFILE )
365 {
366 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
367 if ( !hEMFCopy )
368 {
369 wxLogLastError(_T("CopyEnhMetaFile"));
370
371 return false;
372 }
373
374 *(HENHMETAFILE *)buf = hEMFCopy;
375 }
376 else
377 {
378 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
379
380 // convert to WMF
381
382 ScreenHDC hdc;
383
384 // first get the buffer size and alloc memory
385 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
386 wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") );
387
388 BYTE *bits = (BYTE *)malloc(size);
389
390 // then get the enh metafile bits
391 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
392 {
393 wxLogLastError(_T("GetWinMetaFileBits"));
394
395 free(bits);
396
397 return false;
398 }
399
400 // and finally convert them to the WMF
401 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
402 free(bits);
403 if ( !hMF )
404 {
405 wxLogLastError(_T("SetMetaFileBitsEx"));
406
407 return false;
408 }
409
410 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
411
412 wxSize sizeMF = m_metafile.GetSize();
413 mfpict->hMF = hMF;
414 mfpict->mm = MM_ANISOTROPIC;
415 mfpict->xExt = sizeMF.x;
416 mfpict->yExt = sizeMF.y;
417
418 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
419 }
420
421 return true;
422 }
423
424 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
425 size_t WXUNUSED(len),
426 const void *buf)
427 {
428 HENHMETAFILE hEMF;
429
430 if ( format == wxDF_ENHMETAFILE )
431 {
432 hEMF = *(HENHMETAFILE *)buf;
433
434 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
435 }
436 else
437 {
438 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
439
440 // convert from WMF
441 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
442
443 // first get the buffer size
444 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
445 wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") );
446
447 // then get metafile bits
448 BYTE *bits = (BYTE *)malloc(size);
449 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
450 {
451 wxLogLastError(_T("GetMetaFileBitsEx"));
452
453 free(bits);
454
455 return false;
456 }
457
458 ScreenHDC hdcRef;
459
460 // and finally create an enhanced metafile from them
461 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
462 free(bits);
463 if ( !hEMF )
464 {
465 wxLogLastError(_T("SetWinMetaFileBits"));
466
467 return false;
468 }
469 }
470
471 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
472
473 return true;
474 }
475
476 // ----------------------------------------------------------------------------
477 // wxEnhMetaFileSimpleDataObject
478 // ----------------------------------------------------------------------------
479
480 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
481 {
482 // we pass data by handle and not HGLOBAL
483 return 0u;
484 }
485
486 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
487 {
488 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
489
490 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
491
492 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
493 if ( !hEMFCopy )
494 {
495 wxLogLastError(_T("CopyEnhMetaFile"));
496
497 return false;
498 }
499
500 *(HENHMETAFILE *)buf = hEMFCopy;
501 return true;
502 }
503
504 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
505 const void *buf)
506 {
507 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
508
509 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
510 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
511
512 return true;
513 }
514
515
516 #endif // wxUSE_DRAG_AND_DROP
517
518 #endif // wxUSE_ENH_METAFILE