init.c 20.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Metafile driver initialisation functions
 *
 * Copyright 1996 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22 23
#include <stdarg.h>
#include <string.h>

24
#include "windef.h"
25
#include "winbase.h"
26
#include "wine/winbase16.h"
Michael Stefaniuc's avatar
Michael Stefaniuc committed
27
#include "wownt32.h"
28
#include "gdi_private.h"
29
#include "mfdrv/metafiledrv.h"
30
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31

32
WINE_DEFAULT_DEBUG_CHANNEL(metafile);
33

Alexandre Julliard's avatar
Alexandre Julliard committed
34 35
static const DC_FUNCTIONS MFDRV_Funcs =
{
36
    NULL,                            /* pAbortDoc */
37
    MFDRV_AbortPath,                 /* pAbortPath */
38
    NULL,                            /* pAlphaBlend */
39
    NULL,                            /* pAngleArc */
Alexandre Julliard's avatar
Alexandre Julliard committed
40
    MFDRV_Arc,                       /* pArc */
41 42
    NULL,                            /* pArcTo */
    MFDRV_BeginPath,                 /* pBeginPath */
Alexandre Julliard's avatar
Alexandre Julliard committed
43
    MFDRV_BitBlt,                    /* pBitBlt */
Lionel Ulmer's avatar
Lionel Ulmer committed
44
    NULL,                            /* pChoosePixelFormat */
Alexandre Julliard's avatar
Alexandre Julliard committed
45
    MFDRV_Chord,                     /* pChord */
46
    MFDRV_CloseFigure,               /* pCloseFigure */
47
    NULL,                            /* pCreateBitmap */
48
    NULL,                            /* pCreateDC */
49
    NULL,                            /* pCreateDIBSection */
50
    NULL,                            /* pDeleteBitmap */
51
    NULL,                            /* pDeleteDC */
52
    MFDRV_DeleteObject,              /* pDeleteObject */
Lionel Ulmer's avatar
Lionel Ulmer committed
53
    NULL,                            /* pDescribePixelFormat */
54
    NULL,                            /* pDeviceCapabilities */
Alexandre Julliard's avatar
Alexandre Julliard committed
55
    MFDRV_Ellipse,                   /* pEllipse */
56 57
    NULL,                            /* pEndDoc */
    NULL,                            /* pEndPage */
58
    MFDRV_EndPath,                   /* pEndPath */
Alexandre Julliard's avatar
Alexandre Julliard committed
59
    NULL,                            /* pEnumDeviceFonts */
60
    MFDRV_ExcludeClipRect,           /* pExcludeClipRect */
61
    NULL,                            /* pExtDeviceMode */
62
    MFDRV_ExtEscape,                 /* pExtEscape */
Alexandre Julliard's avatar
Alexandre Julliard committed
63
    MFDRV_ExtFloodFill,              /* pExtFloodFill */
64
    MFDRV_ExtSelectClipRgn,          /* pExtSelectClipRgn */
Alexandre Julliard's avatar
Alexandre Julliard committed
65
    MFDRV_ExtTextOut,                /* pExtTextOut */
66
    MFDRV_FillPath,                  /* pFillPath */
67
    MFDRV_FillRgn,                   /* pFillRgn */
68
    MFDRV_FlattenPath,               /* pFlattenPath */
69
    MFDRV_FrameRgn,                  /* pFrameRgn */
70
    NULL,                            /* pGdiComment */
71
    NULL,                            /* pGetBitmapBits */
Alexandre Julliard's avatar
Alexandre Julliard committed
72
    NULL,                            /* pGetCharWidth */
73
    NULL,                            /* pGetDCOrgEx */
74 75
    NULL,                            /* pGetDIBColorTable */
    NULL,                            /* pGetDIBits */
76
    MFDRV_GetDeviceCaps,             /* pGetDeviceCaps */
77
    NULL,                            /* pGetDeviceGammaRamp */
78
    NULL,                            /* pGetICMProfile */
79
    NULL,                            /* pGetNearestColor */
80
    NULL,                            /* pGetPixel */
Lionel Ulmer's avatar
Lionel Ulmer committed
81
    NULL,                            /* pGetPixelFormat */
82
    NULL,                            /* pGetSystemPaletteEntries */
83
    NULL,                            /* pGetTextExtentExPoint */
Alexandre Julliard's avatar
Alexandre Julliard committed
84
    NULL,                            /* pGetTextMetrics */
85 86
    MFDRV_IntersectClipRect,         /* pIntersectClipRect */
    MFDRV_InvertRgn,                 /* pInvertRgn */
Alexandre Julliard's avatar
Alexandre Julliard committed
87
    MFDRV_LineTo,                    /* pLineTo */
88
    NULL,                            /* pModifyWorldTransform */
89
    MFDRV_MoveTo,                    /* pMoveTo */
90
    MFDRV_OffsetClipRgn,             /* pOffsetClipRgn */
Alexandre Julliard's avatar
Alexandre Julliard committed
91 92
    MFDRV_OffsetViewportOrg,         /* pOffsetViewportOrg */
    MFDRV_OffsetWindowOrg,           /* pOffsetWindowOrg */
Alexandre Julliard's avatar
Alexandre Julliard committed
93
    MFDRV_PaintRgn,                  /* pPaintRgn */
Alexandre Julliard's avatar
Alexandre Julliard committed
94
    MFDRV_PatBlt,                    /* pPatBlt */
Alexandre Julliard's avatar
Alexandre Julliard committed
95
    MFDRV_Pie,                       /* pPie */
96 97
    MFDRV_PolyBezier,                /* pPolyBezier */
    MFDRV_PolyBezierTo,              /* pPolyBezierTo */
98
    NULL,                            /* pPolyDraw */
Alexandre Julliard's avatar
Alexandre Julliard committed
99
    MFDRV_PolyPolygon,               /* pPolyPolygon */
Alexandre Julliard's avatar
Alexandre Julliard committed
100
    NULL,                            /* pPolyPolyline */
Alexandre Julliard's avatar
Alexandre Julliard committed
101 102
    MFDRV_Polygon,                   /* pPolygon */
    MFDRV_Polyline,                  /* pPolyline */
103
    NULL,                            /* pPolylineTo */
104
    NULL,                            /* pRealizeDefaultPalette */
105
    MFDRV_RealizePalette,            /* pRealizePalette */
Alexandre Julliard's avatar
Alexandre Julliard committed
106
    MFDRV_Rectangle,                 /* pRectangle */
107
    NULL,                            /* pResetDC */
108
    MFDRV_RestoreDC,                 /* pRestoreDC */
Alexandre Julliard's avatar
Alexandre Julliard committed
109
    MFDRV_RoundRect,                 /* pRoundRect */
110
    MFDRV_SaveDC,                    /* pSaveDC */
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112
    MFDRV_ScaleViewportExt,          /* pScaleViewportExt */
    MFDRV_ScaleWindowExt,            /* pScaleWindowExt */
113 114
    MFDRV_SelectBitmap,              /* pSelectBitmap */
    MFDRV_SelectBrush,               /* pSelectBrush */
115
    MFDRV_SelectClipPath,            /* pSelectClipPath */
116
    MFDRV_SelectFont,                /* pSelectFont */
117
    MFDRV_SelectPalette,             /* pSelectPalette */
118
    MFDRV_SelectPen,                 /* pSelectPen */
119
    NULL,                            /* pSetArcDirection */
120
    NULL,                            /* pSetBitmapBits */
Alexandre Julliard's avatar
Alexandre Julliard committed
121
    MFDRV_SetBkColor,                /* pSetBkColor */
122
    MFDRV_SetBkMode,                 /* pSetBkMode */
123
    NULL,                            /* pSetDCBrushColor*/
124
    NULL,                            /* pSetDCOrg */
125
    NULL,                            /* pSetDCPenColor*/
126 127 128
    NULL,                            /* pSetDIBColorTable */
    NULL,                            /* pSetDIBits */
    MFDRV_SetDIBitsToDevice,         /* pSetDIBitsToDevice */
Alexandre Julliard's avatar
Alexandre Julliard committed
129
    NULL,                            /* pSetDeviceClipping */
130
    NULL,                            /* pSetDeviceGammaRamp */
Alexandre Julliard's avatar
Alexandre Julliard committed
131
    MFDRV_SetMapMode,                /* pSetMapMode */
132
    MFDRV_SetMapperFlags,            /* pSetMapperFlags */
Alexandre Julliard's avatar
Alexandre Julliard committed
133
    MFDRV_SetPixel,                  /* pSetPixel */
Lionel Ulmer's avatar
Lionel Ulmer committed
134
    NULL,                            /* pSetPixelFormat */
135 136 137 138 139 140
    MFDRV_SetPolyFillMode,           /* pSetPolyFillMode */
    MFDRV_SetROP2,                   /* pSetROP2 */
    MFDRV_SetRelAbs,                 /* pSetRelAbs */
    MFDRV_SetStretchBltMode,         /* pSetStretchBltMode */
    MFDRV_SetTextAlign,              /* pSetTextAlign */
    MFDRV_SetTextCharacterExtra,     /* pSetTextCharacterExtra */
Alexandre Julliard's avatar
Alexandre Julliard committed
141
    MFDRV_SetTextColor,              /* pSetTextColor */
142
    MFDRV_SetTextJustification,      /* pSetTextJustification */
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144 145 146
    MFDRV_SetViewportExt,            /* pSetViewportExt */
    MFDRV_SetViewportOrg,            /* pSetViewportOrg */
    MFDRV_SetWindowExt,              /* pSetWindowExt */
    MFDRV_SetWindowOrg,              /* pSetWindowOrg */
147
    NULL,                            /* pSetWorldTransform */
148 149
    NULL,                            /* pStartDoc */
    NULL,                            /* pStartPage */
Alexandre Julliard's avatar
Alexandre Julliard committed
150
    MFDRV_StretchBlt,                /* pStretchBlt */
151 152 153
    MFDRV_StretchDIBits,             /* pStretchDIBits */
    MFDRV_StrokeAndFillPath,         /* pStrokeAndFillPath */
    MFDRV_StrokePath,                /* pStrokePath */
Lionel Ulmer's avatar
Lionel Ulmer committed
154
    NULL,                            /* pSwapBuffers */
155
    NULL,                            /* pUnrealizePalette */
156
    MFDRV_WidenPath                  /* pWidenPath */
Alexandre Julliard's avatar
Alexandre Julliard committed
157 158 159 160 161 162 163 164 165 166 167
};



/**********************************************************************
 *	     MFDRV_AllocMetaFile
 */
static DC *MFDRV_AllocMetaFile(void)
{
    DC *dc;
    METAFILEDRV_PDEVICE *physDev;
168

169
    if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
170

171
    physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
Alexandre Julliard's avatar
Alexandre Julliard committed
172 173
    if (!physDev)
    {
174
        free_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
175 176
        return NULL;
    }
177 178
    dc->physDev = (PHYSDEV)physDev;
    physDev->hdc = dc->hSelf;
Alexandre Julliard's avatar
Alexandre Julliard committed
179

180
    if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
Alexandre Julliard's avatar
Alexandre Julliard committed
181
    {
182
        HeapFree( GetProcessHeap(), 0, physDev );
183
        free_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
184 185 186
        return NULL;
    }

187 188 189 190
    physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
    physDev->handles_size = HANDLE_LIST_INC;
    physDev->cur_handles = 0;

191
    physDev->hFile = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
192 193 194 195 196 197 198 199 200 201 202 203 204

    physDev->mh->mtHeaderSize   = sizeof(METAHEADER) / sizeof(WORD);
    physDev->mh->mtVersion      = 0x0300;
    physDev->mh->mtSize         = physDev->mh->mtHeaderSize;
    physDev->mh->mtNoObjects    = 0;
    physDev->mh->mtMaxRecord    = 0;
    physDev->mh->mtNoParameters = 0;

    return dc;
}


/**********************************************************************
205
 *	     MFDRV_DeleteDC
Alexandre Julliard's avatar
Alexandre Julliard committed
206
 */
207
static BOOL MFDRV_DeleteDC( DC *dc )
Alexandre Julliard's avatar
Alexandre Julliard committed
208
{
209
    METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
210
    DWORD index;
211

212
    HeapFree( GetProcessHeap(), 0, physDev->mh );
213 214 215 216
    for(index = 0; index < physDev->handles_size; index++)
        if(physDev->handles[index])
            GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
    HeapFree( GetProcessHeap(), 0, physDev->handles );
217
    HeapFree( GetProcessHeap(), 0, physDev );
Alexandre Julliard's avatar
Alexandre Julliard committed
218
    dc->physDev = NULL;
219
    free_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
220 221 222
    return TRUE;
}

223

Alexandre Julliard's avatar
Alexandre Julliard committed
224
/**********************************************************************
225
 *	     CreateMetaFileW   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
226 227 228 229
 *
 *  Create a new DC and associate it with a metafile. Pass a filename
 *  to create a disk-based metafile, NULL to create a memory metafile.
 *
230 231 232
 * PARAMS
 *  filename [I] Filename of disk metafile
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
233 234
 * RETURNS
 *  A handle to the metafile DC if successful, NULL on failure.
Alexandre Julliard's avatar
Alexandre Julliard committed
235
 */
236
HDC WINAPI CreateMetaFileW( LPCWSTR filename )
Alexandre Julliard's avatar
Alexandre Julliard committed
237
{
238
    HDC ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
239 240
    DC *dc;
    METAFILEDRV_PDEVICE *physDev;
241
    HANDLE hFile;
Alexandre Julliard's avatar
Alexandre Julliard committed
242

243
    TRACE("%s\n", debugstr_w(filename) );
Alexandre Julliard's avatar
Alexandre Julliard committed
244 245 246

    if (!(dc = MFDRV_AllocMetaFile())) return 0;
    physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
247

Alexandre Julliard's avatar
Alexandre Julliard committed
248 249 250
    if (filename)  /* disk based metafile */
    {
        physDev->mh->mtType = METAFILE_DISK;
251
        if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
252
				CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
253
            MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
254 255
            return 0;
        }
256
        if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
257
			NULL )) {
258
            MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
259 260
            return 0;
	}
261 262 263
	physDev->hFile = hFile;

	/* Grow METAHEADER to include filename */
264
	physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
265 266 267
    }
    else  /* memory based metafile */
	physDev->mh->mtType = METAFILE_MEMORY;
268

269
    TRACE("returning %p\n", dc->hSelf);
270
    ret = dc->hSelf;
271
    release_dc_ptr( dc );
272
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
273 274
}

275
/**********************************************************************
276
 *          CreateMetaFileA   (GDI32.@)
277 278
 *
 * See CreateMetaFileW.
279
 */
280
HDC WINAPI CreateMetaFileA(LPCSTR filename)
281
{
282
    LPWSTR filenameW;
283
    DWORD len;
284
    HDC hReturnDC;
285

286
    if (!filename) return CreateMetaFileW(NULL);
287

288 289 290
    len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
    filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
    MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
291

292
    hReturnDC = CreateMetaFileW(filenameW);
293

294
    HeapFree( GetProcessHeap(), 0, filenameW );
295 296 297 298

    return hReturnDC;
}

299 300 301 302

/**********************************************************************
 *          MFDRV_CloseMetaFile
 */
303
static DC *MFDRV_CloseMetaFile( HDC hdc )
Alexandre Julliard's avatar
Alexandre Julliard committed
304 305 306
{
    DC *dc;
    METAFILEDRV_PDEVICE *physDev;
307

308
    TRACE("(%p)\n", hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
309

310
    if (!(dc = get_dc_ptr( hdc ))) return NULL;
311 312
    if (GDIMAGIC(dc->header.wMagic) != METAFILE_DC_MAGIC)
    {
313
        release_dc_ptr( dc );
314 315
        return NULL;
    }
316 317 318
    if (dc->refcount != 1)
    {
        FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
319
        release_dc_ptr( dc );
320 321
        return NULL;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
322 323 324 325 326 327
    physDev = (METAFILEDRV_PDEVICE *)dc->physDev;

    /* Construct the end of metafile record - this is documented
     * in SDK Knowledgebase Q99334.
     */

328
    if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
Alexandre Julliard's avatar
Alexandre Julliard committed
329
    {
330
        MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
331
	return 0;
332
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
333 334 335

    if (physDev->mh->mtType == METAFILE_DISK)  /* disk based metafile */
    {
336
        if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
337
            MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
338 339
            return 0;
        }
340 341 342 343

	physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
        if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
                       sizeof(*physDev->mh), NULL, NULL)) {
344
            MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
345 346
            return 0;
        }
347 348
        CloseHandle(physDev->hFile);
	physDev->mh->mtType = METAFILE_DISK;
Alexandre Julliard's avatar
Alexandre Julliard committed
349 350
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
351 352 353
    return dc;
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
/******************************************************************
 *         MF_Create_HMETATFILE16
 *
 * Creates a HMETAFILE16 object from a METAHEADER
 *
 * HMETAFILE16s are Global memory handles.
 */
HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
{
    HMETAFILE16 hmf;
    DWORD size = mh->mtSize * sizeof(WORD);

    hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
    if(hmf)
    {
	METAHEADER *mh_dest = GlobalLock16(hmf);
	memcpy(mh_dest, mh, size);
	GlobalUnlock16(hmf);
    }
    HeapFree(GetProcessHeap(), 0, mh);
    return hmf;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
377
/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
378
 *	     CloseMetaFile     (GDI.126)
379 380 381
 *
 * PARAMS
 *  hdc [I] Metafile DC to close 
Alexandre Julliard's avatar
Alexandre Julliard committed
382
 */
383
HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
384 385 386
{
    HMETAFILE16 hmf;
    METAFILEDRV_PDEVICE *physDev;
Michael Stefaniuc's avatar
Michael Stefaniuc committed
387
    DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
Alexandre Julliard's avatar
Alexandre Julliard committed
388 389 390
    if (!dc) return 0;
    physDev = (METAFILEDRV_PDEVICE *)dc->physDev;

Alexandre Julliard's avatar
Alexandre Julliard committed
391 392
    /* Now allocate a global handle for the metafile */

393 394
    hmf = MF_Create_HMETAFILE16( physDev->mh );

Alexandre Julliard's avatar
Alexandre Julliard committed
395
    physDev->mh = NULL;  /* So it won't be deleted */
396
    MFDRV_DeleteDC( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
397 398 399 400
    return hmf;
}

/******************************************************************
401
 *	     CloseMetaFile   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
402
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
403 404
 *  Stop recording graphics operations in metafile associated with
 *  hdc and retrieve metafile.
Alexandre Julliard's avatar
Alexandre Julliard committed
405
 *
406 407 408
 * PARAMS
 *  hdc [I] Metafile DC to close 
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
409 410 411
 * RETURNS
 *  Handle of newly created metafile on success, NULL on failure.
 */
412
HMETAFILE WINAPI CloseMetaFile(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
413
{
414 415 416 417 418 419 420 421 422 423 424
    HMETAFILE hmf;
    METAFILEDRV_PDEVICE *physDev;
    DC *dc = MFDRV_CloseMetaFile(hdc);
    if (!dc) return 0;
    physDev = (METAFILEDRV_PDEVICE *)dc->physDev;

    /* Now allocate a global handle for the metafile */

    hmf = MF_Create_HMETAFILE( physDev->mh );

    physDev->mh = NULL;  /* So it won't be deleted */
425
    MFDRV_DeleteDC( dc );
426
    return hmf;
Alexandre Julliard's avatar
Alexandre Julliard committed
427 428 429 430
}


/******************************************************************
431 432 433
 *         MFDRV_WriteRecord
 *
 * Warning: this function can change the pointer to the metafile header.
Alexandre Julliard's avatar
Alexandre Julliard committed
434
 */
435
BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
Alexandre Julliard's avatar
Alexandre Julliard committed
436
{
437
    DWORD len, size;
438
    METAHEADER *mh;
439
    METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
440 441 442 443 444

    switch(physDev->mh->mtType)
    {
    case METAFILE_MEMORY:
	len = physDev->mh->mtSize * 2 + rlen;
445 446 447 448 449 450 451 452 453
	/* reallocate memory if needed */
        size = HeapSize( GetProcessHeap(), 0, physDev->mh );
        if (len > size)
        {
            /*expand size*/
            size += size / 2 + rlen;
            mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
            if (!mh) return FALSE;
            physDev->mh = mh;
454
            TRACE("Reallocated metafile: new size is %d\n",size);
455
        }
456 457 458
	memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
        break;
    case METAFILE_DISK:
459
        TRACE("Writing record to disk\n");
460 461 462 463
	if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
	    return FALSE;
        break;
    default:
464
        ERR("Unknown metafile type %d\n", physDev->mh->mtType );
465 466 467 468
        return FALSE;
    }

    physDev->mh->mtSize += rlen / 2;
469
    physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
470
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
471
}
Alexandre Julliard's avatar
Alexandre Julliard committed
472

473

Alexandre Julliard's avatar
Alexandre Julliard committed
474
/******************************************************************
475 476 477
 *         MFDRV_MetaParam0
 */

478
BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
479 480 481
{
    char buffer[8];
    METARECORD *mr = (METARECORD *)&buffer;
482

483 484
    mr->rdSize = 3;
    mr->rdFunction = func;
485
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
486 487 488 489 490 491
}


/******************************************************************
 *         MFDRV_MetaParam1
 */
492
BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
493 494 495
{
    char buffer[8];
    METARECORD *mr = (METARECORD *)&buffer;
496

497 498 499
    mr->rdSize = 4;
    mr->rdFunction = func;
    *(mr->rdParm) = param1;
500
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
501 502 503 504 505 506
}


/******************************************************************
 *         MFDRV_MetaParam2
 */
507
BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
508 509 510
{
    char buffer[10];
    METARECORD *mr = (METARECORD *)&buffer;
511

512 513 514 515
    mr->rdSize = 5;
    mr->rdFunction = func;
    *(mr->rdParm) = param2;
    *(mr->rdParm + 1) = param1;
516
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
517 518 519 520 521 522 523
}


/******************************************************************
 *         MFDRV_MetaParam4
 */

524
BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
525 526 527 528
		      short param3, short param4)
{
    char buffer[14];
    METARECORD *mr = (METARECORD *)&buffer;
529

530 531 532 533 534 535
    mr->rdSize = 7;
    mr->rdFunction = func;
    *(mr->rdParm) = param4;
    *(mr->rdParm + 1) = param3;
    *(mr->rdParm + 2) = param2;
    *(mr->rdParm + 3) = param1;
536
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
537 538 539 540 541 542 543
}


/******************************************************************
 *         MFDRV_MetaParam6
 */

544
BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
545 546 547 548
		      short param3, short param4, short param5, short param6)
{
    char buffer[18];
    METARECORD *mr = (METARECORD *)&buffer;
549

550 551 552 553 554 555 556 557
    mr->rdSize = 9;
    mr->rdFunction = func;
    *(mr->rdParm) = param6;
    *(mr->rdParm + 1) = param5;
    *(mr->rdParm + 2) = param4;
    *(mr->rdParm + 3) = param3;
    *(mr->rdParm + 4) = param2;
    *(mr->rdParm + 5) = param1;
558
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
559 560 561 562 563
}


/******************************************************************
 *         MFDRV_MetaParam8
Alexandre Julliard's avatar
Alexandre Julliard committed
564
 */
565
BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
566 567 568 569 570
		      short param3, short param4, short param5,
		      short param6, short param7, short param8)
{
    char buffer[22];
    METARECORD *mr = (METARECORD *)&buffer;
571

572 573 574 575 576 577 578 579 580 581
    mr->rdSize = 11;
    mr->rdFunction = func;
    *(mr->rdParm) = param8;
    *(mr->rdParm + 1) = param7;
    *(mr->rdParm + 2) = param6;
    *(mr->rdParm + 3) = param5;
    *(mr->rdParm + 4) = param4;
    *(mr->rdParm + 5) = param3;
    *(mr->rdParm + 6) = param2;
    *(mr->rdParm + 7) = param1;
582
    return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
583 584
}

Alexandre Julliard's avatar
Alexandre Julliard committed
585

586 587 588 589 590 591 592 593
/**********************************************************************
 *           MFDRV_ExtEscape
 */
INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
                     INT cbOutput, LPVOID out_data )
{
    METARECORD *mr;
    DWORD len;
594 595
    INT ret;

596 597
    if (cbOutput) return 0;  /* escapes that require output cannot work in metafiles */

598 599 600 601 602 603 604 605 606 607
    len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
    mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
    mr->rdSize = len / 2;
    mr->rdFunction = META_ESCAPE;
    mr->rdParm[0] = nEscape;
    mr->rdParm[1] = cbInput;
    memcpy(&(mr->rdParm[2]), in_data, cbInput);
    ret = MFDRV_WriteRecord( dev, mr, len);
    HeapFree(GetProcessHeap(), 0, mr);
    return ret;
608
}
609

610

611 612 613 614 615 616 617 618 619 620
/******************************************************************
 *         MFDRV_GetDeviceCaps
 *
 *A very simple implementation that returns DT_METAFILE
 */
INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
{
    switch(cap)
    {
    case TECHNOLOGY:
621 622 623
        return DT_METAFILE;
    case TEXTCAPS:
        return 0;
624
    default:
625
        TRACE(" unsupported capability %d, will return 0\n", cap );
626
    }
627
    return 0;
628
}