Fixed WM_GETTEXTLENGTH handling.
[wine/multimedia.git] / graphics / enhmetafiledrv / init.c
blobbc5777c477a1f41b77c4bc4e378f59d1f4f42401
1 /*
2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
5 */
7 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "gdi.h"
12 #include "heap.h"
13 #include "enhmetafile.h"
14 #include "enhmetafiledrv.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(enhmetafile);
19 static const DC_FUNCTIONS EMFDRV_Funcs =
21 NULL, /* pAbortDoc */
22 EMFDRV_AbortPath, /* pAbortPath */
23 NULL, /* pAngleArc */
24 EMFDRV_Arc, /* pArc */
25 NULL, /* pArcTo */
26 EMFDRV_BeginPath, /* pBeginPath */
27 NULL, /* pBitBlt */
28 NULL, /* pBitmapBits */
29 NULL, /* pChoosePixelFormat */
30 EMFDRV_Chord, /* pChord */
31 EMFDRV_CloseFigure, /* pCloseFigure */
32 NULL, /* pCreateBitmap */
33 NULL, /* no implementation */ /* pCreateDC */
34 NULL, /* pCreateDIBSection */
35 NULL, /* pCreateDIBSection16 */
36 NULL, /* no implementation */ /* pDeleteDC */
37 NULL, /* pDeleteObject */
38 NULL, /* pDescribePixelFormat */
39 NULL, /* pDeviceCapabilities */
40 EMFDRV_Ellipse, /* pEllipse */
41 NULL, /* pEndDoc */
42 NULL, /* pEndPage */
43 EMFDRV_EndPath, /* pEndPath */
44 NULL, /* pEnumDeviceFonts */
45 NULL, /* pEscape */
46 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
47 NULL, /* pExtDeviceMode */
48 EMFDRV_ExtFloodFill, /* pExtFloodFill */
49 NULL, /* pExtTextOut */
50 EMFDRV_FillPath, /* pFillPath */
51 EMFDRV_FillRgn, /* pFillRgn */
52 EMFDRV_FlattenPath, /* pFlattenPath */
53 EMFDRV_FrameRgn, /* pFrameRgn */
54 NULL, /* pGetCharWidth */
55 NULL, /* pGetDCOrgEx */
56 NULL, /* no implementation */ /* pGetPixel */
57 NULL, /* pGetPixelFormat */
58 NULL, /* pGetTextExtentPoint */
59 NULL, /* pGetTextMetrics */
60 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
61 EMFDRV_InvertRgn, /* pInvertRgn */
62 EMFDRV_LineTo, /* pLineTo */
63 EMFDRV_MoveTo, /* pMoveTo */
64 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
65 NULL, /* pOffsetViewportOrg */
66 NULL, /* pOffsetWindowOrg */
67 EMFDRV_PaintRgn, /* pPaintRgn */
68 NULL, /* pPatBlt */
69 EMFDRV_Pie, /* pPie */
70 NULL, /* pPolyBezier */
71 NULL, /* pPolyBezierTo */
72 NULL, /* pPolyDraw */
73 EMFDRV_PolyPolygon, /* pPolyPolygon */
74 EMFDRV_PolyPolyline, /* pPolyPolyline */
75 EMFDRV_Polygon, /* pPolygon */
76 EMFDRV_Polyline, /* pPolyline */
77 NULL, /* pPolylineTo */
78 NULL, /* pRealizePalette */
79 EMFDRV_Rectangle, /* pRectangle */
80 EMFDRV_RestoreDC, /* pRestoreDC */
81 EMFDRV_RoundRect, /* pRoundRect */
82 EMFDRV_SaveDC, /* pSaveDC */
83 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
84 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
85 EMFDRV_SelectClipPath, /* pSelectClipPath */
86 NULL, /* pSelectClipRgn */
87 EMFDRV_SelectObject, /* pSelectObject */
88 NULL, /* pSelectPalette */
89 EMFDRV_SetBkColor, /* pSetBkColor */
90 EMFDRV_SetBkMode, /* pSetBkMode */
91 NULL, /* pSetDeviceClipping */
92 NULL, /* pSetDIBitsToDevice */
93 EMFDRV_SetMapMode, /* pSetMapMode */
94 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
95 NULL, /* pSetPixel */
96 NULL, /* pSetPixelFormat */
97 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
98 EMFDRV_SetROP2, /* pSetROP2 */
99 NULL, /* pSetRelAbs */
100 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
101 EMFDRV_SetTextAlign, /* pSetTextAlign */
102 NULL, /* pSetTextCharacterExtra */
103 EMFDRV_SetTextColor, /* pSetTextColor */
104 NULL, /* pSetTextJustification */
105 EMFDRV_SetViewportExt, /* pSetViewportExt */
106 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
107 EMFDRV_SetWindowExt, /* pSetWindowExt */
108 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
109 NULL, /* pStartDoc */
110 NULL, /* pStartPage */
111 NULL, /* pStretchBlt */
112 NULL, /* pStretchDIBits */
113 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
114 EMFDRV_StrokePath, /* pStrokePath */
115 NULL, /* pSwapBuffers */
116 EMFDRV_WidenPath /* pWiddenPath */
120 /**********************************************************************
121 * EMFDRV_DeleteDC
123 static BOOL EMFDRV_DeleteDC( DC *dc )
125 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
127 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
128 HeapFree( GetProcessHeap(), 0, physDev );
129 dc->physDev = NULL;
130 GDI_FreeObject( dc->hSelf, dc );
131 return TRUE;
135 /******************************************************************
136 * EMFDRV_WriteRecord
138 * Warning: this function can change the pointer to the metafile header.
140 BOOL EMFDRV_WriteRecord( DC *dc, EMR *emr )
142 DWORD len;
143 ENHMETAHEADER *emh;
144 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
146 physDev->emh->nBytes += emr->nSize;
147 physDev->emh->nRecords++;
149 if(physDev->hFile) {
150 TRACE("Writing record to disk\n");
151 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
152 return FALSE;
153 } else {
154 len = physDev->emh->nBytes;
155 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
156 if (!emh) return FALSE;
157 physDev->emh = emh;
158 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
159 emr->nSize);
161 return TRUE;
165 /******************************************************************
166 * EMFDRV_UpdateBBox
168 void EMFDRV_UpdateBBox( DC *dc, RECTL *rect )
170 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
171 RECTL *bounds = &physDev->emh->rclBounds;
173 if(bounds->left > bounds->right) {/* first rect */
174 *bounds = *rect;
175 return;
177 bounds->left = min(bounds->left, rect->left);
178 bounds->top = min(bounds->top, rect->top);
179 bounds->right = max(bounds->right, rect->right);
180 bounds->bottom = max(bounds->bottom, rect->bottom);
181 return;
184 /******************************************************************
185 * EMFDRV_AddHandleDC
187 * Note: this function assumes that we never delete objects.
188 * If we do someday, we'll need to maintain a table to re-use deleted
189 * handles.
191 int EMFDRV_AddHandleDC( DC *dc )
193 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dc->physDev;
194 physDev->emh->nHandles++;
195 return physDev->nextHandle++;
199 /**********************************************************************
200 * CreateEnhMetaFileA (GDI32.41)
202 HDC WINAPI CreateEnhMetaFileA(
203 HDC hdc, /* [in] optional reference DC */
204 LPCSTR filename, /* [in] optional filename for disk metafiles */
205 const RECT *rect, /* [in] optional bounding rectangle */
206 LPCSTR description /* [in] optional description */
209 LPWSTR filenameW = NULL;
210 LPWSTR descriptionW = NULL;
211 HDC hReturnDC;
212 DWORD len1, len2, total;
214 if(filename)
215 filenameW = HEAP_strdupAtoW( GetProcessHeap(), 0, filename );
217 if(description) {
218 len1 = strlen(description);
219 len2 = strlen(description + len1 + 1);
220 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
221 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
222 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
225 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
227 if(filenameW)
228 HeapFree( GetProcessHeap(), 0, filenameW );
229 if(descriptionW)
230 HeapFree( GetProcessHeap(), 0, descriptionW );
232 return hReturnDC;
235 /**********************************************************************
236 * CreateEnhMetaFileW (GDI32.42)
238 HDC WINAPI CreateEnhMetaFileW(
239 HDC hdc, /* [in] optional reference DC */
240 LPCWSTR filename, /* [in] optional filename for disk metafiles */
241 const RECT* rect, /* [in] optional bounding rectangle */
242 LPCWSTR description /* [in] optional description */
245 HDC ret;
246 DC *dc;
247 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
248 EMFDRV_PDEVICE *physDev;
249 HFILE hFile;
250 DWORD size = 0, length = 0;
252 TRACE("'%s'\n", debugstr_w(filename) );
254 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
255 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
257 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
258 if (!physDev) {
259 GDI_FreeObject( dc->hSelf, dc );
260 return 0;
262 dc->physDev = physDev;
264 if(description) { /* App name\0Title\0\0 */
265 length = lstrlenW(description);
266 length += lstrlenW(description + length + 1);
267 length += 3;
268 length *= 2;
270 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
272 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
273 HeapFree( GetProcessHeap(), 0, physDev );
274 GDI_FreeObject( dc->hSelf, dc );
275 return 0;
278 physDev->nextHandle = 1;
279 physDev->hFile = 0;
281 physDev->emh->iType = EMR_HEADER;
282 physDev->emh->nSize = size;
284 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
285 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
287 if(rect) {
288 physDev->emh->rclFrame.left = rect->left;
289 physDev->emh->rclFrame.top = rect->top;
290 physDev->emh->rclFrame.right = rect->right;
291 physDev->emh->rclFrame.bottom = rect->bottom;
292 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
293 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
294 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
297 physDev->emh->dSignature = ENHMETA_SIGNATURE;
298 physDev->emh->nVersion = 0x10000;
299 physDev->emh->nBytes = physDev->emh->nSize;
300 physDev->emh->nRecords = 1;
301 physDev->emh->nHandles = 1;
303 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
304 physDev->emh->nDescription = length / 2;
306 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
308 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
310 /* Size in pixels */
311 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
312 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
314 /* Size in millimeters */
315 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
316 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
318 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
320 if (filename) /* disk based metafile */
322 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
323 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
324 EMFDRV_DeleteDC( dc );
325 return 0;
327 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
328 EMFDRV_DeleteDC( dc );
329 return 0;
331 physDev->hFile = hFile;
334 if( !hdc )
335 DeleteDC( hRefDC );
337 TRACE("returning %04x\n", dc->hSelf);
338 ret = dc->hSelf;
339 GDI_ReleaseObj( dc->hSelf );
340 return ret;
343 /******************************************************************
344 * CloseEnhMetaFile
346 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
348 HENHMETAFILE hmf;
349 EMFDRV_PDEVICE *physDev;
350 DC *dc;
351 EMREOF emr;
352 HANDLE hMapping = 0;
354 TRACE("(%04x)\n", hdc );
356 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
357 physDev = (EMFDRV_PDEVICE *)dc->physDev;
359 emr.emr.iType = EMR_EOF;
360 emr.emr.nSize = sizeof(emr);
361 emr.nPalEntries = 0;
362 emr.offPalEntries = 0;
363 emr.nSizeLast = emr.emr.nSize;
364 EMFDRV_WriteRecord( dc, &emr.emr );
366 /* Update rclFrame if not initialized in CreateEnhMetaFile */
367 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
368 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
369 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
370 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
371 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
372 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
373 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
374 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
375 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
378 if (physDev->hFile) /* disk based metafile */
380 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
381 EMFDRV_DeleteDC( dc );
382 return 0;
385 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
386 sizeof(*physDev->emh), NULL, NULL)) {
387 EMFDRV_DeleteDC( dc );
388 return 0;
390 HeapFree( GetProcessHeap(), 0, physDev->emh );
391 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
392 0, NULL);
393 TRACE("hMapping = %08x\n", hMapping );
394 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
395 TRACE("view = %p\n", physDev->emh );
399 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->hFile, hMapping );
400 physDev->emh = NULL; /* So it won't be deleted */
401 EMFDRV_DeleteDC( dc );
402 return hmf;