Release 1.6-rc2.
[wine/testsucceed.git] / dlls / gdi32 / mfdrv / init.c
blob03cd22ce0034c4db5ed99aa4aad78ac53752eb8d
1 /*
2 * Metafile driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
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 <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
33 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev );
34 static BOOL MFDRV_DeleteDC( PHYSDEV dev );
37 /**********************************************************************
38 * MFDRV_ExtEscape
40 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
41 INT cbOutput, LPVOID out_data )
43 METARECORD *mr;
44 DWORD len;
45 INT ret;
47 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
49 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
50 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
51 mr->rdSize = len / 2;
52 mr->rdFunction = META_ESCAPE;
53 mr->rdParm[0] = nEscape;
54 mr->rdParm[1] = cbInput;
55 memcpy(&(mr->rdParm[2]), in_data, cbInput);
56 ret = MFDRV_WriteRecord( dev, mr, len);
57 HeapFree(GetProcessHeap(), 0, mr);
58 return ret;
62 /******************************************************************
63 * MFDRV_GetBoundsRect
65 static UINT MFDRV_GetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
67 return 0;
71 /******************************************************************
72 * MFDRV_SetBoundsRect
74 static UINT MFDRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
76 return 0;
80 /******************************************************************
81 * MFDRV_GetDeviceCaps
83 *A very simple implementation that returns DT_METAFILE
85 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
87 switch(cap)
89 case TECHNOLOGY:
90 return DT_METAFILE;
91 case TEXTCAPS:
92 return 0;
93 default:
94 TRACE(" unsupported capability %d, will return 0\n", cap );
96 return 0;
100 static const struct gdi_dc_funcs MFDRV_Funcs =
102 NULL, /* pAbortDoc */
103 MFDRV_AbortPath, /* pAbortPath */
104 NULL, /* pAlphaBlend */
105 NULL, /* pAngleArc */
106 MFDRV_Arc, /* pArc */
107 NULL, /* pArcTo */
108 MFDRV_BeginPath, /* pBeginPath */
109 NULL, /* pBlendImage */
110 MFDRV_Chord, /* pChord */
111 MFDRV_CloseFigure, /* pCloseFigure */
112 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
113 NULL, /* pCreateDC */
114 MFDRV_DeleteDC, /* pDeleteDC */
115 MFDRV_DeleteObject, /* pDeleteObject */
116 NULL, /* pDeviceCapabilities */
117 MFDRV_Ellipse, /* pEllipse */
118 NULL, /* pEndDoc */
119 NULL, /* pEndPage */
120 MFDRV_EndPath, /* pEndPath */
121 NULL, /* pEnumFonts */
122 NULL, /* pEnumICMProfiles */
123 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
124 NULL, /* pExtDeviceMode */
125 MFDRV_ExtEscape, /* pExtEscape */
126 MFDRV_ExtFloodFill, /* pExtFloodFill */
127 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
128 MFDRV_ExtTextOut, /* pExtTextOut */
129 MFDRV_FillPath, /* pFillPath */
130 MFDRV_FillRgn, /* pFillRgn */
131 MFDRV_FlattenPath, /* pFlattenPath */
132 NULL, /* pFontIsLinked */
133 MFDRV_FrameRgn, /* pFrameRgn */
134 NULL, /* pGdiComment */
135 NULL, /* pGdiRealizationInfo */
136 MFDRV_GetBoundsRect, /* pGetBoundsRect */
137 NULL, /* pGetCharABCWidths */
138 NULL, /* pGetCharABCWidthsI */
139 NULL, /* pGetCharWidth */
140 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
141 NULL, /* pGetDeviceGammaRamp */
142 NULL, /* pGetFontData */
143 NULL, /* pGetFontUnicodeRanges */
144 NULL, /* pGetGlyphIndices */
145 NULL, /* pGetGlyphOutline */
146 NULL, /* pGetICMProfile */
147 NULL, /* pGetImage */
148 NULL, /* pGetKerningPairs */
149 NULL, /* pGetNearestColor */
150 NULL, /* pGetOutlineTextMetrics */
151 NULL, /* pGetPixel */
152 NULL, /* pGetSystemPaletteEntries */
153 NULL, /* pGetTextCharsetInfo */
154 NULL, /* pGetTextExtentExPoint */
155 NULL, /* pGetTextExtentExPointI */
156 NULL, /* pGetTextFace */
157 NULL, /* pGetTextMetrics */
158 NULL, /* pGradientFill */
159 MFDRV_IntersectClipRect, /* pIntersectClipRect */
160 MFDRV_InvertRgn, /* pInvertRgn */
161 MFDRV_LineTo, /* pLineTo */
162 NULL, /* pModifyWorldTransform */
163 MFDRV_MoveTo, /* pMoveTo */
164 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
165 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
166 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
167 MFDRV_PaintRgn, /* pPaintRgn */
168 MFDRV_PatBlt, /* pPatBlt */
169 MFDRV_Pie, /* pPie */
170 MFDRV_PolyBezier, /* pPolyBezier */
171 MFDRV_PolyBezierTo, /* pPolyBezierTo */
172 NULL, /* pPolyDraw */
173 MFDRV_PolyPolygon, /* pPolyPolygon */
174 NULL, /* pPolyPolyline */
175 MFDRV_Polygon, /* pPolygon */
176 MFDRV_Polyline, /* pPolyline */
177 NULL, /* pPolylineTo */
178 NULL, /* pPutImage */
179 NULL, /* pRealizeDefaultPalette */
180 MFDRV_RealizePalette, /* pRealizePalette */
181 MFDRV_Rectangle, /* pRectangle */
182 NULL, /* pResetDC */
183 MFDRV_RestoreDC, /* pRestoreDC */
184 MFDRV_RoundRect, /* pRoundRect */
185 MFDRV_SaveDC, /* pSaveDC */
186 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
187 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
188 MFDRV_SelectBitmap, /* pSelectBitmap */
189 MFDRV_SelectBrush, /* pSelectBrush */
190 MFDRV_SelectClipPath, /* pSelectClipPath */
191 MFDRV_SelectFont, /* pSelectFont */
192 MFDRV_SelectPalette, /* pSelectPalette */
193 MFDRV_SelectPen, /* pSelectPen */
194 NULL, /* pSetArcDirection */
195 MFDRV_SetBkColor, /* pSetBkColor */
196 MFDRV_SetBkMode, /* pSetBkMode */
197 MFDRV_SetBoundsRect, /* pSetBoundsRect */
198 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
199 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
200 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
201 NULL, /* pSetDeviceClipping */
202 NULL, /* pSetDeviceGammaRamp */
203 NULL, /* pSetLayout */
204 MFDRV_SetMapMode, /* pSetMapMode */
205 MFDRV_SetMapperFlags, /* pSetMapperFlags */
206 MFDRV_SetPixel, /* pSetPixel */
207 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
208 MFDRV_SetROP2, /* pSetROP2 */
209 MFDRV_SetRelAbs, /* pSetRelAbs */
210 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
211 MFDRV_SetTextAlign, /* pSetTextAlign */
212 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
213 MFDRV_SetTextColor, /* pSetTextColor */
214 MFDRV_SetTextJustification, /* pSetTextJustification */
215 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
216 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
217 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
218 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
219 NULL, /* pSetWorldTransform */
220 NULL, /* pStartDoc */
221 NULL, /* pStartPage */
222 MFDRV_StretchBlt, /* pStretchBlt */
223 MFDRV_StretchDIBits, /* pStretchDIBits */
224 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
225 MFDRV_StrokePath, /* pStrokePath */
226 NULL, /* pUnrealizePalette */
227 MFDRV_WidenPath, /* pWidenPath */
228 NULL, /* wine_get_wgl_driver */
229 GDI_PRIORITY_GRAPHICS_DRV /* priority */
234 /**********************************************************************
235 * MFDRV_AllocMetaFile
237 static DC *MFDRV_AllocMetaFile(void)
239 DC *dc;
240 METAFILEDRV_PDEVICE *physDev;
242 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
244 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
245 if (!physDev)
247 free_dc_ptr( dc );
248 return NULL;
250 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
252 HeapFree( GetProcessHeap(), 0, physDev );
253 free_dc_ptr( dc );
254 return NULL;
257 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
259 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
260 physDev->handles_size = HANDLE_LIST_INC;
261 physDev->cur_handles = 0;
263 physDev->hFile = 0;
265 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
266 physDev->mh->mtVersion = 0x0300;
267 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
268 physDev->mh->mtNoObjects = 0;
269 physDev->mh->mtMaxRecord = 0;
270 physDev->mh->mtNoParameters = 0;
272 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
274 return dc;
278 /**********************************************************************
279 * MFDRV_CreateCompatibleDC
281 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
283 /* not supported on metafile DCs */
284 return FALSE;
288 /**********************************************************************
289 * MFDRV_DeleteDC
291 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
293 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
294 DWORD index;
296 HeapFree( GetProcessHeap(), 0, physDev->mh );
297 for(index = 0; index < physDev->handles_size; index++)
298 if(physDev->handles[index])
299 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
300 HeapFree( GetProcessHeap(), 0, physDev->handles );
301 HeapFree( GetProcessHeap(), 0, physDev );
302 return TRUE;
306 /**********************************************************************
307 * CreateMetaFileW (GDI32.@)
309 * Create a new DC and associate it with a metafile. Pass a filename
310 * to create a disk-based metafile, NULL to create a memory metafile.
312 * PARAMS
313 * filename [I] Filename of disk metafile
315 * RETURNS
316 * A handle to the metafile DC if successful, NULL on failure.
318 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
320 HDC ret;
321 DC *dc;
322 METAFILEDRV_PDEVICE *physDev;
323 HANDLE hFile;
325 TRACE("%s\n", debugstr_w(filename) );
327 if (!(dc = MFDRV_AllocMetaFile())) return 0;
328 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
330 if (filename) /* disk based metafile */
332 physDev->mh->mtType = METAFILE_DISK;
333 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
334 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
335 free_dc_ptr( dc );
336 return 0;
338 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
339 NULL )) {
340 free_dc_ptr( dc );
341 return 0;
343 physDev->hFile = hFile;
345 /* Grow METAHEADER to include filename */
346 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
348 else /* memory based metafile */
349 physDev->mh->mtType = METAFILE_MEMORY;
351 TRACE("returning %p\n", physDev->dev.hdc);
352 ret = physDev->dev.hdc;
353 release_dc_ptr( dc );
354 return ret;
357 /**********************************************************************
358 * CreateMetaFileA (GDI32.@)
360 * See CreateMetaFileW.
362 HDC WINAPI CreateMetaFileA(LPCSTR filename)
364 LPWSTR filenameW;
365 DWORD len;
366 HDC hReturnDC;
368 if (!filename) return CreateMetaFileW(NULL);
370 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
371 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
372 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
374 hReturnDC = CreateMetaFileW(filenameW);
376 HeapFree( GetProcessHeap(), 0, filenameW );
378 return hReturnDC;
382 /**********************************************************************
383 * MFDRV_CloseMetaFile
385 static DC *MFDRV_CloseMetaFile( HDC hdc )
387 DC *dc;
388 METAFILEDRV_PDEVICE *physDev;
390 TRACE("(%p)\n", hdc );
392 if (!(dc = get_dc_ptr( hdc ))) return NULL;
393 if (GetObjectType( hdc ) != OBJ_METADC)
395 release_dc_ptr( dc );
396 return NULL;
398 if (dc->refcount != 1)
400 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
401 release_dc_ptr( dc );
402 return NULL;
404 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
406 /* Construct the end of metafile record - this is documented
407 * in SDK Knowledgebase Q99334.
410 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
412 free_dc_ptr( dc );
413 return 0;
416 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
418 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
419 free_dc_ptr( dc );
420 return 0;
423 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
424 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
425 NULL, NULL)) {
426 free_dc_ptr( dc );
427 return 0;
429 CloseHandle(physDev->hFile);
430 physDev->mh->mtType = METAFILE_DISK;
433 return dc;
436 /******************************************************************
437 * CloseMetaFile (GDI32.@)
439 * Stop recording graphics operations in metafile associated with
440 * hdc and retrieve metafile.
442 * PARAMS
443 * hdc [I] Metafile DC to close
445 * RETURNS
446 * Handle of newly created metafile on success, NULL on failure.
448 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
450 HMETAFILE hmf;
451 METAFILEDRV_PDEVICE *physDev;
452 DC *dc = MFDRV_CloseMetaFile(hdc);
453 if (!dc) return 0;
454 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
456 /* Now allocate a global handle for the metafile */
458 hmf = MF_Create_HMETAFILE( physDev->mh );
460 physDev->mh = NULL; /* So it won't be deleted */
461 free_dc_ptr( dc );
462 return hmf;
466 /******************************************************************
467 * MFDRV_WriteRecord
469 * Warning: this function can change the pointer to the metafile header.
471 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
473 DWORD len, size;
474 METAHEADER *mh;
475 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
477 switch(physDev->mh->mtType)
479 case METAFILE_MEMORY:
480 len = physDev->mh->mtSize * 2 + rlen;
481 /* reallocate memory if needed */
482 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
483 if (len > size)
485 /*expand size*/
486 size += size / 2 + rlen;
487 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
488 if (!mh) return FALSE;
489 physDev->mh = mh;
490 TRACE("Reallocated metafile: new size is %d\n",size);
492 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
493 break;
494 case METAFILE_DISK:
495 TRACE("Writing record to disk\n");
496 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
497 return FALSE;
498 break;
499 default:
500 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
501 return FALSE;
504 physDev->mh->mtSize += rlen / 2;
505 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
506 return TRUE;
510 /******************************************************************
511 * MFDRV_MetaParam0
514 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
516 char buffer[8];
517 METARECORD *mr = (METARECORD *)&buffer;
519 mr->rdSize = 3;
520 mr->rdFunction = func;
521 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
525 /******************************************************************
526 * MFDRV_MetaParam1
528 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
530 char buffer[8];
531 METARECORD *mr = (METARECORD *)&buffer;
532 WORD *params = mr->rdParm;
534 mr->rdSize = 4;
535 mr->rdFunction = func;
536 params[0] = param1;
537 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
541 /******************************************************************
542 * MFDRV_MetaParam2
544 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
546 char buffer[10];
547 METARECORD *mr = (METARECORD *)&buffer;
548 WORD *params = mr->rdParm;
550 mr->rdSize = 5;
551 mr->rdFunction = func;
552 params[0] = param2;
553 params[1] = param1;
554 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
558 /******************************************************************
559 * MFDRV_MetaParam4
562 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
563 short param3, short param4)
565 char buffer[14];
566 METARECORD *mr = (METARECORD *)&buffer;
567 WORD *params = mr->rdParm;
569 mr->rdSize = 7;
570 mr->rdFunction = func;
571 params[0] = param4;
572 params[1] = param3;
573 params[2] = param2;
574 params[3] = param1;
575 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
579 /******************************************************************
580 * MFDRV_MetaParam6
583 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
584 short param3, short param4, short param5, short param6)
586 char buffer[18];
587 METARECORD *mr = (METARECORD *)&buffer;
588 WORD *params = mr->rdParm;
590 mr->rdSize = 9;
591 mr->rdFunction = func;
592 params[0] = param6;
593 params[1] = param5;
594 params[2] = param4;
595 params[3] = param3;
596 params[4] = param2;
597 params[5] = param1;
598 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
602 /******************************************************************
603 * MFDRV_MetaParam8
605 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
606 short param3, short param4, short param5,
607 short param6, short param7, short param8)
609 char buffer[22];
610 METARECORD *mr = (METARECORD *)&buffer;
611 WORD *params = mr->rdParm;
613 mr->rdSize = 11;
614 mr->rdFunction = func;
615 params[0] = param8;
616 params[1] = param7;
617 params[2] = param6;
618 params[3] = param5;
619 params[4] = param4;
620 params[5] = param3;
621 params[6] = param2;
622 params[7] = param1;
623 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);