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 static UINT
MFDRV_SetBoundsRect( PHYSDEV dev
, RECT
*rect
, UINT flags
)
80 /******************************************************************
83 *A very simple implementation that returns DT_METAFILE
85 static INT
MFDRV_GetDeviceCaps(PHYSDEV dev
, INT cap
)
94 TRACE(" unsupported capability %d, will return 0\n", cap
);
100 static const struct gdi_dc_funcs MFDRV_Funcs
=
102 NULL
, /* pAbortDoc */
103 MFDRV_AbortPath
, /* pAbortPath */
104 NULL
, /* pAlphaBlend */
105 NULL
, /* pAngleArc */
106 MFDRV_Arc
, /* pArc */
108 MFDRV_BeginPath
, /* pBeginPath */
109 NULL
, /* pBlendImage */
110 NULL
, /* pChoosePixelFormat */
111 MFDRV_Chord
, /* pChord */
112 MFDRV_CloseFigure
, /* pCloseFigure */
113 NULL
, /* pCopyBitmap */
114 NULL
, /* pCreateBitmap */
115 MFDRV_CreateCompatibleDC
, /* pCreateCompatibleDC */
116 NULL
, /* pCreateDC */
117 NULL
, /* pDeleteBitmap */
118 MFDRV_DeleteDC
, /* pDeleteDC */
119 MFDRV_DeleteObject
, /* pDeleteObject */
120 NULL
, /* pDescribePixelFormat */
121 NULL
, /* pDeviceCapabilities */
122 MFDRV_Ellipse
, /* pEllipse */
125 MFDRV_EndPath
, /* pEndPath */
126 NULL
, /* pEnumFonts */
127 NULL
, /* pEnumICMProfiles */
128 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
129 NULL
, /* pExtDeviceMode */
130 MFDRV_ExtEscape
, /* pExtEscape */
131 MFDRV_ExtFloodFill
, /* pExtFloodFill */
132 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
133 MFDRV_ExtTextOut
, /* pExtTextOut */
134 MFDRV_FillPath
, /* pFillPath */
135 MFDRV_FillRgn
, /* pFillRgn */
136 MFDRV_FlattenPath
, /* pFlattenPath */
137 NULL
, /* pFontIsLinked */
138 MFDRV_FrameRgn
, /* pFrameRgn */
139 NULL
, /* pGdiComment */
140 NULL
, /* pGdiRealizationInfo */
141 MFDRV_GetBoundsRect
, /* pGetBoundsRect */
142 NULL
, /* pGetCharABCWidths */
143 NULL
, /* pGetCharABCWidthsI */
144 NULL
, /* pGetCharWidth */
145 MFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
146 NULL
, /* pGetDeviceGammaRamp */
147 NULL
, /* pGetFontData */
148 NULL
, /* pGetFontUnicodeRanges */
149 NULL
, /* pGetGlyphIndices */
150 NULL
, /* pGetGlyphOutline */
151 NULL
, /* pGetICMProfile */
152 NULL
, /* pGetImage */
153 NULL
, /* pGetKerningPairs */
154 NULL
, /* pGetNearestColor */
155 NULL
, /* pGetOutlineTextMetrics */
156 NULL
, /* pGetPixel */
157 NULL
, /* pGetPixelFormat */
158 NULL
, /* pGetSystemPaletteEntries */
159 NULL
, /* pGetTextCharsetInfo */
160 NULL
, /* pGetTextExtentExPoint */
161 NULL
, /* pGetTextExtentExPointI */
162 NULL
, /* pGetTextFace */
163 NULL
, /* pGetTextMetrics */
164 NULL
, /* pGradientFill */
165 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
166 MFDRV_InvertRgn
, /* pInvertRgn */
167 MFDRV_LineTo
, /* pLineTo */
168 NULL
, /* pModifyWorldTransform */
169 MFDRV_MoveTo
, /* pMoveTo */
170 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
171 MFDRV_OffsetViewportOrgEx
, /* pOffsetViewportOrgEx */
172 MFDRV_OffsetWindowOrgEx
, /* pOffsetWindowOrgEx */
173 MFDRV_PaintRgn
, /* pPaintRgn */
174 MFDRV_PatBlt
, /* pPatBlt */
175 MFDRV_Pie
, /* pPie */
176 MFDRV_PolyBezier
, /* pPolyBezier */
177 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
178 NULL
, /* pPolyDraw */
179 MFDRV_PolyPolygon
, /* pPolyPolygon */
180 NULL
, /* pPolyPolyline */
181 MFDRV_Polygon
, /* pPolygon */
182 MFDRV_Polyline
, /* pPolyline */
183 NULL
, /* pPolylineTo */
184 NULL
, /* pPutImage */
185 NULL
, /* pRealizeDefaultPalette */
186 MFDRV_RealizePalette
, /* pRealizePalette */
187 MFDRV_Rectangle
, /* pRectangle */
189 MFDRV_RestoreDC
, /* pRestoreDC */
190 MFDRV_RoundRect
, /* pRoundRect */
191 MFDRV_SaveDC
, /* pSaveDC */
192 MFDRV_ScaleViewportExtEx
, /* pScaleViewportExtEx */
193 MFDRV_ScaleWindowExtEx
, /* pScaleWindowExtEx */
194 MFDRV_SelectBitmap
, /* pSelectBitmap */
195 MFDRV_SelectBrush
, /* pSelectBrush */
196 MFDRV_SelectClipPath
, /* pSelectClipPath */
197 MFDRV_SelectFont
, /* pSelectFont */
198 MFDRV_SelectPalette
, /* pSelectPalette */
199 MFDRV_SelectPen
, /* pSelectPen */
200 NULL
, /* pSetArcDirection */
201 MFDRV_SetBkColor
, /* pSetBkColor */
202 MFDRV_SetBkMode
, /* pSetBkMode */
203 MFDRV_SetBoundsRect
, /* pSetBoundsRect */
204 MFDRV_SetDCBrushColor
, /* pSetDCBrushColor*/
205 MFDRV_SetDCPenColor
, /* pSetDCPenColor*/
206 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
207 NULL
, /* pSetDeviceClipping */
208 NULL
, /* pSetDeviceGammaRamp */
209 NULL
, /* pSetLayout */
210 MFDRV_SetMapMode
, /* pSetMapMode */
211 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
212 MFDRV_SetPixel
, /* pSetPixel */
213 NULL
, /* pSetPixelFormat */
214 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
215 MFDRV_SetROP2
, /* pSetROP2 */
216 MFDRV_SetRelAbs
, /* pSetRelAbs */
217 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
218 MFDRV_SetTextAlign
, /* pSetTextAlign */
219 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
220 MFDRV_SetTextColor
, /* pSetTextColor */
221 MFDRV_SetTextJustification
, /* pSetTextJustification */
222 MFDRV_SetViewportExtEx
, /* pSetViewportExtEx */
223 MFDRV_SetViewportOrgEx
, /* pSetViewportOrgEx */
224 MFDRV_SetWindowExtEx
, /* pSetWindowExtEx */
225 MFDRV_SetWindowOrgEx
, /* pSetWindowOrgEx */
226 NULL
, /* pSetWorldTransform */
227 NULL
, /* pStartDoc */
228 NULL
, /* pStartPage */
229 MFDRV_StretchBlt
, /* pStretchBlt */
230 MFDRV_StretchDIBits
, /* pStretchDIBits */
231 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
232 MFDRV_StrokePath
, /* pStrokePath */
233 NULL
, /* pSwapBuffers */
234 NULL
, /* pUnrealizePalette */
235 MFDRV_WidenPath
/* pWidenPath */
240 /**********************************************************************
241 * MFDRV_AllocMetaFile
243 static DC
*MFDRV_AllocMetaFile(void)
246 METAFILEDRV_PDEVICE
*physDev
;
248 if (!(dc
= alloc_dc_ptr( OBJ_METADC
))) return NULL
;
250 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
256 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
258 HeapFree( GetProcessHeap(), 0, physDev
);
263 push_dc_driver( &dc
->physDev
, &physDev
->dev
, &MFDRV_Funcs
);
265 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
266 physDev
->handles_size
= HANDLE_LIST_INC
;
267 physDev
->cur_handles
= 0;
271 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
272 physDev
->mh
->mtVersion
= 0x0300;
273 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
274 physDev
->mh
->mtNoObjects
= 0;
275 physDev
->mh
->mtMaxRecord
= 0;
276 physDev
->mh
->mtNoParameters
= 0;
278 SetVirtualResolution( physDev
->dev
.hdc
, 0, 0, 0, 0);
284 /**********************************************************************
285 * MFDRV_CreateCompatibleDC
287 static BOOL
MFDRV_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
289 /* not supported on metafile DCs */
294 /**********************************************************************
297 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
299 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
302 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
303 for(index
= 0; index
< physDev
->handles_size
; index
++)
304 if(physDev
->handles
[index
])
305 GDI_hdc_not_using_object(physDev
->handles
[index
], dev
->hdc
);
306 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
307 HeapFree( GetProcessHeap(), 0, physDev
);
312 /**********************************************************************
313 * CreateMetaFileW (GDI32.@)
315 * Create a new DC and associate it with a metafile. Pass a filename
316 * to create a disk-based metafile, NULL to create a memory metafile.
319 * filename [I] Filename of disk metafile
322 * A handle to the metafile DC if successful, NULL on failure.
324 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
328 METAFILEDRV_PDEVICE
*physDev
;
331 TRACE("%s\n", debugstr_w(filename
) );
333 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
334 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
336 if (filename
) /* disk based metafile */
338 physDev
->mh
->mtType
= METAFILE_DISK
;
339 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
340 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
344 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
349 physDev
->hFile
= hFile
;
351 /* Grow METAHEADER to include filename */
352 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
354 else /* memory based metafile */
355 physDev
->mh
->mtType
= METAFILE_MEMORY
;
357 TRACE("returning %p\n", physDev
->dev
.hdc
);
358 ret
= physDev
->dev
.hdc
;
359 release_dc_ptr( dc
);
363 /**********************************************************************
364 * CreateMetaFileA (GDI32.@)
366 * See CreateMetaFileW.
368 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
374 if (!filename
) return CreateMetaFileW(NULL
);
376 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
377 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
378 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
380 hReturnDC
= CreateMetaFileW(filenameW
);
382 HeapFree( GetProcessHeap(), 0, filenameW
);
388 /**********************************************************************
389 * MFDRV_CloseMetaFile
391 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
394 METAFILEDRV_PDEVICE
*physDev
;
396 TRACE("(%p)\n", hdc
);
398 if (!(dc
= get_dc_ptr( hdc
))) return NULL
;
399 if (dc
->header
.type
!= OBJ_METADC
)
401 release_dc_ptr( dc
);
404 if (dc
->refcount
!= 1)
406 FIXME( "not deleting busy DC %p refcount %u\n", hdc
, dc
->refcount
);
407 release_dc_ptr( dc
);
410 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
412 /* Construct the end of metafile record - this is documented
413 * in SDK Knowledgebase Q99334.
416 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
422 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
424 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
429 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
430 if (!WriteFile(physDev
->hFile
, physDev
->mh
, sizeof(*physDev
->mh
),
435 CloseHandle(physDev
->hFile
);
436 physDev
->mh
->mtType
= METAFILE_DISK
;
442 /******************************************************************
443 * CloseMetaFile (GDI32.@)
445 * Stop recording graphics operations in metafile associated with
446 * hdc and retrieve metafile.
449 * hdc [I] Metafile DC to close
452 * Handle of newly created metafile on success, NULL on failure.
454 HMETAFILE WINAPI
CloseMetaFile(HDC hdc
)
457 METAFILEDRV_PDEVICE
*physDev
;
458 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
460 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
462 /* Now allocate a global handle for the metafile */
464 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
466 physDev
->mh
= NULL
; /* So it won't be deleted */
472 /******************************************************************
475 * Warning: this function can change the pointer to the metafile header.
477 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
481 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
483 switch(physDev
->mh
->mtType
)
485 case METAFILE_MEMORY
:
486 len
= physDev
->mh
->mtSize
* 2 + rlen
;
487 /* reallocate memory if needed */
488 size
= HeapSize( GetProcessHeap(), 0, physDev
->mh
);
492 size
+= size
/ 2 + rlen
;
493 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, size
);
494 if (!mh
) return FALSE
;
496 TRACE("Reallocated metafile: new size is %d\n",size
);
498 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
501 TRACE("Writing record to disk\n");
502 if (!WriteFile(physDev
->hFile
, mr
, rlen
, NULL
, NULL
))
506 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
510 physDev
->mh
->mtSize
+= rlen
/ 2;
511 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
516 /******************************************************************
520 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
523 METARECORD
*mr
= (METARECORD
*)&buffer
;
526 mr
->rdFunction
= func
;
527 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
531 /******************************************************************
534 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
537 METARECORD
*mr
= (METARECORD
*)&buffer
;
538 WORD
*params
= mr
->rdParm
;
541 mr
->rdFunction
= func
;
543 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
547 /******************************************************************
550 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
553 METARECORD
*mr
= (METARECORD
*)&buffer
;
554 WORD
*params
= mr
->rdParm
;
557 mr
->rdFunction
= func
;
560 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
564 /******************************************************************
568 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
569 short param3
, short param4
)
572 METARECORD
*mr
= (METARECORD
*)&buffer
;
573 WORD
*params
= mr
->rdParm
;
576 mr
->rdFunction
= func
;
581 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
585 /******************************************************************
589 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
590 short param3
, short param4
, short param5
, short param6
)
593 METARECORD
*mr
= (METARECORD
*)&buffer
;
594 WORD
*params
= mr
->rdParm
;
597 mr
->rdFunction
= func
;
604 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
608 /******************************************************************
611 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
612 short param3
, short param4
, short param5
,
613 short param6
, short param7
, short param8
)
616 METARECORD
*mr
= (METARECORD
*)&buffer
;
617 WORD
*params
= mr
->rdParm
;
620 mr
->rdFunction
= func
;
629 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);