DOSFS_ToDosFCBFormat: fail if extension longer than 3 characters.
[wine/gsoc-2012-control.git] / graphics / metafiledrv / init.c
blobf04aa5c743948d576eb2911ff9bef086108f927d
1 /*
2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "windef.h"
8 #include "wine/winbase16.h"
9 #include "dc.h"
10 #include "heap.h"
11 #include "global.h"
12 #include "metafile.h"
13 #include "metafiledrv.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(metafile)
18 #include <string.h>
20 static const DC_FUNCTIONS MFDRV_Funcs =
22 NULL, /* pAbortDoc */
23 MFDRV_AbortPath, /* pAbortPath */
24 NULL, /* pAngleArc */
25 MFDRV_Arc, /* pArc */
26 NULL, /* pArcTo */
27 MFDRV_BeginPath, /* pBeginPath */
28 MFDRV_BitBlt, /* pBitBlt */
29 NULL, /* pBitmapBits */
30 MFDRV_Chord, /* pChord */
31 MFDRV_CloseFigure, /* pCloseFigure */
32 NULL, /* pCreateBitmap */
33 NULL, /* no implementation */ /* pCreateDC */
34 NULL, /* pCreateDIBSection */
35 NULL, /* pCreateDIBSection16 */
36 NULL, /* no implementation */ /* pDeleteDC */
37 NULL, /* pDeleteObject */
38 NULL, /* pDeviceCapabilities */
39 MFDRV_Ellipse, /* pEllipse */
40 NULL, /* pEndDoc */
41 NULL, /* pEndPage */
42 MFDRV_EndPath, /* pEndPath */
43 NULL, /* pEnumDeviceFonts */
44 NULL, /* pEscape */
45 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
46 NULL, /* pExtDeviceMode */
47 MFDRV_ExtFloodFill, /* pExtFloodFill */
48 MFDRV_ExtTextOut, /* pExtTextOut */
49 MFDRV_FillPath, /* pFillPath */
50 MFDRV_FillRgn, /* pFillRgn */
51 MFDRV_FlattenPath, /* pFlattenPath */
52 MFDRV_FrameRgn, /* pFrameRgn */
53 NULL, /* pGetCharWidth */
54 NULL, /* pGetDCOrgEx */
55 NULL, /* no implementation */ /* pGetPixel */
56 NULL, /* pGetTextExtentPoint */
57 NULL, /* pGetTextMetrics */
58 MFDRV_IntersectClipRect, /* pIntersectClipRect */
59 MFDRV_InvertRgn, /* pInvertRgn */
60 MFDRV_LineTo, /* pLineTo */
61 NULL, /* pLoadOEMResource */
62 MFDRV_MoveToEx, /* pMoveToEx */
63 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
64 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
65 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
66 MFDRV_PaintRgn, /* pPaintRgn */
67 MFDRV_PatBlt, /* pPatBlt */
68 MFDRV_Pie, /* pPie */
69 MFDRV_PolyBezier, /* pPolyBezier */
70 MFDRV_PolyBezierTo, /* pPolyBezierTo */
71 NULL, /* pPolyDraw */
72 MFDRV_PolyPolygon, /* pPolyPolygon */
73 NULL, /* pPolyPolyline */
74 MFDRV_Polygon, /* pPolygon */
75 MFDRV_Polyline, /* pPolyline */
76 NULL, /* pPolylineTo */
77 NULL, /* pRealizePalette */
78 MFDRV_Rectangle, /* pRectangle */
79 MFDRV_RestoreDC, /* pRestoreDC */
80 MFDRV_RoundRect, /* pRoundRect */
81 MFDRV_SaveDC, /* pSaveDC */
82 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
83 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
84 MFDRV_SelectClipPath, /* pSelectClipPath */
85 NULL, /* pSelectClipRgn */
86 MFDRV_SelectObject, /* pSelectObject */
87 NULL, /* pSelectPalette */
88 MFDRV_SetBkColor, /* pSetBkColor */
89 MFDRV_SetBkMode, /* pSetBkMode */
90 NULL, /* pSetDeviceClipping */
91 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
92 MFDRV_SetMapMode, /* pSetMapMode */
93 MFDRV_SetMapperFlags, /* pSetMapperFlags */
94 MFDRV_SetPixel, /* pSetPixel */
95 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
96 MFDRV_SetROP2, /* pSetROP2 */
97 MFDRV_SetRelAbs, /* pSetRelAbs */
98 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
99 MFDRV_SetTextAlign, /* pSetTextAlign */
100 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
101 MFDRV_SetTextColor, /* pSetTextColor */
102 MFDRV_SetTextJustification, /* pSetTextJustification */
103 MFDRV_SetViewportExt, /* pSetViewportExt */
104 MFDRV_SetViewportOrg, /* pSetViewportOrg */
105 MFDRV_SetWindowExt, /* pSetWindowExt */
106 MFDRV_SetWindowOrg, /* pSetWindowOrg */
107 NULL, /* pStartDoc */
108 NULL, /* pStartPage */
109 MFDRV_StretchBlt, /* pStretchBlt */
110 MFDRV_StretchDIBits, /* pStretchDIBits */
111 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
112 MFDRV_StrokePath, /* pStrokePath */
113 MFDRV_WidenPath /* pWidenPath */
118 /**********************************************************************
119 * MFDRV_AllocMetaFile
121 static DC *MFDRV_AllocMetaFile(void)
123 DC *dc;
124 METAFILEDRV_PDEVICE *physDev;
126 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
127 dc->header.wMagic = METAFILE_DC_MAGIC;
129 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
130 if (!physDev)
132 GDI_HEAP_FREE( dc->hSelf );
133 return NULL;
135 dc->physDev = physDev;
137 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
139 HeapFree( GetProcessHeap(), 0, physDev );
140 GDI_HEAP_FREE( dc->hSelf );
141 return NULL;
144 physDev->nextHandle = 0;
145 physDev->hFile = 0;
147 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
148 physDev->mh->mtVersion = 0x0300;
149 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
150 physDev->mh->mtNoObjects = 0;
151 physDev->mh->mtMaxRecord = 0;
152 physDev->mh->mtNoParameters = 0;
154 return dc;
158 /**********************************************************************
159 * MFDRV_DeleteDC
161 static BOOL MFDRV_DeleteDC( DC *dc )
163 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
165 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
166 HeapFree( GetProcessHeap(), 0, physDev );
167 dc->physDev = NULL;
168 GDI_FreeObject(dc->hSelf);
169 return TRUE;
172 /**********************************************************************
173 * CreateMetaFile16 (GDI.125)
175 * Create a new DC and associate it with a metafile. Pass a filename
176 * to create a disk-based metafile, NULL to create a memory metafile.
178 * RETURNS
179 * A handle to the metafile DC if successful, NULL on failure.
181 HDC16 WINAPI CreateMetaFile16(
182 LPCSTR filename /* Filename of disk metafile */
185 DC *dc;
186 METAFILEDRV_PDEVICE *physDev;
187 HFILE hFile;
189 TRACE("'%s'\n", filename );
191 if (!(dc = MFDRV_AllocMetaFile())) return 0;
192 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
194 if (filename) /* disk based metafile */
196 physDev->mh->mtType = METAFILE_DISK;
197 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
198 CREATE_ALWAYS, 0, -1)) == HFILE_ERROR) {
199 MFDRV_DeleteDC( dc );
200 return 0;
202 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
203 NULL )) {
204 MFDRV_DeleteDC( dc );
205 return 0;
207 physDev->hFile = hFile;
209 /* Grow METAHEADER to include filename */
210 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
212 else /* memory based metafile */
213 physDev->mh->mtType = METAFILE_MEMORY;
215 TRACE("returning %04x\n", dc->hSelf);
216 return dc->hSelf;
219 /**********************************************************************
220 * CreateMetaFileA (GDI32.51)
222 HDC WINAPI CreateMetaFileA(
223 LPCSTR filename /* Filename of disk metafile */
226 return CreateMetaFile16( filename );
229 /**********************************************************************
230 * CreateMetaFileW (GDI32.52)
232 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
234 LPSTR filenameA;
235 HDC hReturnDC;
237 filenameA = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
239 hReturnDC = CreateMetaFileA(filenameA);
241 HeapFree( GetProcessHeap(), 0, filenameA );
243 return hReturnDC;
247 /**********************************************************************
248 * MFDRV_CloseMetaFile
250 static DC *MFDRV_CloseMetaFile( HDC hdc )
252 DC *dc;
253 METAFILEDRV_PDEVICE *physDev;
255 TRACE("(%04x)\n", hdc );
257 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
258 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
260 /* Construct the end of metafile record - this is documented
261 * in SDK Knowledgebase Q99334.
264 if (!MFDRV_MetaParam0(dc, META_EOF))
266 MFDRV_DeleteDC( dc );
267 return 0;
270 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
272 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
273 MFDRV_DeleteDC( dc );
274 return 0;
277 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
278 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
279 sizeof(*physDev->mh), NULL, NULL)) {
280 MFDRV_DeleteDC( dc );
281 return 0;
283 CloseHandle(physDev->hFile);
284 physDev->mh->mtType = METAFILE_DISK;
287 return dc;
290 /******************************************************************
291 * CloseMetaFile16 (GDI.126)
293 HMETAFILE16 WINAPI CloseMetaFile16(
294 HDC16 hdc /* Metafile DC to close */
297 HMETAFILE16 hmf;
298 METAFILEDRV_PDEVICE *physDev;
299 DC *dc = MFDRV_CloseMetaFile(hdc);
300 if (!dc) return 0;
301 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
303 /* Now allocate a global handle for the metafile */
305 hmf = MF_Create_HMETAFILE16( physDev->mh );
307 physDev->mh = NULL; /* So it won't be deleted */
308 MFDRV_DeleteDC( dc );
309 return hmf;
312 /******************************************************************
313 * CloseMetaFile (GDI32.17)
315 * Stop recording graphics operations in metafile associated with
316 * hdc and retrieve metafile.
318 * RETURNS
319 * Handle of newly created metafile on success, NULL on failure.
321 HMETAFILE WINAPI CloseMetaFile(
322 HDC hdc /* Metafile DC to close */
325 HMETAFILE hmf;
326 METAFILEDRV_PDEVICE *physDev;
327 DC *dc = MFDRV_CloseMetaFile(hdc);
328 if (!dc) return 0;
329 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
331 /* Now allocate a global handle for the metafile */
333 hmf = MF_Create_HMETAFILE( physDev->mh );
335 physDev->mh = NULL; /* So it won't be deleted */
336 MFDRV_DeleteDC( dc );
337 return hmf;
341 /******************************************************************
342 * MFDRV_WriteRecord
344 * Warning: this function can change the pointer to the metafile header.
346 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
348 DWORD len;
349 METAHEADER *mh;
350 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
352 switch(physDev->mh->mtType)
354 case METAFILE_MEMORY:
355 len = physDev->mh->mtSize * 2 + rlen;
356 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
357 if (!mh) return FALSE;
358 physDev->mh = mh;
359 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
360 break;
361 case METAFILE_DISK:
362 TRACE("Writing record to disk\n");
363 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
364 return FALSE;
365 break;
366 default:
367 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
368 return FALSE;
371 physDev->mh->mtSize += rlen / 2;
372 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
373 return TRUE;
377 /******************************************************************
378 * MFDRV_MetaParam0
381 BOOL MFDRV_MetaParam0(DC *dc, short func)
383 char buffer[8];
384 METARECORD *mr = (METARECORD *)&buffer;
386 mr->rdSize = 3;
387 mr->rdFunction = func;
388 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
392 /******************************************************************
393 * MFDRV_MetaParam1
395 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
397 char buffer[8];
398 METARECORD *mr = (METARECORD *)&buffer;
400 mr->rdSize = 4;
401 mr->rdFunction = func;
402 *(mr->rdParm) = param1;
403 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
407 /******************************************************************
408 * MFDRV_MetaParam2
410 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
412 char buffer[10];
413 METARECORD *mr = (METARECORD *)&buffer;
415 mr->rdSize = 5;
416 mr->rdFunction = func;
417 *(mr->rdParm) = param2;
418 *(mr->rdParm + 1) = param1;
419 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
423 /******************************************************************
424 * MFDRV_MetaParam4
427 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2,
428 short param3, short param4)
430 char buffer[14];
431 METARECORD *mr = (METARECORD *)&buffer;
433 mr->rdSize = 7;
434 mr->rdFunction = func;
435 *(mr->rdParm) = param4;
436 *(mr->rdParm + 1) = param3;
437 *(mr->rdParm + 2) = param2;
438 *(mr->rdParm + 3) = param1;
439 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
443 /******************************************************************
444 * MFDRV_MetaParam6
447 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2,
448 short param3, short param4, short param5, short param6)
450 char buffer[18];
451 METARECORD *mr = (METARECORD *)&buffer;
453 mr->rdSize = 9;
454 mr->rdFunction = func;
455 *(mr->rdParm) = param6;
456 *(mr->rdParm + 1) = param5;
457 *(mr->rdParm + 2) = param4;
458 *(mr->rdParm + 3) = param3;
459 *(mr->rdParm + 4) = param2;
460 *(mr->rdParm + 5) = param1;
461 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
465 /******************************************************************
466 * MFDRV_MetaParam8
468 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2,
469 short param3, short param4, short param5,
470 short param6, short param7, short param8)
472 char buffer[22];
473 METARECORD *mr = (METARECORD *)&buffer;
475 mr->rdSize = 11;
476 mr->rdFunction = func;
477 *(mr->rdParm) = param8;
478 *(mr->rdParm + 1) = param7;
479 *(mr->rdParm + 2) = param6;
480 *(mr->rdParm + 3) = param5;
481 *(mr->rdParm + 4) = param4;
482 *(mr->rdParm + 5) = param3;
483 *(mr->rdParm + 6) = param2;
484 *(mr->rdParm + 7) = param1;
485 return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
489 /******************************************************************
490 * MFDRV_AddHandleDC
492 * Note: this function assumes that we never delete objects.
493 * If we do someday, we'll need to maintain a table to re-use deleted
494 * handles.
496 int MFDRV_AddHandleDC( DC *dc )
498 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
499 physDev->mh->mtNoObjects++;
500 return physDev->nextHandle++;