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