mshtml: Rename call_event to fire_event.
[wine] / dlls / gdi32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
30
31
32 /******************************************************************
33  *         EMFDRV_AddHandle
34  */
35 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
36 {
37     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
38     UINT index;
39
40     for(index = 0; index < physDev->handles_size; index++)
41         if(physDev->handles[index] == 0) break;
42     if(index == physDev->handles_size) {
43         physDev->handles_size += HANDLE_LIST_INC;
44         physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
45                                        physDev->handles,
46                                        physDev->handles_size * sizeof(physDev->handles[0]));
47     }
48     physDev->handles[index] = obj;
49
50     physDev->cur_handles++;
51     if(physDev->cur_handles > physDev->emh->nHandles)
52         physDev->emh->nHandles++;
53
54     return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
55 }
56
57 /******************************************************************
58  *         EMFDRV_FindObject
59  */
60 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
61 {
62     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
63     UINT index;
64
65     for(index = 0; index < physDev->handles_size; index++)
66         if(physDev->handles[index] == obj) break;
67
68     if(index == physDev->handles_size) return 0;
69
70     return index + 1;
71 }
72
73
74 /******************************************************************
75  *         EMFDRV_DeleteObject
76  */
77 BOOL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
78 {
79     EMRDELETEOBJECT emr;
80     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
81     UINT index;
82     BOOL ret = TRUE;
83
84     if(!(index = EMFDRV_FindObject(dev, obj))) return 0;
85
86     emr.emr.iType = EMR_DELETEOBJECT;
87     emr.emr.nSize = sizeof(emr);
88     emr.ihObject = index;
89
90     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
91         ret = FALSE;
92
93     physDev->handles[index - 1] = 0;
94     physDev->cur_handles--;
95     return ret;
96 }
97
98
99 /***********************************************************************
100  *           EMFDRV_SelectBitmap
101  */
102 HBITMAP EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
103 {
104     return 0;
105 }
106
107
108 /* Internal helper for EMFDRV_CreateBrushIndirect():
109  * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
110  */
111 static inline void EMFDRV_PadTo32(LPBYTE lpRows, int height, int width)
112 {
113     int bytes16 = 2 * ((width + 15) / 16);
114     int bytes32 = 4 * ((width + 31) / 32);
115     LPBYTE lpSrc, lpDst;
116     int i;
117
118     if (!height)
119         return;
120
121     height = abs(height) - 1;
122     lpSrc = lpRows + height * bytes16;
123     lpDst = lpRows + height * bytes32;
124
125     /* Note that we work backwards so we can re-pad in place */
126     while (height >= 0)
127     {
128         for (i = bytes32; i > bytes16; i--)
129             lpDst[i - 1] = 0; /* Zero the padding bytes */
130         for (; i > 0; i--)
131             lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
132         lpSrc -= bytes16;
133         lpDst -= bytes32;
134         height--;
135     }
136 }
137
138 /***********************************************************************
139  *           EMFDRV_CreateBrushIndirect
140  */
141 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
142 {
143     DWORD index = 0;
144     LOGBRUSH logbrush;
145
146     if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
147
148     switch (logbrush.lbStyle) {
149     case BS_SOLID:
150     case BS_HATCHED:
151     case BS_NULL:
152       {
153         EMRCREATEBRUSHINDIRECT emr;
154         emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
155         emr.emr.nSize = sizeof(emr);
156         emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
157         emr.lb.lbStyle = logbrush.lbStyle;
158         emr.lb.lbColor = logbrush.lbColor;
159         emr.lb.lbHatch = logbrush.lbHatch;
160
161         if(!EMFDRV_WriteRecord( dev, &emr.emr ))
162             index = 0;
163       }
164       break;
165     case BS_DIBPATTERN:
166       {
167         EMRCREATEDIBPATTERNBRUSHPT *emr;
168         DWORD bmSize, biSize, size;
169         BITMAPINFO *info = GlobalLock( (HGLOBAL)logbrush.lbHatch );
170
171         if (info->bmiHeader.biCompression)
172             bmSize = info->bmiHeader.biSizeImage;
173         else
174             bmSize = get_dib_image_size( info );
175         biSize = bitmap_info_size(info, LOWORD(logbrush.lbColor));
176         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
177         emr = HeapAlloc( GetProcessHeap(), 0, size );
178         if(!emr) break;
179         emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
180         emr->emr.nSize = size;
181         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
182         emr->iUsage = LOWORD(logbrush.lbColor);
183         emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
184         emr->cbBmi = biSize;
185         emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
186         emr->cbBits = bmSize;
187         memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
188                biSize + bmSize );
189
190         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
191             index = 0;
192         HeapFree( GetProcessHeap(), 0, emr );
193         GlobalUnlock( (HGLOBAL)logbrush.lbHatch );
194       }
195       break;
196
197     case BS_PATTERN:
198       {
199         EMRCREATEDIBPATTERNBRUSHPT *emr;
200         BITMAPINFOHEADER *info;
201         BITMAP bm;
202         DWORD bmSize, biSize, size;
203
204         GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
205
206         if (bm.bmBitsPixel != 1 || bm.bmPlanes != 1)
207         {
208             FIXME("Trying to create a color pattern brush\n");
209             break;
210         }
211
212         /* BMP will be aligned to 32 bits, not 16 */
213         bmSize = get_dib_stride(bm.bmWidth, bm.bmBitsPixel) * bm.bmHeight;
214
215         biSize = sizeof(BITMAPINFOHEADER);
216         /* FIXME: There is an extra DWORD written by native before the BMI.
217          *        Not sure what its meant to contain.
218          */
219         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize + sizeof(DWORD);
220
221         emr = HeapAlloc( GetProcessHeap(), 0, size );
222         if(!emr)
223           break;
224
225         info = (BITMAPINFOHEADER *)((LPBYTE)emr +
226                 sizeof(EMRCREATEDIBPATTERNBRUSHPT) + sizeof(DWORD));
227         info->biSize = sizeof(BITMAPINFOHEADER);
228         info->biWidth = bm.bmWidth;
229         info->biHeight = bm.bmHeight;
230         info->biPlanes = bm.bmPlanes;
231         info->biBitCount = bm.bmBitsPixel;
232         info->biSizeImage = bmSize;
233         GetBitmapBits((HANDLE)logbrush.lbHatch,
234                       bm.bmHeight * get_bitmap_stride(bm.bmWidth, bm.bmBitsPixel),
235                       (LPBYTE)info + sizeof(BITMAPINFOHEADER));
236
237         /* Change the padding to be DIB compatible if needed */
238         if (bm.bmWidth & 31)
239             EMFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFOHEADER), bm.bmWidth, bm.bmHeight);
240
241         emr->emr.iType = EMR_CREATEMONOBRUSH;
242         emr->emr.nSize = size;
243         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
244         /* Presumably to reduce the size of the written EMF, MS supports an
245          * undocumented iUsage value of 2, indicating a mono bitmap without the
246          * 8 byte 2 entry black/white palette. Stupidly, they could have saved
247          * over 20 bytes more by also ignoring the BITMAPINFO fields that are
248          * irrelevant/constant for monochrome bitmaps.
249          * FIXME: It may be that the DIB functions themselves accept this value.
250          */
251         emr->iUsage = DIB_PAL_MONO;
252         emr->offBmi = (LPBYTE)info - (LPBYTE)emr;
253         emr->cbBmi = biSize;
254         emr->offBits = emr->offBmi + biSize;
255         emr->cbBits = bmSize;
256
257         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
258             index = 0;
259         HeapFree( GetProcessHeap(), 0, emr );
260       }
261       break;
262
263     default:
264         FIXME("Unknown style %x\n", logbrush.lbStyle);
265         break;
266     }
267     return index;
268 }
269
270
271 /***********************************************************************
272  *           EMFDRV_SelectBrush
273  */
274 HBRUSH EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
275 {
276     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
277     EMRSELECTOBJECT emr;
278     DWORD index;
279     int i;
280
281     if (physDev->restoring) return hBrush;  /* don't output SelectObject records during RestoreDC */
282
283     /* If the object is a stock brush object, do not need to create it.
284      * See definitions in  wingdi.h for range of stock brushes.
285      * We do however have to handle setting the higher order bit to
286      * designate that this is a stock object.
287      */
288     for (i = WHITE_BRUSH; i <= DC_BRUSH; i++)
289     {
290         if (hBrush == GetStockObject(i))
291         {
292             index = i | 0x80000000;
293             goto found;
294         }
295     }
296     if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
297         goto found;
298
299     if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
300     GDI_hdc_using_object(hBrush, dev->hdc);
301
302  found:
303     emr.emr.iType = EMR_SELECTOBJECT;
304     emr.emr.nSize = sizeof(emr);
305     emr.ihObject = index;
306     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
307 }
308
309
310 /******************************************************************
311  *         EMFDRV_CreateFontIndirect
312  */
313 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
314 {
315     DWORD index = 0;
316     EMREXTCREATEFONTINDIRECTW emr;
317     int i;
318
319     if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
320
321     emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
322     emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
323     emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
324     emr.elfw.elfFullName[0] = '\0';
325     emr.elfw.elfStyle[0]    = '\0';
326     emr.elfw.elfVersion     = 0;
327     emr.elfw.elfStyleSize   = 0;
328     emr.elfw.elfMatch       = 0;
329     emr.elfw.elfReserved    = 0;
330     for(i = 0; i < ELF_VENDOR_SIZE; i++)
331         emr.elfw.elfVendorId[i] = 0;
332     emr.elfw.elfCulture                 = PAN_CULTURE_LATIN;
333     emr.elfw.elfPanose.bFamilyType      = PAN_NO_FIT;
334     emr.elfw.elfPanose.bSerifStyle      = PAN_NO_FIT;
335     emr.elfw.elfPanose.bWeight          = PAN_NO_FIT;
336     emr.elfw.elfPanose.bProportion      = PAN_NO_FIT;
337     emr.elfw.elfPanose.bContrast        = PAN_NO_FIT;
338     emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
339     emr.elfw.elfPanose.bArmStyle        = PAN_NO_FIT;
340     emr.elfw.elfPanose.bLetterform      = PAN_NO_FIT;
341     emr.elfw.elfPanose.bMidline         = PAN_NO_FIT;
342     emr.elfw.elfPanose.bXHeight         = PAN_NO_FIT;
343
344     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
345         index = 0;
346     return index;
347 }
348
349
350 /***********************************************************************
351  *           EMFDRV_SelectFont
352  */
353 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, HANDLE gdiFont )
354 {
355     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
356     EMRSELECTOBJECT emr;
357     DWORD index;
358     int i;
359
360     if (physDev->restoring) return 0;  /* don't output SelectObject records during RestoreDC */
361
362     /* If the object is a stock font object, do not need to create it.
363      * See definitions in  wingdi.h for range of stock fonts.
364      * We do however have to handle setting the higher order bit to
365      * designate that this is a stock object.
366      */
367
368     for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
369     {
370         if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
371         {
372             index = i | 0x80000000;
373             goto found;
374         }
375     }
376
377     if((index = EMFDRV_FindObject(dev, hFont)) != 0)
378         goto found;
379
380     if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return HGDI_ERROR;
381     GDI_hdc_using_object(hFont, dev->hdc);
382
383  found:
384     emr.emr.iType = EMR_SELECTOBJECT;
385     emr.emr.nSize = sizeof(emr);
386     emr.ihObject = index;
387     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
388         return HGDI_ERROR;
389     return 0;
390 }
391
392
393
394 /******************************************************************
395  *         EMFDRV_CreatePenIndirect
396  */
397 static DWORD EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen)
398 {
399     EMRCREATEPEN emr;
400     DWORD index = 0;
401
402     if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
403     {
404         /* must be an extended pen */
405         EXTLOGPEN *elp;
406         INT size = GetObjectW( hPen, 0, NULL );
407
408         if (!size) return 0;
409
410         elp = HeapAlloc( GetProcessHeap(), 0, size );
411
412         GetObjectW( hPen, size, elp );
413         /* FIXME: add support for user style pens */
414         emr.lopn.lopnStyle = elp->elpPenStyle;
415         emr.lopn.lopnWidth.x = elp->elpWidth;
416         emr.lopn.lopnWidth.y = 0;
417         emr.lopn.lopnColor = elp->elpColor;
418
419         HeapFree( GetProcessHeap(), 0, elp );
420     }
421
422     emr.emr.iType = EMR_CREATEPEN;
423     emr.emr.nSize = sizeof(emr);
424     emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
425
426     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
427         index = 0;
428     return index;
429 }
430
431 /******************************************************************
432  *         EMFDRV_SelectPen
433  */
434 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
435 {
436     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
437     EMRSELECTOBJECT emr;
438     DWORD index;
439     int i;
440
441     if (physDev->restoring) return hPen;  /* don't output SelectObject records during RestoreDC */
442
443     /* If the object is a stock pen object, do not need to create it.
444      * See definitions in  wingdi.h for range of stock pens.
445      * We do however have to handle setting the higher order bit to
446      * designate that this is a stock object.
447      */
448
449     for (i = WHITE_PEN; i <= DC_PEN; i++)
450     {
451         if (hPen == GetStockObject(i))
452         {
453             index = i | 0x80000000;
454             goto found;
455         }
456     }
457     if((index = EMFDRV_FindObject(dev, hPen)) != 0)
458         goto found;
459
460     if (!(index = EMFDRV_CreatePenIndirect(dev, hPen))) return 0;
461     GDI_hdc_using_object(hPen, dev->hdc);
462
463  found:
464     emr.emr.iType = EMR_SELECTOBJECT;
465     emr.emr.nSize = sizeof(emr);
466     emr.ihObject = index;
467     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
468 }
469
470
471 /******************************************************************
472  *         EMFDRV_CreatePalette
473  */
474 static DWORD EMFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPal)
475 {
476     WORD i;
477     struct {
478         EMRCREATEPALETTE hdr;
479         PALETTEENTRY entry[255];
480     } pal;
481
482     memset( &pal, 0, sizeof(pal) );
483
484     if (!GetObjectW( hPal, sizeof(pal.hdr.lgpl) + sizeof(pal.entry), &pal.hdr.lgpl ))
485         return 0;
486
487     for (i = 0; i < pal.hdr.lgpl.palNumEntries; i++)
488         pal.hdr.lgpl.palPalEntry[i].peFlags = 0;
489
490     pal.hdr.emr.iType = EMR_CREATEPALETTE;
491     pal.hdr.emr.nSize = sizeof(pal.hdr) + pal.hdr.lgpl.palNumEntries * sizeof(PALETTEENTRY);
492     pal.hdr.ihPal = EMFDRV_AddHandle( dev, hPal );
493
494     if (!EMFDRV_WriteRecord( dev, &pal.hdr.emr ))
495         pal.hdr.ihPal = 0;
496     return pal.hdr.ihPal;
497 }
498
499 /******************************************************************
500  *         EMFDRV_SelectPalette
501  */
502 HPALETTE EMFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPal, BOOL force )
503 {
504     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
505     EMRSELECTPALETTE emr;
506     DWORD index;
507
508     if (physDev->restoring) return hPal;  /* don't output SelectObject records during RestoreDC */
509
510     if (hPal == GetStockObject( DEFAULT_PALETTE ))
511     {
512         index = DEFAULT_PALETTE | 0x80000000;
513         goto found;
514     }
515
516     if ((index = EMFDRV_FindObject( dev, hPal )) != 0)
517         goto found;
518
519     if (!(index = EMFDRV_CreatePalette( dev, hPal ))) return 0;
520     GDI_hdc_using_object( hPal, dev->hdc );
521
522 found:
523     emr.emr.iType = EMR_SELECTPALETTE;
524     emr.emr.nSize = sizeof(emr);
525     emr.ihPal = index;
526     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPal : 0;
527 }
528
529 /******************************************************************
530  *         EMFDRV_SetDCBrushColor
531  */
532 COLORREF EMFDRV_SetDCBrushColor( PHYSDEV dev, COLORREF color )
533 {
534     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
535     EMRSELECTOBJECT emr;
536     DWORD index;
537
538     if (GetCurrentObject( dev->hdc, OBJ_BRUSH ) != GetStockObject( DC_BRUSH )) return color;
539
540     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
541     if (!(physDev->dc_brush = CreateSolidBrush( color ))) return CLR_INVALID;
542     if (!(index = EMFDRV_CreateBrushIndirect(dev, physDev->dc_brush ))) return CLR_INVALID;
543     GDI_hdc_using_object( physDev->dc_brush, dev->hdc );
544     emr.emr.iType = EMR_SELECTOBJECT;
545     emr.emr.nSize = sizeof(emr);
546     emr.ihObject = index;
547     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
548 }
549
550 /******************************************************************
551  *         EMFDRV_SetDCPenColor
552  */
553 COLORREF EMFDRV_SetDCPenColor( PHYSDEV dev, COLORREF color )
554 {
555     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
556     EMRSELECTOBJECT emr;
557     DWORD index;
558     LOGPEN logpen = { PS_SOLID, { 0, 0 }, color };
559
560     if (GetCurrentObject( dev->hdc, OBJ_PEN ) != GetStockObject( DC_PEN )) return color;
561
562     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
563     if (!(physDev->dc_pen = CreatePenIndirect( &logpen ))) return CLR_INVALID;
564     if (!(index = EMFDRV_CreatePenIndirect(dev, physDev->dc_pen))) return CLR_INVALID;
565     GDI_hdc_using_object( physDev->dc_pen, dev->hdc );
566     emr.emr.iType = EMR_SELECTOBJECT;
567     emr.emr.nSize = sizeof(emr);
568     emr.ihObject = index;
569     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
570 }
571
572 /******************************************************************
573  *         EMFDRV_GdiComment
574  */
575 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
576 {
577     EMRGDICOMMENT *emr;
578     UINT total, rounded_size;
579     BOOL ret;
580
581     rounded_size = (bytes+3) & ~3;
582     total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
583
584     emr = HeapAlloc(GetProcessHeap(), 0, total);
585     emr->emr.iType = EMR_GDICOMMENT;
586     emr->emr.nSize = total;
587     emr->cbData = bytes;
588     memset(&emr->Data[bytes], 0, rounded_size - bytes);
589     memcpy(&emr->Data[0], buffer, bytes);
590
591     ret = EMFDRV_WriteRecord( dev, &emr->emr );
592
593     HeapFree(GetProcessHeap(), 0, emr);
594
595     return ret;
596 }