push 908cd4f159791450f5b4574b2a0f124671d31f65
[wine/hacks.git] / dlls / user32 / cursoricon.c
blobe630bd751519e81eb04dc7a2854a40ec50ced970
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Theory:
28 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
30 * 32-bit cursors and icons are stored in the server.
32 * 16-bit cursors and icons are stored in a global heap block, with the
33 * following layout:
35 * CURSORICONINFO info;
36 * BYTE[] ANDbits;
37 * BYTE[] XORbits;
39 * The bits structures are in the format of a device-dependent bitmap.
41 * This layout is very sub-optimal, as the bitmap bits are stored in
42 * the X client instead of in the server like other bitmaps; however,
43 * some programs (notably Paint Brush) expect to be able to manipulate
44 * the bits directly :-(
47 #include "config.h"
48 #include "wine/port.h"
50 #include <stdarg.h>
51 #include <string.h>
52 #include <stdlib.h>
54 #include "ntstatus.h"
55 #define WIN32_NO_STATUS
56 #include "windef.h"
57 #include "winbase.h"
58 #include "wingdi.h"
59 #include "wownt32.h"
60 #include "winerror.h"
61 #include "excpt.h"
62 #include "wine/winbase16.h"
63 #include "wine/winuser16.h"
64 #include "wine/exception.h"
65 #include "wine/debug.h"
66 #include "wine/list.h"
67 #include "wine/server.h"
68 #include "user_private.h"
70 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
71 WINE_DECLARE_DEBUG_CHANNEL(icon);
72 WINE_DECLARE_DEBUG_CHANNEL(resource);
74 #include "pshpack1.h"
76 typedef struct {
77 BYTE bWidth;
78 BYTE bHeight;
79 BYTE bColorCount;
80 BYTE bReserved;
81 WORD xHotspot;
82 WORD yHotspot;
83 DWORD dwDIBSize;
84 DWORD dwDIBOffset;
85 } CURSORICONFILEDIRENTRY;
87 typedef struct
89 WORD idReserved;
90 WORD idType;
91 WORD idCount;
92 CURSORICONFILEDIRENTRY idEntries[1];
93 } CURSORICONFILEDIR;
95 #include "poppack.h"
97 #define CID_RESOURCE 0x0001
98 #define CID_WIN32 0x0004
99 #define CID_NONSHARED 0x0008
101 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
103 static HDC screen_dc;
105 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
107 /**********************************************************************
108 * ICONCACHE for cursors/icons loaded with LR_SHARED.
110 * FIXME: This should not be allocated on the system heap, but on a
111 * subsystem-global heap (i.e. one for all Win16 processes,
112 * and one for each Win32 process).
114 typedef struct tagICONCACHE
116 struct tagICONCACHE *next;
118 HMODULE hModule;
119 HRSRC hRsrc;
120 HRSRC hGroupRsrc;
121 HICON hIcon;
123 INT count;
125 } ICONCACHE;
127 static ICONCACHE *IconAnchor = NULL;
129 static CRITICAL_SECTION IconCrst;
130 static CRITICAL_SECTION_DEBUG critsect_debug =
132 0, 0, &IconCrst,
133 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
134 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
136 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
138 static const WORD ICON_HOTSPOT = 0x4242;
140 /* What is a good table size? */
141 #define CURSOR_HASH_SIZE 97
143 typedef struct {
144 HCURSOR16 cursor16;
145 HCURSOR cursor32;
146 struct list entry16;
147 struct list entry32;
148 } cursor_map_entry_t;
150 static int get_bitmap_width_bytes( int width, int bpp );
152 static struct list cursor16to32[CURSOR_HASH_SIZE];
153 static struct list cursor32to16[CURSOR_HASH_SIZE];
155 static inline int hash_cursor_handle( DWORD handle )
157 return handle % CURSOR_HASH_SIZE;
160 static void add_cursor16to32_entry( cursor_map_entry_t *entry )
162 int idx = hash_cursor_handle( entry->cursor16 );
164 if (!cursor16to32[idx].next) list_init( &cursor16to32[idx] );
166 list_add_head( &cursor16to32[idx], &entry->entry16 );
169 static void add_cursor32to16_entry( cursor_map_entry_t *entry )
171 int idx = hash_cursor_handle( (DWORD)entry->cursor32 );
173 if (!cursor32to16[idx].next) list_init( &cursor32to16[idx] );
175 list_add_head( &cursor32to16[idx], &entry->entry32 );
178 static cursor_map_entry_t *remove_cursor16to32_entry( HCURSOR16 cursor16 )
180 cursor_map_entry_t *entry = NULL;
181 int idx = hash_cursor_handle( cursor16 );
183 if (cursor16to32[idx].next)
185 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
186 if (entry->cursor16 == cursor16)
188 list_remove( &entry->entry16 );
189 return entry;
193 return entry;
196 static cursor_map_entry_t *remove_cursor32to16_entry( HCURSOR cursor32 )
198 cursor_map_entry_t *entry = NULL;
199 int idx = hash_cursor_handle( (DWORD)cursor32 );
201 if (cursor32to16[idx].next)
203 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
204 if (entry->cursor32 == cursor32)
206 list_remove( &entry->entry32 );
207 return entry;
211 return entry;
214 /* Ask the server for a cursor */
215 static HCURSOR create_cursor( unsigned int num_frames, unsigned int delay )
217 HCURSOR cursor = 0;
219 SERVER_START_REQ(create_cursor)
221 req->num_frames = num_frames;
222 req->delay = delay;
223 if (!wine_server_call_err( req )) cursor = reply->handle;
225 SERVER_END_REQ;
227 return cursor;
230 /* Tell the server to kill a cursor */
231 static HCURSOR16 destroy_cursor( HCURSOR cursor )
233 cursor_map_entry_t *entry;
234 HCURSOR16 cursor16 = 0;
236 if (!cursor) return 0;
238 SERVER_START_REQ(destroy_cursor)
240 req->handle = cursor;
241 wine_server_call( req );
243 SERVER_END_REQ;
245 entry = remove_cursor32to16_entry( cursor );
246 if (entry)
248 cursor16 = entry->cursor16;
249 remove_cursor16to32_entry( cursor16 );
250 HeapFree( GetProcessHeap(), 0, entry );
253 return GlobalFree16( cursor16 );
256 /* Upload a cursor frame to the server */
257 static void set_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
259 SERVER_START_REQ(set_cursor_frame)
261 req->handle = cursor;
262 req->frame_idx = frame_idx;
263 req->xhot = frame->xhot;
264 req->yhot = frame->yhot;
265 req->width = frame->width;
266 req->height = frame->height;
267 req->and_width_bytes = frame->and_width_bytes;
268 req->xor_width_bytes = frame->xor_width_bytes;
269 req->planes = frame->planes;
270 req->bpp = frame->bpp;
271 wine_server_add_data( req, frame->bits, (frame->and_width_bytes + frame->xor_width_bytes) * frame->height );
272 wine_server_call( req );
274 SERVER_END_REQ;
277 /* Download a cursor frame from the server */
278 static BOOL get_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
280 NTSTATUS res;
281 /* Enough for a 32-bits 32x32 cursor / icon. */
282 unsigned int buffer_size = 4224;
283 unsigned int count = 0;
287 frame->bits = HeapAlloc(GetProcessHeap(), 0, buffer_size);
288 SERVER_START_REQ(get_cursor_frame)
290 req->handle = cursor;
291 req->frame_idx = frame_idx;
292 wine_server_set_reply( req, frame->bits, buffer_size);
293 if (!(res = wine_server_call_err( req )))
295 frame->xhot = reply->xhot;
296 frame->yhot = reply->yhot;
297 frame->width = reply->width;
298 frame->height = reply->height;
299 frame->and_width_bytes = reply->and_width_bytes;
300 frame->xor_width_bytes = reply->xor_width_bytes;
301 frame->planes = reply->planes;
302 frame->bpp = reply->bpp;
303 } else {
304 HeapFree( GetProcessHeap(), 0, frame->bits );
305 buffer_size = (reply->and_width_bytes + reply->xor_width_bytes) * reply->height;
308 SERVER_END_REQ;
309 } while (res == STATUS_BUFFER_OVERFLOW && !count++);
311 if (!frame->height)
313 HeapFree( GetProcessHeap(), 0, frame->bits );
315 return FALSE;
318 return TRUE;
321 /* Retrieve a cursor and all its frames from the server */
322 static cursor_t *get_cursor_object( HCURSOR handle )
324 unsigned int i;
325 cursor_t *cursor = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(cursor_t) );
327 SERVER_START_REQ(get_cursor_info)
329 req->handle = handle;
330 if (!wine_server_call_err( req ))
332 cursor->num_frames = reply->num_frames;
333 cursor->delay = reply->delay;
336 SERVER_END_REQ;
338 if (!cursor->num_frames)
340 HeapFree( GetProcessHeap(), 0, cursor );
341 return NULL;
344 cursor->frames = HeapAlloc( GetProcessHeap(), 0, cursor->num_frames * sizeof(cursor_frame_t) );
345 for (i = 0; i < cursor->num_frames; ++i)
347 if (!get_cursor_frame( handle, i, &cursor->frames[i] ))
349 unsigned int j;
351 for (j = 0; j < i; ++j)
353 HeapFree( GetProcessHeap(), 0, cursor->frames[j].bits );
355 HeapFree( GetProcessHeap(), 0, cursor->frames );
356 HeapFree( GetProcessHeap(), 0, cursor );
358 return NULL;
362 return cursor;
365 static void destroy_cursor_object( cursor_t *cursor )
367 unsigned int i;
369 if (!cursor) return;
371 for (i = 0; i < cursor->num_frames; ++i)
373 HeapFree( GetProcessHeap(), 0, cursor->frames[i].bits );
375 HeapFree( GetProcessHeap(), 0, cursor->frames );
376 HeapFree( GetProcessHeap(), 0, cursor );
379 /* Lookup the cursor's 16-bit handle. Create one if it doesn't already exist. */
380 HCURSOR16 get_cursor_handle16( HCURSOR cursor32, BOOL create )
382 cursor_map_entry_t *entry;
383 int idx = hash_cursor_handle( (DWORD)cursor32 );
385 if (!cursor32) return 0;
387 if (cursor32to16[idx].next)
389 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
390 if (entry->cursor32 == cursor32) return entry->cursor16;
393 /* 16-bit cursor handle not found, create one */
394 if (create)
396 size_t bits_size;
397 HCURSOR16 cursor16;
398 cursor_frame_t frame;
400 if (!get_cursor_frame( cursor32, 0, &frame )) return 0;
402 entry = HeapAlloc( GetProcessHeap(), 0, sizeof(cursor_map_entry_t) );
403 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
404 cursor16 = GlobalAlloc16( GMEM_MOVEABLE, sizeof(CURSORICONINFO) + bits_size );
405 if (cursor16)
407 CURSORICONINFO *info;
409 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
410 info->ptHotSpot.x = frame.xhot;
411 info->ptHotSpot.y = frame.yhot;
412 info->nWidth = frame.width;
413 info->nHeight = frame.height;
414 info->nWidthBytes = frame.xor_width_bytes;
415 info->bPlanes = frame.planes;
416 info->bBitsPerPixel = frame.bpp;
417 CopyMemory( info + 1, frame.bits, bits_size );
418 GlobalUnlock16( cursor16 );
420 HeapFree( GetProcessHeap(), 0, frame.bits );
422 entry->cursor16 = cursor16;
423 entry->cursor32 = cursor32;
424 add_cursor16to32_entry( entry );
425 add_cursor32to16_entry( entry );
427 return cursor16;
430 return 0;
433 HCURSOR get_cursor_handle32( HCURSOR16 cursor16 )
435 cursor_map_entry_t *entry;
436 int idx = hash_cursor_handle( cursor16 );
438 if (!cursor16) return 0;
440 if (cursor16to32[idx].next)
442 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
443 if (entry->cursor16 == cursor16) return entry->cursor32;
446 return 0;
449 static void update_cursor_32from16( HCURSOR cursor32 )
451 size_t bits_size;
452 HCURSOR16 cursor16;
453 cursor_frame_t frame;
454 CURSORICONINFO *info;
456 if (!cursor32) return;
458 cursor16 = get_cursor_handle16( cursor32, FALSE );
459 if (!cursor16) return;
461 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
462 frame.xhot = info->ptHotSpot.x;
463 frame.yhot = info->ptHotSpot.y;
464 frame.width = info->nWidth;
465 frame.height = info->nHeight;
466 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
467 frame.xor_width_bytes = info->nWidthBytes;
468 frame.planes = info->bPlanes;
469 frame.bpp = info->bBitsPerPixel;
470 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
471 frame.bits = HeapAlloc( GetProcessHeap(), 0, bits_size );
472 CopyMemory( frame.bits, info + 1, bits_size );
473 GlobalUnlock16( cursor16 );
475 set_cursor_frame( cursor32, 0, &frame );
476 HeapFree( GetProcessHeap(), 0, frame.bits );
479 /***********************************************************************
480 * map_fileW
482 * Helper function to map a file to memory:
483 * name - file name
484 * [RETURN] ptr - pointer to mapped file
485 * [RETURN] filesize - pointer size of file to be stored if not NULL
487 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
489 HANDLE hFile, hMapping;
490 LPVOID ptr = NULL;
492 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
493 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
494 if (hFile != INVALID_HANDLE_VALUE)
496 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
497 if (hMapping)
499 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
500 CloseHandle( hMapping );
501 if (filesize)
502 *filesize = GetFileSize( hFile, NULL );
504 CloseHandle( hFile );
506 return ptr;
510 /***********************************************************************
511 * get_bitmap_width_bytes
513 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
514 * data.
516 static int get_bitmap_width_bytes( int width, int bpp )
518 switch(bpp)
520 case 1:
521 return 2 * ((width+15) / 16);
522 case 4:
523 return 2 * ((width+3) / 4);
524 case 24:
525 width *= 3;
526 /* fall through */
527 case 8:
528 return width + (width & 1);
529 case 16:
530 case 15:
531 return width * 2;
532 case 32:
533 return width * 4;
534 default:
535 WARN("Unknown depth %d, please report.\n", bpp );
537 return -1;
541 /***********************************************************************
542 * get_dib_width_bytes
544 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
546 static int get_dib_width_bytes( int width, int depth )
548 int words;
550 switch(depth)
552 case 1: words = (width + 31) / 32; break;
553 case 4: words = (width + 7) / 8; break;
554 case 8: words = (width + 3) / 4; break;
555 case 15:
556 case 16: words = (width + 1) / 2; break;
557 case 24: words = (width * 3 + 3)/4; break;
558 default:
559 WARN("(%d): Unsupported depth\n", depth );
560 /* fall through */
561 case 32:
562 words = width;
564 return 4 * words;
568 /***********************************************************************
569 * bitmap_info_size
571 * Return the size of the bitmap info structure including color table.
573 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
575 int colors;
577 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
579 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
580 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
581 return sizeof(BITMAPCOREHEADER) + colors *
582 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
584 else /* assume BITMAPINFOHEADER */
586 colors = info->bmiHeader.biClrUsed;
587 if (colors > 256) /* buffer overflow otherwise */
588 colors = 256;
589 if (!colors && (info->bmiHeader.biBitCount <= 8))
590 colors = 1 << info->bmiHeader.biBitCount;
591 return sizeof(BITMAPINFOHEADER) + colors *
592 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
597 /***********************************************************************
598 * is_dib_monochrome
600 * Returns whether a DIB can be converted to a monochrome DDB.
602 * A DIB can be converted if its color table contains only black and
603 * white. Black must be the first color in the color table.
605 * Note : If the first color in the color table is white followed by
606 * black, we can't convert it to a monochrome DDB with
607 * SetDIBits, because black and white would be inverted.
609 static BOOL is_dib_monochrome( const BITMAPINFO* info )
611 if (info->bmiHeader.biBitCount != 1) return FALSE;
613 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
615 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
617 /* Check if the first color is black */
618 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
620 rgb++;
622 /* Check if the second color is white */
623 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
624 && (rgb->rgbtBlue == 0xff));
626 else return FALSE;
628 else /* assume BITMAPINFOHEADER */
630 const RGBQUAD *rgb = info->bmiColors;
632 /* Check if the first color is black */
633 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
634 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
636 rgb++;
638 /* Check if the second color is white */
639 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
640 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
642 else return FALSE;
646 /***********************************************************************
647 * DIB_GetBitmapInfo
649 * Get the info from a bitmap header.
650 * Return 1 for INFOHEADER, 0 for COREHEADER,
651 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
653 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
654 LONG *height, WORD *bpp, DWORD *compr )
656 if (header->biSize == sizeof(BITMAPINFOHEADER))
658 *width = header->biWidth;
659 *height = header->biHeight;
660 *bpp = header->biBitCount;
661 *compr = header->biCompression;
662 return 1;
664 if (header->biSize == sizeof(BITMAPCOREHEADER))
666 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
667 *width = core->bcWidth;
668 *height = core->bcHeight;
669 *bpp = core->bcBitCount;
670 *compr = 0;
671 return 0;
673 if (header->biSize == sizeof(BITMAPV4HEADER))
675 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
676 *width = v4hdr->bV4Width;
677 *height = v4hdr->bV4Height;
678 *bpp = v4hdr->bV4BitCount;
679 *compr = v4hdr->bV4V4Compression;
680 return 4;
682 if (header->biSize == sizeof(BITMAPV5HEADER))
684 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
685 *width = v5hdr->bV5Width;
686 *height = v5hdr->bV5Height;
687 *bpp = v5hdr->bV5BitCount;
688 *compr = v5hdr->bV5Compression;
689 return 5;
691 ERR("(%d): unknown/wrong size for header\n", header->biSize );
692 return -1;
695 /**********************************************************************
696 * CURSORICON_FindSharedIcon
698 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
700 HICON hIcon = 0;
701 ICONCACHE *ptr;
703 EnterCriticalSection( &IconCrst );
705 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
706 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
708 ptr->count++;
709 hIcon = ptr->hIcon;
710 break;
713 LeaveCriticalSection( &IconCrst );
715 return hIcon;
718 /*************************************************************************
719 * CURSORICON_FindCache
721 * Given a handle, find the corresponding cache element
723 * PARAMS
724 * Handle [I] handle to an Image
726 * RETURNS
727 * Success: The cache entry
728 * Failure: NULL
731 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
733 ICONCACHE *ptr;
734 ICONCACHE *pRet=NULL;
735 BOOL IsFound = FALSE;
736 int count;
738 EnterCriticalSection( &IconCrst );
740 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
742 if ( hIcon == ptr->hIcon )
744 IsFound = TRUE;
745 pRet = ptr;
749 LeaveCriticalSection( &IconCrst );
751 return pRet;
754 /**********************************************************************
755 * CURSORICON_AddSharedIcon
757 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
759 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
760 if ( !ptr ) return;
762 ptr->hModule = hModule;
763 ptr->hRsrc = hRsrc;
764 ptr->hIcon = hIcon;
765 ptr->hGroupRsrc = hGroupRsrc;
766 ptr->count = 1;
768 EnterCriticalSection( &IconCrst );
769 ptr->next = IconAnchor;
770 IconAnchor = ptr;
771 LeaveCriticalSection( &IconCrst );
774 /**********************************************************************
775 * CURSORICON_DelSharedIcon
777 static INT CURSORICON_DelSharedIcon( HICON hIcon )
779 INT count = -1;
780 ICONCACHE *ptr;
782 EnterCriticalSection( &IconCrst );
784 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
785 if ( ptr->hIcon == hIcon )
787 if ( ptr->count > 0 ) ptr->count--;
788 count = ptr->count;
789 break;
792 LeaveCriticalSection( &IconCrst );
794 return count;
797 /**********************************************************************
798 * CURSORICON_FreeModuleIcons
800 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
802 ICONCACHE **ptr = &IconAnchor;
803 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
805 EnterCriticalSection( &IconCrst );
807 while ( *ptr )
809 if ( (*ptr)->hModule == hModule )
811 ICONCACHE *freePtr = *ptr;
812 *ptr = freePtr->next;
814 destroy_cursor( freePtr->hIcon );
815 HeapFree( GetProcessHeap(), 0, freePtr );
816 continue;
818 ptr = &(*ptr)->next;
821 LeaveCriticalSection( &IconCrst );
825 * The following macro functions account for the irregularities of
826 * accessing cursor and icon resources in files and resource entries.
828 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
829 int *width, int *height, int *bits );
831 /**********************************************************************
832 * CURSORICON_FindBestIcon
834 * Find the icon closest to the requested size and number of colors.
836 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
837 int width, int height, int colors )
839 int i, cx, cy, bits, bestEntry = -1;
840 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
841 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
843 /* Find Best Fit */
844 iTotalDiff = 0xFFFFFFFF;
845 iColorDiff = 0xFFFFFFFF;
846 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
848 iTempXDiff = abs(width - cx);
849 iTempYDiff = abs(height - cy);
851 if(iTotalDiff > (iTempXDiff + iTempYDiff))
853 iXDiff = iTempXDiff;
854 iYDiff = iTempYDiff;
855 iTotalDiff = iXDiff + iYDiff;
859 /* Find Best Colors for Best Fit */
860 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
862 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
864 iTempColorDiff = abs(colors - (1<<bits));
865 if(iColorDiff > iTempColorDiff)
867 bestEntry = i;
868 iColorDiff = iTempColorDiff;
873 return bestEntry;
876 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
877 int *width, int *height, int *bits )
879 CURSORICONDIR *resdir = dir;
880 ICONRESDIR *icon;
882 if ( resdir->idCount <= n )
883 return FALSE;
884 icon = &resdir->idEntries[n].ResInfo.icon;
885 *width = icon->bWidth;
886 *height = icon->bHeight;
887 *bits = resdir->idEntries[n].wBitCount;
888 return TRUE;
891 /**********************************************************************
892 * CURSORICON_FindBestCursor
894 * Find the cursor closest to the requested size.
895 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
896 * ignored too
898 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
899 int width, int height, int color )
901 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
903 /* Double height to account for AND and XOR masks */
905 height *= 2;
907 /* First find the largest one smaller than or equal to the requested size*/
909 maxwidth = maxheight = 0;
910 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
912 if ((cx <= width) && (cy <= height) &&
913 (cx > maxwidth) && (cy > maxheight) &&
914 (bits == 1))
916 bestEntry = i;
917 maxwidth = cx;
918 maxheight = cy;
921 if (bestEntry != -1) return bestEntry;
923 /* Now find the smallest one larger than the requested size */
925 maxwidth = maxheight = 255;
926 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
928 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
929 (bestEntry==-1))
931 bestEntry = i;
932 maxwidth = cx;
933 maxheight = cy;
937 return bestEntry;
940 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
941 int *width, int *height, int *bits )
943 CURSORICONDIR *resdir = dir;
944 CURSORDIR *cursor;
946 if ( resdir->idCount <= n )
947 return FALSE;
948 cursor = &resdir->idEntries[n].ResInfo.cursor;
949 *width = cursor->wWidth;
950 *height = cursor->wHeight;
951 *bits = resdir->idEntries[n].wBitCount;
952 return TRUE;
955 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
956 int width, int height, int colors )
958 int n;
960 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
961 width, height, colors );
962 if ( n < 0 )
963 return NULL;
964 return &dir->idEntries[n];
967 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
968 int width, int height, int color )
970 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
971 width, height, color );
972 if ( n < 0 )
973 return NULL;
974 return &dir->idEntries[n];
977 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
978 int *width, int *height, int *bits )
980 CURSORICONFILEDIR *filedir = dir;
981 CURSORICONFILEDIRENTRY *entry;
983 if ( filedir->idCount <= n )
984 return FALSE;
985 entry = &filedir->idEntries[n];
986 *width = entry->bWidth;
987 *height = entry->bHeight;
988 *bits = entry->bColorCount;
989 return TRUE;
992 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
993 int width, int height, int color )
995 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
996 width, height, color );
997 if ( n < 0 )
998 return NULL;
999 return &dir->idEntries[n];
1002 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
1003 int width, int height, int color )
1005 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
1006 width, height, color );
1007 if ( n < 0 )
1008 return NULL;
1009 return &dir->idEntries[n];
1012 static BOOL load_cursor_frame( LPBYTE bits, UINT cbSize, POINT16 hotspot, DWORD dwVersion,
1013 INT width, INT height, UINT cFlag, cursor_frame_t *frame )
1015 static HDC hdcMem;
1016 int sizeAnd, sizeXor;
1017 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
1018 BITMAP bmpXor, bmpAnd;
1019 BITMAPINFO *bmi;
1020 BOOL DoStretch;
1021 INT size;
1023 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s\n",
1024 bits, cbSize, (unsigned)dwVersion, width, height,
1025 (cFlag & LR_MONOCHROME) ? "mono" : "" );
1026 if (dwVersion == 0x00020000)
1028 FIXME_(cursor)("\t2.xx resources are not supported\n");
1029 return FALSE;
1032 bmi = (BITMAPINFO *)bits;
1034 /* Check bitmap header */
1036 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
1037 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
1038 bmi->bmiHeader.biCompression != BI_RGB) )
1040 WARN_(cursor)("\tinvalid resource bitmap header.\n");
1041 return FALSE;
1044 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
1046 if (!width) width = bmi->bmiHeader.biWidth;
1047 if (!height) height = bmi->bmiHeader.biHeight/2;
1048 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
1049 (bmi->bmiHeader.biWidth != width);
1051 /* Scale the hotspot */
1052 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
1054 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
1055 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biWidth / 2);
1058 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1059 if (screen_dc)
1061 BITMAPINFO* pInfo;
1063 /* Make sure we have room for the monochrome bitmap later on.
1064 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1065 * up to and including the biBitCount. In-memory icon resource
1066 * format is as follows:
1068 * BITMAPINFOHEADER icHeader // DIB header
1069 * RGBQUAD icColors[] // Color table
1070 * BYTE icXOR[] // DIB bits for XOR mask
1071 * BYTE icAND[] // DIB bits for AND mask
1074 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
1075 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
1077 memcpy( pInfo, bmi, size );
1078 pInfo->bmiHeader.biHeight /= 2;
1080 /* Create the XOR bitmap */
1082 if (DoStretch) {
1083 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1084 if(hXorBits)
1086 HBITMAP hOld;
1087 BOOL res = FALSE;
1089 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1090 if (hdcMem) {
1091 hOld = SelectObject(hdcMem, hXorBits);
1092 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1093 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
1094 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
1095 SelectObject(hdcMem, hOld);
1097 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
1099 } else {
1100 if (is_dib_monochrome(bmi)) {
1101 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1102 SetDIBits(screen_dc, hXorBits, 0, height,
1103 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1104 } else if (bmi->bmiHeader.biBitCount == 32) {
1105 hXorBits = CreateDIBSection(screen_dc, pInfo, DIB_RGB_COLORS, NULL, NULL, 0);
1106 SetDIBits(screen_dc, hXorBits, 0, height,
1107 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1109 else
1110 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
1111 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1114 if( hXorBits )
1116 char* xbits = (char *)bmi + size +
1117 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1118 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1120 pInfo->bmiHeader.biBitCount = 1;
1121 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1123 RGBQUAD *rgb = pInfo->bmiColors;
1125 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
1126 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1127 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1128 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1130 else
1132 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
1134 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1135 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1138 /* Create the AND bitmap */
1140 if (DoStretch) {
1141 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
1142 HBITMAP hOld;
1143 BOOL res = FALSE;
1145 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1146 if (hdcMem) {
1147 hOld = SelectObject(hdcMem, hAndBits);
1148 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1149 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1150 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1151 SelectObject(hdcMem, hOld);
1153 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
1155 } else {
1156 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1158 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
1159 xbits, pInfo, DIB_RGB_COLORS);
1162 if( !hAndBits ) DeleteObject( hXorBits );
1164 HeapFree( GetProcessHeap(), 0, pInfo );
1168 if( !hXorBits || !hAndBits )
1170 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1171 return FALSE;
1174 /* Setup a cursor frame, send it to the server */
1175 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1176 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1177 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1178 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1180 frame->xhot = hotspot.x;
1181 frame->yhot = hotspot.y;
1182 frame->width = bmpXor.bmWidth;
1183 frame->height = bmpXor.bmHeight;
1184 frame->and_width_bytes = bmpAnd.bmWidthBytes;
1185 frame->xor_width_bytes = bmpXor.bmWidthBytes;
1186 frame->planes = bmpXor.bmPlanes;
1187 frame->bpp = bmpXor.bmBitsPixel;
1188 frame->bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1189 GetBitmapBits( hAndBits, sizeAnd, frame->bits );
1190 GetBitmapBits( hXorBits, sizeXor, frame->bits + sizeAnd );
1192 DeleteObject( hAndBits );
1193 DeleteObject( hXorBits );
1195 return TRUE;
1198 /**********************************************************************
1199 * .ANI cursor support
1201 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1202 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1203 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1205 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1206 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1207 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1208 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1209 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1210 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1212 #define ANI_FLAG_ICON 0x1
1213 #define ANI_FLAG_SEQUENCE 0x2
1215 typedef struct {
1216 DWORD header_size;
1217 DWORD num_frames;
1218 DWORD num_steps;
1219 DWORD width;
1220 DWORD height;
1221 DWORD bpp;
1222 DWORD num_planes;
1223 DWORD display_rate;
1224 DWORD flags;
1225 } ani_header;
1227 typedef struct {
1228 DWORD data_size;
1229 const unsigned char *data;
1230 } riff_chunk_t;
1232 static void dump_ani_header( const ani_header *header )
1234 TRACE(" header size: %d\n", header->header_size);
1235 TRACE(" frames: %d\n", header->num_frames);
1236 TRACE(" steps: %d\n", header->num_steps);
1237 TRACE(" width: %d\n", header->width);
1238 TRACE(" height: %d\n", header->height);
1239 TRACE(" bpp: %d\n", header->bpp);
1240 TRACE(" planes: %d\n", header->num_planes);
1241 TRACE(" display rate: %d\n", header->display_rate);
1242 TRACE(" flags: 0x%08x\n", header->flags);
1247 * RIFF:
1248 * DWORD "RIFF"
1249 * DWORD size
1250 * DWORD riff_id
1251 * BYTE[] data
1253 * LIST:
1254 * DWORD "LIST"
1255 * DWORD size
1256 * DWORD list_id
1257 * BYTE[] data
1259 * CHUNK:
1260 * DWORD chunk_id
1261 * DWORD size
1262 * BYTE[] data
1264 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1266 const unsigned char *ptr = parent_chunk->data;
1267 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1269 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1271 while (ptr < end)
1273 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
1274 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
1276 ptr += sizeof(DWORD);
1277 chunk->data_size = *(DWORD *)ptr;
1278 ptr += sizeof(DWORD);
1279 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1280 chunk->data = ptr;
1282 return;
1285 ptr += sizeof(DWORD);
1286 ptr += *(DWORD *)ptr;
1287 ptr += sizeof(DWORD);
1293 * .ANI layout:
1295 * RIFF:'ACON' RIFF chunk
1296 * |- CHUNK:'anih' Header
1297 * |- CHUNK:'seq ' Sequence information (optional)
1298 * \- LIST:'fram' Frame list
1299 * |- CHUNK:icon Cursor frames
1300 * |- CHUNK:icon
1301 * |- ...
1302 * \- CHUNK:icon
1304 static HCURSOR load_ani( const LPBYTE bits, DWORD bits_size, INT width, INT height )
1306 int i;
1307 WORD max_count = 0;
1308 HCURSOR cursor;
1309 CURSORICONFILEDIR *dir = 0;
1310 ani_header header = {0};
1311 DWORD *frame_seq = 0;
1312 cursor_frame_t *frames;
1313 unsigned int frame_bits_size = 0;
1314 LPBYTE frame_bits = 0;
1315 POINT16 hotspot;
1317 riff_chunk_t root_chunk = { bits_size, bits };
1318 riff_chunk_t ACON_chunk = {0};
1319 riff_chunk_t anih_chunk = {0};
1320 riff_chunk_t fram_chunk = {0};
1321 const unsigned char *icon_chunk;
1322 const unsigned char *icon_data;
1324 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1326 if (!bits) return 0;
1328 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1329 if (!ACON_chunk.data)
1331 ERR("Failed to get root chunk.\n");
1332 return 0;
1335 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1336 if (!anih_chunk.data)
1338 ERR("Failed to get 'anih' chunk.\n");
1339 return 0;
1341 memcpy( &header, anih_chunk.data, sizeof(header) );
1342 dump_ani_header( &header );
1344 if (header.flags & ANI_FLAG_SEQUENCE)
1346 riff_chunk_t seq_chunk = {0};
1348 TRACE("Loading sequence data.\n");
1349 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1350 if (!seq_chunk.data)
1352 ERR("Failed to get 'seq ' chunk\n");
1353 return 0;
1355 frame_seq = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) * header.num_steps );
1356 memcpy( frame_seq, seq_chunk.data, sizeof(DWORD) * header.num_steps );
1359 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1360 if (!fram_chunk.data)
1362 ERR("Failed to get icon list\n");
1363 return 0;
1366 icon_chunk = fram_chunk.data;
1367 icon_data = icon_chunk + (2 * sizeof(DWORD));
1368 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1369 cursor = create_cursor( header.num_steps, (100 * header.display_rate) / 6 );
1370 frames = HeapAlloc( GetProcessHeap(), 0, header.num_frames * sizeof(cursor_frame_t) );
1372 for (i = 0; i < header.num_frames; ++i)
1374 WORD count;
1375 CURSORICONFILEDIRENTRY *entry;
1376 DWORD chunk_size = *(DWORD *)(icon_chunk + sizeof(DWORD));
1378 /* Read icon count, skip magic */
1379 memcpy( &count, icon_data + sizeof(DWORD), sizeof(WORD) );
1381 /* There's a decent chance the amount of entries will be the same for each icon */
1382 if (count > max_count)
1384 HeapFree( GetProcessHeap(), 0, dir );
1385 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1386 dir = HeapAlloc( GetProcessHeap(), 0, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1387 max_count = count;
1390 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1391 memcpy( dir, icon_data, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1392 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
1394 if (frame_bits_size < entry->dwDIBSize)
1396 frame_bits_size = entry->dwDIBSize;
1397 HeapFree( GetProcessHeap(), 0, frame_bits );
1398 frame_bits = HeapAlloc( GetProcessHeap(), 0, frame_bits_size );
1401 if (!header.width || !header.height)
1403 header.width = entry->bWidth;
1404 header.height = entry->bHeight;
1407 hotspot.x = entry->xHotspot;
1408 hotspot.y = entry->yHotspot;
1410 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1412 load_cursor_frame( frame_bits, entry->dwDIBSize, hotspot, 0x00030000, header.width, header.height, 0, &frames[i] );
1414 /* Advance to the next chunk */
1415 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1416 icon_data = icon_chunk + (2 * sizeof(DWORD));
1418 HeapFree( GetProcessHeap(), 0, dir );
1420 /* Set the frames in the correct sequence */
1421 for (i = 0; i < header.num_steps; ++i)
1423 int frame_idx = (frame_seq ? frame_seq[i] : i);
1424 set_cursor_frame( cursor, i, &frames[frame_idx] );
1427 /* Cleanup */
1428 for (i = 0; i < header.num_frames; ++i)
1430 HeapFree( GetProcessHeap(), 0, frames[i].bits );
1432 HeapFree( GetProcessHeap(), 0, frame_seq );
1433 HeapFree( GetProcessHeap(), 0, frames );
1435 return cursor;
1439 /**********************************************************************
1440 * CreateIconFromResourceEx (USER32.@)
1442 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1443 * with cbSize parameter as well.
1445 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1446 BOOL bIcon, DWORD dwVersion,
1447 INT width, INT height,
1448 UINT cFlag )
1450 POINT16 hotspot;
1451 HCURSOR cursor = create_cursor( 1, 0 );
1452 cursor_frame_t frame = {0};
1454 if (bIcon)
1456 hotspot.x = ICON_HOTSPOT;
1457 hotspot.y = ICON_HOTSPOT;
1459 else
1461 hotspot = *(POINT16 *)bits;
1462 bits = (LPBYTE)(((POINT16 *)bits) + 1);
1465 if (load_cursor_frame( bits, cbSize, hotspot, dwVersion, width, height, cFlag, &frame ))
1467 set_cursor_frame( cursor, 0, &frame );
1469 else
1471 destroy_cursor( cursor );
1472 cursor = 0;
1475 HeapFree( GetProcessHeap(), 0, frame.bits );
1477 return cursor;
1481 /**********************************************************************
1482 * CreateIconFromResource (USER32.@)
1484 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1485 BOOL bIcon, DWORD dwVersion)
1487 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1491 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1492 INT width, INT height, INT colors,
1493 BOOL fCursor, UINT loadflags)
1495 CURSORICONFILEDIRENTRY *entry;
1496 cursor_frame_t frame = {0};
1497 CURSORICONFILEDIR *dir;
1498 DWORD filesize = 0;
1499 HICON hIcon = 0;
1500 POINT16 hotspot;
1501 LPBYTE bits;
1503 TRACE("loading %s\n", debugstr_w( filename ));
1505 bits = map_fileW( filename, &filesize );
1506 if (!bits)
1507 return hIcon;
1509 /* If the data contains the magic for an .ICO it's an .ICO,
1510 * regardless of what fCursor says. */
1511 if (!memcmp( bits, "\x00\x00\x01\x00", 4 )) fCursor = FALSE;
1512 /* Same thing for .CUR */
1513 else if (!memcmp( bits, "\x00\x00\x02\x00", 4 )) fCursor = TRUE;
1514 else if (!memcmp( bits, "RIFF", 4 ))
1516 hIcon = load_ani( bits, filesize, width, height );
1517 goto end;
1520 dir = (CURSORICONFILEDIR*) bits;
1521 if ( filesize < sizeof(*dir) )
1522 goto end;
1524 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1525 goto end;
1527 if ( fCursor )
1528 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1529 else
1530 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1532 if ( !entry )
1533 goto end;
1535 /* check that we don't run off the end of the file */
1536 if ( entry->dwDIBOffset > filesize )
1537 goto end;
1538 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1539 goto end;
1541 if ( fCursor )
1543 hotspot.x = entry->xHotspot;
1544 hotspot.y = entry->yHotspot;
1546 else
1548 hotspot.x = ICON_HOTSPOT;
1549 hotspot.y = ICON_HOTSPOT;
1552 hIcon = create_cursor( 1, 0 );
1553 load_cursor_frame( &bits[entry->dwDIBOffset], entry->dwDIBSize, hotspot,
1554 0x00030000, width, height, loadflags, &frame );
1555 set_cursor_frame( hIcon, 0, &frame );
1556 HeapFree( GetProcessHeap(), 0, frame.bits );
1558 end:
1559 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1560 UnmapViewOfFile( bits );
1561 return hIcon;
1564 /**********************************************************************
1565 * CURSORICON_Load
1567 * Load a cursor or icon from resource or file.
1569 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1570 INT width, INT height, INT colors,
1571 BOOL fCursor, UINT loadflags)
1573 HANDLE handle = 0;
1574 HICON hIcon = 0;
1575 HRSRC hRsrc, hGroupRsrc;
1576 CURSORICONDIR *dir;
1577 CURSORICONDIRENTRY *dirEntry;
1578 LPBYTE bits;
1579 WORD wResId;
1580 DWORD dwBytesInRes;
1582 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1583 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1585 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1586 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1588 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1590 /* Normalize hInstance (must be uniquely represented for icon cache) */
1592 if (!HIWORD( hInstance ))
1593 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1595 /* Get directory resource ID */
1597 if (!(hRsrc = FindResourceW( hInstance, name,
1598 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1599 return 0;
1600 hGroupRsrc = hRsrc;
1602 /* Find the best entry in the directory */
1604 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1605 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
1606 if (fCursor)
1607 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1608 else
1609 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1610 if (!dirEntry) return 0;
1611 wResId = dirEntry->wResId;
1612 dwBytesInRes = dirEntry->dwBytesInRes;
1613 FreeResource( handle );
1615 /* Load the resource */
1617 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1618 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1620 /* If shared icon, check whether it was already loaded */
1621 if ( (loadflags & LR_SHARED)
1622 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1623 return hIcon;
1625 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1626 bits = (LPBYTE)LockResource( handle );
1627 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1628 !fCursor, 0x00030000, width, height, loadflags);
1629 FreeResource( handle );
1631 /* If shared icon, add to icon cache */
1633 if ( hIcon && (loadflags & LR_SHARED) )
1634 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1636 return hIcon;
1639 /***********************************************************************
1640 * CURSORICON_Copy
1642 * Make a copy of a cursor or icon.
1644 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1646 /* Should animated cursors be copyable like this as well? */
1647 HCURSOR new_cursor;
1648 cursor_frame_t frame;
1650 if (!hIcon || !get_cursor_frame( hIcon, 0, &frame ))
1652 return 0;
1655 new_cursor = create_cursor( 1, 0 );
1656 set_cursor_frame( new_cursor, 0, &frame );
1657 HeapFree( GetProcessHeap(), 0, frame.bits );
1659 return new_cursor;
1662 /*************************************************************************
1663 * CURSORICON_ExtCopy
1665 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1667 * PARAMS
1668 * Handle [I] handle to an Image
1669 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1670 * iDesiredCX [I] The Desired width of the Image
1671 * iDesiredCY [I] The desired height of the Image
1672 * nFlags [I] The flags from CopyImage
1674 * RETURNS
1675 * Success: The new handle of the Image
1677 * NOTES
1678 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1679 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1680 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1685 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1686 INT iDesiredCX, INT iDesiredCY,
1687 UINT nFlags)
1689 HICON hNew=0;
1691 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1692 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1694 if(hIcon == 0)
1696 return 0;
1699 /* Best Fit or Monochrome */
1700 if( (nFlags & LR_COPYFROMRESOURCE
1701 && (iDesiredCX > 0 || iDesiredCY > 0))
1702 || nFlags & LR_MONOCHROME)
1704 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1706 /* Not Found in Cache, then do a straight copy
1708 if(pIconCache == NULL)
1710 hNew = CURSORICON_Copy(0, hIcon);
1711 if(nFlags & LR_COPYFROMRESOURCE)
1713 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1716 else
1718 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1719 LPBYTE pBits;
1720 HANDLE hMem;
1721 HRSRC hRsrc;
1722 DWORD dwBytesInRes;
1723 WORD wResId;
1724 CURSORICONDIR *pDir;
1725 CURSORICONDIRENTRY *pDirEntry;
1726 BOOL bIsIcon = (nType == IMAGE_ICON);
1728 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1730 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1731 || (iDesiredCX == 0 && iDesiredCY == 0))
1733 iDesiredCY = GetSystemMetrics(bIsIcon ?
1734 SM_CYICON : SM_CYCURSOR);
1735 iDesiredCX = GetSystemMetrics(bIsIcon ?
1736 SM_CXICON : SM_CXCURSOR);
1739 /* Retrieve the CURSORICONDIRENTRY
1741 if (!(hMem = LoadResource( pIconCache->hModule ,
1742 pIconCache->hGroupRsrc)))
1744 return 0;
1746 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1748 return 0;
1751 /* Find Best Fit
1753 if(bIsIcon)
1755 pDirEntry = CURSORICON_FindBestIconRes(
1756 pDir, iDesiredCX, iDesiredCY, 256 );
1758 else
1760 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursorRes(
1761 pDir, iDesiredCX, iDesiredCY, 1);
1764 wResId = pDirEntry->wResId;
1765 dwBytesInRes = pDirEntry->dwBytesInRes;
1766 FreeResource(hMem);
1768 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1769 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1770 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1772 /* Get the Best Fit
1774 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1775 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1777 return 0;
1779 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1781 return 0;
1784 pBits = (LPBYTE)LockResource( hMem );
1786 if(nFlags & LR_DEFAULTSIZE)
1788 iTargetCY = GetSystemMetrics(SM_CYICON);
1789 iTargetCX = GetSystemMetrics(SM_CXICON);
1792 /* Create a New Icon with the proper dimension
1794 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1795 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1796 FreeResource(hMem);
1799 else hNew = CURSORICON_Copy(0, hIcon);
1800 return hNew;
1804 /***********************************************************************
1805 * CreateCursor (USER32.@)
1807 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1808 INT xHotSpot, INT yHotSpot,
1809 INT nWidth, INT nHeight,
1810 LPCVOID lpANDbits, LPCVOID lpXORbits )
1812 CURSORICONINFO info;
1814 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1815 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1817 info.ptHotSpot.x = xHotSpot;
1818 info.ptHotSpot.y = yHotSpot;
1819 info.nWidth = nWidth;
1820 info.nHeight = nHeight;
1821 info.nWidthBytes = 0;
1822 info.bPlanes = 1;
1823 info.bBitsPerPixel = 1;
1825 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1829 /***********************************************************************
1830 * CreateIcon (USER.407)
1832 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1833 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1834 LPCVOID lpANDbits, LPCVOID lpXORbits )
1836 CURSORICONINFO info;
1838 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1839 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1841 info.ptHotSpot.x = ICON_HOTSPOT;
1842 info.ptHotSpot.y = ICON_HOTSPOT;
1843 info.nWidth = nWidth;
1844 info.nHeight = nHeight;
1845 info.nWidthBytes = 0;
1846 info.bPlanes = bPlanes;
1847 info.bBitsPerPixel = bBitsPixel;
1849 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1853 /***********************************************************************
1854 * CreateIcon (USER32.@)
1856 * Creates an icon based on the specified bitmaps. The bitmaps must be
1857 * provided in a device dependent format and will be resized to
1858 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1859 * depth. The provided bitmaps must be top-down bitmaps.
1860 * Although Windows does not support 15bpp(*) this API must support it
1861 * for Winelib applications.
1863 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1864 * format!
1866 * RETURNS
1867 * Success: handle to an icon
1868 * Failure: NULL
1870 * FIXME: Do we need to resize the bitmaps?
1872 HICON WINAPI CreateIcon(
1873 HINSTANCE hInstance, /* [in] the application's hInstance */
1874 INT nWidth, /* [in] the width of the provided bitmaps */
1875 INT nHeight, /* [in] the height of the provided bitmaps */
1876 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1877 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1878 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1879 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1881 ICONINFO iinfo;
1882 HICON hIcon;
1884 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1885 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1887 iinfo.fIcon = TRUE;
1888 iinfo.xHotspot = ICON_HOTSPOT;
1889 iinfo.yHotspot = ICON_HOTSPOT;
1890 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1891 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1893 hIcon = CreateIconIndirect( &iinfo );
1895 DeleteObject( iinfo.hbmMask );
1896 DeleteObject( iinfo.hbmColor );
1898 return hIcon;
1902 /***********************************************************************
1903 * CreateCursorIconIndirect (USER.408)
1905 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1906 CURSORICONINFO *info,
1907 LPCVOID lpANDbits,
1908 LPCVOID lpXORbits )
1910 HCURSOR cursor;
1911 cursor_frame_t frame;
1912 int sizeAnd, sizeXor;
1914 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1915 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1916 sizeXor = info->nHeight * info->nWidthBytes;
1917 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1919 cursor = create_cursor( 1, 0 );
1920 frame.xhot = info->ptHotSpot.x;
1921 frame.yhot = info->ptHotSpot.y;
1922 frame.width = info->nWidth;
1923 frame.height = info->nHeight;
1924 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
1925 frame.xor_width_bytes = info->nWidthBytes;
1926 frame.planes = info->bPlanes;
1927 frame.bpp = info->bBitsPerPixel;
1928 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1929 CopyMemory( frame.bits, lpANDbits, sizeAnd );
1930 CopyMemory( frame.bits + sizeAnd, lpXORbits, sizeXor );
1931 set_cursor_frame( cursor, 0, &frame );
1932 HeapFree( GetProcessHeap(), 0, frame.bits );
1934 return HICON_16(cursor);
1938 /***********************************************************************
1939 * CopyIcon (USER.368)
1941 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1943 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1944 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1948 /***********************************************************************
1949 * CopyIcon (USER32.@)
1951 HICON WINAPI CopyIcon( HICON hIcon )
1953 TRACE_(icon)("%p\n", hIcon );
1954 return CURSORICON_Copy( 0, hIcon );
1958 /***********************************************************************
1959 * CopyCursor (USER.369)
1961 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1963 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1964 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1967 /**********************************************************************
1968 * DestroyIcon32 (USER.610)
1970 * This routine is actually exported from Win95 USER under the name
1971 * DestroyIcon32 ... The behaviour implemented here should mimic
1972 * the Win95 one exactly, especially the return values, which
1973 * depend on the setting of various flags.
1975 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1977 WORD retv;
1979 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1981 /* Check whether destroying active cursor */
1983 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1985 WARN_(cursor)("Destroying active cursor!\n" );
1986 SetCursor( 0 );
1989 /* Try shared cursor/icon first */
1991 if ( !(flags & CID_NONSHARED) )
1993 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1995 if ( count != -1 )
1996 return (flags & CID_WIN32)? TRUE : (count == 0);
1998 /* FIXME: OEM cursors/icons should be recognized */
2001 /* Now assume non-shared cursor/icon */
2003 retv = destroy_cursor( HCURSOR_32(handle) );
2004 return (flags & CID_RESOURCE)? retv : TRUE;
2007 /***********************************************************************
2008 * DestroyIcon (USER32.@)
2010 BOOL WINAPI DestroyIcon( HICON hIcon )
2012 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
2016 /***********************************************************************
2017 * DestroyCursor (USER32.@)
2019 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
2021 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
2025 /***********************************************************************
2026 * DrawIcon (USER32.@)
2028 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
2030 CURSORICONINFO *ptr;
2031 HDC hMemDC;
2032 HBITMAP hXorBits, hAndBits;
2033 COLORREF oldFg, oldBg;
2035 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
2037 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
2038 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2039 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
2040 (char *)(ptr+1) );
2041 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
2042 ptr->bBitsPerPixel, (char *)(ptr + 1)
2043 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
2044 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2045 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2047 if (hXorBits && hAndBits)
2049 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2050 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
2051 SelectObject( hMemDC, hXorBits );
2052 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
2053 SelectObject( hMemDC, hBitTemp );
2055 DeleteDC( hMemDC );
2056 if (hXorBits) DeleteObject( hXorBits );
2057 if (hAndBits) DeleteObject( hAndBits );
2058 GlobalUnlock16(HICON_16(hIcon));
2059 SetTextColor( hdc, oldFg );
2060 SetBkColor( hdc, oldBg );
2061 return TRUE;
2064 /***********************************************************************
2065 * DumpIcon (USER.459)
2067 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
2068 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
2070 CURSORICONINFO *info = MapSL( pInfo );
2071 int sizeAnd, sizeXor;
2073 if (!info) return 0;
2074 sizeXor = info->nHeight * info->nWidthBytes;
2075 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
2076 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
2077 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
2078 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
2079 return MAKELONG( sizeXor, sizeXor );
2083 /***********************************************************************
2084 * SetCursor (USER32.@)
2086 * Set the cursor shape.
2088 * RETURNS
2089 * A handle to the previous cursor shape.
2091 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
2093 struct user_thread_info *thread_info = get_user_thread_info();
2094 HCURSOR hOldCursor;
2096 if (hCursor == thread_info->cursor) return hCursor; /* No change */
2097 TRACE("%p\n", hCursor);
2098 hOldCursor = thread_info->cursor;
2099 thread_info->cursor = hCursor;
2100 /* Change the cursor shape only if it is visible */
2101 if (thread_info->cursor_count >= 0)
2103 cursor_t *cursor;
2105 update_cursor_32from16( hCursor );
2106 cursor = get_cursor_object( hCursor );
2107 USER_Driver->pSetCursor( cursor );
2108 destroy_cursor_object( cursor );
2110 return hOldCursor;
2113 /***********************************************************************
2114 * ShowCursor (USER32.@)
2116 INT WINAPI ShowCursor( BOOL bShow )
2118 struct user_thread_info *thread_info = get_user_thread_info();
2120 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
2122 if (bShow)
2124 if (++thread_info->cursor_count == 0) /* Show it */
2126 cursor_t *cursor;
2128 update_cursor_32from16( thread_info->cursor );
2129 cursor = get_cursor_object( thread_info->cursor );
2130 USER_Driver->pSetCursor( cursor );
2131 destroy_cursor_object( cursor );
2134 else
2136 if (--thread_info->cursor_count == -1) /* Hide it */
2137 USER_Driver->pSetCursor( NULL );
2139 return thread_info->cursor_count;
2142 /***********************************************************************
2143 * GetCursor (USER32.@)
2145 HCURSOR WINAPI GetCursor(void)
2147 return get_user_thread_info()->cursor;
2151 /***********************************************************************
2152 * ClipCursor (USER32.@)
2154 BOOL WINAPI ClipCursor( const RECT *rect )
2156 RECT virt;
2158 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
2159 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
2160 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
2161 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
2163 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
2164 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
2166 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
2167 CURSOR_ClipRect = virt;
2169 USER_Driver->pClipCursor( rect );
2170 return TRUE;
2174 /***********************************************************************
2175 * GetClipCursor (USER32.@)
2177 BOOL WINAPI GetClipCursor( RECT *rect )
2179 /* If this is first time - initialize the rect */
2180 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
2182 return CopyRect( rect, &CURSOR_ClipRect );
2186 /***********************************************************************
2187 * SetSystemCursor (USER32.@)
2189 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
2191 FIXME("(%p,%08x),stub!\n", hcur, id);
2192 return TRUE;
2196 /**********************************************************************
2197 * LookupIconIdFromDirectoryEx (USER.364)
2199 * FIXME: exact parameter sizes
2201 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
2202 INT16 width, INT16 height, UINT16 cFlag )
2204 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
2207 /**********************************************************************
2208 * LookupIconIdFromDirectoryEx (USER32.@)
2210 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
2211 INT width, INT height, UINT cFlag )
2213 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
2214 UINT retVal = 0;
2215 if( dir && !dir->idReserved && (dir->idType & 3) )
2217 CURSORICONDIRENTRY* entry;
2218 HDC hdc;
2219 UINT palEnts;
2220 int colors;
2221 hdc = GetDC(0);
2222 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
2223 if (palEnts == 0)
2224 palEnts = 256;
2225 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
2227 ReleaseDC(0, hdc);
2229 if( bIcon )
2230 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
2231 else
2232 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
2234 if( entry ) retVal = entry->wResId;
2236 else WARN_(cursor)("invalid resource directory\n");
2237 return retVal;
2240 /**********************************************************************
2241 * LookupIconIdFromDirectory (USER.?)
2243 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
2245 return LookupIconIdFromDirectoryEx16( dir, bIcon,
2246 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2247 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2250 /**********************************************************************
2251 * LookupIconIdFromDirectory (USER32.@)
2253 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
2255 return LookupIconIdFromDirectoryEx( dir, bIcon,
2256 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2257 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2260 /**********************************************************************
2261 * GetIconID (USER.455)
2263 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
2265 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
2267 TRACE_(cursor)("hRes=%04x, entries=%i\n",
2268 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
2270 switch(resType)
2272 case RT_CURSOR:
2273 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
2274 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
2275 case RT_ICON:
2276 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
2277 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
2278 default:
2279 WARN_(cursor)("invalid res type %d\n", resType );
2281 return 0;
2284 /**********************************************************************
2285 * LoadCursorIconHandler (USER.336)
2287 * Supposed to load resources of Windows 2.x applications.
2289 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
2291 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2292 hResource, hModule, hRsrc);
2293 return (HGLOBAL16)0;
2296 /**********************************************************************
2297 * LoadIconHandler (USER.456)
2299 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2301 LPBYTE bits = (LPBYTE)LockResource16( hResource );
2303 TRACE_(cursor)("hRes=%04x\n",hResource);
2305 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2306 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2309 /***********************************************************************
2310 * LoadCursorW (USER32.@)
2312 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2314 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2316 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2317 LR_SHARED | LR_DEFAULTSIZE );
2320 /***********************************************************************
2321 * LoadCursorA (USER32.@)
2323 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2325 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2327 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2328 LR_SHARED | LR_DEFAULTSIZE );
2331 /***********************************************************************
2332 * LoadCursorFromFileW (USER32.@)
2334 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2336 TRACE("%s\n", debugstr_w(name));
2338 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2339 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2342 /***********************************************************************
2343 * LoadCursorFromFileA (USER32.@)
2345 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2347 TRACE("%s\n", debugstr_a(name));
2349 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2350 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2353 /***********************************************************************
2354 * LoadIconW (USER32.@)
2356 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2358 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2360 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2361 LR_SHARED | LR_DEFAULTSIZE );
2364 /***********************************************************************
2365 * LoadIconA (USER32.@)
2367 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2369 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2371 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2372 LR_SHARED | LR_DEFAULTSIZE );
2375 /**********************************************************************
2376 * GetIconInfo (USER32.@)
2378 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2380 CURSORICONINFO *ciconinfo;
2381 INT height;
2383 ciconinfo = GlobalLock16(HICON_16(hIcon));
2384 if (!ciconinfo)
2385 return FALSE;
2387 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2388 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2390 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2391 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2393 iconinfo->fIcon = TRUE;
2394 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2395 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2397 else
2399 iconinfo->fIcon = FALSE;
2400 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2401 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2404 height = ciconinfo->nHeight;
2406 if (ciconinfo->bBitsPerPixel > 1)
2408 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2409 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2410 (char *)(ciconinfo + 1)
2411 + ciconinfo->nHeight *
2412 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2414 else
2416 iconinfo->hbmColor = 0;
2417 height *= 2;
2420 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2421 1, 1, (char *)(ciconinfo + 1));
2423 GlobalUnlock16(HICON_16(hIcon));
2425 return TRUE;
2428 /**********************************************************************
2429 * CreateIconIndirect (USER32.@)
2431 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2433 HCURSOR cursor;
2434 cursor_frame_t frame;
2435 BITMAP bmpXor,bmpAnd;
2436 int sizeXor,sizeAnd;
2438 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2439 iconinfo->hbmColor, iconinfo->hbmMask,
2440 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2442 if (!iconinfo->hbmMask) return 0;
2444 if (iconinfo->hbmColor)
2446 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2447 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2448 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2449 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2451 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2452 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2453 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2454 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2456 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
2457 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2459 cursor = create_cursor( 1, 0 );
2461 /* If we are creating an icon, the hotspot is unused */
2462 if (iconinfo->fIcon)
2464 frame.xhot = ICON_HOTSPOT;
2465 frame.yhot = ICON_HOTSPOT;
2467 else
2469 frame.xhot = iconinfo->xHotspot;
2470 frame.yhot = iconinfo->yHotspot;
2473 if (iconinfo->hbmColor)
2475 frame.width = bmpXor.bmWidth;
2476 frame.height = bmpXor.bmHeight;
2477 frame.and_width_bytes = bmpAnd.bmWidthBytes;
2478 frame.xor_width_bytes = bmpXor.bmWidthBytes;
2479 frame.planes = bmpXor.bmPlanes;
2480 frame.bpp = bmpXor.bmBitsPixel;
2482 else
2484 frame.width = bmpAnd.bmWidth;
2485 frame.height = bmpAnd.bmHeight / 2;
2486 frame.and_width_bytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2487 frame.xor_width_bytes = 0;
2488 frame.planes = 1;
2489 frame.bpp = 1;
2492 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
2494 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2495 if (bmpAnd.bmBitsPixel == 1)
2497 GetBitmapBits( iconinfo->hbmMask, sizeAnd, frame.bits );
2499 else
2501 HDC hdc, hdc_mem;
2502 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2504 hdc = GetDC( 0 );
2505 hdc_mem = CreateCompatibleDC( hdc );
2507 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2509 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2510 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2512 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2514 SelectObject( hdc, hbmp_old );
2515 SelectObject( hdc_mem, hbmp_mem_old );
2517 DeleteDC( hdc_mem );
2518 ReleaseDC( 0, hdc );
2520 GetBitmapBits( hbmp_mono, sizeAnd, frame.bits );
2521 DeleteObject( hbmp_mono );
2524 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, frame.bits + sizeAnd );
2525 set_cursor_frame( cursor, 0, &frame );
2526 HeapFree( GetProcessHeap(), 0, frame.bits );
2528 return cursor;
2531 /******************************************************************************
2532 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2534 * NOTES
2535 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2537 * PARAMS
2538 * hdc [I] Handle to device context
2539 * x0 [I] X coordinate of upper left corner
2540 * y0 [I] Y coordinate of upper left corner
2541 * hIcon [I] Handle to icon to draw
2542 * cxWidth [I] Width of icon
2543 * cyWidth [I] Height of icon
2544 * istep [I] Index of frame in animated cursor
2545 * hbr [I] Handle to background brush
2546 * flags [I] Icon-drawing flags
2548 * RETURNS
2549 * Success: TRUE
2550 * Failure: FALSE
2552 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2553 INT cxWidth, INT cyWidth, UINT istep,
2554 HBRUSH hbr, UINT flags )
2556 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
2557 HDC hDC_off = 0, hMemDC;
2558 BOOL result = FALSE, DoOffscreen;
2559 HBITMAP hB_off = 0, hOld = 0;
2561 if (!ptr) return FALSE;
2562 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2563 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2565 hMemDC = CreateCompatibleDC (hdc);
2566 if (istep)
2567 FIXME_(icon)("Ignoring istep=%d\n", istep);
2568 if (flags & DI_COMPAT)
2569 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
2571 if (!flags) {
2572 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2573 flags = DI_NORMAL;
2576 /* Calculate the size of the destination image. */
2577 if (cxWidth == 0)
2579 if (flags & DI_DEFAULTSIZE)
2580 cxWidth = GetSystemMetrics (SM_CXICON);
2581 else
2582 cxWidth = ptr->nWidth;
2584 if (cyWidth == 0)
2586 if (flags & DI_DEFAULTSIZE)
2587 cyWidth = GetSystemMetrics (SM_CYICON);
2588 else
2589 cyWidth = ptr->nHeight;
2592 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2594 if (DoOffscreen) {
2595 RECT r;
2597 r.left = 0;
2598 r.top = 0;
2599 r.right = cxWidth;
2600 r.bottom = cxWidth;
2602 hDC_off = CreateCompatibleDC(hdc);
2603 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2604 if (hDC_off && hB_off) {
2605 hOld = SelectObject(hDC_off, hB_off);
2606 FillRect(hDC_off, &r, hbr);
2610 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2612 HBITMAP hXorBits, hAndBits;
2613 COLORREF oldFg, oldBg;
2614 INT nStretchMode;
2616 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2618 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2619 ptr->bPlanes, ptr->bBitsPerPixel,
2620 (char *)(ptr + 1)
2621 + ptr->nHeight *
2622 get_bitmap_width_bytes(ptr->nWidth,1) );
2623 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2624 1, 1, (char *)(ptr+1) );
2625 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2626 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2628 if (hXorBits && hAndBits)
2630 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2631 if (flags & DI_MASK)
2633 if (DoOffscreen)
2634 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2635 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2636 else
2637 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2638 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2640 SelectObject( hMemDC, hXorBits );
2641 if (flags & DI_IMAGE)
2643 if (DoOffscreen)
2644 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2645 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2646 else
2647 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2648 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2650 SelectObject( hMemDC, hBitTemp );
2651 result = TRUE;
2654 SetTextColor( hdc, oldFg );
2655 SetBkColor( hdc, oldBg );
2656 if (hXorBits) DeleteObject( hXorBits );
2657 if (hAndBits) DeleteObject( hAndBits );
2658 SetStretchBltMode (hdc, nStretchMode);
2659 if (DoOffscreen) {
2660 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2661 SelectObject(hDC_off, hOld);
2664 if (hMemDC) DeleteDC( hMemDC );
2665 if (hDC_off) DeleteDC(hDC_off);
2666 if (hB_off) DeleteObject(hB_off);
2667 GlobalUnlock16(HICON_16(hIcon));
2668 return result;
2671 /***********************************************************************
2672 * DIB_FixColorsToLoadflags
2674 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2675 * are in loadflags
2677 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2679 int colors;
2680 COLORREF c_W, c_S, c_F, c_L, c_C;
2681 int incr,i;
2682 RGBQUAD *ptr;
2683 int bitmap_type;
2684 LONG width;
2685 LONG height;
2686 WORD bpp;
2687 DWORD compr;
2689 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2691 WARN_(resource)("Invalid bitmap\n");
2692 return;
2695 if (bpp > 8) return;
2697 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2699 incr = 3;
2700 colors = 1 << bpp;
2702 else
2704 incr = 4;
2705 colors = bmi->bmiHeader.biClrUsed;
2706 if (colors > 256) colors = 256;
2707 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2710 c_W = GetSysColor(COLOR_WINDOW);
2711 c_S = GetSysColor(COLOR_3DSHADOW);
2712 c_F = GetSysColor(COLOR_3DFACE);
2713 c_L = GetSysColor(COLOR_3DLIGHT);
2715 if (loadflags & LR_LOADTRANSPARENT) {
2716 switch (bpp) {
2717 case 1: pix = pix >> 7; break;
2718 case 4: pix = pix >> 4; break;
2719 case 8: break;
2720 default:
2721 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2722 return;
2724 if (pix >= colors) {
2725 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2726 return;
2728 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2729 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2730 ptr->rgbBlue = GetBValue(c_W);
2731 ptr->rgbGreen = GetGValue(c_W);
2732 ptr->rgbRed = GetRValue(c_W);
2734 if (loadflags & LR_LOADMAP3DCOLORS)
2735 for (i=0; i<colors; i++) {
2736 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2737 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2738 if (c_C == RGB(128, 128, 128)) {
2739 ptr->rgbRed = GetRValue(c_S);
2740 ptr->rgbGreen = GetGValue(c_S);
2741 ptr->rgbBlue = GetBValue(c_S);
2742 } else if (c_C == RGB(192, 192, 192)) {
2743 ptr->rgbRed = GetRValue(c_F);
2744 ptr->rgbGreen = GetGValue(c_F);
2745 ptr->rgbBlue = GetBValue(c_F);
2746 } else if (c_C == RGB(223, 223, 223)) {
2747 ptr->rgbRed = GetRValue(c_L);
2748 ptr->rgbGreen = GetGValue(c_L);
2749 ptr->rgbBlue = GetBValue(c_L);
2755 /**********************************************************************
2756 * BITMAP_Load
2758 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2759 INT desiredx, INT desiredy, UINT loadflags )
2761 HBITMAP hbitmap = 0, orig_bm;
2762 HRSRC hRsrc;
2763 HGLOBAL handle;
2764 char *ptr = NULL;
2765 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2766 int size;
2767 BYTE pix;
2768 char *bits;
2769 LONG width, height, new_width, new_height;
2770 WORD bpp_dummy;
2771 DWORD compr_dummy;
2772 INT bm_type;
2773 HDC screen_mem_dc = NULL;
2775 if (!(loadflags & LR_LOADFROMFILE))
2777 if (!instance)
2779 /* OEM bitmap: try to load the resource from user32.dll */
2780 instance = user32_module;
2783 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2784 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2786 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2788 else
2790 if (!(ptr = map_fileW( name, NULL ))) return 0;
2791 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2794 size = bitmap_info_size(info, DIB_RGB_COLORS);
2795 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2796 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2798 if (!fix_info || !scaled_info) goto end;
2799 memcpy(fix_info, info, size);
2801 pix = *((LPBYTE)info + size);
2802 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2804 memcpy(scaled_info, fix_info, size);
2805 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2806 &bpp_dummy, &compr_dummy);
2807 if(desiredx != 0)
2808 new_width = desiredx;
2809 else
2810 new_width = width;
2812 if(desiredy != 0)
2813 new_height = height > 0 ? desiredy : -desiredy;
2814 else
2815 new_height = height;
2817 if(bm_type == 0)
2819 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2820 core->bcWidth = new_width;
2821 core->bcHeight = new_height;
2823 else
2825 scaled_info->bmiHeader.biWidth = new_width;
2826 scaled_info->bmiHeader.biHeight = new_height;
2829 if (new_height < 0) new_height = -new_height;
2831 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2832 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2834 bits = (char *)info + size;
2836 if (loadflags & LR_CREATEDIBSECTION)
2838 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2839 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2841 else
2843 if (is_dib_monochrome(fix_info))
2844 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2845 else
2846 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2849 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2850 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2851 SelectObject(screen_mem_dc, orig_bm);
2853 end:
2854 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2855 HeapFree(GetProcessHeap(), 0, scaled_info);
2856 HeapFree(GetProcessHeap(), 0, fix_info);
2857 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2859 return hbitmap;
2862 /**********************************************************************
2863 * LoadImageA (USER32.@)
2865 * See LoadImageW.
2867 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2868 INT desiredx, INT desiredy, UINT loadflags)
2870 HANDLE res;
2871 LPWSTR u_name;
2873 if (!HIWORD(name))
2874 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2876 __TRY {
2877 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2878 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2879 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2881 __EXCEPT_PAGE_FAULT {
2882 SetLastError( ERROR_INVALID_PARAMETER );
2883 return 0;
2885 __ENDTRY
2886 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2887 HeapFree(GetProcessHeap(), 0, u_name);
2888 return res;
2892 /******************************************************************************
2893 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2895 * PARAMS
2896 * hinst [I] Handle of instance that contains image
2897 * name [I] Name of image
2898 * type [I] Type of image
2899 * desiredx [I] Desired width
2900 * desiredy [I] Desired height
2901 * loadflags [I] Load flags
2903 * RETURNS
2904 * Success: Handle to newly loaded image
2905 * Failure: NULL
2907 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2909 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2910 INT desiredx, INT desiredy, UINT loadflags )
2912 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2913 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2915 if (loadflags & LR_DEFAULTSIZE) {
2916 if (type == IMAGE_ICON) {
2917 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2918 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2919 } else if (type == IMAGE_CURSOR) {
2920 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2921 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2924 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2925 switch (type) {
2926 case IMAGE_BITMAP:
2927 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2929 case IMAGE_ICON:
2930 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2931 if (screen_dc)
2933 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2934 if (palEnts == 0) palEnts = 256;
2935 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2936 palEnts, FALSE, loadflags);
2938 break;
2940 case IMAGE_CURSOR:
2941 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2942 1, TRUE, loadflags);
2944 return 0;
2947 /******************************************************************************
2948 * CopyImage (USER32.@) Creates new image and copies attributes to it
2950 * PARAMS
2951 * hnd [I] Handle to image to copy
2952 * type [I] Type of image to copy
2953 * desiredx [I] Desired width of new image
2954 * desiredy [I] Desired height of new image
2955 * flags [I] Copy flags
2957 * RETURNS
2958 * Success: Handle to newly created image
2959 * Failure: NULL
2961 * BUGS
2962 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2963 * all other versions (95/2000/XP have been tested) ignore it.
2965 * NOTES
2966 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2967 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2968 * the copy will have the same depth as the screen.
2969 * The content of the image will only be copied if the bit depth of the
2970 * original image is compatible with the bit depth of the screen, or
2971 * if the source is a DIB section.
2972 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2974 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2975 INT desiredy, UINT flags )
2977 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2978 hnd, type, desiredx, desiredy, flags);
2980 switch (type)
2982 case IMAGE_BITMAP:
2984 HBITMAP res = NULL;
2985 DIBSECTION ds;
2986 int objSize;
2987 BITMAPINFO * bi;
2989 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2990 if (!objSize) return 0;
2991 if ((desiredx < 0) || (desiredy < 0)) return 0;
2993 if (flags & LR_COPYFROMRESOURCE)
2995 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2998 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2999 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
3001 /* Allocate memory for a BITMAPINFOHEADER structure and a
3002 color table. The maximum number of colors in a color table
3003 is 256 which corresponds to a bitmap with depth 8.
3004 Bitmaps with higher depths don't have color tables. */
3005 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3006 if (!bi) return 0;
3008 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3009 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
3010 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3011 bi->bmiHeader.biCompression = BI_RGB;
3013 if (flags & LR_CREATEDIBSECTION)
3015 /* Create a DIB section. LR_MONOCHROME is ignored */
3016 void * bits;
3017 HDC dc = CreateCompatibleDC(NULL);
3019 if (objSize == sizeof(DIBSECTION))
3021 /* The source bitmap is a DIB.
3022 Get its attributes to create an exact copy */
3023 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
3026 /* Get the color table or the color masks */
3027 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3029 bi->bmiHeader.biWidth = desiredx;
3030 bi->bmiHeader.biHeight = desiredy;
3031 bi->bmiHeader.biSizeImage = 0;
3033 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
3034 DeleteDC(dc);
3036 else
3038 /* Create a device-dependent bitmap */
3040 BOOL monochrome = (flags & LR_MONOCHROME);
3042 if (objSize == sizeof(DIBSECTION))
3044 /* The source bitmap is a DIB section.
3045 Get its attributes */
3046 HDC dc = CreateCompatibleDC(NULL);
3047 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3048 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3049 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3050 DeleteDC(dc);
3052 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
3054 /* Look if the colors of the DIB are black and white */
3056 monochrome =
3057 (bi->bmiColors[0].rgbRed == 0xff
3058 && bi->bmiColors[0].rgbGreen == 0xff
3059 && bi->bmiColors[0].rgbBlue == 0xff
3060 && bi->bmiColors[0].rgbReserved == 0
3061 && bi->bmiColors[1].rgbRed == 0
3062 && bi->bmiColors[1].rgbGreen == 0
3063 && bi->bmiColors[1].rgbBlue == 0
3064 && bi->bmiColors[1].rgbReserved == 0)
3066 (bi->bmiColors[0].rgbRed == 0
3067 && bi->bmiColors[0].rgbGreen == 0
3068 && bi->bmiColors[0].rgbBlue == 0
3069 && bi->bmiColors[0].rgbReserved == 0
3070 && bi->bmiColors[1].rgbRed == 0xff
3071 && bi->bmiColors[1].rgbGreen == 0xff
3072 && bi->bmiColors[1].rgbBlue == 0xff
3073 && bi->bmiColors[1].rgbReserved == 0);
3076 else if (!monochrome)
3078 monochrome = ds.dsBm.bmBitsPixel == 1;
3081 if (monochrome)
3083 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
3085 else
3087 HDC screenDC = GetDC(NULL);
3088 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
3089 ReleaseDC(NULL, screenDC);
3093 if (res)
3095 /* Only copy the bitmap if it's a DIB section or if it's
3096 compatible to the screen */
3097 BOOL copyContents;
3099 if (objSize == sizeof(DIBSECTION))
3101 copyContents = TRUE;
3103 else
3105 HDC screenDC = GetDC(NULL);
3106 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
3107 ReleaseDC(NULL, screenDC);
3109 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
3112 if (copyContents)
3114 /* The source bitmap may already be selected in a device context,
3115 use GetDIBits/StretchDIBits and not StretchBlt */
3117 HDC dc;
3118 void * bits;
3120 dc = CreateCompatibleDC(NULL);
3122 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
3123 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
3124 bi->bmiHeader.biSizeImage = 0;
3125 bi->bmiHeader.biClrUsed = 0;
3126 bi->bmiHeader.biClrImportant = 0;
3128 /* Fill in biSizeImage */
3129 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3130 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
3132 if (bits)
3134 HBITMAP oldBmp;
3136 /* Get the image bits of the source bitmap */
3137 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
3139 /* Copy it to the destination bitmap */
3140 oldBmp = SelectObject(dc, res);
3141 StretchDIBits(dc, 0, 0, desiredx, desiredy,
3142 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
3143 bits, bi, DIB_RGB_COLORS, SRCCOPY);
3144 SelectObject(dc, oldBmp);
3146 HeapFree(GetProcessHeap(), 0, bits);
3149 DeleteDC(dc);
3152 if (flags & LR_COPYDELETEORG)
3154 DeleteObject(hnd);
3157 HeapFree(GetProcessHeap(), 0, bi);
3158 return res;
3160 case IMAGE_ICON:
3161 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
3162 case IMAGE_CURSOR:
3163 /* Should call CURSORICON_ExtCopy but more testing
3164 * needs to be done before we change this
3166 if (flags) FIXME("Flags are ignored\n");
3167 return CopyCursor(hnd);
3169 return 0;
3173 /******************************************************************************
3174 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3176 * RETURNS
3177 * Success: Handle to specified bitmap
3178 * Failure: NULL
3180 HBITMAP WINAPI LoadBitmapW(
3181 HINSTANCE instance, /* [in] Handle to application instance */
3182 LPCWSTR name) /* [in] Address of bitmap resource name */
3184 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3187 /**********************************************************************
3188 * LoadBitmapA (USER32.@)
3190 * See LoadBitmapW.
3192 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3194 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );