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
26 #include "wine/winbase16.h"
29 #include "mfdrv/metafiledrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
34 static const DC_FUNCTIONS MFDRV_Funcs =
37 MFDRV_AbortPath, /* pAbortPath */
41 MFDRV_BeginPath, /* pBeginPath */
42 MFDRV_BitBlt, /* pBitBlt */
43 NULL, /* pChoosePixelFormat */
44 MFDRV_Chord, /* pChord */
45 MFDRV_CloseFigure, /* pCloseFigure */
46 NULL, /* pCreateBitmap */
48 NULL, /* pCreateDIBSection */
49 NULL, /* pDeleteBitmap */
51 NULL, /* pDeleteObject */
52 NULL, /* pDescribePixelFormat */
53 NULL, /* pDeviceCapabilities */
54 MFDRV_Ellipse, /* pEllipse */
57 MFDRV_EndPath, /* pEndPath */
58 NULL, /* pEnumDeviceFonts */
59 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
60 NULL, /* pExtDeviceMode */
61 MFDRV_ExtEscape, /* pExtEscape */
62 MFDRV_ExtFloodFill, /* pExtFloodFill */
63 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
64 MFDRV_ExtTextOut, /* pExtTextOut */
65 MFDRV_FillPath, /* pFillPath */
66 MFDRV_FillRgn, /* pFillRgn */
67 MFDRV_FlattenPath, /* pFlattenPath */
68 MFDRV_FrameRgn, /* pFrameRgn */
69 NULL, /* pGdiComment */
70 NULL, /* pGetBitmapBits */
71 NULL, /* pGetCharWidth */
72 NULL, /* pGetDCOrgEx */
73 NULL, /* pGetDIBColorTable */
74 NULL, /* pGetDIBits */
75 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
76 NULL, /* pGetDeviceGammaRamp */
77 NULL, /* pGetNearestColor */
79 NULL, /* pGetPixelFormat */
80 NULL, /* pGetSystemPaletteEntries */
81 NULL, /* pGetTextExtentPoint */
82 NULL, /* pGetTextMetrics */
83 MFDRV_IntersectClipRect, /* pIntersectClipRect */
84 MFDRV_InvertRgn, /* pInvertRgn */
85 MFDRV_LineTo, /* pLineTo */
86 NULL, /* pModifyWorldTransform */
87 MFDRV_MoveTo, /* pMoveTo */
88 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
89 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
90 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
91 MFDRV_PaintRgn, /* pPaintRgn */
92 MFDRV_PatBlt, /* pPatBlt */
94 MFDRV_PolyBezier, /* pPolyBezier */
95 MFDRV_PolyBezierTo, /* pPolyBezierTo */
97 MFDRV_PolyPolygon, /* pPolyPolygon */
98 NULL, /* pPolyPolyline */
99 MFDRV_Polygon, /* pPolygon */
100 MFDRV_Polyline, /* pPolyline */
101 NULL, /* pPolylineTo */
102 NULL, /* pRealizeDefaultPalette */
103 MFDRV_RealizePalette, /* pRealizePalette */
104 MFDRV_Rectangle, /* pRectangle */
106 MFDRV_RestoreDC, /* pRestoreDC */
107 MFDRV_RoundRect, /* pRoundRect */
108 MFDRV_SaveDC, /* pSaveDC */
109 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
110 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
111 MFDRV_SelectBitmap, /* pSelectBitmap */
112 MFDRV_SelectBrush, /* pSelectBrush */
113 MFDRV_SelectClipPath, /* pSelectClipPath */
114 MFDRV_SelectFont, /* pSelectFont */
115 MFDRV_SelectPalette, /* pSelectPalette */
116 MFDRV_SelectPen, /* pSelectPen */
117 NULL, /* pSetArcDirection */
118 NULL, /* pSetBitmapBits */
119 MFDRV_SetBkColor, /* pSetBkColor */
120 MFDRV_SetBkMode, /* pSetBkMode */
121 NULL, /* pSetDCBrushColor*/
122 NULL, /* pSetDCOrg */
123 NULL, /* pSetDCPenColor*/
124 NULL, /* pSetDIBColorTable */
125 NULL, /* pSetDIBits */
126 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
127 NULL, /* pSetDeviceClipping */
128 NULL, /* pSetDeviceGammaRamp */
129 MFDRV_SetMapMode, /* pSetMapMode */
130 MFDRV_SetMapperFlags, /* pSetMapperFlags */
131 MFDRV_SetPixel, /* pSetPixel */
132 NULL, /* pSetPixelFormat */
133 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
134 MFDRV_SetROP2, /* pSetROP2 */
135 MFDRV_SetRelAbs, /* pSetRelAbs */
136 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
137 MFDRV_SetTextAlign, /* pSetTextAlign */
138 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
139 MFDRV_SetTextColor, /* pSetTextColor */
140 MFDRV_SetTextJustification, /* pSetTextJustification */
141 MFDRV_SetViewportExt, /* pSetViewportExt */
142 MFDRV_SetViewportOrg, /* pSetViewportOrg */
143 MFDRV_SetWindowExt, /* pSetWindowExt */
144 MFDRV_SetWindowOrg, /* pSetWindowOrg */
145 NULL, /* pSetWorldTransform */
146 NULL, /* pStartDoc */
147 NULL, /* pStartPage */
148 MFDRV_StretchBlt, /* pStretchBlt */
149 MFDRV_StretchDIBits, /* pStretchDIBits */
150 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
151 MFDRV_StrokePath, /* pStrokePath */
152 NULL, /* pSwapBuffers */
153 MFDRV_WidenPath /* pWidenPath */
158 /**********************************************************************
159 * MFDRV_AllocMetaFile
161 static DC *MFDRV_AllocMetaFile(void)
164 METAFILEDRV_PDEVICE *physDev;
166 if (!(dc = DC_AllocDC( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
168 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
171 GDI_FreeObject( dc->hSelf, dc );
174 dc->physDev = (PHYSDEV)physDev;
175 physDev->hdc = dc->hSelf;
178 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
180 HeapFree( GetProcessHeap(), 0, physDev );
181 GDI_FreeObject( dc->hSelf, dc );
185 physDev->nextHandle = 0;
188 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
189 physDev->mh->mtVersion = 0x0300;
190 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
191 physDev->mh->mtNoObjects = 0;
192 physDev->mh->mtMaxRecord = 0;
193 physDev->mh->mtNoParameters = 0;
199 /**********************************************************************
202 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
204 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
205 DC *dc = physDev->dc;
207 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
208 HeapFree( GetProcessHeap(), 0, physDev );
210 GDI_FreeObject( dc->hSelf, dc );
215 /**********************************************************************
216 * CreateMetaFileA (GDI32.@)
218 * Create a new DC and associate it with a metafile. Pass a filename
219 * to create a disk-based metafile, NULL to create a memory metafile.
222 * A handle to the metafile DC if successful, NULL on failure.
224 HDC WINAPI CreateMetaFileA( LPCSTR filename ) /* [in] Filename of disk metafile */
228 METAFILEDRV_PDEVICE *physDev;
231 TRACE("'%s'\n", filename );
233 if (!(dc = MFDRV_AllocMetaFile())) return 0;
234 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
236 if (filename) /* disk based metafile */
238 physDev->mh->mtType = METAFILE_DISK;
239 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
240 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
241 MFDRV_DeleteDC( dc->physDev );
244 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
246 MFDRV_DeleteDC( dc->physDev );
249 physDev->hFile = hFile;
251 /* Grow METAHEADER to include filename */
252 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
254 else /* memory based metafile */
255 physDev->mh->mtType = METAFILE_MEMORY;
257 TRACE("returning %p\n", dc->hSelf);
259 GDI_ReleaseObj( dc->hSelf );
263 /**********************************************************************
264 * CreateMetaFileW (GDI32.@)
266 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
272 if (!filename) return CreateMetaFileA(NULL);
274 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
275 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
276 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
278 hReturnDC = CreateMetaFileA(filenameA);
280 HeapFree( GetProcessHeap(), 0, filenameA );
286 /**********************************************************************
287 * MFDRV_CloseMetaFile
289 static DC *MFDRV_CloseMetaFile( HDC hdc )
292 METAFILEDRV_PDEVICE *physDev;
294 TRACE("(%p)\n", hdc );
296 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
297 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
299 /* Construct the end of metafile record - this is documented
300 * in SDK Knowledgebase Q99334.
303 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
305 MFDRV_DeleteDC( dc->physDev );
309 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
311 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
312 MFDRV_DeleteDC( dc->physDev );
316 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
317 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
318 sizeof(*physDev->mh), NULL, NULL)) {
319 MFDRV_DeleteDC( dc->physDev );
322 CloseHandle(physDev->hFile);
323 physDev->mh->mtType = METAFILE_DISK;
329 /******************************************************************
330 * CloseMetaFile (GDI.126)
332 HMETAFILE16 WINAPI CloseMetaFile16(
333 HDC16 hdc /* [in] Metafile DC to close */
337 METAFILEDRV_PDEVICE *physDev;
338 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
340 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
342 /* Now allocate a global handle for the metafile */
344 hmf = MF_Create_HMETAFILE16( physDev->mh );
346 physDev->mh = NULL; /* So it won't be deleted */
347 MFDRV_DeleteDC( dc->physDev );
351 /******************************************************************
352 * CloseMetaFile (GDI32.@)
354 * Stop recording graphics operations in metafile associated with
355 * hdc and retrieve metafile.
358 * Handle of newly created metafile on success, NULL on failure.
360 HMETAFILE WINAPI CloseMetaFile(
361 HDC hdc /* [in] Metafile DC to close */
365 METAFILEDRV_PDEVICE *physDev;
366 DC *dc = MFDRV_CloseMetaFile(hdc);
368 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
370 /* Now allocate a global handle for the metafile */
372 hmf = MF_Create_HMETAFILE( physDev->mh );
374 physDev->mh = NULL; /* So it won't be deleted */
375 MFDRV_DeleteDC( dc->physDev );
380 /******************************************************************
383 * Warning: this function can change the pointer to the metafile header.
385 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
389 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
391 switch(physDev->mh->mtType)
393 case METAFILE_MEMORY:
394 len = physDev->mh->mtSize * 2 + rlen;
395 /* reallocate memory if needed */
396 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
400 size += size / 2 + rlen;
401 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
402 if (!mh) return FALSE;
404 TRACE("Reallocated metafile: new size is %ld\n",size);
406 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
409 TRACE("Writing record to disk\n");
410 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
414 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
418 physDev->mh->mtSize += rlen / 2;
419 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
424 /******************************************************************
428 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
431 METARECORD *mr = (METARECORD *)&buffer;
434 mr->rdFunction = func;
435 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
439 /******************************************************************
442 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
445 METARECORD *mr = (METARECORD *)&buffer;
448 mr->rdFunction = func;
449 *(mr->rdParm) = param1;
450 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
454 /******************************************************************
457 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
460 METARECORD *mr = (METARECORD *)&buffer;
463 mr->rdFunction = func;
464 *(mr->rdParm) = param2;
465 *(mr->rdParm + 1) = param1;
466 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
470 /******************************************************************
474 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
475 short param3, short param4)
478 METARECORD *mr = (METARECORD *)&buffer;
481 mr->rdFunction = func;
482 *(mr->rdParm) = param4;
483 *(mr->rdParm + 1) = param3;
484 *(mr->rdParm + 2) = param2;
485 *(mr->rdParm + 3) = param1;
486 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
490 /******************************************************************
494 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
495 short param3, short param4, short param5, short param6)
498 METARECORD *mr = (METARECORD *)&buffer;
501 mr->rdFunction = func;
502 *(mr->rdParm) = param6;
503 *(mr->rdParm + 1) = param5;
504 *(mr->rdParm + 2) = param4;
505 *(mr->rdParm + 3) = param3;
506 *(mr->rdParm + 4) = param2;
507 *(mr->rdParm + 5) = param1;
508 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
512 /******************************************************************
515 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
516 short param3, short param4, short param5,
517 short param6, short param7, short param8)
520 METARECORD *mr = (METARECORD *)&buffer;
523 mr->rdFunction = func;
524 *(mr->rdParm) = param8;
525 *(mr->rdParm + 1) = param7;
526 *(mr->rdParm + 2) = param6;
527 *(mr->rdParm + 3) = param5;
528 *(mr->rdParm + 4) = param4;
529 *(mr->rdParm + 5) = param3;
530 *(mr->rdParm + 6) = param2;
531 *(mr->rdParm + 7) = param1;
532 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
536 /******************************************************************
539 * Note: this function assumes that we never delete objects.
540 * If we do someday, we'll need to maintain a table to re-use deleted
543 int MFDRV_AddHandleDC( PHYSDEV dev )
545 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
546 physDev->mh->mtNoObjects++;
547 return physDev->nextHandle++;
550 /**********************************************************************
553 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
554 INT cbOutput, LPVOID out_data )
560 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
561 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
562 mr->rdSize = len / 2;
563 mr->rdFunction = META_ESCAPE;
564 mr->rdParm[0] = nEscape;
565 mr->rdParm[1] = cbInput;
566 memcpy(&(mr->rdParm[2]), in_data, cbInput);
567 ret = MFDRV_WriteRecord( dev, mr, len);
568 HeapFree(GetProcessHeap(), 0, mr);
573 /******************************************************************
574 * MFDRV_GetDeviceCaps
576 *A very simple implementation that returns DT_METAFILE
578 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
587 TRACE(" unsupported capability %d, will return 0\n", cap );