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