Corrected operand sizes for the "enter" instruction.
[wine.git] / graphics / enhmetafiledrv / init.c
blobfb51af6bec6bbbf8731d12f29e5556d2ae9048b5
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_Arc, /* pArc */
25 NULL, /* pBitBlt */
26 NULL, /* pBitmapBits */
27 EMFDRV_Chord, /* pChord */
28 NULL, /* pCreateBitmap */
29 NULL, /* no implementation */ /* pCreateDC */
30 NULL, /* pCreateDIBSection */
31 NULL, /* pCreateDIBSection16 */
32 NULL, /* no implementation */ /* pDeleteDC */
33 NULL, /* pDeleteObject */
34 NULL, /* pDeviceCapabilities */
35 EMFDRV_Ellipse, /* pEllipse */
36 NULL, /* pEndDoc */
37 NULL, /* pEndPage */
38 NULL, /* pEnumDeviceFonts */
39 NULL, /* pEscape */
40 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
41 NULL, /* pExtDeviceMode */
42 EMFDRV_ExtFloodFill, /* pExtFloodFill */
43 NULL, /* pExtTextOut */
44 EMFDRV_FillRgn, /* pFillRgn */
45 EMFDRV_FrameRgn, /* pFrameRgn */
46 NULL, /* pGetCharWidth */
47 NULL, /* no implementation */ /* pGetPixel */
48 NULL, /* pGetTextExtentPoint */
49 NULL, /* pGetTextMetrics */
50 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
51 EMFDRV_InvertRgn, /* pInvertRgn */
52 EMFDRV_LineTo, /* pLineTo */
53 NULL, /* pLoadOEMResource */
54 EMFDRV_MoveToEx, /* pMoveToEx */
55 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
56 NULL, /* pOffsetViewportOrg */
57 NULL, /* pOffsetWindowOrg */
58 EMFDRV_PaintRgn, /* pPaintRgn */
59 NULL, /* pPatBlt */
60 EMFDRV_Pie, /* pPie */
61 EMFDRV_PolyPolygon, /* pPolyPolygon */
62 EMFDRV_PolyPolyline, /* pPolyPolyline */
63 EMFDRV_Polygon, /* pPolygon */
64 EMFDRV_Polyline, /* pPolyline */
65 NULL, /* pPolyBezier */
66 NULL, /* pRealizePalette */
67 EMFDRV_Rectangle, /* pRectangle */
68 EMFDRV_RestoreDC, /* pRestoreDC */
69 EMFDRV_RoundRect, /* pRoundRect */
70 EMFDRV_SaveDC, /* pSaveDC */
71 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
72 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
73 NULL, /* pSelectClipRgn */
74 EMFDRV_SelectObject, /* pSelectObject */
75 NULL, /* pSelectPalette */
76 EMFDRV_SetBkColor, /* pSetBkColor */
77 EMFDRV_SetBkMode, /* pSetBkMode */
78 NULL, /* pSetDeviceClipping */
79 NULL, /* pSetDIBitsToDevice */
80 EMFDRV_SetMapMode, /* pSetMapMode */
81 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
82 NULL, /* pSetPixel */
83 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
84 EMFDRV_SetROP2, /* pSetROP2 */
85 NULL, /* pSetRelAbs */
86 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
87 EMFDRV_SetTextAlign, /* pSetTextAlign */
88 NULL, /* pSetTextCharacterExtra */
89 EMFDRV_SetTextColor, /* pSetTextColor */
90 NULL, /* pSetTextJustification */
91 EMFDRV_SetViewportExt, /* pSetViewportExt */
92 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
93 EMFDRV_SetWindowExt, /* pSetWindowExt */
94 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
95 NULL, /* pStartDoc */
96 NULL, /* pStartPage */
97 NULL, /* pStretchBlt */
98 NULL /* pStretchDIBits */
102 /**********************************************************************
103 * EMFDRV_DeleteDC
105 static BOOL EMFDRV_DeleteDC( DC *dc )
107 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
109 if (physDev->emh) HeapFree( SystemHeap, 0, physDev->emh );
110 HeapFree( SystemHeap, 0, physDev );
111 dc->physDev = NULL;
112 GDI_FreeObject(dc->hSelf);
113 return TRUE;
117 /******************************************************************
118 * EMFDRV_WriteRecord
120 * Warning: this function can change the pointer to the metafile header.
122 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
124 DWORD len;
125 ENHMETAHEADER *emh;
126 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
128 physDev->emh->nBytes += emr->nSize;
129 physDev->emh->nRecords++;
131 if(physDev->hFile) {
132 TRACE("Writing record to disk\n");
133 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
134 return FALSE;
135 } else {
136 len = physDev->emh->nBytes;
137 emh = HeapReAlloc( SystemHeap, 0, physDev->emh, len );
138 if (!emh) return FALSE;
139 physDev->emh = emh;
140 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
141 emr->nSize);
143 return TRUE;
147 /******************************************************************
148 * EMFDRV_UpdateBBox
150 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
152 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
153 RECTL *bounds = &physDev->emh->rclBounds;
155 if(bounds->left > bounds->right) {/* first rect */
156 *bounds = *rect;
157 return;
159 bounds->left = MIN(bounds->left, rect->left);
160 bounds->top = MIN(bounds->top, rect->top);
161 bounds->right = MAX(bounds->right, rect->right);
162 bounds->bottom = MAX(bounds->bottom, rect->bottom);
163 return;
166 /******************************************************************
167 * EMFDRV_AddHandleDC
169 * Note: this function assumes that we never delete objects.
170 * If we do someday, we'll need to maintain a table to re-use deleted
171 * handles.
173 int EMFDRV_AddHandleDC( DC *dc )
175 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
176 physDev->emh->nHandles++;
177 return physDev->nextHandle++;
181 /**********************************************************************
182 * CreateEnhMetaFileA (GDI32.41)
184 HDC WINAPI CreateEnhMetaFileA(
185 HDC hdc, /* optional reference DC */
186 LPCSTR filename, /* optional filename for disk metafiles */
187 const RECT *rect, /* optional bounding rectangle */
188 LPCSTR description /* optional description */
191 LPWSTR filenameW = NULL;
192 LPWSTR descriptionW = NULL;
193 HDC hReturnDC;
194 DWORD len1, len2;
196 if(filename)
197 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
199 if(description) {
200 len1 = strlen(description);
201 len2 = strlen(description + len1 + 1);
202 descriptionW = HeapAlloc( GetProcessHeap(), 0, (len1 + len2 + 3) * 2);
203 lstrcpyAtoW(descriptionW, description );
204 lstrcpyAtoW(descriptionW + len1 + 1 , description + len1 + 1);
205 *(descriptionW + len1 + len2 + 2) = 0;
208 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
210 if(filenameW)
211 HeapFree( GetProcessHeap(), 0, filenameW );
212 if(descriptionW)
213 HeapFree( GetProcessHeap(), 0, descriptionW );
215 return hReturnDC;
218 /**********************************************************************
219 * CreateEnhMetaFileW (GDI32.42)
221 HDC WINAPI CreateEnhMetaFileW(
222 HDC hdc, /* optional reference DC */
223 LPCWSTR filename, /* optional filename for disk metafiles */
224 const RECT* rect, /* optional bounding rectangle */
225 LPCWSTR description /* optional description */
228 DC *dc;
229 EMFDRV_PDEVICE *physDev;
230 HFILE hFile;
231 DWORD size = 0, length = 0;
233 TRACE("'%s'\n", debugstr_w(filename) );
235 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
236 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
238 physDev = (EMFDRV_PDEVICE *)HeapAlloc(SystemHeap,0,sizeof(*physDev));
239 if (!physDev) {
240 GDI_HEAP_FREE( dc->hSelf );
241 return 0;
243 dc->physDev = physDev;
245 if(description) { /* App name\0Title\0\0 */
246 length = lstrlenW(description);
247 length += lstrlenW(description + length + 1);
248 length += 3;
249 length *= 2;
251 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
253 if (!(physDev->emh = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, size))) {
254 HeapFree( SystemHeap, 0, physDev );
255 GDI_HEAP_FREE( dc->hSelf );
256 return 0;
259 physDev->nextHandle = 1;
260 physDev->hFile = 0;
262 physDev->emh->iType = EMR_HEADER;
263 physDev->emh->nSize = size;
264 physDev->emh->dSignature = ENHMETA_SIGNATURE;
265 physDev->emh->nVersion = 0x10000;
266 physDev->emh->nBytes = physDev->emh->nSize;
267 physDev->emh->nRecords = 1;
268 physDev->emh->nHandles = 1;
269 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
270 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
271 physDev->emh->nDescription = length / 2;
272 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
273 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
275 if (filename) /* disk based metafile */
277 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
278 NULL, CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
279 EMFDRV_DeleteDC( dc );
280 return 0;
282 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
283 EMFDRV_DeleteDC( dc );
284 return 0;
286 physDev->hFile = hFile;
289 TRACE("returning %04x\n", dc->hSelf);
290 return dc->hSelf;
294 /******************************************************************
295 * CloseEnhMetaFile
297 HENHMETAFILE WINAPI CloseEnhMetaFile( HDC hdc /* metafile DC */ )
299 HENHMETAFILE hmf;
300 EMFDRV_PDEVICE *physDev;
301 DC *dc;
302 EMREOF emr;
303 HANDLE hMapping = 0;
305 TRACE("(%04x)\n", hdc );
307 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
308 physDev = (EMFDRV_PDEVICE *)dc->physDev;
310 emr.emr.iType = EMR_EOF;
311 emr.emr.nSize = sizeof(emr);
312 emr.nPalEntries = 0;
313 emr.offPalEntries = 0;
314 emr.nSizeLast = emr.emr.nSize;
315 EMFDRV_WriteRecord( dc, &emr.emr );
317 if (physDev->hFile) /* disk based metafile */
319 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
320 EMFDRV_DeleteDC( dc );
321 return 0;
324 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
325 sizeof(*physDev->emh), NULL, NULL)) {
326 EMFDRV_DeleteDC( dc );
327 return 0;
329 HeapFree( SystemHeap, 0, physDev->emh );
330 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
331 0, NULL);
332 TRACE("hMapping = %08x\n", hMapping );
333 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
334 TRACE("view = %p\n", physDev->emh );
338 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
339 physDev->emh = NULL; /* So it won't be deleted */
340 EMFDRV_DeleteDC( dc );
341 return hmf;