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
, /* pBitmapBits */
42 NULL
, /* pChoosePixelFormat */
43 MFDRV_Chord
, /* pChord */
44 MFDRV_CloseFigure
, /* pCloseFigure */
45 NULL
, /* pCreateBitmap */
46 NULL
, /* no implementation */ /* pCreateDC */
47 NULL
, /* pCreateDIBSection */
48 NULL
, /* no implementation */ /* pDeleteDC */
49 NULL
, /* pDeleteObject */
50 NULL
, /* pDescribePixelFormat */
51 NULL
, /* pDeviceCapabilities */
52 MFDRV_Ellipse
, /* pEllipse */
55 MFDRV_EndPath
, /* pEndPath */
56 NULL
, /* pEnumDeviceFonts */
57 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
58 NULL
, /* pExtDeviceMode */
59 NULL
, /* pExtEscape */
60 MFDRV_ExtFloodFill
, /* pExtFloodFill */
61 MFDRV_ExtTextOut
, /* pExtTextOut */
62 MFDRV_FillPath
, /* pFillPath */
63 MFDRV_FillRgn
, /* pFillRgn */
64 MFDRV_FlattenPath
, /* pFlattenPath */
65 MFDRV_FrameRgn
, /* pFrameRgn */
66 NULL
, /* pGetCharWidth */
67 NULL
, /* pGetDCOrgEx */
68 NULL
, /* pGetDeviceCaps */
69 NULL
, /* pGetDeviceGammaRamp */
70 NULL
, /* no implementation */ /* pGetPixel */
71 NULL
, /* pGetPixelFormat */
72 NULL
, /* pGetTextExtentPoint */
73 NULL
, /* pGetTextMetrics */
74 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
75 MFDRV_InvertRgn
, /* pInvertRgn */
76 MFDRV_LineTo
, /* pLineTo */
77 MFDRV_MoveTo
, /* pMoveTo */
78 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
79 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
80 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
81 MFDRV_PaintRgn
, /* pPaintRgn */
82 MFDRV_PatBlt
, /* pPatBlt */
84 MFDRV_PolyBezier
, /* pPolyBezier */
85 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
87 MFDRV_PolyPolygon
, /* pPolyPolygon */
88 NULL
, /* pPolyPolyline */
89 MFDRV_Polygon
, /* pPolygon */
90 MFDRV_Polyline
, /* pPolyline */
91 NULL
, /* pPolylineTo */
92 NULL
, /* pRealizePalette */
93 MFDRV_Rectangle
, /* pRectangle */
94 MFDRV_RestoreDC
, /* pRestoreDC */
95 MFDRV_RoundRect
, /* pRoundRect */
96 MFDRV_SaveDC
, /* pSaveDC */
97 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
98 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
99 MFDRV_SelectClipPath
, /* pSelectClipPath */
100 NULL
, /* pSelectClipRgn */
101 MFDRV_SelectObject
, /* pSelectObject */
102 NULL
, /* pSelectPalette */
103 MFDRV_SetBkColor
, /* pSetBkColor */
104 MFDRV_SetBkMode
, /* pSetBkMode */
105 NULL
, /* pSetDeviceClipping */
106 NULL
, /* pSetDeviceGammaRamp */
107 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
108 MFDRV_SetMapMode
, /* pSetMapMode */
109 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
110 MFDRV_SetPixel
, /* pSetPixel */
111 NULL
, /* pSetPixelFormat */
112 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
113 MFDRV_SetROP2
, /* pSetROP2 */
114 MFDRV_SetRelAbs
, /* pSetRelAbs */
115 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
116 MFDRV_SetTextAlign
, /* pSetTextAlign */
117 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
118 MFDRV_SetTextColor
, /* pSetTextColor */
119 MFDRV_SetTextJustification
, /* pSetTextJustification */
120 MFDRV_SetViewportExt
, /* pSetViewportExt */
121 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
122 MFDRV_SetWindowExt
, /* pSetWindowExt */
123 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
124 NULL
, /* pStartDoc */
125 NULL
, /* pStartPage */
126 MFDRV_StretchBlt
, /* pStretchBlt */
127 MFDRV_StretchDIBits
, /* pStretchDIBits */
128 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
129 MFDRV_StrokePath
, /* pStrokePath */
130 NULL
, /* pSwapBuffers */
131 MFDRV_WidenPath
/* pWidenPath */
136 /**********************************************************************
137 * MFDRV_AllocMetaFile
139 static DC
*MFDRV_AllocMetaFile(void)
142 METAFILEDRV_PDEVICE
*physDev
;
144 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
))) return NULL
;
145 dc
->header
.wMagic
= METAFILE_DC_MAGIC
;
147 physDev
= (METAFILEDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
150 GDI_FreeObject( dc
->hSelf
, dc
);
153 dc
->physDev
= physDev
;
155 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
157 HeapFree( GetProcessHeap(), 0, physDev
);
158 GDI_FreeObject( dc
->hSelf
, dc
);
162 physDev
->nextHandle
= 0;
165 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
166 physDev
->mh
->mtVersion
= 0x0300;
167 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
168 physDev
->mh
->mtNoObjects
= 0;
169 physDev
->mh
->mtMaxRecord
= 0;
170 physDev
->mh
->mtNoParameters
= 0;
176 /**********************************************************************
179 static BOOL
MFDRV_DeleteDC( DC
*dc
)
181 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
183 if (physDev
->mh
) HeapFree( GetProcessHeap(), 0, physDev
->mh
);
184 HeapFree( GetProcessHeap(), 0, physDev
);
186 GDI_FreeObject( dc
->hSelf
, dc
);
190 /**********************************************************************
191 * CreateMetaFile (GDI.125)
192 * CreateMetaFile16 (GDI32.@)
194 * Create a new DC and associate it with a metafile. Pass a filename
195 * to create a disk-based metafile, NULL to create a memory metafile.
198 * A handle to the metafile DC if successful, NULL on failure.
200 HDC16 WINAPI
CreateMetaFile16(
201 LPCSTR filename
/* [in] Filename of disk metafile */
206 METAFILEDRV_PDEVICE
*physDev
;
209 TRACE("'%s'\n", filename
);
211 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
212 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
214 if (filename
) /* disk based metafile */
216 physDev
->mh
->mtType
= METAFILE_DISK
;
217 if ((hFile
= CreateFileA(filename
, GENERIC_WRITE
, 0, NULL
,
218 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
219 MFDRV_DeleteDC( dc
);
222 if (!WriteFile( hFile
, (LPSTR
)physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
224 MFDRV_DeleteDC( dc
);
227 physDev
->hFile
= hFile
;
229 /* Grow METAHEADER to include filename */
230 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
);
232 else /* memory based metafile */
233 physDev
->mh
->mtType
= METAFILE_MEMORY
;
235 TRACE("returning %04x\n", dc
->hSelf
);
237 GDI_ReleaseObj( dc
->hSelf
);
241 /**********************************************************************
242 * CreateMetaFileA (GDI32.@)
244 HDC WINAPI
CreateMetaFileA(
245 LPCSTR filename
/* [in] Filename of disk metafile */
248 return CreateMetaFile16( filename
);
251 /**********************************************************************
252 * CreateMetaFileW (GDI32.@)
254 HDC WINAPI
CreateMetaFileW(LPCWSTR filename
)
260 len
= WideCharToMultiByte( CP_ACP
, 0, filename
, -1, NULL
, 0, NULL
, NULL
);
261 filenameA
= HeapAlloc( GetProcessHeap(), 0, len
);
262 WideCharToMultiByte( CP_ACP
, 0, filename
, -1, filenameA
, len
, NULL
, NULL
);
264 hReturnDC
= CreateMetaFileA(filenameA
);
266 HeapFree( GetProcessHeap(), 0, filenameA
);
272 /**********************************************************************
273 * MFDRV_CloseMetaFile
275 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
278 METAFILEDRV_PDEVICE
*physDev
;
280 TRACE("(%04x)\n", hdc
);
282 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
283 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
285 /* Construct the end of metafile record - this is documented
286 * in SDK Knowledgebase Q99334.
289 if (!MFDRV_MetaParam0(dc
, META_EOF
))
291 MFDRV_DeleteDC( dc
);
295 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
297 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
298 MFDRV_DeleteDC( dc
);
302 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
303 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
304 sizeof(*physDev
->mh
), NULL
, NULL
)) {
305 MFDRV_DeleteDC( dc
);
308 CloseHandle(physDev
->hFile
);
309 physDev
->mh
->mtType
= METAFILE_DISK
;
315 /******************************************************************
316 * CloseMetaFile (GDI.126)
317 * CloseMetaFile16 (GDI32.@)
319 HMETAFILE16 WINAPI
CloseMetaFile16(
320 HDC16 hdc
/* [in] Metafile DC to close */
324 METAFILEDRV_PDEVICE
*physDev
;
325 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
327 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
329 /* Now allocate a global handle for the metafile */
331 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
333 physDev
->mh
= NULL
; /* So it won't be deleted */
334 MFDRV_DeleteDC( dc
);
338 /******************************************************************
339 * CloseMetaFile (GDI32.@)
341 * Stop recording graphics operations in metafile associated with
342 * hdc and retrieve metafile.
345 * Handle of newly created metafile on success, NULL on failure.
347 HMETAFILE WINAPI
CloseMetaFile(
348 HDC hdc
/* [in] Metafile DC to close */
352 METAFILEDRV_PDEVICE
*physDev
;
353 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
355 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
357 /* Now allocate a global handle for the metafile */
359 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
361 physDev
->mh
= NULL
; /* So it won't be deleted */
362 MFDRV_DeleteDC( dc
);
367 /******************************************************************
370 * Warning: this function can change the pointer to the metafile header.
372 BOOL
MFDRV_WriteRecord( DC
*dc
, METARECORD
*mr
, DWORD rlen
)
376 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
378 switch(physDev
->mh
->mtType
)
380 case METAFILE_MEMORY
:
381 len
= physDev
->mh
->mtSize
* 2 + rlen
;
382 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, len
);
383 if (!mh
) return FALSE
;
385 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
388 TRACE("Writing record to disk\n");
389 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
393 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
397 physDev
->mh
->mtSize
+= rlen
/ 2;
398 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
403 /******************************************************************
407 BOOL
MFDRV_MetaParam0(DC
*dc
, short func
)
410 METARECORD
*mr
= (METARECORD
*)&buffer
;
413 mr
->rdFunction
= func
;
414 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
418 /******************************************************************
421 BOOL
MFDRV_MetaParam1(DC
*dc
, short func
, short param1
)
424 METARECORD
*mr
= (METARECORD
*)&buffer
;
427 mr
->rdFunction
= func
;
428 *(mr
->rdParm
) = param1
;
429 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
433 /******************************************************************
436 BOOL
MFDRV_MetaParam2(DC
*dc
, short func
, short param1
, short param2
)
439 METARECORD
*mr
= (METARECORD
*)&buffer
;
442 mr
->rdFunction
= func
;
443 *(mr
->rdParm
) = param2
;
444 *(mr
->rdParm
+ 1) = param1
;
445 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
449 /******************************************************************
453 BOOL
MFDRV_MetaParam4(DC
*dc
, short func
, short param1
, short param2
,
454 short param3
, short param4
)
457 METARECORD
*mr
= (METARECORD
*)&buffer
;
460 mr
->rdFunction
= func
;
461 *(mr
->rdParm
) = param4
;
462 *(mr
->rdParm
+ 1) = param3
;
463 *(mr
->rdParm
+ 2) = param2
;
464 *(mr
->rdParm
+ 3) = param1
;
465 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
469 /******************************************************************
473 BOOL
MFDRV_MetaParam6(DC
*dc
, short func
, short param1
, short param2
,
474 short param3
, short param4
, short param5
, short param6
)
477 METARECORD
*mr
= (METARECORD
*)&buffer
;
480 mr
->rdFunction
= func
;
481 *(mr
->rdParm
) = param6
;
482 *(mr
->rdParm
+ 1) = param5
;
483 *(mr
->rdParm
+ 2) = param4
;
484 *(mr
->rdParm
+ 3) = param3
;
485 *(mr
->rdParm
+ 4) = param2
;
486 *(mr
->rdParm
+ 5) = param1
;
487 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
491 /******************************************************************
494 BOOL
MFDRV_MetaParam8(DC
*dc
, short func
, short param1
, short param2
,
495 short param3
, short param4
, short param5
,
496 short param6
, short param7
, short param8
)
499 METARECORD
*mr
= (METARECORD
*)&buffer
;
502 mr
->rdFunction
= func
;
503 *(mr
->rdParm
) = param8
;
504 *(mr
->rdParm
+ 1) = param7
;
505 *(mr
->rdParm
+ 2) = param6
;
506 *(mr
->rdParm
+ 3) = param5
;
507 *(mr
->rdParm
+ 4) = param4
;
508 *(mr
->rdParm
+ 5) = param3
;
509 *(mr
->rdParm
+ 6) = param2
;
510 *(mr
->rdParm
+ 7) = param1
;
511 return MFDRV_WriteRecord( dc
, mr
, mr
->rdSize
* 2);
515 /******************************************************************
518 * Note: this function assumes that we never delete objects.
519 * If we do someday, we'll need to maintain a table to re-use deleted
522 int MFDRV_AddHandleDC( DC
*dc
)
524 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
525 physDev
->mh
->mtNoObjects
++;
526 return physDev
->nextHandle
++;