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