Release 20030408.
[wine/gsoc-2012-control.git] / dlls / gdi / enhmfdrv / init.c
blobe91a8137e45de771c7e5c7ffd20e8531a29dff05
1 /*
2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
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.
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.
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
21 #include <string.h>
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "gdi.h"
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
31 static const DC_FUNCTIONS EMFDRV_Funcs =
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 EMFDRV_ExtTextOut, /* 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 EMFDRV_GetDeviceCaps, /* 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 */
147 /**********************************************************************
148 * EMFDRV_DeleteDC
150 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
152 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
153 DC *dc = physDev->dc;
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;
163 /******************************************************************
164 * EMFDRV_WriteRecord
166 * Warning: this function can change the pointer to the metafile header.
168 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
170 DWORD len;
171 ENHMETAHEADER *emh;
172 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
174 physDev->emh->nBytes += emr->nSize;
175 physDev->emh->nRecords++;
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);
189 return TRUE;
193 /******************************************************************
194 * EMFDRV_UpdateBBox
196 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
198 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
199 RECTL *bounds = &physDev->emh->rclBounds;
201 if(bounds->left > bounds->right) {/* first rect */
202 *bounds = *rect;
203 return;
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;
212 /******************************************************************
213 * EMFDRV_AddHandleDC
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.
219 int EMFDRV_AddHandleDC( PHYSDEV dev )
221 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
222 physDev->emh->nHandles++;
223 return physDev->nextHandle++;
227 /**********************************************************************
228 * CreateEnhMetaFileA (GDI32.@)
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 */
237 LPWSTR filenameW = NULL;
238 LPWSTR descriptionW = NULL;
239 HDC hReturnDC;
240 DWORD len1, len2, total;
242 if(filename)
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 );
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 );
256 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
258 if(filenameW)
259 HeapFree( GetProcessHeap(), 0, filenameW );
260 if(descriptionW)
261 HeapFree( GetProcessHeap(), 0, descriptionW );
263 return hReturnDC;
266 /**********************************************************************
267 * CreateEnhMetaFileW (GDI32.@)
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 */
276 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
277 HDC ret;
278 DC *dc;
279 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
280 /* If no ref, use current display */
281 EMFDRV_PDEVICE *physDev;
282 HANDLE hFile;
283 DWORD size = 0, length = 0;
285 TRACE("%s\n", debugstr_w(filename) );
287 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
288 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
290 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
291 if (!physDev) {
292 GDI_FreeObject( dc->hSelf, dc );
293 return 0;
295 dc->physDev = (PHYSDEV)physDev;
296 physDev->hdc = dc->hSelf;
297 physDev->dc = dc;
299 if(description) { /* App name\0Title\0\0 */
300 length = lstrlenW(description);
301 length += lstrlenW(description + length + 1);
302 length += 3;
303 length *= 2;
305 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
307 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
308 HeapFree( GetProcessHeap(), 0, physDev );
309 GDI_FreeObject( dc->hSelf, dc );
310 return 0;
313 physDev->nextHandle = 1;
314 physDev->hFile = 0;
316 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
317 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
318 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
319 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
320 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
321 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
322 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
324 physDev->emh->iType = EMR_HEADER;
325 physDev->emh->nSize = size;
327 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
328 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
330 if(rect) {
331 physDev->emh->rclFrame.left = rect->left;
332 physDev->emh->rclFrame.top = rect->top;
333 physDev->emh->rclFrame.right = rect->right;
334 physDev->emh->rclFrame.bottom = rect->bottom;
335 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
336 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
337 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
340 physDev->emh->dSignature = ENHMETA_SIGNATURE;
341 physDev->emh->nVersion = 0x10000;
342 physDev->emh->nBytes = physDev->emh->nSize;
343 physDev->emh->nRecords = 1;
344 physDev->emh->nHandles = 1;
346 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
347 physDev->emh->nDescription = length / 2;
349 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
351 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
353 /* Size in pixels */
354 physDev->emh->szlDevice.cx = physDev->horzres;
355 physDev->emh->szlDevice.cy = physDev->vertres;
357 /* Size in millimeters */
358 physDev->emh->szlMillimeters.cx = physDev->horzsize;
359 physDev->emh->szlMillimeters.cy = physDev->vertsize;
361 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
363 if (filename) /* disk based metafile */
365 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
366 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
367 EMFDRV_DeleteDC( dc->physDev );
368 return 0;
370 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
371 EMFDRV_DeleteDC( dc->physDev );
372 return 0;
374 physDev->hFile = hFile;
377 TRACE("returning %p\n", dc->hSelf);
378 ret = dc->hSelf;
379 GDI_ReleaseObj( dc->hSelf );
381 if( !hdc )
382 DeleteDC( hRefDC );
384 return ret;
387 /******************************************************************
388 * CloseEnhMetaFile (GDI32.@)
390 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
392 HENHMETAFILE hmf;
393 EMFDRV_PDEVICE *physDev;
394 DC *dc;
395 EMREOF emr;
396 HANDLE hMapping = 0;
398 TRACE("(%p)\n", hdc );
400 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
401 physDev = (EMFDRV_PDEVICE *)dc->physDev;
403 emr.emr.iType = EMR_EOF;
404 emr.emr.nSize = sizeof(emr);
405 emr.nPalEntries = 0;
406 emr.offPalEntries = 0;
407 emr.nSizeLast = emr.emr.nSize;
408 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
410 /* Update rclFrame if not initialized in CreateEnhMetaFile */
411 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
412 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
413 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
414 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
415 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
416 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
417 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
418 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
419 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
422 if (physDev->hFile) /* disk based metafile */
424 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
426 CloseHandle( physDev->hFile );
427 EMFDRV_DeleteDC( dc->physDev );
428 return 0;
431 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
432 sizeof(*physDev->emh), NULL, NULL))
434 CloseHandle( physDev->hFile );
435 EMFDRV_DeleteDC( dc->physDev );
436 return 0;
438 HeapFree( GetProcessHeap(), 0, physDev->emh );
439 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
440 0, NULL);
441 TRACE("hMapping = %p\n", hMapping );
442 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
443 TRACE("view = %p\n", physDev->emh );
444 CloseHandle( hMapping );
445 CloseHandle( physDev->hFile );
448 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
449 physDev->emh = NULL; /* So it won't be deleted */
450 EMFDRV_DeleteDC( dc->physDev );
451 return hmf;