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 const DC_FUNCTIONS MFDRV_Funcs =
36 MFDRV_AbortPath, /* pAbortPath */
37 NULL, /* pAlphaBlend */
41 MFDRV_BeginPath, /* pBeginPath */
43 NULL, /* pChoosePixelFormat */
44 MFDRV_Chord, /* pChord */
45 MFDRV_CloseFigure, /* pCloseFigure */
46 NULL, /* pCreateBitmap */
48 NULL, /* pCreateDIBSection */
49 NULL, /* pDeleteBitmap */
51 MFDRV_DeleteObject, /* pDeleteObject */
52 NULL, /* pDescribePixelFormat */
53 NULL, /* pDeviceCapabilities */
54 MFDRV_Ellipse, /* pEllipse */
57 MFDRV_EndPath, /* pEndPath */
58 NULL, /* pEnumDeviceFonts */
59 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
60 NULL, /* pExtDeviceMode */
61 MFDRV_ExtEscape, /* pExtEscape */
62 MFDRV_ExtFloodFill, /* pExtFloodFill */
63 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
64 MFDRV_ExtTextOut, /* pExtTextOut */
65 MFDRV_FillPath, /* pFillPath */
66 MFDRV_FillRgn, /* pFillRgn */
67 MFDRV_FlattenPath, /* pFlattenPath */
68 MFDRV_FrameRgn, /* pFrameRgn */
69 NULL, /* pGdiComment */
70 NULL, /* pGetBitmapBits */
71 NULL, /* pGetCharWidth */
72 NULL, /* pGetDCOrgEx */
73 NULL, /* pGetDIBColorTable */
74 NULL, /* pGetDIBits */
75 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
76 NULL, /* pGetDeviceGammaRamp */
77 NULL, /* pGetICMProfile */
78 NULL, /* pGetNearestColor */
80 NULL, /* pGetPixelFormat */
81 NULL, /* pGetSystemPaletteEntries */
82 NULL, /* pGetTextExtentExPoint */
83 NULL, /* pGetTextMetrics */
84 MFDRV_IntersectClipRect, /* pIntersectClipRect */
85 MFDRV_InvertRgn, /* pInvertRgn */
86 MFDRV_LineTo, /* pLineTo */
87 NULL, /* pModifyWorldTransform */
88 MFDRV_MoveTo, /* pMoveTo */
89 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
90 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
91 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
92 MFDRV_PaintRgn, /* pPaintRgn */
93 MFDRV_PatBlt, /* pPatBlt */
95 MFDRV_PolyBezier, /* pPolyBezier */
96 MFDRV_PolyBezierTo, /* pPolyBezierTo */
98 MFDRV_PolyPolygon, /* pPolyPolygon */
99 NULL, /* pPolyPolyline */
100 MFDRV_Polygon, /* pPolygon */
101 MFDRV_Polyline, /* pPolyline */
102 NULL, /* pPolylineTo */
103 NULL, /* pRealizeDefaultPalette */
104 MFDRV_RealizePalette, /* pRealizePalette */
105 MFDRV_Rectangle, /* pRectangle */
107 MFDRV_RestoreDC, /* pRestoreDC */
108 MFDRV_RoundRect, /* pRoundRect */
109 MFDRV_SaveDC, /* pSaveDC */
110 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
111 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
112 MFDRV_SelectBitmap, /* pSelectBitmap */
113 MFDRV_SelectBrush, /* pSelectBrush */
114 MFDRV_SelectClipPath, /* pSelectClipPath */
115 MFDRV_SelectFont, /* pSelectFont */
116 MFDRV_SelectPalette, /* pSelectPalette */
117 MFDRV_SelectPen, /* pSelectPen */
118 NULL, /* pSetArcDirection */
119 NULL, /* pSetBitmapBits */
120 MFDRV_SetBkColor, /* pSetBkColor */
121 MFDRV_SetBkMode, /* pSetBkMode */
122 NULL, /* pSetDCBrushColor*/
123 NULL, /* pSetDCPenColor*/
124 NULL, /* pSetDIBColorTable */
125 NULL, /* pSetDIBits */
126 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
127 NULL, /* pSetDeviceClipping */
128 NULL, /* pSetDeviceGammaRamp */
129 MFDRV_SetMapMode, /* pSetMapMode */
130 MFDRV_SetMapperFlags, /* pSetMapperFlags */
131 MFDRV_SetPixel, /* pSetPixel */
132 NULL, /* pSetPixelFormat */
133 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
134 MFDRV_SetROP2, /* pSetROP2 */
135 MFDRV_SetRelAbs, /* pSetRelAbs */
136 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
137 MFDRV_SetTextAlign, /* pSetTextAlign */
138 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
139 MFDRV_SetTextColor, /* pSetTextColor */
140 MFDRV_SetTextJustification, /* pSetTextJustification */
141 MFDRV_SetViewportExt, /* pSetViewportExt */
142 MFDRV_SetViewportOrg, /* pSetViewportOrg */
143 MFDRV_SetWindowExt, /* pSetWindowExt */
144 MFDRV_SetWindowOrg, /* pSetWindowOrg */
145 NULL, /* pSetWorldTransform */
146 NULL, /* pStartDoc */
147 NULL, /* pStartPage */
148 MFDRV_StretchBlt, /* pStretchBlt */
149 MFDRV_StretchDIBits, /* pStretchDIBits */
150 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
151 MFDRV_StrokePath, /* pStrokePath */
152 NULL, /* pSwapBuffers */
153 NULL, /* pUnrealizePalette */
154 MFDRV_WidenPath /* pWidenPath */
159 /**********************************************************************
160 * MFDRV_AllocMetaFile
162 static DC *MFDRV_AllocMetaFile(void)
165 METAFILEDRV_PDEVICE *physDev;
167 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
169 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
175 dc->physDev = (PHYSDEV)physDev;
176 physDev->hdc = dc->hSelf;
178 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
180 HeapFree( GetProcessHeap(), 0, physDev );
185 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
186 physDev->handles_size = HANDLE_LIST_INC;
187 physDev->cur_handles = 0;
191 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
192 physDev->mh->mtVersion = 0x0300;
193 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
194 physDev->mh->mtNoObjects = 0;
195 physDev->mh->mtMaxRecord = 0;
196 physDev->mh->mtNoParameters = 0;
198 SetVirtualResolution(dc->hSelf, 0, 0, 0, 0);
204 /**********************************************************************
207 static BOOL MFDRV_DeleteDC( DC *dc )
209 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
212 HeapFree( GetProcessHeap(), 0, physDev->mh );
213 for(index = 0; index < physDev->handles_size; index++)
214 if(physDev->handles[index])
215 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
216 HeapFree( GetProcessHeap(), 0, physDev->handles );
217 HeapFree( GetProcessHeap(), 0, physDev );
224 /**********************************************************************
225 * CreateMetaFileW (GDI32.@)
227 * Create a new DC and associate it with a metafile. Pass a filename
228 * to create a disk-based metafile, NULL to create a memory metafile.
231 * filename [I] Filename of disk metafile
234 * A handle to the metafile DC if successful, NULL on failure.
236 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
240 METAFILEDRV_PDEVICE *physDev;
243 TRACE("%s\n", debugstr_w(filename) );
245 if (!(dc = MFDRV_AllocMetaFile())) return 0;
246 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
248 if (filename) /* disk based metafile */
250 physDev->mh->mtType = METAFILE_DISK;
251 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
252 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
253 MFDRV_DeleteDC( dc );
256 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
258 MFDRV_DeleteDC( dc );
261 physDev->hFile = hFile;
263 /* Grow METAHEADER to include filename */
264 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
266 else /* memory based metafile */
267 physDev->mh->mtType = METAFILE_MEMORY;
269 TRACE("returning %p\n", dc->hSelf);
271 release_dc_ptr( dc );
275 /**********************************************************************
276 * CreateMetaFileA (GDI32.@)
278 * See CreateMetaFileW.
280 HDC WINAPI CreateMetaFileA(LPCSTR filename)
286 if (!filename) return CreateMetaFileW(NULL);
288 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
289 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
290 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
292 hReturnDC = CreateMetaFileW(filenameW);
294 HeapFree( GetProcessHeap(), 0, filenameW );
300 /**********************************************************************
301 * MFDRV_CloseMetaFile
303 static DC *MFDRV_CloseMetaFile( HDC hdc )
306 METAFILEDRV_PDEVICE *physDev;
308 TRACE("(%p)\n", hdc );
310 if (!(dc = get_dc_ptr( hdc ))) return NULL;
311 if (dc->header.type != OBJ_METADC)
313 release_dc_ptr( dc );
316 if (dc->refcount != 1)
318 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
319 release_dc_ptr( dc );
322 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
324 /* Construct the end of metafile record - this is documented
325 * in SDK Knowledgebase Q99334.
328 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
330 MFDRV_DeleteDC( dc );
334 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
336 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
337 MFDRV_DeleteDC( dc );
341 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
342 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
344 MFDRV_DeleteDC( dc );
347 CloseHandle(physDev->hFile);
348 physDev->mh->mtType = METAFILE_DISK;
354 /******************************************************************
355 * CloseMetaFile (GDI32.@)
357 * Stop recording graphics operations in metafile associated with
358 * hdc and retrieve metafile.
361 * hdc [I] Metafile DC to close
364 * Handle of newly created metafile on success, NULL on failure.
366 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
369 METAFILEDRV_PDEVICE *physDev;
370 DC *dc = MFDRV_CloseMetaFile(hdc);
372 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
374 /* Now allocate a global handle for the metafile */
376 hmf = MF_Create_HMETAFILE( physDev->mh );
378 physDev->mh = NULL; /* So it won't be deleted */
379 MFDRV_DeleteDC( dc );
384 /******************************************************************
387 * Warning: this function can change the pointer to the metafile header.
389 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
393 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
395 switch(physDev->mh->mtType)
397 case METAFILE_MEMORY:
398 len = physDev->mh->mtSize * 2 + rlen;
399 /* reallocate memory if needed */
400 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
404 size += size / 2 + rlen;
405 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
406 if (!mh) return FALSE;
408 TRACE("Reallocated metafile: new size is %d\n",size);
410 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
413 TRACE("Writing record to disk\n");
414 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
418 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
422 physDev->mh->mtSize += rlen / 2;
423 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
428 /******************************************************************
432 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
435 METARECORD *mr = (METARECORD *)&buffer;
438 mr->rdFunction = func;
439 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
443 /******************************************************************
446 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
449 METARECORD *mr = (METARECORD *)&buffer;
452 mr->rdFunction = func;
453 *(mr->rdParm) = param1;
454 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
458 /******************************************************************
461 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
464 METARECORD *mr = (METARECORD *)&buffer;
467 mr->rdFunction = func;
468 *(mr->rdParm) = param2;
469 *(mr->rdParm + 1) = param1;
470 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
474 /******************************************************************
478 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
479 short param3, short param4)
482 METARECORD *mr = (METARECORD *)&buffer;
485 mr->rdFunction = func;
486 *(mr->rdParm) = param4;
487 *(mr->rdParm + 1) = param3;
488 *(mr->rdParm + 2) = param2;
489 *(mr->rdParm + 3) = param1;
490 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
494 /******************************************************************
498 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
499 short param3, short param4, short param5, short param6)
502 METARECORD *mr = (METARECORD *)&buffer;
505 mr->rdFunction = func;
506 *(mr->rdParm) = param6;
507 *(mr->rdParm + 1) = param5;
508 *(mr->rdParm + 2) = param4;
509 *(mr->rdParm + 3) = param3;
510 *(mr->rdParm + 4) = param2;
511 *(mr->rdParm + 5) = param1;
512 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
516 /******************************************************************
519 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
520 short param3, short param4, short param5,
521 short param6, short param7, short param8)
524 METARECORD *mr = (METARECORD *)&buffer;
527 mr->rdFunction = func;
528 *(mr->rdParm) = param8;
529 *(mr->rdParm + 1) = param7;
530 *(mr->rdParm + 2) = param6;
531 *(mr->rdParm + 3) = param5;
532 *(mr->rdParm + 4) = param4;
533 *(mr->rdParm + 5) = param3;
534 *(mr->rdParm + 6) = param2;
535 *(mr->rdParm + 7) = param1;
536 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
540 /**********************************************************************
543 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
544 INT cbOutput, LPVOID out_data )
550 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
552 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
553 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
554 mr->rdSize = len / 2;
555 mr->rdFunction = META_ESCAPE;
556 mr->rdParm[0] = nEscape;
557 mr->rdParm[1] = cbInput;
558 memcpy(&(mr->rdParm[2]), in_data, cbInput);
559 ret = MFDRV_WriteRecord( dev, mr, len);
560 HeapFree(GetProcessHeap(), 0, mr);
565 /******************************************************************
566 * MFDRV_GetDeviceCaps
568 *A very simple implementation that returns DT_METAFILE
570 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
579 TRACE(" unsupported capability %d, will return 0\n", cap );