gdi32: Add a GetBoundsRect driver entry point.
[wine/multimedia.git] / dlls / gdi32 / mfdrv / init.c
blob6ccbfe443084cebeab2014d9237fbdf4c6df8e10
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
33 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev );
34 static BOOL MFDRV_DeleteDC( PHYSDEV dev );
37 /**********************************************************************
38 * MFDRV_ExtEscape
40 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
41 INT cbOutput, LPVOID out_data )
43 METARECORD *mr;
44 DWORD len;
45 INT ret;
47 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
49 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
50 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
51 mr->rdSize = len / 2;
52 mr->rdFunction = META_ESCAPE;
53 mr->rdParm[0] = nEscape;
54 mr->rdParm[1] = cbInput;
55 memcpy(&(mr->rdParm[2]), in_data, cbInput);
56 ret = MFDRV_WriteRecord( dev, mr, len);
57 HeapFree(GetProcessHeap(), 0, mr);
58 return ret;
62 /******************************************************************
63 * MFDRV_GetBoundsRect
65 static UINT MFDRV_GetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
67 return 0;
71 /******************************************************************
72 * MFDRV_GetDeviceCaps
74 *A very simple implementation that returns DT_METAFILE
76 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
78 switch(cap)
80 case TECHNOLOGY:
81 return DT_METAFILE;
82 case TEXTCAPS:
83 return 0;
84 default:
85 TRACE(" unsupported capability %d, will return 0\n", cap );
87 return 0;
91 static const struct gdi_dc_funcs MFDRV_Funcs =
93 NULL, /* pAbortDoc */
94 MFDRV_AbortPath, /* pAbortPath */
95 NULL, /* pAlphaBlend */
96 NULL, /* pAngleArc */
97 MFDRV_Arc, /* pArc */
98 NULL, /* pArcTo */
99 MFDRV_BeginPath, /* pBeginPath */
100 NULL, /* pBlendImage */
101 NULL, /* pChoosePixelFormat */
102 MFDRV_Chord, /* pChord */
103 MFDRV_CloseFigure, /* pCloseFigure */
104 NULL, /* pCopyBitmap */
105 NULL, /* pCreateBitmap */
106 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
107 NULL, /* pCreateDC */
108 NULL, /* pDeleteBitmap */
109 MFDRV_DeleteDC, /* pDeleteDC */
110 MFDRV_DeleteObject, /* pDeleteObject */
111 NULL, /* pDescribePixelFormat */
112 NULL, /* pDeviceCapabilities */
113 MFDRV_Ellipse, /* pEllipse */
114 NULL, /* pEndDoc */
115 NULL, /* pEndPage */
116 MFDRV_EndPath, /* pEndPath */
117 NULL, /* pEnumFonts */
118 NULL, /* pEnumICMProfiles */
119 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
120 NULL, /* pExtDeviceMode */
121 MFDRV_ExtEscape, /* pExtEscape */
122 MFDRV_ExtFloodFill, /* pExtFloodFill */
123 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
124 MFDRV_ExtTextOut, /* pExtTextOut */
125 MFDRV_FillPath, /* pFillPath */
126 MFDRV_FillRgn, /* pFillRgn */
127 MFDRV_FlattenPath, /* pFlattenPath */
128 NULL, /* pFontIsLinked */
129 MFDRV_FrameRgn, /* pFrameRgn */
130 NULL, /* pGdiComment */
131 NULL, /* pGdiRealizationInfo */
132 MFDRV_GetBoundsRect, /* pGetBoundsRect */
133 NULL, /* pGetCharABCWidths */
134 NULL, /* pGetCharABCWidthsI */
135 NULL, /* pGetCharWidth */
136 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
137 NULL, /* pGetDeviceGammaRamp */
138 NULL, /* pGetFontData */
139 NULL, /* pGetFontUnicodeRanges */
140 NULL, /* pGetGlyphIndices */
141 NULL, /* pGetGlyphOutline */
142 NULL, /* pGetICMProfile */
143 NULL, /* pGetImage */
144 NULL, /* pGetKerningPairs */
145 NULL, /* pGetNearestColor */
146 NULL, /* pGetOutlineTextMetrics */
147 NULL, /* pGetPixel */
148 NULL, /* pGetPixelFormat */
149 NULL, /* pGetSystemPaletteEntries */
150 NULL, /* pGetTextCharsetInfo */
151 NULL, /* pGetTextExtentExPoint */
152 NULL, /* pGetTextExtentExPointI */
153 NULL, /* pGetTextFace */
154 NULL, /* pGetTextMetrics */
155 NULL, /* pGradientFill */
156 MFDRV_IntersectClipRect, /* pIntersectClipRect */
157 MFDRV_InvertRgn, /* pInvertRgn */
158 MFDRV_LineTo, /* pLineTo */
159 NULL, /* pModifyWorldTransform */
160 MFDRV_MoveTo, /* pMoveTo */
161 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
162 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
163 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
164 MFDRV_PaintRgn, /* pPaintRgn */
165 MFDRV_PatBlt, /* pPatBlt */
166 MFDRV_Pie, /* pPie */
167 MFDRV_PolyBezier, /* pPolyBezier */
168 MFDRV_PolyBezierTo, /* pPolyBezierTo */
169 NULL, /* pPolyDraw */
170 MFDRV_PolyPolygon, /* pPolyPolygon */
171 NULL, /* pPolyPolyline */
172 MFDRV_Polygon, /* pPolygon */
173 MFDRV_Polyline, /* pPolyline */
174 NULL, /* pPolylineTo */
175 NULL, /* pPutImage */
176 NULL, /* pRealizeDefaultPalette */
177 MFDRV_RealizePalette, /* pRealizePalette */
178 MFDRV_Rectangle, /* pRectangle */
179 NULL, /* pResetDC */
180 MFDRV_RestoreDC, /* pRestoreDC */
181 MFDRV_RoundRect, /* pRoundRect */
182 MFDRV_SaveDC, /* pSaveDC */
183 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
184 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
185 MFDRV_SelectBitmap, /* pSelectBitmap */
186 MFDRV_SelectBrush, /* pSelectBrush */
187 MFDRV_SelectClipPath, /* pSelectClipPath */
188 MFDRV_SelectFont, /* pSelectFont */
189 MFDRV_SelectPalette, /* pSelectPalette */
190 MFDRV_SelectPen, /* pSelectPen */
191 NULL, /* pSetArcDirection */
192 MFDRV_SetBkColor, /* pSetBkColor */
193 MFDRV_SetBkMode, /* pSetBkMode */
194 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
195 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
196 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
197 NULL, /* pSetDeviceClipping */
198 NULL, /* pSetDeviceGammaRamp */
199 NULL, /* pSetLayout */
200 MFDRV_SetMapMode, /* pSetMapMode */
201 MFDRV_SetMapperFlags, /* pSetMapperFlags */
202 MFDRV_SetPixel, /* pSetPixel */
203 NULL, /* pSetPixelFormat */
204 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
205 MFDRV_SetROP2, /* pSetROP2 */
206 MFDRV_SetRelAbs, /* pSetRelAbs */
207 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
208 MFDRV_SetTextAlign, /* pSetTextAlign */
209 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
210 MFDRV_SetTextColor, /* pSetTextColor */
211 MFDRV_SetTextJustification, /* pSetTextJustification */
212 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
213 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
214 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
215 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
216 NULL, /* pSetWorldTransform */
217 NULL, /* pStartDoc */
218 NULL, /* pStartPage */
219 MFDRV_StretchBlt, /* pStretchBlt */
220 MFDRV_StretchDIBits, /* pStretchDIBits */
221 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
222 MFDRV_StrokePath, /* pStrokePath */
223 NULL, /* pSwapBuffers */
224 NULL, /* pUnrealizePalette */
225 MFDRV_WidenPath /* pWidenPath */
230 /**********************************************************************
231 * MFDRV_AllocMetaFile
233 static DC *MFDRV_AllocMetaFile(void)
235 DC *dc;
236 METAFILEDRV_PDEVICE *physDev;
238 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
240 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
241 if (!physDev)
243 free_dc_ptr( dc );
244 return NULL;
246 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
248 HeapFree( GetProcessHeap(), 0, physDev );
249 free_dc_ptr( dc );
250 return NULL;
253 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
255 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
256 physDev->handles_size = HANDLE_LIST_INC;
257 physDev->cur_handles = 0;
259 physDev->hFile = 0;
261 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
262 physDev->mh->mtVersion = 0x0300;
263 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
264 physDev->mh->mtNoObjects = 0;
265 physDev->mh->mtMaxRecord = 0;
266 physDev->mh->mtNoParameters = 0;
268 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
270 return dc;
274 /**********************************************************************
275 * MFDRV_CreateCompatibleDC
277 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
279 /* not supported on metafile DCs */
280 return FALSE;
284 /**********************************************************************
285 * MFDRV_DeleteDC
287 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
289 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
290 DWORD index;
292 HeapFree( GetProcessHeap(), 0, physDev->mh );
293 for(index = 0; index < physDev->handles_size; index++)
294 if(physDev->handles[index])
295 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
296 HeapFree( GetProcessHeap(), 0, physDev->handles );
297 HeapFree( GetProcessHeap(), 0, physDev );
298 return TRUE;
302 /**********************************************************************
303 * CreateMetaFileW (GDI32.@)
305 * Create a new DC and associate it with a metafile. Pass a filename
306 * to create a disk-based metafile, NULL to create a memory metafile.
308 * PARAMS
309 * filename [I] Filename of disk metafile
311 * RETURNS
312 * A handle to the metafile DC if successful, NULL on failure.
314 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
316 HDC ret;
317 DC *dc;
318 METAFILEDRV_PDEVICE *physDev;
319 HANDLE hFile;
321 TRACE("%s\n", debugstr_w(filename) );
323 if (!(dc = MFDRV_AllocMetaFile())) return 0;
324 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
326 if (filename) /* disk based metafile */
328 physDev->mh->mtType = METAFILE_DISK;
329 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
330 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
331 free_dc_ptr( dc );
332 return 0;
334 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
335 NULL )) {
336 free_dc_ptr( dc );
337 return 0;
339 physDev->hFile = hFile;
341 /* Grow METAHEADER to include filename */
342 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
344 else /* memory based metafile */
345 physDev->mh->mtType = METAFILE_MEMORY;
347 TRACE("returning %p\n", physDev->dev.hdc);
348 ret = physDev->dev.hdc;
349 release_dc_ptr( dc );
350 return ret;
353 /**********************************************************************
354 * CreateMetaFileA (GDI32.@)
356 * See CreateMetaFileW.
358 HDC WINAPI CreateMetaFileA(LPCSTR filename)
360 LPWSTR filenameW;
361 DWORD len;
362 HDC hReturnDC;
364 if (!filename) return CreateMetaFileW(NULL);
366 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
367 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
368 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
370 hReturnDC = CreateMetaFileW(filenameW);
372 HeapFree( GetProcessHeap(), 0, filenameW );
374 return hReturnDC;
378 /**********************************************************************
379 * MFDRV_CloseMetaFile
381 static DC *MFDRV_CloseMetaFile( HDC hdc )
383 DC *dc;
384 METAFILEDRV_PDEVICE *physDev;
386 TRACE("(%p)\n", hdc );
388 if (!(dc = get_dc_ptr( hdc ))) return NULL;
389 if (dc->header.type != OBJ_METADC)
391 release_dc_ptr( dc );
392 return NULL;
394 if (dc->refcount != 1)
396 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
397 release_dc_ptr( dc );
398 return NULL;
400 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
402 /* Construct the end of metafile record - this is documented
403 * in SDK Knowledgebase Q99334.
406 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
408 free_dc_ptr( dc );
409 return 0;
412 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
414 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
415 free_dc_ptr( dc );
416 return 0;
419 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
420 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
421 NULL, NULL)) {
422 free_dc_ptr( dc );
423 return 0;
425 CloseHandle(physDev->hFile);
426 physDev->mh->mtType = METAFILE_DISK;
429 return dc;
432 /******************************************************************
433 * CloseMetaFile (GDI32.@)
435 * Stop recording graphics operations in metafile associated with
436 * hdc and retrieve metafile.
438 * PARAMS
439 * hdc [I] Metafile DC to close
441 * RETURNS
442 * Handle of newly created metafile on success, NULL on failure.
444 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
446 HMETAFILE hmf;
447 METAFILEDRV_PDEVICE *physDev;
448 DC *dc = MFDRV_CloseMetaFile(hdc);
449 if (!dc) return 0;
450 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
452 /* Now allocate a global handle for the metafile */
454 hmf = MF_Create_HMETAFILE( physDev->mh );
456 physDev->mh = NULL; /* So it won't be deleted */
457 free_dc_ptr( dc );
458 return hmf;
462 /******************************************************************
463 * MFDRV_WriteRecord
465 * Warning: this function can change the pointer to the metafile header.
467 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
469 DWORD len, size;
470 METAHEADER *mh;
471 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
473 switch(physDev->mh->mtType)
475 case METAFILE_MEMORY:
476 len = physDev->mh->mtSize * 2 + rlen;
477 /* reallocate memory if needed */
478 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
479 if (len > size)
481 /*expand size*/
482 size += size / 2 + rlen;
483 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
484 if (!mh) return FALSE;
485 physDev->mh = mh;
486 TRACE("Reallocated metafile: new size is %d\n",size);
488 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
489 break;
490 case METAFILE_DISK:
491 TRACE("Writing record to disk\n");
492 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
493 return FALSE;
494 break;
495 default:
496 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
497 return FALSE;
500 physDev->mh->mtSize += rlen / 2;
501 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
502 return TRUE;
506 /******************************************************************
507 * MFDRV_MetaParam0
510 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
512 char buffer[8];
513 METARECORD *mr = (METARECORD *)&buffer;
515 mr->rdSize = 3;
516 mr->rdFunction = func;
517 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
521 /******************************************************************
522 * MFDRV_MetaParam1
524 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
526 char buffer[8];
527 METARECORD *mr = (METARECORD *)&buffer;
528 WORD *params = mr->rdParm;
530 mr->rdSize = 4;
531 mr->rdFunction = func;
532 params[0] = param1;
533 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
537 /******************************************************************
538 * MFDRV_MetaParam2
540 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
542 char buffer[10];
543 METARECORD *mr = (METARECORD *)&buffer;
544 WORD *params = mr->rdParm;
546 mr->rdSize = 5;
547 mr->rdFunction = func;
548 params[0] = param2;
549 params[1] = param1;
550 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
554 /******************************************************************
555 * MFDRV_MetaParam4
558 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
559 short param3, short param4)
561 char buffer[14];
562 METARECORD *mr = (METARECORD *)&buffer;
563 WORD *params = mr->rdParm;
565 mr->rdSize = 7;
566 mr->rdFunction = func;
567 params[0] = param4;
568 params[1] = param3;
569 params[2] = param2;
570 params[3] = param1;
571 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
575 /******************************************************************
576 * MFDRV_MetaParam6
579 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
580 short param3, short param4, short param5, short param6)
582 char buffer[18];
583 METARECORD *mr = (METARECORD *)&buffer;
584 WORD *params = mr->rdParm;
586 mr->rdSize = 9;
587 mr->rdFunction = func;
588 params[0] = param6;
589 params[1] = param5;
590 params[2] = param4;
591 params[3] = param3;
592 params[4] = param2;
593 params[5] = param1;
594 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
598 /******************************************************************
599 * MFDRV_MetaParam8
601 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
602 short param3, short param4, short param5,
603 short param6, short param7, short param8)
605 char buffer[22];
606 METARECORD *mr = (METARECORD *)&buffer;
607 WORD *params = mr->rdParm;
609 mr->rdSize = 11;
610 mr->rdFunction = func;
611 params[0] = param8;
612 params[1] = param7;
613 params[2] = param6;
614 params[3] = param5;
615 params[4] = param4;
616 params[5] = param3;
617 params[6] = param2;
618 params[7] = param1;
619 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);