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