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