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 NULL, /* 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 */
96 MFDRV_RestoreDC, /* pRestoreDC */
97 MFDRV_RoundRect, /* pRoundRect */
98 MFDRV_SaveDC, /* pSaveDC */
99 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
100 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
101 MFDRV_SelectBitmap, /* pSelectBitmap */
102 MFDRV_SelectBrush, /* pSelectBrush */
103 MFDRV_SelectClipPath, /* pSelectClipPath */
104 NULL, /* pSelectClipRgn */
105 MFDRV_SelectFont, /* pSelectFont */
106 NULL, /* pSelectPalette */
107 MFDRV_SelectPen, /* pSelectPen */
108 MFDRV_SetBkColor, /* pSetBkColor */
109 MFDRV_SetBkMode, /* pSetBkMode */
110 NULL, /* pSetDIBColorTable */
111 NULL, /* pSetDIBits */
112 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
113 NULL, /* pSetDeviceClipping */
114 NULL, /* pSetDeviceGammaRamp */
115 MFDRV_SetMapMode, /* pSetMapMode */
116 MFDRV_SetMapperFlags, /* pSetMapperFlags */
117 MFDRV_SetPixel, /* pSetPixel */
118 NULL, /* pSetPixelFormat */
119 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
120 MFDRV_SetROP2, /* pSetROP2 */
121 MFDRV_SetRelAbs, /* pSetRelAbs */
122 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
123 MFDRV_SetTextAlign, /* pSetTextAlign */
124 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
125 MFDRV_SetTextColor, /* pSetTextColor */
126 MFDRV_SetTextJustification, /* pSetTextJustification */
127 MFDRV_SetViewportExt, /* pSetViewportExt */
128 MFDRV_SetViewportOrg, /* pSetViewportOrg */
129 MFDRV_SetWindowExt, /* pSetWindowExt */
130 MFDRV_SetWindowOrg, /* pSetWindowOrg */
131 NULL, /* pStartDoc */
132 NULL, /* pStartPage */
133 MFDRV_StretchBlt, /* pStretchBlt */
134 MFDRV_StretchDIBits, /* pStretchDIBits */
135 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
136 MFDRV_StrokePath, /* pStrokePath */
137 NULL, /* pSwapBuffers */
138 MFDRV_WidenPath /* pWidenPath */
143 /**********************************************************************
144 * MFDRV_AllocMetaFile
146 static DC *MFDRV_AllocMetaFile(void)
149 METAFILEDRV_PDEVICE *physDev;
151 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
152 dc->header.wMagic = METAFILE_DC_MAGIC;
154 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
157 GDI_FreeObject( dc->hSelf, dc );
160 dc->physDev = (PHYSDEV)physDev;
161 physDev->hdc = dc->hSelf;
164 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
166 HeapFree( GetProcessHeap(), 0, physDev );
167 GDI_FreeObject( dc->hSelf, dc );
171 physDev->nextHandle = 0;
174 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
175 physDev->mh->mtVersion = 0x0300;
176 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
177 physDev->mh->mtNoObjects = 0;
178 physDev->mh->mtMaxRecord = 0;
179 physDev->mh->mtNoParameters = 0;
185 /**********************************************************************
188 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
190 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
191 DC *dc = physDev->dc;
193 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
194 HeapFree( GetProcessHeap(), 0, physDev );
196 GDI_FreeObject( dc->hSelf, dc );
200 /**********************************************************************
201 * CreateMetaFile (GDI.125)
202 * CreateMetaFile16 (GDI32.@)
204 * Create a new DC and associate it with a metafile. Pass a filename
205 * to create a disk-based metafile, NULL to create a memory metafile.
208 * A handle to the metafile DC if successful, NULL on failure.
210 HDC16 WINAPI CreateMetaFile16(
211 LPCSTR filename /* [in] Filename of disk metafile */
216 METAFILEDRV_PDEVICE *physDev;
219 TRACE("'%s'\n", filename );
221 if (!(dc = MFDRV_AllocMetaFile())) return 0;
222 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
224 if (filename) /* disk based metafile */
226 physDev->mh->mtType = METAFILE_DISK;
227 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
228 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
229 MFDRV_DeleteDC( dc->physDev );
232 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
234 MFDRV_DeleteDC( dc->physDev );
237 physDev->hFile = hFile;
239 /* Grow METAHEADER to include filename */
240 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
242 else /* memory based metafile */
243 physDev->mh->mtType = METAFILE_MEMORY;
245 TRACE("returning %04x\n", dc->hSelf);
247 GDI_ReleaseObj( dc->hSelf );
251 /**********************************************************************
252 * CreateMetaFileA (GDI32.@)
254 HDC WINAPI CreateMetaFileA(
255 LPCSTR filename /* [in] Filename of disk metafile */
258 return CreateMetaFile16( filename );
261 /**********************************************************************
262 * CreateMetaFileW (GDI32.@)
264 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
270 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
271 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
272 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
274 hReturnDC = CreateMetaFileA(filenameA);
276 HeapFree( GetProcessHeap(), 0, filenameA );
282 /**********************************************************************
283 * MFDRV_CloseMetaFile
285 static DC *MFDRV_CloseMetaFile( HDC hdc )
288 METAFILEDRV_PDEVICE *physDev;
290 TRACE("(%04x)\n", hdc );
292 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
293 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
295 /* Construct the end of metafile record - this is documented
296 * in SDK Knowledgebase Q99334.
299 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
301 MFDRV_DeleteDC( dc->physDev );
305 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
307 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
308 MFDRV_DeleteDC( dc->physDev );
312 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
313 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
314 sizeof(*physDev->mh), NULL, NULL)) {
315 MFDRV_DeleteDC( dc->physDev );
318 CloseHandle(physDev->hFile);
319 physDev->mh->mtType = METAFILE_DISK;
325 /******************************************************************
326 * CloseMetaFile (GDI.126)
327 * CloseMetaFile16 (GDI32.@)
329 HMETAFILE16 WINAPI CloseMetaFile16(
330 HDC16 hdc /* [in] Metafile DC to close */
334 METAFILEDRV_PDEVICE *physDev;
335 DC *dc = MFDRV_CloseMetaFile(hdc);
337 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
339 /* Now allocate a global handle for the metafile */
341 hmf = MF_Create_HMETAFILE16( physDev->mh );
343 physDev->mh = NULL; /* So it won't be deleted */
344 MFDRV_DeleteDC( dc->physDev );
348 /******************************************************************
349 * CloseMetaFile (GDI32.@)
351 * Stop recording graphics operations in metafile associated with
352 * hdc and retrieve metafile.
355 * Handle of newly created metafile on success, NULL on failure.
357 HMETAFILE WINAPI CloseMetaFile(
358 HDC hdc /* [in] Metafile DC to close */
362 METAFILEDRV_PDEVICE *physDev;
363 DC *dc = MFDRV_CloseMetaFile(hdc);
365 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
367 /* Now allocate a global handle for the metafile */
369 hmf = MF_Create_HMETAFILE( physDev->mh );
371 physDev->mh = NULL; /* So it won't be deleted */
372 MFDRV_DeleteDC( dc->physDev );
377 /******************************************************************
380 * Warning: this function can change the pointer to the metafile header.
382 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
386 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
388 switch(physDev->mh->mtType)
390 case METAFILE_MEMORY:
391 len = physDev->mh->mtSize * 2 + rlen;
392 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
393 if (!mh) return FALSE;
395 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
398 TRACE("Writing record to disk\n");
399 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
403 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
407 physDev->mh->mtSize += rlen / 2;
408 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
413 /******************************************************************
417 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
420 METARECORD *mr = (METARECORD *)&buffer;
423 mr->rdFunction = func;
424 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
428 /******************************************************************
431 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
434 METARECORD *mr = (METARECORD *)&buffer;
437 mr->rdFunction = func;
438 *(mr->rdParm) = param1;
439 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
443 /******************************************************************
446 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
449 METARECORD *mr = (METARECORD *)&buffer;
452 mr->rdFunction = func;
453 *(mr->rdParm) = param2;
454 *(mr->rdParm + 1) = param1;
455 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
459 /******************************************************************
463 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
464 short param3, short param4)
467 METARECORD *mr = (METARECORD *)&buffer;
470 mr->rdFunction = func;
471 *(mr->rdParm) = param4;
472 *(mr->rdParm + 1) = param3;
473 *(mr->rdParm + 2) = param2;
474 *(mr->rdParm + 3) = param1;
475 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
479 /******************************************************************
483 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
484 short param3, short param4, short param5, short param6)
487 METARECORD *mr = (METARECORD *)&buffer;
490 mr->rdFunction = func;
491 *(mr->rdParm) = param6;
492 *(mr->rdParm + 1) = param5;
493 *(mr->rdParm + 2) = param4;
494 *(mr->rdParm + 3) = param3;
495 *(mr->rdParm + 4) = param2;
496 *(mr->rdParm + 5) = param1;
497 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
501 /******************************************************************
504 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
505 short param3, short param4, short param5,
506 short param6, short param7, short param8)
509 METARECORD *mr = (METARECORD *)&buffer;
512 mr->rdFunction = func;
513 *(mr->rdParm) = param8;
514 *(mr->rdParm + 1) = param7;
515 *(mr->rdParm + 2) = param6;
516 *(mr->rdParm + 3) = param5;
517 *(mr->rdParm + 4) = param4;
518 *(mr->rdParm + 5) = param3;
519 *(mr->rdParm + 6) = param2;
520 *(mr->rdParm + 7) = param1;
521 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
525 /******************************************************************
528 * Note: this function assumes that we never delete objects.
529 * If we do someday, we'll need to maintain a table to re-use deleted
532 int MFDRV_AddHandleDC( PHYSDEV dev )
534 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
535 physDev->mh->mtNoObjects++;
536 return physDev->nextHandle++;