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