Better separation of the palette functions.
[wine/multimedia.git] / dlls / gdi / mfdrv / init.c
blobf926186a697b3f6349fb32b8c575f9e8383b84d6
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "windef.h"
22 #include "wine/winbase16.h"
23 #include "gdi.h"
24 #include "metafile.h"
25 #include "mfdrv/metafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
30 #include <string.h>
32 static const DC_FUNCTIONS MFDRV_Funcs =
34 NULL, /* pAbortDoc */
35 MFDRV_AbortPath, /* pAbortPath */
36 NULL, /* pAngleArc */
37 MFDRV_Arc, /* pArc */
38 NULL, /* pArcTo */
39 MFDRV_BeginPath, /* pBeginPath */
40 MFDRV_BitBlt, /* pBitBlt */
41 NULL, /* pChoosePixelFormat */
42 MFDRV_Chord, /* pChord */
43 MFDRV_CloseFigure, /* pCloseFigure */
44 NULL, /* pCreateBitmap */
45 NULL, /* pCreateDC */
46 NULL, /* pCreateDIBSection */
47 NULL, /* pDeleteBitmap */
48 NULL, /* pDeleteDC */
49 NULL, /* pDescribePixelFormat */
50 NULL, /* pDeviceCapabilities */
51 MFDRV_Ellipse, /* pEllipse */
52 NULL, /* pEndDoc */
53 NULL, /* pEndPage */
54 MFDRV_EndPath, /* pEndPath */
55 NULL, /* pEnumDeviceFonts */
56 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
57 NULL, /* pExtDeviceMode */
58 MFDRV_ExtEscape, /* pExtEscape */
59 MFDRV_ExtFloodFill, /* pExtFloodFill */
60 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
61 MFDRV_ExtTextOut, /* pExtTextOut */
62 MFDRV_FillPath, /* pFillPath */
63 MFDRV_FillRgn, /* pFillRgn */
64 MFDRV_FlattenPath, /* pFlattenPath */
65 MFDRV_FrameRgn, /* pFrameRgn */
66 NULL, /* pGetBitmapBits */
67 NULL, /* pGetCharWidth */
68 NULL, /* pGetDCOrgEx */
69 NULL, /* pGetDIBColorTable */
70 NULL, /* pGetDIBits */
71 NULL, /* pGetDeviceCaps */
72 NULL, /* pGetDeviceGammaRamp */
73 NULL, /* pGetNearestColor */
74 NULL, /* pGetPixel */
75 NULL, /* pGetPixelFormat */
76 NULL, /* pGetSystemPaletteEntries */
77 NULL, /* pGetTextExtentPoint */
78 NULL, /* pGetTextMetrics */
79 MFDRV_IntersectClipRect, /* pIntersectClipRect */
80 MFDRV_InvertRgn, /* pInvertRgn */
81 MFDRV_LineTo, /* pLineTo */
82 MFDRV_MoveTo, /* pMoveTo */
83 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
84 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
85 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
86 MFDRV_PaintRgn, /* pPaintRgn */
87 MFDRV_PatBlt, /* pPatBlt */
88 MFDRV_Pie, /* pPie */
89 MFDRV_PolyBezier, /* pPolyBezier */
90 MFDRV_PolyBezierTo, /* pPolyBezierTo */
91 NULL, /* pPolyDraw */
92 MFDRV_PolyPolygon, /* pPolyPolygon */
93 NULL, /* pPolyPolyline */
94 MFDRV_Polygon, /* pPolygon */
95 MFDRV_Polyline, /* pPolyline */
96 NULL, /* pPolylineTo */
97 NULL, /* pRealizeDefaultPalette */
98 NULL, /* pRealizePalette */
99 MFDRV_Rectangle, /* pRectangle */
100 NULL, /* pResetDC */
101 MFDRV_RestoreDC, /* pRestoreDC */
102 MFDRV_RoundRect, /* pRoundRect */
103 MFDRV_SaveDC, /* pSaveDC */
104 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
105 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
106 MFDRV_SelectBitmap, /* pSelectBitmap */
107 MFDRV_SelectBrush, /* pSelectBrush */
108 MFDRV_SelectClipPath, /* pSelectClipPath */
109 MFDRV_SelectFont, /* pSelectFont */
110 NULL, /* pSelectPalette */
111 MFDRV_SelectPen, /* pSelectPen */
112 NULL, /* pSetBitmapBits */
113 MFDRV_SetBkColor, /* pSetBkColor */
114 MFDRV_SetBkMode, /* pSetBkMode */
115 NULL, /* pSetDIBColorTable */
116 NULL, /* pSetDIBits */
117 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
118 NULL, /* pSetDeviceClipping */
119 NULL, /* pSetDeviceGammaRamp */
120 MFDRV_SetMapMode, /* pSetMapMode */
121 MFDRV_SetMapperFlags, /* pSetMapperFlags */
122 MFDRV_SetPixel, /* pSetPixel */
123 NULL, /* pSetPixelFormat */
124 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
125 MFDRV_SetROP2, /* pSetROP2 */
126 MFDRV_SetRelAbs, /* pSetRelAbs */
127 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
128 MFDRV_SetTextAlign, /* pSetTextAlign */
129 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
130 MFDRV_SetTextColor, /* pSetTextColor */
131 MFDRV_SetTextJustification, /* pSetTextJustification */
132 MFDRV_SetViewportExt, /* pSetViewportExt */
133 MFDRV_SetViewportOrg, /* pSetViewportOrg */
134 MFDRV_SetWindowExt, /* pSetWindowExt */
135 MFDRV_SetWindowOrg, /* pSetWindowOrg */
136 NULL, /* pStartDoc */
137 NULL, /* pStartPage */
138 MFDRV_StretchBlt, /* pStretchBlt */
139 MFDRV_StretchDIBits, /* pStretchDIBits */
140 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
141 MFDRV_StrokePath, /* pStrokePath */
142 NULL, /* pSwapBuffers */
143 MFDRV_WidenPath /* pWidenPath */
148 /**********************************************************************
149 * MFDRV_AllocMetaFile
151 static DC *MFDRV_AllocMetaFile(void)
153 DC *dc;
154 METAFILEDRV_PDEVICE *physDev;
156 if (!(dc = DC_AllocDC( &MFDRV_Funcs ))) return NULL;
157 dc->header.wMagic = METAFILE_DC_MAGIC;
159 physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
160 if (!physDev)
162 GDI_FreeObject( dc->hSelf, dc );
163 return NULL;
165 dc->physDev = (PHYSDEV)physDev;
166 physDev->hdc = dc->hSelf;
167 physDev->dc = dc;
169 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
171 HeapFree( GetProcessHeap(), 0, physDev );
172 GDI_FreeObject( dc->hSelf, dc );
173 return NULL;
176 physDev->nextHandle = 0;
177 physDev->hFile = 0;
179 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
180 physDev->mh->mtVersion = 0x0300;
181 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
182 physDev->mh->mtNoObjects = 0;
183 physDev->mh->mtMaxRecord = 0;
184 physDev->mh->mtNoParameters = 0;
186 return dc;
190 /**********************************************************************
191 * MFDRV_DeleteDC
193 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
195 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
196 DC *dc = physDev->dc;
198 if (physDev->mh) HeapFree( GetProcessHeap(), 0, physDev->mh );
199 HeapFree( GetProcessHeap(), 0, physDev );
200 dc->physDev = NULL;
201 GDI_FreeObject( dc->hSelf, dc );
202 return TRUE;
205 /**********************************************************************
206 * CreateMetaFile (GDI.125)
207 * CreateMetaFile16 (GDI32.@)
209 * Create a new DC and associate it with a metafile. Pass a filename
210 * to create a disk-based metafile, NULL to create a memory metafile.
212 * RETURNS
213 * A handle to the metafile DC if successful, NULL on failure.
215 HDC16 WINAPI CreateMetaFile16(
216 LPCSTR filename /* [in] Filename of disk metafile */
219 HDC ret;
220 DC *dc;
221 METAFILEDRV_PDEVICE *physDev;
222 HFILE hFile;
224 TRACE("'%s'\n", filename );
226 if (!(dc = MFDRV_AllocMetaFile())) return 0;
227 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
229 if (filename) /* disk based metafile */
231 physDev->mh->mtType = METAFILE_DISK;
232 if ((hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
233 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
234 MFDRV_DeleteDC( dc->physDev );
235 return 0;
237 if (!WriteFile( hFile, (LPSTR)physDev->mh, sizeof(*physDev->mh), NULL,
238 NULL )) {
239 MFDRV_DeleteDC( dc->physDev );
240 return 0;
242 physDev->hFile = hFile;
244 /* Grow METAHEADER to include filename */
245 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename);
247 else /* memory based metafile */
248 physDev->mh->mtType = METAFILE_MEMORY;
250 TRACE("returning %04x\n", dc->hSelf);
251 ret = dc->hSelf;
252 GDI_ReleaseObj( dc->hSelf );
253 return ret;
256 /**********************************************************************
257 * CreateMetaFileA (GDI32.@)
259 HDC WINAPI CreateMetaFileA(
260 LPCSTR filename /* [in] Filename of disk metafile */
263 return CreateMetaFile16( filename );
266 /**********************************************************************
267 * CreateMetaFileW (GDI32.@)
269 HDC WINAPI CreateMetaFileW(LPCWSTR filename)
271 LPSTR filenameA;
272 DWORD len;
273 HDC hReturnDC;
275 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
276 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
277 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
279 hReturnDC = CreateMetaFileA(filenameA);
281 HeapFree( GetProcessHeap(), 0, filenameA );
283 return hReturnDC;
287 /**********************************************************************
288 * MFDRV_CloseMetaFile
290 static DC *MFDRV_CloseMetaFile( HDC hdc )
292 DC *dc;
293 METAFILEDRV_PDEVICE *physDev;
295 TRACE("(%04x)\n", hdc );
297 if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
298 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
300 /* Construct the end of metafile record - this is documented
301 * in SDK Knowledgebase Q99334.
304 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
306 MFDRV_DeleteDC( dc->physDev );
307 return 0;
310 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
312 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
313 MFDRV_DeleteDC( dc->physDev );
314 return 0;
317 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
318 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
319 sizeof(*physDev->mh), NULL, NULL)) {
320 MFDRV_DeleteDC( dc->physDev );
321 return 0;
323 CloseHandle(physDev->hFile);
324 physDev->mh->mtType = METAFILE_DISK;
327 return dc;
330 /******************************************************************
331 * CloseMetaFile (GDI.126)
332 * CloseMetaFile16 (GDI32.@)
334 HMETAFILE16 WINAPI CloseMetaFile16(
335 HDC16 hdc /* [in] Metafile DC to close */
338 HMETAFILE16 hmf;
339 METAFILEDRV_PDEVICE *physDev;
340 DC *dc = MFDRV_CloseMetaFile(hdc);
341 if (!dc) return 0;
342 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
344 /* Now allocate a global handle for the metafile */
346 hmf = MF_Create_HMETAFILE16( physDev->mh );
348 physDev->mh = NULL; /* So it won't be deleted */
349 MFDRV_DeleteDC( dc->physDev );
350 return hmf;
353 /******************************************************************
354 * CloseMetaFile (GDI32.@)
356 * Stop recording graphics operations in metafile associated with
357 * hdc and retrieve metafile.
359 * RETURNS
360 * Handle of newly created metafile on success, NULL on failure.
362 HMETAFILE WINAPI CloseMetaFile(
363 HDC hdc /* [in] Metafile DC to close */
366 HMETAFILE hmf;
367 METAFILEDRV_PDEVICE *physDev;
368 DC *dc = MFDRV_CloseMetaFile(hdc);
369 if (!dc) return 0;
370 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
372 /* Now allocate a global handle for the metafile */
374 hmf = MF_Create_HMETAFILE( physDev->mh );
376 physDev->mh = NULL; /* So it won't be deleted */
377 MFDRV_DeleteDC( dc->physDev );
378 return hmf;
382 /******************************************************************
383 * MFDRV_WriteRecord
385 * Warning: this function can change the pointer to the metafile header.
387 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
389 DWORD len;
390 METAHEADER *mh;
391 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
393 switch(physDev->mh->mtType)
395 case METAFILE_MEMORY:
396 len = physDev->mh->mtSize * 2 + rlen;
397 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, len );
398 if (!mh) return FALSE;
399 physDev->mh = mh;
400 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
401 break;
402 case METAFILE_DISK:
403 TRACE("Writing record to disk\n");
404 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
405 return FALSE;
406 break;
407 default:
408 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
409 return FALSE;
412 physDev->mh->mtSize += rlen / 2;
413 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
414 return TRUE;
418 /******************************************************************
419 * MFDRV_MetaParam0
422 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
424 char buffer[8];
425 METARECORD *mr = (METARECORD *)&buffer;
427 mr->rdSize = 3;
428 mr->rdFunction = func;
429 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
433 /******************************************************************
434 * MFDRV_MetaParam1
436 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
438 char buffer[8];
439 METARECORD *mr = (METARECORD *)&buffer;
441 mr->rdSize = 4;
442 mr->rdFunction = func;
443 *(mr->rdParm) = param1;
444 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
448 /******************************************************************
449 * MFDRV_MetaParam2
451 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
453 char buffer[10];
454 METARECORD *mr = (METARECORD *)&buffer;
456 mr->rdSize = 5;
457 mr->rdFunction = func;
458 *(mr->rdParm) = param2;
459 *(mr->rdParm + 1) = param1;
460 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
464 /******************************************************************
465 * MFDRV_MetaParam4
468 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
469 short param3, short param4)
471 char buffer[14];
472 METARECORD *mr = (METARECORD *)&buffer;
474 mr->rdSize = 7;
475 mr->rdFunction = func;
476 *(mr->rdParm) = param4;
477 *(mr->rdParm + 1) = param3;
478 *(mr->rdParm + 2) = param2;
479 *(mr->rdParm + 3) = param1;
480 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
484 /******************************************************************
485 * MFDRV_MetaParam6
488 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
489 short param3, short param4, short param5, short param6)
491 char buffer[18];
492 METARECORD *mr = (METARECORD *)&buffer;
494 mr->rdSize = 9;
495 mr->rdFunction = func;
496 *(mr->rdParm) = param6;
497 *(mr->rdParm + 1) = param5;
498 *(mr->rdParm + 2) = param4;
499 *(mr->rdParm + 3) = param3;
500 *(mr->rdParm + 4) = param2;
501 *(mr->rdParm + 5) = param1;
502 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
506 /******************************************************************
507 * MFDRV_MetaParam8
509 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
510 short param3, short param4, short param5,
511 short param6, short param7, short param8)
513 char buffer[22];
514 METARECORD *mr = (METARECORD *)&buffer;
516 mr->rdSize = 11;
517 mr->rdFunction = func;
518 *(mr->rdParm) = param8;
519 *(mr->rdParm + 1) = param7;
520 *(mr->rdParm + 2) = param6;
521 *(mr->rdParm + 3) = param5;
522 *(mr->rdParm + 4) = param4;
523 *(mr->rdParm + 5) = param3;
524 *(mr->rdParm + 6) = param2;
525 *(mr->rdParm + 7) = param1;
526 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
530 /******************************************************************
531 * MFDRV_AddHandleDC
533 * Note: this function assumes that we never delete objects.
534 * If we do someday, we'll need to maintain a table to re-use deleted
535 * handles.
537 int MFDRV_AddHandleDC( PHYSDEV dev )
539 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
540 physDev->mh->mtNoObjects++;
541 return physDev->nextHandle++;
544 /**********************************************************************
545 * MFDRV_ExtEscape
547 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
548 INT cbOutput, LPVOID out_data )
550 METARECORD *mr;
551 DWORD len;
553 if(nEscape == MFCOMMENT) {
554 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
555 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
556 mr->rdSize = len / 2;
557 mr->rdFunction = META_ESCAPE;
558 mr->rdParm[0] = nEscape;
559 mr->rdParm[1] = cbInput;
560 memcpy(&(mr->rdParm[2]), in_data, cbInput);
561 MFDRV_WriteRecord( dev, mr, len);
562 HeapFree(GetProcessHeap(), 0, mr);
563 return 1;
565 return 0;