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