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