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 MFDRV_Chord
, /* pChord */
111 MFDRV_CloseFigure
, /* pCloseFigure */
112 MFDRV_CreateCompatibleDC
, /* pCreateCompatibleDC */
113 NULL
, /* pCreateDC */
114 MFDRV_DeleteDC
, /* pDeleteDC */
115 MFDRV_DeleteObject
, /* pDeleteObject */
116 NULL
, /* pDeviceCapabilities */
117 MFDRV_Ellipse
, /* pEllipse */
120 MFDRV_EndPath
, /* pEndPath */
121 NULL
, /* pEnumFonts */
122 NULL
, /* pEnumICMProfiles */
123 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
124 NULL
, /* pExtDeviceMode */
125 MFDRV_ExtEscape
, /* pExtEscape */
126 MFDRV_ExtFloodFill
, /* pExtFloodFill */
127 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
128 MFDRV_ExtTextOut
, /* pExtTextOut */
129 MFDRV_FillPath
, /* pFillPath */
130 MFDRV_FillRgn
, /* pFillRgn */
131 MFDRV_FlattenPath
, /* pFlattenPath */
132 NULL
, /* pFontIsLinked */
133 MFDRV_FrameRgn
, /* pFrameRgn */
134 NULL
, /* pGdiComment */
135 MFDRV_GetBoundsRect
, /* pGetBoundsRect */
136 NULL
, /* pGetCharABCWidths */
137 NULL
, /* pGetCharABCWidthsI */
138 NULL
, /* pGetCharWidth */
139 NULL
, /* pGetCharWidthInfo */
140 MFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
141 NULL
, /* pGetDeviceGammaRamp */
142 NULL
, /* pGetFontData */
143 NULL
, /* pGetFontRealizationInfo */
144 NULL
, /* pGetFontUnicodeRanges */
145 NULL
, /* pGetGlyphIndices */
146 NULL
, /* pGetGlyphOutline */
147 NULL
, /* pGetICMProfile */
148 NULL
, /* pGetImage */
149 NULL
, /* pGetKerningPairs */
150 NULL
, /* pGetNearestColor */
151 NULL
, /* pGetOutlineTextMetrics */
152 NULL
, /* pGetPixel */
153 NULL
, /* pGetSystemPaletteEntries */
154 NULL
, /* pGetTextCharsetInfo */
155 NULL
, /* pGetTextExtentExPoint */
156 NULL
, /* pGetTextExtentExPointI */
157 NULL
, /* pGetTextFace */
158 NULL
, /* pGetTextMetrics */
159 NULL
, /* pGradientFill */
160 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
161 MFDRV_InvertRgn
, /* pInvertRgn */
162 MFDRV_LineTo
, /* pLineTo */
163 NULL
, /* pModifyWorldTransform */
164 MFDRV_MoveTo
, /* pMoveTo */
165 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
166 MFDRV_OffsetViewportOrgEx
, /* pOffsetViewportOrgEx */
167 MFDRV_OffsetWindowOrgEx
, /* pOffsetWindowOrgEx */
168 MFDRV_PaintRgn
, /* pPaintRgn */
169 MFDRV_PatBlt
, /* pPatBlt */
170 MFDRV_Pie
, /* pPie */
171 MFDRV_PolyBezier
, /* pPolyBezier */
172 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
173 NULL
, /* pPolyDraw */
174 MFDRV_PolyPolygon
, /* pPolyPolygon */
175 NULL
, /* pPolyPolyline */
176 MFDRV_Polygon
, /* pPolygon */
177 MFDRV_Polyline
, /* pPolyline */
178 NULL
, /* pPolylineTo */
179 NULL
, /* pPutImage */
180 NULL
, /* pRealizeDefaultPalette */
181 MFDRV_RealizePalette
, /* pRealizePalette */
182 MFDRV_Rectangle
, /* pRectangle */
184 MFDRV_RestoreDC
, /* pRestoreDC */
185 MFDRV_RoundRect
, /* pRoundRect */
186 MFDRV_SaveDC
, /* pSaveDC */
187 MFDRV_ScaleViewportExtEx
, /* pScaleViewportExtEx */
188 MFDRV_ScaleWindowExtEx
, /* pScaleWindowExtEx */
189 MFDRV_SelectBitmap
, /* pSelectBitmap */
190 MFDRV_SelectBrush
, /* pSelectBrush */
191 MFDRV_SelectClipPath
, /* pSelectClipPath */
192 MFDRV_SelectFont
, /* pSelectFont */
193 MFDRV_SelectPalette
, /* pSelectPalette */
194 MFDRV_SelectPen
, /* pSelectPen */
195 NULL
, /* pSetArcDirection */
196 MFDRV_SetBkColor
, /* pSetBkColor */
197 MFDRV_SetBkMode
, /* pSetBkMode */
198 MFDRV_SetBoundsRect
, /* pSetBoundsRect */
199 MFDRV_SetDCBrushColor
, /* pSetDCBrushColor*/
200 MFDRV_SetDCPenColor
, /* pSetDCPenColor*/
201 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
202 NULL
, /* pSetDeviceClipping */
203 NULL
, /* pSetDeviceGammaRamp */
204 NULL
, /* pSetLayout */
205 MFDRV_SetMapMode
, /* pSetMapMode */
206 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
207 MFDRV_SetPixel
, /* pSetPixel */
208 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
209 MFDRV_SetROP2
, /* pSetROP2 */
210 MFDRV_SetRelAbs
, /* pSetRelAbs */
211 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
212 MFDRV_SetTextAlign
, /* pSetTextAlign */
213 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
214 MFDRV_SetTextColor
, /* pSetTextColor */
215 MFDRV_SetTextJustification
, /* pSetTextJustification */
216 MFDRV_SetViewportExtEx
, /* pSetViewportExtEx */
217 MFDRV_SetViewportOrgEx
, /* pSetViewportOrgEx */
218 MFDRV_SetWindowExtEx
, /* pSetWindowExtEx */
219 MFDRV_SetWindowOrgEx
, /* pSetWindowOrgEx */
220 NULL
, /* pSetWorldTransform */
221 NULL
, /* pStartDoc */
222 NULL
, /* pStartPage */
223 MFDRV_StretchBlt
, /* pStretchBlt */
224 MFDRV_StretchDIBits
, /* pStretchDIBits */
225 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
226 MFDRV_StrokePath
, /* pStrokePath */
227 NULL
, /* pUnrealizePalette */
228 MFDRV_WidenPath
, /* pWidenPath */
229 NULL
, /* wine_get_wgl_driver */
230 GDI_PRIORITY_GRAPHICS_DRV
/* priority */
235 /**********************************************************************
236 * MFDRV_AllocMetaFile
238 static DC
*MFDRV_AllocMetaFile(void)
241 METAFILEDRV_PDEVICE
*physDev
;
243 if (!(dc
= alloc_dc_ptr( OBJ_METADC
))) return NULL
;
245 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
251 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
253 HeapFree( GetProcessHeap(), 0, physDev
);
258 push_dc_driver( &dc
->physDev
, &physDev
->dev
, &MFDRV_Funcs
);
260 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
261 physDev
->handles_size
= HANDLE_LIST_INC
;
262 physDev
->cur_handles
= 0;
266 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
267 physDev
->mh
->mtVersion
= 0x0300;
268 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
269 physDev
->mh
->mtNoObjects
= 0;
270 physDev
->mh
->mtMaxRecord
= 0;
271 physDev
->mh
->mtNoParameters
= 0;
273 SetVirtualResolution( physDev
->dev
.hdc
, 0, 0, 0, 0);
279 /**********************************************************************
280 * MFDRV_CreateCompatibleDC
282 static BOOL
MFDRV_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
284 /* not supported on metafile DCs */
289 /**********************************************************************
292 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
294 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
297 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
298 for(index
= 0; index
< physDev
->handles_size
; index
++)
299 if(physDev
->handles
[index
])
300 GDI_hdc_not_using_object(physDev
->handles
[index
], dev
->hdc
);
301 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
302 HeapFree( GetProcessHeap(), 0, physDev
);
307 /**********************************************************************
308 * CreateMetaFileW (GDI32.@)
310 * Create a new DC and associate it with a metafile. Pass a filename
311 * to create a disk-based metafile, NULL to create a memory metafile.
314 * filename [I] Filename of disk metafile
317 * A handle to the metafile DC if successful, NULL on failure.
319 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
323 METAFILEDRV_PDEVICE
*physDev
;
327 TRACE("%s\n", debugstr_w(filename
) );
329 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
330 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
332 if (filename
) /* disk based metafile */
334 physDev
->mh
->mtType
= METAFILE_DISK
;
335 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
336 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
340 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
),
341 &bytes_written
, NULL
)) {
343 CloseHandle (hFile
);
346 physDev
->hFile
= hFile
;
348 /* Grow METAHEADER to include filename */
349 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
351 else /* memory based metafile */
352 physDev
->mh
->mtType
= METAFILE_MEMORY
;
354 TRACE("returning %p\n", physDev
->dev
.hdc
);
355 ret
= physDev
->dev
.hdc
;
356 release_dc_ptr( dc
);
360 /**********************************************************************
361 * CreateMetaFileA (GDI32.@)
363 * See CreateMetaFileW.
365 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
371 if (!filename
) return CreateMetaFileW(NULL
);
373 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
374 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
375 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
377 hReturnDC
= CreateMetaFileW(filenameW
);
379 HeapFree( GetProcessHeap(), 0, filenameW
);
385 /**********************************************************************
386 * MFDRV_CloseMetaFile
388 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
391 METAFILEDRV_PDEVICE
*physDev
;
394 TRACE("(%p)\n", hdc
);
396 if (!(dc
= get_dc_ptr( hdc
))) return NULL
;
397 if (GetObjectType( hdc
) != OBJ_METADC
)
399 release_dc_ptr( dc
);
402 if (dc
->refcount
!= 1)
404 FIXME( "not deleting busy DC %p refcount %u\n", hdc
, dc
->refcount
);
405 release_dc_ptr( dc
);
408 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
410 /* Construct the end of metafile record - this is documented
411 * in SDK Knowledgebase Q99334.
414 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
420 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
422 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
427 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
428 if (!WriteFile(physDev
->hFile
, physDev
->mh
, sizeof(*physDev
->mh
),
429 &bytes_written
, NULL
)) {
433 CloseHandle(physDev
->hFile
);
434 physDev
->mh
->mtType
= METAFILE_DISK
;
440 /******************************************************************
441 * CloseMetaFile (GDI32.@)
443 * Stop recording graphics operations in metafile associated with
444 * hdc and retrieve metafile.
447 * hdc [I] Metafile DC to close
450 * Handle of newly created metafile on success, NULL on failure.
452 HMETAFILE WINAPI
CloseMetaFile(HDC hdc
)
455 METAFILEDRV_PDEVICE
*physDev
;
456 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
458 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
460 /* Now allocate a global handle for the metafile */
462 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
464 physDev
->mh
= NULL
; /* So it won't be deleted */
470 /******************************************************************
473 * Warning: this function can change the pointer to the metafile header.
475 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
479 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
481 switch(physDev
->mh
->mtType
)
483 case METAFILE_MEMORY
:
484 len
= physDev
->mh
->mtSize
* 2 + rlen
;
485 /* reallocate memory if needed */
486 size
= HeapSize( GetProcessHeap(), 0, physDev
->mh
);
490 size
+= size
/ 2 + rlen
;
491 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, size
);
492 if (!mh
) return FALSE
;
494 TRACE("Reallocated metafile: new size is %d\n",size
);
496 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
499 TRACE("Writing record to disk\n");
500 if (!WriteFile(physDev
->hFile
, mr
, rlen
, NULL
, NULL
))
504 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
508 physDev
->mh
->mtSize
+= rlen
/ 2;
509 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
514 /******************************************************************
518 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
521 METARECORD
*mr
= (METARECORD
*)&buffer
;
524 mr
->rdFunction
= func
;
525 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
529 /******************************************************************
532 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
535 METARECORD
*mr
= (METARECORD
*)&buffer
;
536 WORD
*params
= mr
->rdParm
;
539 mr
->rdFunction
= func
;
541 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
545 /******************************************************************
548 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
551 METARECORD
*mr
= (METARECORD
*)&buffer
;
552 WORD
*params
= mr
->rdParm
;
555 mr
->rdFunction
= func
;
558 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
562 /******************************************************************
566 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
567 short param3
, short param4
)
570 METARECORD
*mr
= (METARECORD
*)&buffer
;
571 WORD
*params
= mr
->rdParm
;
574 mr
->rdFunction
= func
;
579 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
583 /******************************************************************
587 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
588 short param3
, short param4
, short param5
, short param6
)
591 METARECORD
*mr
= (METARECORD
*)&buffer
;
592 WORD
*params
= mr
->rdParm
;
595 mr
->rdFunction
= func
;
602 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
606 /******************************************************************
609 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
610 short param3
, short param4
, short param5
,
611 short param6
, short param7
, short param8
)
614 METARECORD
*mr
= (METARECORD
*)&buffer
;
615 WORD
*params
= mr
->rdParm
;
618 mr
->rdFunction
= func
;
627 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);