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 static UINT
MFDRV_GetBoundsRect( PHYSDEV dev
, RECT
*rect
, UINT flags
)
71 /******************************************************************
74 *A very simple implementation that returns DT_METAFILE
76 static INT
MFDRV_GetDeviceCaps(PHYSDEV dev
, INT cap
)
85 TRACE(" unsupported capability %d, will return 0\n", cap
);
91 static const struct gdi_dc_funcs MFDRV_Funcs
=
94 MFDRV_AbortPath
, /* pAbortPath */
95 NULL
, /* pAlphaBlend */
99 MFDRV_BeginPath
, /* pBeginPath */
100 NULL
, /* pBlendImage */
101 NULL
, /* pChoosePixelFormat */
102 MFDRV_Chord
, /* pChord */
103 MFDRV_CloseFigure
, /* pCloseFigure */
104 NULL
, /* pCopyBitmap */
105 NULL
, /* pCreateBitmap */
106 MFDRV_CreateCompatibleDC
, /* pCreateCompatibleDC */
107 NULL
, /* pCreateDC */
108 NULL
, /* pDeleteBitmap */
109 MFDRV_DeleteDC
, /* pDeleteDC */
110 MFDRV_DeleteObject
, /* pDeleteObject */
111 NULL
, /* pDescribePixelFormat */
112 NULL
, /* pDeviceCapabilities */
113 MFDRV_Ellipse
, /* pEllipse */
116 MFDRV_EndPath
, /* pEndPath */
117 NULL
, /* pEnumFonts */
118 NULL
, /* pEnumICMProfiles */
119 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
120 NULL
, /* pExtDeviceMode */
121 MFDRV_ExtEscape
, /* pExtEscape */
122 MFDRV_ExtFloodFill
, /* pExtFloodFill */
123 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
124 MFDRV_ExtTextOut
, /* pExtTextOut */
125 MFDRV_FillPath
, /* pFillPath */
126 MFDRV_FillRgn
, /* pFillRgn */
127 MFDRV_FlattenPath
, /* pFlattenPath */
128 NULL
, /* pFontIsLinked */
129 MFDRV_FrameRgn
, /* pFrameRgn */
130 NULL
, /* pGdiComment */
131 NULL
, /* pGdiRealizationInfo */
132 MFDRV_GetBoundsRect
, /* pGetBoundsRect */
133 NULL
, /* pGetCharABCWidths */
134 NULL
, /* pGetCharABCWidthsI */
135 NULL
, /* pGetCharWidth */
136 MFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
137 NULL
, /* pGetDeviceGammaRamp */
138 NULL
, /* pGetFontData */
139 NULL
, /* pGetFontUnicodeRanges */
140 NULL
, /* pGetGlyphIndices */
141 NULL
, /* pGetGlyphOutline */
142 NULL
, /* pGetICMProfile */
143 NULL
, /* pGetImage */
144 NULL
, /* pGetKerningPairs */
145 NULL
, /* pGetNearestColor */
146 NULL
, /* pGetOutlineTextMetrics */
147 NULL
, /* pGetPixel */
148 NULL
, /* pGetPixelFormat */
149 NULL
, /* pGetSystemPaletteEntries */
150 NULL
, /* pGetTextCharsetInfo */
151 NULL
, /* pGetTextExtentExPoint */
152 NULL
, /* pGetTextExtentExPointI */
153 NULL
, /* pGetTextFace */
154 NULL
, /* pGetTextMetrics */
155 NULL
, /* pGradientFill */
156 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
157 MFDRV_InvertRgn
, /* pInvertRgn */
158 MFDRV_LineTo
, /* pLineTo */
159 NULL
, /* pModifyWorldTransform */
160 MFDRV_MoveTo
, /* pMoveTo */
161 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
162 MFDRV_OffsetViewportOrgEx
, /* pOffsetViewportOrgEx */
163 MFDRV_OffsetWindowOrgEx
, /* pOffsetWindowOrgEx */
164 MFDRV_PaintRgn
, /* pPaintRgn */
165 MFDRV_PatBlt
, /* pPatBlt */
166 MFDRV_Pie
, /* pPie */
167 MFDRV_PolyBezier
, /* pPolyBezier */
168 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
169 NULL
, /* pPolyDraw */
170 MFDRV_PolyPolygon
, /* pPolyPolygon */
171 NULL
, /* pPolyPolyline */
172 MFDRV_Polygon
, /* pPolygon */
173 MFDRV_Polyline
, /* pPolyline */
174 NULL
, /* pPolylineTo */
175 NULL
, /* pPutImage */
176 NULL
, /* pRealizeDefaultPalette */
177 MFDRV_RealizePalette
, /* pRealizePalette */
178 MFDRV_Rectangle
, /* pRectangle */
180 MFDRV_RestoreDC
, /* pRestoreDC */
181 MFDRV_RoundRect
, /* pRoundRect */
182 MFDRV_SaveDC
, /* pSaveDC */
183 MFDRV_ScaleViewportExtEx
, /* pScaleViewportExtEx */
184 MFDRV_ScaleWindowExtEx
, /* pScaleWindowExtEx */
185 MFDRV_SelectBitmap
, /* pSelectBitmap */
186 MFDRV_SelectBrush
, /* pSelectBrush */
187 MFDRV_SelectClipPath
, /* pSelectClipPath */
188 MFDRV_SelectFont
, /* pSelectFont */
189 MFDRV_SelectPalette
, /* pSelectPalette */
190 MFDRV_SelectPen
, /* pSelectPen */
191 NULL
, /* pSetArcDirection */
192 MFDRV_SetBkColor
, /* pSetBkColor */
193 MFDRV_SetBkMode
, /* pSetBkMode */
194 MFDRV_SetDCBrushColor
, /* pSetDCBrushColor*/
195 MFDRV_SetDCPenColor
, /* pSetDCPenColor*/
196 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
197 NULL
, /* pSetDeviceClipping */
198 NULL
, /* pSetDeviceGammaRamp */
199 NULL
, /* pSetLayout */
200 MFDRV_SetMapMode
, /* pSetMapMode */
201 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
202 MFDRV_SetPixel
, /* pSetPixel */
203 NULL
, /* pSetPixelFormat */
204 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
205 MFDRV_SetROP2
, /* pSetROP2 */
206 MFDRV_SetRelAbs
, /* pSetRelAbs */
207 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
208 MFDRV_SetTextAlign
, /* pSetTextAlign */
209 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
210 MFDRV_SetTextColor
, /* pSetTextColor */
211 MFDRV_SetTextJustification
, /* pSetTextJustification */
212 MFDRV_SetViewportExtEx
, /* pSetViewportExtEx */
213 MFDRV_SetViewportOrgEx
, /* pSetViewportOrgEx */
214 MFDRV_SetWindowExtEx
, /* pSetWindowExtEx */
215 MFDRV_SetWindowOrgEx
, /* pSetWindowOrgEx */
216 NULL
, /* pSetWorldTransform */
217 NULL
, /* pStartDoc */
218 NULL
, /* pStartPage */
219 MFDRV_StretchBlt
, /* pStretchBlt */
220 MFDRV_StretchDIBits
, /* pStretchDIBits */
221 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
222 MFDRV_StrokePath
, /* pStrokePath */
223 NULL
, /* pSwapBuffers */
224 NULL
, /* pUnrealizePalette */
225 MFDRV_WidenPath
/* pWidenPath */
230 /**********************************************************************
231 * MFDRV_AllocMetaFile
233 static DC
*MFDRV_AllocMetaFile(void)
236 METAFILEDRV_PDEVICE
*physDev
;
238 if (!(dc
= alloc_dc_ptr( OBJ_METADC
))) return NULL
;
240 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
246 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
248 HeapFree( GetProcessHeap(), 0, physDev
);
253 push_dc_driver( &dc
->physDev
, &physDev
->dev
, &MFDRV_Funcs
);
255 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
256 physDev
->handles_size
= HANDLE_LIST_INC
;
257 physDev
->cur_handles
= 0;
261 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
262 physDev
->mh
->mtVersion
= 0x0300;
263 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
264 physDev
->mh
->mtNoObjects
= 0;
265 physDev
->mh
->mtMaxRecord
= 0;
266 physDev
->mh
->mtNoParameters
= 0;
268 SetVirtualResolution( physDev
->dev
.hdc
, 0, 0, 0, 0);
274 /**********************************************************************
275 * MFDRV_CreateCompatibleDC
277 static BOOL
MFDRV_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
279 /* not supported on metafile DCs */
284 /**********************************************************************
287 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
289 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
292 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
293 for(index
= 0; index
< physDev
->handles_size
; index
++)
294 if(physDev
->handles
[index
])
295 GDI_hdc_not_using_object(physDev
->handles
[index
], dev
->hdc
);
296 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
297 HeapFree( GetProcessHeap(), 0, physDev
);
302 /**********************************************************************
303 * CreateMetaFileW (GDI32.@)
305 * Create a new DC and associate it with a metafile. Pass a filename
306 * to create a disk-based metafile, NULL to create a memory metafile.
309 * filename [I] Filename of disk metafile
312 * A handle to the metafile DC if successful, NULL on failure.
314 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
318 METAFILEDRV_PDEVICE
*physDev
;
321 TRACE("%s\n", debugstr_w(filename
) );
323 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
324 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
326 if (filename
) /* disk based metafile */
328 physDev
->mh
->mtType
= METAFILE_DISK
;
329 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
330 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
334 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
339 physDev
->hFile
= hFile
;
341 /* Grow METAHEADER to include filename */
342 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
344 else /* memory based metafile */
345 physDev
->mh
->mtType
= METAFILE_MEMORY
;
347 TRACE("returning %p\n", physDev
->dev
.hdc
);
348 ret
= physDev
->dev
.hdc
;
349 release_dc_ptr( dc
);
353 /**********************************************************************
354 * CreateMetaFileA (GDI32.@)
356 * See CreateMetaFileW.
358 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
364 if (!filename
) return CreateMetaFileW(NULL
);
366 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
367 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
368 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
370 hReturnDC
= CreateMetaFileW(filenameW
);
372 HeapFree( GetProcessHeap(), 0, filenameW
);
378 /**********************************************************************
379 * MFDRV_CloseMetaFile
381 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
384 METAFILEDRV_PDEVICE
*physDev
;
386 TRACE("(%p)\n", hdc
);
388 if (!(dc
= get_dc_ptr( hdc
))) return NULL
;
389 if (dc
->header
.type
!= OBJ_METADC
)
391 release_dc_ptr( dc
);
394 if (dc
->refcount
!= 1)
396 FIXME( "not deleting busy DC %p refcount %u\n", hdc
, dc
->refcount
);
397 release_dc_ptr( dc
);
400 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
402 /* Construct the end of metafile record - this is documented
403 * in SDK Knowledgebase Q99334.
406 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
412 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
414 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
419 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
420 if (!WriteFile(physDev
->hFile
, physDev
->mh
, sizeof(*physDev
->mh
),
425 CloseHandle(physDev
->hFile
);
426 physDev
->mh
->mtType
= METAFILE_DISK
;
432 /******************************************************************
433 * CloseMetaFile (GDI32.@)
435 * Stop recording graphics operations in metafile associated with
436 * hdc and retrieve metafile.
439 * hdc [I] Metafile DC to close
442 * Handle of newly created metafile on success, NULL on failure.
444 HMETAFILE WINAPI
CloseMetaFile(HDC hdc
)
447 METAFILEDRV_PDEVICE
*physDev
;
448 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
450 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
452 /* Now allocate a global handle for the metafile */
454 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
456 physDev
->mh
= NULL
; /* So it won't be deleted */
462 /******************************************************************
465 * Warning: this function can change the pointer to the metafile header.
467 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
471 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
473 switch(physDev
->mh
->mtType
)
475 case METAFILE_MEMORY
:
476 len
= physDev
->mh
->mtSize
* 2 + rlen
;
477 /* reallocate memory if needed */
478 size
= HeapSize( GetProcessHeap(), 0, physDev
->mh
);
482 size
+= size
/ 2 + rlen
;
483 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, size
);
484 if (!mh
) return FALSE
;
486 TRACE("Reallocated metafile: new size is %d\n",size
);
488 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
491 TRACE("Writing record to disk\n");
492 if (!WriteFile(physDev
->hFile
, mr
, rlen
, NULL
, NULL
))
496 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
500 physDev
->mh
->mtSize
+= rlen
/ 2;
501 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
506 /******************************************************************
510 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
513 METARECORD
*mr
= (METARECORD
*)&buffer
;
516 mr
->rdFunction
= func
;
517 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
521 /******************************************************************
524 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
527 METARECORD
*mr
= (METARECORD
*)&buffer
;
528 WORD
*params
= mr
->rdParm
;
531 mr
->rdFunction
= func
;
533 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
537 /******************************************************************
540 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
543 METARECORD
*mr
= (METARECORD
*)&buffer
;
544 WORD
*params
= mr
->rdParm
;
547 mr
->rdFunction
= func
;
550 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
554 /******************************************************************
558 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
559 short param3
, short param4
)
562 METARECORD
*mr
= (METARECORD
*)&buffer
;
563 WORD
*params
= mr
->rdParm
;
566 mr
->rdFunction
= func
;
571 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
575 /******************************************************************
579 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
580 short param3
, short param4
, short param5
, short param6
)
583 METARECORD
*mr
= (METARECORD
*)&buffer
;
584 WORD
*params
= mr
->rdParm
;
587 mr
->rdFunction
= func
;
594 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
598 /******************************************************************
601 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
602 short param3
, short param4
, short param5
,
603 short param6
, short param7
, short param8
)
606 METARECORD
*mr
= (METARECORD
*)&buffer
;
607 WORD
*params
= mr
->rdParm
;
610 mr
->rdFunction
= func
;
619 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);