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