opengl32: Add the concept of a WGL driver.
[wine/multimedia.git] / dlls / gdi32 / enhmfdrv / init.c
blob9394c380dfd496678ee59950d42076298ae2c561
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, /* pCreateCompatibleDC */
51 NULL, /* pCreateDC */
52 EMFDRV_DeleteDC, /* pDeleteDC */
53 EMFDRV_DeleteObject, /* pDeleteObject */
54 NULL, /* pDescribePixelFormat */
55 NULL, /* pDeviceCapabilities */
56 EMFDRV_Ellipse, /* pEllipse */
57 NULL, /* pEndDoc */
58 NULL, /* pEndPage */
59 EMFDRV_EndPath, /* pEndPath */
60 NULL, /* pEnumFonts */
61 NULL, /* pEnumICMProfiles */
62 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
63 NULL, /* pExtDeviceMode */
64 NULL, /* pExtEscape */
65 EMFDRV_ExtFloodFill, /* pExtFloodFill */
66 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
67 EMFDRV_ExtTextOut, /* pExtTextOut */
68 EMFDRV_FillPath, /* pFillPath */
69 EMFDRV_FillRgn, /* pFillRgn */
70 EMFDRV_FlattenPath, /* pFlattenPath */
71 NULL, /* pFontIsLinked */
72 EMFDRV_FrameRgn, /* pFrameRgn */
73 EMFDRV_GdiComment, /* pGdiComment */
74 NULL, /* pGdiRealizationInfo */
75 NULL, /* pGetBoundsRect */
76 NULL, /* pGetCharABCWidths */
77 NULL, /* pGetCharABCWidthsI */
78 NULL, /* pGetCharWidth */
79 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
80 NULL, /* pGetDeviceGammaRamp */
81 NULL, /* pGetFontData */
82 NULL, /* pGetFontUnicodeRanges */
83 NULL, /* pGetGlyphIndices */
84 NULL, /* pGetGlyphOutline */
85 NULL, /* pGetICMProfile */
86 NULL, /* pGetImage */
87 NULL, /* pGetKerningPairs */
88 NULL, /* pGetNearestColor */
89 NULL, /* pGetOutlineTextMetrics */
90 NULL, /* pGetPixel */
91 NULL, /* pGetPixelFormat */
92 NULL, /* pGetSystemPaletteEntries */
93 NULL, /* pGetTextCharsetInfo */
94 NULL, /* pGetTextExtentExPoint */
95 NULL, /* pGetTextExtentExPointI */
96 NULL, /* pGetTextFace */
97 NULL, /* pGetTextMetrics */
98 NULL, /* pGradientFill */
99 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
100 EMFDRV_InvertRgn, /* pInvertRgn */
101 EMFDRV_LineTo, /* pLineTo */
102 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
103 EMFDRV_MoveTo, /* pMoveTo */
104 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
105 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
106 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
107 EMFDRV_PaintRgn, /* pPaintRgn */
108 EMFDRV_PatBlt, /* pPatBlt */
109 EMFDRV_Pie, /* pPie */
110 EMFDRV_PolyBezier, /* pPolyBezier */
111 EMFDRV_PolyBezierTo, /* pPolyBezierTo */
112 NULL, /* pPolyDraw */
113 EMFDRV_PolyPolygon, /* pPolyPolygon */
114 EMFDRV_PolyPolyline, /* pPolyPolyline */
115 EMFDRV_Polygon, /* pPolygon */
116 EMFDRV_Polyline, /* pPolyline */
117 NULL, /* pPolylineTo */
118 NULL, /* pPutImage */
119 NULL, /* pRealizeDefaultPalette */
120 NULL, /* pRealizePalette */
121 EMFDRV_Rectangle, /* pRectangle */
122 NULL, /* pResetDC */
123 EMFDRV_RestoreDC, /* pRestoreDC */
124 EMFDRV_RoundRect, /* pRoundRect */
125 EMFDRV_SaveDC, /* pSaveDC */
126 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
127 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
128 EMFDRV_SelectBitmap, /* pSelectBitmap */
129 EMFDRV_SelectBrush, /* pSelectBrush */
130 EMFDRV_SelectClipPath, /* pSelectClipPath */
131 EMFDRV_SelectFont, /* pSelectFont */
132 EMFDRV_SelectPalette, /* pSelectPalette */
133 EMFDRV_SelectPen, /* pSelectPen */
134 EMFDRV_SetArcDirection, /* pSetArcDirection */
135 EMFDRV_SetBkColor, /* pSetBkColor */
136 EMFDRV_SetBkMode, /* pSetBkMode */
137 NULL, /* pSetBoundsRect */
138 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
139 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
140 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
141 NULL, /* pSetDeviceClipping */
142 NULL, /* pSetDeviceGammaRamp */
143 EMFDRV_SetLayout, /* pSetLayout */
144 EMFDRV_SetMapMode, /* pSetMapMode */
145 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
146 EMFDRV_SetPixel, /* pSetPixel */
147 NULL, /* pSetPixelFormat */
148 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
149 EMFDRV_SetROP2, /* pSetROP2 */
150 NULL, /* pSetRelAbs */
151 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
152 EMFDRV_SetTextAlign, /* pSetTextAlign */
153 NULL, /* pSetTextCharacterExtra */
154 EMFDRV_SetTextColor, /* pSetTextColor */
155 EMFDRV_SetTextJustification, /* pSetTextJustification */
156 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
157 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
158 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
159 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
160 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
161 NULL, /* pStartDoc */
162 NULL, /* pStartPage */
163 EMFDRV_StretchBlt, /* pStretchBlt */
164 EMFDRV_StretchDIBits, /* pStretchDIBits */
165 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
166 EMFDRV_StrokePath, /* pStrokePath */
167 NULL, /* pSwapBuffers */
168 NULL, /* pUnrealizePalette */
169 EMFDRV_WidenPath, /* pWidenPath */
170 NULL, /* pwglCreateContext */
171 NULL, /* pwglCreateContextAttribsARB */
172 NULL, /* pwglGetProcAddress */
173 NULL, /* wine_get_wgl_driver */
174 GDI_PRIORITY_GRAPHICS_DRV /* priority */
178 /**********************************************************************
179 * EMFDRV_DeleteDC
181 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
183 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
184 UINT index;
186 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
187 for(index = 0; index < physDev->handles_size; index++)
188 if(physDev->handles[index])
189 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
190 HeapFree( GetProcessHeap(), 0, physDev->handles );
191 HeapFree( GetProcessHeap(), 0, physDev );
192 return TRUE;
196 /******************************************************************
197 * EMFDRV_WriteRecord
199 * Warning: this function can change the pointer to the metafile header.
201 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
203 DWORD len;
204 ENHMETAHEADER *emh;
205 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
207 TRACE("record %d, size %d %s\n",
208 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
210 assert( !(emr->nSize & 3) );
212 physDev->emh->nBytes += emr->nSize;
213 physDev->emh->nRecords++;
215 if(physDev->hFile) {
216 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
217 return FALSE;
218 } else {
219 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
220 len = physDev->emh->nBytes;
221 if (len > nEmfSize) {
222 nEmfSize += (nEmfSize / 2) + emr->nSize;
223 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
224 if (!emh) return FALSE;
225 physDev->emh = emh;
227 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
228 emr->nSize);
230 return TRUE;
234 /******************************************************************
235 * EMFDRV_UpdateBBox
237 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
239 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
240 RECTL *bounds = &physDev->emh->rclBounds;
241 RECTL vportRect = *rect;
243 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
245 /* The coordinate systems may be mirrored
246 (LPtoDP handles points, not rectangles) */
247 if (vportRect.left > vportRect.right)
249 LONG temp = vportRect.right;
250 vportRect.right = vportRect.left;
251 vportRect.left = temp;
253 if (vportRect.top > vportRect.bottom)
255 LONG temp = vportRect.bottom;
256 vportRect.bottom = vportRect.top;
257 vportRect.top = temp;
260 if (bounds->left > bounds->right)
262 /* first bounding rectangle */
263 *bounds = vportRect;
265 else
267 bounds->left = min(bounds->left, vportRect.left);
268 bounds->top = min(bounds->top, vportRect.top);
269 bounds->right = max(bounds->right, vportRect.right);
270 bounds->bottom = max(bounds->bottom, vportRect.bottom);
274 /**********************************************************************
275 * CreateEnhMetaFileA (GDI32.@)
277 HDC WINAPI CreateEnhMetaFileA(
278 HDC hdc, /* [in] optional reference DC */
279 LPCSTR filename, /* [in] optional filename for disk metafiles */
280 const RECT *rect, /* [in] optional bounding rectangle */
281 LPCSTR description /* [in] optional description */
284 LPWSTR filenameW = NULL;
285 LPWSTR descriptionW = NULL;
286 HDC hReturnDC;
287 DWORD len1, len2, total;
289 if(filename)
291 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
292 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
293 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
295 if(description) {
296 len1 = strlen(description);
297 len2 = strlen(description + len1 + 1);
298 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
299 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
300 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
303 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
305 HeapFree( GetProcessHeap(), 0, filenameW );
306 HeapFree( GetProcessHeap(), 0, descriptionW );
308 return hReturnDC;
311 /**********************************************************************
312 * CreateEnhMetaFileW (GDI32.@)
314 HDC WINAPI CreateEnhMetaFileW(
315 HDC hdc, /* [in] optional reference DC */
316 LPCWSTR filename, /* [in] optional filename for disk metafiles */
317 const RECT* rect, /* [in] optional bounding rectangle */
318 LPCWSTR description /* [in] optional description */
321 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
322 HDC ret;
323 DC *dc;
324 EMFDRV_PDEVICE *physDev;
325 HANDLE hFile;
326 DWORD size = 0, length = 0;
328 TRACE("%s\n", debugstr_w(filename) );
330 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
332 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
333 if (!physDev) {
334 free_dc_ptr( dc );
335 return 0;
337 if(description) { /* App name\0Title\0\0 */
338 length = lstrlenW(description);
339 length += lstrlenW(description + length + 1);
340 length += 3;
341 length *= 2;
343 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
345 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
346 HeapFree( GetProcessHeap(), 0, physDev );
347 free_dc_ptr( dc );
348 return 0;
351 push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
353 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
354 physDev->handles_size = HANDLE_LIST_INC;
355 physDev->cur_handles = 1;
356 physDev->hFile = 0;
357 physDev->dc_brush = 0;
358 physDev->dc_pen = 0;
359 physDev->screen_dc = 0;
360 physDev->restoring = 0;
361 if (hdc) /* if no ref, use current display */
362 physDev->ref_dc = hdc;
363 else
364 physDev->ref_dc = physDev->screen_dc = CreateDCW( displayW, NULL, NULL, NULL );
366 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
368 physDev->emh->iType = EMR_HEADER;
369 physDev->emh->nSize = size;
371 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
372 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
374 if(rect) {
375 physDev->emh->rclFrame.left = rect->left;
376 physDev->emh->rclFrame.top = rect->top;
377 physDev->emh->rclFrame.right = rect->right;
378 physDev->emh->rclFrame.bottom = rect->bottom;
379 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
380 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
381 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
384 physDev->emh->dSignature = ENHMETA_SIGNATURE;
385 physDev->emh->nVersion = 0x10000;
386 physDev->emh->nBytes = physDev->emh->nSize;
387 physDev->emh->nRecords = 1;
388 physDev->emh->nHandles = 1;
390 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
391 physDev->emh->nDescription = length / 2;
393 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
395 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
397 /* Size in pixels */
398 physDev->emh->szlDevice.cx = GetDeviceCaps( physDev->ref_dc, HORZRES );
399 physDev->emh->szlDevice.cy = GetDeviceCaps( physDev->ref_dc, VERTRES );
401 /* Size in millimeters */
402 physDev->emh->szlMillimeters.cx = GetDeviceCaps( physDev->ref_dc, HORZSIZE );
403 physDev->emh->szlMillimeters.cy = GetDeviceCaps( physDev->ref_dc, VERTSIZE );
405 /* Size in micrometers */
406 physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
407 physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 1000;
409 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
411 if (filename) /* disk based metafile */
413 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
414 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
415 free_dc_ptr( dc );
416 return 0;
418 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
419 free_dc_ptr( dc );
420 return 0;
422 physDev->hFile = hFile;
425 TRACE("returning %p\n", physDev->dev.hdc);
426 ret = physDev->dev.hdc;
427 release_dc_ptr( dc );
429 return ret;
432 /******************************************************************
433 * CloseEnhMetaFile (GDI32.@)
435 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
437 HENHMETAFILE hmf;
438 EMFDRV_PDEVICE *physDev;
439 DC *dc;
440 EMREOF emr;
441 HANDLE hMapping = 0;
443 TRACE("(%p)\n", hdc );
445 if (!(dc = get_dc_ptr( hdc ))) return NULL;
446 if (dc->header.type != OBJ_ENHMETADC)
448 release_dc_ptr( dc );
449 return NULL;
451 if (dc->refcount != 1)
453 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
454 release_dc_ptr( dc );
455 return NULL;
457 physDev = (EMFDRV_PDEVICE *)dc->physDev;
459 if(dc->saveLevel)
460 RestoreDC(hdc, 1);
462 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
463 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
464 if (physDev->screen_dc) DeleteDC( physDev->screen_dc );
466 emr.emr.iType = EMR_EOF;
467 emr.emr.nSize = sizeof(emr);
468 emr.nPalEntries = 0;
469 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
470 emr.nSizeLast = emr.emr.nSize;
471 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
473 /* Update rclFrame if not initialized in CreateEnhMetaFile */
474 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
475 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
476 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
477 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
478 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
479 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
480 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
481 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
482 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
485 if (physDev->hFile) /* disk based metafile */
487 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
489 CloseHandle( physDev->hFile );
490 free_dc_ptr( dc );
491 return 0;
494 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
495 NULL, NULL))
497 CloseHandle( physDev->hFile );
498 free_dc_ptr( dc );
499 return 0;
501 HeapFree( GetProcessHeap(), 0, physDev->emh );
502 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
503 0, NULL);
504 TRACE("hMapping = %p\n", hMapping );
505 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
506 TRACE("view = %p\n", physDev->emh );
507 CloseHandle( hMapping );
508 CloseHandle( physDev->hFile );
511 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
512 physDev->emh = NULL; /* So it won't be deleted */
513 free_dc_ptr( dc );
514 return hmf;