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 NULL, /* pEnumICMProfiles */
60 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
61 NULL, /* pExtDeviceMode */
62 MFDRV_ExtEscape, /* pExtEscape */
63 MFDRV_ExtFloodFill, /* pExtFloodFill */
64 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
65 MFDRV_ExtTextOut, /* pExtTextOut */
66 MFDRV_FillPath, /* pFillPath */
67 MFDRV_FillRgn, /* pFillRgn */
68 MFDRV_FlattenPath, /* pFlattenPath */
69 MFDRV_FrameRgn, /* pFrameRgn */
70 NULL, /* pGdiComment */
71 NULL, /* pGetBitmapBits */
72 NULL, /* pGetCharWidth */
73 NULL, /* pGetDIBits */
74 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
75 NULL, /* pGetDeviceGammaRamp */
76 NULL, /* pGetICMProfile */
77 NULL, /* pGetNearestColor */
79 NULL, /* pGetPixelFormat */
80 NULL, /* pGetSystemPaletteEntries */
81 NULL, /* pGetTextExtentExPoint */
82 NULL, /* pGetTextMetrics */
83 MFDRV_IntersectClipRect, /* pIntersectClipRect */
84 MFDRV_InvertRgn, /* pInvertRgn */
85 MFDRV_LineTo, /* pLineTo */
86 NULL, /* pModifyWorldTransform */
87 MFDRV_MoveTo, /* pMoveTo */
88 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
89 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
90 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
91 MFDRV_PaintRgn, /* pPaintRgn */
92 MFDRV_PatBlt, /* pPatBlt */
94 MFDRV_PolyBezier, /* pPolyBezier */
95 MFDRV_PolyBezierTo, /* pPolyBezierTo */
97 MFDRV_PolyPolygon, /* pPolyPolygon */
98 NULL, /* pPolyPolyline */
99 MFDRV_Polygon, /* pPolygon */
100 MFDRV_Polyline, /* pPolyline */
101 NULL, /* pPolylineTo */
102 NULL, /* pRealizeDefaultPalette */
103 MFDRV_RealizePalette, /* pRealizePalette */
104 MFDRV_Rectangle, /* pRectangle */
106 MFDRV_RestoreDC, /* pRestoreDC */
107 MFDRV_RoundRect, /* pRoundRect */
108 MFDRV_SaveDC, /* pSaveDC */
109 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
110 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
111 MFDRV_SelectBitmap, /* pSelectBitmap */
112 MFDRV_SelectBrush, /* pSelectBrush */
113 MFDRV_SelectClipPath, /* pSelectClipPath */
114 MFDRV_SelectFont, /* pSelectFont */
115 MFDRV_SelectPalette, /* pSelectPalette */
116 MFDRV_SelectPen, /* pSelectPen */
117 NULL, /* pSetArcDirection */
118 NULL, /* pSetBitmapBits */
119 MFDRV_SetBkColor, /* pSetBkColor */
120 MFDRV_SetBkMode, /* pSetBkMode */
121 NULL, /* pSetDCBrushColor*/
122 NULL, /* pSetDCPenColor*/
123 NULL, /* pSetDIBColorTable */
124 NULL, /* pSetDIBits */
125 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
126 NULL, /* pSetDeviceClipping */
127 NULL, /* pSetDeviceGammaRamp */
128 MFDRV_SetMapMode, /* pSetMapMode */
129 MFDRV_SetMapperFlags, /* pSetMapperFlags */
130 MFDRV_SetPixel, /* pSetPixel */
131 NULL, /* pSetPixelFormat */
132 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
133 MFDRV_SetROP2, /* pSetROP2 */
134 MFDRV_SetRelAbs, /* pSetRelAbs */
135 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
136 MFDRV_SetTextAlign, /* pSetTextAlign */
137 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
138 MFDRV_SetTextColor, /* pSetTextColor */
139 MFDRV_SetTextJustification, /* pSetTextJustification */
140 MFDRV_SetViewportExt, /* pSetViewportExt */
141 MFDRV_SetViewportOrg, /* pSetViewportOrg */
142 MFDRV_SetWindowExt, /* pSetWindowExt */
143 MFDRV_SetWindowOrg, /* pSetWindowOrg */
144 NULL, /* pSetWorldTransform */
145 NULL, /* pStartDoc */
146 NULL, /* pStartPage */
147 MFDRV_StretchBlt, /* pStretchBlt */
148 MFDRV_StretchDIBits, /* pStretchDIBits */
149 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
150 MFDRV_StrokePath, /* pStrokePath */
151 NULL, /* pSwapBuffers */
152 NULL, /* pUnrealizePalette */
153 MFDRV_WidenPath /* pWidenPath */
158 /**********************************************************************
159 * MFDRV_AllocMetaFile
161 static DC *MFDRV_AllocMetaFile(void)
164 METAFILEDRV_PDEVICE *physDev;
166 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
168 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
174 physDev->dev.funcs = &MFDRV_Funcs;
175 push_dc_driver( dc, &physDev->dev );
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 );