gdi32: Add a SetBoundsRect driver entry point.
[wine/multimedia.git] / dlls / gdi32 / mfdrv / init.c
blob19db34856f259edd01f67032c0343f06f08afbe5
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_SetBoundsRect
74 static UINT MFDRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
76 return 0;
80 /******************************************************************
81 * MFDRV_GetDeviceCaps
83 *A very simple implementation that returns DT_METAFILE
85 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
87 switch(cap)
89 case TECHNOLOGY:
90 return DT_METAFILE;
91 case TEXTCAPS:
92 return 0;
93 default:
94 TRACE(" unsupported capability %d, will return 0\n", cap );
96 return 0;
100 static const struct gdi_dc_funcs MFDRV_Funcs =
102 NULL, /* pAbortDoc */
103 MFDRV_AbortPath, /* pAbortPath */
104 NULL, /* pAlphaBlend */
105 NULL, /* pAngleArc */
106 MFDRV_Arc, /* pArc */
107 NULL, /* pArcTo */
108 MFDRV_BeginPath, /* pBeginPath */
109 NULL, /* pBlendImage */
110 NULL, /* pChoosePixelFormat */
111 MFDRV_Chord, /* pChord */
112 MFDRV_CloseFigure, /* pCloseFigure */
113 NULL, /* pCopyBitmap */
114 NULL, /* pCreateBitmap */
115 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
116 NULL, /* pCreateDC */
117 NULL, /* pDeleteBitmap */
118 MFDRV_DeleteDC, /* pDeleteDC */
119 MFDRV_DeleteObject, /* pDeleteObject */
120 NULL, /* pDescribePixelFormat */
121 NULL, /* pDeviceCapabilities */
122 MFDRV_Ellipse, /* pEllipse */
123 NULL, /* pEndDoc */
124 NULL, /* pEndPage */
125 MFDRV_EndPath, /* pEndPath */
126 NULL, /* pEnumFonts */
127 NULL, /* pEnumICMProfiles */
128 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
129 NULL, /* pExtDeviceMode */
130 MFDRV_ExtEscape, /* pExtEscape */
131 MFDRV_ExtFloodFill, /* pExtFloodFill */
132 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
133 MFDRV_ExtTextOut, /* pExtTextOut */
134 MFDRV_FillPath, /* pFillPath */
135 MFDRV_FillRgn, /* pFillRgn */
136 MFDRV_FlattenPath, /* pFlattenPath */
137 NULL, /* pFontIsLinked */
138 MFDRV_FrameRgn, /* pFrameRgn */
139 NULL, /* pGdiComment */
140 NULL, /* pGdiRealizationInfo */
141 MFDRV_GetBoundsRect, /* pGetBoundsRect */
142 NULL, /* pGetCharABCWidths */
143 NULL, /* pGetCharABCWidthsI */
144 NULL, /* pGetCharWidth */
145 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
146 NULL, /* pGetDeviceGammaRamp */
147 NULL, /* pGetFontData */
148 NULL, /* pGetFontUnicodeRanges */
149 NULL, /* pGetGlyphIndices */
150 NULL, /* pGetGlyphOutline */
151 NULL, /* pGetICMProfile */
152 NULL, /* pGetImage */
153 NULL, /* pGetKerningPairs */
154 NULL, /* pGetNearestColor */
155 NULL, /* pGetOutlineTextMetrics */
156 NULL, /* pGetPixel */
157 NULL, /* pGetPixelFormat */
158 NULL, /* pGetSystemPaletteEntries */
159 NULL, /* pGetTextCharsetInfo */
160 NULL, /* pGetTextExtentExPoint */
161 NULL, /* pGetTextExtentExPointI */
162 NULL, /* pGetTextFace */
163 NULL, /* pGetTextMetrics */
164 NULL, /* pGradientFill */
165 MFDRV_IntersectClipRect, /* pIntersectClipRect */
166 MFDRV_InvertRgn, /* pInvertRgn */
167 MFDRV_LineTo, /* pLineTo */
168 NULL, /* pModifyWorldTransform */
169 MFDRV_MoveTo, /* pMoveTo */
170 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
171 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
172 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
173 MFDRV_PaintRgn, /* pPaintRgn */
174 MFDRV_PatBlt, /* pPatBlt */
175 MFDRV_Pie, /* pPie */
176 MFDRV_PolyBezier, /* pPolyBezier */
177 MFDRV_PolyBezierTo, /* pPolyBezierTo */
178 NULL, /* pPolyDraw */
179 MFDRV_PolyPolygon, /* pPolyPolygon */
180 NULL, /* pPolyPolyline */
181 MFDRV_Polygon, /* pPolygon */
182 MFDRV_Polyline, /* pPolyline */
183 NULL, /* pPolylineTo */
184 NULL, /* pPutImage */
185 NULL, /* pRealizeDefaultPalette */
186 MFDRV_RealizePalette, /* pRealizePalette */
187 MFDRV_Rectangle, /* pRectangle */
188 NULL, /* pResetDC */
189 MFDRV_RestoreDC, /* pRestoreDC */
190 MFDRV_RoundRect, /* pRoundRect */
191 MFDRV_SaveDC, /* pSaveDC */
192 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
193 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
194 MFDRV_SelectBitmap, /* pSelectBitmap */
195 MFDRV_SelectBrush, /* pSelectBrush */
196 MFDRV_SelectClipPath, /* pSelectClipPath */
197 MFDRV_SelectFont, /* pSelectFont */
198 MFDRV_SelectPalette, /* pSelectPalette */
199 MFDRV_SelectPen, /* pSelectPen */
200 NULL, /* pSetArcDirection */
201 MFDRV_SetBkColor, /* pSetBkColor */
202 MFDRV_SetBkMode, /* pSetBkMode */
203 MFDRV_SetBoundsRect, /* pSetBoundsRect */
204 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
205 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
206 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
207 NULL, /* pSetDeviceClipping */
208 NULL, /* pSetDeviceGammaRamp */
209 NULL, /* pSetLayout */
210 MFDRV_SetMapMode, /* pSetMapMode */
211 MFDRV_SetMapperFlags, /* pSetMapperFlags */
212 MFDRV_SetPixel, /* pSetPixel */
213 NULL, /* pSetPixelFormat */
214 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
215 MFDRV_SetROP2, /* pSetROP2 */
216 MFDRV_SetRelAbs, /* pSetRelAbs */
217 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
218 MFDRV_SetTextAlign, /* pSetTextAlign */
219 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
220 MFDRV_SetTextColor, /* pSetTextColor */
221 MFDRV_SetTextJustification, /* pSetTextJustification */
222 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
223 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
224 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
225 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
226 NULL, /* pSetWorldTransform */
227 NULL, /* pStartDoc */
228 NULL, /* pStartPage */
229 MFDRV_StretchBlt, /* pStretchBlt */
230 MFDRV_StretchDIBits, /* pStretchDIBits */
231 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
232 MFDRV_StrokePath, /* pStrokePath */
233 NULL, /* pSwapBuffers */
234 NULL, /* pUnrealizePalette */
235 MFDRV_WidenPath /* pWidenPath */
240 /**********************************************************************
241 * MFDRV_AllocMetaFile
243 static DC *MFDRV_AllocMetaFile(void)
245 DC *dc;
246 METAFILEDRV_PDEVICE *physDev;
248 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
250 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
251 if (!physDev)
253 free_dc_ptr( dc );
254 return NULL;
256 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
258 HeapFree( GetProcessHeap(), 0, physDev );
259 free_dc_ptr( dc );
260 return NULL;
263 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
265 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
266 physDev->handles_size = HANDLE_LIST_INC;
267 physDev->cur_handles = 0;
269 physDev->hFile = 0;
271 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
272 physDev->mh->mtVersion = 0x0300;
273 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
274 physDev->mh->mtNoObjects = 0;
275 physDev->mh->mtMaxRecord = 0;
276 physDev->mh->mtNoParameters = 0;
278 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
280 return dc;
284 /**********************************************************************
285 * MFDRV_CreateCompatibleDC
287 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
289 /* not supported on metafile DCs */
290 return FALSE;
294 /**********************************************************************
295 * MFDRV_DeleteDC
297 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
299 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
300 DWORD index;
302 HeapFree( GetProcessHeap(), 0, physDev->mh );
303 for(index = 0; index < physDev->handles_size; index++)
304 if(physDev->handles[index])
305 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
306 HeapFree( GetProcessHeap(), 0, physDev->handles );
307 HeapFree( GetProcessHeap(), 0, physDev );
308 return TRUE;
312 /**********************************************************************
313 * CreateMetaFileW (GDI32.@)
315 * Create a new DC and associate it with a metafile. Pass a filename
316 * to create a disk-based metafile, NULL to create a memory metafile.
318 * PARAMS
319 * filename [I] Filename of disk metafile
321 * RETURNS
322 * A handle to the metafile DC if successful, NULL on failure.
324 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
326 HDC ret;
327 DC *dc;
328 METAFILEDRV_PDEVICE *physDev;
329 HANDLE hFile;
331 TRACE("%s\n", debugstr_w(filename) );
333 if (!(dc = MFDRV_AllocMetaFile())) return 0;
334 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
336 if (filename) /* disk based metafile */
338 physDev->mh->mtType = METAFILE_DISK;
339 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
340 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
341 free_dc_ptr( dc );
342 return 0;
344 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
345 NULL )) {
346 free_dc_ptr( dc );
347 return 0;
349 physDev->hFile = hFile;
351 /* Grow METAHEADER to include filename */
352 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
354 else /* memory based metafile */
355 physDev->mh->mtType = METAFILE_MEMORY;
357 TRACE("returning %p\n", physDev->dev.hdc);
358 ret = physDev->dev.hdc;
359 release_dc_ptr( dc );
360 return ret;
363 /**********************************************************************
364 * CreateMetaFileA (GDI32.@)
366 * See CreateMetaFileW.
368 HDC WINAPI CreateMetaFileA(LPCSTR filename)
370 LPWSTR filenameW;
371 DWORD len;
372 HDC hReturnDC;
374 if (!filename) return CreateMetaFileW(NULL);
376 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
377 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
378 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
380 hReturnDC = CreateMetaFileW(filenameW);
382 HeapFree( GetProcessHeap(), 0, filenameW );
384 return hReturnDC;
388 /**********************************************************************
389 * MFDRV_CloseMetaFile
391 static DC *MFDRV_CloseMetaFile( HDC hdc )
393 DC *dc;
394 METAFILEDRV_PDEVICE *physDev;
396 TRACE("(%p)\n", hdc );
398 if (!(dc = get_dc_ptr( hdc ))) return NULL;
399 if (dc->header.type != OBJ_METADC)
401 release_dc_ptr( dc );
402 return NULL;
404 if (dc->refcount != 1)
406 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
407 release_dc_ptr( dc );
408 return NULL;
410 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
412 /* Construct the end of metafile record - this is documented
413 * in SDK Knowledgebase Q99334.
416 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
418 free_dc_ptr( dc );
419 return 0;
422 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
424 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
425 free_dc_ptr( dc );
426 return 0;
429 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
430 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
431 NULL, NULL)) {
432 free_dc_ptr( dc );
433 return 0;
435 CloseHandle(physDev->hFile);
436 physDev->mh->mtType = METAFILE_DISK;
439 return dc;
442 /******************************************************************
443 * CloseMetaFile (GDI32.@)
445 * Stop recording graphics operations in metafile associated with
446 * hdc and retrieve metafile.
448 * PARAMS
449 * hdc [I] Metafile DC to close
451 * RETURNS
452 * Handle of newly created metafile on success, NULL on failure.
454 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
456 HMETAFILE hmf;
457 METAFILEDRV_PDEVICE *physDev;
458 DC *dc = MFDRV_CloseMetaFile(hdc);
459 if (!dc) return 0;
460 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
462 /* Now allocate a global handle for the metafile */
464 hmf = MF_Create_HMETAFILE( physDev->mh );
466 physDev->mh = NULL; /* So it won't be deleted */
467 free_dc_ptr( dc );
468 return hmf;
472 /******************************************************************
473 * MFDRV_WriteRecord
475 * Warning: this function can change the pointer to the metafile header.
477 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
479 DWORD len, size;
480 METAHEADER *mh;
481 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
483 switch(physDev->mh->mtType)
485 case METAFILE_MEMORY:
486 len = physDev->mh->mtSize * 2 + rlen;
487 /* reallocate memory if needed */
488 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
489 if (len > size)
491 /*expand size*/
492 size += size / 2 + rlen;
493 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
494 if (!mh) return FALSE;
495 physDev->mh = mh;
496 TRACE("Reallocated metafile: new size is %d\n",size);
498 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
499 break;
500 case METAFILE_DISK:
501 TRACE("Writing record to disk\n");
502 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
503 return FALSE;
504 break;
505 default:
506 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
507 return FALSE;
510 physDev->mh->mtSize += rlen / 2;
511 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
512 return TRUE;
516 /******************************************************************
517 * MFDRV_MetaParam0
520 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
522 char buffer[8];
523 METARECORD *mr = (METARECORD *)&buffer;
525 mr->rdSize = 3;
526 mr->rdFunction = func;
527 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
531 /******************************************************************
532 * MFDRV_MetaParam1
534 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
536 char buffer[8];
537 METARECORD *mr = (METARECORD *)&buffer;
538 WORD *params = mr->rdParm;
540 mr->rdSize = 4;
541 mr->rdFunction = func;
542 params[0] = param1;
543 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
547 /******************************************************************
548 * MFDRV_MetaParam2
550 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
552 char buffer[10];
553 METARECORD *mr = (METARECORD *)&buffer;
554 WORD *params = mr->rdParm;
556 mr->rdSize = 5;
557 mr->rdFunction = func;
558 params[0] = param2;
559 params[1] = param1;
560 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
564 /******************************************************************
565 * MFDRV_MetaParam4
568 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
569 short param3, short param4)
571 char buffer[14];
572 METARECORD *mr = (METARECORD *)&buffer;
573 WORD *params = mr->rdParm;
575 mr->rdSize = 7;
576 mr->rdFunction = func;
577 params[0] = param4;
578 params[1] = param3;
579 params[2] = param2;
580 params[3] = param1;
581 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
585 /******************************************************************
586 * MFDRV_MetaParam6
589 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
590 short param3, short param4, short param5, short param6)
592 char buffer[18];
593 METARECORD *mr = (METARECORD *)&buffer;
594 WORD *params = mr->rdParm;
596 mr->rdSize = 9;
597 mr->rdFunction = func;
598 params[0] = param6;
599 params[1] = param5;
600 params[2] = param4;
601 params[3] = param3;
602 params[4] = param2;
603 params[5] = param1;
604 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
608 /******************************************************************
609 * MFDRV_MetaParam8
611 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
612 short param3, short param4, short param5,
613 short param6, short param7, short param8)
615 char buffer[22];
616 METARECORD *mr = (METARECORD *)&buffer;
617 WORD *params = mr->rdParm;
619 mr->rdSize = 11;
620 mr->rdFunction = func;
621 params[0] = param8;
622 params[1] = param7;
623 params[2] = param6;
624 params[3] = param5;
625 params[4] = param4;
626 params[5] = param3;
627 params[6] = param2;
628 params[7] = param1;
629 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);