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 NULL, /* pSetDIBColorTable */
188 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
189 NULL, /* pSetDeviceClipping */
190 NULL, /* pSetDeviceGammaRamp */
191 NULL, /* pSetLayout */
192 MFDRV_SetMapMode, /* pSetMapMode */
193 MFDRV_SetMapperFlags, /* pSetMapperFlags */
194 MFDRV_SetPixel, /* pSetPixel */
195 NULL, /* pSetPixelFormat */
196 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
197 MFDRV_SetROP2, /* pSetROP2 */
198 MFDRV_SetRelAbs, /* pSetRelAbs */
199 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
200 MFDRV_SetTextAlign, /* pSetTextAlign */
201 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
202 MFDRV_SetTextColor, /* pSetTextColor */
203 MFDRV_SetTextJustification, /* pSetTextJustification */
204 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
205 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
206 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
207 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
208 NULL, /* pSetWorldTransform */
209 NULL, /* pStartDoc */
210 NULL, /* pStartPage */
211 MFDRV_StretchBlt, /* pStretchBlt */
212 MFDRV_StretchDIBits, /* pStretchDIBits */
213 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
214 MFDRV_StrokePath, /* pStrokePath */
215 NULL, /* pSwapBuffers */
216 NULL, /* pUnrealizePalette */
217 MFDRV_WidenPath /* pWidenPath */
222 /**********************************************************************
223 * MFDRV_AllocMetaFile
225 static DC *MFDRV_AllocMetaFile(void)
228 METAFILEDRV_PDEVICE *physDev;
230 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
232 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
238 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
240 HeapFree( GetProcessHeap(), 0, physDev );
245 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
247 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
248 physDev->handles_size = HANDLE_LIST_INC;
249 physDev->cur_handles = 0;
253 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
254 physDev->mh->mtVersion = 0x0300;
255 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
256 physDev->mh->mtNoObjects = 0;
257 physDev->mh->mtMaxRecord = 0;
258 physDev->mh->mtNoParameters = 0;
260 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
266 /**********************************************************************
267 * MFDRV_CreateCompatibleDC
269 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
271 /* not supported on metafile DCs */
276 /**********************************************************************
279 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
281 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
284 HeapFree( GetProcessHeap(), 0, physDev->mh );
285 for(index = 0; index < physDev->handles_size; index++)
286 if(physDev->handles[index])
287 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
288 HeapFree( GetProcessHeap(), 0, physDev->handles );
289 HeapFree( GetProcessHeap(), 0, physDev );
294 /**********************************************************************
295 * CreateMetaFileW (GDI32.@)
297 * Create a new DC and associate it with a metafile. Pass a filename
298 * to create a disk-based metafile, NULL to create a memory metafile.
301 * filename [I] Filename of disk metafile
304 * A handle to the metafile DC if successful, NULL on failure.
306 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
310 METAFILEDRV_PDEVICE *physDev;
313 TRACE("%s\n", debugstr_w(filename) );
315 if (!(dc = MFDRV_AllocMetaFile())) return 0;
316 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
318 if (filename) /* disk based metafile */
320 physDev->mh->mtType = METAFILE_DISK;
321 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
322 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
326 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
331 physDev->hFile = hFile;
333 /* Grow METAHEADER to include filename */
334 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
336 else /* memory based metafile */
337 physDev->mh->mtType = METAFILE_MEMORY;
339 TRACE("returning %p\n", physDev->dev.hdc);
340 ret = physDev->dev.hdc;
341 release_dc_ptr( dc );
345 /**********************************************************************
346 * CreateMetaFileA (GDI32.@)
348 * See CreateMetaFileW.
350 HDC WINAPI CreateMetaFileA(LPCSTR filename)
356 if (!filename) return CreateMetaFileW(NULL);
358 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
359 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
360 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
362 hReturnDC = CreateMetaFileW(filenameW);
364 HeapFree( GetProcessHeap(), 0, filenameW );
370 /**********************************************************************
371 * MFDRV_CloseMetaFile
373 static DC *MFDRV_CloseMetaFile( HDC hdc )
376 METAFILEDRV_PDEVICE *physDev;
378 TRACE("(%p)\n", hdc );
380 if (!(dc = get_dc_ptr( hdc ))) return NULL;
381 if (dc->header.type != OBJ_METADC)
383 release_dc_ptr( dc );
386 if (dc->refcount != 1)
388 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
389 release_dc_ptr( dc );
392 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
394 /* Construct the end of metafile record - this is documented
395 * in SDK Knowledgebase Q99334.
398 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
404 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
406 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
411 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
412 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
417 CloseHandle(physDev->hFile);
418 physDev->mh->mtType = METAFILE_DISK;
424 /******************************************************************
425 * CloseMetaFile (GDI32.@)
427 * Stop recording graphics operations in metafile associated with
428 * hdc and retrieve metafile.
431 * hdc [I] Metafile DC to close
434 * Handle of newly created metafile on success, NULL on failure.
436 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
439 METAFILEDRV_PDEVICE *physDev;
440 DC *dc = MFDRV_CloseMetaFile(hdc);
442 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
444 /* Now allocate a global handle for the metafile */
446 hmf = MF_Create_HMETAFILE( physDev->mh );
448 physDev->mh = NULL; /* So it won't be deleted */
454 /******************************************************************
457 * Warning: this function can change the pointer to the metafile header.
459 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
463 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
465 switch(physDev->mh->mtType)
467 case METAFILE_MEMORY:
468 len = physDev->mh->mtSize * 2 + rlen;
469 /* reallocate memory if needed */
470 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
474 size += size / 2 + rlen;
475 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
476 if (!mh) return FALSE;
478 TRACE("Reallocated metafile: new size is %d\n",size);
480 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
483 TRACE("Writing record to disk\n");
484 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
488 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
492 physDev->mh->mtSize += rlen / 2;
493 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
498 /******************************************************************
502 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
505 METARECORD *mr = (METARECORD *)&buffer;
508 mr->rdFunction = func;
509 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
513 /******************************************************************
516 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
519 METARECORD *mr = (METARECORD *)&buffer;
520 WORD *params = mr->rdParm;
523 mr->rdFunction = func;
525 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
529 /******************************************************************
532 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
535 METARECORD *mr = (METARECORD *)&buffer;
536 WORD *params = mr->rdParm;
539 mr->rdFunction = func;
542 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
546 /******************************************************************
550 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
551 short param3, short param4)
554 METARECORD *mr = (METARECORD *)&buffer;
555 WORD *params = mr->rdParm;
558 mr->rdFunction = func;
563 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
567 /******************************************************************
571 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
572 short param3, short param4, short param5, short param6)
575 METARECORD *mr = (METARECORD *)&buffer;
576 WORD *params = mr->rdParm;
579 mr->rdFunction = func;
586 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
590 /******************************************************************
593 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
594 short param3, short param4, short param5,
595 short param6, short param7, short param8)
598 METARECORD *mr = (METARECORD *)&buffer;
599 WORD *params = mr->rdParm;
602 mr->rdFunction = func;
611 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);