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