]>
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(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 wxMetafileDC::wxMetafileDC(const wxString
& file
)
178 if (!file
.IsNull() && wxFileExists(file
))
181 if (!file
.IsNull() && (file
!= wxEmptyString
))
182 m_hDC
= (WXHDC
) CreateMetaFile(file
);
184 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
186 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
188 // Actual Windows mapping mode, for future reference.
189 m_windowsMappingMode
= wxMM_TEXT
;
191 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
194 // New constructor that takes origin and extent. If you use this, don't
195 // give origin/extent arguments to wxMakeMetafilePlaceable.
196 wxMetafileDC::wxMetafileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
202 if ( !file
.empty() && wxFileExists(file
) )
204 m_hDC
= (WXHDC
) CreateMetaFile(file
.empty() ? NULL
: file
.wx_str());
208 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
209 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
211 // Actual Windows mapping mode, for future reference.
212 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
214 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
217 wxMetafileDC::~wxMetafileDC()
222 void wxMetafileDC::DoGetTextExtent(const wxString
& string
,
223 wxCoord
*x
, wxCoord
*y
,
224 wxCoord
*descent
, wxCoord
*externalLeading
,
225 const wxFont
*theFont
) const
227 const wxFont
*fontToUse
= theFont
;
232 SelectInHDC
selFont(dc
, GetHfontOf(*fontToUse
));
236 ::GetTextExtentPoint32(dc
, WXSTRINGCAST string
, wxStrlen(WXSTRINGCAST string
), &sizeRect
);
237 ::GetTextMetrics(dc
, &tm
);
244 *descent
= tm
.tmDescent
;
245 if ( externalLeading
)
246 *externalLeading
= tm
.tmExternalLeading
;
249 void wxMetafileDC::DoGetSize(int *width
, int *height
) const
251 wxCHECK_RET( m_refData
, _T("invalid wxMetafileDC") );
254 *width
= M_METAFILEDATA
->m_width
;
256 *height
= M_METAFILEDATA
->m_height
;
259 wxMetafile
*wxMetafileDC::Close()
261 SelectOldObjects(m_hDC
);
262 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
266 wxMetafile
*wx_mf
= new wxMetafile
;
267 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
268 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
274 void wxMetafileDC::SetMapMode(int mode
)
276 m_mappingMode
= mode
;
278 // int pixel_width = 0;
279 // int pixel_height = 0;
281 // int mm_height = 0;
283 float mm2pixelsX
= 10.0;
284 float mm2pixelsY
= 10.0;
290 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
291 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
296 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
297 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
302 m_logicalScaleX
= mm2pixelsX
;
303 m_logicalScaleY
= mm2pixelsY
;
308 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
309 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
315 m_logicalScaleX
= 1.0;
316 m_logicalScaleY
= 1.0;
322 // ----------------------------------------------------------------------------
323 // wxMakeMetafilePlaceable
324 // ----------------------------------------------------------------------------
335 struct mfPLACEABLEHEADER
{
344 struct mfPLACEABLEHEADER
{
355 * Pass filename of existing non-placeable metafile, and bounding box.
356 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
357 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
361 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
363 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, false);
366 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
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
);
372 mfPLACEABLEHEADER header
;
373 header
.key
= 0x9AC6CDD7L
;
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
;
382 // Calculate checksum
384 mfPLACEABLEHEADER
*pMFHead
= &header
;
385 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
386 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
387 pMFHead
->checksum
^= *p
;
389 FILE *fd
= wxFopen(filename
.fn_str(), _T("rb"));
390 if (!fd
) return false;
392 wxChar tempFileBuf
[256];
393 wxGetTempFileName(wxT("mf"), tempFileBuf
);
394 FILE *fHandle
= wxFopen(wxFNCONV(tempFileBuf
), _T("wb"));
397 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
399 // Calculate origin and extent
402 int extentX
= x2
- x1
;
403 int extentY
= (y2
- y1
);
405 // Read metafile header and write
406 METAHEADER metaHeader
;
407 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
409 if (useOriginAndExtent
)
410 metaHeader
.mtSize
+= 15;
412 metaHeader
.mtSize
+= 5;
414 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
416 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
418 char originBuffer
[10];
419 char extentBuffer
[10];
420 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
422 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
423 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
425 modeRecord
->rdSize
= 4;
426 modeRecord
->rdFunction
= META_SETMAPMODE
;
427 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
429 originRecord
->rdSize
= 5;
430 originRecord
->rdFunction
= META_SETWINDOWORG
;
431 originRecord
->rdParm
[0] = originY
;
432 originRecord
->rdParm
[1] = originX
;
434 extentRecord
->rdSize
= 5;
435 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
436 extentRecord
->rdParm
[0] = extentY
;
437 extentRecord
->rdParm
[1] = extentX
;
439 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
441 if (useOriginAndExtent
)
443 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
444 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
458 wxRemoveFile(filename
);
459 wxCopyFile(tempFileBuf
, filename
);
460 wxRemoveFile(tempFileBuf
);
465 #if wxUSE_DRAG_AND_DROP
467 // ----------------------------------------------------------------------------
468 // wxMetafileDataObject
469 // ----------------------------------------------------------------------------
471 size_t wxMetafileDataObject::GetDataSize() const
473 return sizeof(METAFILEPICT
);
476 bool wxMetafileDataObject::GetDataHere(void *buf
) const
478 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
479 const wxMetafile
& mf
= GetMetafile();
481 wxCHECK_MSG( mf
.GetHMETAFILE(), false, _T("copying invalid metafile") );
483 // doesn't seem to work with any other mapping mode...
484 mfpict
->mm
= MM_ANISOTROPIC
; //mf.GetWindowsMappingMode();
485 mfpict
->xExt
= mf
.GetWidth();
486 mfpict
->yExt
= mf
.GetHeight();
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
);
492 mfpict
->hMF
= CopyMetaFile((HMETAFILE
)mf
.GetHMETAFILE(), NULL
);
497 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
499 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
502 mf
.SetWindowsMappingMode(mfpict
->mm
);
504 LONG w
= mfpict
->xExt
,
506 if ( mfpict
->mm
== MM_ANISOTROPIC
)
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
);
515 mf
.SetHMETAFILE((WXHANDLE
)mfpict
->hMF
);
517 wxCHECK_MSG( mfpict
->hMF
, false, _T("pasting invalid metafile") );
524 #endif // wxUSE_DRAG_AND_DROP
526 #endif // wxUSE_METAFILE