]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
26 #include "wx/metafile.h"
27 #include "wx/clipbrd.h"
28 #include "wx/os2/private.h"
33 extern bool wxClipboardIsOpen
;
35 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
36 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
40 * Currently, the only purpose for making a metafile is to put
41 * it on the clipboard.
44 wxMetafileRefData::wxMetafileRefData(void)
47 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
50 wxMetafileRefData::~wxMetafileRefData(void)
54 // TODO: DeleteMetaFile((HMETAFILE) m_metafile);
59 wxMetafile::wxMetafile(const wxString
& file
)
61 m_refData
= new wxMetafileRefData
;
63 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
64 M_METAFILEDATA
->m_metafile
= 0;
65 if (!file
.IsNull() && (file
.Cmp(wxT("")) == 0))
66 M_METAFILEDATA
->m_metafile
= (WXHANDLE
)0; // TODO: GetMetaFile(file);
69 wxMetafile::~wxMetafile(void)
73 bool wxMetafile::SetClipboard(int width
, int height
)
83 bool alreadyOpen
=wxClipboardOpen();
87 if (!wxEmptyClipboard()) return FALSE
;
89 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
90 if (!alreadyOpen
) wxCloseClipboard();
91 return (bool) success
;
95 bool wxMetafile::Play(wxDC
*dc
)
102 // if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
103 // PlayMetaFile((HDC) dc->GetHDC(), (HMETAFILE) M_METAFILEDATA->m_metafile);
110 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
113 m_refData
= new wxMetafileRefData
;
115 M_METAFILEDATA
->m_metafile
= mf
;
118 void wxMetafile::SetWindowsMappingMode(int mm
)
121 m_refData
= new wxMetafileRefData
;
123 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
127 * Metafile device context
131 // Original constructor that does not takes origin and extent. If you use this,
132 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
133 wxMetafileDC::wxMetafileDC(const wxString
& file
)
142 if (!file
.IsNull() && wxFileExists(file
))
147 if (!file.IsNull() && (file != wxT("")))
148 m_hDC = (WXHDC) CreateMetaFile(file);
150 m_hDC = (WXHDC) CreateMetaFile(NULL);
153 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
155 // Actual Windows mapping mode, for future reference.
156 m_windowsMappingMode
= wxMM_TEXT
;
158 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
161 // New constructor that takes origin and extent. If you use this, don't
162 // give origin/extent arguments to wxMakeMetafilePlaceable.
163 wxMetafileDC::wxMetafileDC( const wxString
& file
,
173 if (file
!= wxT("") && wxFileExists(file
))
176 // m_hDC = (WXHDC) CreateMetaFile(file);
180 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
181 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
183 // Actual Windows mapping mode, for future reference.
184 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
186 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
189 wxMetafileDC::~wxMetafileDC(void)
194 void wxMetafileDC::GetTextExtent(const wxString
& WXUNUSED(string
),
197 long *WXUNUSED(descent
),
198 long *WXUNUSED(externalLeading
),
200 bool WXUNUSED(use16bit
) ) const
202 wxFont
*fontToUse
= theFont
;
204 fontToUse
= (wxFont
*) &m_font
;
208 HDC dc = GetDC(NULL);
212 GetTextExtentPoint(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
213 GetTextMetrics(dc, &tm);
222 *descent = tm.tmDescent;
223 if ( externalLeading )
224 *externalLeading = tm.tmExternalLeading;
228 wxMetafile
*wxMetafileDC::Close(void)
230 SelectOldObjects(m_hDC
);
231 HANDLE mf
= 0; // TODO: CloseMetaFile((HDC) m_hDC);
235 wxMetafile
*wx_mf
= new wxMetafile
;
236 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
237 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
243 void wxMetafileDC::SetMapMode(int mode
)
245 m_mappingMode
= mode
;
247 // int pixel_width = 0;
248 // int pixel_height = 0;
250 // int mm_height = 0;
252 float mm2pixelsX
= 10.0;
253 float mm2pixelsY
= 10.0;
259 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
260 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
265 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
266 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
271 m_logicalScaleX
= mm2pixelsX
;
272 m_logicalScaleY
= mm2pixelsY
;
277 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
278 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
284 m_logicalScaleX
= 1.0;
285 m_logicalScaleY
= 1.0;
302 struct mfPLACEABLEHEADER
{
311 struct mfPLACEABLEHEADER
{
322 * Pass filename of existing non-placeable metafile, and bounding box.
323 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
324 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
328 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
330 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, FALSE
);
333 bool wxMakeMetafilePlaceable(const wxString
& WXUNUSED(filename
),
338 float WXUNUSED(scale
),
339 bool WXUNUSED(useOriginAndExtent
))
341 // TODO: the OS/2 PM/MM way to do this
343 // I'm not sure if this is the correct way of suggesting a scale
344 // to the client application, but it's the only way I can find.
345 int unitsPerInch = (int)(576/scale);
347 mfPLACEABLEHEADER header;
348 header.key = 0x9AC6CDD7L;
350 header.bbox.xLeft = (int)(x1);
351 header.bbox.yTop = (int)(y1);
352 header.bbox.xRight = (int)(x2);
353 header.bbox.yBottom = (int)(y2);
354 header.inch = unitsPerInch;
357 // Calculate checksum
359 mfPLACEABLEHEADER *pMFHead = &header;
360 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0; p < (WORD *)&pMFHead ->checksum; ++p)
361 pMFHead ->checksum ^= *p;
363 FILE *fd = fopen(filename.fn_str(), "rb");
367 wxChar tempFileBuf[256];
368 wxGetTempFileName(wxT("mf"), tempFileBuf);
369 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
372 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
374 // Calculate origin and extent
377 int extentX = x2 - x1;
378 int extentY = (y2 - y1);
380 // Read metafile header and write
381 METAHEADER metaHeader;
382 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
384 if (useOriginAndExtent)
385 metaHeader.mtSize += 15;
387 metaHeader.mtSize += 5;
389 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
391 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
393 char originBuffer[10];
394 char extentBuffer[10];
395 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
397 METARECORD *originRecord = (METARECORD *)&originBuffer;
398 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
400 modeRecord->rdSize = 4;
401 modeRecord->rdFunction = META_SETMAPMODE;
402 modeRecord->rdParm[0] = MM_ANISOTROPIC;
404 originRecord->rdSize = 5;
405 originRecord->rdFunction = META_SETWINDOWORG;
406 originRecord->rdParm[0] = originY;
407 originRecord->rdParm[1] = originX;
409 extentRecord->rdSize = 5;
410 extentRecord->rdFunction = META_SETWINDOWEXT;
411 extentRecord->rdParm[0] = extentY;
412 extentRecord->rdParm[1] = extentX;
414 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
416 if (useOriginAndExtent)
418 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
419 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
433 wxRemoveFile(filename);
434 wxCopyFile(tempFileBuf, filename);
435 wxRemoveFile(tempFileBuf);
440 #endif // wxUSE_METAFILE