Moved metafiles and win16 drivers to dlls/gdi.
[wine/testsucceed.git] / dlls / gdi / mfdrv / init.c
blobe42aeadc2a67aa933132762a04cccc43f130aea0
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "windef.h"
22 #include "wine/winbase16.h"
23 #include "gdi.h"
24 #include "metafile.h"
25 #include "mfdrv/metafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
30 #include <string.h>
32 static const DC_FUNCTIONS MFDRV_Funcs =
34 NULL, /* pAbortDoc */
35 MFDRV_AbortPath, /* pAbortPath */
36 NULL, /* pAngleArc */
37 MFDRV_Arc, /* pArc */
38 NULL, /* pArcTo */
39 MFDRV_BeginPath, /* pBeginPath */
40 MFDRV_BitBlt, /* pBitBlt */
41 NULL, /* pBitmapBits */
42 NULL, /* pChoosePixelFormat */
43 MFDRV_Chord, /* pChord */
44 MFDRV_CloseFigure, /* pCloseFigure */
45 NULL, /* pCreateBitmap */
46 NULL, /* no implementation */ /* pCreateDC */
47 NULL, /* pCreateDIBSection */
48 NULL, /* no implementation */ /* pDeleteDC */
49 NULL, /* pDeleteObject */
50 NULL, /* pDescribePixelFormat */
51 NULL, /* pDeviceCapabilities */
52 MFDRV_Ellipse, /* pEllipse */
53 NULL, /* pEndDoc */
54 NULL, /* pEndPage */
55 MFDRV_EndPath, /* pEndPath */
56 NULL, /* pEnumDeviceFonts */
57 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
58 NULL, /* pExtDeviceMode */
59 NULL, /* pExtEscape */
60 MFDRV_ExtFloodFill, /* pExtFloodFill */
61 MFDRV_ExtTextOut, /* pExtTextOut */
62 MFDRV_FillPath, /* pFillPath */
63 MFDRV_FillRgn, /* pFillRgn */
64 MFDRV_FlattenPath, /* pFlattenPath */
65 MFDRV_FrameRgn, /* pFrameRgn */
66 NULL, /* pGetCharWidth */
67 NULL, /* pGetDCOrgEx */
68 NULL, /* pGetDeviceCaps */
69 NULL, /* pGetDeviceGammaRamp */
70 NULL, /* no implementation */ /* pGetPixel */
71 NULL, /* pGetPixelFormat */
72 NULL, /* pGetTextExtentPoint */
73 NULL, /* pGetTextMetrics */
74 MFDRV_IntersectClipRect, /* pIntersectClipRect */
75 MFDRV_InvertRgn, /* pInvertRgn */
76 MFDRV_LineTo, /* pLineTo */
77 MFDRV_MoveTo, /* pMoveTo */
78 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
79 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
80 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
81 MFDRV_PaintRgn, /* pPaintRgn */
82 MFDRV_PatBlt, /* pPatBlt */
83 MFDRV_Pie, /* pPie */
84 MFDRV_PolyBezier, /* pPolyBezier */
85 MFDRV_PolyBezierTo, /* pPolyBezierTo */
86 NULL, /* pPolyDraw */
87 MFDRV_PolyPolygon, /* pPolyPolygon */
88 NULL, /* pPolyPolyline */
89 MFDRV_Polygon, /* pPolygon */
90 MFDRV_Polyline, /* pPolyline */
91 NULL, /* pPolylineTo */
92 NULL, /* pRealizePalette */
93 MFDRV_Rectangle, /* pRectangle */
94 MFDRV_RestoreDC, /* pRestoreDC */
95 MFDRV_RoundRect, /* pRoundRect */
96 MFDRV_SaveDC, /* pSaveDC */
97 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
98 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
99 MFDRV_SelectClipPath, /* pSelectClipPath */
100 NULL, /* pSelectClipRgn */
101 MFDRV_SelectObject, /* pSelectObject */
102 NULL, /* pSelectPalette */
103 MFDRV_SetBkColor, /* pSetBkColor */
104 MFDRV_SetBkMode, /* pSetBkMode */
105 NULL, /* pSetDeviceClipping */
106 NULL, /* pSetDeviceGammaRamp */
107 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
108 MFDRV_SetMapMode, /* pSetMapMode */
109 MFDRV_SetMapperFlags, /* pSetMapperFlags */
110 MFDRV_SetPixel, /* pSetPixel */
111 NULL, /* pSetPixelFormat */
112 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
113 MFDRV_SetROP2, /* pSetROP2 */
114 MFDRV_SetRelAbs, /* pSetRelAbs */
115 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
116 MFDRV_SetTextAlign, /* pSetTextAlign */
117 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
118 MFDRV_SetTextColor, /* pSetTextColor */
119 MFDRV_SetTextJustification, /* pSetTextJustification */
120 MFDRV_SetViewportExt, /* pSetViewportExt */
121 MFDRV_SetViewportOrg, /* pSetViewportOrg */
122 MFDRV_SetWindowExt, /* pSetWindowExt */
123 MFDRV_SetWindowOrg, /* pSetWindowOrg */
124 NULL, /* pStartDoc */
125 NULL, /* pStartPage */
126 MFDRV_StretchBlt, /* pStretchBlt */
127 MFDRV_StretchDIBits, /* pStretchDIBits */
128 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
129 MFDRV_StrokePath, /* pStrokePath */
130 NULL, /* pSwapBuffers */
131 MFDRV_WidenPath /* pWidenPath */
136 /**********************************************************************
137 * MFDRV_AllocMetaFile
139 static DC *MFDRV_AllocMetaFile(void)
141 DC *dc;
142 METAFILEDRV_PDEVICE *physDev;
144 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
145 dc->header.wMagic = METAFILE_DC_MAGIC;
147 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
148 if (!physDev)
150 GDI_FreeObject( dc->hSelf, dc );
151 return NULL;
153 dc->physDev = physDev;
155 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
157 HeapFree( GetProcessHeap(), 0, physDev );
158 GDI_FreeObject( dc->hSelf, dc );
159 return NULL;
162 physDev->nextHandle = 0;
163 physDev->hFile = 0;
165 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
166 physDev->mh->mtVersion = 0x0300;
167 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
168 physDev->mh->mtNoObjects = 0;
169 physDev->mh->mtMaxRecord = 0;
170 physDev->mh->mtNoParameters = 0;
172 return dc;
176 /**********************************************************************
177 * MFDRV_DeleteDC
179 static BOOL MFDRV_DeleteDC( DC *dc )
181 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
183 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
184 HeapFree( GetProcessHeap(), 0, physDev );
185 dc->physDev = NULL;
186 GDI_FreeObject( dc->hSelf, dc );
187 return TRUE;
190 /**********************************************************************
191 * CreateMetaFile (GDI.125)
192 * CreateMetaFile16 (GDI32.@)
194 * Create a new DC and associate it with a metafile. Pass a filename
195 * to create a disk-based metafile, NULL to create a memory metafile.
197 * RETURNS
198 * A handle to the metafile DC if successful, NULL on failure.
200 HDC16 WINAPI CreateMetaFile16(
201 LPCSTR filename /* [in] Filename of disk metafile */
204 HDC ret;
205 DC *dc;
206 METAFILEDRV_PDEVICE *physDev;
207 HFILE hFile;
209 TRACE("'%s'\n", filename );
211 if (!(dc = MFDRV_AllocMetaFile())) return 0;
212 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
214 if (filename) /* disk based metafile */
216 physDev->mh->mtType = METAFILE_DISK;
217 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
218 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
219 MFDRV_DeleteDC( dc );
220 return 0;
222 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
223 NULL )) {
224 MFDRV_DeleteDC( dc );
225 return 0;
227 physDev->hFile = hFile;
229 /* Grow METAHEADER to include filename */
230 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
232 else /* memory based metafile */
233 physDev->mh->mtType = METAFILE_MEMORY;
235 TRACE("returning %04x\n", dc->hSelf);
236 ret = dc->hSelf;
237 GDI_ReleaseObj( dc->hSelf );
238 return ret;
241 /**********************************************************************
242 * CreateMetaFileA (GDI32.@)
244 HDC WINAPI CreateMetaFileA(
245 LPCSTR filename /* [in] Filename of disk metafile */
248 return CreateMetaFile16( filename );
251 /**********************************************************************
252 * CreateMetaFileW (GDI32.@)
254 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
256 LPSTR filenameA;
257 DWORD len;
258 HDC hReturnDC;
260 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
261 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
262 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
264 hReturnDC = CreateMetaFileA(filenameA);
266 HeapFree( GetProcessHeap(), 0, filenameA );
268 return hReturnDC;
272 /**********************************************************************
273 * MFDRV_CloseMetaFile
275 static DC *MFDRV_CloseMetaFile( HDC hdc )
277 DC *dc;
278 METAFILEDRV_PDEVICE *physDev;
280 TRACE("(%04x)\n", hdc );
282 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
283 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
285 /* Construct the end of metafile record - this is documented
286 * in SDK Knowledgebase Q99334.
289 if (!MFDRV_MetaParam0(dc, META_EOF))
291 MFDRV_DeleteDC( dc );
292 return 0;
295 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
297 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
298 MFDRV_DeleteDC( dc );
299 return 0;
302 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
303 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
304 sizeof(*physDev->mh), NULL, NULL)) {
305 MFDRV_DeleteDC( dc );
306 return 0;
308 CloseHandle(physDev->hFile);
309 physDev->mh->mtType = METAFILE_DISK;
312 return dc;
315 /******************************************************************
316 * CloseMetaFile (GDI.126)
317 * CloseMetaFile16 (GDI32.@)
319 HMETAFILE16 WINAPI CloseMetaFile16(
320 HDC16 hdc /* [in] Metafile DC to close */
323 HMETAFILE16 hmf;
324 METAFILEDRV_PDEVICE *physDev;
325 DC *dc = MFDRV_CloseMetaFile(hdc);
326 if (!dc) return 0;
327 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
329 /* Now allocate a global handle for the metafile */
331 hmf = MF_Create_HMETAFILE16( physDev->mh );
333 physDev->mh = NULL; /* So it won't be deleted */
334 MFDRV_DeleteDC( dc );
335 return hmf;
338 /******************************************************************
339 * CloseMetaFile (GDI32.@)
341 * Stop recording graphics operations in metafile associated with
342 * hdc and retrieve metafile.
344 * RETURNS
345 * Handle of newly created metafile on success, NULL on failure.
347 HMETAFILE WINAPI CloseMetaFile(
348 HDC hdc /* [in] Metafile DC to close */
351 HMETAFILE hmf;
352 METAFILEDRV_PDEVICE *physDev;
353 DC *dc = MFDRV_CloseMetaFile(hdc);
354 if (!dc) return 0;
355 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
357 /* Now allocate a global handle for the metafile */
359 hmf = MF_Create_HMETAFILE( physDev->mh );
361 physDev->mh = NULL; /* So it won't be deleted */
362 MFDRV_DeleteDC( dc );
363 return hmf;
367 /******************************************************************
368 * MFDRV_WriteRecord
370 * Warning: this function can change the pointer to the metafile header.
372 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
374 DWORD len;
375 METAHEADER *mh;
376 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
378 switch(physDev->mh->mtType)
380 case METAFILE_MEMORY:
381 len = physDev->mh->mtSize * 2 + rlen;
382 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
383 if (!mh) return FALSE;
384 physDev->mh = mh;
385 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
386 break;
387 case METAFILE_DISK:
388 TRACE("Writing record to disk\n");
389 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
390 return FALSE;
391 break;
392 default:
393 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
394 return FALSE;
397 physDev->mh->mtSize += rlen / 2;
398 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
399 return TRUE;
403 /******************************************************************
404 * MFDRV_MetaParam0
407 BOOL MFDRV_MetaParam0(DC *dc, short func)
409 char buffer[8];
410 METARECORD *mr = (METARECORD *)&buffer;
412 mr->rdSize = 3;
413 mr->rdFunction = func;
414 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
418 /******************************************************************
419 * MFDRV_MetaParam1
421 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
423 char buffer[8];
424 METARECORD *mr = (METARECORD *)&buffer;
426 mr->rdSize = 4;
427 mr->rdFunction = func;
428 *(mr->rdParm) = param1;
429 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
433 /******************************************************************
434 * MFDRV_MetaParam2
436 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
438 char buffer[10];
439 METARECORD *mr = (METARECORD *)&buffer;
441 mr->rdSize = 5;
442 mr->rdFunction = func;
443 *(mr->rdParm) = param2;
444 *(mr->rdParm + 1) = param1;
445 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
449 /******************************************************************
450 * MFDRV_MetaParam4
453 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2,
454 short param3, short param4)
456 char buffer[14];
457 METARECORD *mr = (METARECORD *)&buffer;
459 mr->rdSize = 7;
460 mr->rdFunction = func;
461 *(mr->rdParm) = param4;
462 *(mr->rdParm + 1) = param3;
463 *(mr->rdParm + 2) = param2;
464 *(mr->rdParm + 3) = param1;
465 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
469 /******************************************************************
470 * MFDRV_MetaParam6
473 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2,
474 short param3, short param4, short param5, short param6)
476 char buffer[18];
477 METARECORD *mr = (METARECORD *)&buffer;
479 mr->rdSize = 9;
480 mr->rdFunction = func;
481 *(mr->rdParm) = param6;
482 *(mr->rdParm + 1) = param5;
483 *(mr->rdParm + 2) = param4;
484 *(mr->rdParm + 3) = param3;
485 *(mr->rdParm + 4) = param2;
486 *(mr->rdParm + 5) = param1;
487 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
491 /******************************************************************
492 * MFDRV_MetaParam8
494 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2,
495 short param3, short param4, short param5,
496 short param6, short param7, short param8)
498 char buffer[22];
499 METARECORD *mr = (METARECORD *)&buffer;
501 mr->rdSize = 11;
502 mr->rdFunction = func;
503 *(mr->rdParm) = param8;
504 *(mr->rdParm + 1) = param7;
505 *(mr->rdParm + 2) = param6;
506 *(mr->rdParm + 3) = param5;
507 *(mr->rdParm + 4) = param4;
508 *(mr->rdParm + 5) = param3;
509 *(mr->rdParm + 6) = param2;
510 *(mr->rdParm + 7) = param1;
511 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
515 /******************************************************************
516 * MFDRV_AddHandleDC
518 * Note: this function assumes that we never delete objects.
519 * If we do someday, we'll need to maintain a table to re-use deleted
520 * handles.
522 int MFDRV_AddHandleDC( DC *dc )
524 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
525 physDev->mh->mtNoObjects++;
526 return physDev->nextHandle++;