]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/metafile.cpp
3 // Purpose: wxMetafileDC etc.
4 // Author: Julian Smart
5 // Modified by: VZ 07.01.00: implemented wxMetaFileDataObject
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/metafile.h"
34 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
36 #include "wx/clipbrd.h"
37 #include "wx/msw/private.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
47 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
59 * Currently, the only purpose for making a metafile is to put
60 * it on the clipboard.
63 wxMetafileRefData::wxMetafileRefData()
66 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
67 m_width
= m_height
= 0;
70 wxMetafileRefData::~wxMetafileRefData()
74 DeleteMetaFile((HMETAFILE
) m_metafile
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxMetafile::wxMetafile(const wxString
& file
)
85 m_refData
= new wxMetafileRefData
;
87 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
88 M_METAFILEDATA
->m_metafile
= 0;
90 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile(file
);
93 wxMetafile::~wxMetafile()
97 wxGDIRefData
*wxMetafile::CreateGDIRefData() const
99 return new wxMetafileRefData
;
102 wxGDIRefData
*wxMetafile::CloneGDIRefData(const wxGDIRefData
*data
) const
104 return new wxMetafileRefData(*wx_static_cast(const wxMetafileRefData
*, data
));
107 bool wxMetafile::SetClipboard(int width
, int height
)
115 bool alreadyOpen
= wxClipboardOpen();
119 if (!wxEmptyClipboard())
122 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
130 bool wxMetafile::Play(wxDC
*dc
)
135 if (dc
->GetHDC() && M_METAFILEDATA
->m_metafile
)
137 if ( !::PlayMetaFile(GetHdcOf(*dc
), (HMETAFILE
)
138 M_METAFILEDATA
->m_metafile
) )
140 wxLogLastError(_T("PlayMetaFile"));
147 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
150 m_refData
= new wxMetafileRefData
;
152 M_METAFILEDATA
->m_metafile
= mf
;
155 void wxMetafile::SetWindowsMappingMode(int mm
)
158 m_refData
= new wxMetafileRefData
;
160 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
163 // ----------------------------------------------------------------------------
164 // Metafile device context
165 // ----------------------------------------------------------------------------
167 // Original constructor that does not takes origin and extent. If you use this,
168 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
169 wxMetafileDCImpl::wxMetafileDCImpl(wxDC
*owner
, const wxString
& file
)
179 if (!file
.IsNull() && wxFileExists(file
))
182 if (!file
.IsNull() && (file
!= wxEmptyString
))
183 m_hDC
= (WXHDC
) CreateMetaFile(file
);
185 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
187 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
189 // Actual Windows mapping mode, for future reference.
190 m_windowsMappingMode
= wxMM_TEXT
;
192 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
195 // New constructor that takes origin and extent. If you use this, don't
196 // give origin/extent arguments to wxMakeMetafilePlaceable.
197 wxMetafileDCImpl::wxMetafileDCImpl(wxDC
*owner
, const wxString
& file
,
198 int xext
, int yext
, int xorg
, int yorg
)
205 if ( !file
.empty() && wxFileExists(file
) )
207 m_hDC
= (WXHDC
) CreateMetaFile(file
.empty() ? NULL
: file
.wx_str());
211 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
212 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
214 // Actual Windows mapping mode, for future reference.
215 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
217 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
220 wxMetafileDCImpl::~wxMetafileDCImpl()
225 void wxMetafileDCImpl::DoGetTextExtent(const wxString
& string
,
226 wxCoord
*x
, wxCoord
*y
,
227 wxCoord
*descent
, wxCoord
*externalLeading
,
228 const wxFont
*theFont
) const
230 const wxFont
*fontToUse
= theFont
;
235 SelectInHDC
selFont(dc
, GetHfontOf(*fontToUse
));
239 ::GetTextExtentPoint32(dc
, WXSTRINGCAST string
, wxStrlen(WXSTRINGCAST string
), &sizeRect
);
240 ::GetTextMetrics(dc
, &tm
);
247 *descent
= tm
.tmDescent
;
248 if ( externalLeading
)
249 *externalLeading
= tm
.tmExternalLeading
;
252 void wxMetafileDCImpl::DoGetSize(int *width
, int *height
) const
254 wxCHECK_RET( m_refData
, _T("invalid wxMetafileDC") );
257 *width
= M_METAFILEDATA
->m_width
;
259 *height
= M_METAFILEDATA
->m_height
;
262 wxMetafile
*wxMetafileDCImpl::Close()
264 SelectOldObjects(m_hDC
);
265 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
269 wxMetafile
*wx_mf
= new wxMetafile
;
270 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
271 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
277 void wxMetafileDCImpl::SetMapMode(int mode
)
279 m_mappingMode
= mode
;
281 // int pixel_width = 0;
282 // int pixel_height = 0;
284 // int mm_height = 0;
286 float mm2pixelsX
= 10.0;
287 float mm2pixelsY
= 10.0;
293 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
294 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
299 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
300 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
305 m_logicalScaleX
= mm2pixelsX
;
306 m_logicalScaleY
= mm2pixelsY
;
311 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
312 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
318 m_logicalScaleX
= 1.0;
319 m_logicalScaleY
= 1.0;
325 // ----------------------------------------------------------------------------
326 // wxMakeMetafilePlaceable
327 // ----------------------------------------------------------------------------
338 struct mfPLACEABLEHEADER
{
347 struct mfPLACEABLEHEADER
{
358 * Pass filename of existing non-placeable metafile, and bounding box.
359 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
360 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
364 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
366 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, false);
369 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
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
);
375 mfPLACEABLEHEADER header
;
376 header
.key
= 0x9AC6CDD7L
;
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
;
385 // Calculate checksum
387 mfPLACEABLEHEADER
*pMFHead
= &header
;
388 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
389 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
390 pMFHead
->checksum
^= *p
;
392 FILE *fd
= wxFopen(filename
.fn_str(), _T("rb"));
393 if (!fd
) return false;
395 wxChar tempFileBuf
[256];
396 wxGetTempFileName(wxT("mf"), tempFileBuf
);
397 FILE *fHandle
= wxFopen(wxFNCONV(tempFileBuf
), _T("wb"));
400 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
402 // Calculate origin and extent
405 int extentX
= x2
- x1
;
406 int extentY
= (y2
- y1
);
408 // Read metafile header and write
409 METAHEADER metaHeader
;
410 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
412 if (useOriginAndExtent
)
413 metaHeader
.mtSize
+= 15;
415 metaHeader
.mtSize
+= 5;
417 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
419 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
421 char originBuffer
[10];
422 char extentBuffer
[10];
423 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
425 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
426 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
428 modeRecord
->rdSize
= 4;
429 modeRecord
->rdFunction
= META_SETMAPMODE
;
430 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
432 originRecord
->rdSize
= 5;
433 originRecord
->rdFunction
= META_SETWINDOWORG
;
434 originRecord
->rdParm
[0] = originY
;
435 originRecord
->rdParm
[1] = originX
;
437 extentRecord
->rdSize
= 5;
438 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
439 extentRecord
->rdParm
[0] = extentY
;
440 extentRecord
->rdParm
[1] = extentX
;
442 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
444 if (useOriginAndExtent
)
446 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
447 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
461 wxRemoveFile(filename
);
462 wxCopyFile(tempFileBuf
, filename
);
463 wxRemoveFile(tempFileBuf
);
468 #if wxUSE_DRAG_AND_DROP
470 // ----------------------------------------------------------------------------
471 // wxMetafileDataObject
472 // ----------------------------------------------------------------------------
474 size_t wxMetafileDataObject::GetDataSize() const
476 return sizeof(METAFILEPICT
);
479 bool wxMetafileDataObject::GetDataHere(void *buf
) const
481 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
482 const wxMetafile
& mf
= GetMetafile();
484 wxCHECK_MSG( mf
.GetHMETAFILE(), false, _T("copying invalid metafile") );
486 // doesn't seem to work with any other mapping mode...
487 mfpict
->mm
= MM_ANISOTROPIC
; //mf.GetWindowsMappingMode();
488 mfpict
->xExt
= mf
.GetWidth();
489 mfpict
->yExt
= mf
.GetHeight();
491 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
492 // what DC the picture will be rendered to, use the default display one
493 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
495 mfpict
->hMF
= CopyMetaFile((HMETAFILE
)mf
.GetHMETAFILE(), NULL
);
500 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
502 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
505 mf
.SetWindowsMappingMode(mfpict
->mm
);
507 LONG w
= mfpict
->xExt
,
509 if ( mfpict
->mm
== MM_ANISOTROPIC
)
511 // in this case xExt and yExt contain suggested size in HIMETRIC units
512 // (0.01 mm) - transform this to something more reasonable (pixels)
513 HIMETRICToPixel(&w
, &h
);
518 mf
.SetHMETAFILE((WXHANDLE
)mfpict
->hMF
);
520 wxCHECK_MSG( mfpict
->hMF
, false, _T("pasting invalid metafile") );
527 #endif // wxUSE_DRAG_AND_DROP
529 #endif // wxUSE_METAFILE