]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
Applied patch [ 1374215 ] Bug fix to wxSpinCtrl for Windows
[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
024026be
VZ
246void wxMetafileDC::DoGetSize(int *width, int *height) const
247{
248 wxCHECK_RET( m_refData, _T("invalid wxMetafileDC") );
249
250 if ( width )
251 *width = M_METAFILEDATA->m_width;
252 if ( height )
253 *height = M_METAFILEDATA->m_height;
254}
255
265b0c07 256wxMetafile *wxMetafileDC::Close()
2bda0e17 257{
265b0c07
VZ
258 SelectOldObjects(m_hDC);
259 HANDLE mf = CloseMetaFile((HDC) m_hDC);
260 m_hDC = 0;
261 if (mf)
262 {
263 wxMetafile *wx_mf = new wxMetafile;
264 wx_mf->SetHMETAFILE((WXHANDLE) mf);
265 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
266 return wx_mf;
267 }
268 return NULL;
2bda0e17
KB
269}
270
06e43511 271void wxMetafileDC::SetMapMode(int mode)
2bda0e17 272{
265b0c07 273 m_mappingMode = mode;
2bda0e17 274
265b0c07
VZ
275 // int pixel_width = 0;
276 // int pixel_height = 0;
277 // int mm_width = 0;
278 // int mm_height = 0;
2bda0e17 279
265b0c07
VZ
280 float mm2pixelsX = 10.0;
281 float mm2pixelsY = 10.0;
2bda0e17 282
265b0c07 283 switch (mode)
2bda0e17 284 {
265b0c07
VZ
285 case wxMM_TWIPS:
286 {
287 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
288 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
289 break;
290 }
291 case wxMM_POINTS:
292 {
293 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
294 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
295 break;
296 }
297 case wxMM_METRIC:
298 {
299 m_logicalScaleX = mm2pixelsX;
300 m_logicalScaleY = mm2pixelsY;
301 break;
302 }
303 case wxMM_LOMETRIC:
304 {
305 m_logicalScaleX = (float)(mm2pixelsX/10.0);
306 m_logicalScaleY = (float)(mm2pixelsY/10.0);
307 break;
308 }
309 default:
310 case wxMM_TEXT:
311 {
312 m_logicalScaleX = 1.0;
313 m_logicalScaleY = 1.0;
314 break;
315 }
2bda0e17 316 }
2bda0e17
KB
317}
318
265b0c07
VZ
319// ----------------------------------------------------------------------------
320// wxMakeMetafilePlaceable
321// ----------------------------------------------------------------------------
322
2bda0e17
KB
323#ifdef __WIN32__
324struct RECT32
325{
326 short left;
327 short top;
328 short right;
329 short bottom;
330};
331
332struct mfPLACEABLEHEADER {
265b0c07
VZ
333 DWORD key;
334 short hmf;
335 RECT32 bbox;
336 WORD inch;
337 DWORD reserved;
338 WORD checksum;
2bda0e17
KB
339};
340#else
341struct mfPLACEABLEHEADER {
265b0c07
VZ
342 DWORD key;
343 HANDLE hmf;
344 RECT bbox;
345 WORD inch;
346 DWORD reserved;
347 WORD checksum;
2bda0e17
KB
348};
349#endif
350
351/*
352 * Pass filename of existing non-placeable metafile, and bounding box.
353 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
e3065973 354 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
2bda0e17
KB
355 *
356 */
265b0c07 357
06e43511 358bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
2bda0e17 359{
598ddd96 360 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false);
2bda0e17
KB
361}
362
06e43511 363bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
2bda0e17 364{
265b0c07
VZ
365 // I'm not sure if this is the correct way of suggesting a scale
366 // to the client application, but it's the only way I can find.
367 int unitsPerInch = (int)(576/scale);
368
369 mfPLACEABLEHEADER header;
370 header.key = 0x9AC6CDD7L;
371 header.hmf = 0;
372 header.bbox.left = (int)(x1);
373 header.bbox.top = (int)(y1);
374 header.bbox.right = (int)(x2);
375 header.bbox.bottom = (int)(y2);
376 header.inch = unitsPerInch;
377 header.reserved = 0;
378
379 // Calculate checksum
380 WORD *p;
381 mfPLACEABLEHEADER *pMFHead = &header;
382 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
383 p < (WORD *)&pMFHead ->checksum; ++p)
384 pMFHead ->checksum ^= *p;
385
8534ba34 386 FILE *fd = wxFopen(filename.fn_str(), _T("rb"));
598ddd96 387 if (!fd) return false;
265b0c07
VZ
388
389 wxChar tempFileBuf[256];
390 wxGetTempFileName(wxT("mf"), tempFileBuf);
65eb5b0b 391 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
265b0c07 392 if (!fHandle)
598ddd96 393 return false;
265b0c07
VZ
394 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
395
396 // Calculate origin and extent
397 int originX = x1;
398 int originY = y1;
399 int extentX = x2 - x1;
400 int extentY = (y2 - y1);
401
402 // Read metafile header and write
403 METAHEADER metaHeader;
404 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
405
406 if (useOriginAndExtent)
407 metaHeader.mtSize += 15;
408 else
409 metaHeader.mtSize += 5;
410
411 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
412
413 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
414 char modeBuffer[8];
415 char originBuffer[10];
416 char extentBuffer[10];
417 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
418
419 METARECORD *originRecord = (METARECORD *)&originBuffer;
420 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
421
422 modeRecord->rdSize = 4;
423 modeRecord->rdFunction = META_SETMAPMODE;
424 modeRecord->rdParm[0] = MM_ANISOTROPIC;
425
426 originRecord->rdSize = 5;
427 originRecord->rdFunction = META_SETWINDOWORG;
428 originRecord->rdParm[0] = originY;
429 originRecord->rdParm[1] = originX;
430
431 extentRecord->rdSize = 5;
432 extentRecord->rdFunction = META_SETWINDOWEXT;
433 extentRecord->rdParm[0] = extentY;
434 extentRecord->rdParm[1] = extentX;
435
436 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
437
438 if (useOriginAndExtent)
2bda0e17 439 {
265b0c07
VZ
440 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
441 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
2bda0e17 442 }
265b0c07
VZ
443
444 int ch = -2;
445 while (ch != EOF)
446 {
447 ch = getc(fd);
448 if (ch != EOF)
449 {
450 putc(ch, fHandle);
451 }
452 }
453 fclose(fHandle);
454 fclose(fd);
455 wxRemoveFile(filename);
456 wxCopyFile(tempFileBuf, filename);
457 wxRemoveFile(tempFileBuf);
598ddd96 458 return true;
265b0c07
VZ
459}
460
47cb3382
GT
461
462#if wxUSE_DRAG_AND_DROP
463
265b0c07
VZ
464// ----------------------------------------------------------------------------
465// wxMetafileDataObject
466// ----------------------------------------------------------------------------
467
468size_t wxMetafileDataObject::GetDataSize() const
469{
470 return sizeof(METAFILEPICT);
471}
472
473bool wxMetafileDataObject::GetDataHere(void *buf) const
474{
475 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
d9317fd4
VZ
476 const wxMetafile& mf = GetMetafile();
477
598ddd96 478 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
d9317fd4
VZ
479
480 // doesn't seem to work with any other mapping mode...
481 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
265b0c07
VZ
482 mfpict->xExt = mf.GetWidth();
483 mfpict->yExt = mf.GetHeight();
265b0c07 484
d9317fd4
VZ
485 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
486 // what DC the picture will be rendered to, use the default display one
487 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
488
489 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
265b0c07 490
598ddd96 491 return true;
265b0c07
VZ
492}
493
494bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
495{
496 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
497
498 wxMetafile mf;
499 mf.SetWindowsMappingMode(mfpict->mm);
d9317fd4 500
f40dba93
GRG
501 LONG w = mfpict->xExt,
502 h = mfpict->yExt;
d9317fd4
VZ
503 if ( mfpict->mm == MM_ANISOTROPIC )
504 {
505 // in this case xExt and yExt contain suggested size in HIMETRIC units
506 // (0.01 mm) - transform this to something more reasonable (pixels)
507 HIMETRICToPixel(&w, &h);
508 }
509
510 mf.SetWidth(w);
511 mf.SetHeight(h);
265b0c07
VZ
512 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
513
598ddd96 514 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
265b0c07
VZ
515
516 SetMetafile(mf);
517
598ddd96 518 return true;
2bda0e17
KB
519}
520
47cb3382
GT
521#endif // wxUSE_DRAG_AND_DROP
522
47d67540 523#endif // wxUSE_METAFILE
2845ddc2 524