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