strmbase: Implement BaseControlWindow.
[wine/multimedia.git] / dlls / gdi32 / enhmfdrv / init.c
blobd254cba1431d75a48fcfbb783b968ad5115e5361
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winnls.h"
29 #include "gdi_private.h"
30 #include "enhmfdrv/enhmetafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35 static BOOL EMFDRV_DeleteDC( PHYSDEV dev );
37 static const struct gdi_dc_funcs EMFDRV_Funcs =
39 NULL, /* pAbortDoc */
40 EMFDRV_AbortPath, /* pAbortPath */
41 NULL, /* pAlphaBlend */
42 NULL, /* pAngleArc */
43 EMFDRV_Arc, /* pArc */
44 NULL, /* pArcTo */
45 EMFDRV_BeginPath, /* pBeginPath */
46 NULL, /* pBlendImage */
47 NULL, /* pChoosePixelFormat */
48 EMFDRV_Chord, /* pChord */
49 EMFDRV_CloseFigure, /* pCloseFigure */
50 NULL, /* pCopyBitmap */
51 NULL, /* pCreateBitmap */
52 NULL, /* pCreateCompatibleDC */
53 NULL, /* pCreateDC */
54 NULL, /* pDeleteBitmap */
55 EMFDRV_DeleteDC, /* pDeleteDC */
56 EMFDRV_DeleteObject, /* pDeleteObject */
57 NULL, /* pDescribePixelFormat */
58 NULL, /* pDeviceCapabilities */
59 EMFDRV_Ellipse, /* pEllipse */
60 NULL, /* pEndDoc */
61 NULL, /* pEndPage */
62 EMFDRV_EndPath, /* pEndPath */
63 NULL, /* pEnumFonts */
64 NULL, /* pEnumICMProfiles */
65 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
66 NULL, /* pExtDeviceMode */
67 NULL, /* pExtEscape */
68 EMFDRV_ExtFloodFill, /* pExtFloodFill */
69 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
70 EMFDRV_ExtTextOut, /* pExtTextOut */
71 EMFDRV_FillPath, /* pFillPath */
72 EMFDRV_FillRgn, /* pFillRgn */
73 EMFDRV_FlattenPath, /* pFlattenPath */
74 NULL, /* pFontIsLinked */
75 EMFDRV_FrameRgn, /* pFrameRgn */
76 EMFDRV_GdiComment, /* pGdiComment */
77 NULL, /* pGdiRealizationInfo */
78 NULL, /* pGetCharABCWidths */
79 NULL, /* pGetCharABCWidthsI */
80 NULL, /* pGetCharWidth */
81 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
82 NULL, /* pGetDeviceGammaRamp */
83 NULL, /* pGetFontData */
84 NULL, /* pGetFontUnicodeRanges */
85 NULL, /* pGetGlyphIndices */
86 NULL, /* pGetGlyphOutline */
87 NULL, /* pGetICMProfile */
88 NULL, /* pGetImage */
89 NULL, /* pGetKerningPairs */
90 NULL, /* pGetNearestColor */
91 NULL, /* pGetOutlineTextMetrics */
92 NULL, /* pGetPixel */
93 NULL, /* pGetPixelFormat */
94 NULL, /* pGetSystemPaletteEntries */
95 NULL, /* pGetTextCharsetInfo */
96 NULL, /* pGetTextExtentExPoint */
97 NULL, /* pGetTextExtentExPointI */
98 NULL, /* pGetTextFace */
99 NULL, /* pGetTextMetrics */
100 NULL, /* pGradientFill */
101 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
102 EMFDRV_InvertRgn, /* pInvertRgn */
103 EMFDRV_LineTo, /* pLineTo */
104 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
105 EMFDRV_MoveTo, /* pMoveTo */
106 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
107 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
108 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
109 EMFDRV_PaintRgn, /* pPaintRgn */
110 EMFDRV_PatBlt, /* pPatBlt */
111 EMFDRV_Pie, /* pPie */
112 EMFDRV_PolyBezier, /* pPolyBezier */
113 EMFDRV_PolyBezierTo, /* pPolyBezierTo */
114 NULL, /* pPolyDraw */
115 EMFDRV_PolyPolygon, /* pPolyPolygon */
116 EMFDRV_PolyPolyline, /* pPolyPolyline */
117 EMFDRV_Polygon, /* pPolygon */
118 EMFDRV_Polyline, /* pPolyline */
119 NULL, /* pPolylineTo */
120 NULL, /* pPutImage */
121 NULL, /* pRealizeDefaultPalette */
122 NULL, /* pRealizePalette */
123 EMFDRV_Rectangle, /* pRectangle */
124 NULL, /* pResetDC */
125 EMFDRV_RestoreDC, /* pRestoreDC */
126 EMFDRV_RoundRect, /* pRoundRect */
127 EMFDRV_SaveDC, /* pSaveDC */
128 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
129 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
130 EMFDRV_SelectBitmap, /* pSelectBitmap */
131 EMFDRV_SelectBrush, /* pSelectBrush */
132 EMFDRV_SelectClipPath, /* pSelectClipPath */
133 EMFDRV_SelectFont, /* pSelectFont */
134 EMFDRV_SelectPalette, /* pSelectPalette */
135 EMFDRV_SelectPen, /* pSelectPen */
136 EMFDRV_SetArcDirection, /* pSetArcDirection */
137 EMFDRV_SetBkColor, /* pSetBkColor */
138 EMFDRV_SetBkMode, /* pSetBkMode */
139 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
140 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
141 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
142 NULL, /* pSetDeviceClipping */
143 NULL, /* pSetDeviceGammaRamp */
144 EMFDRV_SetLayout, /* pSetLayout */
145 EMFDRV_SetMapMode, /* pSetMapMode */
146 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
147 EMFDRV_SetPixel, /* pSetPixel */
148 NULL, /* pSetPixelFormat */
149 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
150 EMFDRV_SetROP2, /* pSetROP2 */
151 NULL, /* pSetRelAbs */
152 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
153 EMFDRV_SetTextAlign, /* pSetTextAlign */
154 NULL, /* pSetTextCharacterExtra */
155 EMFDRV_SetTextColor, /* pSetTextColor */
156 EMFDRV_SetTextJustification, /* pSetTextJustification */
157 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
158 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
159 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
160 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
161 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
162 NULL, /* pStartDoc */
163 NULL, /* pStartPage */
164 EMFDRV_StretchBlt, /* pStretchBlt */
165 EMFDRV_StretchDIBits, /* pStretchDIBits */
166 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
167 EMFDRV_StrokePath, /* pStrokePath */
168 NULL, /* pSwapBuffers */
169 NULL, /* pUnrealizePalette */
170 EMFDRV_WidenPath /* pWidenPath */
174 /**********************************************************************
175 * EMFDRV_DeleteDC
177 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
179 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
180 UINT index;
182 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
183 for(index = 0; index < physDev->handles_size; index++)
184 if(physDev->handles[index])
185 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
186 HeapFree( GetProcessHeap(), 0, physDev->handles );
187 HeapFree( GetProcessHeap(), 0, physDev );
188 return TRUE;
192 /******************************************************************
193 * EMFDRV_WriteRecord
195 * Warning: this function can change the pointer to the metafile header.
197 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
199 DWORD len;
200 ENHMETAHEADER *emh;
201 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
203 TRACE("record %d, size %d %s\n",
204 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
206 assert( !(emr->nSize & 3) );
208 physDev->emh->nBytes += emr->nSize;
209 physDev->emh->nRecords++;
211 if(physDev->hFile) {
212 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
213 return FALSE;
214 } else {
215 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
216 len = physDev->emh->nBytes;
217 if (len > nEmfSize) {
218 nEmfSize += (nEmfSize / 2) + emr->nSize;
219 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
220 if (!emh) return FALSE;
221 physDev->emh = emh;
223 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
224 emr->nSize);
226 return TRUE;
230 /******************************************************************
231 * EMFDRV_UpdateBBox
233 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
235 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
236 RECTL *bounds = &physDev->emh->rclBounds;
237 RECTL vportRect = *rect;
239 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
241 /* The coordinate systems may be mirrored
242 (LPtoDP handles points, not rectangles) */
243 if (vportRect.left > vportRect.right)
245 LONG temp = vportRect.right;
246 vportRect.right = vportRect.left;
247 vportRect.left = temp;
249 if (vportRect.top > vportRect.bottom)
251 LONG temp = vportRect.bottom;
252 vportRect.bottom = vportRect.top;
253 vportRect.top = temp;
256 if (bounds->left > bounds->right)
258 /* first bounding rectangle */
259 *bounds = vportRect;
261 else
263 bounds->left = min(bounds->left, vportRect.left);
264 bounds->top = min(bounds->top, vportRect.top);
265 bounds->right = max(bounds->right, vportRect.right);
266 bounds->bottom = max(bounds->bottom, vportRect.bottom);
270 /**********************************************************************
271 * CreateEnhMetaFileA (GDI32.@)
273 HDC WINAPI CreateEnhMetaFileA(
274 HDC hdc, /* [in] optional reference DC */
275 LPCSTR filename, /* [in] optional filename for disk metafiles */
276 const RECT *rect, /* [in] optional bounding rectangle */
277 LPCSTR description /* [in] optional description */
280 LPWSTR filenameW = NULL;
281 LPWSTR descriptionW = NULL;
282 HDC hReturnDC;
283 DWORD len1, len2, total;
285 if(filename)
287 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
288 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
289 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
291 if(description) {
292 len1 = strlen(description);
293 len2 = strlen(description + len1 + 1);
294 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
295 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
296 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
299 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
301 HeapFree( GetProcessHeap(), 0, filenameW );
302 HeapFree( GetProcessHeap(), 0, descriptionW );
304 return hReturnDC;
307 /**********************************************************************
308 * CreateEnhMetaFileW (GDI32.@)
310 HDC WINAPI CreateEnhMetaFileW(
311 HDC hdc, /* [in] optional reference DC */
312 LPCWSTR filename, /* [in] optional filename for disk metafiles */
313 const RECT* rect, /* [in] optional bounding rectangle */
314 LPCWSTR description /* [in] optional description */
317 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
318 HDC ret;
319 DC *dc;
320 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
321 /* If no ref, use current display */
322 EMFDRV_PDEVICE *physDev;
323 HANDLE hFile;
324 DWORD size = 0, length = 0;
326 TRACE("%s\n", debugstr_w(filename) );
328 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
330 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
331 if (!physDev) {
332 free_dc_ptr( dc );
333 return 0;
335 if(description) { /* App name\0Title\0\0 */
336 length = lstrlenW(description);
337 length += lstrlenW(description + length + 1);
338 length += 3;
339 length *= 2;
341 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
343 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
344 HeapFree( GetProcessHeap(), 0, physDev );
345 free_dc_ptr( dc );
346 return 0;
349 push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
351 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
352 physDev->handles_size = HANDLE_LIST_INC;
353 physDev->cur_handles = 1;
354 physDev->hFile = 0;
355 physDev->dc_brush = 0;
356 physDev->dc_pen = 0;
357 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
358 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
359 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
360 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
361 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
362 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
363 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
364 physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
365 physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
366 physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
367 physDev->planes = GetDeviceCaps(hRefDC, PLANES);
368 physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
369 physDev->restoring = 0;
371 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
373 physDev->emh->iType = EMR_HEADER;
374 physDev->emh->nSize = size;
376 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
377 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
379 if(rect) {
380 physDev->emh->rclFrame.left = rect->left;
381 physDev->emh->rclFrame.top = rect->top;
382 physDev->emh->rclFrame.right = rect->right;
383 physDev->emh->rclFrame.bottom = rect->bottom;
384 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
385 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
386 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
389 physDev->emh->dSignature = ENHMETA_SIGNATURE;
390 physDev->emh->nVersion = 0x10000;
391 physDev->emh->nBytes = physDev->emh->nSize;
392 physDev->emh->nRecords = 1;
393 physDev->emh->nHandles = 1;
395 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
396 physDev->emh->nDescription = length / 2;
398 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
400 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
402 /* Size in pixels */
403 physDev->emh->szlDevice.cx = physDev->horzres;
404 physDev->emh->szlDevice.cy = physDev->vertres;
406 /* Size in millimeters */
407 physDev->emh->szlMillimeters.cx = physDev->horzsize;
408 physDev->emh->szlMillimeters.cy = physDev->vertsize;
410 /* Size in micrometers */
411 physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
412 physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
414 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
416 if (filename) /* disk based metafile */
418 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
419 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
420 free_dc_ptr( dc );
421 return 0;
423 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
424 free_dc_ptr( dc );
425 return 0;
427 physDev->hFile = hFile;
430 TRACE("returning %p\n", physDev->dev.hdc);
431 ret = physDev->dev.hdc;
432 release_dc_ptr( dc );
434 if( !hdc )
435 DeleteDC( hRefDC );
437 return ret;
440 /******************************************************************
441 * CloseEnhMetaFile (GDI32.@)
443 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
445 HENHMETAFILE hmf;
446 EMFDRV_PDEVICE *physDev;
447 DC *dc;
448 EMREOF emr;
449 HANDLE hMapping = 0;
451 TRACE("(%p)\n", hdc );
453 if (!(dc = get_dc_ptr( hdc ))) return NULL;
454 if (dc->header.type != OBJ_ENHMETADC)
456 release_dc_ptr( dc );
457 return NULL;
459 if (dc->refcount != 1)
461 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
462 release_dc_ptr( dc );
463 return NULL;
465 physDev = (EMFDRV_PDEVICE *)dc->physDev;
467 if(dc->saveLevel)
468 RestoreDC(hdc, 1);
470 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
471 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
473 emr.emr.iType = EMR_EOF;
474 emr.emr.nSize = sizeof(emr);
475 emr.nPalEntries = 0;
476 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
477 emr.nSizeLast = emr.emr.nSize;
478 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
480 /* Update rclFrame if not initialized in CreateEnhMetaFile */
481 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
482 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
483 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
484 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
485 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
486 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
487 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
488 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
489 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
492 if (physDev->hFile) /* disk based metafile */
494 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
496 CloseHandle( physDev->hFile );
497 free_dc_ptr( dc );
498 return 0;
501 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
502 NULL, NULL))
504 CloseHandle( physDev->hFile );
505 free_dc_ptr( dc );
506 return 0;
508 HeapFree( GetProcessHeap(), 0, physDev->emh );
509 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
510 0, NULL);
511 TRACE("hMapping = %p\n", hMapping );
512 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
513 TRACE("view = %p\n", physDev->emh );
514 CloseHandle( hMapping );
515 CloseHandle( physDev->hFile );
518 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
519 physDev->emh = NULL; /* So it won't be deleted */
520 free_dc_ptr( dc );
521 return hmf;