2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/winbase16.h"
25 #include "mfdrv/metafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
32 static const DC_FUNCTIONS MFDRV_Funcs =
35 MFDRV_AbortPath, /* pAbortPath */
39 MFDRV_BeginPath, /* pBeginPath */
40 MFDRV_BitBlt, /* pBitBlt */
41 NULL, /* pBitmapBits */
42 NULL, /* pChoosePixelFormat */
43 MFDRV_Chord, /* pChord */
44 MFDRV_CloseFigure, /* pCloseFigure */
45 NULL, /* pCreateBitmap */
47 NULL, /* pCreateDIBSection */
49 NULL, /* pDeleteObject */
50 NULL, /* pDescribePixelFormat */
51 NULL, /* pDeviceCapabilities */
52 MFDRV_Ellipse, /* pEllipse */
55 MFDRV_EndPath, /* pEndPath */
56 NULL, /* pEnumDeviceFonts */
57 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
58 NULL, /* pExtDeviceMode */
59 MFDRV_ExtEscape, /* pExtEscape */
60 MFDRV_ExtFloodFill, /* pExtFloodFill */
61 MFDRV_ExtTextOut, /* pExtTextOut */
62 MFDRV_FillPath, /* pFillPath */
63 MFDRV_FillRgn, /* pFillRgn */
64 MFDRV_FlattenPath, /* pFlattenPath */
65 MFDRV_FrameRgn, /* pFrameRgn */
66 NULL, /* pGetCharWidth */
67 NULL, /* pGetDCOrgEx */
68 NULL, /* pGetDIBColorTable */
69 NULL, /* pGetDIBits */
70 NULL, /* pGetDeviceCaps */
71 NULL, /* pGetDeviceGammaRamp */
73 NULL, /* pGetPixelFormat */
74 NULL, /* pGetTextExtentPoint */
75 NULL, /* pGetTextMetrics */
76 MFDRV_IntersectClipRect, /* pIntersectClipRect */
77 MFDRV_InvertRgn, /* pInvertRgn */
78 MFDRV_LineTo, /* pLineTo */
79 MFDRV_MoveTo, /* pMoveTo */
80 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
81 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
82 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
83 MFDRV_PaintRgn, /* pPaintRgn */
84 MFDRV_PatBlt, /* pPatBlt */
86 MFDRV_PolyBezier, /* pPolyBezier */
87 MFDRV_PolyBezierTo, /* pPolyBezierTo */
89 MFDRV_PolyPolygon, /* pPolyPolygon */
90 NULL, /* pPolyPolyline */
91 MFDRV_Polygon, /* pPolygon */
92 MFDRV_Polyline, /* pPolyline */
93 NULL, /* pPolylineTo */
94 NULL, /* pRealizePalette */
95 MFDRV_Rectangle, /* pRectangle */
97 MFDRV_RestoreDC, /* pRestoreDC */
98 MFDRV_RoundRect, /* pRoundRect */
99 MFDRV_SaveDC, /* pSaveDC */
100 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
101 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
102 MFDRV_SelectBitmap, /* pSelectBitmap */
103 MFDRV_SelectBrush, /* pSelectBrush */
104 MFDRV_SelectClipPath, /* pSelectClipPath */
105 NULL, /* pSelectClipRgn */
106 MFDRV_SelectFont, /* pSelectFont */
107 NULL, /* pSelectPalette */
108 MFDRV_SelectPen, /* pSelectPen */
109 MFDRV_SetBkColor, /* pSetBkColor */
110 MFDRV_SetBkMode, /* pSetBkMode */
111 NULL, /* pSetDIBColorTable */
112 NULL, /* pSetDIBits */
113 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
114 NULL, /* pSetDeviceClipping */
115 NULL, /* pSetDeviceGammaRamp */
116 MFDRV_SetMapMode, /* pSetMapMode */
117 MFDRV_SetMapperFlags, /* pSetMapperFlags */
118 MFDRV_SetPixel, /* pSetPixel */
119 NULL, /* pSetPixelFormat */
120 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
121 MFDRV_SetROP2, /* pSetROP2 */
122 MFDRV_SetRelAbs, /* pSetRelAbs */
123 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
124 MFDRV_SetTextAlign, /* pSetTextAlign */
125 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
126 MFDRV_SetTextColor, /* pSetTextColor */
127 MFDRV_SetTextJustification, /* pSetTextJustification */
128 MFDRV_SetViewportExt, /* pSetViewportExt */
129 MFDRV_SetViewportOrg, /* pSetViewportOrg */
130 MFDRV_SetWindowExt, /* pSetWindowExt */
131 MFDRV_SetWindowOrg, /* pSetWindowOrg */
132 NULL, /* pStartDoc */
133 NULL, /* pStartPage */
134 MFDRV_StretchBlt, /* pStretchBlt */
135 MFDRV_StretchDIBits, /* pStretchDIBits */
136 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
137 MFDRV_StrokePath, /* pStrokePath */
138 NULL, /* pSwapBuffers */
139 MFDRV_WidenPath /* pWidenPath */
144 /**********************************************************************
145 * MFDRV_AllocMetaFile
147 static DC *MFDRV_AllocMetaFile(void)
150 METAFILEDRV_PDEVICE *physDev;
152 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
153 dc->header.wMagic = METAFILE_DC_MAGIC;
155 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
158 GDI_FreeObject( dc->hSelf, dc );
161 dc->physDev = (PHYSDEV)physDev;
162 physDev->hdc = dc->hSelf;
165 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
167 HeapFree( GetProcessHeap(), 0, physDev );
168 GDI_FreeObject( dc->hSelf, dc );
172 physDev->nextHandle = 0;
175 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
176 physDev->mh->mtVersion = 0x0300;
177 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
178 physDev->mh->mtNoObjects = 0;
179 physDev->mh->mtMaxRecord = 0;
180 physDev->mh->mtNoParameters = 0;
186 /**********************************************************************
189 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
191 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
192 DC *dc = physDev->dc;
194 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
195 HeapFree( GetProcessHeap(), 0, physDev );
197 GDI_FreeObject( dc->hSelf, dc );
201 /**********************************************************************
202 * CreateMetaFile (GDI.125)
203 * CreateMetaFile16 (GDI32.@)
205 * Create a new DC and associate it with a metafile. Pass a filename
206 * to create a disk-based metafile, NULL to create a memory metafile.
209 * A handle to the metafile DC if successful, NULL on failure.
211 HDC16 WINAPI CreateMetaFile16(
212 LPCSTR filename /* [in] Filename of disk metafile */
217 METAFILEDRV_PDEVICE *physDev;
220 TRACE("'%s'\n", filename );
222 if (!(dc = MFDRV_AllocMetaFile())) return 0;
223 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
225 if (filename) /* disk based metafile */
227 physDev->mh->mtType = METAFILE_DISK;
228 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
229 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
230 MFDRV_DeleteDC( dc->physDev );
233 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
235 MFDRV_DeleteDC( dc->physDev );
238 physDev->hFile = hFile;
240 /* Grow METAHEADER to include filename */
241 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
243 else /* memory based metafile */
244 physDev->mh->mtType = METAFILE_MEMORY;
246 TRACE("returning %04x\n", dc->hSelf);
248 GDI_ReleaseObj( dc->hSelf );
252 /**********************************************************************
253 * CreateMetaFileA (GDI32.@)
255 HDC WINAPI CreateMetaFileA(
256 LPCSTR filename /* [in] Filename of disk metafile */
259 return CreateMetaFile16( filename );
262 /**********************************************************************
263 * CreateMetaFileW (GDI32.@)
265 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
271 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
272 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
273 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
275 hReturnDC = CreateMetaFileA(filenameA);
277 HeapFree( GetProcessHeap(), 0, filenameA );
283 /**********************************************************************
284 * MFDRV_CloseMetaFile
286 static DC *MFDRV_CloseMetaFile( HDC hdc )
289 METAFILEDRV_PDEVICE *physDev;
291 TRACE("(%04x)\n", hdc );
293 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
294 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
296 /* Construct the end of metafile record - this is documented
297 * in SDK Knowledgebase Q99334.
300 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
302 MFDRV_DeleteDC( dc->physDev );
306 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
308 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
309 MFDRV_DeleteDC( dc->physDev );
313 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
314 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
315 sizeof(*physDev->mh), NULL, NULL)) {
316 MFDRV_DeleteDC( dc->physDev );
319 CloseHandle(physDev->hFile);
320 physDev->mh->mtType = METAFILE_DISK;
326 /******************************************************************
327 * CloseMetaFile (GDI.126)
328 * CloseMetaFile16 (GDI32.@)
330 HMETAFILE16 WINAPI CloseMetaFile16(
331 HDC16 hdc /* [in] Metafile DC to close */
335 METAFILEDRV_PDEVICE *physDev;
336 DC *dc = MFDRV_CloseMetaFile(hdc);
338 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
340 /* Now allocate a global handle for the metafile */
342 hmf = MF_Create_HMETAFILE16( physDev->mh );
344 physDev->mh = NULL; /* So it won't be deleted */
345 MFDRV_DeleteDC( dc->physDev );
349 /******************************************************************
350 * CloseMetaFile (GDI32.@)
352 * Stop recording graphics operations in metafile associated with
353 * hdc and retrieve metafile.
356 * Handle of newly created metafile on success, NULL on failure.
358 HMETAFILE WINAPI CloseMetaFile(
359 HDC hdc /* [in] Metafile DC to close */
363 METAFILEDRV_PDEVICE *physDev;
364 DC *dc = MFDRV_CloseMetaFile(hdc);
366 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
368 /* Now allocate a global handle for the metafile */
370 hmf = MF_Create_HMETAFILE( physDev->mh );
372 physDev->mh = NULL; /* So it won't be deleted */
373 MFDRV_DeleteDC( dc->physDev );
378 /******************************************************************
381 * Warning: this function can change the pointer to the metafile header.
383 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
387 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
389 switch(physDev->mh->mtType)
391 case METAFILE_MEMORY:
392 len = physDev->mh->mtSize * 2 + rlen;
393 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
394 if (!mh) return FALSE;
396 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
399 TRACE("Writing record to disk\n");
400 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
404 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
408 physDev->mh->mtSize += rlen / 2;
409 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
414 /******************************************************************
418 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
421 METARECORD *mr = (METARECORD *)&buffer;
424 mr->rdFunction = func;
425 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
429 /******************************************************************
432 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
435 METARECORD *mr = (METARECORD *)&buffer;
438 mr->rdFunction = func;
439 *(mr->rdParm) = param1;
440 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
444 /******************************************************************
447 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
450 METARECORD *mr = (METARECORD *)&buffer;
453 mr->rdFunction = func;
454 *(mr->rdParm) = param2;
455 *(mr->rdParm + 1) = param1;
456 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
460 /******************************************************************
464 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
465 short param3, short param4)
468 METARECORD *mr = (METARECORD *)&buffer;
471 mr->rdFunction = func;
472 *(mr->rdParm) = param4;
473 *(mr->rdParm + 1) = param3;
474 *(mr->rdParm + 2) = param2;
475 *(mr->rdParm + 3) = param1;
476 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
480 /******************************************************************
484 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
485 short param3, short param4, short param5, short param6)
488 METARECORD *mr = (METARECORD *)&buffer;
491 mr->rdFunction = func;
492 *(mr->rdParm) = param6;
493 *(mr->rdParm + 1) = param5;
494 *(mr->rdParm + 2) = param4;
495 *(mr->rdParm + 3) = param3;
496 *(mr->rdParm + 4) = param2;
497 *(mr->rdParm + 5) = param1;
498 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
502 /******************************************************************
505 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
506 short param3, short param4, short param5,
507 short param6, short param7, short param8)
510 METARECORD *mr = (METARECORD *)&buffer;
513 mr->rdFunction = func;
514 *(mr->rdParm) = param8;
515 *(mr->rdParm + 1) = param7;
516 *(mr->rdParm + 2) = param6;
517 *(mr->rdParm + 3) = param5;
518 *(mr->rdParm + 4) = param4;
519 *(mr->rdParm + 5) = param3;
520 *(mr->rdParm + 6) = param2;
521 *(mr->rdParm + 7) = param1;
522 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
526 /******************************************************************
529 * Note: this function assumes that we never delete objects.
530 * If we do someday, we'll need to maintain a table to re-use deleted
533 int MFDRV_AddHandleDC( PHYSDEV dev )
535 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
536 physDev->mh->mtNoObjects++;
537 return physDev->nextHandle++;
540 /**********************************************************************
543 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
544 INT cbOutput, LPVOID out_data )
549 if(nEscape == MFCOMMENT) {
550 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
551 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
552 mr->rdSize = len / 2;
553 mr->rdFunction = META_ESCAPE;
554 mr->rdParm[0] = nEscape;
555 mr->rdParm[1] = cbInput;
556 memcpy(&(mr->rdParm[2]), in_data, cbInput);
557 MFDRV_WriteRecord( dev, mr, len);
558 HeapFree(GetProcessHeap(), 0, mr);