]> git.saurik.com Git - wxWidgets.git/blob - src/os2/metafile.cpp
Don't assume that msw.remap was 1, instead change it back to what it
[wxWidgets.git] / src / os2 / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/metafile.cpp
3 // Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/10/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
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"
23 #include "wx/clipbrd.h"
24 #include "wx/os2/private.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 extern bool wxClipboardIsOpen;
30
31 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
32 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
33
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)
47 {
48 if (m_metafile)
49 {
50 // TODO: DeleteMetaFile((HMETAFILE) m_metafile);
51 m_metafile = 0;
52 }
53 }
54
55 wxMetafile::wxMetafile(const wxString& file)
56 {
57 m_refData = new wxMetafileRefData;
58
59 M_METAFILEDATA->m_windowsMappingMode = wxMM_ANISOTROPIC;
60 M_METAFILEDATA->m_metafile = 0;
61 if (!file.empty())
62 M_METAFILEDATA->m_metafile = (WXHANDLE)0; // TODO: GetMetaFile(file);
63 }
64
65 wxMetafile::~wxMetafile(void)
66 {
67 }
68
69 bool wxMetafile::SetClipboard(int width, int height)
70 {
71 #if !wxUSE_CLIPBOARD
72 wxUnusedVar(width);
73 wxUnusedVar(height);
74 return false;
75 #else
76 if (!m_refData)
77 return false;
78
79 bool alreadyOpen=wxClipboardOpen();
80 if (!alreadyOpen)
81 {
82 wxOpenClipboard();
83 if (!wxEmptyClipboard()) return FALSE;
84 }
85 bool success = wxSetClipboardData(wxDF_METAFILE, this, width,height);
86 if (!alreadyOpen) wxCloseClipboard();
87 return (bool) success;
88 #endif
89 }
90
91 bool wxMetafile::Play(wxDC *dc)
92 {
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
103 return true;
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;
120 }
121
122 /*
123 * Metafile device context
124 *
125 */
126
127 // Original constructor that does not takes origin and extent. If you use this,
128 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
129 wxMetafileDC::wxMetafileDC(const wxString& file)
130 {
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 /*
143 if (!file.empty())
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)
155 }
156
157 // New constructor that takes origin and extent. If you use this, don't
158 // give origin/extent arguments to wxMakeMetafilePlaceable.
159 wxMetafileDC::wxMetafileDC( const wxString& file,
160 int WXUNUSED(xext),
161 int WXUNUSED(yext),
162 int WXUNUSED(xorg),
163 int WXUNUSED(yorg) )
164 {
165 m_minX = 10000;
166 m_minY = 10000;
167 m_maxX = -10000;
168 m_maxY = -10000;
169 if (!file.empty() && wxFileExists(file))
170 wxRemoveFile(file);
171
172 // m_hDC = (WXHDC) CreateMetaFile(file);
173
174 m_ok = true;
175
176 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
177 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
178
179 // Actual Windows mapping mode, for future reference.
180 m_windowsMappingMode = wxMM_ANISOTROPIC;
181
182 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
183 }
184
185 wxMetafileDC::~wxMetafileDC(void)
186 {
187 m_hDC = 0;
188 }
189
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
197 {
198 wxFont *fontToUse = theFont;
199 if (!fontToUse)
200 fontToUse = (wxFont*) &m_font;
201
202 // TODO:
203 /*
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;
221 */
222 }
223
224 wxMetafile *wxMetafileDC::Close(void)
225 {
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;
237 }
238
239 void wxMetafileDC::SetMapMode(int mode)
240 {
241 m_mappingMode = mode;
242
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 }
285 m_nWindowExtX = 100;
286 m_nWindowExtY = 100;
287 }
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 {
299 DWORD key;
300 short hmf;
301 RECT32 bbox;
302 WORD inch;
303 DWORD reserved;
304 WORD checksum;
305 };
306 #else
307 struct mfPLACEABLEHEADER {
308 DWORD key;
309 HANDLE hmf;
310 RECT bbox;
311 WORD inch;
312 DWORD reserved;
313 WORD checksum;
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 */
323
324 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
325 {
326 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
327 }
328
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))
336 {
337 // TODO: the OS/2 PM/MM way to do this
338 /*
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;
362
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);
369
370 // Calculate origin and extent
371 int originX = x1;
372 int originY = y1;
373 int extentX = x2 - x1;
374 int extentY = (y2 - y1);
375
376 // Read metafile header and write
377 METAHEADER metaHeader;
378 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
379
380 if (useOriginAndExtent)
381 metaHeader.mtSize += 15;
382 else
383 metaHeader.mtSize += 5;
384
385 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
386
387 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
388 char modeBuffer[8];
389 char originBuffer[10];
390 char extentBuffer[10];
391 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
392
393 METARECORD *originRecord = (METARECORD *)&originBuffer;
394 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
395
396 modeRecord->rdSize = 4;
397 modeRecord->rdFunction = META_SETMAPMODE;
398 modeRecord->rdParm[0] = MM_ANISOTROPIC;
399
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)
413 {
414 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
415 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
416 }
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);
432 */
433 return true;
434 }
435
436 #endif // wxUSE_METAFILE