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