]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
Compilation fix for !wxUSE_OWNER_DRAWN.
[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 6// Created: 04/01/98
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
265b0c07
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
265b0c07 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
2bda0e17 26#ifndef WX_PRECOMP
265b0c07
VZ
27 #include "wx/utils.h"
28 #include "wx/app.h"
2bda0e17
KB
29#endif
30
31#include "wx/metafile.h"
a51e601e 32#include "wx/filename.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;
e65a6cc1 66 m_windowsMappingMode = MM_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
e65a6cc1 87 M_METAFILEDATA->m_windowsMappingMode = MM_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{
5c33522f 104 return new wxMetafileRefData(*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 {
9a83f860 140 wxLogLastError(wxT("PlayMetaFile"));
d9317fd4
VZ
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
6636ef8d 179 if ( wxFileExists(file) )
265b0c07 180 wxRemoveFile(file);
2bda0e17 181
6636ef8d 182 if ( file.empty() )
265b0c07 183 m_hDC = (WXHDC) CreateMetaFile(NULL);
6636ef8d
DS
184 else
185 m_hDC = (WXHDC) CreateMetaFile(file);
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);
017dc06b 207 m_hDC = (WXHDC) CreateMetaFile(file.empty() ? NULL : wxMSW_CONV_LPCTSTR(file));
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 214 // Actual Windows mapping mode, for future reference.
e65a6cc1 215 m_windowsMappingMode = MM_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 253{
9a83f860 254 wxCHECK_RET( m_refData, wxT("invalid wxMetafileDC") );
024026be
VZ
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
89efaf2b 277void wxMetafileDCImpl::SetMapMode(wxMappingMode 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
9a83f860 392 FILE *fd = wxFopen(filename.fn_str(), wxT("rb"));
598ddd96 393 if (!fd) return false;
265b0c07 394
a51e601e
FM
395 wxString tempFileBuf = wxFileName::CreateTempFileName(wxT("mf"));
396 if (tempFileBuf.empty())
397 return false;
398
9a83f860 399 FILE *fHandle = wxFopen(tempFileBuf.fn_str(), wxT("wb"));
265b0c07 400 if (!fHandle)
598ddd96 401 return false;
784d9056 402 fwrite((void *)&header, 1, sizeof(mfPLACEABLEHEADER), fHandle);
265b0c07
VZ
403
404 // Calculate origin and extent
405 int originX = x1;
406 int originY = y1;
407 int extentX = x2 - x1;
408 int extentY = (y2 - y1);
409
410 // Read metafile header and write
411 METAHEADER metaHeader;
784d9056 412 fread((void *)&metaHeader, 1, sizeof(metaHeader), fd);
265b0c07
VZ
413
414 if (useOriginAndExtent)
415 metaHeader.mtSize += 15;
416 else
417 metaHeader.mtSize += 5;
418
784d9056 419 fwrite((void *)&metaHeader, 1, sizeof(metaHeader), fHandle);
265b0c07
VZ
420
421 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
422 char modeBuffer[8];
423 char originBuffer[10];
424 char extentBuffer[10];
425 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
426
427 METARECORD *originRecord = (METARECORD *)&originBuffer;
428 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
429
430 modeRecord->rdSize = 4;
431 modeRecord->rdFunction = META_SETMAPMODE;
432 modeRecord->rdParm[0] = MM_ANISOTROPIC;
433
434 originRecord->rdSize = 5;
435 originRecord->rdFunction = META_SETWINDOWORG;
436 originRecord->rdParm[0] = originY;
437 originRecord->rdParm[1] = originX;
438
439 extentRecord->rdSize = 5;
440 extentRecord->rdFunction = META_SETWINDOWEXT;
441 extentRecord->rdParm[0] = extentY;
442 extentRecord->rdParm[1] = extentX;
443
784d9056 444 fwrite((void *)modeBuffer, 1, 8, fHandle);
265b0c07
VZ
445
446 if (useOriginAndExtent)
2bda0e17 447 {
784d9056
PC
448 fwrite((void *)originBuffer, 1, 10, fHandle);
449 fwrite((void *)extentBuffer, 1, 10, fHandle);
2bda0e17 450 }
265b0c07
VZ
451
452 int ch = -2;
453 while (ch != EOF)
454 {
455 ch = getc(fd);
456 if (ch != EOF)
457 {
458 putc(ch, fHandle);
459 }
460 }
461 fclose(fHandle);
462 fclose(fd);
463 wxRemoveFile(filename);
464 wxCopyFile(tempFileBuf, filename);
465 wxRemoveFile(tempFileBuf);
598ddd96 466 return true;
265b0c07
VZ
467}
468
47cb3382
GT
469
470#if wxUSE_DRAG_AND_DROP
471
265b0c07
VZ
472// ----------------------------------------------------------------------------
473// wxMetafileDataObject
474// ----------------------------------------------------------------------------
475
476size_t wxMetafileDataObject::GetDataSize() const
477{
478 return sizeof(METAFILEPICT);
479}
480
481bool wxMetafileDataObject::GetDataHere(void *buf) const
482{
483 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
d9317fd4
VZ
484 const wxMetafile& mf = GetMetafile();
485
9a83f860 486 wxCHECK_MSG( mf.GetHMETAFILE(), false, wxT("copying invalid metafile") );
d9317fd4
VZ
487
488 // doesn't seem to work with any other mapping mode...
489 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
265b0c07
VZ
490 mfpict->xExt = mf.GetWidth();
491 mfpict->yExt = mf.GetHeight();
265b0c07 492
d9317fd4
VZ
493 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
494 // what DC the picture will be rendered to, use the default display one
495 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
496
497 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
265b0c07 498
598ddd96 499 return true;
265b0c07
VZ
500}
501
502bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
503{
504 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
505
506 wxMetafile mf;
507 mf.SetWindowsMappingMode(mfpict->mm);
d9317fd4 508
f40dba93
GRG
509 LONG w = mfpict->xExt,
510 h = mfpict->yExt;
d9317fd4
VZ
511 if ( mfpict->mm == MM_ANISOTROPIC )
512 {
513 // in this case xExt and yExt contain suggested size in HIMETRIC units
514 // (0.01 mm) - transform this to something more reasonable (pixels)
515 HIMETRICToPixel(&w, &h);
516 }
517
518 mf.SetWidth(w);
519 mf.SetHeight(h);
265b0c07
VZ
520 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
521
9a83f860 522 wxCHECK_MSG( mfpict->hMF, false, wxT("pasting invalid metafile") );
265b0c07
VZ
523
524 SetMetafile(mf);
525
598ddd96 526 return true;
2bda0e17
KB
527}
528
47cb3382
GT
529#endif // wxUSE_DRAG_AND_DROP
530
47d67540 531#endif // wxUSE_METAFILE