]> git.saurik.com Git - wxWidgets.git/blob - src/os2/metafile.cpp
wxWinCE build fix and source cleaning.
[wxWidgets.git] / src / os2 / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
16 #include "wx/setup.h"
17 #endif
18
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"
27 #include "wx/clipbrd.h"
28 #include "wx/os2/private.h"
29
30 #include <stdio.h>
31 #include <string.h>
32
33 extern bool wxClipboardIsOpen;
34
35 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
36 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
37
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)
51 {
52 if (m_metafile)
53 {
54 // TODO: DeleteMetaFile((HMETAFILE) m_metafile);
55 m_metafile = 0;
56 }
57 }
58
59 wxMetafile::wxMetafile(const wxString& file)
60 {
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);
67 }
68
69 wxMetafile::~wxMetafile(void)
70 {
71 }
72
73 bool wxMetafile::SetClipboard(int width, int height)
74 {
75 #if !wxUSE_CLIPBOARD
76 wxUnusedVar(width);
77 wxUnusedVar(height);
78 return false;
79 #else
80 if (!m_refData)
81 return false;
82
83 bool alreadyOpen=wxClipboardOpen();
84 if (!alreadyOpen)
85 {
86 wxOpenClipboard();
87 if (!wxEmptyClipboard()) return FALSE;
88 }
89 bool success = wxSetClipboardData(wxDF_METAFILE, this, width,height);
90 if (!alreadyOpen) wxCloseClipboard();
91 return (bool) success;
92 #endif
93 }
94
95 bool wxMetafile::Play(wxDC *dc)
96 {
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
107 return true;
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;
124 }
125
126 /*
127 * Metafile device context
128 *
129 */
130
131 // Original constructor that does not takes origin and extent. If you use this,
132 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
133 wxMetafileDC::wxMetafileDC(const wxString& file)
134 {
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)
159 }
160
161 // New constructor that takes origin and extent. If you use this, don't
162 // give origin/extent arguments to wxMakeMetafilePlaceable.
163 wxMetafileDC::wxMetafileDC( const wxString& file,
164 int WXUNUSED(xext),
165 int WXUNUSED(yext),
166 int WXUNUSED(xorg),
167 int WXUNUSED(yorg) )
168 {
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
176 // m_hDC = (WXHDC) CreateMetaFile(file);
177
178 m_ok = true;
179
180 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
181 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
182
183 // Actual Windows mapping mode, for future reference.
184 m_windowsMappingMode = wxMM_ANISOTROPIC;
185
186 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
187 }
188
189 wxMetafileDC::~wxMetafileDC(void)
190 {
191 m_hDC = 0;
192 }
193
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
201 {
202 wxFont *fontToUse = theFont;
203 if (!fontToUse)
204 fontToUse = (wxFont*) &m_font;
205
206 // TODO:
207 /*
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;
225 */
226 }
227
228 wxMetafile *wxMetafileDC::Close(void)
229 {
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;
241 }
242
243 void wxMetafileDC::SetMapMode(int mode)
244 {
245 m_mappingMode = mode;
246
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 }
289 m_nWindowExtX = 100;
290 m_nWindowExtY = 100;
291 }
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 {
303 DWORD key;
304 short hmf;
305 RECT32 bbox;
306 WORD inch;
307 DWORD reserved;
308 WORD checksum;
309 };
310 #else
311 struct mfPLACEABLEHEADER {
312 DWORD key;
313 HANDLE hmf;
314 RECT bbox;
315 WORD inch;
316 DWORD reserved;
317 WORD checksum;
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 */
327
328 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
329 {
330 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
331 }
332
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))
340 {
341 // TODO: the OS/2 PM/MM way to do this
342 /*
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;
366
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);
373
374 // Calculate origin and extent
375 int originX = x1;
376 int originY = y1;
377 int extentX = x2 - x1;
378 int extentY = (y2 - y1);
379
380 // Read metafile header and write
381 METAHEADER metaHeader;
382 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
383
384 if (useOriginAndExtent)
385 metaHeader.mtSize += 15;
386 else
387 metaHeader.mtSize += 5;
388
389 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
390
391 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
392 char modeBuffer[8];
393 char originBuffer[10];
394 char extentBuffer[10];
395 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
396
397 METARECORD *originRecord = (METARECORD *)&originBuffer;
398 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
399
400 modeRecord->rdSize = 4;
401 modeRecord->rdFunction = META_SETMAPMODE;
402 modeRecord->rdParm[0] = MM_ANISOTROPIC;
403
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)
417 {
418 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
419 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
420 }
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);
436 */
437 return true;
438 }
439
440 #endif // wxUSE_METAFILE