A few corrections to bounding boxes.
[wine/gsoc_dplay.git] / graphics / enhmetafiledrv / init.c
blob7133c1f180a63c4ad03cb5762825367f6de96382
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 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, /* pDeviceCapabilities */
41 EMFDRV_Ellipse, /* pEllipse */
42 NULL, /* pEndDoc */
43 NULL, /* pEndPage */
44 EMFDRV_EndPath, /* pEndPath */
45 NULL, /* pEnumDeviceFonts */
46 NULL, /* pEscape */
47 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
48 NULL, /* pExtDeviceMode */
49 EMFDRV_ExtFloodFill, /* pExtFloodFill */
50 NULL, /* pExtTextOut */
51 EMFDRV_FillPath, /* pFillPath */
52 EMFDRV_FillRgn, /* pFillRgn */
53 EMFDRV_FlattenPath, /* pFlattenPath */
54 EMFDRV_FrameRgn, /* pFrameRgn */
55 NULL, /* pGetCharWidth */
56 NULL, /* pGetDCOrgEx */
57 NULL, /* no implementation */ /* pGetPixel */
58 NULL, /* pGetTextExtentPoint */
59 NULL, /* pGetTextMetrics */
60 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
61 EMFDRV_InvertRgn, /* pInvertRgn */
62 EMFDRV_LineTo, /* pLineTo */
63 NULL, /* pLoadOEMResource */
64 EMFDRV_MoveToEx, /* pMoveToEx */
65 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
66 NULL, /* pOffsetViewportOrg */
67 NULL, /* pOffsetWindowOrg */
68 EMFDRV_PaintRgn, /* pPaintRgn */
69 NULL, /* pPatBlt */
70 EMFDRV_Pie, /* pPie */
71 NULL, /* pPolyBezier */
72 NULL, /* pPolyBezierTo */
73 NULL, /* pPolyDraw */
74 EMFDRV_PolyPolygon, /* pPolyPolygon */
75 EMFDRV_PolyPolyline, /* pPolyPolyline */
76 EMFDRV_Polygon, /* pPolygon */
77 EMFDRV_Polyline, /* pPolyline */
78 NULL, /* pPolylineTo */
79 NULL, /* pRealizePalette */
80 EMFDRV_Rectangle, /* pRectangle */
81 EMFDRV_RestoreDC, /* pRestoreDC */
82 EMFDRV_RoundRect, /* pRoundRect */
83 EMFDRV_SaveDC, /* pSaveDC */
84 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
85 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
86 EMFDRV_SelectClipPath, /* pSelectClipPath */
87 NULL, /* pSelectClipRgn */
88 EMFDRV_SelectObject, /* pSelectObject */
89 NULL, /* pSelectPalette */
90 EMFDRV_SetBkColor, /* pSetBkColor */
91 EMFDRV_SetBkMode, /* pSetBkMode */
92 NULL, /* pSetDeviceClipping */
93 NULL, /* pSetDIBitsToDevice */
94 EMFDRV_SetMapMode, /* pSetMapMode */
95 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
96 NULL, /* pSetPixel */
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 EMFDRV_WidenPath /* pWiddenPath */
119 /**********************************************************************
120 * EMFDRV_DeleteDC
122 static BOOL EMFDRV_DeleteDC( DC *dc )
124 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
126 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
127 HeapFree( GetProcessHeap(), 0, physDev );
128 dc->physDev = NULL;
129 GDI_FreeObject(dc->hSelf);
130 return TRUE;
134 /******************************************************************
135 * EMFDRV_WriteRecord
137 * Warning: this function can change the pointer to the metafile header.
139 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
141 DWORD len;
142 ENHMETAHEADER *emh;
143 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
145 physDev->emh->nBytes += emr->nSize;
146 physDev->emh->nRecords++;
148 if(physDev->hFile) {
149 TRACE("Writing record to disk\n");
150 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
151 return FALSE;
152 } else {
153 len = physDev->emh->nBytes;
154 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
155 if (!emh) return FALSE;
156 physDev->emh = emh;
157 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
158 emr->nSize);
160 return TRUE;
164 /******************************************************************
165 * EMFDRV_UpdateBBox
167 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
169 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
170 RECTL *bounds = &physDev->emh->rclBounds;
172 if(bounds->left > bounds->right) {/* first rect */
173 *bounds = *rect;
174 return;
176 bounds->left = min(bounds->left, rect->left);
177 bounds->top = min(bounds->top, rect->top);
178 bounds->right = max(bounds->right, rect->right);
179 bounds->bottom = max(bounds->bottom, rect->bottom);
180 return;
183 /******************************************************************
184 * EMFDRV_AddHandleDC
186 * Note: this function assumes that we never delete objects.
187 * If we do someday, we'll need to maintain a table to re-use deleted
188 * handles.
190 int EMFDRV_AddHandleDC( DC *dc )
192 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
193 physDev->emh->nHandles++;
194 return physDev->nextHandle++;
198 /**********************************************************************
199 * CreateEnhMetaFileA (GDI32.41)
201 HDC WINAPI CreateEnhMetaFileA(
202 HDC hdc, /* optional reference DC */
203 LPCSTR filename, /* optional filename for disk metafiles */
204 const RECT *rect, /* optional bounding rectangle */
205 LPCSTR description /* optional description */
208 LPWSTR filenameW = NULL;
209 LPWSTR descriptionW = NULL;
210 HDC hReturnDC;
211 DWORD len1, len2;
213 if(filename)
214 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
216 if(description) {
217 len1 = strlen(description);
218 len2 = strlen(description + len1 + 1);
219 descriptionW = HeapAlloc( GetProcessHeap(), 0, (len1 + len2 + 3) * 2);
220 lstrcpyAtoW(descriptionW, description );
221 lstrcpyAtoW(descriptionW + len1 + 1 , description + len1 + 1);
222 *(descriptionW + len1 + len2 + 2) = 0;
225 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
227 if(filenameW)
228 HeapFree( GetProcessHeap(), 0, filenameW );
229 if(descriptionW)
230 HeapFree( GetProcessHeap(), 0, descriptionW );
232 return hReturnDC;
235 /**********************************************************************
236 * CreateEnhMetaFileW (GDI32.42)
238 HDC WINAPI CreateEnhMetaFileW(
239 HDC hdc, /* optional reference DC */
240 LPCWSTR filename, /* optional filename for disk metafiles */
241 const RECT* rect, /* optional bounding rectangle */
242 LPCWSTR description /* optional description */
245 DC *dc;
246 HDC hRefDC = hdc ? hdc : GetDC(0); /* If no ref, use current display */
247 EMFDRV_PDEVICE *physDev;
248 HFILE hFile;
249 DWORD size = 0, length = 0;
251 TRACE("'%s'\n", debugstr_w(filename) );
253 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
254 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
256 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
257 if (!physDev) {
258 GDI_HEAP_FREE( dc->hSelf );
259 return 0;
261 dc->physDev = physDev;
263 if(description) { /* App name\0Title\0\0 */
264 length = lstrlenW(description);
265 length += lstrlenW(description + length + 1);
266 length += 3;
267 length *= 2;
269 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
271 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
272 HeapFree( GetProcessHeap(), 0, physDev );
273 GDI_HEAP_FREE( dc->hSelf );
274 return 0;
277 physDev->nextHandle = 1;
278 physDev->hFile = 0;
280 physDev->emh->iType = EMR_HEADER;
281 physDev->emh->nSize = size;
283 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
284 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
286 if(rect) {
287 physDev->emh->rclFrame.left = rect->left;
288 physDev->emh->rclFrame.top = rect->top;
289 physDev->emh->rclFrame.right = rect->right;
290 physDev->emh->rclFrame.bottom = rect->bottom;
291 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
292 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
293 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
296 physDev->emh->dSignature = ENHMETA_SIGNATURE;
297 physDev->emh->nVersion = 0x10000;
298 physDev->emh->nBytes = physDev->emh->nSize;
299 physDev->emh->nRecords = 1;
300 physDev->emh->nHandles = 1;
302 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
303 physDev->emh->nDescription = length / 2;
305 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
307 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
309 /* Size in pixels */
310 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
311 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
313 /* Size in millimeters */
314 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
315 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
317 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
319 if (filename) /* disk based metafile */
321 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
322 NULL, CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
323 EMFDRV_DeleteDC( dc );
324 return 0;
326 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
327 EMFDRV_DeleteDC( dc );
328 return 0;
330 physDev->hFile = hFile;
333 if( !hdc )
334 ReleaseDC( 0, hRefDC );
336 TRACE("returning %04x\n", dc->hSelf);
337 return dc->hSelf;
341 /******************************************************************
342 * CloseEnhMetaFile
344 HENHMETAFILE WINAPI CloseEnhMetaFile( HDC hdc /* metafile DC */ )
346 HENHMETAFILE hmf;
347 EMFDRV_PDEVICE *physDev;
348 DC *dc;
349 EMREOF emr;
350 HANDLE hMapping = 0;
352 TRACE("(%04x)\n", hdc );
354 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
355 physDev = (EMFDRV_PDEVICE *)dc->physDev;
357 emr.emr.iType = EMR_EOF;
358 emr.emr.nSize = sizeof(emr);
359 emr.nPalEntries = 0;
360 emr.offPalEntries = 0;
361 emr.nSizeLast = emr.emr.nSize;
362 EMFDRV_WriteRecord( dc, &emr.emr );
364 /* Update rclFrame if not initialized in CreateEnhMetaFile */
365 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
366 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
367 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
368 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
369 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
370 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
371 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
372 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
373 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
376 if (physDev->hFile) /* disk based metafile */
378 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
379 EMFDRV_DeleteDC( dc );
380 return 0;
383 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
384 sizeof(*physDev->emh), NULL, NULL)) {
385 EMFDRV_DeleteDC( dc );
386 return 0;
388 HeapFree( GetProcessHeap(), 0, physDev->emh );
389 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
390 0, NULL);
391 TRACE("hMapping = %08x\n", hMapping );
392 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
393 TRACE("view = %p\n", physDev->emh );
397 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
398 physDev->emh = NULL; /* So it won't be deleted */
399 EMFDRV_DeleteDC( dc );
400 return hmf;