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 MFDRV_Chord
, /* pChord */
31 MFDRV_CloseFigure
, /* pCloseFigure */
32 NULL
, /* pCreateBitmap */
33 NULL
, /* no implementation */ /* pCreateDC */
34 NULL
, /* pCreateDIBSection */
35 NULL
, /* pCreateDIBSection16 */
36 NULL
, /* no implementation */ /* pDeleteDC */
37 NULL
, /* pDeleteObject */
38 NULL
, /* pDeviceCapabilities */
39 MFDRV_Ellipse
, /* pEllipse */
42 MFDRV_EndPath
, /* pEndPath */
43 NULL
, /* pEnumDeviceFonts */
45 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
46 NULL
, /* pExtDeviceMode */
47 MFDRV_ExtFloodFill
, /* pExtFloodFill */
48 MFDRV_ExtTextOut
, /* pExtTextOut */
49 MFDRV_FillPath
, /* pFillPath */
50 MFDRV_FillRgn
, /* pFillRgn */
51 MFDRV_FlattenPath
, /* pFlattenPath */
52 MFDRV_FrameRgn
, /* pFrameRgn */
53 NULL
, /* pGetCharWidth */
54 NULL
, /* pGetDCOrgEx */
55 NULL
, /* no implementation */ /* pGetPixel */
56 NULL
, /* pGetTextExtentPoint */
57 NULL
, /* pGetTextMetrics */
58 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
59 MFDRV_InvertRgn
, /* pInvertRgn */
60 MFDRV_LineTo
, /* pLineTo */
61 NULL
, /* pLoadOEMResource */
62 MFDRV_MoveToEx
, /* pMoveToEx */
63 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
64 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
65 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
66 MFDRV_PaintRgn
, /* pPaintRgn */
67 MFDRV_PatBlt
, /* pPatBlt */
69 MFDRV_PolyBezier
, /* pPolyBezier */
70 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
72 MFDRV_PolyPolygon
, /* pPolyPolygon */
73 NULL
, /* pPolyPolyline */
74 MFDRV_Polygon
, /* pPolygon */
75 MFDRV_Polyline
, /* pPolyline */
76 NULL
, /* pPolylineTo */
77 NULL
, /* pRealizePalette */
78 MFDRV_Rectangle
, /* pRectangle */
79 MFDRV_RestoreDC
, /* pRestoreDC */
80 MFDRV_RoundRect
, /* pRoundRect */
81 MFDRV_SaveDC
, /* pSaveDC */
82 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
83 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
84 MFDRV_SelectClipPath
, /* pSelectClipPath */
85 NULL
, /* pSelectClipRgn */
86 MFDRV_SelectObject
, /* pSelectObject */
87 NULL
, /* pSelectPalette */
88 MFDRV_SetBkColor
, /* pSetBkColor */
89 MFDRV_SetBkMode
, /* pSetBkMode */
90 NULL
, /* pSetDeviceClipping */
91 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
92 MFDRV_SetMapMode
, /* pSetMapMode */
93 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
94 MFDRV_SetPixel
, /* pSetPixel */
95 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
96 MFDRV_SetROP2
, /* pSetROP2 */
97 MFDRV_SetRelAbs
, /* pSetRelAbs */
98 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
99 MFDRV_SetTextAlign
, /* pSetTextAlign */
100 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
101 MFDRV_SetTextColor
, /* pSetTextColor */
102 MFDRV_SetTextJustification
, /* pSetTextJustification */
103 MFDRV_SetViewportExt
, /* pSetViewportExt */
104 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
105 MFDRV_SetWindowExt
, /* pSetWindowExt */
106 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
107 NULL
, /* pStartDoc */
108 NULL
, /* pStartPage */
109 MFDRV_StretchBlt
, /* pStretchBlt */
110 MFDRV_StretchDIBits
, /* pStretchDIBits */
111 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
112 MFDRV_StrokePath
, /* pStrokePath */
113 MFDRV_WidenPath
/* pWidenPath */
118 /**********************************************************************
119 * MFDRV_AllocMetaFile
121 static DC
*MFDRV_AllocMetaFile(void)
124 METAFILEDRV_PDEVICE
*physDev
;
126 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
))) return NULL
;
127 dc
->header
.wMagic
= METAFILE_DC_MAGIC
;
129 physDev
= (METAFILEDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
132 GDI_HEAP_FREE( dc
->hSelf
);
135 dc
->physDev
= physDev
;
137 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
139 HeapFree( GetProcessHeap(), 0, physDev
);
140 GDI_HEAP_FREE( dc
->hSelf
);
144 physDev
->nextHandle
= 0;
147 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
148 physDev
->mh
->mtVersion
= 0x0300;
149 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
150 physDev
->mh
->mtNoObjects
= 0;
151 physDev
->mh
->mtMaxRecord
= 0;
152 physDev
->mh
->mtNoParameters
= 0;
158 /**********************************************************************
161 static BOOL
MFDRV_DeleteDC( DC
*dc
)
163 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
165 if (physDev
->mh
) HeapFree( GetProcessHeap(), 0, physDev
->mh
);
166 HeapFree( GetProcessHeap(), 0, physDev
);
168 GDI_FreeObject(dc
->hSelf
);
172 /**********************************************************************
173 * CreateMetaFile16 (GDI.125)
175 * Create a new DC and associate it with a metafile. Pass a filename
176 * to create a disk-based metafile, NULL to create a memory metafile.
179 * A handle to the metafile DC if successful, NULL on failure.
181 HDC16 WINAPI
CreateMetaFile16(
182 LPCSTR filename
/* Filename of disk metafile */
186 METAFILEDRV_PDEVICE
*physDev
;
189 TRACE("'%s'\n", filename
);
191 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
192 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
194 if (filename
) /* disk based metafile */
196 physDev
->mh
->mtType
= METAFILE_DISK
;
197 if ((hFile
= CreateFileA(filename
, GENERIC_WRITE
, 0, NULL
,
198 CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
199 MFDRV_DeleteDC( dc
);
202 if (!WriteFile( hFile
, (LPSTR
)physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
204 MFDRV_DeleteDC( dc
);
207 physDev
->hFile
= hFile
;
209 /* Grow METAHEADER to include filename */
210 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
);
212 else /* memory based metafile */
213 physDev
->mh
->mtType
= METAFILE_MEMORY
;
215 TRACE("returning %04x\n", dc
->hSelf
);
219 /**********************************************************************
220 * CreateMetaFileA (GDI32.51)
222 HDC WINAPI
CreateMetaFileA(
223 LPCSTR filename
/* Filename of disk metafile */
226 return CreateMetaFile16( filename
);
229 /**********************************************************************
230 * CreateMetaFileW (GDI32.52)
232 HDC WINAPI
CreateMetaFileW(LPCWSTR filename
)
237 filenameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
239 hReturnDC
= CreateMetaFileA(filenameA
);
241 HeapFree( GetProcessHeap(), 0, filenameA
);
247 /**********************************************************************
248 * MFDRV_CloseMetaFile
250 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
253 METAFILEDRV_PDEVICE
*physDev
;
255 TRACE("(%04x)\n", hdc
);
257 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
258 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
260 /* Construct the end of metafile record - this is documented
261 * in SDK Knowledgebase Q99334.
264 if (!MFDRV_MetaParam0(dc
, META_EOF
))
266 MFDRV_DeleteDC( dc
);
270 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
272 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
273 MFDRV_DeleteDC( dc
);
277 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
278 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
279 sizeof(*physDev
->mh
), NULL
, NULL
)) {
280 MFDRV_DeleteDC( dc
);
283 CloseHandle(physDev
->hFile
);
284 physDev
->mh
->mtType
= METAFILE_DISK
;
290 /******************************************************************
291 * CloseMetaFile16 (GDI.126)
293 HMETAFILE16 WINAPI
CloseMetaFile16(
294 HDC16 hdc
/* Metafile DC to close */
298 METAFILEDRV_PDEVICE
*physDev
;
299 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
301 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
303 /* Now allocate a global handle for the metafile */
305 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
307 physDev
->mh
= NULL
; /* So it won't be deleted */
308 MFDRV_DeleteDC( dc
);
312 /******************************************************************
313 * CloseMetaFile (GDI32.17)
315 * Stop recording graphics operations in metafile associated with
316 * hdc and retrieve metafile.
319 * Handle of newly created metafile on success, NULL on failure.
321 HMETAFILE WINAPI
CloseMetaFile(
322 HDC hdc
/* Metafile DC to close */
326 METAFILEDRV_PDEVICE
*physDev
;
327 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
329 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
331 /* Now allocate a global handle for the metafile */
333 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
335 physDev
->mh
= NULL
; /* So it won't be deleted */
336 MFDRV_DeleteDC( dc
);
341 /******************************************************************
344 * Warning: this function can change the pointer to the metafile header.
346 BOOL
MFDRV_WriteRecord( DC
*dc
, METARECORD
*mr
, DWORD rlen
)
350 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
352 switch(physDev
->mh
->mtType
)
354 case METAFILE_MEMORY
:
355 len
= physDev
->mh
->mtSize
* 2 + rlen
;
356 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, len
);
357 if (!mh
) return FALSE
;
359 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
362 TRACE("Writing record to disk\n");
363 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
367 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
371 physDev
->mh
->mtSize
+= rlen
/ 2;
372 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
377 /******************************************************************
381 BOOL
MFDRV_MetaParam0(DC
*dc
, short func
)
384 METARECORD
*mr
= (METARECORD
*)&buffer
;
387 mr
->rdFunction
= func
;
388 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
392 /******************************************************************
395 BOOL
MFDRV_MetaParam1(DC
*dc
, short func
, short param1
)
398 METARECORD
*mr
= (METARECORD
*)&buffer
;
401 mr
->rdFunction
= func
;
402 *(mr
->rdParm
) = param1
;
403 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
407 /******************************************************************
410 BOOL
MFDRV_MetaParam2(DC
*dc
, short func
, short param1
, short param2
)
413 METARECORD
*mr
= (METARECORD
*)&buffer
;
416 mr
->rdFunction
= func
;
417 *(mr
->rdParm
) = param2
;
418 *(mr
->rdParm
+ 1) = param1
;
419 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
423 /******************************************************************
427 BOOL
MFDRV_MetaParam4(DC
*dc
, short func
, short param1
, short param2
,
428 short param3
, short param4
)
431 METARECORD
*mr
= (METARECORD
*)&buffer
;
434 mr
->rdFunction
= func
;
435 *(mr
->rdParm
) = param4
;
436 *(mr
->rdParm
+ 1) = param3
;
437 *(mr
->rdParm
+ 2) = param2
;
438 *(mr
->rdParm
+ 3) = param1
;
439 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
443 /******************************************************************
447 BOOL
MFDRV_MetaParam6(DC
*dc
, short func
, short param1
, short param2
,
448 short param3
, short param4
, short param5
, short param6
)
451 METARECORD
*mr
= (METARECORD
*)&buffer
;
454 mr
->rdFunction
= func
;
455 *(mr
->rdParm
) = param6
;
456 *(mr
->rdParm
+ 1) = param5
;
457 *(mr
->rdParm
+ 2) = param4
;
458 *(mr
->rdParm
+ 3) = param3
;
459 *(mr
->rdParm
+ 4) = param2
;
460 *(mr
->rdParm
+ 5) = param1
;
461 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
465 /******************************************************************
468 BOOL
MFDRV_MetaParam8(DC
*dc
, short func
, short param1
, short param2
,
469 short param3
, short param4
, short param5
,
470 short param6
, short param7
, short param8
)
473 METARECORD
*mr
= (METARECORD
*)&buffer
;
476 mr
->rdFunction
= func
;
477 *(mr
->rdParm
) = param8
;
478 *(mr
->rdParm
+ 1) = param7
;
479 *(mr
->rdParm
+ 2) = param6
;
480 *(mr
->rdParm
+ 3) = param5
;
481 *(mr
->rdParm
+ 4) = param4
;
482 *(mr
->rdParm
+ 5) = param3
;
483 *(mr
->rdParm
+ 6) = param2
;
484 *(mr
->rdParm
+ 7) = param1
;
485 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
489 /******************************************************************
492 * Note: this function assumes that we never delete objects.
493 * If we do someday, we'll need to maintain a table to re-use deleted
496 int MFDRV_AddHandleDC( DC
*dc
)
498 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
499 physDev
->mh
->mtNoObjects
++;
500 return physDev
->nextHandle
++;