Implemented OleCreatePropertyFrame and
[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 "enhmetafiledrv.h"
14 #include "debugtools.h"
15
16 DEFAULT_DEBUG_CHANNEL(enhmetafile);
17
18 static const DC_FUNCTIONS EMFDRV_Funcs =
19 {
20     NULL,                            /* pAbortDoc */
21     EMFDRV_AbortPath,                /* pAbortPath */
22     NULL,                            /* pAngleArc */
23     EMFDRV_Arc,                      /* pArc */
24     NULL,                            /* pArcTo */
25     EMFDRV_BeginPath,                /* pBeginPath */
26     NULL,                            /* pBitBlt */
27     NULL,                            /* pBitmapBits */  
28     NULL,                            /* pChoosePixelFormat */
29     EMFDRV_Chord,                    /* pChord */
30     EMFDRV_CloseFigure,              /* pCloseFigure */
31     NULL,                            /* pCreateBitmap */
32     NULL, /* no implementation */    /* pCreateDC */
33     NULL,                            /* pCreateDIBSection */
34     NULL, /* no implementation */    /* pDeleteDC */
35     NULL,                            /* pDeleteObject */
36     NULL,                            /* pDescribePixelFormat */
37     NULL,                            /* pDeviceCapabilities */
38     EMFDRV_Ellipse,                  /* pEllipse */
39     NULL,                            /* pEndDoc */
40     NULL,                            /* pEndPage */
41     EMFDRV_EndPath,                  /* pEndPath */
42     NULL,                            /* pEnumDeviceFonts */
43     EMFDRV_ExcludeClipRect,          /* pExcludeClipRect */
44     NULL,                            /* pExtDeviceMode */
45     NULL,                            /* pExtEscape */
46     EMFDRV_ExtFloodFill,             /* pExtFloodFill */
47     NULL,                            /* pExtTextOut */
48     EMFDRV_FillPath,                 /* pFillPath */
49     EMFDRV_FillRgn,                  /* pFillRgn */
50     EMFDRV_FlattenPath,              /* pFlattenPath */
51     EMFDRV_FrameRgn,                 /* pFrameRgn */
52     NULL,                            /* pGetCharWidth */
53     NULL,                            /* pGetDCOrgEx */
54     NULL,                            /* pGetDeviceCaps */
55     NULL,                            /* pGetDeviceGammaRamp */
56     NULL, /* no implementation */    /* pGetPixel */
57     NULL,                            /* pGetPixelFormat */
58     NULL,                            /* pGetTextExtentPoint */
59     NULL,                            /* pGetTextMetrics */
60     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
61     EMFDRV_InvertRgn,                /* pInvertRgn */
62     EMFDRV_LineTo,                   /* pLineTo */
63     EMFDRV_MoveTo,                   /* pMoveTo */
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,                            /* pSetDeviceGammaRamp */
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.@)
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.@)
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, 0)) == INVALID_HANDLE_VALUE) {
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     TRACE("returning %04x\n", dc->hSelf);
336     ret = dc->hSelf;
337     GDI_ReleaseObj( dc->hSelf );
338
339     if( !hdc )
340       DeleteDC( hRefDC );
341         
342     return ret;
343 }
344
345 /******************************************************************
346  *             CloseEnhMetaFile (GDI32.@)
347  */
348 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
349 {
350     HENHMETAFILE hmf;
351     EMFDRV_PDEVICE *physDev;
352     DC *dc;
353     EMREOF emr;
354     HANDLE hMapping = 0;
355
356     TRACE("(%04x)\n", hdc );
357
358     if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
359     physDev = (EMFDRV_PDEVICE *)dc->physDev;
360
361     emr.emr.iType = EMR_EOF;
362     emr.emr.nSize = sizeof(emr);
363     emr.nPalEntries = 0;
364     emr.offPalEntries = 0;
365     emr.nSizeLast = emr.emr.nSize;
366     EMFDRV_WriteRecord( dc, &emr.emr );
367
368     /* Update rclFrame if not initialized in CreateEnhMetaFile */
369     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
370         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
371           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
372         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
373           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
374         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
375           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
376         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
377           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
378     }
379
380     if (physDev->hFile)  /* disk based metafile */
381     {
382         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
383         {
384             CloseHandle( physDev->hFile );
385             EMFDRV_DeleteDC( dc );
386             return 0;
387         }
388
389         if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
390                        sizeof(*physDev->emh), NULL, NULL))
391         {
392             CloseHandle( physDev->hFile );
393             EMFDRV_DeleteDC( dc );
394             return 0;
395         }
396         HeapFree( GetProcessHeap(), 0, physDev->emh );
397         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
398                                       0, NULL);
399         TRACE("hMapping = %08x\n", hMapping );
400         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
401         TRACE("view = %p\n", physDev->emh );
402         CloseHandle( hMapping );
403         CloseHandle( physDev->hFile );
404     }
405
406     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
407     physDev->emh = NULL;  /* So it won't be deleted */
408     EMFDRV_DeleteDC( dc );
409     return hmf;
410 }