]> git.saurik.com Git - wxWidgets.git/blob - src/os2/metafile.cpp
non-PCH build fix (according to Tinderbox).
[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 // if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
97 // PlayMetaFile((HDC) dc->GetHDC(), (HMETAFILE) M_METAFILEDATA->m_metafile);
98
99 return true;
100 }
101
102 void wxMetafile::SetHMETAFILE(WXHANDLE mf)
103 {
104 if (m_refData)
105 m_refData = new wxMetafileRefData;
106
107 M_METAFILEDATA->m_metafile = mf;
108 }
109
110 void wxMetafile::SetWindowsMappingMode(int mm)
111 {
112 if (m_refData)
113 m_refData = new wxMetafileRefData;
114
115 M_METAFILEDATA->m_windowsMappingMode = mm;
116 }
117
118 /*
119 * Metafile device context
120 *
121 */
122
123 // Original constructor that does not takes origin and extent. If you use this,
124 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
125 wxMetafileDC::wxMetafileDC(const wxString& file)
126 {
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 /*
139 if (!file.empty())
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)
151 }
152
153 // New constructor that takes origin and extent. If you use this, don't
154 // give origin/extent arguments to wxMakeMetafilePlaceable.
155 wxMetafileDC::wxMetafileDC( const wxString& file,
156 int WXUNUSED(xext),
157 int WXUNUSED(yext),
158 int WXUNUSED(xorg),
159 int WXUNUSED(yorg) )
160 {
161 m_minX = 10000;
162 m_minY = 10000;
163 m_maxX = -10000;
164 m_maxY = -10000;
165 if (!file.empty() && wxFileExists(file))
166 wxRemoveFile(file);
167
168 // m_hDC = (WXHDC) CreateMetaFile(file);
169
170 m_ok = true;
171
172 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
173 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
174
175 // Actual Windows mapping mode, for future reference.
176 m_windowsMappingMode = wxMM_ANISOTROPIC;
177
178 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
179 }
180
181 wxMetafileDC::~wxMetafileDC(void)
182 {
183 m_hDC = 0;
184 }
185
186 void wxMetafileDC::GetTextExtent(const wxString& WXUNUSED(string),
187 long *WXUNUSED(x),
188 long *WXUNUSED(y),
189 long *WXUNUSED(descent),
190 long *WXUNUSED(externalLeading),
191 wxFont *theFont,
192 bool WXUNUSED(use16bit) ) const
193 {
194 wxFont *fontToUse = theFont;
195 if (!fontToUse)
196 fontToUse = (wxFont*) &m_font;
197
198 // TODO:
199 /*
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;
217 */
218 }
219
220 wxMetafile *wxMetafileDC::Close(void)
221 {
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;
233 }
234
235 void wxMetafileDC::SetMapMode(int mode)
236 {
237 m_mappingMode = mode;
238
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 }
281 m_nWindowExtX = 100;
282 m_nWindowExtY = 100;
283 }
284
285 #ifdef __WIN32__
286 struct RECT32
287 {
288 short left;
289 short top;
290 short right;
291 short bottom;
292 };
293
294 struct mfPLACEABLEHEADER {
295 DWORD key;
296 short hmf;
297 RECT32 bbox;
298 WORD inch;
299 DWORD reserved;
300 WORD checksum;
301 };
302 #else
303 struct mfPLACEABLEHEADER {
304 DWORD key;
305 HANDLE hmf;
306 RECT bbox;
307 WORD inch;
308 DWORD reserved;
309 WORD checksum;
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 */
319
320 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
321 {
322 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
323 }
324
325 bool 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))
332 {
333 // TODO: the OS/2 PM/MM way to do this
334 /*
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)
357 return false;
358
359 wxChar tempFileBuf[256];
360 wxGetTempFileName(wxT("mf"), tempFileBuf);
361 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
362 if (!fHandle)
363 return false;
364 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
365
366 // Calculate origin and extent
367 int originX = x1;
368 int originY = y1;
369 int extentX = x2 - x1;
370 int extentY = (y2 - y1);
371
372 // Read metafile header and write
373 METAHEADER metaHeader;
374 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
375
376 if (useOriginAndExtent)
377 metaHeader.mtSize += 15;
378 else
379 metaHeader.mtSize += 5;
380
381 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
382
383 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
384 char modeBuffer[8];
385 char originBuffer[10];
386 char extentBuffer[10];
387 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
388
389 METARECORD *originRecord = (METARECORD *)&originBuffer;
390 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
391
392 modeRecord->rdSize = 4;
393 modeRecord->rdFunction = META_SETMAPMODE;
394 modeRecord->rdParm[0] = MM_ANISOTROPIC;
395
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)
409 {
410 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
411 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
412 }
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);
428 */
429 return true;
430 }
431
432 #endif // wxUSE_METAFILE