]> git.saurik.com Git - wxWidgets.git/blame - src/msw/enhmeta.cpp
Patch wxGTK1 for recent changes in bmpbuttn
[wxWidgets.git] / src / msw / enhmeta.cpp
CommitLineData
d9317fd4 1///////////////////////////////////////////////////////////////////////////////
658ff7f1 2// Name: src/msw/enhmeta.cpp
d9317fd4
VZ
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
d9317fd4
VZ
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"
7dca9b44 32 #include "wx/intl.h"
d9317fd4
VZ
33#endif //WX_PRECOMP
34
888dde65
RR
35#include "wx/dc.h"
36#include "wx/msw/dc.h"
37
d9317fd4 38#include "wx/metafile.h"
bfbd6dc1 39#include "wx/clipbrd.h"
d9317fd4
VZ
40
41#include "wx/msw/private.h"
42
43// ----------------------------------------------------------------------------
44// wxWin macros
45// ----------------------------------------------------------------------------
46
47IMPLEMENT_DYNAMIC_CLASS(wxEnhMetaFile, wxObject)
d9317fd4
VZ
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
61static inline const wxChar *GetMetaFileName(const wxString& fn)
e0a050e3 62 { return !fn ? (const wxChar *)NULL : (const wxChar*)fn.wx_str(); }
d9317fd4
VZ
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxEnhMetaFile
70// ----------------------------------------------------------------------------
71
8f884a0d
VZ
72wxGDIRefData *wxEnhMetaFile::CreateGDIRefData() const
73{
74 wxFAIL_MSG( _T("must be implemented if used") );
75
76 return NULL;
77}
78
79wxGDIRefData *
80wxEnhMetaFile::CloneGDIRefData(const wxGDIRefData *WXUNUSED(data)) const
81{
82 wxFAIL_MSG( _T("must be implemented if used") );
83
84 return NULL;
85}
86
7a295dfa
VZ
87void 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 {
e0a050e3 95 m_hMF = (WXHANDLE)::GetEnhMetaFile(m_filename.fn_str());
7a295dfa
VZ
96 if ( !m_hMF )
97 wxLogSysError(_("Failed to load metafile from file \"%s\"."),
98 m_filename.c_str());
99 }
100}
101
d9317fd4
VZ
102void 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
122void wxEnhMetaFile::Free()
123{
124 if ( m_hMF )
125 {
126 if ( !::DeleteEnhMetaFile(GetEMF()) )
127 {
128 wxLogLastError(_T("DeleteEnhMetaFile"));
129 }
130 }
131}
132
133bool wxEnhMetaFile::Play(wxDC *dc, wxRect *rectBound)
134{
cbe874bd 135 wxCHECK_MSG( Ok(), false, _T("can't play invalid enhanced metafile") );
caf448e3 136 wxCHECK_MSG( dc, false, _T("invalid wxDC in wxEnhMetaFile::Play") );
d9317fd4
VZ
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
888dde65
RR
156 wxDCImpl *impl = dc->GetImpl();
157 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
158 if (!msw_impl)
159 return false;
f0875501 160
888dde65 161 if ( !::PlayEnhMetaFile(GetHdcOf(*msw_impl), GetEMF(), &rect) )
d9317fd4
VZ
162 {
163 wxLogLastError(_T("PlayEnhMetaFile"));
164
cbe874bd 165 return false;
d9317fd4
VZ
166 }
167
cbe874bd 168 return true;
d9317fd4
VZ
169}
170
171wxSize 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
bfbd6dc1
VZ
199bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
200{
caf448e3
WS
201#if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
202 wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") );
bfbd6dc1
VZ
203
204 return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
a95e38c0
VZ
205#else // !wxUSE_DRAG_AND_DROP
206 wxFAIL_MSG(_T("not implemented"));
cbe874bd 207 return false;
a95e38c0 208#endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
bfbd6dc1
VZ
209}
210
d9317fd4 211// ----------------------------------------------------------------------------
0273787a 212// wxEnhMetaFileDCImpl
d9317fd4
VZ
213// ----------------------------------------------------------------------------
214
888dde65
RR
215class wxEnhMetaFileDCImpl : public wxMSWDCImpl
216{
217public:
f0875501 218 wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner,
888dde65
RR
219 const wxString& filename, int width, int height,
220 const wxString& description );
cc344571
VS
221 wxEnhMetaFileDCImpl( wxEnhMetaFileDC *owner,
222 const wxDC& referenceDC,
223 const wxString& filename, int width, int height,
224 const wxString& description );
888dde65
RR
225 virtual ~wxEnhMetaFileDCImpl();
226
227 // obtain a pointer to the new metafile (caller should delete it)
228 wxEnhMetaFile *Close();
229
230protected:
231 virtual void DoGetSize(int *width, int *height) const;
232
233private:
cc344571
VS
234 void Create(HDC hdcRef,
235 const wxString& filename, int width, int height,
236 const wxString& description);
237
888dde65
RR
238 // size passed to ctor and returned by DoGetSize()
239 int m_width,
240 m_height;
241};
242
243
888dde65
RR
244wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner,
245 const wxString& filename,
246 int width, int height,
247 const wxString& description )
0273787a 248 : wxMSWDCImpl( owner )
cc344571
VS
249{
250 Create(ScreenHDC(), filename, width, height, description);
251}
252
253wxEnhMetaFileDCImpl::wxEnhMetaFileDCImpl( wxEnhMetaFileDC* owner,
254 const wxDC& referenceDC,
255 const wxString& filename,
256 int width, int height,
257 const wxString& description )
258 : wxMSWDCImpl( owner )
259{
260 Create(GetHdcOf(referenceDC), filename, width, height, description);
261}
262
263void wxEnhMetaFileDCImpl::Create(HDC hdcRef,
264 const wxString& filename,
265 int width, int height,
266 const wxString& description)
d9317fd4 267{
024026be
VZ
268 m_width = width;
269 m_height = height;
270
33ac7e6f 271 RECT rect;
cbe874bd 272 RECT *pRect;
d9317fd4
VZ
273 if ( width && height )
274 {
275 rect.top =
276 rect.left = 0;
277 rect.right = width;
278 rect.bottom = height;
279
280 // CreateEnhMetaFile() wants them in HIMETRIC
cc344571 281 PixelToHIMETRIC(&rect.right, &rect.bottom, hdcRef);
cbe874bd 282
d9317fd4
VZ
283 pRect = &rect;
284 }
285 else
286 {
287 // GDI will try to find out the size for us (not recommended)
288 pRect = (LPRECT)NULL;
289 }
290
291 m_hDC = (WXHDC)::CreateEnhMetaFile(hdcRef, GetMetaFileName(filename),
e0a050e3 292 pRect, description.wx_str());
d9317fd4
VZ
293 if ( !m_hDC )
294 {
295 wxLogLastError(_T("CreateEnhMetaFile"));
296 }
297}
298
888dde65 299void wxEnhMetaFileDCImpl::DoGetSize(int *width, int *height) const
024026be
VZ
300{
301 if ( width )
302 *width = m_width;
303 if ( height )
304 *height = m_height;
305}
306
888dde65 307wxEnhMetaFile *wxEnhMetaFileDCImpl::Close()
d9317fd4 308{
888dde65 309 wxCHECK_MSG( IsOk(), NULL, _T("invalid wxEnhMetaFileDC") );
d9317fd4
VZ
310
311 HENHMETAFILE hMF = ::CloseEnhMetaFile(GetHdc());
312 if ( !hMF )
313 {
314 wxLogLastError(_T("CloseEnhMetaFile"));
315
316 return NULL;
317 }
318
319 wxEnhMetaFile *mf = new wxEnhMetaFile;
320 mf->SetHENHMETAFILE((WXHANDLE)hMF);
321 return mf;
322}
323
888dde65 324wxEnhMetaFileDCImpl::~wxEnhMetaFileDCImpl()
d9317fd4
VZ
325{
326 // avoid freeing it in the base class dtor
327 m_hDC = 0;
328}
329
0273787a
VZ
330// ----------------------------------------------------------------------------
331// wxEnhMetaFileDC
332// ----------------------------------------------------------------------------
333
334IMPLEMENT_ABSTRACT_CLASS(wxEnhMetaFileDC, wxDC)
335
336wxEnhMetaFileDC::wxEnhMetaFileDC(const wxString& filename,
337 int width, int height,
338 const wxString& description)
339 : wxDC(new wxEnhMetaFileDCImpl(this,
340 filename,
341 width, height,
342 description))
343{
344}
345
cc344571
VS
346wxEnhMetaFileDC::wxEnhMetaFileDC(const wxDC& referenceDC,
347 const wxString& filename,
348 int width, int height,
349 const wxString& description)
350 : wxDC(new wxEnhMetaFileDCImpl(this,
351 referenceDC,
352 filename,
353 width, height,
354 description))
355{
356}
357
0273787a
VZ
358wxEnhMetaFile *wxEnhMetaFileDC::Close()
359{
360 wxEnhMetaFileDCImpl * const
5c33522f 361 impl = static_cast<wxEnhMetaFileDCImpl *>(GetImpl());
0273787a
VZ
362 wxCHECK_MSG( impl, NULL, _T("no wxEnhMetaFileDC implementation") );
363
364 return impl->Close();
365}
366
7ba4fbeb
VZ
367#if wxUSE_DRAG_AND_DROP
368
d9317fd4
VZ
369// ----------------------------------------------------------------------------
370// wxEnhMetaFileDataObject
371// ----------------------------------------------------------------------------
372
373wxDataFormat
374wxEnhMetaFileDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
375{
376 return wxDF_ENHMETAFILE;
377}
378
379size_t wxEnhMetaFileDataObject::GetFormatCount(Direction WXUNUSED(dir)) const
380{
381 // wxDF_ENHMETAFILE and wxDF_METAFILE
382 return 2;
383}
384
385void wxEnhMetaFileDataObject::GetAllFormats(wxDataFormat *formats,
386 Direction WXUNUSED(dir)) const
387{
388 formats[0] = wxDF_ENHMETAFILE;
389 formats[1] = wxDF_METAFILE;
390}
391
392size_t wxEnhMetaFileDataObject::GetDataSize(const wxDataFormat& format) const
393{
394 if ( format == wxDF_ENHMETAFILE )
395 {
396 // we pass data by handle and not HGLOBAL
397 return 0u;
398 }
399 else
400 {
401 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
402
403 return sizeof(METAFILEPICT);
404 }
405}
406
407bool wxEnhMetaFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
408{
cbe874bd 409 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
d9317fd4
VZ
410
411 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
412
413 if ( format == wxDF_ENHMETAFILE )
414 {
415 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
416 if ( !hEMFCopy )
417 {
418 wxLogLastError(_T("CopyEnhMetaFile"));
419
cbe874bd 420 return false;
d9317fd4
VZ
421 }
422
423 *(HENHMETAFILE *)buf = hEMFCopy;
424 }
425 else
426 {
427 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
428
429 // convert to WMF
430
431 ScreenHDC hdc;
432
433 // first get the buffer size and alloc memory
434 size_t size = ::GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hdc);
caf448e3 435 wxCHECK_MSG( size, false, _T("GetWinMetaFileBits() failed") );
d9317fd4
VZ
436
437 BYTE *bits = (BYTE *)malloc(size);
438
439 // then get the enh metafile bits
440 if ( !::GetWinMetaFileBits(hEMF, size, bits, MM_ANISOTROPIC, hdc) )
441 {
442 wxLogLastError(_T("GetWinMetaFileBits"));
443
444 free(bits);
445
cbe874bd 446 return false;
d9317fd4
VZ
447 }
448
449 // and finally convert them to the WMF
450 HMETAFILE hMF = ::SetMetaFileBitsEx(size, bits);
451 free(bits);
452 if ( !hMF )
453 {
454 wxLogLastError(_T("SetMetaFileBitsEx"));
455
cbe874bd 456 return false;
d9317fd4
VZ
457 }
458
459 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
460
461 wxSize sizeMF = m_metafile.GetSize();
462 mfpict->hMF = hMF;
463 mfpict->mm = MM_ANISOTROPIC;
464 mfpict->xExt = sizeMF.x;
465 mfpict->yExt = sizeMF.y;
466
467 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
468 }
469
cbe874bd 470 return true;
d9317fd4
VZ
471}
472
473bool wxEnhMetaFileDataObject::SetData(const wxDataFormat& format,
474 size_t WXUNUSED(len),
475 const void *buf)
476{
477 HENHMETAFILE hEMF;
478
479 if ( format == wxDF_ENHMETAFILE )
480 {
481 hEMF = *(HENHMETAFILE *)buf;
482
caf448e3 483 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
d9317fd4
VZ
484 }
485 else
486 {
487 wxASSERT_MSG( format == wxDF_METAFILE, _T("unsupported format") );
488
489 // convert from WMF
490 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
491
492 // first get the buffer size
493 size_t size = ::GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
caf448e3 494 wxCHECK_MSG( size, false, _T("GetMetaFileBitsEx() failed") );
d9317fd4
VZ
495
496 // then get metafile bits
497 BYTE *bits = (BYTE *)malloc(size);
498 if ( !::GetMetaFileBitsEx(mfpict->hMF, size, bits) )
499 {
500 wxLogLastError(_T("GetMetaFileBitsEx"));
501
502 free(bits);
503
cbe874bd 504 return false;
d9317fd4
VZ
505 }
506
507 ScreenHDC hdcRef;
508
509 // and finally create an enhanced metafile from them
510 hEMF = ::SetWinMetaFileBits(size, bits, hdcRef, mfpict);
511 free(bits);
512 if ( !hEMF )
513 {
514 wxLogLastError(_T("SetWinMetaFileBits"));
515
cbe874bd 516 return false;
d9317fd4
VZ
517 }
518 }
519
520 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
521
cbe874bd 522 return true;
d9317fd4 523}
7ba4fbeb
VZ
524
525// ----------------------------------------------------------------------------
526// wxEnhMetaFileSimpleDataObject
527// ----------------------------------------------------------------------------
528
529size_t wxEnhMetaFileSimpleDataObject::GetDataSize() const
530{
531 // we pass data by handle and not HGLOBAL
532 return 0u;
533}
534
535bool wxEnhMetaFileSimpleDataObject::GetDataHere(void *buf) const
536{
cbe874bd 537 wxCHECK_MSG( m_metafile.Ok(), false, _T("copying invalid enh metafile") );
7ba4fbeb
VZ
538
539 HENHMETAFILE hEMF = (HENHMETAFILE)m_metafile.GetHENHMETAFILE();
540
541 HENHMETAFILE hEMFCopy = ::CopyEnhMetaFile(hEMF, NULL);
542 if ( !hEMFCopy )
543 {
544 wxLogLastError(_T("CopyEnhMetaFile"));
545
cbe874bd 546 return false;
7ba4fbeb
VZ
547 }
548
549 *(HENHMETAFILE *)buf = hEMFCopy;
cbe874bd 550 return true;
7ba4fbeb
VZ
551}
552
553bool wxEnhMetaFileSimpleDataObject::SetData(size_t WXUNUSED(len),
554 const void *buf)
555{
556 HENHMETAFILE hEMF = *(HENHMETAFILE *)buf;
557
caf448e3 558 wxCHECK_MSG( hEMF, false, _T("pasting invalid enh metafile") );
7ba4fbeb
VZ
559 m_metafile.SetHENHMETAFILE((WXHANDLE)hEMF);
560
cbe874bd 561 return true;
7ba4fbeb
VZ
562}
563
4ce1efe1 564
a2327a9f 565#endif // wxUSE_DRAG_AND_DROP
d9317fd4
VZ
566
567#endif // wxUSE_ENH_METAFILE