mshtml: Rename call_event to fire_event.
[wine] / dlls / gdi32 / mfdrv / init.c
1 /*
2  * Metafile driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
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 <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
32
33 static BOOL MFDRV_DeleteDC( PHYSDEV dev );
34
35
36 /**********************************************************************
37  *           MFDRV_ExtEscape
38  */
39 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
40                             INT cbOutput, LPVOID out_data )
41 {
42     METARECORD *mr;
43     DWORD len;
44     INT ret;
45
46     if (cbOutput) return 0;  /* escapes that require output cannot work in metafiles */
47
48     len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
49     mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
50     mr->rdSize = len / 2;
51     mr->rdFunction = META_ESCAPE;
52     mr->rdParm[0] = nEscape;
53     mr->rdParm[1] = cbInput;
54     memcpy(&(mr->rdParm[2]), in_data, cbInput);
55     ret = MFDRV_WriteRecord( dev, mr, len);
56     HeapFree(GetProcessHeap(), 0, mr);
57     return ret;
58 }
59
60
61 /******************************************************************
62  *         MFDRV_GetDeviceCaps
63  *
64  *A very simple implementation that returns DT_METAFILE
65  */
66 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
67 {
68     switch(cap)
69     {
70     case TECHNOLOGY:
71         return DT_METAFILE;
72     case TEXTCAPS:
73         return 0;
74     default:
75         TRACE(" unsupported capability %d, will return 0\n", cap );
76     }
77     return 0;
78 }
79
80
81 static const DC_FUNCTIONS MFDRV_Funcs =
82 {
83     NULL,                            /* pAbortDoc */
84     MFDRV_AbortPath,                 /* pAbortPath */
85     NULL,                            /* pAlphaBlend */
86     NULL,                            /* pAngleArc */
87     MFDRV_Arc,                       /* pArc */
88     NULL,                            /* pArcTo */
89     MFDRV_BeginPath,                 /* pBeginPath */
90     NULL,                            /* pChoosePixelFormat */
91     MFDRV_Chord,                     /* pChord */
92     MFDRV_CloseFigure,               /* pCloseFigure */
93     NULL,                            /* pCreateBitmap */
94     NULL,                            /* pCreateDC */
95     NULL,                            /* pCreateDIBSection */
96     NULL,                            /* pDeleteBitmap */
97     MFDRV_DeleteDC,                  /* pDeleteDC */
98     MFDRV_DeleteObject,              /* pDeleteObject */
99     NULL,                            /* pDescribePixelFormat */
100     NULL,                            /* pDeviceCapabilities */
101     MFDRV_Ellipse,                   /* pEllipse */
102     NULL,                            /* pEndDoc */
103     NULL,                            /* pEndPage */
104     MFDRV_EndPath,                   /* pEndPath */
105     NULL,                            /* pEnumDeviceFonts */
106     NULL,                            /* pEnumICMProfiles */
107     MFDRV_ExcludeClipRect,           /* pExcludeClipRect */
108     NULL,                            /* pExtDeviceMode */
109     MFDRV_ExtEscape,                 /* pExtEscape */
110     MFDRV_ExtFloodFill,              /* pExtFloodFill */
111     MFDRV_ExtSelectClipRgn,          /* pExtSelectClipRgn */
112     MFDRV_ExtTextOut,                /* pExtTextOut */
113     MFDRV_FillPath,                  /* pFillPath */
114     MFDRV_FillRgn,                   /* pFillRgn */
115     MFDRV_FlattenPath,               /* pFlattenPath */
116     MFDRV_FrameRgn,                  /* pFrameRgn */
117     NULL,                            /* pGdiComment */
118     NULL,                            /* pGetCharWidth */
119     MFDRV_GetDeviceCaps,             /* pGetDeviceCaps */
120     NULL,                            /* pGetDeviceGammaRamp */
121     NULL,                            /* pGetICMProfile */
122     NULL,                            /* pGetImage */
123     NULL,                            /* pGetNearestColor */
124     NULL,                            /* pGetPixel */
125     NULL,                            /* pGetPixelFormat */
126     NULL,                            /* pGetSystemPaletteEntries */
127     NULL,                            /* pGetTextExtentExPoint */
128     NULL,                            /* pGetTextMetrics */
129     MFDRV_IntersectClipRect,         /* pIntersectClipRect */
130     MFDRV_InvertRgn,                 /* pInvertRgn */
131     MFDRV_LineTo,                    /* pLineTo */
132     NULL,                            /* pModifyWorldTransform */
133     MFDRV_MoveTo,                    /* pMoveTo */
134     MFDRV_OffsetClipRgn,             /* pOffsetClipRgn */
135     MFDRV_OffsetViewportOrgEx,       /* pOffsetViewportOrgEx */
136     MFDRV_OffsetWindowOrgEx,         /* pOffsetWindowOrgEx */
137     MFDRV_PaintRgn,                  /* pPaintRgn */
138     MFDRV_PatBlt,                    /* pPatBlt */
139     MFDRV_Pie,                       /* pPie */
140     MFDRV_PolyBezier,                /* pPolyBezier */
141     MFDRV_PolyBezierTo,              /* pPolyBezierTo */
142     NULL,                            /* pPolyDraw */
143     MFDRV_PolyPolygon,               /* pPolyPolygon */
144     NULL,                            /* pPolyPolyline */
145     MFDRV_Polygon,                   /* pPolygon */
146     MFDRV_Polyline,                  /* pPolyline */
147     NULL,                            /* pPolylineTo */
148     NULL,                            /* pPutImage */
149     NULL,                            /* pRealizeDefaultPalette */
150     MFDRV_RealizePalette,            /* pRealizePalette */
151     MFDRV_Rectangle,                 /* pRectangle */
152     NULL,                            /* pResetDC */
153     MFDRV_RestoreDC,                 /* pRestoreDC */
154     MFDRV_RoundRect,                 /* pRoundRect */
155     MFDRV_SaveDC,                    /* pSaveDC */
156     MFDRV_ScaleViewportExtEx,        /* pScaleViewportExtEx */
157     MFDRV_ScaleWindowExtEx,          /* pScaleWindowExtEx */
158     MFDRV_SelectBitmap,              /* pSelectBitmap */
159     MFDRV_SelectBrush,               /* pSelectBrush */
160     MFDRV_SelectClipPath,            /* pSelectClipPath */
161     MFDRV_SelectFont,                /* pSelectFont */
162     MFDRV_SelectPalette,             /* pSelectPalette */
163     MFDRV_SelectPen,                 /* pSelectPen */
164     NULL,                            /* pSetArcDirection */
165     NULL,                            /* pSetBitmapBits */
166     MFDRV_SetBkColor,                /* pSetBkColor */
167     MFDRV_SetBkMode,                 /* pSetBkMode */
168     MFDRV_SetDCBrushColor,           /* pSetDCBrushColor*/
169     MFDRV_SetDCPenColor,             /* pSetDCPenColor*/
170     NULL,                            /* pSetDIBColorTable */
171     MFDRV_SetDIBitsToDevice,         /* pSetDIBitsToDevice */
172     NULL,                            /* pSetDeviceClipping */
173     NULL,                            /* pSetDeviceGammaRamp */
174     NULL,                            /* pSetLayout */
175     MFDRV_SetMapMode,                /* pSetMapMode */
176     MFDRV_SetMapperFlags,            /* pSetMapperFlags */
177     MFDRV_SetPixel,                  /* pSetPixel */
178     NULL,                            /* pSetPixelFormat */
179     MFDRV_SetPolyFillMode,           /* pSetPolyFillMode */
180     MFDRV_SetROP2,                   /* pSetROP2 */
181     MFDRV_SetRelAbs,                 /* pSetRelAbs */
182     MFDRV_SetStretchBltMode,         /* pSetStretchBltMode */
183     MFDRV_SetTextAlign,              /* pSetTextAlign */
184     MFDRV_SetTextCharacterExtra,     /* pSetTextCharacterExtra */
185     MFDRV_SetTextColor,              /* pSetTextColor */
186     MFDRV_SetTextJustification,      /* pSetTextJustification */
187     MFDRV_SetViewportExtEx,          /* pSetViewportExtEx */
188     MFDRV_SetViewportOrgEx,          /* pSetViewportOrgEx */
189     MFDRV_SetWindowExtEx,            /* pSetWindowExtEx */
190     MFDRV_SetWindowOrgEx,            /* pSetWindowOrgEx */
191     NULL,                            /* pSetWorldTransform */
192     NULL,                            /* pStartDoc */
193     NULL,                            /* pStartPage */
194     MFDRV_StretchBlt,                /* pStretchBlt */
195     MFDRV_StretchDIBits,             /* pStretchDIBits */
196     MFDRV_StrokeAndFillPath,         /* pStrokeAndFillPath */
197     MFDRV_StrokePath,                /* pStrokePath */
198     NULL,                            /* pSwapBuffers */
199     NULL,                            /* pUnrealizePalette */
200     MFDRV_WidenPath                  /* pWidenPath */
201 };
202
203
204
205 /**********************************************************************
206  *           MFDRV_AllocMetaFile
207  */
208 static DC *MFDRV_AllocMetaFile(void)
209 {
210     DC *dc;
211     METAFILEDRV_PDEVICE *physDev;
212
213     if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
214
215     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
216     if (!physDev)
217     {
218         free_dc_ptr( dc );
219         return NULL;
220     }
221     if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
222     {
223         HeapFree( GetProcessHeap(), 0, physDev );
224         free_dc_ptr( dc );
225         return NULL;
226     }
227
228     push_dc_driver( dc, &physDev->dev, &MFDRV_Funcs );
229
230     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
231     physDev->handles_size = HANDLE_LIST_INC;
232     physDev->cur_handles = 0;
233
234     physDev->hFile = 0;
235
236     physDev->mh->mtHeaderSize   = sizeof(METAHEADER) / sizeof(WORD);
237     physDev->mh->mtVersion      = 0x0300;
238     physDev->mh->mtSize         = physDev->mh->mtHeaderSize;
239     physDev->mh->mtNoObjects    = 0;
240     physDev->mh->mtMaxRecord    = 0;
241     physDev->mh->mtNoParameters = 0;
242
243     SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
244
245     return dc;
246 }
247
248
249 /**********************************************************************
250  *           MFDRV_DeleteDC
251  */
252 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
253 {
254     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
255     DWORD index;
256
257     HeapFree( GetProcessHeap(), 0, physDev->mh );
258     for(index = 0; index < physDev->handles_size; index++)
259         if(physDev->handles[index])
260             GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
261     HeapFree( GetProcessHeap(), 0, physDev->handles );
262     HeapFree( GetProcessHeap(), 0, physDev );
263     return TRUE;
264 }
265
266
267 /**********************************************************************
268  *           CreateMetaFileW   (GDI32.@)
269  *
270  *  Create a new DC and associate it with a metafile. Pass a filename
271  *  to create a disk-based metafile, NULL to create a memory metafile.
272  *
273  * PARAMS
274  *  filename [I] Filename of disk metafile
275  *
276  * RETURNS
277  *  A handle to the metafile DC if successful, NULL on failure.
278  */
279 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
280 {
281     HDC ret;
282     DC *dc;
283     METAFILEDRV_PDEVICE *physDev;
284     HANDLE hFile;
285
286     TRACE("%s\n", debugstr_w(filename) );
287
288     if (!(dc = MFDRV_AllocMetaFile())) return 0;
289     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
290
291     if (filename)  /* disk based metafile */
292     {
293         physDev->mh->mtType = METAFILE_DISK;
294         if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
295                                 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
296             free_dc_ptr( dc );
297             return 0;
298         }
299         if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
300                         NULL )) {
301             free_dc_ptr( dc );
302             return 0;
303         }
304         physDev->hFile = hFile;
305
306         /* Grow METAHEADER to include filename */
307         physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
308     }
309     else  /* memory based metafile */
310         physDev->mh->mtType = METAFILE_MEMORY;
311
312     TRACE("returning %p\n", physDev->dev.hdc);
313     ret = physDev->dev.hdc;
314     release_dc_ptr( dc );
315     return ret;
316 }
317
318 /**********************************************************************
319  *          CreateMetaFileA   (GDI32.@)
320  *
321  * See CreateMetaFileW.
322  */
323 HDC WINAPI CreateMetaFileA(LPCSTR filename)
324 {
325     LPWSTR filenameW;
326     DWORD len;
327     HDC hReturnDC;
328
329     if (!filename) return CreateMetaFileW(NULL);
330
331     len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
332     filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
333     MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
334
335     hReturnDC = CreateMetaFileW(filenameW);
336
337     HeapFree( GetProcessHeap(), 0, filenameW );
338
339     return hReturnDC;
340 }
341
342
343 /**********************************************************************
344  *          MFDRV_CloseMetaFile
345  */
346 static DC *MFDRV_CloseMetaFile( HDC hdc )
347 {
348     DC *dc;
349     METAFILEDRV_PDEVICE *physDev;
350
351     TRACE("(%p)\n", hdc );
352
353     if (!(dc = get_dc_ptr( hdc ))) return NULL;
354     if (dc->header.type != OBJ_METADC)
355     {
356         release_dc_ptr( dc );
357         return NULL;
358     }
359     if (dc->refcount != 1)
360     {
361         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
362         release_dc_ptr( dc );
363         return NULL;
364     }
365     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
366
367     /* Construct the end of metafile record - this is documented
368      * in SDK Knowledgebase Q99334.
369      */
370
371     if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
372     {
373         free_dc_ptr( dc );
374         return 0;
375     }
376
377     if (physDev->mh->mtType == METAFILE_DISK)  /* disk based metafile */
378     {
379         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
380             free_dc_ptr( dc );
381             return 0;
382         }
383
384         physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
385         if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
386                        NULL, NULL)) {
387             free_dc_ptr( dc );
388             return 0;
389         }
390         CloseHandle(physDev->hFile);
391         physDev->mh->mtType = METAFILE_DISK;
392     }
393
394     return dc;
395 }
396
397 /******************************************************************
398  *           CloseMetaFile   (GDI32.@)
399  *
400  *  Stop recording graphics operations in metafile associated with
401  *  hdc and retrieve metafile.
402  *
403  * PARAMS
404  *  hdc [I] Metafile DC to close 
405  *
406  * RETURNS
407  *  Handle of newly created metafile on success, NULL on failure.
408  */
409 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
410 {
411     HMETAFILE hmf;
412     METAFILEDRV_PDEVICE *physDev;
413     DC *dc = MFDRV_CloseMetaFile(hdc);
414     if (!dc) return 0;
415     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
416
417     /* Now allocate a global handle for the metafile */
418
419     hmf = MF_Create_HMETAFILE( physDev->mh );
420
421     physDev->mh = NULL;  /* So it won't be deleted */
422     free_dc_ptr( dc );
423     return hmf;
424 }
425
426
427 /******************************************************************
428  *         MFDRV_WriteRecord
429  *
430  * Warning: this function can change the pointer to the metafile header.
431  */
432 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
433 {
434     DWORD len, size;
435     METAHEADER *mh;
436     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
437
438     switch(physDev->mh->mtType)
439     {
440     case METAFILE_MEMORY:
441         len = physDev->mh->mtSize * 2 + rlen;
442         /* reallocate memory if needed */
443         size = HeapSize( GetProcessHeap(), 0, physDev->mh );
444         if (len > size)
445         {
446             /*expand size*/
447             size += size / 2 + rlen;
448             mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
449             if (!mh) return FALSE;
450             physDev->mh = mh;
451             TRACE("Reallocated metafile: new size is %d\n",size);
452         }
453         memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
454         break;
455     case METAFILE_DISK:
456         TRACE("Writing record to disk\n");
457         if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
458             return FALSE;
459         break;
460     default:
461         ERR("Unknown metafile type %d\n", physDev->mh->mtType );
462         return FALSE;
463     }
464
465     physDev->mh->mtSize += rlen / 2;
466     physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
467     return TRUE;
468 }
469
470
471 /******************************************************************
472  *         MFDRV_MetaParam0
473  */
474
475 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
476 {
477     char buffer[8];
478     METARECORD *mr = (METARECORD *)&buffer;
479
480     mr->rdSize = 3;
481     mr->rdFunction = func;
482     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
483 }
484
485
486 /******************************************************************
487  *         MFDRV_MetaParam1
488  */
489 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
490 {
491     char buffer[8];
492     METARECORD *mr = (METARECORD *)&buffer;
493
494     mr->rdSize = 4;
495     mr->rdFunction = func;
496     *(mr->rdParm) = param1;
497     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
498 }
499
500
501 /******************************************************************
502  *         MFDRV_MetaParam2
503  */
504 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
505 {
506     char buffer[10];
507     METARECORD *mr = (METARECORD *)&buffer;
508
509     mr->rdSize = 5;
510     mr->rdFunction = func;
511     *(mr->rdParm) = param2;
512     *(mr->rdParm + 1) = param1;
513     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
514 }
515
516
517 /******************************************************************
518  *         MFDRV_MetaParam4
519  */
520
521 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
522                       short param3, short param4)
523 {
524     char buffer[14];
525     METARECORD *mr = (METARECORD *)&buffer;
526
527     mr->rdSize = 7;
528     mr->rdFunction = func;
529     *(mr->rdParm) = param4;
530     *(mr->rdParm + 1) = param3;
531     *(mr->rdParm + 2) = param2;
532     *(mr->rdParm + 3) = param1;
533     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
534 }
535
536
537 /******************************************************************
538  *         MFDRV_MetaParam6
539  */
540
541 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
542                       short param3, short param4, short param5, short param6)
543 {
544     char buffer[18];
545     METARECORD *mr = (METARECORD *)&buffer;
546
547     mr->rdSize = 9;
548     mr->rdFunction = func;
549     *(mr->rdParm) = param6;
550     *(mr->rdParm + 1) = param5;
551     *(mr->rdParm + 2) = param4;
552     *(mr->rdParm + 3) = param3;
553     *(mr->rdParm + 4) = param2;
554     *(mr->rdParm + 5) = param1;
555     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
556 }
557
558
559 /******************************************************************
560  *         MFDRV_MetaParam8
561  */
562 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
563                       short param3, short param4, short param5,
564                       short param6, short param7, short param8)
565 {
566     char buffer[22];
567     METARECORD *mr = (METARECORD *)&buffer;
568
569     mr->rdSize = 11;
570     mr->rdFunction = func;
571     *(mr->rdParm) = param8;
572     *(mr->rdParm + 1) = param7;
573     *(mr->rdParm + 2) = param6;
574     *(mr->rdParm + 3) = param5;
575     *(mr->rdParm + 4) = param4;
576     *(mr->rdParm + 5) = param3;
577     *(mr->rdParm + 6) = param2;
578     *(mr->rdParm + 7) = param1;
579     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
580 }