Implemented StretchDIBits in the EMF code.
[wine/dcerpc.git] / dlls / gdi / enhmfdrv / init.c
blob30963affb7a4d9f4facf87d9280211038581d817
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 EMFDRV_StretchDIBits, /* pStretchDIBits */
140 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
141 EMFDRV_StrokePath, /* pStrokePath */
142 NULL, /* pSwapBuffers */
143 EMFDRV_WidenPath /* pWidenPath */
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, ENHMETAFILE_DC_MAGIC ))) return 0;
289 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
290 if (!physDev) {
291 GDI_FreeObject( dc->hSelf, dc );
292 return 0;
294 dc->physDev = (PHYSDEV)physDev;
295 physDev->hdc = dc->hSelf;
296 physDev->dc = dc;
298 if(description) { /* App name\0Title\0\0 */
299 length = lstrlenW(description);
300 length += lstrlenW(description + length + 1);
301 length += 3;
302 length *= 2;
304 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
306 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
307 HeapFree( GetProcessHeap(), 0, physDev );
308 GDI_FreeObject( dc->hSelf, dc );
309 return 0;
312 physDev->nextHandle = 1;
313 physDev->hFile = 0;
315 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
316 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
317 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
318 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
319 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
320 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
321 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
323 physDev->emh->iType = EMR_HEADER;
324 physDev->emh->nSize = size;
326 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
327 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
329 if(rect) {
330 physDev->emh->rclFrame.left = rect->left;
331 physDev->emh->rclFrame.top = rect->top;
332 physDev->emh->rclFrame.right = rect->right;
333 physDev->emh->rclFrame.bottom = rect->bottom;
334 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
335 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
336 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
339 physDev->emh->dSignature = ENHMETA_SIGNATURE;
340 physDev->emh->nVersion = 0x10000;
341 physDev->emh->nBytes = physDev->emh->nSize;
342 physDev->emh->nRecords = 1;
343 physDev->emh->nHandles = 1;
345 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
346 physDev->emh->nDescription = length / 2;
348 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
350 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
352 /* Size in pixels */
353 physDev->emh->szlDevice.cx = physDev->horzres;
354 physDev->emh->szlDevice.cy = physDev->vertres;
356 /* Size in millimeters */
357 physDev->emh->szlMillimeters.cx = physDev->horzsize;
358 physDev->emh->szlMillimeters.cy = physDev->vertsize;
360 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
362 if (filename) /* disk based metafile */
364 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
365 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
366 EMFDRV_DeleteDC( dc->physDev );
367 return 0;
369 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
370 EMFDRV_DeleteDC( dc->physDev );
371 return 0;
373 physDev->hFile = hFile;
376 TRACE("returning %p\n", dc->hSelf);
377 ret = dc->hSelf;
378 GDI_ReleaseObj( dc->hSelf );
380 if( !hdc )
381 DeleteDC( hRefDC );
383 return ret;
386 /******************************************************************
387 * CloseEnhMetaFile (GDI32.@)
389 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
391 HENHMETAFILE hmf;
392 EMFDRV_PDEVICE *physDev;
393 DC *dc;
394 EMREOF emr;
395 HANDLE hMapping = 0;
397 TRACE("(%p)\n", hdc );
399 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
400 physDev = (EMFDRV_PDEVICE *)dc->physDev;
402 emr.emr.iType = EMR_EOF;
403 emr.emr.nSize = sizeof(emr);
404 emr.nPalEntries = 0;
405 emr.offPalEntries = 0;
406 emr.nSizeLast = emr.emr.nSize;
407 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
409 /* Update rclFrame if not initialized in CreateEnhMetaFile */
410 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
411 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
412 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
413 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
414 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
415 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
416 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
417 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
418 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
421 if (physDev->hFile) /* disk based metafile */
423 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
425 CloseHandle( physDev->hFile );
426 EMFDRV_DeleteDC( dc->physDev );
427 return 0;
430 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
431 sizeof(*physDev->emh), NULL, NULL))
433 CloseHandle( physDev->hFile );
434 EMFDRV_DeleteDC( dc->physDev );
435 return 0;
437 HeapFree( GetProcessHeap(), 0, physDev->emh );
438 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
439 0, NULL);
440 TRACE("hMapping = %p\n", hMapping );
441 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
442 TRACE("view = %p\n", physDev->emh );
443 CloseHandle( hMapping );
444 CloseHandle( physDev->hFile );
447 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
448 physDev->emh = NULL; /* So it won't be deleted */
449 EMFDRV_DeleteDC( dc->physDev );
450 return hmf;