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