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