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