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