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