Implement ResetDC and PHYSICALOFFSET[X|Y] devcaps.
[wine] / dlls / gdi / enhmfdrv / init.c
1 /*
2  * Enhanced MetaFile driver initialisation functions
3  *
4  * Copyright 1999 Huw D M Davies
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 <string.h>
22
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "gdi.h"
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
30
31 static const DC_FUNCTIONS EMFDRV_Funcs =
32 {
33     NULL,                            /* pAbortDoc */
34     EMFDRV_AbortPath,                /* pAbortPath */
35     NULL,                            /* pAngleArc */
36     EMFDRV_Arc,                      /* pArc */
37     NULL,                            /* pArcTo */
38     EMFDRV_BeginPath,                /* pBeginPath */
39     NULL,                            /* pBitBlt */
40     NULL,                            /* pBitmapBits */
41     NULL,                            /* pChoosePixelFormat */
42     EMFDRV_Chord,                    /* pChord */
43     EMFDRV_CloseFigure,              /* pCloseFigure */
44     NULL,                            /* pCreateBitmap */
45     NULL,                            /* pCreateDC */
46     NULL,                            /* pCreateDIBSection */
47     NULL,                            /* pDeleteDC */
48     NULL,                            /* pDeleteObject */
49     NULL,                            /* pDescribePixelFormat */
50     NULL,                            /* pDeviceCapabilities */
51     EMFDRV_Ellipse,                  /* pEllipse */
52     NULL,                            /* pEndDoc */
53     NULL,                            /* pEndPage */
54     EMFDRV_EndPath,                  /* pEndPath */
55     NULL,                            /* pEnumDeviceFonts */
56     EMFDRV_ExcludeClipRect,          /* pExcludeClipRect */
57     NULL,                            /* pExtDeviceMode */
58     NULL,                            /* pExtEscape */
59     EMFDRV_ExtFloodFill,             /* pExtFloodFill */
60     NULL,                            /* pExtTextOut */
61     EMFDRV_FillPath,                 /* pFillPath */
62     EMFDRV_FillRgn,                  /* pFillRgn */
63     EMFDRV_FlattenPath,              /* pFlattenPath */
64     EMFDRV_FrameRgn,                 /* pFrameRgn */
65     NULL,                            /* pGetCharWidth */
66     NULL,                            /* pGetDCOrgEx */
67     NULL,                            /* pGetDIBColorTable */
68     NULL,                            /* pGetDIBits */
69     NULL,                            /* pGetDeviceCaps */
70     NULL,                            /* pGetDeviceGammaRamp */
71     NULL,                            /* pGetPixel */
72     NULL,                            /* pGetPixelFormat */
73     NULL,                            /* pGetTextExtentPoint */
74     NULL,                            /* pGetTextMetrics */
75     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
76     EMFDRV_InvertRgn,                /* pInvertRgn */
77     EMFDRV_LineTo,                   /* pLineTo */
78     EMFDRV_MoveTo,                   /* pMoveTo */
79     EMFDRV_OffsetClipRgn,            /* pOffsetClipRgn */
80     NULL,                            /* pOffsetViewportOrg */
81     NULL,                            /* pOffsetWindowOrg */
82     EMFDRV_PaintRgn,                 /* pPaintRgn */
83     NULL,                            /* pPatBlt */
84     EMFDRV_Pie,                      /* pPie */
85     NULL,                            /* pPolyBezier */
86     NULL,                            /* pPolyBezierTo */
87     NULL,                            /* pPolyDraw */
88     EMFDRV_PolyPolygon,              /* pPolyPolygon */
89     EMFDRV_PolyPolyline,             /* pPolyPolyline */
90     EMFDRV_Polygon,                  /* pPolygon */
91     EMFDRV_Polyline,                 /* pPolyline */
92     NULL,                            /* pPolylineTo */
93     NULL,                            /* pRealizePalette */
94     EMFDRV_Rectangle,                /* pRectangle */
95     NULL,                            /* pResetDC */
96     EMFDRV_RestoreDC,                /* pRestoreDC */
97     EMFDRV_RoundRect,                /* pRoundRect */
98     EMFDRV_SaveDC,                   /* pSaveDC */
99     EMFDRV_ScaleViewportExt,         /* pScaleViewportExt */
100     EMFDRV_ScaleWindowExt,           /* pScaleWindowExt */
101     EMFDRV_SelectBitmap,             /* pSelectBitmap */
102     EMFDRV_SelectBrush,              /* pSelectBrush */
103     EMFDRV_SelectClipPath,           /* pSelectClipPath */
104     NULL,                            /* pSelectClipRgn */
105     EMFDRV_SelectFont,               /* pSelectFont */
106     NULL,                            /* pSelectPalette */
107     EMFDRV_SelectPen,                /* pSelectPen */
108     EMFDRV_SetBkColor,               /* pSetBkColor */
109     EMFDRV_SetBkMode,                /* pSetBkMode */
110     NULL,                            /* pSetDIBColorTable */
111     NULL,                            /* pSetDIBits */
112     NULL,                            /* pSetDIBitsToDevice */
113     NULL,                            /* pSetDeviceClipping */
114     NULL,                            /* pSetDeviceGammaRamp */
115     EMFDRV_SetMapMode,               /* pSetMapMode */
116     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
117     NULL,                            /* pSetPixel */
118     NULL,                            /* pSetPixelFormat */
119     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
120     EMFDRV_SetROP2,                  /* pSetROP2 */
121     NULL,                            /* pSetRelAbs */
122     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
123     EMFDRV_SetTextAlign,             /* pSetTextAlign */
124     NULL,                            /* pSetTextCharacterExtra */
125     EMFDRV_SetTextColor,             /* pSetTextColor */
126     NULL,                            /* pSetTextJustification */
127     EMFDRV_SetViewportExt,           /* pSetViewportExt */
128     EMFDRV_SetViewportOrg,           /* pSetViewportOrg */
129     EMFDRV_SetWindowExt,             /* pSetWindowExt */
130     EMFDRV_SetWindowOrg,             /* pSetWindowOrg */
131     NULL,                            /* pStartDoc */
132     NULL,                            /* pStartPage */
133     NULL,                            /* pStretchBlt */
134     NULL,                            /* pStretchDIBits */
135     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
136     EMFDRV_StrokePath,               /* pStrokePath */
137     NULL,                            /* pSwapBuffers */
138     EMFDRV_WidenPath                 /* pWiddenPath */
139 };
140
141
142 /**********************************************************************
143  *           EMFDRV_DeleteDC
144  */
145 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
146 {
147     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
148     DC *dc = physDev->dc;
149
150     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
151     HeapFree( GetProcessHeap(), 0, physDev );
152     dc->physDev = NULL;
153     GDI_FreeObject( dc->hSelf, dc );
154     return TRUE;
155 }
156
157
158 /******************************************************************
159  *         EMFDRV_WriteRecord
160  *
161  * Warning: this function can change the pointer to the metafile header.
162  */
163 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
164 {
165     DWORD len;
166     ENHMETAHEADER *emh;
167     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
168
169     physDev->emh->nBytes += emr->nSize;
170     physDev->emh->nRecords++;
171
172     if(physDev->hFile) {
173         TRACE("Writing record to disk\n");
174         if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
175             return FALSE;
176     } else {
177         len = physDev->emh->nBytes;
178         emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
179         if (!emh) return FALSE;
180         physDev->emh = emh;
181         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
182                emr->nSize);
183     }
184     return TRUE;
185 }
186
187
188 /******************************************************************
189  *         EMFDRV_UpdateBBox
190  */
191 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
192 {
193     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
194     RECTL *bounds = &physDev->emh->rclBounds;
195
196     if(bounds->left > bounds->right) {/* first rect */
197         *bounds = *rect;
198         return;
199     }
200     bounds->left   = min(bounds->left,   rect->left);
201     bounds->top    = min(bounds->top,    rect->top);
202     bounds->right  = max(bounds->right,  rect->right);
203     bounds->bottom = max(bounds->bottom, rect->bottom);
204     return;
205 }
206
207 /******************************************************************
208  *         EMFDRV_AddHandleDC
209  *
210  * Note: this function assumes that we never delete objects.
211  * If we do someday, we'll need to maintain a table to re-use deleted
212  * handles.
213  */
214 int EMFDRV_AddHandleDC( PHYSDEV dev )
215 {
216     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
217     physDev->emh->nHandles++;
218     return physDev->nextHandle++;
219 }
220
221
222 /**********************************************************************
223  *          CreateEnhMetaFileA   (GDI32.@)
224  */
225 HDC WINAPI CreateEnhMetaFileA( 
226     HDC hdc,           /* [in] optional reference DC */
227     LPCSTR filename,   /* [in] optional filename for disk metafiles */
228     const RECT *rect,  /* [in] optional bounding rectangle */
229     LPCSTR description /* [in] optional description */ 
230     )
231 {
232     LPWSTR filenameW = NULL;
233     LPWSTR descriptionW = NULL;
234     HDC hReturnDC;
235     DWORD len1, len2, total;
236
237     if(filename)
238     {
239         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
240         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
241         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
242     }
243     if(description) {
244         len1 = strlen(description);
245         len2 = strlen(description + len1 + 1);
246         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
247         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
248         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
249     }
250
251     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
252
253     if(filenameW)
254         HeapFree( GetProcessHeap(), 0, filenameW );
255     if(descriptionW)
256         HeapFree( GetProcessHeap(), 0, descriptionW );
257
258     return hReturnDC;
259 }
260
261 /**********************************************************************
262  *          CreateEnhMetaFileW   (GDI32.@)
263  */
264 HDC WINAPI CreateEnhMetaFileW(
265     HDC           hdc,        /* [in] optional reference DC */
266     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
267     const RECT*   rect,       /* [in] optional bounding rectangle */
268     LPCWSTR       description /* [in] optional description */ 
269     )
270 {
271     HDC ret;
272     DC *dc;
273     HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
274     EMFDRV_PDEVICE *physDev;
275     HFILE hFile;
276     DWORD size = 0, length = 0;
277
278     TRACE("%s\n", debugstr_w(filename) );
279
280     if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
281     dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
282
283     physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
284     if (!physDev) {
285         GDI_FreeObject( dc->hSelf, dc );
286         return 0;
287     }
288     dc->physDev = (PHYSDEV)physDev;
289     physDev->hdc = dc->hSelf;
290     physDev->dc = dc;
291
292     if(description) { /* App name\0Title\0\0 */
293         length = lstrlenW(description);
294         length += lstrlenW(description + length + 1);
295         length += 3;
296         length *= 2;
297     }
298     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
299
300     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
301         HeapFree( GetProcessHeap(), 0, physDev );
302         GDI_FreeObject( dc->hSelf, dc );
303         return 0;
304     }
305
306     physDev->nextHandle = 1;
307     physDev->hFile = 0;
308
309     physDev->emh->iType = EMR_HEADER;
310     physDev->emh->nSize = size;
311
312     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
313     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
314
315     if(rect) {
316         physDev->emh->rclFrame.left   = rect->left;
317         physDev->emh->rclFrame.top    = rect->top;
318         physDev->emh->rclFrame.right  = rect->right;
319         physDev->emh->rclFrame.bottom = rect->bottom;
320     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
321         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
322         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
323     }
324
325     physDev->emh->dSignature = ENHMETA_SIGNATURE;
326     physDev->emh->nVersion = 0x10000;
327     physDev->emh->nBytes = physDev->emh->nSize;
328     physDev->emh->nRecords = 1;
329     physDev->emh->nHandles = 1;
330
331     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
332     physDev->emh->nDescription = length / 2;
333
334     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
335
336     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
337   
338     /* Size in pixels */
339     physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
340     physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
341
342     /* Size in millimeters */
343     physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
344     physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
345
346     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
347
348     if (filename)  /* disk based metafile */
349     {
350         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
351                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
352             EMFDRV_DeleteDC( dc->physDev );
353             return 0;
354         }
355         if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
356             EMFDRV_DeleteDC( dc->physDev );
357             return 0;
358         }
359         physDev->hFile = hFile;
360     }
361
362     TRACE("returning %04x\n", dc->hSelf);
363     ret = dc->hSelf;
364     GDI_ReleaseObj( dc->hSelf );
365
366     if( !hdc )
367       DeleteDC( hRefDC );
368         
369     return ret;
370 }
371
372 /******************************************************************
373  *             CloseEnhMetaFile (GDI32.@)
374  */
375 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
376 {
377     HENHMETAFILE hmf;
378     EMFDRV_PDEVICE *physDev;
379     DC *dc;
380     EMREOF emr;
381     HANDLE hMapping = 0;
382
383     TRACE("(%04x)\n", hdc );
384
385     if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
386     physDev = (EMFDRV_PDEVICE *)dc->physDev;
387
388     emr.emr.iType = EMR_EOF;
389     emr.emr.nSize = sizeof(emr);
390     emr.nPalEntries = 0;
391     emr.offPalEntries = 0;
392     emr.nSizeLast = emr.emr.nSize;
393     EMFDRV_WriteRecord( dc->physDev, &emr.emr );
394
395     /* Update rclFrame if not initialized in CreateEnhMetaFile */
396     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
397         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
398           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
399         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
400           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
401         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
402           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
403         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
404           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
405     }
406
407     if (physDev->hFile)  /* disk based metafile */
408     {
409         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
410         {
411             CloseHandle( physDev->hFile );
412             EMFDRV_DeleteDC( dc->physDev );
413             return 0;
414         }
415
416         if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
417                        sizeof(*physDev->emh), NULL, NULL))
418         {
419             CloseHandle( physDev->hFile );
420             EMFDRV_DeleteDC( dc->physDev );
421             return 0;
422         }
423         HeapFree( GetProcessHeap(), 0, physDev->emh );
424         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
425                                       0, NULL);
426         TRACE("hMapping = %08x\n", hMapping );
427         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
428         TRACE("view = %p\n", physDev->emh );
429         CloseHandle( hMapping );
430         CloseHandle( physDev->hFile );
431     }
432
433     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
434     physDev->emh = NULL;  /* So it won't be deleted */
435     EMFDRV_DeleteDC( dc->physDev );
436     return hmf;
437 }