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_DeleteDC( PHYSDEV dev
);
36 /**********************************************************************
39 static INT
MFDRV_ExtEscape( PHYSDEV dev
, INT nEscape
, INT cbInput
, LPCVOID in_data
,
40 INT cbOutput
, LPVOID out_data
)
46 if (cbOutput
) return 0; /* escapes that require output cannot work in metafiles */
48 len
= sizeof(*mr
) + sizeof(WORD
) + ((cbInput
+ 1) & ~1);
49 mr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
51 mr
->rdFunction
= META_ESCAPE
;
52 mr
->rdParm
[0] = nEscape
;
53 mr
->rdParm
[1] = cbInput
;
54 memcpy(&(mr
->rdParm
[2]), in_data
, cbInput
);
55 ret
= MFDRV_WriteRecord( dev
, mr
, len
);
56 HeapFree(GetProcessHeap(), 0, mr
);
61 /******************************************************************
64 *A very simple implementation that returns DT_METAFILE
66 static INT
MFDRV_GetDeviceCaps(PHYSDEV dev
, INT cap
)
75 TRACE(" unsupported capability %d, will return 0\n", cap
);
81 static const DC_FUNCTIONS MFDRV_Funcs
=
84 MFDRV_AbortPath
, /* pAbortPath */
85 NULL
, /* pAlphaBlend */
89 MFDRV_BeginPath
, /* pBeginPath */
90 NULL
, /* pChoosePixelFormat */
91 MFDRV_Chord
, /* pChord */
92 MFDRV_CloseFigure
, /* pCloseFigure */
93 NULL
, /* pCreateBitmap */
95 NULL
, /* pCreateDIBSection */
96 NULL
, /* pDeleteBitmap */
97 MFDRV_DeleteDC
, /* pDeleteDC */
98 MFDRV_DeleteObject
, /* pDeleteObject */
99 NULL
, /* pDescribePixelFormat */
100 NULL
, /* pDeviceCapabilities */
101 MFDRV_Ellipse
, /* pEllipse */
104 MFDRV_EndPath
, /* pEndPath */
105 NULL
, /* pEnumDeviceFonts */
106 NULL
, /* pEnumICMProfiles */
107 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
108 NULL
, /* pExtDeviceMode */
109 MFDRV_ExtEscape
, /* pExtEscape */
110 MFDRV_ExtFloodFill
, /* pExtFloodFill */
111 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
112 MFDRV_ExtTextOut
, /* pExtTextOut */
113 MFDRV_FillPath
, /* pFillPath */
114 MFDRV_FillRgn
, /* pFillRgn */
115 MFDRV_FlattenPath
, /* pFlattenPath */
116 MFDRV_FrameRgn
, /* pFrameRgn */
117 NULL
, /* pGdiComment */
118 NULL
, /* pGetBitmapBits */
119 NULL
, /* pGetCharWidth */
120 NULL
, /* pGetDIBits */
121 MFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
122 NULL
, /* pGetDeviceGammaRamp */
123 NULL
, /* pGetICMProfile */
124 NULL
, /* pGetImage */
125 NULL
, /* pGetNearestColor */
126 NULL
, /* pGetPixel */
127 NULL
, /* pGetPixelFormat */
128 NULL
, /* pGetSystemPaletteEntries */
129 NULL
, /* pGetTextExtentExPoint */
130 NULL
, /* pGetTextMetrics */
131 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
132 MFDRV_InvertRgn
, /* pInvertRgn */
133 MFDRV_LineTo
, /* pLineTo */
134 NULL
, /* pModifyWorldTransform */
135 MFDRV_MoveTo
, /* pMoveTo */
136 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
137 MFDRV_OffsetViewportOrgEx
, /* pOffsetViewportOrgEx */
138 MFDRV_OffsetWindowOrgEx
, /* pOffsetWindowOrgEx */
139 MFDRV_PaintRgn
, /* pPaintRgn */
140 MFDRV_PatBlt
, /* pPatBlt */
141 MFDRV_Pie
, /* pPie */
142 MFDRV_PolyBezier
, /* pPolyBezier */
143 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
144 NULL
, /* pPolyDraw */
145 MFDRV_PolyPolygon
, /* pPolyPolygon */
146 NULL
, /* pPolyPolyline */
147 MFDRV_Polygon
, /* pPolygon */
148 MFDRV_Polyline
, /* pPolyline */
149 NULL
, /* pPolylineTo */
150 NULL
, /* pPutImage */
151 NULL
, /* pRealizeDefaultPalette */
152 MFDRV_RealizePalette
, /* pRealizePalette */
153 MFDRV_Rectangle
, /* pRectangle */
155 MFDRV_RestoreDC
, /* pRestoreDC */
156 MFDRV_RoundRect
, /* pRoundRect */
157 MFDRV_SaveDC
, /* pSaveDC */
158 MFDRV_ScaleViewportExtEx
, /* pScaleViewportExtEx */
159 MFDRV_ScaleWindowExtEx
, /* pScaleWindowExtEx */
160 MFDRV_SelectBitmap
, /* pSelectBitmap */
161 MFDRV_SelectBrush
, /* pSelectBrush */
162 MFDRV_SelectClipPath
, /* pSelectClipPath */
163 MFDRV_SelectFont
, /* pSelectFont */
164 MFDRV_SelectPalette
, /* pSelectPalette */
165 MFDRV_SelectPen
, /* pSelectPen */
166 NULL
, /* pSetArcDirection */
167 NULL
, /* pSetBitmapBits */
168 MFDRV_SetBkColor
, /* pSetBkColor */
169 MFDRV_SetBkMode
, /* pSetBkMode */
170 MFDRV_SetDCBrushColor
, /* pSetDCBrushColor*/
171 MFDRV_SetDCPenColor
, /* pSetDCPenColor*/
172 NULL
, /* pSetDIBColorTable */
173 NULL
, /* pSetDIBits */
174 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
175 NULL
, /* pSetDeviceClipping */
176 NULL
, /* pSetDeviceGammaRamp */
177 NULL
, /* pSetLayout */
178 MFDRV_SetMapMode
, /* pSetMapMode */
179 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
180 MFDRV_SetPixel
, /* pSetPixel */
181 NULL
, /* pSetPixelFormat */
182 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
183 MFDRV_SetROP2
, /* pSetROP2 */
184 MFDRV_SetRelAbs
, /* pSetRelAbs */
185 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
186 MFDRV_SetTextAlign
, /* pSetTextAlign */
187 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
188 MFDRV_SetTextColor
, /* pSetTextColor */
189 MFDRV_SetTextJustification
, /* pSetTextJustification */
190 MFDRV_SetViewportExtEx
, /* pSetViewportExtEx */
191 MFDRV_SetViewportOrgEx
, /* pSetViewportOrgEx */
192 MFDRV_SetWindowExtEx
, /* pSetWindowExtEx */
193 MFDRV_SetWindowOrgEx
, /* pSetWindowOrgEx */
194 NULL
, /* pSetWorldTransform */
195 NULL
, /* pStartDoc */
196 NULL
, /* pStartPage */
197 MFDRV_StretchBlt
, /* pStretchBlt */
198 MFDRV_StretchDIBits
, /* pStretchDIBits */
199 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
200 MFDRV_StrokePath
, /* pStrokePath */
201 NULL
, /* pSwapBuffers */
202 NULL
, /* pUnrealizePalette */
203 MFDRV_WidenPath
/* pWidenPath */
208 /**********************************************************************
209 * MFDRV_AllocMetaFile
211 static DC
*MFDRV_AllocMetaFile(void)
214 METAFILEDRV_PDEVICE
*physDev
;
216 if (!(dc
= alloc_dc_ptr( OBJ_METADC
))) return NULL
;
218 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
224 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
226 HeapFree( GetProcessHeap(), 0, physDev
);
231 push_dc_driver( dc
, &physDev
->dev
, &MFDRV_Funcs
);
233 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
234 physDev
->handles_size
= HANDLE_LIST_INC
;
235 physDev
->cur_handles
= 0;
239 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
240 physDev
->mh
->mtVersion
= 0x0300;
241 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
242 physDev
->mh
->mtNoObjects
= 0;
243 physDev
->mh
->mtMaxRecord
= 0;
244 physDev
->mh
->mtNoParameters
= 0;
246 SetVirtualResolution( physDev
->dev
.hdc
, 0, 0, 0, 0);
252 /**********************************************************************
255 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
257 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
260 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
261 for(index
= 0; index
< physDev
->handles_size
; index
++)
262 if(physDev
->handles
[index
])
263 GDI_hdc_not_using_object(physDev
->handles
[index
], dev
->hdc
);
264 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
265 HeapFree( GetProcessHeap(), 0, physDev
);
270 /**********************************************************************
271 * CreateMetaFileW (GDI32.@)
273 * Create a new DC and associate it with a metafile. Pass a filename
274 * to create a disk-based metafile, NULL to create a memory metafile.
277 * filename [I] Filename of disk metafile
280 * A handle to the metafile DC if successful, NULL on failure.
282 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
286 METAFILEDRV_PDEVICE
*physDev
;
289 TRACE("%s\n", debugstr_w(filename
) );
291 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
292 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
294 if (filename
) /* disk based metafile */
296 physDev
->mh
->mtType
= METAFILE_DISK
;
297 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
298 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
302 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
307 physDev
->hFile
= hFile
;
309 /* Grow METAHEADER to include filename */
310 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
312 else /* memory based metafile */
313 physDev
->mh
->mtType
= METAFILE_MEMORY
;
315 TRACE("returning %p\n", physDev
->dev
.hdc
);
316 ret
= physDev
->dev
.hdc
;
317 release_dc_ptr( dc
);
321 /**********************************************************************
322 * CreateMetaFileA (GDI32.@)
324 * See CreateMetaFileW.
326 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
332 if (!filename
) return CreateMetaFileW(NULL
);
334 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
335 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
336 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
338 hReturnDC
= CreateMetaFileW(filenameW
);
340 HeapFree( GetProcessHeap(), 0, filenameW
);
346 /**********************************************************************
347 * MFDRV_CloseMetaFile
349 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
352 METAFILEDRV_PDEVICE
*physDev
;
354 TRACE("(%p)\n", hdc
);
356 if (!(dc
= get_dc_ptr( hdc
))) return NULL
;
357 if (dc
->header
.type
!= OBJ_METADC
)
359 release_dc_ptr( dc
);
362 if (dc
->refcount
!= 1)
364 FIXME( "not deleting busy DC %p refcount %u\n", hdc
, dc
->refcount
);
365 release_dc_ptr( dc
);
368 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
370 /* Construct the end of metafile record - this is documented
371 * in SDK Knowledgebase Q99334.
374 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
380 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
382 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
387 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
388 if (!WriteFile(physDev
->hFile
, physDev
->mh
, sizeof(*physDev
->mh
),
393 CloseHandle(physDev
->hFile
);
394 physDev
->mh
->mtType
= METAFILE_DISK
;
400 /******************************************************************
401 * CloseMetaFile (GDI32.@)
403 * Stop recording graphics operations in metafile associated with
404 * hdc and retrieve metafile.
407 * hdc [I] Metafile DC to close
410 * Handle of newly created metafile on success, NULL on failure.
412 HMETAFILE WINAPI
CloseMetaFile(HDC hdc
)
415 METAFILEDRV_PDEVICE
*physDev
;
416 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
418 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
420 /* Now allocate a global handle for the metafile */
422 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
424 physDev
->mh
= NULL
; /* So it won't be deleted */
430 /******************************************************************
433 * Warning: this function can change the pointer to the metafile header.
435 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
439 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
441 switch(physDev
->mh
->mtType
)
443 case METAFILE_MEMORY
:
444 len
= physDev
->mh
->mtSize
* 2 + rlen
;
445 /* reallocate memory if needed */
446 size
= HeapSize( GetProcessHeap(), 0, physDev
->mh
);
450 size
+= size
/ 2 + rlen
;
451 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, size
);
452 if (!mh
) return FALSE
;
454 TRACE("Reallocated metafile: new size is %d\n",size
);
456 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
459 TRACE("Writing record to disk\n");
460 if (!WriteFile(physDev
->hFile
, mr
, rlen
, NULL
, NULL
))
464 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
468 physDev
->mh
->mtSize
+= rlen
/ 2;
469 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
474 /******************************************************************
478 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
481 METARECORD
*mr
= (METARECORD
*)&buffer
;
484 mr
->rdFunction
= func
;
485 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
489 /******************************************************************
492 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
495 METARECORD
*mr
= (METARECORD
*)&buffer
;
498 mr
->rdFunction
= func
;
499 *(mr
->rdParm
) = param1
;
500 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
504 /******************************************************************
507 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
510 METARECORD
*mr
= (METARECORD
*)&buffer
;
513 mr
->rdFunction
= func
;
514 *(mr
->rdParm
) = param2
;
515 *(mr
->rdParm
+ 1) = param1
;
516 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
520 /******************************************************************
524 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
525 short param3
, short param4
)
528 METARECORD
*mr
= (METARECORD
*)&buffer
;
531 mr
->rdFunction
= func
;
532 *(mr
->rdParm
) = param4
;
533 *(mr
->rdParm
+ 1) = param3
;
534 *(mr
->rdParm
+ 2) = param2
;
535 *(mr
->rdParm
+ 3) = param1
;
536 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
540 /******************************************************************
544 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
545 short param3
, short param4
, short param5
, short param6
)
548 METARECORD
*mr
= (METARECORD
*)&buffer
;
551 mr
->rdFunction
= func
;
552 *(mr
->rdParm
) = param6
;
553 *(mr
->rdParm
+ 1) = param5
;
554 *(mr
->rdParm
+ 2) = param4
;
555 *(mr
->rdParm
+ 3) = param3
;
556 *(mr
->rdParm
+ 4) = param2
;
557 *(mr
->rdParm
+ 5) = param1
;
558 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
562 /******************************************************************
565 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
566 short param3
, short param4
, short param5
,
567 short param6
, short param7
, short param8
)
570 METARECORD
*mr
= (METARECORD
*)&buffer
;
573 mr
->rdFunction
= func
;
574 *(mr
->rdParm
) = param8
;
575 *(mr
->rdParm
+ 1) = param7
;
576 *(mr
->rdParm
+ 2) = param6
;
577 *(mr
->rdParm
+ 3) = param5
;
578 *(mr
->rdParm
+ 4) = param4
;
579 *(mr
->rdParm
+ 5) = param3
;
580 *(mr
->rdParm
+ 6) = param2
;
581 *(mr
->rdParm
+ 7) = param1
;
582 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);