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, /* pCreateBitmap */
96 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
98 NULL, /* pCreateDIBSection */
99 NULL, /* pDeleteBitmap */
100 MFDRV_DeleteDC, /* pDeleteDC */
101 MFDRV_DeleteObject, /* pDeleteObject */
102 NULL, /* pDescribePixelFormat */
103 NULL, /* pDeviceCapabilities */
104 MFDRV_Ellipse, /* pEllipse */
107 MFDRV_EndPath, /* pEndPath */
108 NULL, /* pEnumFonts */
109 NULL, /* pEnumICMProfiles */
110 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
111 NULL, /* pExtDeviceMode */
112 MFDRV_ExtEscape, /* pExtEscape */
113 MFDRV_ExtFloodFill, /* pExtFloodFill */
114 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
115 MFDRV_ExtTextOut, /* pExtTextOut */
116 MFDRV_FillPath, /* pFillPath */
117 MFDRV_FillRgn, /* pFillRgn */
118 MFDRV_FlattenPath, /* pFlattenPath */
119 NULL, /* pFontIsLinked */
120 MFDRV_FrameRgn, /* pFrameRgn */
121 NULL, /* pGdiComment */
122 NULL, /* pGdiRealizationInfo */
123 NULL, /* pGetCharABCWidths */
124 NULL, /* pGetCharABCWidthsI */
125 NULL, /* pGetCharWidth */
126 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
127 NULL, /* pGetDeviceGammaRamp */
128 NULL, /* pGetFontData */
129 NULL, /* pGetFontUnicodeRanges */
130 NULL, /* pGetGlyphIndices */
131 NULL, /* pGetGlyphOutline */
132 NULL, /* pGetICMProfile */
133 NULL, /* pGetImage */
134 NULL, /* pGetKerningPairs */
135 NULL, /* pGetNearestColor */
136 NULL, /* pGetOutlineTextMetrics */
137 NULL, /* pGetPixel */
138 NULL, /* pGetPixelFormat */
139 NULL, /* pGetSystemPaletteEntries */
140 NULL, /* pGetTextCharsetInfo */
141 NULL, /* pGetTextExtentExPoint */
142 NULL, /* pGetTextExtentExPointI */
143 NULL, /* pGetTextFace */
144 NULL, /* pGetTextMetrics */
145 MFDRV_IntersectClipRect, /* pIntersectClipRect */
146 MFDRV_InvertRgn, /* pInvertRgn */
147 MFDRV_LineTo, /* pLineTo */
148 NULL, /* pModifyWorldTransform */
149 MFDRV_MoveTo, /* pMoveTo */
150 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
151 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
152 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
153 MFDRV_PaintRgn, /* pPaintRgn */
154 MFDRV_PatBlt, /* pPatBlt */
155 MFDRV_Pie, /* pPie */
156 MFDRV_PolyBezier, /* pPolyBezier */
157 MFDRV_PolyBezierTo, /* pPolyBezierTo */
158 NULL, /* pPolyDraw */
159 MFDRV_PolyPolygon, /* pPolyPolygon */
160 NULL, /* pPolyPolyline */
161 MFDRV_Polygon, /* pPolygon */
162 MFDRV_Polyline, /* pPolyline */
163 NULL, /* pPolylineTo */
164 NULL, /* pPutImage */
165 NULL, /* pRealizeDefaultPalette */
166 MFDRV_RealizePalette, /* pRealizePalette */
167 MFDRV_Rectangle, /* pRectangle */
169 MFDRV_RestoreDC, /* pRestoreDC */
170 MFDRV_RoundRect, /* pRoundRect */
171 MFDRV_SaveDC, /* pSaveDC */
172 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
173 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
174 MFDRV_SelectBitmap, /* pSelectBitmap */
175 MFDRV_SelectBrush, /* pSelectBrush */
176 MFDRV_SelectClipPath, /* pSelectClipPath */
177 MFDRV_SelectFont, /* pSelectFont */
178 MFDRV_SelectPalette, /* pSelectPalette */
179 MFDRV_SelectPen, /* pSelectPen */
180 NULL, /* pSetArcDirection */
181 MFDRV_SetBkColor, /* pSetBkColor */
182 MFDRV_SetBkMode, /* pSetBkMode */
183 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
184 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
185 NULL, /* pSetDIBColorTable */
186 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
187 NULL, /* pSetDeviceClipping */
188 NULL, /* pSetDeviceGammaRamp */
189 NULL, /* pSetLayout */
190 MFDRV_SetMapMode, /* pSetMapMode */
191 MFDRV_SetMapperFlags, /* pSetMapperFlags */
192 MFDRV_SetPixel, /* pSetPixel */
193 NULL, /* pSetPixelFormat */
194 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
195 MFDRV_SetROP2, /* pSetROP2 */
196 MFDRV_SetRelAbs, /* pSetRelAbs */
197 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
198 MFDRV_SetTextAlign, /* pSetTextAlign */
199 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
200 MFDRV_SetTextColor, /* pSetTextColor */
201 MFDRV_SetTextJustification, /* pSetTextJustification */
202 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
203 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
204 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
205 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
206 NULL, /* pSetWorldTransform */
207 NULL, /* pStartDoc */
208 NULL, /* pStartPage */
209 MFDRV_StretchBlt, /* pStretchBlt */
210 MFDRV_StretchDIBits, /* pStretchDIBits */
211 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
212 MFDRV_StrokePath, /* pStrokePath */
213 NULL, /* pSwapBuffers */
214 NULL, /* pUnrealizePalette */
215 MFDRV_WidenPath /* pWidenPath */
220 /**********************************************************************
221 * MFDRV_AllocMetaFile
223 static DC *MFDRV_AllocMetaFile(void)
226 METAFILEDRV_PDEVICE *physDev;
228 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
230 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
236 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
238 HeapFree( GetProcessHeap(), 0, physDev );
243 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
245 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
246 physDev->handles_size = HANDLE_LIST_INC;
247 physDev->cur_handles = 0;
251 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
252 physDev->mh->mtVersion = 0x0300;
253 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
254 physDev->mh->mtNoObjects = 0;
255 physDev->mh->mtMaxRecord = 0;
256 physDev->mh->mtNoParameters = 0;
258 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
264 /**********************************************************************
265 * MFDRV_CreateCompatibleDC
267 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
269 /* not supported on metafile DCs */
274 /**********************************************************************
277 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
279 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
282 HeapFree( GetProcessHeap(), 0, physDev->mh );
283 for(index = 0; index < physDev->handles_size; index++)
284 if(physDev->handles[index])
285 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
286 HeapFree( GetProcessHeap(), 0, physDev->handles );
287 HeapFree( GetProcessHeap(), 0, physDev );
292 /**********************************************************************
293 * CreateMetaFileW (GDI32.@)
295 * Create a new DC and associate it with a metafile. Pass a filename
296 * to create a disk-based metafile, NULL to create a memory metafile.
299 * filename [I] Filename of disk metafile
302 * A handle to the metafile DC if successful, NULL on failure.
304 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
308 METAFILEDRV_PDEVICE *physDev;
311 TRACE("%s\n", debugstr_w(filename) );
313 if (!(dc = MFDRV_AllocMetaFile())) return 0;
314 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
316 if (filename) /* disk based metafile */
318 physDev->mh->mtType = METAFILE_DISK;
319 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
320 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
324 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
329 physDev->hFile = hFile;
331 /* Grow METAHEADER to include filename */
332 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
334 else /* memory based metafile */
335 physDev->mh->mtType = METAFILE_MEMORY;
337 TRACE("returning %p\n", physDev->dev.hdc);
338 ret = physDev->dev.hdc;
339 release_dc_ptr( dc );
343 /**********************************************************************
344 * CreateMetaFileA (GDI32.@)
346 * See CreateMetaFileW.
348 HDC WINAPI CreateMetaFileA(LPCSTR filename)
354 if (!filename) return CreateMetaFileW(NULL);
356 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
357 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
358 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
360 hReturnDC = CreateMetaFileW(filenameW);
362 HeapFree( GetProcessHeap(), 0, filenameW );
368 /**********************************************************************
369 * MFDRV_CloseMetaFile
371 static DC *MFDRV_CloseMetaFile( HDC hdc )
374 METAFILEDRV_PDEVICE *physDev;
376 TRACE("(%p)\n", hdc );
378 if (!(dc = get_dc_ptr( hdc ))) return NULL;
379 if (dc->header.type != OBJ_METADC)
381 release_dc_ptr( dc );
384 if (dc->refcount != 1)
386 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
387 release_dc_ptr( dc );
390 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
392 /* Construct the end of metafile record - this is documented
393 * in SDK Knowledgebase Q99334.
396 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
402 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
404 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
409 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
410 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
415 CloseHandle(physDev->hFile);
416 physDev->mh->mtType = METAFILE_DISK;
422 /******************************************************************
423 * CloseMetaFile (GDI32.@)
425 * Stop recording graphics operations in metafile associated with
426 * hdc and retrieve metafile.
429 * hdc [I] Metafile DC to close
432 * Handle of newly created metafile on success, NULL on failure.
434 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
437 METAFILEDRV_PDEVICE *physDev;
438 DC *dc = MFDRV_CloseMetaFile(hdc);
440 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
442 /* Now allocate a global handle for the metafile */
444 hmf = MF_Create_HMETAFILE( physDev->mh );
446 physDev->mh = NULL; /* So it won't be deleted */
452 /******************************************************************
455 * Warning: this function can change the pointer to the metafile header.
457 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
461 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
463 switch(physDev->mh->mtType)
465 case METAFILE_MEMORY:
466 len = physDev->mh->mtSize * 2 + rlen;
467 /* reallocate memory if needed */
468 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
472 size += size / 2 + rlen;
473 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
474 if (!mh) return FALSE;
476 TRACE("Reallocated metafile: new size is %d\n",size);
478 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
481 TRACE("Writing record to disk\n");
482 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
486 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
490 physDev->mh->mtSize += rlen / 2;
491 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
496 /******************************************************************
500 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
503 METARECORD *mr = (METARECORD *)&buffer;
506 mr->rdFunction = func;
507 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
511 /******************************************************************
514 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
517 METARECORD *mr = (METARECORD *)&buffer;
518 WORD *params = mr->rdParm;
521 mr->rdFunction = func;
523 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
527 /******************************************************************
530 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
533 METARECORD *mr = (METARECORD *)&buffer;
534 WORD *params = mr->rdParm;
537 mr->rdFunction = func;
540 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
544 /******************************************************************
548 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
549 short param3, short param4)
552 METARECORD *mr = (METARECORD *)&buffer;
553 WORD *params = mr->rdParm;
556 mr->rdFunction = func;
561 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
565 /******************************************************************
569 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
570 short param3, short param4, short param5, short param6)
573 METARECORD *mr = (METARECORD *)&buffer;
574 WORD *params = mr->rdParm;
577 mr->rdFunction = func;
584 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
588 /******************************************************************
591 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
592 short param3, short param4, short param5,
593 short param6, short param7, short param8)
596 METARECORD *mr = (METARECORD *)&buffer;
597 WORD *params = mr->rdParm;
600 mr->rdFunction = func;
609 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);