xref: /reactos/win32ss/gdi/gdi32/wine/enhmfdrv/init.c (revision 333b8f19)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Enhanced MetaFile driver initialisation functions
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 1999 Huw D M Davies
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21c2c66affSColin Finck #include <assert.h>
22c2c66affSColin Finck #include <stdarg.h>
23c2c66affSColin Finck #include <string.h>
24c2c66affSColin Finck 
25c2c66affSColin Finck #include "windef.h"
26c2c66affSColin Finck #include "winbase.h"
27c2c66affSColin Finck #include "wingdi.h"
28c2c66affSColin Finck #include "winnls.h"
29c2c66affSColin Finck #include "gdi_private.h"
30c2c66affSColin Finck #include "enhmfdrv/enhmetafiledrv.h"
31c2c66affSColin Finck #include "wine/debug.h"
32c2c66affSColin Finck 
33c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
34c2c66affSColin Finck 
35c2c66affSColin Finck static BOOL EMFDRV_DeleteDC( PHYSDEV dev );
36c2c66affSColin Finck 
37c2c66affSColin Finck static const struct gdi_dc_funcs emfdrv_driver =
38c2c66affSColin Finck {
39c2c66affSColin Finck     NULL,                            /* pAbortDoc */
40c2c66affSColin Finck     EMFDRV_AbortPath,                /* pAbortPath */
41c2c66affSColin Finck     NULL,                            /* pAlphaBlend */
42c2c66affSColin Finck     EMFDRV_AngleArc,                 /* pAngleArc */
43c2c66affSColin Finck     EMFDRV_Arc,                      /* pArc */
44c2c66affSColin Finck     EMFDRV_ArcTo,                    /* pArcTo */
45c2c66affSColin Finck     EMFDRV_BeginPath,                /* pBeginPath */
46c2c66affSColin Finck     NULL,                            /* pBlendImage */
47c2c66affSColin Finck     EMFDRV_Chord,                    /* pChord */
48c2c66affSColin Finck     EMFDRV_CloseFigure,              /* pCloseFigure */
49c2c66affSColin Finck     NULL,                            /* pCreateCompatibleDC */
50c2c66affSColin Finck     NULL,                            /* pCreateDC */
51c2c66affSColin Finck     EMFDRV_DeleteDC,                 /* pDeleteDC */
52c2c66affSColin Finck     EMFDRV_DeleteObject,             /* pDeleteObject */
53c2c66affSColin Finck     NULL,                            /* pDeviceCapabilities */
54c2c66affSColin Finck     EMFDRV_Ellipse,                  /* pEllipse */
55c2c66affSColin Finck     NULL,                            /* pEndDoc */
56c2c66affSColin Finck     NULL,                            /* pEndPage */
57c2c66affSColin Finck     EMFDRV_EndPath,                  /* pEndPath */
58c2c66affSColin Finck     NULL,                            /* pEnumFonts */
59c2c66affSColin Finck     NULL,                            /* pEnumICMProfiles */
60c2c66affSColin Finck     EMFDRV_ExcludeClipRect,          /* pExcludeClipRect */
61c2c66affSColin Finck     NULL,                            /* pExtDeviceMode */
62c2c66affSColin Finck     NULL,                            /* pExtEscape */
63c2c66affSColin Finck     EMFDRV_ExtFloodFill,             /* pExtFloodFill */
64c2c66affSColin Finck     EMFDRV_ExtSelectClipRgn,         /* pExtSelectClipRgn */
65c2c66affSColin Finck     EMFDRV_ExtTextOut,               /* pExtTextOut */
66c2c66affSColin Finck     EMFDRV_FillPath,                 /* pFillPath */
67c2c66affSColin Finck     EMFDRV_FillRgn,                  /* pFillRgn */
68c2c66affSColin Finck     EMFDRV_FlattenPath,              /* pFlattenPath */
69c2c66affSColin Finck     NULL,                            /* pFontIsLinked */
70c2c66affSColin Finck     EMFDRV_FrameRgn,                 /* pFrameRgn */
71c2c66affSColin Finck     EMFDRV_GdiComment,               /* pGdiComment */
72c2c66affSColin Finck     NULL,                            /* pGetBoundsRect */
73c2c66affSColin Finck     NULL,                            /* pGetCharABCWidths */
74c2c66affSColin Finck     NULL,                            /* pGetCharABCWidthsI */
75c2c66affSColin Finck     NULL,                            /* pGetCharWidth */
76*333b8f19Sjimtabor     NULL,                            /* pGetCharWidthInfo */
77c2c66affSColin Finck     EMFDRV_GetDeviceCaps,            /* pGetDeviceCaps */
78c2c66affSColin Finck     NULL,                            /* pGetDeviceGammaRamp */
79c2c66affSColin Finck     NULL,                            /* pGetFontData */
80c2c66affSColin Finck     NULL,                            /* pGetFontRealizationInfo */
81c2c66affSColin Finck     NULL,                            /* pGetFontUnicodeRanges */
82c2c66affSColin Finck     NULL,                            /* pGetGlyphIndices */
83c2c66affSColin Finck     NULL,                            /* pGetGlyphOutline */
84c2c66affSColin Finck     NULL,                            /* pGetICMProfile */
85c2c66affSColin Finck     NULL,                            /* pGetImage */
86c2c66affSColin Finck     NULL,                            /* pGetKerningPairs */
87c2c66affSColin Finck     NULL,                            /* pGetNearestColor */
88c2c66affSColin Finck     NULL,                            /* pGetOutlineTextMetrics */
89c2c66affSColin Finck     NULL,                            /* pGetPixel */
90c2c66affSColin Finck     NULL,                            /* pGetSystemPaletteEntries */
91c2c66affSColin Finck     NULL,                            /* pGetTextCharsetInfo */
92c2c66affSColin Finck     NULL,                            /* pGetTextExtentExPoint */
93c2c66affSColin Finck     NULL,                            /* pGetTextExtentExPointI */
94c2c66affSColin Finck     NULL,                            /* pGetTextFace */
95c2c66affSColin Finck     NULL,                            /* pGetTextMetrics */
96c2c66affSColin Finck     EMFDRV_GradientFill,             /* pGradientFill */
97c2c66affSColin Finck     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
98c2c66affSColin Finck     EMFDRV_InvertRgn,                /* pInvertRgn */
99c2c66affSColin Finck     EMFDRV_LineTo,                   /* pLineTo */
100c2c66affSColin Finck     EMFDRV_ModifyWorldTransform,     /* pModifyWorldTransform */
101c2c66affSColin Finck     EMFDRV_MoveTo,                   /* pMoveTo */
102c2c66affSColin Finck     EMFDRV_OffsetClipRgn,            /* pOffsetClipRgn */
103c2c66affSColin Finck     EMFDRV_OffsetViewportOrgEx,      /* pOffsetViewportOrgEx */
104c2c66affSColin Finck     EMFDRV_OffsetWindowOrgEx,        /* pOffsetWindowOrgEx */
105c2c66affSColin Finck     EMFDRV_PaintRgn,                 /* pPaintRgn */
106c2c66affSColin Finck     EMFDRV_PatBlt,                   /* pPatBlt */
107c2c66affSColin Finck     EMFDRV_Pie,                      /* pPie */
108c2c66affSColin Finck     EMFDRV_PolyBezier,               /* pPolyBezier */
109c2c66affSColin Finck     EMFDRV_PolyBezierTo,             /* pPolyBezierTo */
110c2c66affSColin Finck     EMFDRV_PolyDraw,                 /* pPolyDraw */
111c2c66affSColin Finck     EMFDRV_PolyPolygon,              /* pPolyPolygon */
112c2c66affSColin Finck     EMFDRV_PolyPolyline,             /* pPolyPolyline */
113c2c66affSColin Finck     EMFDRV_Polygon,                  /* pPolygon */
114c2c66affSColin Finck     EMFDRV_Polyline,                 /* pPolyline */
115c2c66affSColin Finck     EMFDRV_PolylineTo,               /* pPolylineTo */
116c2c66affSColin Finck     NULL,                            /* pPutImage */
117c2c66affSColin Finck     NULL,                            /* pRealizeDefaultPalette */
118c2c66affSColin Finck     NULL,                            /* pRealizePalette */
119c2c66affSColin Finck     EMFDRV_Rectangle,                /* pRectangle */
120c2c66affSColin Finck     NULL,                            /* pResetDC */
121c2c66affSColin Finck     EMFDRV_RestoreDC,                /* pRestoreDC */
122c2c66affSColin Finck     EMFDRV_RoundRect,                /* pRoundRect */
123c2c66affSColin Finck     EMFDRV_SaveDC,                   /* pSaveDC */
124c2c66affSColin Finck     EMFDRV_ScaleViewportExtEx,       /* pScaleViewportExtEx */
125c2c66affSColin Finck     EMFDRV_ScaleWindowExtEx,         /* pScaleWindowExtEx */
126c2c66affSColin Finck     EMFDRV_SelectBitmap,             /* pSelectBitmap */
127c2c66affSColin Finck     EMFDRV_SelectBrush,              /* pSelectBrush */
128c2c66affSColin Finck     EMFDRV_SelectClipPath,           /* pSelectClipPath */
129c2c66affSColin Finck     EMFDRV_SelectFont,               /* pSelectFont */
130c2c66affSColin Finck     EMFDRV_SelectPalette,            /* pSelectPalette */
131c2c66affSColin Finck     EMFDRV_SelectPen,                /* pSelectPen */
132c2c66affSColin Finck     EMFDRV_SetArcDirection,          /* pSetArcDirection */
133c2c66affSColin Finck     EMFDRV_SetBkColor,               /* pSetBkColor */
134c2c66affSColin Finck     EMFDRV_SetBkMode,                /* pSetBkMode */
135c2c66affSColin Finck     NULL,                            /* pSetBoundsRect */
136c2c66affSColin Finck     EMFDRV_SetDCBrushColor,          /* pSetDCBrushColor*/
137c2c66affSColin Finck     EMFDRV_SetDCPenColor,            /* pSetDCPenColor*/
138c2c66affSColin Finck     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
139c2c66affSColin Finck     NULL,                            /* pSetDeviceClipping */
140c2c66affSColin Finck     NULL,                            /* pSetDeviceGammaRamp */
141c2c66affSColin Finck     EMFDRV_SetLayout,                /* pSetLayout */
142c2c66affSColin Finck     EMFDRV_SetMapMode,               /* pSetMapMode */
143c2c66affSColin Finck     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
144c2c66affSColin Finck     EMFDRV_SetPixel,                 /* pSetPixel */
145c2c66affSColin Finck     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
146c2c66affSColin Finck     EMFDRV_SetROP2,                  /* pSetROP2 */
147c2c66affSColin Finck     NULL,                            /* pSetRelAbs */
148c2c66affSColin Finck     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
149c2c66affSColin Finck     EMFDRV_SetTextAlign,             /* pSetTextAlign */
150c2c66affSColin Finck     NULL,                            /* pSetTextCharacterExtra */
151c2c66affSColin Finck     EMFDRV_SetTextColor,             /* pSetTextColor */
152c2c66affSColin Finck     EMFDRV_SetTextJustification,     /* pSetTextJustification */
153c2c66affSColin Finck     EMFDRV_SetViewportExtEx,         /* pSetViewportExtEx */
154c2c66affSColin Finck     EMFDRV_SetViewportOrgEx,         /* pSetViewportOrgEx */
155c2c66affSColin Finck     EMFDRV_SetWindowExtEx,           /* pSetWindowExtEx */
156c2c66affSColin Finck     EMFDRV_SetWindowOrgEx,           /* pSetWindowOrgEx */
157c2c66affSColin Finck     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
158c2c66affSColin Finck     NULL,                            /* pStartDoc */
159c2c66affSColin Finck     NULL,                            /* pStartPage */
160c2c66affSColin Finck     EMFDRV_StretchBlt,               /* pStretchBlt */
161c2c66affSColin Finck     EMFDRV_StretchDIBits,            /* pStretchDIBits */
162c2c66affSColin Finck     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
163c2c66affSColin Finck     EMFDRV_StrokePath,               /* pStrokePath */
164c2c66affSColin Finck     NULL,                            /* pUnrealizePalette */
165c2c66affSColin Finck     EMFDRV_WidenPath,                /* pWidenPath */
166c2c66affSColin Finck     NULL,                            /* wine_get_wgl_driver */
167c2c66affSColin Finck     GDI_PRIORITY_GRAPHICS_DRV        /* priority */
168c2c66affSColin Finck };
169c2c66affSColin Finck 
170c2c66affSColin Finck 
171c2c66affSColin Finck /**********************************************************************
172c2c66affSColin Finck  *	     EMFDRV_DeleteDC
173c2c66affSColin Finck  */
EMFDRV_DeleteDC(PHYSDEV dev)174c2c66affSColin Finck static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
175c2c66affSColin Finck {
176c2c66affSColin Finck     EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
177c2c66affSColin Finck     UINT index;
178c2c66affSColin Finck 
179c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, physDev->emh );
180c2c66affSColin Finck     for(index = 0; index < physDev->handles_size; index++)
181c2c66affSColin Finck         if(physDev->handles[index])
182c2c66affSColin Finck 	    GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
183c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, physDev->handles );
184c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, physDev );
185c2c66affSColin Finck     return TRUE;
186c2c66affSColin Finck }
187c2c66affSColin Finck 
188c2c66affSColin Finck 
189c2c66affSColin Finck /******************************************************************
190c2c66affSColin Finck  *         EMFDRV_WriteRecord
191c2c66affSColin Finck  *
192c2c66affSColin Finck  * Warning: this function can change the pointer to the metafile header.
193c2c66affSColin Finck  */
EMFDRV_WriteRecord(PHYSDEV dev,EMR * emr)194c2c66affSColin Finck BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
195c2c66affSColin Finck {
196c2c66affSColin Finck     DWORD len;
197c2c66affSColin Finck     DWORD bytes_written;
198c2c66affSColin Finck     ENHMETAHEADER *emh;
199c2c66affSColin Finck     EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
200c2c66affSColin Finck 
201c2c66affSColin Finck     TRACE("record %d, size %d %s\n",
202c2c66affSColin Finck           emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
203c2c66affSColin Finck 
204c2c66affSColin Finck     assert( !(emr->nSize & 3) );
205c2c66affSColin Finck 
206c2c66affSColin Finck     physDev->emh->nBytes += emr->nSize;
207c2c66affSColin Finck     physDev->emh->nRecords++;
208c2c66affSColin Finck 
209c2c66affSColin Finck     if(physDev->hFile) {
210c2c66affSColin Finck         if (!WriteFile(physDev->hFile, emr, emr->nSize, &bytes_written, NULL))
211c2c66affSColin Finck 	    return FALSE;
212c2c66affSColin Finck     } else {
213c2c66affSColin Finck         DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
214c2c66affSColin Finck         len = physDev->emh->nBytes;
215c2c66affSColin Finck         if (len > nEmfSize) {
216c2c66affSColin Finck             nEmfSize += (nEmfSize / 2) + emr->nSize;
217c2c66affSColin Finck             emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
218c2c66affSColin Finck             if (!emh) return FALSE;
219c2c66affSColin Finck             physDev->emh = emh;
220c2c66affSColin Finck         }
221c2c66affSColin Finck         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
222c2c66affSColin Finck                emr->nSize);
223c2c66affSColin Finck     }
224c2c66affSColin Finck     return TRUE;
225c2c66affSColin Finck }
226c2c66affSColin Finck 
227c2c66affSColin Finck 
228c2c66affSColin Finck /******************************************************************
229c2c66affSColin Finck  *         EMFDRV_UpdateBBox
230c2c66affSColin Finck  */
EMFDRV_UpdateBBox(PHYSDEV dev,RECTL * rect)231c2c66affSColin Finck void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
232c2c66affSColin Finck {
233c2c66affSColin Finck     EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
234c2c66affSColin Finck     RECTL *bounds = &physDev->emh->rclBounds;
235c2c66affSColin Finck     RECTL vportRect = *rect;
236c2c66affSColin Finck 
237c2c66affSColin Finck     LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
238c2c66affSColin Finck 
239c2c66affSColin Finck     /* The coordinate systems may be mirrored
240c2c66affSColin Finck        (LPtoDP handles points, not rectangles) */
241c2c66affSColin Finck     if (vportRect.left > vportRect.right)
242c2c66affSColin Finck     {
243c2c66affSColin Finck         LONG temp = vportRect.right;
244c2c66affSColin Finck         vportRect.right = vportRect.left;
245c2c66affSColin Finck         vportRect.left = temp;
246c2c66affSColin Finck     }
247c2c66affSColin Finck     if (vportRect.top > vportRect.bottom)
248c2c66affSColin Finck     {
249c2c66affSColin Finck         LONG temp = vportRect.bottom;
250c2c66affSColin Finck         vportRect.bottom = vportRect.top;
251c2c66affSColin Finck         vportRect.top = temp;
252c2c66affSColin Finck     }
253c2c66affSColin Finck 
254c2c66affSColin Finck     if (bounds->left > bounds->right)
255c2c66affSColin Finck     {
256c2c66affSColin Finck         /* first bounding rectangle */
257c2c66affSColin Finck         *bounds = vportRect;
258c2c66affSColin Finck     }
259c2c66affSColin Finck     else
260c2c66affSColin Finck     {
261c2c66affSColin Finck         bounds->left   = min(bounds->left,   vportRect.left);
262c2c66affSColin Finck         bounds->top    = min(bounds->top,    vportRect.top);
263c2c66affSColin Finck         bounds->right  = max(bounds->right,  vportRect.right);
264c2c66affSColin Finck         bounds->bottom = max(bounds->bottom, vportRect.bottom);
265c2c66affSColin Finck     }
266c2c66affSColin Finck }
267c2c66affSColin Finck 
268c2c66affSColin Finck /**********************************************************************
269c2c66affSColin Finck  *          CreateEnhMetaFileA   (GDI32.@)
270c2c66affSColin Finck  */
CreateEnhMetaFileA(HDC hdc,LPCSTR filename,const RECT * rect,LPCSTR description)271c2c66affSColin Finck HDC WINAPI CreateEnhMetaFileA(
272c2c66affSColin Finck     HDC hdc,           /* [in] optional reference DC */
273c2c66affSColin Finck     LPCSTR filename,   /* [in] optional filename for disk metafiles */
274c2c66affSColin Finck     const RECT *rect,  /* [in] optional bounding rectangle */
275c2c66affSColin Finck     LPCSTR description /* [in] optional description */
276c2c66affSColin Finck     )
277c2c66affSColin Finck {
278c2c66affSColin Finck     LPWSTR filenameW = NULL;
279c2c66affSColin Finck     LPWSTR descriptionW = NULL;
280c2c66affSColin Finck     HDC hReturnDC;
281c2c66affSColin Finck     DWORD len1, len2, total;
282c2c66affSColin Finck 
283c2c66affSColin Finck     if(filename)
284c2c66affSColin Finck     {
285c2c66affSColin Finck         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
286c2c66affSColin Finck         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
287c2c66affSColin Finck         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
288c2c66affSColin Finck     }
289c2c66affSColin Finck     if(description) {
290c2c66affSColin Finck         len1 = strlen(description);
291c2c66affSColin Finck 	len2 = strlen(description + len1 + 1);
292c2c66affSColin Finck         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
293c2c66affSColin Finck 	descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
294c2c66affSColin Finck         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
295c2c66affSColin Finck     }
296c2c66affSColin Finck 
297c2c66affSColin Finck     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
298c2c66affSColin Finck 
299c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, filenameW );
300c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, descriptionW );
301c2c66affSColin Finck 
302c2c66affSColin Finck     return hReturnDC;
303c2c66affSColin Finck }
304c2c66affSColin Finck 
305c2c66affSColin Finck /**********************************************************************
306c2c66affSColin Finck  *          CreateEnhMetaFileW   (GDI32.@)
307c2c66affSColin Finck  */
CreateEnhMetaFileW(HDC hdc,LPCWSTR filename,const RECT * rect,LPCWSTR description)308c2c66affSColin Finck HDC WINAPI CreateEnhMetaFileW(
309c2c66affSColin Finck     HDC           hdc,        /* [in] optional reference DC */
310c2c66affSColin Finck     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
311c2c66affSColin Finck     const RECT*   rect,       /* [in] optional bounding rectangle */
312c2c66affSColin Finck     LPCWSTR       description /* [in] optional description */
313c2c66affSColin Finck     )
314c2c66affSColin Finck {
315c2c66affSColin Finck     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
316c2c66affSColin Finck     HDC ret;
317c2c66affSColin Finck     DC *dc;
318c2c66affSColin Finck     EMFDRV_PDEVICE *physDev;
319c2c66affSColin Finck     HANDLE hFile;
320c2c66affSColin Finck     DWORD size = 0, length = 0;
321c2c66affSColin Finck     DWORD bytes_written;
322c2c66affSColin Finck 
323c2c66affSColin Finck     TRACE("(%p %s %s %s)\n", hdc, debugstr_w(filename), wine_dbgstr_rect(rect), debugstr_w(description) );
324c2c66affSColin Finck 
325c2c66affSColin Finck     if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
326c2c66affSColin Finck 
327c2c66affSColin Finck     physDev = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev));
328c2c66affSColin Finck     if (!physDev) {
329c2c66affSColin Finck         free_dc_ptr( dc );
330c2c66affSColin Finck         return 0;
331c2c66affSColin Finck     }
332c2c66affSColin Finck     if(description) { /* App name\0Title\0\0 */
333c2c66affSColin Finck         length = lstrlenW(description);
334c2c66affSColin Finck 	length += lstrlenW(description + length + 1);
335c2c66affSColin Finck 	length += 3;
336c2c66affSColin Finck 	length *= 2;
337c2c66affSColin Finck     }
338c2c66affSColin Finck     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
339c2c66affSColin Finck 
340c2c66affSColin Finck     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
341c2c66affSColin Finck         HeapFree( GetProcessHeap(), 0, physDev );
342c2c66affSColin Finck         free_dc_ptr( dc );
343c2c66affSColin Finck         return 0;
344c2c66affSColin Finck     }
345c2c66affSColin Finck 
346c2c66affSColin Finck     push_dc_driver( &dc->physDev, &physDev->dev, &emfdrv_driver );
347c2c66affSColin Finck 
348c2c66affSColin Finck     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
349c2c66affSColin Finck     physDev->handles_size = HANDLE_LIST_INC;
350c2c66affSColin Finck     physDev->cur_handles = 1;
351c2c66affSColin Finck     physDev->hFile = 0;
352c2c66affSColin Finck     physDev->dc_brush = 0;
353c2c66affSColin Finck     physDev->dc_pen = 0;
354c2c66affSColin Finck     physDev->screen_dc = 0;
355c2c66affSColin Finck     physDev->restoring = 0;
356c2c66affSColin Finck     physDev->path = FALSE;
357c2c66affSColin Finck     if (hdc)  /* if no ref, use current display */
358c2c66affSColin Finck         physDev->ref_dc = hdc;
359c2c66affSColin Finck     else
360c2c66affSColin Finck         physDev->ref_dc = physDev->screen_dc = CreateDCW( displayW, NULL, NULL, NULL );
361c2c66affSColin Finck 
362c2c66affSColin Finck     SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
363c2c66affSColin Finck 
364c2c66affSColin Finck     physDev->emh->iType = EMR_HEADER;
365c2c66affSColin Finck     physDev->emh->nSize = size;
366c2c66affSColin Finck 
367c2c66affSColin Finck     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
368c2c66affSColin Finck     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
369c2c66affSColin Finck 
370c2c66affSColin Finck     if(rect) {
371c2c66affSColin Finck         physDev->emh->rclFrame.left   = rect->left;
372c2c66affSColin Finck 	physDev->emh->rclFrame.top    = rect->top;
373c2c66affSColin Finck 	physDev->emh->rclFrame.right  = rect->right;
374c2c66affSColin Finck 	physDev->emh->rclFrame.bottom = rect->bottom;
375c2c66affSColin Finck     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
376c2c66affSColin Finck         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
377c2c66affSColin Finck 	physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
378c2c66affSColin Finck     }
379c2c66affSColin Finck 
380c2c66affSColin Finck     physDev->emh->dSignature = ENHMETA_SIGNATURE;
381c2c66affSColin Finck     physDev->emh->nVersion = 0x10000;
382c2c66affSColin Finck     physDev->emh->nBytes = physDev->emh->nSize;
383c2c66affSColin Finck     physDev->emh->nRecords = 1;
384c2c66affSColin Finck     physDev->emh->nHandles = 1;
385c2c66affSColin Finck 
386c2c66affSColin Finck     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
387c2c66affSColin Finck     physDev->emh->nDescription = length / 2;
388c2c66affSColin Finck 
389c2c66affSColin Finck     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
390c2c66affSColin Finck 
391c2c66affSColin Finck     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
392c2c66affSColin Finck 
393c2c66affSColin Finck     /* Size in pixels */
394c2c66affSColin Finck     physDev->emh->szlDevice.cx = GetDeviceCaps( physDev->ref_dc, HORZRES );
395c2c66affSColin Finck     physDev->emh->szlDevice.cy = GetDeviceCaps( physDev->ref_dc, VERTRES );
396c2c66affSColin Finck 
397c2c66affSColin Finck     /* Size in millimeters */
398c2c66affSColin Finck     physDev->emh->szlMillimeters.cx = GetDeviceCaps( physDev->ref_dc, HORZSIZE );
399c2c66affSColin Finck     physDev->emh->szlMillimeters.cy = GetDeviceCaps( physDev->ref_dc, VERTSIZE );
400c2c66affSColin Finck 
401c2c66affSColin Finck     /* Size in micrometers */
402c2c66affSColin Finck     physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
403c2c66affSColin Finck     physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 1000;
404c2c66affSColin Finck 
405c2c66affSColin Finck     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
406c2c66affSColin Finck 
407c2c66affSColin Finck     if (filename)  /* disk based metafile */
408c2c66affSColin Finck     {
409c2c66affSColin Finck         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
410c2c66affSColin Finck 				 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
411c2c66affSColin Finck             free_dc_ptr( dc );
412c2c66affSColin Finck             return 0;
413c2c66affSColin Finck         }
414c2c66affSColin Finck         if (!WriteFile( hFile, physDev->emh, size, &bytes_written, NULL )) {
415c2c66affSColin Finck             free_dc_ptr( dc );
416c2c66affSColin Finck             CloseHandle( hFile );
417c2c66affSColin Finck             return 0;
418c2c66affSColin Finck 	}
419c2c66affSColin Finck 	physDev->hFile = hFile;
420c2c66affSColin Finck     }
421c2c66affSColin Finck 
422c2c66affSColin Finck     TRACE("returning %p\n", physDev->dev.hdc);
423c2c66affSColin Finck     ret = physDev->dev.hdc;
424c2c66affSColin Finck     release_dc_ptr( dc );
425c2c66affSColin Finck 
426c2c66affSColin Finck     return ret;
427c2c66affSColin Finck }
428c2c66affSColin Finck 
429c2c66affSColin Finck /******************************************************************
430c2c66affSColin Finck  *             CloseEnhMetaFile (GDI32.@)
431c2c66affSColin Finck  */
CloseEnhMetaFile(HDC hdc)432c2c66affSColin Finck HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
433c2c66affSColin Finck {
434c2c66affSColin Finck     HENHMETAFILE hmf;
435c2c66affSColin Finck     EMFDRV_PDEVICE *physDev;
436c2c66affSColin Finck     DC *dc;
437c2c66affSColin Finck     EMREOF emr;
438c2c66affSColin Finck     HANDLE hMapping = 0;
439c2c66affSColin Finck 
440c2c66affSColin Finck     TRACE("(%p)\n", hdc );
441c2c66affSColin Finck 
442c2c66affSColin Finck     if (!(dc = get_dc_ptr( hdc ))) return NULL;
443c2c66affSColin Finck     if (GetObjectType( hdc ) != OBJ_ENHMETADC)
444c2c66affSColin Finck     {
445c2c66affSColin Finck         release_dc_ptr( dc );
446c2c66affSColin Finck         return NULL;
447c2c66affSColin Finck     }
448c2c66affSColin Finck     if (dc->refcount != 1)
449c2c66affSColin Finck     {
450c2c66affSColin Finck         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
451c2c66affSColin Finck         release_dc_ptr( dc );
452c2c66affSColin Finck         return NULL;
453c2c66affSColin Finck     }
454c2c66affSColin Finck     physDev = get_emf_physdev( find_dc_driver( dc, &emfdrv_driver ));
455c2c66affSColin Finck 
456c2c66affSColin Finck     if(dc->saveLevel)
457c2c66affSColin Finck         RestoreDC(hdc, 1);
458c2c66affSColin Finck 
459c2c66affSColin Finck     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
460c2c66affSColin Finck     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
461c2c66affSColin Finck     if (physDev->screen_dc) DeleteDC( physDev->screen_dc );
462c2c66affSColin Finck 
463c2c66affSColin Finck     emr.emr.iType = EMR_EOF;
464c2c66affSColin Finck     emr.emr.nSize = sizeof(emr);
465c2c66affSColin Finck     emr.nPalEntries = 0;
466c2c66affSColin Finck     emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
467c2c66affSColin Finck     emr.nSizeLast = emr.emr.nSize;
468c2c66affSColin Finck     EMFDRV_WriteRecord( &physDev->dev, &emr.emr );
469c2c66affSColin Finck 
470c2c66affSColin Finck     /* Update rclFrame if not initialized in CreateEnhMetaFile */
471c2c66affSColin Finck     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
472c2c66affSColin Finck         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
473c2c66affSColin Finck 	  physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
474c2c66affSColin Finck         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
475c2c66affSColin Finck 	  physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
476c2c66affSColin Finck         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
477c2c66affSColin Finck 	  physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
478c2c66affSColin Finck         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
479c2c66affSColin Finck 	  physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
480c2c66affSColin Finck     }
481c2c66affSColin Finck 
482c2c66affSColin Finck     if (physDev->hFile)  /* disk based metafile */
483c2c66affSColin Finck     {
484c2c66affSColin Finck         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
485c2c66affSColin Finck         {
486c2c66affSColin Finck             CloseHandle( physDev->hFile );
487c2c66affSColin Finck             free_dc_ptr( dc );
488c2c66affSColin Finck             return 0;
489c2c66affSColin Finck         }
490c2c66affSColin Finck 
491c2c66affSColin Finck         if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
492c2c66affSColin Finck                        NULL, NULL))
493c2c66affSColin Finck         {
494c2c66affSColin Finck             CloseHandle( physDev->hFile );
495c2c66affSColin Finck             free_dc_ptr( dc );
496c2c66affSColin Finck             return 0;
497c2c66affSColin Finck         }
498c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, physDev->emh );
499c2c66affSColin Finck         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
500c2c66affSColin Finck 				      0, NULL);
501c2c66affSColin Finck 	TRACE("hMapping = %p\n", hMapping );
502c2c66affSColin Finck 	physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
503c2c66affSColin Finck 	TRACE("view = %p\n", physDev->emh );
504c2c66affSColin Finck         CloseHandle( hMapping );
505c2c66affSColin Finck         CloseHandle( physDev->hFile );
506c2c66affSColin Finck     }
507c2c66affSColin Finck 
508c2c66affSColin Finck     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
509c2c66affSColin Finck     physDev->emh = NULL;  /* So it won't be deleted */
510c2c66affSColin Finck     free_dc_ptr( dc );
511c2c66affSColin Finck     return hmf;
512c2c66affSColin Finck }
513