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
26 #include "wine/winbase16.h"
28 #include "gdi_private.h"
29 #include "mfdrv/metafiledrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
34 static const DC_FUNCTIONS MFDRV_Funcs =
37 MFDRV_AbortPath, /* pAbortPath */
38 NULL, /* pAlphaBlend */
42 MFDRV_BeginPath, /* pBeginPath */
43 MFDRV_BitBlt, /* pBitBlt */
44 NULL, /* pChoosePixelFormat */
45 MFDRV_Chord, /* pChord */
46 MFDRV_CloseFigure, /* pCloseFigure */
47 NULL, /* pCreateBitmap */
49 NULL, /* pCreateDIBSection */
50 NULL, /* pDeleteBitmap */
52 MFDRV_DeleteObject, /* pDeleteObject */
53 NULL, /* pDescribePixelFormat */
54 NULL, /* pDeviceCapabilities */
55 MFDRV_Ellipse, /* pEllipse */
58 MFDRV_EndPath, /* pEndPath */
59 NULL, /* pEnumDeviceFonts */
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, /* pGetDCOrgEx */
74 NULL, /* pGetDIBColorTable */
75 NULL, /* pGetDIBits */
76 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
77 NULL, /* pGetDeviceGammaRamp */
78 NULL, /* pGetICMProfile */
79 NULL, /* pGetNearestColor */
81 NULL, /* pGetPixelFormat */
82 NULL, /* pGetSystemPaletteEntries */
83 NULL, /* pGetTextExtentExPoint */
84 NULL, /* pGetTextMetrics */
85 MFDRV_IntersectClipRect, /* pIntersectClipRect */
86 MFDRV_InvertRgn, /* pInvertRgn */
87 MFDRV_LineTo, /* pLineTo */
88 NULL, /* pModifyWorldTransform */
89 MFDRV_MoveTo, /* pMoveTo */
90 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
91 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
92 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
93 MFDRV_PaintRgn, /* pPaintRgn */
94 MFDRV_PatBlt, /* pPatBlt */
96 MFDRV_PolyBezier, /* pPolyBezier */
97 MFDRV_PolyBezierTo, /* pPolyBezierTo */
99 MFDRV_PolyPolygon, /* pPolyPolygon */
100 NULL, /* pPolyPolyline */
101 MFDRV_Polygon, /* pPolygon */
102 MFDRV_Polyline, /* pPolyline */
103 NULL, /* pPolylineTo */
104 NULL, /* pRealizeDefaultPalette */
105 MFDRV_RealizePalette, /* pRealizePalette */
106 MFDRV_Rectangle, /* pRectangle */
108 MFDRV_RestoreDC, /* pRestoreDC */
109 MFDRV_RoundRect, /* pRoundRect */
110 MFDRV_SaveDC, /* pSaveDC */
111 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
112 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
113 MFDRV_SelectBitmap, /* pSelectBitmap */
114 MFDRV_SelectBrush, /* pSelectBrush */
115 MFDRV_SelectClipPath, /* pSelectClipPath */
116 MFDRV_SelectFont, /* pSelectFont */
117 MFDRV_SelectPalette, /* pSelectPalette */
118 MFDRV_SelectPen, /* pSelectPen */
119 NULL, /* pSetArcDirection */
120 NULL, /* pSetBitmapBits */
121 MFDRV_SetBkColor, /* pSetBkColor */
122 MFDRV_SetBkMode, /* pSetBkMode */
123 NULL, /* pSetDCBrushColor*/
124 NULL, /* pSetDCOrg */
125 NULL, /* pSetDCPenColor*/
126 NULL, /* pSetDIBColorTable */
127 NULL, /* pSetDIBits */
128 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
129 NULL, /* pSetDeviceClipping */
130 NULL, /* pSetDeviceGammaRamp */
131 MFDRV_SetMapMode, /* pSetMapMode */
132 MFDRV_SetMapperFlags, /* pSetMapperFlags */
133 MFDRV_SetPixel, /* pSetPixel */
134 NULL, /* pSetPixelFormat */
135 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
136 MFDRV_SetROP2, /* pSetROP2 */
137 MFDRV_SetRelAbs, /* pSetRelAbs */
138 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
139 MFDRV_SetTextAlign, /* pSetTextAlign */
140 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
141 MFDRV_SetTextColor, /* pSetTextColor */
142 MFDRV_SetTextJustification, /* pSetTextJustification */
143 MFDRV_SetViewportExt, /* pSetViewportExt */
144 MFDRV_SetViewportOrg, /* pSetViewportOrg */
145 MFDRV_SetWindowExt, /* pSetWindowExt */
146 MFDRV_SetWindowOrg, /* pSetWindowOrg */
147 NULL, /* pSetWorldTransform */
148 NULL, /* pStartDoc */
149 NULL, /* pStartPage */
150 MFDRV_StretchBlt, /* pStretchBlt */
151 MFDRV_StretchDIBits, /* pStretchDIBits */
152 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
153 MFDRV_StrokePath, /* pStrokePath */
154 NULL, /* pSwapBuffers */
155 NULL, /* pUnrealizePalette */
156 MFDRV_WidenPath /* pWidenPath */
161 /**********************************************************************
162 * MFDRV_AllocMetaFile
164 static DC *MFDRV_AllocMetaFile(void)
167 METAFILEDRV_PDEVICE *physDev;
169 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
171 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
177 dc->physDev = (PHYSDEV)physDev;
178 physDev->hdc = dc->hSelf;
180 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
182 HeapFree( GetProcessHeap(), 0, physDev );
187 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
188 physDev->handles_size = HANDLE_LIST_INC;
189 physDev->cur_handles = 0;
193 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
194 physDev->mh->mtVersion = 0x0300;
195 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
196 physDev->mh->mtNoObjects = 0;
197 physDev->mh->mtMaxRecord = 0;
198 physDev->mh->mtNoParameters = 0;
200 SetVirtualResolution(dc->hSelf, 0, 0, 0, 0);
206 /**********************************************************************
209 static BOOL MFDRV_DeleteDC( DC *dc )
211 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
214 HeapFree( GetProcessHeap(), 0, physDev->mh );
215 for(index = 0; index < physDev->handles_size; index++)
216 if(physDev->handles[index])
217 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
218 HeapFree( GetProcessHeap(), 0, physDev->handles );
219 HeapFree( GetProcessHeap(), 0, physDev );
226 /**********************************************************************
227 * CreateMetaFileW (GDI32.@)
229 * Create a new DC and associate it with a metafile. Pass a filename
230 * to create a disk-based metafile, NULL to create a memory metafile.
233 * filename [I] Filename of disk metafile
236 * A handle to the metafile DC if successful, NULL on failure.
238 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
242 METAFILEDRV_PDEVICE *physDev;
245 TRACE("%s\n", debugstr_w(filename) );
247 if (!(dc = MFDRV_AllocMetaFile())) return 0;
248 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
250 if (filename) /* disk based metafile */
252 physDev->mh->mtType = METAFILE_DISK;
253 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
254 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
255 MFDRV_DeleteDC( dc );
258 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
260 MFDRV_DeleteDC( dc );
263 physDev->hFile = hFile;
265 /* Grow METAHEADER to include filename */
266 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
268 else /* memory based metafile */
269 physDev->mh->mtType = METAFILE_MEMORY;
271 TRACE("returning %p\n", dc->hSelf);
273 release_dc_ptr( dc );
277 /**********************************************************************
278 * CreateMetaFileA (GDI32.@)
280 * See CreateMetaFileW.
282 HDC WINAPI CreateMetaFileA(LPCSTR filename)
288 if (!filename) return CreateMetaFileW(NULL);
290 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
291 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
292 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
294 hReturnDC = CreateMetaFileW(filenameW);
296 HeapFree( GetProcessHeap(), 0, filenameW );
302 /**********************************************************************
303 * MFDRV_CloseMetaFile
305 static DC *MFDRV_CloseMetaFile( HDC hdc )
308 METAFILEDRV_PDEVICE *physDev;
310 TRACE("(%p)\n", hdc );
312 if (!(dc = get_dc_ptr( hdc ))) return NULL;
313 if (dc->header.type != OBJ_METADC)
315 release_dc_ptr( dc );
318 if (dc->refcount != 1)
320 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
321 release_dc_ptr( dc );
324 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
326 /* Construct the end of metafile record - this is documented
327 * in SDK Knowledgebase Q99334.
330 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
332 MFDRV_DeleteDC( dc );
336 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
338 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
339 MFDRV_DeleteDC( dc );
343 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
344 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
346 MFDRV_DeleteDC( dc );
349 CloseHandle(physDev->hFile);
350 physDev->mh->mtType = METAFILE_DISK;
356 /******************************************************************
357 * MF_Create_HMETATFILE16
359 * Creates a HMETAFILE16 object from a METAHEADER
361 * HMETAFILE16s are Global memory handles.
363 HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
366 DWORD size = mh->mtSize * sizeof(WORD);
368 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
371 METAHEADER *mh_dest = GlobalLock16(hmf);
372 memcpy(mh_dest, mh, size);
375 HeapFree(GetProcessHeap(), 0, mh);
379 /******************************************************************
380 * CloseMetaFile (GDI.126)
383 * hdc [I] Metafile DC to close
385 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
388 METAFILEDRV_PDEVICE *physDev;
389 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
391 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
393 /* Now allocate a global handle for the metafile */
395 hmf = MF_Create_HMETAFILE16( physDev->mh );
397 physDev->mh = NULL; /* So it won't be deleted */
398 MFDRV_DeleteDC( dc );
402 /******************************************************************
403 * CloseMetaFile (GDI32.@)
405 * Stop recording graphics operations in metafile associated with
406 * hdc and retrieve metafile.
409 * hdc [I] Metafile DC to close
412 * Handle of newly created metafile on success, NULL on failure.
414 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
417 METAFILEDRV_PDEVICE *physDev;
418 DC *dc = MFDRV_CloseMetaFile(hdc);
420 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
422 /* Now allocate a global handle for the metafile */
424 hmf = MF_Create_HMETAFILE( physDev->mh );
426 physDev->mh = NULL; /* So it won't be deleted */
427 MFDRV_DeleteDC( dc );
432 /******************************************************************
435 * Warning: this function can change the pointer to the metafile header.
437 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
441 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
443 switch(physDev->mh->mtType)
445 case METAFILE_MEMORY:
446 len = physDev->mh->mtSize * 2 + rlen;
447 /* reallocate memory if needed */
448 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
452 size += size / 2 + rlen;
453 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
454 if (!mh) return FALSE;
456 TRACE("Reallocated metafile: new size is %d\n",size);
458 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
461 TRACE("Writing record to disk\n");
462 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
466 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
470 physDev->mh->mtSize += rlen / 2;
471 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
476 /******************************************************************
480 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
483 METARECORD *mr = (METARECORD *)&buffer;
486 mr->rdFunction = func;
487 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
491 /******************************************************************
494 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
497 METARECORD *mr = (METARECORD *)&buffer;
500 mr->rdFunction = func;
501 *(mr->rdParm) = param1;
502 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
506 /******************************************************************
509 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
512 METARECORD *mr = (METARECORD *)&buffer;
515 mr->rdFunction = func;
516 *(mr->rdParm) = param2;
517 *(mr->rdParm + 1) = param1;
518 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
522 /******************************************************************
526 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
527 short param3, short param4)
530 METARECORD *mr = (METARECORD *)&buffer;
533 mr->rdFunction = func;
534 *(mr->rdParm) = param4;
535 *(mr->rdParm + 1) = param3;
536 *(mr->rdParm + 2) = param2;
537 *(mr->rdParm + 3) = param1;
538 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
542 /******************************************************************
546 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
547 short param3, short param4, short param5, short param6)
550 METARECORD *mr = (METARECORD *)&buffer;
553 mr->rdFunction = func;
554 *(mr->rdParm) = param6;
555 *(mr->rdParm + 1) = param5;
556 *(mr->rdParm + 2) = param4;
557 *(mr->rdParm + 3) = param3;
558 *(mr->rdParm + 4) = param2;
559 *(mr->rdParm + 5) = param1;
560 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
564 /******************************************************************
567 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
568 short param3, short param4, short param5,
569 short param6, short param7, short param8)
572 METARECORD *mr = (METARECORD *)&buffer;
575 mr->rdFunction = func;
576 *(mr->rdParm) = param8;
577 *(mr->rdParm + 1) = param7;
578 *(mr->rdParm + 2) = param6;
579 *(mr->rdParm + 3) = param5;
580 *(mr->rdParm + 4) = param4;
581 *(mr->rdParm + 5) = param3;
582 *(mr->rdParm + 6) = param2;
583 *(mr->rdParm + 7) = param1;
584 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
588 /**********************************************************************
591 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
592 INT cbOutput, LPVOID out_data )
598 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
600 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
601 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
602 mr->rdSize = len / 2;
603 mr->rdFunction = META_ESCAPE;
604 mr->rdParm[0] = nEscape;
605 mr->rdParm[1] = cbInput;
606 memcpy(&(mr->rdParm[2]), in_data, cbInput);
607 ret = MFDRV_WriteRecord( dev, mr, len);
608 HeapFree(GetProcessHeap(), 0, mr);
613 /******************************************************************
614 * MFDRV_GetDeviceCaps
616 *A very simple implementation that returns DT_METAFILE
618 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
627 TRACE(" unsupported capability %d, will return 0\n", cap );