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