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 DC_FUNCTIONS MFDRV_Funcs =
85 MFDRV_AbortPath, /* pAbortPath */
86 NULL, /* pAlphaBlend */
90 MFDRV_BeginPath, /* pBeginPath */
91 NULL, /* pChoosePixelFormat */
92 MFDRV_Chord, /* pChord */
93 MFDRV_CloseFigure, /* pCloseFigure */
94 NULL, /* pCreateBitmap */
95 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
97 NULL, /* pCreateDIBSection */
98 NULL, /* pDeleteBitmap */
99 MFDRV_DeleteDC, /* pDeleteDC */
100 MFDRV_DeleteObject, /* pDeleteObject */
101 NULL, /* pDescribePixelFormat */
102 NULL, /* pDeviceCapabilities */
103 MFDRV_Ellipse, /* pEllipse */
106 MFDRV_EndPath, /* pEndPath */
107 NULL, /* pEnumDeviceFonts */
108 NULL, /* pEnumICMProfiles */
109 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
110 NULL, /* pExtDeviceMode */
111 MFDRV_ExtEscape, /* pExtEscape */
112 MFDRV_ExtFloodFill, /* pExtFloodFill */
113 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
114 MFDRV_ExtTextOut, /* pExtTextOut */
115 MFDRV_FillPath, /* pFillPath */
116 MFDRV_FillRgn, /* pFillRgn */
117 MFDRV_FlattenPath, /* pFlattenPath */
118 MFDRV_FrameRgn, /* pFrameRgn */
119 NULL, /* pGdiComment */
120 NULL, /* pGetCharWidth */
121 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
122 NULL, /* pGetDeviceGammaRamp */
123 NULL, /* pGetICMProfile */
124 NULL, /* pGetImage */
125 NULL, /* pGetNearestColor */
126 NULL, /* pGetPixel */
127 NULL, /* pGetPixelFormat */
128 NULL, /* pGetSystemPaletteEntries */
129 NULL, /* pGetTextExtentExPoint */
130 NULL, /* pGetTextMetrics */
131 MFDRV_IntersectClipRect, /* pIntersectClipRect */
132 MFDRV_InvertRgn, /* pInvertRgn */
133 MFDRV_LineTo, /* pLineTo */
134 NULL, /* pModifyWorldTransform */
135 MFDRV_MoveTo, /* pMoveTo */
136 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
137 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
138 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
139 MFDRV_PaintRgn, /* pPaintRgn */
140 MFDRV_PatBlt, /* pPatBlt */
141 MFDRV_Pie, /* pPie */
142 MFDRV_PolyBezier, /* pPolyBezier */
143 MFDRV_PolyBezierTo, /* pPolyBezierTo */
144 NULL, /* pPolyDraw */
145 MFDRV_PolyPolygon, /* pPolyPolygon */
146 NULL, /* pPolyPolyline */
147 MFDRV_Polygon, /* pPolygon */
148 MFDRV_Polyline, /* pPolyline */
149 NULL, /* pPolylineTo */
150 NULL, /* pPutImage */
151 NULL, /* pRealizeDefaultPalette */
152 MFDRV_RealizePalette, /* pRealizePalette */
153 MFDRV_Rectangle, /* pRectangle */
155 MFDRV_RestoreDC, /* pRestoreDC */
156 MFDRV_RoundRect, /* pRoundRect */
157 MFDRV_SaveDC, /* pSaveDC */
158 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
159 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
160 MFDRV_SelectBitmap, /* pSelectBitmap */
161 MFDRV_SelectBrush, /* pSelectBrush */
162 MFDRV_SelectClipPath, /* pSelectClipPath */
163 MFDRV_SelectFont, /* pSelectFont */
164 MFDRV_SelectPalette, /* pSelectPalette */
165 MFDRV_SelectPen, /* pSelectPen */
166 NULL, /* pSetArcDirection */
167 MFDRV_SetBkColor, /* pSetBkColor */
168 MFDRV_SetBkMode, /* pSetBkMode */
169 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
170 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
171 NULL, /* pSetDIBColorTable */
172 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
173 NULL, /* pSetDeviceClipping */
174 NULL, /* pSetDeviceGammaRamp */
175 NULL, /* pSetLayout */
176 MFDRV_SetMapMode, /* pSetMapMode */
177 MFDRV_SetMapperFlags, /* pSetMapperFlags */
178 MFDRV_SetPixel, /* pSetPixel */
179 NULL, /* pSetPixelFormat */
180 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
181 MFDRV_SetROP2, /* pSetROP2 */
182 MFDRV_SetRelAbs, /* pSetRelAbs */
183 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
184 MFDRV_SetTextAlign, /* pSetTextAlign */
185 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
186 MFDRV_SetTextColor, /* pSetTextColor */
187 MFDRV_SetTextJustification, /* pSetTextJustification */
188 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
189 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
190 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
191 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
192 NULL, /* pSetWorldTransform */
193 NULL, /* pStartDoc */
194 NULL, /* pStartPage */
195 MFDRV_StretchBlt, /* pStretchBlt */
196 MFDRV_StretchDIBits, /* pStretchDIBits */
197 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
198 MFDRV_StrokePath, /* pStrokePath */
199 NULL, /* pSwapBuffers */
200 NULL, /* pUnrealizePalette */
201 MFDRV_WidenPath /* pWidenPath */
206 /**********************************************************************
207 * MFDRV_AllocMetaFile
209 static DC *MFDRV_AllocMetaFile(void)
212 METAFILEDRV_PDEVICE *physDev;
214 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
216 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
222 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
224 HeapFree( GetProcessHeap(), 0, physDev );
229 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
231 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
232 physDev->handles_size = HANDLE_LIST_INC;
233 physDev->cur_handles = 0;
237 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
238 physDev->mh->mtVersion = 0x0300;
239 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
240 physDev->mh->mtNoObjects = 0;
241 physDev->mh->mtMaxRecord = 0;
242 physDev->mh->mtNoParameters = 0;
244 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
250 /**********************************************************************
251 * MFDRV_CreateCompatibleDC
253 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
255 /* not supported on metafile DCs */
260 /**********************************************************************
263 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
265 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
268 HeapFree( GetProcessHeap(), 0, physDev->mh );
269 for(index = 0; index < physDev->handles_size; index++)
270 if(physDev->handles[index])
271 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
272 HeapFree( GetProcessHeap(), 0, physDev->handles );
273 HeapFree( GetProcessHeap(), 0, physDev );
278 /**********************************************************************
279 * CreateMetaFileW (GDI32.@)
281 * Create a new DC and associate it with a metafile. Pass a filename
282 * to create a disk-based metafile, NULL to create a memory metafile.
285 * filename [I] Filename of disk metafile
288 * A handle to the metafile DC if successful, NULL on failure.
290 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
294 METAFILEDRV_PDEVICE *physDev;
297 TRACE("%s\n", debugstr_w(filename) );
299 if (!(dc = MFDRV_AllocMetaFile())) return 0;
300 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
302 if (filename) /* disk based metafile */
304 physDev->mh->mtType = METAFILE_DISK;
305 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
306 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
310 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
315 physDev->hFile = hFile;
317 /* Grow METAHEADER to include filename */
318 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
320 else /* memory based metafile */
321 physDev->mh->mtType = METAFILE_MEMORY;
323 TRACE("returning %p\n", physDev->dev.hdc);
324 ret = physDev->dev.hdc;
325 release_dc_ptr( dc );
329 /**********************************************************************
330 * CreateMetaFileA (GDI32.@)
332 * See CreateMetaFileW.
334 HDC WINAPI CreateMetaFileA(LPCSTR filename)
340 if (!filename) return CreateMetaFileW(NULL);
342 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
343 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
344 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
346 hReturnDC = CreateMetaFileW(filenameW);
348 HeapFree( GetProcessHeap(), 0, filenameW );
354 /**********************************************************************
355 * MFDRV_CloseMetaFile
357 static DC *MFDRV_CloseMetaFile( HDC hdc )
360 METAFILEDRV_PDEVICE *physDev;
362 TRACE("(%p)\n", hdc );
364 if (!(dc = get_dc_ptr( hdc ))) return NULL;
365 if (dc->header.type != OBJ_METADC)
367 release_dc_ptr( dc );
370 if (dc->refcount != 1)
372 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
373 release_dc_ptr( dc );
376 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
378 /* Construct the end of metafile record - this is documented
379 * in SDK Knowledgebase Q99334.
382 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
388 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
390 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
395 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
396 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
401 CloseHandle(physDev->hFile);
402 physDev->mh->mtType = METAFILE_DISK;
408 /******************************************************************
409 * CloseMetaFile (GDI32.@)
411 * Stop recording graphics operations in metafile associated with
412 * hdc and retrieve metafile.
415 * hdc [I] Metafile DC to close
418 * Handle of newly created metafile on success, NULL on failure.
420 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
423 METAFILEDRV_PDEVICE *physDev;
424 DC *dc = MFDRV_CloseMetaFile(hdc);
426 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
428 /* Now allocate a global handle for the metafile */
430 hmf = MF_Create_HMETAFILE( physDev->mh );
432 physDev->mh = NULL; /* So it won't be deleted */
438 /******************************************************************
441 * Warning: this function can change the pointer to the metafile header.
443 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
447 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
449 switch(physDev->mh->mtType)
451 case METAFILE_MEMORY:
452 len = physDev->mh->mtSize * 2 + rlen;
453 /* reallocate memory if needed */
454 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
458 size += size / 2 + rlen;
459 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
460 if (!mh) return FALSE;
462 TRACE("Reallocated metafile: new size is %d\n",size);
464 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
467 TRACE("Writing record to disk\n");
468 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
472 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
476 physDev->mh->mtSize += rlen / 2;
477 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
482 /******************************************************************
486 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
489 METARECORD *mr = (METARECORD *)&buffer;
492 mr->rdFunction = func;
493 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
497 /******************************************************************
500 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
503 METARECORD *mr = (METARECORD *)&buffer;
506 mr->rdFunction = func;
507 *(mr->rdParm) = param1;
508 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
512 /******************************************************************
515 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
518 METARECORD *mr = (METARECORD *)&buffer;
521 mr->rdFunction = func;
522 *(mr->rdParm) = param2;
523 *(mr->rdParm + 1) = param1;
524 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
528 /******************************************************************
532 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
533 short param3, short param4)
536 METARECORD *mr = (METARECORD *)&buffer;
539 mr->rdFunction = func;
540 *(mr->rdParm) = param4;
541 *(mr->rdParm + 1) = param3;
542 *(mr->rdParm + 2) = param2;
543 *(mr->rdParm + 3) = param1;
544 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
548 /******************************************************************
552 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
553 short param3, short param4, short param5, short param6)
556 METARECORD *mr = (METARECORD *)&buffer;
559 mr->rdFunction = func;
560 *(mr->rdParm) = param6;
561 *(mr->rdParm + 1) = param5;
562 *(mr->rdParm + 2) = param4;
563 *(mr->rdParm + 3) = param3;
564 *(mr->rdParm + 4) = param2;
565 *(mr->rdParm + 5) = param1;
566 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
570 /******************************************************************
573 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
574 short param3, short param4, short param5,
575 short param6, short param7, short param8)
578 METARECORD *mr = (METARECORD *)&buffer;
581 mr->rdFunction = func;
582 *(mr->rdParm) = param8;
583 *(mr->rdParm + 1) = param7;
584 *(mr->rdParm + 2) = param6;
585 *(mr->rdParm + 3) = param5;
586 *(mr->rdParm + 4) = param4;
587 *(mr->rdParm + 5) = param3;
588 *(mr->rdParm + 6) = param2;
589 *(mr->rdParm + 7) = param1;
590 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);