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 BOOL MFDRV_DeleteDC( PHYSDEV dev );
36 /**********************************************************************
39 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
40 INT cbOutput, LPVOID out_data )
46 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
48 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
49 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
51 mr->rdFunction = META_ESCAPE;
52 mr->rdParm[0] = nEscape;
53 mr->rdParm[1] = cbInput;
54 memcpy(&(mr->rdParm[2]), in_data, cbInput);
55 ret = MFDRV_WriteRecord( dev, mr, len);
56 HeapFree(GetProcessHeap(), 0, mr);
61 /******************************************************************
64 *A very simple implementation that returns DT_METAFILE
66 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
75 TRACE(" unsupported capability %d, will return 0\n", cap );
81 static const DC_FUNCTIONS MFDRV_Funcs =
84 MFDRV_AbortPath, /* pAbortPath */
85 NULL, /* pAlphaBlend */
89 MFDRV_BeginPath, /* pBeginPath */
90 NULL, /* pChoosePixelFormat */
91 MFDRV_Chord, /* pChord */
92 MFDRV_CloseFigure, /* pCloseFigure */
93 NULL, /* pCreateBitmap */
95 NULL, /* pCreateDIBSection */
96 NULL, /* pDeleteBitmap */
97 MFDRV_DeleteDC, /* pDeleteDC */
98 MFDRV_DeleteObject, /* pDeleteObject */
99 NULL, /* pDescribePixelFormat */
100 NULL, /* pDeviceCapabilities */
101 MFDRV_Ellipse, /* pEllipse */
104 MFDRV_EndPath, /* pEndPath */
105 NULL, /* pEnumDeviceFonts */
106 NULL, /* pEnumICMProfiles */
107 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
108 NULL, /* pExtDeviceMode */
109 MFDRV_ExtEscape, /* pExtEscape */
110 MFDRV_ExtFloodFill, /* pExtFloodFill */
111 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
112 MFDRV_ExtTextOut, /* pExtTextOut */
113 MFDRV_FillPath, /* pFillPath */
114 MFDRV_FillRgn, /* pFillRgn */
115 MFDRV_FlattenPath, /* pFlattenPath */
116 MFDRV_FrameRgn, /* pFrameRgn */
117 NULL, /* pGdiComment */
118 NULL, /* pGetCharWidth */
119 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
120 NULL, /* pGetDeviceGammaRamp */
121 NULL, /* pGetICMProfile */
122 NULL, /* pGetImage */
123 NULL, /* pGetNearestColor */
124 NULL, /* pGetPixel */
125 NULL, /* pGetPixelFormat */
126 NULL, /* pGetSystemPaletteEntries */
127 NULL, /* pGetTextExtentExPoint */
128 NULL, /* pGetTextMetrics */
129 MFDRV_IntersectClipRect, /* pIntersectClipRect */
130 MFDRV_InvertRgn, /* pInvertRgn */
131 MFDRV_LineTo, /* pLineTo */
132 NULL, /* pModifyWorldTransform */
133 MFDRV_MoveTo, /* pMoveTo */
134 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
135 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
136 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
137 MFDRV_PaintRgn, /* pPaintRgn */
138 MFDRV_PatBlt, /* pPatBlt */
139 MFDRV_Pie, /* pPie */
140 MFDRV_PolyBezier, /* pPolyBezier */
141 MFDRV_PolyBezierTo, /* pPolyBezierTo */
142 NULL, /* pPolyDraw */
143 MFDRV_PolyPolygon, /* pPolyPolygon */
144 NULL, /* pPolyPolyline */
145 MFDRV_Polygon, /* pPolygon */
146 MFDRV_Polyline, /* pPolyline */
147 NULL, /* pPolylineTo */
148 NULL, /* pPutImage */
149 NULL, /* pRealizeDefaultPalette */
150 MFDRV_RealizePalette, /* pRealizePalette */
151 MFDRV_Rectangle, /* pRectangle */
153 MFDRV_RestoreDC, /* pRestoreDC */
154 MFDRV_RoundRect, /* pRoundRect */
155 MFDRV_SaveDC, /* pSaveDC */
156 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
157 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
158 MFDRV_SelectBitmap, /* pSelectBitmap */
159 MFDRV_SelectBrush, /* pSelectBrush */
160 MFDRV_SelectClipPath, /* pSelectClipPath */
161 MFDRV_SelectFont, /* pSelectFont */
162 MFDRV_SelectPalette, /* pSelectPalette */
163 MFDRV_SelectPen, /* pSelectPen */
164 NULL, /* pSetArcDirection */
165 NULL, /* pSetBitmapBits */
166 MFDRV_SetBkColor, /* pSetBkColor */
167 MFDRV_SetBkMode, /* pSetBkMode */
168 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
169 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
170 NULL, /* pSetDIBColorTable */
171 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
172 NULL, /* pSetDeviceClipping */
173 NULL, /* pSetDeviceGammaRamp */
174 NULL, /* pSetLayout */
175 MFDRV_SetMapMode, /* pSetMapMode */
176 MFDRV_SetMapperFlags, /* pSetMapperFlags */
177 MFDRV_SetPixel, /* pSetPixel */
178 NULL, /* pSetPixelFormat */
179 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
180 MFDRV_SetROP2, /* pSetROP2 */
181 MFDRV_SetRelAbs, /* pSetRelAbs */
182 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
183 MFDRV_SetTextAlign, /* pSetTextAlign */
184 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
185 MFDRV_SetTextColor, /* pSetTextColor */
186 MFDRV_SetTextJustification, /* pSetTextJustification */
187 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
188 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
189 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
190 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
191 NULL, /* pSetWorldTransform */
192 NULL, /* pStartDoc */
193 NULL, /* pStartPage */
194 MFDRV_StretchBlt, /* pStretchBlt */
195 MFDRV_StretchDIBits, /* pStretchDIBits */
196 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
197 MFDRV_StrokePath, /* pStrokePath */
198 NULL, /* pSwapBuffers */
199 NULL, /* pUnrealizePalette */
200 MFDRV_WidenPath /* pWidenPath */
205 /**********************************************************************
206 * MFDRV_AllocMetaFile
208 static DC *MFDRV_AllocMetaFile(void)
211 METAFILEDRV_PDEVICE *physDev;
213 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
215 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
221 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
223 HeapFree( GetProcessHeap(), 0, physDev );
228 push_dc_driver( dc, &physDev->dev, &MFDRV_Funcs );
230 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
231 physDev->handles_size = HANDLE_LIST_INC;
232 physDev->cur_handles = 0;
236 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
237 physDev->mh->mtVersion = 0x0300;
238 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
239 physDev->mh->mtNoObjects = 0;
240 physDev->mh->mtMaxRecord = 0;
241 physDev->mh->mtNoParameters = 0;
243 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
249 /**********************************************************************
252 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
254 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
257 HeapFree( GetProcessHeap(), 0, physDev->mh );
258 for(index = 0; index < physDev->handles_size; index++)
259 if(physDev->handles[index])
260 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
261 HeapFree( GetProcessHeap(), 0, physDev->handles );
262 HeapFree( GetProcessHeap(), 0, physDev );
267 /**********************************************************************
268 * CreateMetaFileW (GDI32.@)
270 * Create a new DC and associate it with a metafile. Pass a filename
271 * to create a disk-based metafile, NULL to create a memory metafile.
274 * filename [I] Filename of disk metafile
277 * A handle to the metafile DC if successful, NULL on failure.
279 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
283 METAFILEDRV_PDEVICE *physDev;
286 TRACE("%s\n", debugstr_w(filename) );
288 if (!(dc = MFDRV_AllocMetaFile())) return 0;
289 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
291 if (filename) /* disk based metafile */
293 physDev->mh->mtType = METAFILE_DISK;
294 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
295 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
299 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
304 physDev->hFile = hFile;
306 /* Grow METAHEADER to include filename */
307 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
309 else /* memory based metafile */
310 physDev->mh->mtType = METAFILE_MEMORY;
312 TRACE("returning %p\n", physDev->dev.hdc);
313 ret = physDev->dev.hdc;
314 release_dc_ptr( dc );
318 /**********************************************************************
319 * CreateMetaFileA (GDI32.@)
321 * See CreateMetaFileW.
323 HDC WINAPI CreateMetaFileA(LPCSTR filename)
329 if (!filename) return CreateMetaFileW(NULL);
331 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
332 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
333 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
335 hReturnDC = CreateMetaFileW(filenameW);
337 HeapFree( GetProcessHeap(), 0, filenameW );
343 /**********************************************************************
344 * MFDRV_CloseMetaFile
346 static DC *MFDRV_CloseMetaFile( HDC hdc )
349 METAFILEDRV_PDEVICE *physDev;
351 TRACE("(%p)\n", hdc );
353 if (!(dc = get_dc_ptr( hdc ))) return NULL;
354 if (dc->header.type != OBJ_METADC)
356 release_dc_ptr( dc );
359 if (dc->refcount != 1)
361 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
362 release_dc_ptr( dc );
365 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
367 /* Construct the end of metafile record - this is documented
368 * in SDK Knowledgebase Q99334.
371 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
377 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
379 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
384 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
385 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
390 CloseHandle(physDev->hFile);
391 physDev->mh->mtType = METAFILE_DISK;
397 /******************************************************************
398 * CloseMetaFile (GDI32.@)
400 * Stop recording graphics operations in metafile associated with
401 * hdc and retrieve metafile.
404 * hdc [I] Metafile DC to close
407 * Handle of newly created metafile on success, NULL on failure.
409 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
412 METAFILEDRV_PDEVICE *physDev;
413 DC *dc = MFDRV_CloseMetaFile(hdc);
415 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
417 /* Now allocate a global handle for the metafile */
419 hmf = MF_Create_HMETAFILE( physDev->mh );
421 physDev->mh = NULL; /* So it won't be deleted */
427 /******************************************************************
430 * Warning: this function can change the pointer to the metafile header.
432 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
436 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
438 switch(physDev->mh->mtType)
440 case METAFILE_MEMORY:
441 len = physDev->mh->mtSize * 2 + rlen;
442 /* reallocate memory if needed */
443 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
447 size += size / 2 + rlen;
448 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
449 if (!mh) return FALSE;
451 TRACE("Reallocated metafile: new size is %d\n",size);
453 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
456 TRACE("Writing record to disk\n");
457 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
461 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
465 physDev->mh->mtSize += rlen / 2;
466 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
471 /******************************************************************
475 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
478 METARECORD *mr = (METARECORD *)&buffer;
481 mr->rdFunction = func;
482 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
486 /******************************************************************
489 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
492 METARECORD *mr = (METARECORD *)&buffer;
495 mr->rdFunction = func;
496 *(mr->rdParm) = param1;
497 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
501 /******************************************************************
504 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
507 METARECORD *mr = (METARECORD *)&buffer;
510 mr->rdFunction = func;
511 *(mr->rdParm) = param2;
512 *(mr->rdParm + 1) = param1;
513 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
517 /******************************************************************
521 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
522 short param3, short param4)
525 METARECORD *mr = (METARECORD *)&buffer;
528 mr->rdFunction = func;
529 *(mr->rdParm) = param4;
530 *(mr->rdParm + 1) = param3;
531 *(mr->rdParm + 2) = param2;
532 *(mr->rdParm + 3) = param1;
533 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
537 /******************************************************************
541 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
542 short param3, short param4, short param5, short param6)
545 METARECORD *mr = (METARECORD *)&buffer;
548 mr->rdFunction = func;
549 *(mr->rdParm) = param6;
550 *(mr->rdParm + 1) = param5;
551 *(mr->rdParm + 2) = param4;
552 *(mr->rdParm + 3) = param3;
553 *(mr->rdParm + 4) = param2;
554 *(mr->rdParm + 5) = param1;
555 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
559 /******************************************************************
562 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
563 short param3, short param4, short param5,
564 short param6, short param7, short param8)
567 METARECORD *mr = (METARECORD *)&buffer;
570 mr->rdFunction = func;
571 *(mr->rdParm) = param8;
572 *(mr->rdParm + 1) = param7;
573 *(mr->rdParm + 2) = param6;
574 *(mr->rdParm + 3) = param5;
575 *(mr->rdParm + 4) = param4;
576 *(mr->rdParm + 5) = param3;
577 *(mr->rdParm + 6) = param2;
578 *(mr->rdParm + 7) = param1;
579 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);