d3d10: Add ID3D10EffectShaderVariableVtbl.
[wine.git] / dlls / gdi32 / metafile16.c
blob812fb9cb7fa2b5d8b2a01900f48508e899bfea49
1 /*
2 * Metafile functions
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
24 #include "config.h"
26 #include <string.h>
27 #include <fcntl.h>
29 #include "wine/winbase16.h"
30 #include "wine/wingdi16.h"
31 #include "wownt32.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "gdi_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
39 /******************************************************************
40 * MF_GetMetaHeader16
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)
69 HMETAFILE16 hmf;
70 DWORD size = mh->mtSize * sizeof(WORD);
72 hmf = GlobalAlloc16(GMEM_MOVEABLE, size);
73 if(hmf)
75 METAHEADER *mh_dest = GlobalLock16(hmf);
76 memcpy(mh_dest, mh, size);
77 GlobalUnlock16(hmf);
79 HeapFree(GetProcessHeap(), 0, mh);
80 return hmf;
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) );
99 if (hmf)
101 UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
103 hmf16 = GlobalAlloc16( GMEM_MOVEABLE, size );
104 if (hmf16)
106 void *buffer = GlobalLock16( hmf16 );
107 GetMetaFileBitsEx( hmf, size, buffer );
108 GlobalUnlock16( hmf16 );
110 DeleteMetaFile( hmf );
112 return hmf16;
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 )
128 METAHEADER *mh;
129 HANDLE hFile;
131 TRACE("%s\n", lpFilename);
133 if(!lpFilename)
134 return 0;
136 if((hFile = CreateFileA(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
137 OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
138 return 0;
140 mh = MF_ReadMetaFile(hFile);
141 CloseHandle(hFile);
142 if(!mh) return 0;
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;
153 HANDLE hFile;
155 TRACE("(%08x,%s)\n", hSrcMetaFile, lpFilename);
157 if(!mh) return 0;
159 if(mh->mtType == METAFILE_DISK)
160 mh2 = MF_LoadDiskBasedMetaFile(mh);
161 else {
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 */
168 DWORD w;
169 if((hFile = CreateFileA(lpFilename, GENERIC_WRITE, 0, NULL,
170 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
171 HeapFree( GetProcessHeap(), 0, mh2 );
172 return 0;
174 WriteFile(hFile, mh2, mh2->mtSize * 2, &w, NULL);
175 CloseHandle(hFile);
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
187 * header.
189 * RETURNS
190 * TRUE if hmf passes some tests for being a valid metafile, FALSE otherwise.
192 * BUGS
193 * This is not exactly what windows does, see _Undocumented_Windows_
194 * for details.
196 BOOL16 WINAPI IsValidMetaFile16(HMETAFILE16 hmf)
198 BOOL16 res=FALSE;
199 METAHEADER *mh = MF_GetMetaHeader16(hmf);
200 if (mh) {
201 if (mh->mtType == METAFILE_MEMORY || mh->mtType == METAFILE_DISK)
202 if (mh->mtHeaderSize == MFHEADERSIZE/sizeof(INT16))
203 if (mh->mtVersion == MFVERSION)
204 res=TRUE;
205 MF_ReleaseMetaHeader16(hmf);
207 TRACE("IsValidMetaFile %x => %d\n",hmf,res);
208 return res;
211 /******************************************************************
212 * PlayMetaFile (GDI.123)
215 BOOL16 WINAPI PlayMetaFile16( HDC16 hdc, HMETAFILE16 hmf )
217 BOOL16 ret;
218 METAHEADER *mh = MF_GetMetaHeader16( hmf );
219 ret = MF_PlayMetaFile( HDC_32(hdc), mh );
220 MF_ReleaseMetaHeader16( hmf );
221 return ret;
225 /******************************************************************
226 * EnumMetaFile (GDI.175)
229 BOOL16 WINAPI EnumMetaFile16( HDC16 hdc16, HMETAFILE16 hmf,
230 MFENUMPROC16 lpEnumFunc, LPARAM lpData )
232 METAHEADER *mh = MF_GetMetaHeader16(hmf);
233 METARECORD *mr;
234 HANDLETABLE16 *ht;
235 HDC hdc = HDC_32(hdc16);
236 HGLOBAL16 hHT;
237 SEGPTR spht;
238 unsigned int offset = 0;
239 WORD i, seg;
240 HPEN hPen;
241 HBRUSH hBrush;
242 HFONT hFont;
243 WORD args[8];
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;
252 loaded = TRUE;
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);
266 seg = hmf | 7;
267 offset = mh->mtHeaderSize * 2;
269 /* loop through metafile records */
271 args[7] = hdc16;
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))
282 DWORD ret;
284 mr = (METARECORD *)((char *)mh + offset);
286 WOWCallback16Ex( (DWORD)lpEnumFunc, WCB16_PASCAL, sizeof(args), args, &ret );
287 if (!LOWORD(ret))
289 result = FALSE;
290 break;
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 */
310 GlobalFree16(hHT);
311 if(loaded)
312 HeapFree( GetProcessHeap(), 0, mh );
313 MF_ReleaseMetaHeader16(hmf);
314 return result;
317 /******************************************************************
318 * GetMetaFileBits (GDI.159)
320 * Trade in a metafile object handle for a handle to the metafile memory.
322 * PARAMS
323 * hmf [I] metafile handle
326 HGLOBAL16 WINAPI GetMetaFileBits16( HMETAFILE16 hmf )
328 TRACE("hMem out: %04x\n", hmf);
329 return 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.
340 * PARAMS
341 * hMem [I] handle to a memory region holding a metafile
343 * RETURNS
344 * Handle to a metafile on success, NULL on failure..
346 HMETAFILE16 WINAPI SetMetaFileBits16( HGLOBAL16 hMem )
348 TRACE("hmf out: %04x\n", hMem);
350 return 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.
360 * RETURNS
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);
367 return 0;