]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/metafile.cpp
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"
22 #include "wx/metafile.h"
23 #include "wx/clipbrd.h"
24 #include "wx/os2/private.h"
29 extern bool wxClipboardIsOpen
;
31 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
32 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
36 * Currently, the only purpose for making a metafile is to put
37 * it on the clipboard.
40 wxMetafileRefData::wxMetafileRefData(void)
43 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
46 wxMetafileRefData::~wxMetafileRefData(void)
50 // TODO: DeleteMetaFile((HMETAFILE) m_metafile);
55 wxMetafile::wxMetafile(const wxString
& file
)
57 m_refData
= new wxMetafileRefData
;
59 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
60 M_METAFILEDATA
->m_metafile
= 0;
62 M_METAFILEDATA
->m_metafile
= (WXHANDLE
)0; // TODO: GetMetaFile(file);
65 wxMetafile::~wxMetafile(void)
69 bool wxMetafile::SetClipboard(int width
, int height
)
79 bool alreadyOpen
=wxClipboardOpen();
83 if (!wxEmptyClipboard()) return FALSE
;
85 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
86 if (!alreadyOpen
) wxCloseClipboard();
87 return (bool) success
;
91 bool wxMetafile::Play(wxDC
*dc
)
98 // if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
99 // PlayMetaFile((HDC) dc->GetHDC(), (HMETAFILE) M_METAFILEDATA->m_metafile);
106 void wxMetafile::SetHMETAFILE(WXHANDLE mf
)
109 m_refData
= new wxMetafileRefData
;
111 M_METAFILEDATA
->m_metafile
= mf
;
114 void wxMetafile::SetWindowsMappingMode(int mm
)
117 m_refData
= new wxMetafileRefData
;
119 M_METAFILEDATA
->m_windowsMappingMode
= mm
;
123 * Metafile device context
127 // Original constructor that does not takes origin and extent. If you use this,
128 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
129 wxMetafileDC::wxMetafileDC(const wxString
& file
)
138 if (!file
.IsNull() && wxFileExists(file
))
144 m_hDC = (WXHDC) CreateMetaFile(file);
146 m_hDC = (WXHDC) CreateMetaFile(NULL);
149 m_ok
= (m_hDC
!= (WXHDC
) 0) ;
151 // Actual Windows mapping mode, for future reference.
152 m_windowsMappingMode
= wxMM_TEXT
;
154 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
157 // New constructor that takes origin and extent. If you use this, don't
158 // give origin/extent arguments to wxMakeMetafilePlaceable.
159 wxMetafileDC::wxMetafileDC( const wxString
& file
,
169 if (!file
.empty() && wxFileExists(file
))
172 // m_hDC = (WXHDC) CreateMetaFile(file);
176 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
177 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
179 // Actual Windows mapping mode, for future reference.
180 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
182 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
185 wxMetafileDC::~wxMetafileDC(void)
190 void wxMetafileDC::GetTextExtent(const wxString
& WXUNUSED(string
),
193 long *WXUNUSED(descent
),
194 long *WXUNUSED(externalLeading
),
196 bool WXUNUSED(use16bit
) ) const
198 wxFont
*fontToUse
= theFont
;
200 fontToUse
= (wxFont
*) &m_font
;
204 HDC dc = GetDC(NULL);
208 GetTextExtentPoint(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
209 GetTextMetrics(dc, &tm);
218 *descent = tm.tmDescent;
219 if ( externalLeading )
220 *externalLeading = tm.tmExternalLeading;
224 wxMetafile
*wxMetafileDC::Close(void)
226 SelectOldObjects(m_hDC
);
227 HANDLE mf
= 0; // TODO: CloseMetaFile((HDC) m_hDC);
231 wxMetafile
*wx_mf
= new wxMetafile
;
232 wx_mf
->SetHMETAFILE((WXHANDLE
) mf
);
233 wx_mf
->SetWindowsMappingMode(m_windowsMappingMode
);
239 void wxMetafileDC::SetMapMode(int mode
)
241 m_mappingMode
= mode
;
243 // int pixel_width = 0;
244 // int pixel_height = 0;
246 // int mm_height = 0;
248 float mm2pixelsX
= 10.0;
249 float mm2pixelsY
= 10.0;
255 m_logicalScaleX
= (float)(twips2mm
* mm2pixelsX
);
256 m_logicalScaleY
= (float)(twips2mm
* mm2pixelsY
);
261 m_logicalScaleX
= (float)(pt2mm
* mm2pixelsX
);
262 m_logicalScaleY
= (float)(pt2mm
* mm2pixelsY
);
267 m_logicalScaleX
= mm2pixelsX
;
268 m_logicalScaleY
= mm2pixelsY
;
273 m_logicalScaleX
= (float)(mm2pixelsX
/10.0);
274 m_logicalScaleY
= (float)(mm2pixelsY
/10.0);
280 m_logicalScaleX
= 1.0;
281 m_logicalScaleY
= 1.0;
298 struct mfPLACEABLEHEADER
{
307 struct mfPLACEABLEHEADER
{
318 * Pass filename of existing non-placeable metafile, and bounding box.
319 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
320 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
324 bool wxMakeMetafilePlaceable(const wxString
& filename
, float scale
)
326 return wxMakeMetafilePlaceable(filename
, 0, 0, 0, 0, scale
, FALSE
);
329 bool wxMakeMetafilePlaceable(const wxString
& WXUNUSED(filename
),
334 float WXUNUSED(scale
),
335 bool WXUNUSED(useOriginAndExtent
))
337 // TODO: the OS/2 PM/MM way to do this
339 // I'm not sure if this is the correct way of suggesting a scale
340 // to the client application, but it's the only way I can find.
341 int unitsPerInch = (int)(576/scale);
343 mfPLACEABLEHEADER header;
344 header.key = 0x9AC6CDD7L;
346 header.bbox.xLeft = (int)(x1);
347 header.bbox.yTop = (int)(y1);
348 header.bbox.xRight = (int)(x2);
349 header.bbox.yBottom = (int)(y2);
350 header.inch = unitsPerInch;
353 // Calculate checksum
355 mfPLACEABLEHEADER *pMFHead = &header;
356 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0; p < (WORD *)&pMFHead ->checksum; ++p)
357 pMFHead ->checksum ^= *p;
359 FILE *fd = fopen(filename.fn_str(), "rb");
363 wxChar tempFileBuf[256];
364 wxGetTempFileName(wxT("mf"), tempFileBuf);
365 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
368 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
370 // Calculate origin and extent
373 int extentX = x2 - x1;
374 int extentY = (y2 - y1);
376 // Read metafile header and write
377 METAHEADER metaHeader;
378 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
380 if (useOriginAndExtent)
381 metaHeader.mtSize += 15;
383 metaHeader.mtSize += 5;
385 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
387 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
389 char originBuffer[10];
390 char extentBuffer[10];
391 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
393 METARECORD *originRecord = (METARECORD *)&originBuffer;
394 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
396 modeRecord->rdSize = 4;
397 modeRecord->rdFunction = META_SETMAPMODE;
398 modeRecord->rdParm[0] = MM_ANISOTROPIC;
400 originRecord->rdSize = 5;
401 originRecord->rdFunction = META_SETWINDOWORG;
402 originRecord->rdParm[0] = originY;
403 originRecord->rdParm[1] = originX;
405 extentRecord->rdSize = 5;
406 extentRecord->rdFunction = META_SETWINDOWEXT;
407 extentRecord->rdParm[0] = extentY;
408 extentRecord->rdParm[1] = extentX;
410 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
412 if (useOriginAndExtent)
414 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
415 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
429 wxRemoveFile(filename);
430 wxCopyFile(tempFileBuf, filename);
431 wxRemoveFile(tempFileBuf);
436 #endif // wxUSE_METAFILE