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