]> git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
call Create from wxGrid non-default ctor instead to ensure both methods of creation...
[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 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #endif
31
32 #include "wx/metafile.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 = wxMM_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 = wxMM_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(*wx_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(_T("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 (!file.IsNull() && wxFileExists(file))
180 wxRemoveFile(file);
181
182 if (!file.IsNull() && (file != wxEmptyString))
183 m_hDC = (WXHDC) CreateMetaFile(file);
184 else
185 m_hDC = (WXHDC) CreateMetaFile(NULL);
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 : file.wx_str());
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 = wxMM_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, _T("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(int 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(), _T("rb"));
393 if (!fd) return false;
394
395 wxChar tempFileBuf[256];
396 wxGetTempFileName(wxT("mf"), tempFileBuf);
397 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
398 if (!fHandle)
399 return false;
400 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
401
402 // Calculate origin and extent
403 int originX = x1;
404 int originY = y1;
405 int extentX = x2 - x1;
406 int extentY = (y2 - y1);
407
408 // Read metafile header and write
409 METAHEADER metaHeader;
410 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
411
412 if (useOriginAndExtent)
413 metaHeader.mtSize += 15;
414 else
415 metaHeader.mtSize += 5;
416
417 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
418
419 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
420 char modeBuffer[8];
421 char originBuffer[10];
422 char extentBuffer[10];
423 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
424
425 METARECORD *originRecord = (METARECORD *)&originBuffer;
426 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
427
428 modeRecord->rdSize = 4;
429 modeRecord->rdFunction = META_SETMAPMODE;
430 modeRecord->rdParm[0] = MM_ANISOTROPIC;
431
432 originRecord->rdSize = 5;
433 originRecord->rdFunction = META_SETWINDOWORG;
434 originRecord->rdParm[0] = originY;
435 originRecord->rdParm[1] = originX;
436
437 extentRecord->rdSize = 5;
438 extentRecord->rdFunction = META_SETWINDOWEXT;
439 extentRecord->rdParm[0] = extentY;
440 extentRecord->rdParm[1] = extentX;
441
442 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
443
444 if (useOriginAndExtent)
445 {
446 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
447 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
448 }
449
450 int ch = -2;
451 while (ch != EOF)
452 {
453 ch = getc(fd);
454 if (ch != EOF)
455 {
456 putc(ch, fHandle);
457 }
458 }
459 fclose(fHandle);
460 fclose(fd);
461 wxRemoveFile(filename);
462 wxCopyFile(tempFileBuf, filename);
463 wxRemoveFile(tempFileBuf);
464 return true;
465 }
466
467
468 #if wxUSE_DRAG_AND_DROP
469
470 // ----------------------------------------------------------------------------
471 // wxMetafileDataObject
472 // ----------------------------------------------------------------------------
473
474 size_t wxMetafileDataObject::GetDataSize() const
475 {
476 return sizeof(METAFILEPICT);
477 }
478
479 bool wxMetafileDataObject::GetDataHere(void *buf) const
480 {
481 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
482 const wxMetafile& mf = GetMetafile();
483
484 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
485
486 // doesn't seem to work with any other mapping mode...
487 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
488 mfpict->xExt = mf.GetWidth();
489 mfpict->yExt = mf.GetHeight();
490
491 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
492 // what DC the picture will be rendered to, use the default display one
493 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
494
495 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
496
497 return true;
498 }
499
500 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
501 {
502 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
503
504 wxMetafile mf;
505 mf.SetWindowsMappingMode(mfpict->mm);
506
507 LONG w = mfpict->xExt,
508 h = mfpict->yExt;
509 if ( mfpict->mm == MM_ANISOTROPIC )
510 {
511 // in this case xExt and yExt contain suggested size in HIMETRIC units
512 // (0.01 mm) - transform this to something more reasonable (pixels)
513 HIMETRICToPixel(&w, &h);
514 }
515
516 mf.SetWidth(w);
517 mf.SetHeight(h);
518 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
519
520 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
521
522 SetMetafile(mf);
523
524 return true;
525 }
526
527 #endif // wxUSE_DRAG_AND_DROP
528
529 #endif // wxUSE_METAFILE