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