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 static UINT MFDRV_GetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
71 /******************************************************************
74 static UINT MFDRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
80 /******************************************************************
83 *A very simple implementation that returns DT_METAFILE
85 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
94 TRACE(" unsupported capability %d, will return 0\n", cap );
100 static const struct gdi_dc_funcs MFDRV_Funcs =
102 NULL, /* pAbortDoc */
103 MFDRV_AbortPath, /* pAbortPath */
104 NULL, /* pAlphaBlend */
105 NULL, /* pAngleArc */
106 MFDRV_Arc, /* pArc */
108 MFDRV_BeginPath, /* pBeginPath */
109 NULL, /* pBlendImage */
110 NULL, /* pChoosePixelFormat */
111 MFDRV_Chord, /* pChord */
112 MFDRV_CloseFigure, /* pCloseFigure */
113 NULL, /* pCopyBitmap */
114 NULL, /* pCreateBitmap */
115 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
116 NULL, /* pCreateDC */
117 NULL, /* pDeleteBitmap */
118 MFDRV_DeleteDC, /* pDeleteDC */
119 MFDRV_DeleteObject, /* pDeleteObject */
120 NULL, /* pDescribePixelFormat */
121 NULL, /* pDeviceCapabilities */
122 MFDRV_Ellipse, /* pEllipse */
125 MFDRV_EndPath, /* pEndPath */
126 NULL, /* pEnumFonts */
127 NULL, /* pEnumICMProfiles */
128 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
129 NULL, /* pExtDeviceMode */
130 MFDRV_ExtEscape, /* pExtEscape */
131 MFDRV_ExtFloodFill, /* pExtFloodFill */
132 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
133 MFDRV_ExtTextOut, /* pExtTextOut */
134 MFDRV_FillPath, /* pFillPath */
135 MFDRV_FillRgn, /* pFillRgn */
136 MFDRV_FlattenPath, /* pFlattenPath */
137 NULL, /* pFontIsLinked */
138 MFDRV_FrameRgn, /* pFrameRgn */
139 NULL, /* pGdiComment */
140 NULL, /* pGdiRealizationInfo */
141 MFDRV_GetBoundsRect, /* pGetBoundsRect */
142 NULL, /* pGetCharABCWidths */
143 NULL, /* pGetCharABCWidthsI */
144 NULL, /* pGetCharWidth */
145 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
146 NULL, /* pGetDeviceGammaRamp */
147 NULL, /* pGetFontData */
148 NULL, /* pGetFontUnicodeRanges */
149 NULL, /* pGetGlyphIndices */
150 NULL, /* pGetGlyphOutline */
151 NULL, /* pGetICMProfile */
152 NULL, /* pGetImage */
153 NULL, /* pGetKerningPairs */
154 NULL, /* pGetNearestColor */
155 NULL, /* pGetOutlineTextMetrics */
156 NULL, /* pGetPixel */
157 NULL, /* pGetPixelFormat */
158 NULL, /* pGetSystemPaletteEntries */
159 NULL, /* pGetTextCharsetInfo */
160 NULL, /* pGetTextExtentExPoint */
161 NULL, /* pGetTextExtentExPointI */
162 NULL, /* pGetTextFace */
163 NULL, /* pGetTextMetrics */
164 NULL, /* pGradientFill */
165 MFDRV_IntersectClipRect, /* pIntersectClipRect */
166 MFDRV_InvertRgn, /* pInvertRgn */
167 MFDRV_LineTo, /* pLineTo */
168 NULL, /* pModifyWorldTransform */
169 MFDRV_MoveTo, /* pMoveTo */
170 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
171 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
172 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
173 MFDRV_PaintRgn, /* pPaintRgn */
174 MFDRV_PatBlt, /* pPatBlt */
175 MFDRV_Pie, /* pPie */
176 MFDRV_PolyBezier, /* pPolyBezier */
177 MFDRV_PolyBezierTo, /* pPolyBezierTo */
178 NULL, /* pPolyDraw */
179 MFDRV_PolyPolygon, /* pPolyPolygon */
180 NULL, /* pPolyPolyline */
181 MFDRV_Polygon, /* pPolygon */
182 MFDRV_Polyline, /* pPolyline */
183 NULL, /* pPolylineTo */
184 NULL, /* pPutImage */
185 NULL, /* pRealizeDefaultPalette */
186 MFDRV_RealizePalette, /* pRealizePalette */
187 MFDRV_Rectangle, /* pRectangle */
189 MFDRV_RestoreDC, /* pRestoreDC */
190 MFDRV_RoundRect, /* pRoundRect */
191 MFDRV_SaveDC, /* pSaveDC */
192 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
193 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
194 MFDRV_SelectBitmap, /* pSelectBitmap */
195 MFDRV_SelectBrush, /* pSelectBrush */
196 MFDRV_SelectClipPath, /* pSelectClipPath */
197 MFDRV_SelectFont, /* pSelectFont */
198 MFDRV_SelectPalette, /* pSelectPalette */
199 MFDRV_SelectPen, /* pSelectPen */
200 NULL, /* pSetArcDirection */
201 MFDRV_SetBkColor, /* pSetBkColor */
202 MFDRV_SetBkMode, /* pSetBkMode */
203 MFDRV_SetBoundsRect, /* pSetBoundsRect */
204 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
205 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
206 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
207 NULL, /* pSetDeviceClipping */
208 NULL, /* pSetDeviceGammaRamp */
209 NULL, /* pSetLayout */
210 MFDRV_SetMapMode, /* pSetMapMode */
211 MFDRV_SetMapperFlags, /* pSetMapperFlags */
212 MFDRV_SetPixel, /* pSetPixel */
213 NULL, /* pSetPixelFormat */
214 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
215 MFDRV_SetROP2, /* pSetROP2 */
216 MFDRV_SetRelAbs, /* pSetRelAbs */
217 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
218 MFDRV_SetTextAlign, /* pSetTextAlign */
219 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
220 MFDRV_SetTextColor, /* pSetTextColor */
221 MFDRV_SetTextJustification, /* pSetTextJustification */
222 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
223 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
224 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
225 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
226 NULL, /* pSetWorldTransform */
227 NULL, /* pStartDoc */
228 NULL, /* pStartPage */
229 MFDRV_StretchBlt, /* pStretchBlt */
230 MFDRV_StretchDIBits, /* pStretchDIBits */
231 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
232 MFDRV_StrokePath, /* pStrokePath */
233 NULL, /* pSwapBuffers */
234 NULL, /* pUnrealizePalette */
235 MFDRV_WidenPath, /* pWidenPath */
236 NULL, /* pwglCopyContext */
237 NULL, /* pwglCreateContext */
238 NULL, /* pwglCreateContextAttribsARB */
239 NULL, /* pwglDeleteContext */
240 NULL, /* pwglGetProcAddress */
241 NULL, /* pwglMakeContextCurrentARB */
242 NULL, /* pwglMakeCurrent */
243 NULL, /* pwglSetPixelFormatWINE */
244 NULL, /* pwglShareLists */
245 NULL, /* pwglUseFontBitmapsA */
246 NULL, /* pwglUseFontBitmapsW */
247 GDI_PRIORITY_GRAPHICS_DRV /* priority */
252 /**********************************************************************
253 * MFDRV_AllocMetaFile
255 static DC *MFDRV_AllocMetaFile(void)
258 METAFILEDRV_PDEVICE *physDev;
260 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
262 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
268 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
270 HeapFree( GetProcessHeap(), 0, physDev );
275 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
277 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
278 physDev->handles_size = HANDLE_LIST_INC;
279 physDev->cur_handles = 0;
283 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
284 physDev->mh->mtVersion = 0x0300;
285 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
286 physDev->mh->mtNoObjects = 0;
287 physDev->mh->mtMaxRecord = 0;
288 physDev->mh->mtNoParameters = 0;
290 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
296 /**********************************************************************
297 * MFDRV_CreateCompatibleDC
299 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
301 /* not supported on metafile DCs */
306 /**********************************************************************
309 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
311 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
314 HeapFree( GetProcessHeap(), 0, physDev->mh );
315 for(index = 0; index < physDev->handles_size; index++)
316 if(physDev->handles[index])
317 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
318 HeapFree( GetProcessHeap(), 0, physDev->handles );
319 HeapFree( GetProcessHeap(), 0, physDev );
324 /**********************************************************************
325 * CreateMetaFileW (GDI32.@)
327 * Create a new DC and associate it with a metafile. Pass a filename
328 * to create a disk-based metafile, NULL to create a memory metafile.
331 * filename [I] Filename of disk metafile
334 * A handle to the metafile DC if successful, NULL on failure.
336 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
340 METAFILEDRV_PDEVICE *physDev;
343 TRACE("%s\n", debugstr_w(filename) );
345 if (!(dc = MFDRV_AllocMetaFile())) return 0;
346 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
348 if (filename) /* disk based metafile */
350 physDev->mh->mtType = METAFILE_DISK;
351 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
352 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
356 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
361 physDev->hFile = hFile;
363 /* Grow METAHEADER to include filename */
364 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
366 else /* memory based metafile */
367 physDev->mh->mtType = METAFILE_MEMORY;
369 TRACE("returning %p\n", physDev->dev.hdc);
370 ret = physDev->dev.hdc;
371 release_dc_ptr( dc );
375 /**********************************************************************
376 * CreateMetaFileA (GDI32.@)
378 * See CreateMetaFileW.
380 HDC WINAPI CreateMetaFileA(LPCSTR filename)
386 if (!filename) return CreateMetaFileW(NULL);
388 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
389 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
390 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
392 hReturnDC = CreateMetaFileW(filenameW);
394 HeapFree( GetProcessHeap(), 0, filenameW );
400 /**********************************************************************
401 * MFDRV_CloseMetaFile
403 static DC *MFDRV_CloseMetaFile( HDC hdc )
406 METAFILEDRV_PDEVICE *physDev;
408 TRACE("(%p)\n", hdc );
410 if (!(dc = get_dc_ptr( hdc ))) return NULL;
411 if (dc->header.type != OBJ_METADC)
413 release_dc_ptr( dc );
416 if (dc->refcount != 1)
418 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
419 release_dc_ptr( dc );
422 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
424 /* Construct the end of metafile record - this is documented
425 * in SDK Knowledgebase Q99334.
428 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
434 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
436 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
441 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
442 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
447 CloseHandle(physDev->hFile);
448 physDev->mh->mtType = METAFILE_DISK;
454 /******************************************************************
455 * CloseMetaFile (GDI32.@)
457 * Stop recording graphics operations in metafile associated with
458 * hdc and retrieve metafile.
461 * hdc [I] Metafile DC to close
464 * Handle of newly created metafile on success, NULL on failure.
466 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
469 METAFILEDRV_PDEVICE *physDev;
470 DC *dc = MFDRV_CloseMetaFile(hdc);
472 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
474 /* Now allocate a global handle for the metafile */
476 hmf = MF_Create_HMETAFILE( physDev->mh );
478 physDev->mh = NULL; /* So it won't be deleted */
484 /******************************************************************
487 * Warning: this function can change the pointer to the metafile header.
489 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
493 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
495 switch(physDev->mh->mtType)
497 case METAFILE_MEMORY:
498 len = physDev->mh->mtSize * 2 + rlen;
499 /* reallocate memory if needed */
500 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
504 size += size / 2 + rlen;
505 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
506 if (!mh) return FALSE;
508 TRACE("Reallocated metafile: new size is %d\n",size);
510 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
513 TRACE("Writing record to disk\n");
514 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
518 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
522 physDev->mh->mtSize += rlen / 2;
523 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
528 /******************************************************************
532 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
535 METARECORD *mr = (METARECORD *)&buffer;
538 mr->rdFunction = func;
539 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
543 /******************************************************************
546 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
549 METARECORD *mr = (METARECORD *)&buffer;
550 WORD *params = mr->rdParm;
553 mr->rdFunction = func;
555 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
559 /******************************************************************
562 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
565 METARECORD *mr = (METARECORD *)&buffer;
566 WORD *params = mr->rdParm;
569 mr->rdFunction = func;
572 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
576 /******************************************************************
580 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
581 short param3, short param4)
584 METARECORD *mr = (METARECORD *)&buffer;
585 WORD *params = mr->rdParm;
588 mr->rdFunction = func;
593 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
597 /******************************************************************
601 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
602 short param3, short param4, short param5, short param6)
605 METARECORD *mr = (METARECORD *)&buffer;
606 WORD *params = mr->rdParm;
609 mr->rdFunction = func;
616 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
620 /******************************************************************
623 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
624 short param3, short param4, short param5,
625 short param6, short param7, short param8)
628 METARECORD *mr = (METARECORD *)&buffer;
629 WORD *params = mr->rdParm;
632 mr->rdFunction = func;
641 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);