2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
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 #include "wine/port.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
48 WINE_DECLARE_DEBUG_CHANNEL(icon);
49 WINE_DECLARE_DEBUG_CHANNEL(resource);
62 } CURSORICONFILEDIRENTRY;
69 CURSORICONFILEDIRENTRY idEntries[1];
76 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
78 static struct list icon_cache = LIST_INIT( icon_cache );
80 /**********************************************************************
81 * User objects management
84 struct cursoricon_frame
86 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
87 HBITMAP color; /* color bitmap */
88 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
89 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
92 struct cursoricon_object
94 struct user_object obj; /* object header */
95 struct list entry; /* entry in shared icons list */
96 ULONG_PTR param; /* opaque param used by 16-bit code */
97 HMODULE module; /* module for icons loaded from resources */
98 LPWSTR resname; /* resource name for icons loaded from resources */
99 HRSRC rsrc; /* resource for shared icons */
100 BOOL is_icon; /* whether icon or cursor */
104 UINT num_frames; /* number of frames in the icon/cursor */
105 UINT num_steps; /* number of sequence steps in the icon/cursor */
106 UINT delay; /* global delay between frames (in jiffies) */
107 struct cursoricon_frame frames[1]; /* icon frame information */
110 static HICON alloc_icon_handle( UINT num_frames )
112 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
113 FIELD_OFFSET( struct cursoricon_object, frames[num_frames] ));
117 obj->num_steps = num_frames; /* changed later for some animated cursors */
118 obj->num_frames = num_frames;
119 return alloc_user_handle( &obj->obj, USER_ICON );
122 static struct cursoricon_object *get_icon_ptr( HICON handle )
124 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
125 if (obj == OBJ_OTHER_PROCESS)
127 WARN( "icon handle %p from other process\n", handle );
133 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
135 release_user_handle_ptr( ptr );
138 static BOOL free_icon_handle( HICON handle )
140 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
142 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
145 ULONG_PTR param = obj->param;
148 assert( !obj->rsrc ); /* shared icons can't be freed */
150 for (i=0; i<obj->num_frames; i++)
152 if (obj->frames[i].alpha) DeleteObject( obj->frames[i].alpha );
153 if (obj->frames[i].color) DeleteObject( obj->frames[i].color );
154 DeleteObject( obj->frames[i].mask );
156 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
157 HeapFree( GetProcessHeap(), 0, obj );
158 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
159 USER_Driver->pDestroyCursorIcon( handle );
165 ULONG_PTR get_icon_param( HICON handle )
168 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
170 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
174 release_user_handle_ptr( obj );
179 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
182 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
184 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
189 release_user_handle_ptr( obj );
195 /***********************************************************************
198 * Helper function to map a file to memory:
200 * [RETURN] ptr - pointer to mapped file
201 * [RETURN] filesize - pointer size of file to be stored if not NULL
203 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
205 HANDLE hFile, hMapping;
208 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
209 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
210 if (hFile != INVALID_HANDLE_VALUE)
212 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
215 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
216 CloseHandle( hMapping );
218 *filesize = GetFileSize( hFile, NULL );
220 CloseHandle( hFile );
226 /***********************************************************************
227 * get_dib_width_bytes
229 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
231 static int get_dib_width_bytes( int width, int depth )
237 case 1: words = (width + 31) / 32; break;
238 case 4: words = (width + 7) / 8; break;
239 case 8: words = (width + 3) / 4; break;
241 case 16: words = (width + 1) / 2; break;
242 case 24: words = (width * 3 + 3)/4; break;
244 WARN("(%d): Unsupported depth\n", depth );
253 /***********************************************************************
256 * Return the size of the bitmap info structure including color table.
258 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
260 unsigned int colors, size, masks = 0;
262 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
264 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
265 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
266 return sizeof(BITMAPCOREHEADER) + colors *
267 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
269 else /* assume BITMAPINFOHEADER */
271 colors = info->bmiHeader.biClrUsed;
272 if (colors > 256) /* buffer overflow otherwise */
274 if (!colors && (info->bmiHeader.biBitCount <= 8))
275 colors = 1 << info->bmiHeader.biBitCount;
276 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
277 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
278 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
283 /***********************************************************************
286 * Helper function to duplicate a bitmap.
288 static HBITMAP copy_bitmap( HBITMAP bitmap )
291 HBITMAP new_bitmap = 0;
294 if (!bitmap) return 0;
295 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
297 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
299 SelectObject( src, bitmap );
300 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
302 SelectObject( dst, new_bitmap );
303 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
312 /***********************************************************************
315 * Returns whether a DIB can be converted to a monochrome DDB.
317 * A DIB can be converted if its color table contains only black and
318 * white. Black must be the first color in the color table.
320 * Note : If the first color in the color table is white followed by
321 * black, we can't convert it to a monochrome DDB with
322 * SetDIBits, because black and white would be inverted.
324 static BOOL is_dib_monochrome( const BITMAPINFO* info )
326 if (info->bmiHeader.biBitCount != 1) return FALSE;
328 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
330 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
332 /* Check if the first color is black */
333 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
337 /* Check if the second color is white */
338 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
339 && (rgb->rgbtBlue == 0xff));
343 else /* assume BITMAPINFOHEADER */
345 const RGBQUAD *rgb = info->bmiColors;
347 /* Check if the first color is black */
348 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
349 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
353 /* Check if the second color is white */
354 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
355 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
361 /***********************************************************************
364 * Get the info from a bitmap header.
365 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
367 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
368 LONG *height, WORD *bpp, DWORD *compr )
370 if (header->biSize == sizeof(BITMAPCOREHEADER))
372 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
373 *width = core->bcWidth;
374 *height = core->bcHeight;
375 *bpp = core->bcBitCount;
379 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
380 header->biSize == sizeof(BITMAPV4HEADER) ||
381 header->biSize == sizeof(BITMAPV5HEADER))
383 *width = header->biWidth;
384 *height = header->biHeight;
385 *bpp = header->biBitCount;
386 *compr = header->biCompression;
389 WARN("unknown/wrong size (%u) for header\n", header->biSize);
393 /**********************************************************************
396 BOOL get_icon_size( HICON handle, SIZE *size )
398 struct cursoricon_object *info;
400 if (!(info = get_icon_ptr( handle ))) return FALSE;
401 size->cx = info->width;
402 size->cy = info->height;
403 release_icon_ptr( handle, info );
408 * The following macro functions account for the irregularities of
409 * accessing cursor and icon resources in files and resource entries.
411 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, int n,
412 int *width, int *height, int *bits );
414 /**********************************************************************
415 * CURSORICON_FindBestIcon
417 * Find the icon closest to the requested size and bit depth.
419 static int CURSORICON_FindBestIcon( LPCVOID dir, fnGetCIEntry get_entry,
420 int width, int height, int depth, UINT loadflags )
422 int i, cx, cy, bits, bestEntry = -1;
423 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
424 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
427 iTotalDiff = 0xFFFFFFFF;
428 iColorDiff = 0xFFFFFFFF;
430 if (loadflags & LR_DEFAULTSIZE)
432 if (!width) width = GetSystemMetrics( SM_CXICON );
433 if (!height) height = GetSystemMetrics( SM_CYICON );
435 else if (!width && !height)
437 /* use the size of the first entry */
438 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
442 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
444 iTempXDiff = abs(width - cx);
445 iTempYDiff = abs(height - cy);
447 if(iTotalDiff > (iTempXDiff + iTempYDiff))
451 iTotalDiff = iXDiff + iYDiff;
455 /* Find Best Colors for Best Fit */
456 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
458 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
460 iTempColorDiff = abs(depth - bits);
461 if(iColorDiff > iTempColorDiff)
464 iColorDiff = iTempColorDiff;
472 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
473 int *width, int *height, int *bits )
475 const CURSORICONDIR *resdir = dir;
476 const ICONRESDIR *icon;
478 if ( resdir->idCount <= n )
480 icon = &resdir->idEntries[n].ResInfo.icon;
481 *width = icon->bWidth;
482 *height = icon->bHeight;
483 *bits = resdir->idEntries[n].wBitCount;
487 /**********************************************************************
488 * CURSORICON_FindBestCursor
490 * Find the cursor closest to the requested size.
492 * FIXME: parameter 'color' ignored.
494 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
495 int width, int height, int depth, UINT loadflags )
497 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
499 if (loadflags & LR_DEFAULTSIZE)
501 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
502 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
504 else if (!width && !height)
506 /* use the first entry */
507 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
511 /* Double height to account for AND and XOR masks */
515 /* First find the largest one smaller than or equal to the requested size*/
517 maxwidth = maxheight = 0;
518 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
520 if ((cx <= width) && (cy <= height) &&
521 (cx > maxwidth) && (cy > maxheight))
528 if (bestEntry != -1) return bestEntry;
530 /* Now find the smallest one larger than the requested size */
532 maxwidth = maxheight = 255;
533 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
535 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
546 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
547 int *width, int *height, int *bits )
549 const CURSORICONDIR *resdir = dir;
550 const CURSORDIR *cursor;
552 if ( resdir->idCount <= n )
554 cursor = &resdir->idEntries[n].ResInfo.cursor;
555 *width = cursor->wWidth;
556 *height = cursor->wHeight;
557 *bits = resdir->idEntries[n].wBitCount;
561 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
562 int width, int height, int depth,
567 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
568 width, height, depth, loadflags );
571 return &dir->idEntries[n];
574 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
575 int width, int height, int depth,
578 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
579 width, height, depth, loadflags );
582 return &dir->idEntries[n];
585 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
586 int *width, int *height, int *bits )
588 const CURSORICONFILEDIR *filedir = dir;
589 const CURSORICONFILEDIRENTRY *entry;
590 const BITMAPINFOHEADER *info;
592 if ( filedir->idCount <= n )
594 entry = &filedir->idEntries[n];
595 /* FIXME: check against file size */
596 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
597 *width = entry->bWidth;
598 *height = entry->bHeight;
599 *bits = info->biBitCount;
603 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
604 int width, int height, int depth,
607 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
608 width, height, depth, loadflags );
611 return &dir->idEntries[n];
614 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
615 int width, int height, int depth,
618 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
619 width, height, depth, loadflags );
622 return &dir->idEntries[n];
625 /***********************************************************************
628 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
631 BOOL has_alpha = FALSE;
632 const unsigned char *ptr = bits;
634 if (info->bmiHeader.biBitCount != 32) return FALSE;
635 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
636 if ((has_alpha = (ptr[3] != 0))) break;
640 /***********************************************************************
641 * create_alpha_bitmap
643 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
645 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
646 const BITMAPINFO *src_info, const void *color_bits )
649 BITMAPINFO *info = NULL;
656 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
657 if (bm.bmBitsPixel != 32) return 0;
659 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
660 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
661 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
662 info->bmiHeader.biWidth = bm.bmWidth;
663 info->bmiHeader.biHeight = -bm.bmHeight;
664 info->bmiHeader.biPlanes = 1;
665 info->bmiHeader.biBitCount = 32;
666 info->bmiHeader.biCompression = BI_RGB;
667 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
668 info->bmiHeader.biXPelsPerMeter = 0;
669 info->bmiHeader.biYPelsPerMeter = 0;
670 info->bmiHeader.biClrUsed = 0;
671 info->bmiHeader.biClrImportant = 0;
672 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
676 SelectObject( hdc, alpha );
677 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
678 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
679 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
684 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
685 if (!bmi_has_alpha( info, bits ))
687 DeleteObject( alpha );
693 /* pre-multiply by alpha */
694 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
696 unsigned int alpha = ptr[3];
697 ptr[0] = ptr[0] * alpha / 255;
698 ptr[1] = ptr[1] * alpha / 255;
699 ptr[2] = ptr[2] * alpha / 255;
704 HeapFree( GetProcessHeap(), 0, info );
709 /***********************************************************************
710 * create_icon_bitmaps
712 * Create the color, mask and alpha bitmaps from the DIB info.
714 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
715 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
717 BOOL monochrome = is_dib_monochrome( bmi );
718 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
720 const void *color_bits, *mask_bits;
724 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
726 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
728 memcpy( info, bmi, size );
729 info->bmiHeader.biHeight /= 2;
731 color_bits = (const char*)bmi + size;
732 mask_bits = (const char*)color_bits +
733 get_dib_width_bytes( bmi->bmiHeader.biWidth,
734 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
739 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
742 /* copy color data into second half of mask bitmap */
743 SelectObject( hdc, *mask );
744 StretchDIBits( hdc, 0, height, width, height,
745 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
746 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
750 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
751 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
752 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
754 DeleteObject( *mask );
757 SelectObject( hdc, *color );
758 StretchDIBits( hdc, 0, 0, width, height,
759 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
760 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
762 if (bmi_has_alpha( info, color_bits ))
763 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
765 /* convert info to monochrome to copy the mask */
766 info->bmiHeader.biBitCount = 1;
767 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
769 RGBQUAD *rgb = info->bmiColors;
771 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
772 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
773 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
774 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
778 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
780 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
781 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
785 SelectObject( hdc, *mask );
786 StretchDIBits( hdc, 0, 0, width, height,
787 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
788 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
793 HeapFree( GetProcessHeap(), 0, info );
797 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
798 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
801 HBITMAP color = 0, mask = 0, alpha = 0;
804 /* Check bitmap header */
806 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
807 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
808 bmi->bmiHeader.biCompression != BI_RGB) )
810 WARN_(cursor)("\tinvalid resource bitmap header.\n");
814 if (cFlag & LR_DEFAULTSIZE)
816 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
817 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
821 if (!width) width = bmi->bmiHeader.biWidth;
822 if (!height) height = bmi->bmiHeader.biHeight/2;
824 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
825 (bmi->bmiHeader.biWidth != width);
827 /* Scale the hotspot */
830 hotspot.x = width / 2;
831 hotspot.y = height / 2;
835 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
836 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
839 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
840 if (!screen_dc) return 0;
842 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
844 hObj = alloc_icon_handle(1);
847 struct cursoricon_object *info = get_icon_ptr( hObj );
849 info->is_icon = bIcon;
850 info->module = module;
851 info->hotspot = hotspot;
853 info->height = height;
854 info->frames[0].delay = ~0;
855 info->frames[0].color = color;
856 info->frames[0].mask = mask;
857 info->frames[0].alpha = alpha;
858 if (!IS_INTRESOURCE(resname))
860 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
861 if (info->resname) strcpyW( info->resname, resname );
863 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
865 if (module && (cFlag & LR_SHARED))
868 list_add_head( &icon_cache, &info->entry );
870 release_icon_ptr( hObj, info );
871 USER_Driver->pCreateCursorIcon( hObj );
875 DeleteObject( color );
876 DeleteObject( alpha );
877 DeleteObject( mask );
883 /**********************************************************************
884 * .ANI cursor support
886 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
887 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
888 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
890 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
891 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
892 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
893 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
894 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
895 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
896 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
898 #define ANI_FLAG_ICON 0x1
899 #define ANI_FLAG_SEQUENCE 0x2
915 const unsigned char *data;
918 static void dump_ani_header( const ani_header *header )
920 TRACE(" header size: %d\n", header->header_size);
921 TRACE(" frames: %d\n", header->num_frames);
922 TRACE(" steps: %d\n", header->num_steps);
923 TRACE(" width: %d\n", header->width);
924 TRACE(" height: %d\n", header->height);
925 TRACE(" bpp: %d\n", header->bpp);
926 TRACE(" planes: %d\n", header->num_planes);
927 TRACE(" display rate: %d\n", header->display_rate);
928 TRACE(" flags: 0x%08x\n", header->flags);
950 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
952 const unsigned char *ptr = parent_chunk->data;
953 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
955 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
959 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
960 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
962 ptr += sizeof(DWORD);
963 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
964 ptr += sizeof(DWORD);
965 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
971 ptr += sizeof(DWORD);
972 ptr += (*(const DWORD *)ptr + 1) & ~1;
973 ptr += sizeof(DWORD);
981 * RIFF:'ACON' RIFF chunk
982 * |- CHUNK:'anih' Header
983 * |- CHUNK:'seq ' Sequence information (optional)
984 * \- LIST:'fram' Frame list
985 * |- CHUNK:icon Cursor frames
990 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
991 INT width, INT height, INT depth, UINT loadflags )
993 struct cursoricon_object *info;
994 DWORD *frame_rates = NULL;
995 ani_header header = {0};
999 riff_chunk_t root_chunk = { bits_size, bits };
1000 riff_chunk_t ACON_chunk = {0};
1001 riff_chunk_t anih_chunk = {0};
1002 riff_chunk_t fram_chunk = {0};
1003 riff_chunk_t rate_chunk = {0};
1004 const unsigned char *icon_chunk;
1005 const unsigned char *icon_data;
1007 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1009 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1010 if (!ACON_chunk.data)
1012 ERR("Failed to get root chunk.\n");
1016 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1017 if (!anih_chunk.data)
1019 ERR("Failed to get 'anih' chunk.\n");
1022 memcpy( &header, anih_chunk.data, sizeof(header) );
1023 dump_ani_header( &header );
1025 if (!(header.flags & ANI_FLAG_ICON))
1027 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1031 if (header.flags & ANI_FLAG_SEQUENCE)
1032 FIXME("Animated icon/cursor sequence data is not currently supported, frames may appear out of sequence.\n");
1034 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1035 if (rate_chunk.data && header.num_steps == header.num_frames)
1036 frame_rates = (DWORD *) rate_chunk.data;
1037 else if (rate_chunk.data && header.num_steps != 1)
1038 FIXME("Animated icon/cursor rate data for sequence-based cursors not supported.\n");
1040 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1041 if (!fram_chunk.data)
1043 ERR("Failed to get icon list.\n");
1047 cursor = alloc_icon_handle( header.num_frames );
1048 if (!cursor) return 0;
1050 info = get_icon_ptr( cursor );
1051 info->is_icon = FALSE;
1052 if (header.num_steps > header.num_frames)
1054 FIXME("More steps than frames and sequence-based cursors not yet supported.\n");
1055 info->num_steps = header.num_frames;
1058 info->num_steps = header.num_steps;
1060 /* The .ANI stores the display rate in jiffies (1/60s) */
1061 info->delay = header.display_rate;
1063 icon_chunk = fram_chunk.data;
1064 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1065 for (i=0; i<header.num_frames; i++)
1067 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1068 struct cursoricon_frame *frame = &info->frames[i];
1069 const CURSORICONFILEDIRENTRY *entry;
1070 const BITMAPINFO *bmi;
1072 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1073 width, height, depth, loadflags );
1075 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1076 info->hotspot.x = entry->xHotspot;
1077 info->hotspot.y = entry->yHotspot;
1078 if (!header.width || !header.height)
1080 header.width = entry->bWidth;
1081 header.height = entry->bHeight;
1084 frame->delay = frame_rates[i];
1088 /* Grab a frame from the animation */
1089 if (!create_icon_bitmaps( bmi, header.width, header.height,
1090 &frame->color, &frame->mask, &frame->alpha ))
1092 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1096 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1097 info->num_frames = 0;
1098 release_icon_ptr( cursor, info );
1099 free_icon_handle( cursor );
1105 /* Advance to the next chunk */
1106 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1107 icon_data = icon_chunk + (2 * sizeof(DWORD));
1110 /* There was an error but we at least decoded the first frame, so just use that frame */
1113 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1114 for (i=1; i<info->num_frames; i++)
1116 if (info->frames[i].mask) DeleteObject( info->frames[i].mask );
1117 if (info->frames[i].color) DeleteObject( info->frames[i].color );
1118 if (info->frames[i].alpha) DeleteObject( info->frames[i].alpha );
1120 info->num_frames = 1;
1121 info->num_steps = 1;
1124 info->width = header.width;
1125 info->height = header.height;
1126 release_icon_ptr( cursor, info );
1132 /**********************************************************************
1133 * CreateIconFromResourceEx (USER32.@)
1135 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1136 * with cbSize parameter as well.
1138 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1139 BOOL bIcon, DWORD dwVersion,
1140 INT width, INT height,
1146 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1147 bits, cbSize, dwVersion, width, height,
1148 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1150 if (!bits) return 0;
1152 if (dwVersion == 0x00020000)
1154 FIXME_(cursor)("\t2.xx resources are not supported\n");
1158 /* Check if the resource is an animated icon/cursor */
1159 if (!memcmp(bits, "RIFF", 4))
1160 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height, 0 /* default depth */, cFlag );
1164 hotspot.x = width / 2;
1165 hotspot.y = height / 2;
1166 bmi = (BITMAPINFO *)bits;
1168 else /* get the hotspot */
1170 SHORT *pt = (SHORT *)bits;
1173 bmi = (BITMAPINFO *)(pt + 2);
1176 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1180 /**********************************************************************
1181 * CreateIconFromResource (USER32.@)
1183 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1184 BOOL bIcon, DWORD dwVersion)
1186 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1190 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1191 INT width, INT height, INT depth,
1192 BOOL fCursor, UINT loadflags)
1194 const CURSORICONFILEDIRENTRY *entry;
1195 const CURSORICONFILEDIR *dir;
1201 TRACE("loading %s\n", debugstr_w( filename ));
1203 bits = map_fileW( filename, &filesize );
1207 /* Check for .ani. */
1208 if (memcmp( bits, "RIFF", 4 ) == 0)
1210 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1214 dir = (const CURSORICONFILEDIR*) bits;
1215 if ( filesize < sizeof(*dir) )
1218 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1222 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1224 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1229 /* check that we don't run off the end of the file */
1230 if ( entry->dwDIBOffset > filesize )
1232 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1235 hotspot.x = entry->xHotspot;
1236 hotspot.y = entry->yHotspot;
1237 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1238 hotspot, !fCursor, width, height, loadflags );
1240 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1241 UnmapViewOfFile( bits );
1245 /**********************************************************************
1248 * Load a cursor or icon from resource or file.
1250 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1251 INT width, INT height, INT depth,
1252 BOOL fCursor, UINT loadflags)
1257 const CURSORICONDIR *dir;
1258 const CURSORICONDIRENTRY *dirEntry;
1263 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1264 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1266 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1267 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1269 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1271 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1272 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1274 /* Get directory resource ID */
1276 if (!(hRsrc = FindResourceW( hInstance, name,
1277 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1280 /* Find the best entry in the directory */
1282 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1283 if (!(dir = LockResource( handle ))) return 0;
1285 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1287 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1288 if (!dirEntry) return 0;
1289 wResId = dirEntry->wResId;
1290 FreeResource( handle );
1292 /* Load the resource */
1294 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1295 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1297 /* If shared icon, check whether it was already loaded */
1298 if (loadflags & LR_SHARED)
1300 struct cursoricon_object *ptr;
1303 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1305 if (ptr->module != hInstance) continue;
1306 if (ptr->rsrc != hRsrc) continue;
1307 hIcon = ptr->obj.handle;
1311 if (hIcon) return hIcon;
1314 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1315 bits = LockResource( handle );
1319 hotspot.x = width / 2;
1320 hotspot.y = height / 2;
1322 else /* get the hotspot */
1324 SHORT *pt = (SHORT *)bits;
1327 bits += 2 * sizeof(SHORT);
1329 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1330 hotspot, !fCursor, width, height, loadflags );
1331 FreeResource( handle );
1336 /***********************************************************************
1337 * CreateCursor (USER32.@)
1339 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1340 INT xHotSpot, INT yHotSpot,
1341 INT nWidth, INT nHeight,
1342 LPCVOID lpANDbits, LPCVOID lpXORbits )
1347 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1348 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1351 info.xHotspot = xHotSpot;
1352 info.yHotspot = yHotSpot;
1353 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1354 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1355 hCursor = CreateIconIndirect( &info );
1356 DeleteObject( info.hbmMask );
1357 DeleteObject( info.hbmColor );
1362 /***********************************************************************
1363 * CreateIcon (USER32.@)
1365 * Creates an icon based on the specified bitmaps. The bitmaps must be
1366 * provided in a device dependent format and will be resized to
1367 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1368 * depth. The provided bitmaps must be top-down bitmaps.
1369 * Although Windows does not support 15bpp(*) this API must support it
1370 * for Winelib applications.
1372 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1376 * Success: handle to an icon
1379 * FIXME: Do we need to resize the bitmaps?
1381 HICON WINAPI CreateIcon(
1382 HINSTANCE hInstance, /* [in] the application's hInstance */
1383 INT nWidth, /* [in] the width of the provided bitmaps */
1384 INT nHeight, /* [in] the height of the provided bitmaps */
1385 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1386 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1387 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1388 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1393 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1394 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1397 iinfo.xHotspot = nWidth / 2;
1398 iinfo.yHotspot = nHeight / 2;
1399 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1400 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1402 hIcon = CreateIconIndirect( &iinfo );
1404 DeleteObject( iinfo.hbmMask );
1405 DeleteObject( iinfo.hbmColor );
1411 /***********************************************************************
1412 * CopyIcon (USER32.@)
1414 HICON WINAPI CopyIcon( HICON hIcon )
1416 struct cursoricon_object *ptrOld, *ptrNew;
1419 if (!(ptrOld = get_icon_ptr( hIcon )))
1421 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1424 if ((hNew = alloc_icon_handle(1)))
1426 ptrNew = get_icon_ptr( hNew );
1427 ptrNew->is_icon = ptrOld->is_icon;
1428 ptrNew->width = ptrOld->width;
1429 ptrNew->height = ptrOld->height;
1430 ptrNew->hotspot = ptrOld->hotspot;
1431 ptrNew->frames[0].delay = ptrOld->frames[0].delay;
1432 ptrNew->frames[0].mask = copy_bitmap( ptrOld->frames[0].mask );
1433 ptrNew->frames[0].color = copy_bitmap( ptrOld->frames[0].color );
1434 ptrNew->frames[0].alpha = copy_bitmap( ptrOld->frames[0].alpha );
1435 release_icon_ptr( hNew, ptrNew );
1437 release_icon_ptr( hIcon, ptrOld );
1438 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1443 /***********************************************************************
1444 * DestroyIcon (USER32.@)
1446 BOOL WINAPI DestroyIcon( HICON hIcon )
1449 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1451 TRACE_(icon)("%p\n", hIcon );
1455 BOOL shared = (obj->rsrc != NULL);
1456 release_icon_ptr( hIcon, obj );
1457 ret = (GetCursor() != hIcon);
1458 if (!shared) free_icon_handle( hIcon );
1464 /***********************************************************************
1465 * DestroyCursor (USER32.@)
1467 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1469 return DestroyIcon( hCursor );
1472 /***********************************************************************
1473 * DrawIcon (USER32.@)
1475 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1477 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1480 /***********************************************************************
1481 * SetCursor (USER32.@)
1483 * Set the cursor shape.
1486 * A handle to the previous cursor shape.
1488 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1490 struct cursoricon_object *obj;
1495 TRACE("%p\n", hCursor);
1497 SERVER_START_REQ( set_cursor )
1499 req->flags = SET_CURSOR_HANDLE;
1500 req->handle = wine_server_user_handle( hCursor );
1501 if ((ret = !wine_server_call_err( req )))
1503 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1504 show_count = reply->prev_count;
1511 /* Change the cursor shape only if it is visible */
1512 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1514 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1515 release_icon_ptr( hOldCursor, obj );
1519 /***********************************************************************
1520 * ShowCursor (USER32.@)
1522 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1525 int increment = bShow ? 1 : -1;
1528 SERVER_START_REQ( set_cursor )
1530 req->flags = SET_CURSOR_COUNT;
1531 req->show_count = increment;
1532 wine_server_call( req );
1533 cursor = wine_server_ptr_handle( reply->prev_handle );
1534 count = reply->prev_count + increment;
1538 TRACE("%d, count=%d\n", bShow, count );
1540 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1541 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1546 /***********************************************************************
1547 * GetCursor (USER32.@)
1549 HCURSOR WINAPI GetCursor(void)
1553 SERVER_START_REQ( set_cursor )
1556 wine_server_call( req );
1557 ret = wine_server_ptr_handle( reply->prev_handle );
1564 /***********************************************************************
1565 * ClipCursor (USER32.@)
1567 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1572 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1574 SERVER_START_REQ( set_cursor )
1576 req->flags = SET_CURSOR_CLIP;
1579 req->clip.left = rect->left;
1580 req->clip.top = rect->top;
1581 req->clip.right = rect->right;
1582 req->clip.bottom = rect->bottom;
1584 if ((ret = !wine_server_call( req )))
1586 new_rect.left = reply->new_clip.left;
1587 new_rect.top = reply->new_clip.top;
1588 new_rect.right = reply->new_clip.right;
1589 new_rect.bottom = reply->new_clip.bottom;
1593 if (ret) USER_Driver->pClipCursor( &new_rect );
1598 /***********************************************************************
1599 * GetClipCursor (USER32.@)
1601 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1605 if (!rect) return FALSE;
1607 SERVER_START_REQ( set_cursor )
1610 if ((ret = !wine_server_call( req )))
1612 rect->left = reply->new_clip.left;
1613 rect->top = reply->new_clip.top;
1614 rect->right = reply->new_clip.right;
1615 rect->bottom = reply->new_clip.bottom;
1623 /***********************************************************************
1624 * SetSystemCursor (USER32.@)
1626 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1628 FIXME("(%p,%08x),stub!\n", hcur, id);
1633 /**********************************************************************
1634 * LookupIconIdFromDirectoryEx (USER32.@)
1636 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1637 INT width, INT height, UINT cFlag )
1639 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1641 if( dir && !dir->idReserved && (dir->idType & 3) )
1643 const CURSORICONDIRENTRY* entry;
1645 const HDC hdc = GetDC(0);
1646 const int depth = (cFlag & LR_MONOCHROME) ?
1647 1 : GetDeviceCaps(hdc, BITSPIXEL);
1651 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1653 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1655 if( entry ) retVal = entry->wResId;
1657 else WARN_(cursor)("invalid resource directory\n");
1661 /**********************************************************************
1662 * LookupIconIdFromDirectory (USER32.@)
1664 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1666 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1669 /***********************************************************************
1670 * LoadCursorW (USER32.@)
1672 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1674 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1676 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1677 LR_SHARED | LR_DEFAULTSIZE );
1680 /***********************************************************************
1681 * LoadCursorA (USER32.@)
1683 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1685 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1687 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1688 LR_SHARED | LR_DEFAULTSIZE );
1691 /***********************************************************************
1692 * LoadCursorFromFileW (USER32.@)
1694 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1696 TRACE("%s\n", debugstr_w(name));
1698 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1699 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1702 /***********************************************************************
1703 * LoadCursorFromFileA (USER32.@)
1705 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1707 TRACE("%s\n", debugstr_a(name));
1709 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1710 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1713 /***********************************************************************
1714 * LoadIconW (USER32.@)
1716 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1718 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1720 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1721 LR_SHARED | LR_DEFAULTSIZE );
1724 /***********************************************************************
1725 * LoadIconA (USER32.@)
1727 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1729 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1731 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1732 LR_SHARED | LR_DEFAULTSIZE );
1735 /**********************************************************************
1736 * GetCursorFrameInfo (USER32.@)
1738 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD unk1, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1740 struct cursoricon_object *ptr;
1743 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1745 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1747 FIXME("semi-stub! %p => %d %d %p %p\n", hCursor, unk1, istep, rate_jiffies, num_steps);
1749 /* Important Note: Sequences are not currently supported, so this implementation
1750 * will not properly handle all cases. */
1751 if (istep < ptr->num_steps || ptr->num_frames == 1)
1754 if (ptr->num_frames == 1)
1761 if (ptr->num_steps == 1)
1764 *num_steps = ptr->num_steps;
1765 /* If this specific frame does not have a delay then use the global delay */
1766 if (ptr->frames[istep].delay == ~0)
1767 *rate_jiffies = ptr->delay;
1769 *rate_jiffies = ptr->frames[istep].delay;
1773 release_icon_ptr( hCursor, ptr );
1778 /**********************************************************************
1779 * GetIconInfo (USER32.@)
1781 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1785 infoW.cbSize = sizeof(infoW);
1786 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1787 iconinfo->fIcon = infoW.fIcon;
1788 iconinfo->xHotspot = infoW.xHotspot;
1789 iconinfo->yHotspot = infoW.yHotspot;
1790 iconinfo->hbmColor = infoW.hbmColor;
1791 iconinfo->hbmMask = infoW.hbmMask;
1795 /**********************************************************************
1796 * GetIconInfoExA (USER32.@)
1798 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1802 if (info->cbSize != sizeof(*info))
1804 SetLastError( ERROR_INVALID_PARAMETER );
1807 infoW.cbSize = sizeof(infoW);
1808 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1809 info->fIcon = infoW.fIcon;
1810 info->xHotspot = infoW.xHotspot;
1811 info->yHotspot = infoW.yHotspot;
1812 info->hbmColor = infoW.hbmColor;
1813 info->hbmMask = infoW.hbmMask;
1814 info->wResID = infoW.wResID;
1815 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1816 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1820 /**********************************************************************
1821 * GetIconInfoExW (USER32.@)
1823 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1825 struct cursoricon_object *ptr;
1829 if (info->cbSize != sizeof(*info))
1831 SetLastError( ERROR_INVALID_PARAMETER );
1834 if (!(ptr = get_icon_ptr( icon )))
1836 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1840 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1842 info->fIcon = ptr->is_icon;
1843 info->xHotspot = ptr->hotspot.x;
1844 info->yHotspot = ptr->hotspot.y;
1845 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1846 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1848 info->szModName[0] = 0;
1849 info->szResName[0] = 0;
1852 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1853 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1855 if (!info->hbmMask || (!info->hbmColor && ptr->frames[0].color))
1857 DeleteObject( info->hbmMask );
1858 DeleteObject( info->hbmColor );
1861 module = ptr->module;
1862 release_icon_ptr( icon, ptr );
1863 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
1867 /* copy an icon bitmap, even when it can't be selected into a DC */
1868 /* helper for CreateIconIndirect */
1869 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1870 HBITMAP src, int width, int height )
1872 HDC hdc = CreateCompatibleDC( 0 );
1874 if (!SelectObject( hdc, src )) /* do it the hard way */
1879 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1880 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1881 info->bmiHeader.biWidth = width;
1882 info->bmiHeader.biHeight = height;
1883 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1884 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1885 info->bmiHeader.biCompression = BI_RGB;
1886 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1887 info->bmiHeader.biXPelsPerMeter = 0;
1888 info->bmiHeader.biYPelsPerMeter = 0;
1889 info->bmiHeader.biClrUsed = 0;
1890 info->bmiHeader.biClrImportant = 0;
1891 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1892 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1893 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1894 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1896 HeapFree( GetProcessHeap(), 0, bits );
1897 HeapFree( GetProcessHeap(), 0, info );
1899 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1904 /**********************************************************************
1905 * CreateIconIndirect (USER32.@)
1907 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1909 BITMAP bmpXor, bmpAnd;
1911 HBITMAP color = 0, mask;
1915 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1916 iconinfo->hbmColor, iconinfo->hbmMask,
1917 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1919 if (!iconinfo->hbmMask) return 0;
1921 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1922 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1923 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1924 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1926 if (iconinfo->hbmColor)
1928 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1929 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1930 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1931 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1933 width = bmpXor.bmWidth;
1934 height = bmpXor.bmHeight;
1935 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1937 color = CreateCompatibleBitmap( screen_dc, width, height );
1938 mask = CreateBitmap( width, height, 1, 1, NULL );
1940 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1944 width = bmpAnd.bmWidth;
1945 height = bmpAnd.bmHeight;
1946 mask = CreateBitmap( width, height, 1, 1, NULL );
1949 hdc = CreateCompatibleDC( 0 );
1950 SelectObject( hdc, mask );
1951 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1955 SelectObject( hdc, color );
1956 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1958 else if (iconinfo->hbmColor)
1960 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1966 hObj = alloc_icon_handle(1);
1969 struct cursoricon_object *info = get_icon_ptr( hObj );
1971 info->is_icon = iconinfo->fIcon;
1972 info->width = width;
1973 info->height = height;
1974 info->frames[0].delay = ~0;
1975 info->frames[0].color = color;
1976 info->frames[0].mask = mask;
1977 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1980 info->hotspot.x = width / 2;
1981 info->hotspot.y = height / 2;
1985 info->hotspot.x = iconinfo->xHotspot;
1986 info->hotspot.y = iconinfo->yHotspot;
1989 release_icon_ptr( hObj, info );
1990 USER_Driver->pCreateCursorIcon( hObj );
1995 /******************************************************************************
1996 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1999 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2002 * hdc [I] Handle to device context
2003 * x0 [I] X coordinate of upper left corner
2004 * y0 [I] Y coordinate of upper left corner
2005 * hIcon [I] Handle to icon to draw
2006 * cxWidth [I] Width of icon
2007 * cyWidth [I] Height of icon
2008 * istep [I] Index of frame in animated cursor
2009 * hbr [I] Handle to background brush
2010 * flags [I] Icon-drawing flags
2016 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2017 INT cxWidth, INT cyWidth, UINT istep,
2018 HBRUSH hbr, UINT flags )
2020 struct cursoricon_object *ptr;
2021 HDC hdc_dest, hMemDC;
2022 BOOL result = FALSE, DoOffscreen;
2024 COLORREF oldFg, oldBg;
2025 INT x, y, nStretchMode;
2027 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2028 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2030 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2031 if (istep >= ptr->num_steps)
2033 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2034 release_icon_ptr( hIcon, ptr );
2037 if (!(hMemDC = CreateCompatibleDC( hdc )))
2039 release_icon_ptr( hIcon, ptr );
2043 if (flags & DI_NOMIRROR)
2044 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2046 /* Calculate the size of the destination image. */
2049 if (flags & DI_DEFAULTSIZE)
2050 cxWidth = GetSystemMetrics (SM_CXICON);
2052 cxWidth = ptr->width;
2056 if (flags & DI_DEFAULTSIZE)
2057 cyWidth = GetSystemMetrics (SM_CYICON);
2059 cyWidth = ptr->height;
2062 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2072 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2073 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2075 DeleteDC( hdc_dest );
2078 SelectObject(hdc_dest, hB_off);
2079 FillRect(hdc_dest, &r, hbr);
2089 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2091 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2092 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2094 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
2096 BOOL is_mono = FALSE;
2098 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2101 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2102 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
2106 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2107 SelectObject( hMemDC, ptr->frames[istep].alpha );
2108 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2109 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
2113 if (flags & DI_MASK)
2115 SelectObject( hMemDC, ptr->frames[istep].mask );
2116 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2117 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
2120 if (flags & DI_IMAGE)
2122 if (ptr->frames[istep].color)
2124 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2125 SelectObject( hMemDC, ptr->frames[istep].color );
2126 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2127 hMemDC, 0, 0, ptr->width, ptr->height, rop );
2131 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2132 SelectObject( hMemDC, ptr->frames[istep].mask );
2133 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2134 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
2139 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2141 SetTextColor( hdc, oldFg );
2142 SetBkColor( hdc, oldBg );
2143 SetStretchBltMode (hdc, nStretchMode);
2145 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2146 if (hB_off) DeleteObject(hB_off);
2149 release_icon_ptr( hIcon, ptr );
2153 /***********************************************************************
2154 * DIB_FixColorsToLoadflags
2156 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2159 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2162 COLORREF c_W, c_S, c_F, c_L, c_C;
2171 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2173 WARN_(resource)("Invalid bitmap\n");
2177 if (bpp > 8) return;
2179 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2187 colors = bmi->bmiHeader.biClrUsed;
2188 if (colors > 256) colors = 256;
2189 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2192 c_W = GetSysColor(COLOR_WINDOW);
2193 c_S = GetSysColor(COLOR_3DSHADOW);
2194 c_F = GetSysColor(COLOR_3DFACE);
2195 c_L = GetSysColor(COLOR_3DLIGHT);
2197 if (loadflags & LR_LOADTRANSPARENT) {
2199 case 1: pix = pix >> 7; break;
2200 case 4: pix = pix >> 4; break;
2203 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2206 if (pix >= colors) {
2207 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2210 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2211 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2212 ptr->rgbBlue = GetBValue(c_W);
2213 ptr->rgbGreen = GetGValue(c_W);
2214 ptr->rgbRed = GetRValue(c_W);
2216 if (loadflags & LR_LOADMAP3DCOLORS)
2217 for (i=0; i<colors; i++) {
2218 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2219 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2220 if (c_C == RGB(128, 128, 128)) {
2221 ptr->rgbRed = GetRValue(c_S);
2222 ptr->rgbGreen = GetGValue(c_S);
2223 ptr->rgbBlue = GetBValue(c_S);
2224 } else if (c_C == RGB(192, 192, 192)) {
2225 ptr->rgbRed = GetRValue(c_F);
2226 ptr->rgbGreen = GetGValue(c_F);
2227 ptr->rgbBlue = GetBValue(c_F);
2228 } else if (c_C == RGB(223, 223, 223)) {
2229 ptr->rgbRed = GetRValue(c_L);
2230 ptr->rgbGreen = GetGValue(c_L);
2231 ptr->rgbBlue = GetBValue(c_L);
2237 /**********************************************************************
2240 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2241 INT desiredx, INT desiredy, UINT loadflags )
2243 HBITMAP hbitmap = 0, orig_bm;
2247 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2251 LONG width, height, new_width, new_height;
2253 DWORD compr_dummy, offbits = 0;
2255 HDC screen_mem_dc = NULL;
2257 if (!(loadflags & LR_LOADFROMFILE))
2261 /* OEM bitmap: try to load the resource from user32.dll */
2262 instance = user32_module;
2265 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2266 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2268 if ((info = LockResource( handle )) == NULL) return 0;
2272 BITMAPFILEHEADER * bmfh;
2274 if (!(ptr = map_fileW( name, NULL ))) return 0;
2275 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2276 bmfh = (BITMAPFILEHEADER *)ptr;
2277 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2279 WARN("Invalid/unsupported bitmap format!\n");
2282 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2285 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2286 &bpp_dummy, &compr_dummy);
2289 WARN("Invalid bitmap format!\n");
2293 size = bitmap_info_size(info, DIB_RGB_COLORS);
2294 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2295 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2297 if (!fix_info || !scaled_info) goto end;
2298 memcpy(fix_info, info, size);
2300 pix = *((LPBYTE)info + size);
2301 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2303 memcpy(scaled_info, fix_info, size);
2306 new_width = desiredx;
2311 new_height = height > 0 ? desiredy : -desiredy;
2313 new_height = height;
2317 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2318 core->bcWidth = new_width;
2319 core->bcHeight = new_height;
2323 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2324 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2325 WARN("Broken BitmapInfoHeader!\n");
2329 scaled_info->bmiHeader.biWidth = new_width;
2330 scaled_info->bmiHeader.biHeight = new_height;
2333 if (new_height < 0) new_height = -new_height;
2335 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2336 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2338 bits = (char *)info + (offbits ? offbits : size);
2340 if (loadflags & LR_CREATEDIBSECTION)
2342 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2343 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2347 if (is_dib_monochrome(fix_info))
2348 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2350 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2353 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2354 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2355 SelectObject(screen_mem_dc, orig_bm);
2358 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2359 HeapFree(GetProcessHeap(), 0, scaled_info);
2360 HeapFree(GetProcessHeap(), 0, fix_info);
2361 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2366 /**********************************************************************
2367 * LoadImageA (USER32.@)
2371 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2372 INT desiredx, INT desiredy, UINT loadflags)
2377 if (IS_INTRESOURCE(name))
2378 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2381 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2382 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2383 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2385 __EXCEPT_PAGE_FAULT {
2386 SetLastError( ERROR_INVALID_PARAMETER );
2390 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2391 HeapFree(GetProcessHeap(), 0, u_name);
2396 /******************************************************************************
2397 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2400 * hinst [I] Handle of instance that contains image
2401 * name [I] Name of image
2402 * type [I] Type of image
2403 * desiredx [I] Desired width
2404 * desiredy [I] Desired height
2405 * loadflags [I] Load flags
2408 * Success: Handle to newly loaded image
2411 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2413 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2414 INT desiredx, INT desiredy, UINT loadflags )
2416 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2417 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2419 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2422 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2425 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2428 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2429 GetDeviceCaps(screen_dc, BITSPIXEL),
2435 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2436 1, TRUE, loadflags);
2441 /******************************************************************************
2442 * CopyImage (USER32.@) Creates new image and copies attributes to it
2445 * hnd [I] Handle to image to copy
2446 * type [I] Type of image to copy
2447 * desiredx [I] Desired width of new image
2448 * desiredy [I] Desired height of new image
2449 * flags [I] Copy flags
2452 * Success: Handle to newly created image
2456 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2457 * all other versions (95/2000/XP have been tested) ignore it.
2460 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2461 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2462 * the copy will have the same depth as the screen.
2463 * The content of the image will only be copied if the bit depth of the
2464 * original image is compatible with the bit depth of the screen, or
2465 * if the source is a DIB section.
2466 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2468 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2469 INT desiredy, UINT flags )
2471 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2472 hnd, type, desiredx, desiredy, flags);
2483 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2484 if (!objSize) return 0;
2485 if ((desiredx < 0) || (desiredy < 0)) return 0;
2487 if (flags & LR_COPYFROMRESOURCE)
2489 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2492 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2493 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2495 /* Allocate memory for a BITMAPINFOHEADER structure and a
2496 color table. The maximum number of colors in a color table
2497 is 256 which corresponds to a bitmap with depth 8.
2498 Bitmaps with higher depths don't have color tables. */
2499 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2502 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2503 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2504 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2505 bi->bmiHeader.biCompression = BI_RGB;
2507 if (flags & LR_CREATEDIBSECTION)
2509 /* Create a DIB section. LR_MONOCHROME is ignored */
2511 HDC dc = CreateCompatibleDC(NULL);
2513 if (objSize == sizeof(DIBSECTION))
2515 /* The source bitmap is a DIB.
2516 Get its attributes to create an exact copy */
2517 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2520 /* Get the color table or the color masks */
2521 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2523 bi->bmiHeader.biWidth = desiredx;
2524 bi->bmiHeader.biHeight = desiredy;
2525 bi->bmiHeader.biSizeImage = 0;
2527 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2532 /* Create a device-dependent bitmap */
2534 BOOL monochrome = (flags & LR_MONOCHROME);
2536 if (objSize == sizeof(DIBSECTION))
2538 /* The source bitmap is a DIB section.
2539 Get its attributes */
2540 HDC dc = CreateCompatibleDC(NULL);
2541 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2542 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2543 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2546 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2548 /* Look if the colors of the DIB are black and white */
2551 (bi->bmiColors[0].rgbRed == 0xff
2552 && bi->bmiColors[0].rgbGreen == 0xff
2553 && bi->bmiColors[0].rgbBlue == 0xff
2554 && bi->bmiColors[0].rgbReserved == 0
2555 && bi->bmiColors[1].rgbRed == 0
2556 && bi->bmiColors[1].rgbGreen == 0
2557 && bi->bmiColors[1].rgbBlue == 0
2558 && bi->bmiColors[1].rgbReserved == 0)
2560 (bi->bmiColors[0].rgbRed == 0
2561 && bi->bmiColors[0].rgbGreen == 0
2562 && bi->bmiColors[0].rgbBlue == 0
2563 && bi->bmiColors[0].rgbReserved == 0
2564 && bi->bmiColors[1].rgbRed == 0xff
2565 && bi->bmiColors[1].rgbGreen == 0xff
2566 && bi->bmiColors[1].rgbBlue == 0xff
2567 && bi->bmiColors[1].rgbReserved == 0);
2570 else if (!monochrome)
2572 monochrome = ds.dsBm.bmBitsPixel == 1;
2577 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2581 HDC screenDC = GetDC(NULL);
2582 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2583 ReleaseDC(NULL, screenDC);
2589 /* Only copy the bitmap if it's a DIB section or if it's
2590 compatible to the screen */
2593 if (objSize == sizeof(DIBSECTION))
2595 copyContents = TRUE;
2599 HDC screenDC = GetDC(NULL);
2600 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2601 ReleaseDC(NULL, screenDC);
2603 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2608 /* The source bitmap may already be selected in a device context,
2609 use GetDIBits/StretchDIBits and not StretchBlt */
2614 dc = CreateCompatibleDC(NULL);
2616 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2617 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2618 bi->bmiHeader.biSizeImage = 0;
2619 bi->bmiHeader.biClrUsed = 0;
2620 bi->bmiHeader.biClrImportant = 0;
2622 /* Fill in biSizeImage */
2623 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2624 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2630 /* Get the image bits of the source bitmap */
2631 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2633 /* Copy it to the destination bitmap */
2634 oldBmp = SelectObject(dc, res);
2635 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2636 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2637 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2638 SelectObject(dc, oldBmp);
2640 HeapFree(GetProcessHeap(), 0, bits);
2646 if (flags & LR_COPYDELETEORG)
2651 HeapFree(GetProcessHeap(), 0, bi);
2657 struct cursoricon_object *icon;
2659 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2661 if (flags & LR_DEFAULTSIZE)
2663 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2664 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2667 if (!(icon = get_icon_ptr( hnd ))) return 0;
2669 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2670 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2671 type == IMAGE_CURSOR, flags );
2673 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2674 release_icon_ptr( hnd, icon );
2676 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2684 /******************************************************************************
2685 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2688 * Success: Handle to specified bitmap
2691 HBITMAP WINAPI LoadBitmapW(
2692 HINSTANCE instance, /* [in] Handle to application instance */
2693 LPCWSTR name) /* [in] Address of bitmap resource name */
2695 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2698 /**********************************************************************
2699 * LoadBitmapA (USER32.@)
2703 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2705 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );