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, /* 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, /* pSetDCOrg */
124 NULL, /* pSetDCPenColor*/
125 NULL, /* pSetDIBColorTable */
126 NULL, /* pSetDIBits */
127 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
128 NULL, /* pSetDeviceClipping */
129 NULL, /* pSetDeviceGammaRamp */
130 MFDRV_SetMapMode, /* pSetMapMode */
131 MFDRV_SetMapperFlags, /* pSetMapperFlags */
132 MFDRV_SetPixel, /* pSetPixel */
133 NULL, /* pSetPixelFormat */
134 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
135 MFDRV_SetROP2, /* pSetROP2 */
136 MFDRV_SetRelAbs, /* pSetRelAbs */
137 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
138 MFDRV_SetTextAlign, /* pSetTextAlign */
139 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
140 MFDRV_SetTextColor, /* pSetTextColor */
141 MFDRV_SetTextJustification, /* pSetTextJustification */
142 MFDRV_SetViewportExt, /* pSetViewportExt */
143 MFDRV_SetViewportOrg, /* pSetViewportOrg */
144 MFDRV_SetWindowExt, /* pSetWindowExt */
145 MFDRV_SetWindowOrg, /* pSetWindowOrg */
146 NULL, /* pSetWorldTransform */
147 NULL, /* pStartDoc */
148 NULL, /* pStartPage */
149 MFDRV_StretchBlt, /* pStretchBlt */
150 MFDRV_StretchDIBits, /* pStretchDIBits */
151 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
152 MFDRV_StrokePath, /* pStrokePath */
153 NULL, /* pSwapBuffers */
154 NULL, /* pUnrealizePalette */
155 MFDRV_WidenPath /* pWidenPath */
160 /**********************************************************************
161 * MFDRV_AllocMetaFile
163 static DC *MFDRV_AllocMetaFile(void)
166 METAFILEDRV_PDEVICE *physDev;
168 if (!(dc = DC_AllocDC( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
170 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
176 dc->physDev = (PHYSDEV)physDev;
177 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;
204 /**********************************************************************
207 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
209 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
210 DC *dc = physDev->dc;
213 HeapFree( GetProcessHeap(), 0, physDev->mh );
214 for(index = 0; index < physDev->handles_size; index++)
215 if(physDev->handles[index])
216 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
217 HeapFree( GetProcessHeap(), 0, physDev->handles );
218 HeapFree( GetProcessHeap(), 0, physDev );
225 /**********************************************************************
226 * CreateMetaFileW (GDI32.@)
228 * Create a new DC and associate it with a metafile. Pass a filename
229 * to create a disk-based metafile, NULL to create a memory metafile.
232 * filename [I] Filename of disk metafile
235 * A handle to the metafile DC if successful, NULL on failure.
237 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
241 METAFILEDRV_PDEVICE *physDev;
244 TRACE("%s\n", debugstr_w(filename) );
246 if (!(dc = MFDRV_AllocMetaFile())) return 0;
247 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
249 if (filename) /* disk based metafile */
251 physDev->mh->mtType = METAFILE_DISK;
252 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
253 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
254 MFDRV_DeleteDC( dc->physDev );
257 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
259 MFDRV_DeleteDC( dc->physDev );
262 physDev->hFile = hFile;
264 /* Grow METAHEADER to include filename */
265 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
267 else /* memory based metafile */
268 physDev->mh->mtType = METAFILE_MEMORY;
270 TRACE("returning %p\n", dc->hSelf);
272 DC_ReleaseDCPtr( dc );
276 /**********************************************************************
277 * CreateMetaFileA (GDI32.@)
279 * See CreateMetaFileW.
281 HDC WINAPI CreateMetaFileA(LPCSTR filename)
287 if (!filename) return CreateMetaFileW(NULL);
289 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
290 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
291 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
293 hReturnDC = CreateMetaFileW(filenameW);
295 HeapFree( GetProcessHeap(), 0, filenameW );
301 /**********************************************************************
302 * MFDRV_CloseMetaFile
304 static DC *MFDRV_CloseMetaFile( HDC hdc )
307 METAFILEDRV_PDEVICE *physDev;
309 TRACE("(%p)\n", hdc );
311 if (!(dc = DC_GetDCPtr( hdc ))) return NULL;
312 if (GDIMAGIC(dc->header.wMagic) != METAFILE_DC_MAGIC)
314 DC_ReleaseDCPtr( dc );
317 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
319 /* Construct the end of metafile record - this is documented
320 * in SDK Knowledgebase Q99334.
323 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
325 MFDRV_DeleteDC( dc->physDev );
329 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
331 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
332 MFDRV_DeleteDC( dc->physDev );
336 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
337 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
338 sizeof(*physDev->mh), NULL, NULL)) {
339 MFDRV_DeleteDC( dc->physDev );
342 CloseHandle(physDev->hFile);
343 physDev->mh->mtType = METAFILE_DISK;
349 /******************************************************************
350 * MF_Create_HMETATFILE16
352 * Creates a HMETAFILE16 object from a METAHEADER
354 * HMETAFILE16s are Global memory handles.
356 HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
359 DWORD size = mh->mtSize * sizeof(WORD);
361 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
364 METAHEADER *mh_dest = GlobalLock16(hmf);
365 memcpy(mh_dest, mh, size);
368 HeapFree(GetProcessHeap(), 0, mh);
372 /******************************************************************
373 * CloseMetaFile (GDI.126)
376 * hdc [I] Metafile DC to close
378 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
381 METAFILEDRV_PDEVICE *physDev;
382 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
384 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
386 /* Now allocate a global handle for the metafile */
388 hmf = MF_Create_HMETAFILE16( physDev->mh );
390 physDev->mh = NULL; /* So it won't be deleted */
391 MFDRV_DeleteDC( dc->physDev );
395 /******************************************************************
396 * CloseMetaFile (GDI32.@)
398 * Stop recording graphics operations in metafile associated with
399 * hdc and retrieve metafile.
402 * hdc [I] Metafile DC to close
405 * Handle of newly created metafile on success, NULL on failure.
407 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
410 METAFILEDRV_PDEVICE *physDev;
411 DC *dc = MFDRV_CloseMetaFile(hdc);
413 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
415 /* Now allocate a global handle for the metafile */
417 hmf = MF_Create_HMETAFILE( physDev->mh );
419 physDev->mh = NULL; /* So it won't be deleted */
420 MFDRV_DeleteDC( dc->physDev );
425 /******************************************************************
428 * Warning: this function can change the pointer to the metafile header.
430 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
434 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
436 switch(physDev->mh->mtType)
438 case METAFILE_MEMORY:
439 len = physDev->mh->mtSize * 2 + rlen;
440 /* reallocate memory if needed */
441 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
445 size += size / 2 + rlen;
446 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
447 if (!mh) return FALSE;
449 TRACE("Reallocated metafile: new size is %d\n",size);
451 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
454 TRACE("Writing record to disk\n");
455 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
459 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
463 physDev->mh->mtSize += rlen / 2;
464 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
469 /******************************************************************
473 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
476 METARECORD *mr = (METARECORD *)&buffer;
479 mr->rdFunction = func;
480 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
484 /******************************************************************
487 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
490 METARECORD *mr = (METARECORD *)&buffer;
493 mr->rdFunction = func;
494 *(mr->rdParm) = param1;
495 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
499 /******************************************************************
502 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
505 METARECORD *mr = (METARECORD *)&buffer;
508 mr->rdFunction = func;
509 *(mr->rdParm) = param2;
510 *(mr->rdParm + 1) = param1;
511 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
515 /******************************************************************
519 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
520 short param3, short param4)
523 METARECORD *mr = (METARECORD *)&buffer;
526 mr->rdFunction = func;
527 *(mr->rdParm) = param4;
528 *(mr->rdParm + 1) = param3;
529 *(mr->rdParm + 2) = param2;
530 *(mr->rdParm + 3) = param1;
531 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
535 /******************************************************************
539 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
540 short param3, short param4, short param5, short param6)
543 METARECORD *mr = (METARECORD *)&buffer;
546 mr->rdFunction = func;
547 *(mr->rdParm) = param6;
548 *(mr->rdParm + 1) = param5;
549 *(mr->rdParm + 2) = param4;
550 *(mr->rdParm + 3) = param3;
551 *(mr->rdParm + 4) = param2;
552 *(mr->rdParm + 5) = param1;
553 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
557 /******************************************************************
560 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
561 short param3, short param4, short param5,
562 short param6, short param7, short param8)
565 METARECORD *mr = (METARECORD *)&buffer;
568 mr->rdFunction = func;
569 *(mr->rdParm) = param8;
570 *(mr->rdParm + 1) = param7;
571 *(mr->rdParm + 2) = param6;
572 *(mr->rdParm + 3) = param5;
573 *(mr->rdParm + 4) = param4;
574 *(mr->rdParm + 5) = param3;
575 *(mr->rdParm + 6) = param2;
576 *(mr->rdParm + 7) = param1;
577 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
581 /**********************************************************************
584 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
585 INT cbOutput, LPVOID out_data )
591 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
593 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
594 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
595 mr->rdSize = len / 2;
596 mr->rdFunction = META_ESCAPE;
597 mr->rdParm[0] = nEscape;
598 mr->rdParm[1] = cbInput;
599 memcpy(&(mr->rdParm[2]), in_data, cbInput);
600 ret = MFDRV_WriteRecord( dev, mr, len);
601 HeapFree(GetProcessHeap(), 0, mr);
606 /******************************************************************
607 * MFDRV_GetDeviceCaps
609 *A very simple implementation that returns DT_METAFILE
611 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
620 TRACE(" unsupported capability %d, will return 0\n", cap );