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 "winnls.h"
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
32
33 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev );
34 static BOOL MFDRV_DeleteDC( PHYSDEV dev );
35
36
37 /**********************************************************************
38 * MFDRV_ExtEscape
39 */
40 static INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
41 INT cbOutput, LPVOID out_data )
42 {
43 METARECORD *mr;
44 DWORD len;
45 INT ret;
46
47 if (cbOutput) return 0; /* escapes that require output cannot work in metafiles */
48
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;
59 }
60
61
62 /******************************************************************
63 * MFDRV_GetDeviceCaps
64 *
65 *A very simple implementation that returns DT_METAFILE
66 */
67 static INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
68 {
69 switch(cap)
70 {
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 );
77 }
78 return 0;
79 }
80
81
82 static const DC_FUNCTIONS MFDRV_Funcs =
83 {
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, /* pChoosePixelFormat */
92 MFDRV_Chord, /* pChord */
93 MFDRV_CloseFigure, /* pCloseFigure */
94 NULL, /* pCreateBitmap */
95 MFDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
96 NULL, /* pCreateDC */
97 NULL, /* pCreateDIBSection */
98 NULL, /* pDeleteBitmap */
99 MFDRV_DeleteDC, /* pDeleteDC */
100 MFDRV_DeleteObject, /* pDeleteObject */
101 NULL, /* pDescribePixelFormat */
102 NULL, /* pDeviceCapabilities */
103 MFDRV_Ellipse, /* pEllipse */
104 NULL, /* pEndDoc */
105 NULL, /* pEndPage */
106 MFDRV_EndPath, /* pEndPath */
107 NULL, /* pEnumDeviceFonts */
108 NULL, /* pEnumICMProfiles */
109 MFDRV_ExcludeClipRect, /* pExcludeClipRect */
110 NULL, /* pExtDeviceMode */
111 MFDRV_ExtEscape, /* pExtEscape */
112 MFDRV_ExtFloodFill, /* pExtFloodFill */
113 MFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
114 MFDRV_ExtTextOut, /* pExtTextOut */
115 MFDRV_FillPath, /* pFillPath */
116 MFDRV_FillRgn, /* pFillRgn */
117 MFDRV_FlattenPath, /* pFlattenPath */
118 MFDRV_FrameRgn, /* pFrameRgn */
119 NULL, /* pGdiComment */
120 NULL, /* pGetCharWidth */
121 MFDRV_GetDeviceCaps, /* pGetDeviceCaps */
122 NULL, /* pGetDeviceGammaRamp */
123 NULL, /* pGetICMProfile */
124 NULL, /* pGetImage */
125 NULL, /* pGetNearestColor */
126 NULL, /* pGetPixel */
127 NULL, /* pGetPixelFormat */
128 NULL, /* pGetSystemPaletteEntries */
129 NULL, /* pGetTextExtentExPoint */
130 NULL, /* pGetTextMetrics */
131 MFDRV_IntersectClipRect, /* pIntersectClipRect */
132 MFDRV_InvertRgn, /* pInvertRgn */
133 MFDRV_LineTo, /* pLineTo */
134 NULL, /* pModifyWorldTransform */
135 MFDRV_MoveTo, /* pMoveTo */
136 MFDRV_OffsetClipRgn, /* pOffsetClipRgn */
137 MFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
138 MFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
139 MFDRV_PaintRgn, /* pPaintRgn */
140 MFDRV_PatBlt, /* pPatBlt */
141 MFDRV_Pie, /* pPie */
142 MFDRV_PolyBezier, /* pPolyBezier */
143 MFDRV_PolyBezierTo, /* pPolyBezierTo */
144 NULL, /* pPolyDraw */
145 MFDRV_PolyPolygon, /* pPolyPolygon */
146 NULL, /* pPolyPolyline */
147 MFDRV_Polygon, /* pPolygon */
148 MFDRV_Polyline, /* pPolyline */
149 NULL, /* pPolylineTo */
150 NULL, /* pPutImage */
151 NULL, /* pRealizeDefaultPalette */
152 MFDRV_RealizePalette, /* pRealizePalette */
153 MFDRV_Rectangle, /* pRectangle */
154 NULL, /* pResetDC */
155 MFDRV_RestoreDC, /* pRestoreDC */
156 MFDRV_RoundRect, /* pRoundRect */
157 MFDRV_SaveDC, /* pSaveDC */
158 MFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
159 MFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
160 MFDRV_SelectBitmap, /* pSelectBitmap */
161 MFDRV_SelectBrush, /* pSelectBrush */
162 MFDRV_SelectClipPath, /* pSelectClipPath */
163 MFDRV_SelectFont, /* pSelectFont */
164 MFDRV_SelectPalette, /* pSelectPalette */
165 MFDRV_SelectPen, /* pSelectPen */
166 NULL, /* pSetArcDirection */
167 MFDRV_SetBkColor, /* pSetBkColor */
168 MFDRV_SetBkMode, /* pSetBkMode */
169 MFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
170 MFDRV_SetDCPenColor, /* pSetDCPenColor*/
171 NULL, /* pSetDIBColorTable */
172 MFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
173 NULL, /* pSetDeviceClipping */
174 NULL, /* pSetDeviceGammaRamp */
175 NULL, /* pSetLayout */
176 MFDRV_SetMapMode, /* pSetMapMode */
177 MFDRV_SetMapperFlags, /* pSetMapperFlags */
178 MFDRV_SetPixel, /* pSetPixel */
179 NULL, /* pSetPixelFormat */
180 MFDRV_SetPolyFillMode, /* pSetPolyFillMode */
181 MFDRV_SetROP2, /* pSetROP2 */
182 MFDRV_SetRelAbs, /* pSetRelAbs */
183 MFDRV_SetStretchBltMode, /* pSetStretchBltMode */
184 MFDRV_SetTextAlign, /* pSetTextAlign */
185 MFDRV_SetTextCharacterExtra, /* pSetTextCharacterExtra */
186 MFDRV_SetTextColor, /* pSetTextColor */
187 MFDRV_SetTextJustification, /* pSetTextJustification */
188 MFDRV_SetViewportExtEx, /* pSetViewportExtEx */
189 MFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
190 MFDRV_SetWindowExtEx, /* pSetWindowExtEx */
191 MFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
192 NULL, /* pSetWorldTransform */
193 NULL, /* pStartDoc */
194 NULL, /* pStartPage */
195 MFDRV_StretchBlt, /* pStretchBlt */
196 MFDRV_StretchDIBits, /* pStretchDIBits */
197 MFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
198 MFDRV_StrokePath, /* pStrokePath */
199 NULL, /* pSwapBuffers */
200 NULL, /* pUnrealizePalette */
201 MFDRV_WidenPath /* pWidenPath */
202 };
203
204
205
206 /**********************************************************************
207 * MFDRV_AllocMetaFile
208 */
209 static DC *MFDRV_AllocMetaFile(void)
210 {
211 DC *dc;
212 METAFILEDRV_PDEVICE *physDev;
213
214 if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
215
216 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
217 if (!physDev)
218 {
219 free_dc_ptr( dc );
220 return NULL;
221 }
222 if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
223 {
224 HeapFree( GetProcessHeap(), 0, physDev );
225 free_dc_ptr( dc );
226 return NULL;
227 }
228
229 push_dc_driver( &dc->physDev, &physDev->dev, &MFDRV_Funcs );
230
231 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
232 physDev->handles_size = HANDLE_LIST_INC;
233 physDev->cur_handles = 0;
234
235 physDev->hFile = 0;
236
237 physDev->mh->mtHeaderSize = sizeof(METAHEADER) / sizeof(WORD);
238 physDev->mh->mtVersion = 0x0300;
239 physDev->mh->mtSize = physDev->mh->mtHeaderSize;
240 physDev->mh->mtNoObjects = 0;
241 physDev->mh->mtMaxRecord = 0;
242 physDev->mh->mtNoParameters = 0;
243
244 SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
245
246 return dc;
247 }
248
249
250 /**********************************************************************
251 * MFDRV_CreateCompatibleDC
252 */
253 static BOOL MFDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
254 {
255 /* not supported on metafile DCs */
256 return FALSE;
257 }
258
259
260 /**********************************************************************
261 * MFDRV_DeleteDC
262 */
263 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
264 {
265 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
266 DWORD index;
267
268 HeapFree( GetProcessHeap(), 0, physDev->mh );
269 for(index = 0; index < physDev->handles_size; index++)
270 if(physDev->handles[index])
271 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
272 HeapFree( GetProcessHeap(), 0, physDev->handles );
273 HeapFree( GetProcessHeap(), 0, physDev );
274 return TRUE;
275 }
276
277
278 /**********************************************************************
279 * CreateMetaFileW (GDI32.@)
280 *
281 * Create a new DC and associate it with a metafile. Pass a filename
282 * to create a disk-based metafile, NULL to create a memory metafile.
283 *
284 * PARAMS
285 * filename [I] Filename of disk metafile
286 *
287 * RETURNS
288 * A handle to the metafile DC if successful, NULL on failure.
289 */
290 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
291 {
292 HDC ret;
293 DC *dc;
294 METAFILEDRV_PDEVICE *physDev;
295 HANDLE hFile;
296
297 TRACE("%s\n", debugstr_w(filename) );
298
299 if (!(dc = MFDRV_AllocMetaFile())) return 0;
300 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
301
302 if (filename) /* disk based metafile */
303 {
304 physDev->mh->mtType = METAFILE_DISK;
305 if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
306 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
307 free_dc_ptr( dc );
308 return 0;
309 }
310 if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
311 NULL )) {
312 free_dc_ptr( dc );
313 return 0;
314 }
315 physDev->hFile = hFile;
316
317 /* Grow METAHEADER to include filename */
318 physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
319 }
320 else /* memory based metafile */
321 physDev->mh->mtType = METAFILE_MEMORY;
322
323 TRACE("returning %p\n", physDev->dev.hdc);
324 ret = physDev->dev.hdc;
325 release_dc_ptr( dc );
326 return ret;
327 }
328
329 /**********************************************************************
330 * CreateMetaFileA (GDI32.@)
331 *
332 * See CreateMetaFileW.
333 */
334 HDC WINAPI CreateMetaFileA(LPCSTR filename)
335 {
336 LPWSTR filenameW;
337 DWORD len;
338 HDC hReturnDC;
339
340 if (!filename) return CreateMetaFileW(NULL);
341
342 len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
343 filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
344 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
345
346 hReturnDC = CreateMetaFileW(filenameW);
347
348 HeapFree( GetProcessHeap(), 0, filenameW );
349
350 return hReturnDC;
351 }
352
353
354 /**********************************************************************
355 * MFDRV_CloseMetaFile
356 */
357 static DC *MFDRV_CloseMetaFile( HDC hdc )
358 {
359 DC *dc;
360 METAFILEDRV_PDEVICE *physDev;
361
362 TRACE("(%p)\n", hdc );
363
364 if (!(dc = get_dc_ptr( hdc ))) return NULL;
365 if (dc->header.type != OBJ_METADC)
366 {
367 release_dc_ptr( dc );
368 return NULL;
369 }
370 if (dc->refcount != 1)
371 {
372 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
373 release_dc_ptr( dc );
374 return NULL;
375 }
376 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
377
378 /* Construct the end of metafile record - this is documented
379 * in SDK Knowledgebase Q99334.
380 */
381
382 if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
383 {
384 free_dc_ptr( dc );
385 return 0;
386 }
387
388 if (physDev->mh->mtType == METAFILE_DISK) /* disk based metafile */
389 {
390 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
391 free_dc_ptr( dc );
392 return 0;
393 }
394
395 physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
396 if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
397 NULL, NULL)) {
398 free_dc_ptr( dc );
399 return 0;
400 }
401 CloseHandle(physDev->hFile);
402 physDev->mh->mtType = METAFILE_DISK;
403 }
404
405 return dc;
406 }
407
408 /******************************************************************
409 * CloseMetaFile (GDI32.@)
410 *
411 * Stop recording graphics operations in metafile associated with
412 * hdc and retrieve metafile.
413 *
414 * PARAMS
415 * hdc [I] Metafile DC to close
416 *
417 * RETURNS
418 * Handle of newly created metafile on success, NULL on failure.
419 */
420 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
421 {
422 HMETAFILE hmf;
423 METAFILEDRV_PDEVICE *physDev;
424 DC *dc = MFDRV_CloseMetaFile(hdc);
425 if (!dc) return 0;
426 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
427
428 /* Now allocate a global handle for the metafile */
429
430 hmf = MF_Create_HMETAFILE( physDev->mh );
431
432 physDev->mh = NULL; /* So it won't be deleted */
433 free_dc_ptr( dc );
434 return hmf;
435 }
436
437
438 /******************************************************************
439 * MFDRV_WriteRecord
440 *
441 * Warning: this function can change the pointer to the metafile header.
442 */
443 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
444 {
445 DWORD len, size;
446 METAHEADER *mh;
447 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
448
449 switch(physDev->mh->mtType)
450 {
451 case METAFILE_MEMORY:
452 len = physDev->mh->mtSize * 2 + rlen;
453 /* reallocate memory if needed */
454 size = HeapSize( GetProcessHeap(), 0, physDev->mh );
455 if (len > size)
456 {
457 /*expand size*/
458 size += size / 2 + rlen;
459 mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
460 if (!mh) return FALSE;
461 physDev->mh = mh;
462 TRACE("Reallocated metafile: new size is %d\n",size);
463 }
464 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
465 break;
466 case METAFILE_DISK:
467 TRACE("Writing record to disk\n");
468 if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
469 return FALSE;
470 break;
471 default:
472 ERR("Unknown metafile type %d\n", physDev->mh->mtType );
473 return FALSE;
474 }
475
476 physDev->mh->mtSize += rlen / 2;
477 physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
478 return TRUE;
479 }
480
481
482 /******************************************************************
483 * MFDRV_MetaParam0
484 */
485
486 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
487 {
488 char buffer[8];
489 METARECORD *mr = (METARECORD *)&buffer;
490
491 mr->rdSize = 3;
492 mr->rdFunction = func;
493 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
494 }
495
496
497 /******************************************************************
498 * MFDRV_MetaParam1
499 */
500 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
501 {
502 char buffer[8];
503 METARECORD *mr = (METARECORD *)&buffer;
504
505 mr->rdSize = 4;
506 mr->rdFunction = func;
507 *(mr->rdParm) = param1;
508 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
509 }
510
511
512 /******************************************************************
513 * MFDRV_MetaParam2
514 */
515 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
516 {
517 char buffer[10];
518 METARECORD *mr = (METARECORD *)&buffer;
519
520 mr->rdSize = 5;
521 mr->rdFunction = func;
522 *(mr->rdParm) = param2;
523 *(mr->rdParm + 1) = param1;
524 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
525 }
526
527
528 /******************************************************************
529 * MFDRV_MetaParam4
530 */
531
532 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
533 short param3, short param4)
534 {
535 char buffer[14];
536 METARECORD *mr = (METARECORD *)&buffer;
537
538 mr->rdSize = 7;
539 mr->rdFunction = func;
540 *(mr->rdParm) = param4;
541 *(mr->rdParm + 1) = param3;
542 *(mr->rdParm + 2) = param2;
543 *(mr->rdParm + 3) = param1;
544 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
545 }
546
547
548 /******************************************************************
549 * MFDRV_MetaParam6
550 */
551
552 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
553 short param3, short param4, short param5, short param6)
554 {
555 char buffer[18];
556 METARECORD *mr = (METARECORD *)&buffer;
557
558 mr->rdSize = 9;
559 mr->rdFunction = func;
560 *(mr->rdParm) = param6;
561 *(mr->rdParm + 1) = param5;
562 *(mr->rdParm + 2) = param4;
563 *(mr->rdParm + 3) = param3;
564 *(mr->rdParm + 4) = param2;
565 *(mr->rdParm + 5) = param1;
566 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
567 }
568
569
570 /******************************************************************
571 * MFDRV_MetaParam8
572 */
573 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
574 short param3, short param4, short param5,
575 short param6, short param7, short param8)
576 {
577 char buffer[22];
578 METARECORD *mr = (METARECORD *)&buffer;
579
580 mr->rdSize = 11;
581 mr->rdFunction = func;
582 *(mr->rdParm) = param8;
583 *(mr->rdParm + 1) = param7;
584 *(mr->rdParm + 2) = param6;
585 *(mr->rdParm + 3) = param5;
586 *(mr->rdParm + 4) = param4;
587 *(mr->rdParm + 5) = param3;
588 *(mr->rdParm + 6) = param2;
589 *(mr->rdParm + 7) = param1;
590 return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
591 }
592
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.