Let enhmetafiles reuse gdi handles. This works by a dc 'registering'
[wine/multimedia.git] / dlls / gdi / mfdrv / init.c
blob75e0e647fa7fa3330f0c54428e66a0b88897310e
1 /*
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
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "gdi.h"
28 #include "wownt32.h"
29 #include "mfdrv/metafiledrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
34 static const DC_FUNCTIONS MFDRV_Funcs =
36 NULL, /* pAbortDoc */
37 MFDRV_AbortPath, /* pAbortPath */
38 NULL, /* pAngleArc */
39 MFDRV_Arc, /* pArc */
40 NULL, /* pArcTo */
41 MFDRV_BeginPath, /* pBeginPath */
42 MFDRV_BitBlt, /* pBitBlt */
43 NULL, /* pChoosePixelFormat */
44 MFDRV_Chord, /* pChord */
45 MFDRV_CloseFigure, /* pCloseFigure */
46 NULL, /* pCreateBitmap */
47 NULL, /* pCreateDC */
48 NULL, /* pCreateDIBSection */
49 NULL, /* pDeleteBitmap */
50 NULL, /* pDeleteDC */
51 NULL, /* pDeleteObject */
52 NULL, /* pDescribePixelFormat */
53 NULL, /* pDeviceCapabilities */
54 MFDRV_Ellipse, /* pEllipse */
55 NULL, /* pEndDoc */
56 NULL, /* pEndPage */
57 MFDRV_EndPath, /* pEndPath */
58 NULL, /* pEnumDeviceFonts */
59 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
60 NULL, /* pExtDeviceMode */
61 MFDRV_ExtEscape, /* pExtEscape */
62 MFDRV_ExtFloodFill, /* pExtFloodFill */
63 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
64 MFDRV_ExtTextOut, /* pExtTextOut */
65 MFDRV_FillPath, /* pFillPath */
66 MFDRV_FillRgn, /* pFillRgn */
67 MFDRV_FlattenPath, /* pFlattenPath */
68 MFDRV_FrameRgn, /* pFrameRgn */
69 NULL, /* pGdiComment */
70 NULL, /* pGetBitmapBits */
71 NULL, /* pGetCharWidth */
72 NULL, /* pGetDCOrgEx */
73 NULL, /* pGetDIBColorTable */
74 NULL, /* pGetDIBits */
75 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
76 NULL, /* pGetDeviceGammaRamp */
77 NULL, /* pGetNearestColor */
78 NULL, /* pGetPixel */
79 NULL, /* pGetPixelFormat */
80 NULL, /* pGetSystemPaletteEntries */
81 NULL, /* pGetTextExtentPoint */
82 NULL, /* pGetTextMetrics */
83 MFDRV_IntersectClipRect, /* pIntersectClipRect */
84 MFDRV_InvertRgn, /* pInvertRgn */
85 MFDRV_LineTo, /* pLineTo */
86 NULL, /* pModifyWorldTransform */
87 MFDRV_MoveTo, /* pMoveTo */
88 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
89 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
90 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
91 MFDRV_PaintRgn, /* pPaintRgn */
92 MFDRV_PatBlt, /* pPatBlt */
93 MFDRV_Pie, /* pPie */
94 MFDRV_PolyBezier, /* pPolyBezier */
95 MFDRV_PolyBezierTo, /* pPolyBezierTo */
96 NULL, /* pPolyDraw */
97 MFDRV_PolyPolygon, /* pPolyPolygon */
98 NULL, /* pPolyPolyline */
99 MFDRV_Polygon, /* pPolygon */
100 MFDRV_Polyline, /* pPolyline */
101 NULL, /* pPolylineTo */
102 NULL, /* pRealizeDefaultPalette */
103 MFDRV_RealizePalette, /* pRealizePalette */
104 MFDRV_Rectangle, /* pRectangle */
105 NULL, /* pResetDC */
106 MFDRV_RestoreDC, /* pRestoreDC */
107 MFDRV_RoundRect, /* pRoundRect */
108 MFDRV_SaveDC, /* pSaveDC */
109 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
110 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
111 MFDRV_SelectBitmap, /* pSelectBitmap */
112 MFDRV_SelectBrush, /* pSelectBrush */
113 MFDRV_SelectClipPath, /* pSelectClipPath */
114 MFDRV_SelectFont, /* pSelectFont */
115 MFDRV_SelectPalette, /* pSelectPalette */
116 MFDRV_SelectPen, /* pSelectPen */
117 NULL, /* pSetArcDirection */
118 NULL, /* pSetBitmapBits */
119 MFDRV_SetBkColor, /* pSetBkColor */
120 MFDRV_SetBkMode, /* pSetBkMode */
121 NULL, /* pSetDCBrushColor*/
122 NULL, /* pSetDCOrg */
123 NULL, /* pSetDCPenColor*/
124 NULL, /* pSetDIBColorTable */
125 NULL, /* pSetDIBits */
126 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
127 NULL, /* pSetDeviceClipping */
128 NULL, /* pSetDeviceGammaRamp */
129 MFDRV_SetMapMode, /* pSetMapMode */
130 MFDRV_SetMapperFlags, /* pSetMapperFlags */
131 MFDRV_SetPixel, /* pSetPixel */
132 NULL, /* pSetPixelFormat */
133 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
134 MFDRV_SetROP2, /* pSetROP2 */
135 MFDRV_SetRelAbs, /* pSetRelAbs */
136 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
137 MFDRV_SetTextAlign, /* pSetTextAlign */
138 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
139 MFDRV_SetTextColor, /* pSetTextColor */
140 MFDRV_SetTextJustification, /* pSetTextJustification */
141 MFDRV_SetViewportExt, /* pSetViewportExt */
142 MFDRV_SetViewportOrg, /* pSetViewportOrg */
143 MFDRV_SetWindowExt, /* pSetWindowExt */
144 MFDRV_SetWindowOrg, /* pSetWindowOrg */
145 NULL, /* pSetWorldTransform */
146 NULL, /* pStartDoc */
147 NULL, /* pStartPage */
148 MFDRV_StretchBlt, /* pStretchBlt */
149 MFDRV_StretchDIBits, /* pStretchDIBits */
150 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
151 MFDRV_StrokePath, /* pStrokePath */
152 NULL, /* pSwapBuffers */
153 MFDRV_WidenPath /* pWidenPath */
158 /**********************************************************************
159 * MFDRV_AllocMetaFile
161 static DC *MFDRV_AllocMetaFile(void)
163 DC *dc;
164 METAFILEDRV_PDEVICE *physDev;
166 if (!(dc = DC_AllocDC( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
168 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
169 if (!physDev)
171 GDI_FreeObject( dc->hSelf, dc );
172 return NULL;
174 dc->physDev = (PHYSDEV)physDev;
175 physDev->hdc = dc->hSelf;
176 physDev->dc = dc;
178 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
180 HeapFree( GetProcessHeap(), 0, physDev );
181 GDI_FreeObject( dc->hSelf, dc );
182 return NULL;
185 physDev->nextHandle = 0;
186 physDev->hFile = 0;
188 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
189 physDev->mh->mtVersion = 0x0300;
190 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
191 physDev->mh->mtNoObjects = 0;
192 physDev->mh->mtMaxRecord = 0;
193 physDev->mh->mtNoParameters = 0;
195 return dc;
199 /**********************************************************************
200 * MFDRV_DeleteDC
202 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
204 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
205 DC *dc = physDev->dc;
207 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
208 HeapFree( GetProcessHeap(), 0, physDev );
209 dc->physDev = NULL;
210 GDI_FreeObject( dc->hSelf, dc );
211 return TRUE;
215 /**********************************************************************
216 * CreateMetaFileA (GDI32.@)
218 * Create a new DC and associate it with a metafile. Pass a filename
219 * to create a disk-based metafile, NULL to create a memory metafile.
221 * RETURNS
222 * A handle to the metafile DC if successful, NULL on failure.
224 HDC WINAPI CreateMetaFileA( LPCSTR filename ) /* [in] Filename of disk metafile */
226 HDC ret;
227 DC *dc;
228 METAFILEDRV_PDEVICE *physDev;
229 HANDLE hFile;
231 TRACE("'%s'\n", filename );
233 if (!(dc = MFDRV_AllocMetaFile())) return 0;
234 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
236 if (filename) /* disk based metafile */
238 physDev->mh->mtType = METAFILE_DISK;
239 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
240 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
241 MFDRV_DeleteDC( dc->physDev );
242 return 0;
244 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
245 NULL )) {
246 MFDRV_DeleteDC( dc->physDev );
247 return 0;
249 physDev->hFile = hFile;
251 /* Grow METAHEADER to include filename */
252 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
254 else /* memory based metafile */
255 physDev->mh->mtType = METAFILE_MEMORY;
257 TRACE("returning %p\n", dc->hSelf);
258 ret = dc->hSelf;
259 GDI_ReleaseObj( dc->hSelf );
260 return ret;
263 /**********************************************************************
264 * CreateMetaFileW (GDI32.@)
266 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
268 LPSTR filenameA;
269 DWORD len;
270 HDC hReturnDC;
272 if (!filename) return CreateMetaFileA(NULL);
274 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
275 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
276 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
278 hReturnDC = CreateMetaFileA(filenameA);
280 HeapFree( GetProcessHeap(), 0, filenameA );
282 return hReturnDC;
286 /**********************************************************************
287 * MFDRV_CloseMetaFile
289 static DC *MFDRV_CloseMetaFile( HDC hdc )
291 DC *dc;
292 METAFILEDRV_PDEVICE *physDev;
294 TRACE("(%p)\n", hdc );
296 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
297 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
299 /* Construct the end of metafile record - this is documented
300 * in SDK Knowledgebase Q99334.
303 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
305 MFDRV_DeleteDC( dc->physDev );
306 return 0;
309 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
311 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
312 MFDRV_DeleteDC( dc->physDev );
313 return 0;
316 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
317 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
318 sizeof(*physDev->mh), NULL, NULL)) {
319 MFDRV_DeleteDC( dc->physDev );
320 return 0;
322 CloseHandle(physDev->hFile);
323 physDev->mh->mtType = METAFILE_DISK;
326 return dc;
329 /******************************************************************
330 * CloseMetaFile (GDI.126)
332 HMETAFILE16 WINAPI CloseMetaFile16(
333 HDC16 hdc /* [in] Metafile DC to close */
336 HMETAFILE16 hmf;
337 METAFILEDRV_PDEVICE *physDev;
338 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
339 if (!dc) return 0;
340 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
342 /* Now allocate a global handle for the metafile */
344 hmf = MF_Create_HMETAFILE16( physDev->mh );
346 physDev->mh = NULL; /* So it won't be deleted */
347 MFDRV_DeleteDC( dc->physDev );
348 return hmf;
351 /******************************************************************
352 * CloseMetaFile (GDI32.@)
354 * Stop recording graphics operations in metafile associated with
355 * hdc and retrieve metafile.
357 * RETURNS
358 * Handle of newly created metafile on success, NULL on failure.
360 HMETAFILE WINAPI CloseMetaFile(
361 HDC hdc /* [in] Metafile DC to close */
364 HMETAFILE hmf;
365 METAFILEDRV_PDEVICE *physDev;
366 DC *dc = MFDRV_CloseMetaFile(hdc);
367 if (!dc) return 0;
368 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
370 /* Now allocate a global handle for the metafile */
372 hmf = MF_Create_HMETAFILE( physDev->mh );
374 physDev->mh = NULL; /* So it won't be deleted */
375 MFDRV_DeleteDC( dc->physDev );
376 return hmf;
380 /******************************************************************
381 * MFDRV_WriteRecord
383 * Warning: this function can change the pointer to the metafile header.
385 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
387 DWORD len, size;
388 METAHEADER *mh;
389 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
391 switch(physDev->mh->mtType)
393 case METAFILE_MEMORY:
394 len = physDev->mh->mtSize * 2 + rlen;
395 /* reallocate memory if needed */
396 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
397 if (len > size)
399 /*expand size*/
400 size += size / 2 + rlen;
401 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
402 if (!mh) return FALSE;
403 physDev->mh = mh;
404 TRACE("Reallocated metafile: new size is %ld\n",size);
406 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
407 break;
408 case METAFILE_DISK:
409 TRACE("Writing record to disk\n");
410 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
411 return FALSE;
412 break;
413 default:
414 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
415 return FALSE;
418 physDev->mh->mtSize += rlen / 2;
419 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
420 return TRUE;
424 /******************************************************************
425 * MFDRV_MetaParam0
428 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
430 char buffer[8];
431 METARECORD *mr = (METARECORD *)&buffer;
433 mr->rdSize = 3;
434 mr->rdFunction = func;
435 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
439 /******************************************************************
440 * MFDRV_MetaParam1
442 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
444 char buffer[8];
445 METARECORD *mr = (METARECORD *)&buffer;
447 mr->rdSize = 4;
448 mr->rdFunction = func;
449 *(mr->rdParm) = param1;
450 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
454 /******************************************************************
455 * MFDRV_MetaParam2
457 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
459 char buffer[10];
460 METARECORD *mr = (METARECORD *)&buffer;
462 mr->rdSize = 5;
463 mr->rdFunction = func;
464 *(mr->rdParm) = param2;
465 *(mr->rdParm + 1) = param1;
466 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
470 /******************************************************************
471 * MFDRV_MetaParam4
474 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
475 short param3, short param4)
477 char buffer[14];
478 METARECORD *mr = (METARECORD *)&buffer;
480 mr->rdSize = 7;
481 mr->rdFunction = func;
482 *(mr->rdParm) = param4;
483 *(mr->rdParm + 1) = param3;
484 *(mr->rdParm + 2) = param2;
485 *(mr->rdParm + 3) = param1;
486 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
490 /******************************************************************
491 * MFDRV_MetaParam6
494 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
495 short param3, short param4, short param5, short param6)
497 char buffer[18];
498 METARECORD *mr = (METARECORD *)&buffer;
500 mr->rdSize = 9;
501 mr->rdFunction = func;
502 *(mr->rdParm) = param6;
503 *(mr->rdParm + 1) = param5;
504 *(mr->rdParm + 2) = param4;
505 *(mr->rdParm + 3) = param3;
506 *(mr->rdParm + 4) = param2;
507 *(mr->rdParm + 5) = param1;
508 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
512 /******************************************************************
513 * MFDRV_MetaParam8
515 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
516 short param3, short param4, short param5,
517 short param6, short param7, short param8)
519 char buffer[22];
520 METARECORD *mr = (METARECORD *)&buffer;
522 mr->rdSize = 11;
523 mr->rdFunction = func;
524 *(mr->rdParm) = param8;
525 *(mr->rdParm + 1) = param7;
526 *(mr->rdParm + 2) = param6;
527 *(mr->rdParm + 3) = param5;
528 *(mr->rdParm + 4) = param4;
529 *(mr->rdParm + 5) = param3;
530 *(mr->rdParm + 6) = param2;
531 *(mr->rdParm + 7) = param1;
532 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
536 /******************************************************************
537 * MFDRV_AddHandleDC
539 * Note: this function assumes that we never delete objects.
540 * If we do someday, we'll need to maintain a table to re-use deleted
541 * handles.
543 int MFDRV_AddHandleDC( PHYSDEV dev )
545 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
546 physDev->mh->mtNoObjects++;
547 return physDev->nextHandle++;
550 /**********************************************************************
551 * MFDRV_ExtEscape
553 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
554 INT cbOutput, LPVOID out_data )
556 METARECORD *mr;
557 DWORD len;
558 INT ret;
560 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
561 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
562 mr->rdSize = len / 2;
563 mr->rdFunction = META_ESCAPE;
564 mr->rdParm[0] = nEscape;
565 mr->rdParm[1] = cbInput;
566 memcpy(&(mr->rdParm[2]), in_data, cbInput);
567 ret = MFDRV_WriteRecord( dev, mr, len);
568 HeapFree(GetProcessHeap(), 0, mr);
569 return ret;
573 /******************************************************************
574 * MFDRV_GetDeviceCaps
576 *A very simple implementation that returns DT_METAFILE
578 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
580 switch(cap)
582 case TECHNOLOGY:
583 return DT_METAFILE;
584 case TEXTCAPS:
585 return 0;
586 default:
587 TRACE(" unsupported capability %d, will return 0\n", cap );
589 return 0;