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