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