]> git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / msw / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/metafile.cpp
3 // Purpose: wxMetafileDC etc.
4 // Author: Julian Smart
5 // Modified by: VZ 07.01.00: implemented wxMetaFileDataObject
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #endif
30
31 #include "wx/metafile.h"
32 #include "wx/filename.h"
33
34 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
35
36 #include "wx/clipbrd.h"
37 #include "wx/msw/private.h"
38
39 #include <stdio.h>
40 #include <string.h>
41
42 // ----------------------------------------------------------------------------
43 // wxWin macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
47 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxMetafileRefData
55 // ----------------------------------------------------------------------------
56
57 /*
58 * Metafiles
59 * Currently, the only purpose for making a metafile is to put
60 * it on the clipboard.
61 */
62
63 wxMetafileRefData::wxMetafileRefData()
64 {
65 m_metafile = 0;
66 m_windowsMappingMode = MM_ANISOTROPIC;
67 m_width = m_height = 0;
68 }
69
70 wxMetafileRefData::~wxMetafileRefData()
71 {
72 if (m_metafile)
73 {
74 DeleteMetaFile((HMETAFILE) m_metafile);
75 m_metafile = 0;
76 }
77 }
78
79 // ----------------------------------------------------------------------------
80 // wxMetafile
81 // ----------------------------------------------------------------------------
82
83 wxMetafile::wxMetafile(const wxString& file)
84 {
85 m_refData = new wxMetafileRefData;
86
87 M_METAFILEDATA->m_windowsMappingMode = MM_ANISOTROPIC;
88 M_METAFILEDATA->m_metafile = 0;
89 if (!file.empty())
90 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
91 }
92
93 wxMetafile::~wxMetafile()
94 {
95 }
96
97 wxGDIRefData *wxMetafile::CreateGDIRefData() const
98 {
99 return new wxMetafileRefData;
100 }
101
102 wxGDIRefData *wxMetafile::CloneGDIRefData(const wxGDIRefData *data) const
103 {
104 return new wxMetafileRefData(*static_cast<const wxMetafileRefData *>(data));
105 }
106
107 bool wxMetafile::SetClipboard(int width, int height)
108 {
109 #if !wxUSE_CLIPBOARD
110 return false;
111 #else
112 if (!m_refData)
113 return false;
114
115 bool alreadyOpen = wxClipboardOpen();
116 if (!alreadyOpen)
117 {
118 wxOpenClipboard();
119 if (!wxEmptyClipboard())
120 return false;
121 }
122 bool success = wxSetClipboardData(wxDF_METAFILE, this, width,height);
123 if (!alreadyOpen)
124 wxCloseClipboard();
125
126 return success;
127 #endif
128 }
129
130 bool wxMetafile::Play(wxDC *dc)
131 {
132 if (!m_refData)
133 return false;
134
135 if (dc->GetHDC() && M_METAFILEDATA->m_metafile)
136 {
137 if ( !::PlayMetaFile(GetHdcOf(*dc), (HMETAFILE)
138 M_METAFILEDATA->m_metafile) )
139 {
140 wxLogLastError(wxT("PlayMetaFile"));
141 }
142 }
143
144 return true;
145 }
146
147 void wxMetafile::SetHMETAFILE(WXHANDLE mf)
148 {
149 if (!m_refData)
150 m_refData = new wxMetafileRefData;
151
152 M_METAFILEDATA->m_metafile = mf;
153 }
154
155 void wxMetafile::SetWindowsMappingMode(int mm)
156 {
157 if (!m_refData)
158 m_refData = new wxMetafileRefData;
159
160 M_METAFILEDATA->m_windowsMappingMode = mm;
161 }
162
163 // ----------------------------------------------------------------------------
164 // Metafile device context
165 // ----------------------------------------------------------------------------
166
167 // Original constructor that does not takes origin and extent. If you use this,
168 // *DO* give origin/extent arguments to wxMakeMetafilePlaceable.
169 wxMetafileDCImpl::wxMetafileDCImpl(wxDC *owner, const wxString& file)
170 : wxMSWDCImpl(owner)
171 {
172 m_metaFile = NULL;
173 m_minX = 10000;
174 m_minY = 10000;
175 m_maxX = -10000;
176 m_maxY = -10000;
177 // m_title = NULL;
178
179 if ( wxFileExists(file) )
180 wxRemoveFile(file);
181
182 if ( file.empty() )
183 m_hDC = (WXHDC) CreateMetaFile(NULL);
184 else
185 m_hDC = (WXHDC) CreateMetaFile(file);
186
187 m_ok = (m_hDC != (WXHDC) 0) ;
188
189 // Actual Windows mapping mode, for future reference.
190 m_windowsMappingMode = wxMM_TEXT;
191
192 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
193 }
194
195 // New constructor that takes origin and extent. If you use this, don't
196 // give origin/extent arguments to wxMakeMetafilePlaceable.
197 wxMetafileDCImpl::wxMetafileDCImpl(wxDC *owner, const wxString& file,
198 int xext, int yext, int xorg, int yorg)
199 : wxMSWDCImpl(owner)
200 {
201 m_minX = 10000;
202 m_minY = 10000;
203 m_maxX = -10000;
204 m_maxY = -10000;
205 if ( !file.empty() && wxFileExists(file) )
206 wxRemoveFile(file);
207 m_hDC = (WXHDC) CreateMetaFile(file.empty() ? NULL : wxMSW_CONV_LPCTSTR(file));
208
209 m_ok = true;
210
211 ::SetWindowOrgEx((HDC) m_hDC,xorg,yorg, NULL);
212 ::SetWindowExtEx((HDC) m_hDC,xext,yext, NULL);
213
214 // Actual Windows mapping mode, for future reference.
215 m_windowsMappingMode = MM_ANISOTROPIC;
216
217 SetMapMode(wxMM_TEXT); // NOTE: does not set HDC mapmode (this is correct)
218 }
219
220 wxMetafileDCImpl::~wxMetafileDCImpl()
221 {
222 m_hDC = 0;
223 }
224
225 void wxMetafileDCImpl::DoGetTextExtent(const wxString& string,
226 wxCoord *x, wxCoord *y,
227 wxCoord *descent, wxCoord *externalLeading,
228 const wxFont *theFont) const
229 {
230 const wxFont *fontToUse = theFont;
231 if (!fontToUse)
232 fontToUse = &m_font;
233
234 ScreenHDC dc;
235 SelectInHDC selFont(dc, GetHfontOf(*fontToUse));
236
237 SIZE sizeRect;
238 TEXTMETRIC tm;
239 ::GetTextExtentPoint32(dc, WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
240 ::GetTextMetrics(dc, &tm);
241
242 if ( x )
243 *x = sizeRect.cx;
244 if ( y )
245 *y = sizeRect.cy;
246 if ( descent )
247 *descent = tm.tmDescent;
248 if ( externalLeading )
249 *externalLeading = tm.tmExternalLeading;
250 }
251
252 void wxMetafileDCImpl::DoGetSize(int *width, int *height) const
253 {
254 wxCHECK_RET( m_refData, wxT("invalid wxMetafileDC") );
255
256 if ( width )
257 *width = M_METAFILEDATA->m_width;
258 if ( height )
259 *height = M_METAFILEDATA->m_height;
260 }
261
262 wxMetafile *wxMetafileDCImpl::Close()
263 {
264 SelectOldObjects(m_hDC);
265 HANDLE mf = CloseMetaFile((HDC) m_hDC);
266 m_hDC = 0;
267 if (mf)
268 {
269 wxMetafile *wx_mf = new wxMetafile;
270 wx_mf->SetHMETAFILE((WXHANDLE) mf);
271 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
272 return wx_mf;
273 }
274 return NULL;
275 }
276
277 void wxMetafileDCImpl::SetMapMode(wxMappingMode mode)
278 {
279 m_mappingMode = mode;
280
281 // int pixel_width = 0;
282 // int pixel_height = 0;
283 // int mm_width = 0;
284 // int mm_height = 0;
285
286 float mm2pixelsX = 10.0;
287 float mm2pixelsY = 10.0;
288
289 switch (mode)
290 {
291 case wxMM_TWIPS:
292 {
293 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
294 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
295 break;
296 }
297 case wxMM_POINTS:
298 {
299 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
300 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
301 break;
302 }
303 case wxMM_METRIC:
304 {
305 m_logicalScaleX = mm2pixelsX;
306 m_logicalScaleY = mm2pixelsY;
307 break;
308 }
309 case wxMM_LOMETRIC:
310 {
311 m_logicalScaleX = (float)(mm2pixelsX/10.0);
312 m_logicalScaleY = (float)(mm2pixelsY/10.0);
313 break;
314 }
315 default:
316 case wxMM_TEXT:
317 {
318 m_logicalScaleX = 1.0;
319 m_logicalScaleY = 1.0;
320 break;
321 }
322 }
323 }
324
325 // ----------------------------------------------------------------------------
326 // wxMakeMetafilePlaceable
327 // ----------------------------------------------------------------------------
328
329 #ifdef __WIN32__
330 struct RECT32
331 {
332 short left;
333 short top;
334 short right;
335 short bottom;
336 };
337
338 struct mfPLACEABLEHEADER {
339 DWORD key;
340 short hmf;
341 RECT32 bbox;
342 WORD inch;
343 DWORD reserved;
344 WORD checksum;
345 };
346 #else
347 struct mfPLACEABLEHEADER {
348 DWORD key;
349 HANDLE hmf;
350 RECT bbox;
351 WORD inch;
352 DWORD reserved;
353 WORD checksum;
354 };
355 #endif
356
357 /*
358 * Pass filename of existing non-placeable metafile, and bounding box.
359 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
360 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
361 *
362 */
363
364 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
365 {
366 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false);
367 }
368
369 bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
370 {
371 // I'm not sure if this is the correct way of suggesting a scale
372 // to the client application, but it's the only way I can find.
373 int unitsPerInch = (int)(576/scale);
374
375 mfPLACEABLEHEADER header;
376 header.key = 0x9AC6CDD7L;
377 header.hmf = 0;
378 header.bbox.left = (int)(x1);
379 header.bbox.top = (int)(y1);
380 header.bbox.right = (int)(x2);
381 header.bbox.bottom = (int)(y2);
382 header.inch = unitsPerInch;
383 header.reserved = 0;
384
385 // Calculate checksum
386 WORD *p;
387 mfPLACEABLEHEADER *pMFHead = &header;
388 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
389 p < (WORD *)&pMFHead ->checksum; ++p)
390 pMFHead ->checksum ^= *p;
391
392 FILE *fd = wxFopen(filename.fn_str(), wxT("rb"));
393 if (!fd) return false;
394
395 wxString tempFileBuf = wxFileName::CreateTempFileName(wxT("mf"));
396 if (tempFileBuf.empty())
397 return false;
398
399 FILE *fHandle = wxFopen(tempFileBuf.fn_str(), wxT("wb"));
400 if (!fHandle)
401 return false;
402 fwrite((void *)&header, 1, sizeof(mfPLACEABLEHEADER), fHandle);
403
404 // Calculate origin and extent
405 int originX = x1;
406 int originY = y1;
407 int extentX = x2 - x1;
408 int extentY = (y2 - y1);
409
410 // Read metafile header and write
411 METAHEADER metaHeader;
412 fread((void *)&metaHeader, 1, sizeof(metaHeader), fd);
413
414 if (useOriginAndExtent)
415 metaHeader.mtSize += 15;
416 else
417 metaHeader.mtSize += 5;
418
419 fwrite((void *)&metaHeader, 1, sizeof(metaHeader), fHandle);
420
421 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
422 char modeBuffer[8];
423 char originBuffer[10];
424 char extentBuffer[10];
425 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
426
427 METARECORD *originRecord = (METARECORD *)&originBuffer;
428 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
429
430 modeRecord->rdSize = 4;
431 modeRecord->rdFunction = META_SETMAPMODE;
432 modeRecord->rdParm[0] = MM_ANISOTROPIC;
433
434 originRecord->rdSize = 5;
435 originRecord->rdFunction = META_SETWINDOWORG;
436 originRecord->rdParm[0] = originY;
437 originRecord->rdParm[1] = originX;
438
439 extentRecord->rdSize = 5;
440 extentRecord->rdFunction = META_SETWINDOWEXT;
441 extentRecord->rdParm[0] = extentY;
442 extentRecord->rdParm[1] = extentX;
443
444 fwrite((void *)modeBuffer, 1, 8, fHandle);
445
446 if (useOriginAndExtent)
447 {
448 fwrite((void *)originBuffer, 1, 10, fHandle);
449 fwrite((void *)extentBuffer, 1, 10, fHandle);
450 }
451
452 int ch = -2;
453 while (ch != EOF)
454 {
455 ch = getc(fd);
456 if (ch != EOF)
457 {
458 putc(ch, fHandle);
459 }
460 }
461 fclose(fHandle);
462 fclose(fd);
463 wxRemoveFile(filename);
464 wxCopyFile(tempFileBuf, filename);
465 wxRemoveFile(tempFileBuf);
466 return true;
467 }
468
469
470 #if wxUSE_DRAG_AND_DROP
471
472 // ----------------------------------------------------------------------------
473 // wxMetafileDataObject
474 // ----------------------------------------------------------------------------
475
476 size_t wxMetafileDataObject::GetDataSize() const
477 {
478 return sizeof(METAFILEPICT);
479 }
480
481 bool wxMetafileDataObject::GetDataHere(void *buf) const
482 {
483 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
484 const wxMetafile& mf = GetMetafile();
485
486 wxCHECK_MSG( mf.GetHMETAFILE(), false, wxT("copying invalid metafile") );
487
488 // doesn't seem to work with any other mapping mode...
489 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
490 mfpict->xExt = mf.GetWidth();
491 mfpict->yExt = mf.GetHeight();
492
493 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
494 // what DC the picture will be rendered to, use the default display one
495 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
496
497 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
498
499 return true;
500 }
501
502 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
503 {
504 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
505
506 wxMetafile mf;
507 mf.SetWindowsMappingMode(mfpict->mm);
508
509 LONG w = mfpict->xExt,
510 h = mfpict->yExt;
511 if ( mfpict->mm == MM_ANISOTROPIC )
512 {
513 // in this case xExt and yExt contain suggested size in HIMETRIC units
514 // (0.01 mm) - transform this to something more reasonable (pixels)
515 HIMETRICToPixel(&w, &h);
516 }
517
518 mf.SetWidth(w);
519 mf.SetHeight(h);
520 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
521
522 wxCHECK_MSG( mfpict->hMF, false, wxT("pasting invalid metafile") );
523
524 SetMetafile(mf);
525
526 return true;
527 }
528
529 #endif // wxUSE_DRAG_AND_DROP
530
531 #endif // wxUSE_METAFILE