push 5c57339901c8d80068fee18365e7574a1e8d922a
[wine/hacks.git] / dlls / gdi32 / mfdrv / init.c
blob59c973a6b340ad8e3c3c02b59b2e34253b514237
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 "gdi_private.h"
27 #include "mfdrv/metafiledrv.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
32 static const DC_FUNCTIONS MFDRV_Funcs =
34 NULL, /* pAbortDoc */
35 MFDRV_AbortPath, /* pAbortPath */
36 NULL, /* pAlphaBlend */
37 NULL, /* pAngleArc */
38 MFDRV_Arc, /* pArc */
39 NULL, /* pArcTo */
40 MFDRV_BeginPath, /* pBeginPath */
41 NULL, /* pBitBlt */
42 NULL, /* pChoosePixelFormat */
43 MFDRV_Chord, /* pChord */
44 MFDRV_CloseFigure, /* pCloseFigure */
45 NULL, /* pCreateBitmap */
46 NULL, /* pCreateDC */
47 NULL, /* pCreateDIBSection */
48 NULL, /* pDeleteBitmap */
49 NULL, /* pDeleteDC */
50 MFDRV_DeleteObject, /* pDeleteObject */
51 NULL, /* pDescribePixelFormat */
52 NULL, /* pDeviceCapabilities */
53 MFDRV_Ellipse, /* pEllipse */
54 NULL, /* pEndDoc */
55 NULL, /* pEndPage */
56 MFDRV_EndPath, /* pEndPath */
57 NULL, /* pEnumDeviceFonts */
58 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
59 NULL, /* pExtDeviceMode */
60 MFDRV_ExtEscape, /* pExtEscape */
61 MFDRV_ExtFloodFill, /* pExtFloodFill */
62 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
63 MFDRV_ExtTextOut, /* pExtTextOut */
64 MFDRV_FillPath, /* pFillPath */
65 MFDRV_FillRgn, /* pFillRgn */
66 MFDRV_FlattenPath, /* pFlattenPath */
67 MFDRV_FrameRgn, /* pFrameRgn */
68 NULL, /* pGdiComment */
69 NULL, /* pGetBitmapBits */
70 NULL, /* pGetCharWidth */
71 NULL, /* pGetDCOrgEx */
72 NULL, /* pGetDIBColorTable */
73 NULL, /* pGetDIBits */
74 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
75 NULL, /* pGetDeviceGammaRamp */
76 NULL, /* pGetICMProfile */
77 NULL, /* pGetNearestColor */
78 NULL, /* pGetPixel */
79 NULL, /* pGetPixelFormat */
80 NULL, /* pGetSystemPaletteEntries */
81 NULL, /* pGetTextExtentExPoint */
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 NULL, /* pUnrealizePalette */
154 MFDRV_WidenPath /* pWidenPath */
159 /**********************************************************************
160 * MFDRV_AllocMetaFile
162 static DC *MFDRV_AllocMetaFile(void)
164 DC *dc;
165 METAFILEDRV_PDEVICE *physDev;
167 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
169 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
170 if (!physDev)
172 free_dc_ptr( dc );
173 return NULL;
175 dc->physDev = (PHYSDEV)physDev;
176 physDev->hdc = dc->hSelf;
178 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
180 HeapFree( GetProcessHeap(), 0, physDev );
181 free_dc_ptr( dc );
182 return NULL;
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(dc->hSelf, 0, 0, 0, 0);
200 return dc;
204 /**********************************************************************
205 * MFDRV_DeleteDC
207 static BOOL MFDRV_DeleteDC( DC *dc )
209 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
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], physDev->hdc);
216 HeapFree( GetProcessHeap(), 0, physDev->handles );
217 HeapFree( GetProcessHeap(), 0, physDev );
218 dc->physDev = NULL;
219 free_dc_ptr( dc );
220 return TRUE;
224 /**********************************************************************
225 * CreateMetaFileW (GDI32.@)
227 * Create a new DC and associate it with a metafile. Pass a filename
228 * to create a disk-based metafile, NULL to create a memory metafile.
230 * PARAMS
231 * filename [I] Filename of disk metafile
233 * RETURNS
234 * A handle to the metafile DC if successful, NULL on failure.
236 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
238 HDC ret;
239 DC *dc;
240 METAFILEDRV_PDEVICE *physDev;
241 HANDLE hFile;
243 TRACE("%s\n", debugstr_w(filename) );
245 if (!(dc = MFDRV_AllocMetaFile())) return 0;
246 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
248 if (filename) /* disk based metafile */
250 physDev->mh->mtType = METAFILE_DISK;
251 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
252 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
253 MFDRV_DeleteDC( dc );
254 return 0;
256 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
257 NULL )) {
258 MFDRV_DeleteDC( dc );
259 return 0;
261 physDev->hFile = hFile;
263 /* Grow METAHEADER to include filename */
264 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
266 else /* memory based metafile */
267 physDev->mh->mtType = METAFILE_MEMORY;
269 TRACE("returning %p\n", dc->hSelf);
270 ret = dc->hSelf;
271 release_dc_ptr( dc );
272 return ret;
275 /**********************************************************************
276 * CreateMetaFileA (GDI32.@)
278 * See CreateMetaFileW.
280 HDC WINAPI CreateMetaFileA(LPCSTR filename)
282 LPWSTR filenameW;
283 DWORD len;
284 HDC hReturnDC;
286 if (!filename) return CreateMetaFileW(NULL);
288 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
289 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
290 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
292 hReturnDC = CreateMetaFileW(filenameW);
294 HeapFree( GetProcessHeap(), 0, filenameW );
296 return hReturnDC;
300 /**********************************************************************
301 * MFDRV_CloseMetaFile
303 static DC *MFDRV_CloseMetaFile( HDC hdc )
305 DC *dc;
306 METAFILEDRV_PDEVICE *physDev;
308 TRACE("(%p)\n", hdc );
310 if (!(dc = get_dc_ptr( hdc ))) return NULL;
311 if (dc->header.type != OBJ_METADC)
313 release_dc_ptr( dc );
314 return NULL;
316 if (dc->refcount != 1)
318 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
319 release_dc_ptr( dc );
320 return NULL;
322 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
324 /* Construct the end of metafile record - this is documented
325 * in SDK Knowledgebase Q99334.
328 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
330 MFDRV_DeleteDC( dc );
331 return 0;
334 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
336 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
337 MFDRV_DeleteDC( dc );
338 return 0;
341 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
342 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
343 NULL, NULL)) {
344 MFDRV_DeleteDC( dc );
345 return 0;
347 CloseHandle(physDev->hFile);
348 physDev->mh->mtType = METAFILE_DISK;
351 return dc;
354 /******************************************************************
355 * CloseMetaFile (GDI32.@)
357 * Stop recording graphics operations in metafile associated with
358 * hdc and retrieve metafile.
360 * PARAMS
361 * hdc [I] Metafile DC to close
363 * RETURNS
364 * Handle of newly created metafile on success, NULL on failure.
366 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
368 HMETAFILE hmf;
369 METAFILEDRV_PDEVICE *physDev;
370 DC *dc = MFDRV_CloseMetaFile(hdc);
371 if (!dc) return 0;
372 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
374 /* Now allocate a global handle for the metafile */
376 hmf = MF_Create_HMETAFILE( physDev->mh );
378 physDev->mh = NULL; /* So it won't be deleted */
379 MFDRV_DeleteDC( dc );
380 return hmf;
384 /******************************************************************
385 * MFDRV_WriteRecord
387 * Warning: this function can change the pointer to the metafile header.
389 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
391 DWORD len, size;
392 METAHEADER *mh;
393 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
395 switch(physDev->mh->mtType)
397 case METAFILE_MEMORY:
398 len = physDev->mh->mtSize * 2 + rlen;
399 /* reallocate memory if needed */
400 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
401 if (len > size)
403 /*expand size*/
404 size += size / 2 + rlen;
405 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
406 if (!mh) return FALSE;
407 physDev->mh = mh;
408 TRACE("Reallocated metafile: new size is %d\n",size);
410 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
411 break;
412 case METAFILE_DISK:
413 TRACE("Writing record to disk\n");
414 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
415 return FALSE;
416 break;
417 default:
418 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
419 return FALSE;
422 physDev->mh->mtSize += rlen / 2;
423 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
424 return TRUE;
428 /******************************************************************
429 * MFDRV_MetaParam0
432 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
434 char buffer[8];
435 METARECORD *mr = (METARECORD *)&buffer;
437 mr->rdSize = 3;
438 mr->rdFunction = func;
439 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
443 /******************************************************************
444 * MFDRV_MetaParam1
446 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
448 char buffer[8];
449 METARECORD *mr = (METARECORD *)&buffer;
451 mr->rdSize = 4;
452 mr->rdFunction = func;
453 *(mr->rdParm) = param1;
454 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
458 /******************************************************************
459 * MFDRV_MetaParam2
461 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
463 char buffer[10];
464 METARECORD *mr = (METARECORD *)&buffer;
466 mr->rdSize = 5;
467 mr->rdFunction = func;
468 *(mr->rdParm) = param2;
469 *(mr->rdParm + 1) = param1;
470 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
474 /******************************************************************
475 * MFDRV_MetaParam4
478 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
479 short param3, short param4)
481 char buffer[14];
482 METARECORD *mr = (METARECORD *)&buffer;
484 mr->rdSize = 7;
485 mr->rdFunction = func;
486 *(mr->rdParm) = param4;
487 *(mr->rdParm + 1) = param3;
488 *(mr->rdParm + 2) = param2;
489 *(mr->rdParm + 3) = param1;
490 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
494 /******************************************************************
495 * MFDRV_MetaParam6
498 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
499 short param3, short param4, short param5, short param6)
501 char buffer[18];
502 METARECORD *mr = (METARECORD *)&buffer;
504 mr->rdSize = 9;
505 mr->rdFunction = func;
506 *(mr->rdParm) = param6;
507 *(mr->rdParm + 1) = param5;
508 *(mr->rdParm + 2) = param4;
509 *(mr->rdParm + 3) = param3;
510 *(mr->rdParm + 4) = param2;
511 *(mr->rdParm + 5) = param1;
512 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
516 /******************************************************************
517 * MFDRV_MetaParam8
519 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
520 short param3, short param4, short param5,
521 short param6, short param7, short param8)
523 char buffer[22];
524 METARECORD *mr = (METARECORD *)&buffer;
526 mr->rdSize = 11;
527 mr->rdFunction = func;
528 *(mr->rdParm) = param8;
529 *(mr->rdParm + 1) = param7;
530 *(mr->rdParm + 2) = param6;
531 *(mr->rdParm + 3) = param5;
532 *(mr->rdParm + 4) = param4;
533 *(mr->rdParm + 5) = param3;
534 *(mr->rdParm + 6) = param2;
535 *(mr->rdParm + 7) = param1;
536 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
540 /**********************************************************************
541 * MFDRV_ExtEscape
543 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
544 INT cbOutput, LPVOID out_data )
546 METARECORD *mr;
547 DWORD len;
548 INT ret;
550 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
552 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
553 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
554 mr->rdSize = len / 2;
555 mr->rdFunction = META_ESCAPE;
556 mr->rdParm[0] = nEscape;
557 mr->rdParm[1] = cbInput;
558 memcpy(&(mr->rdParm[2]), in_data, cbInput);
559 ret = MFDRV_WriteRecord( dev, mr, len);
560 HeapFree(GetProcessHeap(), 0, mr);
561 return ret;
565 /******************************************************************
566 * MFDRV_GetDeviceCaps
568 *A very simple implementation that returns DT_METAFILE
570 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
572 switch(cap)
574 case TECHNOLOGY:
575 return DT_METAFILE;
576 case TEXTCAPS:
577 return 0;
578 default:
579 TRACE(" unsupported capability %d, will return 0\n", cap );
581 return 0;