2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
8 #include "wine/winbase16.h"
13 #include "metafiledrv.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(metafile)
20 static const DC_FUNCTIONS MFDRV_Funcs =
23 MFDRV_AbortPath, /* pAbortPath */
27 MFDRV_BeginPath, /* pBeginPath */
28 MFDRV_BitBlt, /* pBitBlt */
29 NULL, /* pBitmapBits */
30 NULL, /* pChoosePixelFormat */
31 MFDRV_Chord, /* pChord */
32 MFDRV_CloseFigure, /* pCloseFigure */
33 NULL, /* pCreateBitmap */
34 NULL, /* no implementation */ /* pCreateDC */
35 NULL, /* pCreateDIBSection */
36 NULL, /* pCreateDIBSection16 */
37 NULL, /* no implementation */ /* pDeleteDC */
38 NULL, /* pDeleteObject */
39 NULL, /* pDescribePixelFormat */
40 NULL, /* pDeviceCapabilities */
41 MFDRV_Ellipse, /* pEllipse */
44 MFDRV_EndPath, /* pEndPath */
45 NULL, /* pEnumDeviceFonts */
47 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
48 NULL, /* pExtDeviceMode */
49 MFDRV_ExtFloodFill, /* pExtFloodFill */
50 MFDRV_ExtTextOut, /* pExtTextOut */
51 MFDRV_FillPath, /* pFillPath */
52 MFDRV_FillRgn, /* pFillRgn */
53 MFDRV_FlattenPath, /* pFlattenPath */
54 MFDRV_FrameRgn, /* pFrameRgn */
55 NULL, /* pGetCharWidth */
56 NULL, /* pGetDCOrgEx */
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_MoveToEx, /* pMoveToEx */
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 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
94 MFDRV_SetMapMode, /* pSetMapMode */
95 MFDRV_SetMapperFlags, /* pSetMapperFlags */
96 MFDRV_SetPixel, /* pSetPixel */
97 NULL, /* pSetPixelFormat */
98 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
99 MFDRV_SetROP2, /* pSetROP2 */
100 MFDRV_SetRelAbs, /* pSetRelAbs */
101 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
102 MFDRV_SetTextAlign, /* pSetTextAlign */
103 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
104 MFDRV_SetTextColor, /* pSetTextColor */
105 MFDRV_SetTextJustification, /* pSetTextJustification */
106 MFDRV_SetViewportExt, /* pSetViewportExt */
107 MFDRV_SetViewportOrg, /* pSetViewportOrg */
108 MFDRV_SetWindowExt, /* pSetWindowExt */
109 MFDRV_SetWindowOrg, /* pSetWindowOrg */
110 NULL, /* pStartDoc */
111 NULL, /* pStartPage */
112 MFDRV_StretchBlt, /* pStretchBlt */
113 MFDRV_StretchDIBits, /* pStretchDIBits */
114 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
115 MFDRV_StrokePath, /* pStrokePath */
116 NULL, /* pSwapBuffers */
117 MFDRV_WidenPath /* pWidenPath */
122 /**********************************************************************
123 * MFDRV_AllocMetaFile
125 static DC *MFDRV_AllocMetaFile(void)
128 METAFILEDRV_PDEVICE *physDev;
130 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
131 dc->header.wMagic = METAFILE_DC_MAGIC;
133 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
136 GDI_HEAP_FREE( dc->hSelf );
139 dc->physDev = physDev;
141 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
143 HeapFree( GetProcessHeap(), 0, physDev );
144 GDI_HEAP_FREE( dc->hSelf );
148 physDev->nextHandle = 0;
151 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
152 physDev->mh->mtVersion = 0x0300;
153 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
154 physDev->mh->mtNoObjects = 0;
155 physDev->mh->mtMaxRecord = 0;
156 physDev->mh->mtNoParameters = 0;
162 /**********************************************************************
165 static BOOL MFDRV_DeleteDC( DC *dc )
167 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
169 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
170 HeapFree( GetProcessHeap(), 0, physDev );
172 GDI_FreeObject(dc->hSelf);
176 /**********************************************************************
177 * CreateMetaFile16 (GDI.125)
179 * Create a new DC and associate it with a metafile. Pass a filename
180 * to create a disk-based metafile, NULL to create a memory metafile.
183 * A handle to the metafile DC if successful, NULL on failure.
185 HDC16 WINAPI CreateMetaFile16(
186 LPCSTR filename /* Filename of disk metafile */
190 METAFILEDRV_PDEVICE *physDev;
193 TRACE("'%s'\n", filename );
195 if (!(dc = MFDRV_AllocMetaFile())) return 0;
196 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
198 if (filename) /* disk based metafile */
200 physDev->mh->mtType = METAFILE_DISK;
201 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
202 CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
203 MFDRV_DeleteDC( dc );
206 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
208 MFDRV_DeleteDC( dc );
211 physDev->hFile = hFile;
213 /* Grow METAHEADER to include filename */
214 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
216 else /* memory based metafile */
217 physDev->mh->mtType = METAFILE_MEMORY;
219 TRACE("returning %04x\n", dc->hSelf);
223 /**********************************************************************
224 * CreateMetaFileA (GDI32.51)
226 HDC WINAPI CreateMetaFileA(
227 LPCSTR filename /* Filename of disk metafile */
230 return CreateMetaFile16( filename );
233 /**********************************************************************
234 * CreateMetaFileW (GDI32.52)
236 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
241 filenameA = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
243 hReturnDC = CreateMetaFileA(filenameA);
245 HeapFree( GetProcessHeap(), 0, filenameA );
251 /**********************************************************************
252 * MFDRV_CloseMetaFile
254 static DC *MFDRV_CloseMetaFile( HDC hdc )
257 METAFILEDRV_PDEVICE *physDev;
259 TRACE("(%04x)\n", hdc );
261 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
262 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
264 /* Construct the end of metafile record - this is documented
265 * in SDK Knowledgebase Q99334.
268 if (!MFDRV_MetaParam0(dc, META_EOF))
270 MFDRV_DeleteDC( dc );
274 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
276 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
277 MFDRV_DeleteDC( dc );
281 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
282 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
283 sizeof(*physDev->mh), NULL, NULL)) {
284 MFDRV_DeleteDC( dc );
287 CloseHandle(physDev->hFile);
288 physDev->mh->mtType = METAFILE_DISK;
294 /******************************************************************
295 * CloseMetaFile16 (GDI.126)
297 HMETAFILE16 WINAPI CloseMetaFile16(
298 HDC16 hdc /* Metafile DC to close */
302 METAFILEDRV_PDEVICE *physDev;
303 DC *dc = MFDRV_CloseMetaFile(hdc);
305 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
307 /* Now allocate a global handle for the metafile */
309 hmf = MF_Create_HMETAFILE16( physDev->mh );
311 physDev->mh = NULL; /* So it won't be deleted */
312 MFDRV_DeleteDC( dc );
316 /******************************************************************
317 * CloseMetaFile (GDI32.17)
319 * Stop recording graphics operations in metafile associated with
320 * hdc and retrieve metafile.
323 * Handle of newly created metafile on success, NULL on failure.
325 HMETAFILE WINAPI CloseMetaFile(
326 HDC hdc /* Metafile DC to close */
330 METAFILEDRV_PDEVICE *physDev;
331 DC *dc = MFDRV_CloseMetaFile(hdc);
333 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
335 /* Now allocate a global handle for the metafile */
337 hmf = MF_Create_HMETAFILE( physDev->mh );
339 physDev->mh = NULL; /* So it won't be deleted */
340 MFDRV_DeleteDC( dc );
345 /******************************************************************
348 * Warning: this function can change the pointer to the metafile header.
350 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
354 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
356 switch(physDev->mh->mtType)
358 case METAFILE_MEMORY:
359 len = physDev->mh->mtSize * 2 + rlen;
360 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
361 if (!mh) return FALSE;
363 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
366 TRACE("Writing record to disk\n");
367 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
371 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
375 physDev->mh->mtSize += rlen / 2;
376 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
381 /******************************************************************
385 BOOL MFDRV_MetaParam0(DC *dc, short func)
388 METARECORD *mr = (METARECORD *)&buffer;
391 mr->rdFunction = func;
392 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
396 /******************************************************************
399 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
402 METARECORD *mr = (METARECORD *)&buffer;
405 mr->rdFunction = func;
406 *(mr->rdParm) = param1;
407 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
411 /******************************************************************
414 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
417 METARECORD *mr = (METARECORD *)&buffer;
420 mr->rdFunction = func;
421 *(mr->rdParm) = param2;
422 *(mr->rdParm + 1) = param1;
423 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
427 /******************************************************************
431 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2,
432 short param3, short param4)
435 METARECORD *mr = (METARECORD *)&buffer;
438 mr->rdFunction = func;
439 *(mr->rdParm) = param4;
440 *(mr->rdParm + 1) = param3;
441 *(mr->rdParm + 2) = param2;
442 *(mr->rdParm + 3) = param1;
443 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
447 /******************************************************************
451 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2,
452 short param3, short param4, short param5, short param6)
455 METARECORD *mr = (METARECORD *)&buffer;
458 mr->rdFunction = func;
459 *(mr->rdParm) = param6;
460 *(mr->rdParm + 1) = param5;
461 *(mr->rdParm + 2) = param4;
462 *(mr->rdParm + 3) = param3;
463 *(mr->rdParm + 4) = param2;
464 *(mr->rdParm + 5) = param1;
465 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
469 /******************************************************************
472 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2,
473 short param3, short param4, short param5,
474 short param6, short param7, short param8)
477 METARECORD *mr = (METARECORD *)&buffer;
480 mr->rdFunction = func;
481 *(mr->rdParm) = param8;
482 *(mr->rdParm + 1) = param7;
483 *(mr->rdParm + 2) = param6;
484 *(mr->rdParm + 3) = param5;
485 *(mr->rdParm + 4) = param4;
486 *(mr->rdParm + 5) = param3;
487 *(mr->rdParm + 6) = param2;
488 *(mr->rdParm + 7) = param1;
489 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
493 /******************************************************************
496 * Note: this function assumes that we never delete objects.
497 * If we do someday, we'll need to maintain a table to re-use deleted
500 int MFDRV_AddHandleDC( DC *dc )
502 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
503 physDev->mh->mtNoObjects++;
504 return physDev->nextHandle++;