gdi32: Add a GetBoundsRect driver entry point.
[wine/multimedia.git] / dlls / gdi32 / enhmfdrv / init.c
blob335691148643eebb83dc143fb75a9d1c5e8bf164
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 "gdi_private.h"
30 #include "enhmfdrv/enhmetafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35 static BOOL EMFDRV_DeleteDC( PHYSDEV dev );
37 static const struct gdi_dc_funcs EMFDRV_Funcs =
39 NULL, /* pAbortDoc */
40 EMFDRV_AbortPath, /* pAbortPath */
41 NULL, /* pAlphaBlend */
42 NULL, /* pAngleArc */
43 EMFDRV_Arc, /* pArc */
44 NULL, /* pArcTo */
45 EMFDRV_BeginPath, /* pBeginPath */
46 NULL, /* pBlendImage */
47 NULL, /* pChoosePixelFormat */
48 EMFDRV_Chord, /* pChord */
49 EMFDRV_CloseFigure, /* pCloseFigure */
50 NULL, /* pCopyBitmap */
51 NULL, /* pCreateBitmap */
52 NULL, /* pCreateCompatibleDC */
53 NULL, /* pCreateDC */
54 NULL, /* pDeleteBitmap */
55 EMFDRV_DeleteDC, /* pDeleteDC */
56 EMFDRV_DeleteObject, /* pDeleteObject */
57 NULL, /* pDescribePixelFormat */
58 NULL, /* pDeviceCapabilities */
59 EMFDRV_Ellipse, /* pEllipse */
60 NULL, /* pEndDoc */
61 NULL, /* pEndPage */
62 EMFDRV_EndPath, /* pEndPath */
63 NULL, /* pEnumFonts */
64 NULL, /* pEnumICMProfiles */
65 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
66 NULL, /* pExtDeviceMode */
67 NULL, /* pExtEscape */
68 EMFDRV_ExtFloodFill, /* pExtFloodFill */
69 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
70 EMFDRV_ExtTextOut, /* pExtTextOut */
71 EMFDRV_FillPath, /* pFillPath */
72 EMFDRV_FillRgn, /* pFillRgn */
73 EMFDRV_FlattenPath, /* pFlattenPath */
74 NULL, /* pFontIsLinked */
75 EMFDRV_FrameRgn, /* pFrameRgn */
76 EMFDRV_GdiComment, /* pGdiComment */
77 NULL, /* pGdiRealizationInfo */
78 NULL, /* pGetBoundsRect */
79 NULL, /* pGetCharABCWidths */
80 NULL, /* pGetCharABCWidthsI */
81 NULL, /* pGetCharWidth */
82 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
83 NULL, /* pGetDeviceGammaRamp */
84 NULL, /* pGetFontData */
85 NULL, /* pGetFontUnicodeRanges */
86 NULL, /* pGetGlyphIndices */
87 NULL, /* pGetGlyphOutline */
88 NULL, /* pGetICMProfile */
89 NULL, /* pGetImage */
90 NULL, /* pGetKerningPairs */
91 NULL, /* pGetNearestColor */
92 NULL, /* pGetOutlineTextMetrics */
93 NULL, /* pGetPixel */
94 NULL, /* pGetPixelFormat */
95 NULL, /* pGetSystemPaletteEntries */
96 NULL, /* pGetTextCharsetInfo */
97 NULL, /* pGetTextExtentExPoint */
98 NULL, /* pGetTextExtentExPointI */
99 NULL, /* pGetTextFace */
100 NULL, /* pGetTextMetrics */
101 NULL, /* pGradientFill */
102 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
103 EMFDRV_InvertRgn, /* pInvertRgn */
104 EMFDRV_LineTo, /* pLineTo */
105 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
106 EMFDRV_MoveTo, /* pMoveTo */
107 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
108 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
109 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
110 EMFDRV_PaintRgn, /* pPaintRgn */
111 EMFDRV_PatBlt, /* pPatBlt */
112 EMFDRV_Pie, /* pPie */
113 EMFDRV_PolyBezier, /* pPolyBezier */
114 EMFDRV_PolyBezierTo, /* pPolyBezierTo */
115 NULL, /* pPolyDraw */
116 EMFDRV_PolyPolygon, /* pPolyPolygon */
117 EMFDRV_PolyPolyline, /* pPolyPolyline */
118 EMFDRV_Polygon, /* pPolygon */
119 EMFDRV_Polyline, /* pPolyline */
120 NULL, /* pPolylineTo */
121 NULL, /* pPutImage */
122 NULL, /* pRealizeDefaultPalette */
123 NULL, /* pRealizePalette */
124 EMFDRV_Rectangle, /* pRectangle */
125 NULL, /* pResetDC */
126 EMFDRV_RestoreDC, /* pRestoreDC */
127 EMFDRV_RoundRect, /* pRoundRect */
128 EMFDRV_SaveDC, /* pSaveDC */
129 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
130 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
131 EMFDRV_SelectBitmap, /* pSelectBitmap */
132 EMFDRV_SelectBrush, /* pSelectBrush */
133 EMFDRV_SelectClipPath, /* pSelectClipPath */
134 EMFDRV_SelectFont, /* pSelectFont */
135 EMFDRV_SelectPalette, /* pSelectPalette */
136 EMFDRV_SelectPen, /* pSelectPen */
137 EMFDRV_SetArcDirection, /* pSetArcDirection */
138 EMFDRV_SetBkColor, /* pSetBkColor */
139 EMFDRV_SetBkMode, /* pSetBkMode */
140 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
141 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
142 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
143 NULL, /* pSetDeviceClipping */
144 NULL, /* pSetDeviceGammaRamp */
145 EMFDRV_SetLayout, /* pSetLayout */
146 EMFDRV_SetMapMode, /* pSetMapMode */
147 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
148 EMFDRV_SetPixel, /* pSetPixel */
149 NULL, /* pSetPixelFormat */
150 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
151 EMFDRV_SetROP2, /* pSetROP2 */
152 NULL, /* pSetRelAbs */
153 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
154 EMFDRV_SetTextAlign, /* pSetTextAlign */
155 NULL, /* pSetTextCharacterExtra */
156 EMFDRV_SetTextColor, /* pSetTextColor */
157 EMFDRV_SetTextJustification, /* pSetTextJustification */
158 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
159 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
160 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
161 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
162 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
163 NULL, /* pStartDoc */
164 NULL, /* pStartPage */
165 EMFDRV_StretchBlt, /* pStretchBlt */
166 EMFDRV_StretchDIBits, /* pStretchDIBits */
167 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
168 EMFDRV_StrokePath, /* pStrokePath */
169 NULL, /* pSwapBuffers */
170 NULL, /* pUnrealizePalette */
171 EMFDRV_WidenPath /* pWidenPath */
175 /**********************************************************************
176 * EMFDRV_DeleteDC
178 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
180 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
181 UINT index;
183 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
184 for(index = 0; index < physDev->handles_size; index++)
185 if(physDev->handles[index])
186 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
187 HeapFree( GetProcessHeap(), 0, physDev->handles );
188 HeapFree( GetProcessHeap(), 0, physDev );
189 return TRUE;
193 /******************************************************************
194 * EMFDRV_WriteRecord
196 * Warning: this function can change the pointer to the metafile header.
198 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
200 DWORD len;
201 ENHMETAHEADER *emh;
202 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
204 TRACE("record %d, size %d %s\n",
205 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
207 assert( !(emr->nSize & 3) );
209 physDev->emh->nBytes += emr->nSize;
210 physDev->emh->nRecords++;
212 if(physDev->hFile) {
213 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
214 return FALSE;
215 } else {
216 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
217 len = physDev->emh->nBytes;
218 if (len > nEmfSize) {
219 nEmfSize += (nEmfSize / 2) + emr->nSize;
220 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
221 if (!emh) return FALSE;
222 physDev->emh = emh;
224 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
225 emr->nSize);
227 return TRUE;
231 /******************************************************************
232 * EMFDRV_UpdateBBox
234 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
236 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
237 RECTL *bounds = &physDev->emh->rclBounds;
238 RECTL vportRect = *rect;
240 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
242 /* The coordinate systems may be mirrored
243 (LPtoDP handles points, not rectangles) */
244 if (vportRect.left > vportRect.right)
246 LONG temp = vportRect.right;
247 vportRect.right = vportRect.left;
248 vportRect.left = temp;
250 if (vportRect.top > vportRect.bottom)
252 LONG temp = vportRect.bottom;
253 vportRect.bottom = vportRect.top;
254 vportRect.top = temp;
257 if (bounds->left > bounds->right)
259 /* first bounding rectangle */
260 *bounds = vportRect;
262 else
264 bounds->left = min(bounds->left, vportRect.left);
265 bounds->top = min(bounds->top, vportRect.top);
266 bounds->right = max(bounds->right, vportRect.right);
267 bounds->bottom = max(bounds->bottom, vportRect.bottom);
271 /**********************************************************************
272 * CreateEnhMetaFileA (GDI32.@)
274 HDC WINAPI CreateEnhMetaFileA(
275 HDC hdc, /* [in] optional reference DC */
276 LPCSTR filename, /* [in] optional filename for disk metafiles */
277 const RECT *rect, /* [in] optional bounding rectangle */
278 LPCSTR description /* [in] optional description */
281 LPWSTR filenameW = NULL;
282 LPWSTR descriptionW = NULL;
283 HDC hReturnDC;
284 DWORD len1, len2, total;
286 if(filename)
288 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
289 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
290 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
292 if(description) {
293 len1 = strlen(description);
294 len2 = strlen(description + len1 + 1);
295 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
296 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
297 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
300 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
302 HeapFree( GetProcessHeap(), 0, filenameW );
303 HeapFree( GetProcessHeap(), 0, descriptionW );
305 return hReturnDC;
308 /**********************************************************************
309 * CreateEnhMetaFileW (GDI32.@)
311 HDC WINAPI CreateEnhMetaFileW(
312 HDC hdc, /* [in] optional reference DC */
313 LPCWSTR filename, /* [in] optional filename for disk metafiles */
314 const RECT* rect, /* [in] optional bounding rectangle */
315 LPCWSTR description /* [in] optional description */
318 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
319 HDC ret;
320 DC *dc;
321 EMFDRV_PDEVICE *physDev;
322 HANDLE hFile;
323 DWORD size = 0, length = 0;
325 TRACE("%s\n", debugstr_w(filename) );
327 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
329 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
330 if (!physDev) {
331 free_dc_ptr( dc );
332 return 0;
334 if(description) { /* App name\0Title\0\0 */
335 length = lstrlenW(description);
336 length += lstrlenW(description + length + 1);
337 length += 3;
338 length *= 2;
340 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
342 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
343 HeapFree( GetProcessHeap(), 0, physDev );
344 free_dc_ptr( dc );
345 return 0;
348 push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
350 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
351 physDev->handles_size = HANDLE_LIST_INC;
352 physDev->cur_handles = 1;
353 physDev->hFile = 0;
354 physDev->dc_brush = 0;
355 physDev->dc_pen = 0;
356 physDev->screen_dc = 0;
357 physDev->restoring = 0;
358 if (hdc) /* if no ref, use current display */
359 physDev->ref_dc = hdc;
360 else
361 physDev->ref_dc = physDev->screen_dc = CreateDCW( displayW, NULL, NULL, NULL );
363 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
365 physDev->emh->iType = EMR_HEADER;
366 physDev->emh->nSize = size;
368 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
369 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
371 if(rect) {
372 physDev->emh->rclFrame.left = rect->left;
373 physDev->emh->rclFrame.top = rect->top;
374 physDev->emh->rclFrame.right = rect->right;
375 physDev->emh->rclFrame.bottom = rect->bottom;
376 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
377 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
378 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
381 physDev->emh->dSignature = ENHMETA_SIGNATURE;
382 physDev->emh->nVersion = 0x10000;
383 physDev->emh->nBytes = physDev->emh->nSize;
384 physDev->emh->nRecords = 1;
385 physDev->emh->nHandles = 1;
387 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
388 physDev->emh->nDescription = length / 2;
390 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
392 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
394 /* Size in pixels */
395 physDev->emh->szlDevice.cx = GetDeviceCaps( physDev->ref_dc, HORZRES );
396 physDev->emh->szlDevice.cy = GetDeviceCaps( physDev->ref_dc, VERTRES );
398 /* Size in millimeters */
399 physDev->emh->szlMillimeters.cx = GetDeviceCaps( physDev->ref_dc, HORZSIZE );
400 physDev->emh->szlMillimeters.cy = GetDeviceCaps( physDev->ref_dc, VERTSIZE );
402 /* Size in micrometers */
403 physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
404 physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 1000;
406 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
408 if (filename) /* disk based metafile */
410 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
411 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
412 free_dc_ptr( dc );
413 return 0;
415 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
416 free_dc_ptr( dc );
417 return 0;
419 physDev->hFile = hFile;
422 TRACE("returning %p\n", physDev->dev.hdc);
423 ret = physDev->dev.hdc;
424 release_dc_ptr( dc );
426 return ret;
429 /******************************************************************
430 * CloseEnhMetaFile (GDI32.@)
432 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
434 HENHMETAFILE hmf;
435 EMFDRV_PDEVICE *physDev;
436 DC *dc;
437 EMREOF emr;
438 HANDLE hMapping = 0;
440 TRACE("(%p)\n", hdc );
442 if (!(dc = get_dc_ptr( hdc ))) return NULL;
443 if (dc->header.type != OBJ_ENHMETADC)
445 release_dc_ptr( dc );
446 return NULL;
448 if (dc->refcount != 1)
450 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
451 release_dc_ptr( dc );
452 return NULL;
454 physDev = (EMFDRV_PDEVICE *)dc->physDev;
456 if(dc->saveLevel)
457 RestoreDC(hdc, 1);
459 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
460 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
461 if (physDev->screen_dc) DeleteDC( physDev->screen_dc );
463 emr.emr.iType = EMR_EOF;
464 emr.emr.nSize = sizeof(emr);
465 emr.nPalEntries = 0;
466 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
467 emr.nSizeLast = emr.emr.nSize;
468 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
470 /* Update rclFrame if not initialized in CreateEnhMetaFile */
471 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
472 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
473 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
474 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
475 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
476 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
477 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
478 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
479 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
482 if (physDev->hFile) /* disk based metafile */
484 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
486 CloseHandle( physDev->hFile );
487 free_dc_ptr( dc );
488 return 0;
491 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
492 NULL, NULL))
494 CloseHandle( physDev->hFile );
495 free_dc_ptr( dc );
496 return 0;
498 HeapFree( GetProcessHeap(), 0, physDev->emh );
499 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
500 0, NULL);
501 TRACE("hMapping = %p\n", hMapping );
502 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
503 TRACE("view = %p\n", physDev->emh );
504 CloseHandle( hMapping );
505 CloseHandle( physDev->hFile );
508 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
509 physDev->emh = NULL; /* So it won't be deleted */
510 free_dc_ptr( dc );
511 return hmf;