Better separation of the palette functions.
[wine/multimedia.git] / dlls / gdi / enhmfdrv / init.c
blob84249df5ccecd059cedd25bd03a14b626fe1f295
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 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 NULL, /* 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, /* pSetDIBColorTable */
115 NULL, /* pSetDIBits */
116 NULL, /* pSetDIBitsToDevice */
117 NULL, /* pSetDeviceClipping */
118 NULL, /* pSetDeviceGammaRamp */
119 EMFDRV_SetMapMode, /* pSetMapMode */
120 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
121 NULL, /* pSetPixel */
122 NULL, /* pSetPixelFormat */
123 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
124 EMFDRV_SetROP2, /* pSetROP2 */
125 NULL, /* pSetRelAbs */
126 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
127 EMFDRV_SetTextAlign, /* pSetTextAlign */
128 NULL, /* pSetTextCharacterExtra */
129 EMFDRV_SetTextColor, /* pSetTextColor */
130 NULL, /* pSetTextJustification */
131 EMFDRV_SetViewportExt, /* pSetViewportExt */
132 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
133 EMFDRV_SetWindowExt, /* pSetWindowExt */
134 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
135 NULL, /* pStartDoc */
136 NULL, /* pStartPage */
137 NULL, /* pStretchBlt */
138 NULL, /* pStretchDIBits */
139 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
140 EMFDRV_StrokePath, /* pStrokePath */
141 NULL, /* pSwapBuffers */
142 EMFDRV_WidenPath /* pWiddenPath */
146 /**********************************************************************
147 * EMFDRV_DeleteDC
149 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
151 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
152 DC *dc = physDev->dc;
154 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
155 HeapFree( GetProcessHeap(), 0, physDev );
156 dc->physDev = NULL;
157 GDI_FreeObject( dc->hSelf, dc );
158 return TRUE;
162 /******************************************************************
163 * EMFDRV_WriteRecord
165 * Warning: this function can change the pointer to the metafile header.
167 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
169 DWORD len;
170 ENHMETAHEADER *emh;
171 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
173 physDev->emh->nBytes += emr->nSize;
174 physDev->emh->nRecords++;
176 if(physDev->hFile) {
177 TRACE("Writing record to disk\n");
178 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
179 return FALSE;
180 } else {
181 len = physDev->emh->nBytes;
182 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
183 if (!emh) return FALSE;
184 physDev->emh = emh;
185 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
186 emr->nSize);
188 return TRUE;
192 /******************************************************************
193 * EMFDRV_UpdateBBox
195 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
197 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
198 RECTL *bounds = &physDev->emh->rclBounds;
200 if(bounds->left > bounds->right) {/* first rect */
201 *bounds = *rect;
202 return;
204 bounds->left = min(bounds->left, rect->left);
205 bounds->top = min(bounds->top, rect->top);
206 bounds->right = max(bounds->right, rect->right);
207 bounds->bottom = max(bounds->bottom, rect->bottom);
208 return;
211 /******************************************************************
212 * EMFDRV_AddHandleDC
214 * Note: this function assumes that we never delete objects.
215 * If we do someday, we'll need to maintain a table to re-use deleted
216 * handles.
218 int EMFDRV_AddHandleDC( PHYSDEV dev )
220 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
221 physDev->emh->nHandles++;
222 return physDev->nextHandle++;
226 /**********************************************************************
227 * CreateEnhMetaFileA (GDI32.@)
229 HDC WINAPI CreateEnhMetaFileA(
230 HDC hdc, /* [in] optional reference DC */
231 LPCSTR filename, /* [in] optional filename for disk metafiles */
232 const RECT *rect, /* [in] optional bounding rectangle */
233 LPCSTR description /* [in] optional description */
236 LPWSTR filenameW = NULL;
237 LPWSTR descriptionW = NULL;
238 HDC hReturnDC;
239 DWORD len1, len2, total;
241 if(filename)
243 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
244 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
245 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
247 if(description) {
248 len1 = strlen(description);
249 len2 = strlen(description + len1 + 1);
250 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
251 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
252 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
255 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
257 if(filenameW)
258 HeapFree( GetProcessHeap(), 0, filenameW );
259 if(descriptionW)
260 HeapFree( GetProcessHeap(), 0, descriptionW );
262 return hReturnDC;
265 /**********************************************************************
266 * CreateEnhMetaFileW (GDI32.@)
268 HDC WINAPI CreateEnhMetaFileW(
269 HDC hdc, /* [in] optional reference DC */
270 LPCWSTR filename, /* [in] optional filename for disk metafiles */
271 const RECT* rect, /* [in] optional bounding rectangle */
272 LPCWSTR description /* [in] optional description */
275 HDC ret;
276 DC *dc;
277 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
278 EMFDRV_PDEVICE *physDev;
279 HFILE hFile;
280 DWORD size = 0, length = 0;
282 TRACE("%s\n", debugstr_w(filename) );
284 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
285 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
287 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
288 if (!physDev) {
289 GDI_FreeObject( dc->hSelf, dc );
290 return 0;
292 dc->physDev = (PHYSDEV)physDev;
293 physDev->hdc = dc->hSelf;
294 physDev->dc = dc;
296 if(description) { /* App name\0Title\0\0 */
297 length = lstrlenW(description);
298 length += lstrlenW(description + length + 1);
299 length += 3;
300 length *= 2;
302 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
304 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
305 HeapFree( GetProcessHeap(), 0, physDev );
306 GDI_FreeObject( dc->hSelf, dc );
307 return 0;
310 physDev->nextHandle = 1;
311 physDev->hFile = 0;
313 physDev->emh->iType = EMR_HEADER;
314 physDev->emh->nSize = size;
316 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
317 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
319 if(rect) {
320 physDev->emh->rclFrame.left = rect->left;
321 physDev->emh->rclFrame.top = rect->top;
322 physDev->emh->rclFrame.right = rect->right;
323 physDev->emh->rclFrame.bottom = rect->bottom;
324 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
325 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
326 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
329 physDev->emh->dSignature = ENHMETA_SIGNATURE;
330 physDev->emh->nVersion = 0x10000;
331 physDev->emh->nBytes = physDev->emh->nSize;
332 physDev->emh->nRecords = 1;
333 physDev->emh->nHandles = 1;
335 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
336 physDev->emh->nDescription = length / 2;
338 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
340 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
342 /* Size in pixels */
343 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
344 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
346 /* Size in millimeters */
347 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
348 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
350 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
352 if (filename) /* disk based metafile */
354 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
355 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
356 EMFDRV_DeleteDC( dc->physDev );
357 return 0;
359 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
360 EMFDRV_DeleteDC( dc->physDev );
361 return 0;
363 physDev->hFile = hFile;
366 TRACE("returning %04x\n", dc->hSelf);
367 ret = dc->hSelf;
368 GDI_ReleaseObj( dc->hSelf );
370 if( !hdc )
371 DeleteDC( hRefDC );
373 return ret;
376 /******************************************************************
377 * CloseEnhMetaFile (GDI32.@)
379 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
381 HENHMETAFILE hmf;
382 EMFDRV_PDEVICE *physDev;
383 DC *dc;
384 EMREOF emr;
385 HANDLE hMapping = 0;
387 TRACE("(%04x)\n", hdc );
389 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
390 physDev = (EMFDRV_PDEVICE *)dc->physDev;
392 emr.emr.iType = EMR_EOF;
393 emr.emr.nSize = sizeof(emr);
394 emr.nPalEntries = 0;
395 emr.offPalEntries = 0;
396 emr.nSizeLast = emr.emr.nSize;
397 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
399 /* Update rclFrame if not initialized in CreateEnhMetaFile */
400 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
401 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
402 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
403 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
404 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
405 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
406 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
407 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
408 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
411 if (physDev->hFile) /* disk based metafile */
413 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
415 CloseHandle( physDev->hFile );
416 EMFDRV_DeleteDC( dc->physDev );
417 return 0;
420 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
421 sizeof(*physDev->emh), NULL, NULL))
423 CloseHandle( physDev->hFile );
424 EMFDRV_DeleteDC( dc->physDev );
425 return 0;
427 HeapFree( GetProcessHeap(), 0, physDev->emh );
428 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
429 0, NULL);
430 TRACE("hMapping = %08x\n", hMapping );
431 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
432 TRACE("view = %p\n", physDev->emh );
433 CloseHandle( hMapping );
434 CloseHandle( physDev->hFile );
437 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
438 physDev->emh = NULL; /* So it won't be deleted */
439 EMFDRV_DeleteDC( dc->physDev );
440 return hmf;