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