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
26 #include "wine/winbase16.h"
29 #include "gdi_private.h"
30 #include "mfdrv/metafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(metafile
);
35 static const DC_FUNCTIONS MFDRV_Funcs
=
38 MFDRV_AbortPath
, /* pAbortPath */
39 NULL
, /* pAlphaBlend */
43 MFDRV_BeginPath
, /* pBeginPath */
44 MFDRV_BitBlt
, /* pBitBlt */
45 NULL
, /* pChoosePixelFormat */
46 MFDRV_Chord
, /* pChord */
47 MFDRV_CloseFigure
, /* pCloseFigure */
48 NULL
, /* pCreateBitmap */
50 NULL
, /* pCreateDIBSection */
51 NULL
, /* pDeleteBitmap */
53 MFDRV_DeleteObject
, /* pDeleteObject */
54 NULL
, /* pDescribePixelFormat */
55 NULL
, /* pDeviceCapabilities */
56 MFDRV_Ellipse
, /* pEllipse */
59 MFDRV_EndPath
, /* pEndPath */
60 NULL
, /* pEnumDeviceFonts */
61 MFDRV_ExcludeClipRect
, /* pExcludeClipRect */
62 NULL
, /* pExtDeviceMode */
63 MFDRV_ExtEscape
, /* pExtEscape */
64 MFDRV_ExtFloodFill
, /* pExtFloodFill */
65 MFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
66 MFDRV_ExtTextOut
, /* pExtTextOut */
67 MFDRV_FillPath
, /* pFillPath */
68 MFDRV_FillRgn
, /* pFillRgn */
69 MFDRV_FlattenPath
, /* pFlattenPath */
70 MFDRV_FrameRgn
, /* pFrameRgn */
71 NULL
, /* pGdiComment */
72 NULL
, /* pGetBitmapBits */
73 NULL
, /* pGetCharWidth */
74 NULL
, /* pGetDCOrgEx */
75 NULL
, /* pGetDIBColorTable */
76 NULL
, /* pGetDIBits */
77 MFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
78 NULL
, /* pGetDeviceGammaRamp */
79 NULL
, /* pGetNearestColor */
81 NULL
, /* pGetPixelFormat */
82 NULL
, /* pGetSystemPaletteEntries */
83 NULL
, /* pGetTextExtentPoint */
84 NULL
, /* pGetTextMetrics */
85 MFDRV_IntersectClipRect
, /* pIntersectClipRect */
86 MFDRV_InvertRgn
, /* pInvertRgn */
87 MFDRV_LineTo
, /* pLineTo */
88 NULL
, /* pModifyWorldTransform */
89 MFDRV_MoveTo
, /* pMoveTo */
90 MFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
91 MFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
92 MFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
93 MFDRV_PaintRgn
, /* pPaintRgn */
94 MFDRV_PatBlt
, /* pPatBlt */
96 MFDRV_PolyBezier
, /* pPolyBezier */
97 MFDRV_PolyBezierTo
, /* pPolyBezierTo */
99 MFDRV_PolyPolygon
, /* pPolyPolygon */
100 NULL
, /* pPolyPolyline */
101 MFDRV_Polygon
, /* pPolygon */
102 MFDRV_Polyline
, /* pPolyline */
103 NULL
, /* pPolylineTo */
104 NULL
, /* pRealizeDefaultPalette */
105 MFDRV_RealizePalette
, /* pRealizePalette */
106 MFDRV_Rectangle
, /* pRectangle */
108 MFDRV_RestoreDC
, /* pRestoreDC */
109 MFDRV_RoundRect
, /* pRoundRect */
110 MFDRV_SaveDC
, /* pSaveDC */
111 MFDRV_ScaleViewportExt
, /* pScaleViewportExt */
112 MFDRV_ScaleWindowExt
, /* pScaleWindowExt */
113 MFDRV_SelectBitmap
, /* pSelectBitmap */
114 MFDRV_SelectBrush
, /* pSelectBrush */
115 MFDRV_SelectClipPath
, /* pSelectClipPath */
116 MFDRV_SelectFont
, /* pSelectFont */
117 MFDRV_SelectPalette
, /* pSelectPalette */
118 MFDRV_SelectPen
, /* pSelectPen */
119 NULL
, /* pSetArcDirection */
120 NULL
, /* pSetBitmapBits */
121 MFDRV_SetBkColor
, /* pSetBkColor */
122 MFDRV_SetBkMode
, /* pSetBkMode */
123 NULL
, /* pSetDCBrushColor*/
124 NULL
, /* pSetDCOrg */
125 NULL
, /* pSetDCPenColor*/
126 NULL
, /* pSetDIBColorTable */
127 NULL
, /* pSetDIBits */
128 MFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
129 NULL
, /* pSetDeviceClipping */
130 NULL
, /* pSetDeviceGammaRamp */
131 MFDRV_SetMapMode
, /* pSetMapMode */
132 MFDRV_SetMapperFlags
, /* pSetMapperFlags */
133 MFDRV_SetPixel
, /* pSetPixel */
134 NULL
, /* pSetPixelFormat */
135 MFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
136 MFDRV_SetROP2
, /* pSetROP2 */
137 MFDRV_SetRelAbs
, /* pSetRelAbs */
138 MFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
139 MFDRV_SetTextAlign
, /* pSetTextAlign */
140 MFDRV_SetTextCharacterExtra
, /* pSetTextCharacterExtra */
141 MFDRV_SetTextColor
, /* pSetTextColor */
142 MFDRV_SetTextJustification
, /* pSetTextJustification */
143 MFDRV_SetViewportExt
, /* pSetViewportExt */
144 MFDRV_SetViewportOrg
, /* pSetViewportOrg */
145 MFDRV_SetWindowExt
, /* pSetWindowExt */
146 MFDRV_SetWindowOrg
, /* pSetWindowOrg */
147 NULL
, /* pSetWorldTransform */
148 NULL
, /* pStartDoc */
149 NULL
, /* pStartPage */
150 MFDRV_StretchBlt
, /* pStretchBlt */
151 MFDRV_StretchDIBits
, /* pStretchDIBits */
152 MFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
153 MFDRV_StrokePath
, /* pStrokePath */
154 NULL
, /* pSwapBuffers */
155 MFDRV_WidenPath
/* pWidenPath */
160 /**********************************************************************
161 * MFDRV_AllocMetaFile
163 static DC
*MFDRV_AllocMetaFile(void)
166 METAFILEDRV_PDEVICE
*physDev
;
168 if (!(dc
= DC_AllocDC( &MFDRV_Funcs
, METAFILE_DC_MAGIC
))) return NULL
;
170 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
173 GDI_FreeObject( dc
->hSelf
, dc
);
176 dc
->physDev
= (PHYSDEV
)physDev
;
177 physDev
->hdc
= dc
->hSelf
;
180 if (!(physDev
->mh
= HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev
->mh
) )))
182 HeapFree( GetProcessHeap(), 0, physDev
);
183 GDI_FreeObject( dc
->hSelf
, dc
);
187 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
188 physDev
->handles_size
= HANDLE_LIST_INC
;
189 physDev
->cur_handles
= 0;
193 physDev
->mh
->mtHeaderSize
= sizeof(METAHEADER
) / sizeof(WORD
);
194 physDev
->mh
->mtVersion
= 0x0300;
195 physDev
->mh
->mtSize
= physDev
->mh
->mtHeaderSize
;
196 physDev
->mh
->mtNoObjects
= 0;
197 physDev
->mh
->mtMaxRecord
= 0;
198 physDev
->mh
->mtNoParameters
= 0;
204 /**********************************************************************
207 static BOOL
MFDRV_DeleteDC( PHYSDEV dev
)
209 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
210 DC
*dc
= physDev
->dc
;
213 HeapFree( GetProcessHeap(), 0, physDev
->mh
);
214 for(index
= 0; index
< physDev
->handles_size
; index
++)
215 if(physDev
->handles
[index
])
216 GDI_hdc_not_using_object(physDev
->handles
[index
], physDev
->hdc
);
217 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
218 HeapFree( GetProcessHeap(), 0, physDev
);
220 GDI_FreeObject( dc
->hSelf
, dc
);
225 /**********************************************************************
226 * CreateMetaFileW (GDI32.@)
228 * Create a new DC and associate it with a metafile. Pass a filename
229 * to create a disk-based metafile, NULL to create a memory metafile.
232 * filename [I] Filename of disk metafile
235 * A handle to the metafile DC if successful, NULL on failure.
237 HDC WINAPI
CreateMetaFileW( LPCWSTR filename
)
241 METAFILEDRV_PDEVICE
*physDev
;
244 TRACE("'%s'\n", debugstr_w(filename
) );
246 if (!(dc
= MFDRV_AllocMetaFile())) return 0;
247 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
249 if (filename
) /* disk based metafile */
251 physDev
->mh
->mtType
= METAFILE_DISK
;
252 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
, 0, NULL
,
253 CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
254 MFDRV_DeleteDC( dc
->physDev
);
257 if (!WriteFile( hFile
, physDev
->mh
, sizeof(*physDev
->mh
), NULL
,
259 MFDRV_DeleteDC( dc
->physDev
);
262 physDev
->hFile
= hFile
;
264 /* Grow METAHEADER to include filename */
265 physDev
->mh
= MF_CreateMetaHeaderDisk(physDev
->mh
, filename
, TRUE
);
267 else /* memory based metafile */
268 physDev
->mh
->mtType
= METAFILE_MEMORY
;
270 TRACE("returning %p\n", dc
->hSelf
);
272 GDI_ReleaseObj( dc
->hSelf
);
276 /**********************************************************************
277 * CreateMetaFileA (GDI32.@)
279 HDC WINAPI
CreateMetaFileA(LPCSTR filename
)
285 if (!filename
) return CreateMetaFileW(NULL
);
287 len
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
288 filenameW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
289 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, len
);
291 hReturnDC
= CreateMetaFileW(filenameW
);
293 HeapFree( GetProcessHeap(), 0, filenameW
);
299 /**********************************************************************
300 * MFDRV_CloseMetaFile
302 static DC
*MFDRV_CloseMetaFile( HDC hdc
)
305 METAFILEDRV_PDEVICE
*physDev
;
307 TRACE("(%p)\n", hdc
);
309 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
))) return 0;
310 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
312 /* Construct the end of metafile record - this is documented
313 * in SDK Knowledgebase Q99334.
316 if (!MFDRV_MetaParam0(dc
->physDev
, META_EOF
))
318 MFDRV_DeleteDC( dc
->physDev
);
322 if (physDev
->mh
->mtType
== METAFILE_DISK
) /* disk based metafile */
324 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
325 MFDRV_DeleteDC( dc
->physDev
);
329 physDev
->mh
->mtType
= METAFILE_MEMORY
; /* This is what windows does */
330 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->mh
,
331 sizeof(*physDev
->mh
), NULL
, NULL
)) {
332 MFDRV_DeleteDC( dc
->physDev
);
335 CloseHandle(physDev
->hFile
);
336 physDev
->mh
->mtType
= METAFILE_DISK
;
342 /******************************************************************
343 * CloseMetaFile (GDI.126)
346 * hdc [I] Metafile DC to close
348 HMETAFILE16 WINAPI
CloseMetaFile16(HDC16 hdc
)
351 METAFILEDRV_PDEVICE
*physDev
;
352 DC
*dc
= MFDRV_CloseMetaFile(HDC_32(hdc
));
354 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
356 /* Now allocate a global handle for the metafile */
358 hmf
= MF_Create_HMETAFILE16( physDev
->mh
);
360 physDev
->mh
= NULL
; /* So it won't be deleted */
361 MFDRV_DeleteDC( dc
->physDev
);
365 /******************************************************************
366 * CloseMetaFile (GDI32.@)
368 * Stop recording graphics operations in metafile associated with
369 * hdc and retrieve metafile.
372 * hdc [I] Metafile DC to close
375 * Handle of newly created metafile on success, NULL on failure.
377 HMETAFILE WINAPI
CloseMetaFile(HDC hdc
)
380 METAFILEDRV_PDEVICE
*physDev
;
381 DC
*dc
= MFDRV_CloseMetaFile(hdc
);
383 physDev
= (METAFILEDRV_PDEVICE
*)dc
->physDev
;
385 /* Now allocate a global handle for the metafile */
387 hmf
= MF_Create_HMETAFILE( physDev
->mh
);
389 physDev
->mh
= NULL
; /* So it won't be deleted */
390 MFDRV_DeleteDC( dc
->physDev
);
395 /******************************************************************
398 * Warning: this function can change the pointer to the metafile header.
400 BOOL
MFDRV_WriteRecord( PHYSDEV dev
, METARECORD
*mr
, DWORD rlen
)
404 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
406 switch(physDev
->mh
->mtType
)
408 case METAFILE_MEMORY
:
409 len
= physDev
->mh
->mtSize
* 2 + rlen
;
410 /* reallocate memory if needed */
411 size
= HeapSize( GetProcessHeap(), 0, physDev
->mh
);
415 size
+= size
/ 2 + rlen
;
416 mh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->mh
, size
);
417 if (!mh
) return FALSE
;
419 TRACE("Reallocated metafile: new size is %ld\n",size
);
421 memcpy((WORD
*)physDev
->mh
+ physDev
->mh
->mtSize
, mr
, rlen
);
424 TRACE("Writing record to disk\n");
425 if (!WriteFile(physDev
->hFile
, (char *)mr
, rlen
, NULL
, NULL
))
429 ERR("Unknown metafile type %d\n", physDev
->mh
->mtType
);
433 physDev
->mh
->mtSize
+= rlen
/ 2;
434 physDev
->mh
->mtMaxRecord
= max(physDev
->mh
->mtMaxRecord
, rlen
/ 2);
439 /******************************************************************
443 BOOL
MFDRV_MetaParam0(PHYSDEV dev
, short func
)
446 METARECORD
*mr
= (METARECORD
*)&buffer
;
449 mr
->rdFunction
= func
;
450 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
454 /******************************************************************
457 BOOL
MFDRV_MetaParam1(PHYSDEV dev
, short func
, short param1
)
460 METARECORD
*mr
= (METARECORD
*)&buffer
;
463 mr
->rdFunction
= func
;
464 *(mr
->rdParm
) = param1
;
465 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
469 /******************************************************************
472 BOOL
MFDRV_MetaParam2(PHYSDEV dev
, short func
, short param1
, short param2
)
475 METARECORD
*mr
= (METARECORD
*)&buffer
;
478 mr
->rdFunction
= func
;
479 *(mr
->rdParm
) = param2
;
480 *(mr
->rdParm
+ 1) = param1
;
481 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
485 /******************************************************************
489 BOOL
MFDRV_MetaParam4(PHYSDEV dev
, short func
, short param1
, short param2
,
490 short param3
, short param4
)
493 METARECORD
*mr
= (METARECORD
*)&buffer
;
496 mr
->rdFunction
= func
;
497 *(mr
->rdParm
) = param4
;
498 *(mr
->rdParm
+ 1) = param3
;
499 *(mr
->rdParm
+ 2) = param2
;
500 *(mr
->rdParm
+ 3) = param1
;
501 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
505 /******************************************************************
509 BOOL
MFDRV_MetaParam6(PHYSDEV dev
, short func
, short param1
, short param2
,
510 short param3
, short param4
, short param5
, short param6
)
513 METARECORD
*mr
= (METARECORD
*)&buffer
;
516 mr
->rdFunction
= func
;
517 *(mr
->rdParm
) = param6
;
518 *(mr
->rdParm
+ 1) = param5
;
519 *(mr
->rdParm
+ 2) = param4
;
520 *(mr
->rdParm
+ 3) = param3
;
521 *(mr
->rdParm
+ 4) = param2
;
522 *(mr
->rdParm
+ 5) = param1
;
523 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
527 /******************************************************************
530 BOOL
MFDRV_MetaParam8(PHYSDEV dev
, short func
, short param1
, short param2
,
531 short param3
, short param4
, short param5
,
532 short param6
, short param7
, short param8
)
535 METARECORD
*mr
= (METARECORD
*)&buffer
;
538 mr
->rdFunction
= func
;
539 *(mr
->rdParm
) = param8
;
540 *(mr
->rdParm
+ 1) = param7
;
541 *(mr
->rdParm
+ 2) = param6
;
542 *(mr
->rdParm
+ 3) = param5
;
543 *(mr
->rdParm
+ 4) = param4
;
544 *(mr
->rdParm
+ 5) = param3
;
545 *(mr
->rdParm
+ 6) = param2
;
546 *(mr
->rdParm
+ 7) = param1
;
547 return MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2);
551 /**********************************************************************
554 INT
MFDRV_ExtEscape( PHYSDEV dev
, INT nEscape
, INT cbInput
, LPCVOID in_data
,
555 INT cbOutput
, LPVOID out_data
)
561 len
= sizeof(*mr
) + sizeof(WORD
) + ((cbInput
+ 1) & ~1);
562 mr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
563 mr
->rdSize
= len
/ 2;
564 mr
->rdFunction
= META_ESCAPE
;
565 mr
->rdParm
[0] = nEscape
;
566 mr
->rdParm
[1] = cbInput
;
567 memcpy(&(mr
->rdParm
[2]), in_data
, cbInput
);
568 ret
= MFDRV_WriteRecord( dev
, mr
, len
);
569 HeapFree(GetProcessHeap(), 0, mr
);
574 /******************************************************************
575 * MFDRV_GetDeviceCaps
577 *A very simple implementation that returns DT_METAFILE
579 INT
MFDRV_GetDeviceCaps(PHYSDEV dev
, INT cap
)
588 TRACE(" unsupported capability %d, will return 0\n", cap
);