mshtml: Added IHTMLWindow6::get_sessionStorage implementation.
[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     EMFDRV_Chord,                    /* pChord */
48     EMFDRV_CloseFigure,              /* pCloseFigure */
49     NULL,                            /* pCreateCompatibleDC */
50     NULL,                            /* pCreateDC */
51     EMFDRV_DeleteDC,                 /* pDeleteDC */
52     EMFDRV_DeleteObject,             /* pDeleteObject */
53     NULL,                            /* pDeviceCapabilities */
54     EMFDRV_Ellipse,                  /* pEllipse */
55     NULL,                            /* pEndDoc */
56     NULL,                            /* pEndPage */
57     EMFDRV_EndPath,                  /* pEndPath */
58     NULL,                            /* pEnumFonts */
59     NULL,                            /* pEnumICMProfiles */
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     NULL,                            /* pFontIsLinked */
70     EMFDRV_FrameRgn,                 /* pFrameRgn */
71     EMFDRV_GdiComment,               /* pGdiComment */
72     NULL,                            /* pGdiRealizationInfo */
73     NULL,                            /* pGetBoundsRect */
74     NULL,                            /* pGetCharABCWidths */
75     NULL,                            /* pGetCharABCWidthsI */
76     NULL,                            /* pGetCharWidth */
77     EMFDRV_GetDeviceCaps,            /* pGetDeviceCaps */
78     NULL,                            /* pGetDeviceGammaRamp */
79     NULL,                            /* pGetFontData */
80     NULL,                            /* pGetFontUnicodeRanges */
81     NULL,                            /* pGetGlyphIndices */
82     NULL,                            /* pGetGlyphOutline */
83     NULL,                            /* pGetICMProfile */
84     NULL,                            /* pGetImage */
85     NULL,                            /* pGetKerningPairs */
86     NULL,                            /* pGetNearestColor */
87     NULL,                            /* pGetOutlineTextMetrics */
88     NULL,                            /* pGetPixel */
89     NULL,                            /* pGetSystemPaletteEntries */
90     NULL,                            /* pGetTextCharsetInfo */
91     NULL,                            /* pGetTextExtentExPoint */
92     NULL,                            /* pGetTextExtentExPointI */
93     NULL,                            /* pGetTextFace */
94     NULL,                            /* pGetTextMetrics */
95     NULL,                            /* pGradientFill */
96     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
97     EMFDRV_InvertRgn,                /* pInvertRgn */
98     EMFDRV_LineTo,                   /* pLineTo */
99     EMFDRV_ModifyWorldTransform,     /* pModifyWorldTransform */
100     EMFDRV_MoveTo,                   /* pMoveTo */
101     EMFDRV_OffsetClipRgn,            /* pOffsetClipRgn */
102     EMFDRV_OffsetViewportOrgEx,      /* pOffsetViewportOrgEx */
103     EMFDRV_OffsetWindowOrgEx,        /* pOffsetWindowOrgEx */
104     EMFDRV_PaintRgn,                 /* pPaintRgn */
105     EMFDRV_PatBlt,                   /* pPatBlt */
106     EMFDRV_Pie,                      /* pPie */
107     EMFDRV_PolyBezier,               /* pPolyBezier */
108     EMFDRV_PolyBezierTo,             /* pPolyBezierTo */
109     NULL,                            /* pPolyDraw */
110     EMFDRV_PolyPolygon,              /* pPolyPolygon */
111     EMFDRV_PolyPolyline,             /* pPolyPolyline */
112     EMFDRV_Polygon,                  /* pPolygon */
113     EMFDRV_Polyline,                 /* pPolyline */
114     NULL,                            /* pPolylineTo */
115     NULL,                            /* pPutImage */
116     NULL,                            /* pRealizeDefaultPalette */
117     NULL,                            /* pRealizePalette */
118     EMFDRV_Rectangle,                /* pRectangle */
119     NULL,                            /* pResetDC */
120     EMFDRV_RestoreDC,                /* pRestoreDC */
121     EMFDRV_RoundRect,                /* pRoundRect */
122     EMFDRV_SaveDC,                   /* pSaveDC */
123     EMFDRV_ScaleViewportExtEx,       /* pScaleViewportExtEx */
124     EMFDRV_ScaleWindowExtEx,         /* pScaleWindowExtEx */
125     EMFDRV_SelectBitmap,             /* pSelectBitmap */
126     EMFDRV_SelectBrush,              /* pSelectBrush */
127     EMFDRV_SelectClipPath,           /* pSelectClipPath */
128     EMFDRV_SelectFont,               /* pSelectFont */
129     EMFDRV_SelectPalette,            /* pSelectPalette */
130     EMFDRV_SelectPen,                /* pSelectPen */
131     EMFDRV_SetArcDirection,          /* pSetArcDirection */
132     EMFDRV_SetBkColor,               /* pSetBkColor */
133     EMFDRV_SetBkMode,                /* pSetBkMode */
134     NULL,                            /* pSetBoundsRect */
135     EMFDRV_SetDCBrushColor,          /* pSetDCBrushColor*/
136     EMFDRV_SetDCPenColor,            /* pSetDCPenColor*/
137     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
138     NULL,                            /* pSetDeviceClipping */
139     NULL,                            /* pSetDeviceGammaRamp */
140     EMFDRV_SetLayout,                /* pSetLayout */
141     EMFDRV_SetMapMode,               /* pSetMapMode */
142     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
143     EMFDRV_SetPixel,                 /* pSetPixel */
144     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
145     EMFDRV_SetROP2,                  /* pSetROP2 */
146     NULL,                            /* pSetRelAbs */
147     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
148     EMFDRV_SetTextAlign,             /* pSetTextAlign */
149     NULL,                            /* pSetTextCharacterExtra */
150     EMFDRV_SetTextColor,             /* pSetTextColor */
151     EMFDRV_SetTextJustification,     /* pSetTextJustification */ 
152     EMFDRV_SetViewportExtEx,         /* pSetViewportExtEx */
153     EMFDRV_SetViewportOrgEx,         /* pSetViewportOrgEx */
154     EMFDRV_SetWindowExtEx,           /* pSetWindowExtEx */
155     EMFDRV_SetWindowOrgEx,           /* pSetWindowOrgEx */
156     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
157     NULL,                            /* pStartDoc */
158     NULL,                            /* pStartPage */
159     EMFDRV_StretchBlt,               /* pStretchBlt */
160     EMFDRV_StretchDIBits,            /* pStretchDIBits */
161     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
162     EMFDRV_StrokePath,               /* pStrokePath */
163     NULL,                            /* pSwapBuffers */
164     NULL,                            /* pUnrealizePalette */
165     EMFDRV_WidenPath,                /* pWidenPath */
166     NULL,                            /* wine_get_wgl_driver */
167     GDI_PRIORITY_GRAPHICS_DRV        /* priority */
168 };
169
170
171 /**********************************************************************
172  *           EMFDRV_DeleteDC
173  */
174 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
175 {
176     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
177     UINT index;
178
179     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
180     for(index = 0; index < physDev->handles_size; index++)
181         if(physDev->handles[index])
182             GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
183     HeapFree( GetProcessHeap(), 0, physDev->handles );
184     HeapFree( GetProcessHeap(), 0, physDev );
185     return TRUE;
186 }
187
188
189 /******************************************************************
190  *         EMFDRV_WriteRecord
191  *
192  * Warning: this function can change the pointer to the metafile header.
193  */
194 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
195 {
196     DWORD len;
197     ENHMETAHEADER *emh;
198     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
199
200     TRACE("record %d, size %d %s\n",
201           emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
202
203     assert( !(emr->nSize & 3) );
204
205     physDev->emh->nBytes += emr->nSize;
206     physDev->emh->nRecords++;
207
208     if(physDev->hFile) {
209         if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
210             return FALSE;
211     } else {
212         DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
213         len = physDev->emh->nBytes;
214         if (len > nEmfSize) {
215             nEmfSize += (nEmfSize / 2) + emr->nSize;
216             emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
217             if (!emh) return FALSE;
218             physDev->emh = emh;
219         }
220         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
221                emr->nSize);
222     }
223     return TRUE;
224 }
225
226
227 /******************************************************************
228  *         EMFDRV_UpdateBBox
229  */
230 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
231 {
232     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
233     RECTL *bounds = &physDev->emh->rclBounds;
234     RECTL vportRect = *rect;
235
236     LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
237
238     /* The coordinate systems may be mirrored
239        (LPtoDP handles points, not rectangles) */
240     if (vportRect.left > vportRect.right)
241     {
242         LONG temp = vportRect.right;
243         vportRect.right = vportRect.left;
244         vportRect.left = temp;
245     }
246     if (vportRect.top > vportRect.bottom)
247     {
248         LONG temp = vportRect.bottom;
249         vportRect.bottom = vportRect.top;
250         vportRect.top = temp;
251     }
252
253     if (bounds->left > bounds->right)
254     {
255         /* first bounding rectangle */
256         *bounds = vportRect;
257     }
258     else
259     {
260         bounds->left   = min(bounds->left,   vportRect.left);
261         bounds->top    = min(bounds->top,    vportRect.top);
262         bounds->right  = max(bounds->right,  vportRect.right);
263         bounds->bottom = max(bounds->bottom, vportRect.bottom);
264     }
265 }
266
267 /**********************************************************************
268  *          CreateEnhMetaFileA   (GDI32.@)
269  */
270 HDC WINAPI CreateEnhMetaFileA(
271     HDC hdc,           /* [in] optional reference DC */
272     LPCSTR filename,   /* [in] optional filename for disk metafiles */
273     const RECT *rect,  /* [in] optional bounding rectangle */
274     LPCSTR description /* [in] optional description */
275     )
276 {
277     LPWSTR filenameW = NULL;
278     LPWSTR descriptionW = NULL;
279     HDC hReturnDC;
280     DWORD len1, len2, total;
281
282     if(filename)
283     {
284         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
285         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
286         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
287     }
288     if(description) {
289         len1 = strlen(description);
290         len2 = strlen(description + len1 + 1);
291         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
292         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
293         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
294     }
295
296     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
297
298     HeapFree( GetProcessHeap(), 0, filenameW );
299     HeapFree( GetProcessHeap(), 0, descriptionW );
300
301     return hReturnDC;
302 }
303
304 /**********************************************************************
305  *          CreateEnhMetaFileW   (GDI32.@)
306  */
307 HDC WINAPI CreateEnhMetaFileW(
308     HDC           hdc,        /* [in] optional reference DC */
309     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
310     const RECT*   rect,       /* [in] optional bounding rectangle */
311     LPCWSTR       description /* [in] optional description */
312     )
313 {
314     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
315     HDC ret;
316     DC *dc;
317     EMFDRV_PDEVICE *physDev;
318     HANDLE hFile;
319     DWORD size = 0, length = 0;
320
321     TRACE("%s\n", debugstr_w(filename) );
322
323     if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
324
325     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
326     if (!physDev) {
327         free_dc_ptr( dc );
328         return 0;
329     }
330     if(description) { /* App name\0Title\0\0 */
331         length = lstrlenW(description);
332         length += lstrlenW(description + length + 1);
333         length += 3;
334         length *= 2;
335     }
336     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
337
338     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
339         HeapFree( GetProcessHeap(), 0, physDev );
340         free_dc_ptr( dc );
341         return 0;
342     }
343
344     push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
345
346     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
347     physDev->handles_size = HANDLE_LIST_INC;
348     physDev->cur_handles = 1;
349     physDev->hFile = 0;
350     physDev->dc_brush = 0;
351     physDev->dc_pen = 0;
352     physDev->screen_dc = 0;
353     physDev->restoring = 0;
354     if (hdc)  /* if no ref, use current display */
355         physDev->ref_dc = hdc;
356     else
357         physDev->ref_dc = physDev->screen_dc = CreateDCW( displayW, NULL, NULL, NULL );
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 = GetDeviceCaps( physDev->ref_dc, HORZRES );
392     physDev->emh->szlDevice.cy = GetDeviceCaps( physDev->ref_dc, VERTRES );
393
394     /* Size in millimeters */
395     physDev->emh->szlMillimeters.cx = GetDeviceCaps( physDev->ref_dc, HORZSIZE );
396     physDev->emh->szlMillimeters.cy = GetDeviceCaps( physDev->ref_dc, VERTSIZE );
397
398     /* Size in micrometers */
399     physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
400     physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 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     return ret;
423 }
424
425 /******************************************************************
426  *             CloseEnhMetaFile (GDI32.@)
427  */
428 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
429 {
430     HENHMETAFILE hmf;
431     EMFDRV_PDEVICE *physDev;
432     DC *dc;
433     EMREOF emr;
434     HANDLE hMapping = 0;
435
436     TRACE("(%p)\n", hdc );
437
438     if (!(dc = get_dc_ptr( hdc ))) return NULL;
439     if (dc->header.type != OBJ_ENHMETADC)
440     {
441         release_dc_ptr( dc );
442         return NULL;
443     }
444     if (dc->refcount != 1)
445     {
446         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
447         release_dc_ptr( dc );
448         return NULL;
449     }
450     physDev = (EMFDRV_PDEVICE *)dc->physDev;
451
452     if(dc->saveLevel)
453         RestoreDC(hdc, 1);
454
455     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
456     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
457     if (physDev->screen_dc) DeleteDC( physDev->screen_dc );
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             free_dc_ptr( 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             free_dc_ptr( 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     free_dc_ptr( dc );
507     return hmf;
508 }