]>
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 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxMetafile
, wxObject
)
37 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC
, wxDC
)
42 * Currently, the only purpose for making a metafile is to put
43 * it on the clipboard.
46 wxMetafileRefData::wxMetafileRefData(void)
49 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
52 wxMetafileRefData::~wxMetafileRefData(void)
56 // TODO: DeleteMetaFile((HMETAFILE) m_metafile);
61 wxMetafile::wxMetafile(const wxString
& file
)
63 m_refData
= new wxMetafileRefData
;
65 M_METAFILEDATA
->m_windowsMappingMode
= wxMM_ANISOTROPIC
;
66 M_METAFILEDATA
->m_metafile
= 0;
67 if (!file
.IsNull() && (file
.Cmp(wxT("")) == 0))
68 M_METAFILEDATA
->m_metafile
= (WXHANDLE
)0; // TODO: GetMetaFile(file);
71 wxMetafile::~wxMetafile(void)
75 bool wxMetafile::SetClipboard(int width
, int height
)
80 bool alreadyOpen
=wxClipboardOpen();
84 if (!wxEmptyClipboard()) return FALSE
;
86 bool success
= wxSetClipboardData(wxDF_METAFILE
, this, width
,height
);
87 if (!alreadyOpen
) wxCloseClipboard();
88 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
))
143 if (!file.IsNull() && (file != wxT("")))
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
, int xext
, int yext
, int xorg
, int yorg
)
165 if (file
!= wxT("") && wxFileExists(file
)) wxRemoveFile(file
);
166 // m_hDC = (WXHDC) CreateMetaFile(file);
170 // ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
171 // ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
173 // Actual Windows mapping mode, for future reference.
174 m_windowsMappingMode
= wxMM_ANISOTROPIC
;
176 SetMapMode(wxMM_TEXT
); // NOTE: does not set HDC mapmode (this is correct)
179 wxMetafileDC::~wxMetafileDC(void)
184 void wxMetafileDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
185 long *descent
, long *externalLeading
, wxFont
*theFont
, bool use16bit
) const
187 wxFont
*fontToUse
= theFont
;
189 fontToUse
= (wxFont
*) &m_font
;
193 HDC dc = GetDC(NULL);
197 GetTextExtentPoint(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
198 GetTextMetrics(dc, &tm);
207 *descent = tm.tmDescent;
208 if ( externalLeading )
209 *externalLeading = tm.tmExternalLeading;
213 wxMetafile
*wxMetafileDC::Close(void)
215 SelectOldObjects(m_hDC
);
216 HANDLE mf
= 0; // TODO: 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 // TODO: the OS/2 PM/MM way to do this
322 // I'm not sure if this is the correct way of suggesting a scale
323 // to the client application, but it's the only way I can find.
324 int unitsPerInch = (int)(576/scale);
326 mfPLACEABLEHEADER header;
327 header.key = 0x9AC6CDD7L;
329 header.bbox.xLeft = (int)(x1);
330 header.bbox.yTop = (int)(y1);
331 header.bbox.xRight = (int)(x2);
332 header.bbox.yBottom = (int)(y2);
333 header.inch = unitsPerInch;
336 // Calculate checksum
338 mfPLACEABLEHEADER *pMFHead = &header;
339 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
340 p < (WORD *)&pMFHead ->checksum; ++p)
341 pMFHead ->checksum ^= *p;
343 FILE *fd = fopen(filename.fn_str(), "rb");
344 if (!fd) return FALSE;
346 wxChar tempFileBuf[256];
347 wxGetTempFileName(wxT("mf"), tempFileBuf);
348 FILE *fHandle = fopen(wxConvFile.cWX2MB(tempFileBuf), "wb");
351 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
353 // Calculate origin and extent
356 int extentX = x2 - x1;
357 int extentY = (y2 - y1);
359 // Read metafile header and write
360 METAHEADER metaHeader;
361 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
363 if (useOriginAndExtent)
364 metaHeader.mtSize += 15;
366 metaHeader.mtSize += 5;
368 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
370 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
372 char originBuffer[10];
373 char extentBuffer[10];
374 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
376 METARECORD *originRecord = (METARECORD *)&originBuffer;
377 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
379 modeRecord->rdSize = 4;
380 modeRecord->rdFunction = META_SETMAPMODE;
381 modeRecord->rdParm[0] = MM_ANISOTROPIC;
383 originRecord->rdSize = 5;
384 originRecord->rdFunction = META_SETWINDOWORG;
385 originRecord->rdParm[0] = originY;
386 originRecord->rdParm[1] = originX;
388 extentRecord->rdSize = 5;
389 extentRecord->rdFunction = META_SETWINDOWEXT;
390 extentRecord->rdParm[0] = extentY;
391 extentRecord->rdParm[1] = extentX;
393 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
395 if (useOriginAndExtent)
397 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
398 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
412 wxRemoveFile(filename);
413 wxCopyFile(tempFileBuf, filename);
414 wxRemoveFile(tempFileBuf);
419 #endif // wxUSE_METAFILE