comdlg32: Sign-compare warnings fix.
[wine/wine64.git] / dlls / gdi32 / mfdrv / objects.c
blobf5f2aeb0b25d1df96c9adcb5834b0ecc85b8003d
1 /*
2 * GDI objects
4 * Copyright 1993 Alexandre Julliard
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.
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.
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "wownt32.h"
30 #include "mfdrv/metafiledrv.h"
31 #include "gdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
36 /******************************************************************
37 * MFDRV_AddHandle
39 UINT MFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
41 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
42 UINT16 index;
44 for(index = 0; index < physDev->handles_size; index++)
45 if(physDev->handles[index] == 0) break;
46 if(index == physDev->handles_size) {
47 physDev->handles_size += HANDLE_LIST_INC;
48 physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
49 physDev->handles,
50 physDev->handles_size * sizeof(physDev->handles[0]));
52 physDev->handles[index] = obj;
54 physDev->cur_handles++;
55 if(physDev->cur_handles > physDev->mh->mtNoObjects)
56 physDev->mh->mtNoObjects++;
58 return index ; /* index 0 is not reserved for metafiles */
61 /******************************************************************
62 * MFDRV_RemoveHandle
64 BOOL MFDRV_RemoveHandle( PHYSDEV dev, UINT index )
66 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
67 BOOL ret = FALSE;
69 if (index < physDev->handles_size && physDev->handles[index])
71 physDev->handles[index] = 0;
72 physDev->cur_handles--;
73 ret = TRUE;
75 return ret;
78 /******************************************************************
79 * MFDRV_FindObject
81 static INT16 MFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
83 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
84 INT16 index;
86 for(index = 0; index < physDev->handles_size; index++)
87 if(physDev->handles[index] == obj) break;
89 if(index == physDev->handles_size) return -1;
91 return index ;
95 /******************************************************************
96 * MFDRV_DeleteObject
98 BOOL MFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
100 METARECORD mr;
101 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
102 INT16 index;
103 BOOL ret = TRUE;
105 index = MFDRV_FindObject(dev, obj);
106 if( index < 0 )
107 return 0;
109 mr.rdSize = sizeof mr / 2;
110 mr.rdFunction = META_DELETEOBJECT;
111 mr.rdParm[0] = index;
113 if(!MFDRV_WriteRecord( dev, &mr, mr.rdSize*2 ))
114 ret = FALSE;
116 physDev->handles[index] = 0;
117 physDev->cur_handles--;
118 return ret;
122 /***********************************************************************
123 * MFDRV_SelectObject
125 static BOOL MFDRV_SelectObject( PHYSDEV dev, INT16 index)
127 METARECORD mr;
129 mr.rdSize = sizeof mr / 2;
130 mr.rdFunction = META_SELECTOBJECT;
131 mr.rdParm[0] = index;
133 return MFDRV_WriteRecord( dev, &mr, mr.rdSize*2 );
137 /***********************************************************************
138 * MFDRV_SelectBitmap
140 HBITMAP MFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
142 return 0;
145 /***********************************************************************
146 * Internal helper for MFDRV_CreateBrushIndirect():
147 * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
149 static inline void MFDRV_PadTo32(LPBYTE lpRows, int height, int width)
151 int bytes16 = 2 * ((width + 15) / 16);
152 int bytes32 = 4 * ((width + 31) / 32);
153 LPBYTE lpSrc, lpDst;
154 int i;
156 if (!height)
157 return;
159 height = abs(height) - 1;
160 lpSrc = lpRows + height * bytes16;
161 lpDst = lpRows + height * bytes32;
163 /* Note that we work backwards so we can re-pad in place */
164 while (height >= 0)
166 for (i = bytes32; i > bytes16; i--)
167 lpDst[i - 1] = 0; /* Zero the padding bytes */
168 for (; i > 0; i--)
169 lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
170 lpSrc -= bytes16;
171 lpDst -= bytes32;
172 height--;
176 /***********************************************************************
177 * Internal helper for MFDRV_CreateBrushIndirect():
178 * Reverse order of bitmap rows in going from BMP to DIB.
180 static inline void MFDRV_Reverse(LPBYTE lpRows, int height, int width)
182 int bytes = 4 * ((width + 31) / 32);
183 LPBYTE lpSrc, lpDst;
184 BYTE temp;
185 int i;
187 if (!height)
188 return;
190 lpSrc = lpRows;
191 lpDst = lpRows + (height-1) * bytes;
192 height = height/2;
194 while (height > 0)
196 for (i = 0; i < bytes; i++)
198 temp = lpDst[i];
199 lpDst[i] = lpSrc[i];
200 lpSrc[i] = temp;
202 lpSrc += bytes;
203 lpDst -= bytes;
204 height--;
208 /******************************************************************
209 * MFDRV_CreateBrushIndirect
212 INT16 MFDRV_CreateBrushIndirect(PHYSDEV dev, HBRUSH hBrush )
214 DWORD size;
215 METARECORD *mr;
216 LOGBRUSH logbrush;
217 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
218 BOOL r;
220 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return -1;
222 switch(logbrush.lbStyle)
224 case BS_SOLID:
225 case BS_NULL:
226 case BS_HATCHED:
228 LOGBRUSH16 lb16;
230 lb16.lbStyle = logbrush.lbStyle;
231 lb16.lbColor = logbrush.lbColor;
232 lb16.lbHatch = logbrush.lbHatch;
233 size = sizeof(METARECORD) + sizeof(LOGBRUSH16) - 2;
234 mr = HeapAlloc( GetProcessHeap(), 0, size );
235 mr->rdSize = size / 2;
236 mr->rdFunction = META_CREATEBRUSHINDIRECT;
237 memcpy( mr->rdParm, &lb16, sizeof(LOGBRUSH16));
238 break;
240 case BS_PATTERN:
242 BITMAP bm;
243 BITMAPINFO *info;
244 DWORD bmSize;
245 COLORREF cref;
247 GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
248 if(bm.bmBitsPixel != 1 || bm.bmPlanes != 1) {
249 FIXME("Trying to store a colour pattern brush\n");
250 goto done;
253 bmSize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, DIB_PAL_COLORS);
255 size = sizeof(METARECORD) + sizeof(WORD) + sizeof(BITMAPINFO) +
256 sizeof(RGBQUAD) + bmSize;
258 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
259 if(!mr) goto done;
260 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
261 mr->rdSize = size / 2;
262 mr->rdParm[0] = BS_PATTERN;
263 mr->rdParm[1] = DIB_RGB_COLORS;
264 info = (BITMAPINFO *)(mr->rdParm + 2);
266 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
267 info->bmiHeader.biWidth = bm.bmWidth;
268 info->bmiHeader.biHeight = bm.bmHeight;
269 info->bmiHeader.biPlanes = 1;
270 info->bmiHeader.biBitCount = 1;
271 info->bmiHeader.biSizeImage = bmSize;
273 GetBitmapBits((HANDLE)logbrush.lbHatch,
274 bm.bmHeight * BITMAP_GetWidthBytes (bm.bmWidth, bm.bmBitsPixel),
275 (LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD));
277 /* Change the padding to be DIB compatible if needed */
278 if(bm.bmWidth & 31)
279 MFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD),
280 bm.bmWidth, bm.bmHeight);
281 /* BMP and DIB have opposite row order conventions */
282 MFDRV_Reverse((LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD),
283 bm.bmWidth, bm.bmHeight);
285 cref = GetTextColor(physDev->hdc);
286 info->bmiColors[0].rgbRed = GetRValue(cref);
287 info->bmiColors[0].rgbGreen = GetGValue(cref);
288 info->bmiColors[0].rgbBlue = GetBValue(cref);
289 info->bmiColors[0].rgbReserved = 0;
290 cref = GetBkColor(physDev->hdc);
291 info->bmiColors[1].rgbRed = GetRValue(cref);
292 info->bmiColors[1].rgbGreen = GetGValue(cref);
293 info->bmiColors[1].rgbBlue = GetBValue(cref);
294 info->bmiColors[1].rgbReserved = 0;
295 break;
298 case BS_DIBPATTERN:
300 BITMAPINFO *info;
301 DWORD bmSize, biSize;
303 info = GlobalLock16((HGLOBAL16)logbrush.lbHatch);
304 if (info->bmiHeader.biCompression)
305 bmSize = info->bmiHeader.biSizeImage;
306 else
307 bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
308 info->bmiHeader.biHeight,
309 info->bmiHeader.biBitCount);
310 biSize = bitmap_info_size(info, LOWORD(logbrush.lbColor));
311 size = sizeof(METARECORD) + biSize + bmSize + 2;
312 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
313 if(!mr) goto done;
314 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
315 mr->rdSize = size / 2;
316 *(mr->rdParm) = logbrush.lbStyle;
317 *(mr->rdParm + 1) = LOWORD(logbrush.lbColor);
318 memcpy(mr->rdParm + 2, info, biSize + bmSize);
319 break;
321 default:
322 FIXME("Unkonwn brush style %x\n", logbrush.lbStyle);
323 return 0;
325 r = MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
326 HeapFree(GetProcessHeap(), 0, mr);
327 if( !r )
328 return -1;
329 done:
330 return MFDRV_AddHandle( dev, hBrush );
334 /***********************************************************************
335 * MFDRV_SelectBrush
337 HBRUSH MFDRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
339 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
340 INT16 index;
342 index = MFDRV_FindObject(dev, hbrush);
343 if( index < 0 )
345 index = MFDRV_CreateBrushIndirect( dev, hbrush );
346 if( index < 0 )
347 return 0;
348 GDI_hdc_using_object(hbrush, physDev->hdc);
350 return MFDRV_SelectObject( dev, index ) ? hbrush : HGDI_ERROR;
353 /******************************************************************
354 * MFDRV_CreateFontIndirect
357 static UINT16 MFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont, LOGFONTW *logfont)
359 char buffer[sizeof(METARECORD) - 2 + sizeof(LOGFONT16)];
360 METARECORD *mr = (METARECORD *)&buffer;
361 LOGFONT16 *font16;
363 mr->rdSize = (sizeof(METARECORD) + sizeof(LOGFONT16) - 2) / 2;
364 mr->rdFunction = META_CREATEFONTINDIRECT;
365 font16 = (LOGFONT16 *)&mr->rdParm;
367 font16->lfHeight = logfont->lfHeight;
368 font16->lfWidth = logfont->lfWidth;
369 font16->lfEscapement = logfont->lfEscapement;
370 font16->lfOrientation = logfont->lfOrientation;
371 font16->lfWeight = logfont->lfWeight;
372 font16->lfItalic = logfont->lfItalic;
373 font16->lfUnderline = logfont->lfUnderline;
374 font16->lfStrikeOut = logfont->lfStrikeOut;
375 font16->lfCharSet = logfont->lfCharSet;
376 font16->lfOutPrecision = logfont->lfOutPrecision;
377 font16->lfClipPrecision = logfont->lfClipPrecision;
378 font16->lfQuality = logfont->lfQuality;
379 font16->lfPitchAndFamily = logfont->lfPitchAndFamily;
380 WideCharToMultiByte( CP_ACP, 0, logfont->lfFaceName, -1, font16->lfFaceName, LF_FACESIZE, NULL, NULL );
381 font16->lfFaceName[LF_FACESIZE-1] = 0;
383 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * 2)))
384 return 0;
385 return MFDRV_AddHandle( dev, hFont );
389 /***********************************************************************
390 * MFDRV_SelectFont
392 HFONT MFDRV_SelectFont( PHYSDEV dev, HFONT hfont, HANDLE gdiFont )
394 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
395 LOGFONTW font;
396 INT16 index;
398 index = MFDRV_FindObject(dev, hfont);
399 if( index < 0 )
401 if (!GetObjectW( hfont, sizeof(font), &font ))
402 return HGDI_ERROR;
403 index = MFDRV_CreateFontIndirect(dev, hfont, &font);
404 if( index < 0 )
405 return HGDI_ERROR;
406 GDI_hdc_using_object(hfont, physDev->hdc);
408 return MFDRV_SelectObject( dev, index ) ? hfont : HGDI_ERROR;
411 /******************************************************************
412 * MFDRV_CreatePenIndirect
414 static UINT16 MFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen, LOGPEN16 *logpen)
416 char buffer[sizeof(METARECORD) - 2 + sizeof(*logpen)];
417 METARECORD *mr = (METARECORD *)&buffer;
419 mr->rdSize = (sizeof(METARECORD) + sizeof(*logpen) - 2) / 2;
420 mr->rdFunction = META_CREATEPENINDIRECT;
421 memcpy(&(mr->rdParm), logpen, sizeof(*logpen));
422 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * 2)))
423 return 0;
424 return MFDRV_AddHandle( dev, hPen );
428 /***********************************************************************
429 * MFDRV_SelectPen
431 HPEN MFDRV_SelectPen( PHYSDEV dev, HPEN hpen )
433 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
434 LOGPEN16 logpen;
435 INT16 index;
437 index = MFDRV_FindObject(dev, hpen);
438 if( index < 0 )
440 /* must be an extended pen */
441 INT size = GetObjectW( hpen, 0, NULL );
443 if (!size) return 0;
445 if (size == sizeof(LOGPEN))
447 LOGPEN pen;
449 GetObjectW( hpen, sizeof(pen), &pen );
450 logpen.lopnStyle = pen.lopnStyle;
451 logpen.lopnWidth.x = pen.lopnWidth.x;
452 logpen.lopnWidth.y = pen.lopnWidth.y;
453 logpen.lopnColor = pen.lopnColor;
455 else /* must be an extended pen */
457 EXTLOGPEN *elp = HeapAlloc( GetProcessHeap(), 0, size );
459 GetObjectW( hpen, size, elp );
460 /* FIXME: add support for user style pens */
461 logpen.lopnStyle = elp->elpPenStyle;
462 logpen.lopnWidth.x = elp->elpWidth;
463 logpen.lopnWidth.y = 0;
464 logpen.lopnColor = elp->elpColor;
466 HeapFree( GetProcessHeap(), 0, elp );
469 index = MFDRV_CreatePenIndirect( dev, hpen, &logpen );
470 if( index < 0 )
471 return 0;
472 GDI_hdc_using_object(hpen, physDev->hdc);
474 return MFDRV_SelectObject( dev, index ) ? hpen : HGDI_ERROR;
478 /******************************************************************
479 * MFDRV_CreatePalette
481 static BOOL MFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPalette, LOGPALETTE* logPalette, int sizeofPalette)
483 int index;
484 BOOL ret;
485 METARECORD *mr;
487 mr = HeapAlloc( GetProcessHeap(), 0, sizeof(METARECORD) + sizeofPalette - sizeof(WORD) );
488 mr->rdSize = (sizeof(METARECORD) + sizeofPalette - sizeof(WORD)) / sizeof(WORD);
489 mr->rdFunction = META_CREATEPALETTE;
490 memcpy(&(mr->rdParm), logPalette, sizeofPalette);
491 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD))))
493 HeapFree(GetProcessHeap(), 0, mr);
494 return FALSE;
497 mr->rdSize = sizeof(METARECORD) / sizeof(WORD);
498 mr->rdFunction = META_SELECTPALETTE;
500 if ((index = MFDRV_AddHandle( dev, hPalette )) == -1) ret = FALSE;
501 else
503 *(mr->rdParm) = index;
504 ret = MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD));
506 HeapFree(GetProcessHeap(), 0, mr);
507 return ret;
511 /***********************************************************************
512 * MFDRV_SelectPalette
514 HPALETTE MFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPalette, BOOL bForceBackground )
516 #define PALVERSION 0x0300
518 PLOGPALETTE logPalette;
519 WORD wNumEntries = 0;
520 BOOL creationSucceed;
521 int sizeofPalette;
523 GetObjectA(hPalette, sizeof(WORD), (LPSTR) &wNumEntries);
525 if (wNumEntries == 0) return 0;
527 sizeofPalette = sizeof(LOGPALETTE) + ((wNumEntries-1) * sizeof(PALETTEENTRY));
528 logPalette = HeapAlloc( GetProcessHeap(), 0, sizeofPalette );
530 if (logPalette == NULL) return 0;
532 logPalette->palVersion = PALVERSION;
533 logPalette->palNumEntries = wNumEntries;
535 GetPaletteEntries(hPalette, 0, wNumEntries, logPalette->palPalEntry);
537 creationSucceed = MFDRV_CreatePalette( dev, hPalette, logPalette, sizeofPalette );
539 HeapFree( GetProcessHeap(), 0, logPalette );
541 if (creationSucceed)
542 return hPalette;
544 return 0;
547 /***********************************************************************
548 * MFDRV_RealizePalette
550 UINT MFDRV_RealizePalette(PHYSDEV dev, HPALETTE hPalette, BOOL dummy)
552 char buffer[sizeof(METARECORD) - sizeof(WORD)];
553 METARECORD *mr = (METARECORD *)&buffer;
555 mr->rdSize = (sizeof(METARECORD) - sizeof(WORD)) / sizeof(WORD);
556 mr->rdFunction = META_REALIZEPALETTE;
558 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD)))) return 0;
560 /* The return value is suppose to be the number of entries
561 in the logical palette mapped to the system palette or 0
562 if the function failed. Since it's not trivial here to
563 get that kind of information and since it's of little
564 use in the case of metafiles, we'll always return 1. */
565 return 1;