If using the default values, also set dwType to REG_SZ as our default
[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     EMFDRV_BitBlt,                   /* 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     EMFDRV_OffsetViewportOrg,        /* pOffsetViewportOrg */
88     EMFDRV_OffsetWindowOrg,          /* 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,                            /* pSetDCBrushColor */
120     NULL,                            /* pSetDCOrg */
121     NULL,                            /* pSetDCPenColor */
122     NULL,                            /* pSetDIBColorTable */
123     NULL,                            /* pSetDIBits */
124     EMFDRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
125     NULL,                            /* pSetDeviceClipping */
126     NULL,                            /* pSetDeviceGammaRamp */
127     EMFDRV_SetMapMode,               /* pSetMapMode */
128     EMFDRV_SetMapperFlags,           /* pSetMapperFlags */
129     NULL,                            /* pSetPixel */
130     NULL,                            /* pSetPixelFormat */
131     EMFDRV_SetPolyFillMode,          /* pSetPolyFillMode */
132     EMFDRV_SetROP2,                  /* pSetROP2 */
133     NULL,                            /* pSetRelAbs */
134     EMFDRV_SetStretchBltMode,        /* pSetStretchBltMode */
135     EMFDRV_SetTextAlign,             /* pSetTextAlign */
136     NULL,                            /* pSetTextCharacterExtra */
137     EMFDRV_SetTextColor,             /* pSetTextColor */
138     EMFDRV_SetTextJustification,     /* pSetTextJustification */ 
139     EMFDRV_SetViewportExt,           /* pSetViewportExt */
140     EMFDRV_SetViewportOrg,           /* pSetViewportOrg */
141     EMFDRV_SetWindowExt,             /* pSetWindowExt */
142     EMFDRV_SetWindowOrg,             /* pSetWindowOrg */
143     EMFDRV_SetWorldTransform,        /* pSetWorldTransform */
144     NULL,                            /* pStartDoc */
145     NULL,                            /* pStartPage */
146     EMFDRV_StretchBlt,               /* pStretchBlt */
147     EMFDRV_StretchDIBits,            /* pStretchDIBits */
148     EMFDRV_StrokeAndFillPath,        /* pStrokeAndFillPath */
149     EMFDRV_StrokePath,               /* pStrokePath */
150     NULL,                            /* pSwapBuffers */
151     EMFDRV_WidenPath                 /* pWidenPath */
152 };
153
154
155 /**********************************************************************
156  *           EMFDRV_DeleteDC
157  */
158 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
159 {
160     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
161     DC *dc = physDev->dc;
162
163     if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
164     HeapFree( GetProcessHeap(), 0, physDev );
165     dc->physDev = NULL;
166     GDI_FreeObject( dc->hSelf, dc );
167     return TRUE;
168 }
169
170
171 /******************************************************************
172  *         EMFDRV_WriteRecord
173  *
174  * Warning: this function can change the pointer to the metafile header.
175  */
176 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
177 {
178     DWORD len;
179     ENHMETAHEADER *emh;
180     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
181
182     physDev->emh->nBytes += emr->nSize;
183     physDev->emh->nRecords++;
184
185     if(physDev->hFile) {
186         TRACE("Writing record to disk\n");
187         if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
188             return FALSE;
189     } else {
190         DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
191         len = physDev->emh->nBytes;
192         if (len > nEmfSize) {
193             nEmfSize += (nEmfSize / 2) + emr->nSize;
194             emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
195             if (!emh) return FALSE;
196             physDev->emh = emh;
197         }
198         memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
199                emr->nSize);
200     }
201     return TRUE;
202 }
203
204
205 /******************************************************************
206  *         EMFDRV_UpdateBBox
207  */
208 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
209 {
210     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
211     RECTL *bounds = &physDev->emh->rclBounds;
212     RECTL vportRect = *rect;
213
214     LPtoDP(physDev->hdc, (LPPOINT)&vportRect, 2);
215
216     if(bounds->left > bounds->right) {/* first rect */
217         *bounds = vportRect;
218         return;
219     }
220     bounds->left   = min(bounds->left,   vportRect.left);
221     bounds->top    = min(bounds->top,    vportRect.top);
222     bounds->right  = max(bounds->right,  vportRect.right);
223     bounds->bottom = max(bounds->bottom, vportRect.bottom);
224     return;
225 }
226
227 /******************************************************************
228  *         EMFDRV_AddHandleDC
229  *
230  * Note: this function assumes that we never delete objects.
231  * If we do someday, we'll need to maintain a table to re-use deleted
232  * handles.
233  */
234 int EMFDRV_AddHandleDC( PHYSDEV dev )
235 {
236     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
237     physDev->emh->nHandles++;
238     return physDev->nextHandle++;
239 }
240
241
242 /**********************************************************************
243  *          CreateEnhMetaFileA   (GDI32.@)
244  */
245 HDC WINAPI CreateEnhMetaFileA(
246     HDC hdc,           /* [in] optional reference DC */
247     LPCSTR filename,   /* [in] optional filename for disk metafiles */
248     const RECT *rect,  /* [in] optional bounding rectangle */
249     LPCSTR description /* [in] optional description */
250     )
251 {
252     LPWSTR filenameW = NULL;
253     LPWSTR descriptionW = NULL;
254     HDC hReturnDC;
255     DWORD len1, len2, total;
256
257     if(filename)
258     {
259         total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
260         filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
261         MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
262     }
263     if(description) {
264         len1 = strlen(description);
265         len2 = strlen(description + len1 + 1);
266         total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
267         descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
268         MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
269     }
270
271     hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
272
273     if(filenameW)
274         HeapFree( GetProcessHeap(), 0, filenameW );
275     if(descriptionW)
276         HeapFree( GetProcessHeap(), 0, descriptionW );
277
278     return hReturnDC;
279 }
280
281 /**********************************************************************
282  *          CreateEnhMetaFileW   (GDI32.@)
283  */
284 HDC WINAPI CreateEnhMetaFileW(
285     HDC           hdc,        /* [in] optional reference DC */
286     LPCWSTR       filename,   /* [in] optional filename for disk metafiles */
287     const RECT*   rect,       /* [in] optional bounding rectangle */
288     LPCWSTR       description /* [in] optional description */
289     )
290 {
291     static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
292     HDC ret;
293     DC *dc;
294     HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL); 
295         /* If no ref, use current display */
296     EMFDRV_PDEVICE *physDev;
297     HANDLE hFile;
298     DWORD size = 0, length = 0;
299
300     TRACE("%s\n", debugstr_w(filename) );
301
302     if (!(dc = DC_AllocDC( &EMFDRV_Funcs, ENHMETAFILE_DC_MAGIC ))) return 0;
303
304     physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
305     if (!physDev) {
306         GDI_FreeObject( dc->hSelf, dc );
307         return 0;
308     }
309     dc->physDev = (PHYSDEV)physDev;
310     physDev->hdc = dc->hSelf;
311     physDev->dc = dc;
312
313     if(description) { /* App name\0Title\0\0 */
314         length = lstrlenW(description);
315         length += lstrlenW(description + length + 1);
316         length += 3;
317         length *= 2;
318     }
319     size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
320
321     if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
322         HeapFree( GetProcessHeap(), 0, physDev );
323         GDI_FreeObject( dc->hSelf, dc );
324         return 0;
325     }
326
327     physDev->nextHandle = 1;
328     physDev->hFile = 0;
329
330     physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
331     physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
332     physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
333     physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
334     physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
335     physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
336     physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
337     physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
338     physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
339     physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
340     physDev->planes = GetDeviceCaps(hRefDC, PLANES);
341
342     physDev->emh->iType = EMR_HEADER;
343     physDev->emh->nSize = size;
344
345     physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
346     physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
347
348     if(rect) {
349         physDev->emh->rclFrame.left   = rect->left;
350         physDev->emh->rclFrame.top    = rect->top;
351         physDev->emh->rclFrame.right  = rect->right;
352         physDev->emh->rclFrame.bottom = rect->bottom;
353     } else {  /* Set this to {0,0 - -1,-1} and update it at the end */
354         physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
355         physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
356     }
357
358     physDev->emh->dSignature = ENHMETA_SIGNATURE;
359     physDev->emh->nVersion = 0x10000;
360     physDev->emh->nBytes = physDev->emh->nSize;
361     physDev->emh->nRecords = 1;
362     physDev->emh->nHandles = 1;
363
364     physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
365     physDev->emh->nDescription = length / 2;
366
367     physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
368
369     physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
370
371     /* Size in pixels */
372     physDev->emh->szlDevice.cx = physDev->horzres;
373     physDev->emh->szlDevice.cy = physDev->vertres;
374
375     /* Size in millimeters */
376     physDev->emh->szlMillimeters.cx = physDev->horzsize;
377     physDev->emh->szlMillimeters.cy = physDev->vertsize;
378
379     /* Size in micrometers */
380     physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
381     physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
382
383     memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
384
385     if (filename)  /* disk based metafile */
386     {
387         if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
388                                  NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
389             EMFDRV_DeleteDC( dc->physDev );
390             return 0;
391         }
392         if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
393             EMFDRV_DeleteDC( dc->physDev );
394             return 0;
395         }
396         physDev->hFile = hFile;
397     }
398
399     TRACE("returning %p\n", dc->hSelf);
400     ret = dc->hSelf;
401     GDI_ReleaseObj( dc->hSelf );
402
403     if( !hdc )
404       DeleteDC( hRefDC );
405
406     return ret;
407 }
408
409 /******************************************************************
410  *             CloseEnhMetaFile (GDI32.@)
411  */
412 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
413 {
414     HENHMETAFILE hmf;
415     EMFDRV_PDEVICE *physDev;
416     DC *dc;
417     EMREOF emr;
418     HANDLE hMapping = 0;
419
420     TRACE("(%p)\n", hdc );
421
422     if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
423     physDev = (EMFDRV_PDEVICE *)dc->physDev;
424
425     emr.emr.iType = EMR_EOF;
426     emr.emr.nSize = sizeof(emr);
427     emr.nPalEntries = 0;
428     emr.offPalEntries = 0;
429     emr.nSizeLast = emr.emr.nSize;
430     EMFDRV_WriteRecord( dc->physDev, &emr.emr );
431
432     /* Update rclFrame if not initialized in CreateEnhMetaFile */
433     if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
434         physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
435           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
436         physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
437           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
438         physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
439           physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
440         physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
441           physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
442     }
443
444     if (physDev->hFile)  /* disk based metafile */
445     {
446         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
447         {
448             CloseHandle( physDev->hFile );
449             EMFDRV_DeleteDC( dc->physDev );
450             return 0;
451         }
452
453         if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
454                        sizeof(*physDev->emh), NULL, NULL))
455         {
456             CloseHandle( physDev->hFile );
457             EMFDRV_DeleteDC( dc->physDev );
458             return 0;
459         }
460         HeapFree( GetProcessHeap(), 0, physDev->emh );
461         hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
462                                       0, NULL);
463         TRACE("hMapping = %p\n", hMapping );
464         physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
465         TRACE("view = %p\n", physDev->emh );
466         CloseHandle( hMapping );
467         CloseHandle( physDev->hFile );
468     }
469
470     hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
471     physDev->emh = NULL;  /* So it won't be deleted */
472     EMFDRV_DeleteDC( dc->physDev );
473     return hmf;
474 }