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