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