2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
8 #include "wine/winbase16.h"
13 #include "metafiledrv.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(metafile
)
20 static const DC_FUNCTIONS MFDRV_Funcs
=
23 MFDRV_AbortPath
, /* pAbortPath */
27 MFDRV_BeginPath
, /* pBeginPath */
28 MFDRV_BitBlt
, /* pBitBlt */
29 NULL
, /* pBitmapBits */
30 NULL
, /* pChoosePixelFormat */
31 MFDRV_Chord
, /* pChord */
32 MFDRV_CloseFigure
, /* pCloseFigure */
33 NULL
, /* pCreateBitmap */
34 NULL
, /* no implementation */ /* pCreateDC */
35 NULL
, /* pCreateDIBSection */
36 NULL
, /* pCreateDIBSection16 */
37 NULL
, /* no implementation */ /* pDeleteDC */
38 NULL
, /* pDeleteObject */
39 NULL
, /* pDescribePixelFormat */
40 NULL
, /* pDeviceCapabilities */
41 MFDRV_Ellipse
, /* pEllipse */
44 MFDRV_EndPath
, /* pEndPath */
45 NULL
, /* pEnumDeviceFonts */
47 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
48 NULL
, /* pExtDeviceMode */
49 MFDRV_ExtFloodFill
, /* pExtFloodFill */
50 MFDRV_ExtTextOut
, /* pExtTextOut */
51 MFDRV_FillPath
, /* pFillPath */
52 MFDRV_FillRgn
, /* pFillRgn */
53 MFDRV_FlattenPath
, /* pFlattenPath */
54 MFDRV_FrameRgn
, /* pFrameRgn */
55 NULL
, /* pGetCharWidth */
56 NULL
, /* pGetDCOrgEx */
57 NULL
, /* no implementation */ /* pGetPixel */
58 NULL
, /* pGetPixelFormat */
59 NULL
, /* pGetTextExtentPoint */
60 NULL
, /* pGetTextMetrics */
61 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
62 MFDRV_InvertRgn
, /* pInvertRgn */
63 MFDRV_LineTo
, /* pLineTo */
64 MFDRV_MoveToEx
, /* pMoveToEx */
65 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
66 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
67 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
68 MFDRV_PaintRgn
, /* pPaintRgn */
69 MFDRV_PatBlt
, /* pPatBlt */
71 MFDRV_PolyBezier
, /* pPolyBezier */
72 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
74 MFDRV_PolyPolygon
, /* pPolyPolygon */
75 NULL
, /* pPolyPolyline */
76 MFDRV_Polygon
, /* pPolygon */
77 MFDRV_Polyline
, /* pPolyline */
78 NULL
, /* pPolylineTo */
79 NULL
, /* pRealizePalette */
80 MFDRV_Rectangle
, /* pRectangle */
81 MFDRV_RestoreDC
, /* pRestoreDC */
82 MFDRV_RoundRect
, /* pRoundRect */
83 MFDRV_SaveDC
, /* pSaveDC */
84 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
85 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
86 MFDRV_SelectClipPath
, /* pSelectClipPath */
87 NULL
, /* pSelectClipRgn */
88 MFDRV_SelectObject
, /* pSelectObject */
89 NULL
, /* pSelectPalette */
90 MFDRV_SetBkColor
, /* pSetBkColor */
91 MFDRV_SetBkMode
, /* pSetBkMode */
92 NULL
, /* pSetDeviceClipping */
93 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
94 MFDRV_SetMapMode
, /* pSetMapMode */
95 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
96 MFDRV_SetPixel
, /* pSetPixel */
97 NULL
, /* pSetPixelFormat */
98 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
99 MFDRV_SetROP2
, /* pSetROP2 */
100 MFDRV_SetRelAbs
, /* pSetRelAbs */
101 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
102 MFDRV_SetTextAlign
, /* pSetTextAlign */
103 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
104 MFDRV_SetTextColor
, /* pSetTextColor */
105 MFDRV_SetTextJustification
, /* pSetTextJustification */
106 MFDRV_SetViewportExt
, /* pSetViewportExt */
107 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
108 MFDRV_SetWindowExt
, /* pSetWindowExt */
109 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
110 NULL
, /* pStartDoc */
111 NULL
, /* pStartPage */
112 MFDRV_StretchBlt
, /* pStretchBlt */
113 MFDRV_StretchDIBits
, /* pStretchDIBits */
114 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
115 MFDRV_StrokePath
, /* pStrokePath */
116 NULL
, /* pSwapBuffers */
117 MFDRV_WidenPath
/* pWidenPath */
122 /**********************************************************************
123 * MFDRV_AllocMetaFile
125 static DC
*MFDRV_AllocMetaFile(void)
128 METAFILEDRV_PDEVICE
*physDev
;
130 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
))) return NULL
;
131 dc
->header
.wMagic
= METAFILE_DC_MAGIC
;
133 physDev
= (METAFILEDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
136 GDI_FreeObject( dc
->hSelf
, dc
);
139 dc
->physDev
= physDev
;
141 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
143 HeapFree( GetProcessHeap(), 0, physDev
);
144 GDI_FreeObject( dc
->hSelf
, dc
);
148 physDev
->nextHandle
= 0;
151 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
152 physDev
->mh
->mtVersion
= 0x0300;
153 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
154 physDev
->mh
->mtNoObjects
= 0;
155 physDev
->mh
->mtMaxRecord
= 0;
156 physDev
->mh
->mtNoParameters
= 0;
162 /**********************************************************************
165 static BOOL
MFDRV_DeleteDC( DC
*dc
)
167 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
169 if (physDev
->mh
) HeapFree( GetProcessHeap(), 0, physDev
->mh
);
170 HeapFree( GetProcessHeap(), 0, physDev
);
172 GDI_FreeObject( dc
->hSelf
, dc
);
176 /**********************************************************************
177 * CreateMetaFile16 (GDI.125)
179 * Create a new DC and associate it with a metafile. Pass a filename
180 * to create a disk-based metafile, NULL to create a memory metafile.
183 * A handle to the metafile DC if successful, NULL on failure.
185 HDC16 WINAPI
CreateMetaFile16(
186 LPCSTR filename
/* Filename of disk metafile */
191 METAFILEDRV_PDEVICE
*physDev
;
194 TRACE("'%s'\n", filename
);
196 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
197 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
199 if (filename
) /* disk based metafile */
201 physDev
->mh
->mtType
= METAFILE_DISK
;
202 if ((hFile
= CreateFileA(filename
, GENERIC_WRITE
, 0, NULL
,
203 CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
204 MFDRV_DeleteDC( dc
);
207 if (!WriteFile( hFile
, (LPSTR
)physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
209 MFDRV_DeleteDC( dc
);
212 physDev
->hFile
= hFile
;
214 /* Grow METAHEADER to include filename */
215 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
);
217 else /* memory based metafile */
218 physDev
->mh
->mtType
= METAFILE_MEMORY
;
220 TRACE("returning %04x\n", dc
->hSelf
);
222 GDI_ReleaseObj( dc
->hSelf
);
226 /**********************************************************************
227 * CreateMetaFileA (GDI32.51)
229 HDC WINAPI
CreateMetaFileA(
230 LPCSTR filename
/* Filename of disk metafile */
233 return CreateMetaFile16( filename
);
236 /**********************************************************************
237 * CreateMetaFileW (GDI32.52)
239 HDC WINAPI
CreateMetaFileW(LPCWSTR filename
)
244 filenameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
246 hReturnDC
= CreateMetaFileA(filenameA
);
248 HeapFree( GetProcessHeap(), 0, filenameA
);
254 /**********************************************************************
255 * MFDRV_CloseMetaFile
257 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
260 METAFILEDRV_PDEVICE
*physDev
;
262 TRACE("(%04x)\n", hdc
);
264 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
265 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
267 /* Construct the end of metafile record - this is documented
268 * in SDK Knowledgebase Q99334.
271 if (!MFDRV_MetaParam0(dc
, META_EOF
))
273 MFDRV_DeleteDC( dc
);
277 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
279 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
280 MFDRV_DeleteDC( dc
);
284 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
285 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
286 sizeof(*physDev
->mh
), NULL
, NULL
)) {
287 MFDRV_DeleteDC( dc
);
290 CloseHandle(physDev
->hFile
);
291 physDev
->mh
->mtType
= METAFILE_DISK
;
297 /******************************************************************
298 * CloseMetaFile16 (GDI.126)
300 HMETAFILE16 WINAPI
CloseMetaFile16(
301 HDC16 hdc
/* Metafile DC to close */
305 METAFILEDRV_PDEVICE
*physDev
;
306 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
308 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
310 /* Now allocate a global handle for the metafile */
312 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
314 physDev
->mh
= NULL
; /* So it won't be deleted */
315 MFDRV_DeleteDC( dc
);
319 /******************************************************************
320 * CloseMetaFile (GDI32.17)
322 * Stop recording graphics operations in metafile associated with
323 * hdc and retrieve metafile.
326 * Handle of newly created metafile on success, NULL on failure.
328 HMETAFILE WINAPI
CloseMetaFile(
329 HDC hdc
/* Metafile DC to close */
333 METAFILEDRV_PDEVICE
*physDev
;
334 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
336 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
338 /* Now allocate a global handle for the metafile */
340 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
342 physDev
->mh
= NULL
; /* So it won't be deleted */
343 MFDRV_DeleteDC( dc
);
348 /******************************************************************
351 * Warning: this function can change the pointer to the metafile header.
353 BOOL
MFDRV_WriteRecord( DC
*dc
, METARECORD
*mr
, DWORD rlen
)
357 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
359 switch(physDev
->mh
->mtType
)
361 case METAFILE_MEMORY
:
362 len
= physDev
->mh
->mtSize
* 2 + rlen
;
363 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, len
);
364 if (!mh
) return FALSE
;
366 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
369 TRACE("Writing record to disk\n");
370 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
374 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
378 physDev
->mh
->mtSize
+= rlen
/ 2;
379 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
384 /******************************************************************
388 BOOL
MFDRV_MetaParam0(DC
*dc
, short func
)
391 METARECORD
*mr
= (METARECORD
*)&buffer
;
394 mr
->rdFunction
= func
;
395 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
399 /******************************************************************
402 BOOL
MFDRV_MetaParam1(DC
*dc
, short func
, short param1
)
405 METARECORD
*mr
= (METARECORD
*)&buffer
;
408 mr
->rdFunction
= func
;
409 *(mr
->rdParm
) = param1
;
410 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
414 /******************************************************************
417 BOOL
MFDRV_MetaParam2(DC
*dc
, short func
, short param1
, short param2
)
420 METARECORD
*mr
= (METARECORD
*)&buffer
;
423 mr
->rdFunction
= func
;
424 *(mr
->rdParm
) = param2
;
425 *(mr
->rdParm
+ 1) = param1
;
426 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
430 /******************************************************************
434 BOOL
MFDRV_MetaParam4(DC
*dc
, short func
, short param1
, short param2
,
435 short param3
, short param4
)
438 METARECORD
*mr
= (METARECORD
*)&buffer
;
441 mr
->rdFunction
= func
;
442 *(mr
->rdParm
) = param4
;
443 *(mr
->rdParm
+ 1) = param3
;
444 *(mr
->rdParm
+ 2) = param2
;
445 *(mr
->rdParm
+ 3) = param1
;
446 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
450 /******************************************************************
454 BOOL
MFDRV_MetaParam6(DC
*dc
, short func
, short param1
, short param2
,
455 short param3
, short param4
, short param5
, short param6
)
458 METARECORD
*mr
= (METARECORD
*)&buffer
;
461 mr
->rdFunction
= func
;
462 *(mr
->rdParm
) = param6
;
463 *(mr
->rdParm
+ 1) = param5
;
464 *(mr
->rdParm
+ 2) = param4
;
465 *(mr
->rdParm
+ 3) = param3
;
466 *(mr
->rdParm
+ 4) = param2
;
467 *(mr
->rdParm
+ 5) = param1
;
468 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
472 /******************************************************************
475 BOOL
MFDRV_MetaParam8(DC
*dc
, short func
, short param1
, short param2
,
476 short param3
, short param4
, short param5
,
477 short param6
, short param7
, short param8
)
480 METARECORD
*mr
= (METARECORD
*)&buffer
;
483 mr
->rdFunction
= func
;
484 *(mr
->rdParm
) = param8
;
485 *(mr
->rdParm
+ 1) = param7
;
486 *(mr
->rdParm
+ 2) = param6
;
487 *(mr
->rdParm
+ 3) = param5
;
488 *(mr
->rdParm
+ 4) = param4
;
489 *(mr
->rdParm
+ 5) = param3
;
490 *(mr
->rdParm
+ 6) = param2
;
491 *(mr
->rdParm
+ 7) = param1
;
492 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
496 /******************************************************************
499 * Note: this function assumes that we never delete objects.
500 * If we do someday, we'll need to maintain a table to re-use deleted
503 int MFDRV_AddHandleDC( DC
*dc
)
505 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
506 physDev
->mh
->mtNoObjects
++;
507 return physDev
->nextHandle
++;