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_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev );
34 static BOOL MFDRV_DeleteDC( PHYSDEV dev );
37 /**********************************************************************
40 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
41 INT cbOutput, LPVOID out_data )
47 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
49 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
50 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
52 mr->rdFunction = META_ESCAPE;
53 mr->rdParm[0] = nEscape;
54 mr->rdParm[1] = cbInput;
55 memcpy(&(mr->rdParm[2]), in_data, cbInput);
56 ret = MFDRV_WriteRecord( dev, mr, len);
57 HeapFree(GetProcessHeap(), 0, mr);
62 /******************************************************************
65 *A very simple implementation that returns DT_METAFILE
67 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
76 TRACE(" unsupported capability %d, will return 0\n", cap );
82 static const struct gdi_dc_funcs MFDRV_Funcs =
85 MFDRV_AbortPath, /* pAbortPath */
86 NULL, /* pAlphaBlend */
90 MFDRV_BeginPath, /* pBeginPath */
91 NULL, /* pBlendImage */
92 NULL, /* pChoosePixelFormat */
93 MFDRV_Chord, /* pChord */
94 MFDRV_CloseFigure, /* pCloseFigure */
95 NULL, /* pCopyBitmap */
96 NULL, /* pCreateBitmap */
97 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
99 NULL, /* pCreateDIBSection */
100 NULL, /* pDeleteBitmap */
101 MFDRV_DeleteDC, /* pDeleteDC */
102 MFDRV_DeleteObject, /* pDeleteObject */
103 NULL, /* pDescribePixelFormat */
104 NULL, /* pDeviceCapabilities */
105 MFDRV_Ellipse, /* pEllipse */
108 MFDRV_EndPath, /* pEndPath */
109 NULL, /* pEnumFonts */
110 NULL, /* pEnumICMProfiles */
111 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
112 NULL, /* pExtDeviceMode */
113 MFDRV_ExtEscape, /* pExtEscape */
114 MFDRV_ExtFloodFill, /* pExtFloodFill */
115 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
116 MFDRV_ExtTextOut, /* pExtTextOut */
117 MFDRV_FillPath, /* pFillPath */
118 MFDRV_FillRgn, /* pFillRgn */
119 MFDRV_FlattenPath, /* pFlattenPath */
120 NULL, /* pFontIsLinked */
121 MFDRV_FrameRgn, /* pFrameRgn */
122 NULL, /* pGdiComment */
123 NULL, /* pGdiRealizationInfo */
124 NULL, /* pGetCharABCWidths */
125 NULL, /* pGetCharABCWidthsI */
126 NULL, /* pGetCharWidth */
127 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
128 NULL, /* pGetDeviceGammaRamp */
129 NULL, /* pGetFontData */
130 NULL, /* pGetFontUnicodeRanges */
131 NULL, /* pGetGlyphIndices */
132 NULL, /* pGetGlyphOutline */
133 NULL, /* pGetICMProfile */
134 NULL, /* pGetImage */
135 NULL, /* pGetKerningPairs */
136 NULL, /* pGetNearestColor */
137 NULL, /* pGetOutlineTextMetrics */
138 NULL, /* pGetPixel */
139 NULL, /* pGetPixelFormat */
140 NULL, /* pGetSystemPaletteEntries */
141 NULL, /* pGetTextCharsetInfo */
142 NULL, /* pGetTextExtentExPoint */
143 NULL, /* pGetTextExtentExPointI */
144 NULL, /* pGetTextFace */
145 NULL, /* pGetTextMetrics */
146 NULL, /* pGradientFill */
147 MFDRV_IntersectClipRect, /* pIntersectClipRect */
148 MFDRV_InvertRgn, /* pInvertRgn */
149 MFDRV_LineTo, /* pLineTo */
150 NULL, /* pModifyWorldTransform */
151 MFDRV_MoveTo, /* pMoveTo */
152 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
153 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
154 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
155 MFDRV_PaintRgn, /* pPaintRgn */
156 MFDRV_PatBlt, /* pPatBlt */
157 MFDRV_Pie, /* pPie */
158 MFDRV_PolyBezier, /* pPolyBezier */
159 MFDRV_PolyBezierTo, /* pPolyBezierTo */
160 NULL, /* pPolyDraw */
161 MFDRV_PolyPolygon, /* pPolyPolygon */
162 NULL, /* pPolyPolyline */
163 MFDRV_Polygon, /* pPolygon */
164 MFDRV_Polyline, /* pPolyline */
165 NULL, /* pPolylineTo */
166 NULL, /* pPutImage */
167 NULL, /* pRealizeDefaultPalette */
168 MFDRV_RealizePalette, /* pRealizePalette */
169 MFDRV_Rectangle, /* pRectangle */
171 MFDRV_RestoreDC, /* pRestoreDC */
172 MFDRV_RoundRect, /* pRoundRect */
173 MFDRV_SaveDC, /* pSaveDC */
174 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
175 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
176 MFDRV_SelectBitmap, /* pSelectBitmap */
177 MFDRV_SelectBrush, /* pSelectBrush */
178 MFDRV_SelectClipPath, /* pSelectClipPath */
179 MFDRV_SelectFont, /* pSelectFont */
180 MFDRV_SelectPalette, /* pSelectPalette */
181 MFDRV_SelectPen, /* pSelectPen */
182 NULL, /* pSetArcDirection */
183 MFDRV_SetBkColor, /* pSetBkColor */
184 MFDRV_SetBkMode, /* pSetBkMode */
185 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
186 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
187 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
188 NULL, /* pSetDeviceClipping */
189 NULL, /* pSetDeviceGammaRamp */
190 NULL, /* pSetLayout */
191 MFDRV_SetMapMode, /* pSetMapMode */
192 MFDRV_SetMapperFlags, /* pSetMapperFlags */
193 MFDRV_SetPixel, /* pSetPixel */
194 NULL, /* pSetPixelFormat */
195 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
196 MFDRV_SetROP2, /* pSetROP2 */
197 MFDRV_SetRelAbs, /* pSetRelAbs */
198 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
199 MFDRV_SetTextAlign, /* pSetTextAlign */
200 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
201 MFDRV_SetTextColor, /* pSetTextColor */
202 MFDRV_SetTextJustification, /* pSetTextJustification */
203 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
204 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
205 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
206 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
207 NULL, /* pSetWorldTransform */
208 NULL, /* pStartDoc */
209 NULL, /* pStartPage */
210 MFDRV_StretchBlt, /* pStretchBlt */
211 MFDRV_StretchDIBits, /* pStretchDIBits */
212 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
213 MFDRV_StrokePath, /* pStrokePath */
214 NULL, /* pSwapBuffers */
215 NULL, /* pUnrealizePalette */
216 MFDRV_WidenPath /* pWidenPath */
221 /**********************************************************************
222 * MFDRV_AllocMetaFile
224 static DC *MFDRV_AllocMetaFile(void)
227 METAFILEDRV_PDEVICE *physDev;
229 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
231 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
237 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
239 HeapFree( GetProcessHeap(), 0, physDev );
244 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
246 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
247 physDev->handles_size = HANDLE_LIST_INC;
248 physDev->cur_handles = 0;
252 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
253 physDev->mh->mtVersion = 0x0300;
254 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
255 physDev->mh->mtNoObjects = 0;
256 physDev->mh->mtMaxRecord = 0;
257 physDev->mh->mtNoParameters = 0;
259 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
265 /**********************************************************************
266 * MFDRV_CreateCompatibleDC
268 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
270 /* not supported on metafile DCs */
275 /**********************************************************************
278 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
280 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
283 HeapFree( GetProcessHeap(), 0, physDev->mh );
284 for(index = 0; index < physDev->handles_size; index++)
285 if(physDev->handles[index])
286 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
287 HeapFree( GetProcessHeap(), 0, physDev->handles );
288 HeapFree( GetProcessHeap(), 0, physDev );
293 /**********************************************************************
294 * CreateMetaFileW (GDI32.@)
296 * Create a new DC and associate it with a metafile. Pass a filename
297 * to create a disk-based metafile, NULL to create a memory metafile.
300 * filename [I] Filename of disk metafile
303 * A handle to the metafile DC if successful, NULL on failure.
305 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
309 METAFILEDRV_PDEVICE *physDev;
312 TRACE("%s\n", debugstr_w(filename) );
314 if (!(dc = MFDRV_AllocMetaFile())) return 0;
315 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
317 if (filename) /* disk based metafile */
319 physDev->mh->mtType = METAFILE_DISK;
320 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
321 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
325 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
330 physDev->hFile = hFile;
332 /* Grow METAHEADER to include filename */
333 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
335 else /* memory based metafile */
336 physDev->mh->mtType = METAFILE_MEMORY;
338 TRACE("returning %p\n", physDev->dev.hdc);
339 ret = physDev->dev.hdc;
340 release_dc_ptr( dc );
344 /**********************************************************************
345 * CreateMetaFileA (GDI32.@)
347 * See CreateMetaFileW.
349 HDC WINAPI CreateMetaFileA(LPCSTR filename)
355 if (!filename) return CreateMetaFileW(NULL);
357 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
358 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
359 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
361 hReturnDC = CreateMetaFileW(filenameW);
363 HeapFree( GetProcessHeap(), 0, filenameW );
369 /**********************************************************************
370 * MFDRV_CloseMetaFile
372 static DC *MFDRV_CloseMetaFile( HDC hdc )
375 METAFILEDRV_PDEVICE *physDev;
377 TRACE("(%p)\n", hdc );
379 if (!(dc = get_dc_ptr( hdc ))) return NULL;
380 if (dc->header.type != OBJ_METADC)
382 release_dc_ptr( dc );
385 if (dc->refcount != 1)
387 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
388 release_dc_ptr( dc );
391 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
393 /* Construct the end of metafile record - this is documented
394 * in SDK Knowledgebase Q99334.
397 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
403 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
405 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
410 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
411 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
416 CloseHandle(physDev->hFile);
417 physDev->mh->mtType = METAFILE_DISK;
423 /******************************************************************
424 * CloseMetaFile (GDI32.@)
426 * Stop recording graphics operations in metafile associated with
427 * hdc and retrieve metafile.
430 * hdc [I] Metafile DC to close
433 * Handle of newly created metafile on success, NULL on failure.
435 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
438 METAFILEDRV_PDEVICE *physDev;
439 DC *dc = MFDRV_CloseMetaFile(hdc);
441 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
443 /* Now allocate a global handle for the metafile */
445 hmf = MF_Create_HMETAFILE( physDev->mh );
447 physDev->mh = NULL; /* So it won't be deleted */
453 /******************************************************************
456 * Warning: this function can change the pointer to the metafile header.
458 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
462 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
464 switch(physDev->mh->mtType)
466 case METAFILE_MEMORY:
467 len = physDev->mh->mtSize * 2 + rlen;
468 /* reallocate memory if needed */
469 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
473 size += size / 2 + rlen;
474 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
475 if (!mh) return FALSE;
477 TRACE("Reallocated metafile: new size is %d\n",size);
479 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
482 TRACE("Writing record to disk\n");
483 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
487 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
491 physDev->mh->mtSize += rlen / 2;
492 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
497 /******************************************************************
501 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
504 METARECORD *mr = (METARECORD *)&buffer;
507 mr->rdFunction = func;
508 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
512 /******************************************************************
515 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
518 METARECORD *mr = (METARECORD *)&buffer;
519 WORD *params = mr->rdParm;
522 mr->rdFunction = func;
524 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
528 /******************************************************************
531 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
534 METARECORD *mr = (METARECORD *)&buffer;
535 WORD *params = mr->rdParm;
538 mr->rdFunction = func;
541 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
545 /******************************************************************
549 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
550 short param3, short param4)
553 METARECORD *mr = (METARECORD *)&buffer;
554 WORD *params = mr->rdParm;
557 mr->rdFunction = func;
562 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
566 /******************************************************************
570 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
571 short param3, short param4, short param5, short param6)
574 METARECORD *mr = (METARECORD *)&buffer;
575 WORD *params = mr->rdParm;
578 mr->rdFunction = func;
585 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
589 /******************************************************************
592 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
593 short param3, short param4, short param5,
594 short param6, short param7, short param8)
597 METARECORD *mr = (METARECORD *)&buffer;
598 WORD *params = mr->rdParm;
601 mr->rdFunction = func;
610 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);