VxDCall functions do not need to be 'register'.
[wine/multimedia.git] / graphics / enhmetafiledrv / init.c
blob083c2f32fc4321a5484b556c3e12def26400d9bf
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 "debug.h"
17 #include <string.h>
19 DEFAULT_DEBUG_CHANNEL(enhmetafile)
21 static const DC_FUNCTIONS EMFDRV_Funcs =
23 EMFDRV_Arc, /* pArc */
24 NULL, /* pBitBlt */
25 NULL, /* pBitmapBits */
26 EMFDRV_Chord, /* pChord */
27 NULL, /* pCreateBitmap */
28 NULL, /* no implementation */ /* pCreateDC */
29 NULL, /* no implementation */ /* pDeleteDC */
30 NULL, /* pCreateDIBSection */
31 NULL, /* pCreateDIBSection16 */
32 NULL, /* pDeleteObject */
33 EMFDRV_Ellipse, /* pEllipse */
34 NULL, /* pEnumDeviceFonts */
35 NULL, /* pEscape */
36 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
37 EMFDRV_ExtFloodFill, /* pExtFloodFill */
38 NULL, /* pExtTextOut */
39 EMFDRV_FillRgn, /* pFillRgn */
40 EMFDRV_FrameRgn, /* pFrameRgn */
41 NULL, /* pGetCharWidth */
42 NULL, /* no implementation */ /* pGetPixel */
43 NULL, /* pGetTextExtentPoint */
44 NULL, /* pGetTextMetrics */
45 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
46 EMFDRV_InvertRgn, /* pInvertRgn */
47 EMFDRV_LineTo, /* pLineTo */
48 NULL, /* pLoadOEMResource */
49 EMFDRV_MoveToEx, /* pMoveToEx */
50 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
51 NULL, /* pOffsetViewportOrg */
52 NULL, /* pOffsetWindowOrg */
53 EMFDRV_PaintRgn, /* pPaintRgn */
54 NULL, /* pPatBlt */
55 EMFDRV_Pie, /* pPie */
56 EMFDRV_PolyPolygon, /* pPolyPolygon */
57 EMFDRV_PolyPolyline, /* pPolyPolyline */
58 EMFDRV_Polygon, /* pPolygon */
59 EMFDRV_Polyline, /* pPolyline */
60 NULL, /* pPolyBezier */
61 NULL, /* pRealizePalette */
62 EMFDRV_Rectangle, /* pRectangle */
63 EMFDRV_RestoreDC, /* pRestoreDC */
64 EMFDRV_RoundRect, /* pRoundRect */
65 EMFDRV_SaveDC, /* pSaveDC */
66 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
67 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
68 NULL, /* pSelectClipRgn */
69 EMFDRV_SelectObject, /* pSelectObject */
70 NULL, /* pSelectPalette */
71 EMFDRV_SetBkColor, /* pSetBkColor */
72 EMFDRV_SetBkMode, /* pSetBkMode */
73 NULL, /* pSetDeviceClipping */
74 NULL, /* pSetDIBitsToDevice */
75 EMFDRV_SetMapMode, /* pSetMapMode */
76 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
77 NULL, /* pSetPixel */
78 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
79 EMFDRV_SetROP2, /* pSetROP2 */
80 NULL, /* pSetRelAbs */
81 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
82 EMFDRV_SetTextAlign, /* pSetTextAlign */
83 NULL, /* pSetTextCharacterExtra */
84 EMFDRV_SetTextColor, /* pSetTextColor */
85 NULL, /* pSetTextJustification */
86 EMFDRV_SetViewportExt, /* pSetViewportExt */
87 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
88 EMFDRV_SetWindowExt, /* pSetWindowExt */
89 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
90 NULL, /* pStretchBlt */
91 NULL /* pStretchDIBits */
95 /**********************************************************************
96 * EMFDRV_DeleteDC
98 static BOOL EMFDRV_DeleteDC( DC *dc )
100 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
102 if (physDev->emh) HeapFree( SystemHeap, 0, physDev->emh );
103 HeapFree( SystemHeap, 0, physDev );
104 dc->physDev = NULL;
105 GDI_FreeObject(dc->hSelf);
106 return TRUE;
110 /******************************************************************
111 * EMFDRV_WriteRecord
113 * Warning: this function can change the pointer to the metafile header.
115 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
117 DWORD len;
118 ENHMETAHEADER *emh;
119 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
121 physDev->emh->nBytes += emr->nSize;
122 physDev->emh->nRecords++;
124 if(physDev->hFile) {
125 TRACE(enhmetafile,"Writing record to disk\n");
126 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
127 return FALSE;
128 } else {
129 len = physDev->emh->nBytes;
130 emh = HeapReAlloc( SystemHeap, 0, physDev->emh, len );
131 if (!emh) return FALSE;
132 physDev->emh = emh;
133 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
134 emr->nSize);
136 return TRUE;
140 /******************************************************************
141 * EMFDRV_UpdateBBox
143 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
145 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
146 RECTL *bounds = &physDev->emh->rclBounds;
148 if(bounds->left > bounds->right) {/* first rect */
149 *bounds = *rect;
150 return;
152 bounds->left = MIN(bounds->left, rect->left);
153 bounds->top = MIN(bounds->top, rect->top);
154 bounds->right = MAX(bounds->right, rect->right);
155 bounds->bottom = MAX(bounds->bottom, rect->bottom);
156 return;
159 /******************************************************************
160 * EMFDRV_AddHandleDC
162 * Note: this function assumes that we never delete objects.
163 * If we do someday, we'll need to maintain a table to re-use deleted
164 * handles.
166 int EMFDRV_AddHandleDC( DC *dc )
168 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
169 physDev->emh->nHandles++;
170 return physDev->nextHandle++;
174 /**********************************************************************
175 * CreateEnhMetaFileA (GDI32.41)
177 HDC WINAPI CreateEnhMetaFileA(
178 HDC hdc, /* optional reference DC */
179 LPCSTR filename, /* optional filename for disk metafiles */
180 const RECT *rect, /* optional bounding rectangle */
181 LPCSTR description /* optional description */
184 LPWSTR filenameW = NULL;
185 LPWSTR descriptionW = NULL;
186 HDC hReturnDC;
187 DWORD len1, len2;
189 if(filename)
190 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
192 if(description) {
193 len1 = strlen(description);
194 len2 = strlen(description + len1 + 1);
195 descriptionW = HeapAlloc( GetProcessHeap(), 0, (len1 + len2 + 3) * 2);
196 lstrcpyAtoW(descriptionW, description );
197 lstrcpyAtoW(descriptionW + len1 + 1 , description + len1 + 1);
198 *(descriptionW + len1 + len2 + 2) = 0;
201 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
203 if(filenameW)
204 HeapFree( GetProcessHeap(), 0, filenameW );
205 if(descriptionW)
206 HeapFree( GetProcessHeap(), 0, descriptionW );
208 return hReturnDC;
211 /**********************************************************************
212 * CreateEnhMetaFileW (GDI32.42)
214 HDC WINAPI CreateEnhMetaFileW(
215 HDC hdc, /* optional reference DC */
216 LPCWSTR filename, /* optional filename for disk metafiles */
217 const RECT* rect, /* optional bounding rectangle */
218 LPCWSTR description /* optional description */
221 DC *dc;
222 EMFDRV_PDEVICE *physDev;
223 HFILE hFile;
224 DWORD size = 0, length = 0;
226 TRACE(enhmetafile, "'%s'\n", debugstr_w(filename) );
228 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
229 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
231 physDev = (EMFDRV_PDEVICE *)HeapAlloc(SystemHeap,0,sizeof(*physDev));
232 if (!physDev) {
233 GDI_HEAP_FREE( dc->hSelf );
234 return 0;
236 dc->physDev = physDev;
238 if(description) { /* App name\0Title\0\0 */
239 length = lstrlenW(description);
240 length += lstrlenW(description + length + 1);
241 length += 3;
242 length *= 2;
244 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
246 if (!(physDev->emh = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, size))) {
247 HeapFree( SystemHeap, 0, physDev );
248 GDI_HEAP_FREE( dc->hSelf );
249 return 0;
252 physDev->nextHandle = 1;
253 physDev->hFile = 0;
255 physDev->emh->iType = EMR_HEADER;
256 physDev->emh->nSize = size;
257 physDev->emh->dSignature = ENHMETA_SIGNATURE;
258 physDev->emh->nVersion = 0x10000;
259 physDev->emh->nBytes = physDev->emh->nSize;
260 physDev->emh->nRecords = 1;
261 physDev->emh->nHandles = 1;
262 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
263 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
264 physDev->emh->nDescription = length / 2;
265 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
266 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
268 if (filename) /* disk based metafile */
270 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
271 NULL, CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
272 EMFDRV_DeleteDC( dc );
273 return 0;
275 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
276 EMFDRV_DeleteDC( dc );
277 return 0;
279 physDev->hFile = hFile;
282 TRACE(enhmetafile, "returning %04x\n", dc->hSelf);
283 return dc->hSelf;
287 /******************************************************************
288 * CloseEnhMetaFile
290 HENHMETAFILE WINAPI CloseEnhMetaFile( HDC hdc /* metafile DC */ )
292 HENHMETAFILE hmf;
293 EMFDRV_PDEVICE *physDev;
294 DC *dc;
295 EMREOF emr;
296 HANDLE hMapping = 0;
298 TRACE(enhmetafile, "(%04x)\n", hdc );
300 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
301 physDev = (EMFDRV_PDEVICE *)dc->physDev;
303 emr.emr.iType = EMR_EOF;
304 emr.emr.nSize = sizeof(emr);
305 emr.nPalEntries = 0;
306 emr.offPalEntries = 0;
307 emr.nSizeLast = emr.emr.nSize;
308 EMFDRV_WriteRecord( dc, &emr.emr );
310 if (physDev->hFile) /* disk based metafile */
312 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
313 EMFDRV_DeleteDC( dc );
314 return 0;
317 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
318 sizeof(*physDev->emh), NULL, NULL)) {
319 EMFDRV_DeleteDC( dc );
320 return 0;
322 HeapFree( SystemHeap, 0, physDev->emh );
323 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
324 0, NULL);
325 TRACE(enhmetafile, "hMapping = %08x\n", hMapping );
326 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
327 TRACE(enhmetafile, "view = %p\n", physDev->emh );
331 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
332 physDev->emh = NULL; /* So it won't be deleted */
333 EMFDRV_DeleteDC( dc );
334 return hmf;