]>
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
)
49 * Metafiles - Windows 3.1 only
50 * Currently, the only purpose for making a metafile is to put
51 * it on the clipboard.
54 wxMetaFile::wxMetaFile(const wxString
& file
)
56 m_windowsMappingMode
= MM_ANISOTROPIC
;
58 if (!file
.IsNull() && file
== "")
59 m_metaFile
= (WXHANDLE
) GetMetaFile(file
);
62 wxMetaFile::~wxMetaFile(void)
65 { DeleteMetaFile((HMETAFILE
) m_metaFile
); m_metaFile
= 0; }
68 bool wxMetaFile::SetClipboard(int width
, int height
)
70 bool alreadyOpen
=wxClipboardOpen();
74 if (!wxEmptyClipboard()) return FALSE
;
76 bool success
= wxSetClipboardData(wxDF_METAFILE
,this, width
,height
);
77 if (!alreadyOpen
) wxCloseClipboard();
78 return (bool) success
;
81 bool wxMetaFile::Play(wxDC
*dc
)
85 if (dc
->GetHDC() && m_metaFile
)
86 PlayMetaFile((HDC
) dc
->GetHDC(), (HMETAFILE
) m_metaFile
);
94 * Metafile device context
98 // Original constructor that does not takes origin and extent. If you use this,
99 // *DO* give origin/extent arguments to wxMakeMetaFilePlaceable.
100 wxMetaFileDC::wxMetaFileDC(const wxString
& file
)
109 if (!file
.IsNull() && wxFileExists(file
))
112 if (!file
.IsNull() && (file
!= ""))
113 m_hDC
= (WXHDC
) CreateMetaFile(file
);
115 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
117 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
119 // Actual Windows mapping mode, for future reference.
120 m_windowsMappingMode
= MM_TEXT
;
122 SetMapMode(MM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
125 // New constructor that takes origin and extent. If you use this, don't
126 // give origin/extent arguments to wxMakeMetaFilePlaceable.
127 wxMetaFileDC::wxMetaFileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
133 if (file
!= "" && wxFileExists(file
)) wxRemoveFile(file
);
134 m_hDC
= (WXHDC
) CreateMetaFile(file
);
138 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
139 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
141 // Actual Windows mapping mode, for future reference.
142 m_windowsMappingMode
= MM_ANISOTROPIC
;
144 SetMapMode(MM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
147 wxMetaFileDC::~wxMetaFileDC(void)
152 void wxMetaFileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
153 long *descent
, long *externalLeading
, wxFont
*theFont
, bool use16bit
) const
155 wxFont
*fontToUse
= theFont
;
157 fontToUse
= (wxFont
*) &m_font
;
159 HDC dc
= GetDC(NULL
);
163 GetTextExtentPoint(dc
, (char *)(const char *) string
, strlen((char *)(const char *) string
), &sizeRect
);
164 GetTextMetrics(dc
, &tm
);
168 *x
= XDEV2LOGREL(sizeRect
.cx
);
169 *y
= YDEV2LOGREL(sizeRect
.cy
);
170 if (descent
) *descent
= tm
.tmDescent
;
171 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
174 wxMetaFile
*wxMetaFileDC::Close(void)
176 SelectOldObjects(m_hDC
);
177 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
181 wxMetaFile
*wx_mf
= new wxMetaFile
;
182 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
183 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
189 void wxMetaFileDC::SetMapMode(int mode
)
191 m_mappingMode
= mode
;
193 // int pixel_width = 0;
194 // int pixel_height = 0;
196 // int mm_height = 0;
198 float mm2pixelsX
= 10.0;
199 float mm2pixelsY
= 10.0;
205 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
206 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
211 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
212 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
217 m_logicalScaleX
= mm2pixelsX
;
218 m_logicalScaleY
= mm2pixelsY
;
223 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
224 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
230 m_logicalScaleX
= 1.0;
231 m_logicalScaleY
= 1.0;
248 struct mfPLACEABLEHEADER
{
257 struct mfPLACEABLEHEADER
{
268 * Pass filename of existing non-placeable metafile, and bounding box.
269 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
270 * and sets the window origin and extent to mimic the MM_TEXT mapping mode.
274 bool wxMakeMetaFilePlaceable(const wxString
& filename
, float scale
)
276 return wxMakeMetaFilePlaceable(filename
, 0, 0, 0, 0, scale
, FALSE
);
279 bool wxMakeMetaFilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
281 // I'm not sure if this is the correct way of suggesting a scale
282 // to the client application, but it's the only way I can find.
283 int unitsPerInch
= (int)(576/scale
);
285 mfPLACEABLEHEADER header
;
286 header
.key
= 0x9AC6CDD7L
;
288 header
.bbox
.left
= (int)(x1
);
289 header
.bbox
.top
= (int)(y1
);
290 header
.bbox
.right
= (int)(x2
);
291 header
.bbox
.bottom
= (int)(y2
);
292 header
.inch
= unitsPerInch
;
295 // Calculate checksum
297 mfPLACEABLEHEADER
*pMFHead
= &header
;
298 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
299 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
300 pMFHead
->checksum
^= *p
;
302 FILE *fd
= fopen((char *)(const char *)filename
, "rb");
303 if (!fd
) return FALSE
;
305 char tempFileBuf
[256];
306 wxGetTempFileName("mf", tempFileBuf
);
307 FILE *fHandle
= fopen(tempFileBuf
, "wb");
310 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
312 // Calculate origin and extent
315 int extentX
= x2
- x1
;
316 int extentY
= (y2
- y1
);
318 // Read metafile header and write
319 METAHEADER metaHeader
;
320 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
322 if (useOriginAndExtent
)
323 metaHeader
.mtSize
+= 15;
325 metaHeader
.mtSize
+= 5;
327 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
329 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
331 char originBuffer
[10];
332 char extentBuffer
[10];
333 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
335 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
336 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
338 modeRecord
->rdSize
= 4;
339 modeRecord
->rdFunction
= META_SETMAPMODE
;
340 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
342 originRecord
->rdSize
= 5;
343 originRecord
->rdFunction
= META_SETWINDOWORG
;
344 originRecord
->rdParm
[0] = originY
;
345 originRecord
->rdParm
[1] = originX
;
347 extentRecord
->rdSize
= 5;
348 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
349 extentRecord
->rdParm
[0] = extentY
;
350 extentRecord
->rdParm
[1] = extentX
;
352 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
354 if (useOriginAndExtent
)
356 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
357 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
371 wxRemoveFile(filename
);
372 wxCopyFile(tempFileBuf
, filename
);
373 wxRemoveFile(tempFileBuf
);
377 #endif // USE_METAFILE