push b0b97fcd59eff07f047585692fa36859b459324f
[wine/hacks.git] / dlls / user32 / cursoricon.c
blob2279087e552598ecedc68c9f320dac0a3d44d44d
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 * 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
31 * following layout:
33 * CURSORICONINFO info;
34 * BYTE[] ANDbits;
35 * BYTE[] XORbits;
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 :-(
45 #include "config.h"
46 #include "wine/port.h"
48 #include <stdarg.h>
49 #include <string.h>
50 #include <stdlib.h>
52 #include "ntstatus.h"
53 #define WIN32_NO_STATUS
54 #include "winternl.h"
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winerror.h"
59 #include "wine/winbase16.h"
60 #include "wine/winuser16.h"
61 #include "wine/exception.h"
62 #include "wine/debug.h"
63 #include "wine/list.h"
64 #include "wine/server.h"
65 #include "user_private.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
68 WINE_DECLARE_DEBUG_CHANNEL(icon);
69 WINE_DECLARE_DEBUG_CHANNEL(resource);
71 #include "pshpack1.h"
73 typedef struct {
74 BYTE bWidth;
75 BYTE bHeight;
76 BYTE bColorCount;
77 BYTE bReserved;
78 WORD xHotspot;
79 WORD yHotspot;
80 DWORD dwDIBSize;
81 DWORD dwDIBOffset;
82 } CURSORICONFILEDIRENTRY;
84 typedef struct
86 WORD idReserved;
87 WORD idType;
88 WORD idCount;
89 CURSORICONFILEDIRENTRY idEntries[1];
90 } CURSORICONFILEDIR;
92 #include "poppack.h"
94 #define CID_RESOURCE 0x0001
95 #define CID_WIN32 0x0004
96 #define CID_NONSHARED 0x0008
98 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
100 static HDC screen_dc;
102 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
104 /**********************************************************************
105 * ICONCACHE for cursors/icons loaded with LR_SHARED.
107 * FIXME: This should not be allocated on the system heap, but on a
108 * subsystem-global heap (i.e. one for all Win16 processes,
109 * and one for each Win32 process).
111 typedef struct tagICONCACHE
113 struct tagICONCACHE *next;
115 HMODULE hModule;
116 HRSRC hRsrc;
117 HRSRC hGroupRsrc;
118 HICON hIcon;
120 INT count;
122 } ICONCACHE;
124 static ICONCACHE *IconAnchor = NULL;
126 static CRITICAL_SECTION IconCrst;
127 static CRITICAL_SECTION_DEBUG critsect_debug =
129 0, 0, &IconCrst,
130 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
131 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
133 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
135 static const WORD ICON_HOTSPOT = 0x4242;
137 /* What is a good table size? */
138 #define CURSOR_HASH_SIZE 97
140 typedef struct {
141 HCURSOR16 cursor16;
142 HCURSOR cursor32;
143 struct list entry16;
144 struct list entry32;
145 } cursor_map_entry_t;
147 static int get_bitmap_width_bytes( int width, int bpp );
149 static struct list cursor16to32[CURSOR_HASH_SIZE];
150 static struct list cursor32to16[CURSOR_HASH_SIZE];
152 static inline int hash_cursor_handle( DWORD handle )
154 return handle % CURSOR_HASH_SIZE;
157 static void add_cursor16to32_entry( cursor_map_entry_t *entry )
159 int idx = hash_cursor_handle( entry->cursor16 );
161 if (!cursor16to32[idx].next) list_init( &cursor16to32[idx] );
163 list_add_head( &cursor16to32[idx], &entry->entry16 );
166 static void add_cursor32to16_entry( cursor_map_entry_t *entry )
168 int idx = hash_cursor_handle( (DWORD)entry->cursor32 );
170 if (!cursor32to16[idx].next) list_init( &cursor32to16[idx] );
172 list_add_head( &cursor32to16[idx], &entry->entry32 );
175 static cursor_map_entry_t *remove_cursor16to32_entry( HCURSOR16 cursor16 )
177 cursor_map_entry_t *entry = NULL;
178 int idx = hash_cursor_handle( cursor16 );
180 if (cursor16to32[idx].next)
182 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
183 if (entry->cursor16 == cursor16)
185 list_remove( &entry->entry16 );
186 return entry;
190 return entry;
193 static cursor_map_entry_t *remove_cursor32to16_entry( HCURSOR cursor32 )
195 cursor_map_entry_t *entry = NULL;
196 int idx = hash_cursor_handle( (DWORD)cursor32 );
198 if (cursor32to16[idx].next)
200 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
201 if (entry->cursor32 == cursor32)
203 list_remove( &entry->entry32 );
204 return entry;
208 return entry;
211 /* Ask the server for a cursor */
212 static HCURSOR create_cursor( unsigned int num_frames, unsigned int delay )
214 HCURSOR cursor = 0;
216 SERVER_START_REQ(create_cursor)
218 req->num_frames = num_frames;
219 req->delay = delay;
220 if (!wine_server_call_err( req )) cursor = reply->handle;
222 SERVER_END_REQ;
224 return cursor;
227 /* Tell the server to kill a cursor */
228 static HCURSOR16 destroy_cursor( HCURSOR cursor )
230 cursor_map_entry_t *entry;
231 HCURSOR16 cursor16 = 0;
233 if (!cursor) return 0;
235 SERVER_START_REQ(destroy_cursor)
237 req->handle = cursor;
238 wine_server_call( req );
240 SERVER_END_REQ;
242 entry = remove_cursor32to16_entry( cursor );
243 if (entry)
245 cursor16 = entry->cursor16;
246 remove_cursor16to32_entry( cursor16 );
247 HeapFree( GetProcessHeap(), 0, entry );
250 return GlobalFree16( cursor16 );
253 /* Upload a cursor frame to the server */
254 static void set_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
256 SERVER_START_REQ(set_cursor_frame)
258 req->handle = cursor;
259 req->frame_idx = frame_idx;
260 req->xhot = frame->xhot;
261 req->yhot = frame->yhot;
262 req->width = frame->width;
263 req->height = frame->height;
264 req->and_width_bytes = frame->and_width_bytes;
265 req->xor_width_bytes = frame->xor_width_bytes;
266 req->planes = frame->planes;
267 req->bpp = frame->bpp;
268 wine_server_add_data( req, frame->bits, (frame->and_width_bytes + frame->xor_width_bytes) * frame->height );
269 wine_server_call( req );
271 SERVER_END_REQ;
274 /* Download a cursor frame from the server */
275 static BOOL get_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
277 NTSTATUS res;
278 /* Enough for a 32-bits 32x32 cursor / icon. */
279 unsigned int buffer_size = 4224;
280 unsigned int count = 0;
284 frame->bits = HeapAlloc(GetProcessHeap(), 0, buffer_size);
285 SERVER_START_REQ(get_cursor_frame)
287 req->handle = cursor;
288 req->frame_idx = frame_idx;
289 wine_server_set_reply( req, frame->bits, buffer_size);
290 if (!(res = wine_server_call_err( req )))
292 frame->xhot = reply->xhot;
293 frame->yhot = reply->yhot;
294 frame->width = reply->width;
295 frame->height = reply->height;
296 frame->and_width_bytes = reply->and_width_bytes;
297 frame->xor_width_bytes = reply->xor_width_bytes;
298 frame->planes = reply->planes;
299 frame->bpp = reply->bpp;
300 } else {
301 HeapFree( GetProcessHeap(), 0, frame->bits );
302 buffer_size = (reply->and_width_bytes + reply->xor_width_bytes) * reply->height;
305 SERVER_END_REQ;
306 } while (res == STATUS_BUFFER_OVERFLOW && !count++);
308 if (!frame->height)
310 HeapFree( GetProcessHeap(), 0, frame->bits );
312 return FALSE;
315 return TRUE;
318 /* Retrieve a cursor and all its frames from the server */
319 static cursor_t *get_cursor_object( HCURSOR handle )
321 unsigned int i;
322 cursor_t *cursor = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(cursor_t) );
324 SERVER_START_REQ(get_cursor_info)
326 req->handle = handle;
327 if (!wine_server_call_err( req ))
329 cursor->num_frames = reply->num_frames;
330 cursor->delay = reply->delay;
333 SERVER_END_REQ;
335 if (!cursor->num_frames)
337 HeapFree( GetProcessHeap(), 0, cursor );
338 return NULL;
341 cursor->frames = HeapAlloc( GetProcessHeap(), 0, cursor->num_frames * sizeof(cursor_frame_t) );
342 for (i = 0; i < cursor->num_frames; ++i)
344 if (!get_cursor_frame( handle, i, &cursor->frames[i] ))
346 unsigned int j;
348 for (j = 0; j < i; ++j)
350 HeapFree( GetProcessHeap(), 0, cursor->frames[j].bits );
352 HeapFree( GetProcessHeap(), 0, cursor->frames );
353 HeapFree( GetProcessHeap(), 0, cursor );
355 return NULL;
359 return cursor;
362 static void destroy_cursor_object( cursor_t *cursor )
364 unsigned int i;
366 if (!cursor) return;
368 for (i = 0; i < cursor->num_frames; ++i)
370 HeapFree( GetProcessHeap(), 0, cursor->frames[i].bits );
372 HeapFree( GetProcessHeap(), 0, cursor->frames );
373 HeapFree( GetProcessHeap(), 0, cursor );
376 /* Lookup the cursor's 16-bit handle. Create one if it doesn't already exist. */
377 HCURSOR16 get_cursor_handle16( HCURSOR cursor32, BOOL create )
379 cursor_map_entry_t *entry;
380 int idx = hash_cursor_handle( (DWORD)cursor32 );
382 if (!cursor32) return 0;
384 if (cursor32to16[idx].next)
386 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
387 if (entry->cursor32 == cursor32) return entry->cursor16;
390 /* 16-bit cursor handle not found, create one */
391 if (create)
393 size_t bits_size;
394 HCURSOR16 cursor16;
395 cursor_frame_t frame;
397 if (!get_cursor_frame( cursor32, 0, &frame )) return 0;
399 entry = HeapAlloc( GetProcessHeap(), 0, sizeof(cursor_map_entry_t) );
400 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
401 cursor16 = GlobalAlloc16( GMEM_MOVEABLE, sizeof(CURSORICONINFO) + bits_size );
402 if (cursor16)
404 CURSORICONINFO *info;
406 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
407 info->ptHotSpot.x = frame.xhot;
408 info->ptHotSpot.y = frame.yhot;
409 info->nWidth = frame.width;
410 info->nHeight = frame.height;
411 info->nWidthBytes = frame.xor_width_bytes;
412 info->bPlanes = frame.planes;
413 info->bBitsPerPixel = frame.bpp;
414 CopyMemory( info + 1, frame.bits, bits_size );
415 GlobalUnlock16( cursor16 );
417 HeapFree( GetProcessHeap(), 0, frame.bits );
419 entry->cursor16 = cursor16;
420 entry->cursor32 = cursor32;
421 add_cursor16to32_entry( entry );
422 add_cursor32to16_entry( entry );
424 return cursor16;
427 return 0;
430 HCURSOR get_cursor_handle32( HCURSOR16 cursor16 )
432 cursor_map_entry_t *entry;
433 int idx = hash_cursor_handle( cursor16 );
435 if (!cursor16) return 0;
437 if (cursor16to32[idx].next)
439 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
440 if (entry->cursor16 == cursor16) return entry->cursor32;
443 return 0;
446 static void update_cursor_32from16( HCURSOR cursor32 )
448 size_t bits_size;
449 HCURSOR16 cursor16;
450 cursor_frame_t frame;
451 CURSORICONINFO *info;
453 if (!cursor32) return;
455 cursor16 = get_cursor_handle16( cursor32, FALSE );
456 if (!cursor16) return;
458 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
459 frame.xhot = info->ptHotSpot.x;
460 frame.yhot = info->ptHotSpot.y;
461 frame.width = info->nWidth;
462 frame.height = info->nHeight;
463 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
464 frame.xor_width_bytes = info->nWidthBytes;
465 frame.planes = info->bPlanes;
466 frame.bpp = info->bBitsPerPixel;
467 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
468 frame.bits = HeapAlloc( GetProcessHeap(), 0, bits_size );
469 CopyMemory( frame.bits, info + 1, bits_size );
470 GlobalUnlock16( cursor16 );
472 set_cursor_frame( cursor32, 0, &frame );
473 HeapFree( GetProcessHeap(), 0, frame.bits );
476 /***********************************************************************
477 * map_fileW
479 * Helper function to map a file to memory:
480 * name - file name
481 * [RETURN] ptr - pointer to mapped file
482 * [RETURN] filesize - pointer size of file to be stored if not NULL
484 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
486 HANDLE hFile, hMapping;
487 LPVOID ptr = NULL;
489 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
490 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
491 if (hFile != INVALID_HANDLE_VALUE)
493 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
494 if (hMapping)
496 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
497 CloseHandle( hMapping );
498 if (filesize)
499 *filesize = GetFileSize( hFile, NULL );
501 CloseHandle( hFile );
503 return ptr;
507 /***********************************************************************
508 * get_bitmap_width_bytes
510 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
511 * data.
513 static int get_bitmap_width_bytes( int width, int bpp )
515 switch(bpp)
517 case 1:
518 return 2 * ((width+15) / 16);
519 case 4:
520 return 2 * ((width+3) / 4);
521 case 24:
522 width *= 3;
523 /* fall through */
524 case 8:
525 return width + (width & 1);
526 case 16:
527 case 15:
528 return width * 2;
529 case 32:
530 return width * 4;
531 default:
532 WARN("Unknown depth %d, please report.\n", bpp );
534 return -1;
538 /***********************************************************************
539 * get_dib_width_bytes
541 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
543 static int get_dib_width_bytes( int width, int depth )
545 int words;
547 switch(depth)
549 case 1: words = (width + 31) / 32; break;
550 case 4: words = (width + 7) / 8; break;
551 case 8: words = (width + 3) / 4; break;
552 case 15:
553 case 16: words = (width + 1) / 2; break;
554 case 24: words = (width * 3 + 3)/4; break;
555 default:
556 WARN("(%d): Unsupported depth\n", depth );
557 /* fall through */
558 case 32:
559 words = width;
561 return 4 * words;
565 /***********************************************************************
566 * bitmap_info_size
568 * Return the size of the bitmap info structure including color table.
570 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
572 int colors, masks = 0;
574 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
576 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
577 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
578 return sizeof(BITMAPCOREHEADER) + colors *
579 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
581 else /* assume BITMAPINFOHEADER */
583 colors = info->bmiHeader.biClrUsed;
584 if (colors > 256) /* buffer overflow otherwise */
585 colors = 256;
586 if (!colors && (info->bmiHeader.biBitCount <= 8))
587 colors = 1 << info->bmiHeader.biBitCount;
588 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
589 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
590 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
595 /***********************************************************************
596 * is_dib_monochrome
598 * Returns whether a DIB can be converted to a monochrome DDB.
600 * A DIB can be converted if its color table contains only black and
601 * white. Black must be the first color in the color table.
603 * Note : If the first color in the color table is white followed by
604 * black, we can't convert it to a monochrome DDB with
605 * SetDIBits, because black and white would be inverted.
607 static BOOL is_dib_monochrome( const BITMAPINFO* info )
609 if (info->bmiHeader.biBitCount != 1) return FALSE;
611 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
613 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
615 /* Check if the first color is black */
616 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
618 rgb++;
620 /* Check if the second color is white */
621 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
622 && (rgb->rgbtBlue == 0xff));
624 else return FALSE;
626 else /* assume BITMAPINFOHEADER */
628 const RGBQUAD *rgb = info->bmiColors;
630 /* Check if the first color is black */
631 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
632 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
634 rgb++;
636 /* Check if the second color is white */
637 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
638 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
640 else return FALSE;
644 /***********************************************************************
645 * DIB_GetBitmapInfo
647 * Get the info from a bitmap header.
648 * Return 1 for INFOHEADER, 0 for COREHEADER,
649 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
651 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
652 LONG *height, WORD *bpp, DWORD *compr )
654 if (header->biSize == sizeof(BITMAPINFOHEADER))
656 *width = header->biWidth;
657 *height = header->biHeight;
658 *bpp = header->biBitCount;
659 *compr = header->biCompression;
660 return 1;
662 if (header->biSize == sizeof(BITMAPCOREHEADER))
664 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
665 *width = core->bcWidth;
666 *height = core->bcHeight;
667 *bpp = core->bcBitCount;
668 *compr = 0;
669 return 0;
671 if (header->biSize == sizeof(BITMAPV4HEADER))
673 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
674 *width = v4hdr->bV4Width;
675 *height = v4hdr->bV4Height;
676 *bpp = v4hdr->bV4BitCount;
677 *compr = v4hdr->bV4V4Compression;
678 return 4;
680 if (header->biSize == sizeof(BITMAPV5HEADER))
682 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
683 *width = v5hdr->bV5Width;
684 *height = v5hdr->bV5Height;
685 *bpp = v5hdr->bV5BitCount;
686 *compr = v5hdr->bV5Compression;
687 return 5;
689 ERR("(%d): unknown/wrong size for header\n", header->biSize );
690 return -1;
693 /**********************************************************************
694 * CURSORICON_FindSharedIcon
696 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
698 HICON hIcon = 0;
699 ICONCACHE *ptr;
701 EnterCriticalSection( &IconCrst );
703 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
704 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
706 ptr->count++;
707 hIcon = ptr->hIcon;
708 break;
711 LeaveCriticalSection( &IconCrst );
713 return hIcon;
716 /*************************************************************************
717 * CURSORICON_FindCache
719 * Given a handle, find the corresponding cache element
721 * PARAMS
722 * Handle [I] handle to an Image
724 * RETURNS
725 * Success: The cache entry
726 * Failure: NULL
729 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
731 ICONCACHE *ptr;
732 ICONCACHE *pRet=NULL;
733 BOOL IsFound = FALSE;
735 EnterCriticalSection( &IconCrst );
737 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
739 if ( hIcon == ptr->hIcon )
741 IsFound = TRUE;
742 pRet = ptr;
746 LeaveCriticalSection( &IconCrst );
748 return pRet;
751 /**********************************************************************
752 * CURSORICON_AddSharedIcon
754 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
756 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
757 if ( !ptr ) return;
759 ptr->hModule = hModule;
760 ptr->hRsrc = hRsrc;
761 ptr->hIcon = hIcon;
762 ptr->hGroupRsrc = hGroupRsrc;
763 ptr->count = 1;
765 EnterCriticalSection( &IconCrst );
766 ptr->next = IconAnchor;
767 IconAnchor = ptr;
768 LeaveCriticalSection( &IconCrst );
771 /**********************************************************************
772 * CURSORICON_DelSharedIcon
774 static INT CURSORICON_DelSharedIcon( HICON hIcon )
776 INT count = -1;
777 ICONCACHE *ptr;
779 EnterCriticalSection( &IconCrst );
781 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
782 if ( ptr->hIcon == hIcon )
784 if ( ptr->count > 0 ) ptr->count--;
785 count = ptr->count;
786 break;
789 LeaveCriticalSection( &IconCrst );
791 return count;
794 /**********************************************************************
795 * CURSORICON_FreeModuleIcons
797 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
799 ICONCACHE **ptr = &IconAnchor;
800 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
802 EnterCriticalSection( &IconCrst );
804 while ( *ptr )
806 if ( (*ptr)->hModule == hModule )
808 ICONCACHE *freePtr = *ptr;
809 *ptr = freePtr->next;
811 destroy_cursor( freePtr->hIcon );
812 HeapFree( GetProcessHeap(), 0, freePtr );
813 continue;
815 ptr = &(*ptr)->next;
818 LeaveCriticalSection( &IconCrst );
822 * The following macro functions account for the irregularities of
823 * accessing cursor and icon resources in files and resource entries.
825 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
826 int *width, int *height, int *bits );
828 /**********************************************************************
829 * CURSORICON_FindBestIcon
831 * Find the icon closest to the requested size and number of colors.
833 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
834 int width, int height, int colors )
836 int i, cx, cy, bits, bestEntry = -1;
837 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
838 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
840 /* Find Best Fit */
841 iTotalDiff = 0xFFFFFFFF;
842 iColorDiff = 0xFFFFFFFF;
843 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
845 iTempXDiff = abs(width - cx);
846 iTempYDiff = abs(height - cy);
848 if(iTotalDiff > (iTempXDiff + iTempYDiff))
850 iXDiff = iTempXDiff;
851 iYDiff = iTempYDiff;
852 iTotalDiff = iXDiff + iYDiff;
856 /* Find Best Colors for Best Fit */
857 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
859 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
861 iTempColorDiff = abs(colors - (1<<bits));
862 if(iColorDiff > iTempColorDiff)
864 bestEntry = i;
865 iColorDiff = iTempColorDiff;
870 return bestEntry;
873 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
874 int *width, int *height, int *bits )
876 CURSORICONDIR *resdir = dir;
877 ICONRESDIR *icon;
879 if ( resdir->idCount <= n )
880 return FALSE;
881 icon = &resdir->idEntries[n].ResInfo.icon;
882 *width = icon->bWidth;
883 *height = icon->bHeight;
884 *bits = resdir->idEntries[n].wBitCount;
885 return TRUE;
888 /**********************************************************************
889 * CURSORICON_FindBestCursor
891 * Find the cursor closest to the requested size.
892 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
893 * ignored too
895 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
896 int width, int height, int color )
898 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
900 /* Double height to account for AND and XOR masks */
902 height *= 2;
904 /* First find the largest one smaller than or equal to the requested size*/
906 maxwidth = maxheight = 0;
907 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
909 if ((cx <= width) && (cy <= height) &&
910 (cx > maxwidth) && (cy > maxheight) &&
911 (bits == 1))
913 bestEntry = i;
914 maxwidth = cx;
915 maxheight = cy;
918 if (bestEntry != -1) return bestEntry;
920 /* Now find the smallest one larger than the requested size */
922 maxwidth = maxheight = 255;
923 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
925 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
926 (bestEntry==-1))
928 bestEntry = i;
929 maxwidth = cx;
930 maxheight = cy;
934 return bestEntry;
937 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
938 int *width, int *height, int *bits )
940 CURSORICONDIR *resdir = dir;
941 CURSORDIR *cursor;
943 if ( resdir->idCount <= n )
944 return FALSE;
945 cursor = &resdir->idEntries[n].ResInfo.cursor;
946 *width = cursor->wWidth;
947 *height = cursor->wHeight;
948 *bits = resdir->idEntries[n].wBitCount;
949 return TRUE;
952 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
953 int width, int height, int colors )
955 int n;
957 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
958 width, height, colors );
959 if ( n < 0 )
960 return NULL;
961 return &dir->idEntries[n];
964 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
965 int width, int height, int color )
967 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
968 width, height, color );
969 if ( n < 0 )
970 return NULL;
971 return &dir->idEntries[n];
974 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
975 int *width, int *height, int *bits )
977 CURSORICONFILEDIR *filedir = dir;
978 CURSORICONFILEDIRENTRY *entry;
980 if ( filedir->idCount <= n )
981 return FALSE;
982 entry = &filedir->idEntries[n];
983 *width = entry->bWidth;
984 *height = entry->bHeight;
985 *bits = entry->bColorCount;
986 return TRUE;
989 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
990 int width, int height, int color )
992 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
993 width, height, color );
994 if ( n < 0 )
995 return NULL;
996 return &dir->idEntries[n];
999 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
1000 int width, int height, int color )
1002 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
1003 width, height, color );
1004 if ( n < 0 )
1005 return NULL;
1006 return &dir->idEntries[n];
1009 static BOOL load_cursor_frame( BITMAPINFO *bmi,
1010 POINT16 hotspot, BOOL bIcon,
1011 DWORD dwVersion,
1012 INT width, INT height,
1013 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 BOOL DoStretch;
1020 INT size;
1022 if (dwVersion == 0x00020000)
1024 FIXME_(cursor)("\t2.xx resources are not supported\n");
1025 return FALSE;
1028 /* Check bitmap header */
1030 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
1031 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
1032 bmi->bmiHeader.biCompression != BI_RGB) )
1034 WARN_(cursor)("\tinvalid resource bitmap header.\n");
1035 return FALSE;
1038 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
1040 if (!width) width = bmi->bmiHeader.biWidth;
1041 if (!height) height = bmi->bmiHeader.biHeight/2;
1042 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
1043 (bmi->bmiHeader.biWidth != width);
1045 /* Scale the hotspot */
1046 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
1048 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
1049 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biWidth / 2);
1052 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1053 if (screen_dc)
1055 BITMAPINFO* pInfo;
1057 /* Make sure we have room for the monochrome bitmap later on.
1058 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1059 * up to and including the biBitCount. In-memory icon resource
1060 * format is as follows:
1062 * BITMAPINFOHEADER icHeader // DIB header
1063 * RGBQUAD icColors[] // Color table
1064 * BYTE icXOR[] // DIB bits for XOR mask
1065 * BYTE icAND[] // DIB bits for AND mask
1068 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
1069 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
1071 memcpy( pInfo, bmi, size );
1072 pInfo->bmiHeader.biHeight /= 2;
1074 /* Create the XOR bitmap */
1076 if (DoStretch) {
1077 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1078 if(hXorBits)
1080 HBITMAP hOld;
1081 BOOL res = FALSE;
1083 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1084 if (hdcMem) {
1085 hOld = SelectObject(hdcMem, hXorBits);
1086 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1087 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
1088 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
1089 SelectObject(hdcMem, hOld);
1091 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
1093 } else {
1094 if (is_dib_monochrome(bmi)) {
1095 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1096 SetDIBits(screen_dc, hXorBits, 0, height,
1097 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1098 } else if (bmi->bmiHeader.biBitCount == 32) {
1099 hXorBits = CreateDIBSection(screen_dc, pInfo, DIB_RGB_COLORS, NULL, NULL, 0);
1100 SetDIBits(screen_dc, hXorBits, 0, height,
1101 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1103 else
1104 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
1105 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1108 if( hXorBits )
1110 char* xbits = (char *)bmi + size +
1111 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1112 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1114 pInfo->bmiHeader.biBitCount = 1;
1115 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1117 RGBQUAD *rgb = pInfo->bmiColors;
1119 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
1120 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1121 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1122 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1124 else
1126 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
1128 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1129 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1132 /* Create the AND bitmap */
1134 if (DoStretch) {
1135 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
1136 HBITMAP hOld;
1137 BOOL res = FALSE;
1139 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1140 if (hdcMem) {
1141 hOld = SelectObject(hdcMem, hAndBits);
1142 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1143 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1144 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1145 SelectObject(hdcMem, hOld);
1147 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
1149 } else {
1150 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1152 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
1153 xbits, pInfo, DIB_RGB_COLORS);
1156 if( !hAndBits ) DeleteObject( hXorBits );
1158 HeapFree( GetProcessHeap(), 0, pInfo );
1162 if( !hXorBits || !hAndBits )
1164 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1165 return FALSE;
1168 /* Setup a cursor frame, send it to the server */
1169 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1170 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1171 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1172 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1174 frame->xhot = hotspot.x;
1175 frame->yhot = hotspot.y;
1176 frame->width = bmpXor.bmWidth;
1177 frame->height = bmpXor.bmHeight;
1178 frame->and_width_bytes = bmpAnd.bmWidthBytes;
1179 frame->xor_width_bytes = bmpXor.bmWidthBytes;
1180 frame->planes = bmpXor.bmPlanes;
1181 frame->bpp = bmpXor.bmBitsPixel;
1182 frame->bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1183 GetBitmapBits( hAndBits, sizeAnd, frame->bits );
1184 GetBitmapBits( hXorBits, sizeXor, frame->bits + sizeAnd );
1186 DeleteObject( hAndBits );
1187 DeleteObject( hXorBits );
1189 return TRUE;
1193 /**********************************************************************
1194 * .ANI cursor support
1196 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1197 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1198 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1200 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1201 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1202 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1203 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1204 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1205 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1207 #define ANI_FLAG_ICON 0x1
1208 #define ANI_FLAG_SEQUENCE 0x2
1210 typedef struct {
1211 DWORD header_size;
1212 DWORD num_frames;
1213 DWORD num_steps;
1214 DWORD width;
1215 DWORD height;
1216 DWORD bpp;
1217 DWORD num_planes;
1218 DWORD display_rate;
1219 DWORD flags;
1220 } ani_header;
1222 typedef struct {
1223 DWORD data_size;
1224 const unsigned char *data;
1225 } riff_chunk_t;
1227 static void dump_ani_header( const ani_header *header )
1229 TRACE(" header size: %d\n", header->header_size);
1230 TRACE(" frames: %d\n", header->num_frames);
1231 TRACE(" steps: %d\n", header->num_steps);
1232 TRACE(" width: %d\n", header->width);
1233 TRACE(" height: %d\n", header->height);
1234 TRACE(" bpp: %d\n", header->bpp);
1235 TRACE(" planes: %d\n", header->num_planes);
1236 TRACE(" display rate: %d\n", header->display_rate);
1237 TRACE(" flags: 0x%08x\n", header->flags);
1242 * RIFF:
1243 * DWORD "RIFF"
1244 * DWORD size
1245 * DWORD riff_id
1246 * BYTE[] data
1248 * LIST:
1249 * DWORD "LIST"
1250 * DWORD size
1251 * DWORD list_id
1252 * BYTE[] data
1254 * CHUNK:
1255 * DWORD chunk_id
1256 * DWORD size
1257 * BYTE[] data
1259 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1261 const unsigned char *ptr = parent_chunk->data;
1262 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1264 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1266 while (ptr < end)
1268 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
1269 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
1271 ptr += sizeof(DWORD);
1272 chunk->data_size = *(DWORD *)ptr;
1273 ptr += sizeof(DWORD);
1274 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1275 chunk->data = ptr;
1277 return;
1280 ptr += sizeof(DWORD);
1281 ptr += *(DWORD *)ptr;
1282 ptr += sizeof(DWORD);
1288 * .ANI layout:
1290 * RIFF:'ACON' RIFF chunk
1291 * |- CHUNK:'anih' Header
1292 * |- CHUNK:'seq ' Sequence information (optional)
1293 * \- LIST:'fram' Frame list
1294 * |- CHUNK:icon Cursor frames
1295 * |- CHUNK:icon
1296 * |- ...
1297 * \- CHUNK:icon
1299 static HCURSOR load_ani( const LPBYTE bits, DWORD bits_size, INT width, INT height )
1301 int i;
1302 WORD max_count = 0;
1303 HCURSOR cursor;
1304 CURSORICONFILEDIR *dir = 0;
1305 ani_header header = {0};
1306 DWORD *frame_seq = 0;
1307 cursor_frame_t *frames;
1308 unsigned int frame_bits_size = 0;
1309 LPBYTE frame_bits = 0;
1310 POINT16 hotspot;
1312 riff_chunk_t root_chunk = { bits_size, bits };
1313 riff_chunk_t ACON_chunk = {0};
1314 riff_chunk_t anih_chunk = {0};
1315 riff_chunk_t fram_chunk = {0};
1316 const unsigned char *icon_chunk;
1317 const unsigned char *icon_data;
1319 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1321 if (!bits) return 0;
1323 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1324 if (!ACON_chunk.data)
1326 ERR("Failed to get root chunk.\n");
1327 return 0;
1330 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1331 if (!anih_chunk.data)
1333 ERR("Failed to get 'anih' chunk.\n");
1334 return 0;
1336 memcpy( &header, anih_chunk.data, sizeof(header) );
1337 dump_ani_header( &header );
1339 if (header.flags & ANI_FLAG_SEQUENCE)
1341 riff_chunk_t seq_chunk = {0};
1343 TRACE("Loading sequence data.\n");
1344 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1345 if (!seq_chunk.data)
1347 ERR("Failed to get 'seq ' chunk\n");
1348 return 0;
1350 frame_seq = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) * header.num_steps );
1351 memcpy( frame_seq, seq_chunk.data, sizeof(DWORD) * header.num_steps );
1354 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1355 if (!fram_chunk.data)
1357 ERR("Failed to get icon list\n");
1358 return 0;
1361 icon_chunk = fram_chunk.data;
1362 icon_data = icon_chunk + (2 * sizeof(DWORD));
1363 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1364 cursor = create_cursor( header.num_steps, (100 * header.display_rate) / 6 );
1365 frames = HeapAlloc( GetProcessHeap(), 0, header.num_frames * sizeof(cursor_frame_t) );
1367 for (i = 0; i < header.num_frames; ++i)
1369 WORD count;
1370 CURSORICONFILEDIRENTRY *entry;
1371 DWORD chunk_size = *(DWORD *)(icon_chunk + sizeof(DWORD));
1373 /* Read icon count, skip magic */
1374 memcpy( &count, icon_data + sizeof(DWORD), sizeof(WORD) );
1376 /* There's a decent chance the amount of entries will be the same for each icon */
1377 if (count > max_count)
1379 HeapFree( GetProcessHeap(), 0, dir );
1380 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1381 dir = HeapAlloc( GetProcessHeap(), 0, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1382 max_count = count;
1385 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1386 memcpy( dir, icon_data, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1387 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
1389 if (frame_bits_size < entry->dwDIBSize)
1391 frame_bits_size = entry->dwDIBSize;
1392 HeapFree( GetProcessHeap(), 0, frame_bits );
1393 frame_bits = HeapAlloc( GetProcessHeap(), 0, frame_bits_size );
1396 if (!header.width || !header.height)
1398 header.width = entry->bWidth;
1399 header.height = entry->bHeight;
1402 hotspot.x = entry->xHotspot;
1403 hotspot.y = entry->yHotspot;
1405 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1407 load_cursor_frame( frame_bits, entry->dwDIBSize, hotspot, 0x00030000, header.width, header.height, 0, &frames[i] );
1409 /* Advance to the next chunk */
1410 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1411 icon_data = icon_chunk + (2 * sizeof(DWORD));
1413 HeapFree( GetProcessHeap(), 0, dir );
1415 /* Set the frames in the correct sequence */
1416 for (i = 0; i < header.num_steps; ++i)
1418 int frame_idx = (frame_seq ? frame_seq[i] : i);
1419 set_cursor_frame( cursor, i, &frames[frame_idx] );
1422 /* Cleanup */
1423 for (i = 0; i < header.num_frames; ++i)
1425 HeapFree( GetProcessHeap(), 0, frames[i].bits );
1427 HeapFree( GetProcessHeap(), 0, frame_seq );
1428 HeapFree( GetProcessHeap(), 0, frames );
1430 return cursor;
1434 /**********************************************************************
1435 * CreateIconFromResourceEx (USER32.@)
1437 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1438 * with cbSize parameter as well.
1440 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1441 BOOL bIcon, DWORD dwVersion,
1442 INT width, INT height,
1443 UINT cFlag )
1445 POINT16 hotspot;
1446 BITMAPINFO *bmi;
1447 HCURSOR cursor = create_cursor( 1, 0 );
1448 cursor_frame_t frame = {0};
1450 hotspot.x = ICON_HOTSPOT;
1451 hotspot.y = ICON_HOTSPOT;
1453 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1454 bits, cbSize, dwVersion, width, height,
1455 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1457 if (bIcon)
1458 bmi = (BITMAPINFO *)bits;
1459 else /* get the hotspot */
1461 POINT16 *pt = (POINT16 *)bits;
1462 hotspot = *pt;
1463 bmi = (BITMAPINFO *)(pt + 1);
1466 if (load_cursor_frame( bmi, 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 LPBYTE bits;
1501 POINT16 hotspot;
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 /* Check for .ani. */
1515 else if (!memcmp( bits, "RIFF", 4 ))
1517 hIcon = load_ani( bits, filesize, width, height );
1518 goto end;
1521 dir = (CURSORICONFILEDIR*) bits;
1522 if ( filesize < sizeof(*dir) )
1523 goto end;
1525 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1526 goto end;
1528 if ( fCursor )
1529 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1530 else
1531 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1533 if ( !entry )
1534 goto end;
1536 /* check that we don't run off the end of the file */
1537 if ( entry->dwDIBOffset > filesize )
1538 goto end;
1539 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1540 goto end;
1542 if ( fCursor )
1544 hotspot.x = entry->xHotspot;
1545 hotspot.y = entry->yHotspot;
1547 else
1549 hotspot.x = ICON_HOTSPOT;
1550 hotspot.y = ICON_HOTSPOT;
1552 hIcon = create_cursor( 1, 0 );
1553 if (load_cursor_frame( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1554 hotspot, 0x00030000,
1555 width, height, loadflags,
1556 &frame ))
1557 set_cursor_frame( hIcon, 0, &frame );
1559 else
1561 destroy_cursor( hIcon );
1562 hIcon = 0;
1564 HeapFree( GetProcessHeap(), 0, frame.bits );
1566 end:
1567 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1568 UnmapViewOfFile( bits );
1569 return hIcon;
1572 /**********************************************************************
1573 * CURSORICON_Load
1575 * Load a cursor or icon from resource or file.
1577 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1578 INT width, INT height, INT colors,
1579 BOOL fCursor, UINT loadflags)
1581 HANDLE handle = 0;
1582 HICON hIcon = 0;
1583 HRSRC hRsrc, hGroupRsrc;
1584 CURSORICONDIR *dir;
1585 CURSORICONDIRENTRY *dirEntry;
1586 LPBYTE bits;
1587 WORD wResId;
1588 DWORD dwBytesInRes;
1590 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1591 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1593 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1594 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1596 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1598 /* Normalize hInstance (must be uniquely represented for icon cache) */
1600 if (!HIWORD( hInstance ))
1601 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1603 /* Get directory resource ID */
1605 if (!(hRsrc = FindResourceW( hInstance, name,
1606 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1607 return 0;
1608 hGroupRsrc = hRsrc;
1610 /* Find the best entry in the directory */
1612 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1613 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
1614 if (fCursor)
1615 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1616 else
1617 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1618 if (!dirEntry) return 0;
1619 wResId = dirEntry->wResId;
1620 dwBytesInRes = dirEntry->dwBytesInRes;
1621 FreeResource( handle );
1623 /* Load the resource */
1625 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1626 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1628 /* If shared icon, check whether it was already loaded */
1629 if ( (loadflags & LR_SHARED)
1630 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1631 return hIcon;
1633 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1634 bits = (LPBYTE)LockResource( handle );
1635 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1636 !fCursor, 0x00030000, width, height, loadflags);
1637 FreeResource( handle );
1639 /* If shared icon, add to icon cache */
1641 if ( hIcon && (loadflags & LR_SHARED) )
1642 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1644 return hIcon;
1647 /***********************************************************************
1648 * CURSORICON_Copy
1650 * Make a copy of a cursor or icon.
1652 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1654 /* Should animated cursors be copyable like this as well? */
1655 HCURSOR new_cursor;
1656 cursor_frame_t frame;
1658 if (!hIcon || !get_cursor_frame( hIcon, 0, &frame ))
1660 return 0;
1663 new_cursor = create_cursor( 1, 0 );
1664 set_cursor_frame( new_cursor, 0, &frame );
1665 HeapFree( GetProcessHeap(), 0, frame.bits );
1667 return new_cursor;
1670 /*************************************************************************
1671 * CURSORICON_ExtCopy
1673 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1675 * PARAMS
1676 * Handle [I] handle to an Image
1677 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1678 * iDesiredCX [I] The Desired width of the Image
1679 * iDesiredCY [I] The desired height of the Image
1680 * nFlags [I] The flags from CopyImage
1682 * RETURNS
1683 * Success: The new handle of the Image
1685 * NOTES
1686 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1687 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1688 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1693 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1694 INT iDesiredCX, INT iDesiredCY,
1695 UINT nFlags)
1697 HICON hNew=0;
1699 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1700 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1702 if(hIcon == 0)
1704 return 0;
1707 /* Best Fit or Monochrome */
1708 if( (nFlags & LR_COPYFROMRESOURCE
1709 && (iDesiredCX > 0 || iDesiredCY > 0))
1710 || nFlags & LR_MONOCHROME)
1712 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1714 /* Not Found in Cache, then do a straight copy
1716 if(pIconCache == NULL)
1718 hNew = CURSORICON_Copy(0, hIcon);
1719 if(nFlags & LR_COPYFROMRESOURCE)
1721 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1724 else
1726 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1727 LPBYTE pBits;
1728 HANDLE hMem;
1729 HRSRC hRsrc;
1730 DWORD dwBytesInRes;
1731 WORD wResId;
1732 CURSORICONDIR *pDir;
1733 CURSORICONDIRENTRY *pDirEntry;
1734 BOOL bIsIcon = (nType == IMAGE_ICON);
1736 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1738 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1739 || (iDesiredCX == 0 && iDesiredCY == 0))
1741 iDesiredCY = GetSystemMetrics(bIsIcon ?
1742 SM_CYICON : SM_CYCURSOR);
1743 iDesiredCX = GetSystemMetrics(bIsIcon ?
1744 SM_CXICON : SM_CXCURSOR);
1747 /* Retrieve the CURSORICONDIRENTRY
1749 if (!(hMem = LoadResource( pIconCache->hModule ,
1750 pIconCache->hGroupRsrc)))
1752 return 0;
1754 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1756 return 0;
1759 /* Find Best Fit
1761 if(bIsIcon)
1763 pDirEntry = CURSORICON_FindBestIconRes(
1764 pDir, iDesiredCX, iDesiredCY, 256 );
1766 else
1768 pDirEntry = CURSORICON_FindBestCursorRes(
1769 pDir, iDesiredCX, iDesiredCY, 1);
1772 wResId = pDirEntry->wResId;
1773 dwBytesInRes = pDirEntry->dwBytesInRes;
1774 FreeResource(hMem);
1776 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1777 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1778 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1780 /* Get the Best Fit
1782 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1783 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1785 return 0;
1787 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1789 return 0;
1792 pBits = (LPBYTE)LockResource( hMem );
1794 if(nFlags & LR_DEFAULTSIZE)
1796 iTargetCY = GetSystemMetrics(SM_CYICON);
1797 iTargetCX = GetSystemMetrics(SM_CXICON);
1800 /* Create a New Icon with the proper dimension
1802 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1803 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1804 FreeResource(hMem);
1807 else hNew = CURSORICON_Copy(0, hIcon);
1808 return hNew;
1812 /***********************************************************************
1813 * CreateCursor (USER32.@)
1815 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1816 INT xHotSpot, INT yHotSpot,
1817 INT nWidth, INT nHeight,
1818 LPCVOID lpANDbits, LPCVOID lpXORbits )
1820 CURSORICONINFO info;
1822 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1823 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1825 info.ptHotSpot.x = xHotSpot;
1826 info.ptHotSpot.y = yHotSpot;
1827 info.nWidth = nWidth;
1828 info.nHeight = nHeight;
1829 info.nWidthBytes = 0;
1830 info.bPlanes = 1;
1831 info.bBitsPerPixel = 1;
1833 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1837 /***********************************************************************
1838 * CreateIcon (USER.407)
1840 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1841 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1842 LPCVOID lpANDbits, LPCVOID lpXORbits )
1844 CURSORICONINFO info;
1846 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1847 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1849 info.ptHotSpot.x = ICON_HOTSPOT;
1850 info.ptHotSpot.y = ICON_HOTSPOT;
1851 info.nWidth = nWidth;
1852 info.nHeight = nHeight;
1853 info.nWidthBytes = 0;
1854 info.bPlanes = bPlanes;
1855 info.bBitsPerPixel = bBitsPixel;
1857 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1861 /***********************************************************************
1862 * CreateIcon (USER32.@)
1864 * Creates an icon based on the specified bitmaps. The bitmaps must be
1865 * provided in a device dependent format and will be resized to
1866 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1867 * depth. The provided bitmaps must be top-down bitmaps.
1868 * Although Windows does not support 15bpp(*) this API must support it
1869 * for Winelib applications.
1871 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1872 * format!
1874 * RETURNS
1875 * Success: handle to an icon
1876 * Failure: NULL
1878 * FIXME: Do we need to resize the bitmaps?
1880 HICON WINAPI CreateIcon(
1881 HINSTANCE hInstance, /* [in] the application's hInstance */
1882 INT nWidth, /* [in] the width of the provided bitmaps */
1883 INT nHeight, /* [in] the height of the provided bitmaps */
1884 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1885 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1886 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1887 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1889 ICONINFO iinfo;
1890 HICON hIcon;
1892 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1893 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1895 iinfo.fIcon = TRUE;
1896 iinfo.xHotspot = ICON_HOTSPOT;
1897 iinfo.yHotspot = ICON_HOTSPOT;
1898 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1899 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1901 hIcon = CreateIconIndirect( &iinfo );
1903 DeleteObject( iinfo.hbmMask );
1904 DeleteObject( iinfo.hbmColor );
1906 return hIcon;
1910 /***********************************************************************
1911 * CreateCursorIconIndirect (USER.408)
1913 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1914 CURSORICONINFO *info,
1915 LPCVOID lpANDbits,
1916 LPCVOID lpXORbits )
1918 HCURSOR cursor;
1919 cursor_frame_t frame;
1920 int sizeAnd, sizeXor;
1922 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1923 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1924 sizeXor = info->nHeight * info->nWidthBytes;
1925 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1927 cursor = create_cursor( 1, 0 );
1928 frame.xhot = info->ptHotSpot.x;
1929 frame.yhot = info->ptHotSpot.y;
1930 frame.width = info->nWidth;
1931 frame.height = info->nHeight;
1932 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
1933 frame.xor_width_bytes = info->nWidthBytes;
1934 frame.planes = info->bPlanes;
1935 frame.bpp = info->bBitsPerPixel;
1936 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1937 CopyMemory( frame.bits, lpANDbits, sizeAnd );
1938 CopyMemory( frame.bits + sizeAnd, lpXORbits, sizeXor );
1939 set_cursor_frame( cursor, 0, &frame );
1940 HeapFree( GetProcessHeap(), 0, frame.bits );
1942 return HICON_16(cursor);
1946 /***********************************************************************
1947 * CopyIcon (USER.368)
1949 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1951 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1952 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1956 /***********************************************************************
1957 * CopyIcon (USER32.@)
1959 HICON WINAPI CopyIcon( HICON hIcon )
1961 TRACE_(icon)("%p\n", hIcon );
1962 return CURSORICON_Copy( 0, hIcon );
1966 /***********************************************************************
1967 * CopyCursor (USER.369)
1969 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1971 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1972 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1975 /**********************************************************************
1976 * DestroyIcon32 (USER.610)
1978 * This routine is actually exported from Win95 USER under the name
1979 * DestroyIcon32 ... The behaviour implemented here should mimic
1980 * the Win95 one exactly, especially the return values, which
1981 * depend on the setting of various flags.
1983 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1985 WORD retv;
1987 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1989 /* Check whether destroying active cursor */
1991 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1993 WARN_(cursor)("Destroying active cursor!\n" );
1994 return FALSE;
1997 /* Try shared cursor/icon first */
1999 if ( !(flags & CID_NONSHARED) )
2001 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
2003 if ( count != -1 )
2004 return (flags & CID_WIN32)? TRUE : (count == 0);
2006 /* FIXME: OEM cursors/icons should be recognized */
2009 /* Now assume non-shared cursor/icon */
2011 retv = destroy_cursor( HCURSOR_32(handle) );
2012 return (flags & CID_RESOURCE)? retv : TRUE;
2015 /***********************************************************************
2016 * DestroyIcon (USER32.@)
2018 BOOL WINAPI DestroyIcon( HICON hIcon )
2020 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
2024 /***********************************************************************
2025 * DestroyCursor (USER32.@)
2027 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
2029 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
2033 /***********************************************************************
2034 * DrawIcon (USER32.@)
2036 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
2038 CURSORICONINFO *ptr;
2039 HDC hMemDC;
2040 HBITMAP hXorBits, hAndBits;
2041 COLORREF oldFg, oldBg;
2043 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
2045 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
2046 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2047 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
2048 (char *)(ptr+1) );
2049 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
2050 ptr->bBitsPerPixel, (char *)(ptr + 1)
2051 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
2052 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2053 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2055 if (hXorBits && hAndBits)
2057 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2058 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
2059 SelectObject( hMemDC, hXorBits );
2060 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
2061 SelectObject( hMemDC, hBitTemp );
2063 DeleteDC( hMemDC );
2064 if (hXorBits) DeleteObject( hXorBits );
2065 if (hAndBits) DeleteObject( hAndBits );
2066 GlobalUnlock16(HICON_16(hIcon));
2067 SetTextColor( hdc, oldFg );
2068 SetBkColor( hdc, oldBg );
2069 return TRUE;
2072 /***********************************************************************
2073 * DumpIcon (USER.459)
2075 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
2076 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
2078 CURSORICONINFO *info = MapSL( pInfo );
2079 int sizeAnd, sizeXor;
2081 if (!info) return 0;
2082 sizeXor = info->nHeight * info->nWidthBytes;
2083 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
2084 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
2085 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
2086 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
2087 return MAKELONG( sizeXor, sizeXor );
2091 /***********************************************************************
2092 * SetCursor (USER32.@)
2094 * Set the cursor shape.
2096 * RETURNS
2097 * A handle to the previous cursor shape.
2099 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
2101 struct user_thread_info *thread_info = get_user_thread_info();
2102 HCURSOR hOldCursor;
2104 if (hCursor == thread_info->cursor) return hCursor; /* No change */
2105 TRACE("%p\n", hCursor);
2106 hOldCursor = thread_info->cursor;
2107 thread_info->cursor = hCursor;
2108 /* Change the cursor shape only if it is visible */
2109 if (thread_info->cursor_count >= 0)
2111 cursor_t *cursor;
2113 update_cursor_32from16( hCursor );
2114 cursor = get_cursor_object( hCursor );
2115 USER_Driver->pSetCursor( cursor );
2116 destroy_cursor_object( cursor );
2118 return hOldCursor;
2121 /***********************************************************************
2122 * ShowCursor (USER32.@)
2124 INT WINAPI ShowCursor( BOOL bShow )
2126 struct user_thread_info *thread_info = get_user_thread_info();
2128 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
2130 if (bShow)
2132 if (++thread_info->cursor_count == 0) /* Show it */
2134 cursor_t *cursor;
2136 update_cursor_32from16( thread_info->cursor );
2137 cursor = get_cursor_object( thread_info->cursor );
2138 USER_Driver->pSetCursor( cursor );
2139 destroy_cursor_object( cursor );
2142 else
2144 if (--thread_info->cursor_count == -1) /* Hide it */
2145 USER_Driver->pSetCursor( NULL );
2147 return thread_info->cursor_count;
2150 /***********************************************************************
2151 * GetCursor (USER32.@)
2153 HCURSOR WINAPI GetCursor(void)
2155 return get_user_thread_info()->cursor;
2159 /***********************************************************************
2160 * ClipCursor (USER32.@)
2162 BOOL WINAPI ClipCursor( const RECT *rect )
2164 RECT virt;
2166 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
2167 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
2168 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
2169 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
2171 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
2172 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
2174 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
2175 CURSOR_ClipRect = virt;
2177 USER_Driver->pClipCursor( rect );
2178 return TRUE;
2182 /***********************************************************************
2183 * GetClipCursor (USER32.@)
2185 BOOL WINAPI GetClipCursor( RECT *rect )
2187 /* If this is first time - initialize the rect */
2188 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
2190 return CopyRect( rect, &CURSOR_ClipRect );
2194 /***********************************************************************
2195 * SetSystemCursor (USER32.@)
2197 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
2199 FIXME("(%p,%08x),stub!\n", hcur, id);
2200 return TRUE;
2204 /**********************************************************************
2205 * LookupIconIdFromDirectoryEx (USER.364)
2207 * FIXME: exact parameter sizes
2209 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
2210 INT16 width, INT16 height, UINT16 cFlag )
2212 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
2215 /**********************************************************************
2216 * LookupIconIdFromDirectoryEx (USER32.@)
2218 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
2219 INT width, INT height, UINT cFlag )
2221 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
2222 UINT retVal = 0;
2223 if( dir && !dir->idReserved && (dir->idType & 3) )
2225 CURSORICONDIRENTRY* entry;
2226 HDC hdc;
2227 UINT palEnts;
2228 int colors;
2229 hdc = GetDC(0);
2230 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
2231 if (palEnts == 0)
2232 palEnts = 256;
2233 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
2235 ReleaseDC(0, hdc);
2237 if( bIcon )
2238 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
2239 else
2240 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
2242 if( entry ) retVal = entry->wResId;
2244 else WARN_(cursor)("invalid resource directory\n");
2245 return retVal;
2248 /**********************************************************************
2249 * LookupIconIdFromDirectory (USER.?)
2251 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
2253 return LookupIconIdFromDirectoryEx16( dir, bIcon,
2254 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2255 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2258 /**********************************************************************
2259 * LookupIconIdFromDirectory (USER32.@)
2261 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
2263 return LookupIconIdFromDirectoryEx( dir, bIcon,
2264 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2265 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2268 /**********************************************************************
2269 * GetIconID (USER.455)
2271 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
2273 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
2275 TRACE_(cursor)("hRes=%04x, entries=%i\n",
2276 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
2278 switch(resType)
2280 case RT_CURSOR:
2281 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
2282 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
2283 case RT_ICON:
2284 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
2285 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
2286 default:
2287 WARN_(cursor)("invalid res type %d\n", resType );
2289 return 0;
2292 /**********************************************************************
2293 * LoadCursorIconHandler (USER.336)
2295 * Supposed to load resources of Windows 2.x applications.
2297 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
2299 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2300 hResource, hModule, hRsrc);
2301 return (HGLOBAL16)0;
2304 /**********************************************************************
2305 * LoadIconHandler (USER.456)
2307 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2309 LPBYTE bits = (LPBYTE)LockResource16( hResource );
2311 TRACE_(cursor)("hRes=%04x\n",hResource);
2313 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2314 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2317 /***********************************************************************
2318 * LoadCursorW (USER32.@)
2320 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2322 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2324 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2325 LR_SHARED | LR_DEFAULTSIZE );
2328 /***********************************************************************
2329 * LoadCursorA (USER32.@)
2331 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2333 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2335 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2336 LR_SHARED | LR_DEFAULTSIZE );
2339 /***********************************************************************
2340 * LoadCursorFromFileW (USER32.@)
2342 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2344 TRACE("%s\n", debugstr_w(name));
2346 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2347 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2350 /***********************************************************************
2351 * LoadCursorFromFileA (USER32.@)
2353 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2355 TRACE("%s\n", debugstr_a(name));
2357 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2358 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2361 /***********************************************************************
2362 * LoadIconW (USER32.@)
2364 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2366 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2368 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2369 LR_SHARED | LR_DEFAULTSIZE );
2372 /***********************************************************************
2373 * LoadIconA (USER32.@)
2375 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2377 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2379 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2380 LR_SHARED | LR_DEFAULTSIZE );
2383 /**********************************************************************
2384 * GetIconInfo (USER32.@)
2386 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2388 CURSORICONINFO *ciconinfo;
2389 INT height;
2391 ciconinfo = GlobalLock16(HICON_16(hIcon));
2392 if (!ciconinfo)
2393 return FALSE;
2395 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2396 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2398 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2399 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2401 iconinfo->fIcon = TRUE;
2402 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2403 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2405 else
2407 iconinfo->fIcon = FALSE;
2408 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2409 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2412 height = ciconinfo->nHeight;
2414 if (ciconinfo->bBitsPerPixel > 1)
2416 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2417 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2418 (char *)(ciconinfo + 1)
2419 + ciconinfo->nHeight *
2420 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2422 else
2424 iconinfo->hbmColor = 0;
2425 height *= 2;
2428 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2429 1, 1, (char *)(ciconinfo + 1));
2431 GlobalUnlock16(HICON_16(hIcon));
2433 return TRUE;
2436 /**********************************************************************
2437 * CreateIconIndirect (USER32.@)
2439 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2441 HCURSOR cursor;
2442 cursor_frame_t frame;
2443 DIBSECTION bmpXor;
2444 BITMAP bmpAnd;
2445 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
2447 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2448 iconinfo->hbmColor, iconinfo->hbmMask,
2449 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2451 if (!iconinfo->hbmMask) return 0;
2453 planes = GetDeviceCaps( screen_dc, PLANES );
2454 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2456 if (iconinfo->hbmColor)
2458 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2459 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2460 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2461 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2462 /* we can use either depth 1 or screen depth for xor bitmap */
2463 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2464 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2466 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2467 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2468 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2469 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2471 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2473 cursor = create_cursor( 1, 0 );
2475 /* If we are creating an icon, the hotspot is unused */
2476 if (iconinfo->fIcon)
2478 frame.xhot = ICON_HOTSPOT;
2479 frame.yhot = ICON_HOTSPOT;
2481 else
2483 frame.xhot = iconinfo->xHotspot;
2484 frame.yhot = iconinfo->yHotspot;
2487 if (iconinfo->hbmColor)
2489 frame.width = bmpXor.dsBm.bmWidth;
2490 frame.height = bmpXor.dsBm.bmHeight;
2491 frame.and_width_bytes = bmpAnd.bmWidthBytes;
2492 frame.xor_width_bytes = bmpXor.dsBm.bmWidthBytes;
2493 frame.planes = planes;
2494 frame.bpp = bpp;
2496 else
2498 frame.width = bmpAnd.bmWidth;
2499 frame.height = bmpAnd.bmHeight / 2;
2500 frame.and_width_bytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2501 frame.xor_width_bytes = 0;
2502 frame.planes = 1;
2503 frame.bpp = 1;
2506 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
2508 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2509 if (bmpAnd.bmBitsPixel == 1)
2511 GetBitmapBits( iconinfo->hbmMask, sizeAnd, frame.bits );
2513 else
2515 HDC hdc, hdc_mem;
2516 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2518 hdc = GetDC( 0 );
2519 hdc_mem = CreateCompatibleDC( hdc );
2521 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2523 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2524 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2526 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2528 SelectObject( hdc, hbmp_old );
2529 SelectObject( hdc_mem, hbmp_mem_old );
2531 DeleteDC( hdc_mem );
2532 ReleaseDC( 0, hdc );
2534 GetBitmapBits( hbmp_mono, sizeAnd, frame.bits );
2535 DeleteObject( hbmp_mono );
2538 if (iconinfo->hbmColor)
2540 unsigned char *dst_bits = frame.bits + sizeAnd;
2542 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2543 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2544 else
2546 BITMAPINFO bminfo;
2547 int dib_width = get_dib_width_bytes( frame.width, frame.bpp );
2548 int bitmap_width = get_bitmap_width_bytes( frame.width, frame.bpp );
2550 bminfo.bmiHeader.biSize = sizeof(bminfo);
2551 bminfo.bmiHeader.biWidth = frame.width;
2552 bminfo.bmiHeader.biHeight = frame.height;
2553 bminfo.bmiHeader.biPlanes = frame.planes;
2554 bminfo.bmiHeader.biBitCount = frame.bpp;
2555 bminfo.bmiHeader.biCompression = BI_RGB;
2556 bminfo.bmiHeader.biSizeImage = frame.height * dib_width;
2557 bminfo.bmiHeader.biXPelsPerMeter = 0;
2558 bminfo.bmiHeader.biYPelsPerMeter = 0;
2559 bminfo.bmiHeader.biClrUsed = 0;
2560 bminfo.bmiHeader.biClrImportant = 0;
2562 /* swap lines for dib sections */
2563 if (xor_objsize == sizeof(DIBSECTION))
2564 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2566 if (dib_width != bitmap_width) /* need to fixup alignment */
2568 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2570 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, frame.height,
2571 src_bits, &bminfo, DIB_RGB_COLORS ))
2573 int y;
2574 for (y = 0; y < frame.height; y++)
2575 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2577 HeapFree( GetProcessHeap(), 0, src_bits );
2579 else
2580 GetDIBits( screen_dc, iconinfo->hbmColor, 0, frame.height,
2581 dst_bits, &bminfo, DIB_RGB_COLORS );
2584 set_cursor_frame( cursor, 0, &frame );
2585 HeapFree( GetProcessHeap(), 0, frame.bits );
2587 return cursor;
2590 /******************************************************************************
2591 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2593 * NOTES
2594 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2596 * PARAMS
2597 * hdc [I] Handle to device context
2598 * x0 [I] X coordinate of upper left corner
2599 * y0 [I] Y coordinate of upper left corner
2600 * hIcon [I] Handle to icon to draw
2601 * cxWidth [I] Width of icon
2602 * cyWidth [I] Height of icon
2603 * istep [I] Index of frame in animated cursor
2604 * hbr [I] Handle to background brush
2605 * flags [I] Icon-drawing flags
2607 * RETURNS
2608 * Success: TRUE
2609 * Failure: FALSE
2611 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2612 INT cxWidth, INT cyWidth, UINT istep,
2613 HBRUSH hbr, UINT flags )
2615 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
2616 HDC hDC_off = 0, hMemDC;
2617 BOOL result = FALSE, DoOffscreen;
2618 HBITMAP hB_off = 0, hOld = 0;
2620 if (!ptr) return FALSE;
2621 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2622 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2624 hMemDC = CreateCompatibleDC (hdc);
2625 if (istep)
2626 FIXME_(icon)("Ignoring istep=%d\n", istep);
2627 if (flags & DI_NOMIRROR)
2628 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2630 if (!flags) {
2631 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2632 flags = DI_NORMAL;
2635 /* Calculate the size of the destination image. */
2636 if (cxWidth == 0)
2638 if (flags & DI_DEFAULTSIZE)
2639 cxWidth = GetSystemMetrics (SM_CXICON);
2640 else
2641 cxWidth = ptr->nWidth;
2643 if (cyWidth == 0)
2645 if (flags & DI_DEFAULTSIZE)
2646 cyWidth = GetSystemMetrics (SM_CYICON);
2647 else
2648 cyWidth = ptr->nHeight;
2651 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2653 if (DoOffscreen) {
2654 RECT r;
2656 r.left = 0;
2657 r.top = 0;
2658 r.right = cxWidth;
2659 r.bottom = cxWidth;
2661 hDC_off = CreateCompatibleDC(hdc);
2662 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2663 if (hDC_off && hB_off) {
2664 hOld = SelectObject(hDC_off, hB_off);
2665 FillRect(hDC_off, &r, hbr);
2669 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2671 HBITMAP hXorBits, hAndBits;
2672 COLORREF oldFg, oldBg;
2673 INT nStretchMode;
2675 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2677 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2678 ptr->bPlanes, ptr->bBitsPerPixel,
2679 (char *)(ptr + 1)
2680 + ptr->nHeight *
2681 get_bitmap_width_bytes(ptr->nWidth,1) );
2682 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2683 1, 1, (char *)(ptr+1) );
2684 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2685 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2687 if (hXorBits && hAndBits)
2689 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2690 if (flags & DI_MASK)
2692 if (DoOffscreen)
2693 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2694 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2695 else
2696 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2697 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2699 SelectObject( hMemDC, hXorBits );
2700 if (flags & DI_IMAGE)
2702 if (DoOffscreen)
2703 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2704 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2705 else
2706 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2707 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2709 SelectObject( hMemDC, hBitTemp );
2710 result = TRUE;
2713 SetTextColor( hdc, oldFg );
2714 SetBkColor( hdc, oldBg );
2715 if (hXorBits) DeleteObject( hXorBits );
2716 if (hAndBits) DeleteObject( hAndBits );
2717 SetStretchBltMode (hdc, nStretchMode);
2718 if (DoOffscreen) {
2719 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2720 SelectObject(hDC_off, hOld);
2723 if (hMemDC) DeleteDC( hMemDC );
2724 if (hDC_off) DeleteDC(hDC_off);
2725 if (hB_off) DeleteObject(hB_off);
2726 GlobalUnlock16(HICON_16(hIcon));
2727 return result;
2730 /***********************************************************************
2731 * DIB_FixColorsToLoadflags
2733 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2734 * are in loadflags
2736 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2738 int colors;
2739 COLORREF c_W, c_S, c_F, c_L, c_C;
2740 int incr,i;
2741 RGBQUAD *ptr;
2742 int bitmap_type;
2743 LONG width;
2744 LONG height;
2745 WORD bpp;
2746 DWORD compr;
2748 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2750 WARN_(resource)("Invalid bitmap\n");
2751 return;
2754 if (bpp > 8) return;
2756 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2758 incr = 3;
2759 colors = 1 << bpp;
2761 else
2763 incr = 4;
2764 colors = bmi->bmiHeader.biClrUsed;
2765 if (colors > 256) colors = 256;
2766 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2769 c_W = GetSysColor(COLOR_WINDOW);
2770 c_S = GetSysColor(COLOR_3DSHADOW);
2771 c_F = GetSysColor(COLOR_3DFACE);
2772 c_L = GetSysColor(COLOR_3DLIGHT);
2774 if (loadflags & LR_LOADTRANSPARENT) {
2775 switch (bpp) {
2776 case 1: pix = pix >> 7; break;
2777 case 4: pix = pix >> 4; break;
2778 case 8: break;
2779 default:
2780 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2781 return;
2783 if (pix >= colors) {
2784 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2785 return;
2787 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2788 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2789 ptr->rgbBlue = GetBValue(c_W);
2790 ptr->rgbGreen = GetGValue(c_W);
2791 ptr->rgbRed = GetRValue(c_W);
2793 if (loadflags & LR_LOADMAP3DCOLORS)
2794 for (i=0; i<colors; i++) {
2795 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2796 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2797 if (c_C == RGB(128, 128, 128)) {
2798 ptr->rgbRed = GetRValue(c_S);
2799 ptr->rgbGreen = GetGValue(c_S);
2800 ptr->rgbBlue = GetBValue(c_S);
2801 } else if (c_C == RGB(192, 192, 192)) {
2802 ptr->rgbRed = GetRValue(c_F);
2803 ptr->rgbGreen = GetGValue(c_F);
2804 ptr->rgbBlue = GetBValue(c_F);
2805 } else if (c_C == RGB(223, 223, 223)) {
2806 ptr->rgbRed = GetRValue(c_L);
2807 ptr->rgbGreen = GetGValue(c_L);
2808 ptr->rgbBlue = GetBValue(c_L);
2814 /**********************************************************************
2815 * BITMAP_Load
2817 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2818 INT desiredx, INT desiredy, UINT loadflags )
2820 HBITMAP hbitmap = 0, orig_bm;
2821 HRSRC hRsrc;
2822 HGLOBAL handle;
2823 char *ptr = NULL;
2824 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2825 int size;
2826 BYTE pix;
2827 char *bits;
2828 LONG width, height, new_width, new_height;
2829 WORD bpp_dummy;
2830 DWORD compr_dummy;
2831 INT bm_type;
2832 HDC screen_mem_dc = NULL;
2834 if (!(loadflags & LR_LOADFROMFILE))
2836 if (!instance)
2838 /* OEM bitmap: try to load the resource from user32.dll */
2839 instance = user32_module;
2842 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2843 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2845 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2847 else
2849 BITMAPFILEHEADER * bmfh;
2851 if (!(ptr = map_fileW( name, NULL ))) return 0;
2852 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2853 bmfh = (BITMAPFILEHEADER *)ptr;
2854 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2855 bmfh->bfReserved1 == 0 &&
2856 bmfh->bfReserved2 == 0))
2858 WARN("Invalid/unsupported bitmap format!\n");
2859 UnmapViewOfFile( ptr );
2860 return 0;
2864 size = bitmap_info_size(info, DIB_RGB_COLORS);
2865 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2866 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2868 if (!fix_info || !scaled_info) goto end;
2869 memcpy(fix_info, info, size);
2871 pix = *((LPBYTE)info + size);
2872 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2874 memcpy(scaled_info, fix_info, size);
2875 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2876 &bpp_dummy, &compr_dummy);
2877 if(desiredx != 0)
2878 new_width = desiredx;
2879 else
2880 new_width = width;
2882 if(desiredy != 0)
2883 new_height = height > 0 ? desiredy : -desiredy;
2884 else
2885 new_height = height;
2887 if(bm_type == 0)
2889 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2890 core->bcWidth = new_width;
2891 core->bcHeight = new_height;
2893 else
2895 scaled_info->bmiHeader.biWidth = new_width;
2896 scaled_info->bmiHeader.biHeight = new_height;
2899 if (new_height < 0) new_height = -new_height;
2901 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2902 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2904 bits = (char *)info + size;
2906 if (loadflags & LR_CREATEDIBSECTION)
2908 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2909 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2911 else
2913 if (is_dib_monochrome(fix_info))
2914 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2915 else
2916 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2919 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2920 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2921 SelectObject(screen_mem_dc, orig_bm);
2923 end:
2924 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2925 HeapFree(GetProcessHeap(), 0, scaled_info);
2926 HeapFree(GetProcessHeap(), 0, fix_info);
2927 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2929 return hbitmap;
2932 /**********************************************************************
2933 * LoadImageA (USER32.@)
2935 * See LoadImageW.
2937 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2938 INT desiredx, INT desiredy, UINT loadflags)
2940 HANDLE res;
2941 LPWSTR u_name;
2943 if (!HIWORD(name))
2944 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2946 __TRY {
2947 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2948 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2949 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2951 __EXCEPT_PAGE_FAULT {
2952 SetLastError( ERROR_INVALID_PARAMETER );
2953 return 0;
2955 __ENDTRY
2956 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2957 HeapFree(GetProcessHeap(), 0, u_name);
2958 return res;
2962 /******************************************************************************
2963 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2965 * PARAMS
2966 * hinst [I] Handle of instance that contains image
2967 * name [I] Name of image
2968 * type [I] Type of image
2969 * desiredx [I] Desired width
2970 * desiredy [I] Desired height
2971 * loadflags [I] Load flags
2973 * RETURNS
2974 * Success: Handle to newly loaded image
2975 * Failure: NULL
2977 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2979 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2980 INT desiredx, INT desiredy, UINT loadflags )
2982 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2983 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2985 if (loadflags & LR_DEFAULTSIZE) {
2986 if (type == IMAGE_ICON) {
2987 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2988 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2989 } else if (type == IMAGE_CURSOR) {
2990 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2991 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2994 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2995 switch (type) {
2996 case IMAGE_BITMAP:
2997 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2999 case IMAGE_ICON:
3000 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
3001 if (screen_dc)
3003 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
3004 if (palEnts == 0) palEnts = 256;
3005 return CURSORICON_Load(hinst, name, desiredx, desiredy,
3006 palEnts, FALSE, loadflags);
3008 break;
3010 case IMAGE_CURSOR:
3011 return CURSORICON_Load(hinst, name, desiredx, desiredy,
3012 1, TRUE, loadflags);
3014 return 0;
3017 /******************************************************************************
3018 * CopyImage (USER32.@) Creates new image and copies attributes to it
3020 * PARAMS
3021 * hnd [I] Handle to image to copy
3022 * type [I] Type of image to copy
3023 * desiredx [I] Desired width of new image
3024 * desiredy [I] Desired height of new image
3025 * flags [I] Copy flags
3027 * RETURNS
3028 * Success: Handle to newly created image
3029 * Failure: NULL
3031 * BUGS
3032 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
3033 * all other versions (95/2000/XP have been tested) ignore it.
3035 * NOTES
3036 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
3037 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
3038 * the copy will have the same depth as the screen.
3039 * The content of the image will only be copied if the bit depth of the
3040 * original image is compatible with the bit depth of the screen, or
3041 * if the source is a DIB section.
3042 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
3044 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
3045 INT desiredy, UINT flags )
3047 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
3048 hnd, type, desiredx, desiredy, flags);
3050 switch (type)
3052 case IMAGE_BITMAP:
3054 HBITMAP res = NULL;
3055 DIBSECTION ds;
3056 int objSize;
3057 BITMAPINFO * bi;
3059 objSize = GetObjectW( hnd, sizeof(ds), &ds );
3060 if (!objSize) return 0;
3061 if ((desiredx < 0) || (desiredy < 0)) return 0;
3063 if (flags & LR_COPYFROMRESOURCE)
3065 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
3068 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
3069 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
3071 /* Allocate memory for a BITMAPINFOHEADER structure and a
3072 color table. The maximum number of colors in a color table
3073 is 256 which corresponds to a bitmap with depth 8.
3074 Bitmaps with higher depths don't have color tables. */
3075 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3076 if (!bi) return 0;
3078 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3079 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
3080 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3081 bi->bmiHeader.biCompression = BI_RGB;
3083 if (flags & LR_CREATEDIBSECTION)
3085 /* Create a DIB section. LR_MONOCHROME is ignored */
3086 void * bits;
3087 HDC dc = CreateCompatibleDC(NULL);
3089 if (objSize == sizeof(DIBSECTION))
3091 /* The source bitmap is a DIB.
3092 Get its attributes to create an exact copy */
3093 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
3096 /* Get the color table or the color masks */
3097 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3099 bi->bmiHeader.biWidth = desiredx;
3100 bi->bmiHeader.biHeight = desiredy;
3101 bi->bmiHeader.biSizeImage = 0;
3103 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
3104 DeleteDC(dc);
3106 else
3108 /* Create a device-dependent bitmap */
3110 BOOL monochrome = (flags & LR_MONOCHROME);
3112 if (objSize == sizeof(DIBSECTION))
3114 /* The source bitmap is a DIB section.
3115 Get its attributes */
3116 HDC dc = CreateCompatibleDC(NULL);
3117 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3118 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3119 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3120 DeleteDC(dc);
3122 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
3124 /* Look if the colors of the DIB are black and white */
3126 monochrome =
3127 (bi->bmiColors[0].rgbRed == 0xff
3128 && bi->bmiColors[0].rgbGreen == 0xff
3129 && bi->bmiColors[0].rgbBlue == 0xff
3130 && bi->bmiColors[0].rgbReserved == 0
3131 && bi->bmiColors[1].rgbRed == 0
3132 && bi->bmiColors[1].rgbGreen == 0
3133 && bi->bmiColors[1].rgbBlue == 0
3134 && bi->bmiColors[1].rgbReserved == 0)
3136 (bi->bmiColors[0].rgbRed == 0
3137 && bi->bmiColors[0].rgbGreen == 0
3138 && bi->bmiColors[0].rgbBlue == 0
3139 && bi->bmiColors[0].rgbReserved == 0
3140 && bi->bmiColors[1].rgbRed == 0xff
3141 && bi->bmiColors[1].rgbGreen == 0xff
3142 && bi->bmiColors[1].rgbBlue == 0xff
3143 && bi->bmiColors[1].rgbReserved == 0);
3146 else if (!monochrome)
3148 monochrome = ds.dsBm.bmBitsPixel == 1;
3151 if (monochrome)
3153 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
3155 else
3157 HDC screenDC = GetDC(NULL);
3158 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
3159 ReleaseDC(NULL, screenDC);
3163 if (res)
3165 /* Only copy the bitmap if it's a DIB section or if it's
3166 compatible to the screen */
3167 BOOL copyContents;
3169 if (objSize == sizeof(DIBSECTION))
3171 copyContents = TRUE;
3173 else
3175 HDC screenDC = GetDC(NULL);
3176 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
3177 ReleaseDC(NULL, screenDC);
3179 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
3182 if (copyContents)
3184 /* The source bitmap may already be selected in a device context,
3185 use GetDIBits/StretchDIBits and not StretchBlt */
3187 HDC dc;
3188 void * bits;
3190 dc = CreateCompatibleDC(NULL);
3192 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
3193 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
3194 bi->bmiHeader.biSizeImage = 0;
3195 bi->bmiHeader.biClrUsed = 0;
3196 bi->bmiHeader.biClrImportant = 0;
3198 /* Fill in biSizeImage */
3199 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3200 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
3202 if (bits)
3204 HBITMAP oldBmp;
3206 /* Get the image bits of the source bitmap */
3207 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
3209 /* Copy it to the destination bitmap */
3210 oldBmp = SelectObject(dc, res);
3211 StretchDIBits(dc, 0, 0, desiredx, desiredy,
3212 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
3213 bits, bi, DIB_RGB_COLORS, SRCCOPY);
3214 SelectObject(dc, oldBmp);
3216 HeapFree(GetProcessHeap(), 0, bits);
3219 DeleteDC(dc);
3222 if (flags & LR_COPYDELETEORG)
3224 DeleteObject(hnd);
3227 HeapFree(GetProcessHeap(), 0, bi);
3228 return res;
3230 case IMAGE_ICON:
3231 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
3232 case IMAGE_CURSOR:
3233 /* Should call CURSORICON_ExtCopy but more testing
3234 * needs to be done before we change this
3236 if (flags) FIXME("Flags are ignored\n");
3237 return CopyCursor(hnd);
3239 return 0;
3243 /******************************************************************************
3244 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3246 * RETURNS
3247 * Success: Handle to specified bitmap
3248 * Failure: NULL
3250 HBITMAP WINAPI LoadBitmapW(
3251 HINSTANCE instance, /* [in] Handle to application instance */
3252 LPCWSTR name) /* [in] Address of bitmap resource name */
3254 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3257 /**********************************************************************
3258 * LoadBitmapA (USER32.@)
3260 * See LoadBitmapW.
3262 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3264 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );