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