]> git.saurik.com Git - wxWidgets.git/blob - src/msw/metafile.cpp
In GetTextColour, use GetDefaultAttributes instead of ListView_GetTextColor because...
[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 wxMetafile *wxMetafileDC::Close()
247 {
248 SelectOldObjects(m_hDC);
249 HANDLE mf = CloseMetaFile((HDC) m_hDC);
250 m_hDC = 0;
251 if (mf)
252 {
253 wxMetafile *wx_mf = new wxMetafile;
254 wx_mf->SetHMETAFILE((WXHANDLE) mf);
255 wx_mf->SetWindowsMappingMode(m_windowsMappingMode);
256 return wx_mf;
257 }
258 return NULL;
259 }
260
261 void wxMetafileDC::SetMapMode(int mode)
262 {
263 m_mappingMode = mode;
264
265 // int pixel_width = 0;
266 // int pixel_height = 0;
267 // int mm_width = 0;
268 // int mm_height = 0;
269
270 float mm2pixelsX = 10.0;
271 float mm2pixelsY = 10.0;
272
273 switch (mode)
274 {
275 case wxMM_TWIPS:
276 {
277 m_logicalScaleX = (float)(twips2mm * mm2pixelsX);
278 m_logicalScaleY = (float)(twips2mm * mm2pixelsY);
279 break;
280 }
281 case wxMM_POINTS:
282 {
283 m_logicalScaleX = (float)(pt2mm * mm2pixelsX);
284 m_logicalScaleY = (float)(pt2mm * mm2pixelsY);
285 break;
286 }
287 case wxMM_METRIC:
288 {
289 m_logicalScaleX = mm2pixelsX;
290 m_logicalScaleY = mm2pixelsY;
291 break;
292 }
293 case wxMM_LOMETRIC:
294 {
295 m_logicalScaleX = (float)(mm2pixelsX/10.0);
296 m_logicalScaleY = (float)(mm2pixelsY/10.0);
297 break;
298 }
299 default:
300 case wxMM_TEXT:
301 {
302 m_logicalScaleX = 1.0;
303 m_logicalScaleY = 1.0;
304 break;
305 }
306 }
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxMakeMetafilePlaceable
311 // ----------------------------------------------------------------------------
312
313 #ifdef __WIN32__
314 struct RECT32
315 {
316 short left;
317 short top;
318 short right;
319 short bottom;
320 };
321
322 struct mfPLACEABLEHEADER {
323 DWORD key;
324 short hmf;
325 RECT32 bbox;
326 WORD inch;
327 DWORD reserved;
328 WORD checksum;
329 };
330 #else
331 struct mfPLACEABLEHEADER {
332 DWORD key;
333 HANDLE hmf;
334 RECT bbox;
335 WORD inch;
336 DWORD reserved;
337 WORD checksum;
338 };
339 #endif
340
341 /*
342 * Pass filename of existing non-placeable metafile, and bounding box.
343 * Adds a placeable metafile header, sets the mapping mode to anisotropic,
344 * and sets the window origin and extent to mimic the wxMM_TEXT mapping mode.
345 *
346 */
347
348 bool wxMakeMetafilePlaceable(const wxString& filename, float scale)
349 {
350 return wxMakeMetafilePlaceable(filename, 0, 0, 0, 0, scale, false);
351 }
352
353 bool wxMakeMetafilePlaceable(const wxString& filename, int x1, int y1, int x2, int y2, float scale, bool useOriginAndExtent)
354 {
355 // I'm not sure if this is the correct way of suggesting a scale
356 // to the client application, but it's the only way I can find.
357 int unitsPerInch = (int)(576/scale);
358
359 mfPLACEABLEHEADER header;
360 header.key = 0x9AC6CDD7L;
361 header.hmf = 0;
362 header.bbox.left = (int)(x1);
363 header.bbox.top = (int)(y1);
364 header.bbox.right = (int)(x2);
365 header.bbox.bottom = (int)(y2);
366 header.inch = unitsPerInch;
367 header.reserved = 0;
368
369 // Calculate checksum
370 WORD *p;
371 mfPLACEABLEHEADER *pMFHead = &header;
372 for (p =(WORD *)pMFHead,pMFHead -> checksum = 0;
373 p < (WORD *)&pMFHead ->checksum; ++p)
374 pMFHead ->checksum ^= *p;
375
376 FILE *fd = wxFopen(filename.fn_str(), _T("rb"));
377 if (!fd) return false;
378
379 wxChar tempFileBuf[256];
380 wxGetTempFileName(wxT("mf"), tempFileBuf);
381 FILE *fHandle = wxFopen(wxFNCONV(tempFileBuf), _T("wb"));
382 if (!fHandle)
383 return false;
384 fwrite((void *)&header, sizeof(unsigned char), sizeof(mfPLACEABLEHEADER), fHandle);
385
386 // Calculate origin and extent
387 int originX = x1;
388 int originY = y1;
389 int extentX = x2 - x1;
390 int extentY = (y2 - y1);
391
392 // Read metafile header and write
393 METAHEADER metaHeader;
394 fread((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fd);
395
396 if (useOriginAndExtent)
397 metaHeader.mtSize += 15;
398 else
399 metaHeader.mtSize += 5;
400
401 fwrite((void *)&metaHeader, sizeof(unsigned char), sizeof(metaHeader), fHandle);
402
403 // Write SetMapMode, SetWindowOrigin and SetWindowExt records
404 char modeBuffer[8];
405 char originBuffer[10];
406 char extentBuffer[10];
407 METARECORD *modeRecord = (METARECORD *)&modeBuffer;
408
409 METARECORD *originRecord = (METARECORD *)&originBuffer;
410 METARECORD *extentRecord = (METARECORD *)&extentBuffer;
411
412 modeRecord->rdSize = 4;
413 modeRecord->rdFunction = META_SETMAPMODE;
414 modeRecord->rdParm[0] = MM_ANISOTROPIC;
415
416 originRecord->rdSize = 5;
417 originRecord->rdFunction = META_SETWINDOWORG;
418 originRecord->rdParm[0] = originY;
419 originRecord->rdParm[1] = originX;
420
421 extentRecord->rdSize = 5;
422 extentRecord->rdFunction = META_SETWINDOWEXT;
423 extentRecord->rdParm[0] = extentY;
424 extentRecord->rdParm[1] = extentX;
425
426 fwrite((void *)modeBuffer, sizeof(char), 8, fHandle);
427
428 if (useOriginAndExtent)
429 {
430 fwrite((void *)originBuffer, sizeof(char), 10, fHandle);
431 fwrite((void *)extentBuffer, sizeof(char), 10, fHandle);
432 }
433
434 int ch = -2;
435 while (ch != EOF)
436 {
437 ch = getc(fd);
438 if (ch != EOF)
439 {
440 putc(ch, fHandle);
441 }
442 }
443 fclose(fHandle);
444 fclose(fd);
445 wxRemoveFile(filename);
446 wxCopyFile(tempFileBuf, filename);
447 wxRemoveFile(tempFileBuf);
448 return true;
449 }
450
451
452 #if wxUSE_DRAG_AND_DROP
453
454 // ----------------------------------------------------------------------------
455 // wxMetafileDataObject
456 // ----------------------------------------------------------------------------
457
458 size_t wxMetafileDataObject::GetDataSize() const
459 {
460 return sizeof(METAFILEPICT);
461 }
462
463 bool wxMetafileDataObject::GetDataHere(void *buf) const
464 {
465 METAFILEPICT *mfpict = (METAFILEPICT *)buf;
466 const wxMetafile& mf = GetMetafile();
467
468 wxCHECK_MSG( mf.GetHMETAFILE(), false, _T("copying invalid metafile") );
469
470 // doesn't seem to work with any other mapping mode...
471 mfpict->mm = MM_ANISOTROPIC; //mf.GetWindowsMappingMode();
472 mfpict->xExt = mf.GetWidth();
473 mfpict->yExt = mf.GetHeight();
474
475 // transform the picture size to HIMETRIC units (0.01mm) - as we don't know
476 // what DC the picture will be rendered to, use the default display one
477 PixelToHIMETRIC(&mfpict->xExt, &mfpict->yExt);
478
479 mfpict->hMF = CopyMetaFile((HMETAFILE)mf.GetHMETAFILE(), NULL);
480
481 return true;
482 }
483
484 bool wxMetafileDataObject::SetData(size_t WXUNUSED(len), const void *buf)
485 {
486 const METAFILEPICT *mfpict = (const METAFILEPICT *)buf;
487
488 wxMetafile mf;
489 mf.SetWindowsMappingMode(mfpict->mm);
490
491 LONG w = mfpict->xExt,
492 h = mfpict->yExt;
493 if ( mfpict->mm == MM_ANISOTROPIC )
494 {
495 // in this case xExt and yExt contain suggested size in HIMETRIC units
496 // (0.01 mm) - transform this to something more reasonable (pixels)
497 HIMETRICToPixel(&w, &h);
498 }
499
500 mf.SetWidth(w);
501 mf.SetHeight(h);
502 mf.SetHMETAFILE((WXHANDLE)mfpict->hMF);
503
504 wxCHECK_MSG( mfpict->hMF, false, _T("pasting invalid metafile") );
505
506 SetMetafile(mf);
507
508 return true;
509 }
510
511 #endif // wxUSE_DRAG_AND_DROP
512
513 #endif // wxUSE_METAFILE
514