query: Add a stub implementation for LocateCatalogs.
[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., 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 "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 /* Internal helper for EMFDRV_CreateBrushIndirect():
110  * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
111  */
112 static inline void EMFDRV_PadTo32(LPBYTE lpRows, int height, int width)
113 {
114     int bytes16 = 2 * ((width + 15) / 16);
115     int bytes32 = 4 * ((width + 31) / 32);
116     LPBYTE lpSrc, lpDst;
117     int i;
118
119     if (!height)
120         return;
121
122     height = abs(height) - 1;
123     lpSrc = lpRows + height * bytes16;
124     lpDst = lpRows + height * bytes32;
125
126     /* Note that we work backwards so we can re-pad in place */
127     while (height >= 0)
128     {
129         for (i = bytes32; i > bytes16; i--)
130             lpDst[i - 1] = 0; /* Zero the padding bytes */
131         for (; i > 0; i--)
132             lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
133         lpSrc -= bytes16;
134         lpDst -= bytes32;
135         height--;
136     }
137 }
138
139 /***********************************************************************
140  *           EMFDRV_CreateBrushIndirect
141  */
142 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
143 {
144     DWORD index = 0;
145     LOGBRUSH logbrush;
146
147     if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
148
149     switch (logbrush.lbStyle) {
150     case BS_SOLID:
151     case BS_HATCHED:
152     case BS_NULL:
153       {
154         EMRCREATEBRUSHINDIRECT emr;
155         emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
156         emr.emr.nSize = sizeof(emr);
157         emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
158         emr.lb.lbStyle = logbrush.lbStyle;
159         emr.lb.lbColor = logbrush.lbColor;
160         emr.lb.lbHatch = logbrush.lbHatch;
161
162         if(!EMFDRV_WriteRecord( dev, &emr.emr ))
163             index = 0;
164       }
165       break;
166     case BS_DIBPATTERN:
167       {
168         EMRCREATEDIBPATTERNBRUSHPT *emr;
169         DWORD bmSize, biSize, size;
170         BITMAPINFO *info = GlobalLock16(logbrush.lbHatch);
171
172         if (info->bmiHeader.biCompression)
173             bmSize = info->bmiHeader.biSizeImage;
174         else
175             bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
176                                           info->bmiHeader.biHeight,
177                                           info->bmiHeader.biBitCount);
178         biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush.lbColor));
179         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
180         emr = HeapAlloc( GetProcessHeap(), 0, size );
181         if(!emr) break;
182         emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
183         emr->emr.nSize = size;
184         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
185         emr->iUsage = LOWORD(logbrush.lbColor);
186         emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
187         emr->cbBmi = biSize;
188         emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
189         emr->cbBits = bmSize;
190         memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
191                biSize + bmSize );
192
193         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
194             index = 0;
195         HeapFree( GetProcessHeap(), 0, emr );
196         GlobalUnlock16(logbrush.lbHatch);
197       }
198       break;
199
200     case BS_PATTERN:
201       {
202         EMRCREATEDIBPATTERNBRUSHPT *emr;
203         BITMAPINFOHEADER *info;
204         BITMAP bm;
205         DWORD bmSize, biSize, size;
206
207         GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
208
209         if (bm.bmBitsPixel != 1 || bm.bmPlanes != 1)
210         {
211             FIXME("Trying to create a color pattern brush\n");
212             break;
213         }
214
215         /* BMP will be aligned to 32 bits, not 16 */
216         bmSize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
217
218         biSize = sizeof(BITMAPINFOHEADER);
219         /* FIXME: There is an extra DWORD written by native before the BMI.
220          *        Not sure what its meant to contain.
221          */
222         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize + sizeof(DWORD);
223
224         emr = HeapAlloc( GetProcessHeap(), 0, size );
225         if(!emr)
226           break;
227
228         info = (BITMAPINFOHEADER *)((LPBYTE)emr +
229                 sizeof(EMRCREATEDIBPATTERNBRUSHPT) + sizeof(DWORD));
230         info->biSize = sizeof(BITMAPINFOHEADER);
231         info->biWidth = bm.bmWidth;
232         info->biHeight = bm.bmHeight;
233         info->biPlanes = bm.bmPlanes;
234         info->biBitCount = bm.bmBitsPixel;
235         info->biSizeImage = bmSize;
236         GetBitmapBits((HANDLE)logbrush.lbHatch,
237                       bm.bmHeight * BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel),
238                       (LPBYTE)info + sizeof(BITMAPINFOHEADER));
239
240         /* Change the padding to be DIB compatible if needed */
241         if (bm.bmWidth & 31)
242             EMFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFOHEADER), bm.bmWidth, bm.bmHeight);
243
244         emr->emr.iType = EMR_CREATEMONOBRUSH;
245         emr->emr.nSize = size;
246         emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
247         /* Presumably to reduce the size of the written EMF, MS supports an
248          * undocumented iUsage value of 2, indicating a mono bitmap without the
249          * 8 byte 2 entry black/white palette. Stupidly, they could have saved
250          * over 20 bytes more by also ignoring the BITMAPINFO fields that are
251          * irrelevant/constant for monochrome bitmaps.
252          * FIXME: It may be that the DIB functions themselves accept this value.
253          */
254         emr->iUsage = DIB_PAL_MONO;
255         emr->offBmi = (LPBYTE)info - (LPBYTE)emr;
256         emr->cbBmi = biSize;
257         emr->offBits = emr->offBmi + biSize;
258         emr->cbBits = bmSize;
259
260         if(!EMFDRV_WriteRecord( dev, &emr->emr ))
261             index = 0;
262         HeapFree( GetProcessHeap(), 0, emr );
263       }
264       break;
265
266     default:
267         FIXME("Unknown style %x\n", logbrush.lbStyle);
268         break;
269     }
270     return index;
271 }
272
273
274 /***********************************************************************
275  *           EMFDRV_SelectBrush
276  */
277 HBRUSH EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
278 {
279     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
280     EMRSELECTOBJECT emr;
281     DWORD index;
282     int i;
283
284     /* If the object is a stock brush object, do not need to create it.
285      * See definitions in  wingdi.h for range of stock brushes.
286      * We do however have to handle setting the higher order bit to
287      * designate that this is a stock object.
288      */
289     for (i = WHITE_BRUSH; i <= NULL_BRUSH; i++)
290     {
291         if (hBrush == GetStockObject(i))
292         {
293             index = i | 0x80000000;
294             goto found;
295         }
296     }
297     if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
298         goto found;
299
300     if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
301     GDI_hdc_using_object(hBrush, physDev->hdc);
302
303  found:
304     emr.emr.iType = EMR_SELECTOBJECT;
305     emr.emr.nSize = sizeof(emr);
306     emr.ihObject = index;
307     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
308 }
309
310
311 /******************************************************************
312  *         EMFDRV_CreateFontIndirect
313  */
314 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
315 {
316     DWORD index = 0;
317     EMREXTCREATEFONTINDIRECTW emr;
318     int i;
319
320     if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
321
322     emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
323     emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
324     emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
325     emr.elfw.elfFullName[0] = '\0';
326     emr.elfw.elfStyle[0]    = '\0';
327     emr.elfw.elfVersion     = 0;
328     emr.elfw.elfStyleSize   = 0;
329     emr.elfw.elfMatch       = 0;
330     emr.elfw.elfReserved    = 0;
331     for(i = 0; i < ELF_VENDOR_SIZE; i++)
332         emr.elfw.elfVendorId[i] = 0;
333     emr.elfw.elfCulture                 = PAN_CULTURE_LATIN;
334     emr.elfw.elfPanose.bFamilyType      = PAN_NO_FIT;
335     emr.elfw.elfPanose.bSerifStyle      = PAN_NO_FIT;
336     emr.elfw.elfPanose.bWeight          = PAN_NO_FIT;
337     emr.elfw.elfPanose.bProportion      = PAN_NO_FIT;
338     emr.elfw.elfPanose.bContrast        = PAN_NO_FIT;
339     emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
340     emr.elfw.elfPanose.bArmStyle        = PAN_NO_FIT;
341     emr.elfw.elfPanose.bLetterform      = PAN_NO_FIT;
342     emr.elfw.elfPanose.bMidline         = PAN_NO_FIT;
343     emr.elfw.elfPanose.bXHeight         = PAN_NO_FIT;
344
345     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
346         index = 0;
347     return index;
348 }
349
350
351 /***********************************************************************
352  *           EMFDRV_SelectFont
353  */
354 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, HANDLE gdiFont )
355 {
356     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
357     EMRSELECTOBJECT emr;
358     DWORD index;
359     int i;
360
361     /* If the object is a stock font object, do not need to create it.
362      * See definitions in  wingdi.h for range of stock fonts.
363      * We do however have to handle setting the higher order bit to
364      * designate that this is a stock object.
365      */
366
367     for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
368     {
369         if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
370         {
371             index = i | 0x80000000;
372             goto found;
373         }
374     }
375
376     if((index = EMFDRV_FindObject(dev, hFont)) != 0)
377         goto found;
378
379     if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return HGDI_ERROR;
380     GDI_hdc_using_object(hFont, physDev->hdc);
381
382  found:
383     emr.emr.iType = EMR_SELECTOBJECT;
384     emr.emr.nSize = sizeof(emr);
385     emr.ihObject = index;
386     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
387         return HGDI_ERROR;
388     return 0;
389 }
390
391
392
393 /******************************************************************
394  *         EMFDRV_CreatePenIndirect
395  */
396 static HPEN EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen )
397 {
398     EMRCREATEPEN emr;
399     DWORD index = 0;
400
401     if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
402     {
403         /* must be an extended pen */
404         EXTLOGPEN *elp;
405         INT size = GetObjectW( hPen, 0, NULL );
406
407         if (!size) return 0;
408
409         elp = HeapAlloc( GetProcessHeap(), 0, size );
410
411         GetObjectW( hPen, size, elp );
412         /* FIXME: add support for user style pens */
413         emr.lopn.lopnStyle = elp->elpPenStyle;
414         emr.lopn.lopnWidth.x = elp->elpWidth;
415         emr.lopn.lopnWidth.y = 0;
416         emr.lopn.lopnColor = elp->elpColor;
417
418         HeapFree( GetProcessHeap(), 0, elp );
419     }
420
421     emr.emr.iType = EMR_CREATEPEN;
422     emr.emr.nSize = sizeof(emr);
423     emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
424
425     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
426         index = 0;
427     return (HPEN)index;
428 }
429
430 /******************************************************************
431  *         EMFDRV_SelectPen
432  */
433 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
434 {
435     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
436     EMRSELECTOBJECT emr;
437     DWORD index;
438     int i;
439
440     /* If the object is a stock pen object, do not need to create it.
441      * See definitions in  wingdi.h for range of stock pens.
442      * We do however have to handle setting the higher order bit to
443      * designate that this is a stock object.
444      */
445
446     for (i = WHITE_PEN; i <= NULL_PEN; i++)
447     {
448         if (hPen == GetStockObject(i))
449         {
450             index = i | 0x80000000;
451             goto found;
452         }
453     }
454     if((index = EMFDRV_FindObject(dev, hPen)) != 0)
455         goto found;
456
457     if (!(index = (DWORD)EMFDRV_CreatePenIndirect(dev, hPen ))) return 0;
458     GDI_hdc_using_object(hPen, physDev->hdc);
459
460  found:
461     emr.emr.iType = EMR_SELECTOBJECT;
462     emr.emr.nSize = sizeof(emr);
463     emr.ihObject = index;
464     return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
465 }
466
467
468 /******************************************************************
469  *         EMFDRV_GdiComment
470  */
471 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
472 {
473     EMRGDICOMMENT *emr;
474     UINT total, rounded_size;
475     BOOL ret;
476
477     rounded_size = (bytes+3) & ~3;
478     total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
479
480     emr = HeapAlloc(GetProcessHeap(), 0, total);
481     emr->emr.iType = EMR_GDICOMMENT;
482     emr->emr.nSize = total;
483     emr->cbData = bytes;
484     memset(&emr->Data[bytes], 0, rounded_size - bytes);
485     memcpy(&emr->Data[0], buffer, bytes);
486
487     ret = EMFDRV_WriteRecord( dev, &emr->emr );
488
489     HeapFree(GetProcessHeap(), 0, emr);
490
491     return ret;
492 }