2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
29 * Cursors and icons are stored in a global heap block, with the
32 * CURSORICONINFO info;
36 * The bits structures are in the format of a device-dependent bitmap.
38 * This layout is very sub-optimal, as the bitmap bits are stored in
39 * the X client instead of in the server like other bitmaps; however,
40 * some programs (notably Paint Brush) expect to be able to manipulate
41 * the bits directly :-(
43 * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
47 #include "wine/port.h"
60 #include "wine/winbase16.h"
61 #include "wine/winuser16.h"
62 #include "wine/exception.h"
63 #include "wine/debug.h"
64 #include "user_private.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
67 WINE_DECLARE_DEBUG_CHANNEL(icon
);
68 WINE_DECLARE_DEBUG_CHANNEL(resource
);
81 } CURSORICONFILEDIRENTRY
;
88 CURSORICONFILEDIRENTRY idEntries
[1];
93 #define CID_RESOURCE 0x0001
94 #define CID_WIN32 0x0004
95 #define CID_NONSHARED 0x0008
97 static RECT CURSOR_ClipRect
; /* Cursor clipping rect */
101 static const WCHAR DISPLAYW
[] = {'D','I','S','P','L','A','Y',0};
103 /**********************************************************************
104 * ICONCACHE for cursors/icons loaded with LR_SHARED.
106 * FIXME: This should not be allocated on the system heap, but on a
107 * subsystem-global heap (i.e. one for all Win16 processes,
108 * and one for each Win32 process).
110 typedef struct tagICONCACHE
112 struct tagICONCACHE
*next
;
123 static ICONCACHE
*IconAnchor
= NULL
;
125 static CRITICAL_SECTION IconCrst
;
126 static CRITICAL_SECTION_DEBUG critsect_debug
=
129 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
130 0, 0, { (DWORD_PTR
)(__FILE__
": IconCrst") }
132 static CRITICAL_SECTION IconCrst
= { &critsect_debug
, -1, 0, 0, 0, 0 };
134 static WORD ICON_HOTSPOT
= 0x4242;
137 /***********************************************************************
140 * Helper function to map a file to memory:
142 * [RETURN] ptr - pointer to mapped file
143 * [RETURN] filesize - pointer size of file to be stored if not NULL
145 static void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
147 HANDLE hFile
, hMapping
;
150 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
151 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
152 if (hFile
!= INVALID_HANDLE_VALUE
)
154 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
157 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
158 CloseHandle( hMapping
);
160 *filesize
= GetFileSize( hFile
, NULL
);
162 CloseHandle( hFile
);
168 /***********************************************************************
169 * get_bitmap_width_bytes
171 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
174 static int get_bitmap_width_bytes( int width
, int bpp
)
179 return 2 * ((width
+15) / 16);
181 return 2 * ((width
+3) / 4);
186 return width
+ (width
& 1);
193 WARN("Unknown depth %d, please report.\n", bpp
);
199 /***********************************************************************
200 * get_dib_width_bytes
202 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
204 static int get_dib_width_bytes( int width
, int depth
)
210 case 1: words
= (width
+ 31) / 32; break;
211 case 4: words
= (width
+ 7) / 8; break;
212 case 8: words
= (width
+ 3) / 4; break;
214 case 16: words
= (width
+ 1) / 2; break;
215 case 24: words
= (width
* 3 + 3)/4; break;
217 WARN("(%d): Unsupported depth\n", depth
);
226 /***********************************************************************
229 * Return the size of the bitmap info structure including color table.
231 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
235 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
237 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
238 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
239 return sizeof(BITMAPCOREHEADER
) + colors
*
240 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
242 else /* assume BITMAPINFOHEADER */
244 colors
= info
->bmiHeader
.biClrUsed
;
245 if (colors
> 256) /* buffer overflow otherwise */
247 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
248 colors
= 1 << info
->bmiHeader
.biBitCount
;
249 return sizeof(BITMAPINFOHEADER
) + colors
*
250 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
255 /***********************************************************************
258 * Returns whether a DIB can be converted to a monochrome DDB.
260 * A DIB can be converted if its color table contains only black and
261 * white. Black must be the first color in the color table.
263 * Note : If the first color in the color table is white followed by
264 * black, we can't convert it to a monochrome DDB with
265 * SetDIBits, because black and white would be inverted.
267 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
269 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
271 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
273 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*) info
)->bmciColors
;
275 /* Check if the first color is black */
276 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
280 /* Check if the second color is white */
281 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
282 && (rgb
->rgbtBlue
== 0xff));
286 else /* assume BITMAPINFOHEADER */
288 RGBQUAD
*rgb
= info
->bmiColors
;
290 /* Check if the first color is black */
291 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
292 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
296 /* Check if the second color is white */
297 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
298 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
304 /***********************************************************************
307 * Get the info from a bitmap header.
308 * Return 1 for INFOHEADER, 0 for COREHEADER,
309 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
311 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
312 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
314 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
316 *width
= header
->biWidth
;
317 *height
= header
->biHeight
;
318 *bpp
= header
->biBitCount
;
319 *compr
= header
->biCompression
;
322 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
324 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
325 *width
= core
->bcWidth
;
326 *height
= core
->bcHeight
;
327 *bpp
= core
->bcBitCount
;
331 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
333 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
334 *width
= v4hdr
->bV4Width
;
335 *height
= v4hdr
->bV4Height
;
336 *bpp
= v4hdr
->bV4BitCount
;
337 *compr
= v4hdr
->bV4V4Compression
;
340 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
342 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
343 *width
= v5hdr
->bV5Width
;
344 *height
= v5hdr
->bV5Height
;
345 *bpp
= v5hdr
->bV5BitCount
;
346 *compr
= v5hdr
->bV5Compression
;
349 ERR("(%ld): unknown/wrong size for header\n", header
->biSize
);
353 /**********************************************************************
354 * CURSORICON_FindSharedIcon
356 static HICON
CURSORICON_FindSharedIcon( HMODULE hModule
, HRSRC hRsrc
)
361 EnterCriticalSection( &IconCrst
);
363 for ( ptr
= IconAnchor
; ptr
; ptr
= ptr
->next
)
364 if ( ptr
->hModule
== hModule
&& ptr
->hRsrc
== hRsrc
)
371 LeaveCriticalSection( &IconCrst
);
376 /*************************************************************************
377 * CURSORICON_FindCache
379 * Given a handle, find the corresponding cache element
382 * Handle [I] handle to an Image
385 * Success: The cache entry
389 static ICONCACHE
* CURSORICON_FindCache(HICON hIcon
)
392 ICONCACHE
*pRet
=NULL
;
393 BOOL IsFound
= FALSE
;
396 EnterCriticalSection( &IconCrst
);
398 for (count
= 0, ptr
= IconAnchor
; ptr
!= NULL
&& !IsFound
; ptr
= ptr
->next
, count
++ )
400 if ( hIcon
== ptr
->hIcon
)
407 LeaveCriticalSection( &IconCrst
);
412 /**********************************************************************
413 * CURSORICON_AddSharedIcon
415 static void CURSORICON_AddSharedIcon( HMODULE hModule
, HRSRC hRsrc
, HRSRC hGroupRsrc
, HICON hIcon
)
417 ICONCACHE
*ptr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE
) );
420 ptr
->hModule
= hModule
;
423 ptr
->hGroupRsrc
= hGroupRsrc
;
426 EnterCriticalSection( &IconCrst
);
427 ptr
->next
= IconAnchor
;
429 LeaveCriticalSection( &IconCrst
);
432 /**********************************************************************
433 * CURSORICON_DelSharedIcon
435 static INT
CURSORICON_DelSharedIcon( HICON hIcon
)
440 EnterCriticalSection( &IconCrst
);
442 for ( ptr
= IconAnchor
; ptr
; ptr
= ptr
->next
)
443 if ( ptr
->hIcon
== hIcon
)
445 if ( ptr
->count
> 0 ) ptr
->count
--;
450 LeaveCriticalSection( &IconCrst
);
455 /**********************************************************************
456 * CURSORICON_FreeModuleIcons
458 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16
)
460 ICONCACHE
**ptr
= &IconAnchor
;
461 HMODULE hModule
= HMODULE_32(GetExePtr( hMod16
));
463 EnterCriticalSection( &IconCrst
);
467 if ( (*ptr
)->hModule
== hModule
)
469 ICONCACHE
*freePtr
= *ptr
;
470 *ptr
= freePtr
->next
;
472 GlobalFree16(HICON_16(freePtr
->hIcon
));
473 HeapFree( GetProcessHeap(), 0, freePtr
);
479 LeaveCriticalSection( &IconCrst
);
483 * The following macro functions account for the irregularities of
484 * accessing cursor and icon resources in files and resource entries.
486 typedef BOOL (*fnGetCIEntry
)( LPVOID dir
, int n
,
487 int *width
, int *height
, int *bits
);
489 /**********************************************************************
490 * CURSORICON_FindBestIcon
492 * Find the icon closest to the requested size and number of colors.
494 static int CURSORICON_FindBestIcon( LPVOID dir
, fnGetCIEntry get_entry
,
495 int width
, int height
, int colors
)
497 int i
, cx
, cy
, bits
, bestEntry
= -1;
498 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
499 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
502 iTotalDiff
= 0xFFFFFFFF;
503 iColorDiff
= 0xFFFFFFFF;
504 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
506 iTempXDiff
= abs(width
- cx
);
507 iTempYDiff
= abs(height
- cy
);
509 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
513 iTotalDiff
= iXDiff
+ iYDiff
;
517 /* Find Best Colors for Best Fit */
518 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
520 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
522 iTempColorDiff
= abs(colors
- (1<<bits
));
523 if(iColorDiff
> iTempColorDiff
)
526 iColorDiff
= iTempColorDiff
;
534 static BOOL
CURSORICON_GetResIconEntry( LPVOID dir
, int n
,
535 int *width
, int *height
, int *bits
)
537 CURSORICONDIR
*resdir
= dir
;
540 if ( resdir
->idCount
<= n
)
542 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
543 *width
= icon
->bWidth
;
544 *height
= icon
->bWidth
;
545 *bits
= resdir
->idEntries
[n
].wBitCount
;
549 /**********************************************************************
550 * CURSORICON_FindBestCursor
552 * Find the cursor closest to the requested size.
553 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
556 static int CURSORICON_FindBestCursor( LPVOID dir
, fnGetCIEntry get_entry
,
557 int width
, int height
, int color
)
559 int i
, maxwidth
, maxheight
, cx
, cy
, bits
, bestEntry
= -1;
561 /* Double height to account for AND and XOR masks */
565 /* First find the largest one smaller than or equal to the requested size*/
567 maxwidth
= maxheight
= 0;
568 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
570 if ((cx
<= width
) && (cy
<= height
) &&
571 (cx
> maxwidth
) && (cy
> maxheight
) &&
579 if (bestEntry
!= -1) return bestEntry
;
581 /* Now find the smallest one larger than the requested size */
583 maxwidth
= maxheight
= 255;
584 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
586 if (((cx
< maxwidth
) && (cy
< maxheight
) && (bits
== 1)) ||
598 static BOOL
CURSORICON_GetResCursorEntry( LPVOID dir
, int n
,
599 int *width
, int *height
, int *bits
)
601 CURSORICONDIR
*resdir
= dir
;
604 if ( resdir
->idCount
<= n
)
606 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
607 *width
= cursor
->wWidth
;
608 *height
= cursor
->wHeight
;
609 *bits
= resdir
->idEntries
[n
].wBitCount
;
613 static CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( CURSORICONDIR
* dir
,
614 int width
, int height
, int colors
)
618 n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetResIconEntry
,
619 width
, height
, colors
);
622 return &dir
->idEntries
[n
];
625 static CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( CURSORICONDIR
*dir
,
626 int width
, int height
, int color
)
628 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetResCursorEntry
,
629 width
, height
, color
);
632 return &dir
->idEntries
[n
];
635 static BOOL
CURSORICON_GetFileEntry( LPVOID dir
, int n
,
636 int *width
, int *height
, int *bits
)
638 CURSORICONFILEDIR
*filedir
= dir
;
639 CURSORICONFILEDIRENTRY
*entry
;
641 if ( filedir
->idCount
<= n
)
643 entry
= &filedir
->idEntries
[n
];
644 *width
= entry
->bWidth
;
645 *height
= entry
->bHeight
;
646 *bits
= entry
->bColorCount
;
650 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( CURSORICONFILEDIR
*dir
,
651 int width
, int height
, int color
)
653 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetFileEntry
,
654 width
, height
, color
);
657 return &dir
->idEntries
[n
];
660 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( CURSORICONFILEDIR
*dir
,
661 int width
, int height
, int color
)
663 int n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetFileEntry
,
664 width
, height
, color
);
667 return &dir
->idEntries
[n
];
670 /**********************************************************************
671 * CreateIconFromResourceEx (USER32.@)
673 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
674 * with cbSize parameter as well.
676 HICON WINAPI
CreateIconFromResourceEx( LPBYTE bits
, UINT cbSize
,
677 BOOL bIcon
, DWORD dwVersion
,
678 INT width
, INT height
,
683 int sizeAnd
, sizeXor
;
684 HBITMAP hAndBits
= 0, hXorBits
= 0; /* error condition for later */
685 BITMAP bmpXor
, bmpAnd
;
691 hotspot
.x
= ICON_HOTSPOT
;
692 hotspot
.y
= ICON_HOTSPOT
;
694 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
695 bits
, cbSize
, (unsigned)dwVersion
, width
, height
,
696 bIcon
? "icon" : "cursor", (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
697 if (dwVersion
== 0x00020000)
699 FIXME_(cursor
)("\t2.xx resources are not supported\n");
704 bmi
= (BITMAPINFO
*)bits
;
705 else /* get the hotspot */
707 POINT16
*pt
= (POINT16
*)bits
;
709 bmi
= (BITMAPINFO
*)(pt
+ 1);
711 size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
713 if (!width
) width
= bmi
->bmiHeader
.biWidth
;
714 if (!height
) height
= bmi
->bmiHeader
.biHeight
/2;
715 DoStretch
= (bmi
->bmiHeader
.biHeight
/2 != height
) ||
716 (bmi
->bmiHeader
.biWidth
!= width
);
718 /* Check bitmap header */
720 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
721 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
722 bmi
->bmiHeader
.biCompression
!= BI_RGB
) )
724 WARN_(cursor
)("\tinvalid resource bitmap header.\n");
728 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
733 /* Make sure we have room for the monochrome bitmap later on.
734 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
735 * up to and including the biBitCount. In-memory icon resource
736 * format is as follows:
738 * BITMAPINFOHEADER icHeader // DIB header
739 * RGBQUAD icColors[] // Color table
740 * BYTE icXOR[] // DIB bits for XOR mask
741 * BYTE icAND[] // DIB bits for AND mask
744 if ((pInfo
= HeapAlloc( GetProcessHeap(), 0,
745 max(size
, sizeof(BITMAPINFOHEADER
) + 2*sizeof(RGBQUAD
)))))
747 memcpy( pInfo
, bmi
, size
);
748 pInfo
->bmiHeader
.biHeight
/= 2;
750 /* Create the XOR bitmap */
755 hXorBits
= CreateCompatibleBitmap(screen_dc
, width
, height
);
759 hXorBits
= CreateBitmap(width
, height
, 1, 1, NULL
);
766 if (!hdcMem
) hdcMem
= CreateCompatibleDC(screen_dc
);
768 hOld
= SelectObject(hdcMem
, hXorBits
);
769 res
= StretchDIBits(hdcMem
, 0, 0, width
, height
, 0, 0,
770 bmi
->bmiHeader
.biWidth
, bmi
->bmiHeader
.biHeight
/2,
771 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
, SRCCOPY
);
772 SelectObject(hdcMem
, hOld
);
774 if (!res
) { DeleteObject(hXorBits
); hXorBits
= 0; }
777 if (is_dib_monochrome(bmi
)) {
778 hXorBits
= CreateBitmap(width
, height
, 1, 1, NULL
);
779 SetDIBits(screen_dc
, hXorBits
, 0, height
,
780 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
783 hXorBits
= CreateDIBitmap(screen_dc
, &pInfo
->bmiHeader
,
784 CBM_INIT
, (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
789 char* xbits
= (char *)bmi
+ size
+
790 get_dib_width_bytes( bmi
->bmiHeader
.biWidth
,
791 bmi
->bmiHeader
.biBitCount
) * abs( bmi
->bmiHeader
.biHeight
) / 2;
793 pInfo
->bmiHeader
.biBitCount
= 1;
794 if (pInfo
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
796 RGBQUAD
*rgb
= pInfo
->bmiColors
;
798 pInfo
->bmiHeader
.biClrUsed
= pInfo
->bmiHeader
.biClrImportant
= 2;
799 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
800 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
801 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
805 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)pInfo
) + 1);
807 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
808 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
811 /* Create the AND bitmap */
814 if ((hAndBits
= CreateBitmap(width
, height
, 1, 1, NULL
))) {
818 if (!hdcMem
) hdcMem
= CreateCompatibleDC(screen_dc
);
820 hOld
= SelectObject(hdcMem
, hAndBits
);
821 res
= StretchDIBits(hdcMem
, 0, 0, width
, height
, 0, 0,
822 pInfo
->bmiHeader
.biWidth
, pInfo
->bmiHeader
.biHeight
,
823 xbits
, pInfo
, DIB_RGB_COLORS
, SRCCOPY
);
824 SelectObject(hdcMem
, hOld
);
826 if (!res
) { DeleteObject(hAndBits
); hAndBits
= 0; }
829 hAndBits
= CreateBitmap(width
, height
, 1, 1, NULL
);
831 if (hAndBits
) SetDIBits(screen_dc
, hAndBits
, 0, height
,
832 xbits
, pInfo
, DIB_RGB_COLORS
);
835 if( !hAndBits
) DeleteObject( hXorBits
);
837 HeapFree( GetProcessHeap(), 0, pInfo
);
841 if( !hXorBits
|| !hAndBits
)
843 WARN_(cursor
)("\tunable to create an icon bitmap.\n");
847 /* Now create the CURSORICONINFO structure */
848 GetObjectA( hXorBits
, sizeof(bmpXor
), &bmpXor
);
849 GetObjectA( hAndBits
, sizeof(bmpAnd
), &bmpAnd
);
850 sizeXor
= bmpXor
.bmHeight
* bmpXor
.bmWidthBytes
;
851 sizeAnd
= bmpAnd
.bmHeight
* bmpAnd
.bmWidthBytes
;
853 hObj
= GlobalAlloc16( GMEM_MOVEABLE
,
854 sizeof(CURSORICONINFO
) + sizeXor
+ sizeAnd
);
857 CURSORICONINFO
*info
;
859 info
= (CURSORICONINFO
*)GlobalLock16( hObj
);
860 info
->ptHotSpot
.x
= hotspot
.x
;
861 info
->ptHotSpot
.y
= hotspot
.y
;
862 info
->nWidth
= bmpXor
.bmWidth
;
863 info
->nHeight
= bmpXor
.bmHeight
;
864 info
->nWidthBytes
= bmpXor
.bmWidthBytes
;
865 info
->bPlanes
= bmpXor
.bmPlanes
;
866 info
->bBitsPerPixel
= bmpXor
.bmBitsPixel
;
868 /* Transfer the bitmap bits to the CURSORICONINFO structure */
870 GetBitmapBits( hAndBits
, sizeAnd
, (char *)(info
+ 1) );
871 GetBitmapBits( hXorBits
, sizeXor
, (char *)(info
+ 1) + sizeAnd
);
872 GlobalUnlock16( hObj
);
875 DeleteObject( hAndBits
);
876 DeleteObject( hXorBits
);
877 return HICON_32((HICON16
)hObj
);
881 /**********************************************************************
882 * CreateIconFromResource (USER32.@)
884 HICON WINAPI
CreateIconFromResource( LPBYTE bits
, UINT cbSize
,
885 BOOL bIcon
, DWORD dwVersion
)
887 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0,0,0);
891 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
892 INT width
, INT height
, INT colors
,
893 BOOL fCursor
, UINT loadflags
)
895 CURSORICONFILEDIRENTRY
*entry
;
896 CURSORICONFILEDIR
*dir
;
901 TRACE("loading %s\n", debugstr_w( filename
));
903 bits
= map_fileW( filename
, &filesize
);
907 dir
= (CURSORICONFILEDIR
*) bits
;
908 if ( filesize
< sizeof(*dir
) )
911 if ( filesize
< (sizeof(*dir
) + sizeof(dir
->idEntries
[0])*(dir
->idCount
-1)) )
915 entry
= CURSORICON_FindBestCursorFile( dir
, width
, height
, 1 );
917 entry
= CURSORICON_FindBestIconFile( dir
, width
, height
, colors
);
922 /* check that we don't run off the end of the file */
923 if ( entry
->dwDIBOffset
> filesize
)
925 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
928 hIcon
= CreateIconFromResourceEx( &bits
[entry
->dwDIBOffset
], entry
->dwDIBSize
,
929 !fCursor
, 0x00030000, width
, height
, loadflags
);
931 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
932 UnmapViewOfFile( bits
);
936 /**********************************************************************
939 * Load a cursor or icon from resource or file.
941 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
942 INT width
, INT height
, INT colors
,
943 BOOL fCursor
, UINT loadflags
)
947 HRSRC hRsrc
, hGroupRsrc
;
949 CURSORICONDIRENTRY
*dirEntry
;
954 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
955 return CURSORICON_LoadFromFile( name
, width
, height
, colors
, fCursor
, loadflags
);
957 if (!hInstance
) hInstance
= user32_module
; /* Load OEM cursor/icon */
959 /* Normalize hInstance (must be uniquely represented for icon cache) */
961 if (!HIWORD( hInstance
))
962 hInstance
= HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance
) ));
964 /* Get directory resource ID */
966 if (!(hRsrc
= FindResourceW( hInstance
, name
,
967 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
971 /* Find the best entry in the directory */
973 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
974 if (!(dir
= (CURSORICONDIR
*)LockResource( handle
))) return 0;
976 dirEntry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, 1);
978 dirEntry
= CURSORICON_FindBestIconRes( dir
, width
, height
, colors
);
979 if (!dirEntry
) return 0;
980 wResId
= dirEntry
->wResId
;
981 dwBytesInRes
= dirEntry
->dwBytesInRes
;
982 FreeResource( handle
);
984 /* Load the resource */
986 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
987 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
989 /* If shared icon, check whether it was already loaded */
990 if ( (loadflags
& LR_SHARED
)
991 && (hIcon
= CURSORICON_FindSharedIcon( hInstance
, hRsrc
) ) != 0 )
994 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
995 bits
= (LPBYTE
)LockResource( handle
);
996 hIcon
= CreateIconFromResourceEx( bits
, dwBytesInRes
,
997 !fCursor
, 0x00030000, width
, height
, loadflags
);
998 FreeResource( handle
);
1000 /* If shared icon, add to icon cache */
1002 if ( hIcon
&& (loadflags
& LR_SHARED
) )
1003 CURSORICON_AddSharedIcon( hInstance
, hRsrc
, hGroupRsrc
, hIcon
);
1008 /***********************************************************************
1011 * Make a copy of a cursor or icon.
1013 static HICON
CURSORICON_Copy( HINSTANCE16 hInst16
, HICON hIcon
)
1015 char *ptrOld
, *ptrNew
;
1017 HICON16 hOld
= HICON_16(hIcon
);
1020 if (!(ptrOld
= (char *)GlobalLock16( hOld
))) return 0;
1021 if (hInst16
&& !(hInst16
= GetExePtr( hInst16
))) return 0;
1022 size
= GlobalSize16( hOld
);
1023 hNew
= GlobalAlloc16( GMEM_MOVEABLE
, size
);
1024 FarSetOwner16( hNew
, hInst16
);
1025 ptrNew
= (char *)GlobalLock16( hNew
);
1026 memcpy( ptrNew
, ptrOld
, size
);
1027 GlobalUnlock16( hOld
);
1028 GlobalUnlock16( hNew
);
1029 return HICON_32(hNew
);
1032 /*************************************************************************
1033 * CURSORICON_ExtCopy
1035 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1038 * Handle [I] handle to an Image
1039 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1040 * iDesiredCX [I] The Desired width of the Image
1041 * iDesiredCY [I] The desired height of the Image
1042 * nFlags [I] The flags from CopyImage
1045 * Success: The new handle of the Image
1048 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1049 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1050 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1055 static HICON
CURSORICON_ExtCopy(HICON hIcon
, UINT nType
,
1056 INT iDesiredCX
, INT iDesiredCY
,
1061 TRACE_(icon
)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1062 hIcon
, nType
, iDesiredCX
, iDesiredCY
, nFlags
);
1069 /* Best Fit or Monochrome */
1070 if( (nFlags
& LR_COPYFROMRESOURCE
1071 && (iDesiredCX
> 0 || iDesiredCY
> 0))
1072 || nFlags
& LR_MONOCHROME
)
1074 ICONCACHE
* pIconCache
= CURSORICON_FindCache(hIcon
);
1076 /* Not Found in Cache, then do a straight copy
1078 if(pIconCache
== NULL
)
1080 hNew
= CURSORICON_Copy(0, hIcon
);
1081 if(nFlags
& LR_COPYFROMRESOURCE
)
1083 TRACE_(icon
)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1088 int iTargetCY
= iDesiredCY
, iTargetCX
= iDesiredCX
;
1094 CURSORICONDIR
*pDir
;
1095 CURSORICONDIRENTRY
*pDirEntry
;
1096 BOOL bIsIcon
= (nType
== IMAGE_ICON
);
1098 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1100 if(((nFlags
& LR_MONOCHROME
) && !(nFlags
& LR_COPYFROMRESOURCE
))
1101 || (iDesiredCX
== 0 && iDesiredCY
== 0))
1103 iDesiredCY
= GetSystemMetrics(bIsIcon
?
1104 SM_CYICON
: SM_CYCURSOR
);
1105 iDesiredCX
= GetSystemMetrics(bIsIcon
?
1106 SM_CXICON
: SM_CXCURSOR
);
1109 /* Retrieve the CURSORICONDIRENTRY
1111 if (!(hMem
= LoadResource( pIconCache
->hModule
,
1112 pIconCache
->hGroupRsrc
)))
1116 if (!(pDir
= (CURSORICONDIR
*)LockResource( hMem
)))
1125 pDirEntry
= CURSORICON_FindBestIconRes(
1126 pDir
, iDesiredCX
, iDesiredCY
, 256 );
1130 pDirEntry
= (CURSORICONDIRENTRY
*)CURSORICON_FindBestCursorRes(
1131 pDir
, iDesiredCX
, iDesiredCY
, 1);
1134 wResId
= pDirEntry
->wResId
;
1135 dwBytesInRes
= pDirEntry
->dwBytesInRes
;
1138 TRACE_(icon
)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
1139 wResId
, dwBytesInRes
, pDirEntry
->ResInfo
.icon
.bWidth
,
1140 pDirEntry
->ResInfo
.icon
.bHeight
, iDesiredCX
, iDesiredCY
);
1144 if (!(hRsrc
= FindResourceW(pIconCache
->hModule
,
1145 MAKEINTRESOURCEW(wResId
), (LPWSTR
)(bIsIcon
? RT_ICON
: RT_CURSOR
))))
1149 if (!(hMem
= LoadResource( pIconCache
->hModule
, hRsrc
)))
1154 pBits
= (LPBYTE
)LockResource( hMem
);
1156 if(nFlags
& LR_DEFAULTSIZE
)
1158 iTargetCY
= GetSystemMetrics(SM_CYICON
);
1159 iTargetCX
= GetSystemMetrics(SM_CXICON
);
1162 /* Create a New Icon with the proper dimension
1164 hNew
= CreateIconFromResourceEx( pBits
, dwBytesInRes
,
1165 bIsIcon
, 0x00030000, iTargetCX
, iTargetCY
, nFlags
);
1169 else hNew
= CURSORICON_Copy(0, hIcon
);
1174 /***********************************************************************
1175 * CreateCursor (USER32.@)
1177 HCURSOR WINAPI
CreateCursor( HINSTANCE hInstance
,
1178 INT xHotSpot
, INT yHotSpot
,
1179 INT nWidth
, INT nHeight
,
1180 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1182 CURSORICONINFO info
;
1184 TRACE_(cursor
)("%dx%d spot=%d,%d xor=%p and=%p\n",
1185 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
1187 info
.ptHotSpot
.x
= xHotSpot
;
1188 info
.ptHotSpot
.y
= yHotSpot
;
1189 info
.nWidth
= nWidth
;
1190 info
.nHeight
= nHeight
;
1191 info
.nWidthBytes
= 0;
1193 info
.bBitsPerPixel
= 1;
1195 return HICON_32(CreateCursorIconIndirect16(0, &info
, lpANDbits
, lpXORbits
));
1199 /***********************************************************************
1200 * CreateIcon (USER.407)
1202 HICON16 WINAPI
CreateIcon16( HINSTANCE16 hInstance
, INT16 nWidth
,
1203 INT16 nHeight
, BYTE bPlanes
, BYTE bBitsPixel
,
1204 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1206 CURSORICONINFO info
;
1208 TRACE_(icon
)("%dx%dx%d, xor=%p, and=%p\n",
1209 nWidth
, nHeight
, bPlanes
* bBitsPixel
, lpXORbits
, lpANDbits
);
1211 info
.ptHotSpot
.x
= ICON_HOTSPOT
;
1212 info
.ptHotSpot
.y
= ICON_HOTSPOT
;
1213 info
.nWidth
= nWidth
;
1214 info
.nHeight
= nHeight
;
1215 info
.nWidthBytes
= 0;
1216 info
.bPlanes
= bPlanes
;
1217 info
.bBitsPerPixel
= bBitsPixel
;
1219 return CreateCursorIconIndirect16( hInstance
, &info
, lpANDbits
, lpXORbits
);
1223 /***********************************************************************
1224 * CreateIcon (USER32.@)
1226 * Creates an icon based on the specified bitmaps. The bitmaps must be
1227 * provided in a device dependent format and will be resized to
1228 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1229 * depth. The provided bitmaps must be top-down bitmaps.
1230 * Although Windows does not support 15bpp(*) this API must support it
1231 * for Winelib applications.
1233 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1237 * Success: handle to an icon
1242 * - The provided bitmaps are not resized!
1243 * - The documentation says the lpXORbits bitmap must be in a device
1244 * dependent format. But we must still resize it and perform depth
1245 * conversions if necessary.
1246 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1247 * I did some tests on windows and found that if you provide a 16bpp bitmap
1248 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1249 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1250 * else. I don't know if this is part of the GDI specs or if this is a
1251 * quirk of the graphics card driver.
1252 * - You may think that we check whether the bit depths match or not
1253 * as an optimization. But the truth is that the conversion using
1254 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1256 * - I'm pretty sure that all the things we do in CreateIcon should
1257 * also be done in CreateIconIndirect...
1259 HICON WINAPI
CreateIcon(
1260 HINSTANCE hInstance
, /* [in] the application's hInstance */
1261 INT nWidth
, /* [in] the width of the provided bitmaps */
1262 INT nHeight
, /* [in] the height of the provided bitmaps */
1263 BYTE bPlanes
, /* [in] the number of planes in the provided bitmaps */
1264 BYTE bBitsPixel
, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1265 LPCVOID lpANDbits
, /* [in] a monochrome bitmap representing the icon's mask */
1266 LPCVOID lpXORbits
) /* [in] the icon's 'color' bitmap */
1271 TRACE_(icon
)("%dx%dx%d, xor=%p, and=%p\n",
1272 nWidth
, nHeight
, bPlanes
* bBitsPixel
, lpXORbits
, lpANDbits
);
1278 if (GetDeviceCaps(hdc
,BITSPIXEL
)==bBitsPixel
) {
1279 CURSORICONINFO info
;
1281 info
.ptHotSpot
.x
= ICON_HOTSPOT
;
1282 info
.ptHotSpot
.y
= ICON_HOTSPOT
;
1283 info
.nWidth
= nWidth
;
1284 info
.nHeight
= nHeight
;
1285 info
.nWidthBytes
= 0;
1286 info
.bPlanes
= bPlanes
;
1287 info
.bBitsPerPixel
= bBitsPixel
;
1289 hIcon
=HICON_32(CreateCursorIconIndirect16(0, &info
, lpANDbits
, lpXORbits
));
1295 iinfo
.xHotspot
=ICON_HOTSPOT
;
1296 iinfo
.yHotspot
=ICON_HOTSPOT
;
1297 iinfo
.hbmMask
=CreateBitmap(nWidth
,nHeight
,1,1,lpANDbits
);
1299 bmi
.bmiHeader
.biSize
=sizeof(bmi
.bmiHeader
);
1300 bmi
.bmiHeader
.biWidth
=nWidth
;
1301 bmi
.bmiHeader
.biHeight
=-nHeight
;
1302 bmi
.bmiHeader
.biPlanes
=bPlanes
;
1303 bmi
.bmiHeader
.biBitCount
=bBitsPixel
;
1304 bmi
.bmiHeader
.biCompression
=BI_RGB
;
1305 bmi
.bmiHeader
.biSizeImage
=0;
1306 bmi
.bmiHeader
.biXPelsPerMeter
=0;
1307 bmi
.bmiHeader
.biYPelsPerMeter
=0;
1308 bmi
.bmiHeader
.biClrUsed
=0;
1309 bmi
.bmiHeader
.biClrImportant
=0;
1311 iinfo
.hbmColor
= CreateDIBitmap( hdc
, &bmi
.bmiHeader
,
1312 CBM_INIT
, lpXORbits
,
1313 &bmi
, DIB_RGB_COLORS
);
1315 hIcon
=CreateIconIndirect(&iinfo
);
1316 DeleteObject(iinfo
.hbmMask
);
1317 DeleteObject(iinfo
.hbmColor
);
1324 /***********************************************************************
1325 * CreateCursorIconIndirect (USER.408)
1327 HGLOBAL16 WINAPI
CreateCursorIconIndirect16( HINSTANCE16 hInstance
,
1328 CURSORICONINFO
*info
,
1334 int sizeAnd
, sizeXor
;
1336 hInstance
= GetExePtr( hInstance
); /* Make it a module handle */
1337 if (!lpXORbits
|| !lpANDbits
|| info
->bPlanes
!= 1) return 0;
1338 info
->nWidthBytes
= get_bitmap_width_bytes(info
->nWidth
,info
->bBitsPerPixel
);
1339 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
1340 sizeAnd
= info
->nHeight
* get_bitmap_width_bytes( info
->nWidth
, 1 );
1341 if (!(handle
= GlobalAlloc16( GMEM_MOVEABLE
,
1342 sizeof(CURSORICONINFO
) + sizeXor
+ sizeAnd
)))
1344 FarSetOwner16( handle
, hInstance
);
1345 ptr
= (char *)GlobalLock16( handle
);
1346 memcpy( ptr
, info
, sizeof(*info
) );
1347 memcpy( ptr
+ sizeof(CURSORICONINFO
), lpANDbits
, sizeAnd
);
1348 memcpy( ptr
+ sizeof(CURSORICONINFO
) + sizeAnd
, lpXORbits
, sizeXor
);
1349 GlobalUnlock16( handle
);
1354 /***********************************************************************
1355 * CopyIcon (USER.368)
1357 HICON16 WINAPI
CopyIcon16( HINSTANCE16 hInstance
, HICON16 hIcon
)
1359 TRACE_(icon
)("%04x %04x\n", hInstance
, hIcon
);
1360 return HICON_16(CURSORICON_Copy(hInstance
, HICON_32(hIcon
)));
1364 /***********************************************************************
1365 * CopyIcon (USER32.@)
1367 HICON WINAPI
CopyIcon( HICON hIcon
)
1369 TRACE_(icon
)("%p\n", hIcon
);
1370 return CURSORICON_Copy( 0, hIcon
);
1374 /***********************************************************************
1375 * CopyCursor (USER.369)
1377 HCURSOR16 WINAPI
CopyCursor16( HINSTANCE16 hInstance
, HCURSOR16 hCursor
)
1379 TRACE_(cursor
)("%04x %04x\n", hInstance
, hCursor
);
1380 return HICON_16(CURSORICON_Copy(hInstance
, HCURSOR_32(hCursor
)));
1383 /**********************************************************************
1384 * DestroyIcon32 (USER.610)
1386 * This routine is actually exported from Win95 USER under the name
1387 * DestroyIcon32 ... The behaviour implemented here should mimic
1388 * the Win95 one exactly, especially the return values, which
1389 * depend on the setting of various flags.
1391 WORD WINAPI
DestroyIcon32( HGLOBAL16 handle
, UINT16 flags
)
1395 TRACE_(icon
)("(%04x, %04x)\n", handle
, flags
);
1397 /* Check whether destroying active cursor */
1399 if ( get_user_thread_info()->cursor
== HICON_32(handle
) )
1401 WARN_(cursor
)("Destroying active cursor!\n" );
1405 /* Try shared cursor/icon first */
1407 if ( !(flags
& CID_NONSHARED
) )
1409 INT count
= CURSORICON_DelSharedIcon(HICON_32(handle
));
1412 return (flags
& CID_WIN32
)? TRUE
: (count
== 0);
1414 /* FIXME: OEM cursors/icons should be recognized */
1417 /* Now assume non-shared cursor/icon */
1419 retv
= GlobalFree16( handle
);
1420 return (flags
& CID_RESOURCE
)? retv
: TRUE
;
1423 /***********************************************************************
1424 * DestroyIcon (USER32.@)
1426 BOOL WINAPI
DestroyIcon( HICON hIcon
)
1428 return DestroyIcon32(HICON_16(hIcon
), CID_WIN32
);
1432 /***********************************************************************
1433 * DestroyCursor (USER32.@)
1435 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
1437 return DestroyIcon32(HCURSOR_16(hCursor
), CID_WIN32
);
1441 /***********************************************************************
1442 * DrawIcon (USER32.@)
1444 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
1446 CURSORICONINFO
*ptr
;
1448 HBITMAP hXorBits
, hAndBits
;
1449 COLORREF oldFg
, oldBg
;
1451 if (!(ptr
= (CURSORICONINFO
*)GlobalLock16(HICON_16(hIcon
)))) return FALSE
;
1452 if (!(hMemDC
= CreateCompatibleDC( hdc
))) return FALSE
;
1453 hAndBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, 1, 1,
1455 hXorBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
,
1456 ptr
->bBitsPerPixel
, (char *)(ptr
+ 1)
1457 + ptr
->nHeight
* get_bitmap_width_bytes(ptr
->nWidth
,1) );
1458 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
1459 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
1461 if (hXorBits
&& hAndBits
)
1463 HBITMAP hBitTemp
= SelectObject( hMemDC
, hAndBits
);
1464 BitBlt( hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0, SRCAND
);
1465 SelectObject( hMemDC
, hXorBits
);
1466 BitBlt(hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0,SRCINVERT
);
1467 SelectObject( hMemDC
, hBitTemp
);
1470 if (hXorBits
) DeleteObject( hXorBits
);
1471 if (hAndBits
) DeleteObject( hAndBits
);
1472 GlobalUnlock16(HICON_16(hIcon
));
1473 SetTextColor( hdc
, oldFg
);
1474 SetBkColor( hdc
, oldBg
);
1478 /***********************************************************************
1479 * DumpIcon (USER.459)
1481 DWORD WINAPI
DumpIcon16( SEGPTR pInfo
, WORD
*lpLen
,
1482 SEGPTR
*lpXorBits
, SEGPTR
*lpAndBits
)
1484 CURSORICONINFO
*info
= MapSL( pInfo
);
1485 int sizeAnd
, sizeXor
;
1487 if (!info
) return 0;
1488 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
1489 sizeAnd
= info
->nHeight
* get_bitmap_width_bytes( info
->nWidth
, 1 );
1490 if (lpAndBits
) *lpAndBits
= pInfo
+ sizeof(CURSORICONINFO
);
1491 if (lpXorBits
) *lpXorBits
= pInfo
+ sizeof(CURSORICONINFO
) + sizeAnd
;
1492 if (lpLen
) *lpLen
= sizeof(CURSORICONINFO
) + sizeAnd
+ sizeXor
;
1493 return MAKELONG( sizeXor
, sizeXor
);
1497 /***********************************************************************
1498 * SetCursor (USER32.@)
1500 * Set the cursor shape.
1503 * A handle to the previous cursor shape.
1505 HCURSOR WINAPI
SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
1507 struct user_thread_info
*thread_info
= get_user_thread_info();
1510 if (hCursor
== thread_info
->cursor
) return hCursor
; /* No change */
1511 TRACE_(cursor
)("%p\n", hCursor
);
1512 hOldCursor
= thread_info
->cursor
;
1513 thread_info
->cursor
= hCursor
;
1514 /* Change the cursor shape only if it is visible */
1515 if (thread_info
->cursor_count
>= 0)
1517 USER_Driver
->pSetCursor( (CURSORICONINFO
*)GlobalLock16(HCURSOR_16(hCursor
)) );
1518 GlobalUnlock16(HCURSOR_16(hCursor
));
1523 /***********************************************************************
1524 * ShowCursor (USER32.@)
1526 INT WINAPI
ShowCursor( BOOL bShow
)
1528 struct user_thread_info
*thread_info
= get_user_thread_info();
1530 TRACE_(cursor
)("%d, count=%d\n", bShow
, thread_info
->cursor_count
);
1534 if (++thread_info
->cursor_count
== 0) /* Show it */
1536 USER_Driver
->pSetCursor((CURSORICONINFO
*)GlobalLock16(HCURSOR_16(thread_info
->cursor
)));
1537 GlobalUnlock16(HCURSOR_16(thread_info
->cursor
));
1542 if (--thread_info
->cursor_count
== -1) /* Hide it */
1543 USER_Driver
->pSetCursor( NULL
);
1545 return thread_info
->cursor_count
;
1548 /***********************************************************************
1549 * GetCursor (USER32.@)
1551 HCURSOR WINAPI
GetCursor(void)
1553 return get_user_thread_info()->cursor
;
1557 /***********************************************************************
1558 * ClipCursor (USER32.@)
1560 BOOL WINAPI
ClipCursor( const RECT
*rect
)
1562 if (!rect
) SetRectEmpty( &CURSOR_ClipRect
);
1563 else CopyRect( &CURSOR_ClipRect
, rect
);
1568 /***********************************************************************
1569 * GetClipCursor (USER32.@)
1571 BOOL WINAPI
GetClipCursor( RECT
*rect
)
1575 CopyRect( rect
, &CURSOR_ClipRect
);
1582 /***********************************************************************
1583 * SetSystemCursor (USER32.@)
1585 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
1587 FIXME("(%p,%08lx),stub!\n", hcur
, id
);
1592 /**********************************************************************
1593 * LookupIconIdFromDirectoryEx (USER.364)
1595 * FIXME: exact parameter sizes
1597 INT16 WINAPI
LookupIconIdFromDirectoryEx16( LPBYTE dir
, BOOL16 bIcon
,
1598 INT16 width
, INT16 height
, UINT16 cFlag
)
1600 return LookupIconIdFromDirectoryEx( dir
, bIcon
, width
, height
, cFlag
);
1603 /**********************************************************************
1604 * LookupIconIdFromDirectoryEx (USER32.@)
1606 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
1607 INT width
, INT height
, UINT cFlag
)
1609 CURSORICONDIR
*dir
= (CURSORICONDIR
*)xdir
;
1611 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
1613 CURSORICONDIRENTRY
* entry
;
1618 palEnts
= GetSystemPaletteEntries(hdc
, 0, 0, NULL
);
1621 colors
= (cFlag
& LR_MONOCHROME
) ? 2 : palEnts
;
1626 entry
= CURSORICON_FindBestIconRes( dir
, width
, height
, colors
);
1628 entry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, 1);
1630 if( entry
) retVal
= entry
->wResId
;
1632 else WARN_(cursor
)("invalid resource directory\n");
1636 /**********************************************************************
1637 * LookupIconIdFromDirectory (USER.?)
1639 INT16 WINAPI
LookupIconIdFromDirectory16( LPBYTE dir
, BOOL16 bIcon
)
1641 return LookupIconIdFromDirectoryEx16( dir
, bIcon
,
1642 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
1643 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
1646 /**********************************************************************
1647 * LookupIconIdFromDirectory (USER32.@)
1649 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
1651 return LookupIconIdFromDirectoryEx( dir
, bIcon
,
1652 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
1653 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
1656 /**********************************************************************
1657 * GetIconID (USER.455)
1659 WORD WINAPI
GetIconID16( HGLOBAL16 hResource
, DWORD resType
)
1661 LPBYTE lpDir
= (LPBYTE
)GlobalLock16(hResource
);
1663 TRACE_(cursor
)("hRes=%04x, entries=%i\n",
1664 hResource
, lpDir
? ((CURSORICONDIR
*)lpDir
)->idCount
: 0);
1669 return (WORD
)LookupIconIdFromDirectoryEx16( lpDir
, FALSE
,
1670 GetSystemMetrics(SM_CXCURSOR
), GetSystemMetrics(SM_CYCURSOR
), LR_MONOCHROME
);
1672 return (WORD
)LookupIconIdFromDirectoryEx16( lpDir
, TRUE
,
1673 GetSystemMetrics(SM_CXICON
), GetSystemMetrics(SM_CYICON
), 0 );
1675 WARN_(cursor
)("invalid res type %ld\n", resType
);
1680 /**********************************************************************
1681 * LoadCursorIconHandler (USER.336)
1683 * Supposed to load resources of Windows 2.x applications.
1685 HGLOBAL16 WINAPI
LoadCursorIconHandler16( HGLOBAL16 hResource
, HMODULE16 hModule
, HRSRC16 hRsrc
)
1687 FIXME_(cursor
)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1688 hResource
, hModule
, hRsrc
);
1689 return (HGLOBAL16
)0;
1692 /**********************************************************************
1693 * LoadIconHandler (USER.456)
1695 HICON16 WINAPI
LoadIconHandler16( HGLOBAL16 hResource
, BOOL16 bNew
)
1697 LPBYTE bits
= (LPBYTE
)LockResource16( hResource
);
1699 TRACE_(cursor
)("hRes=%04x\n",hResource
);
1701 return HICON_16(CreateIconFromResourceEx( bits
, 0, TRUE
,
1702 bNew
? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR
));
1705 /***********************************************************************
1706 * LoadCursorW (USER32.@)
1708 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
1710 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1711 LR_SHARED
| LR_DEFAULTSIZE
);
1714 /***********************************************************************
1715 * LoadCursorA (USER32.@)
1717 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
1719 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1720 LR_SHARED
| LR_DEFAULTSIZE
);
1723 /***********************************************************************
1724 * LoadCursorFromFileW (USER32.@)
1726 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
1728 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
1729 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1732 /***********************************************************************
1733 * LoadCursorFromFileA (USER32.@)
1735 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
1737 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
1738 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1741 /***********************************************************************
1742 * LoadIconW (USER32.@)
1744 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
1746 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
1747 LR_SHARED
| LR_DEFAULTSIZE
);
1750 /***********************************************************************
1751 * LoadIconA (USER32.@)
1753 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
1755 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
1756 LR_SHARED
| LR_DEFAULTSIZE
);
1759 /**********************************************************************
1760 * GetIconInfo (USER32.@)
1762 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
1764 CURSORICONINFO
*ciconinfo
;
1767 ciconinfo
= GlobalLock16(HICON_16(hIcon
));
1771 if ( (ciconinfo
->ptHotSpot
.x
== ICON_HOTSPOT
) &&
1772 (ciconinfo
->ptHotSpot
.y
== ICON_HOTSPOT
) )
1774 iconinfo
->fIcon
= TRUE
;
1775 iconinfo
->xHotspot
= ciconinfo
->nWidth
/ 2;
1776 iconinfo
->yHotspot
= ciconinfo
->nHeight
/ 2;
1780 iconinfo
->fIcon
= FALSE
;
1781 iconinfo
->xHotspot
= ciconinfo
->ptHotSpot
.x
;
1782 iconinfo
->yHotspot
= ciconinfo
->ptHotSpot
.y
;
1785 if (ciconinfo
->bBitsPerPixel
> 1)
1787 iconinfo
->hbmColor
= CreateBitmap( ciconinfo
->nWidth
, ciconinfo
->nHeight
,
1788 ciconinfo
->bPlanes
, ciconinfo
->bBitsPerPixel
,
1789 (char *)(ciconinfo
+ 1)
1790 + ciconinfo
->nHeight
*
1791 get_bitmap_width_bytes (ciconinfo
->nWidth
,1) );
1792 height
= ciconinfo
->nHeight
;
1796 iconinfo
->hbmColor
= 0;
1797 height
= ciconinfo
->nHeight
* 2;
1800 iconinfo
->hbmMask
= CreateBitmap ( ciconinfo
->nWidth
, height
,
1801 1, 1, (char *)(ciconinfo
+ 1));
1803 GlobalUnlock16(HICON_16(hIcon
));
1808 /**********************************************************************
1809 * CreateIconIndirect (USER32.@)
1811 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
1813 BITMAP bmpXor
,bmpAnd
;
1815 int sizeXor
,sizeAnd
;
1817 if (iconinfo
->hbmColor
) GetObjectA( iconinfo
->hbmColor
, sizeof(bmpXor
), &bmpXor
);
1818 GetObjectA( iconinfo
->hbmMask
, sizeof(bmpAnd
), &bmpAnd
);
1820 sizeXor
= iconinfo
->hbmColor
? (bmpXor
.bmHeight
* bmpXor
.bmWidthBytes
) : 0;
1821 sizeAnd
= bmpAnd
.bmHeight
* bmpAnd
.bmWidthBytes
;
1823 hObj
= GlobalAlloc16( GMEM_MOVEABLE
,
1824 sizeof(CURSORICONINFO
) + sizeXor
+ sizeAnd
);
1827 CURSORICONINFO
*info
;
1829 info
= (CURSORICONINFO
*)GlobalLock16( hObj
);
1831 /* If we are creating an icon, the hotspot is unused */
1832 if (iconinfo
->fIcon
)
1834 info
->ptHotSpot
.x
= ICON_HOTSPOT
;
1835 info
->ptHotSpot
.y
= ICON_HOTSPOT
;
1839 info
->ptHotSpot
.x
= iconinfo
->xHotspot
;
1840 info
->ptHotSpot
.y
= iconinfo
->yHotspot
;
1843 if (iconinfo
->hbmColor
)
1845 info
->nWidth
= bmpXor
.bmWidth
;
1846 info
->nHeight
= bmpXor
.bmHeight
;
1847 info
->nWidthBytes
= bmpXor
.bmWidthBytes
;
1848 info
->bPlanes
= bmpXor
.bmPlanes
;
1849 info
->bBitsPerPixel
= bmpXor
.bmBitsPixel
;
1853 info
->nWidth
= bmpAnd
.bmWidth
;
1854 info
->nHeight
= bmpAnd
.bmHeight
/ 2;
1855 info
->nWidthBytes
= bmpAnd
.bmWidthBytes
;
1856 info
->bPlanes
= bmpAnd
.bmPlanes
;
1857 info
->bBitsPerPixel
= bmpAnd
.bmBitsPixel
;
1860 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1862 GetBitmapBits( iconinfo
->hbmMask
, sizeAnd
, (char*)(info
+ 1) );
1863 if (iconinfo
->hbmColor
) GetBitmapBits( iconinfo
->hbmColor
, sizeXor
, (char*)(info
+ 1) + sizeAnd
);
1864 GlobalUnlock16( hObj
);
1866 return HICON_32(hObj
);
1869 /******************************************************************************
1870 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1873 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1876 * hdc [I] Handle to device context
1877 * x0 [I] X coordinate of upper left corner
1878 * y0 [I] Y coordinate of upper left corner
1879 * hIcon [I] Handle to icon to draw
1880 * cxWidth [I] Width of icon
1881 * cyWidth [I] Height of icon
1882 * istep [I] Index of frame in animated cursor
1883 * hbr [I] Handle to background brush
1884 * flags [I] Icon-drawing flags
1890 BOOL WINAPI
DrawIconEx( HDC hdc
, INT x0
, INT y0
, HICON hIcon
,
1891 INT cxWidth
, INT cyWidth
, UINT istep
,
1892 HBRUSH hbr
, UINT flags
)
1894 CURSORICONINFO
*ptr
= (CURSORICONINFO
*)GlobalLock16(HICON_16(hIcon
));
1895 HDC hDC_off
= 0, hMemDC
;
1896 BOOL result
= FALSE
, DoOffscreen
;
1897 HBITMAP hB_off
= 0, hOld
= 0;
1899 if (!ptr
) return FALSE
;
1900 TRACE_(icon
)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1901 hdc
,x0
,y0
,hIcon
,cxWidth
,cyWidth
,istep
,hbr
,flags
);
1903 hMemDC
= CreateCompatibleDC (hdc
);
1905 FIXME_(icon
)("Ignoring istep=%d\n", istep
);
1906 if (flags
& DI_COMPAT
)
1907 FIXME_(icon
)("Ignoring flag DI_COMPAT\n");
1910 FIXME_(icon
)("no flags set? setting to DI_NORMAL\n");
1914 /* Calculate the size of the destination image. */
1917 if (flags
& DI_DEFAULTSIZE
)
1918 cxWidth
= GetSystemMetrics (SM_CXICON
);
1920 cxWidth
= ptr
->nWidth
;
1924 if (flags
& DI_DEFAULTSIZE
)
1925 cyWidth
= GetSystemMetrics (SM_CYICON
);
1927 cyWidth
= ptr
->nHeight
;
1930 DoOffscreen
= (GetObjectType( hbr
) == OBJ_BRUSH
);
1940 hDC_off
= CreateCompatibleDC(hdc
);
1941 hB_off
= CreateCompatibleBitmap(hdc
, cxWidth
, cyWidth
);
1942 if (hDC_off
&& hB_off
) {
1943 hOld
= SelectObject(hDC_off
, hB_off
);
1944 FillRect(hDC_off
, &r
, hbr
);
1948 if (hMemDC
&& (!DoOffscreen
|| (hDC_off
&& hB_off
)))
1950 HBITMAP hXorBits
, hAndBits
;
1951 COLORREF oldFg
, oldBg
;
1954 nStretchMode
= SetStretchBltMode (hdc
, STRETCH_DELETESCANS
);
1956 hXorBits
= CreateBitmap ( ptr
->nWidth
, ptr
->nHeight
,
1957 ptr
->bPlanes
, ptr
->bBitsPerPixel
,
1960 get_bitmap_width_bytes(ptr
->nWidth
,1) );
1961 hAndBits
= CreateBitmap ( ptr
->nWidth
, ptr
->nHeight
,
1962 1, 1, (char *)(ptr
+1) );
1963 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
1964 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
1966 if (hXorBits
&& hAndBits
)
1968 HBITMAP hBitTemp
= SelectObject( hMemDC
, hAndBits
);
1969 if (flags
& DI_MASK
)
1972 StretchBlt (hDC_off
, 0, 0, cxWidth
, cyWidth
,
1973 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCAND
);
1975 StretchBlt (hdc
, x0
, y0
, cxWidth
, cyWidth
,
1976 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCAND
);
1978 SelectObject( hMemDC
, hXorBits
);
1979 if (flags
& DI_IMAGE
)
1982 StretchBlt (hDC_off
, 0, 0, cxWidth
, cyWidth
,
1983 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCPAINT
);
1985 StretchBlt (hdc
, x0
, y0
, cxWidth
, cyWidth
,
1986 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCPAINT
);
1988 SelectObject( hMemDC
, hBitTemp
);
1992 SetTextColor( hdc
, oldFg
);
1993 SetBkColor( hdc
, oldBg
);
1994 if (hXorBits
) DeleteObject( hXorBits
);
1995 if (hAndBits
) DeleteObject( hAndBits
);
1996 SetStretchBltMode (hdc
, nStretchMode
);
1998 BitBlt(hdc
, x0
, y0
, cxWidth
, cyWidth
, hDC_off
, 0, 0, SRCCOPY
);
1999 SelectObject(hDC_off
, hOld
);
2002 if (hMemDC
) DeleteDC( hMemDC
);
2003 if (hDC_off
) DeleteDC(hDC_off
);
2004 if (hB_off
) DeleteObject(hB_off
);
2005 GlobalUnlock16(HICON_16(hIcon
));
2009 /***********************************************************************
2010 * DIB_FixColorsToLoadflags
2012 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2015 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
2018 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
2027 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
2029 WARN_(resource
)("Invalid bitmap\n");
2033 if (bpp
> 8) return;
2035 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
2043 colors
= bmi
->bmiHeader
.biClrUsed
;
2044 if (colors
> 256) colors
= 256;
2045 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
2048 c_W
= GetSysColor(COLOR_WINDOW
);
2049 c_S
= GetSysColor(COLOR_3DSHADOW
);
2050 c_F
= GetSysColor(COLOR_3DFACE
);
2051 c_L
= GetSysColor(COLOR_3DLIGHT
);
2053 if (loadflags
& LR_LOADTRANSPARENT
) {
2055 case 1: pix
= pix
>> 7; break;
2056 case 4: pix
= pix
>> 4; break;
2059 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
2062 if (pix
>= colors
) {
2063 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
2066 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
2067 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
2068 ptr
->rgbBlue
= GetBValue(c_W
);
2069 ptr
->rgbGreen
= GetGValue(c_W
);
2070 ptr
->rgbRed
= GetRValue(c_W
);
2072 if (loadflags
& LR_LOADMAP3DCOLORS
)
2073 for (i
=0; i
<colors
; i
++) {
2074 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
2075 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
2076 if (c_C
== RGB(128, 128, 128)) {
2077 ptr
->rgbRed
= GetRValue(c_S
);
2078 ptr
->rgbGreen
= GetGValue(c_S
);
2079 ptr
->rgbBlue
= GetBValue(c_S
);
2080 } else if (c_C
== RGB(192, 192, 192)) {
2081 ptr
->rgbRed
= GetRValue(c_F
);
2082 ptr
->rgbGreen
= GetGValue(c_F
);
2083 ptr
->rgbBlue
= GetBValue(c_F
);
2084 } else if (c_C
== RGB(223, 223, 223)) {
2085 ptr
->rgbRed
= GetRValue(c_L
);
2086 ptr
->rgbGreen
= GetGValue(c_L
);
2087 ptr
->rgbBlue
= GetBValue(c_L
);
2093 /**********************************************************************
2096 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
2097 INT desiredx
, INT desiredy
, UINT loadflags
)
2099 HBITMAP hbitmap
= 0, orig_bm
;
2103 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
2107 LONG width
, height
, new_width
, new_height
;
2111 HDC screen_mem_dc
= NULL
;
2113 if (!(loadflags
& LR_LOADFROMFILE
))
2117 /* OEM bitmap: try to load the resource from user32.dll */
2118 if (HIWORD(name
)) return 0;
2119 instance
= user32_module
;
2122 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
2123 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
2125 if ((info
= (BITMAPINFO
*)LockResource( handle
)) == NULL
) return 0;
2129 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
2130 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
2133 size
= bitmap_info_size(info
, DIB_RGB_COLORS
);
2134 fix_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2135 scaled_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2137 if (!fix_info
|| !scaled_info
) goto end
;
2138 memcpy(fix_info
, info
, size
);
2140 pix
= *((LPBYTE
)info
+ size
);
2141 DIB_FixColorsToLoadflags(fix_info
, loadflags
, pix
);
2143 memcpy(scaled_info
, fix_info
, size
);
2144 bm_type
= DIB_GetBitmapInfo( &fix_info
->bmiHeader
, &width
, &height
,
2145 &bpp_dummy
, &compr_dummy
);
2147 new_width
= desiredx
;
2152 new_height
= height
> 0 ? desiredy
: -desiredy
;
2154 new_height
= height
;
2158 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
2159 core
->bcWidth
= new_width
;
2160 core
->bcHeight
= new_height
;
2164 scaled_info
->bmiHeader
.biWidth
= new_width
;
2165 scaled_info
->bmiHeader
.biHeight
= new_height
;
2168 if (new_height
< 0) new_height
= -new_height
;
2170 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2171 if (!(screen_mem_dc
= CreateCompatibleDC( screen_dc
))) goto end
;
2173 bits
= (char *)info
+ size
;
2175 if (loadflags
& LR_CREATEDIBSECTION
)
2177 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
2178 hbitmap
= CreateDIBSection(screen_dc
, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
2182 if (is_dib_monochrome(fix_info
))
2183 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
2185 hbitmap
= CreateCompatibleBitmap(screen_dc
, new_width
, new_height
);
2188 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
2189 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
2190 SelectObject(screen_mem_dc
, orig_bm
);
2193 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
2194 HeapFree(GetProcessHeap(), 0, scaled_info
);
2195 HeapFree(GetProcessHeap(), 0, fix_info
);
2196 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
2201 /**********************************************************************
2202 * LoadImageA (USER32.@)
2207 /* filter for page-fault exceptions */
2208 static WINE_EXCEPTION_FILTER(page_fault
)
2210 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
2211 return EXCEPTION_EXECUTE_HANDLER
;
2212 return EXCEPTION_CONTINUE_SEARCH
;
2215 /*********************************************************************/
2217 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
2218 INT desiredx
, INT desiredy
, UINT loadflags
)
2224 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
2227 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
2228 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
2229 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
2231 __EXCEPT(page_fault
) {
2232 SetLastError( ERROR_INVALID_PARAMETER
);
2236 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
2237 HeapFree(GetProcessHeap(), 0, u_name
);
2242 /******************************************************************************
2243 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2246 * hinst [I] Handle of instance that contains image
2247 * name [I] Name of image
2248 * type [I] Type of image
2249 * desiredx [I] Desired width
2250 * desiredy [I] Desired height
2251 * loadflags [I] Load flags
2254 * Success: Handle to newly loaded image
2257 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2259 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
2260 INT desiredx
, INT desiredy
, UINT loadflags
)
2262 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
2263 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
2265 if (loadflags
& LR_DEFAULTSIZE
) {
2266 if (type
== IMAGE_ICON
) {
2267 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXICON
);
2268 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYICON
);
2269 } else if (type
== IMAGE_CURSOR
) {
2270 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXCURSOR
);
2271 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYCURSOR
);
2274 if (loadflags
& LR_LOADFROMFILE
) loadflags
&= ~LR_SHARED
;
2277 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
2280 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2283 UINT palEnts
= GetSystemPaletteEntries(screen_dc
, 0, 0, NULL
);
2284 if (palEnts
== 0) palEnts
= 256;
2285 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
2286 palEnts
, FALSE
, loadflags
);
2291 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
2292 1, TRUE
, loadflags
);
2297 /******************************************************************************
2298 * CopyImage (USER32.@) Creates new image and copies attributes to it
2301 * hnd [I] Handle to image to copy
2302 * type [I] Type of image to copy
2303 * desiredx [I] Desired width of new image
2304 * desiredy [I] Desired height of new image
2305 * flags [I] Copy flags
2308 * Success: Handle to newly created image
2312 * implementation still lacks nearly all features, see LR_* defines in winuser.h.
2314 HICON WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
2315 INT desiredy
, UINT flags
)
2324 if (!GetObjectW( hnd
, sizeof(bm
), &bm
)) return 0;
2326 if ((res
= CreateBitmapIndirect(&bm
)))
2328 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
* bm
.bmHeight
);
2329 GetBitmapBits( hnd
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
2330 SetBitmapBits( res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
2331 HeapFree( GetProcessHeap(), 0, buf
);
2336 return CURSORICON_ExtCopy(hnd
,type
, desiredx
, desiredy
, flags
);
2338 /* Should call CURSORICON_ExtCopy but more testing
2339 * needs to be done before we change this
2341 return CopyCursor(hnd
);
2347 /******************************************************************************
2348 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2351 * Success: Handle to specified bitmap
2354 HBITMAP WINAPI
LoadBitmapW(
2355 HINSTANCE instance
, /* [in] Handle to application instance */
2356 LPCWSTR name
) /* [in] Address of bitmap resource name */
2358 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
2361 /**********************************************************************
2362 * LoadBitmapA (USER32.@)
2366 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
2368 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );