push f1f5ea25daee4e8628ffc746ffc7f8614d5c24aa
[wine/hacks.git] / dlls / user32 / cursoricon.c
blobfccf6231e4e250ab572b4fded2beba48de679d88
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 if (hotspot.x == ICON_HOTSPOT && hotspot.y == ICON_HOTSPOT)
1029 bmi = (BITMAPINFO *)bits;
1030 else /* get the hotspot */
1032 POINT16 *pt = (POINT16 *)bits;
1033 hotspot = *pt;
1034 bmi = (BITMAPINFO *)(pt + 1);
1037 /* Check bitmap header */
1039 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
1040 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
1041 bmi->bmiHeader.biCompression != BI_RGB) )
1043 WARN_(cursor)("\tinvalid resource bitmap header.\n");
1044 return FALSE;
1047 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
1049 if (!width) width = bmi->bmiHeader.biWidth;
1050 if (!height) height = bmi->bmiHeader.biHeight/2;
1051 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
1052 (bmi->bmiHeader.biWidth != width);
1054 /* Scale the hotspot */
1055 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
1057 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
1058 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biWidth / 2);
1061 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1062 if (screen_dc)
1064 BITMAPINFO* pInfo;
1066 /* Make sure we have room for the monochrome bitmap later on.
1067 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1068 * up to and including the biBitCount. In-memory icon resource
1069 * format is as follows:
1071 * BITMAPINFOHEADER icHeader // DIB header
1072 * RGBQUAD icColors[] // Color table
1073 * BYTE icXOR[] // DIB bits for XOR mask
1074 * BYTE icAND[] // DIB bits for AND mask
1077 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
1078 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
1080 memcpy( pInfo, bmi, size );
1081 pInfo->bmiHeader.biHeight /= 2;
1083 /* Create the XOR bitmap */
1085 if (DoStretch) {
1086 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1087 if(hXorBits)
1089 HBITMAP hOld;
1090 BOOL res = FALSE;
1092 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1093 if (hdcMem) {
1094 hOld = SelectObject(hdcMem, hXorBits);
1095 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1096 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
1097 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
1098 SelectObject(hdcMem, hOld);
1100 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
1102 } else {
1103 if (is_dib_monochrome(bmi)) {
1104 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1105 SetDIBits(screen_dc, hXorBits, 0, height,
1106 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1107 } else if (bmi->bmiHeader.biBitCount == 32) {
1108 hXorBits = CreateDIBSection(screen_dc, pInfo, DIB_RGB_COLORS, NULL, NULL, 0);
1109 SetDIBits(screen_dc, hXorBits, 0, height,
1110 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1112 else
1113 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
1114 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
1117 if( hXorBits )
1119 char* xbits = (char *)bmi + size +
1120 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1121 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1123 pInfo->bmiHeader.biBitCount = 1;
1124 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1126 RGBQUAD *rgb = pInfo->bmiColors;
1128 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
1129 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1130 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1131 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1133 else
1135 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
1137 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1138 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1141 /* Create the AND bitmap */
1143 if (DoStretch) {
1144 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
1145 HBITMAP hOld;
1146 BOOL res = FALSE;
1148 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
1149 if (hdcMem) {
1150 hOld = SelectObject(hdcMem, hAndBits);
1151 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
1152 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1153 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1154 SelectObject(hdcMem, hOld);
1156 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
1158 } else {
1159 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1161 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
1162 xbits, pInfo, DIB_RGB_COLORS);
1165 if( !hAndBits ) DeleteObject( hXorBits );
1167 HeapFree( GetProcessHeap(), 0, pInfo );
1171 if( !hXorBits || !hAndBits )
1173 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1174 return FALSE;
1177 /* Setup a cursor frame, send it to the server */
1178 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1179 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1180 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1181 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1183 frame->xhot = hotspot.x;
1184 frame->yhot = hotspot.y;
1185 frame->width = bmpXor.bmWidth;
1186 frame->height = bmpXor.bmHeight;
1187 frame->and_width_bytes = bmpAnd.bmWidthBytes;
1188 frame->xor_width_bytes = bmpXor.bmWidthBytes;
1189 frame->planes = bmpXor.bmPlanes;
1190 frame->bpp = bmpXor.bmBitsPixel;
1191 frame->bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1192 GetBitmapBits( hAndBits, sizeAnd, frame->bits );
1193 GetBitmapBits( hXorBits, sizeXor, frame->bits + sizeAnd );
1195 DeleteObject( hAndBits );
1196 DeleteObject( hXorBits );
1198 return TRUE;
1201 /**********************************************************************
1202 * .ANI cursor support
1204 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1205 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1206 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1208 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1209 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1210 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1211 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1212 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1213 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1215 #define ANI_FLAG_ICON 0x1
1216 #define ANI_FLAG_SEQUENCE 0x2
1218 typedef struct {
1219 DWORD header_size;
1220 DWORD num_frames;
1221 DWORD num_steps;
1222 DWORD width;
1223 DWORD height;
1224 DWORD bpp;
1225 DWORD num_planes;
1226 DWORD display_rate;
1227 DWORD flags;
1228 } ani_header;
1230 typedef struct {
1231 DWORD data_size;
1232 const unsigned char *data;
1233 } riff_chunk_t;
1235 static void dump_ani_header( const ani_header *header )
1237 TRACE(" header size: %d\n", header->header_size);
1238 TRACE(" frames: %d\n", header->num_frames);
1239 TRACE(" steps: %d\n", header->num_steps);
1240 TRACE(" width: %d\n", header->width);
1241 TRACE(" height: %d\n", header->height);
1242 TRACE(" bpp: %d\n", header->bpp);
1243 TRACE(" planes: %d\n", header->num_planes);
1244 TRACE(" display rate: %d\n", header->display_rate);
1245 TRACE(" flags: 0x%08x\n", header->flags);
1250 * RIFF:
1251 * DWORD "RIFF"
1252 * DWORD size
1253 * DWORD riff_id
1254 * BYTE[] data
1256 * LIST:
1257 * DWORD "LIST"
1258 * DWORD size
1259 * DWORD list_id
1260 * BYTE[] data
1262 * CHUNK:
1263 * DWORD chunk_id
1264 * DWORD size
1265 * BYTE[] data
1267 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1269 const unsigned char *ptr = parent_chunk->data;
1270 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1272 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1274 while (ptr < end)
1276 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
1277 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
1279 ptr += sizeof(DWORD);
1280 chunk->data_size = *(DWORD *)ptr;
1281 ptr += sizeof(DWORD);
1282 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1283 chunk->data = ptr;
1285 return;
1288 ptr += sizeof(DWORD);
1289 ptr += *(DWORD *)ptr;
1290 ptr += sizeof(DWORD);
1296 * .ANI layout:
1298 * RIFF:'ACON' RIFF chunk
1299 * |- CHUNK:'anih' Header
1300 * |- CHUNK:'seq ' Sequence information (optional)
1301 * \- LIST:'fram' Frame list
1302 * |- CHUNK:icon Cursor frames
1303 * |- CHUNK:icon
1304 * |- ...
1305 * \- CHUNK:icon
1307 static HCURSOR load_ani( const LPBYTE bits, DWORD bits_size, INT width, INT height )
1309 int i;
1310 WORD max_count = 0;
1311 HCURSOR cursor;
1312 CURSORICONFILEDIR *dir = 0;
1313 ani_header header = {0};
1314 DWORD *frame_seq = 0;
1315 cursor_frame_t *frames;
1316 unsigned int frame_bits_size = 0;
1317 LPBYTE frame_bits = 0;
1318 POINT16 hotspot;
1320 riff_chunk_t root_chunk = { bits_size, bits };
1321 riff_chunk_t ACON_chunk = {0};
1322 riff_chunk_t anih_chunk = {0};
1323 riff_chunk_t fram_chunk = {0};
1324 const unsigned char *icon_chunk;
1325 const unsigned char *icon_data;
1327 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1329 if (!bits) return 0;
1331 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1332 if (!ACON_chunk.data)
1334 ERR("Failed to get root chunk.\n");
1335 return 0;
1338 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1339 if (!anih_chunk.data)
1341 ERR("Failed to get 'anih' chunk.\n");
1342 return 0;
1344 memcpy( &header, anih_chunk.data, sizeof(header) );
1345 dump_ani_header( &header );
1347 if (header.flags & ANI_FLAG_SEQUENCE)
1349 riff_chunk_t seq_chunk = {0};
1351 TRACE("Loading sequence data.\n");
1352 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1353 if (!seq_chunk.data)
1355 ERR("Failed to get 'seq ' chunk\n");
1356 return 0;
1358 frame_seq = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) * header.num_steps );
1359 memcpy( frame_seq, seq_chunk.data, sizeof(DWORD) * header.num_steps );
1362 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1363 if (!fram_chunk.data)
1365 ERR("Failed to get icon list\n");
1366 return 0;
1369 icon_chunk = fram_chunk.data;
1370 icon_data = icon_chunk + (2 * sizeof(DWORD));
1371 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1372 cursor = create_cursor( header.num_steps, (100 * header.display_rate) / 6 );
1373 frames = HeapAlloc( GetProcessHeap(), 0, header.num_frames * sizeof(cursor_frame_t) );
1375 for (i = 0; i < header.num_frames; ++i)
1377 WORD count;
1378 CURSORICONFILEDIRENTRY *entry;
1379 DWORD chunk_size = *(DWORD *)(icon_chunk + sizeof(DWORD));
1381 /* Read icon count, skip magic */
1382 memcpy( &count, icon_data + sizeof(DWORD), sizeof(WORD) );
1384 /* There's a decent chance the amount of entries will be the same for each icon */
1385 if (count > max_count)
1387 HeapFree( GetProcessHeap(), 0, dir );
1388 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1389 dir = HeapAlloc( GetProcessHeap(), 0, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1390 max_count = count;
1393 /* sizeof(CURSORICONFILEDIRENTRY) for each entry, +6 for magic & count */
1394 memcpy( dir, icon_data, (count * sizeof(CURSORICONFILEDIRENTRY)) + 6 );
1395 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
1397 if (frame_bits_size < entry->dwDIBSize)
1399 frame_bits_size = entry->dwDIBSize;
1400 HeapFree( GetProcessHeap(), 0, frame_bits );
1401 frame_bits = HeapAlloc( GetProcessHeap(), 0, frame_bits_size );
1404 if (!header.width || !header.height)
1406 header.width = entry->bWidth;
1407 header.height = entry->bHeight;
1410 hotspot.x = entry->xHotspot;
1411 hotspot.y = entry->yHotspot;
1413 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1415 load_cursor_frame( frame_bits, entry->dwDIBSize, hotspot, 0x00030000, header.width, header.height, 0, &frames[i] );
1417 /* Advance to the next chunk */
1418 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1419 icon_data = icon_chunk + (2 * sizeof(DWORD));
1421 HeapFree( GetProcessHeap(), 0, dir );
1423 /* Set the frames in the correct sequence */
1424 for (i = 0; i < header.num_steps; ++i)
1426 int frame_idx = (frame_seq ? frame_seq[i] : i);
1427 set_cursor_frame( cursor, i, &frames[frame_idx] );
1430 /* Cleanup */
1431 for (i = 0; i < header.num_frames; ++i)
1433 HeapFree( GetProcessHeap(), 0, frames[i].bits );
1435 HeapFree( GetProcessHeap(), 0, frame_seq );
1436 HeapFree( GetProcessHeap(), 0, frames );
1438 return cursor;
1442 /**********************************************************************
1443 * CreateIconFromResourceEx (USER32.@)
1445 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1446 * with cbSize parameter as well.
1448 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1449 BOOL bIcon, DWORD dwVersion,
1450 INT width, INT height,
1451 UINT cFlag )
1453 POINT16 hotspot;
1454 HCURSOR cursor = create_cursor( 1, 0 );
1455 cursor_frame_t frame = {0};
1457 if (bIcon)
1459 hotspot.x = ICON_HOTSPOT;
1460 hotspot.y = ICON_HOTSPOT;
1462 else
1464 hotspot = *(POINT16 *)bits;
1465 bits = (LPBYTE)(((POINT16 *)bits) + 1);
1468 if (load_cursor_frame( bits, cbSize, hotspot, dwVersion, width, height, cFlag, &frame ))
1470 set_cursor_frame( cursor, 0, &frame );
1472 else
1474 destroy_cursor( cursor );
1475 cursor = 0;
1478 HeapFree( GetProcessHeap(), 0, frame.bits );
1480 return cursor;
1484 /**********************************************************************
1485 * CreateIconFromResource (USER32.@)
1487 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1488 BOOL bIcon, DWORD dwVersion)
1490 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1494 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1495 INT width, INT height, INT colors,
1496 BOOL fCursor, UINT loadflags)
1498 CURSORICONFILEDIRENTRY *entry;
1499 cursor_frame_t frame = {0};
1500 CURSORICONFILEDIR *dir;
1501 DWORD filesize = 0;
1502 HICON hIcon = 0;
1503 POINT16 hotspot;
1504 LPBYTE bits;
1506 TRACE("loading %s\n", debugstr_w( filename ));
1508 bits = map_fileW( filename, &filesize );
1509 if (!bits)
1510 return hIcon;
1512 /* If the data contains the magic for an .ICO it's an .ICO,
1513 * regardless of what fCursor says. */
1514 if (!memcmp( bits, "\x00\x00\x01\x00", 4 )) fCursor = FALSE;
1515 /* Same thing for .CUR */
1516 else if (!memcmp( bits, "\x00\x00\x02\x00", 4 )) fCursor = TRUE;
1517 /* Check for .ani. */
1518 else if (!memcmp( bits, "RIFF", 4 ))
1520 hIcon = load_ani( bits, filesize, width, height );
1521 goto end;
1524 dir = (CURSORICONFILEDIR*) bits;
1525 if ( filesize < sizeof(*dir) )
1526 goto end;
1528 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1529 goto end;
1531 if ( fCursor )
1532 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1533 else
1534 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1536 if ( !entry )
1537 goto end;
1539 /* check that we don't run off the end of the file */
1540 if ( entry->dwDIBOffset > filesize )
1541 goto end;
1542 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1543 goto end;
1545 if ( fCursor )
1547 hotspot.x = entry->xHotspot;
1548 hotspot.y = entry->yHotspot;
1550 else
1552 hotspot.x = ICON_HOTSPOT;
1553 hotspot.y = ICON_HOTSPOT;
1556 hIcon = create_cursor( 1, 0 );
1557 load_cursor_frame( &bits[entry->dwDIBOffset], entry->dwDIBSize, hotspot,
1558 0x00030000, width, height, loadflags, &frame );
1559 set_cursor_frame( hIcon, 0, &frame );
1560 HeapFree( GetProcessHeap(), 0, frame.bits );
1562 end:
1563 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1564 UnmapViewOfFile( bits );
1565 return hIcon;
1568 /**********************************************************************
1569 * CURSORICON_Load
1571 * Load a cursor or icon from resource or file.
1573 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1574 INT width, INT height, INT colors,
1575 BOOL fCursor, UINT loadflags)
1577 HANDLE handle = 0;
1578 HICON hIcon = 0;
1579 HRSRC hRsrc, hGroupRsrc;
1580 CURSORICONDIR *dir;
1581 CURSORICONDIRENTRY *dirEntry;
1582 LPBYTE bits;
1583 WORD wResId;
1584 DWORD dwBytesInRes;
1586 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1587 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1589 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1590 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1592 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1594 /* Normalize hInstance (must be uniquely represented for icon cache) */
1596 if (!HIWORD( hInstance ))
1597 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1599 /* Get directory resource ID */
1601 if (!(hRsrc = FindResourceW( hInstance, name,
1602 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1603 return 0;
1604 hGroupRsrc = hRsrc;
1606 /* Find the best entry in the directory */
1608 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1609 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
1610 if (fCursor)
1611 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1612 else
1613 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1614 if (!dirEntry) return 0;
1615 wResId = dirEntry->wResId;
1616 dwBytesInRes = dirEntry->dwBytesInRes;
1617 FreeResource( handle );
1619 /* Load the resource */
1621 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1622 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1624 /* If shared icon, check whether it was already loaded */
1625 if ( (loadflags & LR_SHARED)
1626 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1627 return hIcon;
1629 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1630 bits = (LPBYTE)LockResource( handle );
1631 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1632 !fCursor, 0x00030000, width, height, loadflags);
1633 FreeResource( handle );
1635 /* If shared icon, add to icon cache */
1637 if ( hIcon && (loadflags & LR_SHARED) )
1638 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1640 return hIcon;
1643 /***********************************************************************
1644 * CURSORICON_Copy
1646 * Make a copy of a cursor or icon.
1648 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1650 /* Should animated cursors be copyable like this as well? */
1651 HCURSOR new_cursor;
1652 cursor_frame_t frame;
1654 if (!hIcon || !get_cursor_frame( hIcon, 0, &frame ))
1656 return 0;
1659 new_cursor = create_cursor( 1, 0 );
1660 set_cursor_frame( new_cursor, 0, &frame );
1661 HeapFree( GetProcessHeap(), 0, frame.bits );
1663 return new_cursor;
1666 /*************************************************************************
1667 * CURSORICON_ExtCopy
1669 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1671 * PARAMS
1672 * Handle [I] handle to an Image
1673 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1674 * iDesiredCX [I] The Desired width of the Image
1675 * iDesiredCY [I] The desired height of the Image
1676 * nFlags [I] The flags from CopyImage
1678 * RETURNS
1679 * Success: The new handle of the Image
1681 * NOTES
1682 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1683 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1684 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1689 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1690 INT iDesiredCX, INT iDesiredCY,
1691 UINT nFlags)
1693 HICON hNew=0;
1695 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1696 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1698 if(hIcon == 0)
1700 return 0;
1703 /* Best Fit or Monochrome */
1704 if( (nFlags & LR_COPYFROMRESOURCE
1705 && (iDesiredCX > 0 || iDesiredCY > 0))
1706 || nFlags & LR_MONOCHROME)
1708 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1710 /* Not Found in Cache, then do a straight copy
1712 if(pIconCache == NULL)
1714 hNew = CURSORICON_Copy(0, hIcon);
1715 if(nFlags & LR_COPYFROMRESOURCE)
1717 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1720 else
1722 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1723 LPBYTE pBits;
1724 HANDLE hMem;
1725 HRSRC hRsrc;
1726 DWORD dwBytesInRes;
1727 WORD wResId;
1728 CURSORICONDIR *pDir;
1729 CURSORICONDIRENTRY *pDirEntry;
1730 BOOL bIsIcon = (nType == IMAGE_ICON);
1732 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1734 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1735 || (iDesiredCX == 0 && iDesiredCY == 0))
1737 iDesiredCY = GetSystemMetrics(bIsIcon ?
1738 SM_CYICON : SM_CYCURSOR);
1739 iDesiredCX = GetSystemMetrics(bIsIcon ?
1740 SM_CXICON : SM_CXCURSOR);
1743 /* Retrieve the CURSORICONDIRENTRY
1745 if (!(hMem = LoadResource( pIconCache->hModule ,
1746 pIconCache->hGroupRsrc)))
1748 return 0;
1750 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1752 return 0;
1755 /* Find Best Fit
1757 if(bIsIcon)
1759 pDirEntry = CURSORICON_FindBestIconRes(
1760 pDir, iDesiredCX, iDesiredCY, 256 );
1762 else
1764 pDirEntry = CURSORICON_FindBestCursorRes(
1765 pDir, iDesiredCX, iDesiredCY, 1);
1768 wResId = pDirEntry->wResId;
1769 dwBytesInRes = pDirEntry->dwBytesInRes;
1770 FreeResource(hMem);
1772 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1773 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1774 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1776 /* Get the Best Fit
1778 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1779 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1781 return 0;
1783 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1785 return 0;
1788 pBits = (LPBYTE)LockResource( hMem );
1790 if(nFlags & LR_DEFAULTSIZE)
1792 iTargetCY = GetSystemMetrics(SM_CYICON);
1793 iTargetCX = GetSystemMetrics(SM_CXICON);
1796 /* Create a New Icon with the proper dimension
1798 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1799 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1800 FreeResource(hMem);
1803 else hNew = CURSORICON_Copy(0, hIcon);
1804 return hNew;
1808 /***********************************************************************
1809 * CreateCursor (USER32.@)
1811 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1812 INT xHotSpot, INT yHotSpot,
1813 INT nWidth, INT nHeight,
1814 LPCVOID lpANDbits, LPCVOID lpXORbits )
1816 CURSORICONINFO info;
1818 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1819 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1821 info.ptHotSpot.x = xHotSpot;
1822 info.ptHotSpot.y = yHotSpot;
1823 info.nWidth = nWidth;
1824 info.nHeight = nHeight;
1825 info.nWidthBytes = 0;
1826 info.bPlanes = 1;
1827 info.bBitsPerPixel = 1;
1829 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1833 /***********************************************************************
1834 * CreateIcon (USER.407)
1836 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1837 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1838 LPCVOID lpANDbits, LPCVOID lpXORbits )
1840 CURSORICONINFO info;
1842 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1843 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1845 info.ptHotSpot.x = ICON_HOTSPOT;
1846 info.ptHotSpot.y = ICON_HOTSPOT;
1847 info.nWidth = nWidth;
1848 info.nHeight = nHeight;
1849 info.nWidthBytes = 0;
1850 info.bPlanes = bPlanes;
1851 info.bBitsPerPixel = bBitsPixel;
1853 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1857 /***********************************************************************
1858 * CreateIcon (USER32.@)
1860 * Creates an icon based on the specified bitmaps. The bitmaps must be
1861 * provided in a device dependent format and will be resized to
1862 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1863 * depth. The provided bitmaps must be top-down bitmaps.
1864 * Although Windows does not support 15bpp(*) this API must support it
1865 * for Winelib applications.
1867 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1868 * format!
1870 * RETURNS
1871 * Success: handle to an icon
1872 * Failure: NULL
1874 * FIXME: Do we need to resize the bitmaps?
1876 HICON WINAPI CreateIcon(
1877 HINSTANCE hInstance, /* [in] the application's hInstance */
1878 INT nWidth, /* [in] the width of the provided bitmaps */
1879 INT nHeight, /* [in] the height of the provided bitmaps */
1880 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1881 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1882 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1883 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1885 ICONINFO iinfo;
1886 HICON hIcon;
1888 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1889 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1891 iinfo.fIcon = TRUE;
1892 iinfo.xHotspot = ICON_HOTSPOT;
1893 iinfo.yHotspot = ICON_HOTSPOT;
1894 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1895 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1897 hIcon = CreateIconIndirect( &iinfo );
1899 DeleteObject( iinfo.hbmMask );
1900 DeleteObject( iinfo.hbmColor );
1902 return hIcon;
1906 /***********************************************************************
1907 * CreateCursorIconIndirect (USER.408)
1909 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1910 CURSORICONINFO *info,
1911 LPCVOID lpANDbits,
1912 LPCVOID lpXORbits )
1914 HCURSOR cursor;
1915 cursor_frame_t frame;
1916 int sizeAnd, sizeXor;
1918 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1919 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1920 sizeXor = info->nHeight * info->nWidthBytes;
1921 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1923 cursor = create_cursor( 1, 0 );
1924 frame.xhot = info->ptHotSpot.x;
1925 frame.yhot = info->ptHotSpot.y;
1926 frame.width = info->nWidth;
1927 frame.height = info->nHeight;
1928 frame.and_width_bytes = get_bitmap_width_bytes( info->nWidth, 1 );
1929 frame.xor_width_bytes = info->nWidthBytes;
1930 frame.planes = info->bPlanes;
1931 frame.bpp = info->bBitsPerPixel;
1932 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
1933 CopyMemory( frame.bits, lpANDbits, sizeAnd );
1934 CopyMemory( frame.bits + sizeAnd, lpXORbits, sizeXor );
1935 set_cursor_frame( cursor, 0, &frame );
1936 HeapFree( GetProcessHeap(), 0, frame.bits );
1938 return HICON_16(cursor);
1942 /***********************************************************************
1943 * CopyIcon (USER.368)
1945 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1947 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1948 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1952 /***********************************************************************
1953 * CopyIcon (USER32.@)
1955 HICON WINAPI CopyIcon( HICON hIcon )
1957 TRACE_(icon)("%p\n", hIcon );
1958 return CURSORICON_Copy( 0, hIcon );
1962 /***********************************************************************
1963 * CopyCursor (USER.369)
1965 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1967 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1968 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1971 /**********************************************************************
1972 * DestroyIcon32 (USER.610)
1974 * This routine is actually exported from Win95 USER under the name
1975 * DestroyIcon32 ... The behaviour implemented here should mimic
1976 * the Win95 one exactly, especially the return values, which
1977 * depend on the setting of various flags.
1979 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1981 WORD retv;
1983 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1985 /* Check whether destroying active cursor */
1987 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1989 WARN_(cursor)("Destroying active cursor!\n" );
1990 return FALSE;
1993 /* Try shared cursor/icon first */
1995 if ( !(flags & CID_NONSHARED) )
1997 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1999 if ( count != -1 )
2000 return (flags & CID_WIN32)? TRUE : (count == 0);
2002 /* FIXME: OEM cursors/icons should be recognized */
2005 /* Now assume non-shared cursor/icon */
2007 retv = destroy_cursor( HCURSOR_32(handle) );
2008 return (flags & CID_RESOURCE)? retv : TRUE;
2011 /***********************************************************************
2012 * DestroyIcon (USER32.@)
2014 BOOL WINAPI DestroyIcon( HICON hIcon )
2016 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
2020 /***********************************************************************
2021 * DestroyCursor (USER32.@)
2023 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
2025 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
2029 /***********************************************************************
2030 * DrawIcon (USER32.@)
2032 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
2034 CURSORICONINFO *ptr;
2035 HDC hMemDC;
2036 HBITMAP hXorBits, hAndBits;
2037 COLORREF oldFg, oldBg;
2039 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
2041 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
2042 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2043 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
2044 (char *)(ptr+1) );
2045 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
2046 ptr->bBitsPerPixel, (char *)(ptr + 1)
2047 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
2048 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2049 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2051 if (hXorBits && hAndBits)
2053 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2054 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
2055 SelectObject( hMemDC, hXorBits );
2056 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
2057 SelectObject( hMemDC, hBitTemp );
2059 DeleteDC( hMemDC );
2060 if (hXorBits) DeleteObject( hXorBits );
2061 if (hAndBits) DeleteObject( hAndBits );
2062 GlobalUnlock16(HICON_16(hIcon));
2063 SetTextColor( hdc, oldFg );
2064 SetBkColor( hdc, oldBg );
2065 return TRUE;
2068 /***********************************************************************
2069 * DumpIcon (USER.459)
2071 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
2072 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
2074 CURSORICONINFO *info = MapSL( pInfo );
2075 int sizeAnd, sizeXor;
2077 if (!info) return 0;
2078 sizeXor = info->nHeight * info->nWidthBytes;
2079 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
2080 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
2081 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
2082 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
2083 return MAKELONG( sizeXor, sizeXor );
2087 /***********************************************************************
2088 * SetCursor (USER32.@)
2090 * Set the cursor shape.
2092 * RETURNS
2093 * A handle to the previous cursor shape.
2095 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
2097 struct user_thread_info *thread_info = get_user_thread_info();
2098 HCURSOR hOldCursor;
2100 if (hCursor == thread_info->cursor) return hCursor; /* No change */
2101 TRACE("%p\n", hCursor);
2102 hOldCursor = thread_info->cursor;
2103 thread_info->cursor = hCursor;
2104 /* Change the cursor shape only if it is visible */
2105 if (thread_info->cursor_count >= 0)
2107 cursor_t *cursor;
2109 update_cursor_32from16( hCursor );
2110 cursor = get_cursor_object( hCursor );
2111 USER_Driver->pSetCursor( cursor );
2112 destroy_cursor_object( cursor );
2114 return hOldCursor;
2117 /***********************************************************************
2118 * ShowCursor (USER32.@)
2120 INT WINAPI ShowCursor( BOOL bShow )
2122 struct user_thread_info *thread_info = get_user_thread_info();
2124 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
2126 if (bShow)
2128 if (++thread_info->cursor_count == 0) /* Show it */
2130 cursor_t *cursor;
2132 update_cursor_32from16( thread_info->cursor );
2133 cursor = get_cursor_object( thread_info->cursor );
2134 USER_Driver->pSetCursor( cursor );
2135 destroy_cursor_object( cursor );
2138 else
2140 if (--thread_info->cursor_count == -1) /* Hide it */
2141 USER_Driver->pSetCursor( NULL );
2143 return thread_info->cursor_count;
2146 /***********************************************************************
2147 * GetCursor (USER32.@)
2149 HCURSOR WINAPI GetCursor(void)
2151 return get_user_thread_info()->cursor;
2155 /***********************************************************************
2156 * ClipCursor (USER32.@)
2158 BOOL WINAPI ClipCursor( const RECT *rect )
2160 RECT virt;
2162 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
2163 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
2164 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
2165 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
2167 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
2168 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
2170 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
2171 CURSOR_ClipRect = virt;
2173 USER_Driver->pClipCursor( rect );
2174 return TRUE;
2178 /***********************************************************************
2179 * GetClipCursor (USER32.@)
2181 BOOL WINAPI GetClipCursor( RECT *rect )
2183 /* If this is first time - initialize the rect */
2184 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
2186 return CopyRect( rect, &CURSOR_ClipRect );
2190 /***********************************************************************
2191 * SetSystemCursor (USER32.@)
2193 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
2195 FIXME("(%p,%08x),stub!\n", hcur, id);
2196 return TRUE;
2200 /**********************************************************************
2201 * LookupIconIdFromDirectoryEx (USER.364)
2203 * FIXME: exact parameter sizes
2205 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
2206 INT16 width, INT16 height, UINT16 cFlag )
2208 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
2211 /**********************************************************************
2212 * LookupIconIdFromDirectoryEx (USER32.@)
2214 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
2215 INT width, INT height, UINT cFlag )
2217 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
2218 UINT retVal = 0;
2219 if( dir && !dir->idReserved && (dir->idType & 3) )
2221 CURSORICONDIRENTRY* entry;
2222 HDC hdc;
2223 UINT palEnts;
2224 int colors;
2225 hdc = GetDC(0);
2226 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
2227 if (palEnts == 0)
2228 palEnts = 256;
2229 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
2231 ReleaseDC(0, hdc);
2233 if( bIcon )
2234 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
2235 else
2236 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
2238 if( entry ) retVal = entry->wResId;
2240 else WARN_(cursor)("invalid resource directory\n");
2241 return retVal;
2244 /**********************************************************************
2245 * LookupIconIdFromDirectory (USER.?)
2247 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
2249 return LookupIconIdFromDirectoryEx16( dir, bIcon,
2250 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2251 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2254 /**********************************************************************
2255 * LookupIconIdFromDirectory (USER32.@)
2257 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
2259 return LookupIconIdFromDirectoryEx( dir, bIcon,
2260 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
2261 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
2264 /**********************************************************************
2265 * GetIconID (USER.455)
2267 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
2269 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
2271 TRACE_(cursor)("hRes=%04x, entries=%i\n",
2272 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
2274 switch(resType)
2276 case RT_CURSOR:
2277 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
2278 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
2279 case RT_ICON:
2280 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
2281 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
2282 default:
2283 WARN_(cursor)("invalid res type %d\n", resType );
2285 return 0;
2288 /**********************************************************************
2289 * LoadCursorIconHandler (USER.336)
2291 * Supposed to load resources of Windows 2.x applications.
2293 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
2295 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2296 hResource, hModule, hRsrc);
2297 return (HGLOBAL16)0;
2300 /**********************************************************************
2301 * LoadIconHandler (USER.456)
2303 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2305 LPBYTE bits = (LPBYTE)LockResource16( hResource );
2307 TRACE_(cursor)("hRes=%04x\n",hResource);
2309 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2310 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2313 /***********************************************************************
2314 * LoadCursorW (USER32.@)
2316 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2318 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2320 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2321 LR_SHARED | LR_DEFAULTSIZE );
2324 /***********************************************************************
2325 * LoadCursorA (USER32.@)
2327 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2329 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2331 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2332 LR_SHARED | LR_DEFAULTSIZE );
2335 /***********************************************************************
2336 * LoadCursorFromFileW (USER32.@)
2338 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2340 TRACE("%s\n", debugstr_w(name));
2342 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2343 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2346 /***********************************************************************
2347 * LoadCursorFromFileA (USER32.@)
2349 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2351 TRACE("%s\n", debugstr_a(name));
2353 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2354 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2357 /***********************************************************************
2358 * LoadIconW (USER32.@)
2360 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2362 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2364 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2365 LR_SHARED | LR_DEFAULTSIZE );
2368 /***********************************************************************
2369 * LoadIconA (USER32.@)
2371 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2373 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2375 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2376 LR_SHARED | LR_DEFAULTSIZE );
2379 /**********************************************************************
2380 * GetIconInfo (USER32.@)
2382 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2384 CURSORICONINFO *ciconinfo;
2385 INT height;
2387 ciconinfo = GlobalLock16(HICON_16(hIcon));
2388 if (!ciconinfo)
2389 return FALSE;
2391 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2392 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2394 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2395 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2397 iconinfo->fIcon = TRUE;
2398 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2399 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2401 else
2403 iconinfo->fIcon = FALSE;
2404 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2405 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2408 height = ciconinfo->nHeight;
2410 if (ciconinfo->bBitsPerPixel > 1)
2412 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2413 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2414 (char *)(ciconinfo + 1)
2415 + ciconinfo->nHeight *
2416 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2418 else
2420 iconinfo->hbmColor = 0;
2421 height *= 2;
2424 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2425 1, 1, (char *)(ciconinfo + 1));
2427 GlobalUnlock16(HICON_16(hIcon));
2429 return TRUE;
2432 /**********************************************************************
2433 * CreateIconIndirect (USER32.@)
2435 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2437 HCURSOR cursor;
2438 cursor_frame_t frame;
2439 BITMAP bmpXor,bmpAnd;
2440 int sizeXor,sizeAnd;
2442 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2443 iconinfo->hbmColor, iconinfo->hbmMask,
2444 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2446 if (!iconinfo->hbmMask) return 0;
2448 if (iconinfo->hbmColor)
2450 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2451 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2452 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2453 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2455 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2456 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2457 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2458 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2460 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
2461 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2463 cursor = create_cursor( 1, 0 );
2465 /* If we are creating an icon, the hotspot is unused */
2466 if (iconinfo->fIcon)
2468 frame.xhot = ICON_HOTSPOT;
2469 frame.yhot = ICON_HOTSPOT;
2471 else
2473 frame.xhot = iconinfo->xHotspot;
2474 frame.yhot = iconinfo->yHotspot;
2477 if (iconinfo->hbmColor)
2479 frame.width = bmpXor.bmWidth;
2480 frame.height = bmpXor.bmHeight;
2481 frame.and_width_bytes = bmpAnd.bmWidthBytes;
2482 frame.xor_width_bytes = bmpXor.bmWidthBytes;
2483 frame.planes = bmpXor.bmPlanes;
2484 frame.bpp = bmpXor.bmBitsPixel;
2486 else
2488 frame.width = bmpAnd.bmWidth;
2489 frame.height = bmpAnd.bmHeight / 2;
2490 frame.and_width_bytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2491 frame.xor_width_bytes = 0;
2492 frame.planes = 1;
2493 frame.bpp = 1;
2496 frame.bits = HeapAlloc( GetProcessHeap(), 0, sizeAnd + sizeXor );
2498 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2499 if (bmpAnd.bmBitsPixel == 1)
2501 GetBitmapBits( iconinfo->hbmMask, sizeAnd, frame.bits );
2503 else
2505 HDC hdc, hdc_mem;
2506 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2508 hdc = GetDC( 0 );
2509 hdc_mem = CreateCompatibleDC( hdc );
2511 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2513 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2514 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2516 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2518 SelectObject( hdc, hbmp_old );
2519 SelectObject( hdc_mem, hbmp_mem_old );
2521 DeleteDC( hdc_mem );
2522 ReleaseDC( 0, hdc );
2524 GetBitmapBits( hbmp_mono, sizeAnd, frame.bits );
2525 DeleteObject( hbmp_mono );
2528 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, frame.bits + sizeAnd );
2529 set_cursor_frame( cursor, 0, &frame );
2530 HeapFree( GetProcessHeap(), 0, frame.bits );
2532 return cursor;
2535 /******************************************************************************
2536 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2538 * NOTES
2539 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2541 * PARAMS
2542 * hdc [I] Handle to device context
2543 * x0 [I] X coordinate of upper left corner
2544 * y0 [I] Y coordinate of upper left corner
2545 * hIcon [I] Handle to icon to draw
2546 * cxWidth [I] Width of icon
2547 * cyWidth [I] Height of icon
2548 * istep [I] Index of frame in animated cursor
2549 * hbr [I] Handle to background brush
2550 * flags [I] Icon-drawing flags
2552 * RETURNS
2553 * Success: TRUE
2554 * Failure: FALSE
2556 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2557 INT cxWidth, INT cyWidth, UINT istep,
2558 HBRUSH hbr, UINT flags )
2560 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
2561 HDC hDC_off = 0, hMemDC;
2562 BOOL result = FALSE, DoOffscreen;
2563 HBITMAP hB_off = 0, hOld = 0;
2565 if (!ptr) return FALSE;
2566 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2567 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2569 hMemDC = CreateCompatibleDC (hdc);
2570 if (istep)
2571 FIXME_(icon)("Ignoring istep=%d\n", istep);
2572 if (flags & DI_COMPAT)
2573 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
2575 if (!flags) {
2576 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2577 flags = DI_NORMAL;
2580 /* Calculate the size of the destination image. */
2581 if (cxWidth == 0)
2583 if (flags & DI_DEFAULTSIZE)
2584 cxWidth = GetSystemMetrics (SM_CXICON);
2585 else
2586 cxWidth = ptr->nWidth;
2588 if (cyWidth == 0)
2590 if (flags & DI_DEFAULTSIZE)
2591 cyWidth = GetSystemMetrics (SM_CYICON);
2592 else
2593 cyWidth = ptr->nHeight;
2596 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2598 if (DoOffscreen) {
2599 RECT r;
2601 r.left = 0;
2602 r.top = 0;
2603 r.right = cxWidth;
2604 r.bottom = cxWidth;
2606 hDC_off = CreateCompatibleDC(hdc);
2607 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2608 if (hDC_off && hB_off) {
2609 hOld = SelectObject(hDC_off, hB_off);
2610 FillRect(hDC_off, &r, hbr);
2614 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2616 HBITMAP hXorBits, hAndBits;
2617 COLORREF oldFg, oldBg;
2618 INT nStretchMode;
2620 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2622 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2623 ptr->bPlanes, ptr->bBitsPerPixel,
2624 (char *)(ptr + 1)
2625 + ptr->nHeight *
2626 get_bitmap_width_bytes(ptr->nWidth,1) );
2627 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2628 1, 1, (char *)(ptr+1) );
2629 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2630 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2632 if (hXorBits && hAndBits)
2634 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2635 if (flags & DI_MASK)
2637 if (DoOffscreen)
2638 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2639 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2640 else
2641 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2642 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2644 SelectObject( hMemDC, hXorBits );
2645 if (flags & DI_IMAGE)
2647 if (DoOffscreen)
2648 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2649 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2650 else
2651 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2652 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2654 SelectObject( hMemDC, hBitTemp );
2655 result = TRUE;
2658 SetTextColor( hdc, oldFg );
2659 SetBkColor( hdc, oldBg );
2660 if (hXorBits) DeleteObject( hXorBits );
2661 if (hAndBits) DeleteObject( hAndBits );
2662 SetStretchBltMode (hdc, nStretchMode);
2663 if (DoOffscreen) {
2664 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2665 SelectObject(hDC_off, hOld);
2668 if (hMemDC) DeleteDC( hMemDC );
2669 if (hDC_off) DeleteDC(hDC_off);
2670 if (hB_off) DeleteObject(hB_off);
2671 GlobalUnlock16(HICON_16(hIcon));
2672 return result;
2675 /***********************************************************************
2676 * DIB_FixColorsToLoadflags
2678 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2679 * are in loadflags
2681 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2683 int colors;
2684 COLORREF c_W, c_S, c_F, c_L, c_C;
2685 int incr,i;
2686 RGBQUAD *ptr;
2687 int bitmap_type;
2688 LONG width;
2689 LONG height;
2690 WORD bpp;
2691 DWORD compr;
2693 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2695 WARN_(resource)("Invalid bitmap\n");
2696 return;
2699 if (bpp > 8) return;
2701 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2703 incr = 3;
2704 colors = 1 << bpp;
2706 else
2708 incr = 4;
2709 colors = bmi->bmiHeader.biClrUsed;
2710 if (colors > 256) colors = 256;
2711 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2714 c_W = GetSysColor(COLOR_WINDOW);
2715 c_S = GetSysColor(COLOR_3DSHADOW);
2716 c_F = GetSysColor(COLOR_3DFACE);
2717 c_L = GetSysColor(COLOR_3DLIGHT);
2719 if (loadflags & LR_LOADTRANSPARENT) {
2720 switch (bpp) {
2721 case 1: pix = pix >> 7; break;
2722 case 4: pix = pix >> 4; break;
2723 case 8: break;
2724 default:
2725 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2726 return;
2728 if (pix >= colors) {
2729 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2730 return;
2732 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2733 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2734 ptr->rgbBlue = GetBValue(c_W);
2735 ptr->rgbGreen = GetGValue(c_W);
2736 ptr->rgbRed = GetRValue(c_W);
2738 if (loadflags & LR_LOADMAP3DCOLORS)
2739 for (i=0; i<colors; i++) {
2740 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2741 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2742 if (c_C == RGB(128, 128, 128)) {
2743 ptr->rgbRed = GetRValue(c_S);
2744 ptr->rgbGreen = GetGValue(c_S);
2745 ptr->rgbBlue = GetBValue(c_S);
2746 } else if (c_C == RGB(192, 192, 192)) {
2747 ptr->rgbRed = GetRValue(c_F);
2748 ptr->rgbGreen = GetGValue(c_F);
2749 ptr->rgbBlue = GetBValue(c_F);
2750 } else if (c_C == RGB(223, 223, 223)) {
2751 ptr->rgbRed = GetRValue(c_L);
2752 ptr->rgbGreen = GetGValue(c_L);
2753 ptr->rgbBlue = GetBValue(c_L);
2759 /**********************************************************************
2760 * BITMAP_Load
2762 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2763 INT desiredx, INT desiredy, UINT loadflags )
2765 HBITMAP hbitmap = 0, orig_bm;
2766 HRSRC hRsrc;
2767 HGLOBAL handle;
2768 char *ptr = NULL;
2769 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2770 int size;
2771 BYTE pix;
2772 char *bits;
2773 LONG width, height, new_width, new_height;
2774 WORD bpp_dummy;
2775 DWORD compr_dummy;
2776 INT bm_type;
2777 HDC screen_mem_dc = NULL;
2779 if (!(loadflags & LR_LOADFROMFILE))
2781 if (!instance)
2783 /* OEM bitmap: try to load the resource from user32.dll */
2784 instance = user32_module;
2787 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2788 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2790 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2792 else
2794 if (!(ptr = map_fileW( name, NULL ))) return 0;
2795 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2798 size = bitmap_info_size(info, DIB_RGB_COLORS);
2799 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2800 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2802 if (!fix_info || !scaled_info) goto end;
2803 memcpy(fix_info, info, size);
2805 pix = *((LPBYTE)info + size);
2806 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2808 memcpy(scaled_info, fix_info, size);
2809 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2810 &bpp_dummy, &compr_dummy);
2811 if(desiredx != 0)
2812 new_width = desiredx;
2813 else
2814 new_width = width;
2816 if(desiredy != 0)
2817 new_height = height > 0 ? desiredy : -desiredy;
2818 else
2819 new_height = height;
2821 if(bm_type == 0)
2823 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2824 core->bcWidth = new_width;
2825 core->bcHeight = new_height;
2827 else
2829 scaled_info->bmiHeader.biWidth = new_width;
2830 scaled_info->bmiHeader.biHeight = new_height;
2833 if (new_height < 0) new_height = -new_height;
2835 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2836 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2838 bits = (char *)info + size;
2840 if (loadflags & LR_CREATEDIBSECTION)
2842 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2843 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2845 else
2847 if (is_dib_monochrome(fix_info))
2848 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2849 else
2850 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2853 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2854 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2855 SelectObject(screen_mem_dc, orig_bm);
2857 end:
2858 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2859 HeapFree(GetProcessHeap(), 0, scaled_info);
2860 HeapFree(GetProcessHeap(), 0, fix_info);
2861 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2863 return hbitmap;
2866 /**********************************************************************
2867 * LoadImageA (USER32.@)
2869 * See LoadImageW.
2871 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2872 INT desiredx, INT desiredy, UINT loadflags)
2874 HANDLE res;
2875 LPWSTR u_name;
2877 if (!HIWORD(name))
2878 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2880 __TRY {
2881 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2882 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2883 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2885 __EXCEPT_PAGE_FAULT {
2886 SetLastError( ERROR_INVALID_PARAMETER );
2887 return 0;
2889 __ENDTRY
2890 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2891 HeapFree(GetProcessHeap(), 0, u_name);
2892 return res;
2896 /******************************************************************************
2897 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2899 * PARAMS
2900 * hinst [I] Handle of instance that contains image
2901 * name [I] Name of image
2902 * type [I] Type of image
2903 * desiredx [I] Desired width
2904 * desiredy [I] Desired height
2905 * loadflags [I] Load flags
2907 * RETURNS
2908 * Success: Handle to newly loaded image
2909 * Failure: NULL
2911 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2913 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2914 INT desiredx, INT desiredy, UINT loadflags )
2916 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2917 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2919 if (loadflags & LR_DEFAULTSIZE) {
2920 if (type == IMAGE_ICON) {
2921 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2922 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2923 } else if (type == IMAGE_CURSOR) {
2924 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2925 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2928 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2929 switch (type) {
2930 case IMAGE_BITMAP:
2931 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2933 case IMAGE_ICON:
2934 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2935 if (screen_dc)
2937 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2938 if (palEnts == 0) palEnts = 256;
2939 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2940 palEnts, FALSE, loadflags);
2942 break;
2944 case IMAGE_CURSOR:
2945 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2946 1, TRUE, loadflags);
2948 return 0;
2951 /******************************************************************************
2952 * CopyImage (USER32.@) Creates new image and copies attributes to it
2954 * PARAMS
2955 * hnd [I] Handle to image to copy
2956 * type [I] Type of image to copy
2957 * desiredx [I] Desired width of new image
2958 * desiredy [I] Desired height of new image
2959 * flags [I] Copy flags
2961 * RETURNS
2962 * Success: Handle to newly created image
2963 * Failure: NULL
2965 * BUGS
2966 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2967 * all other versions (95/2000/XP have been tested) ignore it.
2969 * NOTES
2970 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2971 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2972 * the copy will have the same depth as the screen.
2973 * The content of the image will only be copied if the bit depth of the
2974 * original image is compatible with the bit depth of the screen, or
2975 * if the source is a DIB section.
2976 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2978 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2979 INT desiredy, UINT flags )
2981 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2982 hnd, type, desiredx, desiredy, flags);
2984 switch (type)
2986 case IMAGE_BITMAP:
2988 HBITMAP res = NULL;
2989 DIBSECTION ds;
2990 int objSize;
2991 BITMAPINFO * bi;
2993 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2994 if (!objSize) return 0;
2995 if ((desiredx < 0) || (desiredy < 0)) return 0;
2997 if (flags & LR_COPYFROMRESOURCE)
2999 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
3002 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
3003 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
3005 /* Allocate memory for a BITMAPINFOHEADER structure and a
3006 color table. The maximum number of colors in a color table
3007 is 256 which corresponds to a bitmap with depth 8.
3008 Bitmaps with higher depths don't have color tables. */
3009 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3010 if (!bi) return 0;
3012 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3013 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
3014 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3015 bi->bmiHeader.biCompression = BI_RGB;
3017 if (flags & LR_CREATEDIBSECTION)
3019 /* Create a DIB section. LR_MONOCHROME is ignored */
3020 void * bits;
3021 HDC dc = CreateCompatibleDC(NULL);
3023 if (objSize == sizeof(DIBSECTION))
3025 /* The source bitmap is a DIB.
3026 Get its attributes to create an exact copy */
3027 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
3030 /* Get the color table or the color masks */
3031 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3033 bi->bmiHeader.biWidth = desiredx;
3034 bi->bmiHeader.biHeight = desiredy;
3035 bi->bmiHeader.biSizeImage = 0;
3037 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
3038 DeleteDC(dc);
3040 else
3042 /* Create a device-dependent bitmap */
3044 BOOL monochrome = (flags & LR_MONOCHROME);
3046 if (objSize == sizeof(DIBSECTION))
3048 /* The source bitmap is a DIB section.
3049 Get its attributes */
3050 HDC dc = CreateCompatibleDC(NULL);
3051 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
3052 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
3053 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3054 DeleteDC(dc);
3056 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
3058 /* Look if the colors of the DIB are black and white */
3060 monochrome =
3061 (bi->bmiColors[0].rgbRed == 0xff
3062 && bi->bmiColors[0].rgbGreen == 0xff
3063 && bi->bmiColors[0].rgbBlue == 0xff
3064 && bi->bmiColors[0].rgbReserved == 0
3065 && bi->bmiColors[1].rgbRed == 0
3066 && bi->bmiColors[1].rgbGreen == 0
3067 && bi->bmiColors[1].rgbBlue == 0
3068 && bi->bmiColors[1].rgbReserved == 0)
3070 (bi->bmiColors[0].rgbRed == 0
3071 && bi->bmiColors[0].rgbGreen == 0
3072 && bi->bmiColors[0].rgbBlue == 0
3073 && bi->bmiColors[0].rgbReserved == 0
3074 && bi->bmiColors[1].rgbRed == 0xff
3075 && bi->bmiColors[1].rgbGreen == 0xff
3076 && bi->bmiColors[1].rgbBlue == 0xff
3077 && bi->bmiColors[1].rgbReserved == 0);
3080 else if (!monochrome)
3082 monochrome = ds.dsBm.bmBitsPixel == 1;
3085 if (monochrome)
3087 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
3089 else
3091 HDC screenDC = GetDC(NULL);
3092 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
3093 ReleaseDC(NULL, screenDC);
3097 if (res)
3099 /* Only copy the bitmap if it's a DIB section or if it's
3100 compatible to the screen */
3101 BOOL copyContents;
3103 if (objSize == sizeof(DIBSECTION))
3105 copyContents = TRUE;
3107 else
3109 HDC screenDC = GetDC(NULL);
3110 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
3111 ReleaseDC(NULL, screenDC);
3113 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
3116 if (copyContents)
3118 /* The source bitmap may already be selected in a device context,
3119 use GetDIBits/StretchDIBits and not StretchBlt */
3121 HDC dc;
3122 void * bits;
3124 dc = CreateCompatibleDC(NULL);
3126 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
3127 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
3128 bi->bmiHeader.biSizeImage = 0;
3129 bi->bmiHeader.biClrUsed = 0;
3130 bi->bmiHeader.biClrImportant = 0;
3132 /* Fill in biSizeImage */
3133 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
3134 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
3136 if (bits)
3138 HBITMAP oldBmp;
3140 /* Get the image bits of the source bitmap */
3141 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
3143 /* Copy it to the destination bitmap */
3144 oldBmp = SelectObject(dc, res);
3145 StretchDIBits(dc, 0, 0, desiredx, desiredy,
3146 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
3147 bits, bi, DIB_RGB_COLORS, SRCCOPY);
3148 SelectObject(dc, oldBmp);
3150 HeapFree(GetProcessHeap(), 0, bits);
3153 DeleteDC(dc);
3156 if (flags & LR_COPYDELETEORG)
3158 DeleteObject(hnd);
3161 HeapFree(GetProcessHeap(), 0, bi);
3162 return res;
3164 case IMAGE_ICON:
3165 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
3166 case IMAGE_CURSOR:
3167 /* Should call CURSORICON_ExtCopy but more testing
3168 * needs to be done before we change this
3170 if (flags) FIXME("Flags are ignored\n");
3171 return CopyCursor(hnd);
3173 return 0;
3177 /******************************************************************************
3178 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3180 * RETURNS
3181 * Success: Handle to specified bitmap
3182 * Failure: NULL
3184 HBITMAP WINAPI LoadBitmapW(
3185 HINSTANCE instance, /* [in] Handle to application instance */
3186 LPCWSTR name) /* [in] Address of bitmap resource name */
3188 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3191 /**********************************************************************
3192 * LoadBitmapA (USER32.@)
3194 * See LoadBitmapW.
3196 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3198 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );