Changed the GDI driver interface to pass an opaque PHYSDEV pointer
[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     EMFDRV_RestoreDC,                /* pRestoreDC */
96     EMFDRV_RoundRect,                /* pRoundRect */
97     EMFDRV_SaveDC,                   /* pSaveDC */
98     EMFDRV_ScaleViewportExt,         /* pScaleViewportExt */
99     EMFDRV_ScaleWindowExt,           /* pScaleWindowExt */
100     EMFDRV_SelectBitmap,             /* pSelectBitmap */
101     EMFDRV_SelectBrush,              /* pSelectBrush */
102     EMFDRV_SelectClipPath,           /* pSelectClipPath */
103     NULL,                            /* pSelectClipRgn */
104     EMFDRV_SelectFont,               /* pSelectFont */
105     NULL,                            /* pSelectPalette */
106     EMFDRV_SelectPen,                /* pSelectPen */
107     EMFDRV_SetBkColor,               /* pSetBkColor */
108     EMFDRV_SetBkMode,                /* pSetBkMode */
109     NULL,                            /* pSetDIBColorTable */
110     NULL,                            /* pSetDIBits */
111     NULL,                            /* pSetDIBitsToDevice */
112     NULL,                            /* pSetDeviceClipping */
113     NULL,                            /* pSetDeviceGammaRamp */
114     EMFDRV_SetMapMode,               /* pSetMapMode */
115     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
116     NULL,                            /* pSetPixel */
117     NULL,                            /* pSetPixelFormat */
118     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
119     EMFDRV_SetROP2,                  /* pSetROP2 */
120     NULL,                            /* pSetRelAbs */
121     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
122     EMFDRV_SetTextAlign,             /* pSetTextAlign */
123     NULL,                            /* pSetTextCharacterExtra */
124     EMFDRV_SetTextColor,             /* pSetTextColor */
125     NULL,                            /* pSetTextJustification */
126     EMFDRV_SetViewportExt,           /* pSetViewportExt */
127     EMFDRV_SetViewportOrg,           /* pSetViewportOrg */
128     EMFDRV_SetWindowExt,             /* pSetWindowExt */
129     EMFDRV_SetWindowOrg,             /* pSetWindowOrg */
130     NULL,                            /* pStartDoc */
131     NULL,                            /* pStartPage */
132     NULL,                            /* pStretchBlt */
133     NULL,                            /* pStretchDIBits */
134     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
135     EMFDRV_StrokePath,               /* pStrokePath */
136     NULL,                            /* pSwapBuffers */
137     EMFDRV_WidenPath                 /* pWiddenPath */
138 };
139
140
141 /**********************************************************************
142  *           EMFDRV_DeleteDC
143  */
144 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
145 {
146     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
147     DC *dc = physDev->dc;
148
149     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
150     HeapFree( GetProcessHeap(), 0, physDev );
151     dc->physDev = NULL;
152     GDI_FreeObject( dc->hSelf, dc );
153     return TRUE;
154 }
155
156
157 /******************************************************************
158  *         EMFDRV_WriteRecord
159  *
160  * Warning: this function can change the pointer to the metafile header.
161  */
162 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
163 {
164     DWORD len;
165     ENHMETAHEADER *emh;
166     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
167
168     physDev->emh->nBytes += emr->nSize;
169     physDev->emh->nRecords++;
170
171     if(physDev->hFile) {
172         TRACE("Writing record to disk\n");
173         if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
174             return FALSE;
175     } else {
176         len = physDev->emh->nBytes;
177         emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
178         if (!emh) return FALSE;
179         physDev->emh = emh;
180         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
181                emr->nSize);
182     }
183     return TRUE;
184 }
185
186
187 /******************************************************************
188  *         EMFDRV_UpdateBBox
189  */
190 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
191 {
192     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
193     RECTL *bounds = &physDev->emh->rclBounds;
194
195     if(bounds->left > bounds->right) {/* first rect */
196         *bounds = *rect;
197         return;
198     }
199     bounds->left   = min(bounds->left,   rect->left);
200     bounds->top    = min(bounds->top,    rect->top);
201     bounds->right  = max(bounds->right,  rect->right);
202     bounds->bottom = max(bounds->bottom, rect->bottom);
203     return;
204 }
205
206 /******************************************************************
207  *         EMFDRV_AddHandleDC
208  *
209  * Note: this function assumes that we never delete objects.
210  * If we do someday, we'll need to maintain a table to re-use deleted
211  * handles.
212  */
213 int EMFDRV_AddHandleDC( PHYSDEV dev )
214 {
215     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
216     physDev->emh->nHandles++;
217     return physDev->nextHandle++;
218 }
219
220
221 /**********************************************************************
222  *          CreateEnhMetaFileA   (GDI32.@)
223  */
224 HDC WINAPI CreateEnhMetaFileA( 
225     HDC hdc,           /* [in] optional reference DC */
226     LPCSTR filename,   /* [in] optional filename for disk metafiles */
227     const RECT *rect,  /* [in] optional bounding rectangle */
228     LPCSTR description /* [in] optional description */ 
229     )
230 {
231     LPWSTR filenameW = NULL;
232     LPWSTR descriptionW = NULL;
233     HDC hReturnDC;
234     DWORD len1, len2, total;
235
236     if(filename)
237     {
238         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
239         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
240         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
241     }
242     if(description) {
243         len1 = strlen(description);
244         len2 = strlen(description + len1 + 1);
245         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
246         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
247         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
248     }
249
250     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
251
252     if(filenameW)
253         HeapFree( GetProcessHeap(), 0, filenameW );
254     if(descriptionW)
255         HeapFree( GetProcessHeap(), 0, descriptionW );
256
257     return hReturnDC;
258 }
259
260 /**********************************************************************
261  *          CreateEnhMetaFileW   (GDI32.@)
262  */
263 HDC WINAPI CreateEnhMetaFileW(
264     HDC           hdc,        /* [in] optional reference DC */
265     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
266     const RECT*   rect,       /* [in] optional bounding rectangle */
267     LPCWSTR       description /* [in] optional description */ 
268     )
269 {
270     HDC ret;
271     DC *dc;
272     HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
273     EMFDRV_PDEVICE *physDev;
274     HFILE hFile;
275     DWORD size = 0, length = 0;
276
277     TRACE("%s\n", debugstr_w(filename) );
278
279     if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
280     dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
281
282     physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
283     if (!physDev) {
284         GDI_FreeObject( dc->hSelf, dc );
285         return 0;
286     }
287     dc->physDev = (PHYSDEV)physDev;
288     physDev->hdc = dc->hSelf;
289     physDev->dc = dc;
290
291     if(description) { /* App name\0Title\0\0 */
292         length = lstrlenW(description);
293         length += lstrlenW(description + length + 1);
294         length += 3;
295         length *= 2;
296     }
297     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
298
299     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
300         HeapFree( GetProcessHeap(), 0, physDev );
301         GDI_FreeObject( dc->hSelf, dc );
302         return 0;
303     }
304
305     physDev->nextHandle = 1;
306     physDev->hFile = 0;
307
308     physDev->emh->iType = EMR_HEADER;
309     physDev->emh->nSize = size;
310
311     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
312     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
313
314     if(rect) {
315         physDev->emh->rclFrame.left   = rect->left;
316         physDev->emh->rclFrame.top    = rect->top;
317         physDev->emh->rclFrame.right  = rect->right;
318         physDev->emh->rclFrame.bottom = rect->bottom;
319     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
320         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
321         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
322     }
323
324     physDev->emh->dSignature = ENHMETA_SIGNATURE;
325     physDev->emh->nVersion = 0x10000;
326     physDev->emh->nBytes = physDev->emh->nSize;
327     physDev->emh->nRecords = 1;
328     physDev->emh->nHandles = 1;
329
330     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
331     physDev->emh->nDescription = length / 2;
332
333     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
334
335     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
336   
337     /* Size in pixels */
338     physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
339     physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
340
341     /* Size in millimeters */
342     physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
343     physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
344
345     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
346
347     if (filename)  /* disk based metafile */
348     {
349         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
350                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
351             EMFDRV_DeleteDC( dc->physDev );
352             return 0;
353         }
354         if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
355             EMFDRV_DeleteDC( dc->physDev );
356             return 0;
357         }
358         physDev->hFile = hFile;
359     }
360
361     TRACE("returning %04x\n", dc->hSelf);
362     ret = dc->hSelf;
363     GDI_ReleaseObj( dc->hSelf );
364
365     if( !hdc )
366       DeleteDC( hRefDC );
367         
368     return ret;
369 }
370
371 /******************************************************************
372  *             CloseEnhMetaFile (GDI32.@)
373  */
374 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
375 {
376     HENHMETAFILE hmf;
377     EMFDRV_PDEVICE *physDev;
378     DC *dc;
379     EMREOF emr;
380     HANDLE hMapping = 0;
381
382     TRACE("(%04x)\n", hdc );
383
384     if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
385     physDev = (EMFDRV_PDEVICE *)dc->physDev;
386
387     emr.emr.iType = EMR_EOF;
388     emr.emr.nSize = sizeof(emr);
389     emr.nPalEntries = 0;
390     emr.offPalEntries = 0;
391     emr.nSizeLast = emr.emr.nSize;
392     EMFDRV_WriteRecord( dc->physDev, &emr.emr );
393
394     /* Update rclFrame if not initialized in CreateEnhMetaFile */
395     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
396         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
397           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
398         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
399           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
400         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
401           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
402         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
403           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
404     }
405
406     if (physDev->hFile)  /* disk based metafile */
407     {
408         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
409         {
410             CloseHandle( physDev->hFile );
411             EMFDRV_DeleteDC( dc->physDev );
412             return 0;
413         }
414
415         if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
416                        sizeof(*physDev->emh), NULL, NULL))
417         {
418             CloseHandle( physDev->hFile );
419             EMFDRV_DeleteDC( dc->physDev );
420             return 0;
421         }
422         HeapFree( GetProcessHeap(), 0, physDev->emh );
423         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
424                                       0, NULL);
425         TRACE("hMapping = %08x\n", hMapping );
426         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
427         TRACE("view = %p\n", physDev->emh );
428         CloseHandle( hMapping );
429         CloseHandle( physDev->hFile );
430     }
431
432     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
433     physDev->emh = NULL;  /* So it won't be deleted */
434     EMFDRV_DeleteDC( dc->physDev );
435     return hmf;
436 }