2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
8 #include "wine/winbase16.h"
13 #include "metafiledrv.h"
18 static const DC_FUNCTIONS MFDRV_Funcs
=
21 MFDRV_BitBlt
, /* pBitBlt */
22 NULL
, /* pBitmapBits */
23 MFDRV_Chord
, /* pChord */
24 NULL
, /* pCreateBitmap */
25 NULL
, /* no implementation */ /* pCreateDC */
26 NULL
, /* no implementation */ /* pDeleteDC */
27 NULL
, /* pCreateDIBSection */
28 NULL
, /* pCreateDIBSection16 */
29 NULL
, /* pDeleteObject */
30 MFDRV_Ellipse
, /* pEllipse */
31 NULL
, /* pEnumDeviceFonts */
33 NULL
, /* pExcludeClipRect */
34 NULL
, /* pExcludeVisRect */
35 MFDRV_ExtFloodFill
, /* pExtFloodFill */
36 MFDRV_ExtTextOut
, /* pExtTextOut */
37 NULL
, /* pGetCharWidth */
38 NULL
, /* no implementation */ /* pGetPixel */
39 NULL
, /* pGetTextExtentPoint */
40 NULL
, /* pGetTextMetrics */
41 NULL
, /* pIntersectClipRect */
42 NULL
, /* pIntersectVisRect */
43 MFDRV_LineTo
, /* pLineTo */
44 NULL
, /* pLoadOEMResource */
45 MFDRV_MoveToEx
, /* pMoveToEx */
46 NULL
, /* pOffsetClipRgn */
47 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
48 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
49 MFDRV_PaintRgn
, /* pPaintRgn */
50 MFDRV_PatBlt
, /* pPatBlt */
52 MFDRV_PolyPolygon
, /* pPolyPolygon */
53 NULL
, /* pPolyPolyline */
54 MFDRV_Polygon
, /* pPolygon */
55 MFDRV_Polyline
, /* pPolyline */
56 NULL
, /* pPolyBezier */
57 NULL
, /* pRealizePalette */
58 MFDRV_Rectangle
, /* pRectangle */
59 NULL
, /* pRestoreDC */
60 MFDRV_RoundRect
, /* pRoundRect */
62 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
63 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
64 NULL
, /* pSelectClipRgn */
65 MFDRV_SelectObject
, /* pSelectObject */
66 NULL
, /* pSelectPalette */
67 MFDRV_SetBkColor
, /* pSetBkColor */
68 NULL
, /* pSetBkMode */
69 NULL
, /* pSetDeviceClipping */
70 NULL
, /* pSetDIBitsToDevice */
71 MFDRV_SetMapMode
, /* pSetMapMode */
72 NULL
, /* pSetMapperFlags */
73 MFDRV_SetPixel
, /* pSetPixel */
74 NULL
, /* pSetPolyFillMode */
76 NULL
, /* pSetRelAbs */
77 NULL
, /* pSetStretchBltMode */
78 NULL
, /* pSetTextAlign */
79 NULL
, /* pSetTextCharacterExtra */
80 MFDRV_SetTextColor
, /* pSetTextColor */
81 NULL
, /* pSetTextJustification */
82 MFDRV_SetViewportExt
, /* pSetViewportExt */
83 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
84 MFDRV_SetWindowExt
, /* pSetWindowExt */
85 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
86 MFDRV_StretchBlt
, /* pStretchBlt */
87 NULL
/* pStretchDIBits */
92 /**********************************************************************
95 static DC
*MFDRV_AllocMetaFile(void)
98 METAFILEDRV_PDEVICE
*physDev
;
100 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
))) return NULL
;
101 dc
->header
.wMagic
= METAFILE_DC_MAGIC
;
103 physDev
= (METAFILEDRV_PDEVICE
*)HeapAlloc(SystemHeap
,0,sizeof(*physDev
));
106 GDI_HEAP_FREE( dc
->hSelf
);
109 dc
->physDev
= physDev
;
111 if (!(physDev
->mh
= HeapAlloc( SystemHeap
, 0, sizeof(*physDev
->mh
) )))
113 HeapFree( SystemHeap
, 0, physDev
);
114 GDI_HEAP_FREE( dc
->hSelf
);
118 physDev
->nextHandle
= 0;
121 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
122 physDev
->mh
->mtVersion
= 0x0300;
123 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
124 physDev
->mh
->mtNoObjects
= 0;
125 physDev
->mh
->mtMaxRecord
= 0;
126 physDev
->mh
->mtNoParameters
= 0;
132 /**********************************************************************
135 static BOOL
MFDRV_DeleteDC( DC
*dc
)
137 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
139 if (physDev
->mh
) HeapFree( SystemHeap
, 0, physDev
->mh
);
140 HeapFree( SystemHeap
, 0, physDev
);
142 GDI_FreeObject(dc
->hSelf
);
146 /**********************************************************************
147 * CreateMetaFile16 (GDI.125)
149 * Create a new DC and associate it with a metafile. Pass a filename
150 * to create a disk-based metafile, NULL to create a memory metafile.
153 * A handle to the metafile DC if successful, NULL on failure.
155 HDC16 WINAPI
CreateMetaFile16(
156 LPCSTR filename
/* Filename of disk metafile */
160 METAFILEDRV_PDEVICE
*physDev
;
163 TRACE(metafile
, "'%s'\n", filename
);
165 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
166 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
168 if (filename
) /* disk based metafile */
170 physDev
->mh
->mtType
= METAFILE_DISK
;
171 if ((hFile
= CreateFileA(filename
, GENERIC_WRITE
, 0, NULL
,
172 CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
173 MFDRV_DeleteDC( dc
);
176 if (!WriteFile( hFile
, (LPSTR
)physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
178 MFDRV_DeleteDC( dc
);
181 physDev
->hFile
= hFile
;
183 /* Grow METAHEADER to include filename */
184 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
);
186 else /* memory based metafile */
187 physDev
->mh
->mtType
= METAFILE_MEMORY
;
189 TRACE(metafile
, "returning %04x\n", dc
->hSelf
);
193 /**********************************************************************
194 * CreateMetaFileA (GDI32.51)
196 HDC WINAPI
CreateMetaFileA(
197 LPCSTR filename
/* Filename of disk metafile */
200 return CreateMetaFile16( filename
);
203 /**********************************************************************
204 * CreateMetaFileW (GDI32.52)
206 HDC WINAPI
CreateMetaFileW(LPCWSTR filename
)
211 filenameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
213 hReturnDC
= CreateMetaFileA(filenameA
);
215 HeapFree( GetProcessHeap(), 0, filenameA
);
221 /**********************************************************************
222 * MFDRV_CloseMetaFile
224 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
227 METAFILEDRV_PDEVICE
*physDev
;
229 TRACE(metafile
, "(%04x)\n", hdc
);
231 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
232 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
234 /* Construct the end of metafile record - this is documented
235 * in SDK Knowledgebase Q99334.
238 if (!MFDRV_MetaParam0(dc
, META_EOF
))
240 MFDRV_DeleteDC( dc
);
244 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
246 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
247 MFDRV_DeleteDC( dc
);
251 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
252 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
253 sizeof(*physDev
->mh
), NULL
, NULL
)) {
254 MFDRV_DeleteDC( dc
);
257 CloseHandle(physDev
->hFile
);
258 physDev
->mh
->mtType
= METAFILE_DISK
;
264 /******************************************************************
265 * CloseMetaFile16 (GDI.126)
267 HMETAFILE16 WINAPI
CloseMetaFile16(
268 HDC16 hdc
/* Metafile DC to close */
272 METAFILEDRV_PDEVICE
*physDev
;
273 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
275 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
277 /* Now allocate a global handle for the metafile */
279 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
281 physDev
->mh
= NULL
; /* So it won't be deleted */
282 MFDRV_DeleteDC( dc
);
286 /******************************************************************
287 * CloseMetaFile (GDI32.17)
289 * Stop recording graphics operations in metafile associated with
290 * hdc and retrieve metafile.
293 * Handle of newly created metafile on success, NULL on failure.
295 HMETAFILE WINAPI
CloseMetaFile(
296 HDC hdc
/* Metafile DC to close */
300 METAFILEDRV_PDEVICE
*physDev
;
301 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
303 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
305 /* Now allocate a global handle for the metafile */
307 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
309 physDev
->mh
= NULL
; /* So it won't be deleted */
310 MFDRV_DeleteDC( dc
);
315 /******************************************************************
318 * Warning: this function can change the pointer to the metafile header.
320 BOOL
MFDRV_WriteRecord( DC
*dc
, METARECORD
*mr
, DWORD rlen
)
324 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
326 switch(physDev
->mh
->mtType
)
328 case METAFILE_MEMORY
:
329 len
= physDev
->mh
->mtSize
* 2 + rlen
;
330 mh
= HeapReAlloc( SystemHeap
, 0, physDev
->mh
, len
);
331 if (!mh
) return FALSE
;
333 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
336 TRACE(metafile
,"Writing record to disk\n");
337 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
341 ERR(metafile
, "Unknown metafile type %d\n", physDev
->mh
->mtType
);
345 physDev
->mh
->mtSize
+= rlen
/ 2;
346 physDev
->mh
->mtMaxRecord
= MAX(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
351 /******************************************************************
355 BOOL
MFDRV_MetaParam0(DC
*dc
, short func
)
358 METARECORD
*mr
= (METARECORD
*)&buffer
;
361 mr
->rdFunction
= func
;
362 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
366 /******************************************************************
369 BOOL
MFDRV_MetaParam1(DC
*dc
, short func
, short param1
)
372 METARECORD
*mr
= (METARECORD
*)&buffer
;
375 mr
->rdFunction
= func
;
376 *(mr
->rdParm
) = param1
;
377 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
381 /******************************************************************
384 BOOL
MFDRV_MetaParam2(DC
*dc
, short func
, short param1
, short param2
)
387 METARECORD
*mr
= (METARECORD
*)&buffer
;
390 mr
->rdFunction
= func
;
391 *(mr
->rdParm
) = param2
;
392 *(mr
->rdParm
+ 1) = param1
;
393 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
397 /******************************************************************
401 BOOL
MFDRV_MetaParam4(DC
*dc
, short func
, short param1
, short param2
,
402 short param3
, short param4
)
405 METARECORD
*mr
= (METARECORD
*)&buffer
;
408 mr
->rdFunction
= func
;
409 *(mr
->rdParm
) = param4
;
410 *(mr
->rdParm
+ 1) = param3
;
411 *(mr
->rdParm
+ 2) = param2
;
412 *(mr
->rdParm
+ 3) = param1
;
413 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
417 /******************************************************************
421 BOOL
MFDRV_MetaParam6(DC
*dc
, short func
, short param1
, short param2
,
422 short param3
, short param4
, short param5
, short param6
)
425 METARECORD
*mr
= (METARECORD
*)&buffer
;
428 mr
->rdFunction
= func
;
429 *(mr
->rdParm
) = param6
;
430 *(mr
->rdParm
+ 1) = param5
;
431 *(mr
->rdParm
+ 2) = param4
;
432 *(mr
->rdParm
+ 3) = param3
;
433 *(mr
->rdParm
+ 4) = param2
;
434 *(mr
->rdParm
+ 5) = param1
;
435 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
439 /******************************************************************
442 BOOL
MFDRV_MetaParam8(DC
*dc
, short func
, short param1
, short param2
,
443 short param3
, short param4
, short param5
,
444 short param6
, short param7
, short param8
)
447 METARECORD
*mr
= (METARECORD
*)&buffer
;
450 mr
->rdFunction
= func
;
451 *(mr
->rdParm
) = param8
;
452 *(mr
->rdParm
+ 1) = param7
;
453 *(mr
->rdParm
+ 2) = param6
;
454 *(mr
->rdParm
+ 3) = param5
;
455 *(mr
->rdParm
+ 4) = param4
;
456 *(mr
->rdParm
+ 5) = param3
;
457 *(mr
->rdParm
+ 6) = param2
;
458 *(mr
->rdParm
+ 7) = param1
;
459 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
463 /******************************************************************
466 * Note: this function assumes that we never delete objects.
467 * If we do someday, we'll need to maintain a table to re-use deleted
470 int MFDRV_AddHandleDC( DC
*dc
)
472 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
473 physDev
->mh
->mtNoObjects
++;
474 return physDev
->nextHandle
++;
485 /********************************************************************
487 Enhanced Metafile driver initializations
489 This possibly should be moved to their own file/directory
492 **********************************************************************/
494 /**********************************************************************
495 * CreateEnhMetaFile32A (GDI32.41)
497 HDC WINAPI
CreateEnhMetaFileA(
498 HDC hdc
, /* optional reference DC */
499 LPCSTR filename
, /* optional filename for disk metafiles */
500 const RECT
*rect
, /* optional bounding rectangle */
501 LPCSTR description
/* optional description */
506 METAFILEDRV_PDEVICE
*physDev
;
509 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
510 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
512 if (filename
) /* disk based metafile */
514 physDev
->mh
->mtType
= METAFILE_DISK
;
515 if ((hFile
= _lcreat( filename
, 0 )) == HFILE_ERROR
)
517 MFDRV_DeleteDC( dc
);
520 if (_lwrite( hFile
, (LPSTR
)physDev
->mh
,
521 sizeof(*physDev
->mh
)) == HFILE_ERROR
)
523 MFDRV_DeleteDC( dc
);
526 physDev
->mh
->mtNoParameters
= hFile
; /* store file descriptor here */
527 /* windows probably uses this too*/
529 else /* memory based metafile */
530 physDev
->mh
->mtType
= METAFILE_MEMORY
;
532 TRACE(metafile
, "returning %04x\n", dc
->hSelf
);
536 FIXME(metafile
, "(0x%lx,%s,%p,%s): stub\n",
537 (DWORD
)hdc
, filename
, rect
, description
);
542 /**********************************************************************
543 * CreateEnhMetaFile32W (GDI32.42)
545 HDC WINAPI
CreateEnhMetaFileW(
546 HDC hdc
, /* optional reference DC */
547 LPCWSTR filename
, /* optional filename for disk metafiles */
548 const RECT
* rect
, /* optional bounding rectangle */
549 LPCWSTR description
/* optional description */
556 filenameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
557 descriptionA
= HEAP_strdupWtoA( GetProcessHeap(), 0, description
);
559 hReturnDC
= CreateEnhMetaFileA(hdc
,
564 HeapFree( GetProcessHeap(), 0, filenameA
);
565 HeapFree( GetProcessHeap(), 0, descriptionA
);
570 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* metafile DC */ )
572 /* write EMR_EOF(0x0, 0x10, 0x14) */