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