2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
8 #include "wine/winbase16.h"
12 #include "metafiledrv.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(metafile);
19 static const DC_FUNCTIONS MFDRV_Funcs =
22 MFDRV_AbortPath, /* pAbortPath */
26 MFDRV_BeginPath, /* pBeginPath */
27 MFDRV_BitBlt, /* pBitBlt */
28 NULL, /* pBitmapBits */
29 NULL, /* pChoosePixelFormat */
30 MFDRV_Chord, /* pChord */
31 MFDRV_CloseFigure, /* pCloseFigure */
32 NULL, /* pCreateBitmap */
33 NULL, /* no implementation */ /* pCreateDC */
34 NULL, /* pCreateDIBSection */
35 NULL, /* pCreateDIBSection16 */
36 NULL, /* no implementation */ /* pDeleteDC */
37 NULL, /* pDeleteObject */
38 NULL, /* pDescribePixelFormat */
39 NULL, /* pDeviceCapabilities */
40 MFDRV_Ellipse, /* pEllipse */
43 MFDRV_EndPath, /* pEndPath */
44 NULL, /* pEnumDeviceFonts */
46 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
47 NULL, /* pExtDeviceMode */
48 MFDRV_ExtFloodFill, /* pExtFloodFill */
49 MFDRV_ExtTextOut, /* pExtTextOut */
50 MFDRV_FillPath, /* pFillPath */
51 MFDRV_FillRgn, /* pFillRgn */
52 MFDRV_FlattenPath, /* pFlattenPath */
53 MFDRV_FrameRgn, /* pFrameRgn */
54 NULL, /* pGetCharWidth */
55 NULL, /* pGetDCOrgEx */
56 NULL, /* pGetDeviceGammaRamp */
57 NULL, /* no implementation */ /* pGetPixel */
58 NULL, /* pGetPixelFormat */
59 NULL, /* pGetTextExtentPoint */
60 NULL, /* pGetTextMetrics */
61 MFDRV_IntersectClipRect, /* pIntersectClipRect */
62 MFDRV_InvertRgn, /* pInvertRgn */
63 MFDRV_LineTo, /* pLineTo */
64 MFDRV_MoveTo, /* pMoveTo */
65 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
66 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
67 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
68 MFDRV_PaintRgn, /* pPaintRgn */
69 MFDRV_PatBlt, /* pPatBlt */
71 MFDRV_PolyBezier, /* pPolyBezier */
72 MFDRV_PolyBezierTo, /* pPolyBezierTo */
74 MFDRV_PolyPolygon, /* pPolyPolygon */
75 NULL, /* pPolyPolyline */
76 MFDRV_Polygon, /* pPolygon */
77 MFDRV_Polyline, /* pPolyline */
78 NULL, /* pPolylineTo */
79 NULL, /* pRealizePalette */
80 MFDRV_Rectangle, /* pRectangle */
81 MFDRV_RestoreDC, /* pRestoreDC */
82 MFDRV_RoundRect, /* pRoundRect */
83 MFDRV_SaveDC, /* pSaveDC */
84 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
85 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
86 MFDRV_SelectClipPath, /* pSelectClipPath */
87 NULL, /* pSelectClipRgn */
88 MFDRV_SelectObject, /* pSelectObject */
89 NULL, /* pSelectPalette */
90 MFDRV_SetBkColor, /* pSetBkColor */
91 MFDRV_SetBkMode, /* pSetBkMode */
92 NULL, /* pSetDeviceClipping */
93 NULL, /* pSetDeviceGammaRamp */
94 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
95 MFDRV_SetMapMode, /* pSetMapMode */
96 MFDRV_SetMapperFlags, /* pSetMapperFlags */
97 MFDRV_SetPixel, /* pSetPixel */
98 NULL, /* pSetPixelFormat */
99 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
100 MFDRV_SetROP2, /* pSetROP2 */
101 MFDRV_SetRelAbs, /* pSetRelAbs */
102 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
103 MFDRV_SetTextAlign, /* pSetTextAlign */
104 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
105 MFDRV_SetTextColor, /* pSetTextColor */
106 MFDRV_SetTextJustification, /* pSetTextJustification */
107 MFDRV_SetViewportExt, /* pSetViewportExt */
108 MFDRV_SetViewportOrg, /* pSetViewportOrg */
109 MFDRV_SetWindowExt, /* pSetWindowExt */
110 MFDRV_SetWindowOrg, /* pSetWindowOrg */
111 NULL, /* pStartDoc */
112 NULL, /* pStartPage */
113 MFDRV_StretchBlt, /* pStretchBlt */
114 MFDRV_StretchDIBits, /* pStretchDIBits */
115 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
116 MFDRV_StrokePath, /* pStrokePath */
117 NULL, /* pSwapBuffers */
118 MFDRV_WidenPath /* pWidenPath */
123 /**********************************************************************
124 * MFDRV_AllocMetaFile
126 static DC *MFDRV_AllocMetaFile(void)
129 METAFILEDRV_PDEVICE *physDev;
131 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
132 dc->header.wMagic = METAFILE_DC_MAGIC;
134 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
137 GDI_FreeObject( dc->hSelf, dc );
140 dc->physDev = physDev;
142 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
144 HeapFree( GetProcessHeap(), 0, physDev );
145 GDI_FreeObject( dc->hSelf, dc );
149 physDev->nextHandle = 0;
152 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
153 physDev->mh->mtVersion = 0x0300;
154 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
155 physDev->mh->mtNoObjects = 0;
156 physDev->mh->mtMaxRecord = 0;
157 physDev->mh->mtNoParameters = 0;
163 /**********************************************************************
166 static BOOL MFDRV_DeleteDC( DC *dc )
168 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
170 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
171 HeapFree( GetProcessHeap(), 0, physDev );
173 GDI_FreeObject( dc->hSelf, dc );
177 /**********************************************************************
178 * CreateMetaFile16 (GDI.125)
179 * CreateMetaFile16 (GDI32.@)
181 * Create a new DC and associate it with a metafile. Pass a filename
182 * to create a disk-based metafile, NULL to create a memory metafile.
185 * A handle to the metafile DC if successful, NULL on failure.
187 HDC16 WINAPI CreateMetaFile16(
188 LPCSTR filename /* [in] Filename of disk metafile */
193 METAFILEDRV_PDEVICE *physDev;
196 TRACE("'%s'\n", filename );
198 if (!(dc = MFDRV_AllocMetaFile())) return 0;
199 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
201 if (filename) /* disk based metafile */
203 physDev->mh->mtType = METAFILE_DISK;
204 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
205 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
206 MFDRV_DeleteDC( dc );
209 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
211 MFDRV_DeleteDC( dc );
214 physDev->hFile = hFile;
216 /* Grow METAHEADER to include filename */
217 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
219 else /* memory based metafile */
220 physDev->mh->mtType = METAFILE_MEMORY;
222 TRACE("returning %04x\n", dc->hSelf);
224 GDI_ReleaseObj( dc->hSelf );
228 /**********************************************************************
229 * CreateMetaFileA (GDI32.@)
231 HDC WINAPI CreateMetaFileA(
232 LPCSTR filename /* [in] Filename of disk metafile */
235 return CreateMetaFile16( filename );
238 /**********************************************************************
239 * CreateMetaFileW (GDI32.@)
241 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
246 filenameA = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
248 hReturnDC = CreateMetaFileA(filenameA);
250 HeapFree( GetProcessHeap(), 0, filenameA );
256 /**********************************************************************
257 * MFDRV_CloseMetaFile
259 static DC *MFDRV_CloseMetaFile( HDC hdc )
262 METAFILEDRV_PDEVICE *physDev;
264 TRACE("(%04x)\n", hdc );
266 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
267 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
269 /* Construct the end of metafile record - this is documented
270 * in SDK Knowledgebase Q99334.
273 if (!MFDRV_MetaParam0(dc, META_EOF))
275 MFDRV_DeleteDC( dc );
279 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
281 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
282 MFDRV_DeleteDC( dc );
286 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
287 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
288 sizeof(*physDev->mh), NULL, NULL)) {
289 MFDRV_DeleteDC( dc );
292 CloseHandle(physDev->hFile);
293 physDev->mh->mtType = METAFILE_DISK;
299 /******************************************************************
300 * CloseMetaFile16 (GDI.126)
301 * CloseMetaFile16 (GDI32.@)
303 HMETAFILE16 WINAPI CloseMetaFile16(
304 HDC16 hdc /* [in] Metafile DC to close */
308 METAFILEDRV_PDEVICE *physDev;
309 DC *dc = MFDRV_CloseMetaFile(hdc);
311 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
313 /* Now allocate a global handle for the metafile */
315 hmf = MF_Create_HMETAFILE16( physDev->mh );
317 physDev->mh = NULL; /* So it won't be deleted */
318 MFDRV_DeleteDC( dc );
322 /******************************************************************
323 * CloseMetaFile (GDI32.@)
325 * Stop recording graphics operations in metafile associated with
326 * hdc and retrieve metafile.
329 * Handle of newly created metafile on success, NULL on failure.
331 HMETAFILE WINAPI CloseMetaFile(
332 HDC hdc /* [in] Metafile DC to close */
336 METAFILEDRV_PDEVICE *physDev;
337 DC *dc = MFDRV_CloseMetaFile(hdc);
339 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
341 /* Now allocate a global handle for the metafile */
343 hmf = MF_Create_HMETAFILE( physDev->mh );
345 physDev->mh = NULL; /* So it won't be deleted */
346 MFDRV_DeleteDC( dc );
351 /******************************************************************
354 * Warning: this function can change the pointer to the metafile header.
356 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
360 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
362 switch(physDev->mh->mtType)
364 case METAFILE_MEMORY:
365 len = physDev->mh->mtSize * 2 + rlen;
366 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
367 if (!mh) return FALSE;
369 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
372 TRACE("Writing record to disk\n");
373 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
377 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
381 physDev->mh->mtSize += rlen / 2;
382 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
387 /******************************************************************
391 BOOL MFDRV_MetaParam0(DC *dc, short func)
394 METARECORD *mr = (METARECORD *)&buffer;
397 mr->rdFunction = func;
398 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
402 /******************************************************************
405 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
408 METARECORD *mr = (METARECORD *)&buffer;
411 mr->rdFunction = func;
412 *(mr->rdParm) = param1;
413 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
417 /******************************************************************
420 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
423 METARECORD *mr = (METARECORD *)&buffer;
426 mr->rdFunction = func;
427 *(mr->rdParm) = param2;
428 *(mr->rdParm + 1) = param1;
429 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
433 /******************************************************************
437 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2,
438 short param3, short param4)
441 METARECORD *mr = (METARECORD *)&buffer;
444 mr->rdFunction = func;
445 *(mr->rdParm) = param4;
446 *(mr->rdParm + 1) = param3;
447 *(mr->rdParm + 2) = param2;
448 *(mr->rdParm + 3) = param1;
449 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
453 /******************************************************************
457 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2,
458 short param3, short param4, short param5, short param6)
461 METARECORD *mr = (METARECORD *)&buffer;
464 mr->rdFunction = func;
465 *(mr->rdParm) = param6;
466 *(mr->rdParm + 1) = param5;
467 *(mr->rdParm + 2) = param4;
468 *(mr->rdParm + 3) = param3;
469 *(mr->rdParm + 4) = param2;
470 *(mr->rdParm + 5) = param1;
471 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
475 /******************************************************************
478 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2,
479 short param3, short param4, short param5,
480 short param6, short param7, short param8)
483 METARECORD *mr = (METARECORD *)&buffer;
486 mr->rdFunction = func;
487 *(mr->rdParm) = param8;
488 *(mr->rdParm + 1) = param7;
489 *(mr->rdParm + 2) = param6;
490 *(mr->rdParm + 3) = param5;
491 *(mr->rdParm + 4) = param4;
492 *(mr->rdParm + 5) = param3;
493 *(mr->rdParm + 6) = param2;
494 *(mr->rdParm + 7) = param1;
495 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
499 /******************************************************************
502 * Note: this function assumes that we never delete objects.
503 * If we do someday, we'll need to maintain a table to re-use deleted
506 int MFDRV_AddHandleDC( DC *dc )
508 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
509 physDev->mh->mtNoObjects++;
510 return physDev->nextHandle++;