]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/metafile.cpp | |
3 | // Purpose: wxMetafileDC etc. | |
4 | // Author: Julian Smart | |
5 | // Modified by: VZ 07.01.00: implemented wxMetaFileDataObject | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
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 | #ifndef WX_PRECOMP | |
28 | #include "wx/utils.h" | |
29 | #include "wx/app.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/metafile.h" | |
33 | #include "wx/filename.h" | |
34 | ||
35 | #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) | |
36 | ||
37 | #include "wx/clipbrd.h" | |
38 | #include "wx/msw/private.h" | |
39 | ||
40 | #include <stdio.h> | |
41 | #include <string.h> | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // wxWin macros | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject) | |
48 | IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC) | |
49 | ||
50 | // ============================================================================ | |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxMetafileRefData | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | /* | |
59 | * Metafiles | |
60 | * Currently, the only purpose for making a metafile is to put | |
61 | * it on the clipboard. | |
62 | */ | |
63 | ||
64 | wxMetafileRefData::wxMetafileRefData() | |
65 | { | |
66 | m_metafile = 0; | |
67 | m_windowsMappingMode = MM_ANISOTROPIC; | |
68 | m_width = m_height = 0; | |
69 | } | |
70 | ||
71 | wxMetafileRefData::~wxMetafileRefData() | |
72 | { | |
73 | if (m_metafile) | |
74 | { | |
75 | DeleteMetaFile((HMETAFILE) m_metafile); | |
76 | m_metafile = 0; | |
77 | } | |
78 | } | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // wxMetafile | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | wxMetafile::wxMetafile(const wxString& file) | |
85 | { | |
86 | m_refData = new wxMetafileRefData; | |
87 | ||
88 | M_METAFILEDATA->m_windowsMappingMode = MM_ANISOTROPIC; | |
89 | M_METAFILEDATA->m_metafile = 0; | |
90 | if (!file.empty()) | |
91 | M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file); | |
92 | } | |
93 | ||
94 | wxMetafile::~wxMetafile() | |
95 | { | |
96 | } | |
97 | ||
98 | wxGDIRefData *wxMetafile::CreateGDIRefData() const | |
99 | { | |
100 | return new wxMetafileRefData; | |
101 | } | |
102 | ||
103 | wxGDIRefData *wxMetafile::CloneGDIRefData(const wxGDIRefData *data) const | |
104 | { | |
105 | return new wxMetafileRefData(*static_cast<const wxMetafileRefData *>(data)); | |
106 | } | |
107 | ||
108 | bool wxMetafile::SetClipboard(int width, int height) | |
109 | { | |
110 | #if !wxUSE_CLIPBOARD | |
111 | return false; | |
112 | #else | |
113 | if (!m_refData) | |
114 | return false; | |
115 | ||
116 | bool alreadyOpen = wxClipboardOpen(); | |
117 | if (!alreadyOpen) | |
118 | { | |
119 | wxOpenClipboard(); | |
120 | if (!wxEmptyClipboard()) | |
121 | return false; | |
122 | } | |
123 | bool success = wxSetClipboardData(wxDF_METAFILE, this, width,height); | |
124 | if (!alreadyOpen) | |
125 | wxCloseClipboard(); | |
126 | ||
127 | return success; | |
128 | #endif | |
129 | } | |
130 | ||
131 | bool wxMetafile::Play(wxDC *dc) | |
132 | { | |
133 | if (!m_refData) | |
134 | return false; | |
135 | ||
136 | if (dc->GetHDC() && M_METAFILEDATA->m_metafile) | |
137 | { | |
138 | if ( !::PlayMetaFile(GetHdcOf(*dc), (HMETAFILE) | |
139 | M_METAFILEDATA->m_metafile) ) | |
140 | { | |
141 | wxLogLastError(wxT("PlayMetaFile")); | |
142 | } | |
143 | } | |
144 | ||
145 | return true; | |
146 | } | |
147 | ||
148 | void wxMetafile::SetHMETAFILE(WXHANDLE mf) | |
149 | { | |
150 | if (!m_refData) | |
151 | m_refData = new wxMetafileRefData; | |
152 | ||
153 | M_METAFILEDATA->m_metafile = mf; | |
154 | } | |
155 | ||
156 | void wxMetafile::SetWindowsMappingMode(int mm) | |
157 | { | |
158 | if (!m_refData) | |
159 | m_refData = new wxMetafileRefData; | |
160 | ||
161 | M_METAFILEDATA->m_windowsMappingMode = mm; | |
162 | } | |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // Metafile device context | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | // Original constructor that does not takes origin and extent. If you use this, | |
169 | // *DO* give origin/extent arguments to wxMakeMetafilePlaceable. | |
170 | wxMetafileDCImpl::wxMetafileDCImpl(wxDC *owner, const wxString& file) | |
171 | : wxMSWDCImpl(owner) | |
172 | { | |
173 | m_metaFile = NULL; | |
174 | m_minX = 10000; | |
175 | m_minY = 10000; | |
176 | m_maxX = -10000; | |
177 | m_maxY = -10000; | |
178 | // m_title = NULL; | |
179 | ||
180 | if ( wxFileExists(file) ) | |
181 | wxRemoveFile(file); | |
182 | ||
183 | if ( file.empty() ) | |
184 | m_hDC = (WXHDC) CreateMetaFile(NULL); | |
185 | else | |
186 | m_hDC = (WXHDC) CreateMetaFile(file); | |
187 | ||
188 | m_ok = (m_hDC != (WXHDC) 0) ; | |
189 | ||
190 | // Actual Windows mapping mode, for future reference. | |
191 | m_windowsMappingMode = wxMM_TEXT; | |
192 | ||
193 | SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct) | |
194 | } | |
195 | ||
196 | // New constructor that takes origin and extent. If you use this, don't | |
197 | // give origin/extent arguments to wxMakeMetafilePlaceable. | |
198 | wxMetafileDCImpl::wxMetafileDCImpl(wxDC *owner, const wxString& file, | |
199 | int xext, int yext, int xorg, int yorg) | |
200 | : wxMSWDCImpl(owner) | |
201 | { | |
202 | m_minX = 10000; | |
203 | m_minY = 10000; | |
204 | m_maxX = -10000; | |
205 | m_maxY = -10000; | |
206 | if ( !file.empty() && wxFileExists(file) ) | |
207 | wxRemoveFile(file); | |
208 | m_hDC = (WXHDC) CreateMetaFile(file.empty() ? NULL : wxMSW_CONV_LPCTSTR(file)); | |
209 | ||
210 | m_ok = true; | |
211 | ||
212 | ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL); | |
213 | ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL); | |
214 | ||
215 | // Actual Windows mapping mode, for future reference. | |
216 | m_windowsMappingMode = MM_ANISOTROPIC; | |
217 | ||
218 | SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct) | |
219 | } | |
220 | ||
221 | wxMetafileDCImpl::~wxMetafileDCImpl() | |
222 | { | |
223 | m_hDC = 0; | |
224 | } | |
225 | ||
226 | void wxMetafileDCImpl::DoGetTextExtent(const wxString& string, | |
227 | wxCoord *x, wxCoord *y, | |
228 | wxCoord *descent, wxCoord *externalLeading, | |
229 | const wxFont *theFont) const | |
230 | { | |
231 | const wxFont *fontToUse = theFont; | |
232 | if (!fontToUse) | |
233 | fontToUse = &m_font; | |
234 | ||
235 | ScreenHDC dc; | |
236 | SelectInHDC selFont(dc, GetHfontOf(*fontToUse)); | |
237 | ||
238 | SIZE sizeRect; | |
239 | TEXTMETRIC tm; | |
240 | ::GetTextExtentPoint32(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect); | |
241 | ::GetTextMetrics(dc, &tm); | |
242 | ||
243 | if ( x ) | |
244 | *x = sizeRect.cx; | |
245 | if ( y ) | |
246 | *y = sizeRect.cy; | |
247 | if ( descent ) | |
248 | *descent = tm.tmDescent; | |
249 | if ( externalLeading ) | |
250 | *externalLeading = tm.tmExternalLeading; | |
251 | } | |
252 | ||
253 | void wxMetafileDCImpl::DoGetSize(int *width, int *height) const | |
254 | { | |
255 | wxCHECK_RET( m_refData, wxT("invalid wxMetafileDC") ); | |
256 | ||
257 | if ( width ) | |
258 | *width = M_METAFILEDATA->m_width; | |
259 | if ( height ) | |
260 | *height = M_METAFILEDATA->m_height; | |
261 | } | |
262 | ||
263 | wxMetafile *wxMetafileDCImpl::Close() | |
264 | { | |
265 | SelectOldObjects(m_hDC); | |
266 | HANDLE mf = CloseMetaFile((HDC) m_hDC); | |
267 | m_hDC = 0; | |
268 | if (mf) | |
269 | { | |
270 | wxMetafile *wx_mf = new wxMetafile; | |
271 | wx_mf->SetHMETAFILE((WXHANDLE) mf); | |
272 | wx_mf->SetWindowsMappingMode(m_windowsMappingMode); | |
273 | return wx_mf; | |
274 | } | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | void wxMetafileDCImpl::SetMapMode(wxMappingMode mode) | |
279 | { | |
280 | m_mappingMode = mode; | |
281 | ||
282 | // int pixel_width = 0; | |
283 | // int pixel_height = 0; | |
284 | // int mm_width = 0; | |
285 | // int mm_height = 0; | |
286 | ||
287 | float mm2pixelsX = 10.0; | |
288 | float mm2pixelsY = 10.0; | |
289 | ||
290 | switch (mode) | |
291 | { | |
292 | case wxMM_TWIPS: | |
293 | { | |
294 | m_logicalScaleX = (float)(twips2mm * mm2pixelsX); | |
295 | m_logicalScaleY = (float)(twips2mm * mm2pixelsY); | |
296 | break; | |
297 | } | |
298 | case wxMM_POINTS: | |
299 | { | |
300 | m_logicalScaleX = (float)(pt2mm * mm2pixelsX); | |
301 | m_logicalScaleY = (float)(pt2mm * mm2pixelsY); | |
302 | break; | |
303 | } | |
304 | case wxMM_METRIC: | |
305 | { | |
306 | m_logicalScaleX = mm2pixelsX; | |
307 | m_logicalScaleY = mm2pixelsY; | |
308 | break; | |
309 | } | |
310 | case wxMM_LOMETRIC: | |
311 | { | |
312 | m_logicalScaleX = (float)(mm2pixelsX/10.0); | |
313 | m_logicalScaleY = (float)(mm2pixelsY/10.0); | |
314 | break; | |
315 | } | |
316 | default: | |
317 | case wxMM_TEXT: | |
318 | { | |
319 | m_logicalScaleX = 1.0; | |
320 | m_logicalScaleY = 1.0; | |
321 | break; | |
322 | } | |
323 | } | |
324 | } | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // wxMakeMetafilePlaceable | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | #ifdef __WIN32__ | |
331 | struct RECT32 | |
332 | { | |
333 | short left; | |
334 | short top; | |
335 | short right; | |
336 | short bottom; | |
337 | }; | |
338 | ||
339 | struct mfPLACEABLEHEADER { | |
340 | DWORD key; | |
341 | short hmf; | |
342 | RECT32 bbox; | |
343 | WORD inch; | |
344 | DWORD reserved; | |
345 | WORD checksum; | |
346 | }; | |
347 | #else | |
348 | struct mfPLACEABLEHEADER { | |
349 | DWORD key; | |
350 | HANDLE hmf; | |
351 | RECT bbox; | |
352 | WORD inch; | |
353 | DWORD reserved; | |
354 | WORD checksum; | |
355 | }; | |
356 | #endif | |
357 | ||
358 | /* | |
359 | * Pass filename of existing non-placeable metafile, and bounding box. | |
360 | * Adds a placeable metafile header, sets the mapping mode to anisotropic, | |
361 | * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode. | |
362 | * | |
363 | */ | |
364 | ||
365 | bool wxMakeMetafilePlaceable(const wxString& filename, float scale) | |
366 | { | |
367 | return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false); | |
368 | } | |
369 | ||
370 | bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent) | |
371 | { | |
372 | // I'm not sure if this is the correct way of suggesting a scale | |
373 | // to the client application, but it's the only way I can find. | |
374 | int unitsPerInch = (int)(576/scale); | |
375 | ||
376 | mfPLACEABLEHEADER header; | |
377 | header.key = 0x9AC6CDD7L; | |
378 | header.hmf = 0; | |
379 | header.bbox.left = (int)(x1); | |
380 | header.bbox.top = (int)(y1); | |
381 | header.bbox.right = (int)(x2); | |
382 | header.bbox.bottom = (int)(y2); | |
383 | header.inch = unitsPerInch; | |
384 | header.reserved = 0; | |
385 | ||
386 | // Calculate checksum | |
387 | WORD *p; | |
388 | mfPLACEABLEHEADER *pMFHead = &header; | |
389 | for (p =(WORD *)pMFHead,pMFHead -> checksum = 0; | |
390 | p < (WORD *)&pMFHead ->checksum; ++p) | |
391 | pMFHead ->checksum ^= *p; | |
392 | ||
393 | FILE *fd = wxFopen(filename.fn_str(), wxT("rb")); | |
394 | if (!fd) return false; | |
395 | ||
396 | wxString tempFileBuf = wxFileName::CreateTempFileName(wxT("mf")); | |
397 | if (tempFileBuf.empty()) | |
398 | return false; | |
399 | ||
400 | FILE *fHandle = wxFopen(tempFileBuf.fn_str(), wxT("wb")); | |
401 | if (!fHandle) | |
402 | return false; | |
403 | fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle); | |
404 | ||
405 | // Calculate origin and extent | |
406 | int originX = x1; | |
407 | int originY = y1; | |
408 | int extentX = x2 - x1; | |
409 | int extentY = (y2 - y1); | |
410 | ||
411 | // Read metafile header and write | |
412 | METAHEADER metaHeader; | |
413 | fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd); | |
414 | ||
415 | if (useOriginAndExtent) | |
416 | metaHeader.mtSize += 15; | |
417 | else | |
418 | metaHeader.mtSize += 5; | |
419 | ||
420 | fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle); | |
421 | ||
422 | // Write SetMapMode, SetWindowOrigin and SetWindowExt records | |
423 | char modeBuffer[8]; | |
424 | char originBuffer[10]; | |
425 | char extentBuffer[10]; | |
426 | METARECORD *modeRecord = (METARECORD *)&modeBuffer; | |
427 | ||
428 | METARECORD *originRecord = (METARECORD *)&originBuffer; | |
429 | METARECORD *extentRecord = (METARECORD *)&extentBuffer; | |
430 | ||
431 | modeRecord->rdSize = 4; | |
432 | modeRecord->rdFunction = META_SETMAPMODE; | |
433 | modeRecord->rdParm[0] = MM_ANISOTROPIC; | |
434 | ||
435 | originRecord->rdSize = 5; | |
436 | originRecord->rdFunction = META_SETWINDOWORG; | |
437 | originRecord->rdParm[0] = originY; | |
438 | originRecord->rdParm[1] = originX; | |
439 | ||
440 | extentRecord->rdSize = 5; | |
441 | extentRecord->rdFunction = META_SETWINDOWEXT; | |
442 | extentRecord->rdParm[0] = extentY; | |
443 | extentRecord->rdParm[1] = extentX; | |
444 | ||
445 | fwrite((void *)modeBuffer, sizeof(char), 8, fHandle); | |
446 | ||
447 | if (useOriginAndExtent) | |
448 | { | |
449 | fwrite((void *)originBuffer, sizeof(char), 10, fHandle); | |
450 | fwrite((void *)extentBuffer, sizeof(char), 10, fHandle); | |
451 | } | |
452 | ||
453 | int ch = -2; | |
454 | while (ch != EOF) | |
455 | { | |
456 | ch = getc(fd); | |
457 | if (ch != EOF) | |
458 | { | |
459 | putc(ch, fHandle); | |
460 | } | |
461 | } | |
462 | fclose(fHandle); | |
463 | fclose(fd); | |
464 | wxRemoveFile(filename); | |
465 | wxCopyFile(tempFileBuf, filename); | |
466 | wxRemoveFile(tempFileBuf); | |
467 | return true; | |
468 | } | |
469 | ||
470 | ||
471 | #if wxUSE_DRAG_AND_DROP | |
472 | ||
473 | // ---------------------------------------------------------------------------- | |
474 | // wxMetafileDataObject | |
475 | // ---------------------------------------------------------------------------- | |
476 | ||
477 | size_t wxMetafileDataObject::GetDataSize() const | |
478 | { | |
479 | return sizeof(METAFILEPICT); | |
480 | } | |
481 | ||
482 | bool wxMetafileDataObject::GetDataHere(void *buf) const | |
483 | { | |
484 | METAFILEPICT *mfpict = (METAFILEPICT *)buf; | |
485 | const wxMetafile& mf = GetMetafile(); | |
486 | ||
487 | wxCHECK_MSG( mf.GetHMETAFILE(), false, wxT("copying invalid metafile") ); | |
488 | ||
489 | // doesn't seem to work with any other mapping mode... | |
490 | mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode(); | |
491 | mfpict->xExt = mf.GetWidth(); | |
492 | mfpict->yExt = mf.GetHeight(); | |
493 | ||
494 | // transform the picture size to HIMETRIC units (0.01mm) - as we don't know | |
495 | // what DC the picture will be rendered to, use the default display one | |
496 | PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt); | |
497 | ||
498 | mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL); | |
499 | ||
500 | return true; | |
501 | } | |
502 | ||
503 | bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
504 | { | |
505 | const METAFILEPICT *mfpict = (const METAFILEPICT *)buf; | |
506 | ||
507 | wxMetafile mf; | |
508 | mf.SetWindowsMappingMode(mfpict->mm); | |
509 | ||
510 | LONG w = mfpict->xExt, | |
511 | h = mfpict->yExt; | |
512 | if ( mfpict->mm == MM_ANISOTROPIC ) | |
513 | { | |
514 | // in this case xExt and yExt contain suggested size in HIMETRIC units | |
515 | // (0.01 mm) - transform this to something more reasonable (pixels) | |
516 | HIMETRICToPixel(&w, &h); | |
517 | } | |
518 | ||
519 | mf.SetWidth(w); | |
520 | mf.SetHeight(h); | |
521 | mf.SetHMETAFILE((WXHANDLE)mfpict->hMF); | |
522 | ||
523 | wxCHECK_MSG( mfpict->hMF, false, wxT("pasting invalid metafile") ); | |
524 | ||
525 | SetMetafile(mf); | |
526 | ||
527 | return true; | |
528 | } | |
529 | ||
530 | #endif // wxUSE_DRAG_AND_DROP | |
531 | ||
532 | #endif // wxUSE_METAFILE |