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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/winbase16.h"
25 #include "mfdrv/metafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(metafile
);
32 static const DC_FUNCTIONS MFDRV_Funcs
=
35 MFDRV_AbortPath
, /* pAbortPath */
39 MFDRV_BeginPath
, /* pBeginPath */
40 MFDRV_BitBlt
, /* pBitBlt */
41 NULL
, /* pChoosePixelFormat */
42 MFDRV_Chord
, /* pChord */
43 MFDRV_CloseFigure
, /* pCloseFigure */
44 NULL
, /* pCreateBitmap */
46 NULL
, /* pCreateDIBSection */
47 NULL
, /* pDeleteBitmap */
49 NULL
, /* pDescribePixelFormat */
50 NULL
, /* pDeviceCapabilities */
51 MFDRV_Ellipse
, /* pEllipse */
54 MFDRV_EndPath
, /* pEndPath */
55 NULL
, /* pEnumDeviceFonts */
56 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
57 NULL
, /* pExtDeviceMode */
58 MFDRV_ExtEscape
, /* pExtEscape */
59 MFDRV_ExtFloodFill
, /* pExtFloodFill */
60 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
61 MFDRV_ExtTextOut
, /* pExtTextOut */
62 MFDRV_FillPath
, /* pFillPath */
63 MFDRV_FillRgn
, /* pFillRgn */
64 MFDRV_FlattenPath
, /* pFlattenPath */
65 MFDRV_FrameRgn
, /* pFrameRgn */
66 NULL
, /* pGetBitmapBits */
67 NULL
, /* pGetCharWidth */
68 NULL
, /* pGetDCOrgEx */
69 NULL
, /* pGetDIBColorTable */
70 NULL
, /* pGetDIBits */
71 NULL
, /* pGetDeviceCaps */
72 NULL
, /* pGetDeviceGammaRamp */
74 NULL
, /* pGetPixelFormat */
75 NULL
, /* pGetTextExtentPoint */
76 NULL
, /* pGetTextMetrics */
77 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
78 MFDRV_InvertRgn
, /* pInvertRgn */
79 MFDRV_LineTo
, /* pLineTo */
80 MFDRV_MoveTo
, /* pMoveTo */
81 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
82 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
83 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
84 MFDRV_PaintRgn
, /* pPaintRgn */
85 MFDRV_PatBlt
, /* pPatBlt */
87 MFDRV_PolyBezier
, /* pPolyBezier */
88 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
90 MFDRV_PolyPolygon
, /* pPolyPolygon */
91 NULL
, /* pPolyPolyline */
92 MFDRV_Polygon
, /* pPolygon */
93 MFDRV_Polyline
, /* pPolyline */
94 NULL
, /* pPolylineTo */
95 NULL
, /* pRealizePalette */
96 MFDRV_Rectangle
, /* pRectangle */
98 MFDRV_RestoreDC
, /* pRestoreDC */
99 MFDRV_RoundRect
, /* pRoundRect */
100 MFDRV_SaveDC
, /* pSaveDC */
101 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
102 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
103 MFDRV_SelectBitmap
, /* pSelectBitmap */
104 MFDRV_SelectBrush
, /* pSelectBrush */
105 MFDRV_SelectClipPath
, /* pSelectClipPath */
106 MFDRV_SelectFont
, /* pSelectFont */
107 NULL
, /* pSelectPalette */
108 MFDRV_SelectPen
, /* pSelectPen */
109 NULL
, /* pSetBitmapBits */
110 MFDRV_SetBkColor
, /* pSetBkColor */
111 MFDRV_SetBkMode
, /* pSetBkMode */
112 NULL
, /* pSetDIBColorTable */
113 NULL
, /* pSetDIBits */
114 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
115 NULL
, /* pSetDeviceClipping */
116 NULL
, /* pSetDeviceGammaRamp */
117 MFDRV_SetMapMode
, /* pSetMapMode */
118 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
119 MFDRV_SetPixel
, /* pSetPixel */
120 NULL
, /* pSetPixelFormat */
121 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
122 MFDRV_SetROP2
, /* pSetROP2 */
123 MFDRV_SetRelAbs
, /* pSetRelAbs */
124 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
125 MFDRV_SetTextAlign
, /* pSetTextAlign */
126 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
127 MFDRV_SetTextColor
, /* pSetTextColor */
128 MFDRV_SetTextJustification
, /* pSetTextJustification */
129 MFDRV_SetViewportExt
, /* pSetViewportExt */
130 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
131 MFDRV_SetWindowExt
, /* pSetWindowExt */
132 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
133 NULL
, /* pStartDoc */
134 NULL
, /* pStartPage */
135 MFDRV_StretchBlt
, /* pStretchBlt */
136 MFDRV_StretchDIBits
, /* pStretchDIBits */
137 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
138 MFDRV_StrokePath
, /* pStrokePath */
139 NULL
, /* pSwapBuffers */
140 MFDRV_WidenPath
/* pWidenPath */
145 /**********************************************************************
146 * MFDRV_AllocMetaFile
148 static DC
*MFDRV_AllocMetaFile(void)
151 METAFILEDRV_PDEVICE
*physDev
;
153 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
))) return NULL
;
154 dc
->header
.wMagic
= METAFILE_DC_MAGIC
;
156 physDev
= (METAFILEDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
159 GDI_FreeObject( dc
->hSelf
, dc
);
162 dc
->physDev
= (PHYSDEV
)physDev
;
163 physDev
->hdc
= dc
->hSelf
;
166 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
168 HeapFree( GetProcessHeap(), 0, physDev
);
169 GDI_FreeObject( dc
->hSelf
, dc
);
173 physDev
->nextHandle
= 0;
176 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
177 physDev
->mh
->mtVersion
= 0x0300;
178 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
179 physDev
->mh
->mtNoObjects
= 0;
180 physDev
->mh
->mtMaxRecord
= 0;
181 physDev
->mh
->mtNoParameters
= 0;
187 /**********************************************************************
190 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
192 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
193 DC
*dc
= physDev
->dc
;
195 if (physDev
->mh
) HeapFree( GetProcessHeap(), 0, physDev
->mh
);
196 HeapFree( GetProcessHeap(), 0, physDev
);
198 GDI_FreeObject( dc
->hSelf
, dc
);
202 /**********************************************************************
203 * CreateMetaFile (GDI.125)
204 * CreateMetaFile16 (GDI32.@)
206 * Create a new DC and associate it with a metafile. Pass a filename
207 * to create a disk-based metafile, NULL to create a memory metafile.
210 * A handle to the metafile DC if successful, NULL on failure.
212 HDC16 WINAPI
CreateMetaFile16(
213 LPCSTR filename
/* [in] Filename of disk metafile */
218 METAFILEDRV_PDEVICE
*physDev
;
221 TRACE("'%s'\n", filename
);
223 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
224 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
226 if (filename
) /* disk based metafile */
228 physDev
->mh
->mtType
= METAFILE_DISK
;
229 if ((hFile
= CreateFileA(filename
, GENERIC_WRITE
, 0, NULL
,
230 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
231 MFDRV_DeleteDC( dc
->physDev
);
234 if (!WriteFile( hFile
, (LPSTR
)physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
236 MFDRV_DeleteDC( dc
->physDev
);
239 physDev
->hFile
= hFile
;
241 /* Grow METAHEADER to include filename */
242 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
);
244 else /* memory based metafile */
245 physDev
->mh
->mtType
= METAFILE_MEMORY
;
247 TRACE("returning %04x\n", dc
->hSelf
);
249 GDI_ReleaseObj( dc
->hSelf
);
253 /**********************************************************************
254 * CreateMetaFileA (GDI32.@)
256 HDC WINAPI
CreateMetaFileA(
257 LPCSTR filename
/* [in] Filename of disk metafile */
260 return CreateMetaFile16( filename
);
263 /**********************************************************************
264 * CreateMetaFileW (GDI32.@)
266 HDC WINAPI
CreateMetaFileW(LPCWSTR filename
)
272 len
= WideCharToMultiByte( CP_ACP
, 0, filename
, -1, NULL
, 0, NULL
, NULL
);
273 filenameA
= HeapAlloc( GetProcessHeap(), 0, len
);
274 WideCharToMultiByte( CP_ACP
, 0, filename
, -1, filenameA
, len
, NULL
, NULL
);
276 hReturnDC
= CreateMetaFileA(filenameA
);
278 HeapFree( GetProcessHeap(), 0, filenameA
);
284 /**********************************************************************
285 * MFDRV_CloseMetaFile
287 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
290 METAFILEDRV_PDEVICE
*physDev
;
292 TRACE("(%04x)\n", hdc
);
294 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
295 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
297 /* Construct the end of metafile record - this is documented
298 * in SDK Knowledgebase Q99334.
301 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
303 MFDRV_DeleteDC( dc
->physDev
);
307 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
309 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
310 MFDRV_DeleteDC( dc
->physDev
);
314 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
315 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
316 sizeof(*physDev
->mh
), NULL
, NULL
)) {
317 MFDRV_DeleteDC( dc
->physDev
);
320 CloseHandle(physDev
->hFile
);
321 physDev
->mh
->mtType
= METAFILE_DISK
;
327 /******************************************************************
328 * CloseMetaFile (GDI.126)
329 * CloseMetaFile16 (GDI32.@)
331 HMETAFILE16 WINAPI
CloseMetaFile16(
332 HDC16 hdc
/* [in] Metafile DC to close */
336 METAFILEDRV_PDEVICE
*physDev
;
337 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
339 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
341 /* Now allocate a global handle for the metafile */
343 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
345 physDev
->mh
= NULL
; /* So it won't be deleted */
346 MFDRV_DeleteDC( dc
->physDev
);
350 /******************************************************************
351 * CloseMetaFile (GDI32.@)
353 * Stop recording graphics operations in metafile associated with
354 * hdc and retrieve metafile.
357 * Handle of newly created metafile on success, NULL on failure.
359 HMETAFILE WINAPI
CloseMetaFile(
360 HDC hdc
/* [in] Metafile DC to close */
364 METAFILEDRV_PDEVICE
*physDev
;
365 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
367 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
369 /* Now allocate a global handle for the metafile */
371 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
373 physDev
->mh
= NULL
; /* So it won't be deleted */
374 MFDRV_DeleteDC( dc
->physDev
);
379 /******************************************************************
382 * Warning: this function can change the pointer to the metafile header.
384 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
388 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
390 switch(physDev
->mh
->mtType
)
392 case METAFILE_MEMORY
:
393 len
= physDev
->mh
->mtSize
* 2 + rlen
;
394 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, len
);
395 if (!mh
) return FALSE
;
397 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
400 TRACE("Writing record to disk\n");
401 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
405 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
409 physDev
->mh
->mtSize
+= rlen
/ 2;
410 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
415 /******************************************************************
419 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
422 METARECORD
*mr
= (METARECORD
*)&buffer
;
425 mr
->rdFunction
= func
;
426 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
430 /******************************************************************
433 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
436 METARECORD
*mr
= (METARECORD
*)&buffer
;
439 mr
->rdFunction
= func
;
440 *(mr
->rdParm
) = param1
;
441 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
445 /******************************************************************
448 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
451 METARECORD
*mr
= (METARECORD
*)&buffer
;
454 mr
->rdFunction
= func
;
455 *(mr
->rdParm
) = param2
;
456 *(mr
->rdParm
+ 1) = param1
;
457 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
461 /******************************************************************
465 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
466 short param3
, short param4
)
469 METARECORD
*mr
= (METARECORD
*)&buffer
;
472 mr
->rdFunction
= func
;
473 *(mr
->rdParm
) = param4
;
474 *(mr
->rdParm
+ 1) = param3
;
475 *(mr
->rdParm
+ 2) = param2
;
476 *(mr
->rdParm
+ 3) = param1
;
477 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
481 /******************************************************************
485 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
486 short param3
, short param4
, short param5
, short param6
)
489 METARECORD
*mr
= (METARECORD
*)&buffer
;
492 mr
->rdFunction
= func
;
493 *(mr
->rdParm
) = param6
;
494 *(mr
->rdParm
+ 1) = param5
;
495 *(mr
->rdParm
+ 2) = param4
;
496 *(mr
->rdParm
+ 3) = param3
;
497 *(mr
->rdParm
+ 4) = param2
;
498 *(mr
->rdParm
+ 5) = param1
;
499 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
503 /******************************************************************
506 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
507 short param3
, short param4
, short param5
,
508 short param6
, short param7
, short param8
)
511 METARECORD
*mr
= (METARECORD
*)&buffer
;
514 mr
->rdFunction
= func
;
515 *(mr
->rdParm
) = param8
;
516 *(mr
->rdParm
+ 1) = param7
;
517 *(mr
->rdParm
+ 2) = param6
;
518 *(mr
->rdParm
+ 3) = param5
;
519 *(mr
->rdParm
+ 4) = param4
;
520 *(mr
->rdParm
+ 5) = param3
;
521 *(mr
->rdParm
+ 6) = param2
;
522 *(mr
->rdParm
+ 7) = param1
;
523 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
527 /******************************************************************
530 * Note: this function assumes that we never delete objects.
531 * If we do someday, we'll need to maintain a table to re-use deleted
534 int MFDRV_AddHandleDC( PHYSDEV dev
)
536 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
537 physDev
->mh
->mtNoObjects
++;
538 return physDev
->nextHandle
++;
541 /**********************************************************************
544 INT
MFDRV_ExtEscape( PHYSDEV dev
, INT nEscape
, INT cbInput
, LPCVOID in_data
,
545 INT cbOutput
, LPVOID out_data
)
550 if(nEscape
== MFCOMMENT
) {
551 len
= sizeof(*mr
) + sizeof(WORD
) + ((cbInput
+ 1) & ~1);
552 mr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
553 mr
->rdSize
= len
/ 2;
554 mr
->rdFunction
= META_ESCAPE
;
555 mr
->rdParm
[0] = nEscape
;
556 mr
->rdParm
[1] = cbInput
;
557 memcpy(&(mr
->rdParm
[2]), in_data
, cbInput
);
558 MFDRV_WriteRecord( dev
, mr
, len
);
559 HeapFree(GetProcessHeap(), 0, mr
);