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