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