dsound: Always enumerate the default device first.
[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 /***********************************************************************
109  *           EMFDRV_CreateBrushIndirect
110  */
111 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
112 {
113     DWORD index = 0;
114     LOGBRUSH logbrush;
115
116     if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
117
118     switch (logbrush.lbStyle) {
119     case BS_SOLID:
120     case BS_HATCHED:
121     case BS_NULL:
122       {
123         EMRCREATEBRUSHINDIRECT emr;
124         emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
125         emr.emr.nSize = sizeof(emr);
126         emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
127         emr.lb.lbStyle = logbrush.lbStyle;
128         emr.lb.lbColor = logbrush.lbColor;
129         emr.lb.lbHatch = logbrush.lbHatch;
130
131         if(!EMFDRV_WriteRecord( dev, &emr.emr ))
132             index = 0;
133       }
134       break;
135     case BS_DIBPATTERN:
136       {
137         EMRCREATEDIBPATTERNBRUSHPT *emr;
138         DWORD bmSize, biSize, size;
139         BITMAPINFO *info = GlobalLock( (HGLOBAL)logbrush.lbHatch );
140
141         if (info->bmiHeader.biCompression)
142             bmSize = info->bmiHeader.biSizeImage;
143         else
144             bmSize = get_dib_image_size( info );
145         biSize = bitmap_info_size(info, LOWORD(logbrush.lbColor));
146         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
147         emr = HeapAlloc( GetProcessHeap(), 0, size );
148         if(!emr) break;
149         emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
150         emr->emr.nSize = size;
151         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
152         emr->iUsage = LOWORD(logbrush.lbColor);
153         emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
154         emr->cbBmi = biSize;
155         emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
156         emr->cbBits = bmSize;
157         memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
158                biSize + bmSize );
159
160         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
161             index = 0;
162         HeapFree( GetProcessHeap(), 0, emr );
163         GlobalUnlock( (HGLOBAL)logbrush.lbHatch );
164       }
165       break;
166
167     case BS_PATTERN:
168       {
169         EMRCREATEDIBPATTERNBRUSHPT *emr;
170         char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
171         BITMAPINFO *dst_info, *src_info = (BITMAPINFO *)buffer;
172         struct gdi_image_bits bits;
173         DWORD size;
174
175         if (!get_bitmap_image( (HANDLE)logbrush.lbHatch, src_info, &bits )) break;
176         if (src_info->bmiHeader.biBitCount != 1)
177         {
178             FIXME("Trying to create a color pattern brush\n");
179             if (bits.free) bits.free( &bits );
180             break;
181         }
182
183         /* FIXME: There is an extra DWORD written by native before the BMI.
184          *        Not sure what its meant to contain.
185          */
186         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + sizeof(DWORD) +
187             sizeof(BITMAPINFOHEADER) + src_info->bmiHeader.biSizeImage;
188
189         emr = HeapAlloc( GetProcessHeap(), 0, size );
190         if(!emr)
191         {
192             if (bits.free) bits.free( &bits );
193             break;
194         }
195
196         dst_info = (BITMAPINFO *)((LPBYTE)(emr + 1) + sizeof(DWORD));
197         dst_info->bmiHeader = src_info->bmiHeader;
198         memcpy( &dst_info->bmiHeader + 1, bits.ptr, dst_info->bmiHeader.biSizeImage );
199         if (bits.free) bits.free( &bits );
200
201         emr->emr.iType = EMR_CREATEMONOBRUSH;
202         emr->emr.nSize = size;
203         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
204         /* Presumably to reduce the size of the written EMF, MS supports an
205          * undocumented iUsage value of 2, indicating a mono bitmap without the
206          * 8 byte 2 entry black/white palette. Stupidly, they could have saved
207          * over 20 bytes more by also ignoring the BITMAPINFO fields that are
208          * irrelevant/constant for monochrome bitmaps.
209          * FIXME: It may be that the DIB functions themselves accept this value.
210          */
211         emr->iUsage = DIB_PAL_MONO;
212         emr->offBmi = (LPBYTE)dst_info - (LPBYTE)emr;
213         emr->cbBmi = sizeof( BITMAPINFOHEADER );
214         emr->offBits = emr->offBmi + emr->cbBmi;
215         emr->cbBits = dst_info->bmiHeader.biSizeImage;
216
217         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
218             index = 0;
219         HeapFree( GetProcessHeap(), 0, emr );
220       }
221       break;
222
223     default:
224         FIXME("Unknown style %x\n", logbrush.lbStyle);
225         break;
226     }
227     return index;
228 }
229
230
231 /***********************************************************************
232  *           EMFDRV_SelectBrush
233  */
234 HBRUSH EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
235 {
236     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
237     EMRSELECTOBJECT emr;
238     DWORD index;
239     int i;
240
241     if (physDev->restoring) return hBrush;  /* don't output SelectObject records during RestoreDC */
242
243     /* If the object is a stock brush object, do not need to create it.
244      * See definitions in  wingdi.h for range of stock brushes.
245      * We do however have to handle setting the higher order bit to
246      * designate that this is a stock object.
247      */
248     for (i = WHITE_BRUSH; i <= DC_BRUSH; i++)
249     {
250         if (hBrush == GetStockObject(i))
251         {
252             index = i | 0x80000000;
253             goto found;
254         }
255     }
256     if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
257         goto found;
258
259     if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
260     GDI_hdc_using_object(hBrush, dev->hdc);
261
262  found:
263     emr.emr.iType = EMR_SELECTOBJECT;
264     emr.emr.nSize = sizeof(emr);
265     emr.ihObject = index;
266     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
267 }
268
269
270 /******************************************************************
271  *         EMFDRV_CreateFontIndirect
272  */
273 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
274 {
275     DWORD index = 0;
276     EMREXTCREATEFONTINDIRECTW emr;
277     int i;
278
279     if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
280
281     emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
282     emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
283     emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
284     emr.elfw.elfFullName[0] = '\0';
285     emr.elfw.elfStyle[0]    = '\0';
286     emr.elfw.elfVersion     = 0;
287     emr.elfw.elfStyleSize   = 0;
288     emr.elfw.elfMatch       = 0;
289     emr.elfw.elfReserved    = 0;
290     for(i = 0; i < ELF_VENDOR_SIZE; i++)
291         emr.elfw.elfVendorId[i] = 0;
292     emr.elfw.elfCulture                 = PAN_CULTURE_LATIN;
293     emr.elfw.elfPanose.bFamilyType      = PAN_NO_FIT;
294     emr.elfw.elfPanose.bSerifStyle      = PAN_NO_FIT;
295     emr.elfw.elfPanose.bWeight          = PAN_NO_FIT;
296     emr.elfw.elfPanose.bProportion      = PAN_NO_FIT;
297     emr.elfw.elfPanose.bContrast        = PAN_NO_FIT;
298     emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
299     emr.elfw.elfPanose.bArmStyle        = PAN_NO_FIT;
300     emr.elfw.elfPanose.bLetterform      = PAN_NO_FIT;
301     emr.elfw.elfPanose.bMidline         = PAN_NO_FIT;
302     emr.elfw.elfPanose.bXHeight         = PAN_NO_FIT;
303
304     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
305         index = 0;
306     return index;
307 }
308
309
310 /***********************************************************************
311  *           EMFDRV_SelectFont
312  */
313 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont )
314 {
315     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
316     EMRSELECTOBJECT emr;
317     DWORD index;
318     int i;
319
320     if (physDev->restoring) goto done;  /* don't output SelectObject records during RestoreDC */
321
322     /* If the object is a stock font object, do not need to create it.
323      * See definitions in  wingdi.h for range of stock fonts.
324      * We do however have to handle setting the higher order bit to
325      * designate that this is a stock object.
326      */
327
328     for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
329     {
330         if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
331         {
332             index = i | 0x80000000;
333             goto found;
334         }
335     }
336
337     if((index = EMFDRV_FindObject(dev, hFont)) != 0)
338         goto found;
339
340     if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return 0;
341     GDI_hdc_using_object(hFont, dev->hdc);
342
343  found:
344     emr.emr.iType = EMR_SELECTOBJECT;
345     emr.emr.nSize = sizeof(emr);
346     emr.ihObject = index;
347     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
348         return 0;
349 done:
350     dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
351     dev->funcs->pSelectFont( dev, hFont );
352     return hFont;
353 }
354
355
356
357 /******************************************************************
358  *         EMFDRV_CreatePenIndirect
359  */
360 static DWORD EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen)
361 {
362     EMRCREATEPEN emr;
363     DWORD index = 0;
364
365     if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
366     {
367         /* must be an extended pen */
368         EXTLOGPEN *elp;
369         INT size = GetObjectW( hPen, 0, NULL );
370
371         if (!size) return 0;
372
373         elp = HeapAlloc( GetProcessHeap(), 0, size );
374
375         GetObjectW( hPen, size, elp );
376         /* FIXME: add support for user style pens */
377         emr.lopn.lopnStyle = elp->elpPenStyle;
378         emr.lopn.lopnWidth.x = elp->elpWidth;
379         emr.lopn.lopnWidth.y = 0;
380         emr.lopn.lopnColor = elp->elpColor;
381
382         HeapFree( GetProcessHeap(), 0, elp );
383     }
384
385     emr.emr.iType = EMR_CREATEPEN;
386     emr.emr.nSize = sizeof(emr);
387     emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
388
389     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
390         index = 0;
391     return index;
392 }
393
394 /******************************************************************
395  *         EMFDRV_SelectPen
396  */
397 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
398 {
399     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
400     EMRSELECTOBJECT emr;
401     DWORD index;
402     int i;
403
404     if (physDev->restoring) return hPen;  /* don't output SelectObject records during RestoreDC */
405
406     /* If the object is a stock pen object, do not need to create it.
407      * See definitions in  wingdi.h for range of stock pens.
408      * We do however have to handle setting the higher order bit to
409      * designate that this is a stock object.
410      */
411
412     for (i = WHITE_PEN; i <= DC_PEN; i++)
413     {
414         if (hPen == GetStockObject(i))
415         {
416             index = i | 0x80000000;
417             goto found;
418         }
419     }
420     if((index = EMFDRV_FindObject(dev, hPen)) != 0)
421         goto found;
422
423     if (!(index = EMFDRV_CreatePenIndirect(dev, hPen))) return 0;
424     GDI_hdc_using_object(hPen, dev->hdc);
425
426  found:
427     emr.emr.iType = EMR_SELECTOBJECT;
428     emr.emr.nSize = sizeof(emr);
429     emr.ihObject = index;
430     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
431 }
432
433
434 /******************************************************************
435  *         EMFDRV_CreatePalette
436  */
437 static DWORD EMFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPal)
438 {
439     WORD i;
440     struct {
441         EMRCREATEPALETTE hdr;
442         PALETTEENTRY entry[255];
443     } pal;
444
445     memset( &pal, 0, sizeof(pal) );
446
447     if (!GetObjectW( hPal, sizeof(pal.hdr.lgpl) + sizeof(pal.entry), &pal.hdr.lgpl ))
448         return 0;
449
450     for (i = 0; i < pal.hdr.lgpl.palNumEntries; i++)
451         pal.hdr.lgpl.palPalEntry[i].peFlags = 0;
452
453     pal.hdr.emr.iType = EMR_CREATEPALETTE;
454     pal.hdr.emr.nSize = sizeof(pal.hdr) + pal.hdr.lgpl.palNumEntries * sizeof(PALETTEENTRY);
455     pal.hdr.ihPal = EMFDRV_AddHandle( dev, hPal );
456
457     if (!EMFDRV_WriteRecord( dev, &pal.hdr.emr ))
458         pal.hdr.ihPal = 0;
459     return pal.hdr.ihPal;
460 }
461
462 /******************************************************************
463  *         EMFDRV_SelectPalette
464  */
465 HPALETTE EMFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPal, BOOL force )
466 {
467     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
468     EMRSELECTPALETTE emr;
469     DWORD index;
470
471     if (physDev->restoring) return hPal;  /* don't output SelectObject records during RestoreDC */
472
473     if (hPal == GetStockObject( DEFAULT_PALETTE ))
474     {
475         index = DEFAULT_PALETTE | 0x80000000;
476         goto found;
477     }
478
479     if ((index = EMFDRV_FindObject( dev, hPal )) != 0)
480         goto found;
481
482     if (!(index = EMFDRV_CreatePalette( dev, hPal ))) return 0;
483     GDI_hdc_using_object( hPal, dev->hdc );
484
485 found:
486     emr.emr.iType = EMR_SELECTPALETTE;
487     emr.emr.nSize = sizeof(emr);
488     emr.ihPal = index;
489     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPal : 0;
490 }
491
492 /******************************************************************
493  *         EMFDRV_SetDCBrushColor
494  */
495 COLORREF EMFDRV_SetDCBrushColor( PHYSDEV dev, COLORREF color )
496 {
497     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
498     EMRSELECTOBJECT emr;
499     DWORD index;
500
501     if (GetCurrentObject( dev->hdc, OBJ_BRUSH ) != GetStockObject( DC_BRUSH )) return color;
502
503     if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
504     if (!(physDev->dc_brush = CreateSolidBrush( color ))) return CLR_INVALID;
505     if (!(index = EMFDRV_CreateBrushIndirect(dev, physDev->dc_brush ))) return CLR_INVALID;
506     GDI_hdc_using_object( physDev->dc_brush, dev->hdc );
507     emr.emr.iType = EMR_SELECTOBJECT;
508     emr.emr.nSize = sizeof(emr);
509     emr.ihObject = index;
510     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
511 }
512
513 /******************************************************************
514  *         EMFDRV_SetDCPenColor
515  */
516 COLORREF EMFDRV_SetDCPenColor( PHYSDEV dev, COLORREF color )
517 {
518     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
519     EMRSELECTOBJECT emr;
520     DWORD index;
521     LOGPEN logpen = { PS_SOLID, { 0, 0 }, color };
522
523     if (GetCurrentObject( dev->hdc, OBJ_PEN ) != GetStockObject( DC_PEN )) return color;
524
525     if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
526     if (!(physDev->dc_pen = CreatePenIndirect( &logpen ))) return CLR_INVALID;
527     if (!(index = EMFDRV_CreatePenIndirect(dev, physDev->dc_pen))) return CLR_INVALID;
528     GDI_hdc_using_object( physDev->dc_pen, dev->hdc );
529     emr.emr.iType = EMR_SELECTOBJECT;
530     emr.emr.nSize = sizeof(emr);
531     emr.ihObject = index;
532     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
533 }
534
535 /******************************************************************
536  *         EMFDRV_GdiComment
537  */
538 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
539 {
540     EMRGDICOMMENT *emr;
541     UINT total, rounded_size;
542     BOOL ret;
543
544     rounded_size = (bytes+3) & ~3;
545     total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
546
547     emr = HeapAlloc(GetProcessHeap(), 0, total);
548     emr->emr.iType = EMR_GDICOMMENT;
549     emr->emr.nSize = total;
550     emr->cbData = bytes;
551     memset(&emr->Data[bytes], 0, rounded_size - bytes);
552     memcpy(&emr->Data[0], buffer, bytes);
553
554     ret = EMFDRV_WriteRecord( dev, &emr->emr );
555
556     HeapFree(GetProcessHeap(), 0, emr);
557
558     return ret;
559 }