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