push ab3cc77eb114d2bd400734a9dccad24563f936a6
[wine/hacks.git] / dlls / gdi32 / mfdrv / init.c
blobc2cf404dacedc209f557996d873bed6b3b016ce5
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, /* 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_OffsetViewportOrg, /* pOffsetViewportOrg */
91 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
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_ScaleViewportExt, /* pScaleViewportExt */
111 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
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 NULL, /* pSetDCBrushColor*/
123 NULL, /* pSetDCOrg */
124 NULL, /* pSetDCPenColor*/
125 NULL, /* pSetDIBColorTable */
126 NULL, /* pSetDIBits */
127 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
128 NULL, /* pSetDeviceClipping */
129 NULL, /* pSetDeviceGammaRamp */
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_SetViewportExt, /* pSetViewportExt */
143 MFDRV_SetViewportOrg, /* pSetViewportOrg */
144 MFDRV_SetWindowExt, /* pSetWindowExt */
145 MFDRV_SetWindowOrg, /* pSetWindowOrg */
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( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
170 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
171 if (!physDev)
173 free_dc_ptr( dc );
174 return NULL;
176 dc->physDev = (PHYSDEV)physDev;
177 physDev->hdc = dc->hSelf;
179 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
181 HeapFree( GetProcessHeap(), 0, physDev );
182 free_dc_ptr( dc );
183 return NULL;
186 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
187 physDev->handles_size = HANDLE_LIST_INC;
188 physDev->cur_handles = 0;
190 physDev->hFile = 0;
192 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
193 physDev->mh->mtVersion = 0x0300;
194 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
195 physDev->mh->mtNoObjects = 0;
196 physDev->mh->mtMaxRecord = 0;
197 physDev->mh->mtNoParameters = 0;
199 return dc;
203 /**********************************************************************
204 * MFDRV_DeleteDC
206 static BOOL MFDRV_DeleteDC( DC *dc )
208 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
209 DWORD index;
211 HeapFree( GetProcessHeap(), 0, physDev->mh );
212 for(index = 0; index < physDev->handles_size; index++)
213 if(physDev->handles[index])
214 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
215 HeapFree( GetProcessHeap(), 0, physDev->handles );
216 HeapFree( GetProcessHeap(), 0, physDev );
217 dc->physDev = NULL;
218 free_dc_ptr( dc );
219 return TRUE;
223 /**********************************************************************
224 * CreateMetaFileW (GDI32.@)
226 * Create a new DC and associate it with a metafile. Pass a filename
227 * to create a disk-based metafile, NULL to create a memory metafile.
229 * PARAMS
230 * filename [I] Filename of disk metafile
232 * RETURNS
233 * A handle to the metafile DC if successful, NULL on failure.
235 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
237 HDC ret;
238 DC *dc;
239 METAFILEDRV_PDEVICE *physDev;
240 HANDLE hFile;
242 TRACE("%s\n", debugstr_w(filename) );
244 if (!(dc = MFDRV_AllocMetaFile())) return 0;
245 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
247 if (filename) /* disk based metafile */
249 physDev->mh->mtType = METAFILE_DISK;
250 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
251 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
252 MFDRV_DeleteDC( dc );
253 return 0;
255 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
256 NULL )) {
257 MFDRV_DeleteDC( dc );
258 return 0;
260 physDev->hFile = hFile;
262 /* Grow METAHEADER to include filename */
263 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
265 else /* memory based metafile */
266 physDev->mh->mtType = METAFILE_MEMORY;
268 TRACE("returning %p\n", dc->hSelf);
269 ret = dc->hSelf;
270 release_dc_ptr( dc );
271 return ret;
274 /**********************************************************************
275 * CreateMetaFileA (GDI32.@)
277 * See CreateMetaFileW.
279 HDC WINAPI CreateMetaFileA(LPCSTR filename)
281 LPWSTR filenameW;
282 DWORD len;
283 HDC hReturnDC;
285 if (!filename) return CreateMetaFileW(NULL);
287 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
288 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
289 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
291 hReturnDC = CreateMetaFileW(filenameW);
293 HeapFree( GetProcessHeap(), 0, filenameW );
295 return hReturnDC;
299 /**********************************************************************
300 * MFDRV_CloseMetaFile
302 static DC *MFDRV_CloseMetaFile( HDC hdc )
304 DC *dc;
305 METAFILEDRV_PDEVICE *physDev;
307 TRACE("(%p)\n", hdc );
309 if (!(dc = get_dc_ptr( hdc ))) return NULL;
310 if (GDIMAGIC(dc->header.wMagic) != METAFILE_DC_MAGIC)
312 release_dc_ptr( dc );
313 return NULL;
315 if (dc->refcount != 1)
317 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
318 release_dc_ptr( dc );
319 return NULL;
321 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
323 /* Construct the end of metafile record - this is documented
324 * in SDK Knowledgebase Q99334.
327 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
329 MFDRV_DeleteDC( dc );
330 return 0;
333 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
335 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
336 MFDRV_DeleteDC( dc );
337 return 0;
340 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
341 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
342 sizeof(*physDev->mh), NULL, NULL)) {
343 MFDRV_DeleteDC( dc );
344 return 0;
346 CloseHandle(physDev->hFile);
347 physDev->mh->mtType = METAFILE_DISK;
350 return dc;
353 /******************************************************************
354 * MF_Create_HMETATFILE16
356 * Creates a HMETAFILE16 object from a METAHEADER
358 * HMETAFILE16s are Global memory handles.
360 HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
362 HMETAFILE16 hmf;
363 DWORD size = mh->mtSize * sizeof(WORD);
365 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
366 if(hmf)
368 METAHEADER *mh_dest = GlobalLock16(hmf);
369 memcpy(mh_dest, mh, size);
370 GlobalUnlock16(hmf);
372 HeapFree(GetProcessHeap(), 0, mh);
373 return hmf;
376 /******************************************************************
377 * CloseMetaFile (GDI.126)
379 * PARAMS
380 * hdc [I] Metafile DC to close
382 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
384 HMETAFILE16 hmf;
385 METAFILEDRV_PDEVICE *physDev;
386 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
387 if (!dc) return 0;
388 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
390 /* Now allocate a global handle for the metafile */
392 hmf = MF_Create_HMETAFILE16( physDev->mh );
394 physDev->mh = NULL; /* So it won't be deleted */
395 MFDRV_DeleteDC( dc );
396 return hmf;
399 /******************************************************************
400 * CloseMetaFile (GDI32.@)
402 * Stop recording graphics operations in metafile associated with
403 * hdc and retrieve metafile.
405 * PARAMS
406 * hdc [I] Metafile DC to close
408 * RETURNS
409 * Handle of newly created metafile on success, NULL on failure.
411 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
413 HMETAFILE hmf;
414 METAFILEDRV_PDEVICE *physDev;
415 DC *dc = MFDRV_CloseMetaFile(hdc);
416 if (!dc) return 0;
417 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
419 /* Now allocate a global handle for the metafile */
421 hmf = MF_Create_HMETAFILE( physDev->mh );
423 physDev->mh = NULL; /* So it won't be deleted */
424 MFDRV_DeleteDC( dc );
425 return hmf;
429 /******************************************************************
430 * MFDRV_WriteRecord
432 * Warning: this function can change the pointer to the metafile header.
434 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
436 DWORD len, size;
437 METAHEADER *mh;
438 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
440 switch(physDev->mh->mtType)
442 case METAFILE_MEMORY:
443 len = physDev->mh->mtSize * 2 + rlen;
444 /* reallocate memory if needed */
445 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
446 if (len > size)
448 /*expand size*/
449 size += size / 2 + rlen;
450 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
451 if (!mh) return FALSE;
452 physDev->mh = mh;
453 TRACE("Reallocated metafile: new size is %d\n",size);
455 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
456 break;
457 case METAFILE_DISK:
458 TRACE("Writing record to disk\n");
459 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
460 return FALSE;
461 break;
462 default:
463 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
464 return FALSE;
467 physDev->mh->mtSize += rlen / 2;
468 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
469 return TRUE;
473 /******************************************************************
474 * MFDRV_MetaParam0
477 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
479 char buffer[8];
480 METARECORD *mr = (METARECORD *)&buffer;
482 mr->rdSize = 3;
483 mr->rdFunction = func;
484 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
488 /******************************************************************
489 * MFDRV_MetaParam1
491 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
493 char buffer[8];
494 METARECORD *mr = (METARECORD *)&buffer;
496 mr->rdSize = 4;
497 mr->rdFunction = func;
498 *(mr->rdParm) = param1;
499 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
503 /******************************************************************
504 * MFDRV_MetaParam2
506 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
508 char buffer[10];
509 METARECORD *mr = (METARECORD *)&buffer;
511 mr->rdSize = 5;
512 mr->rdFunction = func;
513 *(mr->rdParm) = param2;
514 *(mr->rdParm + 1) = param1;
515 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
519 /******************************************************************
520 * MFDRV_MetaParam4
523 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
524 short param3, short param4)
526 char buffer[14];
527 METARECORD *mr = (METARECORD *)&buffer;
529 mr->rdSize = 7;
530 mr->rdFunction = func;
531 *(mr->rdParm) = param4;
532 *(mr->rdParm + 1) = param3;
533 *(mr->rdParm + 2) = param2;
534 *(mr->rdParm + 3) = param1;
535 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
539 /******************************************************************
540 * MFDRV_MetaParam6
543 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
544 short param3, short param4, short param5, short param6)
546 char buffer[18];
547 METARECORD *mr = (METARECORD *)&buffer;
549 mr->rdSize = 9;
550 mr->rdFunction = func;
551 *(mr->rdParm) = param6;
552 *(mr->rdParm + 1) = param5;
553 *(mr->rdParm + 2) = param4;
554 *(mr->rdParm + 3) = param3;
555 *(mr->rdParm + 4) = param2;
556 *(mr->rdParm + 5) = param1;
557 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
561 /******************************************************************
562 * MFDRV_MetaParam8
564 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
565 short param3, short param4, short param5,
566 short param6, short param7, short param8)
568 char buffer[22];
569 METARECORD *mr = (METARECORD *)&buffer;
571 mr->rdSize = 11;
572 mr->rdFunction = func;
573 *(mr->rdParm) = param8;
574 *(mr->rdParm + 1) = param7;
575 *(mr->rdParm + 2) = param6;
576 *(mr->rdParm + 3) = param5;
577 *(mr->rdParm + 4) = param4;
578 *(mr->rdParm + 5) = param3;
579 *(mr->rdParm + 6) = param2;
580 *(mr->rdParm + 7) = param1;
581 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
585 /**********************************************************************
586 * MFDRV_ExtEscape
588 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
589 INT cbOutput, LPVOID out_data )
591 METARECORD *mr;
592 DWORD len;
593 INT ret;
595 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
597 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
598 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
599 mr->rdSize = len / 2;
600 mr->rdFunction = META_ESCAPE;
601 mr->rdParm[0] = nEscape;
602 mr->rdParm[1] = cbInput;
603 memcpy(&(mr->rdParm[2]), in_data, cbInput);
604 ret = MFDRV_WriteRecord( dev, mr, len);
605 HeapFree(GetProcessHeap(), 0, mr);
606 return ret;
610 /******************************************************************
611 * MFDRV_GetDeviceCaps
613 *A very simple implementation that returns DT_METAFILE
615 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
617 switch(cap)
619 case TECHNOLOGY:
620 return DT_METAFILE;
621 case TEXTCAPS:
622 return 0;
623 default:
624 TRACE(" unsupported capability %d, will return 0\n", cap );
626 return 0;