]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/metafile.h"
32 #include "wx/filename.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
= MM_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
= MM_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(*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(wxT("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 ( wxFileExists(file
) )
183 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
185 m_hDC
= (WXHDC
) CreateMetaFile(file
);
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
: wxMSW_CONV_LPCTSTR(file
));
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
= MM_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
, wxT("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(wxMappingMode 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(), wxT("rb"));
393 if (!fd
) return false;
395 wxString tempFileBuf
= wxFileName::CreateTempFileName(wxT("mf"));
396 if (tempFileBuf
.empty())
399 FILE *fHandle
= wxFopen(tempFileBuf
.fn_str(), wxT("wb"));
402 fwrite((void *)&header
, 1, sizeof(mfPLACEABLEHEADER
), fHandle
);
404 // Calculate origin and extent
407 int extentX
= x2
- x1
;
408 int extentY
= (y2
- y1
);
410 // Read metafile header and write
411 METAHEADER metaHeader
;
412 fread((void *)&metaHeader
, 1, sizeof(metaHeader
), fd
);
414 if (useOriginAndExtent
)
415 metaHeader
.mtSize
+= 15;
417 metaHeader
.mtSize
+= 5;
419 fwrite((void *)&metaHeader
, 1, sizeof(metaHeader
), fHandle
);
421 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
423 char originBuffer
[10];
424 char extentBuffer
[10];
425 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
427 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
428 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
430 modeRecord
->rdSize
= 4;
431 modeRecord
->rdFunction
= META_SETMAPMODE
;
432 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
434 originRecord
->rdSize
= 5;
435 originRecord
->rdFunction
= META_SETWINDOWORG
;
436 originRecord
->rdParm
[0] = originY
;
437 originRecord
->rdParm
[1] = originX
;
439 extentRecord
->rdSize
= 5;
440 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
441 extentRecord
->rdParm
[0] = extentY
;
442 extentRecord
->rdParm
[1] = extentX
;
444 fwrite((void *)modeBuffer
, 1, 8, fHandle
);
446 if (useOriginAndExtent
)
448 fwrite((void *)originBuffer
, 1, 10, fHandle
);
449 fwrite((void *)extentBuffer
, 1, 10, fHandle
);
463 wxRemoveFile(filename
);
464 wxCopyFile(tempFileBuf
, filename
);
465 wxRemoveFile(tempFileBuf
);
470 #if wxUSE_DRAG_AND_DROP
472 // ----------------------------------------------------------------------------
473 // wxMetafileDataObject
474 // ----------------------------------------------------------------------------
476 size_t wxMetafileDataObject::GetDataSize() const
478 return sizeof(METAFILEPICT
);
481 bool wxMetafileDataObject::GetDataHere(void *buf
) const
483 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
484 const wxMetafile
& mf
= GetMetafile();
486 wxCHECK_MSG( mf
.GetHMETAFILE(), false, wxT("copying invalid metafile") );
488 // doesn't seem to work with any other mapping mode...
489 mfpict
->mm
= MM_ANISOTROPIC
; //mf.GetWindowsMappingMode();
490 mfpict
->xExt
= mf
.GetWidth();
491 mfpict
->yExt
= mf
.GetHeight();
493 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
494 // what DC the picture will be rendered to, use the default display one
495 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
497 mfpict
->hMF
= CopyMetaFile((HMETAFILE
)mf
.GetHMETAFILE(), NULL
);
502 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
504 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
507 mf
.SetWindowsMappingMode(mfpict
->mm
);
509 LONG w
= mfpict
->xExt
,
511 if ( mfpict
->mm
== MM_ANISOTROPIC
)
513 // in this case xExt and yExt contain suggested size in HIMETRIC units
514 // (0.01 mm) - transform this to something more reasonable (pixels)
515 HIMETRICToPixel(&w
, &h
);
520 mf
.SetHMETAFILE((WXHANDLE
)mfpict
->hMF
);
522 wxCHECK_MSG( mfpict
->hMF
, false, wxT("pasting invalid metafile") );
529 #endif // wxUSE_DRAG_AND_DROP
531 #endif // wxUSE_METAFILE