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
28 * 32-bit cursors and icons are stored in the server.
30 * 16-bit cursors and icons are stored in a global heap block, with the
33 * CURSORICONINFO info;
37 * The bits structures are in the format of a device-dependent bitmap.
39 * This layout is very sub-optimal, as the bitmap bits are stored in
40 * the X client instead of in the server like other bitmaps; however,
41 * some programs (notably Paint Brush) expect to be able to manipulate
42 * the bits directly :-(
46 #include "wine/port.h"
58 #include "wine/winbase16.h"
59 #include "wine/winuser16.h"
60 #include "wine/exception.h"
61 #include "wine/debug.h"
62 #include "wine/list.h"
63 #include "wine/server.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 const WORD ICON_HOTSPOT
= 0x4242;
136 /* What is a good table size? */
137 #define CURSOR_HASH_SIZE 97
144 } cursor_map_entry_t
;
146 static int get_bitmap_width_bytes( int width
, int bpp
);
148 static struct list cursor16to32
[CURSOR_HASH_SIZE
];
149 static struct list cursor32to16
[CURSOR_HASH_SIZE
];
151 static inline int hash_cursor_handle( DWORD handle
)
153 return handle
% CURSOR_HASH_SIZE
;
156 static void add_cursor16to32_entry( cursor_map_entry_t
*entry
)
158 int idx
= hash_cursor_handle( entry
->cursor16
);
160 if (!cursor16to32
[idx
].next
) list_init( &cursor16to32
[idx
] );
162 list_add_head( &cursor16to32
[idx
], &entry
->entry16
);
165 static void add_cursor32to16_entry( cursor_map_entry_t
*entry
)
167 int idx
= hash_cursor_handle( (DWORD
)entry
->cursor32
);
169 if (!cursor32to16
[idx
].next
) list_init( &cursor32to16
[idx
] );
171 list_add_head( &cursor32to16
[idx
], &entry
->entry32
);
174 static cursor_map_entry_t
*remove_cursor16to32_entry( HCURSOR16 cursor16
)
176 cursor_map_entry_t
*entry
= NULL
;
177 int idx
= hash_cursor_handle( cursor16
);
179 if (cursor16to32
[idx
].next
)
181 LIST_FOR_EACH_ENTRY( entry
, &cursor16to32
[idx
], cursor_map_entry_t
, entry16
)
182 if (entry
->cursor16
== cursor16
)
184 list_remove( &entry
->entry16
);
192 static cursor_map_entry_t
*remove_cursor32to16_entry( HCURSOR cursor32
)
194 cursor_map_entry_t
*entry
= NULL
;
195 int idx
= hash_cursor_handle( (DWORD
)cursor32
);
197 if (cursor32to16
[idx
].next
)
199 LIST_FOR_EACH_ENTRY( entry
, &cursor32to16
[idx
], cursor_map_entry_t
, entry32
)
200 if (entry
->cursor32
== cursor32
)
202 list_remove( &entry
->entry32
);
210 /* Ask the server for a cursor */
211 static HCURSOR
create_cursor( unsigned int num_frames
, unsigned int delay
)
215 SERVER_START_REQ(create_cursor
)
217 req
->num_frames
= num_frames
;
219 if (!wine_server_call_err( req
)) cursor
= reply
->handle
;
226 /* Tell the server to kill a cursor */
227 static HCURSOR16
destroy_cursor( HCURSOR cursor
)
229 cursor_map_entry_t
*entry
;
230 HCURSOR16 cursor16
= 0;
232 if (!cursor
) return 0;
234 SERVER_START_REQ(destroy_cursor
)
236 req
->handle
= cursor
;
237 wine_server_call( req
);
241 entry
= remove_cursor32to16_entry( cursor
);
244 cursor16
= entry
->cursor16
;
245 remove_cursor16to32_entry( cursor16
);
246 HeapFree( GetProcessHeap(), 0, entry
);
249 return GlobalFree16( cursor16
);
252 /* Upload a cursor frame to the server */
253 static void set_cursor_frame( HCURSOR cursor
, unsigned int frame_idx
, cursor_frame_t
*frame
)
255 SERVER_START_REQ(set_cursor_frame
)
257 req
->handle
= cursor
;
258 req
->frame_idx
= frame_idx
;
259 req
->xhot
= frame
->xhot
;
260 req
->yhot
= frame
->yhot
;
261 req
->width
= frame
->width
;
262 req
->height
= frame
->height
;
263 req
->and_width_bytes
= frame
->and_width_bytes
;
264 req
->xor_width_bytes
= frame
->xor_width_bytes
;
265 req
->planes
= frame
->planes
;
266 req
->bpp
= frame
->bpp
;
267 wine_server_add_data( req
, frame
->bits
, (frame
->and_width_bytes
+ frame
->xor_width_bytes
) * frame
->height
);
268 wine_server_call( req
);
273 /* Download a cursor frame from the server */
274 static BOOL
get_cursor_frame( HCURSOR cursor
, unsigned int frame_idx
, cursor_frame_t
*frame
)
277 /* Enough for a 32-bits 32x32 cursor / icon. */
278 unsigned int buffer_size
= 4224;
279 unsigned int count
= 0;
283 frame
->bits
= HeapAlloc(GetProcessHeap(), 0, buffer_size
);
284 SERVER_START_REQ(get_cursor_frame
)
286 req
->handle
= cursor
;
287 req
->frame_idx
= frame_idx
;
288 wine_server_set_reply( req
, frame
->bits
, buffer_size
);
289 if (!(res
= wine_server_call_err( req
)))
291 frame
->xhot
= reply
->xhot
;
292 frame
->yhot
= reply
->yhot
;
293 frame
->width
= reply
->width
;
294 frame
->height
= reply
->height
;
295 frame
->and_width_bytes
= reply
->and_width_bytes
;
296 frame
->xor_width_bytes
= reply
->xor_width_bytes
;
297 frame
->planes
= reply
->planes
;
298 frame
->bpp
= reply
->bpp
;
300 HeapFree( GetProcessHeap(), 0, frame
->bits
);
301 buffer_size
= (reply
->and_width_bytes
+ reply
->xor_width_bytes
) * reply
->height
;
305 } while (res
== STATUS_BUFFER_OVERFLOW
&& !count
++);
309 HeapFree( GetProcessHeap(), 0, frame
->bits
);
317 /* Retrieve a cursor and all its frames from the server */
318 static cursor_t
*get_cursor_object( HCURSOR handle
)
321 cursor_t
*cursor
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(cursor_t
) );
323 SERVER_START_REQ(get_cursor_info
)
325 req
->handle
= handle
;
326 if (!wine_server_call_err( req
))
328 cursor
->num_frames
= reply
->num_frames
;
329 cursor
->delay
= reply
->delay
;
334 if (!cursor
->num_frames
)
336 HeapFree( GetProcessHeap(), 0, cursor
);
340 cursor
->frames
= HeapAlloc( GetProcessHeap(), 0, cursor
->num_frames
* sizeof(cursor_frame_t
) );
341 for (i
= 0; i
< cursor
->num_frames
; ++i
)
343 if (!get_cursor_frame( handle
, i
, &cursor
->frames
[i
] ))
347 for (j
= 0; j
< i
; ++j
)
349 HeapFree( GetProcessHeap(), 0, cursor
->frames
[j
].bits
);
351 HeapFree( GetProcessHeap(), 0, cursor
->frames
);
352 HeapFree( GetProcessHeap(), 0, cursor
);
361 static void destroy_cursor_object( cursor_t
*cursor
)
367 for (i
= 0; i
< cursor
->num_frames
; ++i
)
369 HeapFree( GetProcessHeap(), 0, cursor
->frames
[i
].bits
);
371 HeapFree( GetProcessHeap(), 0, cursor
->frames
);
372 HeapFree( GetProcessHeap(), 0, cursor
);
375 /* Lookup the cursor's 16-bit handle. Create one if it doesn't already exist. */
376 HCURSOR16
get_cursor_handle16( HCURSOR cursor32
, BOOL create
)
378 cursor_map_entry_t
*entry
;
379 int idx
= hash_cursor_handle( (DWORD
)cursor32
);
381 if (!cursor32
) return 0;
383 if (cursor32to16
[idx
].next
)
385 LIST_FOR_EACH_ENTRY( entry
, &cursor32to16
[idx
], cursor_map_entry_t
, entry32
)
386 if (entry
->cursor32
== cursor32
) return entry
->cursor16
;
389 /* 16-bit cursor handle not found, create one */
394 cursor_frame_t frame
;
396 if (!get_cursor_frame( cursor32
, 0, &frame
)) return 0;
398 entry
= HeapAlloc( GetProcessHeap(), 0, sizeof(cursor_map_entry_t
) );
399 bits_size
= (frame
.and_width_bytes
+ frame
.xor_width_bytes
) * frame
.height
;
400 cursor16
= GlobalAlloc16( GMEM_MOVEABLE
, sizeof(CURSORICONINFO
) + bits_size
);
403 CURSORICONINFO
*info
;
405 info
= (CURSORICONINFO
*)GlobalLock16( cursor16
);
406 info
->ptHotSpot
.x
= frame
.xhot
;
407 info
->ptHotSpot
.y
= frame
.yhot
;
408 info
->nWidth
= frame
.width
;
409 info
->nHeight
= frame
.height
;
410 info
->nWidthBytes
= frame
.xor_width_bytes
;
411 info
->bPlanes
= frame
.planes
;
412 info
->bBitsPerPixel
= frame
.bpp
;
413 CopyMemory( info
+ 1, frame
.bits
, bits_size
);
414 GlobalUnlock16( cursor16
);
416 HeapFree( GetProcessHeap(), 0, frame
.bits
);
418 entry
->cursor16
= cursor16
;
419 entry
->cursor32
= cursor32
;
420 add_cursor16to32_entry( entry
);
421 add_cursor32to16_entry( entry
);
429 HCURSOR
get_cursor_handle32( HCURSOR16 cursor16
)
431 cursor_map_entry_t
*entry
;
432 int idx
= hash_cursor_handle( cursor16
);
434 if (!cursor16
) return 0;
436 if (cursor16to32
[idx
].next
)
438 LIST_FOR_EACH_ENTRY( entry
, &cursor16to32
[idx
], cursor_map_entry_t
, entry16
)
439 if (entry
->cursor16
== cursor16
) return entry
->cursor32
;
445 static void update_cursor_32from16( HCURSOR cursor32
)
449 cursor_frame_t frame
;
450 CURSORICONINFO
*info
;
452 if (!cursor32
) return;
454 cursor16
= get_cursor_handle16( cursor32
, FALSE
);
455 if (!cursor16
) return;
457 info
= (CURSORICONINFO
*)GlobalLock16( cursor16
);
458 frame
.xhot
= info
->ptHotSpot
.x
;
459 frame
.yhot
= info
->ptHotSpot
.y
;
460 frame
.width
= info
->nWidth
;
461 frame
.height
= info
->nHeight
;
462 frame
.and_width_bytes
= get_bitmap_width_bytes( info
->nWidth
, 1 );
463 frame
.xor_width_bytes
= info
->nWidthBytes
;
464 frame
.planes
= info
->bPlanes
;
465 frame
.bpp
= info
->bBitsPerPixel
;
466 bits_size
= (frame
.and_width_bytes
+ frame
.xor_width_bytes
) * frame
.height
;
467 frame
.bits
= HeapAlloc( GetProcessHeap(), 0, bits_size
);
468 CopyMemory( frame
.bits
, info
+ 1, bits_size
);
469 GlobalUnlock16( cursor16
);
471 set_cursor_frame( cursor32
, 0, &frame
);
472 HeapFree( GetProcessHeap(), 0, frame
.bits
);
475 /***********************************************************************
478 * Helper function to map a file to memory:
480 * [RETURN] ptr - pointer to mapped file
481 * [RETURN] filesize - pointer size of file to be stored if not NULL
483 static void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
485 HANDLE hFile
, hMapping
;
488 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
489 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
490 if (hFile
!= INVALID_HANDLE_VALUE
)
492 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
495 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
496 CloseHandle( hMapping
);
498 *filesize
= GetFileSize( hFile
, NULL
);
500 CloseHandle( hFile
);
506 /***********************************************************************
507 * get_bitmap_width_bytes
509 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
512 static int get_bitmap_width_bytes( int width
, int bpp
)
517 return 2 * ((width
+15) / 16);
519 return 2 * ((width
+3) / 4);
524 return width
+ (width
& 1);
531 WARN("Unknown depth %d, please report.\n", bpp
);
537 /***********************************************************************
538 * get_dib_width_bytes
540 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
542 static int get_dib_width_bytes( int width
, int depth
)
548 case 1: words
= (width
+ 31) / 32; break;
549 case 4: words
= (width
+ 7) / 8; break;
550 case 8: words
= (width
+ 3) / 4; break;
552 case 16: words
= (width
+ 1) / 2; break;
553 case 24: words
= (width
* 3 + 3)/4; break;
555 WARN("(%d): Unsupported depth\n", depth
);
564 /***********************************************************************
567 * Return the size of the bitmap info structure including color table.
569 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
571 int colors
, masks
= 0;
573 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
575 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
576 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
577 return sizeof(BITMAPCOREHEADER
) + colors
*
578 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
580 else /* assume BITMAPINFOHEADER */
582 colors
= info
->bmiHeader
.biClrUsed
;
583 if (colors
> 256) /* buffer overflow otherwise */
585 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
586 colors
= 1 << info
->bmiHeader
.biBitCount
;
587 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
588 return sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) + colors
*
589 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
594 /***********************************************************************
597 * Returns whether a DIB can be converted to a monochrome DDB.
599 * A DIB can be converted if its color table contains only black and
600 * white. Black must be the first color in the color table.
602 * Note : If the first color in the color table is white followed by
603 * black, we can't convert it to a monochrome DDB with
604 * SetDIBits, because black and white would be inverted.
606 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
608 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
610 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
612 const RGBTRIPLE
*rgb
= ((const BITMAPCOREINFO
*)info
)->bmciColors
;
614 /* Check if the first color is black */
615 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
619 /* Check if the second color is white */
620 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
621 && (rgb
->rgbtBlue
== 0xff));
625 else /* assume BITMAPINFOHEADER */
627 const RGBQUAD
*rgb
= info
->bmiColors
;
629 /* Check if the first color is black */
630 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
631 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
635 /* Check if the second color is white */
636 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
637 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
643 /***********************************************************************
646 * Get the info from a bitmap header.
647 * Return 1 for INFOHEADER, 0 for COREHEADER,
648 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
650 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
651 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
653 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
655 *width
= header
->biWidth
;
656 *height
= header
->biHeight
;
657 *bpp
= header
->biBitCount
;
658 *compr
= header
->biCompression
;
661 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
663 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
664 *width
= core
->bcWidth
;
665 *height
= core
->bcHeight
;
666 *bpp
= core
->bcBitCount
;
670 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
672 const BITMAPV4HEADER
*v4hdr
= (const BITMAPV4HEADER
*)header
;
673 *width
= v4hdr
->bV4Width
;
674 *height
= v4hdr
->bV4Height
;
675 *bpp
= v4hdr
->bV4BitCount
;
676 *compr
= v4hdr
->bV4V4Compression
;
679 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
681 const BITMAPV5HEADER
*v5hdr
= (const BITMAPV5HEADER
*)header
;
682 *width
= v5hdr
->bV5Width
;
683 *height
= v5hdr
->bV5Height
;
684 *bpp
= v5hdr
->bV5BitCount
;
685 *compr
= v5hdr
->bV5Compression
;
688 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
692 /**********************************************************************
693 * CURSORICON_FindSharedIcon
695 static HICON
CURSORICON_FindSharedIcon( HMODULE hModule
, HRSRC hRsrc
)
700 EnterCriticalSection( &IconCrst
);
702 for ( ptr
= IconAnchor
; ptr
; ptr
= ptr
->next
)
703 if ( ptr
->hModule
== hModule
&& ptr
->hRsrc
== hRsrc
)
710 LeaveCriticalSection( &IconCrst
);
715 /*************************************************************************
716 * CURSORICON_FindCache
718 * Given a handle, find the corresponding cache element
721 * Handle [I] handle to an Image
724 * Success: The cache entry
728 static ICONCACHE
* CURSORICON_FindCache(HICON hIcon
)
731 ICONCACHE
*pRet
=NULL
;
732 BOOL IsFound
= FALSE
;
734 EnterCriticalSection( &IconCrst
);
736 for (ptr
= IconAnchor
; ptr
!= NULL
&& !IsFound
; ptr
= ptr
->next
)
738 if ( hIcon
== ptr
->hIcon
)
745 LeaveCriticalSection( &IconCrst
);
750 /**********************************************************************
751 * CURSORICON_AddSharedIcon
753 static void CURSORICON_AddSharedIcon( HMODULE hModule
, HRSRC hRsrc
, HRSRC hGroupRsrc
, HICON hIcon
)
755 ICONCACHE
*ptr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE
) );
758 ptr
->hModule
= hModule
;
761 ptr
->hGroupRsrc
= hGroupRsrc
;
764 EnterCriticalSection( &IconCrst
);
765 ptr
->next
= IconAnchor
;
767 LeaveCriticalSection( &IconCrst
);
770 /**********************************************************************
771 * CURSORICON_DelSharedIcon
773 static INT
CURSORICON_DelSharedIcon( HICON hIcon
)
778 EnterCriticalSection( &IconCrst
);
780 for ( ptr
= IconAnchor
; ptr
; ptr
= ptr
->next
)
781 if ( ptr
->hIcon
== hIcon
)
783 if ( ptr
->count
> 0 ) ptr
->count
--;
788 LeaveCriticalSection( &IconCrst
);
793 /**********************************************************************
794 * CURSORICON_FreeModuleIcons
796 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16
)
798 ICONCACHE
**ptr
= &IconAnchor
;
799 HMODULE hModule
= HMODULE_32(GetExePtr( hMod16
));
801 EnterCriticalSection( &IconCrst
);
805 if ( (*ptr
)->hModule
== hModule
)
807 ICONCACHE
*freePtr
= *ptr
;
808 *ptr
= freePtr
->next
;
810 destroy_cursor( freePtr
->hIcon
);
811 HeapFree( GetProcessHeap(), 0, freePtr
);
817 LeaveCriticalSection( &IconCrst
);
821 * The following macro functions account for the irregularities of
822 * accessing cursor and icon resources in files and resource entries.
824 typedef BOOL (*fnGetCIEntry
)( LPVOID dir
, int n
,
825 int *width
, int *height
, int *bits
);
827 /**********************************************************************
828 * CURSORICON_FindBestIcon
830 * Find the icon closest to the requested size and number of colors.
832 static int CURSORICON_FindBestIcon( LPVOID dir
, fnGetCIEntry get_entry
,
833 int width
, int height
, int colors
)
835 int i
, cx
, cy
, bits
, bestEntry
= -1;
836 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
837 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
840 iTotalDiff
= 0xFFFFFFFF;
841 iColorDiff
= 0xFFFFFFFF;
842 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
844 iTempXDiff
= abs(width
- cx
);
845 iTempYDiff
= abs(height
- cy
);
847 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
851 iTotalDiff
= iXDiff
+ iYDiff
;
855 /* Find Best Colors for Best Fit */
856 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
858 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
860 iTempColorDiff
= abs(colors
- (1<<bits
));
861 if(iColorDiff
> iTempColorDiff
)
864 iColorDiff
= iTempColorDiff
;
872 static BOOL
CURSORICON_GetResIconEntry( LPVOID dir
, int n
,
873 int *width
, int *height
, int *bits
)
875 CURSORICONDIR
*resdir
= dir
;
878 if ( resdir
->idCount
<= n
)
880 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
881 *width
= icon
->bWidth
;
882 *height
= icon
->bHeight
;
883 *bits
= resdir
->idEntries
[n
].wBitCount
;
887 /**********************************************************************
888 * CURSORICON_FindBestCursor
890 * Find the cursor closest to the requested size.
891 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
894 static int CURSORICON_FindBestCursor( LPVOID dir
, fnGetCIEntry get_entry
,
895 int width
, int height
, int color
)
897 int i
, maxwidth
, maxheight
, cx
, cy
, bits
, bestEntry
= -1;
899 /* Double height to account for AND and XOR masks */
903 /* First find the largest one smaller than or equal to the requested size*/
905 maxwidth
= maxheight
= 0;
906 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
908 if ((cx
<= width
) && (cy
<= height
) &&
909 (cx
> maxwidth
) && (cy
> maxheight
) &&
917 if (bestEntry
!= -1) return bestEntry
;
919 /* Now find the smallest one larger than the requested size */
921 maxwidth
= maxheight
= 255;
922 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
924 if (((cx
< maxwidth
) && (cy
< maxheight
) && (bits
== 1)) ||
936 static BOOL
CURSORICON_GetResCursorEntry( LPVOID dir
, int n
,
937 int *width
, int *height
, int *bits
)
939 CURSORICONDIR
*resdir
= dir
;
942 if ( resdir
->idCount
<= n
)
944 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
945 *width
= cursor
->wWidth
;
946 *height
= cursor
->wHeight
;
947 *bits
= resdir
->idEntries
[n
].wBitCount
;
951 static CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( CURSORICONDIR
* dir
,
952 int width
, int height
, int colors
)
956 n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetResIconEntry
,
957 width
, height
, colors
);
960 return &dir
->idEntries
[n
];
963 static CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( CURSORICONDIR
*dir
,
964 int width
, int height
, int color
)
966 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetResCursorEntry
,
967 width
, height
, color
);
970 return &dir
->idEntries
[n
];
973 static BOOL
CURSORICON_GetFileEntry( LPVOID dir
, int n
,
974 int *width
, int *height
, int *bits
)
976 CURSORICONFILEDIR
*filedir
= dir
;
977 CURSORICONFILEDIRENTRY
*entry
;
979 if ( filedir
->idCount
<= n
)
981 entry
= &filedir
->idEntries
[n
];
982 *width
= entry
->bWidth
;
983 *height
= entry
->bHeight
;
984 *bits
= entry
->bColorCount
;
988 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( CURSORICONFILEDIR
*dir
,
989 int width
, int height
, int color
)
991 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetFileEntry
,
992 width
, height
, color
);
995 return &dir
->idEntries
[n
];
998 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( CURSORICONFILEDIR
*dir
,
999 int width
, int height
, int color
)
1001 int n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetFileEntry
,
1002 width
, height
, color
);
1005 return &dir
->idEntries
[n
];
1008 static BOOL
load_cursor_frame( LPBYTE bits
, UINT cbSize
, POINT16 hotspot
, DWORD dwVersion
,
1009 INT width
, INT height
, UINT cFlag
, cursor_frame_t
*frame
)
1012 int sizeAnd
, sizeXor
;
1013 HBITMAP hAndBits
= 0, hXorBits
= 0; /* error condition for later */
1014 BITMAP bmpXor
, bmpAnd
;
1019 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s\n",
1020 bits
, cbSize
, dwVersion
, width
, height
,
1021 (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
1022 if (dwVersion
== 0x00020000)
1024 FIXME_(cursor
)("\t2.xx resources are not supported\n");
1028 bmi
= (BITMAPINFO
*)bits
;
1030 /* Check bitmap header */
1032 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
1033 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
1034 bmi
->bmiHeader
.biCompression
!= BI_RGB
) )
1036 WARN_(cursor
)("\tinvalid resource bitmap header.\n");
1040 size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
1042 if (!width
) width
= bmi
->bmiHeader
.biWidth
;
1043 if (!height
) height
= bmi
->bmiHeader
.biHeight
/2;
1044 DoStretch
= (bmi
->bmiHeader
.biHeight
/2 != height
) ||
1045 (bmi
->bmiHeader
.biWidth
!= width
);
1047 /* Scale the hotspot */
1048 if (DoStretch
&& hotspot
.x
!= ICON_HOTSPOT
&& hotspot
.y
!= ICON_HOTSPOT
)
1050 hotspot
.x
= (hotspot
.x
* width
) / bmi
->bmiHeader
.biWidth
;
1051 hotspot
.y
= (hotspot
.y
* height
) / (bmi
->bmiHeader
.biWidth
/ 2);
1054 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
1059 /* Make sure we have room for the monochrome bitmap later on.
1060 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1061 * up to and including the biBitCount. In-memory icon resource
1062 * format is as follows:
1064 * BITMAPINFOHEADER icHeader // DIB header
1065 * RGBQUAD icColors[] // Color table
1066 * BYTE icXOR[] // DIB bits for XOR mask
1067 * BYTE icAND[] // DIB bits for AND mask
1070 if ((pInfo
= HeapAlloc( GetProcessHeap(), 0,
1071 max(size
, sizeof(BITMAPINFOHEADER
) + 2*sizeof(RGBQUAD
)))))
1073 memcpy( pInfo
, bmi
, size
);
1074 pInfo
->bmiHeader
.biHeight
/= 2;
1076 /* Create the XOR bitmap */
1079 hXorBits
= CreateCompatibleBitmap(screen_dc
, width
, height
);
1085 if (!hdcMem
) hdcMem
= CreateCompatibleDC(screen_dc
);
1087 hOld
= SelectObject(hdcMem
, hXorBits
);
1088 res
= StretchDIBits(hdcMem
, 0, 0, width
, height
, 0, 0,
1089 bmi
->bmiHeader
.biWidth
, bmi
->bmiHeader
.biHeight
/2,
1090 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
, SRCCOPY
);
1091 SelectObject(hdcMem
, hOld
);
1093 if (!res
) { DeleteObject(hXorBits
); hXorBits
= 0; }
1096 if (is_dib_monochrome(bmi
)) {
1097 hXorBits
= CreateBitmap(width
, height
, 1, 1, NULL
);
1098 SetDIBits(screen_dc
, hXorBits
, 0, height
,
1099 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
1100 } else if (bmi
->bmiHeader
.biBitCount
== 32) {
1101 hXorBits
= CreateDIBSection(screen_dc
, pInfo
, DIB_RGB_COLORS
, NULL
, NULL
, 0);
1102 SetDIBits(screen_dc
, hXorBits
, 0, height
,
1103 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
1106 hXorBits
= CreateDIBitmap(screen_dc
, &pInfo
->bmiHeader
,
1107 CBM_INIT
, (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
1112 char* xbits
= (char *)bmi
+ size
+
1113 get_dib_width_bytes( bmi
->bmiHeader
.biWidth
,
1114 bmi
->bmiHeader
.biBitCount
) * abs( bmi
->bmiHeader
.biHeight
) / 2;
1116 pInfo
->bmiHeader
.biBitCount
= 1;
1117 if (pInfo
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
1119 RGBQUAD
*rgb
= pInfo
->bmiColors
;
1121 pInfo
->bmiHeader
.biClrUsed
= pInfo
->bmiHeader
.biClrImportant
= 2;
1122 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
1123 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
1124 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
1128 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)pInfo
) + 1);
1130 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
1131 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
1134 /* Create the AND bitmap */
1137 if ((hAndBits
= CreateBitmap(width
, height
, 1, 1, NULL
))) {
1141 if (!hdcMem
) hdcMem
= CreateCompatibleDC(screen_dc
);
1143 hOld
= SelectObject(hdcMem
, hAndBits
);
1144 res
= StretchDIBits(hdcMem
, 0, 0, width
, height
, 0, 0,
1145 pInfo
->bmiHeader
.biWidth
, pInfo
->bmiHeader
.biHeight
,
1146 xbits
, pInfo
, DIB_RGB_COLORS
, SRCCOPY
);
1147 SelectObject(hdcMem
, hOld
);
1149 if (!res
) { DeleteObject(hAndBits
); hAndBits
= 0; }
1152 hAndBits
= CreateBitmap(width
, height
, 1, 1, NULL
);
1154 if (hAndBits
) SetDIBits(screen_dc
, hAndBits
, 0, height
,
1155 xbits
, pInfo
, DIB_RGB_COLORS
);
1158 if( !hAndBits
) DeleteObject( hXorBits
);
1160 HeapFree( GetProcessHeap(), 0, pInfo
);
1164 if( !hXorBits
|| !hAndBits
)
1166 WARN_(cursor
)("\tunable to create an icon bitmap.\n");
1170 /* Setup a cursor frame, send it to the server */
1171 GetObjectA( hXorBits
, sizeof(bmpXor
), &bmpXor
);
1172 GetObjectA( hAndBits
, sizeof(bmpAnd
), &bmpAnd
);
1173 sizeXor
= bmpXor
.bmHeight
* bmpXor
.bmWidthBytes
;
1174 sizeAnd
= bmpAnd
.bmHeight
* bmpAnd
.bmWidthBytes
;
1176 frame
->xhot
= hotspot
.x
;
1177 frame
->yhot
= hotspot
.y
;
1178 frame
->width
= bmpXor
.bmWidth
;
1179 frame
->height
= bmpXor
.bmHeight
;
1180 frame
->and_width_bytes
= bmpAnd
.bmWidthBytes
;
1181 frame
->xor_width_bytes
= bmpXor
.bmWidthBytes
;
1182 frame
->planes
= bmpXor
.bmPlanes
;
1183 frame
->bpp
= bmpXor
.bmBitsPixel
;
1184 frame
->bits
= HeapAlloc( GetProcessHeap(), 0, sizeAnd
+ sizeXor
);
1185 GetBitmapBits( hAndBits
, sizeAnd
, frame
->bits
);
1186 GetBitmapBits( hXorBits
, sizeXor
, frame
->bits
+ sizeAnd
);
1188 DeleteObject( hAndBits
);
1189 DeleteObject( hXorBits
);
1194 /**********************************************************************
1195 * .ANI cursor support
1197 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1198 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1199 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1201 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1202 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1203 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1204 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1205 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1206 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1208 #define ANI_FLAG_ICON 0x1
1209 #define ANI_FLAG_SEQUENCE 0x2
1225 const unsigned char *data
;
1228 static void dump_ani_header( const ani_header
*header
)
1230 TRACE(" header size: %d\n", header
->header_size
);
1231 TRACE(" frames: %d\n", header
->num_frames
);
1232 TRACE(" steps: %d\n", header
->num_steps
);
1233 TRACE(" width: %d\n", header
->width
);
1234 TRACE(" height: %d\n", header
->height
);
1235 TRACE(" bpp: %d\n", header
->bpp
);
1236 TRACE(" planes: %d\n", header
->num_planes
);
1237 TRACE(" display rate: %d\n", header
->display_rate
);
1238 TRACE(" flags: 0x%08x\n", header
->flags
);
1260 static void riff_find_chunk( DWORD chunk_id
, DWORD chunk_type
, const riff_chunk_t
*parent_chunk
, riff_chunk_t
*chunk
)
1262 const unsigned char *ptr
= parent_chunk
->data
;
1263 const unsigned char *end
= parent_chunk
->data
+ (parent_chunk
->data_size
- (2 * sizeof(DWORD
)));
1265 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) end
-= sizeof(DWORD
);
1269 if ((!chunk_type
&& *(DWORD
*)ptr
== chunk_id
)
1270 || (chunk_type
&& *(DWORD
*)ptr
== chunk_type
&& *((DWORD
*)ptr
+ 2) == chunk_id
))
1272 ptr
+= sizeof(DWORD
);
1273 chunk
->data_size
= *(DWORD
*)ptr
;
1274 ptr
+= sizeof(DWORD
);
1275 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) ptr
+= sizeof(DWORD
);
1281 ptr
+= sizeof(DWORD
);
1282 ptr
+= *(DWORD
*)ptr
;
1283 ptr
+= sizeof(DWORD
);
1291 * RIFF:'ACON' RIFF chunk
1292 * |- CHUNK:'anih' Header
1293 * |- CHUNK:'seq ' Sequence information (optional)
1294 * \- LIST:'fram' Frame list
1295 * |- CHUNK:icon Cursor frames
1300 static HCURSOR
load_ani( const LPBYTE bits
, DWORD bits_size
, INT width
, INT height
)
1305 CURSORICONFILEDIR
*dir
= 0;
1306 ani_header header
= {0};
1307 DWORD
*frame_seq
= 0;
1308 cursor_frame_t
*frames
;
1309 unsigned int frame_bits_size
= 0;
1310 LPBYTE frame_bits
= 0;
1313 riff_chunk_t root_chunk
= { bits_size
, bits
};
1314 riff_chunk_t ACON_chunk
= {0};
1315 riff_chunk_t anih_chunk
= {0};
1316 riff_chunk_t fram_chunk
= {0};
1317 const unsigned char *icon_chunk
;
1318 const unsigned char *icon_data
;
1320 TRACE("bits %p, bits_size %d\n", bits
, bits_size
);
1322 if (!bits
) return 0;
1324 riff_find_chunk( ANI_ACON_ID
, ANI_RIFF_ID
, &root_chunk
, &ACON_chunk
);
1325 if (!ACON_chunk
.data
)
1327 ERR("Failed to get root chunk.\n");
1331 riff_find_chunk( ANI_anih_ID
, 0, &ACON_chunk
, &anih_chunk
);
1332 if (!anih_chunk
.data
)
1334 ERR("Failed to get 'anih' chunk.\n");
1337 memcpy( &header
, anih_chunk
.data
, sizeof(header
) );
1338 dump_ani_header( &header
);
1340 if (header
.flags
& ANI_FLAG_SEQUENCE
)
1342 riff_chunk_t seq_chunk
= {0};
1344 TRACE("Loading sequence data.\n");
1345 riff_find_chunk( ANI_seq__ID
, 0, &ACON_chunk
, &seq_chunk
);
1346 if (!seq_chunk
.data
)
1348 ERR("Failed to get 'seq ' chunk\n");
1351 frame_seq
= HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD
) * header
.num_steps
);
1352 memcpy( frame_seq
, seq_chunk
.data
, sizeof(DWORD
) * header
.num_steps
);
1355 riff_find_chunk( ANI_fram_ID
, ANI_LIST_ID
, &ACON_chunk
, &fram_chunk
);
1356 if (!fram_chunk
.data
)
1358 ERR("Failed to get icon list\n");
1362 icon_chunk
= fram_chunk
.data
;
1363 icon_data
= icon_chunk
+ (2 * sizeof(DWORD
));
1364 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1365 cursor
= create_cursor( header
.num_steps
, (100 * header
.display_rate
) / 6 );
1366 frames
= HeapAlloc( GetProcessHeap(), 0, header
.num_frames
* sizeof(cursor_frame_t
) );
1368 for (i
= 0; i
< header
.num_frames
; ++i
)
1371 CURSORICONFILEDIRENTRY
*entry
;
1372 DWORD chunk_size
= *(DWORD
*)(icon_chunk
+ sizeof(DWORD
));
1374 /* Read icon count, skip magic */
1375 memcpy( &count
, icon_data
+ sizeof(DWORD
), sizeof(WORD
) );
1377 /* There's a decent chance the amount of entries will be the same for each icon */
1378 if (count
> max_count
)
1380 HeapFree( GetProcessHeap(), 0, dir
);
1381 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1382 dir
= HeapAlloc( GetProcessHeap(), 0, (count
* sizeof(CURSORICONFILEDIRENTRY
)) + 6 );
1386 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1387 memcpy( dir
, icon_data
, (count
* sizeof(CURSORICONFILEDIRENTRY
)) + 6 );
1388 entry
= CURSORICON_FindBestCursorFile( dir
, width
, height
, 1 );
1390 if (frame_bits_size
< entry
->dwDIBSize
)
1392 frame_bits_size
= entry
->dwDIBSize
;
1393 HeapFree( GetProcessHeap(), 0, frame_bits
);
1394 frame_bits
= HeapAlloc( GetProcessHeap(), 0, frame_bits_size
);
1397 if (!header
.width
|| !header
.height
)
1399 header
.width
= entry
->bWidth
;
1400 header
.height
= entry
->bHeight
;
1403 hotspot
.x
= entry
->xHotspot
;
1404 hotspot
.y
= entry
->yHotspot
;
1406 memcpy( frame_bits
, icon_data
+ entry
->dwDIBOffset
, entry
->dwDIBSize
);
1408 load_cursor_frame( frame_bits
, entry
->dwDIBSize
, hotspot
, 0x00030000, header
.width
, header
.height
, 0, &frames
[i
] );
1410 /* Advance to the next chunk */
1411 icon_chunk
+= chunk_size
+ (2 * sizeof(DWORD
));
1412 icon_data
= icon_chunk
+ (2 * sizeof(DWORD
));
1414 HeapFree( GetProcessHeap(), 0, dir
);
1416 /* Set the frames in the correct sequence */
1417 for (i
= 0; i
< header
.num_steps
; ++i
)
1419 int frame_idx
= (frame_seq
? frame_seq
[i
] : i
);
1420 set_cursor_frame( cursor
, i
, &frames
[frame_idx
] );
1424 for (i
= 0; i
< header
.num_frames
; ++i
)
1426 HeapFree( GetProcessHeap(), 0, frames
[i
].bits
);
1428 HeapFree( GetProcessHeap(), 0, frame_seq
);
1429 HeapFree( GetProcessHeap(), 0, frames
);
1435 /**********************************************************************
1436 * CreateIconFromResourceEx (USER32.@)
1438 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1439 * with cbSize parameter as well.
1441 HICON WINAPI
CreateIconFromResourceEx( LPBYTE bits
, UINT cbSize
,
1442 BOOL bIcon
, DWORD dwVersion
,
1443 INT width
, INT height
,
1447 HCURSOR cursor
= create_cursor( 1, 0 );
1448 cursor_frame_t frame
= {0};
1452 hotspot
.x
= ICON_HOTSPOT
;
1453 hotspot
.y
= ICON_HOTSPOT
;
1457 hotspot
= *(POINT16
*)bits
;
1458 bits
= (LPBYTE
)(((POINT16
*)bits
) + 1);
1461 if (load_cursor_frame( bits
, cbSize
, hotspot
, dwVersion
, width
, height
, cFlag
, &frame
))
1463 set_cursor_frame( cursor
, 0, &frame
);
1467 destroy_cursor( cursor
);
1471 HeapFree( GetProcessHeap(), 0, frame
.bits
);
1477 /**********************************************************************
1478 * CreateIconFromResource (USER32.@)
1480 HICON WINAPI
CreateIconFromResource( LPBYTE bits
, UINT cbSize
,
1481 BOOL bIcon
, DWORD dwVersion
)
1483 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0,0,0);
1487 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
1488 INT width
, INT height
, INT colors
,
1489 BOOL fCursor
, UINT loadflags
)
1491 CURSORICONFILEDIRENTRY
*entry
;
1492 cursor_frame_t frame
= {0};
1493 CURSORICONFILEDIR
*dir
;
1499 TRACE("loading %s\n", debugstr_w( filename
));
1501 bits
= map_fileW( filename
, &filesize
);
1505 /* If the data contains the magic for an .ICO it's an .ICO,
1506 * regardless of what fCursor says. */
1507 if (!memcmp( bits
, "\x00\x00\x01\x00", 4 )) fCursor
= FALSE
;
1508 /* Same thing for .CUR */
1509 else if (!memcmp( bits
, "\x00\x00\x02\x00", 4 )) fCursor
= TRUE
;
1510 /* Check for .ani. */
1511 else if (!memcmp( bits
, "RIFF", 4 ))
1513 hIcon
= load_ani( bits
, filesize
, width
, height
);
1517 dir
= (CURSORICONFILEDIR
*) bits
;
1518 if ( filesize
< sizeof(*dir
) )
1521 if ( filesize
< (sizeof(*dir
) + sizeof(dir
->idEntries
[0])*(dir
->idCount
-1)) )
1525 entry
= CURSORICON_FindBestCursorFile( dir
, width
, height
, colors
);
1527 entry
= CURSORICON_FindBestIconFile( dir
, width
, height
, colors
);
1532 /* check that we don't run off the end of the file */
1533 if ( entry
->dwDIBOffset
> filesize
)
1535 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
1540 hotspot
.x
= entry
->xHotspot
;
1541 hotspot
.y
= entry
->yHotspot
;
1545 hotspot
.x
= ICON_HOTSPOT
;
1546 hotspot
.y
= ICON_HOTSPOT
;
1549 hIcon
= create_cursor( 1, 0 );
1550 load_cursor_frame( &bits
[entry
->dwDIBOffset
], entry
->dwDIBSize
, hotspot
,
1551 0x00030000, width
, height
, loadflags
, &frame
);
1552 set_cursor_frame( hIcon
, 0, &frame
);
1553 HeapFree( GetProcessHeap(), 0, frame
.bits
);
1556 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
1557 UnmapViewOfFile( bits
);
1561 /**********************************************************************
1564 * Load a cursor or icon from resource or file.
1566 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
1567 INT width
, INT height
, INT colors
,
1568 BOOL fCursor
, UINT loadflags
)
1572 HRSRC hRsrc
, hGroupRsrc
;
1574 CURSORICONDIRENTRY
*dirEntry
;
1579 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1580 hInstance
, debugstr_w(name
), width
, height
, colors
, fCursor
, loadflags
);
1582 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
1583 return CURSORICON_LoadFromFile( name
, width
, height
, colors
, fCursor
, loadflags
);
1585 if (!hInstance
) hInstance
= user32_module
; /* Load OEM cursor/icon */
1587 /* Normalize hInstance (must be uniquely represented for icon cache) */
1589 if (!HIWORD( hInstance
))
1590 hInstance
= HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance
) ));
1592 /* Get directory resource ID */
1594 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1595 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
1599 /* Find the best entry in the directory */
1601 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1602 if (!(dir
= (CURSORICONDIR
*)LockResource( handle
))) return 0;
1604 dirEntry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, 1);
1606 dirEntry
= CURSORICON_FindBestIconRes( dir
, width
, height
, colors
);
1607 if (!dirEntry
) return 0;
1608 wResId
= dirEntry
->wResId
;
1609 dwBytesInRes
= dirEntry
->dwBytesInRes
;
1610 FreeResource( handle
);
1612 /* Load the resource */
1614 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
1615 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
1617 /* If shared icon, check whether it was already loaded */
1618 if ( (loadflags
& LR_SHARED
)
1619 && (hIcon
= CURSORICON_FindSharedIcon( hInstance
, hRsrc
) ) != 0 )
1622 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1623 bits
= (LPBYTE
)LockResource( handle
);
1624 hIcon
= CreateIconFromResourceEx( bits
, dwBytesInRes
,
1625 !fCursor
, 0x00030000, width
, height
, loadflags
);
1626 FreeResource( handle
);
1628 /* If shared icon, add to icon cache */
1630 if ( hIcon
&& (loadflags
& LR_SHARED
) )
1631 CURSORICON_AddSharedIcon( hInstance
, hRsrc
, hGroupRsrc
, hIcon
);
1636 /***********************************************************************
1639 * Make a copy of a cursor or icon.
1641 static HICON
CURSORICON_Copy( HINSTANCE16 hInst16
, HICON hIcon
)
1643 /* Should animated cursors be copyable like this as well? */
1645 cursor_frame_t frame
;
1647 if (!hIcon
|| !get_cursor_frame( hIcon
, 0, &frame
))
1652 new_cursor
= create_cursor( 1, 0 );
1653 set_cursor_frame( new_cursor
, 0, &frame
);
1654 HeapFree( GetProcessHeap(), 0, frame
.bits
);
1659 /*************************************************************************
1660 * CURSORICON_ExtCopy
1662 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1665 * Handle [I] handle to an Image
1666 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1667 * iDesiredCX [I] The Desired width of the Image
1668 * iDesiredCY [I] The desired height of the Image
1669 * nFlags [I] The flags from CopyImage
1672 * Success: The new handle of the Image
1675 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1676 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1677 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1682 static HICON
CURSORICON_ExtCopy(HICON hIcon
, UINT nType
,
1683 INT iDesiredCX
, INT iDesiredCY
,
1688 TRACE_(icon
)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1689 hIcon
, nType
, iDesiredCX
, iDesiredCY
, nFlags
);
1696 /* Best Fit or Monochrome */
1697 if( (nFlags
& LR_COPYFROMRESOURCE
1698 && (iDesiredCX
> 0 || iDesiredCY
> 0))
1699 || nFlags
& LR_MONOCHROME
)
1701 ICONCACHE
* pIconCache
= CURSORICON_FindCache(hIcon
);
1703 /* Not Found in Cache, then do a straight copy
1705 if(pIconCache
== NULL
)
1707 hNew
= CURSORICON_Copy(0, hIcon
);
1708 if(nFlags
& LR_COPYFROMRESOURCE
)
1710 TRACE_(icon
)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1715 int iTargetCY
= iDesiredCY
, iTargetCX
= iDesiredCX
;
1721 CURSORICONDIR
*pDir
;
1722 CURSORICONDIRENTRY
*pDirEntry
;
1723 BOOL bIsIcon
= (nType
== IMAGE_ICON
);
1725 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1727 if(((nFlags
& LR_MONOCHROME
) && !(nFlags
& LR_COPYFROMRESOURCE
))
1728 || (iDesiredCX
== 0 && iDesiredCY
== 0))
1730 iDesiredCY
= GetSystemMetrics(bIsIcon
?
1731 SM_CYICON
: SM_CYCURSOR
);
1732 iDesiredCX
= GetSystemMetrics(bIsIcon
?
1733 SM_CXICON
: SM_CXCURSOR
);
1736 /* Retrieve the CURSORICONDIRENTRY
1738 if (!(hMem
= LoadResource( pIconCache
->hModule
,
1739 pIconCache
->hGroupRsrc
)))
1743 if (!(pDir
= (CURSORICONDIR
*)LockResource( hMem
)))
1752 pDirEntry
= CURSORICON_FindBestIconRes(
1753 pDir
, iDesiredCX
, iDesiredCY
, 256 );
1757 pDirEntry
= CURSORICON_FindBestCursorRes(
1758 pDir
, iDesiredCX
, iDesiredCY
, 1);
1761 wResId
= pDirEntry
->wResId
;
1762 dwBytesInRes
= pDirEntry
->dwBytesInRes
;
1765 TRACE_(icon
)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1766 wResId
, dwBytesInRes
, pDirEntry
->ResInfo
.icon
.bWidth
,
1767 pDirEntry
->ResInfo
.icon
.bHeight
, iDesiredCX
, iDesiredCY
);
1771 if (!(hRsrc
= FindResourceW(pIconCache
->hModule
,
1772 MAKEINTRESOURCEW(wResId
), (LPWSTR
)(bIsIcon
? RT_ICON
: RT_CURSOR
))))
1776 if (!(hMem
= LoadResource( pIconCache
->hModule
, hRsrc
)))
1781 pBits
= (LPBYTE
)LockResource( hMem
);
1783 if(nFlags
& LR_DEFAULTSIZE
)
1785 iTargetCY
= GetSystemMetrics(SM_CYICON
);
1786 iTargetCX
= GetSystemMetrics(SM_CXICON
);
1789 /* Create a New Icon with the proper dimension
1791 hNew
= CreateIconFromResourceEx( pBits
, dwBytesInRes
,
1792 bIsIcon
, 0x00030000, iTargetCX
, iTargetCY
, nFlags
);
1796 else hNew
= CURSORICON_Copy(0, hIcon
);
1801 /***********************************************************************
1802 * CreateCursor (USER32.@)
1804 HCURSOR WINAPI
CreateCursor( HINSTANCE hInstance
,
1805 INT xHotSpot
, INT yHotSpot
,
1806 INT nWidth
, INT nHeight
,
1807 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1809 CURSORICONINFO info
;
1811 TRACE_(cursor
)("%dx%d spot=%d,%d xor=%p and=%p\n",
1812 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
1814 info
.ptHotSpot
.x
= xHotSpot
;
1815 info
.ptHotSpot
.y
= yHotSpot
;
1816 info
.nWidth
= nWidth
;
1817 info
.nHeight
= nHeight
;
1818 info
.nWidthBytes
= 0;
1820 info
.bBitsPerPixel
= 1;
1822 return HICON_32(CreateCursorIconIndirect16(0, &info
, lpANDbits
, lpXORbits
));
1826 /***********************************************************************
1827 * CreateIcon (USER.407)
1829 HICON16 WINAPI
CreateIcon16( HINSTANCE16 hInstance
, INT16 nWidth
,
1830 INT16 nHeight
, BYTE bPlanes
, BYTE bBitsPixel
,
1831 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1833 CURSORICONINFO info
;
1835 TRACE_(icon
)("%dx%dx%d, xor=%p, and=%p\n",
1836 nWidth
, nHeight
, bPlanes
* bBitsPixel
, lpXORbits
, lpANDbits
);
1838 info
.ptHotSpot
.x
= ICON_HOTSPOT
;
1839 info
.ptHotSpot
.y
= ICON_HOTSPOT
;
1840 info
.nWidth
= nWidth
;
1841 info
.nHeight
= nHeight
;
1842 info
.nWidthBytes
= 0;
1843 info
.bPlanes
= bPlanes
;
1844 info
.bBitsPerPixel
= bBitsPixel
;
1846 return CreateCursorIconIndirect16( hInstance
, &info
, lpANDbits
, lpXORbits
);
1850 /***********************************************************************
1851 * CreateIcon (USER32.@)
1853 * Creates an icon based on the specified bitmaps. The bitmaps must be
1854 * provided in a device dependent format and will be resized to
1855 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1856 * depth. The provided bitmaps must be top-down bitmaps.
1857 * Although Windows does not support 15bpp(*) this API must support it
1858 * for Winelib applications.
1860 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1864 * Success: handle to an icon
1867 * FIXME: Do we need to resize the bitmaps?
1869 HICON WINAPI
CreateIcon(
1870 HINSTANCE hInstance
, /* [in] the application's hInstance */
1871 INT nWidth
, /* [in] the width of the provided bitmaps */
1872 INT nHeight
, /* [in] the height of the provided bitmaps */
1873 BYTE bPlanes
, /* [in] the number of planes in the provided bitmaps */
1874 BYTE bBitsPixel
, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1875 LPCVOID lpANDbits
, /* [in] a monochrome bitmap representing the icon's mask */
1876 LPCVOID lpXORbits
) /* [in] the icon's 'color' bitmap */
1881 TRACE_(icon
)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1882 nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
, lpANDbits
);
1885 iinfo
.xHotspot
= ICON_HOTSPOT
;
1886 iinfo
.yHotspot
= ICON_HOTSPOT
;
1887 iinfo
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1888 iinfo
.hbmColor
= CreateBitmap( nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
);
1890 hIcon
= CreateIconIndirect( &iinfo
);
1892 DeleteObject( iinfo
.hbmMask
);
1893 DeleteObject( iinfo
.hbmColor
);
1899 /***********************************************************************
1900 * CreateCursorIconIndirect (USER.408)
1902 HGLOBAL16 WINAPI
CreateCursorIconIndirect16( HINSTANCE16 hInstance
,
1903 CURSORICONINFO
*info
,
1908 cursor_frame_t frame
;
1909 int sizeAnd
, sizeXor
;
1911 if (!lpXORbits
|| !lpANDbits
|| info
->bPlanes
!= 1) return 0;
1912 info
->nWidthBytes
= get_bitmap_width_bytes(info
->nWidth
,info
->bBitsPerPixel
);
1913 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
1914 sizeAnd
= info
->nHeight
* get_bitmap_width_bytes( info
->nWidth
, 1 );
1916 cursor
= create_cursor( 1, 0 );
1917 frame
.xhot
= info
->ptHotSpot
.x
;
1918 frame
.yhot
= info
->ptHotSpot
.y
;
1919 frame
.width
= info
->nWidth
;
1920 frame
.height
= info
->nHeight
;
1921 frame
.and_width_bytes
= get_bitmap_width_bytes( info
->nWidth
, 1 );
1922 frame
.xor_width_bytes
= info
->nWidthBytes
;
1923 frame
.planes
= info
->bPlanes
;
1924 frame
.bpp
= info
->bBitsPerPixel
;
1925 frame
.bits
= HeapAlloc( GetProcessHeap(), 0, sizeAnd
+ sizeXor
);
1926 CopyMemory( frame
.bits
, lpANDbits
, sizeAnd
);
1927 CopyMemory( frame
.bits
+ sizeAnd
, lpXORbits
, sizeXor
);
1928 set_cursor_frame( cursor
, 0, &frame
);
1929 HeapFree( GetProcessHeap(), 0, frame
.bits
);
1931 return HICON_16(cursor
);
1935 /***********************************************************************
1936 * CopyIcon (USER.368)
1938 HICON16 WINAPI
CopyIcon16( HINSTANCE16 hInstance
, HICON16 hIcon
)
1940 TRACE_(icon
)("%04x %04x\n", hInstance
, hIcon
);
1941 return HICON_16(CURSORICON_Copy(hInstance
, HICON_32(hIcon
)));
1945 /***********************************************************************
1946 * CopyIcon (USER32.@)
1948 HICON WINAPI
CopyIcon( HICON hIcon
)
1950 TRACE_(icon
)("%p\n", hIcon
);
1951 return CURSORICON_Copy( 0, hIcon
);
1955 /***********************************************************************
1956 * CopyCursor (USER.369)
1958 HCURSOR16 WINAPI
CopyCursor16( HINSTANCE16 hInstance
, HCURSOR16 hCursor
)
1960 TRACE_(cursor
)("%04x %04x\n", hInstance
, hCursor
);
1961 return HICON_16(CURSORICON_Copy(hInstance
, HCURSOR_32(hCursor
)));
1964 /**********************************************************************
1965 * DestroyIcon32 (USER.610)
1967 * This routine is actually exported from Win95 USER under the name
1968 * DestroyIcon32 ... The behaviour implemented here should mimic
1969 * the Win95 one exactly, especially the return values, which
1970 * depend on the setting of various flags.
1972 WORD WINAPI
DestroyIcon32( HGLOBAL16 handle
, UINT16 flags
)
1976 TRACE_(icon
)("(%04x, %04x)\n", handle
, flags
);
1978 /* Check whether destroying active cursor */
1980 if ( get_user_thread_info()->cursor
== HICON_32(handle
) )
1982 WARN_(cursor
)("Destroying active cursor!\n" );
1986 /* Try shared cursor/icon first */
1988 if ( !(flags
& CID_NONSHARED
) )
1990 INT count
= CURSORICON_DelSharedIcon(HICON_32(handle
));
1993 return (flags
& CID_WIN32
)? TRUE
: (count
== 0);
1995 /* FIXME: OEM cursors/icons should be recognized */
1998 /* Now assume non-shared cursor/icon */
2000 retv
= destroy_cursor( HCURSOR_32(handle
) );
2001 return (flags
& CID_RESOURCE
)? retv
: TRUE
;
2004 /***********************************************************************
2005 * DestroyIcon (USER32.@)
2007 BOOL WINAPI
DestroyIcon( HICON hIcon
)
2009 return DestroyIcon32(HICON_16(hIcon
), CID_WIN32
);
2013 /***********************************************************************
2014 * DestroyCursor (USER32.@)
2016 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
2018 return DestroyIcon32(HCURSOR_16(hCursor
), CID_WIN32
);
2022 /***********************************************************************
2023 * DrawIcon (USER32.@)
2025 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
2027 CURSORICONINFO
*ptr
;
2029 HBITMAP hXorBits
, hAndBits
;
2030 COLORREF oldFg
, oldBg
;
2032 TRACE("%p, (%d,%d), %p\n", hdc
, x
, y
, hIcon
);
2034 if (!(ptr
= (CURSORICONINFO
*)GlobalLock16(HICON_16(hIcon
)))) return FALSE
;
2035 if (!(hMemDC
= CreateCompatibleDC( hdc
))) return FALSE
;
2036 hAndBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, 1, 1,
2038 hXorBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
,
2039 ptr
->bBitsPerPixel
, (char *)(ptr
+ 1)
2040 + ptr
->nHeight
* get_bitmap_width_bytes(ptr
->nWidth
,1) );
2041 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
2042 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
2044 if (hXorBits
&& hAndBits
)
2046 HBITMAP hBitTemp
= SelectObject( hMemDC
, hAndBits
);
2047 BitBlt( hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0, SRCAND
);
2048 SelectObject( hMemDC
, hXorBits
);
2049 BitBlt(hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0,SRCINVERT
);
2050 SelectObject( hMemDC
, hBitTemp
);
2053 if (hXorBits
) DeleteObject( hXorBits
);
2054 if (hAndBits
) DeleteObject( hAndBits
);
2055 GlobalUnlock16(HICON_16(hIcon
));
2056 SetTextColor( hdc
, oldFg
);
2057 SetBkColor( hdc
, oldBg
);
2061 /***********************************************************************
2062 * DumpIcon (USER.459)
2064 DWORD WINAPI
DumpIcon16( SEGPTR pInfo
, WORD
*lpLen
,
2065 SEGPTR
*lpXorBits
, SEGPTR
*lpAndBits
)
2067 CURSORICONINFO
*info
= MapSL( pInfo
);
2068 int sizeAnd
, sizeXor
;
2070 if (!info
) return 0;
2071 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
2072 sizeAnd
= info
->nHeight
* get_bitmap_width_bytes( info
->nWidth
, 1 );
2073 if (lpAndBits
) *lpAndBits
= pInfo
+ sizeof(CURSORICONINFO
);
2074 if (lpXorBits
) *lpXorBits
= pInfo
+ sizeof(CURSORICONINFO
) + sizeAnd
;
2075 if (lpLen
) *lpLen
= sizeof(CURSORICONINFO
) + sizeAnd
+ sizeXor
;
2076 return MAKELONG( sizeXor
, sizeXor
);
2080 /***********************************************************************
2081 * SetCursor (USER32.@)
2083 * Set the cursor shape.
2086 * A handle to the previous cursor shape.
2088 HCURSOR WINAPI
SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
2090 struct user_thread_info
*thread_info
= get_user_thread_info();
2093 if (hCursor
== thread_info
->cursor
) return hCursor
; /* No change */
2094 TRACE("%p\n", hCursor
);
2095 hOldCursor
= thread_info
->cursor
;
2096 thread_info
->cursor
= hCursor
;
2097 /* Change the cursor shape only if it is visible */
2098 if (thread_info
->cursor_count
>= 0)
2102 update_cursor_32from16( hCursor
);
2103 cursor
= get_cursor_object( hCursor
);
2104 USER_Driver
->pSetCursor( cursor
);
2105 destroy_cursor_object( cursor
);
2110 /***********************************************************************
2111 * ShowCursor (USER32.@)
2113 INT WINAPI
ShowCursor( BOOL bShow
)
2115 struct user_thread_info
*thread_info
= get_user_thread_info();
2117 TRACE("%d, count=%d\n", bShow
, thread_info
->cursor_count
);
2121 if (++thread_info
->cursor_count
== 0) /* Show it */
2125 update_cursor_32from16( thread_info
->cursor
);
2126 cursor
= get_cursor_object( thread_info
->cursor
);
2127 USER_Driver
->pSetCursor( cursor
);
2128 destroy_cursor_object( cursor
);
2133 if (--thread_info
->cursor_count
== -1) /* Hide it */
2134 USER_Driver
->pSetCursor( NULL
);
2136 return thread_info
->cursor_count
;
2139 /***********************************************************************
2140 * GetCursor (USER32.@)
2142 HCURSOR WINAPI
GetCursor(void)
2144 return get_user_thread_info()->cursor
;
2148 /***********************************************************************
2149 * ClipCursor (USER32.@)
2151 BOOL WINAPI
ClipCursor( const RECT
*rect
)
2155 SetRect( &virt
, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN
),
2156 GetSystemMetrics( SM_CYVIRTUALSCREEN
) );
2157 OffsetRect( &virt
, GetSystemMetrics( SM_XVIRTUALSCREEN
),
2158 GetSystemMetrics( SM_YVIRTUALSCREEN
) );
2160 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect
),
2161 wine_dbgstr_rect(&CURSOR_ClipRect
), wine_dbgstr_rect(&virt
) );
2163 if (!IntersectRect( &CURSOR_ClipRect
, &virt
, rect
))
2164 CURSOR_ClipRect
= virt
;
2166 USER_Driver
->pClipCursor( rect
);
2171 /***********************************************************************
2172 * GetClipCursor (USER32.@)
2174 BOOL WINAPI
GetClipCursor( RECT
*rect
)
2176 /* If this is first time - initialize the rect */
2177 if (IsRectEmpty( &CURSOR_ClipRect
)) ClipCursor( NULL
);
2179 return CopyRect( rect
, &CURSOR_ClipRect
);
2183 /***********************************************************************
2184 * SetSystemCursor (USER32.@)
2186 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
2188 FIXME("(%p,%08x),stub!\n", hcur
, id
);
2193 /**********************************************************************
2194 * LookupIconIdFromDirectoryEx (USER.364)
2196 * FIXME: exact parameter sizes
2198 INT16 WINAPI
LookupIconIdFromDirectoryEx16( LPBYTE dir
, BOOL16 bIcon
,
2199 INT16 width
, INT16 height
, UINT16 cFlag
)
2201 return LookupIconIdFromDirectoryEx( dir
, bIcon
, width
, height
, cFlag
);
2204 /**********************************************************************
2205 * LookupIconIdFromDirectoryEx (USER32.@)
2207 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
2208 INT width
, INT height
, UINT cFlag
)
2210 CURSORICONDIR
*dir
= (CURSORICONDIR
*)xdir
;
2212 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
2214 CURSORICONDIRENTRY
* entry
;
2219 palEnts
= GetSystemPaletteEntries(hdc
, 0, 0, NULL
);
2222 colors
= (cFlag
& LR_MONOCHROME
) ? 2 : palEnts
;
2227 entry
= CURSORICON_FindBestIconRes( dir
, width
, height
, colors
);
2229 entry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, 1);
2231 if( entry
) retVal
= entry
->wResId
;
2233 else WARN_(cursor
)("invalid resource directory\n");
2237 /**********************************************************************
2238 * LookupIconIdFromDirectory (USER.?)
2240 INT16 WINAPI
LookupIconIdFromDirectory16( LPBYTE dir
, BOOL16 bIcon
)
2242 return LookupIconIdFromDirectoryEx16( dir
, bIcon
,
2243 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
2244 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
2247 /**********************************************************************
2248 * LookupIconIdFromDirectory (USER32.@)
2250 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
2252 return LookupIconIdFromDirectoryEx( dir
, bIcon
,
2253 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
2254 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
2257 /**********************************************************************
2258 * GetIconID (USER.455)
2260 WORD WINAPI
GetIconID16( HGLOBAL16 hResource
, DWORD resType
)
2262 LPBYTE lpDir
= (LPBYTE
)GlobalLock16(hResource
);
2264 TRACE_(cursor
)("hRes=%04x, entries=%i\n",
2265 hResource
, lpDir
? ((CURSORICONDIR
*)lpDir
)->idCount
: 0);
2270 return (WORD
)LookupIconIdFromDirectoryEx16( lpDir
, FALSE
,
2271 GetSystemMetrics(SM_CXCURSOR
), GetSystemMetrics(SM_CYCURSOR
), LR_MONOCHROME
);
2273 return (WORD
)LookupIconIdFromDirectoryEx16( lpDir
, TRUE
,
2274 GetSystemMetrics(SM_CXICON
), GetSystemMetrics(SM_CYICON
), 0 );
2276 WARN_(cursor
)("invalid res type %d\n", resType
);
2281 /**********************************************************************
2282 * LoadCursorIconHandler (USER.336)
2284 * Supposed to load resources of Windows 2.x applications.
2286 HGLOBAL16 WINAPI
LoadCursorIconHandler16( HGLOBAL16 hResource
, HMODULE16 hModule
, HRSRC16 hRsrc
)
2288 FIXME_(cursor
)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2289 hResource
, hModule
, hRsrc
);
2290 return (HGLOBAL16
)0;
2293 /**********************************************************************
2294 * LoadIconHandler (USER.456)
2296 HICON16 WINAPI
LoadIconHandler16( HGLOBAL16 hResource
, BOOL16 bNew
)
2298 LPBYTE bits
= (LPBYTE
)LockResource16( hResource
);
2300 TRACE_(cursor
)("hRes=%04x\n",hResource
);
2302 return HICON_16(CreateIconFromResourceEx( bits
, 0, TRUE
,
2303 bNew
? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR
));
2306 /***********************************************************************
2307 * LoadCursorW (USER32.@)
2309 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
2311 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
2313 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
2314 LR_SHARED
| LR_DEFAULTSIZE
);
2317 /***********************************************************************
2318 * LoadCursorA (USER32.@)
2320 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
2322 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
2324 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
2325 LR_SHARED
| LR_DEFAULTSIZE
);
2328 /***********************************************************************
2329 * LoadCursorFromFileW (USER32.@)
2331 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
2333 TRACE("%s\n", debugstr_w(name
));
2335 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
2336 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
2339 /***********************************************************************
2340 * LoadCursorFromFileA (USER32.@)
2342 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
2344 TRACE("%s\n", debugstr_a(name
));
2346 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
2347 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
2350 /***********************************************************************
2351 * LoadIconW (USER32.@)
2353 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
2355 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
2357 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
2358 LR_SHARED
| LR_DEFAULTSIZE
);
2361 /***********************************************************************
2362 * LoadIconA (USER32.@)
2364 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
2366 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
2368 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
2369 LR_SHARED
| LR_DEFAULTSIZE
);
2372 /**********************************************************************
2373 * GetIconInfo (USER32.@)
2375 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
2377 CURSORICONINFO
*ciconinfo
;
2380 ciconinfo
= GlobalLock16(HICON_16(hIcon
));
2384 TRACE("%p => %dx%d, %d bpp\n", hIcon
,
2385 ciconinfo
->nWidth
, ciconinfo
->nHeight
, ciconinfo
->bBitsPerPixel
);
2387 if ( (ciconinfo
->ptHotSpot
.x
== ICON_HOTSPOT
) &&
2388 (ciconinfo
->ptHotSpot
.y
== ICON_HOTSPOT
) )
2390 iconinfo
->fIcon
= TRUE
;
2391 iconinfo
->xHotspot
= ciconinfo
->nWidth
/ 2;
2392 iconinfo
->yHotspot
= ciconinfo
->nHeight
/ 2;
2396 iconinfo
->fIcon
= FALSE
;
2397 iconinfo
->xHotspot
= ciconinfo
->ptHotSpot
.x
;
2398 iconinfo
->yHotspot
= ciconinfo
->ptHotSpot
.y
;
2401 height
= ciconinfo
->nHeight
;
2403 if (ciconinfo
->bBitsPerPixel
> 1)
2405 iconinfo
->hbmColor
= CreateBitmap( ciconinfo
->nWidth
, ciconinfo
->nHeight
,
2406 ciconinfo
->bPlanes
, ciconinfo
->bBitsPerPixel
,
2407 (char *)(ciconinfo
+ 1)
2408 + ciconinfo
->nHeight
*
2409 get_bitmap_width_bytes (ciconinfo
->nWidth
,1) );
2413 iconinfo
->hbmColor
= 0;
2417 iconinfo
->hbmMask
= CreateBitmap ( ciconinfo
->nWidth
, height
,
2418 1, 1, (char *)(ciconinfo
+ 1));
2420 GlobalUnlock16(HICON_16(hIcon
));
2425 /**********************************************************************
2426 * CreateIconIndirect (USER32.@)
2428 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
2431 cursor_frame_t frame
;
2434 int xor_objsize
= 0, sizeXor
= 0, sizeAnd
, planes
, bpp
;
2436 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2437 iconinfo
->hbmColor
, iconinfo
->hbmMask
,
2438 iconinfo
->xHotspot
, iconinfo
->yHotspot
, iconinfo
->fIcon
);
2440 if (!iconinfo
->hbmMask
) return 0;
2442 planes
= GetDeviceCaps( screen_dc
, PLANES
);
2443 bpp
= GetDeviceCaps( screen_dc
, BITSPIXEL
);
2445 if (iconinfo
->hbmColor
)
2447 xor_objsize
= GetObjectW( iconinfo
->hbmColor
, sizeof(bmpXor
), &bmpXor
);
2448 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2449 bmpXor
.dsBm
.bmWidth
, bmpXor
.dsBm
.bmHeight
, bmpXor
.dsBm
.bmWidthBytes
,
2450 bmpXor
.dsBm
.bmPlanes
, bmpXor
.dsBm
.bmBitsPixel
);
2451 /* we can use either depth 1 or screen depth for xor bitmap */
2452 if (bmpXor
.dsBm
.bmPlanes
== 1 && bmpXor
.dsBm
.bmBitsPixel
== 1) planes
= bpp
= 1;
2453 sizeXor
= bmpXor
.dsBm
.bmHeight
* planes
* get_bitmap_width_bytes( bmpXor
.dsBm
.bmWidth
, bpp
);
2455 GetObjectW( iconinfo
->hbmMask
, sizeof(bmpAnd
), &bmpAnd
);
2456 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2457 bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, bmpAnd
.bmWidthBytes
,
2458 bmpAnd
.bmPlanes
, bmpAnd
.bmBitsPixel
);
2460 sizeAnd
= bmpAnd
.bmHeight
* get_bitmap_width_bytes(bmpAnd
.bmWidth
, 1);
2462 cursor
= create_cursor( 1, 0 );
2464 /* If we are creating an icon, the hotspot is unused */
2465 if (iconinfo
->fIcon
)
2467 frame
.xhot
= ICON_HOTSPOT
;
2468 frame
.yhot
= ICON_HOTSPOT
;
2472 frame
.xhot
= iconinfo
->xHotspot
;
2473 frame
.yhot
= iconinfo
->yHotspot
;
2476 if (iconinfo
->hbmColor
)
2478 frame
.width
= bmpXor
.dsBm
.bmWidth
;
2479 frame
.height
= bmpXor
.dsBm
.bmHeight
;
2480 frame
.and_width_bytes
= bmpAnd
.bmWidthBytes
;
2481 frame
.xor_width_bytes
= bmpXor
.dsBm
.bmWidthBytes
;
2482 frame
.planes
= planes
;
2487 frame
.width
= bmpAnd
.bmWidth
;
2488 frame
.height
= bmpAnd
.bmHeight
/ 2;
2489 frame
.and_width_bytes
= get_bitmap_width_bytes(bmpAnd
.bmWidth
, 1);
2490 frame
.xor_width_bytes
= 0;
2495 frame
.bits
= HeapAlloc( GetProcessHeap(), 0, sizeAnd
+ sizeXor
);
2497 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2498 if (bmpAnd
.bmBitsPixel
== 1)
2500 GetBitmapBits( iconinfo
->hbmMask
, sizeAnd
, frame
.bits
);
2505 HBITMAP hbmp_old
, hbmp_mem_old
, hbmp_mono
;
2508 hdc_mem
= CreateCompatibleDC( hdc
);
2510 hbmp_mono
= CreateBitmap( bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, 1, 1, NULL
);
2512 hbmp_old
= SelectObject( hdc
, iconinfo
->hbmMask
);
2513 hbmp_mem_old
= SelectObject( hdc_mem
, hbmp_mono
);
2515 BitBlt( hdc_mem
, 0, 0, bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, hdc
, 0, 0, SRCCOPY
);
2517 SelectObject( hdc
, hbmp_old
);
2518 SelectObject( hdc_mem
, hbmp_mem_old
);
2520 DeleteDC( hdc_mem
);
2521 ReleaseDC( 0, hdc
);
2523 GetBitmapBits( hbmp_mono
, sizeAnd
, frame
.bits
);
2524 DeleteObject( hbmp_mono
);
2527 if (iconinfo
->hbmColor
)
2529 char *dst_bits
= frame
.bits
+ sizeAnd
;
2531 if (bmpXor
.dsBm
.bmPlanes
== planes
&& bmpXor
.dsBm
.bmBitsPixel
== bpp
)
2532 GetBitmapBits( iconinfo
->hbmColor
, sizeXor
, dst_bits
);
2536 int dib_width
= get_dib_width_bytes( frame
.width
, frame
.bpp
);
2537 int bitmap_width
= get_bitmap_width_bytes( frame
.width
, frame
.bpp
);
2539 bminfo
.bmiHeader
.biSize
= sizeof(bminfo
);
2540 bminfo
.bmiHeader
.biWidth
= frame
.width
;
2541 bminfo
.bmiHeader
.biHeight
= frame
.height
;
2542 bminfo
.bmiHeader
.biPlanes
= frame
.planes
;
2543 bminfo
.bmiHeader
.biBitCount
= frame
.bpp
;
2544 bminfo
.bmiHeader
.biCompression
= BI_RGB
;
2545 bminfo
.bmiHeader
.biSizeImage
= frame
.height
* dib_width
;
2546 bminfo
.bmiHeader
.biXPelsPerMeter
= 0;
2547 bminfo
.bmiHeader
.biYPelsPerMeter
= 0;
2548 bminfo
.bmiHeader
.biClrUsed
= 0;
2549 bminfo
.bmiHeader
.biClrImportant
= 0;
2551 /* swap lines for dib sections */
2552 if (xor_objsize
== sizeof(DIBSECTION
))
2553 bminfo
.bmiHeader
.biHeight
= -bminfo
.bmiHeader
.biHeight
;
2555 if (dib_width
!= bitmap_width
) /* need to fixup alignment */
2557 char *src_bits
= HeapAlloc( GetProcessHeap(), 0, bminfo
.bmiHeader
.biSizeImage
);
2559 if (src_bits
&& GetDIBits( screen_dc
, iconinfo
->hbmColor
, 0, frame
.height
,
2560 src_bits
, &bminfo
, DIB_RGB_COLORS
))
2563 for (y
= 0; y
< frame
.height
; y
++)
2564 memcpy( dst_bits
+ y
* bitmap_width
, src_bits
+ y
* dib_width
, bitmap_width
);
2566 HeapFree( GetProcessHeap(), 0, src_bits
);
2569 GetDIBits( screen_dc
, iconinfo
->hbmColor
, 0, frame
.height
,
2570 dst_bits
, &bminfo
, DIB_RGB_COLORS
);
2573 set_cursor_frame( cursor
, 0, &frame
);
2574 HeapFree( GetProcessHeap(), 0, frame
.bits
);
2579 /******************************************************************************
2580 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2583 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2586 * hdc [I] Handle to device context
2587 * x0 [I] X coordinate of upper left corner
2588 * y0 [I] Y coordinate of upper left corner
2589 * hIcon [I] Handle to icon to draw
2590 * cxWidth [I] Width of icon
2591 * cyWidth [I] Height of icon
2592 * istep [I] Index of frame in animated cursor
2593 * hbr [I] Handle to background brush
2594 * flags [I] Icon-drawing flags
2600 BOOL WINAPI
DrawIconEx( HDC hdc
, INT x0
, INT y0
, HICON hIcon
,
2601 INT cxWidth
, INT cyWidth
, UINT istep
,
2602 HBRUSH hbr
, UINT flags
)
2604 CURSORICONINFO
*ptr
= (CURSORICONINFO
*)GlobalLock16(HICON_16(hIcon
));
2605 HDC hDC_off
= 0, hMemDC
;
2606 BOOL result
= FALSE
, DoOffscreen
;
2607 HBITMAP hB_off
= 0, hOld
= 0;
2609 if (!ptr
) return FALSE
;
2610 TRACE_(icon
)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2611 hdc
,x0
,y0
,hIcon
,cxWidth
,cyWidth
,istep
,hbr
,flags
);
2613 hMemDC
= CreateCompatibleDC (hdc
);
2615 FIXME_(icon
)("Ignoring istep=%d\n", istep
);
2616 if (flags
& DI_COMPAT
)
2617 FIXME_(icon
)("Ignoring flag DI_COMPAT\n");
2620 FIXME_(icon
)("no flags set? setting to DI_NORMAL\n");
2624 /* Calculate the size of the destination image. */
2627 if (flags
& DI_DEFAULTSIZE
)
2628 cxWidth
= GetSystemMetrics (SM_CXICON
);
2630 cxWidth
= ptr
->nWidth
;
2634 if (flags
& DI_DEFAULTSIZE
)
2635 cyWidth
= GetSystemMetrics (SM_CYICON
);
2637 cyWidth
= ptr
->nHeight
;
2640 DoOffscreen
= (GetObjectType( hbr
) == OBJ_BRUSH
);
2650 hDC_off
= CreateCompatibleDC(hdc
);
2651 hB_off
= CreateCompatibleBitmap(hdc
, cxWidth
, cyWidth
);
2652 if (hDC_off
&& hB_off
) {
2653 hOld
= SelectObject(hDC_off
, hB_off
);
2654 FillRect(hDC_off
, &r
, hbr
);
2658 if (hMemDC
&& (!DoOffscreen
|| (hDC_off
&& hB_off
)))
2660 HBITMAP hXorBits
, hAndBits
;
2661 COLORREF oldFg
, oldBg
;
2664 nStretchMode
= SetStretchBltMode (hdc
, STRETCH_DELETESCANS
);
2666 hXorBits
= CreateBitmap ( ptr
->nWidth
, ptr
->nHeight
,
2667 ptr
->bPlanes
, ptr
->bBitsPerPixel
,
2670 get_bitmap_width_bytes(ptr
->nWidth
,1) );
2671 hAndBits
= CreateBitmap ( ptr
->nWidth
, ptr
->nHeight
,
2672 1, 1, (char *)(ptr
+1) );
2673 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
2674 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
2676 if (hXorBits
&& hAndBits
)
2678 HBITMAP hBitTemp
= SelectObject( hMemDC
, hAndBits
);
2679 if (flags
& DI_MASK
)
2682 StretchBlt (hDC_off
, 0, 0, cxWidth
, cyWidth
,
2683 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCAND
);
2685 StretchBlt (hdc
, x0
, y0
, cxWidth
, cyWidth
,
2686 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCAND
);
2688 SelectObject( hMemDC
, hXorBits
);
2689 if (flags
& DI_IMAGE
)
2692 StretchBlt (hDC_off
, 0, 0, cxWidth
, cyWidth
,
2693 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCPAINT
);
2695 StretchBlt (hdc
, x0
, y0
, cxWidth
, cyWidth
,
2696 hMemDC
, 0, 0, ptr
->nWidth
, ptr
->nHeight
, SRCPAINT
);
2698 SelectObject( hMemDC
, hBitTemp
);
2702 SetTextColor( hdc
, oldFg
);
2703 SetBkColor( hdc
, oldBg
);
2704 if (hXorBits
) DeleteObject( hXorBits
);
2705 if (hAndBits
) DeleteObject( hAndBits
);
2706 SetStretchBltMode (hdc
, nStretchMode
);
2708 BitBlt(hdc
, x0
, y0
, cxWidth
, cyWidth
, hDC_off
, 0, 0, SRCCOPY
);
2709 SelectObject(hDC_off
, hOld
);
2712 if (hMemDC
) DeleteDC( hMemDC
);
2713 if (hDC_off
) DeleteDC(hDC_off
);
2714 if (hB_off
) DeleteObject(hB_off
);
2715 GlobalUnlock16(HICON_16(hIcon
));
2719 /***********************************************************************
2720 * DIB_FixColorsToLoadflags
2722 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2725 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
2728 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
2737 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
2739 WARN_(resource
)("Invalid bitmap\n");
2743 if (bpp
> 8) return;
2745 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
2753 colors
= bmi
->bmiHeader
.biClrUsed
;
2754 if (colors
> 256) colors
= 256;
2755 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
2758 c_W
= GetSysColor(COLOR_WINDOW
);
2759 c_S
= GetSysColor(COLOR_3DSHADOW
);
2760 c_F
= GetSysColor(COLOR_3DFACE
);
2761 c_L
= GetSysColor(COLOR_3DLIGHT
);
2763 if (loadflags
& LR_LOADTRANSPARENT
) {
2765 case 1: pix
= pix
>> 7; break;
2766 case 4: pix
= pix
>> 4; break;
2769 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
2772 if (pix
>= colors
) {
2773 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
2776 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
2777 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
2778 ptr
->rgbBlue
= GetBValue(c_W
);
2779 ptr
->rgbGreen
= GetGValue(c_W
);
2780 ptr
->rgbRed
= GetRValue(c_W
);
2782 if (loadflags
& LR_LOADMAP3DCOLORS
)
2783 for (i
=0; i
<colors
; i
++) {
2784 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
2785 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
2786 if (c_C
== RGB(128, 128, 128)) {
2787 ptr
->rgbRed
= GetRValue(c_S
);
2788 ptr
->rgbGreen
= GetGValue(c_S
);
2789 ptr
->rgbBlue
= GetBValue(c_S
);
2790 } else if (c_C
== RGB(192, 192, 192)) {
2791 ptr
->rgbRed
= GetRValue(c_F
);
2792 ptr
->rgbGreen
= GetGValue(c_F
);
2793 ptr
->rgbBlue
= GetBValue(c_F
);
2794 } else if (c_C
== RGB(223, 223, 223)) {
2795 ptr
->rgbRed
= GetRValue(c_L
);
2796 ptr
->rgbGreen
= GetGValue(c_L
);
2797 ptr
->rgbBlue
= GetBValue(c_L
);
2803 /**********************************************************************
2806 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
2807 INT desiredx
, INT desiredy
, UINT loadflags
)
2809 HBITMAP hbitmap
= 0, orig_bm
;
2813 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
2817 LONG width
, height
, new_width
, new_height
;
2821 HDC screen_mem_dc
= NULL
;
2823 if (!(loadflags
& LR_LOADFROMFILE
))
2827 /* OEM bitmap: try to load the resource from user32.dll */
2828 instance
= user32_module
;
2831 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
2832 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
2834 if ((info
= (BITMAPINFO
*)LockResource( handle
)) == NULL
) return 0;
2838 BITMAPFILEHEADER
* bmfh
;
2840 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
2841 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
2842 bmfh
= (BITMAPFILEHEADER
*)ptr
;
2843 if (!( bmfh
->bfType
== 0x4d42 /* 'BM' */ &&
2844 bmfh
->bfReserved1
== 0 &&
2845 bmfh
->bfReserved2
== 0))
2847 WARN("Invalid/unsupported bitmap format!\n");
2848 UnmapViewOfFile( ptr
);
2853 size
= bitmap_info_size(info
, DIB_RGB_COLORS
);
2854 fix_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2855 scaled_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2857 if (!fix_info
|| !scaled_info
) goto end
;
2858 memcpy(fix_info
, info
, size
);
2860 pix
= *((LPBYTE
)info
+ size
);
2861 DIB_FixColorsToLoadflags(fix_info
, loadflags
, pix
);
2863 memcpy(scaled_info
, fix_info
, size
);
2864 bm_type
= DIB_GetBitmapInfo( &fix_info
->bmiHeader
, &width
, &height
,
2865 &bpp_dummy
, &compr_dummy
);
2867 new_width
= desiredx
;
2872 new_height
= height
> 0 ? desiredy
: -desiredy
;
2874 new_height
= height
;
2878 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
2879 core
->bcWidth
= new_width
;
2880 core
->bcHeight
= new_height
;
2884 scaled_info
->bmiHeader
.biWidth
= new_width
;
2885 scaled_info
->bmiHeader
.biHeight
= new_height
;
2888 if (new_height
< 0) new_height
= -new_height
;
2890 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2891 if (!(screen_mem_dc
= CreateCompatibleDC( screen_dc
))) goto end
;
2893 bits
= (char *)info
+ size
;
2895 if (loadflags
& LR_CREATEDIBSECTION
)
2897 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
2898 hbitmap
= CreateDIBSection(screen_dc
, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
2902 if (is_dib_monochrome(fix_info
))
2903 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
2905 hbitmap
= CreateCompatibleBitmap(screen_dc
, new_width
, new_height
);
2908 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
2909 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
2910 SelectObject(screen_mem_dc
, orig_bm
);
2913 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
2914 HeapFree(GetProcessHeap(), 0, scaled_info
);
2915 HeapFree(GetProcessHeap(), 0, fix_info
);
2916 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
2921 /**********************************************************************
2922 * LoadImageA (USER32.@)
2926 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
2927 INT desiredx
, INT desiredy
, UINT loadflags
)
2933 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
2936 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
2937 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
2938 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
2940 __EXCEPT_PAGE_FAULT
{
2941 SetLastError( ERROR_INVALID_PARAMETER
);
2945 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
2946 HeapFree(GetProcessHeap(), 0, u_name
);
2951 /******************************************************************************
2952 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2955 * hinst [I] Handle of instance that contains image
2956 * name [I] Name of image
2957 * type [I] Type of image
2958 * desiredx [I] Desired width
2959 * desiredy [I] Desired height
2960 * loadflags [I] Load flags
2963 * Success: Handle to newly loaded image
2966 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2968 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
2969 INT desiredx
, INT desiredy
, UINT loadflags
)
2971 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
2972 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
2974 if (loadflags
& LR_DEFAULTSIZE
) {
2975 if (type
== IMAGE_ICON
) {
2976 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXICON
);
2977 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYICON
);
2978 } else if (type
== IMAGE_CURSOR
) {
2979 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXCURSOR
);
2980 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYCURSOR
);
2983 if (loadflags
& LR_LOADFROMFILE
) loadflags
&= ~LR_SHARED
;
2986 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
2989 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2992 UINT palEnts
= GetSystemPaletteEntries(screen_dc
, 0, 0, NULL
);
2993 if (palEnts
== 0) palEnts
= 256;
2994 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
2995 palEnts
, FALSE
, loadflags
);
3000 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
3001 1, TRUE
, loadflags
);
3006 /******************************************************************************
3007 * CopyImage (USER32.@) Creates new image and copies attributes to it
3010 * hnd [I] Handle to image to copy
3011 * type [I] Type of image to copy
3012 * desiredx [I] Desired width of new image
3013 * desiredy [I] Desired height of new image
3014 * flags [I] Copy flags
3017 * Success: Handle to newly created image
3021 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
3022 * all other versions (95/2000/XP have been tested) ignore it.
3025 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
3026 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
3027 * the copy will have the same depth as the screen.
3028 * The content of the image will only be copied if the bit depth of the
3029 * original image is compatible with the bit depth of the screen, or
3030 * if the source is a DIB section.
3031 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
3033 HANDLE WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
3034 INT desiredy
, UINT flags
)
3036 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
3037 hnd
, type
, desiredx
, desiredy
, flags
);
3048 objSize
= GetObjectW( hnd
, sizeof(ds
), &ds
);
3049 if (!objSize
) return 0;
3050 if ((desiredx
< 0) || (desiredy
< 0)) return 0;
3052 if (flags
& LR_COPYFROMRESOURCE
)
3054 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
3057 if (desiredx
== 0) desiredx
= ds
.dsBm
.bmWidth
;
3058 if (desiredy
== 0) desiredy
= ds
.dsBm
.bmHeight
;
3060 /* Allocate memory for a BITMAPINFOHEADER structure and a
3061 color table. The maximum number of colors in a color table
3062 is 256 which corresponds to a bitmap with depth 8.
3063 Bitmaps with higher depths don't have color tables. */
3064 bi
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
3067 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
3068 bi
->bmiHeader
.biPlanes
= ds
.dsBm
.bmPlanes
;
3069 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
3070 bi
->bmiHeader
.biCompression
= BI_RGB
;
3072 if (flags
& LR_CREATEDIBSECTION
)
3074 /* Create a DIB section. LR_MONOCHROME is ignored */
3076 HDC dc
= CreateCompatibleDC(NULL
);
3078 if (objSize
== sizeof(DIBSECTION
))
3080 /* The source bitmap is a DIB.
3081 Get its attributes to create an exact copy */
3082 memcpy(bi
, &ds
.dsBmih
, sizeof(BITMAPINFOHEADER
));
3085 /* Get the color table or the color masks */
3086 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3088 bi
->bmiHeader
.biWidth
= desiredx
;
3089 bi
->bmiHeader
.biHeight
= desiredy
;
3090 bi
->bmiHeader
.biSizeImage
= 0;
3092 res
= CreateDIBSection(dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
3097 /* Create a device-dependent bitmap */
3099 BOOL monochrome
= (flags
& LR_MONOCHROME
);
3101 if (objSize
== sizeof(DIBSECTION
))
3103 /* The source bitmap is a DIB section.
3104 Get its attributes */
3105 HDC dc
= CreateCompatibleDC(NULL
);
3106 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
3107 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
3108 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3111 if (!monochrome
&& ds
.dsBm
.bmBitsPixel
== 1)
3113 /* Look if the colors of the DIB are black and white */
3116 (bi
->bmiColors
[0].rgbRed
== 0xff
3117 && bi
->bmiColors
[0].rgbGreen
== 0xff
3118 && bi
->bmiColors
[0].rgbBlue
== 0xff
3119 && bi
->bmiColors
[0].rgbReserved
== 0
3120 && bi
->bmiColors
[1].rgbRed
== 0
3121 && bi
->bmiColors
[1].rgbGreen
== 0
3122 && bi
->bmiColors
[1].rgbBlue
== 0
3123 && bi
->bmiColors
[1].rgbReserved
== 0)
3125 (bi
->bmiColors
[0].rgbRed
== 0
3126 && bi
->bmiColors
[0].rgbGreen
== 0
3127 && bi
->bmiColors
[0].rgbBlue
== 0
3128 && bi
->bmiColors
[0].rgbReserved
== 0
3129 && bi
->bmiColors
[1].rgbRed
== 0xff
3130 && bi
->bmiColors
[1].rgbGreen
== 0xff
3131 && bi
->bmiColors
[1].rgbBlue
== 0xff
3132 && bi
->bmiColors
[1].rgbReserved
== 0);
3135 else if (!monochrome
)
3137 monochrome
= ds
.dsBm
.bmBitsPixel
== 1;
3142 res
= CreateBitmap(desiredx
, desiredy
, 1, 1, NULL
);
3146 HDC screenDC
= GetDC(NULL
);
3147 res
= CreateCompatibleBitmap(screenDC
, desiredx
, desiredy
);
3148 ReleaseDC(NULL
, screenDC
);
3154 /* Only copy the bitmap if it's a DIB section or if it's
3155 compatible to the screen */
3158 if (objSize
== sizeof(DIBSECTION
))
3160 copyContents
= TRUE
;
3164 HDC screenDC
= GetDC(NULL
);
3165 int screen_depth
= GetDeviceCaps(screenDC
, BITSPIXEL
);
3166 ReleaseDC(NULL
, screenDC
);
3168 copyContents
= (ds
.dsBm
.bmBitsPixel
== 1 || ds
.dsBm
.bmBitsPixel
== screen_depth
);
3173 /* The source bitmap may already be selected in a device context,
3174 use GetDIBits/StretchDIBits and not StretchBlt */
3179 dc
= CreateCompatibleDC(NULL
);
3181 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
3182 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
3183 bi
->bmiHeader
.biSizeImage
= 0;
3184 bi
->bmiHeader
.biClrUsed
= 0;
3185 bi
->bmiHeader
.biClrImportant
= 0;
3187 /* Fill in biSizeImage */
3188 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3189 bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bi
->bmiHeader
.biSizeImage
);
3195 /* Get the image bits of the source bitmap */
3196 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, bits
, bi
, DIB_RGB_COLORS
);
3198 /* Copy it to the destination bitmap */
3199 oldBmp
= SelectObject(dc
, res
);
3200 StretchDIBits(dc
, 0, 0, desiredx
, desiredy
,
3201 0, 0, ds
.dsBm
.bmWidth
, ds
.dsBm
.bmHeight
,
3202 bits
, bi
, DIB_RGB_COLORS
, SRCCOPY
);
3203 SelectObject(dc
, oldBmp
);
3205 HeapFree(GetProcessHeap(), 0, bits
);
3211 if (flags
& LR_COPYDELETEORG
)
3216 HeapFree(GetProcessHeap(), 0, bi
);
3220 return CURSORICON_ExtCopy(hnd
,type
, desiredx
, desiredy
, flags
);
3222 /* Should call CURSORICON_ExtCopy but more testing
3223 * needs to be done before we change this
3225 if (flags
) FIXME("Flags are ignored\n");
3226 return CopyCursor(hnd
);
3232 /******************************************************************************
3233 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3236 * Success: Handle to specified bitmap
3239 HBITMAP WINAPI
LoadBitmapW(
3240 HINSTANCE instance
, /* [in] Handle to application instance */
3241 LPCWSTR name
) /* [in] Address of bitmap resource name */
3243 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
3246 /**********************************************************************
3247 * LoadBitmapA (USER32.@)
3251 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
3253 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );