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