]> git.saurik.com Git - wxWidgets.git/blame - src/msw/metafile.cpp
Added wxDataViewListIndexModel::RowsDeleted() and various related corrections
[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
8f884a0d
VZ
97wxGDIRefData *wxMetafile::CreateGDIRefData() const
98{
99 return new wxMetafileRefData;
100}
101
102wxGDIRefData *wxMetafile::CloneGDIRefData(const wxGDIRefData *data) const
103{
104 return new wxMetafileRefData(wx_static_cast(wxMetafileRefData *, data));
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 {
140 wxLogLastError(_T("PlayMetaFile"));
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
JS
168// *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
169wxMetafileDC::wxMetafileDC(const wxString& file)
2bda0e17 170{
265b0c07
VZ
171 m_metaFile = NULL;
172 m_minX = 10000;
173 m_minY = 10000;
174 m_maxX = -10000;
175 m_maxY = -10000;
176 // m_title = NULL;
2bda0e17 177
265b0c07
VZ
178 if (!file.IsNull() && wxFileExists(file))
179 wxRemoveFile(file);
2bda0e17 180
fda7962d 181 if (!file.IsNull() && (file != wxEmptyString))
265b0c07
VZ
182 m_hDC = (WXHDC) CreateMetaFile(file);
183 else
184 m_hDC = (WXHDC) CreateMetaFile(NULL);
7f555861 185
265b0c07 186 m_ok = (m_hDC != (WXHDC) 0) ;
2bda0e17 187
265b0c07
VZ
188 // Actual Windows mapping mode, for future reference.
189 m_windowsMappingMode = wxMM_TEXT;
e3065973 190
265b0c07 191 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
192}
193
194// New constructor that takes origin and extent. If you use this, don't
06e43511
JS
195// give origin/extent arguments to wxMakeMetafilePlaceable.
196wxMetafileDC::wxMetafileDC(const wxString& file, int xext, int yext, int xorg, int yorg)
2bda0e17 197{
265b0c07
VZ
198 m_minX = 10000;
199 m_minY = 10000;
200 m_maxX = -10000;
201 m_maxY = -10000;
800991b6 202 if ( !file.empty() && wxFileExists(file) )
265b0c07 203 wxRemoveFile(file);
802e382f 204 m_hDC = (WXHDC) CreateMetaFile(file.empty() ? NULL : file.wx_str());
2bda0e17 205
598ddd96 206 m_ok = true;
2bda0e17 207
265b0c07
VZ
208 ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
209 ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
2bda0e17 210
265b0c07
VZ
211 // Actual Windows mapping mode, for future reference.
212 m_windowsMappingMode = wxMM_ANISOTROPIC;
e3065973 213
265b0c07 214 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
2bda0e17
KB
215}
216
265b0c07 217wxMetafileDC::~wxMetafileDC()
2bda0e17 218{
265b0c07 219 m_hDC = 0;
2bda0e17
KB
220}
221
d1ae2638
VZ
222void wxMetafileDC::DoGetTextExtent(const wxString& string,
223 wxCoord *x, wxCoord *y,
224 wxCoord *descent, wxCoord *externalLeading,
225 const wxFont *theFont) const
2bda0e17 226{
c94f845b 227 const wxFont *fontToUse = theFont;
265b0c07 228 if (!fontToUse)
c94f845b 229 fontToUse = &m_font;
265b0c07 230
666e33ab
VZ
231 ScreenHDC dc;
232 SelectInHDC selFont(dc, GetHfontOf(*fontToUse));
265b0c07
VZ
233
234 SIZE sizeRect;
235 TEXTMETRIC tm;
767b35a5 236 ::GetTextExtentPoint32(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
666e33ab 237 ::GetTextMetrics(dc, &tm);
265b0c07
VZ
238
239 if ( x )
240 *x = sizeRect.cx;
241 if ( y )
242 *y = sizeRect.cy;
243 if ( descent )
244 *descent = tm.tmDescent;
245 if ( externalLeading )
246 *externalLeading = tm.tmExternalLeading;
2bda0e17
KB
247}
248
024026be
VZ
249void wxMetafileDC::DoGetSize(int *width, int *height) const
250{
251 wxCHECK_RET( m_refData, _T("invalid wxMetafileDC") );
252
253 if ( width )
254 *width = M_METAFILEDATA->m_width;
255 if ( height )
256 *height = M_METAFILEDATA->m_height;
257}
258
265b0c07 259wxMetafile *wxMetafileDC::Close()
2bda0e17 260{
265b0c07
VZ
261 SelectOldObjects(m_hDC);
262 HANDLE mf = CloseMetaFile((HDC) m_hDC);
263 m_hDC = 0;
264 if (mf)
265 {
266 wxMetafile *wx_mf = new wxMetafile;
267 wx_mf->SetHMETAFILE((WXHANDLE) mf);
268 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
269 return wx_mf;
270 }
271 return NULL;
2bda0e17
KB
272}
273
06e43511 274void wxMetafileDC::SetMapMode(int mode)
2bda0e17 275{
265b0c07 276 m_mappingMode = mode;
2bda0e17 277
265b0c07
VZ
278 // int pixel_width = 0;
279 // int pixel_height = 0;
280 // int mm_width = 0;
281 // int mm_height = 0;
2bda0e17 282
265b0c07
VZ
283 float mm2pixelsX = 10.0;
284 float mm2pixelsY = 10.0;
2bda0e17 285
265b0c07 286 switch (mode)
2bda0e17 287 {
265b0c07
VZ
288 case wxMM_TWIPS:
289 {
290 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
291 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
292 break;
293 }
294 case wxMM_POINTS:
295 {
296 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
297 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
298 break;
299 }
300 case wxMM_METRIC:
301 {
302 m_logicalScaleX = mm2pixelsX;
303 m_logicalScaleY = mm2pixelsY;
304 break;
305 }
306 case wxMM_LOMETRIC:
307 {
308 m_logicalScaleX = (float)(mm2pixelsX/10.0);
309 m_logicalScaleY = (float)(mm2pixelsY/10.0);
310 break;
311 }
312 default:
313 case wxMM_TEXT:
314 {
315 m_logicalScaleX = 1.0;
316 m_logicalScaleY = 1.0;
317 break;
318 }
2bda0e17 319 }
2bda0e17
KB
320}
321
265b0c07
VZ
322// ----------------------------------------------------------------------------
323// wxMakeMetafilePlaceable
324// ----------------------------------------------------------------------------
325
2bda0e17
KB
326#ifdef __WIN32__
327struct RECT32
328{
329 short left;
330 short top;
331 short right;
332 short bottom;
333};
334
335struct mfPLACEABLEHEADER {
265b0c07
VZ
336 DWORD key;
337 short hmf;
338 RECT32 bbox;
339 WORD inch;
340 DWORD reserved;
341 WORD checksum;
2bda0e17
KB
342};
343#else
344struct mfPLACEABLEHEADER {
265b0c07
VZ
345 DWORD key;
346 HANDLE hmf;
347 RECT bbox;
348 WORD inch;
349 DWORD reserved;
350 WORD checksum;
2bda0e17
KB
351};
352#endif
353
354/*
355 * Pass filename of existing non-placeable metafile, and bounding box.
356 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
e3065973 357 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
2bda0e17
KB
358 *
359 */
265b0c07 360
06e43511 361bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
2bda0e17 362{
598ddd96 363 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false);
2bda0e17
KB
364}
365
06e43511 366bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
2bda0e17 367{
265b0c07
VZ
368 // I'm not sure if this is the correct way of suggesting a scale
369 // to the client application, but it's the only way I can find.
370 int unitsPerInch = (int)(576/scale);
371
372 mfPLACEABLEHEADER header;
373 header.key = 0x9AC6CDD7L;
374 header.hmf = 0;
375 header.bbox.left = (int)(x1);
376 header.bbox.top = (int)(y1);
377 header.bbox.right = (int)(x2);
378 header.bbox.bottom = (int)(y2);
379 header.inch = unitsPerInch;
380 header.reserved = 0;
381
382 // Calculate checksum
383 WORD *p;
384 mfPLACEABLEHEADER *pMFHead = &header;
385 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
386 p < (WORD *)&pMFHead ->checksum; ++p)
387 pMFHead ->checksum ^= *p;
388
8534ba34 389 FILE *fd = wxFopen(filename.fn_str(), _T("rb"));
598ddd96 390 if (!fd) return false;
265b0c07
VZ
391
392 wxChar tempFileBuf[256];
393 wxGetTempFileName(wxT("mf"), tempFileBuf);
65eb5b0b 394 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
265b0c07 395 if (!fHandle)
598ddd96 396 return false;
265b0c07
VZ
397 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
398
399 // Calculate origin and extent
400 int originX = x1;
401 int originY = y1;
402 int extentX = x2 - x1;
403 int extentY = (y2 - y1);
404
405 // Read metafile header and write
406 METAHEADER metaHeader;
407 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
408
409 if (useOriginAndExtent)
410 metaHeader.mtSize += 15;
411 else
412 metaHeader.mtSize += 5;
413
414 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
415
416 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
417 char modeBuffer[8];
418 char originBuffer[10];
419 char extentBuffer[10];
420 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
421
422 METARECORD *originRecord = (METARECORD *)&originBuffer;
423 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
424
425 modeRecord->rdSize = 4;
426 modeRecord->rdFunction = META_SETMAPMODE;
427 modeRecord->rdParm[0] = MM_ANISOTROPIC;
428
429 originRecord->rdSize = 5;
430 originRecord->rdFunction = META_SETWINDOWORG;
431 originRecord->rdParm[0] = originY;
432 originRecord->rdParm[1] = originX;
433
434 extentRecord->rdSize = 5;
435 extentRecord->rdFunction = META_SETWINDOWEXT;
436 extentRecord->rdParm[0] = extentY;
437 extentRecord->rdParm[1] = extentX;
438
439 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
440
441 if (useOriginAndExtent)
2bda0e17 442 {
265b0c07
VZ
443 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
444 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
2bda0e17 445 }
265b0c07
VZ
446
447 int ch = -2;
448 while (ch != EOF)
449 {
450 ch = getc(fd);
451 if (ch != EOF)
452 {
453 putc(ch, fHandle);
454 }
455 }
456 fclose(fHandle);
457 fclose(fd);
458 wxRemoveFile(filename);
459 wxCopyFile(tempFileBuf, filename);
460 wxRemoveFile(tempFileBuf);
598ddd96 461 return true;
265b0c07
VZ
462}
463
47cb3382
GT
464
465#if wxUSE_DRAG_AND_DROP
466
265b0c07
VZ
467// ----------------------------------------------------------------------------
468// wxMetafileDataObject
469// ----------------------------------------------------------------------------
470
471size_t wxMetafileDataObject::GetDataSize() const
472{
473 return sizeof(METAFILEPICT);
474}
475
476bool wxMetafileDataObject::GetDataHere(void *buf) const
477{
478 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
d9317fd4
VZ
479 const wxMetafile& mf = GetMetafile();
480
598ddd96 481 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
d9317fd4
VZ
482
483 // doesn't seem to work with any other mapping mode...
484 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
265b0c07
VZ
485 mfpict->xExt = mf.GetWidth();
486 mfpict->yExt = mf.GetHeight();
265b0c07 487
d9317fd4
VZ
488 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
489 // what DC the picture will be rendered to, use the default display one
490 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
491
492 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
265b0c07 493
598ddd96 494 return true;
265b0c07
VZ
495}
496
497bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
498{
499 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
500
501 wxMetafile mf;
502 mf.SetWindowsMappingMode(mfpict->mm);
d9317fd4 503
f40dba93
GRG
504 LONG w = mfpict->xExt,
505 h = mfpict->yExt;
d9317fd4
VZ
506 if ( mfpict->mm == MM_ANISOTROPIC )
507 {
508 // in this case xExt and yExt contain suggested size in HIMETRIC units
509 // (0.01 mm) - transform this to something more reasonable (pixels)
510 HIMETRICToPixel(&w, &h);
511 }
512
513 mf.SetWidth(w);
514 mf.SetHeight(h);
265b0c07
VZ
515 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
516
598ddd96 517 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
265b0c07
VZ
518
519 SetMetafile(mf);
520
598ddd96 521 return true;
2bda0e17
KB
522}
523
47cb3382
GT
524#endif // wxUSE_DRAG_AND_DROP
525
47d67540 526#endif // wxUSE_METAFILE