]>
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 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
44 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
48 * Currently, the only purpose for making a metafile is to put
49 * it on the clipboard.
52 wxMetafileRefData::wxMetafileRefData(void)
55 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
58 wxMetafileRefData::~wxMetafileRefData(void)
62 DeleteMetaFile((HMETAFILE
) m_metafile
);
67 wxMetafile::wxMetafile(const wxString
& file
)
69 m_refData
= new wxMetafileRefData
;
71 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
72 M_METAFILEDATA
->m_metafile
= 0;
73 if (!file
.IsNull() && (file
.Cmp(wxT("")) == 0))
74 M_METAFILEDATA
->m_metafile
= (WXHANDLE
) GetMetaFile(file
);
77 wxMetafile::~wxMetafile(void)
81 bool wxMetafile::SetClipboard(int width
, int height
)
86 bool alreadyOpen
=wxClipboardOpen();
90 if (!wxEmptyClipboard()) return FALSE
;
92 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
93 if (!alreadyOpen
) wxCloseClipboard();
94 return (bool) success
;
97 bool wxMetafile::Play(wxDC
*dc
)
104 if (dc
->GetHDC() && M_METAFILEDATA
->m_metafile
)
105 PlayMetaFile((HDC
) dc
->GetHDC(), (HMETAFILE
) M_METAFILEDATA
->m_metafile
);
112 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
115 m_refData
= new wxMetafileRefData
;
117 M_METAFILEDATA
->m_metafile
= mf
;
120 void wxMetafile::SetWindowsMappingMode(int mm
)
123 m_refData
= new wxMetafileRefData
;
125 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
129 * Metafile device context
133 // Original constructor that does not takes origin and extent. If you use this,
134 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
135 wxMetafileDC::wxMetafileDC(const wxString
& file
)
144 if (!file
.IsNull() && wxFileExists(file
))
147 if (!file
.IsNull() && (file
!= wxT("")))
148 m_hDC
= (WXHDC
) CreateMetaFile(file
);
150 m_hDC
= (WXHDC
) CreateMetaFile(NULL
);
152 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
154 // Actual Windows mapping mode, for future reference.
155 m_windowsMappingMode
= wxMM_TEXT
;
157 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
160 // New constructor that takes origin and extent. If you use this, don't
161 // give origin/extent arguments to wxMakeMetafilePlaceable.
162 wxMetafileDC::wxMetafileDC(const wxString
& file
, int xext
, int yext
, int xorg
, int yorg
)
168 if (file
!= wxT("") && wxFileExists(file
)) wxRemoveFile(file
);
169 m_hDC
= (WXHDC
) CreateMetaFile(file
);
173 ::SetWindowOrgEx((HDC
) m_hDC
,xorg
,yorg
, NULL
);
174 ::SetWindowExtEx((HDC
) m_hDC
,xext
,yext
, NULL
);
176 // Actual Windows mapping mode, for future reference.
177 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
179 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
182 wxMetafileDC::~wxMetafileDC(void)
187 void wxMetafileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
188 long *descent
, long *externalLeading
, wxFont
*theFont
, bool use16bit
) const
190 wxFont
*fontToUse
= theFont
;
192 fontToUse
= (wxFont
*) &m_font
;
194 HDC dc
= GetDC(NULL
);
198 GetTextExtentPoint(dc
, WXSTRINGCAST string
, wxStrlen(WXSTRINGCAST string
), &sizeRect
);
199 GetTextMetrics(dc
, &tm
);
208 *descent
= tm
.tmDescent
;
209 if ( externalLeading
)
210 *externalLeading
= tm
.tmExternalLeading
;
213 wxMetafile
*wxMetafileDC::Close(void)
215 SelectOldObjects(m_hDC
);
216 HANDLE mf
= CloseMetaFile((HDC
) m_hDC
);
220 wxMetafile
*wx_mf
= new wxMetafile
;
221 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
222 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
228 void wxMetafileDC::SetMapMode(int mode
)
230 m_mappingMode
= mode
;
232 // int pixel_width = 0;
233 // int pixel_height = 0;
235 // int mm_height = 0;
237 float mm2pixelsX
= 10.0;
238 float mm2pixelsY
= 10.0;
244 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
245 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
250 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
251 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
256 m_logicalScaleX
= mm2pixelsX
;
257 m_logicalScaleY
= mm2pixelsY
;
262 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
263 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
269 m_logicalScaleX
= 1.0;
270 m_logicalScaleY
= 1.0;
287 struct mfPLACEABLEHEADER
{
296 struct mfPLACEABLEHEADER
{
307 * Pass filename of existing non-placeable metafile, and bounding box.
308 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
309 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
313 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
315 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, FALSE
);
318 bool wxMakeMetafilePlaceable(const wxString
& filename
, int x1
, int y1
, int x2
, int y2
, float scale
, bool useOriginAndExtent
)
320 // I'm not sure if this is the correct way of suggesting a scale
321 // to the client application, but it's the only way I can find.
322 int unitsPerInch
= (int)(576/scale
);
324 mfPLACEABLEHEADER header
;
325 header
.key
= 0x9AC6CDD7L
;
327 header
.bbox
.left
= (int)(x1
);
328 header
.bbox
.top
= (int)(y1
);
329 header
.bbox
.right
= (int)(x2
);
330 header
.bbox
.bottom
= (int)(y2
);
331 header
.inch
= unitsPerInch
;
334 // Calculate checksum
336 mfPLACEABLEHEADER
*pMFHead
= &header
;
337 for (p
=(WORD
*)pMFHead
,pMFHead
-> checksum
= 0;
338 p
< (WORD
*)&pMFHead
->checksum
; ++p
)
339 pMFHead
->checksum
^= *p
;
341 FILE *fd
= fopen(filename
.fn_str(), "rb");
342 if (!fd
) return FALSE
;
344 wxChar tempFileBuf
[256];
345 wxGetTempFileName(wxT("mf"), tempFileBuf
);
346 FILE *fHandle
= fopen(wxConvFile
.cWX2MB(tempFileBuf
), "wb");
349 fwrite((void *)&header
, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER
), fHandle
);
351 // Calculate origin and extent
354 int extentX
= x2
- x1
;
355 int extentY
= (y2
- y1
);
357 // Read metafile header and write
358 METAHEADER metaHeader
;
359 fread((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fd
);
361 if (useOriginAndExtent
)
362 metaHeader
.mtSize
+= 15;
364 metaHeader
.mtSize
+= 5;
366 fwrite((void *)&metaHeader
, sizeof(unsigned char), sizeof(metaHeader
), fHandle
);
368 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
370 char originBuffer
[10];
371 char extentBuffer
[10];
372 METARECORD
*modeRecord
= (METARECORD
*)&modeBuffer
;
374 METARECORD
*originRecord
= (METARECORD
*)&originBuffer
;
375 METARECORD
*extentRecord
= (METARECORD
*)&extentBuffer
;
377 modeRecord
->rdSize
= 4;
378 modeRecord
->rdFunction
= META_SETMAPMODE
;
379 modeRecord
->rdParm
[0] = MM_ANISOTROPIC
;
381 originRecord
->rdSize
= 5;
382 originRecord
->rdFunction
= META_SETWINDOWORG
;
383 originRecord
->rdParm
[0] = originY
;
384 originRecord
->rdParm
[1] = originX
;
386 extentRecord
->rdSize
= 5;
387 extentRecord
->rdFunction
= META_SETWINDOWEXT
;
388 extentRecord
->rdParm
[0] = extentY
;
389 extentRecord
->rdParm
[1] = extentX
;
391 fwrite((void *)modeBuffer
, sizeof(char), 8, fHandle
);
393 if (useOriginAndExtent
)
395 fwrite((void *)originBuffer
, sizeof(char), 10, fHandle
);
396 fwrite((void *)extentBuffer
, sizeof(char), 10, fHandle
);
410 wxRemoveFile(filename
);
411 wxCopyFile(tempFileBuf
, filename
);
412 wxRemoveFile(tempFileBuf
);
416 #endif // wxUSE_METAFILE