user32: Use the stored color and mask bitmaps instead of the raw bits in DrawIconEx.
[wine/hacks.git] / dlls / user32 / cursoricon.c
blob26473652e5245bf9d67287730d29e4320afdccc7
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/server.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
63 WINE_DECLARE_DEBUG_CHANNEL(icon);
64 WINE_DECLARE_DEBUG_CHANNEL(resource);
66 #include "pshpack1.h"
68 typedef struct {
69 BYTE bWidth;
70 BYTE bHeight;
71 BYTE bColorCount;
72 BYTE bReserved;
73 WORD xHotspot;
74 WORD yHotspot;
75 DWORD dwDIBSize;
76 DWORD dwDIBOffset;
77 } CURSORICONFILEDIRENTRY;
79 typedef struct
81 WORD idReserved;
82 WORD idType;
83 WORD idCount;
84 CURSORICONFILEDIRENTRY idEntries[1];
85 } CURSORICONFILEDIR;
87 #include "poppack.h"
89 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
91 static HDC screen_dc;
93 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96 /**********************************************************************
97 * ICONCACHE for cursors/icons loaded with LR_SHARED.
99 * FIXME: This should not be allocated on the system heap, but on a
100 * subsystem-global heap (i.e. one for all Win16 processes,
101 * and one for each Win32 process).
103 typedef struct tagICONCACHE
105 struct tagICONCACHE *next;
107 HMODULE hModule;
108 HRSRC hRsrc;
109 HRSRC hGroupRsrc;
110 HICON hIcon;
112 INT count;
114 } ICONCACHE;
116 static ICONCACHE *IconAnchor = NULL;
118 static CRITICAL_SECTION IconCrst;
119 static CRITICAL_SECTION_DEBUG critsect_debug =
121 0, 0, &IconCrst,
122 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127 static const WORD ICON_HOTSPOT = 0x4242;
130 /**********************************************************************
131 * User objects management
134 struct cursoricon_object
136 struct user_object obj; /* object header */
137 ULONG_PTR param; /* opaque param used by 16-bit code */
138 HBITMAP color; /* color bitmap */
139 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
140 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
141 CURSORICONINFO data;
142 /* followed by cursor bits in CURSORICONINFO format */
145 static HICON alloc_icon_handle( unsigned int size )
147 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) + size );
148 if (!obj) return 0;
149 obj->param = 0;
150 obj->color = 0;
151 obj->alpha = 0;
152 obj->mask = 0;
153 return alloc_user_handle( &obj->obj, USER_ICON );
156 static struct cursoricon_object *get_icon_ptr( HICON handle )
158 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
159 if (obj == OBJ_OTHER_PROCESS)
161 WARN( "icon handle %p from other process\n", handle );
162 obj = NULL;
164 return obj;
167 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
169 release_user_handle_ptr( ptr );
172 static BOOL free_icon_handle( HICON handle )
174 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
176 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
177 else if (obj)
179 ULONG_PTR param = obj->param;
180 if (obj->color) DeleteObject( obj->color );
181 if (obj->alpha) DeleteObject( obj->alpha );
182 DeleteObject( obj->mask );
183 HeapFree( GetProcessHeap(), 0, obj );
184 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
185 USER_Driver->pDestroyCursorIcon( handle );
186 return TRUE;
188 return FALSE;
191 ULONG_PTR get_icon_param( HICON handle )
193 ULONG_PTR ret = 0;
194 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
196 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
197 else if (obj)
199 ret = obj->param;
200 release_user_handle_ptr( obj );
202 return ret;
205 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
207 ULONG_PTR ret = 0;
208 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
210 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
211 else if (obj)
213 ret = obj->param;
214 obj->param = param;
215 release_user_handle_ptr( obj );
217 return ret;
221 /***********************************************************************
222 * map_fileW
224 * Helper function to map a file to memory:
225 * name - file name
226 * [RETURN] ptr - pointer to mapped file
227 * [RETURN] filesize - pointer size of file to be stored if not NULL
229 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
231 HANDLE hFile, hMapping;
232 LPVOID ptr = NULL;
234 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
235 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
236 if (hFile != INVALID_HANDLE_VALUE)
238 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
239 if (hMapping)
241 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
242 CloseHandle( hMapping );
243 if (filesize)
244 *filesize = GetFileSize( hFile, NULL );
246 CloseHandle( hFile );
248 return ptr;
252 /***********************************************************************
253 * get_bitmap_width_bytes
255 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
256 * data.
258 static int get_bitmap_width_bytes( int width, int bpp )
260 switch(bpp)
262 case 1:
263 return 2 * ((width+15) / 16);
264 case 4:
265 return 2 * ((width+3) / 4);
266 case 24:
267 width *= 3;
268 /* fall through */
269 case 8:
270 return width + (width & 1);
271 case 16:
272 case 15:
273 return width * 2;
274 case 32:
275 return width * 4;
276 default:
277 WARN("Unknown depth %d, please report.\n", bpp );
279 return -1;
283 /***********************************************************************
284 * get_dib_width_bytes
286 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
288 static int get_dib_width_bytes( int width, int depth )
290 int words;
292 switch(depth)
294 case 1: words = (width + 31) / 32; break;
295 case 4: words = (width + 7) / 8; break;
296 case 8: words = (width + 3) / 4; break;
297 case 15:
298 case 16: words = (width + 1) / 2; break;
299 case 24: words = (width * 3 + 3)/4; break;
300 default:
301 WARN("(%d): Unsupported depth\n", depth );
302 /* fall through */
303 case 32:
304 words = width;
306 return 4 * words;
310 /***********************************************************************
311 * bitmap_info_size
313 * Return the size of the bitmap info structure including color table.
315 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
317 unsigned int colors, size, masks = 0;
319 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
321 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
322 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
323 return sizeof(BITMAPCOREHEADER) + colors *
324 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
326 else /* assume BITMAPINFOHEADER */
328 colors = info->bmiHeader.biClrUsed;
329 if (colors > 256) /* buffer overflow otherwise */
330 colors = 256;
331 if (!colors && (info->bmiHeader.biBitCount <= 8))
332 colors = 1 << info->bmiHeader.biBitCount;
333 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
334 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
335 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
340 /***********************************************************************
341 * copy_bitmap
343 * Helper function to duplicate a bitmap.
345 static HBITMAP copy_bitmap( HBITMAP bitmap )
347 HDC src, dst;
348 HBITMAP new_bitmap;
349 BITMAP bmp;
351 if (!bitmap) return 0;
352 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
354 src = CreateCompatibleDC( 0 );
355 dst = CreateCompatibleDC( 0 );
356 SelectObject( src, bitmap );
357 new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight );
358 SelectObject( dst, new_bitmap );
359 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
360 DeleteDC( dst );
361 DeleteDC( src );
362 return new_bitmap;
366 /***********************************************************************
367 * is_dib_monochrome
369 * Returns whether a DIB can be converted to a monochrome DDB.
371 * A DIB can be converted if its color table contains only black and
372 * white. Black must be the first color in the color table.
374 * Note : If the first color in the color table is white followed by
375 * black, we can't convert it to a monochrome DDB with
376 * SetDIBits, because black and white would be inverted.
378 static BOOL is_dib_monochrome( const BITMAPINFO* info )
380 if (info->bmiHeader.biBitCount != 1) return FALSE;
382 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
384 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
386 /* Check if the first color is black */
387 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
389 rgb++;
391 /* Check if the second color is white */
392 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
393 && (rgb->rgbtBlue == 0xff));
395 else return FALSE;
397 else /* assume BITMAPINFOHEADER */
399 const RGBQUAD *rgb = info->bmiColors;
401 /* Check if the first color is black */
402 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
403 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
405 rgb++;
407 /* Check if the second color is white */
408 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
409 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
411 else return FALSE;
415 /***********************************************************************
416 * DIB_GetBitmapInfo
418 * Get the info from a bitmap header.
419 * Return 1 for INFOHEADER, 0 for COREHEADER,
421 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
422 LONG *height, WORD *bpp, DWORD *compr )
424 if (header->biSize == sizeof(BITMAPCOREHEADER))
426 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
427 *width = core->bcWidth;
428 *height = core->bcHeight;
429 *bpp = core->bcBitCount;
430 *compr = 0;
431 return 0;
433 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
435 *width = header->biWidth;
436 *height = header->biHeight;
437 *bpp = header->biBitCount;
438 *compr = header->biCompression;
439 return 1;
441 ERR("(%d): unknown/wrong size for header\n", header->biSize );
442 return -1;
445 /**********************************************************************
446 * CURSORICON_FindSharedIcon
448 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
450 HICON hIcon = 0;
451 ICONCACHE *ptr;
453 EnterCriticalSection( &IconCrst );
455 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
456 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
458 ptr->count++;
459 hIcon = ptr->hIcon;
460 break;
463 LeaveCriticalSection( &IconCrst );
465 return hIcon;
468 /*************************************************************************
469 * CURSORICON_FindCache
471 * Given a handle, find the corresponding cache element
473 * PARAMS
474 * Handle [I] handle to an Image
476 * RETURNS
477 * Success: The cache entry
478 * Failure: NULL
481 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
483 ICONCACHE *ptr;
484 ICONCACHE *pRet=NULL;
485 BOOL IsFound = FALSE;
487 EnterCriticalSection( &IconCrst );
489 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
491 if ( hIcon == ptr->hIcon )
493 IsFound = TRUE;
494 pRet = ptr;
498 LeaveCriticalSection( &IconCrst );
500 return pRet;
503 /**********************************************************************
504 * CURSORICON_AddSharedIcon
506 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
508 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
509 if ( !ptr ) return;
511 ptr->hModule = hModule;
512 ptr->hRsrc = hRsrc;
513 ptr->hIcon = hIcon;
514 ptr->hGroupRsrc = hGroupRsrc;
515 ptr->count = 1;
517 EnterCriticalSection( &IconCrst );
518 ptr->next = IconAnchor;
519 IconAnchor = ptr;
520 LeaveCriticalSection( &IconCrst );
523 /**********************************************************************
524 * CURSORICON_DelSharedIcon
526 static INT CURSORICON_DelSharedIcon( HICON hIcon )
528 INT count = -1;
529 ICONCACHE *ptr;
531 EnterCriticalSection( &IconCrst );
533 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
534 if ( ptr->hIcon == hIcon )
536 if ( ptr->count > 0 ) ptr->count--;
537 count = ptr->count;
538 break;
541 LeaveCriticalSection( &IconCrst );
543 return count;
546 /**********************************************************************
547 * get_icon_size
549 BOOL get_icon_size( HICON handle, SIZE *size )
551 struct cursoricon_object *info;
553 if (!(info = get_icon_ptr( handle ))) return FALSE;
554 size->cx = info->data.nWidth;
555 size->cy = info->data.nHeight;
556 release_icon_ptr( handle, info );
557 return TRUE;
561 * The following macro functions account for the irregularities of
562 * accessing cursor and icon resources in files and resource entries.
564 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
565 int *width, int *height, int *bits );
567 /**********************************************************************
568 * CURSORICON_FindBestIcon
570 * Find the icon closest to the requested size and bit depth.
572 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
573 int width, int height, int depth )
575 int i, cx, cy, bits, bestEntry = -1;
576 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
577 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
579 /* Find Best Fit */
580 iTotalDiff = 0xFFFFFFFF;
581 iColorDiff = 0xFFFFFFFF;
582 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
584 iTempXDiff = abs(width - cx);
585 iTempYDiff = abs(height - cy);
587 if(iTotalDiff > (iTempXDiff + iTempYDiff))
589 iXDiff = iTempXDiff;
590 iYDiff = iTempYDiff;
591 iTotalDiff = iXDiff + iYDiff;
595 /* Find Best Colors for Best Fit */
596 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
598 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
600 iTempColorDiff = abs(depth - bits);
601 if(iColorDiff > iTempColorDiff)
603 bestEntry = i;
604 iColorDiff = iTempColorDiff;
609 return bestEntry;
612 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
613 int *width, int *height, int *bits )
615 CURSORICONDIR *resdir = dir;
616 ICONRESDIR *icon;
618 if ( resdir->idCount <= n )
619 return FALSE;
620 icon = &resdir->idEntries[n].ResInfo.icon;
621 *width = icon->bWidth;
622 *height = icon->bHeight;
623 *bits = resdir->idEntries[n].wBitCount;
624 return TRUE;
627 /**********************************************************************
628 * CURSORICON_FindBestCursor
630 * Find the cursor closest to the requested size.
632 * FIXME: parameter 'color' ignored.
634 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
635 int width, int height, int depth )
637 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
639 /* Double height to account for AND and XOR masks */
641 height *= 2;
643 /* First find the largest one smaller than or equal to the requested size*/
645 maxwidth = maxheight = 0;
646 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
648 if ((cx <= width) && (cy <= height) &&
649 (cx > maxwidth) && (cy > maxheight))
651 bestEntry = i;
652 maxwidth = cx;
653 maxheight = cy;
656 if (bestEntry != -1) return bestEntry;
658 /* Now find the smallest one larger than the requested size */
660 maxwidth = maxheight = 255;
661 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
663 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
665 bestEntry = i;
666 maxwidth = cx;
667 maxheight = cy;
671 return bestEntry;
674 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
675 int *width, int *height, int *bits )
677 CURSORICONDIR *resdir = dir;
678 CURSORDIR *cursor;
680 if ( resdir->idCount <= n )
681 return FALSE;
682 cursor = &resdir->idEntries[n].ResInfo.cursor;
683 *width = cursor->wWidth;
684 *height = cursor->wHeight;
685 *bits = resdir->idEntries[n].wBitCount;
686 return TRUE;
689 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
690 int width, int height, int depth )
692 int n;
694 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
695 width, height, depth );
696 if ( n < 0 )
697 return NULL;
698 return &dir->idEntries[n];
701 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
702 int width, int height, int depth )
704 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
705 width, height, depth );
706 if ( n < 0 )
707 return NULL;
708 return &dir->idEntries[n];
711 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
712 int *width, int *height, int *bits )
714 CURSORICONFILEDIR *filedir = dir;
715 CURSORICONFILEDIRENTRY *entry;
716 BITMAPINFOHEADER *info;
718 if ( filedir->idCount <= n )
719 return FALSE;
720 entry = &filedir->idEntries[n];
721 /* FIXME: check against file size */
722 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
723 *width = entry->bWidth;
724 *height = entry->bHeight;
725 *bits = info->biBitCount;
726 return TRUE;
729 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
730 int width, int height, int depth )
732 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
733 width, height, depth );
734 if ( n < 0 )
735 return NULL;
736 return &dir->idEntries[n];
739 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
740 int width, int height, int depth )
742 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
743 width, height, depth );
744 if ( n < 0 )
745 return NULL;
746 return &dir->idEntries[n];
749 /***********************************************************************
750 * stretch_blt_icon
752 * A helper function that stretches a bitmap buffer into an HBITMAP.
754 * PARAMS
755 * hDest [I] The handle of the destination bitmap.
756 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
757 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
758 * pSrcBits [I] A pointer to the source bitmap buffer.
760 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
762 HBITMAP hOld;
763 BOOL res = FALSE;
764 HDC hdcMem = CreateCompatibleDC(screen_dc);
766 if (hdcMem)
768 hOld = SelectObject(hdcMem, hDest);
769 res = StretchDIBits(hdcMem,
770 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
771 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
772 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
773 SelectObject(hdcMem, hOld);
774 DeleteDC( hdcMem );
777 return res;
780 /***********************************************************************
781 * bmi_has_alpha
783 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
785 int i;
786 BOOL has_alpha = FALSE;
787 const unsigned char *ptr = bits;
789 if (info->bmiHeader.biBitCount != 32) return FALSE;
790 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
791 if ((has_alpha = (ptr[3] != 0))) break;
792 return has_alpha;
795 /***********************************************************************
796 * create_alpha_bitmap
798 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
800 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
801 const BITMAPINFO *src_info, const void *color_bits )
803 HBITMAP alpha = 0;
804 BITMAPINFO *info = NULL;
805 BITMAP bm;
806 HDC hdc;
807 void *bits;
808 unsigned char *ptr;
809 int i;
811 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
812 if (bm.bmBitsPixel != 32) return 0;
814 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
815 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
816 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
817 info->bmiHeader.biWidth = bm.bmWidth;
818 info->bmiHeader.biHeight = -bm.bmHeight;
819 info->bmiHeader.biPlanes = 1;
820 info->bmiHeader.biBitCount = 32;
821 info->bmiHeader.biCompression = BI_RGB;
822 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
823 info->bmiHeader.biXPelsPerMeter = 0;
824 info->bmiHeader.biYPelsPerMeter = 0;
825 info->bmiHeader.biClrUsed = 0;
826 info->bmiHeader.biClrImportant = 0;
827 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
829 if (src_info)
831 SelectObject( hdc, alpha );
832 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
833 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
834 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
837 else
839 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
840 if (!bmi_has_alpha( info, bits ))
842 DeleteObject( alpha );
843 alpha = 0;
844 goto done;
848 /* pre-multiply by alpha */
849 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
851 unsigned int alpha = ptr[3];
852 ptr[0] = ptr[0] * alpha / 255;
853 ptr[1] = ptr[1] * alpha / 255;
854 ptr[2] = ptr[2] * alpha / 255;
857 done:
858 DeleteDC( hdc );
859 HeapFree( GetProcessHeap(), 0, info );
860 return alpha;
864 /***********************************************************************
865 * create_icon_bitmaps
867 * Create the color, mask and alpha bitmaps from the DIB info.
869 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
870 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
872 BOOL monochrome = is_dib_monochrome( bmi );
873 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
874 BITMAPINFO *info;
875 void *color_bits, *mask_bits;
876 BOOL ret = FALSE;
877 HDC hdc = 0;
879 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
880 return FALSE;
881 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
883 memcpy( info, bmi, size );
884 info->bmiHeader.biHeight /= 2;
886 color_bits = (char *)bmi + size;
887 mask_bits = (char *)color_bits +
888 get_dib_width_bytes( bmi->bmiHeader.biWidth,
889 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
891 *alpha = 0;
892 if (monochrome)
894 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
895 *color = 0;
897 /* copy color data into second half of mask bitmap */
898 SelectObject( hdc, *mask );
899 StretchDIBits( hdc, 0, height, width, height,
900 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
901 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
903 else
905 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
906 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
907 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
909 DeleteObject( *mask );
910 goto done;
912 SelectObject( hdc, *color );
913 StretchDIBits( hdc, 0, 0, width, height,
914 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
915 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
917 if (bmi_has_alpha( info, color_bits ))
918 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
920 /* convert info to monochrome to copy the mask */
921 info->bmiHeader.biBitCount = 1;
922 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
924 RGBQUAD *rgb = info->bmiColors;
926 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
927 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
928 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
929 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
931 else
933 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
935 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
936 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
940 SelectObject( hdc, *mask );
941 StretchDIBits( hdc, 0, 0, width, height,
942 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
943 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
944 ret = TRUE;
946 done:
947 DeleteDC( hdc );
948 HeapFree( GetProcessHeap(), 0, info );
949 return ret;
952 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
953 POINT16 hotspot, BOOL bIcon,
954 DWORD dwVersion,
955 INT width, INT height,
956 UINT cFlag )
958 HICON hObj;
959 int sizeAnd, sizeXor;
960 HBITMAP color = 0, mask = 0, alpha = 0, hAndBits = 0, hXorBits = 0; /* error condition for later */
961 BITMAP bmpXor, bmpAnd;
962 BOOL do_stretch;
963 INT size;
964 BITMAPINFO *pSrcInfo, *pDestInfo;
966 if (dwVersion == 0x00020000)
968 FIXME_(cursor)("\t2.xx resources are not supported\n");
969 return 0;
972 /* Check bitmap header */
974 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
975 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
976 bmi->bmiHeader.biCompression != BI_RGB) )
978 WARN_(cursor)("\tinvalid resource bitmap header.\n");
979 return 0;
982 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
984 if (!width) width = bmi->bmiHeader.biWidth;
985 if (!height) height = bmi->bmiHeader.biHeight/2;
986 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
987 (bmi->bmiHeader.biWidth != width);
989 /* Scale the hotspot */
990 if (do_stretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
992 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
993 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
996 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
997 if (!screen_dc) return 0;
999 if (create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha ))
1001 /* Make sure we have room for the monochrome bitmap later on.
1002 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1003 * up to and including the biBitCount. In-memory icon resource
1004 * format is as follows:
1006 * BITMAPINFOHEADER icHeader // DIB header
1007 * RGBQUAD icColors[] // Color table
1008 * BYTE icXOR[] // DIB bits for XOR mask
1009 * BYTE icAND[] // DIB bits for AND mask
1012 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
1013 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
1014 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
1015 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
1016 if (pSrcInfo && pDestInfo)
1018 memcpy( pSrcInfo, bmi, size );
1019 pSrcInfo->bmiHeader.biHeight /= 2;
1021 memcpy( pDestInfo, bmi, size );
1022 pDestInfo->bmiHeader.biWidth = width;
1023 pDestInfo->bmiHeader.biHeight = height;
1024 pDestInfo->bmiHeader.biSizeImage = 0;
1026 /* Create the XOR bitmap */
1027 if(pSrcInfo->bmiHeader.biBitCount == 32)
1029 void *pDIBBuffer = NULL;
1030 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
1032 if(hXorBits)
1034 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
1036 DeleteObject(hXorBits);
1037 hXorBits = 0;
1041 else
1043 if (do_stretch)
1045 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1046 if (hXorBits)
1048 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
1050 DeleteObject(hXorBits);
1051 hXorBits = 0;
1055 else
1057 if (is_dib_monochrome(bmi))
1059 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1060 SetDIBits(screen_dc, hXorBits, 0, height,
1061 (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
1063 else
1064 hXorBits = CreateDIBitmap(screen_dc, &pSrcInfo->bmiHeader,
1065 CBM_INIT, (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
1069 if( hXorBits )
1071 char* xbits = (char *)bmi + size +
1072 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1073 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1075 pSrcInfo->bmiHeader.biBitCount = 1;
1076 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1078 RGBQUAD *rgb = pSrcInfo->bmiColors;
1080 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
1081 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1082 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1083 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1085 else
1087 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
1089 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1090 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1093 /* Create the AND bitmap */
1094 if (do_stretch)
1096 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1098 if (!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
1100 DeleteObject(hAndBits);
1101 hAndBits = 0;
1104 else
1106 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1107 SetDIBits(screen_dc, hAndBits, 0, height,
1108 xbits, pSrcInfo, DIB_RGB_COLORS);
1111 if( !hAndBits )
1113 DeleteObject( hXorBits );
1114 hXorBits = 0;
1119 HeapFree( GetProcessHeap(), 0, pSrcInfo );
1120 HeapFree( GetProcessHeap(), 0, pDestInfo );
1123 if( !hXorBits || !hAndBits )
1125 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1126 DeleteObject( color );
1127 DeleteObject( alpha );
1128 DeleteObject( mask );
1129 return 0;
1132 /* Now create the CURSORICONINFO structure */
1133 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1134 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1135 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1136 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1138 hObj = alloc_icon_handle( sizeXor + sizeAnd );
1139 if (hObj)
1141 struct cursoricon_object *info = get_icon_ptr( hObj );
1143 info->color = color;
1144 info->mask = mask;
1145 info->alpha = alpha;
1146 info->data.ptHotSpot.x = hotspot.x;
1147 info->data.ptHotSpot.y = hotspot.y;
1148 info->data.nWidth = bmpXor.bmWidth;
1149 info->data.nHeight = bmpXor.bmHeight;
1150 info->data.nWidthBytes = bmpXor.bmWidthBytes;
1151 info->data.bPlanes = bmpXor.bmPlanes;
1152 info->data.bBitsPerPixel = bmpXor.bmBitsPixel;
1154 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1156 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
1157 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
1158 release_icon_ptr( hObj, info );
1159 USER_Driver->pCreateCursorIcon( hObj, &info->data );
1161 else
1163 DeleteObject( color );
1164 DeleteObject( alpha );
1165 DeleteObject( mask );
1168 DeleteObject( hAndBits );
1169 DeleteObject( hXorBits );
1170 return hObj;
1174 /**********************************************************************
1175 * .ANI cursor support
1177 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1178 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1179 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1181 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1182 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1183 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1184 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1185 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1186 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1188 #define ANI_FLAG_ICON 0x1
1189 #define ANI_FLAG_SEQUENCE 0x2
1191 typedef struct {
1192 DWORD header_size;
1193 DWORD num_frames;
1194 DWORD num_steps;
1195 DWORD width;
1196 DWORD height;
1197 DWORD bpp;
1198 DWORD num_planes;
1199 DWORD display_rate;
1200 DWORD flags;
1201 } ani_header;
1203 typedef struct {
1204 DWORD data_size;
1205 const unsigned char *data;
1206 } riff_chunk_t;
1208 static void dump_ani_header( const ani_header *header )
1210 TRACE(" header size: %d\n", header->header_size);
1211 TRACE(" frames: %d\n", header->num_frames);
1212 TRACE(" steps: %d\n", header->num_steps);
1213 TRACE(" width: %d\n", header->width);
1214 TRACE(" height: %d\n", header->height);
1215 TRACE(" bpp: %d\n", header->bpp);
1216 TRACE(" planes: %d\n", header->num_planes);
1217 TRACE(" display rate: %d\n", header->display_rate);
1218 TRACE(" flags: 0x%08x\n", header->flags);
1223 * RIFF:
1224 * DWORD "RIFF"
1225 * DWORD size
1226 * DWORD riff_id
1227 * BYTE[] data
1229 * LIST:
1230 * DWORD "LIST"
1231 * DWORD size
1232 * DWORD list_id
1233 * BYTE[] data
1235 * CHUNK:
1236 * DWORD chunk_id
1237 * DWORD size
1238 * BYTE[] data
1240 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1242 const unsigned char *ptr = parent_chunk->data;
1243 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1245 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1247 while (ptr < end)
1249 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1250 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1252 ptr += sizeof(DWORD);
1253 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1254 ptr += sizeof(DWORD);
1255 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1256 chunk->data = ptr;
1258 return;
1261 ptr += sizeof(DWORD);
1262 ptr += (*(const DWORD *)ptr + 1) & ~1;
1263 ptr += sizeof(DWORD);
1269 * .ANI layout:
1271 * RIFF:'ACON' RIFF chunk
1272 * |- CHUNK:'anih' Header
1273 * |- CHUNK:'seq ' Sequence information (optional)
1274 * \- LIST:'fram' Frame list
1275 * |- CHUNK:icon Cursor frames
1276 * |- CHUNK:icon
1277 * |- ...
1278 * \- CHUNK:icon
1280 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1281 INT width, INT height, INT depth )
1283 HCURSOR cursor;
1284 ani_header header = {0};
1285 LPBYTE frame_bits = 0;
1286 POINT16 hotspot;
1287 CURSORICONFILEDIRENTRY *entry;
1289 riff_chunk_t root_chunk = { bits_size, bits };
1290 riff_chunk_t ACON_chunk = {0};
1291 riff_chunk_t anih_chunk = {0};
1292 riff_chunk_t fram_chunk = {0};
1293 const unsigned char *icon_data;
1295 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1297 if (!bits) return 0;
1299 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1300 if (!ACON_chunk.data)
1302 ERR("Failed to get root chunk.\n");
1303 return 0;
1306 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1307 if (!anih_chunk.data)
1309 ERR("Failed to get 'anih' chunk.\n");
1310 return 0;
1312 memcpy( &header, anih_chunk.data, sizeof(header) );
1313 dump_ani_header( &header );
1315 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1316 if (!fram_chunk.data)
1318 ERR("Failed to get icon list.\n");
1319 return 0;
1322 /* FIXME: For now, just load the first frame. Before we can load all the
1323 * frames, we need to write the needed code in wineserver, etc. to handle
1324 * cursors. Once this code is written, we can extend it to support .ani
1325 * cursors and then update user32 and winex11.drv to load all frames.
1327 * Hopefully this will at least make some games (C&C3, etc.) more playable
1328 * in the meantime.
1330 FIXME("Loading all frames for .ani cursors not implemented.\n");
1331 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1333 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1334 width, height, depth );
1336 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1337 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1339 if (!header.width || !header.height)
1341 header.width = entry->bWidth;
1342 header.height = entry->bHeight;
1345 hotspot.x = entry->xHotspot;
1346 hotspot.y = entry->yHotspot;
1348 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1349 FALSE, 0x00030000, header.width, header.height, 0 );
1351 HeapFree( GetProcessHeap(), 0, frame_bits );
1353 return cursor;
1357 /**********************************************************************
1358 * CreateIconFromResourceEx (USER32.@)
1360 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1361 * with cbSize parameter as well.
1363 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1364 BOOL bIcon, DWORD dwVersion,
1365 INT width, INT height,
1366 UINT cFlag )
1368 POINT16 hotspot;
1369 BITMAPINFO *bmi;
1371 hotspot.x = ICON_HOTSPOT;
1372 hotspot.y = ICON_HOTSPOT;
1374 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1375 bits, cbSize, dwVersion, width, height,
1376 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1378 if (bIcon)
1379 bmi = (BITMAPINFO *)bits;
1380 else /* get the hotspot */
1382 POINT16 *pt = (POINT16 *)bits;
1383 hotspot = *pt;
1384 bmi = (BITMAPINFO *)(pt + 1);
1387 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1388 width, height, cFlag );
1392 /**********************************************************************
1393 * CreateIconFromResource (USER32.@)
1395 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1396 BOOL bIcon, DWORD dwVersion)
1398 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1402 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1403 INT width, INT height, INT depth,
1404 BOOL fCursor, UINT loadflags)
1406 CURSORICONFILEDIRENTRY *entry;
1407 CURSORICONFILEDIR *dir;
1408 DWORD filesize = 0;
1409 HICON hIcon = 0;
1410 LPBYTE bits;
1411 POINT16 hotspot;
1413 TRACE("loading %s\n", debugstr_w( filename ));
1415 bits = map_fileW( filename, &filesize );
1416 if (!bits)
1417 return hIcon;
1419 /* Check for .ani. */
1420 if (memcmp( bits, "RIFF", 4 ) == 0)
1422 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1423 depth );
1424 goto end;
1427 dir = (CURSORICONFILEDIR*) bits;
1428 if ( filesize < sizeof(*dir) )
1429 goto end;
1431 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1432 goto end;
1434 if ( fCursor )
1435 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1436 else
1437 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1439 if ( !entry )
1440 goto end;
1442 /* check that we don't run off the end of the file */
1443 if ( entry->dwDIBOffset > filesize )
1444 goto end;
1445 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1446 goto end;
1448 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1449 if ( fCursor )
1451 hotspot.x = entry->xHotspot;
1452 hotspot.y = entry->yHotspot;
1454 else
1456 hotspot.x = ICON_HOTSPOT;
1457 hotspot.y = ICON_HOTSPOT;
1459 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1460 hotspot, !fCursor, 0x00030000,
1461 width, height, loadflags );
1462 end:
1463 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1464 UnmapViewOfFile( bits );
1465 return hIcon;
1468 /**********************************************************************
1469 * CURSORICON_Load
1471 * Load a cursor or icon from resource or file.
1473 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1474 INT width, INT height, INT depth,
1475 BOOL fCursor, UINT loadflags)
1477 HANDLE handle = 0;
1478 HICON hIcon = 0;
1479 HRSRC hRsrc, hGroupRsrc;
1480 CURSORICONDIR *dir;
1481 CURSORICONDIRENTRY *dirEntry;
1482 LPBYTE bits;
1483 WORD wResId;
1484 DWORD dwBytesInRes;
1486 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1487 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1489 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1490 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1492 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1494 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1495 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1497 /* Get directory resource ID */
1499 if (!(hRsrc = FindResourceW( hInstance, name,
1500 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1501 return 0;
1502 hGroupRsrc = hRsrc;
1504 /* Find the best entry in the directory */
1506 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1507 if (!(dir = LockResource( handle ))) return 0;
1508 if (fCursor)
1509 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1510 else
1511 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1512 if (!dirEntry) return 0;
1513 wResId = dirEntry->wResId;
1514 dwBytesInRes = dirEntry->dwBytesInRes;
1515 FreeResource( handle );
1517 /* Load the resource */
1519 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1520 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1522 /* If shared icon, check whether it was already loaded */
1523 if ( (loadflags & LR_SHARED)
1524 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1525 return hIcon;
1527 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1528 bits = LockResource( handle );
1529 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1530 !fCursor, 0x00030000, width, height, loadflags);
1531 FreeResource( handle );
1533 /* If shared icon, add to icon cache */
1535 if ( hIcon && (loadflags & LR_SHARED) )
1536 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1538 return hIcon;
1542 /*************************************************************************
1543 * CURSORICON_ExtCopy
1545 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1547 * PARAMS
1548 * Handle [I] handle to an Image
1549 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1550 * iDesiredCX [I] The Desired width of the Image
1551 * iDesiredCY [I] The desired height of the Image
1552 * nFlags [I] The flags from CopyImage
1554 * RETURNS
1555 * Success: The new handle of the Image
1557 * NOTES
1558 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1559 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1560 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1565 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1566 INT iDesiredCX, INT iDesiredCY,
1567 UINT nFlags)
1569 HICON hNew=0;
1571 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1572 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1574 if(hIcon == 0)
1576 return 0;
1579 /* Best Fit or Monochrome */
1580 if( (nFlags & LR_COPYFROMRESOURCE
1581 && (iDesiredCX > 0 || iDesiredCY > 0))
1582 || nFlags & LR_MONOCHROME)
1584 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1586 /* Not Found in Cache, then do a straight copy
1588 if(pIconCache == NULL)
1590 hNew = CopyIcon( hIcon );
1591 if(nFlags & LR_COPYFROMRESOURCE)
1593 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1596 else
1598 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1599 LPBYTE pBits;
1600 HANDLE hMem;
1601 HRSRC hRsrc;
1602 DWORD dwBytesInRes;
1603 WORD wResId;
1604 CURSORICONDIR *pDir;
1605 CURSORICONDIRENTRY *pDirEntry;
1606 BOOL bIsIcon = (nType == IMAGE_ICON);
1608 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1610 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1611 || (iDesiredCX == 0 && iDesiredCY == 0))
1613 iDesiredCY = GetSystemMetrics(bIsIcon ?
1614 SM_CYICON : SM_CYCURSOR);
1615 iDesiredCX = GetSystemMetrics(bIsIcon ?
1616 SM_CXICON : SM_CXCURSOR);
1619 /* Retrieve the CURSORICONDIRENTRY
1621 if (!(hMem = LoadResource( pIconCache->hModule ,
1622 pIconCache->hGroupRsrc)))
1624 return 0;
1626 if (!(pDir = LockResource( hMem )))
1628 return 0;
1631 /* Find Best Fit
1633 if(bIsIcon)
1635 pDirEntry = CURSORICON_FindBestIconRes(
1636 pDir, iDesiredCX, iDesiredCY, 256 );
1638 else
1640 pDirEntry = CURSORICON_FindBestCursorRes(
1641 pDir, iDesiredCX, iDesiredCY, 1);
1644 wResId = pDirEntry->wResId;
1645 dwBytesInRes = pDirEntry->dwBytesInRes;
1646 FreeResource(hMem);
1648 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1649 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1650 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1652 /* Get the Best Fit
1654 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1655 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1657 return 0;
1659 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1661 return 0;
1664 pBits = LockResource( hMem );
1666 if(nFlags & LR_DEFAULTSIZE)
1668 iTargetCY = GetSystemMetrics(SM_CYICON);
1669 iTargetCX = GetSystemMetrics(SM_CXICON);
1672 /* Create a New Icon with the proper dimension
1674 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1675 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1676 FreeResource(hMem);
1679 else hNew = CopyIcon( hIcon );
1680 return hNew;
1684 /***********************************************************************
1685 * CreateCursor (USER32.@)
1687 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1688 INT xHotSpot, INT yHotSpot,
1689 INT nWidth, INT nHeight,
1690 LPCVOID lpANDbits, LPCVOID lpXORbits )
1692 ICONINFO info;
1693 HCURSOR hCursor;
1695 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1696 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1698 info.fIcon = FALSE;
1699 info.xHotspot = xHotSpot;
1700 info.yHotspot = yHotSpot;
1701 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1702 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1703 hCursor = CreateIconIndirect( &info );
1704 DeleteObject( info.hbmMask );
1705 DeleteObject( info.hbmColor );
1706 return hCursor;
1710 /***********************************************************************
1711 * CreateIcon (USER32.@)
1713 * Creates an icon based on the specified bitmaps. The bitmaps must be
1714 * provided in a device dependent format and will be resized to
1715 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1716 * depth. The provided bitmaps must be top-down bitmaps.
1717 * Although Windows does not support 15bpp(*) this API must support it
1718 * for Winelib applications.
1720 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1721 * format!
1723 * RETURNS
1724 * Success: handle to an icon
1725 * Failure: NULL
1727 * FIXME: Do we need to resize the bitmaps?
1729 HICON WINAPI CreateIcon(
1730 HINSTANCE hInstance, /* [in] the application's hInstance */
1731 INT nWidth, /* [in] the width of the provided bitmaps */
1732 INT nHeight, /* [in] the height of the provided bitmaps */
1733 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1734 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1735 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1736 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1738 ICONINFO iinfo;
1739 HICON hIcon;
1741 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1742 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1744 iinfo.fIcon = TRUE;
1745 iinfo.xHotspot = ICON_HOTSPOT;
1746 iinfo.yHotspot = ICON_HOTSPOT;
1747 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1748 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1750 hIcon = CreateIconIndirect( &iinfo );
1752 DeleteObject( iinfo.hbmMask );
1753 DeleteObject( iinfo.hbmColor );
1755 return hIcon;
1759 /***********************************************************************
1760 * CopyIcon (USER32.@)
1762 HICON WINAPI CopyIcon( HICON hIcon )
1764 struct cursoricon_object *ptrOld, *ptrNew;
1765 int size;
1766 HICON hNew;
1768 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1769 size = ptrOld->data.nHeight * get_bitmap_width_bytes( ptrOld->data.nWidth, 1 ); /* and bitmap */
1770 size += ptrOld->data.nHeight * ptrOld->data.nWidthBytes; /* xor bitmap */
1771 hNew = alloc_icon_handle( size );
1772 ptrNew = get_icon_ptr( hNew );
1773 memcpy( &ptrNew->data, &ptrOld->data, sizeof(ptrNew->data) + size );
1774 ptrNew->color = copy_bitmap( ptrOld->color );
1775 ptrNew->alpha = copy_bitmap( ptrOld->alpha );
1776 ptrNew->mask = copy_bitmap( ptrOld->mask );
1777 release_icon_ptr( hIcon, ptrOld );
1778 release_icon_ptr( hNew, ptrNew );
1779 USER_Driver->pCreateCursorIcon( hNew, &ptrNew->data );
1780 return hNew;
1784 /***********************************************************************
1785 * DestroyIcon (USER32.@)
1787 BOOL WINAPI DestroyIcon( HICON hIcon )
1789 TRACE_(icon)("%p\n", hIcon );
1791 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1792 free_icon_handle( hIcon );
1793 return TRUE;
1797 /***********************************************************************
1798 * DestroyCursor (USER32.@)
1800 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1802 if (GetCursor() == hCursor)
1804 WARN_(cursor)("Destroying active cursor!\n" );
1805 return FALSE;
1807 return DestroyIcon( hCursor );
1810 /***********************************************************************
1811 * DrawIcon (USER32.@)
1813 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1815 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1818 /***********************************************************************
1819 * SetCursor (USER32.@)
1821 * Set the cursor shape.
1823 * RETURNS
1824 * A handle to the previous cursor shape.
1826 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1828 HCURSOR hOldCursor;
1829 int show_count;
1830 BOOL ret;
1832 TRACE("%p\n", hCursor);
1834 SERVER_START_REQ( set_cursor )
1836 req->flags = SET_CURSOR_HANDLE;
1837 req->handle = wine_server_user_handle( hCursor );
1838 if ((ret = !wine_server_call_err( req )))
1840 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1841 show_count = reply->prev_count;
1844 SERVER_END_REQ;
1846 if (!ret) return 0;
1848 /* Change the cursor shape only if it is visible */
1849 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1850 return hOldCursor;
1853 /***********************************************************************
1854 * ShowCursor (USER32.@)
1856 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1858 HCURSOR cursor;
1859 int increment = bShow ? 1 : -1;
1860 int count;
1862 SERVER_START_REQ( set_cursor )
1864 req->flags = SET_CURSOR_COUNT;
1865 req->show_count = increment;
1866 wine_server_call( req );
1867 cursor = wine_server_ptr_handle( reply->prev_handle );
1868 count = reply->prev_count + increment;
1870 SERVER_END_REQ;
1872 TRACE("%d, count=%d\n", bShow, count );
1874 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1875 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1877 return count;
1880 /***********************************************************************
1881 * GetCursor (USER32.@)
1883 HCURSOR WINAPI GetCursor(void)
1885 HCURSOR ret;
1887 SERVER_START_REQ( set_cursor )
1889 req->flags = 0;
1890 wine_server_call( req );
1891 ret = wine_server_ptr_handle( reply->prev_handle );
1893 SERVER_END_REQ;
1894 return ret;
1898 /***********************************************************************
1899 * ClipCursor (USER32.@)
1901 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1903 RECT virt;
1905 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1906 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1907 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1908 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1910 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1911 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1913 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1914 CURSOR_ClipRect = virt;
1916 USER_Driver->pClipCursor( rect );
1917 return TRUE;
1921 /***********************************************************************
1922 * GetClipCursor (USER32.@)
1924 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1926 /* If this is first time - initialize the rect */
1927 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1929 return CopyRect( rect, &CURSOR_ClipRect );
1933 /***********************************************************************
1934 * SetSystemCursor (USER32.@)
1936 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1938 FIXME("(%p,%08x),stub!\n", hcur, id);
1939 return TRUE;
1943 /**********************************************************************
1944 * LookupIconIdFromDirectoryEx (USER32.@)
1946 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1947 INT width, INT height, UINT cFlag )
1949 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1950 UINT retVal = 0;
1951 if( dir && !dir->idReserved && (dir->idType & 3) )
1953 CURSORICONDIRENTRY* entry;
1955 const HDC hdc = GetDC(0);
1956 const int depth = (cFlag & LR_MONOCHROME) ?
1957 1 : GetDeviceCaps(hdc, BITSPIXEL);
1958 ReleaseDC(0, hdc);
1960 if( bIcon )
1961 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1962 else
1963 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1965 if( entry ) retVal = entry->wResId;
1967 else WARN_(cursor)("invalid resource directory\n");
1968 return retVal;
1971 /**********************************************************************
1972 * LookupIconIdFromDirectory (USER32.@)
1974 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1976 return LookupIconIdFromDirectoryEx( dir, bIcon,
1977 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1978 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1981 /***********************************************************************
1982 * LoadCursorW (USER32.@)
1984 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1986 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1988 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1989 LR_SHARED | LR_DEFAULTSIZE );
1992 /***********************************************************************
1993 * LoadCursorA (USER32.@)
1995 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1997 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1999 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2000 LR_SHARED | LR_DEFAULTSIZE );
2003 /***********************************************************************
2004 * LoadCursorFromFileW (USER32.@)
2006 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2008 TRACE("%s\n", debugstr_w(name));
2010 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2011 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2014 /***********************************************************************
2015 * LoadCursorFromFileA (USER32.@)
2017 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2019 TRACE("%s\n", debugstr_a(name));
2021 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2022 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2025 /***********************************************************************
2026 * LoadIconW (USER32.@)
2028 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2030 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2032 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2033 LR_SHARED | LR_DEFAULTSIZE );
2036 /***********************************************************************
2037 * LoadIconA (USER32.@)
2039 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2041 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2043 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2044 LR_SHARED | LR_DEFAULTSIZE );
2047 /**********************************************************************
2048 * GetIconInfo (USER32.@)
2050 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2052 struct cursoricon_object *ciconinfo;
2053 INT height;
2055 if (!(ciconinfo = get_icon_ptr( hIcon ))) return FALSE;
2057 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2058 ciconinfo->data.nWidth, ciconinfo->data.nHeight, ciconinfo->data.bBitsPerPixel);
2060 if ( (ciconinfo->data.ptHotSpot.x == ICON_HOTSPOT) &&
2061 (ciconinfo->data.ptHotSpot.y == ICON_HOTSPOT) )
2063 iconinfo->fIcon = TRUE;
2064 iconinfo->xHotspot = ciconinfo->data.nWidth / 2;
2065 iconinfo->yHotspot = ciconinfo->data.nHeight / 2;
2067 else
2069 iconinfo->fIcon = FALSE;
2070 iconinfo->xHotspot = ciconinfo->data.ptHotSpot.x;
2071 iconinfo->yHotspot = ciconinfo->data.ptHotSpot.y;
2074 height = ciconinfo->data.nHeight;
2076 if (ciconinfo->data.bBitsPerPixel > 1)
2078 iconinfo->hbmColor = CreateBitmap( ciconinfo->data.nWidth, ciconinfo->data.nHeight,
2079 ciconinfo->data.bPlanes, ciconinfo->data.bBitsPerPixel,
2080 (char *)(ciconinfo + 1)
2081 + ciconinfo->data.nHeight *
2082 get_bitmap_width_bytes (ciconinfo->data.nWidth,1) );
2084 else
2086 iconinfo->hbmColor = 0;
2087 height *= 2;
2090 iconinfo->hbmMask = CreateBitmap ( ciconinfo->data.nWidth, height,
2091 1, 1, ciconinfo + 1);
2092 release_icon_ptr( hIcon, ciconinfo );
2094 return TRUE;
2097 /**********************************************************************
2098 * CreateIconIndirect (USER32.@)
2100 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2102 DIBSECTION bmpXor;
2103 BITMAP bmpAnd;
2104 HICON hObj;
2105 HBITMAP color = 0, mask;
2106 int xor_objsize = 0, sizeXor = 0, sizeAnd, width, height, planes, bpp;
2107 HDC src, dst;
2109 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2110 iconinfo->hbmColor, iconinfo->hbmMask,
2111 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2113 if (!iconinfo->hbmMask) return 0;
2115 planes = GetDeviceCaps( screen_dc, PLANES );
2116 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2118 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2119 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2120 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2121 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2123 if (iconinfo->hbmColor)
2125 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2126 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2127 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2128 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2129 /* we can use either depth 1 or screen depth for xor bitmap */
2130 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2131 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2133 width = bmpXor.dsBm.bmWidth;
2134 height = bmpXor.dsBm.bmHeight;
2135 if (bmpXor.dsBm.bmPlanes * bmpXor.dsBm.bmBitsPixel != 1)
2137 color = CreateCompatibleBitmap( screen_dc, width, height );
2138 mask = CreateBitmap( width, height, 1, 1, NULL );
2140 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2142 else
2144 width = bmpAnd.bmWidth;
2145 height = bmpAnd.bmHeight;
2146 mask = CreateBitmap( width, height, 1, 1, NULL );
2149 sizeAnd = height * get_bitmap_width_bytes(width, 1);
2151 src = CreateCompatibleDC( 0 );
2152 dst = CreateCompatibleDC( 0 );
2154 SelectObject( src, iconinfo->hbmMask );
2155 SelectObject( dst, mask );
2156 StretchBlt( dst, 0, 0, width, height, src, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, SRCCOPY );
2158 if (color)
2160 SelectObject( src, iconinfo->hbmColor );
2161 SelectObject( dst, color );
2162 BitBlt( dst, 0, 0, width, height, src, 0, 0, SRCCOPY );
2164 else if (iconinfo->hbmColor)
2166 SelectObject( src, iconinfo->hbmColor );
2167 BitBlt( dst, 0, height, width, height, src, 0, 0, SRCCOPY );
2169 DeleteDC( src );
2170 DeleteDC( dst );
2172 hObj = alloc_icon_handle( sizeXor + sizeAnd );
2173 if (hObj)
2175 struct cursoricon_object *info = get_icon_ptr( hObj );
2177 info->color = color;
2178 info->mask = mask;
2179 info->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2181 /* If we are creating an icon, the hotspot is unused */
2182 if (iconinfo->fIcon)
2184 info->data.ptHotSpot.x = ICON_HOTSPOT;
2185 info->data.ptHotSpot.y = ICON_HOTSPOT;
2187 else
2189 info->data.ptHotSpot.x = iconinfo->xHotspot;
2190 info->data.ptHotSpot.y = iconinfo->yHotspot;
2193 if (iconinfo->hbmColor)
2195 info->data.nWidth = bmpXor.dsBm.bmWidth;
2196 info->data.nHeight = bmpXor.dsBm.bmHeight;
2197 info->data.nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2198 info->data.bPlanes = planes;
2199 info->data.bBitsPerPixel = bpp;
2201 else
2203 info->data.nWidth = bmpAnd.bmWidth;
2204 info->data.nHeight = bmpAnd.bmHeight / 2;
2205 info->data.nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2206 info->data.bPlanes = 1;
2207 info->data.bBitsPerPixel = 1;
2210 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2212 GetBitmapBits( mask, sizeAnd, info + 1 );
2214 if (color)
2216 char *dst_bits = (char*)(info + 1) + sizeAnd;
2218 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2219 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2220 else
2222 BITMAPINFO bminfo;
2223 int dib_width = get_dib_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2224 int bitmap_width = get_bitmap_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2226 bminfo.bmiHeader.biSize = sizeof(bminfo);
2227 bminfo.bmiHeader.biWidth = info->data.nWidth;
2228 bminfo.bmiHeader.biHeight = info->data.nHeight;
2229 bminfo.bmiHeader.biPlanes = info->data.bPlanes;
2230 bminfo.bmiHeader.biBitCount = info->data.bBitsPerPixel;
2231 bminfo.bmiHeader.biCompression = BI_RGB;
2232 bminfo.bmiHeader.biSizeImage = info->data.nHeight * dib_width;
2233 bminfo.bmiHeader.biXPelsPerMeter = 0;
2234 bminfo.bmiHeader.biYPelsPerMeter = 0;
2235 bminfo.bmiHeader.biClrUsed = 0;
2236 bminfo.bmiHeader.biClrImportant = 0;
2238 /* swap lines for dib sections */
2239 if (xor_objsize == sizeof(DIBSECTION))
2240 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2242 if (dib_width != bitmap_width) /* need to fixup alignment */
2244 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2246 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2247 src_bits, &bminfo, DIB_RGB_COLORS ))
2249 int y;
2250 for (y = 0; y < info->data.nHeight; y++)
2251 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2253 HeapFree( GetProcessHeap(), 0, src_bits );
2255 else
2256 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2257 dst_bits, &bminfo, DIB_RGB_COLORS );
2260 release_icon_ptr( hObj, info );
2261 USER_Driver->pCreateCursorIcon( hObj, &info->data );
2263 return hObj;
2266 /******************************************************************************
2267 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2269 * NOTES
2270 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2272 * PARAMS
2273 * hdc [I] Handle to device context
2274 * x0 [I] X coordinate of upper left corner
2275 * y0 [I] Y coordinate of upper left corner
2276 * hIcon [I] Handle to icon to draw
2277 * cxWidth [I] Width of icon
2278 * cyWidth [I] Height of icon
2279 * istep [I] Index of frame in animated cursor
2280 * hbr [I] Handle to background brush
2281 * flags [I] Icon-drawing flags
2283 * RETURNS
2284 * Success: TRUE
2285 * Failure: FALSE
2287 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2288 INT cxWidth, INT cyWidth, UINT istep,
2289 HBRUSH hbr, UINT flags )
2291 struct cursoricon_object *ptr;
2292 HDC hdc_dest, hMemDC;
2293 BOOL result = FALSE, DoOffscreen;
2294 HBITMAP hB_off = 0;
2295 COLORREF oldFg, oldBg;
2296 INT x, y, nStretchMode;
2298 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2299 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2301 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2302 if (!(hMemDC = CreateCompatibleDC( hdc )))
2304 release_icon_ptr( hIcon, ptr );
2305 return FALSE;
2308 if (istep)
2309 FIXME_(icon)("Ignoring istep=%d\n", istep);
2310 if (flags & DI_NOMIRROR)
2311 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2313 /* Calculate the size of the destination image. */
2314 if (cxWidth == 0)
2316 if (flags & DI_DEFAULTSIZE)
2317 cxWidth = GetSystemMetrics (SM_CXICON);
2318 else
2319 cxWidth = ptr->data.nWidth;
2321 if (cyWidth == 0)
2323 if (flags & DI_DEFAULTSIZE)
2324 cyWidth = GetSystemMetrics (SM_CYICON);
2325 else
2326 cyWidth = ptr->data.nHeight;
2329 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2331 if (DoOffscreen) {
2332 RECT r;
2334 r.left = 0;
2335 r.top = 0;
2336 r.right = cxWidth;
2337 r.bottom = cxWidth;
2339 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto done;
2340 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2342 DeleteDC( hdc_dest );
2343 goto done;
2345 SelectObject(hdc_dest, hB_off);
2346 FillRect(hdc_dest, &r, hbr);
2347 x = y = 0;
2349 else
2351 hdc_dest = hdc;
2352 x = x0;
2353 y = y0;
2356 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2358 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2359 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2361 if ((flags & DI_MASK) && (!ptr->alpha || !(flags & DI_IMAGE)))
2363 SelectObject( hMemDC, ptr->mask );
2364 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2365 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND );
2368 if (flags & DI_IMAGE)
2370 if (ptr->alpha)
2372 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2374 SelectObject( hMemDC, ptr->alpha );
2375 GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2376 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend );
2378 else if (ptr->color)
2380 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2381 SelectObject( hMemDC, ptr->color );
2382 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2383 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop );
2385 else
2387 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2388 SelectObject( hMemDC, ptr->mask );
2389 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2390 hMemDC, 0, ptr->data.nHeight, ptr->data.nWidth, ptr->data.nHeight, rop );
2394 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2396 SetTextColor( hdc, oldFg );
2397 SetBkColor( hdc, oldBg );
2398 SetStretchBltMode (hdc, nStretchMode);
2399 result = TRUE;
2400 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2401 if (hB_off) DeleteObject(hB_off);
2402 done:
2403 DeleteDC( hMemDC );
2404 release_icon_ptr( hIcon, ptr );
2405 return result;
2408 /***********************************************************************
2409 * DIB_FixColorsToLoadflags
2411 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2412 * are in loadflags
2414 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2416 int colors;
2417 COLORREF c_W, c_S, c_F, c_L, c_C;
2418 int incr,i;
2419 RGBQUAD *ptr;
2420 int bitmap_type;
2421 LONG width;
2422 LONG height;
2423 WORD bpp;
2424 DWORD compr;
2426 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2428 WARN_(resource)("Invalid bitmap\n");
2429 return;
2432 if (bpp > 8) return;
2434 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2436 incr = 3;
2437 colors = 1 << bpp;
2439 else
2441 incr = 4;
2442 colors = bmi->bmiHeader.biClrUsed;
2443 if (colors > 256) colors = 256;
2444 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2447 c_W = GetSysColor(COLOR_WINDOW);
2448 c_S = GetSysColor(COLOR_3DSHADOW);
2449 c_F = GetSysColor(COLOR_3DFACE);
2450 c_L = GetSysColor(COLOR_3DLIGHT);
2452 if (loadflags & LR_LOADTRANSPARENT) {
2453 switch (bpp) {
2454 case 1: pix = pix >> 7; break;
2455 case 4: pix = pix >> 4; break;
2456 case 8: break;
2457 default:
2458 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2459 return;
2461 if (pix >= colors) {
2462 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2463 return;
2465 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2466 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2467 ptr->rgbBlue = GetBValue(c_W);
2468 ptr->rgbGreen = GetGValue(c_W);
2469 ptr->rgbRed = GetRValue(c_W);
2471 if (loadflags & LR_LOADMAP3DCOLORS)
2472 for (i=0; i<colors; i++) {
2473 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2474 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2475 if (c_C == RGB(128, 128, 128)) {
2476 ptr->rgbRed = GetRValue(c_S);
2477 ptr->rgbGreen = GetGValue(c_S);
2478 ptr->rgbBlue = GetBValue(c_S);
2479 } else if (c_C == RGB(192, 192, 192)) {
2480 ptr->rgbRed = GetRValue(c_F);
2481 ptr->rgbGreen = GetGValue(c_F);
2482 ptr->rgbBlue = GetBValue(c_F);
2483 } else if (c_C == RGB(223, 223, 223)) {
2484 ptr->rgbRed = GetRValue(c_L);
2485 ptr->rgbGreen = GetGValue(c_L);
2486 ptr->rgbBlue = GetBValue(c_L);
2492 /**********************************************************************
2493 * BITMAP_Load
2495 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2496 INT desiredx, INT desiredy, UINT loadflags )
2498 HBITMAP hbitmap = 0, orig_bm;
2499 HRSRC hRsrc;
2500 HGLOBAL handle;
2501 char *ptr = NULL;
2502 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2503 int size;
2504 BYTE pix;
2505 char *bits;
2506 LONG width, height, new_width, new_height;
2507 WORD bpp_dummy;
2508 DWORD compr_dummy, offbits = 0;
2509 INT bm_type;
2510 HDC screen_mem_dc = NULL;
2512 if (!(loadflags & LR_LOADFROMFILE))
2514 if (!instance)
2516 /* OEM bitmap: try to load the resource from user32.dll */
2517 instance = user32_module;
2520 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2521 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2523 if ((info = LockResource( handle )) == NULL) return 0;
2525 else
2527 BITMAPFILEHEADER * bmfh;
2529 if (!(ptr = map_fileW( name, NULL ))) return 0;
2530 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2531 bmfh = (BITMAPFILEHEADER *)ptr;
2532 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2534 WARN("Invalid/unsupported bitmap format!\n");
2535 goto end_close;
2537 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2540 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2541 WARN("Broken BitmapInfoHeader!\n");
2542 goto end_close;
2545 size = bitmap_info_size(info, DIB_RGB_COLORS);
2546 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2547 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2549 if (!fix_info || !scaled_info) goto end;
2550 memcpy(fix_info, info, size);
2552 pix = *((LPBYTE)info + size);
2553 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2555 memcpy(scaled_info, fix_info, size);
2556 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2557 &bpp_dummy, &compr_dummy);
2558 if(desiredx != 0)
2559 new_width = desiredx;
2560 else
2561 new_width = width;
2563 if(desiredy != 0)
2564 new_height = height > 0 ? desiredy : -desiredy;
2565 else
2566 new_height = height;
2568 if(bm_type == 0)
2570 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2571 core->bcWidth = new_width;
2572 core->bcHeight = new_height;
2574 else
2576 scaled_info->bmiHeader.biWidth = new_width;
2577 scaled_info->bmiHeader.biHeight = new_height;
2580 if (new_height < 0) new_height = -new_height;
2582 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2583 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2585 bits = (char *)info + (offbits ? offbits : size);
2587 if (loadflags & LR_CREATEDIBSECTION)
2589 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2590 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2592 else
2594 if (is_dib_monochrome(fix_info))
2595 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2596 else
2597 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2600 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2601 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2602 SelectObject(screen_mem_dc, orig_bm);
2604 end:
2605 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2606 HeapFree(GetProcessHeap(), 0, scaled_info);
2607 HeapFree(GetProcessHeap(), 0, fix_info);
2608 end_close:
2609 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2611 return hbitmap;
2614 /**********************************************************************
2615 * LoadImageA (USER32.@)
2617 * See LoadImageW.
2619 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2620 INT desiredx, INT desiredy, UINT loadflags)
2622 HANDLE res;
2623 LPWSTR u_name;
2625 if (IS_INTRESOURCE(name))
2626 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2628 __TRY {
2629 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2630 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2631 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2633 __EXCEPT_PAGE_FAULT {
2634 SetLastError( ERROR_INVALID_PARAMETER );
2635 return 0;
2637 __ENDTRY
2638 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2639 HeapFree(GetProcessHeap(), 0, u_name);
2640 return res;
2644 /******************************************************************************
2645 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2647 * PARAMS
2648 * hinst [I] Handle of instance that contains image
2649 * name [I] Name of image
2650 * type [I] Type of image
2651 * desiredx [I] Desired width
2652 * desiredy [I] Desired height
2653 * loadflags [I] Load flags
2655 * RETURNS
2656 * Success: Handle to newly loaded image
2657 * Failure: NULL
2659 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2661 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2662 INT desiredx, INT desiredy, UINT loadflags )
2664 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2665 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2667 if (loadflags & LR_DEFAULTSIZE) {
2668 if (type == IMAGE_ICON) {
2669 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2670 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2671 } else if (type == IMAGE_CURSOR) {
2672 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2673 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2676 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2677 switch (type) {
2678 case IMAGE_BITMAP:
2679 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2681 case IMAGE_ICON:
2682 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2683 if (screen_dc)
2685 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2686 GetDeviceCaps(screen_dc, BITSPIXEL),
2687 FALSE, loadflags);
2689 break;
2691 case IMAGE_CURSOR:
2692 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2693 1, TRUE, loadflags);
2695 return 0;
2698 /******************************************************************************
2699 * CopyImage (USER32.@) Creates new image and copies attributes to it
2701 * PARAMS
2702 * hnd [I] Handle to image to copy
2703 * type [I] Type of image to copy
2704 * desiredx [I] Desired width of new image
2705 * desiredy [I] Desired height of new image
2706 * flags [I] Copy flags
2708 * RETURNS
2709 * Success: Handle to newly created image
2710 * Failure: NULL
2712 * BUGS
2713 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2714 * all other versions (95/2000/XP have been tested) ignore it.
2716 * NOTES
2717 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2718 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2719 * the copy will have the same depth as the screen.
2720 * The content of the image will only be copied if the bit depth of the
2721 * original image is compatible with the bit depth of the screen, or
2722 * if the source is a DIB section.
2723 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2725 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2726 INT desiredy, UINT flags )
2728 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2729 hnd, type, desiredx, desiredy, flags);
2731 switch (type)
2733 case IMAGE_BITMAP:
2735 HBITMAP res = NULL;
2736 DIBSECTION ds;
2737 int objSize;
2738 BITMAPINFO * bi;
2740 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2741 if (!objSize) return 0;
2742 if ((desiredx < 0) || (desiredy < 0)) return 0;
2744 if (flags & LR_COPYFROMRESOURCE)
2746 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2749 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2750 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2752 /* Allocate memory for a BITMAPINFOHEADER structure and a
2753 color table. The maximum number of colors in a color table
2754 is 256 which corresponds to a bitmap with depth 8.
2755 Bitmaps with higher depths don't have color tables. */
2756 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2757 if (!bi) return 0;
2759 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2760 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2761 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2762 bi->bmiHeader.biCompression = BI_RGB;
2764 if (flags & LR_CREATEDIBSECTION)
2766 /* Create a DIB section. LR_MONOCHROME is ignored */
2767 void * bits;
2768 HDC dc = CreateCompatibleDC(NULL);
2770 if (objSize == sizeof(DIBSECTION))
2772 /* The source bitmap is a DIB.
2773 Get its attributes to create an exact copy */
2774 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2777 /* Get the color table or the color masks */
2778 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2780 bi->bmiHeader.biWidth = desiredx;
2781 bi->bmiHeader.biHeight = desiredy;
2782 bi->bmiHeader.biSizeImage = 0;
2784 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2785 DeleteDC(dc);
2787 else
2789 /* Create a device-dependent bitmap */
2791 BOOL monochrome = (flags & LR_MONOCHROME);
2793 if (objSize == sizeof(DIBSECTION))
2795 /* The source bitmap is a DIB section.
2796 Get its attributes */
2797 HDC dc = CreateCompatibleDC(NULL);
2798 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2799 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2800 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2801 DeleteDC(dc);
2803 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2805 /* Look if the colors of the DIB are black and white */
2807 monochrome =
2808 (bi->bmiColors[0].rgbRed == 0xff
2809 && bi->bmiColors[0].rgbGreen == 0xff
2810 && bi->bmiColors[0].rgbBlue == 0xff
2811 && bi->bmiColors[0].rgbReserved == 0
2812 && bi->bmiColors[1].rgbRed == 0
2813 && bi->bmiColors[1].rgbGreen == 0
2814 && bi->bmiColors[1].rgbBlue == 0
2815 && bi->bmiColors[1].rgbReserved == 0)
2817 (bi->bmiColors[0].rgbRed == 0
2818 && bi->bmiColors[0].rgbGreen == 0
2819 && bi->bmiColors[0].rgbBlue == 0
2820 && bi->bmiColors[0].rgbReserved == 0
2821 && bi->bmiColors[1].rgbRed == 0xff
2822 && bi->bmiColors[1].rgbGreen == 0xff
2823 && bi->bmiColors[1].rgbBlue == 0xff
2824 && bi->bmiColors[1].rgbReserved == 0);
2827 else if (!monochrome)
2829 monochrome = ds.dsBm.bmBitsPixel == 1;
2832 if (monochrome)
2834 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2836 else
2838 HDC screenDC = GetDC(NULL);
2839 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2840 ReleaseDC(NULL, screenDC);
2844 if (res)
2846 /* Only copy the bitmap if it's a DIB section or if it's
2847 compatible to the screen */
2848 BOOL copyContents;
2850 if (objSize == sizeof(DIBSECTION))
2852 copyContents = TRUE;
2854 else
2856 HDC screenDC = GetDC(NULL);
2857 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2858 ReleaseDC(NULL, screenDC);
2860 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2863 if (copyContents)
2865 /* The source bitmap may already be selected in a device context,
2866 use GetDIBits/StretchDIBits and not StretchBlt */
2868 HDC dc;
2869 void * bits;
2871 dc = CreateCompatibleDC(NULL);
2873 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2874 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2875 bi->bmiHeader.biSizeImage = 0;
2876 bi->bmiHeader.biClrUsed = 0;
2877 bi->bmiHeader.biClrImportant = 0;
2879 /* Fill in biSizeImage */
2880 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2881 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2883 if (bits)
2885 HBITMAP oldBmp;
2887 /* Get the image bits of the source bitmap */
2888 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2890 /* Copy it to the destination bitmap */
2891 oldBmp = SelectObject(dc, res);
2892 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2893 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2894 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2895 SelectObject(dc, oldBmp);
2897 HeapFree(GetProcessHeap(), 0, bits);
2900 DeleteDC(dc);
2903 if (flags & LR_COPYDELETEORG)
2905 DeleteObject(hnd);
2908 HeapFree(GetProcessHeap(), 0, bi);
2909 return res;
2911 case IMAGE_ICON:
2912 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2913 case IMAGE_CURSOR:
2914 /* Should call CURSORICON_ExtCopy but more testing
2915 * needs to be done before we change this
2917 if (flags) FIXME("Flags are ignored\n");
2918 return CopyCursor(hnd);
2920 return 0;
2924 /******************************************************************************
2925 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2927 * RETURNS
2928 * Success: Handle to specified bitmap
2929 * Failure: NULL
2931 HBITMAP WINAPI LoadBitmapW(
2932 HINSTANCE instance, /* [in] Handle to application instance */
2933 LPCWSTR name) /* [in] Address of bitmap resource name */
2935 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2938 /**********************************************************************
2939 * LoadBitmapA (USER32.@)
2941 * See LoadBitmapW.
2943 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2945 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );