d3d9: Add a projected texture test.
[wine] / dlls / gdi32 / metafile16.c
1 /*
2  * Metafile functions
3  *
4  * Copyright  David W. Metcalfe, 1994
5  * Copyright  Niels de Carpentier, 1996
6  * Copyright  Albrecht Kleine, 1996
7  * Copyright  Huw Davies, 1996
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <fcntl.h>
28
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"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
38
39 /******************************************************************
40  *         MF_GetMetaHeader16
41  *
42  * Returns ptr to METAHEADER associated with HMETAFILE16
43  * Should be followed by call to MF_ReleaseMetaHeader16
44  */
45 static METAHEADER *MF_GetMetaHeader16( HMETAFILE16 hmf )
46 {
47     return GlobalLock16(hmf);
48 }
49
50 /******************************************************************
51  *         MF_ReleaseMetaHeader16
52  *
53  * Releases METAHEADER associated with HMETAFILE16
54  */
55 static BOOL16 MF_ReleaseMetaHeader16( HMETAFILE16 hmf )
56 {
57     return GlobalUnlock16( hmf );
58 }
59
60 /******************************************************************
61  *           DeleteMetaFile   (GDI.127)
62  */
63 BOOL16 WINAPI DeleteMetaFile16(  HMETAFILE16 hmf )
64 {
65     return !GlobalFree16( hmf );
66 }
67
68 /******************************************************************
69  *         GetMetaFile   (GDI.124)
70  */
71 HMETAFILE16 WINAPI GetMetaFile16( LPCSTR lpFilename )
72 {
73     METAHEADER *mh;
74     HANDLE hFile;
75
76     TRACE("%s\n", lpFilename);
77
78     if(!lpFilename)
79         return 0;
80
81     if((hFile = CreateFileA(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
82                             OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
83         return 0;
84
85     mh = MF_ReadMetaFile(hFile);
86     CloseHandle(hFile);
87     if(!mh) return 0;
88     return MF_Create_HMETAFILE16( mh );
89 }
90
91 /******************************************************************
92  *         CopyMetaFile   (GDI.151)
93  */
94 HMETAFILE16 WINAPI CopyMetaFile16( HMETAFILE16 hSrcMetaFile, LPCSTR lpFilename)
95 {
96     METAHEADER *mh = MF_GetMetaHeader16( hSrcMetaFile );
97     METAHEADER *mh2 = NULL;
98     HANDLE hFile;
99
100     TRACE("(%08x,%s)\n", hSrcMetaFile, lpFilename);
101
102     if(!mh) return 0;
103
104     if(mh->mtType == METAFILE_DISK)
105         mh2 = MF_LoadDiskBasedMetaFile(mh);
106     else {
107         mh2 = HeapAlloc( GetProcessHeap(), 0, mh->mtSize * 2 );
108         memcpy( mh2, mh, mh->mtSize * 2 );
109     }
110     MF_ReleaseMetaHeader16( hSrcMetaFile );
111
112     if(lpFilename) {         /* disk based metafile */
113         DWORD w;
114         if((hFile = CreateFileA(lpFilename, GENERIC_WRITE, 0, NULL,
115                                 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
116             HeapFree( GetProcessHeap(), 0, mh2 );
117             return 0;
118         }
119         WriteFile(hFile, mh2, mh2->mtSize * 2, &w, NULL);
120         CloseHandle(hFile);
121         mh2 = MF_CreateMetaHeaderDisk(mh2, lpFilename, FALSE);
122     }
123
124     return MF_Create_HMETAFILE16( mh2 );
125 }
126
127 /******************************************************************
128  *         IsValidMetaFile   (GDI.410)
129  *
130  *  Attempts to check if a given metafile is correctly formatted.
131  *  Currently, the only things verified are several properties of the
132  *  header.
133  *
134  * RETURNS
135  *  TRUE if hmf passes some tests for being a valid metafile, FALSE otherwise.
136  *
137  * BUGS
138  *  This is not exactly what windows does, see _Undocumented_Windows_
139  *  for details.
140  */
141 BOOL16 WINAPI IsValidMetaFile16(HMETAFILE16 hmf)
142 {
143     BOOL16 res=FALSE;
144     METAHEADER *mh = MF_GetMetaHeader16(hmf);
145     if (mh) {
146         if (mh->mtType == METAFILE_MEMORY || mh->mtType == METAFILE_DISK)
147             if (mh->mtHeaderSize == MFHEADERSIZE/sizeof(INT16))
148                 if (mh->mtVersion == MFVERSION)
149                     res=TRUE;
150         MF_ReleaseMetaHeader16(hmf);
151     }
152     TRACE("IsValidMetaFile %x => %d\n",hmf,res);
153     return res;
154 }
155
156 /******************************************************************
157  *         PlayMetaFile   (GDI.123)
158  *
159  */
160 BOOL16 WINAPI PlayMetaFile16( HDC16 hdc, HMETAFILE16 hmf )
161 {
162     BOOL16 ret;
163     METAHEADER *mh = MF_GetMetaHeader16( hmf );
164     ret = MF_PlayMetaFile( HDC_32(hdc), mh );
165     MF_ReleaseMetaHeader16( hmf );
166     return ret;
167 }
168
169
170 /******************************************************************
171  *            EnumMetaFile   (GDI.175)
172  *
173  */
174 BOOL16 WINAPI EnumMetaFile16( HDC16 hdc16, HMETAFILE16 hmf,
175                               MFENUMPROC16 lpEnumFunc, LPARAM lpData )
176 {
177     METAHEADER *mh = MF_GetMetaHeader16(hmf);
178     METARECORD *mr;
179     HANDLETABLE16 *ht;
180     HDC hdc = HDC_32(hdc16);
181     HGLOBAL16 hHT;
182     SEGPTR spht;
183     unsigned int offset = 0;
184     WORD i, seg;
185     HPEN hPen;
186     HBRUSH hBrush;
187     HFONT hFont;
188     WORD args[8];
189     BOOL16 result = TRUE, loaded = FALSE;
190
191     TRACE("(%p, %04x, %p, %08lx)\n", hdc, hmf, lpEnumFunc, lpData);
192
193     if(!mh) return FALSE;
194     if(mh->mtType == METAFILE_DISK) { /* Create a memory-based copy */
195         mh = MF_LoadDiskBasedMetaFile(mh);
196         if(!mh) return FALSE;
197         loaded = TRUE;
198     }
199
200     /* save the current pen, brush and font */
201     hPen = GetCurrentObject(hdc, OBJ_PEN);
202     hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
203     hFont = GetCurrentObject(hdc, OBJ_FONT);
204
205     /* create the handle table */
206
207     hHT = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT,
208                      sizeof(HANDLETABLE16) * mh->mtNoObjects);
209     spht = WOWGlobalLock16(hHT);
210
211     seg = hmf | 7;
212     offset = mh->mtHeaderSize * 2;
213
214     /* loop through metafile records */
215
216     args[7] = hdc16;
217     args[6] = SELECTOROF(spht);
218     args[5] = OFFSETOF(spht);
219     args[4] = seg + (HIWORD(offset) << __AHSHIFT);
220     args[3] = LOWORD(offset);
221     args[2] = mh->mtNoObjects;
222     args[1] = HIWORD(lpData);
223     args[0] = LOWORD(lpData);
224
225     while (offset < (mh->mtSize * 2))
226     {
227         DWORD ret;
228
229         mr = (METARECORD *)((char *)mh + offset);
230
231         WOWCallback16Ex( (DWORD)lpEnumFunc, WCB16_PASCAL, sizeof(args), args, &ret );
232         if (!LOWORD(ret))
233         {
234             result = FALSE;
235             break;
236         }
237
238         offset += (mr->rdSize * 2);
239         args[4] = seg + (HIWORD(offset) << __AHSHIFT);
240         args[3] = LOWORD(offset);
241     }
242
243     SelectObject(hdc, hBrush);
244     SelectObject(hdc, hPen);
245     SelectObject(hdc, hFont);
246
247     ht = (HANDLETABLE16 *)GlobalLock16(hHT);
248
249     /* free objects in handle table */
250     for(i = 0; i < mh->mtNoObjects; i++)
251       if(*(ht->objectHandle + i) != 0)
252         DeleteObject( (HGDIOBJ)(ULONG_PTR)(*(ht->objectHandle + i) ));
253
254     /* free handle table */
255     GlobalFree16(hHT);
256     if(loaded)
257         HeapFree( GetProcessHeap(), 0, mh );
258     MF_ReleaseMetaHeader16(hmf);
259     return result;
260 }
261
262 /******************************************************************
263  *         GetMetaFileBits   (GDI.159)
264  *
265  * Trade in a metafile object handle for a handle to the metafile memory.
266  *
267  * PARAMS
268  *  hmf [I] metafile handle
269  */
270
271 HGLOBAL16 WINAPI GetMetaFileBits16( HMETAFILE16 hmf )
272 {
273     TRACE("hMem out: %04x\n", hmf);
274     return hmf;
275 }
276
277 /******************************************************************
278  *         SetMetaFileBits   (GDI.160)
279  *
280  * Trade in a metafile memory handle for a handle to a metafile object.
281  * The memory region should hold a proper metafile, otherwise
282  * problems will occur when it is used. Validity of the memory is not
283  * checked. The function is essentially just the identity function.
284  *
285  * PARAMS
286  *  hMem [I] handle to a memory region holding a metafile
287  *
288  * RETURNS
289  *  Handle to a metafile on success, NULL on failure..
290  */
291 HMETAFILE16 WINAPI SetMetaFileBits16( HGLOBAL16 hMem )
292 {
293     TRACE("hmf out: %04x\n", hMem);
294
295     return hMem;
296 }
297
298 /******************************************************************
299  *         SetMetaFileBitsBetter   (GDI.196)
300  *
301  * Trade in a metafile memory handle for a handle to a metafile object,
302  * making a cursory check (using IsValidMetaFile()) that the memory
303  * handle points to a valid metafile.
304  *
305  * RETURNS
306  *  Handle to a metafile on success, NULL on failure..
307  */
308 HMETAFILE16 WINAPI SetMetaFileBitsBetter16( HMETAFILE16 hMeta )
309 {
310     if( IsValidMetaFile16( hMeta ) )
311         return GlobalReAlloc16( hMeta, 0, GMEM_SHARE | GMEM_NODISCARD | GMEM_MODIFY);
312     return (HMETAFILE16)0;
313 }