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