d3dx9: Implement ID3DXBaseEffect::GetAnnotation().
[wine/multimedia.git] / dlls / gdi32 / mfdrv / init.c
blobcba26ebdeb3e4ea420aefddfad408a0191ef9115
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 CDECL MFDRV_DeleteDC( PHYSDEV dev );
35 static const DC_FUNCTIONS MFDRV_Funcs =
37 NULL, /* pAbortDoc */
38 MFDRV_AbortPath, /* pAbortPath */
39 NULL, /* pAlphaBlend */
40 NULL, /* pAngleArc */
41 MFDRV_Arc, /* pArc */
42 NULL, /* pArcTo */
43 MFDRV_BeginPath, /* pBeginPath */
44 NULL, /* pChoosePixelFormat */
45 MFDRV_Chord, /* pChord */
46 MFDRV_CloseFigure, /* pCloseFigure */
47 NULL, /* pCreateBitmap */
48 NULL, /* pCreateDC */
49 NULL, /* pCreateDIBSection */
50 NULL, /* pDeleteBitmap */
51 MFDRV_DeleteDC, /* pDeleteDC */
52 MFDRV_DeleteObject, /* pDeleteObject */
53 NULL, /* pDescribePixelFormat */
54 NULL, /* pDeviceCapabilities */
55 MFDRV_Ellipse, /* pEllipse */
56 NULL, /* pEndDoc */
57 NULL, /* pEndPage */
58 MFDRV_EndPath, /* pEndPath */
59 NULL, /* pEnumDeviceFonts */
60 NULL, /* pEnumICMProfiles */
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, /* pGetDIBits */
75 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
76 NULL, /* pGetDeviceGammaRamp */
77 NULL, /* pGetICMProfile */
78 NULL, /* pGetNearestColor */
79 NULL, /* pGetPixel */
80 NULL, /* pGetPixelFormat */
81 NULL, /* pGetSystemPaletteEntries */
82 NULL, /* pGetTextExtentExPoint */
83 NULL, /* pGetTextMetrics */
84 MFDRV_IntersectClipRect, /* pIntersectClipRect */
85 MFDRV_InvertRgn, /* pInvertRgn */
86 MFDRV_LineTo, /* pLineTo */
87 NULL, /* pModifyWorldTransform */
88 MFDRV_MoveTo, /* pMoveTo */
89 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
90 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
91 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
92 MFDRV_PaintRgn, /* pPaintRgn */
93 MFDRV_PatBlt, /* pPatBlt */
94 MFDRV_Pie, /* pPie */
95 MFDRV_PolyBezier, /* pPolyBezier */
96 MFDRV_PolyBezierTo, /* pPolyBezierTo */
97 NULL, /* pPolyDraw */
98 MFDRV_PolyPolygon, /* pPolyPolygon */
99 NULL, /* pPolyPolyline */
100 MFDRV_Polygon, /* pPolygon */
101 MFDRV_Polyline, /* pPolyline */
102 NULL, /* pPolylineTo */
103 NULL, /* pRealizeDefaultPalette */
104 MFDRV_RealizePalette, /* pRealizePalette */
105 MFDRV_Rectangle, /* pRectangle */
106 NULL, /* pResetDC */
107 MFDRV_RestoreDC, /* pRestoreDC */
108 MFDRV_RoundRect, /* pRoundRect */
109 MFDRV_SaveDC, /* pSaveDC */
110 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
111 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
112 MFDRV_SelectBitmap, /* pSelectBitmap */
113 MFDRV_SelectBrush, /* pSelectBrush */
114 MFDRV_SelectClipPath, /* pSelectClipPath */
115 MFDRV_SelectFont, /* pSelectFont */
116 MFDRV_SelectPalette, /* pSelectPalette */
117 MFDRV_SelectPen, /* pSelectPen */
118 NULL, /* pSetArcDirection */
119 NULL, /* pSetBitmapBits */
120 MFDRV_SetBkColor, /* pSetBkColor */
121 MFDRV_SetBkMode, /* pSetBkMode */
122 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
123 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
124 NULL, /* pSetDIBColorTable */
125 NULL, /* pSetDIBits */
126 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
127 NULL, /* pSetDeviceClipping */
128 NULL, /* pSetDeviceGammaRamp */
129 NULL, /* pSetLayout */
130 MFDRV_SetMapMode, /* pSetMapMode */
131 MFDRV_SetMapperFlags, /* pSetMapperFlags */
132 MFDRV_SetPixel, /* pSetPixel */
133 NULL, /* pSetPixelFormat */
134 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
135 MFDRV_SetROP2, /* pSetROP2 */
136 MFDRV_SetRelAbs, /* pSetRelAbs */
137 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
138 MFDRV_SetTextAlign, /* pSetTextAlign */
139 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
140 MFDRV_SetTextColor, /* pSetTextColor */
141 MFDRV_SetTextJustification, /* pSetTextJustification */
142 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
143 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
144 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
145 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
146 NULL, /* pSetWorldTransform */
147 NULL, /* pStartDoc */
148 NULL, /* pStartPage */
149 MFDRV_StretchBlt, /* pStretchBlt */
150 MFDRV_StretchDIBits, /* pStretchDIBits */
151 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
152 MFDRV_StrokePath, /* pStrokePath */
153 NULL, /* pSwapBuffers */
154 NULL, /* pUnrealizePalette */
155 MFDRV_WidenPath /* pWidenPath */
160 /**********************************************************************
161 * MFDRV_AllocMetaFile
163 static DC *MFDRV_AllocMetaFile(void)
165 DC *dc;
166 METAFILEDRV_PDEVICE *physDev;
168 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
170 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
171 if (!physDev)
173 free_dc_ptr( dc );
174 return NULL;
176 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
178 HeapFree( GetProcessHeap(), 0, physDev );
179 free_dc_ptr( dc );
180 return NULL;
183 push_dc_driver( dc, &physDev->dev, &MFDRV_Funcs );
185 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
186 physDev->handles_size = HANDLE_LIST_INC;
187 physDev->cur_handles = 0;
189 physDev->hFile = 0;
191 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
192 physDev->mh->mtVersion = 0x0300;
193 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
194 physDev->mh->mtNoObjects = 0;
195 physDev->mh->mtMaxRecord = 0;
196 physDev->mh->mtNoParameters = 0;
198 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
200 return dc;
204 /**********************************************************************
205 * MFDRV_DeleteDC
207 static BOOL CDECL MFDRV_DeleteDC( PHYSDEV dev )
209 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
210 DWORD index;
212 HeapFree( GetProcessHeap(), 0, physDev->mh );
213 for(index = 0; index < physDev->handles_size; index++)
214 if(physDev->handles[index])
215 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
216 HeapFree( GetProcessHeap(), 0, physDev->handles );
217 HeapFree( GetProcessHeap(), 0, physDev );
218 return TRUE;
222 /**********************************************************************
223 * CreateMetaFileW (GDI32.@)
225 * Create a new DC and associate it with a metafile. Pass a filename
226 * to create a disk-based metafile, NULL to create a memory metafile.
228 * PARAMS
229 * filename [I] Filename of disk metafile
231 * RETURNS
232 * A handle to the metafile DC if successful, NULL on failure.
234 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
236 HDC ret;
237 DC *dc;
238 METAFILEDRV_PDEVICE *physDev;
239 HANDLE hFile;
241 TRACE("%s\n", debugstr_w(filename) );
243 if (!(dc = MFDRV_AllocMetaFile())) return 0;
244 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
246 if (filename) /* disk based metafile */
248 physDev->mh->mtType = METAFILE_DISK;
249 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
250 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
251 free_dc_ptr( dc );
252 return 0;
254 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
255 NULL )) {
256 free_dc_ptr( dc );
257 return 0;
259 physDev->hFile = hFile;
261 /* Grow METAHEADER to include filename */
262 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
264 else /* memory based metafile */
265 physDev->mh->mtType = METAFILE_MEMORY;
267 TRACE("returning %p\n", physDev->dev.hdc);
268 ret = physDev->dev.hdc;
269 release_dc_ptr( dc );
270 return ret;
273 /**********************************************************************
274 * CreateMetaFileA (GDI32.@)
276 * See CreateMetaFileW.
278 HDC WINAPI CreateMetaFileA(LPCSTR filename)
280 LPWSTR filenameW;
281 DWORD len;
282 HDC hReturnDC;
284 if (!filename) return CreateMetaFileW(NULL);
286 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
287 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
288 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
290 hReturnDC = CreateMetaFileW(filenameW);
292 HeapFree( GetProcessHeap(), 0, filenameW );
294 return hReturnDC;
298 /**********************************************************************
299 * MFDRV_CloseMetaFile
301 static DC *MFDRV_CloseMetaFile( HDC hdc )
303 DC *dc;
304 METAFILEDRV_PDEVICE *physDev;
306 TRACE("(%p)\n", hdc );
308 if (!(dc = get_dc_ptr( hdc ))) return NULL;
309 if (dc->header.type != OBJ_METADC)
311 release_dc_ptr( dc );
312 return NULL;
314 if (dc->refcount != 1)
316 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
317 release_dc_ptr( dc );
318 return NULL;
320 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
322 /* Construct the end of metafile record - this is documented
323 * in SDK Knowledgebase Q99334.
326 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
328 free_dc_ptr( dc );
329 return 0;
332 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
334 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
335 free_dc_ptr( dc );
336 return 0;
339 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
340 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
341 NULL, NULL)) {
342 free_dc_ptr( dc );
343 return 0;
345 CloseHandle(physDev->hFile);
346 physDev->mh->mtType = METAFILE_DISK;
349 return dc;
352 /******************************************************************
353 * CloseMetaFile (GDI32.@)
355 * Stop recording graphics operations in metafile associated with
356 * hdc and retrieve metafile.
358 * PARAMS
359 * hdc [I] Metafile DC to close
361 * RETURNS
362 * Handle of newly created metafile on success, NULL on failure.
364 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
366 HMETAFILE hmf;
367 METAFILEDRV_PDEVICE *physDev;
368 DC *dc = MFDRV_CloseMetaFile(hdc);
369 if (!dc) return 0;
370 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
372 /* Now allocate a global handle for the metafile */
374 hmf = MF_Create_HMETAFILE( physDev->mh );
376 physDev->mh = NULL; /* So it won't be deleted */
377 free_dc_ptr( dc );
378 return hmf;
382 /******************************************************************
383 * MFDRV_WriteRecord
385 * Warning: this function can change the pointer to the metafile header.
387 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
389 DWORD len, size;
390 METAHEADER *mh;
391 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
393 switch(physDev->mh->mtType)
395 case METAFILE_MEMORY:
396 len = physDev->mh->mtSize * 2 + rlen;
397 /* reallocate memory if needed */
398 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
399 if (len > size)
401 /*expand size*/
402 size += size / 2 + rlen;
403 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
404 if (!mh) return FALSE;
405 physDev->mh = mh;
406 TRACE("Reallocated metafile: new size is %d\n",size);
408 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
409 break;
410 case METAFILE_DISK:
411 TRACE("Writing record to disk\n");
412 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
413 return FALSE;
414 break;
415 default:
416 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
417 return FALSE;
420 physDev->mh->mtSize += rlen / 2;
421 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
422 return TRUE;
426 /******************************************************************
427 * MFDRV_MetaParam0
430 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
432 char buffer[8];
433 METARECORD *mr = (METARECORD *)&buffer;
435 mr->rdSize = 3;
436 mr->rdFunction = func;
437 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
441 /******************************************************************
442 * MFDRV_MetaParam1
444 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
446 char buffer[8];
447 METARECORD *mr = (METARECORD *)&buffer;
449 mr->rdSize = 4;
450 mr->rdFunction = func;
451 *(mr->rdParm) = param1;
452 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
456 /******************************************************************
457 * MFDRV_MetaParam2
459 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
461 char buffer[10];
462 METARECORD *mr = (METARECORD *)&buffer;
464 mr->rdSize = 5;
465 mr->rdFunction = func;
466 *(mr->rdParm) = param2;
467 *(mr->rdParm + 1) = param1;
468 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
472 /******************************************************************
473 * MFDRV_MetaParam4
476 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
477 short param3, short param4)
479 char buffer[14];
480 METARECORD *mr = (METARECORD *)&buffer;
482 mr->rdSize = 7;
483 mr->rdFunction = func;
484 *(mr->rdParm) = param4;
485 *(mr->rdParm + 1) = param3;
486 *(mr->rdParm + 2) = param2;
487 *(mr->rdParm + 3) = param1;
488 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
492 /******************************************************************
493 * MFDRV_MetaParam6
496 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
497 short param3, short param4, short param5, short param6)
499 char buffer[18];
500 METARECORD *mr = (METARECORD *)&buffer;
502 mr->rdSize = 9;
503 mr->rdFunction = func;
504 *(mr->rdParm) = param6;
505 *(mr->rdParm + 1) = param5;
506 *(mr->rdParm + 2) = param4;
507 *(mr->rdParm + 3) = param3;
508 *(mr->rdParm + 4) = param2;
509 *(mr->rdParm + 5) = param1;
510 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
514 /******************************************************************
515 * MFDRV_MetaParam8
517 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
518 short param3, short param4, short param5,
519 short param6, short param7, short param8)
521 char buffer[22];
522 METARECORD *mr = (METARECORD *)&buffer;
524 mr->rdSize = 11;
525 mr->rdFunction = func;
526 *(mr->rdParm) = param8;
527 *(mr->rdParm + 1) = param7;
528 *(mr->rdParm + 2) = param6;
529 *(mr->rdParm + 3) = param5;
530 *(mr->rdParm + 4) = param4;
531 *(mr->rdParm + 5) = param3;
532 *(mr->rdParm + 6) = param2;
533 *(mr->rdParm + 7) = param1;
534 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
538 /**********************************************************************
539 * MFDRV_ExtEscape
541 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
542 INT cbOutput, LPVOID out_data )
544 METARECORD *mr;
545 DWORD len;
546 INT ret;
548 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
550 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
551 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
552 mr->rdSize = len / 2;
553 mr->rdFunction = META_ESCAPE;
554 mr->rdParm[0] = nEscape;
555 mr->rdParm[1] = cbInput;
556 memcpy(&(mr->rdParm[2]), in_data, cbInput);
557 ret = MFDRV_WriteRecord( dev, mr, len);
558 HeapFree(GetProcessHeap(), 0, mr);
559 return ret;
563 /******************************************************************
564 * MFDRV_GetDeviceCaps
566 *A very simple implementation that returns DT_METAFILE
568 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
570 switch(cap)
572 case TECHNOLOGY:
573 return DT_METAFILE;
574 case TEXTCAPS:
575 return 0;
576 default:
577 TRACE(" unsupported capability %d, will return 0\n", cap );
579 return 0;