]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
1. DoSetSize() simplified, DoGetBestSize() introduced
[wxWidgets.git] / src / msw / metafile.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: metafile.cpp
06e43511 3// Purpose: wxMetafileDC etc.
2bda0e17
KB
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
47d67540 27#if wxUSE_METAFILE
2bda0e17
KB
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
06e43511
JS
44IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
45IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
2bda0e17
KB
46#endif
47
48/*
06e43511 49 * Metafiles
2bda0e17
KB
50 * Currently, the only purpose for making a metafile is to put
51 * it on the clipboard.
52 */
53
06e43511 54wxMetafileRefData::wxMetafileRefData(void)
2bda0e17 55{
06e43511 56 m_metafile = 0;
e3065973 57 m_windowsMappingMode = wxMM_ANISOTROPIC;
2bda0e17
KB
58}
59
06e43511 60wxMetafileRefData::~wxMetafileRefData(void)
2bda0e17 61{
06e43511
JS
62 if (m_metafile)
63 {
64 DeleteMetaFile((HMETAFILE) m_metafile);
65 m_metafile = 0;
66 }
2bda0e17
KB
67}
68
06e43511 69wxMetafile::wxMetafile(const wxString& file)
2bda0e17 70{
06e43511
JS
71 m_refData = new wxMetafileRefData;
72
e3065973 73 M_METAFILEDATA->m_windowsMappingMode = wxMM_ANISOTROPIC;
06e43511 74 M_METAFILEDATA->m_metafile = 0;
837e5743 75 if (!file.IsNull() && (file.Cmp(_T("")) == 0))
06e43511
JS
76 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
77}
78
79wxMetafile::~wxMetafile(void)
80{
81}
82
83bool 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
99bool 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;
2bda0e17
KB
112}
113
06e43511 114void wxMetafile::SetHMETAFILE(WXHANDLE mf)
2bda0e17 115{
06e43511
JS
116 if (m_refData)
117 m_refData = new wxMetafileRefData;
2bda0e17 118
06e43511
JS
119 M_METAFILEDATA->m_metafile = mf;
120}
2bda0e17 121
06e43511
JS
122void wxMetafile::SetWindowsMappingMode(int mm)
123{
124 if (m_refData)
125 m_refData = new wxMetafileRefData;
2bda0e17 126
06e43511 127 M_METAFILEDATA->m_windowsMappingMode = mm;
2bda0e17
KB
128}
129
130/*
131 * Metafile device context
132 *
133 */
134
135// Original constructor that does not takes origin and extent. If you use this,
06e43511
JS
136// *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
137wxMetafileDC::wxMetafileDC(const wxString& file)
2bda0e17
KB
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);
2bda0e17 148
837e5743 149 if (!file.IsNull() && (file != _T("")))
7f555861
JS
150 m_hDC = (WXHDC) CreateMetaFile(file);
151 else
152 m_hDC = (WXHDC) CreateMetaFile(NULL);
153
154 m_ok = (m_hDC != (WXHDC) 0) ;
2bda0e17
KB
155
156 // Actual Windows mapping mode, for future reference.
e3065973
JS
157 m_windowsMappingMode = wxMM_TEXT;
158
159 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
160}
161
162// New constructor that takes origin and extent. If you use this, don't
06e43511
JS
163// give origin/extent arguments to wxMakeMetafilePlaceable.
164wxMetafileDC::wxMetafileDC(const wxString& file, int xext, int yext, int xorg, int yorg)
2bda0e17
KB
165{
166 m_minX = 10000;
167 m_minY = 10000;
168 m_maxX = -10000;
169 m_maxY = -10000;
837e5743 170 if (file != _T("") && wxFileExists(file)) wxRemoveFile(file);
2bda0e17
KB
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.
e3065973
JS
179 m_windowsMappingMode = wxMM_ANISOTROPIC;
180
181 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
182}
183
06e43511 184wxMetafileDC::~wxMetafileDC(void)
2bda0e17
KB
185{
186 m_hDC = 0;
187}
188
06e43511 189void wxMetafileDC::GetTextExtent(const wxString& string, long *x, long *y,
7f555861 190 long *descent, long *externalLeading, wxFont *theFont, bool use16bit) const
2bda0e17
KB
191{
192 wxFont *fontToUse = theFont;
193 if (!fontToUse)
7f555861 194 fontToUse = (wxFont*) &m_font;
2bda0e17
KB
195
196 HDC dc = GetDC(NULL);
197
198 SIZE sizeRect;
199 TEXTMETRIC tm;
837e5743 200 GetTextExtentPoint(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
2bda0e17
KB
201 GetTextMetrics(dc, &tm);
202
203 ReleaseDC(NULL, dc);
204
42e69d6b
VZ
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;
2bda0e17
KB
213}
214
06e43511 215wxMetafile *wxMetafileDC::Close(void)
2bda0e17
KB
216{
217 SelectOldObjects(m_hDC);
218 HANDLE mf = CloseMetaFile((HDC) m_hDC);
219 m_hDC = 0;
220 if (mf)
221 {
06e43511 222 wxMetafile *wx_mf = new wxMetafile;
2bda0e17
KB
223 wx_mf->SetHMETAFILE((WXHANDLE) mf);
224 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
225 return wx_mf;
226 }
227 return NULL;
228}
229
06e43511 230void wxMetafileDC::SetMapMode(int mode)
2bda0e17
KB
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 {
e3065973 244 case wxMM_TWIPS:
2bda0e17
KB
245 {
246 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
247 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
248 break;
249 }
e3065973 250 case wxMM_POINTS:
2bda0e17
KB
251 {
252 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
253 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
254 break;
255 }
e3065973 256 case wxMM_METRIC:
2bda0e17
KB
257 {
258 m_logicalScaleX = mm2pixelsX;
259 m_logicalScaleY = mm2pixelsY;
260 break;
261 }
e3065973 262 case wxMM_LOMETRIC:
2bda0e17
KB
263 {
264 m_logicalScaleX = (float)(mm2pixelsX/10.0);
265 m_logicalScaleY = (float)(mm2pixelsY/10.0);
266 break;
267 }
268 default:
e3065973 269 case wxMM_TEXT:
2bda0e17
KB
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__
281struct RECT32
282{
283 short left;
284 short top;
285 short right;
286 short bottom;
287};
288
289struct mfPLACEABLEHEADER {
290 DWORD key;
291 short hmf;
292 RECT32 bbox;
293 WORD inch;
294 DWORD reserved;
295 WORD checksum;
296};
297#else
298struct 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,
e3065973 311 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
2bda0e17
KB
312 *
313 */
314
06e43511 315bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
2bda0e17 316{
06e43511 317 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
2bda0e17
KB
318}
319
06e43511 320bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
2bda0e17
KB
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
837e5743 343 FILE *fd = fopen(filename.mb_str(wxConvFile), "rb");
2bda0e17
KB
344 if (!fd) return FALSE;
345
837e5743
OK
346 wxChar tempFileBuf[256];
347 wxGetTempFileName(_T("mf"), tempFileBuf);
348 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
2bda0e17
KB
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
47d67540 418#endif // wxUSE_METAFILE
2845ddc2 419