user32: When building linedefs prev can be less than 0 if the format width is less...
[wine.git] / dlls / gdi32 / mfdrv / init.c
blobe9eca7a910ede625156bffa64613ad26946c4662
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_GetDeviceCaps
65 *A very simple implementation that returns DT_METAFILE
67 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
69 switch(cap)
71 case TECHNOLOGY:
72 return DT_METAFILE;
73 case TEXTCAPS:
74 return 0;
75 default:
76 TRACE(" unsupported capability %d, will return 0\n", cap );
78 return 0;
82 static const struct gdi_dc_funcs MFDRV_Funcs =
84 NULL, /* pAbortDoc */
85 MFDRV_AbortPath, /* pAbortPath */
86 NULL, /* pAlphaBlend */
87 NULL, /* pAngleArc */
88 MFDRV_Arc, /* pArc */
89 NULL, /* pArcTo */
90 MFDRV_BeginPath, /* pBeginPath */
91 NULL, /* pBlendImage */
92 NULL, /* pChoosePixelFormat */
93 MFDRV_Chord, /* pChord */
94 MFDRV_CloseFigure, /* pCloseFigure */
95 NULL, /* pCreateBitmap */
96 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
97 NULL, /* pCreateDC */
98 NULL, /* pCreateDIBSection */
99 NULL, /* pDeleteBitmap */
100 MFDRV_DeleteDC, /* pDeleteDC */
101 MFDRV_DeleteObject, /* pDeleteObject */
102 NULL, /* pDescribePixelFormat */
103 NULL, /* pDeviceCapabilities */
104 MFDRV_Ellipse, /* pEllipse */
105 NULL, /* pEndDoc */
106 NULL, /* pEndPage */
107 MFDRV_EndPath, /* pEndPath */
108 NULL, /* pEnumFonts */
109 NULL, /* pEnumICMProfiles */
110 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
111 NULL, /* pExtDeviceMode */
112 MFDRV_ExtEscape, /* pExtEscape */
113 MFDRV_ExtFloodFill, /* pExtFloodFill */
114 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
115 MFDRV_ExtTextOut, /* pExtTextOut */
116 MFDRV_FillPath, /* pFillPath */
117 MFDRV_FillRgn, /* pFillRgn */
118 MFDRV_FlattenPath, /* pFlattenPath */
119 NULL, /* pFontIsLinked */
120 MFDRV_FrameRgn, /* pFrameRgn */
121 NULL, /* pGdiComment */
122 NULL, /* pGdiRealizationInfo */
123 NULL, /* pGetCharABCWidths */
124 NULL, /* pGetCharABCWidthsI */
125 NULL, /* pGetCharWidth */
126 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
127 NULL, /* pGetDeviceGammaRamp */
128 NULL, /* pGetFontData */
129 NULL, /* pGetFontUnicodeRanges */
130 NULL, /* pGetGlyphIndices */
131 NULL, /* pGetGlyphOutline */
132 NULL, /* pGetICMProfile */
133 NULL, /* pGetImage */
134 NULL, /* pGetKerningPairs */
135 NULL, /* pGetNearestColor */
136 NULL, /* pGetOutlineTextMetrics */
137 NULL, /* pGetPixel */
138 NULL, /* pGetPixelFormat */
139 NULL, /* pGetSystemPaletteEntries */
140 NULL, /* pGetTextCharsetInfo */
141 NULL, /* pGetTextExtentExPoint */
142 NULL, /* pGetTextExtentExPointI */
143 NULL, /* pGetTextFace */
144 NULL, /* pGetTextMetrics */
145 MFDRV_IntersectClipRect, /* pIntersectClipRect */
146 MFDRV_InvertRgn, /* pInvertRgn */
147 MFDRV_LineTo, /* pLineTo */
148 NULL, /* pModifyWorldTransform */
149 MFDRV_MoveTo, /* pMoveTo */
150 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
151 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
152 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
153 MFDRV_PaintRgn, /* pPaintRgn */
154 MFDRV_PatBlt, /* pPatBlt */
155 MFDRV_Pie, /* pPie */
156 MFDRV_PolyBezier, /* pPolyBezier */
157 MFDRV_PolyBezierTo, /* pPolyBezierTo */
158 NULL, /* pPolyDraw */
159 MFDRV_PolyPolygon, /* pPolyPolygon */
160 NULL, /* pPolyPolyline */
161 MFDRV_Polygon, /* pPolygon */
162 MFDRV_Polyline, /* pPolyline */
163 NULL, /* pPolylineTo */
164 NULL, /* pPutImage */
165 NULL, /* pRealizeDefaultPalette */
166 MFDRV_RealizePalette, /* pRealizePalette */
167 MFDRV_Rectangle, /* pRectangle */
168 NULL, /* pResetDC */
169 MFDRV_RestoreDC, /* pRestoreDC */
170 MFDRV_RoundRect, /* pRoundRect */
171 MFDRV_SaveDC, /* pSaveDC */
172 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
173 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
174 MFDRV_SelectBitmap, /* pSelectBitmap */
175 MFDRV_SelectBrush, /* pSelectBrush */
176 MFDRV_SelectClipPath, /* pSelectClipPath */
177 MFDRV_SelectFont, /* pSelectFont */
178 MFDRV_SelectPalette, /* pSelectPalette */
179 MFDRV_SelectPen, /* pSelectPen */
180 NULL, /* pSetArcDirection */
181 MFDRV_SetBkColor, /* pSetBkColor */
182 MFDRV_SetBkMode, /* pSetBkMode */
183 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
184 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
185 NULL, /* pSetDIBColorTable */
186 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
187 NULL, /* pSetDeviceClipping */
188 NULL, /* pSetDeviceGammaRamp */
189 NULL, /* pSetLayout */
190 MFDRV_SetMapMode, /* pSetMapMode */
191 MFDRV_SetMapperFlags, /* pSetMapperFlags */
192 MFDRV_SetPixel, /* pSetPixel */
193 NULL, /* pSetPixelFormat */
194 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
195 MFDRV_SetROP2, /* pSetROP2 */
196 MFDRV_SetRelAbs, /* pSetRelAbs */
197 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
198 MFDRV_SetTextAlign, /* pSetTextAlign */
199 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
200 MFDRV_SetTextColor, /* pSetTextColor */
201 MFDRV_SetTextJustification, /* pSetTextJustification */
202 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
203 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
204 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
205 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
206 NULL, /* pSetWorldTransform */
207 NULL, /* pStartDoc */
208 NULL, /* pStartPage */
209 MFDRV_StretchBlt, /* pStretchBlt */
210 MFDRV_StretchDIBits, /* pStretchDIBits */
211 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
212 MFDRV_StrokePath, /* pStrokePath */
213 NULL, /* pSwapBuffers */
214 NULL, /* pUnrealizePalette */
215 MFDRV_WidenPath /* pWidenPath */
220 /**********************************************************************
221 * MFDRV_AllocMetaFile
223 static DC *MFDRV_AllocMetaFile(void)
225 DC *dc;
226 METAFILEDRV_PDEVICE *physDev;
228 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
230 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
231 if (!physDev)
233 free_dc_ptr( dc );
234 return NULL;
236 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
238 HeapFree( GetProcessHeap(), 0, physDev );
239 free_dc_ptr( dc );
240 return NULL;
243 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
245 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
246 physDev->handles_size = HANDLE_LIST_INC;
247 physDev->cur_handles = 0;
249 physDev->hFile = 0;
251 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
252 physDev->mh->mtVersion = 0x0300;
253 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
254 physDev->mh->mtNoObjects = 0;
255 physDev->mh->mtMaxRecord = 0;
256 physDev->mh->mtNoParameters = 0;
258 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
260 return dc;
264 /**********************************************************************
265 * MFDRV_CreateCompatibleDC
267 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
269 /* not supported on metafile DCs */
270 return FALSE;
274 /**********************************************************************
275 * MFDRV_DeleteDC
277 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
279 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
280 DWORD index;
282 HeapFree( GetProcessHeap(), 0, physDev->mh );
283 for(index = 0; index < physDev->handles_size; index++)
284 if(physDev->handles[index])
285 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
286 HeapFree( GetProcessHeap(), 0, physDev->handles );
287 HeapFree( GetProcessHeap(), 0, physDev );
288 return TRUE;
292 /**********************************************************************
293 * CreateMetaFileW (GDI32.@)
295 * Create a new DC and associate it with a metafile. Pass a filename
296 * to create a disk-based metafile, NULL to create a memory metafile.
298 * PARAMS
299 * filename [I] Filename of disk metafile
301 * RETURNS
302 * A handle to the metafile DC if successful, NULL on failure.
304 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
306 HDC ret;
307 DC *dc;
308 METAFILEDRV_PDEVICE *physDev;
309 HANDLE hFile;
311 TRACE("%s\n", debugstr_w(filename) );
313 if (!(dc = MFDRV_AllocMetaFile())) return 0;
314 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
316 if (filename) /* disk based metafile */
318 physDev->mh->mtType = METAFILE_DISK;
319 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
320 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
321 free_dc_ptr( dc );
322 return 0;
324 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
325 NULL )) {
326 free_dc_ptr( dc );
327 return 0;
329 physDev->hFile = hFile;
331 /* Grow METAHEADER to include filename */
332 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
334 else /* memory based metafile */
335 physDev->mh->mtType = METAFILE_MEMORY;
337 TRACE("returning %p\n", physDev->dev.hdc);
338 ret = physDev->dev.hdc;
339 release_dc_ptr( dc );
340 return ret;
343 /**********************************************************************
344 * CreateMetaFileA (GDI32.@)
346 * See CreateMetaFileW.
348 HDC WINAPI CreateMetaFileA(LPCSTR filename)
350 LPWSTR filenameW;
351 DWORD len;
352 HDC hReturnDC;
354 if (!filename) return CreateMetaFileW(NULL);
356 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
357 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
358 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
360 hReturnDC = CreateMetaFileW(filenameW);
362 HeapFree( GetProcessHeap(), 0, filenameW );
364 return hReturnDC;
368 /**********************************************************************
369 * MFDRV_CloseMetaFile
371 static DC *MFDRV_CloseMetaFile( HDC hdc )
373 DC *dc;
374 METAFILEDRV_PDEVICE *physDev;
376 TRACE("(%p)\n", hdc );
378 if (!(dc = get_dc_ptr( hdc ))) return NULL;
379 if (dc->header.type != OBJ_METADC)
381 release_dc_ptr( dc );
382 return NULL;
384 if (dc->refcount != 1)
386 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
387 release_dc_ptr( dc );
388 return NULL;
390 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
392 /* Construct the end of metafile record - this is documented
393 * in SDK Knowledgebase Q99334.
396 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
398 free_dc_ptr( dc );
399 return 0;
402 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
404 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
405 free_dc_ptr( dc );
406 return 0;
409 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
410 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
411 NULL, NULL)) {
412 free_dc_ptr( dc );
413 return 0;
415 CloseHandle(physDev->hFile);
416 physDev->mh->mtType = METAFILE_DISK;
419 return dc;
422 /******************************************************************
423 * CloseMetaFile (GDI32.@)
425 * Stop recording graphics operations in metafile associated with
426 * hdc and retrieve metafile.
428 * PARAMS
429 * hdc [I] Metafile DC to close
431 * RETURNS
432 * Handle of newly created metafile on success, NULL on failure.
434 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
436 HMETAFILE hmf;
437 METAFILEDRV_PDEVICE *physDev;
438 DC *dc = MFDRV_CloseMetaFile(hdc);
439 if (!dc) return 0;
440 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
442 /* Now allocate a global handle for the metafile */
444 hmf = MF_Create_HMETAFILE( physDev->mh );
446 physDev->mh = NULL; /* So it won't be deleted */
447 free_dc_ptr( dc );
448 return hmf;
452 /******************************************************************
453 * MFDRV_WriteRecord
455 * Warning: this function can change the pointer to the metafile header.
457 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
459 DWORD len, size;
460 METAHEADER *mh;
461 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
463 switch(physDev->mh->mtType)
465 case METAFILE_MEMORY:
466 len = physDev->mh->mtSize * 2 + rlen;
467 /* reallocate memory if needed */
468 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
469 if (len > size)
471 /*expand size*/
472 size += size / 2 + rlen;
473 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
474 if (!mh) return FALSE;
475 physDev->mh = mh;
476 TRACE("Reallocated metafile: new size is %d\n",size);
478 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
479 break;
480 case METAFILE_DISK:
481 TRACE("Writing record to disk\n");
482 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
483 return FALSE;
484 break;
485 default:
486 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
487 return FALSE;
490 physDev->mh->mtSize += rlen / 2;
491 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
492 return TRUE;
496 /******************************************************************
497 * MFDRV_MetaParam0
500 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
502 char buffer[8];
503 METARECORD *mr = (METARECORD *)&buffer;
505 mr->rdSize = 3;
506 mr->rdFunction = func;
507 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
511 /******************************************************************
512 * MFDRV_MetaParam1
514 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
516 char buffer[8];
517 METARECORD *mr = (METARECORD *)&buffer;
518 WORD *params = mr->rdParm;
520 mr->rdSize = 4;
521 mr->rdFunction = func;
522 params[0] = param1;
523 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
527 /******************************************************************
528 * MFDRV_MetaParam2
530 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
532 char buffer[10];
533 METARECORD *mr = (METARECORD *)&buffer;
534 WORD *params = mr->rdParm;
536 mr->rdSize = 5;
537 mr->rdFunction = func;
538 params[0] = param2;
539 params[1] = param1;
540 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
544 /******************************************************************
545 * MFDRV_MetaParam4
548 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
549 short param3, short param4)
551 char buffer[14];
552 METARECORD *mr = (METARECORD *)&buffer;
553 WORD *params = mr->rdParm;
555 mr->rdSize = 7;
556 mr->rdFunction = func;
557 params[0] = param4;
558 params[1] = param3;
559 params[2] = param2;
560 params[3] = param1;
561 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
565 /******************************************************************
566 * MFDRV_MetaParam6
569 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
570 short param3, short param4, short param5, short param6)
572 char buffer[18];
573 METARECORD *mr = (METARECORD *)&buffer;
574 WORD *params = mr->rdParm;
576 mr->rdSize = 9;
577 mr->rdFunction = func;
578 params[0] = param6;
579 params[1] = param5;
580 params[2] = param4;
581 params[3] = param3;
582 params[4] = param2;
583 params[5] = param1;
584 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
588 /******************************************************************
589 * MFDRV_MetaParam8
591 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
592 short param3, short param4, short param5,
593 short param6, short param7, short param8)
595 char buffer[22];
596 METARECORD *mr = (METARECORD *)&buffer;
597 WORD *params = mr->rdParm;
599 mr->rdSize = 11;
600 mr->rdFunction = func;
601 params[0] = param8;
602 params[1] = param7;
603 params[2] = param6;
604 params[3] = param5;
605 params[4] = param4;
606 params[5] = param3;
607 params[6] = param2;
608 params[7] = param1;
609 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);