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 MFDRV_SetBkColor, /* pSetBkColor */
166 MFDRV_SetBkMode, /* pSetBkMode */
167 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
168 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
169 NULL, /* pSetDIBColorTable */
170 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
171 NULL, /* pSetDeviceClipping */
172 NULL, /* pSetDeviceGammaRamp */
173 NULL, /* pSetLayout */
174 MFDRV_SetMapMode, /* pSetMapMode */
175 MFDRV_SetMapperFlags, /* pSetMapperFlags */
176 MFDRV_SetPixel, /* pSetPixel */
177 NULL, /* pSetPixelFormat */
178 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
179 MFDRV_SetROP2, /* pSetROP2 */
180 MFDRV_SetRelAbs, /* pSetRelAbs */
181 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
182 MFDRV_SetTextAlign, /* pSetTextAlign */
183 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
184 MFDRV_SetTextColor, /* pSetTextColor */
185 MFDRV_SetTextJustification, /* pSetTextJustification */
186 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
187 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
188 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
189 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
190 NULL, /* pSetWorldTransform */
191 NULL, /* pStartDoc */
192 NULL, /* pStartPage */
193 MFDRV_StretchBlt, /* pStretchBlt */
194 MFDRV_StretchDIBits, /* pStretchDIBits */
195 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
196 MFDRV_StrokePath, /* pStrokePath */
197 NULL, /* pSwapBuffers */
198 NULL, /* pUnrealizePalette */
199 MFDRV_WidenPath /* pWidenPath */
204 /**********************************************************************
205 * MFDRV_AllocMetaFile
207 static DC *MFDRV_AllocMetaFile(void)
210 METAFILEDRV_PDEVICE *physDev;
212 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
214 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
220 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
222 HeapFree( GetProcessHeap(), 0, physDev );
227 push_dc_driver( dc, &physDev->dev, &MFDRV_Funcs );
229 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
230 physDev->handles_size = HANDLE_LIST_INC;
231 physDev->cur_handles = 0;
235 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
236 physDev->mh->mtVersion = 0x0300;
237 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
238 physDev->mh->mtNoObjects = 0;
239 physDev->mh->mtMaxRecord = 0;
240 physDev->mh->mtNoParameters = 0;
242 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
248 /**********************************************************************
251 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
253 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
256 HeapFree( GetProcessHeap(), 0, physDev->mh );
257 for(index = 0; index < physDev->handles_size; index++)
258 if(physDev->handles[index])
259 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
260 HeapFree( GetProcessHeap(), 0, physDev->handles );
261 HeapFree( GetProcessHeap(), 0, physDev );
266 /**********************************************************************
267 * CreateMetaFileW (GDI32.@)
269 * Create a new DC and associate it with a metafile. Pass a filename
270 * to create a disk-based metafile, NULL to create a memory metafile.
273 * filename [I] Filename of disk metafile
276 * A handle to the metafile DC if successful, NULL on failure.
278 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
282 METAFILEDRV_PDEVICE *physDev;
285 TRACE("%s\n", debugstr_w(filename) );
287 if (!(dc = MFDRV_AllocMetaFile())) return 0;
288 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
290 if (filename) /* disk based metafile */
292 physDev->mh->mtType = METAFILE_DISK;
293 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
294 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
298 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
303 physDev->hFile = hFile;
305 /* Grow METAHEADER to include filename */
306 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
308 else /* memory based metafile */
309 physDev->mh->mtType = METAFILE_MEMORY;
311 TRACE("returning %p\n", physDev->dev.hdc);
312 ret = physDev->dev.hdc;
313 release_dc_ptr( dc );
317 /**********************************************************************
318 * CreateMetaFileA (GDI32.@)
320 * See CreateMetaFileW.
322 HDC WINAPI CreateMetaFileA(LPCSTR filename)
328 if (!filename) return CreateMetaFileW(NULL);
330 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
331 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
332 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
334 hReturnDC = CreateMetaFileW(filenameW);
336 HeapFree( GetProcessHeap(), 0, filenameW );
342 /**********************************************************************
343 * MFDRV_CloseMetaFile
345 static DC *MFDRV_CloseMetaFile( HDC hdc )
348 METAFILEDRV_PDEVICE *physDev;
350 TRACE("(%p)\n", hdc );
352 if (!(dc = get_dc_ptr( hdc ))) return NULL;
353 if (dc->header.type != OBJ_METADC)
355 release_dc_ptr( dc );
358 if (dc->refcount != 1)
360 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
361 release_dc_ptr( dc );
364 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
366 /* Construct the end of metafile record - this is documented
367 * in SDK Knowledgebase Q99334.
370 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
376 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
378 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
383 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
384 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
389 CloseHandle(physDev->hFile);
390 physDev->mh->mtType = METAFILE_DISK;
396 /******************************************************************
397 * CloseMetaFile (GDI32.@)
399 * Stop recording graphics operations in metafile associated with
400 * hdc and retrieve metafile.
403 * hdc [I] Metafile DC to close
406 * Handle of newly created metafile on success, NULL on failure.
408 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
411 METAFILEDRV_PDEVICE *physDev;
412 DC *dc = MFDRV_CloseMetaFile(hdc);
414 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
416 /* Now allocate a global handle for the metafile */
418 hmf = MF_Create_HMETAFILE( physDev->mh );
420 physDev->mh = NULL; /* So it won't be deleted */
426 /******************************************************************
429 * Warning: this function can change the pointer to the metafile header.
431 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
435 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
437 switch(physDev->mh->mtType)
439 case METAFILE_MEMORY:
440 len = physDev->mh->mtSize * 2 + rlen;
441 /* reallocate memory if needed */
442 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
446 size += size / 2 + rlen;
447 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
448 if (!mh) return FALSE;
450 TRACE("Reallocated metafile: new size is %d\n",size);
452 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
455 TRACE("Writing record to disk\n");
456 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
460 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
464 physDev->mh->mtSize += rlen / 2;
465 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
470 /******************************************************************
474 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
477 METARECORD *mr = (METARECORD *)&buffer;
480 mr->rdFunction = func;
481 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
485 /******************************************************************
488 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
491 METARECORD *mr = (METARECORD *)&buffer;
494 mr->rdFunction = func;
495 *(mr->rdParm) = param1;
496 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
500 /******************************************************************
503 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
506 METARECORD *mr = (METARECORD *)&buffer;
509 mr->rdFunction = func;
510 *(mr->rdParm) = param2;
511 *(mr->rdParm + 1) = param1;
512 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
516 /******************************************************************
520 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
521 short param3, short param4)
524 METARECORD *mr = (METARECORD *)&buffer;
527 mr->rdFunction = func;
528 *(mr->rdParm) = param4;
529 *(mr->rdParm + 1) = param3;
530 *(mr->rdParm + 2) = param2;
531 *(mr->rdParm + 3) = param1;
532 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
536 /******************************************************************
540 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
541 short param3, short param4, short param5, short param6)
544 METARECORD *mr = (METARECORD *)&buffer;
547 mr->rdFunction = func;
548 *(mr->rdParm) = param6;
549 *(mr->rdParm + 1) = param5;
550 *(mr->rdParm + 2) = param4;
551 *(mr->rdParm + 3) = param3;
552 *(mr->rdParm + 4) = param2;
553 *(mr->rdParm + 5) = param1;
554 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
558 /******************************************************************
561 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
562 short param3, short param4, short param5,
563 short param6, short param7, short param8)
566 METARECORD *mr = (METARECORD *)&buffer;
569 mr->rdFunction = func;
570 *(mr->rdParm) = param8;
571 *(mr->rdParm + 1) = param7;
572 *(mr->rdParm + 2) = param6;
573 *(mr->rdParm + 3) = param5;
574 *(mr->rdParm + 4) = param4;
575 *(mr->rdParm + 5) = param3;
576 *(mr->rdParm + 6) = param2;
577 *(mr->rdParm + 7) = param1;
578 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);