]> git.saurik.com Git - wxWidgets.git/blame - src/msw/enhmeta.cpp
fixed bug due to missing break in GetColourFromGTKWidget() (part of patch 1160117)
[wxWidgets.git] / src / msw / enhmeta.cpp
CommitLineData
d9317fd4
VZ
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>
65571936 9// Licence: wxWindows licence
d9317fd4
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
d9317fd4
VZ
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"
7dca9b44 36 #include "wx/intl.h"
d9317fd4
VZ
37#endif //WX_PRECOMP
38
39#include "wx/metafile.h"
bfbd6dc1 40#include "wx/clipbrd.h"
d9317fd4
VZ
41
42#include "wx/msw/private.h"
43
44// ----------------------------------------------------------------------------
45// wxWin macros
46// ----------------------------------------------------------------------------
47
48IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
49IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
50
51// ----------------------------------------------------------------------------
52// macros
53// ----------------------------------------------------------------------------
54
55#define GetEMF() ((HENHMETAFILE)m_hMF)
56#define GetEMFOf(mf) ((HENHMETAFILE)((mf).m_hMF))
57
58// ----------------------------------------------------------------------------
59// private functions
60// ----------------------------------------------------------------------------
61
62// we must pass NULL if the string is empty to metafile functions
63static inline const wxChar *GetMetaFileName(const wxString& fn)
64 { return !fn ? (wxChar *)NULL : fn.c_str(); }
65
66// ============================================================================
67// implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// wxEnhMetaFile
72// ----------------------------------------------------------------------------
73
7a295dfa
VZ
74void wxEnhMetaFile::Init()
75{
76 if ( m_filename.empty() )
77 {
78 m_hMF = 0;
79 }
80 else // have valid file name, load metafile from it
81 {
82 m_hMF = GetEnhMetaFile(m_filename);
83 if ( !m_hMF )
84 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
85 m_filename.c_str());
86 }
87}
88
d9317fd4
VZ
89void wxEnhMetaFile::Assign(const wxEnhMetaFile& mf)
90{
91 if ( &mf == this )
92 return;
93
94 if ( mf.m_hMF )
95 {
96 m_hMF = (WXHANDLE)::CopyEnhMetaFile(GetEMFOf(mf),
97 GetMetaFileName(m_filename));
98 if ( !m_hMF )
99 {
100 wxLogLastError(_T("CopyEnhMetaFile"));
101 }
102 }
103 else
104 {
105 m_hMF = 0;
106 }
107}
108
109void wxEnhMetaFile::Free()
110{
111 if ( m_hMF )
112 {
113 if ( !::DeleteEnhMetaFile(GetEMF()) )
114 {
115 wxLogLastError(_T("DeleteEnhMetaFile"));
116 }
117 }
118}
119
120bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
121{
cbe874bd 122 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
d9317fd4
VZ
123 wxCHECK_MSG( dc, FALSE, _T("invalid wxDC in wxEnhMetaFile::Play") );
124
125 RECT rect;
126 if ( rectBound )
127 {
128 rect.top = rectBound->y;
129 rect.left = rectBound->x;
130 rect.right = rectBound->x + rectBound->width;
131 rect.bottom = rectBound->y + rectBound->height;
132 }
133 else
134 {
135 wxSize size = GetSize();
136
137 rect.top =
138 rect.left = 0;
139 rect.right = size.x;
140 rect.bottom = size.y;
141 }
142
143 if ( !::PlayEnhMetaFile(GetHdcOf(*dc), GetEMF(), &rect) )
144 {
145 wxLogLastError(_T("PlayEnhMetaFile"));
146
cbe874bd 147 return false;
d9317fd4
VZ
148 }
149
cbe874bd 150 return true;
d9317fd4
VZ
151}
152
153wxSize wxEnhMetaFile::GetSize() const
154{
155 wxSize size = wxDefaultSize;
156
157 if ( Ok() )
158 {
159 ENHMETAHEADER hdr;
160 if ( !::GetEnhMetaFileHeader(GetEMF(), sizeof(hdr), &hdr) )
161 {
162 wxLogLastError(_T("GetEnhMetaFileHeader"));
163 }
164 else
165 {
166 // the width and height are in HIMETRIC (0.01mm) units, transform
167 // them to pixels
168 LONG w = hdr.rclFrame.right,
169 h = hdr.rclFrame.bottom;
170
171 HIMETRICToPixel(&w, &h);
172
173 size.x = w;
174 size.y = h;
175 }
176 }
177
178 return size;
179}
180
bfbd6dc1
VZ
181bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
182{
a95e38c0 183#if wxUSE_DRAG_AND_DROP
bfbd6dc1
VZ
184 wxCHECK_MSG( m_hMF, FALSE, _T("can't copy invalid metafile to clipboard") );
185
186 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
a95e38c0
VZ
187#else // !wxUSE_DRAG_AND_DROP
188 wxFAIL_MSG(_T("not implemented"));
cbe874bd 189 return false;
a95e38c0 190#endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
bfbd6dc1
VZ
191}
192
d9317fd4
VZ
193// ----------------------------------------------------------------------------
194// wxEnhMetaFileDC
195// ----------------------------------------------------------------------------
196
197wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
198 int width, int height,
199 const wxString& description)
200{
201 ScreenHDC hdcRef;
33ac7e6f 202 RECT rect;
cbe874bd 203 RECT *pRect;
d9317fd4
VZ
204 if ( width && height )
205 {
206 rect.top =
207 rect.left = 0;
208 rect.right = width;
209 rect.bottom = height;
210
211 // CreateEnhMetaFile() wants them in HIMETRIC
212 PixelToHIMETRIC(&rect.right, &rect.bottom);
cbe874bd 213
d9317fd4
VZ
214 pRect = &rect;
215 }
216 else
217 {
218 // GDI will try to find out the size for us (not recommended)
219 pRect = (LPRECT)NULL;
220 }
221
222 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
223 pRect, description);
224 if ( !m_hDC )
225 {
226 wxLogLastError(_T("CreateEnhMetaFile"));
227 }
228}
229
230wxEnhMetaFile *wxEnhMetaFileDC::Close()
231{
232 wxCHECK_MSG( Ok(), NULL, _T("invalid wxEnhMetaFileDC") );
233
234 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
235 if ( !hMF )
236 {
237 wxLogLastError(_T("CloseEnhMetaFile"));
238
239 return NULL;
240 }
241
242 wxEnhMetaFile *mf = new wxEnhMetaFile;
243 mf->SetHENHMETAFILE((WXHANDLE)hMF);
244 return mf;
245}
246
247wxEnhMetaFileDC::~wxEnhMetaFileDC()
248{
249 // avoid freeing it in the base class dtor
250 m_hDC = 0;
251}
252
7ba4fbeb
VZ
253#if wxUSE_DRAG_AND_DROP
254
d9317fd4
VZ
255// ----------------------------------------------------------------------------
256// wxEnhMetaFileDataObject
257// ----------------------------------------------------------------------------
258
259wxDataFormat
260wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
261{
262 return wxDF_ENHMETAFILE;
263}
264
265size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
266{
267 // wxDF_ENHMETAFILE and wxDF_METAFILE
268 return 2;
269}
270
271void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
272 Direction WXUNUSED(dir)) const
273{
274 formats[0] = wxDF_ENHMETAFILE;
275 formats[1] = wxDF_METAFILE;
276}
277
278size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
279{
280 if ( format == wxDF_ENHMETAFILE )
281 {
282 // we pass data by handle and not HGLOBAL
283 return 0u;
284 }
285 else
286 {
287 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
288
289 return sizeof(METAFILEPICT);
290 }
291}
292
293bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
294{
cbe874bd 295 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
d9317fd4
VZ
296
297 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
298
299 if ( format == wxDF_ENHMETAFILE )
300 {
301 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
302 if ( !hEMFCopy )
303 {
304 wxLogLastError(_T("CopyEnhMetaFile"));
305
cbe874bd 306 return false;
d9317fd4
VZ
307 }
308
309 *(HENHMETAFILE *)buf = hEMFCopy;
310 }
311 else
312 {
313 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
314
315 // convert to WMF
316
317 ScreenHDC hdc;
318
319 // first get the buffer size and alloc memory
320 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
321 wxCHECK_MSG( size, FALSE, _T("GetWinMetaFileBits() failed") );
322
323 BYTE *bits = (BYTE *)malloc(size);
324
325 // then get the enh metafile bits
326 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
327 {
328 wxLogLastError(_T("GetWinMetaFileBits"));
329
330 free(bits);
331
cbe874bd 332 return false;
d9317fd4
VZ
333 }
334
335 // and finally convert them to the WMF
336 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
337 free(bits);
338 if ( !hMF )
339 {
340 wxLogLastError(_T("SetMetaFileBitsEx"));
341
cbe874bd 342 return false;
d9317fd4
VZ
343 }
344
345 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
346
347 wxSize sizeMF = m_metafile.GetSize();
348 mfpict->hMF = hMF;
349 mfpict->mm = MM_ANISOTROPIC;
350 mfpict->xExt = sizeMF.x;
351 mfpict->yExt = sizeMF.y;
352
353 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
354 }
355
cbe874bd 356 return true;
d9317fd4
VZ
357}
358
359bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
360 size_t WXUNUSED(len),
361 const void *buf)
362{
363 HENHMETAFILE hEMF;
364
365 if ( format == wxDF_ENHMETAFILE )
366 {
367 hEMF = *(HENHMETAFILE *)buf;
368
369 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
370 }
371 else
372 {
373 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
374
375 // convert from WMF
376 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
377
378 // first get the buffer size
379 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
380 wxCHECK_MSG( size, FALSE, _T("GetMetaFileBitsEx() failed") );
381
382 // then get metafile bits
383 BYTE *bits = (BYTE *)malloc(size);
384 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
385 {
386 wxLogLastError(_T("GetMetaFileBitsEx"));
387
388 free(bits);
389
cbe874bd 390 return false;
d9317fd4
VZ
391 }
392
393 ScreenHDC hdcRef;
394
395 // and finally create an enhanced metafile from them
396 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
397 free(bits);
398 if ( !hEMF )
399 {
400 wxLogLastError(_T("SetWinMetaFileBits"));
401
cbe874bd 402 return false;
d9317fd4
VZ
403 }
404 }
405
406 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
407
cbe874bd 408 return true;
d9317fd4 409}
7ba4fbeb
VZ
410
411// ----------------------------------------------------------------------------
412// wxEnhMetaFileSimpleDataObject
413// ----------------------------------------------------------------------------
414
415size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
416{
417 // we pass data by handle and not HGLOBAL
418 return 0u;
419}
420
421bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
422{
cbe874bd 423 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
7ba4fbeb
VZ
424
425 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
426
427 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
428 if ( !hEMFCopy )
429 {
430 wxLogLastError(_T("CopyEnhMetaFile"));
431
cbe874bd 432 return false;
7ba4fbeb
VZ
433 }
434
435 *(HENHMETAFILE *)buf = hEMFCopy;
cbe874bd 436 return true;
7ba4fbeb
VZ
437}
438
439bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
440 const void *buf)
441{
442 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
443
444 wxCHECK_MSG( hEMF, FALSE, _T("pasting invalid enh metafile") );
445 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
446
cbe874bd 447 return true;
7ba4fbeb
VZ
448}
449
4ce1efe1 450
a2327a9f 451#endif // wxUSE_DRAG_AND_DROP
d9317fd4
VZ
452
453#endif // wxUSE_ENH_METAFILE