]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
Compile fixes for the const patch
[wxWidgets.git] / src / msw / metafile.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
265b0c07 2// Name: msw/metafile.cpp
06e43511 3// Purpose: wxMetafileDC etc.
2bda0e17 4// Author: Julian Smart
265b0c07 5// Modified by: VZ 07.01.00: implemented wxMetaFileDataObject
2bda0e17
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
265b0c07
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
265b0c07 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
27#ifndef WX_PRECOMP
265b0c07 28 #include "wx/setup.h"
2bda0e17
KB
29#endif
30
2bda0e17 31#ifndef WX_PRECOMP
265b0c07
VZ
32 #include "wx/utils.h"
33 #include "wx/app.h"
2bda0e17
KB
34#endif
35
36#include "wx/metafile.h"
d9317fd4
VZ
37
38#if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
39
2bda0e17
KB
40#include "wx/clipbrd.h"
41#include "wx/msw/private.h"
42
43#include <stdio.h>
44#include <string.h>
45
265b0c07
VZ
46// ----------------------------------------------------------------------------
47// wxWin macros
48// ----------------------------------------------------------------------------
2bda0e17 49
06e43511
JS
50IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
51IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
2bda0e17 52
265b0c07
VZ
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ----------------------------------------------------------------------------
58// wxMetafileRefData
59// ----------------------------------------------------------------------------
60
2bda0e17 61/*
06e43511 62 * Metafiles
2bda0e17
KB
63 * Currently, the only purpose for making a metafile is to put
64 * it on the clipboard.
65 */
66
265b0c07 67wxMetafileRefData::wxMetafileRefData()
2bda0e17 68{
06e43511 69 m_metafile = 0;
e3065973 70 m_windowsMappingMode = wxMM_ANISOTROPIC;
265b0c07 71 m_width = m_height = 0;
2bda0e17
KB
72}
73
265b0c07 74wxMetafileRefData::~wxMetafileRefData()
2bda0e17 75{
06e43511
JS
76 if (m_metafile)
77 {
78 DeleteMetaFile((HMETAFILE) m_metafile);
79 m_metafile = 0;
80 }
2bda0e17
KB
81}
82
265b0c07
VZ
83// ----------------------------------------------------------------------------
84// wxMetafile
85// ----------------------------------------------------------------------------
86
06e43511 87wxMetafile::wxMetafile(const wxString& file)
2bda0e17 88{
06e43511
JS
89 m_refData = new wxMetafileRefData;
90
e3065973 91 M_METAFILEDATA->m_windowsMappingMode = wxMM_ANISOTROPIC;
06e43511 92 M_METAFILEDATA->m_metafile = 0;
63407846 93 if (!file.empty())
06e43511
JS
94 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
95}
96
265b0c07 97wxMetafile::~wxMetafile()
06e43511
JS
98{
99}
100
101bool wxMetafile::SetClipboard(int width, int height)
102{
f6bcfd97 103#if !wxUSE_CLIPBOARD
598ddd96 104 return false;
f6bcfd97 105#else
06e43511 106 if (!m_refData)
598ddd96 107 return false;
06e43511 108
265b0c07 109 bool alreadyOpen = wxClipboardOpen();
06e43511
JS
110 if (!alreadyOpen)
111 {
112 wxOpenClipboard();
265b0c07 113 if (!wxEmptyClipboard())
598ddd96 114 return false;
06e43511
JS
115 }
116 bool success = wxSetClipboardData(wxDF_METAFILE, this, width,height);
265b0c07
VZ
117 if (!alreadyOpen)
118 wxCloseClipboard();
119
120 return success;
f6bcfd97 121#endif
06e43511
JS
122}
123
124bool wxMetafile::Play(wxDC *dc)
125{
126 if (!m_refData)
598ddd96 127 return false;
06e43511
JS
128
129 dc->BeginDrawing();
130
131 if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
d9317fd4
VZ
132 {
133 if ( !::PlayMetaFile(GetHdcOf(*dc), (HMETAFILE)
134 M_METAFILEDATA->m_metafile) )
135 {
136 wxLogLastError(_T("PlayMetaFile"));
137 }
138 }
06e43511
JS
139
140 dc->EndDrawing();
141
598ddd96 142 return true;
2bda0e17
KB
143}
144
06e43511 145void wxMetafile::SetHMETAFILE(WXHANDLE mf)
2bda0e17 146{
265b0c07 147 if (!m_refData)
06e43511 148 m_refData = new wxMetafileRefData;
2bda0e17 149
06e43511
JS
150 M_METAFILEDATA->m_metafile = mf;
151}
2bda0e17 152
06e43511
JS
153void wxMetafile::SetWindowsMappingMode(int mm)
154{
265b0c07 155 if (!m_refData)
06e43511 156 m_refData = new wxMetafileRefData;
2bda0e17 157
06e43511 158 M_METAFILEDATA->m_windowsMappingMode = mm;
2bda0e17
KB
159}
160
265b0c07
VZ
161// ----------------------------------------------------------------------------
162// Metafile device context
163// ----------------------------------------------------------------------------
2bda0e17
KB
164
165// Original constructor that does not takes origin and extent. If you use this,
06e43511
JS
166// *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
167wxMetafileDC::wxMetafileDC(const wxString& file)
2bda0e17 168{
265b0c07
VZ
169 m_metaFile = NULL;
170 m_minX = 10000;
171 m_minY = 10000;
172 m_maxX = -10000;
173 m_maxY = -10000;
174 // m_title = NULL;
2bda0e17 175
265b0c07
VZ
176 if (!file.IsNull() && wxFileExists(file))
177 wxRemoveFile(file);
2bda0e17 178
fda7962d 179 if (!file.IsNull() && (file != wxEmptyString))
265b0c07
VZ
180 m_hDC = (WXHDC) CreateMetaFile(file);
181 else
182 m_hDC = (WXHDC) CreateMetaFile(NULL);
7f555861 183
265b0c07 184 m_ok = (m_hDC != (WXHDC) 0) ;
2bda0e17 185
265b0c07
VZ
186 // Actual Windows mapping mode, for future reference.
187 m_windowsMappingMode = wxMM_TEXT;
e3065973 188
265b0c07 189 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
190}
191
192// New constructor that takes origin and extent. If you use this, don't
06e43511
JS
193// give origin/extent arguments to wxMakeMetafilePlaceable.
194wxMetafileDC::wxMetafileDC(const wxString& file, int xext, int yext, int xorg, int yorg)
2bda0e17 195{
265b0c07
VZ
196 m_minX = 10000;
197 m_minY = 10000;
198 m_maxX = -10000;
199 m_maxY = -10000;
598ddd96 200 if ( !file.IsEmpty() && wxFileExists(file))
265b0c07
VZ
201 wxRemoveFile(file);
202 m_hDC = (WXHDC) CreateMetaFile(file);
2bda0e17 203
598ddd96 204 m_ok = true;
2bda0e17 205
265b0c07
VZ
206 ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
207 ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
2bda0e17 208
265b0c07
VZ
209 // Actual Windows mapping mode, for future reference.
210 m_windowsMappingMode = wxMM_ANISOTROPIC;
e3065973 211
265b0c07 212 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
213}
214
265b0c07 215wxMetafileDC::~wxMetafileDC()
2bda0e17 216{
265b0c07 217 m_hDC = 0;
2bda0e17
KB
218}
219
06e43511 220void wxMetafileDC::GetTextExtent(const wxString& string, long *x, long *y,
65eb5b0b 221 long *descent, long *externalLeading, wxFont *theFont, bool WXUNUSED(use16bit)) const
2bda0e17 222{
265b0c07
VZ
223 wxFont *fontToUse = theFont;
224 if (!fontToUse)
225 fontToUse = (wxFont*) &m_font;
226
227 HDC dc = GetDC(NULL);
228
229 SIZE sizeRect;
230 TEXTMETRIC tm;
767b35a5 231 ::GetTextExtentPoint32(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
265b0c07
VZ
232 GetTextMetrics(dc, &tm);
233
234 ReleaseDC(NULL, dc);
235
236 if ( x )
237 *x = sizeRect.cx;
238 if ( y )
239 *y = sizeRect.cy;
240 if ( descent )
241 *descent = tm.tmDescent;
242 if ( externalLeading )
243 *externalLeading = tm.tmExternalLeading;
2bda0e17
KB
244}
245
265b0c07 246wxMetafile *wxMetafileDC::Close()
2bda0e17 247{
265b0c07
VZ
248 SelectOldObjects(m_hDC);
249 HANDLE mf = CloseMetaFile((HDC) m_hDC);
250 m_hDC = 0;
251 if (mf)
252 {
253 wxMetafile *wx_mf = new wxMetafile;
254 wx_mf->SetHMETAFILE((WXHANDLE) mf);
255 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
256 return wx_mf;
257 }
258 return NULL;
2bda0e17
KB
259}
260
06e43511 261void wxMetafileDC::SetMapMode(int mode)
2bda0e17 262{
265b0c07 263 m_mappingMode = mode;
2bda0e17 264
265b0c07
VZ
265 // int pixel_width = 0;
266 // int pixel_height = 0;
267 // int mm_width = 0;
268 // int mm_height = 0;
2bda0e17 269
265b0c07
VZ
270 float mm2pixelsX = 10.0;
271 float mm2pixelsY = 10.0;
2bda0e17 272
265b0c07 273 switch (mode)
2bda0e17 274 {
265b0c07
VZ
275 case wxMM_TWIPS:
276 {
277 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
278 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
279 break;
280 }
281 case wxMM_POINTS:
282 {
283 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
284 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
285 break;
286 }
287 case wxMM_METRIC:
288 {
289 m_logicalScaleX = mm2pixelsX;
290 m_logicalScaleY = mm2pixelsY;
291 break;
292 }
293 case wxMM_LOMETRIC:
294 {
295 m_logicalScaleX = (float)(mm2pixelsX/10.0);
296 m_logicalScaleY = (float)(mm2pixelsY/10.0);
297 break;
298 }
299 default:
300 case wxMM_TEXT:
301 {
302 m_logicalScaleX = 1.0;
303 m_logicalScaleY = 1.0;
304 break;
305 }
2bda0e17 306 }
2bda0e17
KB
307}
308
265b0c07
VZ
309// ----------------------------------------------------------------------------
310// wxMakeMetafilePlaceable
311// ----------------------------------------------------------------------------
312
2bda0e17
KB
313#ifdef __WIN32__
314struct RECT32
315{
316 short left;
317 short top;
318 short right;
319 short bottom;
320};
321
322struct mfPLACEABLEHEADER {
265b0c07
VZ
323 DWORD key;
324 short hmf;
325 RECT32 bbox;
326 WORD inch;
327 DWORD reserved;
328 WORD checksum;
2bda0e17
KB
329};
330#else
331struct mfPLACEABLEHEADER {
265b0c07
VZ
332 DWORD key;
333 HANDLE hmf;
334 RECT bbox;
335 WORD inch;
336 DWORD reserved;
337 WORD checksum;
2bda0e17
KB
338};
339#endif
340
341/*
342 * Pass filename of existing non-placeable metafile, and bounding box.
343 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
e3065973 344 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
2bda0e17
KB
345 *
346 */
265b0c07 347
06e43511 348bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
2bda0e17 349{
598ddd96 350 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false);
2bda0e17
KB
351}
352
06e43511 353bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
2bda0e17 354{
265b0c07
VZ
355 // I'm not sure if this is the correct way of suggesting a scale
356 // to the client application, but it's the only way I can find.
357 int unitsPerInch = (int)(576/scale);
358
359 mfPLACEABLEHEADER header;
360 header.key = 0x9AC6CDD7L;
361 header.hmf = 0;
362 header.bbox.left = (int)(x1);
363 header.bbox.top = (int)(y1);
364 header.bbox.right = (int)(x2);
365 header.bbox.bottom = (int)(y2);
366 header.inch = unitsPerInch;
367 header.reserved = 0;
368
369 // Calculate checksum
370 WORD *p;
371 mfPLACEABLEHEADER *pMFHead = &header;
372 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
373 p < (WORD *)&pMFHead ->checksum; ++p)
374 pMFHead ->checksum ^= *p;
375
8534ba34 376 FILE *fd = wxFopen(filename.fn_str(), _T("rb"));
598ddd96 377 if (!fd) return false;
265b0c07
VZ
378
379 wxChar tempFileBuf[256];
380 wxGetTempFileName(wxT("mf"), tempFileBuf);
65eb5b0b 381 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
265b0c07 382 if (!fHandle)
598ddd96 383 return false;
265b0c07
VZ
384 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
385
386 // Calculate origin and extent
387 int originX = x1;
388 int originY = y1;
389 int extentX = x2 - x1;
390 int extentY = (y2 - y1);
391
392 // Read metafile header and write
393 METAHEADER metaHeader;
394 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
395
396 if (useOriginAndExtent)
397 metaHeader.mtSize += 15;
398 else
399 metaHeader.mtSize += 5;
400
401 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
402
403 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
404 char modeBuffer[8];
405 char originBuffer[10];
406 char extentBuffer[10];
407 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
408
409 METARECORD *originRecord = (METARECORD *)&originBuffer;
410 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
411
412 modeRecord->rdSize = 4;
413 modeRecord->rdFunction = META_SETMAPMODE;
414 modeRecord->rdParm[0] = MM_ANISOTROPIC;
415
416 originRecord->rdSize = 5;
417 originRecord->rdFunction = META_SETWINDOWORG;
418 originRecord->rdParm[0] = originY;
419 originRecord->rdParm[1] = originX;
420
421 extentRecord->rdSize = 5;
422 extentRecord->rdFunction = META_SETWINDOWEXT;
423 extentRecord->rdParm[0] = extentY;
424 extentRecord->rdParm[1] = extentX;
425
426 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
427
428 if (useOriginAndExtent)
2bda0e17 429 {
265b0c07
VZ
430 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
431 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
2bda0e17 432 }
265b0c07
VZ
433
434 int ch = -2;
435 while (ch != EOF)
436 {
437 ch = getc(fd);
438 if (ch != EOF)
439 {
440 putc(ch, fHandle);
441 }
442 }
443 fclose(fHandle);
444 fclose(fd);
445 wxRemoveFile(filename);
446 wxCopyFile(tempFileBuf, filename);
447 wxRemoveFile(tempFileBuf);
598ddd96 448 return true;
265b0c07
VZ
449}
450
47cb3382
GT
451
452#if wxUSE_DRAG_AND_DROP
453
265b0c07
VZ
454// ----------------------------------------------------------------------------
455// wxMetafileDataObject
456// ----------------------------------------------------------------------------
457
458size_t wxMetafileDataObject::GetDataSize() const
459{
460 return sizeof(METAFILEPICT);
461}
462
463bool wxMetafileDataObject::GetDataHere(void *buf) const
464{
465 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
d9317fd4
VZ
466 const wxMetafile& mf = GetMetafile();
467
598ddd96 468 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
d9317fd4
VZ
469
470 // doesn't seem to work with any other mapping mode...
471 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
265b0c07
VZ
472 mfpict->xExt = mf.GetWidth();
473 mfpict->yExt = mf.GetHeight();
265b0c07 474
d9317fd4
VZ
475 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
476 // what DC the picture will be rendered to, use the default display one
477 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
478
479 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
265b0c07 480
598ddd96 481 return true;
265b0c07
VZ
482}
483
484bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
485{
486 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
487
488 wxMetafile mf;
489 mf.SetWindowsMappingMode(mfpict->mm);
d9317fd4 490
f40dba93
GRG
491 LONG w = mfpict->xExt,
492 h = mfpict->yExt;
d9317fd4
VZ
493 if ( mfpict->mm == MM_ANISOTROPIC )
494 {
495 // in this case xExt and yExt contain suggested size in HIMETRIC units
496 // (0.01 mm) - transform this to something more reasonable (pixels)
497 HIMETRICToPixel(&w, &h);
498 }
499
500 mf.SetWidth(w);
501 mf.SetHeight(h);
265b0c07
VZ
502 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
503
598ddd96 504 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
265b0c07
VZ
505
506 SetMetafile(mf);
507
598ddd96 508 return true;
2bda0e17
KB
509}
510
47cb3382
GT
511#endif // wxUSE_DRAG_AND_DROP
512
47d67540 513#endif // wxUSE_METAFILE
2845ddc2 514