Replaced GetDC by CreateDC.
[wine.git] / graphics / enhmetafiledrv / init.c
blob9a8e259b8a2c7012133512409f6b3a109772b835
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"
17 #include <string.h>
19 DEFAULT_DEBUG_CHANNEL(enhmetafile)
21 static const DC_FUNCTIONS EMFDRV_Funcs =
23 NULL, /* pAbortDoc */
24 EMFDRV_AbortPath, /* pAbortPath */
25 NULL, /* pAngleArc */
26 EMFDRV_Arc, /* pArc */
27 NULL, /* pArcTo */
28 EMFDRV_BeginPath, /* pBeginPath */
29 NULL, /* pBitBlt */
30 NULL, /* pBitmapBits */
31 NULL, /* pChoosePixelFormat */
32 EMFDRV_Chord, /* pChord */
33 EMFDRV_CloseFigure, /* pCloseFigure */
34 NULL, /* pCreateBitmap */
35 NULL, /* no implementation */ /* pCreateDC */
36 NULL, /* pCreateDIBSection */
37 NULL, /* pCreateDIBSection16 */
38 NULL, /* no implementation */ /* pDeleteDC */
39 NULL, /* pDeleteObject */
40 NULL, /* pDescribePixelFormat */
41 NULL, /* pDeviceCapabilities */
42 EMFDRV_Ellipse, /* pEllipse */
43 NULL, /* pEndDoc */
44 NULL, /* pEndPage */
45 EMFDRV_EndPath, /* pEndPath */
46 NULL, /* pEnumDeviceFonts */
47 NULL, /* pEscape */
48 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
49 NULL, /* pExtDeviceMode */
50 EMFDRV_ExtFloodFill, /* pExtFloodFill */
51 NULL, /* pExtTextOut */
52 EMFDRV_FillPath, /* pFillPath */
53 EMFDRV_FillRgn, /* pFillRgn */
54 EMFDRV_FlattenPath, /* pFlattenPath */
55 EMFDRV_FrameRgn, /* pFrameRgn */
56 NULL, /* pGetCharWidth */
57 NULL, /* pGetDCOrgEx */
58 NULL, /* no implementation */ /* pGetPixel */
59 NULL, /* pGetPixelFormat */
60 NULL, /* pGetTextExtentPoint */
61 NULL, /* pGetTextMetrics */
62 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
63 EMFDRV_InvertRgn, /* pInvertRgn */
64 EMFDRV_LineTo, /* pLineTo */
65 NULL, /* pLoadOEMResource */
66 EMFDRV_MoveToEx, /* pMoveToEx */
67 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
68 NULL, /* pOffsetViewportOrg */
69 NULL, /* pOffsetWindowOrg */
70 EMFDRV_PaintRgn, /* pPaintRgn */
71 NULL, /* pPatBlt */
72 EMFDRV_Pie, /* pPie */
73 NULL, /* pPolyBezier */
74 NULL, /* pPolyBezierTo */
75 NULL, /* pPolyDraw */
76 EMFDRV_PolyPolygon, /* pPolyPolygon */
77 EMFDRV_PolyPolyline, /* pPolyPolyline */
78 EMFDRV_Polygon, /* pPolygon */
79 EMFDRV_Polyline, /* pPolyline */
80 NULL, /* pPolylineTo */
81 NULL, /* pRealizePalette */
82 EMFDRV_Rectangle, /* pRectangle */
83 EMFDRV_RestoreDC, /* pRestoreDC */
84 EMFDRV_RoundRect, /* pRoundRect */
85 EMFDRV_SaveDC, /* pSaveDC */
86 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
87 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
88 EMFDRV_SelectClipPath, /* pSelectClipPath */
89 NULL, /* pSelectClipRgn */
90 EMFDRV_SelectObject, /* pSelectObject */
91 NULL, /* pSelectPalette */
92 EMFDRV_SetBkColor, /* pSetBkColor */
93 EMFDRV_SetBkMode, /* pSetBkMode */
94 NULL, /* pSetDeviceClipping */
95 NULL, /* pSetDIBitsToDevice */
96 EMFDRV_SetMapMode, /* pSetMapMode */
97 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
98 NULL, /* pSetPixel */
99 NULL, /* pSetPixelFormat */
100 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
101 EMFDRV_SetROP2, /* pSetROP2 */
102 NULL, /* pSetRelAbs */
103 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
104 EMFDRV_SetTextAlign, /* pSetTextAlign */
105 NULL, /* pSetTextCharacterExtra */
106 EMFDRV_SetTextColor, /* pSetTextColor */
107 NULL, /* pSetTextJustification */
108 EMFDRV_SetViewportExt, /* pSetViewportExt */
109 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
110 EMFDRV_SetWindowExt, /* pSetWindowExt */
111 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
112 NULL, /* pStartDoc */
113 NULL, /* pStartPage */
114 NULL, /* pStretchBlt */
115 NULL, /* pStretchDIBits */
116 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
117 EMFDRV_StrokePath, /* pStrokePath */
118 NULL, /* pSwapBuffers */
119 EMFDRV_WidenPath /* pWiddenPath */
123 /**********************************************************************
124 * EMFDRV_DeleteDC
126 static BOOL EMFDRV_DeleteDC( DC *dc )
128 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
130 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
131 HeapFree( GetProcessHeap(), 0, physDev );
132 dc->physDev = NULL;
133 GDI_FreeObject(dc->hSelf);
134 return TRUE;
138 /******************************************************************
139 * EMFDRV_WriteRecord
141 * Warning: this function can change the pointer to the metafile header.
143 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
145 DWORD len;
146 ENHMETAHEADER *emh;
147 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
149 physDev->emh->nBytes += emr->nSize;
150 physDev->emh->nRecords++;
152 if(physDev->hFile) {
153 TRACE("Writing record to disk\n");
154 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
155 return FALSE;
156 } else {
157 len = physDev->emh->nBytes;
158 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
159 if (!emh) return FALSE;
160 physDev->emh = emh;
161 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
162 emr->nSize);
164 return TRUE;
168 /******************************************************************
169 * EMFDRV_UpdateBBox
171 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
173 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
174 RECTL *bounds = &physDev->emh->rclBounds;
176 if(bounds->left > bounds->right) {/* first rect */
177 *bounds = *rect;
178 return;
180 bounds->left = min(bounds->left, rect->left);
181 bounds->top = min(bounds->top, rect->top);
182 bounds->right = max(bounds->right, rect->right);
183 bounds->bottom = max(bounds->bottom, rect->bottom);
184 return;
187 /******************************************************************
188 * EMFDRV_AddHandleDC
190 * Note: this function assumes that we never delete objects.
191 * If we do someday, we'll need to maintain a table to re-use deleted
192 * handles.
194 int EMFDRV_AddHandleDC( DC *dc )
196 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
197 physDev->emh->nHandles++;
198 return physDev->nextHandle++;
202 /**********************************************************************
203 * CreateEnhMetaFileA (GDI32.41)
205 HDC WINAPI CreateEnhMetaFileA(
206 HDC hdc, /* optional reference DC */
207 LPCSTR filename, /* optional filename for disk metafiles */
208 const RECT *rect, /* optional bounding rectangle */
209 LPCSTR description /* optional description */
212 LPWSTR filenameW = NULL;
213 LPWSTR descriptionW = NULL;
214 HDC hReturnDC;
215 DWORD len1, len2;
217 if(filename)
218 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
220 if(description) {
221 len1 = strlen(description);
222 len2 = strlen(description + len1 + 1);
223 descriptionW = HeapAlloc( GetProcessHeap(), 0, (len1 + len2 + 3) * 2);
224 lstrcpyAtoW(descriptionW, description );
225 lstrcpyAtoW(descriptionW + len1 + 1 , description + len1 + 1);
226 *(descriptionW + len1 + len2 + 2) = 0;
229 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
231 if(filenameW)
232 HeapFree( GetProcessHeap(), 0, filenameW );
233 if(descriptionW)
234 HeapFree( GetProcessHeap(), 0, descriptionW );
236 return hReturnDC;
239 /**********************************************************************
240 * CreateEnhMetaFileW (GDI32.42)
242 HDC WINAPI CreateEnhMetaFileW(
243 HDC hdc, /* optional reference DC */
244 LPCWSTR filename, /* optional filename for disk metafiles */
245 const RECT* rect, /* optional bounding rectangle */
246 LPCWSTR description /* optional description */
249 DC *dc;
250 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
251 EMFDRV_PDEVICE *physDev;
252 HFILE hFile;
253 DWORD size = 0, length = 0;
255 TRACE("'%s'\n", debugstr_w(filename) );
257 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
258 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
260 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
261 if (!physDev) {
262 GDI_HEAP_FREE( dc->hSelf );
263 return 0;
265 dc->physDev = physDev;
267 if(description) { /* App name\0Title\0\0 */
268 length = lstrlenW(description);
269 length += lstrlenW(description + length + 1);
270 length += 3;
271 length *= 2;
273 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
275 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
276 HeapFree( GetProcessHeap(), 0, physDev );
277 GDI_HEAP_FREE( dc->hSelf );
278 return 0;
281 physDev->nextHandle = 1;
282 physDev->hFile = 0;
284 physDev->emh->iType = EMR_HEADER;
285 physDev->emh->nSize = size;
287 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
288 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
290 if(rect) {
291 physDev->emh->rclFrame.left = rect->left;
292 physDev->emh->rclFrame.top = rect->top;
293 physDev->emh->rclFrame.right = rect->right;
294 physDev->emh->rclFrame.bottom = rect->bottom;
295 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
296 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
297 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
300 physDev->emh->dSignature = ENHMETA_SIGNATURE;
301 physDev->emh->nVersion = 0x10000;
302 physDev->emh->nBytes = physDev->emh->nSize;
303 physDev->emh->nRecords = 1;
304 physDev->emh->nHandles = 1;
306 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
307 physDev->emh->nDescription = length / 2;
309 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
311 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
313 /* Size in pixels */
314 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
315 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
317 /* Size in millimeters */
318 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
319 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
321 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
323 if (filename) /* disk based metafile */
325 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
326 NULL, CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
327 EMFDRV_DeleteDC( dc );
328 return 0;
330 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
331 EMFDRV_DeleteDC( dc );
332 return 0;
334 physDev->hFile = hFile;
337 if( !hdc )
338 DeleteDC( hRefDC );
340 TRACE("returning %04x\n", dc->hSelf);
341 return dc->hSelf;
345 /******************************************************************
346 * CloseEnhMetaFile
348 HENHMETAFILE WINAPI CloseEnhMetaFile( HDC hdc /* metafile DC */ )
350 HENHMETAFILE hmf;
351 EMFDRV_PDEVICE *physDev;
352 DC *dc;
353 EMREOF emr;
354 HANDLE hMapping = 0;
356 TRACE("(%04x)\n", hdc );
358 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
359 physDev = (EMFDRV_PDEVICE *)dc->physDev;
361 emr.emr.iType = EMR_EOF;
362 emr.emr.nSize = sizeof(emr);
363 emr.nPalEntries = 0;
364 emr.offPalEntries = 0;
365 emr.nSizeLast = emr.emr.nSize;
366 EMFDRV_WriteRecord( dc, &emr.emr );
368 /* Update rclFrame if not initialized in CreateEnhMetaFile */
369 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
370 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
371 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
372 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
373 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
374 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
375 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
376 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
377 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
380 if (physDev->hFile) /* disk based metafile */
382 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
383 EMFDRV_DeleteDC( dc );
384 return 0;
387 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
388 sizeof(*physDev->emh), NULL, NULL)) {
389 EMFDRV_DeleteDC( dc );
390 return 0;
392 HeapFree( GetProcessHeap(), 0, physDev->emh );
393 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
394 0, NULL);
395 TRACE("hMapping = %08x\n", hMapping );
396 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
397 TRACE("view = %p\n", physDev->emh );
401 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
402 physDev->emh = NULL; /* So it won't be deleted */
403 EMFDRV_DeleteDC( dc );
404 return hmf;