]>
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 bool wxMetafile::SetClipboard(int width
, int height
)
105 bool alreadyOpen
= wxClipboardOpen();
109 if (!wxEmptyClipboard())
112 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
120 bool wxMetafile::Play(wxDC
*dc
)
127 if (dc
->GetHDC() && M_METAFILEDATA
->m_metafile
)
129 if ( !::PlayMetaFile(GetHdcOf(*dc
), (HMETAFILE
)
130 M_METAFILEDATA
->m_metafile
) )
132 wxLogLastError(_T("PlayMetaFile"));
141 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
144 m_refData
= new wxMetafileRefData
;
146 M_METAFILEDATA
->m_metafile
= mf
;
149 void wxMetafile::SetWindowsMappingMode(int mm
)
152 m_refData
= new wxMetafileRefData
;
154 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
157 // ----------------------------------------------------------------------------
158 // Metafile device context
159 // ----------------------------------------------------------------------------
161 // Original constructor that does not takes origin and extent. If you use this,
162 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
163 wxMetafileDC::wxMetafileDC(const wxString
& file
)
172 if (!file
.IsNull() && wxFileExists(file
))
175 if (!file
.IsNull() && (file
!= wxEmptyString
))
176 m_hDC
= (WXHDC
) CreateMetaFile(file
);
178 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
180 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
182 // Actual Windows mapping mode, for future reference.
183 m_windowsMappingMode
= wxMM_TEXT
;
185 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
188 // New constructor that takes origin and extent. If you use this, don't
189 // give origin/extent arguments to wxMakeMetafilePlaceable.
190 wxMetafileDC::wxMetafileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
196 if ( !file
.empty() && wxFileExists(file
))
198 m_hDC
= (WXHDC
) CreateMetaFile(file
);
202 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
203 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
205 // Actual Windows mapping mode, for future reference.
206 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
208 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
211 wxMetafileDC::~wxMetafileDC()
216 void wxMetafileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
217 long *descent
, long *externalLeading
, wxFont
*theFont
, bool WXUNUSED(use16bit
)) const
219 wxFont
*fontToUse
= theFont
;
221 fontToUse
= (wxFont
*) &m_font
;
223 HDC dc
= GetDC(NULL
);
227 ::GetTextExtentPoint32(dc
, WXSTRINGCAST string
, wxStrlen(WXSTRINGCAST string
), &sizeRect
);
228 GetTextMetrics(dc
, &tm
);
237 *descent
= tm
.tmDescent
;
238 if ( externalLeading
)
239 *externalLeading
= tm
.tmExternalLeading
;
242 void wxMetafileDC::DoGetSize(int *width
, int *height
) const
244 wxCHECK_RET( m_refData
, _T("invalid wxMetafileDC") );
247 *width
= M_METAFILEDATA
->m_width
;
249 *height
= M_METAFILEDATA
->m_height
;
252 wxMetafile
*wxMetafileDC::Close()
254 SelectOldObjects(m_hDC
);
255 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
259 wxMetafile
*wx_mf
= new wxMetafile
;
260 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
261 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
267 void wxMetafileDC::SetMapMode(int mode
)
269 m_mappingMode
= mode
;
271 // int pixel_width = 0;
272 // int pixel_height = 0;
274 // int mm_height = 0;
276 float mm2pixelsX
= 10.0;
277 float mm2pixelsY
= 10.0;
283 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
284 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
289 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
290 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
295 m_logicalScaleX
= mm2pixelsX
;
296 m_logicalScaleY
= mm2pixelsY
;
301 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
302 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
308 m_logicalScaleX
= 1.0;
309 m_logicalScaleY
= 1.0;
315 // ----------------------------------------------------------------------------
316 // wxMakeMetafilePlaceable
317 // ----------------------------------------------------------------------------
328 struct mfPLACEABLEHEADER
{
337 struct mfPLACEABLEHEADER
{
348 * Pass filename of existing non-placeable metafile, and bounding box.
349 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
350 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
354 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
356 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, false);
359 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
361 // I'm not sure if this is the correct way of suggesting a scale
362 // to the client application, but it's the only way I can find.
363 int unitsPerInch
= (int)(576/scale
);
365 mfPLACEABLEHEADER header
;
366 header
.key
= 0x9AC6CDD7L
;
368 header
.bbox
.left
= (int)(x1
);
369 header
.bbox
.top
= (int)(y1
);
370 header
.bbox
.right
= (int)(x2
);
371 header
.bbox
.bottom
= (int)(y2
);
372 header
.inch
= unitsPerInch
;
375 // Calculate checksum
377 mfPLACEABLEHEADER
*pMFHead
= &header
;
378 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
379 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
380 pMFHead
->checksum
^= *p
;
382 FILE *fd
= wxFopen(filename
.fn_str(), _T("rb"));
383 if (!fd
) return false;
385 wxChar tempFileBuf
[256];
386 wxGetTempFileName(wxT("mf"), tempFileBuf
);
387 FILE *fHandle
= wxFopen(wxFNCONV(tempFileBuf
), _T("wb"));
390 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
392 // Calculate origin and extent
395 int extentX
= x2
- x1
;
396 int extentY
= (y2
- y1
);
398 // Read metafile header and write
399 METAHEADER metaHeader
;
400 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
402 if (useOriginAndExtent
)
403 metaHeader
.mtSize
+= 15;
405 metaHeader
.mtSize
+= 5;
407 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
409 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
411 char originBuffer
[10];
412 char extentBuffer
[10];
413 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
415 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
416 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
418 modeRecord
->rdSize
= 4;
419 modeRecord
->rdFunction
= META_SETMAPMODE
;
420 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
422 originRecord
->rdSize
= 5;
423 originRecord
->rdFunction
= META_SETWINDOWORG
;
424 originRecord
->rdParm
[0] = originY
;
425 originRecord
->rdParm
[1] = originX
;
427 extentRecord
->rdSize
= 5;
428 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
429 extentRecord
->rdParm
[0] = extentY
;
430 extentRecord
->rdParm
[1] = extentX
;
432 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
434 if (useOriginAndExtent
)
436 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
437 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
451 wxRemoveFile(filename
);
452 wxCopyFile(tempFileBuf
, filename
);
453 wxRemoveFile(tempFileBuf
);
458 #if wxUSE_DRAG_AND_DROP
460 // ----------------------------------------------------------------------------
461 // wxMetafileDataObject
462 // ----------------------------------------------------------------------------
464 size_t wxMetafileDataObject::GetDataSize() const
466 return sizeof(METAFILEPICT
);
469 bool wxMetafileDataObject::GetDataHere(void *buf
) const
471 METAFILEPICT
*mfpict
= (METAFILEPICT
*)buf
;
472 const wxMetafile
& mf
= GetMetafile();
474 wxCHECK_MSG( mf
.GetHMETAFILE(), false, _T("copying invalid metafile") );
476 // doesn't seem to work with any other mapping mode...
477 mfpict
->mm
= MM_ANISOTROPIC
; //mf.GetWindowsMappingMode();
478 mfpict
->xExt
= mf
.GetWidth();
479 mfpict
->yExt
= mf
.GetHeight();
481 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
482 // what DC the picture will be rendered to, use the default display one
483 PixelToHIMETRIC(&mfpict
->xExt
, &mfpict
->yExt
);
485 mfpict
->hMF
= CopyMetaFile((HMETAFILE
)mf
.GetHMETAFILE(), NULL
);
490 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
492 const METAFILEPICT
*mfpict
= (const METAFILEPICT
*)buf
;
495 mf
.SetWindowsMappingMode(mfpict
->mm
);
497 LONG w
= mfpict
->xExt
,
499 if ( mfpict
->mm
== MM_ANISOTROPIC
)
501 // in this case xExt and yExt contain suggested size in HIMETRIC units
502 // (0.01 mm) - transform this to something more reasonable (pixels)
503 HIMETRICToPixel(&w
, &h
);
508 mf
.SetHMETAFILE((WXHANDLE
)mfpict
->hMF
);
510 wxCHECK_MSG( mfpict
->hMF
, false, _T("pasting invalid metafile") );
517 #endif // wxUSE_DRAG_AND_DROP
519 #endif // wxUSE_METAFILE