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