- add documentation section to README
[wine/dcerpc.git] / graphics / enhmetafiledrv / init.c
blob2ef4444129b1e52c72733572c85fa5a8624e6f3a
1 /*
2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
5 */
7 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "gdi.h"
12 #include "enhmetafiledrv.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(enhmetafile);
17 static const DC_FUNCTIONS EMFDRV_Funcs =
19 NULL, /* pAbortDoc */
20 EMFDRV_AbortPath, /* pAbortPath */
21 NULL, /* pAngleArc */
22 EMFDRV_Arc, /* pArc */
23 NULL, /* pArcTo */
24 EMFDRV_BeginPath, /* pBeginPath */
25 NULL, /* pBitBlt */
26 NULL, /* pBitmapBits */
27 NULL, /* pChoosePixelFormat */
28 EMFDRV_Chord, /* pChord */
29 EMFDRV_CloseFigure, /* pCloseFigure */
30 NULL, /* pCreateBitmap */
31 NULL, /* no implementation */ /* pCreateDC */
32 NULL, /* pCreateDIBSection */
33 NULL, /* no implementation */ /* pDeleteDC */
34 NULL, /* pDeleteObject */
35 NULL, /* pDescribePixelFormat */
36 NULL, /* pDeviceCapabilities */
37 EMFDRV_Ellipse, /* pEllipse */
38 NULL, /* pEndDoc */
39 NULL, /* pEndPage */
40 EMFDRV_EndPath, /* pEndPath */
41 NULL, /* pEnumDeviceFonts */
42 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
43 NULL, /* pExtDeviceMode */
44 NULL, /* pExtEscape */
45 EMFDRV_ExtFloodFill, /* pExtFloodFill */
46 NULL, /* pExtTextOut */
47 EMFDRV_FillPath, /* pFillPath */
48 EMFDRV_FillRgn, /* pFillRgn */
49 EMFDRV_FlattenPath, /* pFlattenPath */
50 EMFDRV_FrameRgn, /* pFrameRgn */
51 NULL, /* pGetCharWidth */
52 NULL, /* pGetDCOrgEx */
53 NULL, /* pGetDeviceCaps */
54 NULL, /* pGetDeviceGammaRamp */
55 NULL, /* no implementation */ /* pGetPixel */
56 NULL, /* pGetPixelFormat */
57 NULL, /* pGetTextExtentPoint */
58 NULL, /* pGetTextMetrics */
59 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
60 EMFDRV_InvertRgn, /* pInvertRgn */
61 EMFDRV_LineTo, /* pLineTo */
62 EMFDRV_MoveTo, /* pMoveTo */
63 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
64 NULL, /* pOffsetViewportOrg */
65 NULL, /* pOffsetWindowOrg */
66 EMFDRV_PaintRgn, /* pPaintRgn */
67 NULL, /* pPatBlt */
68 EMFDRV_Pie, /* pPie */
69 NULL, /* pPolyBezier */
70 NULL, /* pPolyBezierTo */
71 NULL, /* pPolyDraw */
72 EMFDRV_PolyPolygon, /* pPolyPolygon */
73 EMFDRV_PolyPolyline, /* pPolyPolyline */
74 EMFDRV_Polygon, /* pPolygon */
75 EMFDRV_Polyline, /* pPolyline */
76 NULL, /* pPolylineTo */
77 NULL, /* pRealizePalette */
78 EMFDRV_Rectangle, /* pRectangle */
79 EMFDRV_RestoreDC, /* pRestoreDC */
80 EMFDRV_RoundRect, /* pRoundRect */
81 EMFDRV_SaveDC, /* pSaveDC */
82 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
83 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
84 EMFDRV_SelectClipPath, /* pSelectClipPath */
85 NULL, /* pSelectClipRgn */
86 EMFDRV_SelectObject, /* pSelectObject */
87 NULL, /* pSelectPalette */
88 EMFDRV_SetBkColor, /* pSetBkColor */
89 EMFDRV_SetBkMode, /* pSetBkMode */
90 NULL, /* pSetDeviceClipping */
91 NULL, /* pSetDeviceGammaRamp */
92 NULL, /* pSetDIBitsToDevice */
93 EMFDRV_SetMapMode, /* pSetMapMode */
94 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
95 NULL, /* pSetPixel */
96 NULL, /* pSetPixelFormat */
97 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
98 EMFDRV_SetROP2, /* pSetROP2 */
99 NULL, /* pSetRelAbs */
100 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
101 EMFDRV_SetTextAlign, /* pSetTextAlign */
102 NULL, /* pSetTextCharacterExtra */
103 EMFDRV_SetTextColor, /* pSetTextColor */
104 NULL, /* pSetTextJustification */
105 EMFDRV_SetViewportExt, /* pSetViewportExt */
106 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
107 EMFDRV_SetWindowExt, /* pSetWindowExt */
108 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
109 NULL, /* pStartDoc */
110 NULL, /* pStartPage */
111 NULL, /* pStretchBlt */
112 NULL, /* pStretchDIBits */
113 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
114 EMFDRV_StrokePath, /* pStrokePath */
115 NULL, /* pSwapBuffers */
116 EMFDRV_WidenPath /* pWiddenPath */
120 /**********************************************************************
121 * EMFDRV_DeleteDC
123 static BOOL EMFDRV_DeleteDC( DC *dc )
125 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
127 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
128 HeapFree( GetProcessHeap(), 0, physDev );
129 dc->physDev = NULL;
130 GDI_FreeObject( dc->hSelf, dc );
131 return TRUE;
135 /******************************************************************
136 * EMFDRV_WriteRecord
138 * Warning: this function can change the pointer to the metafile header.
140 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
142 DWORD len;
143 ENHMETAHEADER *emh;
144 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
146 physDev->emh->nBytes += emr->nSize;
147 physDev->emh->nRecords++;
149 if(physDev->hFile) {
150 TRACE("Writing record to disk\n");
151 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
152 return FALSE;
153 } else {
154 len = physDev->emh->nBytes;
155 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
156 if (!emh) return FALSE;
157 physDev->emh = emh;
158 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
159 emr->nSize);
161 return TRUE;
165 /******************************************************************
166 * EMFDRV_UpdateBBox
168 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
170 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
171 RECTL *bounds = &physDev->emh->rclBounds;
173 if(bounds->left > bounds->right) {/* first rect */
174 *bounds = *rect;
175 return;
177 bounds->left = min(bounds->left, rect->left);
178 bounds->top = min(bounds->top, rect->top);
179 bounds->right = max(bounds->right, rect->right);
180 bounds->bottom = max(bounds->bottom, rect->bottom);
181 return;
184 /******************************************************************
185 * EMFDRV_AddHandleDC
187 * Note: this function assumes that we never delete objects.
188 * If we do someday, we'll need to maintain a table to re-use deleted
189 * handles.
191 int EMFDRV_AddHandleDC( DC *dc )
193 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
194 physDev->emh->nHandles++;
195 return physDev->nextHandle++;
199 /**********************************************************************
200 * CreateEnhMetaFileA (GDI32.@)
202 HDC WINAPI CreateEnhMetaFileA(
203 HDC hdc, /* [in] optional reference DC */
204 LPCSTR filename, /* [in] optional filename for disk metafiles */
205 const RECT *rect, /* [in] optional bounding rectangle */
206 LPCSTR description /* [in] optional description */
209 LPWSTR filenameW = NULL;
210 LPWSTR descriptionW = NULL;
211 HDC hReturnDC;
212 DWORD len1, len2, total;
214 if(filename)
216 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
217 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
218 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
220 if(description) {
221 len1 = strlen(description);
222 len2 = strlen(description + len1 + 1);
223 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
224 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
225 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
228 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
230 if(filenameW)
231 HeapFree( GetProcessHeap(), 0, filenameW );
232 if(descriptionW)
233 HeapFree( GetProcessHeap(), 0, descriptionW );
235 return hReturnDC;
238 /**********************************************************************
239 * CreateEnhMetaFileW (GDI32.@)
241 HDC WINAPI CreateEnhMetaFileW(
242 HDC hdc, /* [in] optional reference DC */
243 LPCWSTR filename, /* [in] optional filename for disk metafiles */
244 const RECT* rect, /* [in] optional bounding rectangle */
245 LPCWSTR description /* [in] optional description */
248 HDC ret;
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_FreeObject( dc->hSelf, dc );
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_FreeObject( dc->hSelf, dc );
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, 0)) == INVALID_HANDLE_VALUE) {
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 TRACE("returning %04x\n", dc->hSelf);
338 ret = dc->hSelf;
339 GDI_ReleaseObj( dc->hSelf );
341 if( !hdc )
342 DeleteDC( hRefDC );
344 return ret;
347 /******************************************************************
348 * CloseEnhMetaFile (GDI32.@)
350 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
352 HENHMETAFILE hmf;
353 EMFDRV_PDEVICE *physDev;
354 DC *dc;
355 EMREOF emr;
356 HANDLE hMapping = 0;
358 TRACE("(%04x)\n", hdc );
360 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
361 physDev = (EMFDRV_PDEVICE *)dc->physDev;
363 emr.emr.iType = EMR_EOF;
364 emr.emr.nSize = sizeof(emr);
365 emr.nPalEntries = 0;
366 emr.offPalEntries = 0;
367 emr.nSizeLast = emr.emr.nSize;
368 EMFDRV_WriteRecord( dc, &emr.emr );
370 /* Update rclFrame if not initialized in CreateEnhMetaFile */
371 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
372 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
373 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
374 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
375 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
376 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
377 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
378 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
379 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
382 if (physDev->hFile) /* disk based metafile */
384 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
386 CloseHandle( physDev->hFile );
387 EMFDRV_DeleteDC( dc );
388 return 0;
391 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
392 sizeof(*physDev->emh), NULL, NULL))
394 CloseHandle( physDev->hFile );
395 EMFDRV_DeleteDC( dc );
396 return 0;
398 HeapFree( GetProcessHeap(), 0, physDev->emh );
399 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
400 0, NULL);
401 TRACE("hMapping = %08x\n", hMapping );
402 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
403 TRACE("view = %p\n", physDev->emh );
404 CloseHandle( hMapping );
405 CloseHandle( physDev->hFile );
408 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
409 physDev->emh = NULL; /* So it won't be deleted */
410 EMFDRV_DeleteDC( dc );
411 return hmf;