]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
Applied patch from Neil Robinson to allow manipulation of unattached menubars
[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
JS
124
125 dc->BeginDrawing();
126
127 if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
d9317fd4
VZ
128 {
129 if ( !::PlayMetaFile(GetHdcOf(*dc), (HMETAFILE)
130 M_METAFILEDATA->m_metafile) )
131 {
132 wxLogLastError(_T("PlayMetaFile"));
133 }
134 }
06e43511
JS
135
136 dc->EndDrawing();
137
598ddd96 138 return true;
2bda0e17
KB
139}
140
06e43511 141void wxMetafile::SetHMETAFILE(WXHANDLE mf)
2bda0e17 142{
265b0c07 143 if (!m_refData)
06e43511 144 m_refData = new wxMetafileRefData;
2bda0e17 145
06e43511
JS
146 M_METAFILEDATA->m_metafile = mf;
147}
2bda0e17 148
06e43511
JS
149void wxMetafile::SetWindowsMappingMode(int mm)
150{
265b0c07 151 if (!m_refData)
06e43511 152 m_refData = new wxMetafileRefData;
2bda0e17 153
06e43511 154 M_METAFILEDATA->m_windowsMappingMode = mm;
2bda0e17
KB
155}
156
265b0c07
VZ
157// ----------------------------------------------------------------------------
158// Metafile device context
159// ----------------------------------------------------------------------------
2bda0e17
KB
160
161// Original constructor that does not takes origin and extent. If you use this,
06e43511
JS
162// *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
163wxMetafileDC::wxMetafileDC(const wxString& file)
2bda0e17 164{
265b0c07
VZ
165 m_metaFile = NULL;
166 m_minX = 10000;
167 m_minY = 10000;
168 m_maxX = -10000;
169 m_maxY = -10000;
170 // m_title = NULL;
2bda0e17 171
265b0c07
VZ
172 if (!file.IsNull() && wxFileExists(file))
173 wxRemoveFile(file);
2bda0e17 174
fda7962d 175 if (!file.IsNull() && (file != wxEmptyString))
265b0c07
VZ
176 m_hDC = (WXHDC) CreateMetaFile(file);
177 else
178 m_hDC = (WXHDC) CreateMetaFile(NULL);
7f555861 179
265b0c07 180 m_ok = (m_hDC != (WXHDC) 0) ;
2bda0e17 181
265b0c07
VZ
182 // Actual Windows mapping mode, for future reference.
183 m_windowsMappingMode = wxMM_TEXT;
e3065973 184
265b0c07 185 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
186}
187
188// New constructor that takes origin and extent. If you use this, don't
06e43511
JS
189// give origin/extent arguments to wxMakeMetafilePlaceable.
190wxMetafileDC::wxMetafileDC(const wxString& file, int xext, int yext, int xorg, int yorg)
2bda0e17 191{
265b0c07
VZ
192 m_minX = 10000;
193 m_minY = 10000;
194 m_maxX = -10000;
195 m_maxY = -10000;
521bf4ff 196 if ( !file.empty() && wxFileExists(file))
265b0c07
VZ
197 wxRemoveFile(file);
198 m_hDC = (WXHDC) CreateMetaFile(file);
2bda0e17 199
598ddd96 200 m_ok = true;
2bda0e17 201
265b0c07
VZ
202 ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
203 ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
2bda0e17 204
265b0c07
VZ
205 // Actual Windows mapping mode, for future reference.
206 m_windowsMappingMode = wxMM_ANISOTROPIC;
e3065973 207
265b0c07 208 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
209}
210
265b0c07 211wxMetafileDC::~wxMetafileDC()
2bda0e17 212{
265b0c07 213 m_hDC = 0;
2bda0e17
KB
214}
215
06e43511 216void wxMetafileDC::GetTextExtent(const wxString& string, long *x, long *y,
65eb5b0b 217 long *descent, long *externalLeading, wxFont *theFont, bool WXUNUSED(use16bit)) const
2bda0e17 218{
265b0c07
VZ
219 wxFont *fontToUse = theFont;
220 if (!fontToUse)
221 fontToUse = (wxFont*) &m_font;
222
223 HDC dc = GetDC(NULL);
224
225 SIZE sizeRect;
226 TEXTMETRIC tm;
767b35a5 227 ::GetTextExtentPoint32(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
265b0c07
VZ
228 GetTextMetrics(dc, &tm);
229
230 ReleaseDC(NULL, dc);
231
232 if ( x )
233 *x = sizeRect.cx;
234 if ( y )
235 *y = sizeRect.cy;
236 if ( descent )
237 *descent = tm.tmDescent;
238 if ( externalLeading )
239 *externalLeading = tm.tmExternalLeading;
2bda0e17
KB
240}
241
024026be
VZ
242void wxMetafileDC::DoGetSize(int *width, int *height) const
243{
244 wxCHECK_RET( m_refData, _T("invalid wxMetafileDC") );
245
246 if ( width )
247 *width = M_METAFILEDATA->m_width;
248 if ( height )
249 *height = M_METAFILEDATA->m_height;
250}
251
265b0c07 252wxMetafile *wxMetafileDC::Close()
2bda0e17 253{
265b0c07
VZ
254 SelectOldObjects(m_hDC);
255 HANDLE mf = CloseMetaFile((HDC) m_hDC);
256 m_hDC = 0;
257 if (mf)
258 {
259 wxMetafile *wx_mf = new wxMetafile;
260 wx_mf->SetHMETAFILE((WXHANDLE) mf);
261 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
262 return wx_mf;
263 }
264 return NULL;
2bda0e17
KB
265}
266
06e43511 267void wxMetafileDC::SetMapMode(int mode)
2bda0e17 268{
265b0c07 269 m_mappingMode = mode;
2bda0e17 270
265b0c07
VZ
271 // int pixel_width = 0;
272 // int pixel_height = 0;
273 // int mm_width = 0;
274 // int mm_height = 0;
2bda0e17 275
265b0c07
VZ
276 float mm2pixelsX = 10.0;
277 float mm2pixelsY = 10.0;
2bda0e17 278
265b0c07 279 switch (mode)
2bda0e17 280 {
265b0c07
VZ
281 case wxMM_TWIPS:
282 {
283 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
284 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
285 break;
286 }
287 case wxMM_POINTS:
288 {
289 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
290 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
291 break;
292 }
293 case wxMM_METRIC:
294 {
295 m_logicalScaleX = mm2pixelsX;
296 m_logicalScaleY = mm2pixelsY;
297 break;
298 }
299 case wxMM_LOMETRIC:
300 {
301 m_logicalScaleX = (float)(mm2pixelsX/10.0);
302 m_logicalScaleY = (float)(mm2pixelsY/10.0);
303 break;
304 }
305 default:
306 case wxMM_TEXT:
307 {
308 m_logicalScaleX = 1.0;
309 m_logicalScaleY = 1.0;
310 break;
311 }
2bda0e17 312 }
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{
598ddd96 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
8534ba34 382 FILE *fd = wxFopen(filename.fn_str(), _T("rb"));
598ddd96 383 if (!fd) return false;
265b0c07
VZ
384
385 wxChar tempFileBuf[256];
386 wxGetTempFileName(wxT("mf"), tempFileBuf);
65eb5b0b 387 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
265b0c07 388 if (!fHandle)
598ddd96 389 return false;
265b0c07
VZ
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);
598ddd96 454 return true;
265b0c07
VZ
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
598ddd96 474 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
d9317fd4
VZ
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 486
598ddd96 487 return true;
265b0c07
VZ
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
598ddd96 510 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
265b0c07
VZ
511
512 SetMetafile(mf);
513
598ddd96 514 return true;
2bda0e17
KB
515}
516
47cb3382
GT
517#endif // wxUSE_DRAG_AND_DROP
518
47d67540 519#endif // wxUSE_METAFILE