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