dsound: Always enumerate the default device first.
[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 struct gdi_dc_funcs 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,                            /* pBlendImage */
47     NULL,                            /* pChoosePixelFormat */
48     EMFDRV_Chord,                    /* pChord */
49     EMFDRV_CloseFigure,              /* pCloseFigure */
50     NULL,                            /* pCreateBitmap */
51     NULL,                            /* pCreateCompatibleDC */
52     NULL,                            /* pCreateDC */
53     NULL,                            /* pCreateDIBSection */
54     NULL,                            /* pDeleteBitmap */
55     EMFDRV_DeleteDC,                 /* pDeleteDC */
56     EMFDRV_DeleteObject,             /* pDeleteObject */
57     NULL,                            /* pDescribePixelFormat */
58     NULL,                            /* pDeviceCapabilities */
59     EMFDRV_Ellipse,                  /* pEllipse */
60     NULL,                            /* pEndDoc */
61     NULL,                            /* pEndPage */
62     EMFDRV_EndPath,                  /* pEndPath */
63     NULL,                            /* pEnumDeviceFonts */
64     NULL,                            /* pEnumICMProfiles */
65     EMFDRV_ExcludeClipRect,          /* pExcludeClipRect */
66     NULL,                            /* pExtDeviceMode */
67     NULL,                            /* pExtEscape */
68     EMFDRV_ExtFloodFill,             /* pExtFloodFill */
69     EMFDRV_ExtSelectClipRgn,         /* pExtSelectClipRgn */
70     EMFDRV_ExtTextOut,               /* pExtTextOut */
71     EMFDRV_FillPath,                 /* pFillPath */
72     EMFDRV_FillRgn,                  /* pFillRgn */
73     EMFDRV_FlattenPath,              /* pFlattenPath */
74     EMFDRV_FrameRgn,                 /* pFrameRgn */
75     EMFDRV_GdiComment,               /* pGdiComment */
76     NULL,                            /* pGetCharWidth */
77     EMFDRV_GetDeviceCaps,            /* pGetDeviceCaps */
78     NULL,                            /* pGetDeviceGammaRamp */
79     NULL,                            /* pGetICMProfile */
80     NULL,                            /* pGetImage */
81     NULL,                            /* pGetNearestColor */
82     NULL,                            /* pGetPixel */
83     NULL,                            /* pGetPixelFormat */
84     NULL,                            /* pGetSystemPaletteEntries */
85     NULL,                            /* pGetTextExtentExPoint */
86     NULL,                            /* pGetTextMetrics */
87     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
88     EMFDRV_InvertRgn,                /* pInvertRgn */
89     EMFDRV_LineTo,                   /* pLineTo */
90     EMFDRV_ModifyWorldTransform,     /* pModifyWorldTransform */
91     EMFDRV_MoveTo,                   /* pMoveTo */
92     EMFDRV_OffsetClipRgn,            /* pOffsetClipRgn */
93     EMFDRV_OffsetViewportOrgEx,      /* pOffsetViewportOrgEx */
94     EMFDRV_OffsetWindowOrgEx,        /* pOffsetWindowOrgEx */
95     EMFDRV_PaintRgn,                 /* pPaintRgn */
96     EMFDRV_PatBlt,                   /* pPatBlt */
97     EMFDRV_Pie,                      /* pPie */
98     NULL,                            /* pPolyBezier */
99     NULL,                            /* pPolyBezierTo */
100     NULL,                            /* pPolyDraw */
101     EMFDRV_PolyPolygon,              /* pPolyPolygon */
102     EMFDRV_PolyPolyline,             /* pPolyPolyline */
103     EMFDRV_Polygon,                  /* pPolygon */
104     EMFDRV_Polyline,                 /* pPolyline */
105     NULL,                            /* pPolylineTo */
106     NULL,                            /* pPutImage */
107     NULL,                            /* pRealizeDefaultPalette */
108     NULL,                            /* pRealizePalette */
109     EMFDRV_Rectangle,                /* pRectangle */
110     NULL,                            /* pResetDC */
111     EMFDRV_RestoreDC,                /* pRestoreDC */
112     EMFDRV_RoundRect,                /* pRoundRect */
113     EMFDRV_SaveDC,                   /* pSaveDC */
114     EMFDRV_ScaleViewportExtEx,       /* pScaleViewportExtEx */
115     EMFDRV_ScaleWindowExtEx,         /* pScaleWindowExtEx */
116     EMFDRV_SelectBitmap,             /* pSelectBitmap */
117     EMFDRV_SelectBrush,              /* pSelectBrush */
118     EMFDRV_SelectClipPath,           /* pSelectClipPath */
119     EMFDRV_SelectFont,               /* pSelectFont */
120     EMFDRV_SelectPalette,            /* pSelectPalette */
121     EMFDRV_SelectPen,                /* pSelectPen */
122     EMFDRV_SetArcDirection,          /* pSetArcDirection */
123     EMFDRV_SetBkColor,               /* pSetBkColor */
124     EMFDRV_SetBkMode,                /* pSetBkMode */
125     EMFDRV_SetDCBrushColor,          /* pSetDCBrushColor*/
126     EMFDRV_SetDCPenColor,            /* pSetDCPenColor*/
127     NULL,                            /* pSetDIBColorTable */
128     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
129     NULL,                            /* pSetDeviceClipping */
130     NULL,                            /* pSetDeviceGammaRamp */
131     EMFDRV_SetLayout,                /* pSetLayout */
132     EMFDRV_SetMapMode,               /* pSetMapMode */
133     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
134     EMFDRV_SetPixel,                 /* pSetPixel */
135     NULL,                            /* pSetPixelFormat */
136     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
137     EMFDRV_SetROP2,                  /* pSetROP2 */
138     NULL,                            /* pSetRelAbs */
139     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
140     EMFDRV_SetTextAlign,             /* pSetTextAlign */
141     NULL,                            /* pSetTextCharacterExtra */
142     EMFDRV_SetTextColor,             /* pSetTextColor */
143     EMFDRV_SetTextJustification,     /* pSetTextJustification */ 
144     EMFDRV_SetViewportExtEx,         /* pSetViewportExtEx */
145     EMFDRV_SetViewportOrgEx,         /* pSetViewportOrgEx */
146     EMFDRV_SetWindowExtEx,           /* pSetWindowExtEx */
147     EMFDRV_SetWindowOrgEx,           /* pSetWindowOrgEx */
148     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
149     NULL,                            /* pStartDoc */
150     NULL,                            /* pStartPage */
151     EMFDRV_StretchBlt,               /* pStretchBlt */
152     EMFDRV_StretchDIBits,            /* pStretchDIBits */
153     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
154     EMFDRV_StrokePath,               /* pStrokePath */
155     NULL,                            /* pSwapBuffers */
156     NULL,                            /* pUnrealizePalette */
157     EMFDRV_WidenPath                 /* pWidenPath */
158 };
159
160
161 /**********************************************************************
162  *           EMFDRV_DeleteDC
163  */
164 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
165 {
166     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
167     UINT index;
168
169     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
170     for(index = 0; index < physDev->handles_size; index++)
171         if(physDev->handles[index])
172             GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
173     HeapFree( GetProcessHeap(), 0, physDev->handles );
174     HeapFree( GetProcessHeap(), 0, physDev );
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( dev->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     HeapFree( GetProcessHeap(), 0, filenameW );
289     HeapFree( GetProcessHeap(), 0, descriptionW );
290
291     return hReturnDC;
292 }
293
294 /**********************************************************************
295  *          CreateEnhMetaFileW   (GDI32.@)
296  */
297 HDC WINAPI CreateEnhMetaFileW(
298     HDC           hdc,        /* [in] optional reference DC */
299     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
300     const RECT*   rect,       /* [in] optional bounding rectangle */
301     LPCWSTR       description /* [in] optional description */
302     )
303 {
304     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
305     HDC ret;
306     DC *dc;
307     HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL); 
308         /* If no ref, use current display */
309     EMFDRV_PDEVICE *physDev;
310     HANDLE hFile;
311     DWORD size = 0, length = 0;
312
313     TRACE("%s\n", debugstr_w(filename) );
314
315     if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
316
317     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
318     if (!physDev) {
319         free_dc_ptr( dc );
320         return 0;
321     }
322     if(description) { /* App name\0Title\0\0 */
323         length = lstrlenW(description);
324         length += lstrlenW(description + length + 1);
325         length += 3;
326         length *= 2;
327     }
328     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
329
330     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
331         HeapFree( GetProcessHeap(), 0, physDev );
332         free_dc_ptr( dc );
333         return 0;
334     }
335
336     push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
337
338     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
339     physDev->handles_size = HANDLE_LIST_INC;
340     physDev->cur_handles = 1;
341     physDev->hFile = 0;
342     physDev->dc_brush = 0;
343     physDev->dc_pen = 0;
344     physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
345     physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
346     physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
347     physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
348     physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
349     physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
350     physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
351     physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
352     physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
353     physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
354     physDev->planes = GetDeviceCaps(hRefDC, PLANES);
355     physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
356     physDev->restoring = 0;
357
358     SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
359
360     physDev->emh->iType = EMR_HEADER;
361     physDev->emh->nSize = size;
362
363     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
364     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
365
366     if(rect) {
367         physDev->emh->rclFrame.left   = rect->left;
368         physDev->emh->rclFrame.top    = rect->top;
369         physDev->emh->rclFrame.right  = rect->right;
370         physDev->emh->rclFrame.bottom = rect->bottom;
371     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
372         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
373         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
374     }
375
376     physDev->emh->dSignature = ENHMETA_SIGNATURE;
377     physDev->emh->nVersion = 0x10000;
378     physDev->emh->nBytes = physDev->emh->nSize;
379     physDev->emh->nRecords = 1;
380     physDev->emh->nHandles = 1;
381
382     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
383     physDev->emh->nDescription = length / 2;
384
385     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
386
387     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
388
389     /* Size in pixels */
390     physDev->emh->szlDevice.cx = physDev->horzres;
391     physDev->emh->szlDevice.cy = physDev->vertres;
392
393     /* Size in millimeters */
394     physDev->emh->szlMillimeters.cx = physDev->horzsize;
395     physDev->emh->szlMillimeters.cy = physDev->vertsize;
396
397     /* Size in micrometers */
398     physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
399     physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
400
401     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
402
403     if (filename)  /* disk based metafile */
404     {
405         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
406                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
407             free_dc_ptr( dc );
408             return 0;
409         }
410         if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
411             free_dc_ptr( dc );
412             return 0;
413         }
414         physDev->hFile = hFile;
415     }
416
417     TRACE("returning %p\n", physDev->dev.hdc);
418     ret = physDev->dev.hdc;
419     release_dc_ptr( dc );
420
421     if( !hdc )
422       DeleteDC( hRefDC );
423
424     return ret;
425 }
426
427 /******************************************************************
428  *             CloseEnhMetaFile (GDI32.@)
429  */
430 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
431 {
432     HENHMETAFILE hmf;
433     EMFDRV_PDEVICE *physDev;
434     DC *dc;
435     EMREOF emr;
436     HANDLE hMapping = 0;
437
438     TRACE("(%p)\n", hdc );
439
440     if (!(dc = get_dc_ptr( hdc ))) return NULL;
441     if (dc->header.type != OBJ_ENHMETADC)
442     {
443         release_dc_ptr( dc );
444         return NULL;
445     }
446     if (dc->refcount != 1)
447     {
448         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
449         release_dc_ptr( dc );
450         return NULL;
451     }
452     physDev = (EMFDRV_PDEVICE *)dc->physDev;
453
454     if(dc->saveLevel)
455         RestoreDC(hdc, 1);
456
457     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
458     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
459
460     emr.emr.iType = EMR_EOF;
461     emr.emr.nSize = sizeof(emr);
462     emr.nPalEntries = 0;
463     emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
464     emr.nSizeLast = emr.emr.nSize;
465     EMFDRV_WriteRecord( dc->physDev, &emr.emr );
466
467     /* Update rclFrame if not initialized in CreateEnhMetaFile */
468     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
469         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
470           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
471         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
472           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
473         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
474           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
475         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
476           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
477     }
478
479     if (physDev->hFile)  /* disk based metafile */
480     {
481         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
482         {
483             CloseHandle( physDev->hFile );
484             free_dc_ptr( dc );
485             return 0;
486         }
487
488         if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
489                        NULL, NULL))
490         {
491             CloseHandle( physDev->hFile );
492             free_dc_ptr( dc );
493             return 0;
494         }
495         HeapFree( GetProcessHeap(), 0, physDev->emh );
496         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
497                                       0, NULL);
498         TRACE("hMapping = %p\n", hMapping );
499         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
500         TRACE("view = %p\n", physDev->emh );
501         CloseHandle( hMapping );
502         CloseHandle( physDev->hFile );
503     }
504
505     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
506     physDev->emh = NULL;  /* So it won't be deleted */
507     free_dc_ptr( dc );
508     return hmf;
509 }