push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / gdi32 / mfdrv / init.c
blobad29cb0cdf8d277c02a3d1e7318935558e95d338
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 "wine/winbase16.h"
27 #include "wownt32.h"
28 #include "gdi_private.h"
29 #include "mfdrv/metafiledrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
34 static const DC_FUNCTIONS MFDRV_Funcs =
36 NULL, /* pAbortDoc */
37 MFDRV_AbortPath, /* pAbortPath */
38 NULL, /* pAlphaBlend */
39 NULL, /* pAngleArc */
40 MFDRV_Arc, /* pArc */
41 NULL, /* pArcTo */
42 MFDRV_BeginPath, /* pBeginPath */
43 MFDRV_BitBlt, /* pBitBlt */
44 NULL, /* pChoosePixelFormat */
45 MFDRV_Chord, /* pChord */
46 MFDRV_CloseFigure, /* pCloseFigure */
47 NULL, /* pCreateBitmap */
48 NULL, /* pCreateDC */
49 NULL, /* pCreateDIBSection */
50 NULL, /* pDeleteBitmap */
51 NULL, /* 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 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
61 NULL, /* pExtDeviceMode */
62 MFDRV_ExtEscape, /* pExtEscape */
63 MFDRV_ExtFloodFill, /* pExtFloodFill */
64 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
65 MFDRV_ExtTextOut, /* pExtTextOut */
66 MFDRV_FillPath, /* pFillPath */
67 MFDRV_FillRgn, /* pFillRgn */
68 MFDRV_FlattenPath, /* pFlattenPath */
69 MFDRV_FrameRgn, /* pFrameRgn */
70 NULL, /* pGdiComment */
71 NULL, /* pGetBitmapBits */
72 NULL, /* pGetCharWidth */
73 NULL, /* pGetDCOrgEx */
74 NULL, /* pGetDIBColorTable */
75 NULL, /* pGetDIBits */
76 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
77 NULL, /* pGetDeviceGammaRamp */
78 NULL, /* pGetICMProfile */
79 NULL, /* pGetNearestColor */
80 NULL, /* pGetPixel */
81 NULL, /* pGetPixelFormat */
82 NULL, /* pGetSystemPaletteEntries */
83 NULL, /* pGetTextExtentExPoint */
84 NULL, /* pGetTextMetrics */
85 MFDRV_IntersectClipRect, /* pIntersectClipRect */
86 MFDRV_InvertRgn, /* pInvertRgn */
87 MFDRV_LineTo, /* pLineTo */
88 NULL, /* pModifyWorldTransform */
89 MFDRV_MoveTo, /* pMoveTo */
90 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
91 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
92 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
93 MFDRV_PaintRgn, /* pPaintRgn */
94 MFDRV_PatBlt, /* pPatBlt */
95 MFDRV_Pie, /* pPie */
96 MFDRV_PolyBezier, /* pPolyBezier */
97 MFDRV_PolyBezierTo, /* pPolyBezierTo */
98 NULL, /* pPolyDraw */
99 MFDRV_PolyPolygon, /* pPolyPolygon */
100 NULL, /* pPolyPolyline */
101 MFDRV_Polygon, /* pPolygon */
102 MFDRV_Polyline, /* pPolyline */
103 NULL, /* pPolylineTo */
104 NULL, /* pRealizeDefaultPalette */
105 MFDRV_RealizePalette, /* pRealizePalette */
106 MFDRV_Rectangle, /* pRectangle */
107 NULL, /* pResetDC */
108 MFDRV_RestoreDC, /* pRestoreDC */
109 MFDRV_RoundRect, /* pRoundRect */
110 MFDRV_SaveDC, /* pSaveDC */
111 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
112 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
113 MFDRV_SelectBitmap, /* pSelectBitmap */
114 MFDRV_SelectBrush, /* pSelectBrush */
115 MFDRV_SelectClipPath, /* pSelectClipPath */
116 MFDRV_SelectFont, /* pSelectFont */
117 MFDRV_SelectPalette, /* pSelectPalette */
118 MFDRV_SelectPen, /* pSelectPen */
119 NULL, /* pSetArcDirection */
120 NULL, /* pSetBitmapBits */
121 MFDRV_SetBkColor, /* pSetBkColor */
122 MFDRV_SetBkMode, /* pSetBkMode */
123 NULL, /* pSetDCBrushColor*/
124 NULL, /* pSetDCOrg */
125 NULL, /* pSetDCPenColor*/
126 NULL, /* pSetDIBColorTable */
127 NULL, /* pSetDIBits */
128 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
129 NULL, /* pSetDeviceClipping */
130 NULL, /* pSetDeviceGammaRamp */
131 MFDRV_SetMapMode, /* pSetMapMode */
132 MFDRV_SetMapperFlags, /* pSetMapperFlags */
133 MFDRV_SetPixel, /* pSetPixel */
134 NULL, /* pSetPixelFormat */
135 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
136 MFDRV_SetROP2, /* pSetROP2 */
137 MFDRV_SetRelAbs, /* pSetRelAbs */
138 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
139 MFDRV_SetTextAlign, /* pSetTextAlign */
140 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
141 MFDRV_SetTextColor, /* pSetTextColor */
142 MFDRV_SetTextJustification, /* pSetTextJustification */
143 MFDRV_SetViewportExt, /* pSetViewportExt */
144 MFDRV_SetViewportOrg, /* pSetViewportOrg */
145 MFDRV_SetWindowExt, /* pSetWindowExt */
146 MFDRV_SetWindowOrg, /* pSetWindowOrg */
147 NULL, /* pSetWorldTransform */
148 NULL, /* pStartDoc */
149 NULL, /* pStartPage */
150 MFDRV_StretchBlt, /* pStretchBlt */
151 MFDRV_StretchDIBits, /* pStretchDIBits */
152 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
153 MFDRV_StrokePath, /* pStrokePath */
154 NULL, /* pSwapBuffers */
155 NULL, /* pUnrealizePalette */
156 MFDRV_WidenPath /* pWidenPath */
161 /**********************************************************************
162 * MFDRV_AllocMetaFile
164 static DC *MFDRV_AllocMetaFile(void)
166 DC *dc;
167 METAFILEDRV_PDEVICE *physDev;
169 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, OBJ_METADC ))) return NULL;
171 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
172 if (!physDev)
174 free_dc_ptr( dc );
175 return NULL;
177 dc->physDev = (PHYSDEV)physDev;
178 physDev->hdc = dc->hSelf;
180 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
182 HeapFree( GetProcessHeap(), 0, physDev );
183 free_dc_ptr( dc );
184 return NULL;
187 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
188 physDev->handles_size = HANDLE_LIST_INC;
189 physDev->cur_handles = 0;
191 physDev->hFile = 0;
193 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
194 physDev->mh->mtVersion = 0x0300;
195 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
196 physDev->mh->mtNoObjects = 0;
197 physDev->mh->mtMaxRecord = 0;
198 physDev->mh->mtNoParameters = 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 * MF_Create_HMETATFILE16
357 * Creates a HMETAFILE16 object from a METAHEADER
359 * HMETAFILE16s are Global memory handles.
361 HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
363 HMETAFILE16 hmf;
364 DWORD size = mh->mtSize * sizeof(WORD);
366 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
367 if(hmf)
369 METAHEADER *mh_dest = GlobalLock16(hmf);
370 memcpy(mh_dest, mh, size);
371 GlobalUnlock16(hmf);
373 HeapFree(GetProcessHeap(), 0, mh);
374 return hmf;
377 /******************************************************************
378 * CloseMetaFile (GDI.126)
380 * PARAMS
381 * hdc [I] Metafile DC to close
383 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
385 HMETAFILE16 hmf;
386 METAFILEDRV_PDEVICE *physDev;
387 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
388 if (!dc) return 0;
389 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
391 /* Now allocate a global handle for the metafile */
393 hmf = MF_Create_HMETAFILE16( physDev->mh );
395 physDev->mh = NULL; /* So it won't be deleted */
396 MFDRV_DeleteDC( dc );
397 return hmf;
400 /******************************************************************
401 * CloseMetaFile (GDI32.@)
403 * Stop recording graphics operations in metafile associated with
404 * hdc and retrieve metafile.
406 * PARAMS
407 * hdc [I] Metafile DC to close
409 * RETURNS
410 * Handle of newly created metafile on success, NULL on failure.
412 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
414 HMETAFILE hmf;
415 METAFILEDRV_PDEVICE *physDev;
416 DC *dc = MFDRV_CloseMetaFile(hdc);
417 if (!dc) return 0;
418 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
420 /* Now allocate a global handle for the metafile */
422 hmf = MF_Create_HMETAFILE( physDev->mh );
424 physDev->mh = NULL; /* So it won't be deleted */
425 MFDRV_DeleteDC( dc );
426 return hmf;
430 /******************************************************************
431 * MFDRV_WriteRecord
433 * Warning: this function can change the pointer to the metafile header.
435 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
437 DWORD len, size;
438 METAHEADER *mh;
439 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
441 switch(physDev->mh->mtType)
443 case METAFILE_MEMORY:
444 len = physDev->mh->mtSize * 2 + rlen;
445 /* reallocate memory if needed */
446 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
447 if (len > size)
449 /*expand size*/
450 size += size / 2 + rlen;
451 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
452 if (!mh) return FALSE;
453 physDev->mh = mh;
454 TRACE("Reallocated metafile: new size is %d\n",size);
456 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
457 break;
458 case METAFILE_DISK:
459 TRACE("Writing record to disk\n");
460 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
461 return FALSE;
462 break;
463 default:
464 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
465 return FALSE;
468 physDev->mh->mtSize += rlen / 2;
469 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
470 return TRUE;
474 /******************************************************************
475 * MFDRV_MetaParam0
478 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
480 char buffer[8];
481 METARECORD *mr = (METARECORD *)&buffer;
483 mr->rdSize = 3;
484 mr->rdFunction = func;
485 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
489 /******************************************************************
490 * MFDRV_MetaParam1
492 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
494 char buffer[8];
495 METARECORD *mr = (METARECORD *)&buffer;
497 mr->rdSize = 4;
498 mr->rdFunction = func;
499 *(mr->rdParm) = param1;
500 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
504 /******************************************************************
505 * MFDRV_MetaParam2
507 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
509 char buffer[10];
510 METARECORD *mr = (METARECORD *)&buffer;
512 mr->rdSize = 5;
513 mr->rdFunction = func;
514 *(mr->rdParm) = param2;
515 *(mr->rdParm + 1) = param1;
516 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
520 /******************************************************************
521 * MFDRV_MetaParam4
524 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
525 short param3, short param4)
527 char buffer[14];
528 METARECORD *mr = (METARECORD *)&buffer;
530 mr->rdSize = 7;
531 mr->rdFunction = func;
532 *(mr->rdParm) = param4;
533 *(mr->rdParm + 1) = param3;
534 *(mr->rdParm + 2) = param2;
535 *(mr->rdParm + 3) = param1;
536 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
540 /******************************************************************
541 * MFDRV_MetaParam6
544 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
545 short param3, short param4, short param5, short param6)
547 char buffer[18];
548 METARECORD *mr = (METARECORD *)&buffer;
550 mr->rdSize = 9;
551 mr->rdFunction = func;
552 *(mr->rdParm) = param6;
553 *(mr->rdParm + 1) = param5;
554 *(mr->rdParm + 2) = param4;
555 *(mr->rdParm + 3) = param3;
556 *(mr->rdParm + 4) = param2;
557 *(mr->rdParm + 5) = param1;
558 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
562 /******************************************************************
563 * MFDRV_MetaParam8
565 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
566 short param3, short param4, short param5,
567 short param6, short param7, short param8)
569 char buffer[22];
570 METARECORD *mr = (METARECORD *)&buffer;
572 mr->rdSize = 11;
573 mr->rdFunction = func;
574 *(mr->rdParm) = param8;
575 *(mr->rdParm + 1) = param7;
576 *(mr->rdParm + 2) = param6;
577 *(mr->rdParm + 3) = param5;
578 *(mr->rdParm + 4) = param4;
579 *(mr->rdParm + 5) = param3;
580 *(mr->rdParm + 6) = param2;
581 *(mr->rdParm + 7) = param1;
582 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
586 /**********************************************************************
587 * MFDRV_ExtEscape
589 INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
590 INT cbOutput, LPVOID out_data )
592 METARECORD *mr;
593 DWORD len;
594 INT ret;
596 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
598 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
599 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
600 mr->rdSize = len / 2;
601 mr->rdFunction = META_ESCAPE;
602 mr->rdParm[0] = nEscape;
603 mr->rdParm[1] = cbInput;
604 memcpy(&(mr->rdParm[2]), in_data, cbInput);
605 ret = MFDRV_WriteRecord( dev, mr, len);
606 HeapFree(GetProcessHeap(), 0, mr);
607 return ret;
611 /******************************************************************
612 * MFDRV_GetDeviceCaps
614 *A very simple implementation that returns DT_METAFILE
616 INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
618 switch(cap)
620 case TECHNOLOGY:
621 return DT_METAFILE;
622 case TEXTCAPS:
623 return 0;
624 default:
625 TRACE(" unsupported capability %d, will return 0\n", cap );
627 return 0;