]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
36 #include "wx/metafile.h"
38 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
40 #include "wx/clipbrd.h"
41 #include "wx/msw/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
51 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
63 * Currently, the only purpose for making a metafile is to put
64 * it on the clipboard.
67 wxMetafileRefData::wxMetafileRefData()
70 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
71 m_width
= m_height
= 0;
74 wxMetafileRefData::~wxMetafileRefData()
78 DeleteMetaFile((HMETAFILE
) m_metafile
);
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 wxMetafile::wxMetafile(const wxString
& file
)
89 m_refData
= new wxMetafileRefData
;
91 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
92 M_METAFILEDATA
->m_metafile
= 0;
94 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile(file
);
97 wxMetafile::~wxMetafile()
101 bool wxMetafile::SetClipboard(int width
, int height
)
109 bool alreadyOpen
= wxClipboardOpen();
113 if (!wxEmptyClipboard())
116 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
124 bool wxMetafile::Play(wxDC
*dc
)
131 if (dc
->GetHDC() && M_METAFILEDATA
->m_metafile
)
133 if ( !::PlayMetaFile(GetHdcOf(*dc
), (HMETAFILE
)
134 M_METAFILEDATA
->m_metafile
) )
136 wxLogLastError(_T("PlayMetaFile"));
145 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
148 m_refData
= new wxMetafileRefData
;
150 M_METAFILEDATA
->m_metafile
= mf
;
153 void wxMetafile::SetWindowsMappingMode(int mm
)
156 m_refData
= new wxMetafileRefData
;
158 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
161 // ----------------------------------------------------------------------------
162 // Metafile device context
163 // ----------------------------------------------------------------------------
165 // Original constructor that does not takes origin and extent. If you use this,
166 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
167 wxMetafileDC::wxMetafileDC(const wxString
& file
)
176 if (!file
.IsNull() && wxFileExists(file
))
179 if (!file
.IsNull() && (file
!= wxEmptyString
))
180 m_hDC
= (WXHDC
) CreateMetaFile(file
);
182 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
184 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
186 // Actual Windows mapping mode, for future reference.
187 m_windowsMappingMode
= wxMM_TEXT
;
189 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
192 // New constructor that takes origin and extent. If you use this, don't
193 // give origin/extent arguments to wxMakeMetafilePlaceable.
194 wxMetafileDC::wxMetafileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
200 if ( !file
.IsEmpty() && wxFileExists(file
))
202 m_hDC
= (WXHDC
) CreateMetaFile(file
);
206 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
207 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
209 // Actual Windows mapping mode, for future reference.
210 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
212 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
215 wxMetafileDC::~wxMetafileDC()
220 void wxMetafileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
221 long *descent
, long *externalLeading
, wxFont
*theFont
, bool WXUNUSED(use16bit
)) const
223 wxFont
*fontToUse
= theFont
;
225 fontToUse
= (wxFont
*) &m_font
;
227 HDC dc
= GetDC(NULL
);
231 ::GetTextExtentPoint32(dc
, WXSTRINGCAST string
, wxStrlen(WXSTRINGCAST string
), &sizeRect
);
232 GetTextMetrics(dc
, &tm
);
241 *descent
= tm
.tmDescent
;
242 if ( externalLeading
)
243 *externalLeading
= tm
.tmExternalLeading
;
246 wxMetafile
*wxMetafileDC::Close()
248 SelectOldObjects(m_hDC
);
249 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
253 wxMetafile
*wx_mf
= new wxMetafile
;
254 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
255 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
261 void wxMetafileDC::SetMapMode(int mode
)
263 m_mappingMode
= mode
;
265 // int pixel_width = 0;
266 // int pixel_height = 0;
268 // int mm_height = 0;
270 float mm2pixelsX
= 10.0;
271 float mm2pixelsY
= 10.0;
277 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
278 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
283 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
284 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
289 m_logicalScaleX
= mm2pixelsX
;
290 m_logicalScaleY
= mm2pixelsY
;
295 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
296 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
302 m_logicalScaleX
= 1.0;
303 m_logicalScaleY
= 1.0;
309 // ----------------------------------------------------------------------------
310 // wxMakeMetafilePlaceable
311 // ----------------------------------------------------------------------------
322 struct mfPLACEABLEHEADER
{
331 struct mfPLACEABLEHEADER
{
342 * Pass filename of existing non-placeable metafile, and bounding box.
343 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
344 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
348 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
350 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, false);
353 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
355 // I'm not sure if this is the correct way of suggesting a scale
356 // to the client application, but it's the only way I can find.
357 int unitsPerInch
= (int)(576/scale
);
359 mfPLACEABLEHEADER header
;
360 header
.key
= 0x9AC6CDD7L
;
362 header
.bbox
.left
= (int)(x1
);
363 header
.bbox
.top
= (int)(y1
);
364 header
.bbox
.right
= (int)(x2
);
365 header
.bbox
.bottom
= (int)(y2
);
366 header
.inch
= unitsPerInch
;
369 // Calculate checksum
371 mfPLACEABLEHEADER
*pMFHead
= &header
;
372 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
373 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
374 pMFHead
->checksum
^= *p
;
376 FILE *fd
= wxFopen(filename
.fn_str(), _T("rb"));
377 if (!fd
) return false;
379 wxChar tempFileBuf
[256];
380 wxGetTempFileName(wxT("mf"), tempFileBuf
);
381 FILE *fHandle
= wxFopen(wxFNCONV(tempFileBuf
), _T("wb"));
384 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
386 // Calculate origin and extent
389 int extentX
= x2
- x1
;
390 int extentY
= (y2
- y1
);
392 // Read metafile header and write
393 METAHEADER metaHeader
;
394 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
396 if (useOriginAndExtent
)
397 metaHeader
.mtSize
+= 15;
399 metaHeader
.mtSize
+= 5;
401 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
403 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
405 char originBuffer
[10];
406 char extentBuffer
[10];
407 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
409 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
410 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
412 modeRecord
->rdSize
= 4;
413 modeRecord
->rdFunction
= META_SETMAPMODE
;
414 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
416 originRecord
->rdSize
= 5;
417 originRecord
->rdFunction
= META_SETWINDOWORG
;
418 originRecord
->rdParm
[0] = originY
;
419 originRecord
->rdParm
[1] = originX
;
421 extentRecord
->rdSize
= 5;
422 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
423 extentRecord
->rdParm
[0] = extentY
;
424 extentRecord
->rdParm
[1] = extentX
;
426 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
428 if (useOriginAndExtent
)
430 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
431 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
445 wxRemoveFile(filename
);
446 wxCopyFile(tempFileBuf
, filename
);
447 wxRemoveFile(tempFileBuf
);
452 #if wxUSE_DRAG_AND_DROP
454 // ----------------------------------------------------------------------------
455 // wxMetafileDataObject
456 // ----------------------------------------------------------------------------
458 size_t wxMetafileDataObject::GetDataSize() const
460 return sizeof(METAFILEPICT
);
463 bool wxMetafileDataObject::GetDataHere(void *buf
) const
465 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
466 const wxMetafile
& mf
= GetMetafile();
468 wxCHECK_MSG( mf
.GetHMETAFILE(), false, _T("copying invalid metafile") );
470 // doesn't seem to work with any other mapping mode...
471 mfpict
->mm
= MM_ANISOTROPIC
; //mf.GetWindowsMappingMode();
472 mfpict
->xExt
= mf
.GetWidth();
473 mfpict
->yExt
= mf
.GetHeight();
475 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
476 // what DC the picture will be rendered to, use the default display one
477 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
479 mfpict
->hMF
= CopyMetaFile((HMETAFILE
)mf
.GetHMETAFILE(), NULL
);
484 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
486 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
489 mf
.SetWindowsMappingMode(mfpict
->mm
);
491 LONG w
= mfpict
->xExt
,
493 if ( mfpict
->mm
== MM_ANISOTROPIC
)
495 // in this case xExt and yExt contain suggested size in HIMETRIC units
496 // (0.01 mm) - transform this to something more reasonable (pixels)
497 HIMETRICToPixel(&w
, &h
);
502 mf
.SetHMETAFILE((WXHANDLE
)mfpict
->hMF
);
504 wxCHECK_MSG( mfpict
->hMF
, false, _T("pasting invalid metafile") );
511 #endif // wxUSE_DRAG_AND_DROP
513 #endif // wxUSE_METAFILE