Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / gdi / enhmfdrv / objects.c
1 /*
2  * Enhanced MetaFile objects
3  *
4  * Copyright 1999 Huw D M Davies
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "gdi.h"
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "gdi_private.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
31
32
33 /******************************************************************
34  *         EMFDRV_AddHandle
35  */
36 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
37 {
38     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
39     UINT index;
40
41     for(index = 0; index < physDev->handles_size; index++)
42         if(physDev->handles[index] == 0) break;
43     if(index == physDev->handles_size) {
44         physDev->handles_size += HANDLE_LIST_INC;
45         physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46                                        physDev->handles,
47                                        physDev->handles_size * sizeof(physDev->handles[0]));
48     }
49     physDev->handles[index] = obj;
50
51     physDev->cur_handles++;
52     if(physDev->cur_handles > physDev->emh->nHandles)
53         physDev->emh->nHandles++;
54
55     return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
56 }
57
58 /******************************************************************
59  *         EMFDRV_FindObject
60  */
61 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
62 {
63     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
64     UINT index;
65
66     for(index = 0; index < physDev->handles_size; index++)
67         if(physDev->handles[index] == obj) break;
68
69     if(index == physDev->handles_size) return 0;
70
71     return index + 1;
72 }
73
74
75 /******************************************************************
76  *         EMFDRV_DeleteObject
77  */
78 BOOL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
79 {
80     EMRDELETEOBJECT emr;
81     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
82     UINT index;
83     BOOL ret = TRUE;
84
85     if(!(index = EMFDRV_FindObject(dev, obj))) return 0;
86
87     emr.emr.iType = EMR_DELETEOBJECT;
88     emr.emr.nSize = sizeof(emr);
89     emr.ihObject = index;
90
91     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
92         ret = FALSE;
93
94     physDev->handles[index - 1] = 0;
95     physDev->cur_handles--;
96     return ret;
97 }
98
99   
100 /***********************************************************************
101  *           EMFDRV_SelectBitmap
102  */
103 HBITMAP EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
104 {
105     return 0;
106 }
107
108
109 /***********************************************************************
110  *           EMFDRV_CreateBrushIndirect
111  */
112 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
113 {
114     DWORD index = 0;
115     LOGBRUSH logbrush;
116
117     if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
118
119     switch (logbrush.lbStyle) {
120     case BS_SOLID:
121     case BS_HATCHED:
122     case BS_NULL:
123       {
124         EMRCREATEBRUSHINDIRECT emr;
125         emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
126         emr.emr.nSize = sizeof(emr);
127         emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
128         emr.lb = logbrush;
129
130         if(!EMFDRV_WriteRecord( dev, &emr.emr ))
131             index = 0;
132       }
133       break;
134     case BS_DIBPATTERN:
135       {
136         EMRCREATEDIBPATTERNBRUSHPT *emr;
137         DWORD bmSize, biSize, size;
138         BITMAPINFO *info = GlobalLock16(logbrush.lbHatch);
139
140         if (info->bmiHeader.biCompression)
141             bmSize = info->bmiHeader.biSizeImage;
142         else
143             bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
144                                           info->bmiHeader.biHeight,
145                                           info->bmiHeader.biBitCount);
146         biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush.lbColor));
147         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
148         emr = HeapAlloc( GetProcessHeap(), 0, size );
149         if(!emr) break;
150         emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
151         emr->emr.nSize = size;
152         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
153         emr->iUsage = LOWORD(logbrush.lbColor);
154         emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
155         emr->cbBmi = biSize;
156         emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
157         emr->cbBits = bmSize;
158         memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
159                biSize + bmSize );
160
161         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
162             index = 0;
163         HeapFree( GetProcessHeap(), 0, emr );
164         GlobalUnlock16(logbrush.lbHatch);
165       }
166       break;
167
168     case BS_PATTERN:
169         FIXME("Unsupported style %x\n",
170               logbrush.lbStyle);
171         break;
172     default:
173         FIXME("Unknown style %x\n", logbrush.lbStyle);
174         break;
175     }
176     return index;
177 }
178
179
180 /***********************************************************************
181  *           EMFDRV_SelectBrush
182  */
183 HBRUSH EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
184 {
185     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
186     EMRSELECTOBJECT emr;
187     DWORD index;
188     int i;
189
190     /* If the object is a stock brush object, do not need to create it.
191      * See definitions in  wingdi.h for range of stock brushes.
192      * We do however have to handle setting the higher order bit to
193      * designate that this is a stock object.
194      */
195     for (i = WHITE_BRUSH; i <= NULL_BRUSH; i++)
196     {
197         if (hBrush == GetStockObject(i))
198         {
199             index = i | 0x80000000;
200             goto found;
201         }
202     }
203     if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
204         goto found;
205
206     if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
207     GDI_hdc_using_object(hBrush, physDev->hdc);
208
209  found:
210     emr.emr.iType = EMR_SELECTOBJECT;
211     emr.emr.nSize = sizeof(emr);
212     emr.ihObject = index;
213     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
214 }
215
216
217 /******************************************************************
218  *         EMFDRV_CreateFontIndirect
219  */
220 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
221 {
222     DWORD index = 0;
223     EMREXTCREATEFONTINDIRECTW emr;
224     int i;
225
226     if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
227
228     emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
229     emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
230     emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
231     emr.elfw.elfFullName[0] = '\0';
232     emr.elfw.elfStyle[0]    = '\0';
233     emr.elfw.elfVersion     = 0;
234     emr.elfw.elfStyleSize   = 0;
235     emr.elfw.elfMatch       = 0;
236     emr.elfw.elfReserved    = 0;
237     for(i = 0; i < ELF_VENDOR_SIZE; i++)
238         emr.elfw.elfVendorId[i] = 0;
239     emr.elfw.elfCulture                 = PAN_CULTURE_LATIN;
240     emr.elfw.elfPanose.bFamilyType      = PAN_NO_FIT;
241     emr.elfw.elfPanose.bSerifStyle      = PAN_NO_FIT;
242     emr.elfw.elfPanose.bWeight          = PAN_NO_FIT;
243     emr.elfw.elfPanose.bProportion      = PAN_NO_FIT;
244     emr.elfw.elfPanose.bContrast        = PAN_NO_FIT;
245     emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
246     emr.elfw.elfPanose.bArmStyle        = PAN_NO_FIT;
247     emr.elfw.elfPanose.bLetterform      = PAN_NO_FIT;
248     emr.elfw.elfPanose.bMidline         = PAN_NO_FIT;
249     emr.elfw.elfPanose.bXHeight         = PAN_NO_FIT;
250
251     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
252         index = 0;
253     return index;
254 }
255
256
257 /***********************************************************************
258  *           EMFDRV_SelectFont
259  */
260 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, HANDLE gdiFont )
261 {
262     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
263     EMRSELECTOBJECT emr;
264     DWORD index;
265     int i;
266
267     /* If the object is a stock font object, do not need to create it.
268      * See definitions in  wingdi.h for range of stock fonts.
269      * We do however have to handle setting the higher order bit to
270      * designate that this is a stock object.
271      */
272
273     for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
274     {
275         if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
276         {
277             index = i | 0x80000000;
278             goto found;
279         }
280     }
281
282     if((index = EMFDRV_FindObject(dev, hFont)) != 0)
283         goto found;
284
285     if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return HGDI_ERROR;
286     GDI_hdc_using_object(hFont, physDev->hdc);
287
288  found:
289     emr.emr.iType = EMR_SELECTOBJECT;
290     emr.emr.nSize = sizeof(emr);
291     emr.ihObject = index;
292     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
293         return HGDI_ERROR;
294     return 0;
295 }
296
297
298
299 /******************************************************************
300  *         EMFDRV_CreatePenIndirect
301  */
302 static HPEN EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen )
303 {
304     EMRCREATEPEN emr;
305     DWORD index = 0;
306
307     if (!GetObjectA( hPen, sizeof(emr.lopn), &emr.lopn )) return 0;
308
309     emr.emr.iType = EMR_CREATEPEN;
310     emr.emr.nSize = sizeof(emr);
311     emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
312
313     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
314         index = 0;
315     return (HPEN)index;
316 }
317
318 /******************************************************************
319  *         EMFDRV_SelectPen
320  */
321 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
322 {
323     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
324     EMRSELECTOBJECT emr;
325     DWORD index;
326     int i;
327
328     /* If the object is a stock pen object, do not need to create it.
329      * See definitions in  wingdi.h for range of stock pens.
330      * We do however have to handle setting the higher order bit to
331      * designate that this is a stock object.
332      */
333
334     for (i = WHITE_PEN; i <= NULL_PEN; i++)
335     {
336         if (hPen == GetStockObject(i))
337         {
338             index = i | 0x80000000;
339             goto found;
340         }
341     }
342     if((index = EMFDRV_FindObject(dev, hPen)) != 0)
343         goto found;
344
345     if (!(index = (DWORD)EMFDRV_CreatePenIndirect(dev, hPen ))) return 0;
346     GDI_hdc_using_object(hPen, physDev->hdc);
347
348  found:
349     emr.emr.iType = EMR_SELECTOBJECT;
350     emr.emr.nSize = sizeof(emr);
351     emr.ihObject = index;
352     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
353 }
354
355
356 /******************************************************************
357  *         EMFDRV_GdiComment
358  */
359 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
360 {
361     EMRGDICOMMENT *emr;
362     UINT total, rounded_size;
363     BOOL ret;
364
365     rounded_size = (bytes+3) & ~3;
366     total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
367
368     emr = HeapAlloc(GetProcessHeap(), 0, total);
369     emr->emr.iType = EMR_GDICOMMENT;
370     emr->emr.nSize = total;
371     emr->cbData = bytes;
372     memset(&emr->Data[bytes], 0, rounded_size - bytes);
373     memcpy(&emr->Data[0], buffer, bytes);
374
375     ret = EMFDRV_WriteRecord( dev, &emr->emr );
376
377     HeapFree(GetProcessHeap(), 0, emr);
378
379     return ret;
380 }