opengl32: Add the concept of a WGL driver.
[wine/multimedia.git] / dlls / gdi32 / mfdrv / init.c
blobef840beda93fafc3507a1d4cf764d136d2776b9a
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 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
114 NULL, /* pCreateDC */
115 MFDRV_DeleteDC, /* pDeleteDC */
116 MFDRV_DeleteObject, /* pDeleteObject */
117 NULL, /* pDescribePixelFormat */
118 NULL, /* pDeviceCapabilities */
119 MFDRV_Ellipse, /* pEllipse */
120 NULL, /* pEndDoc */
121 NULL, /* pEndPage */
122 MFDRV_EndPath, /* pEndPath */
123 NULL, /* pEnumFonts */
124 NULL, /* pEnumICMProfiles */
125 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
126 NULL, /* pExtDeviceMode */
127 MFDRV_ExtEscape, /* pExtEscape */
128 MFDRV_ExtFloodFill, /* pExtFloodFill */
129 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
130 MFDRV_ExtTextOut, /* pExtTextOut */
131 MFDRV_FillPath, /* pFillPath */
132 MFDRV_FillRgn, /* pFillRgn */
133 MFDRV_FlattenPath, /* pFlattenPath */
134 NULL, /* pFontIsLinked */
135 MFDRV_FrameRgn, /* pFrameRgn */
136 NULL, /* pGdiComment */
137 NULL, /* pGdiRealizationInfo */
138 MFDRV_GetBoundsRect, /* pGetBoundsRect */
139 NULL, /* pGetCharABCWidths */
140 NULL, /* pGetCharABCWidthsI */
141 NULL, /* pGetCharWidth */
142 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
143 NULL, /* pGetDeviceGammaRamp */
144 NULL, /* pGetFontData */
145 NULL, /* pGetFontUnicodeRanges */
146 NULL, /* pGetGlyphIndices */
147 NULL, /* pGetGlyphOutline */
148 NULL, /* pGetICMProfile */
149 NULL, /* pGetImage */
150 NULL, /* pGetKerningPairs */
151 NULL, /* pGetNearestColor */
152 NULL, /* pGetOutlineTextMetrics */
153 NULL, /* pGetPixel */
154 NULL, /* pGetPixelFormat */
155 NULL, /* pGetSystemPaletteEntries */
156 NULL, /* pGetTextCharsetInfo */
157 NULL, /* pGetTextExtentExPoint */
158 NULL, /* pGetTextExtentExPointI */
159 NULL, /* pGetTextFace */
160 NULL, /* pGetTextMetrics */
161 NULL, /* pGradientFill */
162 MFDRV_IntersectClipRect, /* pIntersectClipRect */
163 MFDRV_InvertRgn, /* pInvertRgn */
164 MFDRV_LineTo, /* pLineTo */
165 NULL, /* pModifyWorldTransform */
166 MFDRV_MoveTo, /* pMoveTo */
167 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
168 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
169 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
170 MFDRV_PaintRgn, /* pPaintRgn */
171 MFDRV_PatBlt, /* pPatBlt */
172 MFDRV_Pie, /* pPie */
173 MFDRV_PolyBezier, /* pPolyBezier */
174 MFDRV_PolyBezierTo, /* pPolyBezierTo */
175 NULL, /* pPolyDraw */
176 MFDRV_PolyPolygon, /* pPolyPolygon */
177 NULL, /* pPolyPolyline */
178 MFDRV_Polygon, /* pPolygon */
179 MFDRV_Polyline, /* pPolyline */
180 NULL, /* pPolylineTo */
181 NULL, /* pPutImage */
182 NULL, /* pRealizeDefaultPalette */
183 MFDRV_RealizePalette, /* pRealizePalette */
184 MFDRV_Rectangle, /* pRectangle */
185 NULL, /* pResetDC */
186 MFDRV_RestoreDC, /* pRestoreDC */
187 MFDRV_RoundRect, /* pRoundRect */
188 MFDRV_SaveDC, /* pSaveDC */
189 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
190 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
191 MFDRV_SelectBitmap, /* pSelectBitmap */
192 MFDRV_SelectBrush, /* pSelectBrush */
193 MFDRV_SelectClipPath, /* pSelectClipPath */
194 MFDRV_SelectFont, /* pSelectFont */
195 MFDRV_SelectPalette, /* pSelectPalette */
196 MFDRV_SelectPen, /* pSelectPen */
197 NULL, /* pSetArcDirection */
198 MFDRV_SetBkColor, /* pSetBkColor */
199 MFDRV_SetBkMode, /* pSetBkMode */
200 MFDRV_SetBoundsRect, /* pSetBoundsRect */
201 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
202 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
203 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
204 NULL, /* pSetDeviceClipping */
205 NULL, /* pSetDeviceGammaRamp */
206 NULL, /* pSetLayout */
207 MFDRV_SetMapMode, /* pSetMapMode */
208 MFDRV_SetMapperFlags, /* pSetMapperFlags */
209 MFDRV_SetPixel, /* pSetPixel */
210 NULL, /* pSetPixelFormat */
211 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
212 MFDRV_SetROP2, /* pSetROP2 */
213 MFDRV_SetRelAbs, /* pSetRelAbs */
214 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
215 MFDRV_SetTextAlign, /* pSetTextAlign */
216 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
217 MFDRV_SetTextColor, /* pSetTextColor */
218 MFDRV_SetTextJustification, /* pSetTextJustification */
219 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
220 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
221 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
222 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
223 NULL, /* pSetWorldTransform */
224 NULL, /* pStartDoc */
225 NULL, /* pStartPage */
226 MFDRV_StretchBlt, /* pStretchBlt */
227 MFDRV_StretchDIBits, /* pStretchDIBits */
228 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
229 MFDRV_StrokePath, /* pStrokePath */
230 NULL, /* pSwapBuffers */
231 NULL, /* pUnrealizePalette */
232 MFDRV_WidenPath, /* pWidenPath */
233 NULL, /* pwglCreateContext */
234 NULL, /* pwglCreateContextAttribsARB */
235 NULL, /* pwglGetProcAddress */
236 NULL, /* wine_get_wgl_driver */
237 GDI_PRIORITY_GRAPHICS_DRV /* priority */
242 /**********************************************************************
243 * MFDRV_AllocMetaFile
245 static DC *MFDRV_AllocMetaFile(void)
247 DC *dc;
248 METAFILEDRV_PDEVICE *physDev;
250 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
252 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
253 if (!physDev)
255 free_dc_ptr( dc );
256 return NULL;
258 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
260 HeapFree( GetProcessHeap(), 0, physDev );
261 free_dc_ptr( dc );
262 return NULL;
265 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
267 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
268 physDev->handles_size = HANDLE_LIST_INC;
269 physDev->cur_handles = 0;
271 physDev->hFile = 0;
273 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
274 physDev->mh->mtVersion = 0x0300;
275 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
276 physDev->mh->mtNoObjects = 0;
277 physDev->mh->mtMaxRecord = 0;
278 physDev->mh->mtNoParameters = 0;
280 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
282 return dc;
286 /**********************************************************************
287 * MFDRV_CreateCompatibleDC
289 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
291 /* not supported on metafile DCs */
292 return FALSE;
296 /**********************************************************************
297 * MFDRV_DeleteDC
299 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
301 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
302 DWORD index;
304 HeapFree( GetProcessHeap(), 0, physDev->mh );
305 for(index = 0; index < physDev->handles_size; index++)
306 if(physDev->handles[index])
307 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
308 HeapFree( GetProcessHeap(), 0, physDev->handles );
309 HeapFree( GetProcessHeap(), 0, physDev );
310 return TRUE;
314 /**********************************************************************
315 * CreateMetaFileW (GDI32.@)
317 * Create a new DC and associate it with a metafile. Pass a filename
318 * to create a disk-based metafile, NULL to create a memory metafile.
320 * PARAMS
321 * filename [I] Filename of disk metafile
323 * RETURNS
324 * A handle to the metafile DC if successful, NULL on failure.
326 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
328 HDC ret;
329 DC *dc;
330 METAFILEDRV_PDEVICE *physDev;
331 HANDLE hFile;
333 TRACE("%s\n", debugstr_w(filename) );
335 if (!(dc = MFDRV_AllocMetaFile())) return 0;
336 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
338 if (filename) /* disk based metafile */
340 physDev->mh->mtType = METAFILE_DISK;
341 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
342 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
343 free_dc_ptr( dc );
344 return 0;
346 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
347 NULL )) {
348 free_dc_ptr( dc );
349 return 0;
351 physDev->hFile = hFile;
353 /* Grow METAHEADER to include filename */
354 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
356 else /* memory based metafile */
357 physDev->mh->mtType = METAFILE_MEMORY;
359 TRACE("returning %p\n", physDev->dev.hdc);
360 ret = physDev->dev.hdc;
361 release_dc_ptr( dc );
362 return ret;
365 /**********************************************************************
366 * CreateMetaFileA (GDI32.@)
368 * See CreateMetaFileW.
370 HDC WINAPI CreateMetaFileA(LPCSTR filename)
372 LPWSTR filenameW;
373 DWORD len;
374 HDC hReturnDC;
376 if (!filename) return CreateMetaFileW(NULL);
378 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
379 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
380 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
382 hReturnDC = CreateMetaFileW(filenameW);
384 HeapFree( GetProcessHeap(), 0, filenameW );
386 return hReturnDC;
390 /**********************************************************************
391 * MFDRV_CloseMetaFile
393 static DC *MFDRV_CloseMetaFile( HDC hdc )
395 DC *dc;
396 METAFILEDRV_PDEVICE *physDev;
398 TRACE("(%p)\n", hdc );
400 if (!(dc = get_dc_ptr( hdc ))) return NULL;
401 if (dc->header.type != OBJ_METADC)
403 release_dc_ptr( dc );
404 return NULL;
406 if (dc->refcount != 1)
408 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
409 release_dc_ptr( dc );
410 return NULL;
412 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
414 /* Construct the end of metafile record - this is documented
415 * in SDK Knowledgebase Q99334.
418 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
420 free_dc_ptr( dc );
421 return 0;
424 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
426 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
427 free_dc_ptr( dc );
428 return 0;
431 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
432 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
433 NULL, NULL)) {
434 free_dc_ptr( dc );
435 return 0;
437 CloseHandle(physDev->hFile);
438 physDev->mh->mtType = METAFILE_DISK;
441 return dc;
444 /******************************************************************
445 * CloseMetaFile (GDI32.@)
447 * Stop recording graphics operations in metafile associated with
448 * hdc and retrieve metafile.
450 * PARAMS
451 * hdc [I] Metafile DC to close
453 * RETURNS
454 * Handle of newly created metafile on success, NULL on failure.
456 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
458 HMETAFILE hmf;
459 METAFILEDRV_PDEVICE *physDev;
460 DC *dc = MFDRV_CloseMetaFile(hdc);
461 if (!dc) return 0;
462 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
464 /* Now allocate a global handle for the metafile */
466 hmf = MF_Create_HMETAFILE( physDev->mh );
468 physDev->mh = NULL; /* So it won't be deleted */
469 free_dc_ptr( dc );
470 return hmf;
474 /******************************************************************
475 * MFDRV_WriteRecord
477 * Warning: this function can change the pointer to the metafile header.
479 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
481 DWORD len, size;
482 METAHEADER *mh;
483 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
485 switch(physDev->mh->mtType)
487 case METAFILE_MEMORY:
488 len = physDev->mh->mtSize * 2 + rlen;
489 /* reallocate memory if needed */
490 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
491 if (len > size)
493 /*expand size*/
494 size += size / 2 + rlen;
495 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
496 if (!mh) return FALSE;
497 physDev->mh = mh;
498 TRACE("Reallocated metafile: new size is %d\n",size);
500 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
501 break;
502 case METAFILE_DISK:
503 TRACE("Writing record to disk\n");
504 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
505 return FALSE;
506 break;
507 default:
508 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
509 return FALSE;
512 physDev->mh->mtSize += rlen / 2;
513 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
514 return TRUE;
518 /******************************************************************
519 * MFDRV_MetaParam0
522 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
524 char buffer[8];
525 METARECORD *mr = (METARECORD *)&buffer;
527 mr->rdSize = 3;
528 mr->rdFunction = func;
529 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
533 /******************************************************************
534 * MFDRV_MetaParam1
536 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
538 char buffer[8];
539 METARECORD *mr = (METARECORD *)&buffer;
540 WORD *params = mr->rdParm;
542 mr->rdSize = 4;
543 mr->rdFunction = func;
544 params[0] = param1;
545 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
549 /******************************************************************
550 * MFDRV_MetaParam2
552 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
554 char buffer[10];
555 METARECORD *mr = (METARECORD *)&buffer;
556 WORD *params = mr->rdParm;
558 mr->rdSize = 5;
559 mr->rdFunction = func;
560 params[0] = param2;
561 params[1] = param1;
562 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
566 /******************************************************************
567 * MFDRV_MetaParam4
570 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
571 short param3, short param4)
573 char buffer[14];
574 METARECORD *mr = (METARECORD *)&buffer;
575 WORD *params = mr->rdParm;
577 mr->rdSize = 7;
578 mr->rdFunction = func;
579 params[0] = param4;
580 params[1] = param3;
581 params[2] = param2;
582 params[3] = param1;
583 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
587 /******************************************************************
588 * MFDRV_MetaParam6
591 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
592 short param3, short param4, short param5, short param6)
594 char buffer[18];
595 METARECORD *mr = (METARECORD *)&buffer;
596 WORD *params = mr->rdParm;
598 mr->rdSize = 9;
599 mr->rdFunction = func;
600 params[0] = param6;
601 params[1] = param5;
602 params[2] = param4;
603 params[3] = param3;
604 params[4] = param2;
605 params[5] = param1;
606 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
610 /******************************************************************
611 * MFDRV_MetaParam8
613 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
614 short param3, short param4, short param5,
615 short param6, short param7, short param8)
617 char buffer[22];
618 METARECORD *mr = (METARECORD *)&buffer;
619 WORD *params = mr->rdParm;
621 mr->rdSize = 11;
622 mr->rdFunction = func;
623 params[0] = param8;
624 params[1] = param7;
625 params[2] = param6;
626 params[3] = param5;
627 params[4] = param4;
628 params[5] = param3;
629 params[6] = param2;
630 params[7] = param1;
631 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);