user32: Improve handling of invalid bitmap headers in CreateIconFromResource().
[wine/hacks.git] / dlls / user32 / cursoricon.c
blob0658c2bb1b8199bfbfd581eb89c6444027d92d08
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Theory:
28 * Cursors and icons are stored in a global heap block, with the
29 * following layout:
31 * CURSORICONINFO info;
32 * BYTE[] ANDbits;
33 * BYTE[] XORbits;
35 * The bits structures are in the format of a device-dependent bitmap.
37 * This layout is very sub-optimal, as the bitmap bits are stored in
38 * the X client instead of in the server like other bitmaps; however,
39 * some programs (notably Paint Brush) expect to be able to manipulate
40 * the bits directly :-(
43 #include "config.h"
44 #include "wine/port.h"
46 #include <stdarg.h>
47 #include <string.h>
48 #include <stdlib.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "user_private.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 WINE_DECLARE_DEBUG_CHANNEL(icon);
62 WINE_DECLARE_DEBUG_CHANNEL(resource);
64 #include "pshpack1.h"
66 typedef struct {
67 BYTE bWidth;
68 BYTE bHeight;
69 BYTE bColorCount;
70 BYTE bReserved;
71 WORD xHotspot;
72 WORD yHotspot;
73 DWORD dwDIBSize;
74 DWORD dwDIBOffset;
75 } CURSORICONFILEDIRENTRY;
77 typedef struct
79 WORD idReserved;
80 WORD idType;
81 WORD idCount;
82 CURSORICONFILEDIRENTRY idEntries[1];
83 } CURSORICONFILEDIR;
85 #include "poppack.h"
87 #define CID_RESOURCE 0x0001
88 #define CID_WIN32 0x0004
89 #define CID_NONSHARED 0x0008
91 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
93 static HDC screen_dc;
95 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
97 /**********************************************************************
98 * ICONCACHE for cursors/icons loaded with LR_SHARED.
100 * FIXME: This should not be allocated on the system heap, but on a
101 * subsystem-global heap (i.e. one for all Win16 processes,
102 * and one for each Win32 process).
104 typedef struct tagICONCACHE
106 struct tagICONCACHE *next;
108 HMODULE hModule;
109 HRSRC hRsrc;
110 HRSRC hGroupRsrc;
111 HICON hIcon;
113 INT count;
115 } ICONCACHE;
117 static ICONCACHE *IconAnchor = NULL;
119 static CRITICAL_SECTION IconCrst;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
122 0, 0, &IconCrst,
123 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
126 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
128 static const WORD ICON_HOTSPOT = 0x4242;
131 /***********************************************************************
132 * map_fileW
134 * Helper function to map a file to memory:
135 * name - file name
136 * [RETURN] ptr - pointer to mapped file
137 * [RETURN] filesize - pointer size of file to be stored if not NULL
139 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
141 HANDLE hFile, hMapping;
142 LPVOID ptr = NULL;
144 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
145 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
146 if (hFile != INVALID_HANDLE_VALUE)
148 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
149 if (hMapping)
151 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
152 CloseHandle( hMapping );
153 if (filesize)
154 *filesize = GetFileSize( hFile, NULL );
156 CloseHandle( hFile );
158 return ptr;
162 /***********************************************************************
163 * get_bitmap_width_bytes
165 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
166 * data.
168 static int get_bitmap_width_bytes( int width, int bpp )
170 switch(bpp)
172 case 1:
173 return 2 * ((width+15) / 16);
174 case 4:
175 return 2 * ((width+3) / 4);
176 case 24:
177 width *= 3;
178 /* fall through */
179 case 8:
180 return width + (width & 1);
181 case 16:
182 case 15:
183 return width * 2;
184 case 32:
185 return width * 4;
186 default:
187 WARN("Unknown depth %d, please report.\n", bpp );
189 return -1;
193 /***********************************************************************
194 * get_dib_width_bytes
196 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
198 static int get_dib_width_bytes( int width, int depth )
200 int words;
202 switch(depth)
204 case 1: words = (width + 31) / 32; break;
205 case 4: words = (width + 7) / 8; break;
206 case 8: words = (width + 3) / 4; break;
207 case 15:
208 case 16: words = (width + 1) / 2; break;
209 case 24: words = (width * 3 + 3)/4; break;
210 default:
211 WARN("(%d): Unsupported depth\n", depth );
212 /* fall through */
213 case 32:
214 words = width;
216 return 4 * words;
220 /***********************************************************************
221 * bitmap_info_size
223 * Return the size of the bitmap info structure including color table.
225 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
227 int colors, masks = 0;
229 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
231 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
232 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
233 return sizeof(BITMAPCOREHEADER) + colors *
234 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
236 else /* assume BITMAPINFOHEADER */
238 colors = info->bmiHeader.biClrUsed;
239 if (colors > 256) /* buffer overflow otherwise */
240 colors = 256;
241 if (!colors && (info->bmiHeader.biBitCount <= 8))
242 colors = 1 << info->bmiHeader.biBitCount;
243 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
244 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
245 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
250 /***********************************************************************
251 * is_dib_monochrome
253 * Returns whether a DIB can be converted to a monochrome DDB.
255 * A DIB can be converted if its color table contains only black and
256 * white. Black must be the first color in the color table.
258 * Note : If the first color in the color table is white followed by
259 * black, we can't convert it to a monochrome DDB with
260 * SetDIBits, because black and white would be inverted.
262 static BOOL is_dib_monochrome( const BITMAPINFO* info )
264 if (info->bmiHeader.biBitCount != 1) return FALSE;
266 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
268 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
270 /* Check if the first color is black */
271 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
273 rgb++;
275 /* Check if the second color is white */
276 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
277 && (rgb->rgbtBlue == 0xff));
279 else return FALSE;
281 else /* assume BITMAPINFOHEADER */
283 const RGBQUAD *rgb = info->bmiColors;
285 /* Check if the first color is black */
286 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
287 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
289 rgb++;
291 /* Check if the second color is white */
292 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
293 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
295 else return FALSE;
299 /***********************************************************************
300 * DIB_GetBitmapInfo
302 * Get the info from a bitmap header.
303 * Return 1 for INFOHEADER, 0 for COREHEADER,
304 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307 LONG *height, WORD *bpp, DWORD *compr )
309 if (header->biSize == sizeof(BITMAPINFOHEADER))
311 *width = header->biWidth;
312 *height = header->biHeight;
313 *bpp = header->biBitCount;
314 *compr = header->biCompression;
315 return 1;
317 if (header->biSize == sizeof(BITMAPCOREHEADER))
319 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
320 *width = core->bcWidth;
321 *height = core->bcHeight;
322 *bpp = core->bcBitCount;
323 *compr = 0;
324 return 0;
326 if (header->biSize == sizeof(BITMAPV4HEADER))
328 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
329 *width = v4hdr->bV4Width;
330 *height = v4hdr->bV4Height;
331 *bpp = v4hdr->bV4BitCount;
332 *compr = v4hdr->bV4V4Compression;
333 return 4;
335 if (header->biSize == sizeof(BITMAPV5HEADER))
337 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
338 *width = v5hdr->bV5Width;
339 *height = v5hdr->bV5Height;
340 *bpp = v5hdr->bV5BitCount;
341 *compr = v5hdr->bV5Compression;
342 return 5;
344 ERR("(%d): unknown/wrong size for header\n", header->biSize );
345 return -1;
348 /**********************************************************************
349 * CURSORICON_FindSharedIcon
351 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
353 HICON hIcon = 0;
354 ICONCACHE *ptr;
356 EnterCriticalSection( &IconCrst );
358 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
359 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
361 ptr->count++;
362 hIcon = ptr->hIcon;
363 break;
366 LeaveCriticalSection( &IconCrst );
368 return hIcon;
371 /*************************************************************************
372 * CURSORICON_FindCache
374 * Given a handle, find the corresponding cache element
376 * PARAMS
377 * Handle [I] handle to an Image
379 * RETURNS
380 * Success: The cache entry
381 * Failure: NULL
384 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
386 ICONCACHE *ptr;
387 ICONCACHE *pRet=NULL;
388 BOOL IsFound = FALSE;
390 EnterCriticalSection( &IconCrst );
392 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
394 if ( hIcon == ptr->hIcon )
396 IsFound = TRUE;
397 pRet = ptr;
401 LeaveCriticalSection( &IconCrst );
403 return pRet;
406 /**********************************************************************
407 * CURSORICON_AddSharedIcon
409 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
411 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
412 if ( !ptr ) return;
414 ptr->hModule = hModule;
415 ptr->hRsrc = hRsrc;
416 ptr->hIcon = hIcon;
417 ptr->hGroupRsrc = hGroupRsrc;
418 ptr->count = 1;
420 EnterCriticalSection( &IconCrst );
421 ptr->next = IconAnchor;
422 IconAnchor = ptr;
423 LeaveCriticalSection( &IconCrst );
426 /**********************************************************************
427 * CURSORICON_DelSharedIcon
429 static INT CURSORICON_DelSharedIcon( HICON hIcon )
431 INT count = -1;
432 ICONCACHE *ptr;
434 EnterCriticalSection( &IconCrst );
436 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
437 if ( ptr->hIcon == hIcon )
439 if ( ptr->count > 0 ) ptr->count--;
440 count = ptr->count;
441 break;
444 LeaveCriticalSection( &IconCrst );
446 return count;
449 /**********************************************************************
450 * CURSORICON_FreeModuleIcons
452 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
454 ICONCACHE **ptr = &IconAnchor;
455 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
457 EnterCriticalSection( &IconCrst );
459 while ( *ptr )
461 if ( (*ptr)->hModule == hModule )
463 ICONCACHE *freePtr = *ptr;
464 *ptr = freePtr->next;
466 GlobalFree16(HICON_16(freePtr->hIcon));
467 HeapFree( GetProcessHeap(), 0, freePtr );
468 continue;
470 ptr = &(*ptr)->next;
473 LeaveCriticalSection( &IconCrst );
477 * The following macro functions account for the irregularities of
478 * accessing cursor and icon resources in files and resource entries.
480 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
481 int *width, int *height, int *bits );
483 /**********************************************************************
484 * CURSORICON_FindBestIcon
486 * Find the icon closest to the requested size and number of colors.
488 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
489 int width, int height, int colors )
491 int i, cx, cy, bits, bestEntry = -1;
492 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
493 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
495 /* Find Best Fit */
496 iTotalDiff = 0xFFFFFFFF;
497 iColorDiff = 0xFFFFFFFF;
498 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
500 iTempXDiff = abs(width - cx);
501 iTempYDiff = abs(height - cy);
503 if(iTotalDiff > (iTempXDiff + iTempYDiff))
505 iXDiff = iTempXDiff;
506 iYDiff = iTempYDiff;
507 iTotalDiff = iXDiff + iYDiff;
511 /* Find Best Colors for Best Fit */
512 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
514 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
516 iTempColorDiff = abs(colors - (1<<bits));
517 if(iColorDiff > iTempColorDiff)
519 bestEntry = i;
520 iColorDiff = iTempColorDiff;
525 return bestEntry;
528 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
529 int *width, int *height, int *bits )
531 CURSORICONDIR *resdir = dir;
532 ICONRESDIR *icon;
534 if ( resdir->idCount <= n )
535 return FALSE;
536 icon = &resdir->idEntries[n].ResInfo.icon;
537 *width = icon->bWidth;
538 *height = icon->bHeight;
539 *bits = resdir->idEntries[n].wBitCount;
540 return TRUE;
543 /**********************************************************************
544 * CURSORICON_FindBestCursor
546 * Find the cursor closest to the requested size.
547 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
548 * ignored too
550 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
551 int width, int height, int color )
553 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
555 /* Double height to account for AND and XOR masks */
557 height *= 2;
559 /* First find the largest one smaller than or equal to the requested size*/
561 maxwidth = maxheight = 0;
562 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
564 if ((cx <= width) && (cy <= height) &&
565 (cx > maxwidth) && (cy > maxheight) &&
566 (bits == 1))
568 bestEntry = i;
569 maxwidth = cx;
570 maxheight = cy;
573 if (bestEntry != -1) return bestEntry;
575 /* Now find the smallest one larger than the requested size */
577 maxwidth = maxheight = 255;
578 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
580 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
581 (bestEntry==-1))
583 bestEntry = i;
584 maxwidth = cx;
585 maxheight = cy;
589 return bestEntry;
592 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
593 int *width, int *height, int *bits )
595 CURSORICONDIR *resdir = dir;
596 CURSORDIR *cursor;
598 if ( resdir->idCount <= n )
599 return FALSE;
600 cursor = &resdir->idEntries[n].ResInfo.cursor;
601 *width = cursor->wWidth;
602 *height = cursor->wHeight;
603 *bits = resdir->idEntries[n].wBitCount;
604 return TRUE;
607 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
608 int width, int height, int colors )
610 int n;
612 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
613 width, height, colors );
614 if ( n < 0 )
615 return NULL;
616 return &dir->idEntries[n];
619 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
620 int width, int height, int color )
622 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
623 width, height, color );
624 if ( n < 0 )
625 return NULL;
626 return &dir->idEntries[n];
629 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
630 int *width, int *height, int *bits )
632 CURSORICONFILEDIR *filedir = dir;
633 CURSORICONFILEDIRENTRY *entry;
635 if ( filedir->idCount <= n )
636 return FALSE;
637 entry = &filedir->idEntries[n];
638 *width = entry->bWidth;
639 *height = entry->bHeight;
640 *bits = entry->bColorCount;
641 return TRUE;
644 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
645 int width, int height, int color )
647 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
648 width, height, color );
649 if ( n < 0 )
650 return NULL;
651 return &dir->idEntries[n];
654 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
655 int width, int height, int color )
657 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
658 width, height, color );
659 if ( n < 0 )
660 return NULL;
661 return &dir->idEntries[n];
664 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
665 POINT16 hotspot, BOOL bIcon,
666 DWORD dwVersion,
667 INT width, INT height,
668 UINT cFlag )
670 HGLOBAL16 hObj;
671 static HDC hdcMem;
672 int sizeAnd, sizeXor;
673 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
674 BITMAP bmpXor, bmpAnd;
675 BOOL DoStretch;
676 INT size;
678 if (dwVersion == 0x00020000)
680 FIXME_(cursor)("\t2.xx resources are not supported\n");
681 return 0;
684 /* Check bitmap header */
686 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
687 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
688 bmi->bmiHeader.biCompression != BI_RGB) )
690 WARN_(cursor)("Invalid resource bitmap header. (size: %u, comp: %u)\n",
691 bmi->bmiHeader.biSize, bmi->bmiHeader.biCompression);
692 if (hotspot.x == ICON_HOTSPOT && hotspot.y == ICON_HOTSPOT)
694 WARN_(cursor)("Returning 0.\n");
695 return 0;
697 else
699 WARN_(cursor)("Loading an IDC_ARROW to prevent a crash.\n");
700 return LoadCursorA(NULL, (char *) IDC_ARROW);
704 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
706 if (!width) width = bmi->bmiHeader.biWidth;
707 if (!height) height = bmi->bmiHeader.biHeight/2;
708 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
709 (bmi->bmiHeader.biWidth != width);
711 /* Scale the hotspot */
712 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
714 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
715 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
718 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
719 if (screen_dc)
721 BITMAPINFO* pInfo;
723 /* Make sure we have room for the monochrome bitmap later on.
724 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
725 * up to and including the biBitCount. In-memory icon resource
726 * format is as follows:
728 * BITMAPINFOHEADER icHeader // DIB header
729 * RGBQUAD icColors[] // Color table
730 * BYTE icXOR[] // DIB bits for XOR mask
731 * BYTE icAND[] // DIB bits for AND mask
734 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
735 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
737 memcpy( pInfo, bmi, size );
738 pInfo->bmiHeader.biHeight /= 2;
740 /* Create the XOR bitmap */
742 if (DoStretch) {
743 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
744 if(hXorBits)
746 HBITMAP hOld;
747 BOOL res = FALSE;
749 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
750 if (hdcMem) {
751 hOld = SelectObject(hdcMem, hXorBits);
752 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
753 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
754 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
755 SelectObject(hdcMem, hOld);
757 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
759 } else {
760 if (is_dib_monochrome(bmi)) {
761 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
762 SetDIBits(screen_dc, hXorBits, 0, height,
763 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
765 else
766 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
767 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
770 if( hXorBits )
772 char* xbits = (char *)bmi + size +
773 get_dib_width_bytes( bmi->bmiHeader.biWidth,
774 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
776 pInfo->bmiHeader.biBitCount = 1;
777 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
779 RGBQUAD *rgb = pInfo->bmiColors;
781 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
782 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
783 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
784 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
786 else
788 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
790 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
791 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
794 /* Create the AND bitmap */
796 if (DoStretch) {
797 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
798 HBITMAP hOld;
799 BOOL res = FALSE;
801 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
802 if (hdcMem) {
803 hOld = SelectObject(hdcMem, hAndBits);
804 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
805 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
806 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
807 SelectObject(hdcMem, hOld);
809 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
811 } else {
812 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
814 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
815 xbits, pInfo, DIB_RGB_COLORS);
818 if( !hAndBits ) DeleteObject( hXorBits );
820 HeapFree( GetProcessHeap(), 0, pInfo );
824 if( !hXorBits || !hAndBits )
826 WARN_(cursor)("\tunable to create an icon bitmap.\n");
827 return 0;
830 /* Now create the CURSORICONINFO structure */
831 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
832 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
833 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
834 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
836 hObj = GlobalAlloc16( GMEM_MOVEABLE,
837 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
838 if (hObj)
840 CURSORICONINFO *info;
842 info = GlobalLock16( hObj );
843 info->ptHotSpot.x = hotspot.x;
844 info->ptHotSpot.y = hotspot.y;
845 info->nWidth = bmpXor.bmWidth;
846 info->nHeight = bmpXor.bmHeight;
847 info->nWidthBytes = bmpXor.bmWidthBytes;
848 info->bPlanes = bmpXor.bmPlanes;
849 info->bBitsPerPixel = bmpXor.bmBitsPixel;
851 /* Transfer the bitmap bits to the CURSORICONINFO structure */
853 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
854 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
855 GlobalUnlock16( hObj );
858 DeleteObject( hAndBits );
859 DeleteObject( hXorBits );
860 return HICON_32(hObj);
864 /**********************************************************************
865 * .ANI cursor support
867 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
868 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
869 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
871 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
872 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
873 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
874 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
875 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
876 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
878 #define ANI_FLAG_ICON 0x1
879 #define ANI_FLAG_SEQUENCE 0x2
881 typedef struct {
882 DWORD header_size;
883 DWORD num_frames;
884 DWORD num_steps;
885 DWORD width;
886 DWORD height;
887 DWORD bpp;
888 DWORD num_planes;
889 DWORD display_rate;
890 DWORD flags;
891 } ani_header;
893 typedef struct {
894 DWORD data_size;
895 const unsigned char *data;
896 } riff_chunk_t;
898 static void dump_ani_header( const ani_header *header )
900 TRACE(" header size: %d\n", header->header_size);
901 TRACE(" frames: %d\n", header->num_frames);
902 TRACE(" steps: %d\n", header->num_steps);
903 TRACE(" width: %d\n", header->width);
904 TRACE(" height: %d\n", header->height);
905 TRACE(" bpp: %d\n", header->bpp);
906 TRACE(" planes: %d\n", header->num_planes);
907 TRACE(" display rate: %d\n", header->display_rate);
908 TRACE(" flags: 0x%08x\n", header->flags);
913 * RIFF:
914 * DWORD "RIFF"
915 * DWORD size
916 * DWORD riff_id
917 * BYTE[] data
919 * LIST:
920 * DWORD "LIST"
921 * DWORD size
922 * DWORD list_id
923 * BYTE[] data
925 * CHUNK:
926 * DWORD chunk_id
927 * DWORD size
928 * BYTE[] data
930 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
932 const unsigned char *ptr = parent_chunk->data;
933 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
935 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
937 while (ptr < end)
939 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
940 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
942 ptr += sizeof(DWORD);
943 chunk->data_size = *(DWORD *)ptr;
944 ptr += sizeof(DWORD);
945 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
946 chunk->data = ptr;
948 return;
951 ptr += sizeof(DWORD);
952 ptr += *(DWORD *)ptr;
953 ptr += sizeof(DWORD);
959 * .ANI layout:
961 * RIFF:'ACON' RIFF chunk
962 * |- CHUNK:'anih' Header
963 * |- CHUNK:'seq ' Sequence information (optional)
964 * \- LIST:'fram' Frame list
965 * |- CHUNK:icon Cursor frames
966 * |- CHUNK:icon
967 * |- ...
968 * \- CHUNK:icon
970 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
971 INT width, INT height )
973 HCURSOR cursor;
974 ani_header header = {0};
975 LPBYTE frame_bits = 0;
976 POINT16 hotspot;
977 CURSORICONFILEDIRENTRY *entry;
979 riff_chunk_t root_chunk = { bits_size, bits };
980 riff_chunk_t ACON_chunk = {0};
981 riff_chunk_t anih_chunk = {0};
982 riff_chunk_t fram_chunk = {0};
983 const unsigned char *icon_data;
985 TRACE("bits %p, bits_size %d\n", bits, bits_size);
987 if (!bits) return 0;
989 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
990 if (!ACON_chunk.data)
992 ERR("Failed to get root chunk.\n");
993 return 0;
996 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
997 if (!anih_chunk.data)
999 ERR("Failed to get 'anih' chunk.\n");
1000 return 0;
1002 memcpy( &header, anih_chunk.data, sizeof(header) );
1003 dump_ani_header( &header );
1005 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1006 if (!fram_chunk.data)
1008 ERR("Failed to get icon list.\n");
1009 return 0;
1012 /* FIXME: For now, just load the first frame. Before we can load all the
1013 * frames, we need to write the needed code in wineserver, etc. to handle
1014 * cursors. Once this code is written, we can extend it to support .ani
1015 * cursors and then update user32 and winex11.drv to load all frames.
1017 * Hopefully this will at least make some games (C&C3, etc.) more playable
1018 * in the meantime.
1020 FIXME("Loading all frames for .ani cursors not implemented.\n");
1021 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1023 entry = CURSORICON_FindBestCursorFile( (CURSORICONFILEDIR *) icon_data,
1024 width, height, 1 );
1026 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1027 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1029 if (!header.width || !header.height)
1031 header.width = entry->bWidth;
1032 header.height = entry->bHeight;
1035 hotspot.x = entry->xHotspot;
1036 hotspot.y = entry->yHotspot;
1038 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1039 FALSE, 0x00030000, header.width, header.height, 0 );
1041 HeapFree( GetProcessHeap(), 0, frame_bits );
1043 return cursor;
1047 /**********************************************************************
1048 * CreateIconFromResourceEx (USER32.@)
1050 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1051 * with cbSize parameter as well.
1053 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1054 BOOL bIcon, DWORD dwVersion,
1055 INT width, INT height,
1056 UINT cFlag )
1058 POINT16 hotspot;
1059 BITMAPINFO *bmi;
1061 hotspot.x = ICON_HOTSPOT;
1062 hotspot.y = ICON_HOTSPOT;
1064 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1065 bits, cbSize, dwVersion, width, height,
1066 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1068 if (bIcon)
1069 bmi = (BITMAPINFO *)bits;
1070 else /* get the hotspot */
1072 POINT16 *pt = (POINT16 *)bits;
1073 hotspot = *pt;
1074 bmi = (BITMAPINFO *)(pt + 1);
1077 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1078 width, height, cFlag );
1082 /**********************************************************************
1083 * CreateIconFromResource (USER32.@)
1085 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1086 BOOL bIcon, DWORD dwVersion)
1088 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1092 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1093 INT width, INT height, INT colors,
1094 BOOL fCursor, UINT loadflags)
1096 CURSORICONFILEDIRENTRY *entry;
1097 CURSORICONFILEDIR *dir;
1098 DWORD filesize = 0;
1099 HICON hIcon = 0;
1100 LPBYTE bits;
1101 POINT16 hotspot;
1103 TRACE("loading %s\n", debugstr_w( filename ));
1105 bits = map_fileW( filename, &filesize );
1106 if (!bits)
1107 return hIcon;
1109 /* Check for .ani. */
1110 if (memcmp( bits, "RIFF", 4 ) == 0)
1112 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height );
1113 goto end;
1116 dir = (CURSORICONFILEDIR*) bits;
1117 if ( filesize < sizeof(*dir) )
1118 goto end;
1120 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1121 goto end;
1123 if ( fCursor )
1124 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1125 else
1126 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1128 if ( !entry )
1129 goto end;
1131 /* check that we don't run off the end of the file */
1132 if ( entry->dwDIBOffset > filesize )
1133 goto end;
1134 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1135 goto end;
1137 hotspot.x = entry->xHotspot;
1138 hotspot.y = entry->yHotspot;
1139 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1140 hotspot, !fCursor, 0x00030000,
1141 width, height, loadflags );
1142 end:
1143 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1144 UnmapViewOfFile( bits );
1145 return hIcon;
1148 /**********************************************************************
1149 * CURSORICON_Load
1151 * Load a cursor or icon from resource or file.
1153 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1154 INT width, INT height, INT colors,
1155 BOOL fCursor, UINT loadflags)
1157 HANDLE handle = 0;
1158 HICON hIcon = 0;
1159 HRSRC hRsrc, hGroupRsrc;
1160 CURSORICONDIR *dir;
1161 CURSORICONDIRENTRY *dirEntry;
1162 LPBYTE bits;
1163 WORD wResId;
1164 DWORD dwBytesInRes;
1166 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1167 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1169 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1170 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1172 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1174 /* Normalize hInstance (must be uniquely represented for icon cache) */
1176 if (!HIWORD( hInstance ))
1177 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1179 /* Get directory resource ID */
1181 if (!(hRsrc = FindResourceW( hInstance, name,
1182 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1183 return 0;
1184 hGroupRsrc = hRsrc;
1186 /* Find the best entry in the directory */
1188 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1189 if (!(dir = LockResource( handle ))) return 0;
1190 if (fCursor)
1191 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1192 else
1193 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1194 if (!dirEntry) return 0;
1195 wResId = dirEntry->wResId;
1196 dwBytesInRes = dirEntry->dwBytesInRes;
1197 FreeResource( handle );
1199 /* Load the resource */
1201 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1202 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1204 /* If shared icon, check whether it was already loaded */
1205 if ( (loadflags & LR_SHARED)
1206 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1207 return hIcon;
1209 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1210 bits = LockResource( handle );
1211 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1212 !fCursor, 0x00030000, width, height, loadflags);
1213 FreeResource( handle );
1215 /* If shared icon, add to icon cache */
1217 if ( hIcon && (loadflags & LR_SHARED) )
1218 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1220 return hIcon;
1223 /***********************************************************************
1224 * CURSORICON_Copy
1226 * Make a copy of a cursor or icon.
1228 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1230 char *ptrOld, *ptrNew;
1231 int size;
1232 HICON16 hOld = HICON_16(hIcon);
1233 HICON16 hNew;
1235 if (!(ptrOld = GlobalLock16( hOld ))) return 0;
1236 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1237 size = GlobalSize16( hOld );
1238 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1239 FarSetOwner16( hNew, hInst16 );
1240 ptrNew = GlobalLock16( hNew );
1241 memcpy( ptrNew, ptrOld, size );
1242 GlobalUnlock16( hOld );
1243 GlobalUnlock16( hNew );
1244 return HICON_32(hNew);
1247 /*************************************************************************
1248 * CURSORICON_ExtCopy
1250 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1252 * PARAMS
1253 * Handle [I] handle to an Image
1254 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1255 * iDesiredCX [I] The Desired width of the Image
1256 * iDesiredCY [I] The desired height of the Image
1257 * nFlags [I] The flags from CopyImage
1259 * RETURNS
1260 * Success: The new handle of the Image
1262 * NOTES
1263 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1264 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1265 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1270 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1271 INT iDesiredCX, INT iDesiredCY,
1272 UINT nFlags)
1274 HICON hNew=0;
1276 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1277 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1279 if(hIcon == 0)
1281 return 0;
1284 /* Best Fit or Monochrome */
1285 if( (nFlags & LR_COPYFROMRESOURCE
1286 && (iDesiredCX > 0 || iDesiredCY > 0))
1287 || nFlags & LR_MONOCHROME)
1289 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1291 /* Not Found in Cache, then do a straight copy
1293 if(pIconCache == NULL)
1295 hNew = CURSORICON_Copy(0, hIcon);
1296 if(nFlags & LR_COPYFROMRESOURCE)
1298 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1301 else
1303 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1304 LPBYTE pBits;
1305 HANDLE hMem;
1306 HRSRC hRsrc;
1307 DWORD dwBytesInRes;
1308 WORD wResId;
1309 CURSORICONDIR *pDir;
1310 CURSORICONDIRENTRY *pDirEntry;
1311 BOOL bIsIcon = (nType == IMAGE_ICON);
1313 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1315 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1316 || (iDesiredCX == 0 && iDesiredCY == 0))
1318 iDesiredCY = GetSystemMetrics(bIsIcon ?
1319 SM_CYICON : SM_CYCURSOR);
1320 iDesiredCX = GetSystemMetrics(bIsIcon ?
1321 SM_CXICON : SM_CXCURSOR);
1324 /* Retrieve the CURSORICONDIRENTRY
1326 if (!(hMem = LoadResource( pIconCache->hModule ,
1327 pIconCache->hGroupRsrc)))
1329 return 0;
1331 if (!(pDir = LockResource( hMem )))
1333 return 0;
1336 /* Find Best Fit
1338 if(bIsIcon)
1340 pDirEntry = CURSORICON_FindBestIconRes(
1341 pDir, iDesiredCX, iDesiredCY, 256 );
1343 else
1345 pDirEntry = CURSORICON_FindBestCursorRes(
1346 pDir, iDesiredCX, iDesiredCY, 1);
1349 wResId = pDirEntry->wResId;
1350 dwBytesInRes = pDirEntry->dwBytesInRes;
1351 FreeResource(hMem);
1353 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1354 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1355 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1357 /* Get the Best Fit
1359 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1360 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1362 return 0;
1364 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1366 return 0;
1369 pBits = LockResource( hMem );
1371 if(nFlags & LR_DEFAULTSIZE)
1373 iTargetCY = GetSystemMetrics(SM_CYICON);
1374 iTargetCX = GetSystemMetrics(SM_CXICON);
1377 /* Create a New Icon with the proper dimension
1379 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1380 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1381 FreeResource(hMem);
1384 else hNew = CURSORICON_Copy(0, hIcon);
1385 return hNew;
1389 /***********************************************************************
1390 * CreateCursor (USER32.@)
1392 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1393 INT xHotSpot, INT yHotSpot,
1394 INT nWidth, INT nHeight,
1395 LPCVOID lpANDbits, LPCVOID lpXORbits )
1397 CURSORICONINFO info;
1399 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1400 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1402 info.ptHotSpot.x = xHotSpot;
1403 info.ptHotSpot.y = yHotSpot;
1404 info.nWidth = nWidth;
1405 info.nHeight = nHeight;
1406 info.nWidthBytes = 0;
1407 info.bPlanes = 1;
1408 info.bBitsPerPixel = 1;
1410 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1414 /***********************************************************************
1415 * CreateIcon (USER.407)
1417 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1418 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1419 LPCVOID lpANDbits, LPCVOID lpXORbits )
1421 CURSORICONINFO info;
1423 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1424 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1426 info.ptHotSpot.x = ICON_HOTSPOT;
1427 info.ptHotSpot.y = ICON_HOTSPOT;
1428 info.nWidth = nWidth;
1429 info.nHeight = nHeight;
1430 info.nWidthBytes = 0;
1431 info.bPlanes = bPlanes;
1432 info.bBitsPerPixel = bBitsPixel;
1434 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1438 /***********************************************************************
1439 * CreateIcon (USER32.@)
1441 * Creates an icon based on the specified bitmaps. The bitmaps must be
1442 * provided in a device dependent format and will be resized to
1443 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1444 * depth. The provided bitmaps must be top-down bitmaps.
1445 * Although Windows does not support 15bpp(*) this API must support it
1446 * for Winelib applications.
1448 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1449 * format!
1451 * RETURNS
1452 * Success: handle to an icon
1453 * Failure: NULL
1455 * FIXME: Do we need to resize the bitmaps?
1457 HICON WINAPI CreateIcon(
1458 HINSTANCE hInstance, /* [in] the application's hInstance */
1459 INT nWidth, /* [in] the width of the provided bitmaps */
1460 INT nHeight, /* [in] the height of the provided bitmaps */
1461 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1462 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1463 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1464 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1466 ICONINFO iinfo;
1467 HICON hIcon;
1469 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1470 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1472 iinfo.fIcon = TRUE;
1473 iinfo.xHotspot = ICON_HOTSPOT;
1474 iinfo.yHotspot = ICON_HOTSPOT;
1475 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1476 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1478 hIcon = CreateIconIndirect( &iinfo );
1480 DeleteObject( iinfo.hbmMask );
1481 DeleteObject( iinfo.hbmColor );
1483 return hIcon;
1487 /***********************************************************************
1488 * CreateCursorIconIndirect (USER.408)
1490 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1491 CURSORICONINFO *info,
1492 LPCVOID lpANDbits,
1493 LPCVOID lpXORbits )
1495 HGLOBAL16 handle;
1496 char *ptr;
1497 int sizeAnd, sizeXor;
1499 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1500 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1501 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1502 sizeXor = info->nHeight * info->nWidthBytes;
1503 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1504 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1505 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1506 return 0;
1507 FarSetOwner16( handle, hInstance );
1508 ptr = GlobalLock16( handle );
1509 memcpy( ptr, info, sizeof(*info) );
1510 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1511 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1512 GlobalUnlock16( handle );
1513 return handle;
1517 /***********************************************************************
1518 * CopyIcon (USER.368)
1520 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1522 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1523 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1527 /***********************************************************************
1528 * CopyIcon (USER32.@)
1530 HICON WINAPI CopyIcon( HICON hIcon )
1532 TRACE_(icon)("%p\n", hIcon );
1533 return CURSORICON_Copy( 0, hIcon );
1537 /***********************************************************************
1538 * CopyCursor (USER.369)
1540 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1542 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1543 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1546 /**********************************************************************
1547 * DestroyIcon32 (USER.610)
1549 * This routine is actually exported from Win95 USER under the name
1550 * DestroyIcon32 ... The behaviour implemented here should mimic
1551 * the Win95 one exactly, especially the return values, which
1552 * depend on the setting of various flags.
1554 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1556 WORD retv;
1558 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1560 /* Check whether destroying active cursor */
1562 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1564 WARN_(cursor)("Destroying active cursor!\n" );
1565 return FALSE;
1568 /* Try shared cursor/icon first */
1570 if ( !(flags & CID_NONSHARED) )
1572 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1574 if ( count != -1 )
1575 return (flags & CID_WIN32)? TRUE : (count == 0);
1577 /* FIXME: OEM cursors/icons should be recognized */
1580 /* Now assume non-shared cursor/icon */
1582 retv = GlobalFree16( handle );
1583 return (flags & CID_RESOURCE)? retv : TRUE;
1586 /***********************************************************************
1587 * DestroyIcon (USER32.@)
1589 BOOL WINAPI DestroyIcon( HICON hIcon )
1591 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1595 /***********************************************************************
1596 * DestroyCursor (USER32.@)
1598 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1600 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1604 /***********************************************************************
1605 * DrawIcon (USER32.@)
1607 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1609 CURSORICONINFO *ptr;
1610 HDC hMemDC;
1611 HBITMAP hXorBits, hAndBits;
1612 COLORREF oldFg, oldBg;
1614 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1616 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
1617 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1618 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1619 (char *)(ptr+1) );
1620 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1621 ptr->bBitsPerPixel, (char *)(ptr + 1)
1622 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1623 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1624 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1626 if (hXorBits && hAndBits)
1628 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1629 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1630 SelectObject( hMemDC, hXorBits );
1631 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1632 SelectObject( hMemDC, hBitTemp );
1634 DeleteDC( hMemDC );
1635 if (hXorBits) DeleteObject( hXorBits );
1636 if (hAndBits) DeleteObject( hAndBits );
1637 GlobalUnlock16(HICON_16(hIcon));
1638 SetTextColor( hdc, oldFg );
1639 SetBkColor( hdc, oldBg );
1640 return TRUE;
1643 /***********************************************************************
1644 * DumpIcon (USER.459)
1646 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1647 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1649 CURSORICONINFO *info = MapSL( pInfo );
1650 int sizeAnd, sizeXor;
1652 if (!info) return 0;
1653 sizeXor = info->nHeight * info->nWidthBytes;
1654 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1655 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1656 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1657 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1658 return MAKELONG( sizeXor, sizeXor );
1662 /***********************************************************************
1663 * SetCursor (USER32.@)
1665 * Set the cursor shape.
1667 * RETURNS
1668 * A handle to the previous cursor shape.
1670 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1672 struct user_thread_info *thread_info = get_user_thread_info();
1673 HCURSOR hOldCursor;
1675 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1676 TRACE("%p\n", hCursor);
1677 hOldCursor = thread_info->cursor;
1678 thread_info->cursor = hCursor;
1679 /* Change the cursor shape only if it is visible */
1680 if (thread_info->cursor_count >= 0)
1682 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1683 GlobalUnlock16(HCURSOR_16(hCursor));
1685 return hOldCursor;
1688 /***********************************************************************
1689 * ShowCursor (USER32.@)
1691 INT WINAPI ShowCursor( BOOL bShow )
1693 struct user_thread_info *thread_info = get_user_thread_info();
1695 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1697 if (bShow)
1699 if (++thread_info->cursor_count == 0) /* Show it */
1701 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1702 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1705 else
1707 if (--thread_info->cursor_count == -1) /* Hide it */
1708 USER_Driver->pSetCursor( NULL );
1710 return thread_info->cursor_count;
1713 /***********************************************************************
1714 * GetCursor (USER32.@)
1716 HCURSOR WINAPI GetCursor(void)
1718 return get_user_thread_info()->cursor;
1722 /***********************************************************************
1723 * ClipCursor (USER32.@)
1725 BOOL WINAPI ClipCursor( const RECT *rect )
1727 RECT virt;
1729 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1730 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1731 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1732 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1734 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1735 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1737 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1738 CURSOR_ClipRect = virt;
1740 USER_Driver->pClipCursor( rect );
1741 return TRUE;
1745 /***********************************************************************
1746 * GetClipCursor (USER32.@)
1748 BOOL WINAPI GetClipCursor( RECT *rect )
1750 /* If this is first time - initialize the rect */
1751 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1753 return CopyRect( rect, &CURSOR_ClipRect );
1757 /***********************************************************************
1758 * SetSystemCursor (USER32.@)
1760 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1762 FIXME("(%p,%08x),stub!\n", hcur, id);
1763 return TRUE;
1767 /**********************************************************************
1768 * LookupIconIdFromDirectoryEx (USER.364)
1770 * FIXME: exact parameter sizes
1772 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1773 INT16 width, INT16 height, UINT16 cFlag )
1775 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1778 /**********************************************************************
1779 * LookupIconIdFromDirectoryEx (USER32.@)
1781 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1782 INT width, INT height, UINT cFlag )
1784 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1785 UINT retVal = 0;
1786 if( dir && !dir->idReserved && (dir->idType & 3) )
1788 CURSORICONDIRENTRY* entry;
1789 HDC hdc;
1790 UINT palEnts;
1791 int colors;
1792 hdc = GetDC(0);
1793 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1794 if (palEnts == 0)
1795 palEnts = 256;
1796 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1798 ReleaseDC(0, hdc);
1800 if( bIcon )
1801 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1802 else
1803 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1805 if( entry ) retVal = entry->wResId;
1807 else WARN_(cursor)("invalid resource directory\n");
1808 return retVal;
1811 /**********************************************************************
1812 * LookupIconIdFromDirectory (USER.?)
1814 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1816 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1817 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1818 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1821 /**********************************************************************
1822 * LookupIconIdFromDirectory (USER32.@)
1824 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1826 return LookupIconIdFromDirectoryEx( dir, bIcon,
1827 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1828 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1831 /**********************************************************************
1832 * GetIconID (USER.455)
1834 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1836 LPBYTE lpDir = GlobalLock16(hResource);
1838 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1839 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1841 switch(resType)
1843 case RT_CURSOR:
1844 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1845 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1846 case RT_ICON:
1847 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1848 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1849 default:
1850 WARN_(cursor)("invalid res type %d\n", resType );
1852 return 0;
1855 /**********************************************************************
1856 * LoadCursorIconHandler (USER.336)
1858 * Supposed to load resources of Windows 2.x applications.
1860 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1862 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1863 hResource, hModule, hRsrc);
1864 return 0;
1867 /**********************************************************************
1868 * LoadIconHandler (USER.456)
1870 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1872 LPBYTE bits = LockResource16( hResource );
1874 TRACE_(cursor)("hRes=%04x\n",hResource);
1876 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1877 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1880 /***********************************************************************
1881 * LoadCursorW (USER32.@)
1883 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1885 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1887 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1888 LR_SHARED | LR_DEFAULTSIZE );
1891 /***********************************************************************
1892 * LoadCursorA (USER32.@)
1894 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1896 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1898 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1899 LR_SHARED | LR_DEFAULTSIZE );
1902 /***********************************************************************
1903 * LoadCursorFromFileW (USER32.@)
1905 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1907 TRACE("%s\n", debugstr_w(name));
1909 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1910 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1913 /***********************************************************************
1914 * LoadCursorFromFileA (USER32.@)
1916 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1918 TRACE("%s\n", debugstr_a(name));
1920 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1921 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1924 /***********************************************************************
1925 * LoadIconW (USER32.@)
1927 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1929 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1931 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1932 LR_SHARED | LR_DEFAULTSIZE );
1935 /***********************************************************************
1936 * LoadIconA (USER32.@)
1938 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1940 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1942 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1943 LR_SHARED | LR_DEFAULTSIZE );
1946 /**********************************************************************
1947 * GetIconInfo (USER32.@)
1949 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1951 CURSORICONINFO *ciconinfo;
1952 INT height;
1954 ciconinfo = GlobalLock16(HICON_16(hIcon));
1955 if (!ciconinfo)
1956 return FALSE;
1958 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1959 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1961 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1962 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1964 iconinfo->fIcon = TRUE;
1965 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1966 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1968 else
1970 iconinfo->fIcon = FALSE;
1971 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1972 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1975 height = ciconinfo->nHeight;
1977 if (ciconinfo->bBitsPerPixel > 1)
1979 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1980 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1981 (char *)(ciconinfo + 1)
1982 + ciconinfo->nHeight *
1983 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1985 else
1987 iconinfo->hbmColor = 0;
1988 height *= 2;
1991 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1992 1, 1, (char *)(ciconinfo + 1));
1994 GlobalUnlock16(HICON_16(hIcon));
1996 return TRUE;
1999 /**********************************************************************
2000 * CreateIconIndirect (USER32.@)
2002 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2004 DIBSECTION bmpXor;
2005 BITMAP bmpAnd;
2006 HICON16 hObj;
2007 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
2009 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2010 iconinfo->hbmColor, iconinfo->hbmMask,
2011 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2013 if (!iconinfo->hbmMask) return 0;
2015 planes = GetDeviceCaps( screen_dc, PLANES );
2016 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2018 if (iconinfo->hbmColor)
2020 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2021 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2022 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2023 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2024 /* we can use either depth 1 or screen depth for xor bitmap */
2025 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2026 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2028 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2029 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2030 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2031 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2033 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2035 hObj = GlobalAlloc16( GMEM_MOVEABLE,
2036 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
2037 if (hObj)
2039 CURSORICONINFO *info;
2041 info = GlobalLock16( hObj );
2043 /* If we are creating an icon, the hotspot is unused */
2044 if (iconinfo->fIcon)
2046 info->ptHotSpot.x = ICON_HOTSPOT;
2047 info->ptHotSpot.y = ICON_HOTSPOT;
2049 else
2051 info->ptHotSpot.x = iconinfo->xHotspot;
2052 info->ptHotSpot.y = iconinfo->yHotspot;
2055 if (iconinfo->hbmColor)
2057 info->nWidth = bmpXor.dsBm.bmWidth;
2058 info->nHeight = bmpXor.dsBm.bmHeight;
2059 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2060 info->bPlanes = planes;
2061 info->bBitsPerPixel = bpp;
2063 else
2065 info->nWidth = bmpAnd.bmWidth;
2066 info->nHeight = bmpAnd.bmHeight / 2;
2067 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2068 info->bPlanes = 1;
2069 info->bBitsPerPixel = 1;
2072 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2074 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2075 if (bmpAnd.bmBitsPixel == 1)
2077 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
2079 else
2081 HDC hdc, hdc_mem;
2082 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2084 hdc = GetDC( 0 );
2085 hdc_mem = CreateCompatibleDC( hdc );
2087 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2089 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2090 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2092 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2094 SelectObject( hdc, hbmp_old );
2095 SelectObject( hdc_mem, hbmp_mem_old );
2097 DeleteDC( hdc_mem );
2098 ReleaseDC( 0, hdc );
2100 GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
2101 DeleteObject( hbmp_mono );
2104 if (iconinfo->hbmColor)
2106 char *dst_bits = (char*)(info + 1) + sizeAnd;
2108 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2109 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2110 else
2112 BITMAPINFO bminfo;
2113 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2114 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2116 bminfo.bmiHeader.biSize = sizeof(bminfo);
2117 bminfo.bmiHeader.biWidth = info->nWidth;
2118 bminfo.bmiHeader.biHeight = info->nHeight;
2119 bminfo.bmiHeader.biPlanes = info->bPlanes;
2120 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2121 bminfo.bmiHeader.biCompression = BI_RGB;
2122 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2123 bminfo.bmiHeader.biXPelsPerMeter = 0;
2124 bminfo.bmiHeader.biYPelsPerMeter = 0;
2125 bminfo.bmiHeader.biClrUsed = 0;
2126 bminfo.bmiHeader.biClrImportant = 0;
2128 /* swap lines for dib sections */
2129 if (xor_objsize == sizeof(DIBSECTION))
2130 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2132 if (dib_width != bitmap_width) /* need to fixup alignment */
2134 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2136 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2137 src_bits, &bminfo, DIB_RGB_COLORS ))
2139 int y;
2140 for (y = 0; y < info->nHeight; y++)
2141 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2143 HeapFree( GetProcessHeap(), 0, src_bits );
2145 else
2146 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2147 dst_bits, &bminfo, DIB_RGB_COLORS );
2150 GlobalUnlock16( hObj );
2152 return HICON_32(hObj);
2155 /******************************************************************************
2156 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2158 * NOTES
2159 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2161 * PARAMS
2162 * hdc [I] Handle to device context
2163 * x0 [I] X coordinate of upper left corner
2164 * y0 [I] Y coordinate of upper left corner
2165 * hIcon [I] Handle to icon to draw
2166 * cxWidth [I] Width of icon
2167 * cyWidth [I] Height of icon
2168 * istep [I] Index of frame in animated cursor
2169 * hbr [I] Handle to background brush
2170 * flags [I] Icon-drawing flags
2172 * RETURNS
2173 * Success: TRUE
2174 * Failure: FALSE
2176 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2177 INT cxWidth, INT cyWidth, UINT istep,
2178 HBRUSH hbr, UINT flags )
2180 CURSORICONINFO *ptr = GlobalLock16(HICON_16(hIcon));
2181 HDC hDC_off = 0, hMemDC;
2182 BOOL result = FALSE, DoOffscreen;
2183 HBITMAP hB_off = 0, hOld = 0;
2185 if (!ptr) return FALSE;
2186 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2187 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2189 hMemDC = CreateCompatibleDC (hdc);
2190 if (istep)
2191 FIXME_(icon)("Ignoring istep=%d\n", istep);
2192 if (flags & DI_NOMIRROR)
2193 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2195 if (!flags) {
2196 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2197 flags = DI_NORMAL;
2200 /* Calculate the size of the destination image. */
2201 if (cxWidth == 0)
2203 if (flags & DI_DEFAULTSIZE)
2204 cxWidth = GetSystemMetrics (SM_CXICON);
2205 else
2206 cxWidth = ptr->nWidth;
2208 if (cyWidth == 0)
2210 if (flags & DI_DEFAULTSIZE)
2211 cyWidth = GetSystemMetrics (SM_CYICON);
2212 else
2213 cyWidth = ptr->nHeight;
2216 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2218 if (DoOffscreen) {
2219 RECT r;
2221 r.left = 0;
2222 r.top = 0;
2223 r.right = cxWidth;
2224 r.bottom = cxWidth;
2226 hDC_off = CreateCompatibleDC(hdc);
2227 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2228 if (hDC_off && hB_off) {
2229 hOld = SelectObject(hDC_off, hB_off);
2230 FillRect(hDC_off, &r, hbr);
2234 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2236 HBITMAP hXorBits, hAndBits;
2237 COLORREF oldFg, oldBg;
2238 INT nStretchMode;
2240 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2242 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2243 ptr->bPlanes, ptr->bBitsPerPixel,
2244 (char *)(ptr + 1)
2245 + ptr->nHeight *
2246 get_bitmap_width_bytes(ptr->nWidth,1) );
2247 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2248 1, 1, (char *)(ptr+1) );
2249 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2250 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2252 if (hXorBits && hAndBits)
2254 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2255 if (flags & DI_MASK)
2257 if (DoOffscreen)
2258 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2259 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2260 else
2261 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2262 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2264 SelectObject( hMemDC, hXorBits );
2265 if (flags & DI_IMAGE)
2267 if (DoOffscreen)
2268 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2269 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2270 else
2271 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2272 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2274 SelectObject( hMemDC, hBitTemp );
2275 result = TRUE;
2278 SetTextColor( hdc, oldFg );
2279 SetBkColor( hdc, oldBg );
2280 if (hXorBits) DeleteObject( hXorBits );
2281 if (hAndBits) DeleteObject( hAndBits );
2282 SetStretchBltMode (hdc, nStretchMode);
2283 if (DoOffscreen) {
2284 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2285 SelectObject(hDC_off, hOld);
2288 if (hMemDC) DeleteDC( hMemDC );
2289 if (hDC_off) DeleteDC(hDC_off);
2290 if (hB_off) DeleteObject(hB_off);
2291 GlobalUnlock16(HICON_16(hIcon));
2292 return result;
2295 /***********************************************************************
2296 * DIB_FixColorsToLoadflags
2298 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2299 * are in loadflags
2301 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2303 int colors;
2304 COLORREF c_W, c_S, c_F, c_L, c_C;
2305 int incr,i;
2306 RGBQUAD *ptr;
2307 int bitmap_type;
2308 LONG width;
2309 LONG height;
2310 WORD bpp;
2311 DWORD compr;
2313 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2315 WARN_(resource)("Invalid bitmap\n");
2316 return;
2319 if (bpp > 8) return;
2321 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2323 incr = 3;
2324 colors = 1 << bpp;
2326 else
2328 incr = 4;
2329 colors = bmi->bmiHeader.biClrUsed;
2330 if (colors > 256) colors = 256;
2331 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2334 c_W = GetSysColor(COLOR_WINDOW);
2335 c_S = GetSysColor(COLOR_3DSHADOW);
2336 c_F = GetSysColor(COLOR_3DFACE);
2337 c_L = GetSysColor(COLOR_3DLIGHT);
2339 if (loadflags & LR_LOADTRANSPARENT) {
2340 switch (bpp) {
2341 case 1: pix = pix >> 7; break;
2342 case 4: pix = pix >> 4; break;
2343 case 8: break;
2344 default:
2345 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2346 return;
2348 if (pix >= colors) {
2349 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2350 return;
2352 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2353 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2354 ptr->rgbBlue = GetBValue(c_W);
2355 ptr->rgbGreen = GetGValue(c_W);
2356 ptr->rgbRed = GetRValue(c_W);
2358 if (loadflags & LR_LOADMAP3DCOLORS)
2359 for (i=0; i<colors; i++) {
2360 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2361 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2362 if (c_C == RGB(128, 128, 128)) {
2363 ptr->rgbRed = GetRValue(c_S);
2364 ptr->rgbGreen = GetGValue(c_S);
2365 ptr->rgbBlue = GetBValue(c_S);
2366 } else if (c_C == RGB(192, 192, 192)) {
2367 ptr->rgbRed = GetRValue(c_F);
2368 ptr->rgbGreen = GetGValue(c_F);
2369 ptr->rgbBlue = GetBValue(c_F);
2370 } else if (c_C == RGB(223, 223, 223)) {
2371 ptr->rgbRed = GetRValue(c_L);
2372 ptr->rgbGreen = GetGValue(c_L);
2373 ptr->rgbBlue = GetBValue(c_L);
2379 /**********************************************************************
2380 * BITMAP_Load
2382 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2383 INT desiredx, INT desiredy, UINT loadflags )
2385 HBITMAP hbitmap = 0, orig_bm;
2386 HRSRC hRsrc;
2387 HGLOBAL handle;
2388 char *ptr = NULL;
2389 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2390 int size;
2391 BYTE pix;
2392 char *bits;
2393 LONG width, height, new_width, new_height;
2394 WORD bpp_dummy;
2395 DWORD compr_dummy;
2396 INT bm_type;
2397 HDC screen_mem_dc = NULL;
2399 if (!(loadflags & LR_LOADFROMFILE))
2401 if (!instance)
2403 /* OEM bitmap: try to load the resource from user32.dll */
2404 instance = user32_module;
2407 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2408 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2410 if ((info = LockResource( handle )) == NULL) return 0;
2412 else
2414 BITMAPFILEHEADER * bmfh;
2416 if (!(ptr = map_fileW( name, NULL ))) return 0;
2417 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2418 bmfh = (BITMAPFILEHEADER *)ptr;
2419 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2420 bmfh->bfReserved1 == 0 &&
2421 bmfh->bfReserved2 == 0))
2423 WARN("Invalid/unsupported bitmap format!\n");
2424 UnmapViewOfFile( ptr );
2425 return 0;
2429 size = bitmap_info_size(info, DIB_RGB_COLORS);
2430 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2431 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2433 if (!fix_info || !scaled_info) goto end;
2434 memcpy(fix_info, info, size);
2436 pix = *((LPBYTE)info + size);
2437 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2439 memcpy(scaled_info, fix_info, size);
2440 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2441 &bpp_dummy, &compr_dummy);
2442 if(desiredx != 0)
2443 new_width = desiredx;
2444 else
2445 new_width = width;
2447 if(desiredy != 0)
2448 new_height = height > 0 ? desiredy : -desiredy;
2449 else
2450 new_height = height;
2452 if(bm_type == 0)
2454 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2455 core->bcWidth = new_width;
2456 core->bcHeight = new_height;
2458 else
2460 scaled_info->bmiHeader.biWidth = new_width;
2461 scaled_info->bmiHeader.biHeight = new_height;
2464 if (new_height < 0) new_height = -new_height;
2466 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2467 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2469 bits = (char *)info + size;
2471 if (loadflags & LR_CREATEDIBSECTION)
2473 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2474 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2476 else
2478 if (is_dib_monochrome(fix_info))
2479 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2480 else
2481 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2484 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2485 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2486 SelectObject(screen_mem_dc, orig_bm);
2488 end:
2489 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2490 HeapFree(GetProcessHeap(), 0, scaled_info);
2491 HeapFree(GetProcessHeap(), 0, fix_info);
2492 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2494 return hbitmap;
2497 /**********************************************************************
2498 * LoadImageA (USER32.@)
2500 * See LoadImageW.
2502 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2503 INT desiredx, INT desiredy, UINT loadflags)
2505 HANDLE res;
2506 LPWSTR u_name;
2508 if (!HIWORD(name))
2509 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2511 __TRY {
2512 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2513 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2514 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2516 __EXCEPT_PAGE_FAULT {
2517 SetLastError( ERROR_INVALID_PARAMETER );
2518 return 0;
2520 __ENDTRY
2521 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2522 HeapFree(GetProcessHeap(), 0, u_name);
2523 return res;
2527 /******************************************************************************
2528 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2530 * PARAMS
2531 * hinst [I] Handle of instance that contains image
2532 * name [I] Name of image
2533 * type [I] Type of image
2534 * desiredx [I] Desired width
2535 * desiredy [I] Desired height
2536 * loadflags [I] Load flags
2538 * RETURNS
2539 * Success: Handle to newly loaded image
2540 * Failure: NULL
2542 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2544 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2545 INT desiredx, INT desiredy, UINT loadflags )
2547 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2548 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2550 if (loadflags & LR_DEFAULTSIZE) {
2551 if (type == IMAGE_ICON) {
2552 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2553 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2554 } else if (type == IMAGE_CURSOR) {
2555 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2556 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2559 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2560 switch (type) {
2561 case IMAGE_BITMAP:
2562 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2564 case IMAGE_ICON:
2565 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2566 if (screen_dc)
2568 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2569 if (palEnts == 0) palEnts = 256;
2570 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2571 palEnts, FALSE, loadflags);
2573 break;
2575 case IMAGE_CURSOR:
2576 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2577 1, TRUE, loadflags);
2579 return 0;
2582 /******************************************************************************
2583 * CopyImage (USER32.@) Creates new image and copies attributes to it
2585 * PARAMS
2586 * hnd [I] Handle to image to copy
2587 * type [I] Type of image to copy
2588 * desiredx [I] Desired width of new image
2589 * desiredy [I] Desired height of new image
2590 * flags [I] Copy flags
2592 * RETURNS
2593 * Success: Handle to newly created image
2594 * Failure: NULL
2596 * BUGS
2597 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2598 * all other versions (95/2000/XP have been tested) ignore it.
2600 * NOTES
2601 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2602 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2603 * the copy will have the same depth as the screen.
2604 * The content of the image will only be copied if the bit depth of the
2605 * original image is compatible with the bit depth of the screen, or
2606 * if the source is a DIB section.
2607 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2609 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2610 INT desiredy, UINT flags )
2612 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2613 hnd, type, desiredx, desiredy, flags);
2615 switch (type)
2617 case IMAGE_BITMAP:
2619 HBITMAP res = NULL;
2620 DIBSECTION ds;
2621 int objSize;
2622 BITMAPINFO * bi;
2624 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2625 if (!objSize) return 0;
2626 if ((desiredx < 0) || (desiredy < 0)) return 0;
2628 if (flags & LR_COPYFROMRESOURCE)
2630 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2633 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2634 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2636 /* Allocate memory for a BITMAPINFOHEADER structure and a
2637 color table. The maximum number of colors in a color table
2638 is 256 which corresponds to a bitmap with depth 8.
2639 Bitmaps with higher depths don't have color tables. */
2640 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2641 if (!bi) return 0;
2643 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2644 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2645 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2646 bi->bmiHeader.biCompression = BI_RGB;
2648 if (flags & LR_CREATEDIBSECTION)
2650 /* Create a DIB section. LR_MONOCHROME is ignored */
2651 void * bits;
2652 HDC dc = CreateCompatibleDC(NULL);
2654 if (objSize == sizeof(DIBSECTION))
2656 /* The source bitmap is a DIB.
2657 Get its attributes to create an exact copy */
2658 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2661 /* Get the color table or the color masks */
2662 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2664 bi->bmiHeader.biWidth = desiredx;
2665 bi->bmiHeader.biHeight = desiredy;
2666 bi->bmiHeader.biSizeImage = 0;
2668 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2669 DeleteDC(dc);
2671 else
2673 /* Create a device-dependent bitmap */
2675 BOOL monochrome = (flags & LR_MONOCHROME);
2677 if (objSize == sizeof(DIBSECTION))
2679 /* The source bitmap is a DIB section.
2680 Get its attributes */
2681 HDC dc = CreateCompatibleDC(NULL);
2682 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2683 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2684 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2685 DeleteDC(dc);
2687 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2689 /* Look if the colors of the DIB are black and white */
2691 monochrome =
2692 (bi->bmiColors[0].rgbRed == 0xff
2693 && bi->bmiColors[0].rgbGreen == 0xff
2694 && bi->bmiColors[0].rgbBlue == 0xff
2695 && bi->bmiColors[0].rgbReserved == 0
2696 && bi->bmiColors[1].rgbRed == 0
2697 && bi->bmiColors[1].rgbGreen == 0
2698 && bi->bmiColors[1].rgbBlue == 0
2699 && bi->bmiColors[1].rgbReserved == 0)
2701 (bi->bmiColors[0].rgbRed == 0
2702 && bi->bmiColors[0].rgbGreen == 0
2703 && bi->bmiColors[0].rgbBlue == 0
2704 && bi->bmiColors[0].rgbReserved == 0
2705 && bi->bmiColors[1].rgbRed == 0xff
2706 && bi->bmiColors[1].rgbGreen == 0xff
2707 && bi->bmiColors[1].rgbBlue == 0xff
2708 && bi->bmiColors[1].rgbReserved == 0);
2711 else if (!monochrome)
2713 monochrome = ds.dsBm.bmBitsPixel == 1;
2716 if (monochrome)
2718 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2720 else
2722 HDC screenDC = GetDC(NULL);
2723 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2724 ReleaseDC(NULL, screenDC);
2728 if (res)
2730 /* Only copy the bitmap if it's a DIB section or if it's
2731 compatible to the screen */
2732 BOOL copyContents;
2734 if (objSize == sizeof(DIBSECTION))
2736 copyContents = TRUE;
2738 else
2740 HDC screenDC = GetDC(NULL);
2741 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2742 ReleaseDC(NULL, screenDC);
2744 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2747 if (copyContents)
2749 /* The source bitmap may already be selected in a device context,
2750 use GetDIBits/StretchDIBits and not StretchBlt */
2752 HDC dc;
2753 void * bits;
2755 dc = CreateCompatibleDC(NULL);
2757 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2758 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2759 bi->bmiHeader.biSizeImage = 0;
2760 bi->bmiHeader.biClrUsed = 0;
2761 bi->bmiHeader.biClrImportant = 0;
2763 /* Fill in biSizeImage */
2764 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2765 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2767 if (bits)
2769 HBITMAP oldBmp;
2771 /* Get the image bits of the source bitmap */
2772 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2774 /* Copy it to the destination bitmap */
2775 oldBmp = SelectObject(dc, res);
2776 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2777 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2778 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2779 SelectObject(dc, oldBmp);
2781 HeapFree(GetProcessHeap(), 0, bits);
2784 DeleteDC(dc);
2787 if (flags & LR_COPYDELETEORG)
2789 DeleteObject(hnd);
2792 HeapFree(GetProcessHeap(), 0, bi);
2793 return res;
2795 case IMAGE_ICON:
2796 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2797 case IMAGE_CURSOR:
2798 /* Should call CURSORICON_ExtCopy but more testing
2799 * needs to be done before we change this
2801 if (flags) FIXME("Flags are ignored\n");
2802 return CopyCursor(hnd);
2804 return 0;
2808 /******************************************************************************
2809 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2811 * RETURNS
2812 * Success: Handle to specified bitmap
2813 * Failure: NULL
2815 HBITMAP WINAPI LoadBitmapW(
2816 HINSTANCE instance, /* [in] Handle to application instance */
2817 LPCWSTR name) /* [in] Address of bitmap resource name */
2819 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2822 /**********************************************************************
2823 * LoadBitmapA (USER32.@)
2825 * See LoadBitmapW.
2827 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2829 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );