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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
33 static const DC_FUNCTIONS MFDRV_Funcs =
36 MFDRV_AbortPath, /* pAbortPath */
37 NULL, /* pAlphaBlend */
41 MFDRV_BeginPath, /* pBeginPath */
43 NULL, /* pChoosePixelFormat */
44 MFDRV_Chord, /* pChord */
45 MFDRV_CloseFigure, /* pCloseFigure */
46 NULL, /* pCreateBitmap */
48 NULL, /* pCreateDIBSection */
49 NULL, /* pDeleteBitmap */
51 MFDRV_DeleteObject, /* pDeleteObject */
52 NULL, /* pDescribePixelFormat */
53 NULL, /* pDeviceCapabilities */
54 MFDRV_Ellipse, /* pEllipse */
57 MFDRV_EndPath, /* pEndPath */
58 NULL, /* pEnumDeviceFonts */
59 NULL, /* pEnumICMProfiles */
60 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
61 NULL, /* pExtDeviceMode */
62 MFDRV_ExtEscape, /* pExtEscape */
63 MFDRV_ExtFloodFill, /* pExtFloodFill */
64 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
65 MFDRV_ExtTextOut, /* pExtTextOut */
66 MFDRV_FillPath, /* pFillPath */
67 MFDRV_FillRgn, /* pFillRgn */
68 MFDRV_FlattenPath, /* pFlattenPath */
69 MFDRV_FrameRgn, /* pFrameRgn */
70 NULL, /* pGdiComment */
71 NULL, /* pGetBitmapBits */
72 NULL, /* pGetCharWidth */
73 NULL, /* pGetDIBits */
74 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
75 NULL, /* pGetDeviceGammaRamp */
76 NULL, /* pGetICMProfile */
77 NULL, /* pGetNearestColor */
79 NULL, /* pGetPixelFormat */
80 NULL, /* pGetSystemPaletteEntries */
81 NULL, /* pGetTextExtentExPoint */
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_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
90 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
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_ScaleViewportExtEx, /* pScaleViewportExtEx */
110 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
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 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
122 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
123 NULL, /* pSetDIBColorTable */
124 NULL, /* pSetDIBits */
125 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
126 NULL, /* pSetDeviceClipping */
127 NULL, /* pSetDeviceGammaRamp */
128 NULL, /* pSetLayout */
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_SetViewportExtEx, /* pSetViewportExtEx */
142 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
143 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
144 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
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 NULL, /* pUnrealizePalette */
154 MFDRV_WidenPath /* pWidenPath */
159 /**********************************************************************
160 * MFDRV_AllocMetaFile
162 static DC *MFDRV_AllocMetaFile(void)
165 METAFILEDRV_PDEVICE *physDev;
167 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
169 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
175 physDev->dev.funcs = &MFDRV_Funcs;
176 push_dc_driver( dc, &physDev->dev );
177 physDev->hdc = dc->hSelf;
179 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
181 HeapFree( GetProcessHeap(), 0, physDev );
186 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
187 physDev->handles_size = HANDLE_LIST_INC;
188 physDev->cur_handles = 0;
192 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
193 physDev->mh->mtVersion = 0x0300;
194 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
195 physDev->mh->mtNoObjects = 0;
196 physDev->mh->mtMaxRecord = 0;
197 physDev->mh->mtNoParameters = 0;
199 SetVirtualResolution(dc->hSelf, 0, 0, 0, 0);
205 /**********************************************************************
208 static BOOL MFDRV_DeleteDC( DC *dc )
210 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
213 HeapFree( GetProcessHeap(), 0, physDev->mh );
214 for(index = 0; index < physDev->handles_size; index++)
215 if(physDev->handles[index])
216 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
217 HeapFree( GetProcessHeap(), 0, physDev->handles );
218 HeapFree( GetProcessHeap(), 0, physDev );
225 /**********************************************************************
226 * CreateMetaFileW (GDI32.@)
228 * Create a new DC and associate it with a metafile. Pass a filename
229 * to create a disk-based metafile, NULL to create a memory metafile.
232 * filename [I] Filename of disk metafile
235 * A handle to the metafile DC if successful, NULL on failure.
237 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
241 METAFILEDRV_PDEVICE *physDev;
244 TRACE("%s\n", debugstr_w(filename) );
246 if (!(dc = MFDRV_AllocMetaFile())) return 0;
247 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
249 if (filename) /* disk based metafile */
251 physDev->mh->mtType = METAFILE_DISK;
252 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
253 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
254 MFDRV_DeleteDC( dc );
257 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
259 MFDRV_DeleteDC( dc );
262 physDev->hFile = hFile;
264 /* Grow METAHEADER to include filename */
265 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
267 else /* memory based metafile */
268 physDev->mh->mtType = METAFILE_MEMORY;
270 TRACE("returning %p\n", dc->hSelf);
272 release_dc_ptr( dc );
276 /**********************************************************************
277 * CreateMetaFileA (GDI32.@)
279 * See CreateMetaFileW.
281 HDC WINAPI CreateMetaFileA(LPCSTR filename)
287 if (!filename) return CreateMetaFileW(NULL);
289 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
290 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
291 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
293 hReturnDC = CreateMetaFileW(filenameW);
295 HeapFree( GetProcessHeap(), 0, filenameW );
301 /**********************************************************************
302 * MFDRV_CloseMetaFile
304 static DC *MFDRV_CloseMetaFile( HDC hdc )
307 METAFILEDRV_PDEVICE *physDev;
309 TRACE("(%p)\n", hdc );
311 if (!(dc = get_dc_ptr( hdc ))) return NULL;
312 if (dc->header.type != OBJ_METADC)
314 release_dc_ptr( dc );
317 if (dc->refcount != 1)
319 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
320 release_dc_ptr( dc );
323 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
325 /* Construct the end of metafile record - this is documented
326 * in SDK Knowledgebase Q99334.
329 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
331 MFDRV_DeleteDC( dc );
335 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
337 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
338 MFDRV_DeleteDC( dc );
342 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
343 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
345 MFDRV_DeleteDC( dc );
348 CloseHandle(physDev->hFile);
349 physDev->mh->mtType = METAFILE_DISK;
355 /******************************************************************
356 * CloseMetaFile (GDI32.@)
358 * Stop recording graphics operations in metafile associated with
359 * hdc and retrieve metafile.
362 * hdc [I] Metafile DC to close
365 * Handle of newly created metafile on success, NULL on failure.
367 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
370 METAFILEDRV_PDEVICE *physDev;
371 DC *dc = MFDRV_CloseMetaFile(hdc);
373 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
375 /* Now allocate a global handle for the metafile */
377 hmf = MF_Create_HMETAFILE( physDev->mh );
379 physDev->mh = NULL; /* So it won't be deleted */
380 MFDRV_DeleteDC( dc );
385 /******************************************************************
388 * Warning: this function can change the pointer to the metafile header.
390 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
394 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
396 switch(physDev->mh->mtType)
398 case METAFILE_MEMORY:
399 len = physDev->mh->mtSize * 2 + rlen;
400 /* reallocate memory if needed */
401 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
405 size += size / 2 + rlen;
406 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
407 if (!mh) return FALSE;
409 TRACE("Reallocated metafile: new size is %d\n",size);
411 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
414 TRACE("Writing record to disk\n");
415 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
419 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
423 physDev->mh->mtSize += rlen / 2;
424 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
429 /******************************************************************
433 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
436 METARECORD *mr = (METARECORD *)&buffer;
439 mr->rdFunction = func;
440 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
444 /******************************************************************
447 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
450 METARECORD *mr = (METARECORD *)&buffer;
453 mr->rdFunction = func;
454 *(mr->rdParm) = param1;
455 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
459 /******************************************************************
462 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
465 METARECORD *mr = (METARECORD *)&buffer;
468 mr->rdFunction = func;
469 *(mr->rdParm) = param2;
470 *(mr->rdParm + 1) = param1;
471 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
475 /******************************************************************
479 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
480 short param3, short param4)
483 METARECORD *mr = (METARECORD *)&buffer;
486 mr->rdFunction = func;
487 *(mr->rdParm) = param4;
488 *(mr->rdParm + 1) = param3;
489 *(mr->rdParm + 2) = param2;
490 *(mr->rdParm + 3) = param1;
491 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
495 /******************************************************************
499 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
500 short param3, short param4, short param5, short param6)
503 METARECORD *mr = (METARECORD *)&buffer;
506 mr->rdFunction = func;
507 *(mr->rdParm) = param6;
508 *(mr->rdParm + 1) = param5;
509 *(mr->rdParm + 2) = param4;
510 *(mr->rdParm + 3) = param3;
511 *(mr->rdParm + 4) = param2;
512 *(mr->rdParm + 5) = param1;
513 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
517 /******************************************************************
520 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
521 short param3, short param4, short param5,
522 short param6, short param7, short param8)
525 METARECORD *mr = (METARECORD *)&buffer;
528 mr->rdFunction = func;
529 *(mr->rdParm) = param8;
530 *(mr->rdParm + 1) = param7;
531 *(mr->rdParm + 2) = param6;
532 *(mr->rdParm + 3) = param5;
533 *(mr->rdParm + 4) = param4;
534 *(mr->rdParm + 5) = param3;
535 *(mr->rdParm + 6) = param2;
536 *(mr->rdParm + 7) = param1;
537 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
541 /**********************************************************************
544 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
545 INT cbOutput, LPVOID out_data )
551 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
553 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
554 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
555 mr->rdSize = len / 2;
556 mr->rdFunction = META_ESCAPE;
557 mr->rdParm[0] = nEscape;
558 mr->rdParm[1] = cbInput;
559 memcpy(&(mr->rdParm[2]), in_data, cbInput);
560 ret = MFDRV_WriteRecord( dev, mr, len);
561 HeapFree(GetProcessHeap(), 0, mr);
566 /******************************************************************
567 * MFDRV_GetDeviceCaps
569 *A very simple implementation that returns DT_METAFILE
571 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
580 TRACE(" unsupported capability %d, will return 0\n", cap );