Added an unknown VxD error code.
[wine] / graphics / metafiledrv / init.c
1 /*
2  * Metafile driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "windef.h"
8 #include "wine/winbase16.h"
9 #include "gdi.h"
10 #include "heap.h"
11 #include "metafile.h"
12 #include "metafiledrv.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(metafile);
16
17 #include <string.h>
18
19 static const DC_FUNCTIONS MFDRV_Funcs =
20 {
21     NULL,                            /* pAbortDoc */
22     MFDRV_AbortPath,                 /* pAbortPath */
23     NULL,                            /* pAngleArc */
24     MFDRV_Arc,                       /* pArc */
25     NULL,                            /* pArcTo */
26     MFDRV_BeginPath,                 /* pBeginPath */
27     MFDRV_BitBlt,                    /* pBitBlt */
28     NULL,                            /* pBitmapBits */  
29     NULL,                            /* pChoosePixelFormat */
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,                            /* pDescribePixelFormat */
39     NULL,                            /* pDeviceCapabilities */
40     MFDRV_Ellipse,                   /* pEllipse */
41     NULL,                            /* pEndDoc */
42     NULL,                            /* pEndPage */
43     MFDRV_EndPath,                   /* pEndPath */
44     NULL,                            /* pEnumDeviceFonts */
45     NULL,                            /* pEscape */
46     MFDRV_ExcludeClipRect,           /* pExcludeClipRect */
47     NULL,                            /* pExtDeviceMode */
48     MFDRV_ExtFloodFill,              /* pExtFloodFill */
49     MFDRV_ExtTextOut,                /* pExtTextOut */
50     MFDRV_FillPath,                  /* pFillPath */
51     MFDRV_FillRgn,                   /* pFillRgn */
52     MFDRV_FlattenPath,               /* pFlattenPath */
53     MFDRV_FrameRgn,                  /* pFrameRgn */
54     NULL,                            /* pGetCharWidth */
55     NULL,                            /* pGetDCOrgEx */
56     NULL,                            /* pGetDeviceGammaRamp */
57     NULL, /* no implementation */    /* pGetPixel */
58     NULL,                            /* pGetPixelFormat */
59     NULL,                            /* pGetTextExtentPoint */
60     NULL,                            /* pGetTextMetrics */
61     MFDRV_IntersectClipRect,         /* pIntersectClipRect */
62     MFDRV_InvertRgn,                 /* pInvertRgn */
63     MFDRV_LineTo,                    /* pLineTo */
64     MFDRV_MoveTo,                    /* pMoveTo */
65     MFDRV_OffsetClipRgn,             /* pOffsetClipRgn */
66     MFDRV_OffsetViewportOrg,         /* pOffsetViewportOrg */
67     MFDRV_OffsetWindowOrg,           /* pOffsetWindowOrg */
68     MFDRV_PaintRgn,                  /* pPaintRgn */
69     MFDRV_PatBlt,                    /* pPatBlt */
70     MFDRV_Pie,                       /* pPie */
71     MFDRV_PolyBezier,                /* pPolyBezier */
72     MFDRV_PolyBezierTo,              /* pPolyBezierTo */
73     NULL,                            /* pPolyDraw */
74     MFDRV_PolyPolygon,               /* pPolyPolygon */
75     NULL,                            /* pPolyPolyline */
76     MFDRV_Polygon,                   /* pPolygon */
77     MFDRV_Polyline,                  /* pPolyline */
78     NULL,                            /* pPolylineTo */
79     NULL,                            /* pRealizePalette */
80     MFDRV_Rectangle,                 /* pRectangle */
81     MFDRV_RestoreDC,                 /* pRestoreDC */
82     MFDRV_RoundRect,                 /* pRoundRect */
83     MFDRV_SaveDC,                    /* pSaveDC */
84     MFDRV_ScaleViewportExt,          /* pScaleViewportExt */
85     MFDRV_ScaleWindowExt,            /* pScaleWindowExt */
86     MFDRV_SelectClipPath,            /* pSelectClipPath */
87     NULL,                            /* pSelectClipRgn */
88     MFDRV_SelectObject,              /* pSelectObject */
89     NULL,                            /* pSelectPalette */
90     MFDRV_SetBkColor,                /* pSetBkColor */
91     MFDRV_SetBkMode,                 /* pSetBkMode */
92     NULL,                            /* pSetDeviceClipping */
93     NULL,                            /* pSetDeviceGammaRamp */
94     MFDRV_SetDIBitsToDevice,         /* pSetDIBitsToDevice */
95     MFDRV_SetMapMode,                /* pSetMapMode */
96     MFDRV_SetMapperFlags,            /* pSetMapperFlags */
97     MFDRV_SetPixel,                  /* pSetPixel */
98     NULL,                            /* pSetPixelFormat */
99     MFDRV_SetPolyFillMode,           /* pSetPolyFillMode */
100     MFDRV_SetROP2,                   /* pSetROP2 */
101     MFDRV_SetRelAbs,                 /* pSetRelAbs */
102     MFDRV_SetStretchBltMode,         /* pSetStretchBltMode */
103     MFDRV_SetTextAlign,              /* pSetTextAlign */
104     MFDRV_SetTextCharacterExtra,     /* pSetTextCharacterExtra */
105     MFDRV_SetTextColor,              /* pSetTextColor */
106     MFDRV_SetTextJustification,      /* pSetTextJustification */
107     MFDRV_SetViewportExt,            /* pSetViewportExt */
108     MFDRV_SetViewportOrg,            /* pSetViewportOrg */
109     MFDRV_SetWindowExt,              /* pSetWindowExt */
110     MFDRV_SetWindowOrg,              /* pSetWindowOrg */
111     NULL,                            /* pStartDoc */
112     NULL,                            /* pStartPage */
113     MFDRV_StretchBlt,                /* pStretchBlt */
114     MFDRV_StretchDIBits,             /* pStretchDIBits */
115     MFDRV_StrokeAndFillPath,         /* pStrokeAndFillPath */
116     MFDRV_StrokePath,                /* pStrokePath */
117     NULL,                            /* pSwapBuffers */
118     MFDRV_WidenPath                  /* pWidenPath */
119 };
120
121
122
123 /**********************************************************************
124  *           MFDRV_AllocMetaFile
125  */
126 static DC *MFDRV_AllocMetaFile(void)
127 {
128     DC *dc;
129     METAFILEDRV_PDEVICE *physDev;
130     
131     if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
132     dc->header.wMagic = METAFILE_DC_MAGIC;
133
134     physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
135     if (!physDev)
136     {
137         GDI_FreeObject( dc->hSelf, dc );
138         return NULL;
139     }
140     dc->physDev = physDev;
141
142     if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
143     {
144         HeapFree( GetProcessHeap(), 0, physDev );
145         GDI_FreeObject( dc->hSelf, dc );
146         return NULL;
147     }
148
149     physDev->nextHandle = 0;
150     physDev->hFile = 0;
151
152     physDev->mh->mtHeaderSize   = sizeof(METAHEADER) / sizeof(WORD);
153     physDev->mh->mtVersion      = 0x0300;
154     physDev->mh->mtSize         = physDev->mh->mtHeaderSize;
155     physDev->mh->mtNoObjects    = 0;
156     physDev->mh->mtMaxRecord    = 0;
157     physDev->mh->mtNoParameters = 0;
158
159     return dc;
160 }
161
162
163 /**********************************************************************
164  *           MFDRV_DeleteDC
165  */
166 static BOOL MFDRV_DeleteDC( DC *dc )
167 {
168     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
169     
170     if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
171     HeapFree( GetProcessHeap(), 0, physDev );
172     dc->physDev = NULL;
173     GDI_FreeObject( dc->hSelf, dc );
174     return TRUE;
175 }
176
177 /**********************************************************************
178  *           CreateMetaFile16   (GDI.125)
179  *           CreateMetaFile16   (GDI32.@)
180  *
181  *  Create a new DC and associate it with a metafile. Pass a filename
182  *  to create a disk-based metafile, NULL to create a memory metafile.
183  *
184  * RETURNS
185  *  A handle to the metafile DC if successful, NULL on failure.
186  */
187 HDC16 WINAPI CreateMetaFile16( 
188                               LPCSTR filename /* [in] Filename of disk metafile */
189 )
190 {
191     HDC ret;
192     DC *dc;
193     METAFILEDRV_PDEVICE *physDev;
194     HFILE hFile;
195
196     TRACE("'%s'\n", filename );
197
198     if (!(dc = MFDRV_AllocMetaFile())) return 0;
199     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
200     
201     if (filename)  /* disk based metafile */
202     {
203         physDev->mh->mtType = METAFILE_DISK;
204         if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
205                                 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
206             MFDRV_DeleteDC( dc );
207             return 0;
208         }
209         if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
210                         NULL )) {
211             MFDRV_DeleteDC( dc );
212             return 0;
213         }
214         physDev->hFile = hFile;
215
216         /* Grow METAHEADER to include filename */
217         physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
218     }
219     else  /* memory based metafile */
220         physDev->mh->mtType = METAFILE_MEMORY;
221         
222     TRACE("returning %04x\n", dc->hSelf);
223     ret = dc->hSelf;
224     GDI_ReleaseObj( dc->hSelf );
225     return ret;
226 }
227
228 /**********************************************************************
229  *           CreateMetaFileA   (GDI32.@)
230  */
231 HDC WINAPI CreateMetaFileA( 
232                               LPCSTR filename /* [in] Filename of disk metafile */
233 )
234 {
235   return CreateMetaFile16( filename );
236 }
237
238 /**********************************************************************
239  *          CreateMetaFileW   (GDI32.@)
240  */
241 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
242 {
243     LPSTR filenameA;
244     HDC hReturnDC;
245
246     filenameA = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
247
248     hReturnDC = CreateMetaFileA(filenameA);
249
250     HeapFree( GetProcessHeap(), 0, filenameA );
251
252     return hReturnDC;
253 }
254
255
256 /**********************************************************************
257  *          MFDRV_CloseMetaFile
258  */
259 static DC *MFDRV_CloseMetaFile( HDC hdc ) 
260 {
261     DC *dc;
262     METAFILEDRV_PDEVICE *physDev;
263     
264     TRACE("(%04x)\n", hdc );
265
266     if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
267     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
268
269     /* Construct the end of metafile record - this is documented
270      * in SDK Knowledgebase Q99334.
271      */
272
273     if (!MFDRV_MetaParam0(dc, META_EOF))
274     {
275         MFDRV_DeleteDC( dc );
276         return 0;
277     }   
278
279     if (physDev->mh->mtType == METAFILE_DISK)  /* disk based metafile */
280     {
281         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
282             MFDRV_DeleteDC( dc );
283             return 0;
284         }
285
286         physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
287         if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
288                        sizeof(*physDev->mh), NULL, NULL)) {
289             MFDRV_DeleteDC( dc );
290             return 0;
291         }
292         CloseHandle(physDev->hFile);
293         physDev->mh->mtType = METAFILE_DISK;
294     }
295
296     return dc;
297 }
298
299 /******************************************************************
300  *           CloseMetaFile16   (GDI.126)
301  *           CloseMetaFile16   (GDI32.@)
302  */
303 HMETAFILE16 WINAPI CloseMetaFile16( 
304                                    HDC16 hdc /* [in] Metafile DC to close */
305 )
306 {
307     HMETAFILE16 hmf;
308     METAFILEDRV_PDEVICE *physDev;
309     DC *dc = MFDRV_CloseMetaFile(hdc);
310     if (!dc) return 0;
311     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
312
313     /* Now allocate a global handle for the metafile */
314
315     hmf = MF_Create_HMETAFILE16( physDev->mh );
316
317     physDev->mh = NULL;  /* So it won't be deleted */
318     MFDRV_DeleteDC( dc );
319     return hmf;
320 }
321
322 /******************************************************************
323  *           CloseMetaFile   (GDI32.@)
324  *
325  *  Stop recording graphics operations in metafile associated with
326  *  hdc and retrieve metafile.
327  *
328  * RETURNS
329  *  Handle of newly created metafile on success, NULL on failure.
330  */
331 HMETAFILE WINAPI CloseMetaFile( 
332                                    HDC hdc /* [in] Metafile DC to close */
333 )
334 {
335     HMETAFILE hmf;
336     METAFILEDRV_PDEVICE *physDev;
337     DC *dc = MFDRV_CloseMetaFile(hdc);
338     if (!dc) return 0;
339     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
340
341     /* Now allocate a global handle for the metafile */
342
343     hmf = MF_Create_HMETAFILE( physDev->mh );
344
345     physDev->mh = NULL;  /* So it won't be deleted */
346     MFDRV_DeleteDC( dc );
347     return hmf;
348 }
349
350
351 /******************************************************************
352  *         MFDRV_WriteRecord
353  *
354  * Warning: this function can change the pointer to the metafile header.
355  */
356 BOOL MFDRV_WriteRecord( DC *dc, METARECORD *mr, DWORD rlen)
357 {
358     DWORD len;
359     METAHEADER *mh;
360     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
361
362     switch(physDev->mh->mtType)
363     {
364     case METAFILE_MEMORY:
365         len = physDev->mh->mtSize * 2 + rlen;
366         mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
367         if (!mh) return FALSE;
368         physDev->mh = mh;
369         memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
370         break;
371     case METAFILE_DISK:
372         TRACE("Writing record to disk\n");
373         if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
374             return FALSE;
375         break;
376     default:
377         ERR("Unknown metafile type %d\n", physDev->mh->mtType );
378         return FALSE;
379     }
380
381     physDev->mh->mtSize += rlen / 2;
382     physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
383     return TRUE;
384 }
385
386
387 /******************************************************************
388  *         MFDRV_MetaParam0
389  */
390
391 BOOL MFDRV_MetaParam0(DC *dc, short func)
392 {
393     char buffer[8];
394     METARECORD *mr = (METARECORD *)&buffer;
395     
396     mr->rdSize = 3;
397     mr->rdFunction = func;
398     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
399 }
400
401
402 /******************************************************************
403  *         MFDRV_MetaParam1
404  */
405 BOOL MFDRV_MetaParam1(DC *dc, short func, short param1)
406 {
407     char buffer[8];
408     METARECORD *mr = (METARECORD *)&buffer;
409     
410     mr->rdSize = 4;
411     mr->rdFunction = func;
412     *(mr->rdParm) = param1;
413     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
414 }
415
416
417 /******************************************************************
418  *         MFDRV_MetaParam2
419  */
420 BOOL MFDRV_MetaParam2(DC *dc, short func, short param1, short param2)
421 {
422     char buffer[10];
423     METARECORD *mr = (METARECORD *)&buffer;
424     
425     mr->rdSize = 5;
426     mr->rdFunction = func;
427     *(mr->rdParm) = param2;
428     *(mr->rdParm + 1) = param1;
429     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
430 }
431
432
433 /******************************************************************
434  *         MFDRV_MetaParam4
435  */
436
437 BOOL MFDRV_MetaParam4(DC *dc, short func, short param1, short param2, 
438                       short param3, short param4)
439 {
440     char buffer[14];
441     METARECORD *mr = (METARECORD *)&buffer;
442     
443     mr->rdSize = 7;
444     mr->rdFunction = func;
445     *(mr->rdParm) = param4;
446     *(mr->rdParm + 1) = param3;
447     *(mr->rdParm + 2) = param2;
448     *(mr->rdParm + 3) = param1;
449     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
450 }
451
452
453 /******************************************************************
454  *         MFDRV_MetaParam6
455  */
456
457 BOOL MFDRV_MetaParam6(DC *dc, short func, short param1, short param2, 
458                       short param3, short param4, short param5, short param6)
459 {
460     char buffer[18];
461     METARECORD *mr = (METARECORD *)&buffer;
462     
463     mr->rdSize = 9;
464     mr->rdFunction = func;
465     *(mr->rdParm) = param6;
466     *(mr->rdParm + 1) = param5;
467     *(mr->rdParm + 2) = param4;
468     *(mr->rdParm + 3) = param3;
469     *(mr->rdParm + 4) = param2;
470     *(mr->rdParm + 5) = param1;
471     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
472 }
473
474
475 /******************************************************************
476  *         MFDRV_MetaParam8
477  */
478 BOOL MFDRV_MetaParam8(DC *dc, short func, short param1, short param2, 
479                       short param3, short param4, short param5,
480                       short param6, short param7, short param8)
481 {
482     char buffer[22];
483     METARECORD *mr = (METARECORD *)&buffer;
484     
485     mr->rdSize = 11;
486     mr->rdFunction = func;
487     *(mr->rdParm) = param8;
488     *(mr->rdParm + 1) = param7;
489     *(mr->rdParm + 2) = param6;
490     *(mr->rdParm + 3) = param5;
491     *(mr->rdParm + 4) = param4;
492     *(mr->rdParm + 5) = param3;
493     *(mr->rdParm + 6) = param2;
494     *(mr->rdParm + 7) = param1;
495     return MFDRV_WriteRecord( dc, mr, mr->rdSize * 2);
496 }
497
498
499 /******************************************************************
500  *         MFDRV_AddHandleDC
501  *
502  * Note: this function assumes that we never delete objects.
503  * If we do someday, we'll need to maintain a table to re-use deleted
504  * handles.
505  */
506 int MFDRV_AddHandleDC( DC *dc )
507 {
508     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
509     physDev->mh->mtNoObjects++;
510     return physDev->nextHandle++;
511 }
512
513