]> git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
VC++ 1.5 makefile fixes (lib split, C objs)
[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 = wxMM_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 = wxMM_ANISOTROPIC;
74 M_METAFILEDATA->m_metafile = 0;
75 if (!file.IsNull() && (file.Cmp(wxT("")) == 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 != wxT("")))
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 = wxMM_TEXT;
158
159 SetMapMode(wxMM_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 != wxT("") && 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 = wxMM_ANISOTROPIC;
180
181 SetMapMode(wxMM_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, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
201 GetTextMetrics(dc, &tm);
202
203 ReleaseDC(NULL, dc);
204
205 if ( x )
206 *x = sizeRect.cx;
207 if ( y )
208 *y = sizeRect.cy;
209 if ( descent )
210 *descent = tm.tmDescent;
211 if ( externalLeading )
212 *externalLeading = tm.tmExternalLeading;
213 }
214
215 wxMetafile *wxMetafileDC::Close(void)
216 {
217 SelectOldObjects(m_hDC);
218 HANDLE mf = CloseMetaFile((HDC) m_hDC);
219 m_hDC = 0;
220 if (mf)
221 {
222 wxMetafile *wx_mf = new wxMetafile;
223 wx_mf->SetHMETAFILE((WXHANDLE) mf);
224 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
225 return wx_mf;
226 }
227 return NULL;
228 }
229
230 void wxMetafileDC::SetMapMode(int mode)
231 {
232 m_mappingMode = mode;
233
234 // int pixel_width = 0;
235 // int pixel_height = 0;
236 // int mm_width = 0;
237 // int mm_height = 0;
238
239 float mm2pixelsX = 10.0;
240 float mm2pixelsY = 10.0;
241
242 switch (mode)
243 {
244 case wxMM_TWIPS:
245 {
246 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
247 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
248 break;
249 }
250 case wxMM_POINTS:
251 {
252 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
253 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
254 break;
255 }
256 case wxMM_METRIC:
257 {
258 m_logicalScaleX = mm2pixelsX;
259 m_logicalScaleY = mm2pixelsY;
260 break;
261 }
262 case wxMM_LOMETRIC:
263 {
264 m_logicalScaleX = (float)(mm2pixelsX/10.0);
265 m_logicalScaleY = (float)(mm2pixelsY/10.0);
266 break;
267 }
268 default:
269 case wxMM_TEXT:
270 {
271 m_logicalScaleX = 1.0;
272 m_logicalScaleY = 1.0;
273 break;
274 }
275 }
276 m_windowExtX = 100;
277 m_windowExtY = 100;
278 }
279
280 #ifdef __WIN32__
281 struct RECT32
282 {
283 short left;
284 short top;
285 short right;
286 short bottom;
287 };
288
289 struct mfPLACEABLEHEADER {
290 DWORD key;
291 short hmf;
292 RECT32 bbox;
293 WORD inch;
294 DWORD reserved;
295 WORD checksum;
296 };
297 #else
298 struct mfPLACEABLEHEADER {
299 DWORD key;
300 HANDLE hmf;
301 RECT bbox;
302 WORD inch;
303 DWORD reserved;
304 WORD checksum;
305 };
306 #endif
307
308 /*
309 * Pass filename of existing non-placeable metafile, and bounding box.
310 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
311 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
312 *
313 */
314
315 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
316 {
317 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
318 }
319
320 bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
321 {
322 // I'm not sure if this is the correct way of suggesting a scale
323 // to the client application, but it's the only way I can find.
324 int unitsPerInch = (int)(576/scale);
325
326 mfPLACEABLEHEADER header;
327 header.key = 0x9AC6CDD7L;
328 header.hmf = 0;
329 header.bbox.left = (int)(x1);
330 header.bbox.top = (int)(y1);
331 header.bbox.right = (int)(x2);
332 header.bbox.bottom = (int)(y2);
333 header.inch = unitsPerInch;
334 header.reserved = 0;
335
336 // Calculate checksum
337 WORD *p;
338 mfPLACEABLEHEADER *pMFHead = &header;
339 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
340 p < (WORD *)&pMFHead ->checksum; ++p)
341 pMFHead ->checksum ^= *p;
342
343 FILE *fd = fopen(filename.fn_str(), "rb");
344 if (!fd) return FALSE;
345
346 wxChar tempFileBuf[256];
347 wxGetTempFileName(wxT("mf"), tempFileBuf);
348 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
349 if (!fHandle)
350 return FALSE;
351 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
352
353 // Calculate origin and extent
354 int originX = x1;
355 int originY = y1;
356 int extentX = x2 - x1;
357 int extentY = (y2 - y1);
358
359 // Read metafile header and write
360 METAHEADER metaHeader;
361 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
362
363 if (useOriginAndExtent)
364 metaHeader.mtSize += 15;
365 else
366 metaHeader.mtSize += 5;
367
368 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
369
370 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
371 char modeBuffer[8];
372 char originBuffer[10];
373 char extentBuffer[10];
374 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
375
376 METARECORD *originRecord = (METARECORD *)&originBuffer;
377 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
378
379 modeRecord->rdSize = 4;
380 modeRecord->rdFunction = META_SETMAPMODE;
381 modeRecord->rdParm[0] = MM_ANISOTROPIC;
382
383 originRecord->rdSize = 5;
384 originRecord->rdFunction = META_SETWINDOWORG;
385 originRecord->rdParm[0] = originY;
386 originRecord->rdParm[1] = originX;
387
388 extentRecord->rdSize = 5;
389 extentRecord->rdFunction = META_SETWINDOWEXT;
390 extentRecord->rdParm[0] = extentY;
391 extentRecord->rdParm[1] = extentX;
392
393 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
394
395 if (useOriginAndExtent)
396 {
397 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
398 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
399 }
400
401 int ch = -2;
402 while (ch != EOF)
403 {
404 ch = getc(fd);
405 if (ch != EOF)
406 {
407 putc(ch, fHandle);
408 }
409 }
410 fclose(fHandle);
411 fclose(fd);
412 wxRemoveFile(filename);
413 wxCopyFile(tempFileBuf, filename);
414 wxRemoveFile(tempFileBuf);
415 return TRUE;
416 }
417
418 #endif // wxUSE_METAFILE
419