Added support for OpenGL.
[wine.git] / graphics / enhmetafiledrv / init.c
blobee8c295ba726a12b38df77300ac56dffce51ccfd
1 /*
2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
5 */
7 #include "windef.h"
8 #include "wingdi.h"
9 #include "dc.h"
10 #include "heap.h"
11 #include "global.h"
12 #include "enhmetafile.h"
13 #include "enhmetafiledrv.h"
14 #include "wine/winestring.h"
15 #include "debugtools.h"
16 #include "winuser.h"
18 #include <string.h>
20 DEFAULT_DEBUG_CHANNEL(enhmetafile)
22 static const DC_FUNCTIONS EMFDRV_Funcs =
24 NULL, /* pAbortDoc */
25 EMFDRV_AbortPath, /* pAbortPath */
26 NULL, /* pAngleArc */
27 EMFDRV_Arc, /* pArc */
28 NULL, /* pArcTo */
29 EMFDRV_BeginPath, /* pBeginPath */
30 NULL, /* pBitBlt */
31 NULL, /* pBitmapBits */
32 NULL, /* pChoosePixelFormat */
33 EMFDRV_Chord, /* pChord */
34 EMFDRV_CloseFigure, /* pCloseFigure */
35 NULL, /* pCreateBitmap */
36 NULL, /* no implementation */ /* pCreateDC */
37 NULL, /* pCreateDIBSection */
38 NULL, /* pCreateDIBSection16 */
39 NULL, /* no implementation */ /* pDeleteDC */
40 NULL, /* pDeleteObject */
41 NULL, /* pDescribePixelFormat */
42 NULL, /* pDeviceCapabilities */
43 EMFDRV_Ellipse, /* pEllipse */
44 NULL, /* pEndDoc */
45 NULL, /* pEndPage */
46 EMFDRV_EndPath, /* pEndPath */
47 NULL, /* pEnumDeviceFonts */
48 NULL, /* pEscape */
49 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
50 NULL, /* pExtDeviceMode */
51 EMFDRV_ExtFloodFill, /* pExtFloodFill */
52 NULL, /* pExtTextOut */
53 EMFDRV_FillPath, /* pFillPath */
54 EMFDRV_FillRgn, /* pFillRgn */
55 EMFDRV_FlattenPath, /* pFlattenPath */
56 EMFDRV_FrameRgn, /* pFrameRgn */
57 NULL, /* pGetCharWidth */
58 NULL, /* pGetDCOrgEx */
59 NULL, /* no implementation */ /* pGetPixel */
60 NULL, /* pGetPixelFormat */
61 NULL, /* pGetTextExtentPoint */
62 NULL, /* pGetTextMetrics */
63 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
64 EMFDRV_InvertRgn, /* pInvertRgn */
65 EMFDRV_LineTo, /* pLineTo */
66 NULL, /* pLoadOEMResource */
67 EMFDRV_MoveToEx, /* pMoveToEx */
68 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
69 NULL, /* pOffsetViewportOrg */
70 NULL, /* pOffsetWindowOrg */
71 EMFDRV_PaintRgn, /* pPaintRgn */
72 NULL, /* pPatBlt */
73 EMFDRV_Pie, /* pPie */
74 NULL, /* pPolyBezier */
75 NULL, /* pPolyBezierTo */
76 NULL, /* pPolyDraw */
77 EMFDRV_PolyPolygon, /* pPolyPolygon */
78 EMFDRV_PolyPolyline, /* pPolyPolyline */
79 EMFDRV_Polygon, /* pPolygon */
80 EMFDRV_Polyline, /* pPolyline */
81 NULL, /* pPolylineTo */
82 NULL, /* pRealizePalette */
83 EMFDRV_Rectangle, /* pRectangle */
84 EMFDRV_RestoreDC, /* pRestoreDC */
85 EMFDRV_RoundRect, /* pRoundRect */
86 EMFDRV_SaveDC, /* pSaveDC */
87 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
88 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
89 EMFDRV_SelectClipPath, /* pSelectClipPath */
90 NULL, /* pSelectClipRgn */
91 EMFDRV_SelectObject, /* pSelectObject */
92 NULL, /* pSelectPalette */
93 EMFDRV_SetBkColor, /* pSetBkColor */
94 EMFDRV_SetBkMode, /* pSetBkMode */
95 NULL, /* pSetDeviceClipping */
96 NULL, /* pSetDIBitsToDevice */
97 EMFDRV_SetMapMode, /* pSetMapMode */
98 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
99 NULL, /* pSetPixel */
100 NULL, /* pSetPixelFormat */
101 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
102 EMFDRV_SetROP2, /* pSetROP2 */
103 NULL, /* pSetRelAbs */
104 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
105 EMFDRV_SetTextAlign, /* pSetTextAlign */
106 NULL, /* pSetTextCharacterExtra */
107 EMFDRV_SetTextColor, /* pSetTextColor */
108 NULL, /* pSetTextJustification */
109 EMFDRV_SetViewportExt, /* pSetViewportExt */
110 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
111 EMFDRV_SetWindowExt, /* pSetWindowExt */
112 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
113 NULL, /* pStartDoc */
114 NULL, /* pStartPage */
115 NULL, /* pStretchBlt */
116 NULL, /* pStretchDIBits */
117 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
118 EMFDRV_StrokePath, /* pStrokePath */
119 NULL, /* pSwapBuffers */
120 EMFDRV_WidenPath /* pWiddenPath */
124 /**********************************************************************
125 * EMFDRV_DeleteDC
127 static BOOL EMFDRV_DeleteDC( DC *dc )
129 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
131 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
132 HeapFree( GetProcessHeap(), 0, physDev );
133 dc->physDev = NULL;
134 GDI_FreeObject(dc->hSelf);
135 return TRUE;
139 /******************************************************************
140 * EMFDRV_WriteRecord
142 * Warning: this function can change the pointer to the metafile header.
144 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
146 DWORD len;
147 ENHMETAHEADER *emh;
148 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
150 physDev->emh->nBytes += emr->nSize;
151 physDev->emh->nRecords++;
153 if(physDev->hFile) {
154 TRACE("Writing record to disk\n");
155 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
156 return FALSE;
157 } else {
158 len = physDev->emh->nBytes;
159 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
160 if (!emh) return FALSE;
161 physDev->emh = emh;
162 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
163 emr->nSize);
165 return TRUE;
169 /******************************************************************
170 * EMFDRV_UpdateBBox
172 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
174 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
175 RECTL *bounds = &physDev->emh->rclBounds;
177 if(bounds->left > bounds->right) {/* first rect */
178 *bounds = *rect;
179 return;
181 bounds->left = min(bounds->left, rect->left);
182 bounds->top = min(bounds->top, rect->top);
183 bounds->right = max(bounds->right, rect->right);
184 bounds->bottom = max(bounds->bottom, rect->bottom);
185 return;
188 /******************************************************************
189 * EMFDRV_AddHandleDC
191 * Note: this function assumes that we never delete objects.
192 * If we do someday, we'll need to maintain a table to re-use deleted
193 * handles.
195 int EMFDRV_AddHandleDC( DC *dc )
197 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
198 physDev->emh->nHandles++;
199 return physDev->nextHandle++;
203 /**********************************************************************
204 * CreateEnhMetaFileA (GDI32.41)
206 HDC WINAPI CreateEnhMetaFileA(
207 HDC hdc, /* optional reference DC */
208 LPCSTR filename, /* optional filename for disk metafiles */
209 const RECT *rect, /* optional bounding rectangle */
210 LPCSTR description /* optional description */
213 LPWSTR filenameW = NULL;
214 LPWSTR descriptionW = NULL;
215 HDC hReturnDC;
216 DWORD len1, len2;
218 if(filename)
219 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
221 if(description) {
222 len1 = strlen(description);
223 len2 = strlen(description + len1 + 1);
224 descriptionW = HeapAlloc( GetProcessHeap(), 0, (len1 + len2 + 3) * 2);
225 lstrcpyAtoW(descriptionW, description );
226 lstrcpyAtoW(descriptionW + len1 + 1 , description + len1 + 1);
227 *(descriptionW + len1 + len2 + 2) = 0;
230 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
232 if(filenameW)
233 HeapFree( GetProcessHeap(), 0, filenameW );
234 if(descriptionW)
235 HeapFree( GetProcessHeap(), 0, descriptionW );
237 return hReturnDC;
240 /**********************************************************************
241 * CreateEnhMetaFileW (GDI32.42)
243 HDC WINAPI CreateEnhMetaFileW(
244 HDC hdc, /* optional reference DC */
245 LPCWSTR filename, /* optional filename for disk metafiles */
246 const RECT* rect, /* optional bounding rectangle */
247 LPCWSTR description /* optional description */
250 DC *dc;
251 HDC hRefDC = hdc ? hdc : GetDC(0); /* If no ref, use current display */
252 EMFDRV_PDEVICE *physDev;
253 HFILE hFile;
254 DWORD size = 0, length = 0;
256 TRACE("'%s'\n", debugstr_w(filename) );
258 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
259 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
261 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
262 if (!physDev) {
263 GDI_HEAP_FREE( dc->hSelf );
264 return 0;
266 dc->physDev = physDev;
268 if(description) { /* App name\0Title\0\0 */
269 length = lstrlenW(description);
270 length += lstrlenW(description + length + 1);
271 length += 3;
272 length *= 2;
274 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
276 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
277 HeapFree( GetProcessHeap(), 0, physDev );
278 GDI_HEAP_FREE( dc->hSelf );
279 return 0;
282 physDev->nextHandle = 1;
283 physDev->hFile = 0;
285 physDev->emh->iType = EMR_HEADER;
286 physDev->emh->nSize = size;
288 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
289 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
291 if(rect) {
292 physDev->emh->rclFrame.left = rect->left;
293 physDev->emh->rclFrame.top = rect->top;
294 physDev->emh->rclFrame.right = rect->right;
295 physDev->emh->rclFrame.bottom = rect->bottom;
296 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
297 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
298 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
301 physDev->emh->dSignature = ENHMETA_SIGNATURE;
302 physDev->emh->nVersion = 0x10000;
303 physDev->emh->nBytes = physDev->emh->nSize;
304 physDev->emh->nRecords = 1;
305 physDev->emh->nHandles = 1;
307 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
308 physDev->emh->nDescription = length / 2;
310 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
312 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
314 /* Size in pixels */
315 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
316 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
318 /* Size in millimeters */
319 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
320 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
322 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
324 if (filename) /* disk based metafile */
326 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
327 NULL, CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
328 EMFDRV_DeleteDC( dc );
329 return 0;
331 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
332 EMFDRV_DeleteDC( dc );
333 return 0;
335 physDev->hFile = hFile;
338 if( !hdc )
339 ReleaseDC( 0, hRefDC );
341 TRACE("returning %04x\n", dc->hSelf);
342 return dc->hSelf;
346 /******************************************************************
347 * CloseEnhMetaFile
349 HENHMETAFILE WINAPI CloseEnhMetaFile( HDC hdc /* metafile DC */ )
351 HENHMETAFILE hmf;
352 EMFDRV_PDEVICE *physDev;
353 DC *dc;
354 EMREOF emr;
355 HANDLE hMapping = 0;
357 TRACE("(%04x)\n", hdc );
359 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
360 physDev = (EMFDRV_PDEVICE *)dc->physDev;
362 emr.emr.iType = EMR_EOF;
363 emr.emr.nSize = sizeof(emr);
364 emr.nPalEntries = 0;
365 emr.offPalEntries = 0;
366 emr.nSizeLast = emr.emr.nSize;
367 EMFDRV_WriteRecord( dc, &emr.emr );
369 /* Update rclFrame if not initialized in CreateEnhMetaFile */
370 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
371 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
372 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
373 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
374 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
375 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
376 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
377 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
378 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
381 if (physDev->hFile) /* disk based metafile */
383 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
384 EMFDRV_DeleteDC( dc );
385 return 0;
388 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
389 sizeof(*physDev->emh), NULL, NULL)) {
390 EMFDRV_DeleteDC( dc );
391 return 0;
393 HeapFree( GetProcessHeap(), 0, physDev->emh );
394 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
395 0, NULL);
396 TRACE("hMapping = %08x\n", hMapping );
397 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
398 TRACE("view = %p\n", physDev->emh );
402 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
403 physDev->emh = NULL; /* So it won't be deleted */
404 EMFDRV_DeleteDC( dc );
405 return hmf;