Display thread id instead of %fs in relay trace.
[wine.git] / graphics / metafiledrv / init.c
blobb325f8d61b476c4b05667d9e6ec1428dac857ca4
1 /*
2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "windef.h"
8 #include "wine/winbase16.h"
9 #include "gdi.h"
10 #include "heap.h"
11 #include "metafile.h"
12 #include "metafiledrv.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(metafile);
17 #include <string.h>
19 static const DC_FUNCTIONS MFDRV_Funcs =
21 NULL, /* pAbortDoc */
22 MFDRV_AbortPath, /* pAbortPath */
23 NULL, /* pAngleArc */
24 MFDRV_Arc, /* pArc */
25 NULL, /* pArcTo */
26 MFDRV_BeginPath, /* pBeginPath */
27 MFDRV_BitBlt, /* pBitBlt */
28 NULL, /* pBitmapBits */
29 NULL, /* pChoosePixelFormat */
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, /* pDescribePixelFormat */
39 NULL, /* pDeviceCapabilities */
40 MFDRV_Ellipse, /* pEllipse */
41 NULL, /* pEndDoc */
42 NULL, /* pEndPage */
43 MFDRV_EndPath, /* pEndPath */
44 NULL, /* pEnumDeviceFonts */
45 NULL, /* pEscape */
46 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
47 NULL, /* pExtDeviceMode */
48 MFDRV_ExtFloodFill, /* pExtFloodFill */
49 MFDRV_ExtTextOut, /* pExtTextOut */
50 MFDRV_FillPath, /* pFillPath */
51 MFDRV_FillRgn, /* pFillRgn */
52 MFDRV_FlattenPath, /* pFlattenPath */
53 MFDRV_FrameRgn, /* pFrameRgn */
54 NULL, /* pGetCharWidth */
55 NULL, /* pGetDCOrgEx */
56 NULL, /* pGetDeviceGammaRamp */
57 NULL, /* no implementation */ /* pGetPixel */
58 NULL, /* pGetPixelFormat */
59 NULL, /* pGetTextExtentPoint */
60 NULL, /* pGetTextMetrics */
61 MFDRV_IntersectClipRect, /* pIntersectClipRect */
62 MFDRV_InvertRgn, /* pInvertRgn */
63 MFDRV_LineTo, /* pLineTo */
64 MFDRV_MoveTo, /* pMoveTo */
65 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
66 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
67 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
68 MFDRV_PaintRgn, /* pPaintRgn */
69 MFDRV_PatBlt, /* pPatBlt */
70 MFDRV_Pie, /* pPie */
71 MFDRV_PolyBezier, /* pPolyBezier */
72 MFDRV_PolyBezierTo, /* pPolyBezierTo */
73 NULL, /* pPolyDraw */
74 MFDRV_PolyPolygon, /* pPolyPolygon */
75 NULL, /* pPolyPolyline */
76 MFDRV_Polygon, /* pPolygon */
77 MFDRV_Polyline, /* pPolyline */
78 NULL, /* pPolylineTo */
79 NULL, /* pRealizePalette */
80 MFDRV_Rectangle, /* pRectangle */
81 MFDRV_RestoreDC, /* pRestoreDC */
82 MFDRV_RoundRect, /* pRoundRect */
83 MFDRV_SaveDC, /* pSaveDC */
84 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
85 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
86 MFDRV_SelectClipPath, /* pSelectClipPath */
87 NULL, /* pSelectClipRgn */
88 MFDRV_SelectObject, /* pSelectObject */
89 NULL, /* pSelectPalette */
90 MFDRV_SetBkColor, /* pSetBkColor */
91 MFDRV_SetBkMode, /* pSetBkMode */
92 NULL, /* pSetDeviceClipping */
93 NULL, /* pSetDeviceGammaRamp */
94 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
95 MFDRV_SetMapMode, /* pSetMapMode */
96 MFDRV_SetMapperFlags, /* pSetMapperFlags */
97 MFDRV_SetPixel, /* pSetPixel */
98 NULL, /* pSetPixelFormat */
99 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
100 MFDRV_SetROP2, /* pSetROP2 */
101 MFDRV_SetRelAbs, /* pSetRelAbs */
102 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
103 MFDRV_SetTextAlign, /* pSetTextAlign */
104 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
105 MFDRV_SetTextColor, /* pSetTextColor */
106 MFDRV_SetTextJustification, /* pSetTextJustification */
107 MFDRV_SetViewportExt, /* pSetViewportExt */
108 MFDRV_SetViewportOrg, /* pSetViewportOrg */
109 MFDRV_SetWindowExt, /* pSetWindowExt */
110 MFDRV_SetWindowOrg, /* pSetWindowOrg */
111 NULL, /* pStartDoc */
112 NULL, /* pStartPage */
113 MFDRV_StretchBlt, /* pStretchBlt */
114 MFDRV_StretchDIBits, /* pStretchDIBits */
115 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
116 MFDRV_StrokePath, /* pStrokePath */
117 NULL, /* pSwapBuffers */
118 MFDRV_WidenPath /* pWidenPath */
123 /**********************************************************************
124 * MFDRV_AllocMetaFile
126 static DC *MFDRV_AllocMetaFile(void)
128 DC *dc;
129 METAFILEDRV_PDEVICE *physDev;
131 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
132 dc->header.wMagic = METAFILE_DC_MAGIC;
134 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
135 if (!physDev)
137 GDI_FreeObject( dc->hSelf, dc );
138 return NULL;
140 dc->physDev = physDev;
142 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
144 HeapFree( GetProcessHeap(), 0, physDev );
145 GDI_FreeObject( dc->hSelf, dc );
146 return NULL;
149 physDev->nextHandle = 0;
150 physDev->hFile = 0;
152 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
153 physDev->mh->mtVersion = 0x0300;
154 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
155 physDev->mh->mtNoObjects = 0;
156 physDev->mh->mtMaxRecord = 0;
157 physDev->mh->mtNoParameters = 0;
159 return dc;
163 /**********************************************************************
164 * MFDRV_DeleteDC
166 static BOOL MFDRV_DeleteDC( DC *dc )
168 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
170 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
171 HeapFree( GetProcessHeap(), 0, physDev );
172 dc->physDev = NULL;
173 GDI_FreeObject( dc->hSelf, dc );
174 return TRUE;
177 /**********************************************************************
178 * CreateMetaFile16 (GDI.125)
179 * CreateMetaFile16 (GDI32.@)
181 * Create a new DC and associate it with a metafile. Pass a filename
182 * to create a disk-based metafile, NULL to create a memory metafile.
184 * RETURNS
185 * A handle to the metafile DC if successful, NULL on failure.
187 HDC16 WINAPI CreateMetaFile16(
188 LPCSTR filename /* [in] Filename of disk metafile */
191 HDC ret;
192 DC *dc;
193 METAFILEDRV_PDEVICE *physDev;
194 HFILE hFile;
196 TRACE("'%s'\n", filename );
198 if (!(dc = MFDRV_AllocMetaFile())) return 0;
199 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
201 if (filename) /* disk based metafile */
203 physDev->mh->mtType = METAFILE_DISK;
204 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
205 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
206 MFDRV_DeleteDC( dc );
207 return 0;
209 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
210 NULL )) {
211 MFDRV_DeleteDC( dc );
212 return 0;
214 physDev->hFile = hFile;
216 /* Grow METAHEADER to include filename */
217 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
219 else /* memory based metafile */
220 physDev->mh->mtType = METAFILE_MEMORY;
222 TRACE("returning %04x\n", dc->hSelf);
223 ret = dc->hSelf;
224 GDI_ReleaseObj( dc->hSelf );
225 return ret;
228 /**********************************************************************
229 * CreateMetaFileA (GDI32.@)
231 HDC WINAPI CreateMetaFileA(
232 LPCSTR filename /* [in] Filename of disk metafile */
235 return CreateMetaFile16( filename );
238 /**********************************************************************
239 * CreateMetaFileW (GDI32.@)
241 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
243 LPSTR filenameA;
244 HDC hReturnDC;
246 filenameA = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
248 hReturnDC = CreateMetaFileA(filenameA);
250 HeapFree( GetProcessHeap(), 0, filenameA );
252 return hReturnDC;
256 /**********************************************************************
257 * MFDRV_CloseMetaFile
259 static DC *MFDRV_CloseMetaFile( HDC hdc )
261 DC *dc;
262 METAFILEDRV_PDEVICE *physDev;
264 TRACE("(%04x)\n", hdc );
266 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
267 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
269 /* Construct the end of metafile record - this is documented
270 * in SDK Knowledgebase Q99334.
273 if (!MFDRV_MetaParam0(dc, META_EOF))
275 MFDRV_DeleteDC( dc );
276 return 0;
279 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
281 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
282 MFDRV_DeleteDC( dc );
283 return 0;
286 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
287 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
288 sizeof(*physDev->mh), NULL, NULL)) {
289 MFDRV_DeleteDC( dc );
290 return 0;
292 CloseHandle(physDev->hFile);
293 physDev->mh->mtType = METAFILE_DISK;
296 return dc;
299 /******************************************************************
300 * CloseMetaFile16 (GDI.126)
301 * CloseMetaFile16 (GDI32.@)
303 HMETAFILE16 WINAPI CloseMetaFile16(
304 HDC16 hdc /* [in] Metafile DC to close */
307 HMETAFILE16 hmf;
308 METAFILEDRV_PDEVICE *physDev;
309 DC *dc = MFDRV_CloseMetaFile(hdc);
310 if (!dc) return 0;
311 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
313 /* Now allocate a global handle for the metafile */
315 hmf = MF_Create_HMETAFILE16( physDev->mh );
317 physDev->mh = NULL; /* So it won't be deleted */
318 MFDRV_DeleteDC( dc );
319 return hmf;
322 /******************************************************************
323 * CloseMetaFile (GDI32.@)
325 * Stop recording graphics operations in metafile associated with
326 * hdc and retrieve metafile.
328 * RETURNS
329 * Handle of newly created metafile on success, NULL on failure.
331 HMETAFILE WINAPI CloseMetaFile(
332 HDC hdc /* [in] Metafile DC to close */
335 HMETAFILE hmf;
336 METAFILEDRV_PDEVICE *physDev;
337 DC *dc = MFDRV_CloseMetaFile(hdc);
338 if (!dc) return 0;
339 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
341 /* Now allocate a global handle for the metafile */
343 hmf = MF_Create_HMETAFILE( physDev->mh );
345 physDev->mh = NULL; /* So it won't be deleted */
346 MFDRV_DeleteDC( dc );
347 return hmf;
351 /******************************************************************
352 * MFDRV_WriteRecord
354 * Warning: this function can change the pointer to the metafile header.
356 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
358 DWORD len;
359 METAHEADER *mh;
360 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
362 switch(physDev->mh->mtType)
364 case METAFILE_MEMORY:
365 len = physDev->mh->mtSize * 2 + rlen;
366 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
367 if (!mh) return FALSE;
368 physDev->mh = mh;
369 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
370 break;
371 case METAFILE_DISK:
372 TRACE("Writing record to disk\n");
373 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
374 return FALSE;
375 break;
376 default:
377 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
378 return FALSE;
381 physDev->mh->mtSize += rlen / 2;
382 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
383 return TRUE;
387 /******************************************************************
388 * MFDRV_MetaParam0
391 BOOL MFDRV_MetaParam0(DC *dc, short func)
393 char buffer[8];
394 METARECORD *mr = (METARECORD *)&buffer;
396 mr->rdSize = 3;
397 mr->rdFunction = func;
398 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
402 /******************************************************************
403 * MFDRV_MetaParam1
405 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
407 char buffer[8];
408 METARECORD *mr = (METARECORD *)&buffer;
410 mr->rdSize = 4;
411 mr->rdFunction = func;
412 *(mr->rdParm) = param1;
413 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
417 /******************************************************************
418 * MFDRV_MetaParam2
420 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
422 char buffer[10];
423 METARECORD *mr = (METARECORD *)&buffer;
425 mr->rdSize = 5;
426 mr->rdFunction = func;
427 *(mr->rdParm) = param2;
428 *(mr->rdParm + 1) = param1;
429 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
433 /******************************************************************
434 * MFDRV_MetaParam4
437 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2,
438 short param3, short param4)
440 char buffer[14];
441 METARECORD *mr = (METARECORD *)&buffer;
443 mr->rdSize = 7;
444 mr->rdFunction = func;
445 *(mr->rdParm) = param4;
446 *(mr->rdParm + 1) = param3;
447 *(mr->rdParm + 2) = param2;
448 *(mr->rdParm + 3) = param1;
449 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
453 /******************************************************************
454 * MFDRV_MetaParam6
457 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2,
458 short param3, short param4, short param5, short param6)
460 char buffer[18];
461 METARECORD *mr = (METARECORD *)&buffer;
463 mr->rdSize = 9;
464 mr->rdFunction = func;
465 *(mr->rdParm) = param6;
466 *(mr->rdParm + 1) = param5;
467 *(mr->rdParm + 2) = param4;
468 *(mr->rdParm + 3) = param3;
469 *(mr->rdParm + 4) = param2;
470 *(mr->rdParm + 5) = param1;
471 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
475 /******************************************************************
476 * MFDRV_MetaParam8
478 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2,
479 short param3, short param4, short param5,
480 short param6, short param7, short param8)
482 char buffer[22];
483 METARECORD *mr = (METARECORD *)&buffer;
485 mr->rdSize = 11;
486 mr->rdFunction = func;
487 *(mr->rdParm) = param8;
488 *(mr->rdParm + 1) = param7;
489 *(mr->rdParm + 2) = param6;
490 *(mr->rdParm + 3) = param5;
491 *(mr->rdParm + 4) = param4;
492 *(mr->rdParm + 5) = param3;
493 *(mr->rdParm + 6) = param2;
494 *(mr->rdParm + 7) = param1;
495 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
499 /******************************************************************
500 * MFDRV_AddHandleDC
502 * Note: this function assumes that we never delete objects.
503 * If we do someday, we'll need to maintain a table to re-use deleted
504 * handles.
506 int MFDRV_AddHandleDC( DC *dc )
508 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
509 physDev->mh->mtNoObjects++;
510 return physDev->nextHandle++;