]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
::GetWindowText() -> wxGetWindowText
[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;
ce3ed50d 75 if (!file.IsNull() && (file.Cmp("") == 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
7f555861
JS
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) ;
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;
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.
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;
200 GetTextExtentPoint(dc, (char *)(const char *) string, strlen((char *)(const char *) string), &sizeRect);
201 GetTextMetrics(dc, &tm);
202
203 ReleaseDC(NULL, dc);
204
7f555861
JS
205 *x = XDEV2LOGREL(sizeRect.cx);
206 *y = YDEV2LOGREL(sizeRect.cy);
207 if (descent) *descent = tm.tmDescent;
208 if (externalLeading) *externalLeading = tm.tmExternalLeading;
2bda0e17
KB
209}
210
06e43511 211wxMetafile *wxMetafileDC::Close(void)
2bda0e17
KB
212{
213 SelectOldObjects(m_hDC);
214 HANDLE mf = CloseMetaFile((HDC) m_hDC);
215 m_hDC = 0;
216 if (mf)
217 {
06e43511 218 wxMetafile *wx_mf = new wxMetafile;
2bda0e17
KB
219 wx_mf->SetHMETAFILE((WXHANDLE) mf);
220 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
221 return wx_mf;
222 }
223 return NULL;
224}
225
06e43511 226void wxMetafileDC::SetMapMode(int mode)
2bda0e17
KB
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 {
e3065973 240 case wxMM_TWIPS:
2bda0e17
KB
241 {
242 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
243 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
244 break;
245 }
e3065973 246 case wxMM_POINTS:
2bda0e17
KB
247 {
248 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
249 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
250 break;
251 }
e3065973 252 case wxMM_METRIC:
2bda0e17
KB
253 {
254 m_logicalScaleX = mm2pixelsX;
255 m_logicalScaleY = mm2pixelsY;
256 break;
257 }
e3065973 258 case wxMM_LOMETRIC:
2bda0e17
KB
259 {
260 m_logicalScaleX = (float)(mm2pixelsX/10.0);
261 m_logicalScaleY = (float)(mm2pixelsY/10.0);
262 break;
263 }
264 default:
e3065973 265 case wxMM_TEXT:
2bda0e17
KB
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__
277struct RECT32
278{
279 short left;
280 short top;
281 short right;
282 short bottom;
283};
284
285struct mfPLACEABLEHEADER {
286 DWORD key;
287 short hmf;
288 RECT32 bbox;
289 WORD inch;
290 DWORD reserved;
291 WORD checksum;
292};
293#else
294struct 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,
e3065973 307 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
2bda0e17
KB
308 *
309 */
310
06e43511 311bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
2bda0e17 312{
06e43511 313 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, FALSE);
2bda0e17
KB
314}
315
06e43511 316bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
2bda0e17
KB
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
47d67540 414#endif // wxUSE_METAFILE
2845ddc2 415