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