]> git.saurik.com Git - wxWidgets.git/blob - src/msw/enhmeta.cpp
fix unused parameter warning
[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 // wxEnhMetaFileDCImpl
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 wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner,
237 const wxString& filename,
238 int width, int height,
239 const wxString& description )
240 : wxMSWDCImpl( owner )
241 {
242 m_width = width;
243 m_height = height;
244
245 RECT rect;
246 RECT *pRect;
247 if ( width && height )
248 {
249 rect.top =
250 rect.left = 0;
251 rect.right = width;
252 rect.bottom = height;
253
254 // CreateEnhMetaFile() wants them in HIMETRIC
255 PixelToHIMETRIC(&rect.right, &rect.bottom);
256
257 pRect = &rect;
258 }
259 else
260 {
261 // GDI will try to find out the size for us (not recommended)
262 pRect = (LPRECT)NULL;
263 }
264
265 ScreenHDC hdcRef;
266 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
267 pRect, description.wx_str());
268 if ( !m_hDC )
269 {
270 wxLogLastError(_T("CreateEnhMetaFile"));
271 }
272 }
273
274 void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const
275 {
276 if ( width )
277 *width = m_width;
278 if ( height )
279 *height = m_height;
280 }
281
282 wxEnhMetaFile *wxEnhMetaFileDCImpl::Close()
283 {
284 wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") );
285
286 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
287 if ( !hMF )
288 {
289 wxLogLastError(_T("CloseEnhMetaFile"));
290
291 return NULL;
292 }
293
294 wxEnhMetaFile *mf = new wxEnhMetaFile;
295 mf->SetHENHMETAFILE((WXHANDLE)hMF);
296 return mf;
297 }
298
299 wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
300 {
301 // avoid freeing it in the base class dtor
302 m_hDC = 0;
303 }
304
305 // ----------------------------------------------------------------------------
306 // wxEnhMetaFileDC
307 // ----------------------------------------------------------------------------
308
309 IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
310
311 wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
312 int width, int height,
313 const wxString& description)
314 : wxDC(new wxEnhMetaFileDCImpl(this,
315 filename,
316 width, height,
317 description))
318 {
319 }
320
321 wxEnhMetaFile *wxEnhMetaFileDC::Close()
322 {
323 wxEnhMetaFileDCImpl * const
324 impl = wx_static_cast(wxEnhMetaFileDCImpl *, GetImpl());
325 wxCHECK_MSG( impl, NULL, _T("no wxEnhMetaFileDC implementation") );
326
327 return impl->Close();
328 }
329
330 #if wxUSE_DRAG_AND_DROP
331
332 // ----------------------------------------------------------------------------
333 // wxEnhMetaFileDataObject
334 // ----------------------------------------------------------------------------
335
336 wxDataFormat
337 wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
338 {
339 return wxDF_ENHMETAFILE;
340 }
341
342 size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
343 {
344 // wxDF_ENHMETAFILE and wxDF_METAFILE
345 return 2;
346 }
347
348 void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
349 Direction WXUNUSED(dir)) const
350 {
351 formats[0] = wxDF_ENHMETAFILE;
352 formats[1] = wxDF_METAFILE;
353 }
354
355 size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
356 {
357 if ( format == wxDF_ENHMETAFILE )
358 {
359 // we pass data by handle and not HGLOBAL
360 return 0u;
361 }
362 else
363 {
364 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
365
366 return sizeof(METAFILEPICT);
367 }
368 }
369
370 bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
371 {
372 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
373
374 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
375
376 if ( format == wxDF_ENHMETAFILE )
377 {
378 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
379 if ( !hEMFCopy )
380 {
381 wxLogLastError(_T("CopyEnhMetaFile"));
382
383 return false;
384 }
385
386 *(HENHMETAFILE *)buf = hEMFCopy;
387 }
388 else
389 {
390 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
391
392 // convert to WMF
393
394 ScreenHDC hdc;
395
396 // first get the buffer size and alloc memory
397 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
398 wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") );
399
400 BYTE *bits = (BYTE *)malloc(size);
401
402 // then get the enh metafile bits
403 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
404 {
405 wxLogLastError(_T("GetWinMetaFileBits"));
406
407 free(bits);
408
409 return false;
410 }
411
412 // and finally convert them to the WMF
413 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
414 free(bits);
415 if ( !hMF )
416 {
417 wxLogLastError(_T("SetMetaFileBitsEx"));
418
419 return false;
420 }
421
422 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
423
424 wxSize sizeMF = m_metafile.GetSize();
425 mfpict->hMF = hMF;
426 mfpict->mm = MM_ANISOTROPIC;
427 mfpict->xExt = sizeMF.x;
428 mfpict->yExt = sizeMF.y;
429
430 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
431 }
432
433 return true;
434 }
435
436 bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
437 size_t WXUNUSED(len),
438 const void *buf)
439 {
440 HENHMETAFILE hEMF;
441
442 if ( format == wxDF_ENHMETAFILE )
443 {
444 hEMF = *(HENHMETAFILE *)buf;
445
446 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
447 }
448 else
449 {
450 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
451
452 // convert from WMF
453 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
454
455 // first get the buffer size
456 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
457 wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") );
458
459 // then get metafile bits
460 BYTE *bits = (BYTE *)malloc(size);
461 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
462 {
463 wxLogLastError(_T("GetMetaFileBitsEx"));
464
465 free(bits);
466
467 return false;
468 }
469
470 ScreenHDC hdcRef;
471
472 // and finally create an enhanced metafile from them
473 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
474 free(bits);
475 if ( !hEMF )
476 {
477 wxLogLastError(_T("SetWinMetaFileBits"));
478
479 return false;
480 }
481 }
482
483 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
484
485 return true;
486 }
487
488 // ----------------------------------------------------------------------------
489 // wxEnhMetaFileSimpleDataObject
490 // ----------------------------------------------------------------------------
491
492 size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
493 {
494 // we pass data by handle and not HGLOBAL
495 return 0u;
496 }
497
498 bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
499 {
500 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
501
502 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
503
504 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
505 if ( !hEMFCopy )
506 {
507 wxLogLastError(_T("CopyEnhMetaFile"));
508
509 return false;
510 }
511
512 *(HENHMETAFILE *)buf = hEMFCopy;
513 return true;
514 }
515
516 bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
517 const void *buf)
518 {
519 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
520
521 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
522 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
523
524 return true;
525 }
526
527
528 #endif // wxUSE_DRAG_AND_DROP
529
530 #endif // wxUSE_ENH_METAFILE