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
, /* pGetICMProfile */
79 NULL
, /* pGetNearestColor */
81 NULL
, /* pGetPixelFormat */
82 NULL
, /* pGetSystemPaletteEntries */
83 NULL
, /* pGetTextExtentExPoint */
84 NULL
, /* pGetTextMetrics */
85 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
86 MFDRV_InvertRgn
, /* pInvertRgn */
87 MFDRV_LineTo
, /* pLineTo */
88 NULL
, /* pModifyWorldTransform */
89 MFDRV_MoveTo
, /* pMoveTo */
90 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
91 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
92 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
93 MFDRV_PaintRgn
, /* pPaintRgn */
94 MFDRV_PatBlt
, /* pPatBlt */
96 MFDRV_PolyBezier
, /* pPolyBezier */
97 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
99 MFDRV_PolyPolygon
, /* pPolyPolygon */
100 NULL
, /* pPolyPolyline */
101 MFDRV_Polygon
, /* pPolygon */
102 MFDRV_Polyline
, /* pPolyline */
103 NULL
, /* pPolylineTo */
104 NULL
, /* pRealizeDefaultPalette */
105 MFDRV_RealizePalette
, /* pRealizePalette */
106 MFDRV_Rectangle
, /* pRectangle */
108 MFDRV_RestoreDC
, /* pRestoreDC */
109 MFDRV_RoundRect
, /* pRoundRect */
110 MFDRV_SaveDC
, /* pSaveDC */
111 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
112 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
113 MFDRV_SelectBitmap
, /* pSelectBitmap */
114 MFDRV_SelectBrush
, /* pSelectBrush */
115 MFDRV_SelectClipPath
, /* pSelectClipPath */
116 MFDRV_SelectFont
, /* pSelectFont */
117 MFDRV_SelectPalette
, /* pSelectPalette */
118 MFDRV_SelectPen
, /* pSelectPen */
119 NULL
, /* pSetArcDirection */
120 NULL
, /* pSetBitmapBits */
121 MFDRV_SetBkColor
, /* pSetBkColor */
122 MFDRV_SetBkMode
, /* pSetBkMode */
123 NULL
, /* pSetDCBrushColor*/
124 NULL
, /* pSetDCOrg */
125 NULL
, /* pSetDCPenColor*/
126 NULL
, /* pSetDIBColorTable */
127 NULL
, /* pSetDIBits */
128 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
129 NULL
, /* pSetDeviceClipping */
130 NULL
, /* pSetDeviceGammaRamp */
131 MFDRV_SetMapMode
, /* pSetMapMode */
132 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
133 MFDRV_SetPixel
, /* pSetPixel */
134 NULL
, /* pSetPixelFormat */
135 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
136 MFDRV_SetROP2
, /* pSetROP2 */
137 MFDRV_SetRelAbs
, /* pSetRelAbs */
138 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
139 MFDRV_SetTextAlign
, /* pSetTextAlign */
140 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
141 MFDRV_SetTextColor
, /* pSetTextColor */
142 MFDRV_SetTextJustification
, /* pSetTextJustification */
143 MFDRV_SetViewportExt
, /* pSetViewportExt */
144 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
145 MFDRV_SetWindowExt
, /* pSetWindowExt */
146 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
147 NULL
, /* pSetWorldTransform */
148 NULL
, /* pStartDoc */
149 NULL
, /* pStartPage */
150 MFDRV_StretchBlt
, /* pStretchBlt */
151 MFDRV_StretchDIBits
, /* pStretchDIBits */
152 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
153 MFDRV_StrokePath
, /* pStrokePath */
154 NULL
, /* pSwapBuffers */
155 NULL
, /* pUnrealizePalette */
156 MFDRV_WidenPath
/* pWidenPath */
161 /**********************************************************************
162 * MFDRV_AllocMetaFile
164 static DC
*MFDRV_AllocMetaFile(void)
167 METAFILEDRV_PDEVICE
*physDev
;
169 if (!(dc
= alloc_dc_ptr( &MFDRV_Funcs
, METAFILE_DC_MAGIC
))) return NULL
;
171 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
177 dc
->physDev
= (PHYSDEV
)physDev
;
178 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( DC
*dc
)
209 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
212 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
213 for(index
= 0; index
< physDev
->handles_size
; index
++)
214 if(physDev
->handles
[index
])
215 GDI_hdc_not_using_object(physDev
->handles
[index
], physDev
->hdc
);
216 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
217 HeapFree( GetProcessHeap(), 0, physDev
);
224 /**********************************************************************
225 * CreateMetaFileW (GDI32.@)
227 * Create a new DC and associate it with a metafile. Pass a filename
228 * to create a disk-based metafile, NULL to create a memory metafile.
231 * filename [I] Filename of disk metafile
234 * A handle to the metafile DC if successful, NULL on failure.
236 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
240 METAFILEDRV_PDEVICE
*physDev
;
243 TRACE("%s\n", debugstr_w(filename
) );
245 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
246 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
248 if (filename
) /* disk based metafile */
250 physDev
->mh
->mtType
= METAFILE_DISK
;
251 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
252 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
253 MFDRV_DeleteDC( dc
);
256 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
258 MFDRV_DeleteDC( dc
);
261 physDev
->hFile
= hFile
;
263 /* Grow METAHEADER to include filename */
264 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
266 else /* memory based metafile */
267 physDev
->mh
->mtType
= METAFILE_MEMORY
;
269 TRACE("returning %p\n", dc
->hSelf
);
271 release_dc_ptr( dc
);
275 /**********************************************************************
276 * CreateMetaFileA (GDI32.@)
278 * See CreateMetaFileW.
280 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
286 if (!filename
) return CreateMetaFileW(NULL
);
288 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
289 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
290 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
292 hReturnDC
= CreateMetaFileW(filenameW
);
294 HeapFree( GetProcessHeap(), 0, filenameW
);
300 /**********************************************************************
301 * MFDRV_CloseMetaFile
303 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
306 METAFILEDRV_PDEVICE
*physDev
;
308 TRACE("(%p)\n", hdc
);
310 if (!(dc
= get_dc_ptr( hdc
))) return NULL
;
311 if (GDIMAGIC(dc
->header
.wMagic
) != METAFILE_DC_MAGIC
)
313 release_dc_ptr( dc
);
316 if (dc
->refcount
!= 1)
318 FIXME( "not deleting busy DC %p refcount %u\n", dc
->hSelf
, dc
->refcount
);
319 release_dc_ptr( dc
);
322 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
324 /* Construct the end of metafile record - this is documented
325 * in SDK Knowledgebase Q99334.
328 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
330 MFDRV_DeleteDC( dc
);
334 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
336 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
337 MFDRV_DeleteDC( dc
);
341 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
342 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
343 sizeof(*physDev
->mh
), NULL
, NULL
)) {
344 MFDRV_DeleteDC( dc
);
347 CloseHandle(physDev
->hFile
);
348 physDev
->mh
->mtType
= METAFILE_DISK
;
354 /******************************************************************
355 * MF_Create_HMETATFILE16
357 * Creates a HMETAFILE16 object from a METAHEADER
359 * HMETAFILE16s are Global memory handles.
361 HMETAFILE16
MF_Create_HMETAFILE16(METAHEADER
*mh
)
364 DWORD size
= mh
->mtSize
* sizeof(WORD
);
366 hmf
= GlobalAlloc16(GMEM_MOVEABLE
, size
);
369 METAHEADER
*mh_dest
= GlobalLock16(hmf
);
370 memcpy(mh_dest
, mh
, size
);
373 HeapFree(GetProcessHeap(), 0, mh
);
377 /******************************************************************
378 * CloseMetaFile (GDI.126)
381 * hdc [I] Metafile DC to close
383 HMETAFILE16 WINAPI
CloseMetaFile16(HDC16 hdc
)
386 METAFILEDRV_PDEVICE
*physDev
;
387 DC
*dc
= MFDRV_CloseMetaFile(HDC_32(hdc
));
389 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
391 /* Now allocate a global handle for the metafile */
393 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
395 physDev
->mh
= NULL
; /* So it won't be deleted */
396 MFDRV_DeleteDC( dc
);
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 */
425 MFDRV_DeleteDC( dc
);
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
, (char *)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);
586 /**********************************************************************
589 INT
MFDRV_ExtEscape( PHYSDEV dev
, INT nEscape
, INT cbInput
, LPCVOID in_data
,
590 INT cbOutput
, LPVOID out_data
)
596 if (cbOutput
) return 0; /* escapes that require output cannot work in metafiles */
598 len
= sizeof(*mr
) + sizeof(WORD
) + ((cbInput
+ 1) & ~1);
599 mr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
600 mr
->rdSize
= len
/ 2;
601 mr
->rdFunction
= META_ESCAPE
;
602 mr
->rdParm
[0] = nEscape
;
603 mr
->rdParm
[1] = cbInput
;
604 memcpy(&(mr
->rdParm
[2]), in_data
, cbInput
);
605 ret
= MFDRV_WriteRecord( dev
, mr
, len
);
606 HeapFree(GetProcessHeap(), 0, mr
);
611 /******************************************************************
612 * MFDRV_GetDeviceCaps
614 *A very simple implementation that returns DT_METAFILE
616 INT
MFDRV_GetDeviceCaps(PHYSDEV dev
, INT cap
)
625 TRACE(" unsupported capability %d, will return 0\n", cap
);