mshtml: Rename call_event to fire_event.
[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,                            /* pGetCharWidth */
75     EMFDRV_GetDeviceCaps,            /* pGetDeviceCaps */
76     NULL,                            /* pGetDeviceGammaRamp */
77     NULL,                            /* pGetICMProfile */
78     NULL,                            /* pGetImage */
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_OffsetViewportOrgEx,      /* pOffsetViewportOrgEx */
92     EMFDRV_OffsetWindowOrgEx,        /* pOffsetWindowOrgEx */
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,                            /* pPutImage */
105     NULL,                            /* pRealizeDefaultPalette */
106     NULL,                            /* pRealizePalette */
107     EMFDRV_Rectangle,                /* pRectangle */
108     NULL,                            /* pResetDC */
109     EMFDRV_RestoreDC,                /* pRestoreDC */
110     EMFDRV_RoundRect,                /* pRoundRect */
111     EMFDRV_SaveDC,                   /* pSaveDC */
112     EMFDRV_ScaleViewportExtEx,       /* pScaleViewportExtEx */
113     EMFDRV_ScaleWindowExtEx,         /* pScaleWindowExtEx */
114     EMFDRV_SelectBitmap,             /* pSelectBitmap */
115     EMFDRV_SelectBrush,              /* pSelectBrush */
116     EMFDRV_SelectClipPath,           /* pSelectClipPath */
117     EMFDRV_SelectFont,               /* pSelectFont */
118     EMFDRV_SelectPalette,            /* pSelectPalette */
119     EMFDRV_SelectPen,                /* pSelectPen */
120     EMFDRV_SetArcDirection,          /* pSetArcDirection */
121     NULL,                            /* pSetBitmapBits */
122     EMFDRV_SetBkColor,               /* pSetBkColor */
123     EMFDRV_SetBkMode,                /* pSetBkMode */
124     EMFDRV_SetDCBrushColor,          /* pSetDCBrushColor*/
125     EMFDRV_SetDCPenColor,            /* pSetDCPenColor*/
126     NULL,                            /* pSetDIBColorTable */
127     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
128     NULL,                            /* pSetDeviceClipping */
129     NULL,                            /* pSetDeviceGammaRamp */
130     EMFDRV_SetLayout,                /* pSetLayout */
131     EMFDRV_SetMapMode,               /* pSetMapMode */
132     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
133     EMFDRV_SetPixel,                 /* pSetPixel */
134     NULL,                            /* pSetPixelFormat */
135     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
136     EMFDRV_SetROP2,                  /* pSetROP2 */
137     NULL,                            /* pSetRelAbs */
138     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
139     EMFDRV_SetTextAlign,             /* pSetTextAlign */
140     NULL,                            /* pSetTextCharacterExtra */
141     EMFDRV_SetTextColor,             /* pSetTextColor */
142     EMFDRV_SetTextJustification,     /* pSetTextJustification */ 
143     EMFDRV_SetViewportExtEx,         /* pSetViewportExtEx */
144     EMFDRV_SetViewportOrgEx,         /* pSetViewportOrgEx */
145     EMFDRV_SetWindowExtEx,           /* pSetWindowExtEx */
146     EMFDRV_SetWindowOrgEx,           /* pSetWindowOrgEx */
147     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
148     NULL,                            /* pStartDoc */
149     NULL,                            /* pStartPage */
150     EMFDRV_StretchBlt,               /* pStretchBlt */
151     EMFDRV_StretchDIBits,            /* pStretchDIBits */
152     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
153     EMFDRV_StrokePath,               /* pStrokePath */
154     NULL,                            /* pSwapBuffers */
155     NULL,                            /* pUnrealizePalette */
156     EMFDRV_WidenPath                 /* pWidenPath */
157 };
158
159
160 /**********************************************************************
161  *           EMFDRV_DeleteDC
162  */
163 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
164 {
165     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
166     UINT index;
167
168     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
169     for(index = 0; index < physDev->handles_size; index++)
170         if(physDev->handles[index])
171             GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
172     HeapFree( GetProcessHeap(), 0, physDev->handles );
173     HeapFree( GetProcessHeap(), 0, physDev );
174     return TRUE;
175 }
176
177
178 /******************************************************************
179  *         EMFDRV_WriteRecord
180  *
181  * Warning: this function can change the pointer to the metafile header.
182  */
183 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
184 {
185     DWORD len;
186     ENHMETAHEADER *emh;
187     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
188
189     TRACE("record %d, size %d %s\n",
190           emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
191
192     assert( !(emr->nSize & 3) );
193
194     physDev->emh->nBytes += emr->nSize;
195     physDev->emh->nRecords++;
196
197     if(physDev->hFile) {
198         if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
199             return FALSE;
200     } else {
201         DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
202         len = physDev->emh->nBytes;
203         if (len > nEmfSize) {
204             nEmfSize += (nEmfSize / 2) + emr->nSize;
205             emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
206             if (!emh) return FALSE;
207             physDev->emh = emh;
208         }
209         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
210                emr->nSize);
211     }
212     return TRUE;
213 }
214
215
216 /******************************************************************
217  *         EMFDRV_UpdateBBox
218  */
219 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
220 {
221     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
222     RECTL *bounds = &physDev->emh->rclBounds;
223     RECTL vportRect = *rect;
224
225     LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
226
227     /* The coordinate systems may be mirrored
228        (LPtoDP handles points, not rectangles) */
229     if (vportRect.left > vportRect.right)
230     {
231         LONG temp = vportRect.right;
232         vportRect.right = vportRect.left;
233         vportRect.left = temp;
234     }
235     if (vportRect.top > vportRect.bottom)
236     {
237         LONG temp = vportRect.bottom;
238         vportRect.bottom = vportRect.top;
239         vportRect.top = temp;
240     }
241
242     if (bounds->left > bounds->right)
243     {
244         /* first bounding rectangle */
245         *bounds = vportRect;
246     }
247     else
248     {
249         bounds->left   = min(bounds->left,   vportRect.left);
250         bounds->top    = min(bounds->top,    vportRect.top);
251         bounds->right  = max(bounds->right,  vportRect.right);
252         bounds->bottom = max(bounds->bottom, vportRect.bottom);
253     }
254 }
255
256 /**********************************************************************
257  *          CreateEnhMetaFileA   (GDI32.@)
258  */
259 HDC WINAPI CreateEnhMetaFileA(
260     HDC hdc,           /* [in] optional reference DC */
261     LPCSTR filename,   /* [in] optional filename for disk metafiles */
262     const RECT *rect,  /* [in] optional bounding rectangle */
263     LPCSTR description /* [in] optional description */
264     )
265 {
266     LPWSTR filenameW = NULL;
267     LPWSTR descriptionW = NULL;
268     HDC hReturnDC;
269     DWORD len1, len2, total;
270
271     if(filename)
272     {
273         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
274         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
275         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
276     }
277     if(description) {
278         len1 = strlen(description);
279         len2 = strlen(description + len1 + 1);
280         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
281         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
282         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
283     }
284
285     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
286
287     HeapFree( GetProcessHeap(), 0, filenameW );
288     HeapFree( GetProcessHeap(), 0, descriptionW );
289
290     return hReturnDC;
291 }
292
293 /**********************************************************************
294  *          CreateEnhMetaFileW   (GDI32.@)
295  */
296 HDC WINAPI CreateEnhMetaFileW(
297     HDC           hdc,        /* [in] optional reference DC */
298     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
299     const RECT*   rect,       /* [in] optional bounding rectangle */
300     LPCWSTR       description /* [in] optional description */
301     )
302 {
303     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
304     HDC ret;
305     DC *dc;
306     HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL); 
307         /* If no ref, use current display */
308     EMFDRV_PDEVICE *physDev;
309     HANDLE hFile;
310     DWORD size = 0, length = 0;
311
312     TRACE("%s\n", debugstr_w(filename) );
313
314     if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
315
316     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
317     if (!physDev) {
318         free_dc_ptr( dc );
319         return 0;
320     }
321     if(description) { /* App name\0Title\0\0 */
322         length = lstrlenW(description);
323         length += lstrlenW(description + length + 1);
324         length += 3;
325         length *= 2;
326     }
327     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
328
329     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
330         HeapFree( GetProcessHeap(), 0, physDev );
331         free_dc_ptr( dc );
332         return 0;
333     }
334
335     push_dc_driver( dc, &physDev->dev, &EMFDRV_Funcs );
336
337     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
338     physDev->handles_size = HANDLE_LIST_INC;
339     physDev->cur_handles = 1;
340     physDev->hFile = 0;
341     physDev->dc_brush = 0;
342     physDev->dc_pen = 0;
343     physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
344     physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
345     physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
346     physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
347     physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
348     physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
349     physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
350     physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
351     physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
352     physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
353     physDev->planes = GetDeviceCaps(hRefDC, PLANES);
354     physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
355     physDev->restoring = 0;
356
357     SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
358
359     physDev->emh->iType = EMR_HEADER;
360     physDev->emh->nSize = size;
361
362     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
363     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
364
365     if(rect) {
366         physDev->emh->rclFrame.left   = rect->left;
367         physDev->emh->rclFrame.top    = rect->top;
368         physDev->emh->rclFrame.right  = rect->right;
369         physDev->emh->rclFrame.bottom = rect->bottom;
370     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
371         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
372         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
373     }
374
375     physDev->emh->dSignature = ENHMETA_SIGNATURE;
376     physDev->emh->nVersion = 0x10000;
377     physDev->emh->nBytes = physDev->emh->nSize;
378     physDev->emh->nRecords = 1;
379     physDev->emh->nHandles = 1;
380
381     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
382     physDev->emh->nDescription = length / 2;
383
384     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
385
386     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
387
388     /* Size in pixels */
389     physDev->emh->szlDevice.cx = physDev->horzres;
390     physDev->emh->szlDevice.cy = physDev->vertres;
391
392     /* Size in millimeters */
393     physDev->emh->szlMillimeters.cx = physDev->horzsize;
394     physDev->emh->szlMillimeters.cy = physDev->vertsize;
395
396     /* Size in micrometers */
397     physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
398     physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
399
400     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
401
402     if (filename)  /* disk based metafile */
403     {
404         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
405                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
406             free_dc_ptr( dc );
407             return 0;
408         }
409         if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
410             free_dc_ptr( dc );
411             return 0;
412         }
413         physDev->hFile = hFile;
414     }
415
416     TRACE("returning %p\n", physDev->dev.hdc);
417     ret = physDev->dev.hdc;
418     release_dc_ptr( dc );
419
420     if( !hdc )
421       DeleteDC( hRefDC );
422
423     return ret;
424 }
425
426 /******************************************************************
427  *             CloseEnhMetaFile (GDI32.@)
428  */
429 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
430 {
431     HENHMETAFILE hmf;
432     EMFDRV_PDEVICE *physDev;
433     DC *dc;
434     EMREOF emr;
435     HANDLE hMapping = 0;
436
437     TRACE("(%p)\n", hdc );
438
439     if (!(dc = get_dc_ptr( hdc ))) return NULL;
440     if (dc->header.type != OBJ_ENHMETADC)
441     {
442         release_dc_ptr( dc );
443         return NULL;
444     }
445     if (dc->refcount != 1)
446     {
447         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
448         release_dc_ptr( dc );
449         return NULL;
450     }
451     physDev = (EMFDRV_PDEVICE *)dc->physDev;
452
453     if(dc->saveLevel)
454         RestoreDC(hdc, 1);
455
456     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
457     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
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 }