]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMetafileDC etc.
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "metafile.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
34 #include "wx/metafile.h"
35 #include "wx/clipbrd.h"
36 #include "wx/msw/private.h"
41 extern bool wxClipboardIsOpen
;
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
45 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
50 * Currently, the only purpose for making a metafile is to put
51 * it on the clipboard.
54 wxMetafileRefData::wxMetafileRefData(void)
57 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
60 wxMetafileRefData::~wxMetafileRefData(void)
64 DeleteMetaFile((HMETAFILE
) m_metafile
);
69 wxMetafile::wxMetafile(const wxString
& file
)
71 m_refData
= new wxMetafileRefData
;
73 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
74 M_METAFILEDATA
->m_metafile
= 0;
75 if (!file
.IsNull() && (file
.Cmp("") == 0))
76 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile(file
);
79 wxMetafile::~wxMetafile(void)
83 bool wxMetafile::SetClipboard(int width
, int height
)
88 bool alreadyOpen
=wxClipboardOpen();
92 if (!wxEmptyClipboard()) return FALSE
;
94 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
95 if (!alreadyOpen
) wxCloseClipboard();
96 return (bool) success
;
99 bool wxMetafile::Play(wxDC
*dc
)
106 if (dc
->GetHDC() && M_METAFILEDATA
->m_metafile
)
107 PlayMetaFile((HDC
) dc
->GetHDC(), (HMETAFILE
) M_METAFILEDATA
->m_metafile
);
114 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
117 m_refData
= new wxMetafileRefData
;
119 M_METAFILEDATA
->m_metafile
= mf
;
122 void wxMetafile::SetWindowsMappingMode(int mm
)
125 m_refData
= new wxMetafileRefData
;
127 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
131 * Metafile device context
135 // Original constructor that does not takes origin and extent. If you use this,
136 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
137 wxMetafileDC::wxMetafileDC(const wxString
& file
)
146 if (!file
.IsNull() && wxFileExists(file
))
149 if (!file
.IsNull() && (file
!= ""))
150 m_hDC
= (WXHDC
) CreateMetaFile(file
);
152 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
154 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
156 // Actual Windows mapping mode, for future reference.
157 m_windowsMappingMode
= wxMM_TEXT
;
159 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
162 // New constructor that takes origin and extent. If you use this, don't
163 // give origin/extent arguments to wxMakeMetafilePlaceable.
164 wxMetafileDC::wxMetafileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
170 if (file
!= "" && wxFileExists(file
)) wxRemoveFile(file
);
171 m_hDC
= (WXHDC
) CreateMetaFile(file
);
175 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
176 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
178 // Actual Windows mapping mode, for future reference.
179 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
181 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
184 wxMetafileDC::~wxMetafileDC(void)
189 void wxMetafileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
190 long *descent
, long *externalLeading
, wxFont
*theFont
, bool use16bit
) const
192 wxFont
*fontToUse
= theFont
;
194 fontToUse
= (wxFont
*) &m_font
;
196 HDC dc
= GetDC(NULL
);
200 GetTextExtentPoint(dc
, (char *)(const char *) string
, strlen((char *)(const char *) string
), &sizeRect
);
201 GetTextMetrics(dc
, &tm
);
205 *x
= XDEV2LOGREL(sizeRect
.cx
);
206 *y
= YDEV2LOGREL(sizeRect
.cy
);
207 if (descent
) *descent
= tm
.tmDescent
;
208 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
211 wxMetafile
*wxMetafileDC::Close(void)
213 SelectOldObjects(m_hDC
);
214 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
218 wxMetafile
*wx_mf
= new wxMetafile
;
219 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
220 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
226 void wxMetafileDC::SetMapMode(int mode
)
228 m_mappingMode
= mode
;
230 // int pixel_width = 0;
231 // int pixel_height = 0;
233 // int mm_height = 0;
235 float mm2pixelsX
= 10.0;
236 float mm2pixelsY
= 10.0;
242 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
243 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
248 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
249 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
254 m_logicalScaleX
= mm2pixelsX
;
255 m_logicalScaleY
= mm2pixelsY
;
260 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
261 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
267 m_logicalScaleX
= 1.0;
268 m_logicalScaleY
= 1.0;
285 struct mfPLACEABLEHEADER
{
294 struct mfPLACEABLEHEADER
{
305 * Pass filename of existing non-placeable metafile, and bounding box.
306 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
307 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
311 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
313 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, FALSE
);
316 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
318 // I'm not sure if this is the correct way of suggesting a scale
319 // to the client application, but it's the only way I can find.
320 int unitsPerInch
= (int)(576/scale
);
322 mfPLACEABLEHEADER header
;
323 header
.key
= 0x9AC6CDD7L
;
325 header
.bbox
.left
= (int)(x1
);
326 header
.bbox
.top
= (int)(y1
);
327 header
.bbox
.right
= (int)(x2
);
328 header
.bbox
.bottom
= (int)(y2
);
329 header
.inch
= unitsPerInch
;
332 // Calculate checksum
334 mfPLACEABLEHEADER
*pMFHead
= &header
;
335 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
336 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
337 pMFHead
->checksum
^= *p
;
339 FILE *fd
= fopen((char *)(const char *)filename
, "rb");
340 if (!fd
) return FALSE
;
342 char tempFileBuf
[256];
343 wxGetTempFileName("mf", tempFileBuf
);
344 FILE *fHandle
= fopen(tempFileBuf
, "wb");
347 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
349 // Calculate origin and extent
352 int extentX
= x2
- x1
;
353 int extentY
= (y2
- y1
);
355 // Read metafile header and write
356 METAHEADER metaHeader
;
357 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
359 if (useOriginAndExtent
)
360 metaHeader
.mtSize
+= 15;
362 metaHeader
.mtSize
+= 5;
364 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
366 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
368 char originBuffer
[10];
369 char extentBuffer
[10];
370 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
372 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
373 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
375 modeRecord
->rdSize
= 4;
376 modeRecord
->rdFunction
= META_SETMAPMODE
;
377 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
379 originRecord
->rdSize
= 5;
380 originRecord
->rdFunction
= META_SETWINDOWORG
;
381 originRecord
->rdParm
[0] = originY
;
382 originRecord
->rdParm
[1] = originX
;
384 extentRecord
->rdSize
= 5;
385 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
386 extentRecord
->rdParm
[0] = extentY
;
387 extentRecord
->rdParm
[1] = extentX
;
389 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
391 if (useOriginAndExtent
)
393 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
394 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
408 wxRemoveFile(filename
);
409 wxCopyFile(tempFileBuf
, filename
);
410 wxRemoveFile(tempFileBuf
);
414 #endif // wxUSE_METAFILE