iexplore: Renamed iexplore resource file.
[wine.git] / dlls / user32 / cursoricon.c
blobb54d9fd0067b07617daaca942fa662d952e256f5
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winerror.h"
36 #include "winnls.h"
37 #include "wine/exception.h"
38 #include "wine/server.h"
39 #include "controls.h"
40 #include "user_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
44 WINE_DECLARE_DEBUG_CHANNEL(icon);
45 WINE_DECLARE_DEBUG_CHANNEL(resource);
47 #include "pshpack1.h"
49 typedef struct {
50 BYTE bWidth;
51 BYTE bHeight;
52 BYTE bColorCount;
53 BYTE bReserved;
54 WORD xHotspot;
55 WORD yHotspot;
56 DWORD dwDIBSize;
57 DWORD dwDIBOffset;
58 } CURSORICONFILEDIRENTRY;
60 typedef struct
62 WORD idReserved;
63 WORD idType;
64 WORD idCount;
65 CURSORICONFILEDIRENTRY idEntries[1];
66 } CURSORICONFILEDIR;
68 #include "poppack.h"
70 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
72 static HDC screen_dc;
74 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
77 /**********************************************************************
78 * ICONCACHE for cursors/icons loaded with LR_SHARED.
80 * FIXME: This should not be allocated on the system heap, but on a
81 * subsystem-global heap (i.e. one for all Win16 processes,
82 * and one for each Win32 process).
84 typedef struct tagICONCACHE
86 struct tagICONCACHE *next;
88 HMODULE hModule;
89 HRSRC hRsrc;
90 HRSRC hGroupRsrc;
91 HICON hIcon;
93 INT count;
95 } ICONCACHE;
97 static ICONCACHE *IconAnchor = NULL;
99 static CRITICAL_SECTION IconCrst;
100 static CRITICAL_SECTION_DEBUG critsect_debug =
102 0, 0, &IconCrst,
103 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
104 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
106 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
109 /**********************************************************************
110 * User objects management
113 struct cursoricon_object
115 struct user_object obj; /* object header */
116 ULONG_PTR param; /* opaque param used by 16-bit code */
117 HBITMAP color; /* color bitmap */
118 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
119 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
120 BOOL is_icon; /* whether icon or cursor */
121 UINT width;
122 UINT height;
123 POINT hotspot;
126 static HICON alloc_icon_handle(void)
128 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) );
129 if (!obj) return 0;
130 obj->param = 0;
131 obj->color = 0;
132 obj->alpha = 0;
133 obj->mask = 0;
134 return alloc_user_handle( &obj->obj, USER_ICON );
137 static struct cursoricon_object *get_icon_ptr( HICON handle )
139 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
140 if (obj == OBJ_OTHER_PROCESS)
142 WARN( "icon handle %p from other process\n", handle );
143 obj = NULL;
145 return obj;
148 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
150 release_user_handle_ptr( ptr );
153 static BOOL free_icon_handle( HICON handle )
155 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
157 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
158 else if (obj)
160 ULONG_PTR param = obj->param;
161 if (obj->color) DeleteObject( obj->color );
162 if (obj->alpha) DeleteObject( obj->alpha );
163 DeleteObject( obj->mask );
164 HeapFree( GetProcessHeap(), 0, obj );
165 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
166 USER_Driver->pDestroyCursorIcon( handle );
167 return TRUE;
169 return FALSE;
172 ULONG_PTR get_icon_param( HICON handle )
174 ULONG_PTR ret = 0;
175 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
177 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
178 else if (obj)
180 ret = obj->param;
181 release_user_handle_ptr( obj );
183 return ret;
186 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
188 ULONG_PTR ret = 0;
189 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
191 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
192 else if (obj)
194 ret = obj->param;
195 obj->param = param;
196 release_user_handle_ptr( obj );
198 return ret;
202 /***********************************************************************
203 * map_fileW
205 * Helper function to map a file to memory:
206 * name - file name
207 * [RETURN] ptr - pointer to mapped file
208 * [RETURN] filesize - pointer size of file to be stored if not NULL
210 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
212 HANDLE hFile, hMapping;
213 LPVOID ptr = NULL;
215 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
216 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
217 if (hFile != INVALID_HANDLE_VALUE)
219 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
220 if (hMapping)
222 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
223 CloseHandle( hMapping );
224 if (filesize)
225 *filesize = GetFileSize( hFile, NULL );
227 CloseHandle( hFile );
229 return ptr;
233 /***********************************************************************
234 * get_dib_width_bytes
236 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
238 static int get_dib_width_bytes( int width, int depth )
240 int words;
242 switch(depth)
244 case 1: words = (width + 31) / 32; break;
245 case 4: words = (width + 7) / 8; break;
246 case 8: words = (width + 3) / 4; break;
247 case 15:
248 case 16: words = (width + 1) / 2; break;
249 case 24: words = (width * 3 + 3)/4; break;
250 default:
251 WARN("(%d): Unsupported depth\n", depth );
252 /* fall through */
253 case 32:
254 words = width;
256 return 4 * words;
260 /***********************************************************************
261 * bitmap_info_size
263 * Return the size of the bitmap info structure including color table.
265 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
267 unsigned int colors, size, masks = 0;
269 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
271 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
272 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
273 return sizeof(BITMAPCOREHEADER) + colors *
274 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
276 else /* assume BITMAPINFOHEADER */
278 colors = info->bmiHeader.biClrUsed;
279 if (colors > 256) /* buffer overflow otherwise */
280 colors = 256;
281 if (!colors && (info->bmiHeader.biBitCount <= 8))
282 colors = 1 << info->bmiHeader.biBitCount;
283 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
284 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
285 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
290 /***********************************************************************
291 * copy_bitmap
293 * Helper function to duplicate a bitmap.
295 static HBITMAP copy_bitmap( HBITMAP bitmap )
297 HDC src, dst;
298 HBITMAP new_bitmap;
299 BITMAP bmp;
301 if (!bitmap) return 0;
302 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
304 src = CreateCompatibleDC( 0 );
305 dst = CreateCompatibleDC( 0 );
306 SelectObject( src, bitmap );
307 new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight );
308 SelectObject( dst, new_bitmap );
309 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
310 DeleteDC( dst );
311 DeleteDC( src );
312 return new_bitmap;
316 /***********************************************************************
317 * is_dib_monochrome
319 * Returns whether a DIB can be converted to a monochrome DDB.
321 * A DIB can be converted if its color table contains only black and
322 * white. Black must be the first color in the color table.
324 * Note : If the first color in the color table is white followed by
325 * black, we can't convert it to a monochrome DDB with
326 * SetDIBits, because black and white would be inverted.
328 static BOOL is_dib_monochrome( const BITMAPINFO* info )
330 if (info->bmiHeader.biBitCount != 1) return FALSE;
332 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
334 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
336 /* Check if the first color is black */
337 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
339 rgb++;
341 /* Check if the second color is white */
342 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
343 && (rgb->rgbtBlue == 0xff));
345 else return FALSE;
347 else /* assume BITMAPINFOHEADER */
349 const RGBQUAD *rgb = info->bmiColors;
351 /* Check if the first color is black */
352 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
353 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
355 rgb++;
357 /* Check if the second color is white */
358 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
359 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
361 else return FALSE;
365 /***********************************************************************
366 * DIB_GetBitmapInfo
368 * Get the info from a bitmap header.
369 * Return 1 for INFOHEADER, 0 for COREHEADER,
371 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
372 LONG *height, WORD *bpp, DWORD *compr )
374 if (header->biSize == sizeof(BITMAPCOREHEADER))
376 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
377 *width = core->bcWidth;
378 *height = core->bcHeight;
379 *bpp = core->bcBitCount;
380 *compr = 0;
381 return 0;
383 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
385 *width = header->biWidth;
386 *height = header->biHeight;
387 *bpp = header->biBitCount;
388 *compr = header->biCompression;
389 return 1;
391 ERR("(%d): unknown/wrong size for header\n", header->biSize );
392 return -1;
395 /**********************************************************************
396 * CURSORICON_FindSharedIcon
398 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
400 HICON hIcon = 0;
401 ICONCACHE *ptr;
403 EnterCriticalSection( &IconCrst );
405 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
406 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
408 ptr->count++;
409 hIcon = ptr->hIcon;
410 break;
413 LeaveCriticalSection( &IconCrst );
415 return hIcon;
418 /*************************************************************************
419 * CURSORICON_FindCache
421 * Given a handle, find the corresponding cache element
423 * PARAMS
424 * Handle [I] handle to an Image
426 * RETURNS
427 * Success: The cache entry
428 * Failure: NULL
431 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
433 ICONCACHE *ptr;
434 ICONCACHE *pRet=NULL;
435 BOOL IsFound = FALSE;
437 EnterCriticalSection( &IconCrst );
439 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
441 if ( hIcon == ptr->hIcon )
443 IsFound = TRUE;
444 pRet = ptr;
448 LeaveCriticalSection( &IconCrst );
450 return pRet;
453 /**********************************************************************
454 * CURSORICON_AddSharedIcon
456 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
458 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
459 if ( !ptr ) return;
461 ptr->hModule = hModule;
462 ptr->hRsrc = hRsrc;
463 ptr->hIcon = hIcon;
464 ptr->hGroupRsrc = hGroupRsrc;
465 ptr->count = 1;
467 EnterCriticalSection( &IconCrst );
468 ptr->next = IconAnchor;
469 IconAnchor = ptr;
470 LeaveCriticalSection( &IconCrst );
473 /**********************************************************************
474 * CURSORICON_DelSharedIcon
476 static INT CURSORICON_DelSharedIcon( HICON hIcon )
478 INT count = -1;
479 ICONCACHE *ptr;
481 EnterCriticalSection( &IconCrst );
483 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
484 if ( ptr->hIcon == hIcon )
486 if ( ptr->count > 0 ) ptr->count--;
487 count = ptr->count;
488 break;
491 LeaveCriticalSection( &IconCrst );
493 return count;
496 /**********************************************************************
497 * get_icon_size
499 BOOL get_icon_size( HICON handle, SIZE *size )
501 struct cursoricon_object *info;
503 if (!(info = get_icon_ptr( handle ))) return FALSE;
504 size->cx = info->width;
505 size->cy = info->height;
506 release_icon_ptr( handle, info );
507 return TRUE;
511 * The following macro functions account for the irregularities of
512 * accessing cursor and icon resources in files and resource entries.
514 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
515 int *width, int *height, int *bits );
517 /**********************************************************************
518 * CURSORICON_FindBestIcon
520 * Find the icon closest to the requested size and bit depth.
522 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
523 int width, int height, int depth )
525 int i, cx, cy, bits, bestEntry = -1;
526 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
527 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
529 /* Find Best Fit */
530 iTotalDiff = 0xFFFFFFFF;
531 iColorDiff = 0xFFFFFFFF;
532 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
534 iTempXDiff = abs(width - cx);
535 iTempYDiff = abs(height - cy);
537 if(iTotalDiff > (iTempXDiff + iTempYDiff))
539 iXDiff = iTempXDiff;
540 iYDiff = iTempYDiff;
541 iTotalDiff = iXDiff + iYDiff;
545 /* Find Best Colors for Best Fit */
546 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
548 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
550 iTempColorDiff = abs(depth - bits);
551 if(iColorDiff > iTempColorDiff)
553 bestEntry = i;
554 iColorDiff = iTempColorDiff;
559 return bestEntry;
562 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
563 int *width, int *height, int *bits )
565 CURSORICONDIR *resdir = dir;
566 ICONRESDIR *icon;
568 if ( resdir->idCount <= n )
569 return FALSE;
570 icon = &resdir->idEntries[n].ResInfo.icon;
571 *width = icon->bWidth;
572 *height = icon->bHeight;
573 *bits = resdir->idEntries[n].wBitCount;
574 return TRUE;
577 /**********************************************************************
578 * CURSORICON_FindBestCursor
580 * Find the cursor closest to the requested size.
582 * FIXME: parameter 'color' ignored.
584 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
585 int width, int height, int depth )
587 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
589 /* Double height to account for AND and XOR masks */
591 height *= 2;
593 /* First find the largest one smaller than or equal to the requested size*/
595 maxwidth = maxheight = 0;
596 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
598 if ((cx <= width) && (cy <= height) &&
599 (cx > maxwidth) && (cy > maxheight))
601 bestEntry = i;
602 maxwidth = cx;
603 maxheight = cy;
606 if (bestEntry != -1) return bestEntry;
608 /* Now find the smallest one larger than the requested size */
610 maxwidth = maxheight = 255;
611 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
613 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
615 bestEntry = i;
616 maxwidth = cx;
617 maxheight = cy;
621 return bestEntry;
624 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
625 int *width, int *height, int *bits )
627 CURSORICONDIR *resdir = dir;
628 CURSORDIR *cursor;
630 if ( resdir->idCount <= n )
631 return FALSE;
632 cursor = &resdir->idEntries[n].ResInfo.cursor;
633 *width = cursor->wWidth;
634 *height = cursor->wHeight;
635 *bits = resdir->idEntries[n].wBitCount;
636 return TRUE;
639 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
640 int width, int height, int depth )
642 int n;
644 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
645 width, height, depth );
646 if ( n < 0 )
647 return NULL;
648 return &dir->idEntries[n];
651 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
652 int width, int height, int depth )
654 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
655 width, height, depth );
656 if ( n < 0 )
657 return NULL;
658 return &dir->idEntries[n];
661 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
662 int *width, int *height, int *bits )
664 CURSORICONFILEDIR *filedir = dir;
665 CURSORICONFILEDIRENTRY *entry;
666 BITMAPINFOHEADER *info;
668 if ( filedir->idCount <= n )
669 return FALSE;
670 entry = &filedir->idEntries[n];
671 /* FIXME: check against file size */
672 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
673 *width = entry->bWidth;
674 *height = entry->bHeight;
675 *bits = info->biBitCount;
676 return TRUE;
679 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
680 int width, int height, int depth )
682 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
683 width, height, depth );
684 if ( n < 0 )
685 return NULL;
686 return &dir->idEntries[n];
689 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
690 int width, int height, int depth )
692 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
693 width, height, depth );
694 if ( n < 0 )
695 return NULL;
696 return &dir->idEntries[n];
699 /***********************************************************************
700 * bmi_has_alpha
702 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
704 int i;
705 BOOL has_alpha = FALSE;
706 const unsigned char *ptr = bits;
708 if (info->bmiHeader.biBitCount != 32) return FALSE;
709 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
710 if ((has_alpha = (ptr[3] != 0))) break;
711 return has_alpha;
714 /***********************************************************************
715 * create_alpha_bitmap
717 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
719 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
720 const BITMAPINFO *src_info, const void *color_bits )
722 HBITMAP alpha = 0;
723 BITMAPINFO *info = NULL;
724 BITMAP bm;
725 HDC hdc;
726 void *bits;
727 unsigned char *ptr;
728 int i;
730 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
731 if (bm.bmBitsPixel != 32) return 0;
733 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
734 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
735 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
736 info->bmiHeader.biWidth = bm.bmWidth;
737 info->bmiHeader.biHeight = -bm.bmHeight;
738 info->bmiHeader.biPlanes = 1;
739 info->bmiHeader.biBitCount = 32;
740 info->bmiHeader.biCompression = BI_RGB;
741 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
742 info->bmiHeader.biXPelsPerMeter = 0;
743 info->bmiHeader.biYPelsPerMeter = 0;
744 info->bmiHeader.biClrUsed = 0;
745 info->bmiHeader.biClrImportant = 0;
746 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
748 if (src_info)
750 SelectObject( hdc, alpha );
751 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
752 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
753 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
756 else
758 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
759 if (!bmi_has_alpha( info, bits ))
761 DeleteObject( alpha );
762 alpha = 0;
763 goto done;
767 /* pre-multiply by alpha */
768 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
770 unsigned int alpha = ptr[3];
771 ptr[0] = ptr[0] * alpha / 255;
772 ptr[1] = ptr[1] * alpha / 255;
773 ptr[2] = ptr[2] * alpha / 255;
776 done:
777 DeleteDC( hdc );
778 HeapFree( GetProcessHeap(), 0, info );
779 return alpha;
783 /***********************************************************************
784 * create_icon_bitmaps
786 * Create the color, mask and alpha bitmaps from the DIB info.
788 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
789 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
791 BOOL monochrome = is_dib_monochrome( bmi );
792 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
793 BITMAPINFO *info;
794 void *color_bits, *mask_bits;
795 BOOL ret = FALSE;
796 HDC hdc = 0;
798 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
799 return FALSE;
800 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
802 memcpy( info, bmi, size );
803 info->bmiHeader.biHeight /= 2;
805 color_bits = (char *)bmi + size;
806 mask_bits = (char *)color_bits +
807 get_dib_width_bytes( bmi->bmiHeader.biWidth,
808 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
810 *alpha = 0;
811 if (monochrome)
813 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
814 *color = 0;
816 /* copy color data into second half of mask bitmap */
817 SelectObject( hdc, *mask );
818 StretchDIBits( hdc, 0, height, width, height,
819 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
820 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
822 else
824 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
825 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
826 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
828 DeleteObject( *mask );
829 goto done;
831 SelectObject( hdc, *color );
832 StretchDIBits( hdc, 0, 0, width, height,
833 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
834 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
836 if (bmi_has_alpha( info, color_bits ))
837 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
839 /* convert info to monochrome to copy the mask */
840 info->bmiHeader.biBitCount = 1;
841 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
843 RGBQUAD *rgb = info->bmiColors;
845 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
846 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
847 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
848 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
850 else
852 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
854 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
855 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
859 SelectObject( hdc, *mask );
860 StretchDIBits( hdc, 0, 0, width, height,
861 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
862 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
863 ret = TRUE;
865 done:
866 DeleteDC( hdc );
867 HeapFree( GetProcessHeap(), 0, info );
868 return ret;
871 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
872 POINT hotspot, BOOL bIcon,
873 DWORD dwVersion,
874 INT width, INT height,
875 UINT cFlag )
877 HICON hObj;
878 HBITMAP color = 0, mask = 0, alpha = 0;
879 BOOL do_stretch;
881 if (dwVersion == 0x00020000)
883 FIXME_(cursor)("\t2.xx resources are not supported\n");
884 return 0;
887 /* Check bitmap header */
889 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
890 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
891 bmi->bmiHeader.biCompression != BI_RGB) )
893 WARN_(cursor)("\tinvalid resource bitmap header.\n");
894 return 0;
897 if (!width) width = bmi->bmiHeader.biWidth;
898 if (!height) height = bmi->bmiHeader.biHeight/2;
899 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
900 (bmi->bmiHeader.biWidth != width);
902 /* Scale the hotspot */
903 if (bIcon)
905 hotspot.x = width / 2;
906 hotspot.y = height / 2;
908 else if (do_stretch)
910 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
911 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
914 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
915 if (!screen_dc) return 0;
917 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
919 hObj = alloc_icon_handle();
920 if (hObj)
922 struct cursoricon_object *info = get_icon_ptr( hObj );
924 info->color = color;
925 info->mask = mask;
926 info->alpha = alpha;
927 info->is_icon = bIcon;
928 info->hotspot = hotspot;
929 info->width = width;
930 info->height = height;
931 release_icon_ptr( hObj, info );
932 USER_Driver->pCreateCursorIcon( hObj );
934 else
936 DeleteObject( color );
937 DeleteObject( alpha );
938 DeleteObject( mask );
940 return hObj;
944 /**********************************************************************
945 * .ANI cursor support
947 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
948 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
949 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
951 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
952 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
953 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
954 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
955 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
956 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
958 #define ANI_FLAG_ICON 0x1
959 #define ANI_FLAG_SEQUENCE 0x2
961 typedef struct {
962 DWORD header_size;
963 DWORD num_frames;
964 DWORD num_steps;
965 DWORD width;
966 DWORD height;
967 DWORD bpp;
968 DWORD num_planes;
969 DWORD display_rate;
970 DWORD flags;
971 } ani_header;
973 typedef struct {
974 DWORD data_size;
975 const unsigned char *data;
976 } riff_chunk_t;
978 static void dump_ani_header( const ani_header *header )
980 TRACE(" header size: %d\n", header->header_size);
981 TRACE(" frames: %d\n", header->num_frames);
982 TRACE(" steps: %d\n", header->num_steps);
983 TRACE(" width: %d\n", header->width);
984 TRACE(" height: %d\n", header->height);
985 TRACE(" bpp: %d\n", header->bpp);
986 TRACE(" planes: %d\n", header->num_planes);
987 TRACE(" display rate: %d\n", header->display_rate);
988 TRACE(" flags: 0x%08x\n", header->flags);
993 * RIFF:
994 * DWORD "RIFF"
995 * DWORD size
996 * DWORD riff_id
997 * BYTE[] data
999 * LIST:
1000 * DWORD "LIST"
1001 * DWORD size
1002 * DWORD list_id
1003 * BYTE[] data
1005 * CHUNK:
1006 * DWORD chunk_id
1007 * DWORD size
1008 * BYTE[] data
1010 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1012 const unsigned char *ptr = parent_chunk->data;
1013 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1015 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1017 while (ptr < end)
1019 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1020 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1022 ptr += sizeof(DWORD);
1023 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1024 ptr += sizeof(DWORD);
1025 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1026 chunk->data = ptr;
1028 return;
1031 ptr += sizeof(DWORD);
1032 ptr += (*(const DWORD *)ptr + 1) & ~1;
1033 ptr += sizeof(DWORD);
1039 * .ANI layout:
1041 * RIFF:'ACON' RIFF chunk
1042 * |- CHUNK:'anih' Header
1043 * |- CHUNK:'seq ' Sequence information (optional)
1044 * \- LIST:'fram' Frame list
1045 * |- CHUNK:icon Cursor frames
1046 * |- CHUNK:icon
1047 * |- ...
1048 * \- CHUNK:icon
1050 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1051 INT width, INT height, INT depth )
1053 HCURSOR cursor;
1054 ani_header header = {0};
1055 LPBYTE frame_bits = 0;
1056 POINT hotspot;
1057 CURSORICONFILEDIRENTRY *entry;
1059 riff_chunk_t root_chunk = { bits_size, bits };
1060 riff_chunk_t ACON_chunk = {0};
1061 riff_chunk_t anih_chunk = {0};
1062 riff_chunk_t fram_chunk = {0};
1063 const unsigned char *icon_data;
1065 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1067 if (!bits) return 0;
1069 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1070 if (!ACON_chunk.data)
1072 ERR("Failed to get root chunk.\n");
1073 return 0;
1076 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1077 if (!anih_chunk.data)
1079 ERR("Failed to get 'anih' chunk.\n");
1080 return 0;
1082 memcpy( &header, anih_chunk.data, sizeof(header) );
1083 dump_ani_header( &header );
1085 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1086 if (!fram_chunk.data)
1088 ERR("Failed to get icon list.\n");
1089 return 0;
1092 /* FIXME: For now, just load the first frame. Before we can load all the
1093 * frames, we need to write the needed code in wineserver, etc. to handle
1094 * cursors. Once this code is written, we can extend it to support .ani
1095 * cursors and then update user32 and winex11.drv to load all frames.
1097 * Hopefully this will at least make some games (C&C3, etc.) more playable
1098 * in the meantime.
1100 FIXME("Loading all frames for .ani cursors not implemented.\n");
1101 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1103 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1104 width, height, depth );
1106 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1107 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1109 if (!header.width || !header.height)
1111 header.width = entry->bWidth;
1112 header.height = entry->bHeight;
1115 hotspot.x = entry->xHotspot;
1116 hotspot.y = entry->yHotspot;
1118 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1119 FALSE, 0x00030000, header.width, header.height, 0 );
1121 HeapFree( GetProcessHeap(), 0, frame_bits );
1123 return cursor;
1127 /**********************************************************************
1128 * CreateIconFromResourceEx (USER32.@)
1130 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1131 * with cbSize parameter as well.
1133 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1134 BOOL bIcon, DWORD dwVersion,
1135 INT width, INT height,
1136 UINT cFlag )
1138 POINT hotspot;
1139 BITMAPINFO *bmi;
1141 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1142 bits, cbSize, dwVersion, width, height,
1143 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1145 if (bIcon)
1147 hotspot.x = width / 2;
1148 hotspot.y = height / 2;
1149 bmi = (BITMAPINFO *)bits;
1151 else /* get the hotspot */
1153 SHORT *pt = (SHORT *)bits;
1154 hotspot.x = pt[0];
1155 hotspot.y = pt[1];
1156 bmi = (BITMAPINFO *)(pt + 2);
1159 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1160 width, height, cFlag );
1164 /**********************************************************************
1165 * CreateIconFromResource (USER32.@)
1167 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1168 BOOL bIcon, DWORD dwVersion)
1170 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1174 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1175 INT width, INT height, INT depth,
1176 BOOL fCursor, UINT loadflags)
1178 CURSORICONFILEDIRENTRY *entry;
1179 CURSORICONFILEDIR *dir;
1180 DWORD filesize = 0;
1181 HICON hIcon = 0;
1182 LPBYTE bits;
1183 POINT hotspot;
1185 TRACE("loading %s\n", debugstr_w( filename ));
1187 bits = map_fileW( filename, &filesize );
1188 if (!bits)
1189 return hIcon;
1191 /* Check for .ani. */
1192 if (memcmp( bits, "RIFF", 4 ) == 0)
1194 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1195 depth );
1196 goto end;
1199 dir = (CURSORICONFILEDIR*) bits;
1200 if ( filesize < sizeof(*dir) )
1201 goto end;
1203 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1204 goto end;
1206 if ( fCursor )
1207 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1208 else
1209 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1211 if ( !entry )
1212 goto end;
1214 /* check that we don't run off the end of the file */
1215 if ( entry->dwDIBOffset > filesize )
1216 goto end;
1217 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1218 goto end;
1220 hotspot.x = entry->xHotspot;
1221 hotspot.y = entry->yHotspot;
1222 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1223 hotspot, !fCursor, 0x00030000,
1224 width, height, loadflags );
1225 end:
1226 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1227 UnmapViewOfFile( bits );
1228 return hIcon;
1231 /**********************************************************************
1232 * CURSORICON_Load
1234 * Load a cursor or icon from resource or file.
1236 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1237 INT width, INT height, INT depth,
1238 BOOL fCursor, UINT loadflags)
1240 HANDLE handle = 0;
1241 HICON hIcon = 0;
1242 HRSRC hRsrc, hGroupRsrc;
1243 CURSORICONDIR *dir;
1244 CURSORICONDIRENTRY *dirEntry;
1245 LPBYTE bits;
1246 WORD wResId;
1247 DWORD dwBytesInRes;
1249 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1250 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1252 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1253 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1255 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1257 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1258 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1260 /* Get directory resource ID */
1262 if (!(hRsrc = FindResourceW( hInstance, name,
1263 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1264 return 0;
1265 hGroupRsrc = hRsrc;
1267 /* Find the best entry in the directory */
1269 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1270 if (!(dir = LockResource( handle ))) return 0;
1271 if (fCursor)
1272 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1273 else
1274 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1275 if (!dirEntry) return 0;
1276 wResId = dirEntry->wResId;
1277 dwBytesInRes = dirEntry->dwBytesInRes;
1278 FreeResource( handle );
1280 /* Load the resource */
1282 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1283 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1285 /* If shared icon, check whether it was already loaded */
1286 if ( (loadflags & LR_SHARED)
1287 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1288 return hIcon;
1290 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1291 bits = LockResource( handle );
1292 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1293 !fCursor, 0x00030000, width, height, loadflags);
1294 FreeResource( handle );
1296 /* If shared icon, add to icon cache */
1298 if ( hIcon && (loadflags & LR_SHARED) )
1299 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1301 return hIcon;
1305 /*************************************************************************
1306 * CURSORICON_ExtCopy
1308 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1310 * PARAMS
1311 * Handle [I] handle to an Image
1312 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1313 * iDesiredCX [I] The Desired width of the Image
1314 * iDesiredCY [I] The desired height of the Image
1315 * nFlags [I] The flags from CopyImage
1317 * RETURNS
1318 * Success: The new handle of the Image
1320 * NOTES
1321 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1322 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1323 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1328 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1329 INT iDesiredCX, INT iDesiredCY,
1330 UINT nFlags)
1332 HICON hNew=0;
1334 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1335 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1337 if(hIcon == 0)
1339 return 0;
1342 /* Best Fit or Monochrome */
1343 if( (nFlags & LR_COPYFROMRESOURCE
1344 && (iDesiredCX > 0 || iDesiredCY > 0))
1345 || nFlags & LR_MONOCHROME)
1347 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1349 /* Not Found in Cache, then do a straight copy
1351 if(pIconCache == NULL)
1353 hNew = CopyIcon( hIcon );
1354 if(nFlags & LR_COPYFROMRESOURCE)
1356 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1359 else
1361 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1362 LPBYTE pBits;
1363 HANDLE hMem;
1364 HRSRC hRsrc;
1365 DWORD dwBytesInRes;
1366 WORD wResId;
1367 CURSORICONDIR *pDir;
1368 CURSORICONDIRENTRY *pDirEntry;
1369 BOOL bIsIcon = (nType == IMAGE_ICON);
1371 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1373 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1374 || (iDesiredCX == 0 && iDesiredCY == 0))
1376 iDesiredCY = GetSystemMetrics(bIsIcon ?
1377 SM_CYICON : SM_CYCURSOR);
1378 iDesiredCX = GetSystemMetrics(bIsIcon ?
1379 SM_CXICON : SM_CXCURSOR);
1382 /* Retrieve the CURSORICONDIRENTRY
1384 if (!(hMem = LoadResource( pIconCache->hModule ,
1385 pIconCache->hGroupRsrc)))
1387 return 0;
1389 if (!(pDir = LockResource( hMem )))
1391 return 0;
1394 /* Find Best Fit
1396 if(bIsIcon)
1398 pDirEntry = CURSORICON_FindBestIconRes(
1399 pDir, iDesiredCX, iDesiredCY, 256 );
1401 else
1403 pDirEntry = CURSORICON_FindBestCursorRes(
1404 pDir, iDesiredCX, iDesiredCY, 1);
1407 wResId = pDirEntry->wResId;
1408 dwBytesInRes = pDirEntry->dwBytesInRes;
1409 FreeResource(hMem);
1411 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1412 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1413 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1415 /* Get the Best Fit
1417 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1418 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1420 return 0;
1422 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1424 return 0;
1427 pBits = LockResource( hMem );
1429 if(nFlags & LR_DEFAULTSIZE)
1431 iTargetCY = GetSystemMetrics(SM_CYICON);
1432 iTargetCX = GetSystemMetrics(SM_CXICON);
1435 /* Create a New Icon with the proper dimension
1437 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1438 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1439 FreeResource(hMem);
1442 else hNew = CopyIcon( hIcon );
1443 return hNew;
1447 /***********************************************************************
1448 * CreateCursor (USER32.@)
1450 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1451 INT xHotSpot, INT yHotSpot,
1452 INT nWidth, INT nHeight,
1453 LPCVOID lpANDbits, LPCVOID lpXORbits )
1455 ICONINFO info;
1456 HCURSOR hCursor;
1458 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1459 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1461 info.fIcon = FALSE;
1462 info.xHotspot = xHotSpot;
1463 info.yHotspot = yHotSpot;
1464 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1465 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1466 hCursor = CreateIconIndirect( &info );
1467 DeleteObject( info.hbmMask );
1468 DeleteObject( info.hbmColor );
1469 return hCursor;
1473 /***********************************************************************
1474 * CreateIcon (USER32.@)
1476 * Creates an icon based on the specified bitmaps. The bitmaps must be
1477 * provided in a device dependent format and will be resized to
1478 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1479 * depth. The provided bitmaps must be top-down bitmaps.
1480 * Although Windows does not support 15bpp(*) this API must support it
1481 * for Winelib applications.
1483 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1484 * format!
1486 * RETURNS
1487 * Success: handle to an icon
1488 * Failure: NULL
1490 * FIXME: Do we need to resize the bitmaps?
1492 HICON WINAPI CreateIcon(
1493 HINSTANCE hInstance, /* [in] the application's hInstance */
1494 INT nWidth, /* [in] the width of the provided bitmaps */
1495 INT nHeight, /* [in] the height of the provided bitmaps */
1496 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1497 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1498 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1499 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1501 ICONINFO iinfo;
1502 HICON hIcon;
1504 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1505 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1507 iinfo.fIcon = TRUE;
1508 iinfo.xHotspot = nWidth / 2;
1509 iinfo.yHotspot = nHeight / 2;
1510 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1511 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1513 hIcon = CreateIconIndirect( &iinfo );
1515 DeleteObject( iinfo.hbmMask );
1516 DeleteObject( iinfo.hbmColor );
1518 return hIcon;
1522 /***********************************************************************
1523 * CopyIcon (USER32.@)
1525 HICON WINAPI CopyIcon( HICON hIcon )
1527 struct cursoricon_object *ptrOld, *ptrNew;
1528 HICON hNew;
1530 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1531 if ((hNew = alloc_icon_handle()))
1533 ptrNew = get_icon_ptr( hNew );
1534 ptrNew->color = copy_bitmap( ptrOld->color );
1535 ptrNew->alpha = copy_bitmap( ptrOld->alpha );
1536 ptrNew->mask = copy_bitmap( ptrOld->mask );
1537 ptrNew->is_icon = ptrOld->is_icon;
1538 ptrNew->width = ptrOld->width;
1539 ptrNew->height = ptrOld->height;
1540 ptrNew->hotspot = ptrOld->hotspot;
1541 release_icon_ptr( hNew, ptrNew );
1543 release_icon_ptr( hIcon, ptrOld );
1544 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1545 return hNew;
1549 /***********************************************************************
1550 * DestroyIcon (USER32.@)
1552 BOOL WINAPI DestroyIcon( HICON hIcon )
1554 TRACE_(icon)("%p\n", hIcon );
1556 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1557 free_icon_handle( hIcon );
1558 return TRUE;
1562 /***********************************************************************
1563 * DestroyCursor (USER32.@)
1565 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1567 if (GetCursor() == hCursor)
1569 WARN_(cursor)("Destroying active cursor!\n" );
1570 return FALSE;
1572 return DestroyIcon( hCursor );
1575 /***********************************************************************
1576 * DrawIcon (USER32.@)
1578 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1580 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1583 /***********************************************************************
1584 * SetCursor (USER32.@)
1586 * Set the cursor shape.
1588 * RETURNS
1589 * A handle to the previous cursor shape.
1591 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1593 HCURSOR hOldCursor;
1594 int show_count;
1595 BOOL ret;
1597 TRACE("%p\n", hCursor);
1599 SERVER_START_REQ( set_cursor )
1601 req->flags = SET_CURSOR_HANDLE;
1602 req->handle = wine_server_user_handle( hCursor );
1603 if ((ret = !wine_server_call_err( req )))
1605 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1606 show_count = reply->prev_count;
1609 SERVER_END_REQ;
1611 if (!ret) return 0;
1613 /* Change the cursor shape only if it is visible */
1614 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1615 return hOldCursor;
1618 /***********************************************************************
1619 * ShowCursor (USER32.@)
1621 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1623 HCURSOR cursor;
1624 int increment = bShow ? 1 : -1;
1625 int count;
1627 SERVER_START_REQ( set_cursor )
1629 req->flags = SET_CURSOR_COUNT;
1630 req->show_count = increment;
1631 wine_server_call( req );
1632 cursor = wine_server_ptr_handle( reply->prev_handle );
1633 count = reply->prev_count + increment;
1635 SERVER_END_REQ;
1637 TRACE("%d, count=%d\n", bShow, count );
1639 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1640 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1642 return count;
1645 /***********************************************************************
1646 * GetCursor (USER32.@)
1648 HCURSOR WINAPI GetCursor(void)
1650 HCURSOR ret;
1652 SERVER_START_REQ( set_cursor )
1654 req->flags = 0;
1655 wine_server_call( req );
1656 ret = wine_server_ptr_handle( reply->prev_handle );
1658 SERVER_END_REQ;
1659 return ret;
1663 /***********************************************************************
1664 * ClipCursor (USER32.@)
1666 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1668 RECT virt;
1670 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1671 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1672 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1673 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1675 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1676 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1678 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1679 CURSOR_ClipRect = virt;
1681 USER_Driver->pClipCursor( rect );
1682 return TRUE;
1686 /***********************************************************************
1687 * GetClipCursor (USER32.@)
1689 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1691 /* If this is first time - initialize the rect */
1692 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1694 return CopyRect( rect, &CURSOR_ClipRect );
1698 /***********************************************************************
1699 * SetSystemCursor (USER32.@)
1701 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1703 FIXME("(%p,%08x),stub!\n", hcur, id);
1704 return TRUE;
1708 /**********************************************************************
1709 * LookupIconIdFromDirectoryEx (USER32.@)
1711 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1712 INT width, INT height, UINT cFlag )
1714 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1715 UINT retVal = 0;
1716 if( dir && !dir->idReserved && (dir->idType & 3) )
1718 CURSORICONDIRENTRY* entry;
1720 const HDC hdc = GetDC(0);
1721 const int depth = (cFlag & LR_MONOCHROME) ?
1722 1 : GetDeviceCaps(hdc, BITSPIXEL);
1723 ReleaseDC(0, hdc);
1725 if( bIcon )
1726 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1727 else
1728 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1730 if( entry ) retVal = entry->wResId;
1732 else WARN_(cursor)("invalid resource directory\n");
1733 return retVal;
1736 /**********************************************************************
1737 * LookupIconIdFromDirectory (USER32.@)
1739 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1741 return LookupIconIdFromDirectoryEx( dir, bIcon,
1742 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1743 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1746 /***********************************************************************
1747 * LoadCursorW (USER32.@)
1749 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1751 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1753 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1754 LR_SHARED | LR_DEFAULTSIZE );
1757 /***********************************************************************
1758 * LoadCursorA (USER32.@)
1760 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1762 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1764 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1765 LR_SHARED | LR_DEFAULTSIZE );
1768 /***********************************************************************
1769 * LoadCursorFromFileW (USER32.@)
1771 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1773 TRACE("%s\n", debugstr_w(name));
1775 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1776 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1779 /***********************************************************************
1780 * LoadCursorFromFileA (USER32.@)
1782 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1784 TRACE("%s\n", debugstr_a(name));
1786 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1787 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1790 /***********************************************************************
1791 * LoadIconW (USER32.@)
1793 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1795 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1797 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1798 LR_SHARED | LR_DEFAULTSIZE );
1801 /***********************************************************************
1802 * LoadIconA (USER32.@)
1804 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1806 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1808 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1809 LR_SHARED | LR_DEFAULTSIZE );
1812 /**********************************************************************
1813 * GetIconInfo (USER32.@)
1815 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1817 struct cursoricon_object *ptr;
1819 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1821 TRACE("%p => %dx%d\n", hIcon, ptr->width, ptr->height);
1823 iconinfo->fIcon = ptr->is_icon;
1824 iconinfo->xHotspot = ptr->hotspot.x;
1825 iconinfo->yHotspot = ptr->hotspot.y;
1826 iconinfo->hbmColor = copy_bitmap( ptr->color );
1827 iconinfo->hbmMask = copy_bitmap( ptr->mask );
1828 release_icon_ptr( hIcon, ptr );
1830 return TRUE;
1833 /* copy an icon bitmap, even when it can't be selected into a DC */
1834 /* helper for CreateIconIndirect */
1835 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1836 HBITMAP src, int width, int height )
1838 HDC hdc = CreateCompatibleDC( 0 );
1840 if (!SelectObject( hdc, src )) /* do it the hard way */
1842 BITMAPINFO *info;
1843 void *bits;
1845 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1846 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1847 info->bmiHeader.biWidth = width;
1848 info->bmiHeader.biHeight = height;
1849 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1850 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1851 info->bmiHeader.biCompression = BI_RGB;
1852 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1853 info->bmiHeader.biXPelsPerMeter = 0;
1854 info->bmiHeader.biYPelsPerMeter = 0;
1855 info->bmiHeader.biClrUsed = 0;
1856 info->bmiHeader.biClrImportant = 0;
1857 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1858 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1859 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1860 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1862 HeapFree( GetProcessHeap(), 0, bits );
1863 HeapFree( GetProcessHeap(), 0, info );
1865 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1867 DeleteDC( hdc );
1870 /**********************************************************************
1871 * CreateIconIndirect (USER32.@)
1873 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1875 BITMAP bmpXor, bmpAnd;
1876 HICON hObj;
1877 HBITMAP color = 0, mask;
1878 int width, height;
1879 HDC hdc;
1881 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1882 iconinfo->hbmColor, iconinfo->hbmMask,
1883 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1885 if (!iconinfo->hbmMask) return 0;
1887 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1888 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1889 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1890 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1892 if (iconinfo->hbmColor)
1894 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1895 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1896 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1897 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1899 width = bmpXor.bmWidth;
1900 height = bmpXor.bmHeight;
1901 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1903 color = CreateCompatibleBitmap( screen_dc, width, height );
1904 mask = CreateBitmap( width, height, 1, 1, NULL );
1906 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1908 else
1910 width = bmpAnd.bmWidth;
1911 height = bmpAnd.bmHeight;
1912 mask = CreateBitmap( width, height, 1, 1, NULL );
1915 hdc = CreateCompatibleDC( 0 );
1916 SelectObject( hdc, mask );
1917 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1919 if (color)
1921 SelectObject( hdc, color );
1922 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1924 else if (iconinfo->hbmColor)
1926 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1928 else height /= 2;
1930 DeleteDC( hdc );
1932 hObj = alloc_icon_handle();
1933 if (hObj)
1935 struct cursoricon_object *info = get_icon_ptr( hObj );
1937 info->color = color;
1938 info->mask = mask;
1939 info->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1940 info->is_icon = iconinfo->fIcon;
1941 info->width = width;
1942 info->height = height;
1943 if (info->is_icon)
1945 info->hotspot.x = width / 2;
1946 info->hotspot.y = height / 2;
1948 else
1950 info->hotspot.x = iconinfo->xHotspot;
1951 info->hotspot.y = iconinfo->yHotspot;
1954 release_icon_ptr( hObj, info );
1955 USER_Driver->pCreateCursorIcon( hObj );
1957 return hObj;
1960 /******************************************************************************
1961 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1963 * NOTES
1964 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1966 * PARAMS
1967 * hdc [I] Handle to device context
1968 * x0 [I] X coordinate of upper left corner
1969 * y0 [I] Y coordinate of upper left corner
1970 * hIcon [I] Handle to icon to draw
1971 * cxWidth [I] Width of icon
1972 * cyWidth [I] Height of icon
1973 * istep [I] Index of frame in animated cursor
1974 * hbr [I] Handle to background brush
1975 * flags [I] Icon-drawing flags
1977 * RETURNS
1978 * Success: TRUE
1979 * Failure: FALSE
1981 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1982 INT cxWidth, INT cyWidth, UINT istep,
1983 HBRUSH hbr, UINT flags )
1985 struct cursoricon_object *ptr;
1986 HDC hdc_dest, hMemDC;
1987 BOOL result = FALSE, DoOffscreen;
1988 HBITMAP hB_off = 0;
1989 COLORREF oldFg, oldBg;
1990 INT x, y, nStretchMode;
1992 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1993 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1995 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1996 if (!(hMemDC = CreateCompatibleDC( hdc )))
1998 release_icon_ptr( hIcon, ptr );
1999 return FALSE;
2002 if (istep)
2003 FIXME_(icon)("Ignoring istep=%d\n", istep);
2004 if (flags & DI_NOMIRROR)
2005 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2007 /* Calculate the size of the destination image. */
2008 if (cxWidth == 0)
2010 if (flags & DI_DEFAULTSIZE)
2011 cxWidth = GetSystemMetrics (SM_CXICON);
2012 else
2013 cxWidth = ptr->width;
2015 if (cyWidth == 0)
2017 if (flags & DI_DEFAULTSIZE)
2018 cyWidth = GetSystemMetrics (SM_CYICON);
2019 else
2020 cyWidth = ptr->height;
2023 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2025 if (DoOffscreen) {
2026 RECT r;
2028 r.left = 0;
2029 r.top = 0;
2030 r.right = cxWidth;
2031 r.bottom = cxWidth;
2033 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2034 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2036 DeleteDC( hdc_dest );
2037 goto failed;
2039 SelectObject(hdc_dest, hB_off);
2040 FillRect(hdc_dest, &r, hbr);
2041 x = y = 0;
2043 else
2045 hdc_dest = hdc;
2046 x = x0;
2047 y = y0;
2050 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2052 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2053 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2055 if (ptr->alpha && (flags & DI_IMAGE))
2057 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2059 SelectObject( hMemDC, ptr->alpha );
2060 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2061 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
2064 if (flags & DI_MASK)
2066 SelectObject( hMemDC, ptr->mask );
2067 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2068 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
2071 if (flags & DI_IMAGE)
2073 if (ptr->color)
2075 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2076 SelectObject( hMemDC, ptr->color );
2077 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2078 hMemDC, 0, 0, ptr->width, ptr->height, rop );
2080 else
2082 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2083 SelectObject( hMemDC, ptr->mask );
2084 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2085 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
2089 done:
2090 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2092 SetTextColor( hdc, oldFg );
2093 SetBkColor( hdc, oldBg );
2094 SetStretchBltMode (hdc, nStretchMode);
2095 result = TRUE;
2096 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2097 if (hB_off) DeleteObject(hB_off);
2098 failed:
2099 DeleteDC( hMemDC );
2100 release_icon_ptr( hIcon, ptr );
2101 return result;
2104 /***********************************************************************
2105 * DIB_FixColorsToLoadflags
2107 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2108 * are in loadflags
2110 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2112 int colors;
2113 COLORREF c_W, c_S, c_F, c_L, c_C;
2114 int incr,i;
2115 RGBQUAD *ptr;
2116 int bitmap_type;
2117 LONG width;
2118 LONG height;
2119 WORD bpp;
2120 DWORD compr;
2122 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2124 WARN_(resource)("Invalid bitmap\n");
2125 return;
2128 if (bpp > 8) return;
2130 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2132 incr = 3;
2133 colors = 1 << bpp;
2135 else
2137 incr = 4;
2138 colors = bmi->bmiHeader.biClrUsed;
2139 if (colors > 256) colors = 256;
2140 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2143 c_W = GetSysColor(COLOR_WINDOW);
2144 c_S = GetSysColor(COLOR_3DSHADOW);
2145 c_F = GetSysColor(COLOR_3DFACE);
2146 c_L = GetSysColor(COLOR_3DLIGHT);
2148 if (loadflags & LR_LOADTRANSPARENT) {
2149 switch (bpp) {
2150 case 1: pix = pix >> 7; break;
2151 case 4: pix = pix >> 4; break;
2152 case 8: break;
2153 default:
2154 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2155 return;
2157 if (pix >= colors) {
2158 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2159 return;
2161 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2162 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2163 ptr->rgbBlue = GetBValue(c_W);
2164 ptr->rgbGreen = GetGValue(c_W);
2165 ptr->rgbRed = GetRValue(c_W);
2167 if (loadflags & LR_LOADMAP3DCOLORS)
2168 for (i=0; i<colors; i++) {
2169 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2170 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2171 if (c_C == RGB(128, 128, 128)) {
2172 ptr->rgbRed = GetRValue(c_S);
2173 ptr->rgbGreen = GetGValue(c_S);
2174 ptr->rgbBlue = GetBValue(c_S);
2175 } else if (c_C == RGB(192, 192, 192)) {
2176 ptr->rgbRed = GetRValue(c_F);
2177 ptr->rgbGreen = GetGValue(c_F);
2178 ptr->rgbBlue = GetBValue(c_F);
2179 } else if (c_C == RGB(223, 223, 223)) {
2180 ptr->rgbRed = GetRValue(c_L);
2181 ptr->rgbGreen = GetGValue(c_L);
2182 ptr->rgbBlue = GetBValue(c_L);
2188 /**********************************************************************
2189 * BITMAP_Load
2191 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2192 INT desiredx, INT desiredy, UINT loadflags )
2194 HBITMAP hbitmap = 0, orig_bm;
2195 HRSRC hRsrc;
2196 HGLOBAL handle;
2197 char *ptr = NULL;
2198 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2199 int size;
2200 BYTE pix;
2201 char *bits;
2202 LONG width, height, new_width, new_height;
2203 WORD bpp_dummy;
2204 DWORD compr_dummy, offbits = 0;
2205 INT bm_type;
2206 HDC screen_mem_dc = NULL;
2208 if (!(loadflags & LR_LOADFROMFILE))
2210 if (!instance)
2212 /* OEM bitmap: try to load the resource from user32.dll */
2213 instance = user32_module;
2216 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2217 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2219 if ((info = LockResource( handle )) == NULL) return 0;
2221 else
2223 BITMAPFILEHEADER * bmfh;
2225 if (!(ptr = map_fileW( name, NULL ))) return 0;
2226 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2227 bmfh = (BITMAPFILEHEADER *)ptr;
2228 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2230 WARN("Invalid/unsupported bitmap format!\n");
2231 goto end_close;
2233 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2236 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2237 WARN("Broken BitmapInfoHeader!\n");
2238 goto end_close;
2241 size = bitmap_info_size(info, DIB_RGB_COLORS);
2242 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2243 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2245 if (!fix_info || !scaled_info) goto end;
2246 memcpy(fix_info, info, size);
2248 pix = *((LPBYTE)info + size);
2249 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2251 memcpy(scaled_info, fix_info, size);
2252 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2253 &bpp_dummy, &compr_dummy);
2254 if(desiredx != 0)
2255 new_width = desiredx;
2256 else
2257 new_width = width;
2259 if(desiredy != 0)
2260 new_height = height > 0 ? desiredy : -desiredy;
2261 else
2262 new_height = height;
2264 if(bm_type == 0)
2266 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2267 core->bcWidth = new_width;
2268 core->bcHeight = new_height;
2270 else
2272 scaled_info->bmiHeader.biWidth = new_width;
2273 scaled_info->bmiHeader.biHeight = new_height;
2276 if (new_height < 0) new_height = -new_height;
2278 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2279 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2281 bits = (char *)info + (offbits ? offbits : size);
2283 if (loadflags & LR_CREATEDIBSECTION)
2285 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2286 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2288 else
2290 if (is_dib_monochrome(fix_info))
2291 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2292 else
2293 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2296 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2297 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2298 SelectObject(screen_mem_dc, orig_bm);
2300 end:
2301 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2302 HeapFree(GetProcessHeap(), 0, scaled_info);
2303 HeapFree(GetProcessHeap(), 0, fix_info);
2304 end_close:
2305 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2307 return hbitmap;
2310 /**********************************************************************
2311 * LoadImageA (USER32.@)
2313 * See LoadImageW.
2315 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2316 INT desiredx, INT desiredy, UINT loadflags)
2318 HANDLE res;
2319 LPWSTR u_name;
2321 if (IS_INTRESOURCE(name))
2322 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2324 __TRY {
2325 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2326 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2327 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2329 __EXCEPT_PAGE_FAULT {
2330 SetLastError( ERROR_INVALID_PARAMETER );
2331 return 0;
2333 __ENDTRY
2334 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2335 HeapFree(GetProcessHeap(), 0, u_name);
2336 return res;
2340 /******************************************************************************
2341 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2343 * PARAMS
2344 * hinst [I] Handle of instance that contains image
2345 * name [I] Name of image
2346 * type [I] Type of image
2347 * desiredx [I] Desired width
2348 * desiredy [I] Desired height
2349 * loadflags [I] Load flags
2351 * RETURNS
2352 * Success: Handle to newly loaded image
2353 * Failure: NULL
2355 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2357 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2358 INT desiredx, INT desiredy, UINT loadflags )
2360 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2361 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2363 if (loadflags & LR_DEFAULTSIZE) {
2364 if (type == IMAGE_ICON) {
2365 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2366 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2367 } else if (type == IMAGE_CURSOR) {
2368 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2369 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2372 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2373 switch (type) {
2374 case IMAGE_BITMAP:
2375 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2377 case IMAGE_ICON:
2378 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2379 if (screen_dc)
2381 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2382 GetDeviceCaps(screen_dc, BITSPIXEL),
2383 FALSE, loadflags);
2385 break;
2387 case IMAGE_CURSOR:
2388 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2389 1, TRUE, loadflags);
2391 return 0;
2394 /******************************************************************************
2395 * CopyImage (USER32.@) Creates new image and copies attributes to it
2397 * PARAMS
2398 * hnd [I] Handle to image to copy
2399 * type [I] Type of image to copy
2400 * desiredx [I] Desired width of new image
2401 * desiredy [I] Desired height of new image
2402 * flags [I] Copy flags
2404 * RETURNS
2405 * Success: Handle to newly created image
2406 * Failure: NULL
2408 * BUGS
2409 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2410 * all other versions (95/2000/XP have been tested) ignore it.
2412 * NOTES
2413 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2414 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2415 * the copy will have the same depth as the screen.
2416 * The content of the image will only be copied if the bit depth of the
2417 * original image is compatible with the bit depth of the screen, or
2418 * if the source is a DIB section.
2419 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2421 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2422 INT desiredy, UINT flags )
2424 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2425 hnd, type, desiredx, desiredy, flags);
2427 switch (type)
2429 case IMAGE_BITMAP:
2431 HBITMAP res = NULL;
2432 DIBSECTION ds;
2433 int objSize;
2434 BITMAPINFO * bi;
2436 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2437 if (!objSize) return 0;
2438 if ((desiredx < 0) || (desiredy < 0)) return 0;
2440 if (flags & LR_COPYFROMRESOURCE)
2442 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2445 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2446 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2448 /* Allocate memory for a BITMAPINFOHEADER structure and a
2449 color table. The maximum number of colors in a color table
2450 is 256 which corresponds to a bitmap with depth 8.
2451 Bitmaps with higher depths don't have color tables. */
2452 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2453 if (!bi) return 0;
2455 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2456 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2457 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2458 bi->bmiHeader.biCompression = BI_RGB;
2460 if (flags & LR_CREATEDIBSECTION)
2462 /* Create a DIB section. LR_MONOCHROME is ignored */
2463 void * bits;
2464 HDC dc = CreateCompatibleDC(NULL);
2466 if (objSize == sizeof(DIBSECTION))
2468 /* The source bitmap is a DIB.
2469 Get its attributes to create an exact copy */
2470 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2473 /* Get the color table or the color masks */
2474 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2476 bi->bmiHeader.biWidth = desiredx;
2477 bi->bmiHeader.biHeight = desiredy;
2478 bi->bmiHeader.biSizeImage = 0;
2480 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2481 DeleteDC(dc);
2483 else
2485 /* Create a device-dependent bitmap */
2487 BOOL monochrome = (flags & LR_MONOCHROME);
2489 if (objSize == sizeof(DIBSECTION))
2491 /* The source bitmap is a DIB section.
2492 Get its attributes */
2493 HDC dc = CreateCompatibleDC(NULL);
2494 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2495 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2496 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2497 DeleteDC(dc);
2499 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2501 /* Look if the colors of the DIB are black and white */
2503 monochrome =
2504 (bi->bmiColors[0].rgbRed == 0xff
2505 && bi->bmiColors[0].rgbGreen == 0xff
2506 && bi->bmiColors[0].rgbBlue == 0xff
2507 && bi->bmiColors[0].rgbReserved == 0
2508 && bi->bmiColors[1].rgbRed == 0
2509 && bi->bmiColors[1].rgbGreen == 0
2510 && bi->bmiColors[1].rgbBlue == 0
2511 && bi->bmiColors[1].rgbReserved == 0)
2513 (bi->bmiColors[0].rgbRed == 0
2514 && bi->bmiColors[0].rgbGreen == 0
2515 && bi->bmiColors[0].rgbBlue == 0
2516 && bi->bmiColors[0].rgbReserved == 0
2517 && bi->bmiColors[1].rgbRed == 0xff
2518 && bi->bmiColors[1].rgbGreen == 0xff
2519 && bi->bmiColors[1].rgbBlue == 0xff
2520 && bi->bmiColors[1].rgbReserved == 0);
2523 else if (!monochrome)
2525 monochrome = ds.dsBm.bmBitsPixel == 1;
2528 if (monochrome)
2530 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2532 else
2534 HDC screenDC = GetDC(NULL);
2535 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2536 ReleaseDC(NULL, screenDC);
2540 if (res)
2542 /* Only copy the bitmap if it's a DIB section or if it's
2543 compatible to the screen */
2544 BOOL copyContents;
2546 if (objSize == sizeof(DIBSECTION))
2548 copyContents = TRUE;
2550 else
2552 HDC screenDC = GetDC(NULL);
2553 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2554 ReleaseDC(NULL, screenDC);
2556 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2559 if (copyContents)
2561 /* The source bitmap may already be selected in a device context,
2562 use GetDIBits/StretchDIBits and not StretchBlt */
2564 HDC dc;
2565 void * bits;
2567 dc = CreateCompatibleDC(NULL);
2569 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2570 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2571 bi->bmiHeader.biSizeImage = 0;
2572 bi->bmiHeader.biClrUsed = 0;
2573 bi->bmiHeader.biClrImportant = 0;
2575 /* Fill in biSizeImage */
2576 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2577 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2579 if (bits)
2581 HBITMAP oldBmp;
2583 /* Get the image bits of the source bitmap */
2584 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2586 /* Copy it to the destination bitmap */
2587 oldBmp = SelectObject(dc, res);
2588 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2589 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2590 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2591 SelectObject(dc, oldBmp);
2593 HeapFree(GetProcessHeap(), 0, bits);
2596 DeleteDC(dc);
2599 if (flags & LR_COPYDELETEORG)
2601 DeleteObject(hnd);
2604 HeapFree(GetProcessHeap(), 0, bi);
2605 return res;
2607 case IMAGE_ICON:
2608 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2609 case IMAGE_CURSOR:
2610 /* Should call CURSORICON_ExtCopy but more testing
2611 * needs to be done before we change this
2613 if (flags) FIXME("Flags are ignored\n");
2614 return CopyCursor(hnd);
2616 return 0;
2620 /******************************************************************************
2621 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2623 * RETURNS
2624 * Success: Handle to specified bitmap
2625 * Failure: NULL
2627 HBITMAP WINAPI LoadBitmapW(
2628 HINSTANCE instance, /* [in] Handle to application instance */
2629 LPCWSTR name) /* [in] Address of bitmap resource name */
2631 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2634 /**********************************************************************
2635 * LoadBitmapA (USER32.@)
2637 * See LoadBitmapW.
2639 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2641 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );