gdi32: Use the official constants instead of the old Win9x magic numbers for GDI...
[wine] / dlls / gdi32 / enhmfdrv / init.c
1 /*
2  * Enhanced MetaFile driver initialisation functions
3  *
4  * Copyright 1999 Huw D M Davies
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 <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "gdi_private.h"
29 #include "enhmfdrv/enhmetafiledrv.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33
34 static const DC_FUNCTIONS EMFDRV_Funcs =
35 {
36     NULL,                            /* pAbortDoc */
37     EMFDRV_AbortPath,                /* pAbortPath */
38     NULL,                            /* pAlphaBlend */
39     NULL,                            /* pAngleArc */
40     EMFDRV_Arc,                      /* pArc */
41     NULL,                            /* pArcTo */
42     EMFDRV_BeginPath,                /* pBeginPath */
43     EMFDRV_BitBlt,                   /* pBitBlt */
44     NULL,                            /* pChoosePixelFormat */
45     EMFDRV_Chord,                    /* pChord */
46     EMFDRV_CloseFigure,              /* pCloseFigure */
47     NULL,                            /* pCreateBitmap */
48     NULL,                            /* pCreateDC */
49     NULL,                            /* pCreateDIBSection */
50     NULL,                            /* pDeleteBitmap */
51     NULL,                            /* pDeleteDC */
52     EMFDRV_DeleteObject,             /* pDeleteObject */
53     NULL,                            /* pDescribePixelFormat */
54     NULL,                            /* pDeviceCapabilities */
55     EMFDRV_Ellipse,                  /* pEllipse */
56     NULL,                            /* pEndDoc */
57     NULL,                            /* pEndPage */
58     EMFDRV_EndPath,                  /* pEndPath */
59     NULL,                            /* pEnumDeviceFonts */
60     EMFDRV_ExcludeClipRect,          /* pExcludeClipRect */
61     NULL,                            /* pExtDeviceMode */
62     NULL,                            /* pExtEscape */
63     EMFDRV_ExtFloodFill,             /* pExtFloodFill */
64     EMFDRV_ExtSelectClipRgn,         /* pExtSelectClipRgn */
65     EMFDRV_ExtTextOut,               /* pExtTextOut */
66     EMFDRV_FillPath,                 /* pFillPath */
67     EMFDRV_FillRgn,                  /* pFillRgn */
68     EMFDRV_FlattenPath,              /* pFlattenPath */
69     EMFDRV_FrameRgn,                 /* pFrameRgn */
70     EMFDRV_GdiComment,               /* pGdiComment */
71     NULL,                            /* pGetBitmapBits */
72     NULL,                            /* pGetCharWidth */
73     NULL,                            /* pGetDCOrgEx */
74     NULL,                            /* pGetDIBColorTable */
75     NULL,                            /* pGetDIBits */
76     EMFDRV_GetDeviceCaps,            /* pGetDeviceCaps */
77     NULL,                            /* pGetDeviceGammaRamp */
78     NULL,                            /* pGetICMProfile */
79     NULL,                            /* pGetNearestColor */
80     NULL,                            /* pGetPixel */
81     NULL,                            /* pGetPixelFormat */
82     NULL,                            /* pGetSystemPaletteEntries */
83     NULL,                            /* pGetTextExtentExPoint */
84     NULL,                            /* pGetTextMetrics */
85     EMFDRV_IntersectClipRect,        /* pIntersectClipRect */
86     EMFDRV_InvertRgn,                /* pInvertRgn */
87     EMFDRV_LineTo,                   /* pLineTo */
88     EMFDRV_ModifyWorldTransform,     /* pModifyWorldTransform */
89     EMFDRV_MoveTo,                   /* pMoveTo */
90     EMFDRV_OffsetClipRgn,            /* pOffsetClipRgn */
91     EMFDRV_OffsetViewportOrg,        /* pOffsetViewportOrg */
92     EMFDRV_OffsetWindowOrg,          /* pOffsetWindowOrg */
93     EMFDRV_PaintRgn,                 /* pPaintRgn */
94     EMFDRV_PatBlt,                   /* pPatBlt */
95     EMFDRV_Pie,                      /* pPie */
96     NULL,                            /* pPolyBezier */
97     NULL,                            /* pPolyBezierTo */
98     NULL,                            /* pPolyDraw */
99     EMFDRV_PolyPolygon,              /* pPolyPolygon */
100     EMFDRV_PolyPolyline,             /* pPolyPolyline */
101     EMFDRV_Polygon,                  /* pPolygon */
102     EMFDRV_Polyline,                 /* pPolyline */
103     NULL,                            /* pPolylineTo */
104     NULL,                            /* pRealizeDefaultPalette */
105     NULL,                            /* pRealizePalette */
106     EMFDRV_Rectangle,                /* pRectangle */
107     NULL,                            /* pResetDC */
108     EMFDRV_RestoreDC,                /* pRestoreDC */
109     EMFDRV_RoundRect,                /* pRoundRect */
110     EMFDRV_SaveDC,                   /* pSaveDC */
111     EMFDRV_ScaleViewportExt,         /* pScaleViewportExt */
112     EMFDRV_ScaleWindowExt,           /* pScaleWindowExt */
113     EMFDRV_SelectBitmap,             /* pSelectBitmap */
114     EMFDRV_SelectBrush,              /* pSelectBrush */
115     EMFDRV_SelectClipPath,           /* pSelectClipPath */
116     EMFDRV_SelectFont,               /* pSelectFont */
117     EMFDRV_SelectPalette,            /* pSelectPalette */
118     EMFDRV_SelectPen,                /* pSelectPen */
119     EMFDRV_SetArcDirection,          /* pSetArcDirection */
120     NULL,                            /* pSetBitmapBits */
121     EMFDRV_SetBkColor,               /* pSetBkColor */
122     EMFDRV_SetBkMode,                /* pSetBkMode */
123     NULL,                            /* pSetDCBrushColor */
124     NULL,                            /* pSetDCOrg */
125     NULL,                            /* pSetDCPenColor */
126     NULL,                            /* pSetDIBColorTable */
127     NULL,                            /* pSetDIBits */
128     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
129     NULL,                            /* pSetDeviceClipping */
130     NULL,                            /* pSetDeviceGammaRamp */
131     EMFDRV_SetMapMode,               /* pSetMapMode */
132     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
133     EMFDRV_SetPixel,                 /* pSetPixel */
134     NULL,                            /* pSetPixelFormat */
135     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
136     EMFDRV_SetROP2,                  /* pSetROP2 */
137     NULL,                            /* pSetRelAbs */
138     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
139     EMFDRV_SetTextAlign,             /* pSetTextAlign */
140     NULL,                            /* pSetTextCharacterExtra */
141     EMFDRV_SetTextColor,             /* pSetTextColor */
142     EMFDRV_SetTextJustification,     /* pSetTextJustification */ 
143     EMFDRV_SetViewportExt,           /* pSetViewportExt */
144     EMFDRV_SetViewportOrg,           /* pSetViewportOrg */
145     EMFDRV_SetWindowExt,             /* pSetWindowExt */
146     EMFDRV_SetWindowOrg,             /* pSetWindowOrg */
147     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
148     NULL,                            /* pStartDoc */
149     NULL,                            /* pStartPage */
150     EMFDRV_StretchBlt,               /* pStretchBlt */
151     EMFDRV_StretchDIBits,            /* pStretchDIBits */
152     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
153     EMFDRV_StrokePath,               /* pStrokePath */
154     NULL,                            /* pSwapBuffers */
155     NULL,                            /* pUnrealizePalette */
156     EMFDRV_WidenPath                 /* pWidenPath */
157 };
158
159
160 /**********************************************************************
161  *           EMFDRV_DeleteDC
162  */
163 static BOOL EMFDRV_DeleteDC( DC *dc )
164 {
165     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
166     UINT index;
167
168     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
169     for(index = 0; index < physDev->handles_size; index++)
170         if(physDev->handles[index])
171             GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
172     HeapFree( GetProcessHeap(), 0, physDev->handles );
173     HeapFree( GetProcessHeap(), 0, physDev );
174     dc->physDev = NULL;
175     free_dc_ptr( dc );
176     return TRUE;
177 }
178
179
180 /******************************************************************
181  *         EMFDRV_WriteRecord
182  *
183  * Warning: this function can change the pointer to the metafile header.
184  */
185 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
186 {
187     DWORD len;
188     ENHMETAHEADER *emh;
189     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
190
191     TRACE("record %d, size %d %s\n",
192           emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
193
194     assert( !(emr->nSize & 3) );
195
196     physDev->emh->nBytes += emr->nSize;
197     physDev->emh->nRecords++;
198
199     if(physDev->hFile) {
200         if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
201             return FALSE;
202     } else {
203         DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
204         len = physDev->emh->nBytes;
205         if (len > nEmfSize) {
206             nEmfSize += (nEmfSize / 2) + emr->nSize;
207             emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
208             if (!emh) return FALSE;
209             physDev->emh = emh;
210         }
211         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
212                emr->nSize);
213     }
214     return TRUE;
215 }
216
217
218 /******************************************************************
219  *         EMFDRV_UpdateBBox
220  */
221 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
222 {
223     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
224     RECTL *bounds = &physDev->emh->rclBounds;
225     RECTL vportRect = *rect;
226
227     LPtoDP(physDev->hdc, (LPPOINT)&vportRect, 2);
228     
229     /* The coordinate systems may be mirrored
230        (LPtoDP handles points, not rectangles) */
231     if (vportRect.left > vportRect.right)
232     {
233         LONG temp = vportRect.right;
234         vportRect.right = vportRect.left;
235         vportRect.left = temp;
236     }
237     if (vportRect.top > vportRect.bottom)
238     {
239         LONG temp = vportRect.bottom;
240         vportRect.bottom = vportRect.top;
241         vportRect.top = temp;
242     }
243
244     if (bounds->left > bounds->right)
245     {
246         /* first bounding rectangle */
247         *bounds = vportRect;
248     }
249     else
250     {
251         bounds->left   = min(bounds->left,   vportRect.left);
252         bounds->top    = min(bounds->top,    vportRect.top);
253         bounds->right  = max(bounds->right,  vportRect.right);
254         bounds->bottom = max(bounds->bottom, vportRect.bottom);
255     }
256 }
257
258 /**********************************************************************
259  *          CreateEnhMetaFileA   (GDI32.@)
260  */
261 HDC WINAPI CreateEnhMetaFileA(
262     HDC hdc,           /* [in] optional reference DC */
263     LPCSTR filename,   /* [in] optional filename for disk metafiles */
264     const RECT *rect,  /* [in] optional bounding rectangle */
265     LPCSTR description /* [in] optional description */
266     )
267 {
268     LPWSTR filenameW = NULL;
269     LPWSTR descriptionW = NULL;
270     HDC hReturnDC;
271     DWORD len1, len2, total;
272
273     if(filename)
274     {
275         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
276         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
277         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
278     }
279     if(description) {
280         len1 = strlen(description);
281         len2 = strlen(description + len1 + 1);
282         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
283         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
284         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
285     }
286
287     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
288
289     if(filenameW)
290         HeapFree( GetProcessHeap(), 0, filenameW );
291     if(descriptionW)
292         HeapFree( GetProcessHeap(), 0, descriptionW );
293
294     return hReturnDC;
295 }
296
297 /**********************************************************************
298  *          CreateEnhMetaFileW   (GDI32.@)
299  */
300 HDC WINAPI CreateEnhMetaFileW(
301     HDC           hdc,        /* [in] optional reference DC */
302     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
303     const RECT*   rect,       /* [in] optional bounding rectangle */
304     LPCWSTR       description /* [in] optional description */
305     )
306 {
307     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
308     HDC ret;
309     DC *dc;
310     HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL); 
311         /* If no ref, use current display */
312     EMFDRV_PDEVICE *physDev;
313     HANDLE hFile;
314     DWORD size = 0, length = 0;
315
316     TRACE("%s\n", debugstr_w(filename) );
317
318     if (!(dc = alloc_dc_ptr( &EMFDRV_Funcs, OBJ_ENHMETADC ))) return 0;
319
320     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
321     if (!physDev) {
322         free_dc_ptr( dc );
323         return 0;
324     }
325     dc->physDev = (PHYSDEV)physDev;
326     physDev->hdc = dc->hSelf;
327
328     if(description) { /* App name\0Title\0\0 */
329         length = lstrlenW(description);
330         length += lstrlenW(description + length + 1);
331         length += 3;
332         length *= 2;
333     }
334     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
335
336     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
337         HeapFree( GetProcessHeap(), 0, physDev );
338         free_dc_ptr( dc );
339         return 0;
340     }
341
342     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
343     physDev->handles_size = HANDLE_LIST_INC;
344     physDev->cur_handles = 1;
345     physDev->hFile = 0;
346
347     physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
348     physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
349     physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
350     physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
351     physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
352     physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
353     physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
354     physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
355     physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
356     physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
357     physDev->planes = GetDeviceCaps(hRefDC, PLANES);
358     physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
359
360     physDev->emh->iType = EMR_HEADER;
361     physDev->emh->nSize = size;
362
363     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
364     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
365
366     if(rect) {
367         physDev->emh->rclFrame.left   = rect->left;
368         physDev->emh->rclFrame.top    = rect->top;
369         physDev->emh->rclFrame.right  = rect->right;
370         physDev->emh->rclFrame.bottom = rect->bottom;
371     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
372         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
373         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
374     }
375
376     physDev->emh->dSignature = ENHMETA_SIGNATURE;
377     physDev->emh->nVersion = 0x10000;
378     physDev->emh->nBytes = physDev->emh->nSize;
379     physDev->emh->nRecords = 1;
380     physDev->emh->nHandles = 1;
381
382     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
383     physDev->emh->nDescription = length / 2;
384
385     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
386
387     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
388
389     /* Size in pixels */
390     physDev->emh->szlDevice.cx = physDev->horzres;
391     physDev->emh->szlDevice.cy = physDev->vertres;
392
393     /* Size in millimeters */
394     physDev->emh->szlMillimeters.cx = physDev->horzsize;
395     physDev->emh->szlMillimeters.cy = physDev->vertsize;
396
397     /* Size in micrometers */
398     physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
399     physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
400
401     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
402
403     if (filename)  /* disk based metafile */
404     {
405         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
406                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
407             EMFDRV_DeleteDC( dc );
408             return 0;
409         }
410         if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
411             EMFDRV_DeleteDC( dc );
412             return 0;
413         }
414         physDev->hFile = hFile;
415     }
416
417     TRACE("returning %p\n", dc->hSelf);
418     ret = dc->hSelf;
419     release_dc_ptr( dc );
420
421     if( !hdc )
422       DeleteDC( hRefDC );
423
424     return ret;
425 }
426
427 /******************************************************************
428  *             CloseEnhMetaFile (GDI32.@)
429  */
430 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
431 {
432     HENHMETAFILE hmf;
433     EMFDRV_PDEVICE *physDev;
434     DC *dc;
435     EMREOF emr;
436     HANDLE hMapping = 0;
437
438     TRACE("(%p)\n", hdc );
439
440     if (!(dc = get_dc_ptr( hdc ))) return NULL;
441     if (dc->header.type != OBJ_ENHMETADC)
442     {
443         release_dc_ptr( dc );
444         return NULL;
445     }
446     if (dc->refcount != 1)
447     {
448         FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
449         release_dc_ptr( dc );
450         return NULL;
451     }
452     physDev = (EMFDRV_PDEVICE *)dc->physDev;
453
454     if(dc->saveLevel)
455         RestoreDC(hdc, 1);
456
457     emr.emr.iType = EMR_EOF;
458     emr.emr.nSize = sizeof(emr);
459     emr.nPalEntries = 0;
460     emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
461     emr.nSizeLast = emr.emr.nSize;
462     EMFDRV_WriteRecord( dc->physDev, &emr.emr );
463
464     /* Update rclFrame if not initialized in CreateEnhMetaFile */
465     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
466         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
467           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
468         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
469           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
470         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
471           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
472         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
473           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
474     }
475
476     if (physDev->hFile)  /* disk based metafile */
477     {
478         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
479         {
480             CloseHandle( physDev->hFile );
481             EMFDRV_DeleteDC( dc );
482             return 0;
483         }
484
485         if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
486                        NULL, NULL))
487         {
488             CloseHandle( physDev->hFile );
489             EMFDRV_DeleteDC( dc );
490             return 0;
491         }
492         HeapFree( GetProcessHeap(), 0, physDev->emh );
493         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
494                                       0, NULL);
495         TRACE("hMapping = %p\n", hMapping );
496         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
497         TRACE("view = %p\n", physDev->emh );
498         CloseHandle( hMapping );
499         CloseHandle( physDev->hFile );
500     }
501
502     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
503     physDev->emh = NULL;  /* So it won't be deleted */
504     EMFDRV_DeleteDC( dc );
505     return hmf;
506 }