user32: Store color and mask bitmaps in the cursor data.
[wine/multimedia.git] / dlls / user32 / cursoricon.c
blob24099a80e8f997f0565399fb17ea4c63e257c19b
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 mask; /* mask bitmap (followed by color for 1-bpp icons) */
140 CURSORICONINFO data;
141 /* followed by cursor bits in CURSORICONINFO format */
144 static HICON alloc_icon_handle( unsigned int size )
146 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) + size );
147 if (!obj) return 0;
148 obj->param = 0;
149 obj->color = 0;
150 obj->mask = 0;
151 return alloc_user_handle( &obj->obj, USER_ICON );
154 static struct cursoricon_object *get_icon_ptr( HICON handle )
156 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
157 if (obj == OBJ_OTHER_PROCESS)
159 WARN( "icon handle %p from other process\n", handle );
160 obj = NULL;
162 return obj;
165 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
167 release_user_handle_ptr( ptr );
170 static BOOL free_icon_handle( HICON handle )
172 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
174 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
175 else if (obj)
177 ULONG_PTR param = obj->param;
178 if (obj->color) DeleteObject( obj->color );
179 DeleteObject( obj->mask );
180 HeapFree( GetProcessHeap(), 0, obj );
181 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
182 USER_Driver->pDestroyCursorIcon( handle );
183 return TRUE;
185 return FALSE;
188 ULONG_PTR get_icon_param( HICON handle )
190 ULONG_PTR ret = 0;
191 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
193 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
194 else if (obj)
196 ret = obj->param;
197 release_user_handle_ptr( obj );
199 return ret;
202 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
204 ULONG_PTR ret = 0;
205 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
207 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
208 else if (obj)
210 ret = obj->param;
211 obj->param = param;
212 release_user_handle_ptr( obj );
214 return ret;
218 /***********************************************************************
219 * map_fileW
221 * Helper function to map a file to memory:
222 * name - file name
223 * [RETURN] ptr - pointer to mapped file
224 * [RETURN] filesize - pointer size of file to be stored if not NULL
226 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
228 HANDLE hFile, hMapping;
229 LPVOID ptr = NULL;
231 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
232 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
233 if (hFile != INVALID_HANDLE_VALUE)
235 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
236 if (hMapping)
238 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
239 CloseHandle( hMapping );
240 if (filesize)
241 *filesize = GetFileSize( hFile, NULL );
243 CloseHandle( hFile );
245 return ptr;
249 /***********************************************************************
250 * get_bitmap_width_bytes
252 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
253 * data.
255 static int get_bitmap_width_bytes( int width, int bpp )
257 switch(bpp)
259 case 1:
260 return 2 * ((width+15) / 16);
261 case 4:
262 return 2 * ((width+3) / 4);
263 case 24:
264 width *= 3;
265 /* fall through */
266 case 8:
267 return width + (width & 1);
268 case 16:
269 case 15:
270 return width * 2;
271 case 32:
272 return width * 4;
273 default:
274 WARN("Unknown depth %d, please report.\n", bpp );
276 return -1;
280 /***********************************************************************
281 * get_dib_width_bytes
283 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
285 static int get_dib_width_bytes( int width, int depth )
287 int words;
289 switch(depth)
291 case 1: words = (width + 31) / 32; break;
292 case 4: words = (width + 7) / 8; break;
293 case 8: words = (width + 3) / 4; break;
294 case 15:
295 case 16: words = (width + 1) / 2; break;
296 case 24: words = (width * 3 + 3)/4; break;
297 default:
298 WARN("(%d): Unsupported depth\n", depth );
299 /* fall through */
300 case 32:
301 words = width;
303 return 4 * words;
307 /***********************************************************************
308 * bitmap_info_size
310 * Return the size of the bitmap info structure including color table.
312 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
314 unsigned int colors, size, masks = 0;
316 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
318 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
319 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
320 return sizeof(BITMAPCOREHEADER) + colors *
321 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
323 else /* assume BITMAPINFOHEADER */
325 colors = info->bmiHeader.biClrUsed;
326 if (colors > 256) /* buffer overflow otherwise */
327 colors = 256;
328 if (!colors && (info->bmiHeader.biBitCount <= 8))
329 colors = 1 << info->bmiHeader.biBitCount;
330 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
331 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
332 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
337 /***********************************************************************
338 * copy_bitmap
340 * Helper function to duplicate a bitmap.
342 static HBITMAP copy_bitmap( HBITMAP bitmap )
344 HDC src, dst;
345 HBITMAP new_bitmap;
346 BITMAP bmp;
348 if (!bitmap) return 0;
349 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
351 src = CreateCompatibleDC( 0 );
352 dst = CreateCompatibleDC( 0 );
353 SelectObject( src, bitmap );
354 new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight );
355 SelectObject( dst, new_bitmap );
356 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
357 DeleteDC( dst );
358 DeleteDC( src );
359 return new_bitmap;
363 /***********************************************************************
364 * is_dib_monochrome
366 * Returns whether a DIB can be converted to a monochrome DDB.
368 * A DIB can be converted if its color table contains only black and
369 * white. Black must be the first color in the color table.
371 * Note : If the first color in the color table is white followed by
372 * black, we can't convert it to a monochrome DDB with
373 * SetDIBits, because black and white would be inverted.
375 static BOOL is_dib_monochrome( const BITMAPINFO* info )
377 if (info->bmiHeader.biBitCount != 1) return FALSE;
379 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
381 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
383 /* Check if the first color is black */
384 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
386 rgb++;
388 /* Check if the second color is white */
389 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
390 && (rgb->rgbtBlue == 0xff));
392 else return FALSE;
394 else /* assume BITMAPINFOHEADER */
396 const RGBQUAD *rgb = info->bmiColors;
398 /* Check if the first color is black */
399 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
400 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
402 rgb++;
404 /* Check if the second color is white */
405 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
406 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
408 else return FALSE;
412 /***********************************************************************
413 * DIB_GetBitmapInfo
415 * Get the info from a bitmap header.
416 * Return 1 for INFOHEADER, 0 for COREHEADER,
418 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
419 LONG *height, WORD *bpp, DWORD *compr )
421 if (header->biSize == sizeof(BITMAPCOREHEADER))
423 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
424 *width = core->bcWidth;
425 *height = core->bcHeight;
426 *bpp = core->bcBitCount;
427 *compr = 0;
428 return 0;
430 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
432 *width = header->biWidth;
433 *height = header->biHeight;
434 *bpp = header->biBitCount;
435 *compr = header->biCompression;
436 return 1;
438 ERR("(%d): unknown/wrong size for header\n", header->biSize );
439 return -1;
442 /**********************************************************************
443 * CURSORICON_FindSharedIcon
445 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
447 HICON hIcon = 0;
448 ICONCACHE *ptr;
450 EnterCriticalSection( &IconCrst );
452 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
453 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
455 ptr->count++;
456 hIcon = ptr->hIcon;
457 break;
460 LeaveCriticalSection( &IconCrst );
462 return hIcon;
465 /*************************************************************************
466 * CURSORICON_FindCache
468 * Given a handle, find the corresponding cache element
470 * PARAMS
471 * Handle [I] handle to an Image
473 * RETURNS
474 * Success: The cache entry
475 * Failure: NULL
478 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
480 ICONCACHE *ptr;
481 ICONCACHE *pRet=NULL;
482 BOOL IsFound = FALSE;
484 EnterCriticalSection( &IconCrst );
486 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
488 if ( hIcon == ptr->hIcon )
490 IsFound = TRUE;
491 pRet = ptr;
495 LeaveCriticalSection( &IconCrst );
497 return pRet;
500 /**********************************************************************
501 * CURSORICON_AddSharedIcon
503 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
505 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
506 if ( !ptr ) return;
508 ptr->hModule = hModule;
509 ptr->hRsrc = hRsrc;
510 ptr->hIcon = hIcon;
511 ptr->hGroupRsrc = hGroupRsrc;
512 ptr->count = 1;
514 EnterCriticalSection( &IconCrst );
515 ptr->next = IconAnchor;
516 IconAnchor = ptr;
517 LeaveCriticalSection( &IconCrst );
520 /**********************************************************************
521 * CURSORICON_DelSharedIcon
523 static INT CURSORICON_DelSharedIcon( HICON hIcon )
525 INT count = -1;
526 ICONCACHE *ptr;
528 EnterCriticalSection( &IconCrst );
530 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
531 if ( ptr->hIcon == hIcon )
533 if ( ptr->count > 0 ) ptr->count--;
534 count = ptr->count;
535 break;
538 LeaveCriticalSection( &IconCrst );
540 return count;
543 /**********************************************************************
544 * get_icon_size
546 BOOL get_icon_size( HICON handle, SIZE *size )
548 struct cursoricon_object *info;
550 if (!(info = get_icon_ptr( handle ))) return FALSE;
551 size->cx = info->data.nWidth;
552 size->cy = info->data.nHeight;
553 release_icon_ptr( handle, info );
554 return TRUE;
558 * The following macro functions account for the irregularities of
559 * accessing cursor and icon resources in files and resource entries.
561 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
562 int *width, int *height, int *bits );
564 /**********************************************************************
565 * CURSORICON_FindBestIcon
567 * Find the icon closest to the requested size and bit depth.
569 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
570 int width, int height, int depth )
572 int i, cx, cy, bits, bestEntry = -1;
573 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
574 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
576 /* Find Best Fit */
577 iTotalDiff = 0xFFFFFFFF;
578 iColorDiff = 0xFFFFFFFF;
579 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
581 iTempXDiff = abs(width - cx);
582 iTempYDiff = abs(height - cy);
584 if(iTotalDiff > (iTempXDiff + iTempYDiff))
586 iXDiff = iTempXDiff;
587 iYDiff = iTempYDiff;
588 iTotalDiff = iXDiff + iYDiff;
592 /* Find Best Colors for Best Fit */
593 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
595 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
597 iTempColorDiff = abs(depth - bits);
598 if(iColorDiff > iTempColorDiff)
600 bestEntry = i;
601 iColorDiff = iTempColorDiff;
606 return bestEntry;
609 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
610 int *width, int *height, int *bits )
612 CURSORICONDIR *resdir = dir;
613 ICONRESDIR *icon;
615 if ( resdir->idCount <= n )
616 return FALSE;
617 icon = &resdir->idEntries[n].ResInfo.icon;
618 *width = icon->bWidth;
619 *height = icon->bHeight;
620 *bits = resdir->idEntries[n].wBitCount;
621 return TRUE;
624 /**********************************************************************
625 * CURSORICON_FindBestCursor
627 * Find the cursor closest to the requested size.
629 * FIXME: parameter 'color' ignored.
631 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
632 int width, int height, int depth )
634 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
636 /* Double height to account for AND and XOR masks */
638 height *= 2;
640 /* First find the largest one smaller than or equal to the requested size*/
642 maxwidth = maxheight = 0;
643 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
645 if ((cx <= width) && (cy <= height) &&
646 (cx > maxwidth) && (cy > maxheight))
648 bestEntry = i;
649 maxwidth = cx;
650 maxheight = cy;
653 if (bestEntry != -1) return bestEntry;
655 /* Now find the smallest one larger than the requested size */
657 maxwidth = maxheight = 255;
658 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
660 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
662 bestEntry = i;
663 maxwidth = cx;
664 maxheight = cy;
668 return bestEntry;
671 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
672 int *width, int *height, int *bits )
674 CURSORICONDIR *resdir = dir;
675 CURSORDIR *cursor;
677 if ( resdir->idCount <= n )
678 return FALSE;
679 cursor = &resdir->idEntries[n].ResInfo.cursor;
680 *width = cursor->wWidth;
681 *height = cursor->wHeight;
682 *bits = resdir->idEntries[n].wBitCount;
683 return TRUE;
686 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
687 int width, int height, int depth )
689 int n;
691 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
692 width, height, depth );
693 if ( n < 0 )
694 return NULL;
695 return &dir->idEntries[n];
698 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
699 int width, int height, int depth )
701 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
702 width, height, depth );
703 if ( n < 0 )
704 return NULL;
705 return &dir->idEntries[n];
708 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
709 int *width, int *height, int *bits )
711 CURSORICONFILEDIR *filedir = dir;
712 CURSORICONFILEDIRENTRY *entry;
713 BITMAPINFOHEADER *info;
715 if ( filedir->idCount <= n )
716 return FALSE;
717 entry = &filedir->idEntries[n];
718 /* FIXME: check against file size */
719 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
720 *width = entry->bWidth;
721 *height = entry->bHeight;
722 *bits = info->biBitCount;
723 return TRUE;
726 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
727 int width, int height, int depth )
729 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
730 width, height, depth );
731 if ( n < 0 )
732 return NULL;
733 return &dir->idEntries[n];
736 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
737 int width, int height, int depth )
739 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
740 width, height, depth );
741 if ( n < 0 )
742 return NULL;
743 return &dir->idEntries[n];
746 /***********************************************************************
747 * stretch_blt_icon
749 * A helper function that stretches a bitmap buffer into an HBITMAP.
751 * PARAMS
752 * hDest [I] The handle of the destination bitmap.
753 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
754 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
755 * pSrcBits [I] A pointer to the source bitmap buffer.
757 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
759 HBITMAP hOld;
760 BOOL res = FALSE;
761 HDC hdcMem = CreateCompatibleDC(screen_dc);
763 if (hdcMem)
765 hOld = SelectObject(hdcMem, hDest);
766 res = StretchDIBits(hdcMem,
767 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
768 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
769 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
770 SelectObject(hdcMem, hOld);
771 DeleteDC( hdcMem );
774 return res;
777 /***********************************************************************
778 * create_icon_bitmaps
780 * Create the color and mask bitmaps from the DIB info.
782 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
783 HBITMAP *color, HBITMAP *mask )
785 BOOL monochrome = is_dib_monochrome( bmi );
786 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
787 BITMAPINFO *info;
788 void *color_bits, *mask_bits;
789 BOOL ret = FALSE;
790 HDC hdc = 0;
792 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
793 return FALSE;
794 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
796 memcpy( info, bmi, size );
797 info->bmiHeader.biHeight /= 2;
799 color_bits = (char *)bmi + size;
800 mask_bits = (char *)color_bits +
801 get_dib_width_bytes( bmi->bmiHeader.biWidth,
802 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
804 if (monochrome)
806 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
807 *color = 0;
809 /* copy color data into second half of mask bitmap */
810 SelectObject( hdc, *mask );
811 StretchDIBits( hdc, 0, height, width, height,
812 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
813 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
815 else
817 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
818 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
819 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
821 DeleteObject( *mask );
822 goto done;
824 SelectObject( hdc, *color );
825 StretchDIBits( hdc, 0, 0, width, height,
826 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
827 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
829 /* convert info to monochrome to copy the mask */
830 info->bmiHeader.biBitCount = 1;
831 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
833 RGBQUAD *rgb = info->bmiColors;
835 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
836 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
837 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
838 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
840 else
842 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
844 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
845 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
849 SelectObject( hdc, *mask );
850 StretchDIBits( hdc, 0, 0, width, height,
851 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
852 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
853 ret = TRUE;
855 done:
856 DeleteDC( hdc );
857 HeapFree( GetProcessHeap(), 0, info );
858 return ret;
861 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
862 POINT16 hotspot, BOOL bIcon,
863 DWORD dwVersion,
864 INT width, INT height,
865 UINT cFlag )
867 HICON hObj;
868 int sizeAnd, sizeXor;
869 HBITMAP color = 0, mask = 0, hAndBits = 0, hXorBits = 0; /* error condition for later */
870 BITMAP bmpXor, bmpAnd;
871 BOOL do_stretch;
872 INT size;
873 BITMAPINFO *pSrcInfo, *pDestInfo;
875 if (dwVersion == 0x00020000)
877 FIXME_(cursor)("\t2.xx resources are not supported\n");
878 return 0;
881 /* Check bitmap header */
883 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
884 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
885 bmi->bmiHeader.biCompression != BI_RGB) )
887 WARN_(cursor)("\tinvalid resource bitmap header.\n");
888 return 0;
891 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
893 if (!width) width = bmi->bmiHeader.biWidth;
894 if (!height) height = bmi->bmiHeader.biHeight/2;
895 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
896 (bmi->bmiHeader.biWidth != width);
898 /* Scale the hotspot */
899 if (do_stretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
901 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
902 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
905 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
906 if (!screen_dc) return 0;
908 if (create_icon_bitmaps( bmi, width, height, &color, &mask ))
910 /* Make sure we have room for the monochrome bitmap later on.
911 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
912 * up to and including the biBitCount. In-memory icon resource
913 * format is as follows:
915 * BITMAPINFOHEADER icHeader // DIB header
916 * RGBQUAD icColors[] // Color table
917 * BYTE icXOR[] // DIB bits for XOR mask
918 * BYTE icAND[] // DIB bits for AND mask
921 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
922 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
923 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
924 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
925 if (pSrcInfo && pDestInfo)
927 memcpy( pSrcInfo, bmi, size );
928 pSrcInfo->bmiHeader.biHeight /= 2;
930 memcpy( pDestInfo, bmi, size );
931 pDestInfo->bmiHeader.biWidth = width;
932 pDestInfo->bmiHeader.biHeight = height;
933 pDestInfo->bmiHeader.biSizeImage = 0;
935 /* Create the XOR bitmap */
936 if(pSrcInfo->bmiHeader.biBitCount == 32)
938 void *pDIBBuffer = NULL;
939 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
941 if(hXorBits)
943 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
945 DeleteObject(hXorBits);
946 hXorBits = 0;
950 else
952 if (do_stretch)
954 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
955 if (hXorBits)
957 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
959 DeleteObject(hXorBits);
960 hXorBits = 0;
964 else
966 if (is_dib_monochrome(bmi))
968 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
969 SetDIBits(screen_dc, hXorBits, 0, height,
970 (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
972 else
973 hXorBits = CreateDIBitmap(screen_dc, &pSrcInfo->bmiHeader,
974 CBM_INIT, (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
978 if( hXorBits )
980 char* xbits = (char *)bmi + size +
981 get_dib_width_bytes( bmi->bmiHeader.biWidth,
982 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
984 pSrcInfo->bmiHeader.biBitCount = 1;
985 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
987 RGBQUAD *rgb = pSrcInfo->bmiColors;
989 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
990 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
991 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
992 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
994 else
996 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
998 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
999 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1002 /* Create the AND bitmap */
1003 if (do_stretch)
1005 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1007 if (!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
1009 DeleteObject(hAndBits);
1010 hAndBits = 0;
1013 else
1015 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1016 SetDIBits(screen_dc, hAndBits, 0, height,
1017 xbits, pSrcInfo, DIB_RGB_COLORS);
1020 if( !hAndBits )
1022 DeleteObject( hXorBits );
1023 hXorBits = 0;
1028 HeapFree( GetProcessHeap(), 0, pSrcInfo );
1029 HeapFree( GetProcessHeap(), 0, pDestInfo );
1032 if( !hXorBits || !hAndBits )
1034 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1035 DeleteObject( color );
1036 DeleteObject( mask );
1037 return 0;
1040 /* Now create the CURSORICONINFO structure */
1041 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1042 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1043 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1044 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1046 hObj = alloc_icon_handle( sizeXor + sizeAnd );
1047 if (hObj)
1049 struct cursoricon_object *info = get_icon_ptr( hObj );
1051 info->color = color;
1052 info->mask = mask;
1053 info->data.ptHotSpot.x = hotspot.x;
1054 info->data.ptHotSpot.y = hotspot.y;
1055 info->data.nWidth = bmpXor.bmWidth;
1056 info->data.nHeight = bmpXor.bmHeight;
1057 info->data.nWidthBytes = bmpXor.bmWidthBytes;
1058 info->data.bPlanes = bmpXor.bmPlanes;
1059 info->data.bBitsPerPixel = bmpXor.bmBitsPixel;
1061 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1063 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
1064 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
1065 release_icon_ptr( hObj, info );
1066 USER_Driver->pCreateCursorIcon( hObj, &info->data );
1068 else
1070 DeleteObject( color );
1071 DeleteObject( mask );
1074 DeleteObject( hAndBits );
1075 DeleteObject( hXorBits );
1076 return hObj;
1080 /**********************************************************************
1081 * .ANI cursor support
1083 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1084 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1085 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1087 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1088 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1089 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1090 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1091 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1092 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1094 #define ANI_FLAG_ICON 0x1
1095 #define ANI_FLAG_SEQUENCE 0x2
1097 typedef struct {
1098 DWORD header_size;
1099 DWORD num_frames;
1100 DWORD num_steps;
1101 DWORD width;
1102 DWORD height;
1103 DWORD bpp;
1104 DWORD num_planes;
1105 DWORD display_rate;
1106 DWORD flags;
1107 } ani_header;
1109 typedef struct {
1110 DWORD data_size;
1111 const unsigned char *data;
1112 } riff_chunk_t;
1114 static void dump_ani_header( const ani_header *header )
1116 TRACE(" header size: %d\n", header->header_size);
1117 TRACE(" frames: %d\n", header->num_frames);
1118 TRACE(" steps: %d\n", header->num_steps);
1119 TRACE(" width: %d\n", header->width);
1120 TRACE(" height: %d\n", header->height);
1121 TRACE(" bpp: %d\n", header->bpp);
1122 TRACE(" planes: %d\n", header->num_planes);
1123 TRACE(" display rate: %d\n", header->display_rate);
1124 TRACE(" flags: 0x%08x\n", header->flags);
1129 * RIFF:
1130 * DWORD "RIFF"
1131 * DWORD size
1132 * DWORD riff_id
1133 * BYTE[] data
1135 * LIST:
1136 * DWORD "LIST"
1137 * DWORD size
1138 * DWORD list_id
1139 * BYTE[] data
1141 * CHUNK:
1142 * DWORD chunk_id
1143 * DWORD size
1144 * BYTE[] data
1146 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1148 const unsigned char *ptr = parent_chunk->data;
1149 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1151 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1153 while (ptr < end)
1155 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1156 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1158 ptr += sizeof(DWORD);
1159 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1160 ptr += sizeof(DWORD);
1161 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1162 chunk->data = ptr;
1164 return;
1167 ptr += sizeof(DWORD);
1168 ptr += (*(const DWORD *)ptr + 1) & ~1;
1169 ptr += sizeof(DWORD);
1175 * .ANI layout:
1177 * RIFF:'ACON' RIFF chunk
1178 * |- CHUNK:'anih' Header
1179 * |- CHUNK:'seq ' Sequence information (optional)
1180 * \- LIST:'fram' Frame list
1181 * |- CHUNK:icon Cursor frames
1182 * |- CHUNK:icon
1183 * |- ...
1184 * \- CHUNK:icon
1186 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1187 INT width, INT height, INT depth )
1189 HCURSOR cursor;
1190 ani_header header = {0};
1191 LPBYTE frame_bits = 0;
1192 POINT16 hotspot;
1193 CURSORICONFILEDIRENTRY *entry;
1195 riff_chunk_t root_chunk = { bits_size, bits };
1196 riff_chunk_t ACON_chunk = {0};
1197 riff_chunk_t anih_chunk = {0};
1198 riff_chunk_t fram_chunk = {0};
1199 const unsigned char *icon_data;
1201 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1203 if (!bits) return 0;
1205 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1206 if (!ACON_chunk.data)
1208 ERR("Failed to get root chunk.\n");
1209 return 0;
1212 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1213 if (!anih_chunk.data)
1215 ERR("Failed to get 'anih' chunk.\n");
1216 return 0;
1218 memcpy( &header, anih_chunk.data, sizeof(header) );
1219 dump_ani_header( &header );
1221 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1222 if (!fram_chunk.data)
1224 ERR("Failed to get icon list.\n");
1225 return 0;
1228 /* FIXME: For now, just load the first frame. Before we can load all the
1229 * frames, we need to write the needed code in wineserver, etc. to handle
1230 * cursors. Once this code is written, we can extend it to support .ani
1231 * cursors and then update user32 and winex11.drv to load all frames.
1233 * Hopefully this will at least make some games (C&C3, etc.) more playable
1234 * in the meantime.
1236 FIXME("Loading all frames for .ani cursors not implemented.\n");
1237 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1239 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1240 width, height, depth );
1242 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1243 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1245 if (!header.width || !header.height)
1247 header.width = entry->bWidth;
1248 header.height = entry->bHeight;
1251 hotspot.x = entry->xHotspot;
1252 hotspot.y = entry->yHotspot;
1254 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1255 FALSE, 0x00030000, header.width, header.height, 0 );
1257 HeapFree( GetProcessHeap(), 0, frame_bits );
1259 return cursor;
1263 /**********************************************************************
1264 * CreateIconFromResourceEx (USER32.@)
1266 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1267 * with cbSize parameter as well.
1269 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1270 BOOL bIcon, DWORD dwVersion,
1271 INT width, INT height,
1272 UINT cFlag )
1274 POINT16 hotspot;
1275 BITMAPINFO *bmi;
1277 hotspot.x = ICON_HOTSPOT;
1278 hotspot.y = ICON_HOTSPOT;
1280 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1281 bits, cbSize, dwVersion, width, height,
1282 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1284 if (bIcon)
1285 bmi = (BITMAPINFO *)bits;
1286 else /* get the hotspot */
1288 POINT16 *pt = (POINT16 *)bits;
1289 hotspot = *pt;
1290 bmi = (BITMAPINFO *)(pt + 1);
1293 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1294 width, height, cFlag );
1298 /**********************************************************************
1299 * CreateIconFromResource (USER32.@)
1301 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1302 BOOL bIcon, DWORD dwVersion)
1304 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1308 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1309 INT width, INT height, INT depth,
1310 BOOL fCursor, UINT loadflags)
1312 CURSORICONFILEDIRENTRY *entry;
1313 CURSORICONFILEDIR *dir;
1314 DWORD filesize = 0;
1315 HICON hIcon = 0;
1316 LPBYTE bits;
1317 POINT16 hotspot;
1319 TRACE("loading %s\n", debugstr_w( filename ));
1321 bits = map_fileW( filename, &filesize );
1322 if (!bits)
1323 return hIcon;
1325 /* Check for .ani. */
1326 if (memcmp( bits, "RIFF", 4 ) == 0)
1328 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1329 depth );
1330 goto end;
1333 dir = (CURSORICONFILEDIR*) bits;
1334 if ( filesize < sizeof(*dir) )
1335 goto end;
1337 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1338 goto end;
1340 if ( fCursor )
1341 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1342 else
1343 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1345 if ( !entry )
1346 goto end;
1348 /* check that we don't run off the end of the file */
1349 if ( entry->dwDIBOffset > filesize )
1350 goto end;
1351 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1352 goto end;
1354 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1355 if ( fCursor )
1357 hotspot.x = entry->xHotspot;
1358 hotspot.y = entry->yHotspot;
1360 else
1362 hotspot.x = ICON_HOTSPOT;
1363 hotspot.y = ICON_HOTSPOT;
1365 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1366 hotspot, !fCursor, 0x00030000,
1367 width, height, loadflags );
1368 end:
1369 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1370 UnmapViewOfFile( bits );
1371 return hIcon;
1374 /**********************************************************************
1375 * CURSORICON_Load
1377 * Load a cursor or icon from resource or file.
1379 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1380 INT width, INT height, INT depth,
1381 BOOL fCursor, UINT loadflags)
1383 HANDLE handle = 0;
1384 HICON hIcon = 0;
1385 HRSRC hRsrc, hGroupRsrc;
1386 CURSORICONDIR *dir;
1387 CURSORICONDIRENTRY *dirEntry;
1388 LPBYTE bits;
1389 WORD wResId;
1390 DWORD dwBytesInRes;
1392 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1393 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1395 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1396 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1398 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1400 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1401 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1403 /* Get directory resource ID */
1405 if (!(hRsrc = FindResourceW( hInstance, name,
1406 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1407 return 0;
1408 hGroupRsrc = hRsrc;
1410 /* Find the best entry in the directory */
1412 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1413 if (!(dir = LockResource( handle ))) return 0;
1414 if (fCursor)
1415 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1416 else
1417 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1418 if (!dirEntry) return 0;
1419 wResId = dirEntry->wResId;
1420 dwBytesInRes = dirEntry->dwBytesInRes;
1421 FreeResource( handle );
1423 /* Load the resource */
1425 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1426 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1428 /* If shared icon, check whether it was already loaded */
1429 if ( (loadflags & LR_SHARED)
1430 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1431 return hIcon;
1433 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1434 bits = LockResource( handle );
1435 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1436 !fCursor, 0x00030000, width, height, loadflags);
1437 FreeResource( handle );
1439 /* If shared icon, add to icon cache */
1441 if ( hIcon && (loadflags & LR_SHARED) )
1442 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1444 return hIcon;
1448 /*************************************************************************
1449 * CURSORICON_ExtCopy
1451 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1453 * PARAMS
1454 * Handle [I] handle to an Image
1455 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1456 * iDesiredCX [I] The Desired width of the Image
1457 * iDesiredCY [I] The desired height of the Image
1458 * nFlags [I] The flags from CopyImage
1460 * RETURNS
1461 * Success: The new handle of the Image
1463 * NOTES
1464 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1465 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1466 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1471 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1472 INT iDesiredCX, INT iDesiredCY,
1473 UINT nFlags)
1475 HICON hNew=0;
1477 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1478 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1480 if(hIcon == 0)
1482 return 0;
1485 /* Best Fit or Monochrome */
1486 if( (nFlags & LR_COPYFROMRESOURCE
1487 && (iDesiredCX > 0 || iDesiredCY > 0))
1488 || nFlags & LR_MONOCHROME)
1490 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1492 /* Not Found in Cache, then do a straight copy
1494 if(pIconCache == NULL)
1496 hNew = CopyIcon( hIcon );
1497 if(nFlags & LR_COPYFROMRESOURCE)
1499 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1502 else
1504 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1505 LPBYTE pBits;
1506 HANDLE hMem;
1507 HRSRC hRsrc;
1508 DWORD dwBytesInRes;
1509 WORD wResId;
1510 CURSORICONDIR *pDir;
1511 CURSORICONDIRENTRY *pDirEntry;
1512 BOOL bIsIcon = (nType == IMAGE_ICON);
1514 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1516 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1517 || (iDesiredCX == 0 && iDesiredCY == 0))
1519 iDesiredCY = GetSystemMetrics(bIsIcon ?
1520 SM_CYICON : SM_CYCURSOR);
1521 iDesiredCX = GetSystemMetrics(bIsIcon ?
1522 SM_CXICON : SM_CXCURSOR);
1525 /* Retrieve the CURSORICONDIRENTRY
1527 if (!(hMem = LoadResource( pIconCache->hModule ,
1528 pIconCache->hGroupRsrc)))
1530 return 0;
1532 if (!(pDir = LockResource( hMem )))
1534 return 0;
1537 /* Find Best Fit
1539 if(bIsIcon)
1541 pDirEntry = CURSORICON_FindBestIconRes(
1542 pDir, iDesiredCX, iDesiredCY, 256 );
1544 else
1546 pDirEntry = CURSORICON_FindBestCursorRes(
1547 pDir, iDesiredCX, iDesiredCY, 1);
1550 wResId = pDirEntry->wResId;
1551 dwBytesInRes = pDirEntry->dwBytesInRes;
1552 FreeResource(hMem);
1554 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1555 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1556 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1558 /* Get the Best Fit
1560 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1561 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1563 return 0;
1565 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1567 return 0;
1570 pBits = LockResource( hMem );
1572 if(nFlags & LR_DEFAULTSIZE)
1574 iTargetCY = GetSystemMetrics(SM_CYICON);
1575 iTargetCX = GetSystemMetrics(SM_CXICON);
1578 /* Create a New Icon with the proper dimension
1580 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1581 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1582 FreeResource(hMem);
1585 else hNew = CopyIcon( hIcon );
1586 return hNew;
1590 /***********************************************************************
1591 * CreateCursor (USER32.@)
1593 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1594 INT xHotSpot, INT yHotSpot,
1595 INT nWidth, INT nHeight,
1596 LPCVOID lpANDbits, LPCVOID lpXORbits )
1598 ICONINFO info;
1599 HCURSOR hCursor;
1601 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1602 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1604 info.fIcon = FALSE;
1605 info.xHotspot = xHotSpot;
1606 info.yHotspot = yHotSpot;
1607 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1608 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1609 hCursor = CreateIconIndirect( &info );
1610 DeleteObject( info.hbmMask );
1611 DeleteObject( info.hbmColor );
1612 return hCursor;
1616 /***********************************************************************
1617 * CreateIcon (USER32.@)
1619 * Creates an icon based on the specified bitmaps. The bitmaps must be
1620 * provided in a device dependent format and will be resized to
1621 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1622 * depth. The provided bitmaps must be top-down bitmaps.
1623 * Although Windows does not support 15bpp(*) this API must support it
1624 * for Winelib applications.
1626 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1627 * format!
1629 * RETURNS
1630 * Success: handle to an icon
1631 * Failure: NULL
1633 * FIXME: Do we need to resize the bitmaps?
1635 HICON WINAPI CreateIcon(
1636 HINSTANCE hInstance, /* [in] the application's hInstance */
1637 INT nWidth, /* [in] the width of the provided bitmaps */
1638 INT nHeight, /* [in] the height of the provided bitmaps */
1639 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1640 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1641 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1642 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1644 ICONINFO iinfo;
1645 HICON hIcon;
1647 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1648 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1650 iinfo.fIcon = TRUE;
1651 iinfo.xHotspot = ICON_HOTSPOT;
1652 iinfo.yHotspot = ICON_HOTSPOT;
1653 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1654 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1656 hIcon = CreateIconIndirect( &iinfo );
1658 DeleteObject( iinfo.hbmMask );
1659 DeleteObject( iinfo.hbmColor );
1661 return hIcon;
1665 /***********************************************************************
1666 * CopyIcon (USER32.@)
1668 HICON WINAPI CopyIcon( HICON hIcon )
1670 struct cursoricon_object *ptrOld, *ptrNew;
1671 int size;
1672 HICON hNew;
1674 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1675 size = ptrOld->data.nHeight * get_bitmap_width_bytes( ptrOld->data.nWidth, 1 ); /* and bitmap */
1676 size += ptrOld->data.nHeight * ptrOld->data.nWidthBytes; /* xor bitmap */
1677 hNew = alloc_icon_handle( size );
1678 ptrNew = get_icon_ptr( hNew );
1679 memcpy( &ptrNew->data, &ptrOld->data, sizeof(ptrNew->data) + size );
1680 ptrNew->color = copy_bitmap( ptrOld->color );
1681 ptrNew->mask = copy_bitmap( ptrOld->mask );
1682 release_icon_ptr( hIcon, ptrOld );
1683 release_icon_ptr( hNew, ptrNew );
1684 USER_Driver->pCreateCursorIcon( hNew, &ptrNew->data );
1685 return hNew;
1689 /***********************************************************************
1690 * DestroyIcon (USER32.@)
1692 BOOL WINAPI DestroyIcon( HICON hIcon )
1694 TRACE_(icon)("%p\n", hIcon );
1696 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1697 free_icon_handle( hIcon );
1698 return TRUE;
1702 /***********************************************************************
1703 * DestroyCursor (USER32.@)
1705 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1707 if (GetCursor() == hCursor)
1709 WARN_(cursor)("Destroying active cursor!\n" );
1710 return FALSE;
1712 return DestroyIcon( hCursor );
1715 /***********************************************************************
1716 * bitmap_has_alpha_channel
1718 * Analyses bits bitmap to determine if alpha data is present.
1720 * PARAMS
1721 * bpp [I] The bits-per-pixel of the bitmap
1722 * bitmapBits [I] A pointer to the bitmap data
1723 * bitmapLength [I] The length of the bitmap in bytes
1725 * RETURNS
1726 * TRUE if an alpha channel is discovered, FALSE
1728 * NOTE
1729 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1730 * least one pixel has a non-zero alpha, then the bitmap is a
1731 * treated as having an alpha channel transparentcy. Otherwise,
1732 * it's treated as being completely opaque.
1735 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1736 unsigned int bitmapLength )
1738 /* Detect an alpha channel by looking for non-zero alpha pixels */
1739 if(bpp == 32)
1741 unsigned int offset;
1742 for(offset = 3; offset < bitmapLength; offset += 4)
1744 if(bitmapBits[offset] != 0)
1746 return TRUE;
1750 return FALSE;
1753 /***********************************************************************
1754 * premultiply_alpha_channel
1756 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1757 * channel. This is a necessary step that must be carried out on
1758 * the image before it is passed to GdiAlphaBlend
1760 * PARAMS
1761 * destBitmap [I] The destination bitmap buffer
1762 * srcBitmap [I] The source bitmap buffer
1763 * bitmapLength [I] The length of the bitmap in bytes
1766 static void premultiply_alpha_channel( unsigned char *destBitmap,
1767 unsigned char *srcBitmap,
1768 unsigned int bitmapLength )
1770 unsigned char *destPixel = destBitmap;
1771 unsigned char *srcPixel = srcBitmap;
1773 while(destPixel < destBitmap + bitmapLength)
1775 unsigned char alpha = srcPixel[3];
1776 *(destPixel++) = *(srcPixel++) * alpha / 255;
1777 *(destPixel++) = *(srcPixel++) * alpha / 255;
1778 *(destPixel++) = *(srcPixel++) * alpha / 255;
1779 *(destPixel++) = *(srcPixel++);
1783 /***********************************************************************
1784 * DrawIcon (USER32.@)
1786 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1788 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1791 /***********************************************************************
1792 * SetCursor (USER32.@)
1794 * Set the cursor shape.
1796 * RETURNS
1797 * A handle to the previous cursor shape.
1799 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1801 HCURSOR hOldCursor;
1802 int show_count;
1803 BOOL ret;
1805 TRACE("%p\n", hCursor);
1807 SERVER_START_REQ( set_cursor )
1809 req->flags = SET_CURSOR_HANDLE;
1810 req->handle = wine_server_user_handle( hCursor );
1811 if ((ret = !wine_server_call_err( req )))
1813 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1814 show_count = reply->prev_count;
1817 SERVER_END_REQ;
1819 if (!ret) return 0;
1821 /* Change the cursor shape only if it is visible */
1822 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1823 return hOldCursor;
1826 /***********************************************************************
1827 * ShowCursor (USER32.@)
1829 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1831 HCURSOR cursor;
1832 int increment = bShow ? 1 : -1;
1833 int count;
1835 SERVER_START_REQ( set_cursor )
1837 req->flags = SET_CURSOR_COUNT;
1838 req->show_count = increment;
1839 wine_server_call( req );
1840 cursor = wine_server_ptr_handle( reply->prev_handle );
1841 count = reply->prev_count + increment;
1843 SERVER_END_REQ;
1845 TRACE("%d, count=%d\n", bShow, count );
1847 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1848 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1850 return count;
1853 /***********************************************************************
1854 * GetCursor (USER32.@)
1856 HCURSOR WINAPI GetCursor(void)
1858 HCURSOR ret;
1860 SERVER_START_REQ( set_cursor )
1862 req->flags = 0;
1863 wine_server_call( req );
1864 ret = wine_server_ptr_handle( reply->prev_handle );
1866 SERVER_END_REQ;
1867 return ret;
1871 /***********************************************************************
1872 * ClipCursor (USER32.@)
1874 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1876 RECT virt;
1878 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1879 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1880 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1881 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1883 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1884 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1886 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1887 CURSOR_ClipRect = virt;
1889 USER_Driver->pClipCursor( rect );
1890 return TRUE;
1894 /***********************************************************************
1895 * GetClipCursor (USER32.@)
1897 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1899 /* If this is first time - initialize the rect */
1900 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1902 return CopyRect( rect, &CURSOR_ClipRect );
1906 /***********************************************************************
1907 * SetSystemCursor (USER32.@)
1909 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1911 FIXME("(%p,%08x),stub!\n", hcur, id);
1912 return TRUE;
1916 /**********************************************************************
1917 * LookupIconIdFromDirectoryEx (USER32.@)
1919 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1920 INT width, INT height, UINT cFlag )
1922 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1923 UINT retVal = 0;
1924 if( dir && !dir->idReserved && (dir->idType & 3) )
1926 CURSORICONDIRENTRY* entry;
1928 const HDC hdc = GetDC(0);
1929 const int depth = (cFlag & LR_MONOCHROME) ?
1930 1 : GetDeviceCaps(hdc, BITSPIXEL);
1931 ReleaseDC(0, hdc);
1933 if( bIcon )
1934 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1935 else
1936 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1938 if( entry ) retVal = entry->wResId;
1940 else WARN_(cursor)("invalid resource directory\n");
1941 return retVal;
1944 /**********************************************************************
1945 * LookupIconIdFromDirectory (USER32.@)
1947 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1949 return LookupIconIdFromDirectoryEx( dir, bIcon,
1950 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1951 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1954 /***********************************************************************
1955 * LoadCursorW (USER32.@)
1957 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1959 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1961 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1962 LR_SHARED | LR_DEFAULTSIZE );
1965 /***********************************************************************
1966 * LoadCursorA (USER32.@)
1968 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1970 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1972 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1973 LR_SHARED | LR_DEFAULTSIZE );
1976 /***********************************************************************
1977 * LoadCursorFromFileW (USER32.@)
1979 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1981 TRACE("%s\n", debugstr_w(name));
1983 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1984 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1987 /***********************************************************************
1988 * LoadCursorFromFileA (USER32.@)
1990 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1992 TRACE("%s\n", debugstr_a(name));
1994 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1995 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1998 /***********************************************************************
1999 * LoadIconW (USER32.@)
2001 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2003 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2005 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2006 LR_SHARED | LR_DEFAULTSIZE );
2009 /***********************************************************************
2010 * LoadIconA (USER32.@)
2012 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2014 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2016 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2017 LR_SHARED | LR_DEFAULTSIZE );
2020 /**********************************************************************
2021 * GetIconInfo (USER32.@)
2023 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2025 struct cursoricon_object *ciconinfo;
2026 INT height;
2028 if (!(ciconinfo = get_icon_ptr( hIcon ))) return FALSE;
2030 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2031 ciconinfo->data.nWidth, ciconinfo->data.nHeight, ciconinfo->data.bBitsPerPixel);
2033 if ( (ciconinfo->data.ptHotSpot.x == ICON_HOTSPOT) &&
2034 (ciconinfo->data.ptHotSpot.y == ICON_HOTSPOT) )
2036 iconinfo->fIcon = TRUE;
2037 iconinfo->xHotspot = ciconinfo->data.nWidth / 2;
2038 iconinfo->yHotspot = ciconinfo->data.nHeight / 2;
2040 else
2042 iconinfo->fIcon = FALSE;
2043 iconinfo->xHotspot = ciconinfo->data.ptHotSpot.x;
2044 iconinfo->yHotspot = ciconinfo->data.ptHotSpot.y;
2047 height = ciconinfo->data.nHeight;
2049 if (ciconinfo->data.bBitsPerPixel > 1)
2051 iconinfo->hbmColor = CreateBitmap( ciconinfo->data.nWidth, ciconinfo->data.nHeight,
2052 ciconinfo->data.bPlanes, ciconinfo->data.bBitsPerPixel,
2053 (char *)(ciconinfo + 1)
2054 + ciconinfo->data.nHeight *
2055 get_bitmap_width_bytes (ciconinfo->data.nWidth,1) );
2057 else
2059 iconinfo->hbmColor = 0;
2060 height *= 2;
2063 iconinfo->hbmMask = CreateBitmap ( ciconinfo->data.nWidth, height,
2064 1, 1, ciconinfo + 1);
2065 release_icon_ptr( hIcon, ciconinfo );
2067 return TRUE;
2070 /**********************************************************************
2071 * CreateIconIndirect (USER32.@)
2073 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2075 DIBSECTION bmpXor;
2076 BITMAP bmpAnd;
2077 HICON hObj;
2078 HBITMAP color = 0, mask;
2079 int xor_objsize = 0, sizeXor = 0, sizeAnd, width, height, planes, bpp;
2080 HDC src, dst;
2082 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2083 iconinfo->hbmColor, iconinfo->hbmMask,
2084 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2086 if (!iconinfo->hbmMask) return 0;
2088 planes = GetDeviceCaps( screen_dc, PLANES );
2089 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2091 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2092 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2093 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2094 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2096 if (iconinfo->hbmColor)
2098 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2099 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2100 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2101 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2102 /* we can use either depth 1 or screen depth for xor bitmap */
2103 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2104 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2106 width = bmpXor.dsBm.bmWidth;
2107 height = bmpXor.dsBm.bmHeight;
2108 if (bmpXor.dsBm.bmPlanes * bmpXor.dsBm.bmBitsPixel != 1)
2110 color = CreateCompatibleBitmap( screen_dc, width, height );
2111 mask = CreateBitmap( width, height, 1, 1, NULL );
2113 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2115 else
2117 width = bmpAnd.bmWidth;
2118 height = bmpAnd.bmHeight;
2119 mask = CreateBitmap( width, height, 1, 1, NULL );
2122 sizeAnd = height * get_bitmap_width_bytes(width, 1);
2124 src = CreateCompatibleDC( 0 );
2125 dst = CreateCompatibleDC( 0 );
2127 SelectObject( src, iconinfo->hbmMask );
2128 SelectObject( dst, mask );
2129 StretchBlt( dst, 0, 0, width, height, src, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, SRCCOPY );
2131 if (color)
2133 SelectObject( src, iconinfo->hbmColor );
2134 SelectObject( dst, color );
2135 BitBlt( dst, 0, 0, width, height, src, 0, 0, SRCCOPY );
2137 else if (iconinfo->hbmColor)
2139 SelectObject( src, iconinfo->hbmColor );
2140 BitBlt( dst, 0, height, width, height, src, 0, 0, SRCCOPY );
2142 DeleteDC( src );
2143 DeleteDC( dst );
2145 hObj = alloc_icon_handle( sizeXor + sizeAnd );
2146 if (hObj)
2148 struct cursoricon_object *info = get_icon_ptr( hObj );
2150 info->color = color;
2151 info->mask = mask;
2153 /* If we are creating an icon, the hotspot is unused */
2154 if (iconinfo->fIcon)
2156 info->data.ptHotSpot.x = ICON_HOTSPOT;
2157 info->data.ptHotSpot.y = ICON_HOTSPOT;
2159 else
2161 info->data.ptHotSpot.x = iconinfo->xHotspot;
2162 info->data.ptHotSpot.y = iconinfo->yHotspot;
2165 if (iconinfo->hbmColor)
2167 info->data.nWidth = bmpXor.dsBm.bmWidth;
2168 info->data.nHeight = bmpXor.dsBm.bmHeight;
2169 info->data.nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2170 info->data.bPlanes = planes;
2171 info->data.bBitsPerPixel = bpp;
2173 else
2175 info->data.nWidth = bmpAnd.bmWidth;
2176 info->data.nHeight = bmpAnd.bmHeight / 2;
2177 info->data.nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2178 info->data.bPlanes = 1;
2179 info->data.bBitsPerPixel = 1;
2182 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2184 GetBitmapBits( mask, sizeAnd, info + 1 );
2186 if (color)
2188 char *dst_bits = (char*)(info + 1) + sizeAnd;
2190 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2191 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2192 else
2194 BITMAPINFO bminfo;
2195 int dib_width = get_dib_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2196 int bitmap_width = get_bitmap_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2198 bminfo.bmiHeader.biSize = sizeof(bminfo);
2199 bminfo.bmiHeader.biWidth = info->data.nWidth;
2200 bminfo.bmiHeader.biHeight = info->data.nHeight;
2201 bminfo.bmiHeader.biPlanes = info->data.bPlanes;
2202 bminfo.bmiHeader.biBitCount = info->data.bBitsPerPixel;
2203 bminfo.bmiHeader.biCompression = BI_RGB;
2204 bminfo.bmiHeader.biSizeImage = info->data.nHeight * dib_width;
2205 bminfo.bmiHeader.biXPelsPerMeter = 0;
2206 bminfo.bmiHeader.biYPelsPerMeter = 0;
2207 bminfo.bmiHeader.biClrUsed = 0;
2208 bminfo.bmiHeader.biClrImportant = 0;
2210 /* swap lines for dib sections */
2211 if (xor_objsize == sizeof(DIBSECTION))
2212 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2214 if (dib_width != bitmap_width) /* need to fixup alignment */
2216 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2218 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2219 src_bits, &bminfo, DIB_RGB_COLORS ))
2221 int y;
2222 for (y = 0; y < info->data.nHeight; y++)
2223 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2225 HeapFree( GetProcessHeap(), 0, src_bits );
2227 else
2228 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2229 dst_bits, &bminfo, DIB_RGB_COLORS );
2232 release_icon_ptr( hObj, info );
2233 USER_Driver->pCreateCursorIcon( hObj, &info->data );
2235 return hObj;
2238 /******************************************************************************
2239 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2241 * NOTES
2242 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2244 * PARAMS
2245 * hdc [I] Handle to device context
2246 * x0 [I] X coordinate of upper left corner
2247 * y0 [I] Y coordinate of upper left corner
2248 * hIcon [I] Handle to icon to draw
2249 * cxWidth [I] Width of icon
2250 * cyWidth [I] Height of icon
2251 * istep [I] Index of frame in animated cursor
2252 * hbr [I] Handle to background brush
2253 * flags [I] Icon-drawing flags
2255 * RETURNS
2256 * Success: TRUE
2257 * Failure: FALSE
2259 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2260 INT cxWidth, INT cyWidth, UINT istep,
2261 HBRUSH hbr, UINT flags )
2263 struct cursoricon_object *ptr;
2264 HDC hDC_off = 0, hMemDC;
2265 BOOL result = FALSE, DoOffscreen;
2266 HBITMAP hB_off = 0, hOld = 0;
2267 unsigned char *xorBitmapBits;
2268 unsigned int xorLength;
2269 BOOL has_alpha = FALSE;
2271 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2272 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2274 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2275 if (!(hMemDC = CreateCompatibleDC( hdc )))
2277 release_icon_ptr( hIcon, ptr );
2278 return FALSE;
2281 if (istep)
2282 FIXME_(icon)("Ignoring istep=%d\n", istep);
2283 if (flags & DI_NOMIRROR)
2284 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2286 xorLength = ptr->data.nHeight * get_bitmap_width_bytes(
2287 ptr->data.nWidth, ptr->data.bBitsPerPixel);
2288 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->data.nHeight *
2289 get_bitmap_width_bytes(ptr->data.nWidth, 1);
2291 if (flags & DI_IMAGE)
2292 has_alpha = bitmap_has_alpha_channel(
2293 ptr->data.bBitsPerPixel, xorBitmapBits, xorLength);
2295 /* Calculate the size of the destination image. */
2296 if (cxWidth == 0)
2298 if (flags & DI_DEFAULTSIZE)
2299 cxWidth = GetSystemMetrics (SM_CXICON);
2300 else
2301 cxWidth = ptr->data.nWidth;
2303 if (cyWidth == 0)
2305 if (flags & DI_DEFAULTSIZE)
2306 cyWidth = GetSystemMetrics (SM_CYICON);
2307 else
2308 cyWidth = ptr->data.nHeight;
2311 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2313 if (DoOffscreen) {
2314 RECT r;
2316 r.left = 0;
2317 r.top = 0;
2318 r.right = cxWidth;
2319 r.bottom = cxWidth;
2321 hDC_off = CreateCompatibleDC(hdc);
2322 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2323 if (hDC_off && hB_off) {
2324 hOld = SelectObject(hDC_off, hB_off);
2325 FillRect(hDC_off, &r, hbr);
2329 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2331 HBITMAP hBitTemp;
2332 HBITMAP hXorBits = NULL, hAndBits = NULL;
2333 COLORREF oldFg, oldBg;
2334 INT nStretchMode;
2336 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2338 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2339 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2341 if ((flags & DI_MASK) && !has_alpha)
2343 hAndBits = CreateBitmap ( ptr->data.nWidth, ptr->data.nHeight, 1, 1, ptr + 1 );
2344 if (hAndBits)
2346 hBitTemp = SelectObject( hMemDC, hAndBits );
2347 if (DoOffscreen)
2348 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2349 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND);
2350 else
2351 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2352 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND);
2353 SelectObject( hMemDC, hBitTemp );
2357 if (flags & DI_IMAGE)
2359 if (ptr->data.bPlanes * ptr->data.bBitsPerPixel == 1)
2361 hXorBits = CreateBitmap( ptr->data.nWidth, ptr->data.nHeight, 1, 1, xorBitmapBits );
2363 else
2365 unsigned char *dibBits;
2366 BITMAPINFO *bmi = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2367 FIELD_OFFSET( BITMAPINFO, bmiColors[256] ));
2368 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2369 bmi->bmiHeader.biWidth = ptr->data.nWidth;
2370 bmi->bmiHeader.biHeight = -ptr->data.nHeight;
2371 bmi->bmiHeader.biPlanes = ptr->data.bPlanes;
2372 bmi->bmiHeader.biBitCount = ptr->data.bBitsPerPixel;
2373 bmi->bmiHeader.biCompression = BI_RGB;
2374 /* FIXME: color table */
2376 hXorBits = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, (void*)&dibBits, NULL, 0);
2377 if (hXorBits)
2379 if(has_alpha)
2380 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2381 else
2382 memcpy(dibBits, xorBitmapBits, xorLength);
2386 if (hXorBits)
2388 if(has_alpha)
2390 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2392 /* Do the alpha blending render */
2393 hBitTemp = SelectObject( hMemDC, hXorBits );
2395 if (DoOffscreen)
2396 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2397 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend);
2398 else
2399 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2400 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend);
2402 SelectObject( hMemDC, hBitTemp );
2404 else
2406 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2407 hBitTemp = SelectObject( hMemDC, hXorBits );
2408 if (DoOffscreen)
2409 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2410 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop);
2411 else
2412 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2413 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop);
2414 SelectObject( hMemDC, hBitTemp );
2417 DeleteObject( hXorBits );
2421 result = TRUE;
2423 SetTextColor( hdc, oldFg );
2424 SetBkColor( hdc, oldBg );
2426 if (hAndBits) DeleteObject( hAndBits );
2427 SetStretchBltMode (hdc, nStretchMode);
2428 if (DoOffscreen) {
2429 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2430 SelectObject(hDC_off, hOld);
2433 if (hMemDC) DeleteDC( hMemDC );
2434 if (hDC_off) DeleteDC(hDC_off);
2435 if (hB_off) DeleteObject(hB_off);
2436 release_icon_ptr( hIcon, ptr );
2437 return result;
2440 /***********************************************************************
2441 * DIB_FixColorsToLoadflags
2443 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2444 * are in loadflags
2446 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2448 int colors;
2449 COLORREF c_W, c_S, c_F, c_L, c_C;
2450 int incr,i;
2451 RGBQUAD *ptr;
2452 int bitmap_type;
2453 LONG width;
2454 LONG height;
2455 WORD bpp;
2456 DWORD compr;
2458 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2460 WARN_(resource)("Invalid bitmap\n");
2461 return;
2464 if (bpp > 8) return;
2466 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2468 incr = 3;
2469 colors = 1 << bpp;
2471 else
2473 incr = 4;
2474 colors = bmi->bmiHeader.biClrUsed;
2475 if (colors > 256) colors = 256;
2476 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2479 c_W = GetSysColor(COLOR_WINDOW);
2480 c_S = GetSysColor(COLOR_3DSHADOW);
2481 c_F = GetSysColor(COLOR_3DFACE);
2482 c_L = GetSysColor(COLOR_3DLIGHT);
2484 if (loadflags & LR_LOADTRANSPARENT) {
2485 switch (bpp) {
2486 case 1: pix = pix >> 7; break;
2487 case 4: pix = pix >> 4; break;
2488 case 8: break;
2489 default:
2490 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2491 return;
2493 if (pix >= colors) {
2494 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2495 return;
2497 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2498 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2499 ptr->rgbBlue = GetBValue(c_W);
2500 ptr->rgbGreen = GetGValue(c_W);
2501 ptr->rgbRed = GetRValue(c_W);
2503 if (loadflags & LR_LOADMAP3DCOLORS)
2504 for (i=0; i<colors; i++) {
2505 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2506 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2507 if (c_C == RGB(128, 128, 128)) {
2508 ptr->rgbRed = GetRValue(c_S);
2509 ptr->rgbGreen = GetGValue(c_S);
2510 ptr->rgbBlue = GetBValue(c_S);
2511 } else if (c_C == RGB(192, 192, 192)) {
2512 ptr->rgbRed = GetRValue(c_F);
2513 ptr->rgbGreen = GetGValue(c_F);
2514 ptr->rgbBlue = GetBValue(c_F);
2515 } else if (c_C == RGB(223, 223, 223)) {
2516 ptr->rgbRed = GetRValue(c_L);
2517 ptr->rgbGreen = GetGValue(c_L);
2518 ptr->rgbBlue = GetBValue(c_L);
2524 /**********************************************************************
2525 * BITMAP_Load
2527 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2528 INT desiredx, INT desiredy, UINT loadflags )
2530 HBITMAP hbitmap = 0, orig_bm;
2531 HRSRC hRsrc;
2532 HGLOBAL handle;
2533 char *ptr = NULL;
2534 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2535 int size;
2536 BYTE pix;
2537 char *bits;
2538 LONG width, height, new_width, new_height;
2539 WORD bpp_dummy;
2540 DWORD compr_dummy, offbits = 0;
2541 INT bm_type;
2542 HDC screen_mem_dc = NULL;
2544 if (!(loadflags & LR_LOADFROMFILE))
2546 if (!instance)
2548 /* OEM bitmap: try to load the resource from user32.dll */
2549 instance = user32_module;
2552 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2553 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2555 if ((info = LockResource( handle )) == NULL) return 0;
2557 else
2559 BITMAPFILEHEADER * bmfh;
2561 if (!(ptr = map_fileW( name, NULL ))) return 0;
2562 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2563 bmfh = (BITMAPFILEHEADER *)ptr;
2564 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2566 WARN("Invalid/unsupported bitmap format!\n");
2567 goto end_close;
2569 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2572 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2573 WARN("Broken BitmapInfoHeader!\n");
2574 goto end_close;
2577 size = bitmap_info_size(info, DIB_RGB_COLORS);
2578 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2579 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2581 if (!fix_info || !scaled_info) goto end;
2582 memcpy(fix_info, info, size);
2584 pix = *((LPBYTE)info + size);
2585 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2587 memcpy(scaled_info, fix_info, size);
2588 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2589 &bpp_dummy, &compr_dummy);
2590 if(desiredx != 0)
2591 new_width = desiredx;
2592 else
2593 new_width = width;
2595 if(desiredy != 0)
2596 new_height = height > 0 ? desiredy : -desiredy;
2597 else
2598 new_height = height;
2600 if(bm_type == 0)
2602 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2603 core->bcWidth = new_width;
2604 core->bcHeight = new_height;
2606 else
2608 scaled_info->bmiHeader.biWidth = new_width;
2609 scaled_info->bmiHeader.biHeight = new_height;
2612 if (new_height < 0) new_height = -new_height;
2614 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2615 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2617 bits = (char *)info + (offbits ? offbits : size);
2619 if (loadflags & LR_CREATEDIBSECTION)
2621 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2622 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2624 else
2626 if (is_dib_monochrome(fix_info))
2627 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2628 else
2629 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2632 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2633 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2634 SelectObject(screen_mem_dc, orig_bm);
2636 end:
2637 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2638 HeapFree(GetProcessHeap(), 0, scaled_info);
2639 HeapFree(GetProcessHeap(), 0, fix_info);
2640 end_close:
2641 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2643 return hbitmap;
2646 /**********************************************************************
2647 * LoadImageA (USER32.@)
2649 * See LoadImageW.
2651 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2652 INT desiredx, INT desiredy, UINT loadflags)
2654 HANDLE res;
2655 LPWSTR u_name;
2657 if (IS_INTRESOURCE(name))
2658 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2660 __TRY {
2661 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2662 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2663 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2665 __EXCEPT_PAGE_FAULT {
2666 SetLastError( ERROR_INVALID_PARAMETER );
2667 return 0;
2669 __ENDTRY
2670 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2671 HeapFree(GetProcessHeap(), 0, u_name);
2672 return res;
2676 /******************************************************************************
2677 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2679 * PARAMS
2680 * hinst [I] Handle of instance that contains image
2681 * name [I] Name of image
2682 * type [I] Type of image
2683 * desiredx [I] Desired width
2684 * desiredy [I] Desired height
2685 * loadflags [I] Load flags
2687 * RETURNS
2688 * Success: Handle to newly loaded image
2689 * Failure: NULL
2691 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2693 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2694 INT desiredx, INT desiredy, UINT loadflags )
2696 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2697 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2699 if (loadflags & LR_DEFAULTSIZE) {
2700 if (type == IMAGE_ICON) {
2701 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2702 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2703 } else if (type == IMAGE_CURSOR) {
2704 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2705 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2708 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2709 switch (type) {
2710 case IMAGE_BITMAP:
2711 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2713 case IMAGE_ICON:
2714 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2715 if (screen_dc)
2717 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2718 GetDeviceCaps(screen_dc, BITSPIXEL),
2719 FALSE, loadflags);
2721 break;
2723 case IMAGE_CURSOR:
2724 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2725 1, TRUE, loadflags);
2727 return 0;
2730 /******************************************************************************
2731 * CopyImage (USER32.@) Creates new image and copies attributes to it
2733 * PARAMS
2734 * hnd [I] Handle to image to copy
2735 * type [I] Type of image to copy
2736 * desiredx [I] Desired width of new image
2737 * desiredy [I] Desired height of new image
2738 * flags [I] Copy flags
2740 * RETURNS
2741 * Success: Handle to newly created image
2742 * Failure: NULL
2744 * BUGS
2745 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2746 * all other versions (95/2000/XP have been tested) ignore it.
2748 * NOTES
2749 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2750 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2751 * the copy will have the same depth as the screen.
2752 * The content of the image will only be copied if the bit depth of the
2753 * original image is compatible with the bit depth of the screen, or
2754 * if the source is a DIB section.
2755 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2757 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2758 INT desiredy, UINT flags )
2760 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2761 hnd, type, desiredx, desiredy, flags);
2763 switch (type)
2765 case IMAGE_BITMAP:
2767 HBITMAP res = NULL;
2768 DIBSECTION ds;
2769 int objSize;
2770 BITMAPINFO * bi;
2772 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2773 if (!objSize) return 0;
2774 if ((desiredx < 0) || (desiredy < 0)) return 0;
2776 if (flags & LR_COPYFROMRESOURCE)
2778 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2781 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2782 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2784 /* Allocate memory for a BITMAPINFOHEADER structure and a
2785 color table. The maximum number of colors in a color table
2786 is 256 which corresponds to a bitmap with depth 8.
2787 Bitmaps with higher depths don't have color tables. */
2788 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2789 if (!bi) return 0;
2791 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2792 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2793 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2794 bi->bmiHeader.biCompression = BI_RGB;
2796 if (flags & LR_CREATEDIBSECTION)
2798 /* Create a DIB section. LR_MONOCHROME is ignored */
2799 void * bits;
2800 HDC dc = CreateCompatibleDC(NULL);
2802 if (objSize == sizeof(DIBSECTION))
2804 /* The source bitmap is a DIB.
2805 Get its attributes to create an exact copy */
2806 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2809 /* Get the color table or the color masks */
2810 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2812 bi->bmiHeader.biWidth = desiredx;
2813 bi->bmiHeader.biHeight = desiredy;
2814 bi->bmiHeader.biSizeImage = 0;
2816 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2817 DeleteDC(dc);
2819 else
2821 /* Create a device-dependent bitmap */
2823 BOOL monochrome = (flags & LR_MONOCHROME);
2825 if (objSize == sizeof(DIBSECTION))
2827 /* The source bitmap is a DIB section.
2828 Get its attributes */
2829 HDC dc = CreateCompatibleDC(NULL);
2830 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2831 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2832 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2833 DeleteDC(dc);
2835 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2837 /* Look if the colors of the DIB are black and white */
2839 monochrome =
2840 (bi->bmiColors[0].rgbRed == 0xff
2841 && bi->bmiColors[0].rgbGreen == 0xff
2842 && bi->bmiColors[0].rgbBlue == 0xff
2843 && bi->bmiColors[0].rgbReserved == 0
2844 && bi->bmiColors[1].rgbRed == 0
2845 && bi->bmiColors[1].rgbGreen == 0
2846 && bi->bmiColors[1].rgbBlue == 0
2847 && bi->bmiColors[1].rgbReserved == 0)
2849 (bi->bmiColors[0].rgbRed == 0
2850 && bi->bmiColors[0].rgbGreen == 0
2851 && bi->bmiColors[0].rgbBlue == 0
2852 && bi->bmiColors[0].rgbReserved == 0
2853 && bi->bmiColors[1].rgbRed == 0xff
2854 && bi->bmiColors[1].rgbGreen == 0xff
2855 && bi->bmiColors[1].rgbBlue == 0xff
2856 && bi->bmiColors[1].rgbReserved == 0);
2859 else if (!monochrome)
2861 monochrome = ds.dsBm.bmBitsPixel == 1;
2864 if (monochrome)
2866 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2868 else
2870 HDC screenDC = GetDC(NULL);
2871 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2872 ReleaseDC(NULL, screenDC);
2876 if (res)
2878 /* Only copy the bitmap if it's a DIB section or if it's
2879 compatible to the screen */
2880 BOOL copyContents;
2882 if (objSize == sizeof(DIBSECTION))
2884 copyContents = TRUE;
2886 else
2888 HDC screenDC = GetDC(NULL);
2889 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2890 ReleaseDC(NULL, screenDC);
2892 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2895 if (copyContents)
2897 /* The source bitmap may already be selected in a device context,
2898 use GetDIBits/StretchDIBits and not StretchBlt */
2900 HDC dc;
2901 void * bits;
2903 dc = CreateCompatibleDC(NULL);
2905 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2906 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2907 bi->bmiHeader.biSizeImage = 0;
2908 bi->bmiHeader.biClrUsed = 0;
2909 bi->bmiHeader.biClrImportant = 0;
2911 /* Fill in biSizeImage */
2912 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2913 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2915 if (bits)
2917 HBITMAP oldBmp;
2919 /* Get the image bits of the source bitmap */
2920 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2922 /* Copy it to the destination bitmap */
2923 oldBmp = SelectObject(dc, res);
2924 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2925 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2926 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2927 SelectObject(dc, oldBmp);
2929 HeapFree(GetProcessHeap(), 0, bits);
2932 DeleteDC(dc);
2935 if (flags & LR_COPYDELETEORG)
2937 DeleteObject(hnd);
2940 HeapFree(GetProcessHeap(), 0, bi);
2941 return res;
2943 case IMAGE_ICON:
2944 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2945 case IMAGE_CURSOR:
2946 /* Should call CURSORICON_ExtCopy but more testing
2947 * needs to be done before we change this
2949 if (flags) FIXME("Flags are ignored\n");
2950 return CopyCursor(hnd);
2952 return 0;
2956 /******************************************************************************
2957 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2959 * RETURNS
2960 * Success: Handle to specified bitmap
2961 * Failure: NULL
2963 HBITMAP WINAPI LoadBitmapW(
2964 HINSTANCE instance, /* [in] Handle to application instance */
2965 LPCWSTR name) /* [in] Address of bitmap resource name */
2967 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2970 /**********************************************************************
2971 * LoadBitmapA (USER32.@)
2973 * See LoadBitmapW.
2975 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2977 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );