4 * Copyright David W. Metcalfe, 1994
5 * Copyright Niels de Carpentier, 1996
6 * Copyright Albrecht Kleine, 1996
7 * Copyright Huw Davies, 1996
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/winbase16.h"
30 #include "wine/wingdi16.h"
34 #include "gdi_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
39 /******************************************************************
42 * Returns ptr to METAHEADER associated with HMETAFILE16
43 * Should be followed by call to MF_ReleaseMetaHeader16
45 static METAHEADER *MF_GetMetaHeader16( HMETAFILE16 hmf )
47 return GlobalLock16(hmf);
50 /******************************************************************
51 * MF_ReleaseMetaHeader16
53 * Releases METAHEADER associated with HMETAFILE16
55 static BOOL16 MF_ReleaseMetaHeader16( HMETAFILE16 hmf )
57 return GlobalUnlock16( hmf );
60 /******************************************************************
61 * MF_Create_HMETATFILE16
63 * Creates a HMETAFILE16 object from a METAHEADER
65 * HMETAFILE16s are Global memory handles.
67 static HMETAFILE16 MF_Create_HMETAFILE16(METAHEADER *mh)
70 DWORD size = mh->mtSize * sizeof(WORD);
72 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
75 METAHEADER *mh_dest = GlobalLock16(hmf);
76 memcpy(mh_dest, mh, size);
79 HeapFree(GetProcessHeap(), 0, mh);
83 /**********************************************************************
84 * CreateMetaFile (GDI.125)
86 HDC16 WINAPI CreateMetaFile16( LPCSTR filename )
88 return HDC_16( CreateMetaFileA( filename ) );
91 /******************************************************************
92 * CloseMetaFile (GDI.126)
94 HMETAFILE16 WINAPI CloseMetaFile16(HDC16 hdc)
96 HMETAFILE16 hmf16 = 0;
97 HMETAFILE hmf = CloseMetaFile( HDC_32(hdc) );
101 UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
103 hmf16 = GlobalAlloc16( GMEM_MOVEABLE, size );
106 void *buffer = GlobalLock16( hmf16 );
107 GetMetaFileBitsEx( hmf, size, buffer );
108 GlobalUnlock16( hmf16 );
110 DeleteMetaFile( hmf );
115 /******************************************************************
116 * DeleteMetaFile (GDI.127)
118 BOOL16 WINAPI DeleteMetaFile16( HMETAFILE16 hmf )
120 return !GlobalFree16( hmf );
123 /******************************************************************
124 * GetMetaFile (GDI.124)
126 HMETAFILE16 WINAPI GetMetaFile16( LPCSTR lpFilename )
131 TRACE("%s\n", lpFilename);
136 if((hFile = CreateFileA(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
137 OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
140 mh = MF_ReadMetaFile(hFile);
143 return MF_Create_HMETAFILE16( mh );
146 /******************************************************************
147 * CopyMetaFile (GDI.151)
149 HMETAFILE16 WINAPI CopyMetaFile16( HMETAFILE16 hSrcMetaFile, LPCSTR lpFilename)
151 METAHEADER *mh = MF_GetMetaHeader16( hSrcMetaFile );
152 METAHEADER *mh2 = NULL;
155 TRACE("(%08x,%s)\n", hSrcMetaFile, lpFilename);
159 if(mh->mtType == METAFILE_DISK)
160 mh2 = MF_LoadDiskBasedMetaFile(mh);
162 mh2 = HeapAlloc( GetProcessHeap(), 0, mh->mtSize * 2 );
163 memcpy( mh2, mh, mh->mtSize * 2 );
165 MF_ReleaseMetaHeader16( hSrcMetaFile );
167 if(lpFilename) { /* disk based metafile */
169 if((hFile = CreateFileA(lpFilename, GENERIC_WRITE, 0, NULL,
170 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
171 HeapFree( GetProcessHeap(), 0, mh2 );
174 WriteFile(hFile, mh2, mh2->mtSize * 2, &w, NULL);
176 mh2 = MF_CreateMetaHeaderDisk(mh2, lpFilename, FALSE);
179 return MF_Create_HMETAFILE16( mh2 );
182 /******************************************************************
183 * IsValidMetaFile (GDI.410)
185 * Attempts to check if a given metafile is correctly formatted.
186 * Currently, the only things verified are several properties of the
190 * TRUE if hmf passes some tests for being a valid metafile, FALSE otherwise.
193 * This is not exactly what windows does, see _Undocumented_Windows_
196 BOOL16 WINAPI IsValidMetaFile16(HMETAFILE16 hmf)
199 METAHEADER *mh = MF_GetMetaHeader16(hmf);
201 if (mh->mtType == METAFILE_MEMORY || mh->mtType == METAFILE_DISK)
202 if (mh->mtHeaderSize == MFHEADERSIZE/sizeof(INT16))
203 if (mh->mtVersion == MFVERSION)
205 MF_ReleaseMetaHeader16(hmf);
207 TRACE("IsValidMetaFile %x => %d\n",hmf,res);
211 /******************************************************************
212 * PlayMetaFile (GDI.123)
215 BOOL16 WINAPI PlayMetaFile16( HDC16 hdc, HMETAFILE16 hmf )
218 METAHEADER *mh = MF_GetMetaHeader16( hmf );
219 ret = MF_PlayMetaFile( HDC_32(hdc), mh );
220 MF_ReleaseMetaHeader16( hmf );
225 /******************************************************************
226 * EnumMetaFile (GDI.175)
229 BOOL16 WINAPI EnumMetaFile16( HDC16 hdc16, HMETAFILE16 hmf,
230 MFENUMPROC16 lpEnumFunc, LPARAM lpData )
232 METAHEADER *mh = MF_GetMetaHeader16(hmf);
235 HDC hdc = HDC_32(hdc16);
238 unsigned int offset = 0;
244 BOOL16 result = TRUE, loaded = FALSE;
246 TRACE("(%p, %04x, %p, %08lx)\n", hdc, hmf, lpEnumFunc, lpData);
248 if(!mh) return FALSE;
249 if(mh->mtType == METAFILE_DISK) { /* Create a memory-based copy */
250 mh = MF_LoadDiskBasedMetaFile(mh);
251 if(!mh) return FALSE;
255 /* save the current pen, brush and font */
256 hPen = GetCurrentObject(hdc, OBJ_PEN);
257 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
258 hFont = GetCurrentObject(hdc, OBJ_FONT);
260 /* create the handle table */
262 hHT = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT,
263 sizeof(HANDLETABLE16) * mh->mtNoObjects);
264 spht = WOWGlobalLock16(hHT);
267 offset = mh->mtHeaderSize * 2;
269 /* loop through metafile records */
272 args[6] = SELECTOROF(spht);
273 args[5] = OFFSETOF(spht);
274 args[4] = seg + (HIWORD(offset) << __AHSHIFT);
275 args[3] = LOWORD(offset);
276 args[2] = mh->mtNoObjects;
277 args[1] = HIWORD(lpData);
278 args[0] = LOWORD(lpData);
280 while (offset < (mh->mtSize * 2))
284 mr = (METARECORD *)((char *)mh + offset);
286 WOWCallback16Ex( (DWORD)lpEnumFunc, WCB16_PASCAL, sizeof(args), args, &ret );
293 offset += (mr->rdSize * 2);
294 args[4] = seg + (HIWORD(offset) << __AHSHIFT);
295 args[3] = LOWORD(offset);
298 SelectObject(hdc, hBrush);
299 SelectObject(hdc, hPen);
300 SelectObject(hdc, hFont);
302 ht = GlobalLock16(hHT);
304 /* free objects in handle table */
305 for(i = 0; i < mh->mtNoObjects; i++)
306 if(*(ht->objectHandle + i) != 0)
307 DeleteObject( (HGDIOBJ)(ULONG_PTR)(*(ht->objectHandle + i) ));
309 /* free handle table */
312 HeapFree( GetProcessHeap(), 0, mh );
313 MF_ReleaseMetaHeader16(hmf);
317 /******************************************************************
318 * GetMetaFileBits (GDI.159)
320 * Trade in a metafile object handle for a handle to the metafile memory.
323 * hmf [I] metafile handle
326 HGLOBAL16 WINAPI GetMetaFileBits16( HMETAFILE16 hmf )
328 TRACE("hMem out: %04x\n", hmf);
332 /******************************************************************
333 * SetMetaFileBits (GDI.160)
335 * Trade in a metafile memory handle for a handle to a metafile object.
336 * The memory region should hold a proper metafile, otherwise
337 * problems will occur when it is used. Validity of the memory is not
338 * checked. The function is essentially just the identity function.
341 * hMem [I] handle to a memory region holding a metafile
344 * Handle to a metafile on success, NULL on failure..
346 HMETAFILE16 WINAPI SetMetaFileBits16( HGLOBAL16 hMem )
348 TRACE("hmf out: %04x\n", hMem);
353 /******************************************************************
354 * SetMetaFileBitsBetter (GDI.196)
356 * Trade in a metafile memory handle for a handle to a metafile object,
357 * making a cursory check (using IsValidMetaFile()) that the memory
358 * handle points to a valid metafile.
361 * Handle to a metafile on success, NULL on failure..
363 HMETAFILE16 WINAPI SetMetaFileBitsBetter16( HMETAFILE16 hMeta )
365 if( IsValidMetaFile16( hMeta ) )
366 return GlobalReAlloc16( hMeta, 0, GMEM_SHARE | GMEM_NODISCARD | GMEM_MODIFY);