1 /*
2 * Metafile driver initialisation functions
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "wownt32.h"
28 #include "gdi_private.h"
29 #include "mfdrv/metafiledrv.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
33
34 static const DC_FUNCTIONS MFDRV_Funcs =
35 {
36 NULL, /* pAbortDoc */
37 MFDRV_AbortPath, /* pAbortPath */
38 NULL, /* pAlphaBlend */
39 NULL, /* pAngleArc */
40 MFDRV_Arc, /* pArc */
41 NULL, /* pArcTo */
42 MFDRV_BeginPath, /* pBeginPath */
43 MFDRV_BitBlt, /* pBitBlt */
44 NULL, /* pChoosePixelFormat */
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 */
53 NULL, /* pDescribePixelFormat */
54 NULL, /* pDeviceCapabilities */
55 MFDRV_Ellipse, /* pEllipse */
56 NULL, /* pEndDoc */
57 NULL, /* pEndPage */
58 MFDRV_EndPath, /* pEndPath */
59 NULL, /* pEnumDeviceFonts */
60 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
61 NULL, /* pExtDeviceMode */
62 MFDRV_ExtEscape, /* pExtEscape */
63 MFDRV_ExtFloodFill, /* pExtFloodFill */
64 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
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 */
72 NULL, /* pGetCharWidth */
73 NULL, /* pGetDCOrgEx */
74 NULL, /* pGetDIBColorTable */
75 NULL, /* pGetDIBits */
76 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
77 NULL, /* pGetDeviceGammaRamp */
78 NULL, /* pGetICMProfile */
79 NULL, /* pGetNearestColor */
80 NULL, /* pGetPixel */
81 NULL, /* pGetPixelFormat */
82 NULL, /* pGetSystemPaletteEntries */
83 NULL, /* pGetTextExtentExPoint */
84 NULL, /* pGetTextMetrics */
85 MFDRV_IntersectClipRect, /* pIntersectClipRect */
86 MFDRV_InvertRgn, /* pInvertRgn */
87 MFDRV_LineTo, /* pLineTo */
88 NULL, /* pModifyWorldTransform */
89 MFDRV_MoveTo, /* pMoveTo */
90 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
91 MFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
92 MFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
93 MFDRV_PaintRgn, /* pPaintRgn */
94 MFDRV_PatBlt, /* pPatBlt */
95 MFDRV_Pie, /* pPie */
96 MFDRV_PolyBezier, /* pPolyBezier */
97 MFDRV_PolyBezierTo, /* pPolyBezierTo */
98 NULL, /* pPolyDraw */
99 MFDRV_PolyPolygon, /* pPolyPolygon */
100 NULL, /* pPolyPolyline */
101 MFDRV_Polygon, /* pPolygon */
102 MFDRV_Polyline, /* pPolyline */
103 NULL, /* pPolylineTo */
104 NULL, /* pRealizeDefaultPalette */
105 MFDRV_RealizePalette, /* pRealizePalette */
106 MFDRV_Rectangle, /* pRectangle */
107 NULL, /* pResetDC */
108 MFDRV_RestoreDC, /* pRestoreDC */
109 MFDRV_RoundRect, /* pRoundRect */
110 MFDRV_SaveDC, /* pSaveDC */
111 MFDRV_ScaleViewportExt, /* pScaleViewportExt */
112 MFDRV_ScaleWindowExt, /* pScaleWindowExt */
113 MFDRV_SelectBitmap, /* pSelectBitmap */
114 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 */
121 MFDRV_SetBkColor, /* pSetBkColor */
122 MFDRV_SetBkMode, /* pSetBkMode */
123 NULL, /* pSetDCBrushColor*/
124 NULL, /* pSetDCOrg */
125 NULL, /* pSetDCPenColor*/
126 NULL, /* pSetDIBColorTable */
127 NULL, /* pSetDIBits */
128 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
129 NULL, /* pSetDeviceClipping */
130 NULL, /* pSetDeviceGammaRamp */
131 MFDRV_SetMapMode, /* pSetMapMode */
132 MFDRV_SetMapperFlags, /* pSetMapperFlags */
133 MFDRV_SetPixel, /* pSetPixel */
134 NULL, /* pSetPixelFormat */
135 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
136 MFDRV_SetROP2, /* pSetROP2 */
137 MFDRV_SetRelAbs, /* pSetRelAbs */
138 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
139 MFDRV_SetTextAlign, /* pSetTextAlign */
140 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
141 MFDRV_SetTextColor, /* pSetTextColor */
142 MFDRV_SetTextJustification, /* pSetTextJustification */
143 MFDRV_SetViewportExt, /* pSetViewportExt */
144 MFDRV_SetViewportOrg, /* pSetViewportOrg */
145 MFDRV_SetWindowExt, /* pSetWindowExt */
146 MFDRV_SetWindowOrg, /* pSetWindowOrg */
147 NULL, /* pSetWorldTransform */
148 NULL, /* pStartDoc */
149 NULL, /* pStartPage */
150 MFDRV_StretchBlt, /* pStretchBlt */
151 MFDRV_StretchDIBits, /* pStretchDIBits */
152 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
153 MFDRV_StrokePath, /* pStrokePath */
154 NULL, /* pSwapBuffers */
155 NULL, /* pUnrealizePalette */
156 MFDRV_WidenPath /* pWidenPath */
157 };
158
159
160
161 /**********************************************************************
162 * MFDRV_AllocMetaFile
163 */
164 static DC *MFDRV_AllocMetaFile(void)
165 {
166 DC *dc;
167 METAFILEDRV_PDEVICE *physDev;
168
169 if (!(dc = alloc_dc_ptr( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
170
171 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
172 if (!physDev)
173 {
174 free_dc_ptr( dc );
175 return NULL;
176 }
177 dc->physDev = (PHYSDEV)physDev;
178 physDev->hdc = dc->hSelf;
179
180 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
181 {
182 HeapFree( GetProcessHeap(), 0, physDev );
183 free_dc_ptr( dc );
184 return NULL;
185 }
186
187 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
188 physDev->handles_size = HANDLE_LIST_INC;
189 physDev->cur_handles = 0;
190
191 physDev->hFile = 0;
192
193 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
194 physDev->mh->mtVersion = 0x0300;
195 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
196 physDev->mh->mtNoObjects = 0;
197 physDev->mh->mtMaxRecord = 0;
198 physDev->mh->mtNoParameters = 0;
199
200 return dc;
201 }
202
203
204 /**********************************************************************
205 * MFDRV_DeleteDC
206 */
207 static BOOL MFDRV_DeleteDC( DC *dc )
208 {
209 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
210 DWORD index;
211
212 HeapFree( GetProcessHeap(), 0, physDev->mh );
213 for(index = 0; index < physDev->handles_size; index++)
214 if(physDev->handles[index])
215 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
216 HeapFree( GetProcessHeap(), 0, physDev->handles );
217 HeapFree( GetProcessHeap(), 0, physDev );
218 dc->physDev = NULL;
219 free_dc_ptr( dc );
220 return TRUE;
221 }
222
223
224 /**********************************************************************
225 * CreateMetaFileW (GDI32.@)
226 *
227 * Create a new DC and associate it with a metafile. Pass a filename
228 * to create a disk-based metafile, NULL to create a memory metafile.
229 *
230 * PARAMS
231 * filename [I] Filename of disk metafile
232 *
233 * RETURNS
234 * A handle to the metafile DC if successful, NULL on failure.
235 */
236 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
237 {
238 HDC ret;
239 DC *dc;
240 METAFILEDRV_PDEVICE *physDev;
241 HANDLE hFile;
242
243 TRACE("%s\n", debugstr_w(filename) );
244
245 if (!(dc = MFDRV_AllocMetaFile())) return 0;
246 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
247
248 if (filename) /* disk based metafile */
249 {
250 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 );
254 return 0;
255 }
256 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
257 NULL )) {
258 MFDRV_DeleteDC( dc );
259 return 0;
260 }
261 physDev->hFile = hFile;
262
263 /* Grow METAHEADER to include filename */
264 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
265 }
266 else /* memory based metafile */
267 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;
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 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
289 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
290 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
291
292 hReturnDC = CreateMetaFileW(filenameW);
293
294 HeapFree( GetProcessHeap(), 0, filenameW );
295
296 return hReturnDC;
297 }
298
299
300 /**********************************************************************
301 * MFDRV_CloseMetaFile
302 */
303 static DC *MFDRV_CloseMetaFile( HDC hdc )
304 {
305 DC *dc;
306 METAFILEDRV_PDEVICE *physDev;
307
308 TRACE("(%p)\n", hdc );
309
310 if (!(dc = get_dc_ptr( hdc ))) return NULL;
311 if (GDIMAGIC(dc->header.wMagic) != METAFILE_DC_MAGIC)
312 {
313 release_dc_ptr( dc );
314 return NULL;
315 }
316 if (dc->refcount != 1)
317 {
318 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
319 release_dc_ptr( dc );
320 return NULL;
321 }
322 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
323
324 /* Construct the end of metafile record - this is documented
325 * in SDK Knowledgebase Q99334.
326 */
327
328 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
329 {
330 MFDRV_DeleteDC( dc );
331 return 0;
332 }
333
334 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
335 {
336 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
337 MFDRV_DeleteDC( dc );
338 return 0;
339 }
340
341 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
342 if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
343 sizeof(*physDev->mh), NULL, NULL)) {
344 MFDRV_DeleteDC( dc );
345 return 0;
346 }
347 CloseHandle(physDev->hFile);
348 physDev->mh->mtType = METAFILE_DISK;
349 }
350
351 return dc;
352 }
353
354 /******************************************************************
355 * MF_Create_HMETATFILE16
356 *
357 * Creates a HMETAFILE16 object from a METAHEADER
358 *
359 * HMETAFILE16s are Global memory handles.
360 */
361 HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
362 {
363 HMETAFILE16 hmf;
364 DWORD size = mh->mtSize * sizeof(WORD);
365
366 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
367 if(hmf)
368 {
369 METAHEADER *mh_dest = GlobalLock16(hmf);
370 memcpy(mh_dest, mh, size);
371 GlobalUnlock16(hmf);
372 }
373 HeapFree(GetProcessHeap(), 0, mh);
374 return hmf;
375 }
376
377 /******************************************************************
378 * CloseMetaFile (GDI.126)
379 *
380 * PARAMS
381 * hdc [I] Metafile DC to close
382 */
383 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
384 {
385 HMETAFILE16 hmf;
386 METAFILEDRV_PDEVICE *physDev;
387 DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
388 if (!dc) return 0;
389 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
390
391 /* Now allocate a global handle for the metafile */
392
393 hmf = MF_Create_HMETAFILE16( physDev->mh );
394
395 physDev->mh = NULL; /* So it won't be deleted */
396 MFDRV_DeleteDC( dc );
397 return hmf;
398 }
399
400 /******************************************************************
401 * CloseMetaFile (GDI32.@)
402 *
403 * Stop recording graphics operations in metafile associated with
404 * hdc and retrieve metafile.
405 *
406 * PARAMS
407 * hdc [I] Metafile DC to close
408 *
409 * RETURNS
410 * Handle of newly created metafile on success, NULL on failure.
411 */
412 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
413 {
414 HMETAFILE hmf;
415 METAFILEDRV_PDEVICE *physDev;
416 DC *dc = MFDRV_CloseMetaFile(hdc);
417 if (!dc) return 0;
418 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
419
420 /* Now allocate a global handle for the metafile */
421
422 hmf = MF_Create_HMETAFILE( physDev->mh );
423
424 physDev->mh = NULL; /* So it won't be deleted */
425 MFDRV_DeleteDC( dc );
426 return hmf;
427 }
428
429
430 /******************************************************************
431 * MFDRV_WriteRecord
432 *
433 * Warning: this function can change the pointer to the metafile header.
434 */
435 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
436 {
437 DWORD len, size;
438 METAHEADER *mh;
439 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
440
441 switch(physDev->mh->mtType)
442 {
443 case METAFILE_MEMORY:
444 len = physDev->mh->mtSize * 2 + rlen;
445 /* reallocate memory if needed */
446 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
447 if (len > size)
448 {
449 /*expand size*/
450 size += size / 2 + rlen;
451 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
452 if (!mh) return FALSE;
453 physDev->mh = mh;
454 TRACE("Reallocated metafile: new size is %d\n",size);
455 }
456 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
457 break;
458 case METAFILE_DISK:
459 TRACE("Writing record to disk\n");
460 if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
461 return FALSE;
462 break;
463 default:
464 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
465 return FALSE;
466 }
467
468 physDev->mh->mtSize += rlen / 2;
469 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
470 return TRUE;
471 }
472
473
474 /******************************************************************
475 * MFDRV_MetaParam0
476 */
477
478 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
479 {
480 char buffer[8];
481 METARECORD *mr = (METARECORD *)&buffer;
482
483 mr->rdSize = 3;
484 mr->rdFunction = func;
485 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
486 }
487
488
489 /******************************************************************
490 * MFDRV_MetaParam1
491 */
492 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
493 {
494 char buffer[8];
495 METARECORD *mr = (METARECORD *)&buffer;
496
497 mr->rdSize = 4;
498 mr->rdFunction = func;
499 *(mr->rdParm) = param1;
500 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
501 }
502
503
504 /******************************************************************
505 * MFDRV_MetaParam2
506 */
507 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
508 {
509 char buffer[10];
510 METARECORD *mr = (METARECORD *)&buffer;
511
512 mr->rdSize = 5;
513 mr->rdFunction = func;
514 *(mr->rdParm) = param2;
515 *(mr->rdParm + 1) = param1;
516 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
517 }
518
519
520 /******************************************************************
521 * MFDRV_MetaParam4
522 */
523
524 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
525 short param3, short param4)
526 {
527 char buffer[14];
528 METARECORD *mr = (METARECORD *)&buffer;
529
530 mr->rdSize = 7;
531 mr->rdFunction = func;
532 *(mr->rdParm) = param4;
533 *(mr->rdParm + 1) = param3;
534 *(mr->rdParm + 2) = param2;
535 *(mr->rdParm + 3) = param1;
536 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
537 }
538
539
540 /******************************************************************
541 * MFDRV_MetaParam6
542 */
543
544 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
545 short param3, short param4, short param5, short param6)
546 {
547 char buffer[18];
548 METARECORD *mr = (METARECORD *)&buffer;
549
550 mr->rdSize = 9;
551 mr->rdFunction = func;
552 *(mr->rdParm) = param6;
553 *(mr->rdParm + 1) = param5;
554 *(mr->rdParm + 2) = param4;
555 *(mr->rdParm + 3) = param3;
556 *(mr->rdParm + 4) = param2;
557 *(mr->rdParm + 5) = param1;
558 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
559 }
560
561
562 /******************************************************************
563 * MFDRV_MetaParam8
564 */
565 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
566 short param3, short param4, short param5,
567 short param6, short param7, short param8)
568 {
569 char buffer[22];
570 METARECORD *mr = (METARECORD *)&buffer;
571
572 mr->rdSize = 11;
573 mr->rdFunction = func;
574 *(mr->rdParm) = param8;
575 *(mr->rdParm + 1) = param7;
576 *(mr->rdParm + 2) = param6;
577 *(mr->rdParm + 3) = param5;
578 *(mr->rdParm + 4) = param4;
579 *(mr->rdParm + 5) = param3;
580 *(mr->rdParm + 6) = param2;
581 *(mr->rdParm + 7) = param1;
582 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
583 }
584
585
586 /**********************************************************************
587 * MFDRV_ExtEscape
588 */
589 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
590 INT cbOutput, LPVOID out_data )
591 {
592 METARECORD *mr;
593 DWORD len;
594 INT ret;
595
596 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
597
598 len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
599 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
600 mr->rdSize = len / 2;
601 mr->rdFunction = META_ESCAPE;
602 mr->rdParm[0] = nEscape;
603 mr->rdParm[1] = cbInput;
604 memcpy(&(mr->rdParm[2]), in_data, cbInput);
605 ret = MFDRV_WriteRecord( dev, mr, len);
606 HeapFree(GetProcessHeap(), 0, mr);
607 return ret;
608 }
609
610
611 /******************************************************************
612 * MFDRV_GetDeviceCaps
613 *
614 *A very simple implementation that returns DT_METAFILE
615 */
616 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
617 {
618 switch(cap)
619 {
620 case TECHNOLOGY:
621 return DT_METAFILE;
622 case TEXTCAPS:
623 return 0;
624 default:
625 TRACE(" unsupported capability %d, will return 0\n", cap );
626 }
627 return 0;
628 }
629
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.