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