push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / user32 / cursoricon.c
blobeecf5094cbd9dbb3159c2c28fddae379e1344a6c
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 "windef.h"
55 #include "winbase.h"
56 #include "wingdi.h"
57 #include "winerror.h"
58 #include "wine/winbase16.h"
59 #include "wine/winuser16.h"
60 #include "wine/exception.h"
61 #include "wine/debug.h"
62 #include "wine/list.h"
63 #include "wine/server.h"
64 #include "user_private.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
67 WINE_DECLARE_DEBUG_CHANNEL(icon);
68 WINE_DECLARE_DEBUG_CHANNEL(resource);
70 #include "pshpack1.h"
72 typedef struct {
73 BYTE bWidth;
74 BYTE bHeight;
75 BYTE bColorCount;
76 BYTE bReserved;
77 WORD xHotspot;
78 WORD yHotspot;
79 DWORD dwDIBSize;
80 DWORD dwDIBOffset;
81 } CURSORICONFILEDIRENTRY;
83 typedef struct
85 WORD idReserved;
86 WORD idType;
87 WORD idCount;
88 CURSORICONFILEDIRENTRY idEntries[1];
89 } CURSORICONFILEDIR;
91 #include "poppack.h"
93 #define CID_RESOURCE 0x0001
94 #define CID_WIN32 0x0004
95 #define CID_NONSHARED 0x0008
97 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
99 static HDC screen_dc;
101 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
103 /**********************************************************************
104 * ICONCACHE for cursors/icons loaded with LR_SHARED.
106 * FIXME: This should not be allocated on the system heap, but on a
107 * subsystem-global heap (i.e. one for all Win16 processes,
108 * and one for each Win32 process).
110 typedef struct tagICONCACHE
112 struct tagICONCACHE *next;
114 HMODULE hModule;
115 HRSRC hRsrc;
116 HRSRC hGroupRsrc;
117 HICON hIcon;
119 INT count;
121 } ICONCACHE;
123 static ICONCACHE *IconAnchor = NULL;
125 static CRITICAL_SECTION IconCrst;
126 static CRITICAL_SECTION_DEBUG critsect_debug =
128 0, 0, &IconCrst,
129 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
130 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
132 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
134 static const WORD ICON_HOTSPOT = 0x4242;
136 /* What is a good table size? */
137 #define CURSOR_HASH_SIZE 97
139 typedef struct {
140 HCURSOR16 cursor16;
141 HCURSOR cursor32;
142 struct list entry16;
143 struct list entry32;
144 } cursor_map_entry_t;
146 static int get_bitmap_width_bytes( int width, int bpp );
148 static struct list cursor16to32[CURSOR_HASH_SIZE];
149 static struct list cursor32to16[CURSOR_HASH_SIZE];
151 static inline int hash_cursor_handle( DWORD handle )
153 return handle % CURSOR_HASH_SIZE;
156 static void add_cursor16to32_entry( cursor_map_entry_t *entry )
158 int idx = hash_cursor_handle( entry->cursor16 );
160 if (!cursor16to32[idx].next) list_init( &cursor16to32[idx] );
162 list_add_head( &cursor16to32[idx], &entry->entry16 );
165 static void add_cursor32to16_entry( cursor_map_entry_t *entry )
167 int idx = hash_cursor_handle( (DWORD)entry->cursor32 );
169 if (!cursor32to16[idx].next) list_init( &cursor32to16[idx] );
171 list_add_head( &cursor32to16[idx], &entry->entry32 );
174 static cursor_map_entry_t *remove_cursor16to32_entry( HCURSOR16 cursor16 )
176 cursor_map_entry_t *entry = NULL;
177 int idx = hash_cursor_handle( cursor16 );
179 if (cursor16to32[idx].next)
181 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
182 if (entry->cursor16 == cursor16)
184 list_remove( &entry->entry16 );
185 return entry;
189 return entry;
192 static cursor_map_entry_t *remove_cursor32to16_entry( HCURSOR cursor32 )
194 cursor_map_entry_t *entry = NULL;
195 int idx = hash_cursor_handle( (DWORD)cursor32 );
197 if (cursor32to16[idx].next)
199 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
200 if (entry->cursor32 == cursor32)
202 list_remove( &entry->entry32 );
203 return entry;
207 return entry;
210 /* Ask the server for a cursor */
211 static HCURSOR create_cursor( unsigned int num_frames, unsigned int delay )
213 HCURSOR cursor = 0;
215 SERVER_START_REQ(create_cursor)
217 req->num_frames = num_frames;
218 req->delay = delay;
219 if (!wine_server_call_err( req )) cursor = reply->handle;
221 SERVER_END_REQ;
223 return cursor;
226 /* Tell the server to kill a cursor */
227 static HCURSOR16 destroy_cursor( HCURSOR cursor )
229 cursor_map_entry_t *entry;
230 HCURSOR16 cursor16 = 0;
232 if (!cursor) return 0;
234 SERVER_START_REQ(destroy_cursor)
236 req->handle = cursor;
237 wine_server_call( req );
239 SERVER_END_REQ;
241 entry = remove_cursor32to16_entry( cursor );
242 if (entry)
244 cursor16 = entry->cursor16;
245 remove_cursor16to32_entry( cursor16 );
246 HeapFree( GetProcessHeap(), 0, entry );
249 return GlobalFree16( cursor16 );
252 /* Upload a cursor frame to the server */
253 static void set_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
255 SERVER_START_REQ(set_cursor_frame)
257 req->handle = cursor;
258 req->frame_idx = frame_idx;
259 req->xhot = frame->xhot;
260 req->yhot = frame->yhot;
261 req->width = frame->width;
262 req->height = frame->height;
263 req->and_width_bytes = frame->and_width_bytes;
264 req->xor_width_bytes = frame->xor_width_bytes;
265 req->planes = frame->planes;
266 req->bpp = frame->bpp;
267 wine_server_add_data( req, frame->bits, (frame->and_width_bytes + frame->xor_width_bytes) * frame->height );
268 wine_server_call( req );
270 SERVER_END_REQ;
273 /* Download a cursor frame from the server */
274 static BOOL get_cursor_frame( HCURSOR cursor, unsigned int frame_idx, cursor_frame_t *frame )
276 NTSTATUS res;
277 /* Enough for a 32-bits 32x32 cursor / icon. */
278 unsigned int buffer_size = 4224;
279 unsigned int count = 0;
283 frame->bits = HeapAlloc(GetProcessHeap(), 0, buffer_size);
284 SERVER_START_REQ(get_cursor_frame)
286 req->handle = cursor;
287 req->frame_idx = frame_idx;
288 wine_server_set_reply( req, frame->bits, buffer_size);
289 if (!(res = wine_server_call_err( req )))
291 frame->xhot = reply->xhot;
292 frame->yhot = reply->yhot;
293 frame->width = reply->width;
294 frame->height = reply->height;
295 frame->and_width_bytes = reply->and_width_bytes;
296 frame->xor_width_bytes = reply->xor_width_bytes;
297 frame->planes = reply->planes;
298 frame->bpp = reply->bpp;
299 } else {
300 HeapFree( GetProcessHeap(), 0, frame->bits );
301 buffer_size = (reply->and_width_bytes + reply->xor_width_bytes) * reply->height;
304 SERVER_END_REQ;
305 } while (res == STATUS_BUFFER_OVERFLOW && !count++);
307 if (!frame->height)
309 HeapFree( GetProcessHeap(), 0, frame->bits );
311 return FALSE;
314 return TRUE;
317 /* Retrieve a cursor and all its frames from the server */
318 static cursor_t *get_cursor_object( HCURSOR handle )
320 unsigned int i;
321 cursor_t *cursor = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(cursor_t) );
323 SERVER_START_REQ(get_cursor_info)
325 req->handle = handle;
326 if (!wine_server_call_err( req ))
328 cursor->num_frames = reply->num_frames;
329 cursor->delay = reply->delay;
332 SERVER_END_REQ;
334 if (!cursor->num_frames)
336 HeapFree( GetProcessHeap(), 0, cursor );
337 return NULL;
340 cursor->frames = HeapAlloc( GetProcessHeap(), 0, cursor->num_frames * sizeof(cursor_frame_t) );
341 for (i = 0; i < cursor->num_frames; ++i)
343 if (!get_cursor_frame( handle, i, &cursor->frames[i] ))
345 unsigned int j;
347 for (j = 0; j < i; ++j)
349 HeapFree( GetProcessHeap(), 0, cursor->frames[j].bits );
351 HeapFree( GetProcessHeap(), 0, cursor->frames );
352 HeapFree( GetProcessHeap(), 0, cursor );
354 return NULL;
358 return cursor;
361 static void destroy_cursor_object( cursor_t *cursor )
363 unsigned int i;
365 if (!cursor) return;
367 for (i = 0; i < cursor->num_frames; ++i)
369 HeapFree( GetProcessHeap(), 0, cursor->frames[i].bits );
371 HeapFree( GetProcessHeap(), 0, cursor->frames );
372 HeapFree( GetProcessHeap(), 0, cursor );
375 /* Lookup the cursor's 16-bit handle. Create one if it doesn't already exist. */
376 HCURSOR16 get_cursor_handle16( HCURSOR cursor32, BOOL create )
378 cursor_map_entry_t *entry;
379 int idx = hash_cursor_handle( (DWORD)cursor32 );
381 if (!cursor32) return 0;
383 if (cursor32to16[idx].next)
385 LIST_FOR_EACH_ENTRY( entry, &cursor32to16[idx], cursor_map_entry_t, entry32 )
386 if (entry->cursor32 == cursor32) return entry->cursor16;
389 /* 16-bit cursor handle not found, create one */
390 if (create)
392 size_t bits_size;
393 HCURSOR16 cursor16;
394 cursor_frame_t frame;
396 if (!get_cursor_frame( cursor32, 0, &frame )) return 0;
398 entry = HeapAlloc( GetProcessHeap(), 0, sizeof(cursor_map_entry_t) );
399 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
400 cursor16 = GlobalAlloc16( GMEM_MOVEABLE, sizeof(CURSORICONINFO) + bits_size );
401 if (cursor16)
403 CURSORICONINFO *info;
405 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
406 info->ptHotSpot.x = frame.xhot;
407 info->ptHotSpot.y = frame.yhot;
408 info->nWidth = frame.width;
409 info->nHeight = frame.height;
410 info->nWidthBytes = frame.xor_width_bytes;
411 info->bPlanes = frame.planes;
412 info->bBitsPerPixel = frame.bpp;
413 CopyMemory( info + 1, frame.bits, bits_size );
414 GlobalUnlock16( cursor16 );
416 HeapFree( GetProcessHeap(), 0, frame.bits );
418 entry->cursor16 = cursor16;
419 entry->cursor32 = cursor32;
420 add_cursor16to32_entry( entry );
421 add_cursor32to16_entry( entry );
423 return cursor16;
426 return 0;
429 HCURSOR get_cursor_handle32( HCURSOR16 cursor16 )
431 cursor_map_entry_t *entry;
432 int idx = hash_cursor_handle( cursor16 );
434 if (!cursor16) return 0;
436 if (cursor16to32[idx].next)
438 LIST_FOR_EACH_ENTRY( entry, &cursor16to32[idx], cursor_map_entry_t, entry16 )
439 if (entry->cursor16 == cursor16) return entry->cursor32;
442 return 0;
445 static void update_cursor_32from16( HCURSOR cursor32 )
447 size_t bits_size;
448 HCURSOR16 cursor16;
449 cursor_frame_t frame;
450 CURSORICONINFO *info;
452 if (!cursor32) return;
454 cursor16 = get_cursor_handle16( cursor32, FALSE );
455 if (!cursor16) return;
457 info = (CURSORICONINFO *)GlobalLock16( cursor16 );
458 frame.xhot = info->ptHotSpot.x;
459 frame.yhot = info->ptHotSpot.y;
460 frame.width = info->nWidth;
461 frame.height = info->nHeight;
462 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
463 frame.xor_width_bytes = info->nWidthBytes;
464 frame.planes = info->bPlanes;
465 frame.bpp = info->bBitsPerPixel;
466 bits_size = (frame.and_width_bytes + frame.xor_width_bytes) * frame.height;
467 frame.bits = HeapAlloc( GetProcessHeap(), 0, bits_size );
468 CopyMemory( frame.bits, info + 1, bits_size );
469 GlobalUnlock16( cursor16 );
471 set_cursor_frame( cursor32, 0, &frame );
472 HeapFree( GetProcessHeap(), 0, frame.bits );
475 /***********************************************************************
476 * map_fileW
478 * Helper function to map a file to memory:
479 * name - file name
480 * [RETURN] ptr - pointer to mapped file
481 * [RETURN] filesize - pointer size of file to be stored if not NULL
483 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
485 HANDLE hFile, hMapping;
486 LPVOID ptr = NULL;
488 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
489 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
490 if (hFile != INVALID_HANDLE_VALUE)
492 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
493 if (hMapping)
495 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
496 CloseHandle( hMapping );
497 if (filesize)
498 *filesize = GetFileSize( hFile, NULL );
500 CloseHandle( hFile );
502 return ptr;
506 /***********************************************************************
507 * get_bitmap_width_bytes
509 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
510 * data.
512 static int get_bitmap_width_bytes( int width, int bpp )
514 switch(bpp)
516 case 1:
517 return 2 * ((width+15) / 16);
518 case 4:
519 return 2 * ((width+3) / 4);
520 case 24:
521 width *= 3;
522 /* fall through */
523 case 8:
524 return width + (width & 1);
525 case 16:
526 case 15:
527 return width * 2;
528 case 32:
529 return width * 4;
530 default:
531 WARN("Unknown depth %d, please report.\n", bpp );
533 return -1;
537 /***********************************************************************
538 * get_dib_width_bytes
540 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
542 static int get_dib_width_bytes( int width, int depth )
544 int words;
546 switch(depth)
548 case 1: words = (width + 31) / 32; break;
549 case 4: words = (width + 7) / 8; break;
550 case 8: words = (width + 3) / 4; break;
551 case 15:
552 case 16: words = (width + 1) / 2; break;
553 case 24: words = (width * 3 + 3)/4; break;
554 default:
555 WARN("(%d): Unsupported depth\n", depth );
556 /* fall through */
557 case 32:
558 words = width;
560 return 4 * words;
564 /***********************************************************************
565 * bitmap_info_size
567 * Return the size of the bitmap info structure including color table.
569 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
571 int colors;
573 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
575 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
576 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
577 return sizeof(BITMAPCOREHEADER) + colors *
578 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
580 else /* assume BITMAPINFOHEADER */
582 colors = info->bmiHeader.biClrUsed;
583 if (colors > 256) /* buffer overflow otherwise */
584 colors = 256;
585 if (!colors && (info->bmiHeader.biBitCount <= 8))
586 colors = 1 << info->bmiHeader.biBitCount;
587 return sizeof(BITMAPINFOHEADER) + colors *
588 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
593 /***********************************************************************
594 * is_dib_monochrome
596 * Returns whether a DIB can be converted to a monochrome DDB.
598 * A DIB can be converted if its color table contains only black and
599 * white. Black must be the first color in the color table.
601 * Note : If the first color in the color table is white followed by
602 * black, we can't convert it to a monochrome DDB with
603 * SetDIBits, because black and white would be inverted.
605 static BOOL is_dib_monochrome( const BITMAPINFO* info )
607 if (info->bmiHeader.biBitCount != 1) return FALSE;
609 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
611 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
613 /* Check if the first color is black */
614 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
616 rgb++;
618 /* Check if the second color is white */
619 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
620 && (rgb->rgbtBlue == 0xff));
622 else return FALSE;
624 else /* assume BITMAPINFOHEADER */
626 const RGBQUAD *rgb = info->bmiColors;
628 /* Check if the first color is black */
629 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
630 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
632 rgb++;
634 /* Check if the second color is white */
635 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
636 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
638 else return FALSE;
642 /***********************************************************************
643 * DIB_GetBitmapInfo
645 * Get the info from a bitmap header.
646 * Return 1 for INFOHEADER, 0 for COREHEADER,
647 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
649 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
650 LONG *height, WORD *bpp, DWORD *compr )
652 if (header->biSize == sizeof(BITMAPINFOHEADER))
654 *width = header->biWidth;
655 *height = header->biHeight;
656 *bpp = header->biBitCount;
657 *compr = header->biCompression;
658 return 1;
660 if (header->biSize == sizeof(BITMAPCOREHEADER))
662 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
663 *width = core->bcWidth;
664 *height = core->bcHeight;
665 *bpp = core->bcBitCount;
666 *compr = 0;
667 return 0;
669 if (header->biSize == sizeof(BITMAPV4HEADER))
671 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
672 *width = v4hdr->bV4Width;
673 *height = v4hdr->bV4Height;
674 *bpp = v4hdr->bV4BitCount;
675 *compr = v4hdr->bV4V4Compression;
676 return 4;
678 if (header->biSize == sizeof(BITMAPV5HEADER))
680 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
681 *width = v5hdr->bV5Width;
682 *height = v5hdr->bV5Height;
683 *bpp = v5hdr->bV5BitCount;
684 *compr = v5hdr->bV5Compression;
685 return 5;
687 ERR("(%d): unknown/wrong size for header\n", header->biSize );
688 return -1;
691 /**********************************************************************
692 * CURSORICON_FindSharedIcon
694 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
696 HICON hIcon = 0;
697 ICONCACHE *ptr;
699 EnterCriticalSection( &IconCrst );
701 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
702 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
704 ptr->count++;
705 hIcon = ptr->hIcon;
706 break;
709 LeaveCriticalSection( &IconCrst );
711 return hIcon;
714 /*************************************************************************
715 * CURSORICON_FindCache
717 * Given a handle, find the corresponding cache element
719 * PARAMS
720 * Handle [I] handle to an Image
722 * RETURNS
723 * Success: The cache entry
724 * Failure: NULL
727 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
729 ICONCACHE *ptr;
730 ICONCACHE *pRet=NULL;
731 BOOL IsFound = FALSE;
732 int count;
734 EnterCriticalSection( &IconCrst );
736 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
738 if ( hIcon == ptr->hIcon )
740 IsFound = TRUE;
741 pRet = ptr;
745 LeaveCriticalSection( &IconCrst );
747 return pRet;
750 /**********************************************************************
751 * CURSORICON_AddSharedIcon
753 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
755 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
756 if ( !ptr ) return;
758 ptr->hModule = hModule;
759 ptr->hRsrc = hRsrc;
760 ptr->hIcon = hIcon;
761 ptr->hGroupRsrc = hGroupRsrc;
762 ptr->count = 1;
764 EnterCriticalSection( &IconCrst );
765 ptr->next = IconAnchor;
766 IconAnchor = ptr;
767 LeaveCriticalSection( &IconCrst );
770 /**********************************************************************
771 * CURSORICON_DelSharedIcon
773 static INT CURSORICON_DelSharedIcon( HICON hIcon )
775 INT count = -1;
776 ICONCACHE *ptr;
778 EnterCriticalSection( &IconCrst );
780 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
781 if ( ptr->hIcon == hIcon )
783 if ( ptr->count > 0 ) ptr->count--;
784 count = ptr->count;
785 break;
788 LeaveCriticalSection( &IconCrst );
790 return count;
793 /**********************************************************************
794 * CURSORICON_FreeModuleIcons
796 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
798 ICONCACHE **ptr = &IconAnchor;
799 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
801 EnterCriticalSection( &IconCrst );
803 while ( *ptr )
805 if ( (*ptr)->hModule == hModule )
807 ICONCACHE *freePtr = *ptr;
808 *ptr = freePtr->next;
810 destroy_cursor( freePtr->hIcon );
811 HeapFree( GetProcessHeap(), 0, freePtr );
812 continue;
814 ptr = &(*ptr)->next;
817 LeaveCriticalSection( &IconCrst );
821 * The following macro functions account for the irregularities of
822 * accessing cursor and icon resources in files and resource entries.
824 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
825 int *width, int *height, int *bits );
827 /**********************************************************************
828 * CURSORICON_FindBestIcon
830 * Find the icon closest to the requested size and number of colors.
832 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
833 int width, int height, int colors )
835 int i, cx, cy, bits, bestEntry = -1;
836 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
837 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
839 /* Find Best Fit */
840 iTotalDiff = 0xFFFFFFFF;
841 iColorDiff = 0xFFFFFFFF;
842 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
844 iTempXDiff = abs(width - cx);
845 iTempYDiff = abs(height - cy);
847 if(iTotalDiff > (iTempXDiff + iTempYDiff))
849 iXDiff = iTempXDiff;
850 iYDiff = iTempYDiff;
851 iTotalDiff = iXDiff + iYDiff;
855 /* Find Best Colors for Best Fit */
856 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
858 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
860 iTempColorDiff = abs(colors - (1<<bits));
861 if(iColorDiff > iTempColorDiff)
863 bestEntry = i;
864 iColorDiff = iTempColorDiff;
869 return bestEntry;
872 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
873 int *width, int *height, int *bits )
875 CURSORICONDIR *resdir = dir;
876 ICONRESDIR *icon;
878 if ( resdir->idCount <= n )
879 return FALSE;
880 icon = &resdir->idEntries[n].ResInfo.icon;
881 *width = icon->bWidth;
882 *height = icon->bHeight;
883 *bits = resdir->idEntries[n].wBitCount;
884 return TRUE;
887 /**********************************************************************
888 * CURSORICON_FindBestCursor
890 * Find the cursor closest to the requested size.
891 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
892 * ignored too
894 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
895 int width, int height, int color )
897 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
899 /* Double height to account for AND and XOR masks */
901 height *= 2;
903 /* First find the largest one smaller than or equal to the requested size*/
905 maxwidth = maxheight = 0;
906 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
908 if ((cx <= width) && (cy <= height) &&
909 (cx > maxwidth) && (cy > maxheight) &&
910 (bits == 1))
912 bestEntry = i;
913 maxwidth = cx;
914 maxheight = cy;
917 if (bestEntry != -1) return bestEntry;
919 /* Now find the smallest one larger than the requested size */
921 maxwidth = maxheight = 255;
922 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
924 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
925 (bestEntry==-1))
927 bestEntry = i;
928 maxwidth = cx;
929 maxheight = cy;
933 return bestEntry;
936 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
937 int *width, int *height, int *bits )
939 CURSORICONDIR *resdir = dir;
940 CURSORDIR *cursor;
942 if ( resdir->idCount <= n )
943 return FALSE;
944 cursor = &resdir->idEntries[n].ResInfo.cursor;
945 *width = cursor->wWidth;
946 *height = cursor->wHeight;
947 *bits = resdir->idEntries[n].wBitCount;
948 return TRUE;
951 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
952 int width, int height, int colors )
954 int n;
956 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
957 width, height, colors );
958 if ( n < 0 )
959 return NULL;
960 return &dir->idEntries[n];
963 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
964 int width, int height, int color )
966 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
967 width, height, color );
968 if ( n < 0 )
969 return NULL;
970 return &dir->idEntries[n];
973 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
974 int *width, int *height, int *bits )
976 CURSORICONFILEDIR *filedir = dir;
977 CURSORICONFILEDIRENTRY *entry;
979 if ( filedir->idCount <= n )
980 return FALSE;
981 entry = &filedir->idEntries[n];
982 *width = entry->bWidth;
983 *height = entry->bHeight;
984 *bits = entry->bColorCount;
985 return TRUE;
988 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
989 int width, int height, int color )
991 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
992 width, height, color );
993 if ( n < 0 )
994 return NULL;
995 return &dir->idEntries[n];
998 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
999 int width, int height, int color )
1001 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
1002 width, height, color );
1003 if ( n < 0 )
1004 return NULL;
1005 return &dir->idEntries[n];
1008 static BOOL load_cursor_frame( LPBYTE bits, UINT cbSize, POINT16 hotspot, DWORD dwVersion,
1009 INT width, INT height, UINT cFlag, cursor_frame_t *frame )
1011 static HDC hdcMem;
1012 int sizeAnd, sizeXor;
1013 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
1014 BITMAP bmpXor, bmpAnd;
1015 BITMAPINFO *bmi;
1016 BOOL DoStretch;
1017 INT size;
1019 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s\n",
1020 bits, cbSize, dwVersion, width, height,
1021 (cFlag & LR_MONOCHROME) ? "mono" : "" );
1022 if (dwVersion == 0x00020000)
1024 FIXME_(cursor)("\t2.xx resources are not supported\n");
1025 return FALSE;
1028 bmi = (BITMAPINFO *)bits;
1030 /* Check bitmap header */
1032 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
1033 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
1034 bmi->bmiHeader.biCompression != BI_RGB) )
1036 WARN_(cursor)("\tinvalid resource bitmap header.\n");
1037 return FALSE;
1040 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
1042 if (!width) width = bmi->bmiHeader.biWidth;
1043 if (!height) height = bmi->bmiHeader.biHeight/2;
1044 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
1045 (bmi->bmiHeader.biWidth != width);
1047 /* Scale the hotspot */
1048 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
1050 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
1051 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biWidth / 2);
1054 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1055 if (screen_dc)
1057 BITMAPINFO* pInfo;
1059 /* Make sure we have room for the monochrome bitmap later on.
1060 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1061 * up to and including the biBitCount. In-memory icon resource
1062 * format is as follows:
1064 * BITMAPINFOHEADER icHeader // DIB header
1065 * RGBQUAD icColors[] // Color table
1066 * BYTE icXOR[] // DIB bits for XOR mask
1067 * BYTE icAND[] // DIB bits for AND mask
1070 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
1071 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
1073 memcpy( pInfo, bmi, size );
1074 pInfo->bmiHeader.biHeight /= 2;
1076 /* Create the XOR bitmap */
1078 if (DoStretch) {
1079 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1080 if(hXorBits)
1082 HBITMAP hOld;
1083 BOOL res = FALSE;
1085 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1086 if (hdcMem) {
1087 hOld = SelectObject(hdcMem, hXorBits);
1088 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1089 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
1090 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
1091 SelectObject(hdcMem, hOld);
1093 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
1095 } else {
1096 if (is_dib_monochrome(bmi)) {
1097 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1098 SetDIBits(screen_dc, hXorBits, 0, height,
1099 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1100 } else if (bmi->bmiHeader.biBitCount == 32) {
1101 hXorBits = CreateDIBSection(screen_dc, pInfo, DIB_RGB_COLORS, NULL, NULL, 0);
1102 SetDIBits(screen_dc, hXorBits, 0, height,
1103 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1105 else
1106 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
1107 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1110 if( hXorBits )
1112 char* xbits = (char *)bmi + size +
1113 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1114 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1116 pInfo->bmiHeader.biBitCount = 1;
1117 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1119 RGBQUAD *rgb = pInfo->bmiColors;
1121 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
1122 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1123 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1124 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1126 else
1128 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
1130 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1131 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1134 /* Create the AND bitmap */
1136 if (DoStretch) {
1137 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
1138 HBITMAP hOld;
1139 BOOL res = FALSE;
1141 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1142 if (hdcMem) {
1143 hOld = SelectObject(hdcMem, hAndBits);
1144 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1145 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1146 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1147 SelectObject(hdcMem, hOld);
1149 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
1151 } else {
1152 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1154 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
1155 xbits, pInfo, DIB_RGB_COLORS);
1158 if( !hAndBits ) DeleteObject( hXorBits );
1160 HeapFree( GetProcessHeap(), 0, pInfo );
1164 if( !hXorBits || !hAndBits )
1166 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1167 return FALSE;
1170 /* Setup a cursor frame, send it to the server */
1171 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1172 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1173 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1174 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1176 frame->xhot = hotspot.x;
1177 frame->yhot = hotspot.y;
1178 frame->width = bmpXor.bmWidth;
1179 frame->height = bmpXor.bmHeight;
1180 frame->and_width_bytes = bmpAnd.bmWidthBytes;
1181 frame->xor_width_bytes = bmpXor.bmWidthBytes;
1182 frame->planes = bmpXor.bmPlanes;
1183 frame->bpp = bmpXor.bmBitsPixel;
1184 frame->bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1185 GetBitmapBits( hAndBits, sizeAnd, frame->bits );
1186 GetBitmapBits( hXorBits, sizeXor, frame->bits + sizeAnd );
1188 DeleteObject( hAndBits );
1189 DeleteObject( hXorBits );
1191 return TRUE;
1194 /**********************************************************************
1195 * .ANI cursor support
1197 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1198 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1199 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1201 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1202 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1203 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1204 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1205 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1206 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1208 #define ANI_FLAG_ICON 0x1
1209 #define ANI_FLAG_SEQUENCE 0x2
1211 typedef struct {
1212 DWORD header_size;
1213 DWORD num_frames;
1214 DWORD num_steps;
1215 DWORD width;
1216 DWORD height;
1217 DWORD bpp;
1218 DWORD num_planes;
1219 DWORD display_rate;
1220 DWORD flags;
1221 } ani_header;
1223 typedef struct {
1224 DWORD data_size;
1225 const unsigned char *data;
1226 } riff_chunk_t;
1228 static void dump_ani_header( const ani_header *header )
1230 TRACE(" header size: %d\n", header->header_size);
1231 TRACE(" frames: %d\n", header->num_frames);
1232 TRACE(" steps: %d\n", header->num_steps);
1233 TRACE(" width: %d\n", header->width);
1234 TRACE(" height: %d\n", header->height);
1235 TRACE(" bpp: %d\n", header->bpp);
1236 TRACE(" planes: %d\n", header->num_planes);
1237 TRACE(" display rate: %d\n", header->display_rate);
1238 TRACE(" flags: 0x%08x\n", header->flags);
1243 * RIFF:
1244 * DWORD "RIFF"
1245 * DWORD size
1246 * DWORD riff_id
1247 * BYTE[] data
1249 * LIST:
1250 * DWORD "LIST"
1251 * DWORD size
1252 * DWORD list_id
1253 * BYTE[] data
1255 * CHUNK:
1256 * DWORD chunk_id
1257 * DWORD size
1258 * BYTE[] data
1260 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1262 const unsigned char *ptr = parent_chunk->data;
1263 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1265 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1267 while (ptr < end)
1269 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
1270 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
1272 ptr += sizeof(DWORD);
1273 chunk->data_size = *(DWORD *)ptr;
1274 ptr += sizeof(DWORD);
1275 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1276 chunk->data = ptr;
1278 return;
1281 ptr += sizeof(DWORD);
1282 ptr += *(DWORD *)ptr;
1283 ptr += sizeof(DWORD);
1289 * .ANI layout:
1291 * RIFF:'ACON' RIFF chunk
1292 * |- CHUNK:'anih' Header
1293 * |- CHUNK:'seq ' Sequence information (optional)
1294 * \- LIST:'fram' Frame list
1295 * |- CHUNK:icon Cursor frames
1296 * |- CHUNK:icon
1297 * |- ...
1298 * \- CHUNK:icon
1300 static HCURSOR load_ani( const LPBYTE bits, DWORD bits_size, INT width, INT height )
1302 int i;
1303 WORD max_count = 0;
1304 HCURSOR cursor;
1305 CURSORICONFILEDIR *dir = 0;
1306 ani_header header = {0};
1307 DWORD *frame_seq = 0;
1308 cursor_frame_t *frames;
1309 unsigned int frame_bits_size = 0;
1310 LPBYTE frame_bits = 0;
1311 POINT16 hotspot;
1313 riff_chunk_t root_chunk = { bits_size, bits };
1314 riff_chunk_t ACON_chunk = {0};
1315 riff_chunk_t anih_chunk = {0};
1316 riff_chunk_t fram_chunk = {0};
1317 const unsigned char *icon_chunk;
1318 const unsigned char *icon_data;
1320 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1322 if (!bits) return 0;
1324 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1325 if (!ACON_chunk.data)
1327 ERR("Failed to get root chunk.\n");
1328 return 0;
1331 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1332 if (!anih_chunk.data)
1334 ERR("Failed to get 'anih' chunk.\n");
1335 return 0;
1337 memcpy( &header, anih_chunk.data, sizeof(header) );
1338 dump_ani_header( &header );
1340 if (header.flags & ANI_FLAG_SEQUENCE)
1342 riff_chunk_t seq_chunk = {0};
1344 TRACE("Loading sequence data.\n");
1345 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1346 if (!seq_chunk.data)
1348 ERR("Failed to get 'seq ' chunk\n");
1349 return 0;
1351 frame_seq = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) * header.num_steps );
1352 memcpy( frame_seq, seq_chunk.data, sizeof(DWORD) * header.num_steps );
1355 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1356 if (!fram_chunk.data)
1358 ERR("Failed to get icon list\n");
1359 return 0;
1362 icon_chunk = fram_chunk.data;
1363 icon_data = icon_chunk + (2 * sizeof(DWORD));
1364 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1365 cursor = create_cursor( header.num_steps, (100 * header.display_rate) / 6 );
1366 frames = HeapAlloc( GetProcessHeap(), 0, header.num_frames * sizeof(cursor_frame_t) );
1368 for (i = 0; i < header.num_frames; ++i)
1370 WORD count;
1371 CURSORICONFILEDIRENTRY *entry;
1372 DWORD chunk_size = *(DWORD *)(icon_chunk + sizeof(DWORD));
1374 /* Read icon count, skip magic */
1375 memcpy( &count, icon_data + sizeof(DWORD), sizeof(WORD) );
1377 /* There's a decent chance the amount of entries will be the same for each icon */
1378 if (count > max_count)
1380 HeapFree( GetProcessHeap(), 0, dir );
1381 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1382 dir = HeapAlloc( GetProcessHeap(), 0, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1383 max_count = count;
1386 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1387 memcpy( dir, icon_data, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1388 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
1390 if (frame_bits_size < entry->dwDIBSize)
1392 frame_bits_size = entry->dwDIBSize;
1393 HeapFree( GetProcessHeap(), 0, frame_bits );
1394 frame_bits = HeapAlloc( GetProcessHeap(), 0, frame_bits_size );
1397 if (!header.width || !header.height)
1399 header.width = entry->bWidth;
1400 header.height = entry->bHeight;
1403 hotspot.x = entry->xHotspot;
1404 hotspot.y = entry->yHotspot;
1406 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1408 load_cursor_frame( frame_bits, entry->dwDIBSize, hotspot, 0x00030000, header.width, header.height, 0, &frames[i] );
1410 /* Advance to the next chunk */
1411 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1412 icon_data = icon_chunk + (2 * sizeof(DWORD));
1414 HeapFree( GetProcessHeap(), 0, dir );
1416 /* Set the frames in the correct sequence */
1417 for (i = 0; i < header.num_steps; ++i)
1419 int frame_idx = (frame_seq ? frame_seq[i] : i);
1420 set_cursor_frame( cursor, i, &frames[frame_idx] );
1423 /* Cleanup */
1424 for (i = 0; i < header.num_frames; ++i)
1426 HeapFree( GetProcessHeap(), 0, frames[i].bits );
1428 HeapFree( GetProcessHeap(), 0, frame_seq );
1429 HeapFree( GetProcessHeap(), 0, frames );
1431 return cursor;
1435 /**********************************************************************
1436 * CreateIconFromResourceEx (USER32.@)
1438 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1439 * with cbSize parameter as well.
1441 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1442 BOOL bIcon, DWORD dwVersion,
1443 INT width, INT height,
1444 UINT cFlag )
1446 POINT16 hotspot;
1447 HCURSOR cursor = create_cursor( 1, 0 );
1448 cursor_frame_t frame = {0};
1450 if (bIcon)
1452 hotspot.x = ICON_HOTSPOT;
1453 hotspot.y = ICON_HOTSPOT;
1455 else
1457 hotspot = *(POINT16 *)bits;
1458 bits = (LPBYTE)(((POINT16 *)bits) + 1);
1461 if (load_cursor_frame( bits, cbSize, hotspot, dwVersion, width, height, cFlag, &frame ))
1463 set_cursor_frame( cursor, 0, &frame );
1465 else
1467 destroy_cursor( cursor );
1468 cursor = 0;
1471 HeapFree( GetProcessHeap(), 0, frame.bits );
1473 return cursor;
1477 /**********************************************************************
1478 * CreateIconFromResource (USER32.@)
1480 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1481 BOOL bIcon, DWORD dwVersion)
1483 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1487 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1488 INT width, INT height, INT colors,
1489 BOOL fCursor, UINT loadflags)
1491 CURSORICONFILEDIRENTRY *entry;
1492 cursor_frame_t frame = {0};
1493 CURSORICONFILEDIR *dir;
1494 DWORD filesize = 0;
1495 HICON hIcon = 0;
1496 POINT16 hotspot;
1497 LPBYTE bits;
1499 TRACE("loading %s\n", debugstr_w( filename ));
1501 bits = map_fileW( filename, &filesize );
1502 if (!bits)
1503 return hIcon;
1505 /* If the data contains the magic for an .ICO it's an .ICO,
1506 * regardless of what fCursor says. */
1507 if (!memcmp( bits, "\x00\x00\x01\x00", 4 )) fCursor = FALSE;
1508 /* Same thing for .CUR */
1509 else if (!memcmp( bits, "\x00\x00\x02\x00", 4 )) fCursor = TRUE;
1510 /* Check for .ani. */
1511 else if (!memcmp( bits, "RIFF", 4 ))
1513 hIcon = load_ani( bits, filesize, width, height );
1514 goto end;
1517 dir = (CURSORICONFILEDIR*) bits;
1518 if ( filesize < sizeof(*dir) )
1519 goto end;
1521 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1522 goto end;
1524 if ( fCursor )
1525 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1526 else
1527 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1529 if ( !entry )
1530 goto end;
1532 /* check that we don't run off the end of the file */
1533 if ( entry->dwDIBOffset > filesize )
1534 goto end;
1535 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1536 goto end;
1538 if ( fCursor )
1540 hotspot.x = entry->xHotspot;
1541 hotspot.y = entry->yHotspot;
1543 else
1545 hotspot.x = ICON_HOTSPOT;
1546 hotspot.y = ICON_HOTSPOT;
1549 hIcon = create_cursor( 1, 0 );
1550 load_cursor_frame( &bits[entry->dwDIBOffset], entry->dwDIBSize, hotspot,
1551 0x00030000, width, height, loadflags, &frame );
1552 set_cursor_frame( hIcon, 0, &frame );
1553 HeapFree( GetProcessHeap(), 0, frame.bits );
1555 end:
1556 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1557 UnmapViewOfFile( bits );
1558 return hIcon;
1561 /**********************************************************************
1562 * CURSORICON_Load
1564 * Load a cursor or icon from resource or file.
1566 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1567 INT width, INT height, INT colors,
1568 BOOL fCursor, UINT loadflags)
1570 HANDLE handle = 0;
1571 HICON hIcon = 0;
1572 HRSRC hRsrc, hGroupRsrc;
1573 CURSORICONDIR *dir;
1574 CURSORICONDIRENTRY *dirEntry;
1575 LPBYTE bits;
1576 WORD wResId;
1577 DWORD dwBytesInRes;
1579 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1580 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1582 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1583 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1585 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1587 /* Normalize hInstance (must be uniquely represented for icon cache) */
1589 if (!HIWORD( hInstance ))
1590 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1592 /* Get directory resource ID */
1594 if (!(hRsrc = FindResourceW( hInstance, name,
1595 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1596 return 0;
1597 hGroupRsrc = hRsrc;
1599 /* Find the best entry in the directory */
1601 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1602 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
1603 if (fCursor)
1604 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1605 else
1606 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1607 if (!dirEntry) return 0;
1608 wResId = dirEntry->wResId;
1609 dwBytesInRes = dirEntry->dwBytesInRes;
1610 FreeResource( handle );
1612 /* Load the resource */
1614 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1615 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1617 /* If shared icon, check whether it was already loaded */
1618 if ( (loadflags & LR_SHARED)
1619 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1620 return hIcon;
1622 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1623 bits = (LPBYTE)LockResource( handle );
1624 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1625 !fCursor, 0x00030000, width, height, loadflags);
1626 FreeResource( handle );
1628 /* If shared icon, add to icon cache */
1630 if ( hIcon && (loadflags & LR_SHARED) )
1631 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1633 return hIcon;
1636 /***********************************************************************
1637 * CURSORICON_Copy
1639 * Make a copy of a cursor or icon.
1641 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1643 /* Should animated cursors be copyable like this as well? */
1644 HCURSOR new_cursor;
1645 cursor_frame_t frame;
1647 if (!hIcon || !get_cursor_frame( hIcon, 0, &frame ))
1649 return 0;
1652 new_cursor = create_cursor( 1, 0 );
1653 set_cursor_frame( new_cursor, 0, &frame );
1654 HeapFree( GetProcessHeap(), 0, frame.bits );
1656 return new_cursor;
1659 /*************************************************************************
1660 * CURSORICON_ExtCopy
1662 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1664 * PARAMS
1665 * Handle [I] handle to an Image
1666 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1667 * iDesiredCX [I] The Desired width of the Image
1668 * iDesiredCY [I] The desired height of the Image
1669 * nFlags [I] The flags from CopyImage
1671 * RETURNS
1672 * Success: The new handle of the Image
1674 * NOTES
1675 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1676 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1677 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1682 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1683 INT iDesiredCX, INT iDesiredCY,
1684 UINT nFlags)
1686 HICON hNew=0;
1688 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1689 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1691 if(hIcon == 0)
1693 return 0;
1696 /* Best Fit or Monochrome */
1697 if( (nFlags & LR_COPYFROMRESOURCE
1698 && (iDesiredCX > 0 || iDesiredCY > 0))
1699 || nFlags & LR_MONOCHROME)
1701 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1703 /* Not Found in Cache, then do a straight copy
1705 if(pIconCache == NULL)
1707 hNew = CURSORICON_Copy(0, hIcon);
1708 if(nFlags & LR_COPYFROMRESOURCE)
1710 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1713 else
1715 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1716 LPBYTE pBits;
1717 HANDLE hMem;
1718 HRSRC hRsrc;
1719 DWORD dwBytesInRes;
1720 WORD wResId;
1721 CURSORICONDIR *pDir;
1722 CURSORICONDIRENTRY *pDirEntry;
1723 BOOL bIsIcon = (nType == IMAGE_ICON);
1725 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1727 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1728 || (iDesiredCX == 0 && iDesiredCY == 0))
1730 iDesiredCY = GetSystemMetrics(bIsIcon ?
1731 SM_CYICON : SM_CYCURSOR);
1732 iDesiredCX = GetSystemMetrics(bIsIcon ?
1733 SM_CXICON : SM_CXCURSOR);
1736 /* Retrieve the CURSORICONDIRENTRY
1738 if (!(hMem = LoadResource( pIconCache->hModule ,
1739 pIconCache->hGroupRsrc)))
1741 return 0;
1743 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1745 return 0;
1748 /* Find Best Fit
1750 if(bIsIcon)
1752 pDirEntry = CURSORICON_FindBestIconRes(
1753 pDir, iDesiredCX, iDesiredCY, 256 );
1755 else
1757 pDirEntry = CURSORICON_FindBestCursorRes(
1758 pDir, iDesiredCX, iDesiredCY, 1);
1761 wResId = pDirEntry->wResId;
1762 dwBytesInRes = pDirEntry->dwBytesInRes;
1763 FreeResource(hMem);
1765 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1766 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1767 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1769 /* Get the Best Fit
1771 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1772 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1774 return 0;
1776 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1778 return 0;
1781 pBits = (LPBYTE)LockResource( hMem );
1783 if(nFlags & LR_DEFAULTSIZE)
1785 iTargetCY = GetSystemMetrics(SM_CYICON);
1786 iTargetCX = GetSystemMetrics(SM_CXICON);
1789 /* Create a New Icon with the proper dimension
1791 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1792 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1793 FreeResource(hMem);
1796 else hNew = CURSORICON_Copy(0, hIcon);
1797 return hNew;
1801 /***********************************************************************
1802 * CreateCursor (USER32.@)
1804 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1805 INT xHotSpot, INT yHotSpot,
1806 INT nWidth, INT nHeight,
1807 LPCVOID lpANDbits, LPCVOID lpXORbits )
1809 CURSORICONINFO info;
1811 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1812 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1814 info.ptHotSpot.x = xHotSpot;
1815 info.ptHotSpot.y = yHotSpot;
1816 info.nWidth = nWidth;
1817 info.nHeight = nHeight;
1818 info.nWidthBytes = 0;
1819 info.bPlanes = 1;
1820 info.bBitsPerPixel = 1;
1822 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1826 /***********************************************************************
1827 * CreateIcon (USER.407)
1829 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1830 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1831 LPCVOID lpANDbits, LPCVOID lpXORbits )
1833 CURSORICONINFO info;
1835 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1836 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1838 info.ptHotSpot.x = ICON_HOTSPOT;
1839 info.ptHotSpot.y = ICON_HOTSPOT;
1840 info.nWidth = nWidth;
1841 info.nHeight = nHeight;
1842 info.nWidthBytes = 0;
1843 info.bPlanes = bPlanes;
1844 info.bBitsPerPixel = bBitsPixel;
1846 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1850 /***********************************************************************
1851 * CreateIcon (USER32.@)
1853 * Creates an icon based on the specified bitmaps. The bitmaps must be
1854 * provided in a device dependent format and will be resized to
1855 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1856 * depth. The provided bitmaps must be top-down bitmaps.
1857 * Although Windows does not support 15bpp(*) this API must support it
1858 * for Winelib applications.
1860 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1861 * format!
1863 * RETURNS
1864 * Success: handle to an icon
1865 * Failure: NULL
1867 * FIXME: Do we need to resize the bitmaps?
1869 HICON WINAPI CreateIcon(
1870 HINSTANCE hInstance, /* [in] the application's hInstance */
1871 INT nWidth, /* [in] the width of the provided bitmaps */
1872 INT nHeight, /* [in] the height of the provided bitmaps */
1873 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1874 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1875 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1876 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1878 ICONINFO iinfo;
1879 HICON hIcon;
1881 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1882 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1884 iinfo.fIcon = TRUE;
1885 iinfo.xHotspot = ICON_HOTSPOT;
1886 iinfo.yHotspot = ICON_HOTSPOT;
1887 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1888 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1890 hIcon = CreateIconIndirect( &iinfo );
1892 DeleteObject( iinfo.hbmMask );
1893 DeleteObject( iinfo.hbmColor );
1895 return hIcon;
1899 /***********************************************************************
1900 * CreateCursorIconIndirect (USER.408)
1902 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1903 CURSORICONINFO *info,
1904 LPCVOID lpANDbits,
1905 LPCVOID lpXORbits )
1907 HCURSOR cursor;
1908 cursor_frame_t frame;
1909 int sizeAnd, sizeXor;
1911 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1912 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1913 sizeXor = info->nHeight * info->nWidthBytes;
1914 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1916 cursor = create_cursor( 1, 0 );
1917 frame.xhot = info->ptHotSpot.x;
1918 frame.yhot = info->ptHotSpot.y;
1919 frame.width = info->nWidth;
1920 frame.height = info->nHeight;
1921 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
1922 frame.xor_width_bytes = info->nWidthBytes;
1923 frame.planes = info->bPlanes;
1924 frame.bpp = info->bBitsPerPixel;
1925 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1926 CopyMemory( frame.bits, lpANDbits, sizeAnd );
1927 CopyMemory( frame.bits + sizeAnd, lpXORbits, sizeXor );
1928 set_cursor_frame( cursor, 0, &frame );
1929 HeapFree( GetProcessHeap(), 0, frame.bits );
1931 return HICON_16(cursor);
1935 /***********************************************************************
1936 * CopyIcon (USER.368)
1938 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1940 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1941 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1945 /***********************************************************************
1946 * CopyIcon (USER32.@)
1948 HICON WINAPI CopyIcon( HICON hIcon )
1950 TRACE_(icon)("%p\n", hIcon );
1951 return CURSORICON_Copy( 0, hIcon );
1955 /***********************************************************************
1956 * CopyCursor (USER.369)
1958 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1960 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1961 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1964 /**********************************************************************
1965 * DestroyIcon32 (USER.610)
1967 * This routine is actually exported from Win95 USER under the name
1968 * DestroyIcon32 ... The behaviour implemented here should mimic
1969 * the Win95 one exactly, especially the return values, which
1970 * depend on the setting of various flags.
1972 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1974 WORD retv;
1976 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1978 /* Check whether destroying active cursor */
1980 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1982 WARN_(cursor)("Destroying active cursor!\n" );
1983 return FALSE;
1986 /* Try shared cursor/icon first */
1988 if ( !(flags & CID_NONSHARED) )
1990 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1992 if ( count != -1 )
1993 return (flags & CID_WIN32)? TRUE : (count == 0);
1995 /* FIXME: OEM cursors/icons should be recognized */
1998 /* Now assume non-shared cursor/icon */
2000 retv = destroy_cursor( HCURSOR_32(handle) );
2001 return (flags & CID_RESOURCE)? retv : TRUE;
2004 /***********************************************************************
2005 * DestroyIcon (USER32.@)
2007 BOOL WINAPI DestroyIcon( HICON hIcon )
2009 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
2013 /***********************************************************************
2014 * DestroyCursor (USER32.@)
2016 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
2018 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
2022 /***********************************************************************
2023 * DrawIcon (USER32.@)
2025 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
2027 CURSORICONINFO *ptr;
2028 HDC hMemDC;
2029 HBITMAP hXorBits, hAndBits;
2030 COLORREF oldFg, oldBg;
2032 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
2034 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
2035 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2036 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
2037 (char *)(ptr+1) );
2038 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
2039 ptr->bBitsPerPixel, (char *)(ptr + 1)
2040 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
2041 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2042 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2044 if (hXorBits && hAndBits)
2046 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2047 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
2048 SelectObject( hMemDC, hXorBits );
2049 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
2050 SelectObject( hMemDC, hBitTemp );
2052 DeleteDC( hMemDC );
2053 if (hXorBits) DeleteObject( hXorBits );
2054 if (hAndBits) DeleteObject( hAndBits );
2055 GlobalUnlock16(HICON_16(hIcon));
2056 SetTextColor( hdc, oldFg );
2057 SetBkColor( hdc, oldBg );
2058 return TRUE;
2061 /***********************************************************************
2062 * DumpIcon (USER.459)
2064 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
2065 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
2067 CURSORICONINFO *info = MapSL( pInfo );
2068 int sizeAnd, sizeXor;
2070 if (!info) return 0;
2071 sizeXor = info->nHeight * info->nWidthBytes;
2072 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
2073 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
2074 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
2075 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
2076 return MAKELONG( sizeXor, sizeXor );
2080 /***********************************************************************
2081 * SetCursor (USER32.@)
2083 * Set the cursor shape.
2085 * RETURNS
2086 * A handle to the previous cursor shape.
2088 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
2090 struct user_thread_info *thread_info = get_user_thread_info();
2091 HCURSOR hOldCursor;
2093 if (hCursor == thread_info->cursor) return hCursor; /* No change */
2094 TRACE("%p\n", hCursor);
2095 hOldCursor = thread_info->cursor;
2096 thread_info->cursor = hCursor;
2097 /* Change the cursor shape only if it is visible */
2098 if (thread_info->cursor_count >= 0)
2100 cursor_t *cursor;
2102 update_cursor_32from16( hCursor );
2103 cursor = get_cursor_object( hCursor );
2104 USER_Driver->pSetCursor( cursor );
2105 destroy_cursor_object( cursor );
2107 return hOldCursor;
2110 /***********************************************************************
2111 * ShowCursor (USER32.@)
2113 INT WINAPI ShowCursor( BOOL bShow )
2115 struct user_thread_info *thread_info = get_user_thread_info();
2117 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
2119 if (bShow)
2121 if (++thread_info->cursor_count == 0) /* Show it */
2123 cursor_t *cursor;
2125 update_cursor_32from16( thread_info->cursor );
2126 cursor = get_cursor_object( thread_info->cursor );
2127 USER_Driver->pSetCursor( cursor );
2128 destroy_cursor_object( cursor );
2131 else
2133 if (--thread_info->cursor_count == -1) /* Hide it */
2134 USER_Driver->pSetCursor( NULL );
2136 return thread_info->cursor_count;
2139 /***********************************************************************
2140 * GetCursor (USER32.@)
2142 HCURSOR WINAPI GetCursor(void)
2144 return get_user_thread_info()->cursor;
2148 /***********************************************************************
2149 * ClipCursor (USER32.@)
2151 BOOL WINAPI ClipCursor( const RECT *rect )
2153 RECT virt;
2155 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
2156 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
2157 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
2158 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
2160 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
2161 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
2163 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
2164 CURSOR_ClipRect = virt;
2166 USER_Driver->pClipCursor( rect );
2167 return TRUE;
2171 /***********************************************************************
2172 * GetClipCursor (USER32.@)
2174 BOOL WINAPI GetClipCursor( RECT *rect )
2176 /* If this is first time - initialize the rect */
2177 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
2179 return CopyRect( rect, &CURSOR_ClipRect );
2183 /***********************************************************************
2184 * SetSystemCursor (USER32.@)
2186 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
2188 FIXME("(%p,%08x),stub!\n", hcur, id);
2189 return TRUE;
2193 /**********************************************************************
2194 * LookupIconIdFromDirectoryEx (USER.364)
2196 * FIXME: exact parameter sizes
2198 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
2199 INT16 width, INT16 height, UINT16 cFlag )
2201 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
2204 /**********************************************************************
2205 * LookupIconIdFromDirectoryEx (USER32.@)
2207 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
2208 INT width, INT height, UINT cFlag )
2210 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
2211 UINT retVal = 0;
2212 if( dir && !dir->idReserved && (dir->idType & 3) )
2214 CURSORICONDIRENTRY* entry;
2215 HDC hdc;
2216 UINT palEnts;
2217 int colors;
2218 hdc = GetDC(0);
2219 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
2220 if (palEnts == 0)
2221 palEnts = 256;
2222 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
2224 ReleaseDC(0, hdc);
2226 if( bIcon )
2227 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
2228 else
2229 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
2231 if( entry ) retVal = entry->wResId;
2233 else WARN_(cursor)("invalid resource directory\n");
2234 return retVal;
2237 /**********************************************************************
2238 * LookupIconIdFromDirectory (USER.?)
2240 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
2242 return LookupIconIdFromDirectoryEx16( dir, bIcon,
2243 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2244 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2247 /**********************************************************************
2248 * LookupIconIdFromDirectory (USER32.@)
2250 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
2252 return LookupIconIdFromDirectoryEx( dir, bIcon,
2253 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2254 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2257 /**********************************************************************
2258 * GetIconID (USER.455)
2260 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
2262 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
2264 TRACE_(cursor)("hRes=%04x, entries=%i\n",
2265 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
2267 switch(resType)
2269 case RT_CURSOR:
2270 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
2271 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
2272 case RT_ICON:
2273 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
2274 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
2275 default:
2276 WARN_(cursor)("invalid res type %d\n", resType );
2278 return 0;
2281 /**********************************************************************
2282 * LoadCursorIconHandler (USER.336)
2284 * Supposed to load resources of Windows 2.x applications.
2286 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
2288 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2289 hResource, hModule, hRsrc);
2290 return (HGLOBAL16)0;
2293 /**********************************************************************
2294 * LoadIconHandler (USER.456)
2296 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2298 LPBYTE bits = (LPBYTE)LockResource16( hResource );
2300 TRACE_(cursor)("hRes=%04x\n",hResource);
2302 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2303 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2306 /***********************************************************************
2307 * LoadCursorW (USER32.@)
2309 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2311 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2313 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2314 LR_SHARED | LR_DEFAULTSIZE );
2317 /***********************************************************************
2318 * LoadCursorA (USER32.@)
2320 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2322 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2324 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2325 LR_SHARED | LR_DEFAULTSIZE );
2328 /***********************************************************************
2329 * LoadCursorFromFileW (USER32.@)
2331 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2333 TRACE("%s\n", debugstr_w(name));
2335 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2336 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2339 /***********************************************************************
2340 * LoadCursorFromFileA (USER32.@)
2342 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2344 TRACE("%s\n", debugstr_a(name));
2346 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2347 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2350 /***********************************************************************
2351 * LoadIconW (USER32.@)
2353 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2355 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2357 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2358 LR_SHARED | LR_DEFAULTSIZE );
2361 /***********************************************************************
2362 * LoadIconA (USER32.@)
2364 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2366 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2368 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2369 LR_SHARED | LR_DEFAULTSIZE );
2372 /**********************************************************************
2373 * GetIconInfo (USER32.@)
2375 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2377 CURSORICONINFO *ciconinfo;
2378 INT height;
2380 ciconinfo = GlobalLock16(HICON_16(hIcon));
2381 if (!ciconinfo)
2382 return FALSE;
2384 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2385 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2387 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2388 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2390 iconinfo->fIcon = TRUE;
2391 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2392 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2394 else
2396 iconinfo->fIcon = FALSE;
2397 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2398 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2401 height = ciconinfo->nHeight;
2403 if (ciconinfo->bBitsPerPixel > 1)
2405 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2406 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2407 (char *)(ciconinfo + 1)
2408 + ciconinfo->nHeight *
2409 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2411 else
2413 iconinfo->hbmColor = 0;
2414 height *= 2;
2417 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2418 1, 1, (char *)(ciconinfo + 1));
2420 GlobalUnlock16(HICON_16(hIcon));
2422 return TRUE;
2425 /**********************************************************************
2426 * CreateIconIndirect (USER32.@)
2428 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2430 HCURSOR cursor;
2431 cursor_frame_t frame;
2432 BITMAP bmpXor,bmpAnd;
2433 int sizeXor,sizeAnd;
2435 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2436 iconinfo->hbmColor, iconinfo->hbmMask,
2437 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2439 if (!iconinfo->hbmMask) return 0;
2441 if (iconinfo->hbmColor)
2443 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2444 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2445 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2446 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2448 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2449 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2450 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2451 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2453 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
2454 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2456 cursor = create_cursor( 1, 0 );
2458 /* If we are creating an icon, the hotspot is unused */
2459 if (iconinfo->fIcon)
2461 frame.xhot = ICON_HOTSPOT;
2462 frame.yhot = ICON_HOTSPOT;
2464 else
2466 frame.xhot = iconinfo->xHotspot;
2467 frame.yhot = iconinfo->yHotspot;
2470 if (iconinfo->hbmColor)
2472 frame.width = bmpXor.bmWidth;
2473 frame.height = bmpXor.bmHeight;
2474 frame.and_width_bytes = bmpAnd.bmWidthBytes;
2475 frame.xor_width_bytes = bmpXor.bmWidthBytes;
2476 frame.planes = bmpXor.bmPlanes;
2477 frame.bpp = bmpXor.bmBitsPixel;
2479 else
2481 frame.width = bmpAnd.bmWidth;
2482 frame.height = bmpAnd.bmHeight / 2;
2483 frame.and_width_bytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2484 frame.xor_width_bytes = 0;
2485 frame.planes = 1;
2486 frame.bpp = 1;
2489 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
2491 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2492 if (bmpAnd.bmBitsPixel == 1)
2494 GetBitmapBits( iconinfo->hbmMask, sizeAnd, frame.bits );
2496 else
2498 HDC hdc, hdc_mem;
2499 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2501 hdc = GetDC( 0 );
2502 hdc_mem = CreateCompatibleDC( hdc );
2504 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2506 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2507 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2509 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2511 SelectObject( hdc, hbmp_old );
2512 SelectObject( hdc_mem, hbmp_mem_old );
2514 DeleteDC( hdc_mem );
2515 ReleaseDC( 0, hdc );
2517 GetBitmapBits( hbmp_mono, sizeAnd, frame.bits );
2518 DeleteObject( hbmp_mono );
2521 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, frame.bits + sizeAnd );
2522 set_cursor_frame( cursor, 0, &frame );
2523 HeapFree( GetProcessHeap(), 0, frame.bits );
2525 return cursor;
2528 /******************************************************************************
2529 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2531 * NOTES
2532 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2534 * PARAMS
2535 * hdc [I] Handle to device context
2536 * x0 [I] X coordinate of upper left corner
2537 * y0 [I] Y coordinate of upper left corner
2538 * hIcon [I] Handle to icon to draw
2539 * cxWidth [I] Width of icon
2540 * cyWidth [I] Height of icon
2541 * istep [I] Index of frame in animated cursor
2542 * hbr [I] Handle to background brush
2543 * flags [I] Icon-drawing flags
2545 * RETURNS
2546 * Success: TRUE
2547 * Failure: FALSE
2549 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2550 INT cxWidth, INT cyWidth, UINT istep,
2551 HBRUSH hbr, UINT flags )
2553 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
2554 HDC hDC_off = 0, hMemDC;
2555 BOOL result = FALSE, DoOffscreen;
2556 HBITMAP hB_off = 0, hOld = 0;
2558 if (!ptr) return FALSE;
2559 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2560 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2562 hMemDC = CreateCompatibleDC (hdc);
2563 if (istep)
2564 FIXME_(icon)("Ignoring istep=%d\n", istep);
2565 if (flags & DI_COMPAT)
2566 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
2568 if (!flags) {
2569 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2570 flags = DI_NORMAL;
2573 /* Calculate the size of the destination image. */
2574 if (cxWidth == 0)
2576 if (flags & DI_DEFAULTSIZE)
2577 cxWidth = GetSystemMetrics (SM_CXICON);
2578 else
2579 cxWidth = ptr->nWidth;
2581 if (cyWidth == 0)
2583 if (flags & DI_DEFAULTSIZE)
2584 cyWidth = GetSystemMetrics (SM_CYICON);
2585 else
2586 cyWidth = ptr->nHeight;
2589 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2591 if (DoOffscreen) {
2592 RECT r;
2594 r.left = 0;
2595 r.top = 0;
2596 r.right = cxWidth;
2597 r.bottom = cxWidth;
2599 hDC_off = CreateCompatibleDC(hdc);
2600 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2601 if (hDC_off && hB_off) {
2602 hOld = SelectObject(hDC_off, hB_off);
2603 FillRect(hDC_off, &r, hbr);
2607 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2609 HBITMAP hXorBits, hAndBits;
2610 COLORREF oldFg, oldBg;
2611 INT nStretchMode;
2613 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2615 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2616 ptr->bPlanes, ptr->bBitsPerPixel,
2617 (char *)(ptr + 1)
2618 + ptr->nHeight *
2619 get_bitmap_width_bytes(ptr->nWidth,1) );
2620 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2621 1, 1, (char *)(ptr+1) );
2622 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2623 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2625 if (hXorBits && hAndBits)
2627 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2628 if (flags & DI_MASK)
2630 if (DoOffscreen)
2631 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2632 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2633 else
2634 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2635 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2637 SelectObject( hMemDC, hXorBits );
2638 if (flags & DI_IMAGE)
2640 if (DoOffscreen)
2641 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2642 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2643 else
2644 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2645 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2647 SelectObject( hMemDC, hBitTemp );
2648 result = TRUE;
2651 SetTextColor( hdc, oldFg );
2652 SetBkColor( hdc, oldBg );
2653 if (hXorBits) DeleteObject( hXorBits );
2654 if (hAndBits) DeleteObject( hAndBits );
2655 SetStretchBltMode (hdc, nStretchMode);
2656 if (DoOffscreen) {
2657 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2658 SelectObject(hDC_off, hOld);
2661 if (hMemDC) DeleteDC( hMemDC );
2662 if (hDC_off) DeleteDC(hDC_off);
2663 if (hB_off) DeleteObject(hB_off);
2664 GlobalUnlock16(HICON_16(hIcon));
2665 return result;
2668 /***********************************************************************
2669 * DIB_FixColorsToLoadflags
2671 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2672 * are in loadflags
2674 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2676 int colors;
2677 COLORREF c_W, c_S, c_F, c_L, c_C;
2678 int incr,i;
2679 RGBQUAD *ptr;
2680 int bitmap_type;
2681 LONG width;
2682 LONG height;
2683 WORD bpp;
2684 DWORD compr;
2686 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2688 WARN_(resource)("Invalid bitmap\n");
2689 return;
2692 if (bpp > 8) return;
2694 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2696 incr = 3;
2697 colors = 1 << bpp;
2699 else
2701 incr = 4;
2702 colors = bmi->bmiHeader.biClrUsed;
2703 if (colors > 256) colors = 256;
2704 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2707 c_W = GetSysColor(COLOR_WINDOW);
2708 c_S = GetSysColor(COLOR_3DSHADOW);
2709 c_F = GetSysColor(COLOR_3DFACE);
2710 c_L = GetSysColor(COLOR_3DLIGHT);
2712 if (loadflags & LR_LOADTRANSPARENT) {
2713 switch (bpp) {
2714 case 1: pix = pix >> 7; break;
2715 case 4: pix = pix >> 4; break;
2716 case 8: break;
2717 default:
2718 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2719 return;
2721 if (pix >= colors) {
2722 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2723 return;
2725 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2726 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2727 ptr->rgbBlue = GetBValue(c_W);
2728 ptr->rgbGreen = GetGValue(c_W);
2729 ptr->rgbRed = GetRValue(c_W);
2731 if (loadflags & LR_LOADMAP3DCOLORS)
2732 for (i=0; i<colors; i++) {
2733 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2734 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2735 if (c_C == RGB(128, 128, 128)) {
2736 ptr->rgbRed = GetRValue(c_S);
2737 ptr->rgbGreen = GetGValue(c_S);
2738 ptr->rgbBlue = GetBValue(c_S);
2739 } else if (c_C == RGB(192, 192, 192)) {
2740 ptr->rgbRed = GetRValue(c_F);
2741 ptr->rgbGreen = GetGValue(c_F);
2742 ptr->rgbBlue = GetBValue(c_F);
2743 } else if (c_C == RGB(223, 223, 223)) {
2744 ptr->rgbRed = GetRValue(c_L);
2745 ptr->rgbGreen = GetGValue(c_L);
2746 ptr->rgbBlue = GetBValue(c_L);
2752 /**********************************************************************
2753 * BITMAP_Load
2755 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2756 INT desiredx, INT desiredy, UINT loadflags )
2758 HBITMAP hbitmap = 0, orig_bm;
2759 HRSRC hRsrc;
2760 HGLOBAL handle;
2761 char *ptr = NULL;
2762 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2763 int size;
2764 BYTE pix;
2765 char *bits;
2766 LONG width, height, new_width, new_height;
2767 WORD bpp_dummy;
2768 DWORD compr_dummy;
2769 INT bm_type;
2770 HDC screen_mem_dc = NULL;
2772 if (!(loadflags & LR_LOADFROMFILE))
2774 if (!instance)
2776 /* OEM bitmap: try to load the resource from user32.dll */
2777 instance = user32_module;
2780 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2781 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2783 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2785 else
2787 if (!(ptr = map_fileW( name, NULL ))) return 0;
2788 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2791 size = bitmap_info_size(info, DIB_RGB_COLORS);
2792 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2793 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2795 if (!fix_info || !scaled_info) goto end;
2796 memcpy(fix_info, info, size);
2798 pix = *((LPBYTE)info + size);
2799 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2801 memcpy(scaled_info, fix_info, size);
2802 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2803 &bpp_dummy, &compr_dummy);
2804 if(desiredx != 0)
2805 new_width = desiredx;
2806 else
2807 new_width = width;
2809 if(desiredy != 0)
2810 new_height = height > 0 ? desiredy : -desiredy;
2811 else
2812 new_height = height;
2814 if(bm_type == 0)
2816 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2817 core->bcWidth = new_width;
2818 core->bcHeight = new_height;
2820 else
2822 scaled_info->bmiHeader.biWidth = new_width;
2823 scaled_info->bmiHeader.biHeight = new_height;
2826 if (new_height < 0) new_height = -new_height;
2828 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2829 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2831 bits = (char *)info + size;
2833 if (loadflags & LR_CREATEDIBSECTION)
2835 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2836 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2838 else
2840 if (is_dib_monochrome(fix_info))
2841 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2842 else
2843 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2846 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2847 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2848 SelectObject(screen_mem_dc, orig_bm);
2850 end:
2851 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2852 HeapFree(GetProcessHeap(), 0, scaled_info);
2853 HeapFree(GetProcessHeap(), 0, fix_info);
2854 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2856 return hbitmap;
2859 /**********************************************************************
2860 * LoadImageA (USER32.@)
2862 * See LoadImageW.
2864 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2865 INT desiredx, INT desiredy, UINT loadflags)
2867 HANDLE res;
2868 LPWSTR u_name;
2870 if (!HIWORD(name))
2871 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2873 __TRY {
2874 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2875 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2876 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2878 __EXCEPT_PAGE_FAULT {
2879 SetLastError( ERROR_INVALID_PARAMETER );
2880 return 0;
2882 __ENDTRY
2883 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2884 HeapFree(GetProcessHeap(), 0, u_name);
2885 return res;
2889 /******************************************************************************
2890 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2892 * PARAMS
2893 * hinst [I] Handle of instance that contains image
2894 * name [I] Name of image
2895 * type [I] Type of image
2896 * desiredx [I] Desired width
2897 * desiredy [I] Desired height
2898 * loadflags [I] Load flags
2900 * RETURNS
2901 * Success: Handle to newly loaded image
2902 * Failure: NULL
2904 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2906 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2907 INT desiredx, INT desiredy, UINT loadflags )
2909 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2910 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2912 if (loadflags & LR_DEFAULTSIZE) {
2913 if (type == IMAGE_ICON) {
2914 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2915 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2916 } else if (type == IMAGE_CURSOR) {
2917 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2918 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2921 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2922 switch (type) {
2923 case IMAGE_BITMAP:
2924 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2926 case IMAGE_ICON:
2927 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2928 if (screen_dc)
2930 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2931 if (palEnts == 0) palEnts = 256;
2932 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2933 palEnts, FALSE, loadflags);
2935 break;
2937 case IMAGE_CURSOR:
2938 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2939 1, TRUE, loadflags);
2941 return 0;
2944 /******************************************************************************
2945 * CopyImage (USER32.@) Creates new image and copies attributes to it
2947 * PARAMS
2948 * hnd [I] Handle to image to copy
2949 * type [I] Type of image to copy
2950 * desiredx [I] Desired width of new image
2951 * desiredy [I] Desired height of new image
2952 * flags [I] Copy flags
2954 * RETURNS
2955 * Success: Handle to newly created image
2956 * Failure: NULL
2958 * BUGS
2959 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2960 * all other versions (95/2000/XP have been tested) ignore it.
2962 * NOTES
2963 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2964 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2965 * the copy will have the same depth as the screen.
2966 * The content of the image will only be copied if the bit depth of the
2967 * original image is compatible with the bit depth of the screen, or
2968 * if the source is a DIB section.
2969 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2971 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2972 INT desiredy, UINT flags )
2974 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2975 hnd, type, desiredx, desiredy, flags);
2977 switch (type)
2979 case IMAGE_BITMAP:
2981 HBITMAP res = NULL;
2982 DIBSECTION ds;
2983 int objSize;
2984 BITMAPINFO * bi;
2986 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2987 if (!objSize) return 0;
2988 if ((desiredx < 0) || (desiredy < 0)) return 0;
2990 if (flags & LR_COPYFROMRESOURCE)
2992 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2995 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2996 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2998 /* Allocate memory for a BITMAPINFOHEADER structure and a
2999 color table. The maximum number of colors in a color table
3000 is 256 which corresponds to a bitmap with depth 8.
3001 Bitmaps with higher depths don't have color tables. */
3002 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3003 if (!bi) return 0;
3005 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3006 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
3007 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3008 bi->bmiHeader.biCompression = BI_RGB;
3010 if (flags & LR_CREATEDIBSECTION)
3012 /* Create a DIB section. LR_MONOCHROME is ignored */
3013 void * bits;
3014 HDC dc = CreateCompatibleDC(NULL);
3016 if (objSize == sizeof(DIBSECTION))
3018 /* The source bitmap is a DIB.
3019 Get its attributes to create an exact copy */
3020 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
3023 /* Get the color table or the color masks */
3024 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3026 bi->bmiHeader.biWidth = desiredx;
3027 bi->bmiHeader.biHeight = desiredy;
3028 bi->bmiHeader.biSizeImage = 0;
3030 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
3031 DeleteDC(dc);
3033 else
3035 /* Create a device-dependent bitmap */
3037 BOOL monochrome = (flags & LR_MONOCHROME);
3039 if (objSize == sizeof(DIBSECTION))
3041 /* The source bitmap is a DIB section.
3042 Get its attributes */
3043 HDC dc = CreateCompatibleDC(NULL);
3044 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3045 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3046 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3047 DeleteDC(dc);
3049 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
3051 /* Look if the colors of the DIB are black and white */
3053 monochrome =
3054 (bi->bmiColors[0].rgbRed == 0xff
3055 && bi->bmiColors[0].rgbGreen == 0xff
3056 && bi->bmiColors[0].rgbBlue == 0xff
3057 && bi->bmiColors[0].rgbReserved == 0
3058 && bi->bmiColors[1].rgbRed == 0
3059 && bi->bmiColors[1].rgbGreen == 0
3060 && bi->bmiColors[1].rgbBlue == 0
3061 && bi->bmiColors[1].rgbReserved == 0)
3063 (bi->bmiColors[0].rgbRed == 0
3064 && bi->bmiColors[0].rgbGreen == 0
3065 && bi->bmiColors[0].rgbBlue == 0
3066 && bi->bmiColors[0].rgbReserved == 0
3067 && bi->bmiColors[1].rgbRed == 0xff
3068 && bi->bmiColors[1].rgbGreen == 0xff
3069 && bi->bmiColors[1].rgbBlue == 0xff
3070 && bi->bmiColors[1].rgbReserved == 0);
3073 else if (!monochrome)
3075 monochrome = ds.dsBm.bmBitsPixel == 1;
3078 if (monochrome)
3080 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
3082 else
3084 HDC screenDC = GetDC(NULL);
3085 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
3086 ReleaseDC(NULL, screenDC);
3090 if (res)
3092 /* Only copy the bitmap if it's a DIB section or if it's
3093 compatible to the screen */
3094 BOOL copyContents;
3096 if (objSize == sizeof(DIBSECTION))
3098 copyContents = TRUE;
3100 else
3102 HDC screenDC = GetDC(NULL);
3103 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
3104 ReleaseDC(NULL, screenDC);
3106 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
3109 if (copyContents)
3111 /* The source bitmap may already be selected in a device context,
3112 use GetDIBits/StretchDIBits and not StretchBlt */
3114 HDC dc;
3115 void * bits;
3117 dc = CreateCompatibleDC(NULL);
3119 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
3120 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
3121 bi->bmiHeader.biSizeImage = 0;
3122 bi->bmiHeader.biClrUsed = 0;
3123 bi->bmiHeader.biClrImportant = 0;
3125 /* Fill in biSizeImage */
3126 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3127 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
3129 if (bits)
3131 HBITMAP oldBmp;
3133 /* Get the image bits of the source bitmap */
3134 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
3136 /* Copy it to the destination bitmap */
3137 oldBmp = SelectObject(dc, res);
3138 StretchDIBits(dc, 0, 0, desiredx, desiredy,
3139 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
3140 bits, bi, DIB_RGB_COLORS, SRCCOPY);
3141 SelectObject(dc, oldBmp);
3143 HeapFree(GetProcessHeap(), 0, bits);
3146 DeleteDC(dc);
3149 if (flags & LR_COPYDELETEORG)
3151 DeleteObject(hnd);
3154 HeapFree(GetProcessHeap(), 0, bi);
3155 return res;
3157 case IMAGE_ICON:
3158 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
3159 case IMAGE_CURSOR:
3160 /* Should call CURSORICON_ExtCopy but more testing
3161 * needs to be done before we change this
3163 if (flags) FIXME("Flags are ignored\n");
3164 return CopyCursor(hnd);
3166 return 0;
3170 /******************************************************************************
3171 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3173 * RETURNS
3174 * Success: Handle to specified bitmap
3175 * Failure: NULL
3177 HBITMAP WINAPI LoadBitmapW(
3178 HINSTANCE instance, /* [in] Handle to application instance */
3179 LPCWSTR name) /* [in] Address of bitmap resource name */
3181 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3184 /**********************************************************************
3185 * LoadBitmapA (USER32.@)
3187 * See LoadBitmapW.
3189 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3191 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );