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