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