pop 7b3ecd624d768eecb2eda237f54815110f480faa
[wine/hacks.git] / dlls / user32 / cursoricon.c
blob856d90007049c91fc4f168d8f749413d8b9993f3
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 * Cursors and icons are stored in a global heap block, with the
29 * following layout:
31 * CURSORICONINFO info;
32 * BYTE[] ANDbits;
33 * BYTE[] XORbits;
35 * The bits structures are in the format of a device-dependent bitmap.
37 * This layout is very sub-optimal, as the bitmap bits are stored in
38 * the X client instead of in the server like other bitmaps; however,
39 * some programs (notably Paint Brush) expect to be able to manipulate
40 * the bits directly :-(
43 #include "config.h"
44 #include "wine/port.h"
46 #include <stdarg.h>
47 #include <string.h>
48 #include <stdlib.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/server.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
63 WINE_DECLARE_DEBUG_CHANNEL(icon);
64 WINE_DECLARE_DEBUG_CHANNEL(resource);
66 #include "pshpack1.h"
68 typedef struct {
69 BYTE bWidth;
70 BYTE bHeight;
71 BYTE bColorCount;
72 BYTE bReserved;
73 WORD xHotspot;
74 WORD yHotspot;
75 DWORD dwDIBSize;
76 DWORD dwDIBOffset;
77 } CURSORICONFILEDIRENTRY;
79 typedef struct
81 WORD idReserved;
82 WORD idType;
83 WORD idCount;
84 CURSORICONFILEDIRENTRY idEntries[1];
85 } CURSORICONFILEDIR;
87 #include "poppack.h"
89 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
91 static HDC screen_dc;
93 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96 /**********************************************************************
97 * ICONCACHE for cursors/icons loaded with LR_SHARED.
99 * FIXME: This should not be allocated on the system heap, but on a
100 * subsystem-global heap (i.e. one for all Win16 processes,
101 * and one for each Win32 process).
103 typedef struct tagICONCACHE
105 struct tagICONCACHE *next;
107 HMODULE hModule;
108 HRSRC hRsrc;
109 HRSRC hGroupRsrc;
110 HICON hIcon;
112 INT count;
114 } ICONCACHE;
116 static ICONCACHE *IconAnchor = NULL;
118 static CRITICAL_SECTION IconCrst;
119 static CRITICAL_SECTION_DEBUG critsect_debug =
121 0, 0, &IconCrst,
122 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127 static const WORD ICON_HOTSPOT = 0x4242;
130 /**********************************************************************
131 * User objects management
134 struct cursoricon_object
136 struct user_object obj; /* object header */
137 ULONG_PTR param; /* opaque param used by 16-bit code */
138 /* followed by cursor data in CURSORICONINFO format */
141 static HICON alloc_icon_handle( unsigned int size )
143 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) + size );
144 if (!obj) return 0;
145 obj->param = 0;
146 return alloc_user_handle( &obj->obj, USER_ICON );
149 static struct tagCURSORICONINFO *get_icon_ptr( HICON handle )
151 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
152 if (obj == OBJ_OTHER_PROCESS)
154 WARN( "icon handle %p from other process\n", handle );
155 obj = NULL;
157 return obj ? (struct tagCURSORICONINFO *)(obj + 1) : NULL;
160 static void release_icon_ptr( HICON handle, struct tagCURSORICONINFO *ptr )
162 release_user_handle_ptr( (struct cursoricon_object *)ptr - 1 );
165 static BOOL free_icon_handle( HICON handle )
167 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
169 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
170 else if (obj)
172 ULONG_PTR param = obj->param;
173 HeapFree( GetProcessHeap(), 0, obj );
174 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
175 USER_Driver->pDestroyCursorIcon( handle );
176 return TRUE;
178 return FALSE;
181 ULONG_PTR get_icon_param( HICON handle )
183 ULONG_PTR ret = 0;
184 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
186 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
187 else if (obj)
189 ret = obj->param;
190 release_user_handle_ptr( obj );
192 return ret;
195 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
197 ULONG_PTR ret = 0;
198 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
200 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
201 else if (obj)
203 ret = obj->param;
204 obj->param = param;
205 release_user_handle_ptr( obj );
207 return ret;
211 /***********************************************************************
212 * map_fileW
214 * Helper function to map a file to memory:
215 * name - file name
216 * [RETURN] ptr - pointer to mapped file
217 * [RETURN] filesize - pointer size of file to be stored if not NULL
219 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
221 HANDLE hFile, hMapping;
222 LPVOID ptr = NULL;
224 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
225 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
226 if (hFile != INVALID_HANDLE_VALUE)
228 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
229 if (hMapping)
231 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
232 CloseHandle( hMapping );
233 if (filesize)
234 *filesize = GetFileSize( hFile, NULL );
236 CloseHandle( hFile );
238 return ptr;
242 /***********************************************************************
243 * get_bitmap_width_bytes
245 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
246 * data.
248 static int get_bitmap_width_bytes( int width, int bpp )
250 switch(bpp)
252 case 1:
253 return 2 * ((width+15) / 16);
254 case 4:
255 return 2 * ((width+3) / 4);
256 case 24:
257 width *= 3;
258 /* fall through */
259 case 8:
260 return width + (width & 1);
261 case 16:
262 case 15:
263 return width * 2;
264 case 32:
265 return width * 4;
266 default:
267 WARN("Unknown depth %d, please report.\n", bpp );
269 return -1;
273 /***********************************************************************
274 * get_dib_width_bytes
276 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
278 static int get_dib_width_bytes( int width, int depth )
280 int words;
282 switch(depth)
284 case 1: words = (width + 31) / 32; break;
285 case 4: words = (width + 7) / 8; break;
286 case 8: words = (width + 3) / 4; break;
287 case 15:
288 case 16: words = (width + 1) / 2; break;
289 case 24: words = (width * 3 + 3)/4; break;
290 default:
291 WARN("(%d): Unsupported depth\n", depth );
292 /* fall through */
293 case 32:
294 words = width;
296 return 4 * words;
300 /***********************************************************************
301 * bitmap_info_size
303 * Return the size of the bitmap info structure including color table.
305 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
307 int colors, masks = 0;
309 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
311 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
312 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
313 return sizeof(BITMAPCOREHEADER) + colors *
314 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
316 else /* assume BITMAPINFOHEADER */
318 colors = info->bmiHeader.biClrUsed;
319 if (colors > 256) /* buffer overflow otherwise */
320 colors = 256;
321 if (!colors && (info->bmiHeader.biBitCount <= 8))
322 colors = 1 << info->bmiHeader.biBitCount;
323 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
324 return info->bmiHeader.biSize + masks * sizeof(DWORD) + colors *
325 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
330 /***********************************************************************
331 * is_dib_monochrome
333 * Returns whether a DIB can be converted to a monochrome DDB.
335 * A DIB can be converted if its color table contains only black and
336 * white. Black must be the first color in the color table.
338 * Note : If the first color in the color table is white followed by
339 * black, we can't convert it to a monochrome DDB with
340 * SetDIBits, because black and white would be inverted.
342 static BOOL is_dib_monochrome( const BITMAPINFO* info )
344 if (info->bmiHeader.biBitCount != 1) return FALSE;
346 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
348 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
350 /* Check if the first color is black */
351 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
353 rgb++;
355 /* Check if the second color is white */
356 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
357 && (rgb->rgbtBlue == 0xff));
359 else return FALSE;
361 else /* assume BITMAPINFOHEADER */
363 const RGBQUAD *rgb = info->bmiColors;
365 /* Check if the first color is black */
366 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
367 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
369 rgb++;
371 /* Check if the second color is white */
372 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
373 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
375 else return FALSE;
379 /***********************************************************************
380 * DIB_GetBitmapInfo
382 * Get the info from a bitmap header.
383 * Return 1 for INFOHEADER, 0 for COREHEADER,
385 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
386 LONG *height, WORD *bpp, DWORD *compr )
388 if (header->biSize == sizeof(BITMAPCOREHEADER))
390 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
391 *width = core->bcWidth;
392 *height = core->bcHeight;
393 *bpp = core->bcBitCount;
394 *compr = 0;
395 return 0;
397 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
399 *width = header->biWidth;
400 *height = header->biHeight;
401 *bpp = header->biBitCount;
402 *compr = header->biCompression;
403 return 1;
405 ERR("(%d): unknown/wrong size for header\n", header->biSize );
406 return -1;
409 /**********************************************************************
410 * CURSORICON_FindSharedIcon
412 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
414 HICON hIcon = 0;
415 ICONCACHE *ptr;
417 EnterCriticalSection( &IconCrst );
419 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
420 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
422 ptr->count++;
423 hIcon = ptr->hIcon;
424 break;
427 LeaveCriticalSection( &IconCrst );
429 return hIcon;
432 /*************************************************************************
433 * CURSORICON_FindCache
435 * Given a handle, find the corresponding cache element
437 * PARAMS
438 * Handle [I] handle to an Image
440 * RETURNS
441 * Success: The cache entry
442 * Failure: NULL
445 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
447 ICONCACHE *ptr;
448 ICONCACHE *pRet=NULL;
449 BOOL IsFound = FALSE;
451 EnterCriticalSection( &IconCrst );
453 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
455 if ( hIcon == ptr->hIcon )
457 IsFound = TRUE;
458 pRet = ptr;
462 LeaveCriticalSection( &IconCrst );
464 return pRet;
467 /**********************************************************************
468 * CURSORICON_AddSharedIcon
470 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
472 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
473 if ( !ptr ) return;
475 ptr->hModule = hModule;
476 ptr->hRsrc = hRsrc;
477 ptr->hIcon = hIcon;
478 ptr->hGroupRsrc = hGroupRsrc;
479 ptr->count = 1;
481 EnterCriticalSection( &IconCrst );
482 ptr->next = IconAnchor;
483 IconAnchor = ptr;
484 LeaveCriticalSection( &IconCrst );
487 /**********************************************************************
488 * CURSORICON_DelSharedIcon
490 static INT CURSORICON_DelSharedIcon( HICON hIcon )
492 INT count = -1;
493 ICONCACHE *ptr;
495 EnterCriticalSection( &IconCrst );
497 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
498 if ( ptr->hIcon == hIcon )
500 if ( ptr->count > 0 ) ptr->count--;
501 count = ptr->count;
502 break;
505 LeaveCriticalSection( &IconCrst );
507 return count;
510 /**********************************************************************
511 * get_icon_size
513 BOOL get_icon_size( HICON handle, SIZE *size )
515 CURSORICONINFO *info;
517 if (!(info = get_icon_ptr( handle ))) return FALSE;
518 size->cx = info->nWidth;
519 size->cy = info->nHeight;
520 release_icon_ptr( handle, info );
521 return TRUE;
525 * The following macro functions account for the irregularities of
526 * accessing cursor and icon resources in files and resource entries.
528 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
529 int *width, int *height, int *bits );
531 /**********************************************************************
532 * CURSORICON_FindBestIcon
534 * Find the icon closest to the requested size and bit depth.
536 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
537 int width, int height, int depth )
539 int i, cx, cy, bits, bestEntry = -1;
540 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
541 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
543 /* Find Best Fit */
544 iTotalDiff = 0xFFFFFFFF;
545 iColorDiff = 0xFFFFFFFF;
546 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
548 iTempXDiff = abs(width - cx);
549 iTempYDiff = abs(height - cy);
551 if(iTotalDiff > (iTempXDiff + iTempYDiff))
553 iXDiff = iTempXDiff;
554 iYDiff = iTempYDiff;
555 iTotalDiff = iXDiff + iYDiff;
559 /* Find Best Colors for Best Fit */
560 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
562 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
564 iTempColorDiff = abs(depth - bits);
565 if(iColorDiff > iTempColorDiff)
567 bestEntry = i;
568 iColorDiff = iTempColorDiff;
573 return bestEntry;
576 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
577 int *width, int *height, int *bits )
579 CURSORICONDIR *resdir = dir;
580 ICONRESDIR *icon;
582 if ( resdir->idCount <= n )
583 return FALSE;
584 icon = &resdir->idEntries[n].ResInfo.icon;
585 *width = icon->bWidth;
586 *height = icon->bHeight;
587 *bits = resdir->idEntries[n].wBitCount;
588 return TRUE;
591 /**********************************************************************
592 * CURSORICON_FindBestCursor
594 * Find the cursor closest to the requested size.
596 * FIXME: parameter 'color' ignored.
598 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
599 int width, int height, int depth )
601 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
603 /* Double height to account for AND and XOR masks */
605 height *= 2;
607 /* First find the largest one smaller than or equal to the requested size*/
609 maxwidth = maxheight = 0;
610 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
612 if ((cx <= width) && (cy <= height) &&
613 (cx > maxwidth) && (cy > maxheight))
615 bestEntry = i;
616 maxwidth = cx;
617 maxheight = cy;
620 if (bestEntry != -1) return bestEntry;
622 /* Now find the smallest one larger than the requested size */
624 maxwidth = maxheight = 255;
625 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
627 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
629 bestEntry = i;
630 maxwidth = cx;
631 maxheight = cy;
635 return bestEntry;
638 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
639 int *width, int *height, int *bits )
641 CURSORICONDIR *resdir = dir;
642 CURSORDIR *cursor;
644 if ( resdir->idCount <= n )
645 return FALSE;
646 cursor = &resdir->idEntries[n].ResInfo.cursor;
647 *width = cursor->wWidth;
648 *height = cursor->wHeight;
649 *bits = resdir->idEntries[n].wBitCount;
650 return TRUE;
653 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
654 int width, int height, int depth )
656 int n;
658 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
659 width, height, depth );
660 if ( n < 0 )
661 return NULL;
662 return &dir->idEntries[n];
665 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
666 int width, int height, int depth )
668 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
669 width, height, depth );
670 if ( n < 0 )
671 return NULL;
672 return &dir->idEntries[n];
675 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
676 int *width, int *height, int *bits )
678 CURSORICONFILEDIR *filedir = dir;
679 CURSORICONFILEDIRENTRY *entry;
680 BITMAPINFOHEADER *info;
682 if ( filedir->idCount <= n )
683 return FALSE;
684 entry = &filedir->idEntries[n];
685 /* FIXME: check against file size */
686 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
687 *width = entry->bWidth;
688 *height = entry->bHeight;
689 *bits = info->biBitCount;
690 return TRUE;
693 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
694 int width, int height, int depth )
696 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
697 width, height, depth );
698 if ( n < 0 )
699 return NULL;
700 return &dir->idEntries[n];
703 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
704 int width, int height, int depth )
706 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
707 width, height, depth );
708 if ( n < 0 )
709 return NULL;
710 return &dir->idEntries[n];
713 /***********************************************************************
714 * stretch_blt_icon
716 * A helper function that stretches a bitmap buffer into an HBITMAP.
718 * PARAMS
719 * hDest [I] The handle of the destination bitmap.
720 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
721 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
722 * pSrcBits [I] A pointer to the source bitmap buffer.
724 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
726 HBITMAP hOld;
727 BOOL res = FALSE;
728 HDC hdcMem = CreateCompatibleDC(screen_dc);
730 if (hdcMem)
732 hOld = SelectObject(hdcMem, hDest);
733 res = StretchDIBits(hdcMem,
734 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
735 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
736 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
737 SelectObject(hdcMem, hOld);
738 DeleteDC( hdcMem );
741 return res;
744 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
745 POINT16 hotspot, BOOL bIcon,
746 DWORD dwVersion,
747 INT width, INT height,
748 UINT cFlag )
750 HICON hObj;
751 int sizeAnd, sizeXor;
752 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
753 BITMAP bmpXor, bmpAnd;
754 BOOL do_stretch;
755 INT size;
756 BITMAPINFO *pSrcInfo, *pDestInfo;
758 if (dwVersion == 0x00020000)
760 FIXME_(cursor)("\t2.xx resources are not supported\n");
761 return 0;
764 /* Check bitmap header */
766 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
767 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
768 bmi->bmiHeader.biCompression != BI_RGB) )
770 WARN_(cursor)("\tinvalid resource bitmap header.\n");
771 return 0;
774 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
776 if (!width) width = bmi->bmiHeader.biWidth;
777 if (!height) height = bmi->bmiHeader.biHeight/2;
778 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
779 (bmi->bmiHeader.biWidth != width);
781 /* Scale the hotspot */
782 if (do_stretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
784 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
785 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
788 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
789 if (screen_dc)
791 /* Make sure we have room for the monochrome bitmap later on.
792 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
793 * up to and including the biBitCount. In-memory icon resource
794 * format is as follows:
796 * BITMAPINFOHEADER icHeader // DIB header
797 * RGBQUAD icColors[] // Color table
798 * BYTE icXOR[] // DIB bits for XOR mask
799 * BYTE icAND[] // DIB bits for AND mask
802 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
803 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
804 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
805 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
806 if (pSrcInfo && pDestInfo)
808 memcpy( pSrcInfo, bmi, size );
809 pSrcInfo->bmiHeader.biHeight /= 2;
811 memcpy( pDestInfo, bmi, size );
812 pDestInfo->bmiHeader.biWidth = width;
813 pDestInfo->bmiHeader.biHeight = height;
814 pDestInfo->bmiHeader.biSizeImage = 0;
816 /* Create the XOR bitmap */
817 if(pSrcInfo->bmiHeader.biBitCount == 32)
819 void *pDIBBuffer = NULL;
820 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
822 if(hXorBits)
824 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
826 DeleteObject(hXorBits);
827 hXorBits = 0;
831 else
833 if (do_stretch)
835 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
836 if (hXorBits)
838 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
840 DeleteObject(hXorBits);
841 hXorBits = 0;
845 else
847 if (is_dib_monochrome(bmi))
849 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
850 SetDIBits(screen_dc, hXorBits, 0, height,
851 (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
853 else
854 hXorBits = CreateDIBitmap(screen_dc, &pSrcInfo->bmiHeader,
855 CBM_INIT, (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
859 if( hXorBits )
861 char* xbits = (char *)bmi + size +
862 get_dib_width_bytes( bmi->bmiHeader.biWidth,
863 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
865 pSrcInfo->bmiHeader.biBitCount = 1;
866 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
868 RGBQUAD *rgb = pSrcInfo->bmiColors;
870 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
871 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
872 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
873 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
875 else
877 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
879 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
880 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
883 /* Create the AND bitmap */
884 if (do_stretch)
886 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
888 if (!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
890 DeleteObject(hAndBits);
891 hAndBits = 0;
894 else
896 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
897 SetDIBits(screen_dc, hAndBits, 0, height,
898 xbits, pSrcInfo, DIB_RGB_COLORS);
901 if( !hAndBits )
903 DeleteObject( hXorBits );
904 hXorBits = 0;
909 HeapFree( GetProcessHeap(), 0, pSrcInfo );
910 HeapFree( GetProcessHeap(), 0, pDestInfo );
913 if( !hXorBits || !hAndBits )
915 WARN_(cursor)("\tunable to create an icon bitmap.\n");
916 return 0;
919 /* Now create the CURSORICONINFO structure */
920 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
921 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
922 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
923 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
925 hObj = alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
926 if (hObj)
928 CURSORICONINFO *info = get_icon_ptr( hObj );
930 info->ptHotSpot.x = hotspot.x;
931 info->ptHotSpot.y = hotspot.y;
932 info->nWidth = bmpXor.bmWidth;
933 info->nHeight = bmpXor.bmHeight;
934 info->nWidthBytes = bmpXor.bmWidthBytes;
935 info->bPlanes = bmpXor.bmPlanes;
936 info->bBitsPerPixel = bmpXor.bmBitsPixel;
938 /* Transfer the bitmap bits to the CURSORICONINFO structure */
940 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
941 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
942 release_icon_ptr( hObj, info );
943 USER_Driver->pCreateCursorIcon( hObj, info );
946 DeleteObject( hAndBits );
947 DeleteObject( hXorBits );
948 return hObj;
952 /**********************************************************************
953 * .ANI cursor support
955 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
956 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
957 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
959 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
960 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
961 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
962 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
963 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
964 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
966 #define ANI_FLAG_ICON 0x1
967 #define ANI_FLAG_SEQUENCE 0x2
969 typedef struct {
970 DWORD header_size;
971 DWORD num_frames;
972 DWORD num_steps;
973 DWORD width;
974 DWORD height;
975 DWORD bpp;
976 DWORD num_planes;
977 DWORD display_rate;
978 DWORD flags;
979 } ani_header;
981 typedef struct {
982 DWORD data_size;
983 const unsigned char *data;
984 } riff_chunk_t;
986 static void dump_ani_header( const ani_header *header )
988 TRACE(" header size: %d\n", header->header_size);
989 TRACE(" frames: %d\n", header->num_frames);
990 TRACE(" steps: %d\n", header->num_steps);
991 TRACE(" width: %d\n", header->width);
992 TRACE(" height: %d\n", header->height);
993 TRACE(" bpp: %d\n", header->bpp);
994 TRACE(" planes: %d\n", header->num_planes);
995 TRACE(" display rate: %d\n", header->display_rate);
996 TRACE(" flags: 0x%08x\n", header->flags);
1001 * RIFF:
1002 * DWORD "RIFF"
1003 * DWORD size
1004 * DWORD riff_id
1005 * BYTE[] data
1007 * LIST:
1008 * DWORD "LIST"
1009 * DWORD size
1010 * DWORD list_id
1011 * BYTE[] data
1013 * CHUNK:
1014 * DWORD chunk_id
1015 * DWORD size
1016 * BYTE[] data
1018 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1020 const unsigned char *ptr = parent_chunk->data;
1021 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1023 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1025 while (ptr < end)
1027 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1028 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1030 ptr += sizeof(DWORD);
1031 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1032 ptr += sizeof(DWORD);
1033 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1034 chunk->data = ptr;
1036 return;
1039 ptr += sizeof(DWORD);
1040 ptr += (*(const DWORD *)ptr + 1) & ~1;
1041 ptr += sizeof(DWORD);
1047 * .ANI layout:
1049 * RIFF:'ACON' RIFF chunk
1050 * |- CHUNK:'anih' Header
1051 * |- CHUNK:'seq ' Sequence information (optional)
1052 * \- LIST:'fram' Frame list
1053 * |- CHUNK:icon Cursor frames
1054 * |- CHUNK:icon
1055 * |- ...
1056 * \- CHUNK:icon
1058 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1059 INT width, INT height, INT depth )
1061 HCURSOR cursor;
1062 ani_header header = {0};
1063 LPBYTE frame_bits = 0;
1064 POINT16 hotspot;
1065 CURSORICONFILEDIRENTRY *entry;
1067 riff_chunk_t root_chunk = { bits_size, bits };
1068 riff_chunk_t ACON_chunk = {0};
1069 riff_chunk_t anih_chunk = {0};
1070 riff_chunk_t fram_chunk = {0};
1071 const unsigned char *icon_data;
1073 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1075 if (!bits) return 0;
1077 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1078 if (!ACON_chunk.data)
1080 ERR("Failed to get root chunk.\n");
1081 return 0;
1084 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1085 if (!anih_chunk.data)
1087 ERR("Failed to get 'anih' chunk.\n");
1088 return 0;
1090 memcpy( &header, anih_chunk.data, sizeof(header) );
1091 dump_ani_header( &header );
1093 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1094 if (!fram_chunk.data)
1096 ERR("Failed to get icon list.\n");
1097 return 0;
1100 /* FIXME: For now, just load the first frame. Before we can load all the
1101 * frames, we need to write the needed code in wineserver, etc. to handle
1102 * cursors. Once this code is written, we can extend it to support .ani
1103 * cursors and then update user32 and winex11.drv to load all frames.
1105 * Hopefully this will at least make some games (C&C3, etc.) more playable
1106 * in the meantime.
1108 FIXME("Loading all frames for .ani cursors not implemented.\n");
1109 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1111 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1112 width, height, depth );
1114 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1115 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1117 if (!header.width || !header.height)
1119 header.width = entry->bWidth;
1120 header.height = entry->bHeight;
1123 hotspot.x = entry->xHotspot;
1124 hotspot.y = entry->yHotspot;
1126 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1127 FALSE, 0x00030000, header.width, header.height, 0 );
1129 HeapFree( GetProcessHeap(), 0, frame_bits );
1131 return cursor;
1135 /**********************************************************************
1136 * CreateIconFromResourceEx (USER32.@)
1138 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1139 * with cbSize parameter as well.
1141 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1142 BOOL bIcon, DWORD dwVersion,
1143 INT width, INT height,
1144 UINT cFlag )
1146 POINT16 hotspot;
1147 BITMAPINFO *bmi;
1149 hotspot.x = ICON_HOTSPOT;
1150 hotspot.y = ICON_HOTSPOT;
1152 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1153 bits, cbSize, dwVersion, width, height,
1154 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1156 if (bIcon)
1157 bmi = (BITMAPINFO *)bits;
1158 else /* get the hotspot */
1160 POINT16 *pt = (POINT16 *)bits;
1161 hotspot = *pt;
1162 bmi = (BITMAPINFO *)(pt + 1);
1165 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1166 width, height, cFlag );
1170 /**********************************************************************
1171 * CreateIconFromResource (USER32.@)
1173 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1174 BOOL bIcon, DWORD dwVersion)
1176 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1180 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1181 INT width, INT height, INT depth,
1182 BOOL fCursor, UINT loadflags)
1184 CURSORICONFILEDIRENTRY *entry;
1185 CURSORICONFILEDIR *dir;
1186 DWORD filesize = 0;
1187 HICON hIcon = 0;
1188 LPBYTE bits;
1189 POINT16 hotspot;
1191 TRACE("loading %s\n", debugstr_w( filename ));
1193 bits = map_fileW( filename, &filesize );
1194 if (!bits)
1195 return hIcon;
1197 /* Check for .ani. */
1198 if (memcmp( bits, "RIFF", 4 ) == 0)
1200 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1201 depth );
1202 goto end;
1205 dir = (CURSORICONFILEDIR*) bits;
1206 if ( filesize < sizeof(*dir) )
1207 goto end;
1209 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1210 goto end;
1212 if ( fCursor )
1213 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1214 else
1215 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1217 if ( !entry )
1218 goto end;
1220 /* check that we don't run off the end of the file */
1221 if ( entry->dwDIBOffset > filesize )
1222 goto end;
1223 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1224 goto end;
1226 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1227 if ( fCursor )
1229 hotspot.x = entry->xHotspot;
1230 hotspot.y = entry->yHotspot;
1232 else
1234 hotspot.x = ICON_HOTSPOT;
1235 hotspot.y = ICON_HOTSPOT;
1237 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1238 hotspot, !fCursor, 0x00030000,
1239 width, height, loadflags );
1240 end:
1241 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1242 UnmapViewOfFile( bits );
1243 return hIcon;
1246 /**********************************************************************
1247 * CURSORICON_Load
1249 * Load a cursor or icon from resource or file.
1251 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1252 INT width, INT height, INT depth,
1253 BOOL fCursor, UINT loadflags)
1255 HANDLE handle = 0;
1256 HICON hIcon = 0;
1257 HRSRC hRsrc, hGroupRsrc;
1258 CURSORICONDIR *dir;
1259 CURSORICONDIRENTRY *dirEntry;
1260 LPBYTE bits;
1261 WORD wResId;
1262 DWORD dwBytesInRes;
1264 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1265 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1267 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1268 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1270 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1272 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1273 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1275 /* Get directory resource ID */
1277 if (!(hRsrc = FindResourceW( hInstance, name,
1278 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1279 return 0;
1280 hGroupRsrc = hRsrc;
1282 /* Find the best entry in the directory */
1284 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1285 if (!(dir = LockResource( handle ))) return 0;
1286 if (fCursor)
1287 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1288 else
1289 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1290 if (!dirEntry) return 0;
1291 wResId = dirEntry->wResId;
1292 dwBytesInRes = dirEntry->dwBytesInRes;
1293 FreeResource( handle );
1295 /* Load the resource */
1297 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1298 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1300 /* If shared icon, check whether it was already loaded */
1301 if ( (loadflags & LR_SHARED)
1302 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1303 return hIcon;
1305 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1306 bits = LockResource( handle );
1307 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1308 !fCursor, 0x00030000, width, height, loadflags);
1309 FreeResource( handle );
1311 /* If shared icon, add to icon cache */
1313 if ( hIcon && (loadflags & LR_SHARED) )
1314 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1316 return hIcon;
1320 /*************************************************************************
1321 * CURSORICON_ExtCopy
1323 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1325 * PARAMS
1326 * Handle [I] handle to an Image
1327 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1328 * iDesiredCX [I] The Desired width of the Image
1329 * iDesiredCY [I] The desired height of the Image
1330 * nFlags [I] The flags from CopyImage
1332 * RETURNS
1333 * Success: The new handle of the Image
1335 * NOTES
1336 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1337 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1338 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1343 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1344 INT iDesiredCX, INT iDesiredCY,
1345 UINT nFlags)
1347 HICON hNew=0;
1349 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1350 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1352 if(hIcon == 0)
1354 return 0;
1357 /* Best Fit or Monochrome */
1358 if( (nFlags & LR_COPYFROMRESOURCE
1359 && (iDesiredCX > 0 || iDesiredCY > 0))
1360 || nFlags & LR_MONOCHROME)
1362 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1364 /* Not Found in Cache, then do a straight copy
1366 if(pIconCache == NULL)
1368 hNew = CopyIcon( hIcon );
1369 if(nFlags & LR_COPYFROMRESOURCE)
1371 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1374 else
1376 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1377 LPBYTE pBits;
1378 HANDLE hMem;
1379 HRSRC hRsrc;
1380 DWORD dwBytesInRes;
1381 WORD wResId;
1382 CURSORICONDIR *pDir;
1383 CURSORICONDIRENTRY *pDirEntry;
1384 BOOL bIsIcon = (nType == IMAGE_ICON);
1386 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1388 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1389 || (iDesiredCX == 0 && iDesiredCY == 0))
1391 iDesiredCY = GetSystemMetrics(bIsIcon ?
1392 SM_CYICON : SM_CYCURSOR);
1393 iDesiredCX = GetSystemMetrics(bIsIcon ?
1394 SM_CXICON : SM_CXCURSOR);
1397 /* Retrieve the CURSORICONDIRENTRY
1399 if (!(hMem = LoadResource( pIconCache->hModule ,
1400 pIconCache->hGroupRsrc)))
1402 return 0;
1404 if (!(pDir = LockResource( hMem )))
1406 return 0;
1409 /* Find Best Fit
1411 if(bIsIcon)
1413 pDirEntry = CURSORICON_FindBestIconRes(
1414 pDir, iDesiredCX, iDesiredCY, 256 );
1416 else
1418 pDirEntry = CURSORICON_FindBestCursorRes(
1419 pDir, iDesiredCX, iDesiredCY, 1);
1422 wResId = pDirEntry->wResId;
1423 dwBytesInRes = pDirEntry->dwBytesInRes;
1424 FreeResource(hMem);
1426 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1427 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1428 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1430 /* Get the Best Fit
1432 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1433 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1435 return 0;
1437 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1439 return 0;
1442 pBits = LockResource( hMem );
1444 if(nFlags & LR_DEFAULTSIZE)
1446 iTargetCY = GetSystemMetrics(SM_CYICON);
1447 iTargetCX = GetSystemMetrics(SM_CXICON);
1450 /* Create a New Icon with the proper dimension
1452 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1453 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1454 FreeResource(hMem);
1457 else hNew = CopyIcon( hIcon );
1458 return hNew;
1462 /***********************************************************************
1463 * CreateCursor (USER32.@)
1465 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1466 INT xHotSpot, INT yHotSpot,
1467 INT nWidth, INT nHeight,
1468 LPCVOID lpANDbits, LPCVOID lpXORbits )
1470 ICONINFO info;
1471 HCURSOR hCursor;
1473 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1474 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1476 info.fIcon = FALSE;
1477 info.xHotspot = xHotSpot;
1478 info.yHotspot = yHotSpot;
1479 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1480 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1481 hCursor = CreateIconIndirect( &info );
1482 DeleteObject( info.hbmMask );
1483 DeleteObject( info.hbmColor );
1484 return hCursor;
1488 /***********************************************************************
1489 * CreateIcon (USER32.@)
1491 * Creates an icon based on the specified bitmaps. The bitmaps must be
1492 * provided in a device dependent format and will be resized to
1493 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1494 * depth. The provided bitmaps must be top-down bitmaps.
1495 * Although Windows does not support 15bpp(*) this API must support it
1496 * for Winelib applications.
1498 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1499 * format!
1501 * RETURNS
1502 * Success: handle to an icon
1503 * Failure: NULL
1505 * FIXME: Do we need to resize the bitmaps?
1507 HICON WINAPI CreateIcon(
1508 HINSTANCE hInstance, /* [in] the application's hInstance */
1509 INT nWidth, /* [in] the width of the provided bitmaps */
1510 INT nHeight, /* [in] the height of the provided bitmaps */
1511 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1512 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1513 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1514 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1516 ICONINFO iinfo;
1517 HICON hIcon;
1519 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1520 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1522 iinfo.fIcon = TRUE;
1523 iinfo.xHotspot = ICON_HOTSPOT;
1524 iinfo.yHotspot = ICON_HOTSPOT;
1525 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1526 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1528 hIcon = CreateIconIndirect( &iinfo );
1530 DeleteObject( iinfo.hbmMask );
1531 DeleteObject( iinfo.hbmColor );
1533 return hIcon;
1537 /***********************************************************************
1538 * CopyIcon (USER32.@)
1540 HICON WINAPI CopyIcon( HICON hIcon )
1542 CURSORICONINFO *ptrOld, *ptrNew;
1543 int size;
1544 HICON hNew;
1546 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1547 size = sizeof(CURSORICONINFO);
1548 size += ptrOld->nHeight * get_bitmap_width_bytes( ptrOld->nWidth, 1 ); /* and bitmap */
1549 size += ptrOld->nHeight * ptrOld->nWidthBytes; /* xor bitmap */
1550 hNew = alloc_icon_handle( size );
1551 ptrNew = get_icon_ptr( hNew );
1552 memcpy( ptrNew, ptrOld, size );
1553 release_icon_ptr( hIcon, ptrOld );
1554 release_icon_ptr( hNew, ptrNew );
1555 USER_Driver->pCreateCursorIcon( hNew, ptrNew );
1556 return hNew;
1560 /***********************************************************************
1561 * DestroyIcon (USER32.@)
1563 BOOL WINAPI DestroyIcon( HICON hIcon )
1565 TRACE_(icon)("%p\n", hIcon );
1567 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1568 free_icon_handle( hIcon );
1569 return TRUE;
1573 /***********************************************************************
1574 * DestroyCursor (USER32.@)
1576 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1578 if (GetCursor() == hCursor)
1580 WARN_(cursor)("Destroying active cursor!\n" );
1581 return FALSE;
1583 return DestroyIcon( hCursor );
1586 /***********************************************************************
1587 * bitmap_has_alpha_channel
1589 * Analyses bits bitmap to determine if alpha data is present.
1591 * PARAMS
1592 * bpp [I] The bits-per-pixel of the bitmap
1593 * bitmapBits [I] A pointer to the bitmap data
1594 * bitmapLength [I] The length of the bitmap in bytes
1596 * RETURNS
1597 * TRUE if an alpha channel is discovered, FALSE
1599 * NOTE
1600 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1601 * least one pixel has a non-zero alpha, then the bitmap is a
1602 * treated as having an alpha channel transparentcy. Otherwise,
1603 * it's treated as being completely opaque.
1606 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1607 unsigned int bitmapLength )
1609 /* Detect an alpha channel by looking for non-zero alpha pixels */
1610 if(bpp == 32)
1612 unsigned int offset;
1613 for(offset = 3; offset < bitmapLength; offset += 4)
1615 if(bitmapBits[offset] != 0)
1617 return TRUE;
1621 return FALSE;
1624 /***********************************************************************
1625 * premultiply_alpha_channel
1627 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1628 * channel. This is a necessary step that must be carried out on
1629 * the image before it is passed to GdiAlphaBlend
1631 * PARAMS
1632 * destBitmap [I] The destination bitmap buffer
1633 * srcBitmap [I] The source bitmap buffer
1634 * bitmapLength [I] The length of the bitmap in bytes
1637 static void premultiply_alpha_channel( unsigned char *destBitmap,
1638 unsigned char *srcBitmap,
1639 unsigned int bitmapLength )
1641 unsigned char *destPixel = destBitmap;
1642 unsigned char *srcPixel = srcBitmap;
1644 while(destPixel < destBitmap + bitmapLength)
1646 unsigned char alpha = srcPixel[3];
1647 *(destPixel++) = *(srcPixel++) * alpha / 255;
1648 *(destPixel++) = *(srcPixel++) * alpha / 255;
1649 *(destPixel++) = *(srcPixel++) * alpha / 255;
1650 *(destPixel++) = *(srcPixel++);
1654 /***********************************************************************
1655 * DrawIcon (USER32.@)
1657 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1659 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1662 /***********************************************************************
1663 * SetCursor (USER32.@)
1665 * Set the cursor shape.
1667 * RETURNS
1668 * A handle to the previous cursor shape.
1670 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1672 HCURSOR hOldCursor;
1673 int show_count;
1674 BOOL ret;
1676 TRACE("%p\n", hCursor);
1678 SERVER_START_REQ( set_cursor )
1680 req->flags = SET_CURSOR_HANDLE;
1681 req->handle = wine_server_user_handle( hCursor );
1682 if ((ret = !wine_server_call_err( req )))
1684 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1685 show_count = reply->prev_count;
1688 SERVER_END_REQ;
1690 if (!ret) return 0;
1692 /* Change the cursor shape only if it is visible */
1693 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1694 return hOldCursor;
1697 /***********************************************************************
1698 * ShowCursor (USER32.@)
1700 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1702 HCURSOR cursor;
1703 int increment = bShow ? 1 : -1;
1704 int prev_count;
1706 SERVER_START_REQ( set_cursor )
1708 req->flags = SET_CURSOR_COUNT;
1709 req->show_count = increment;
1710 wine_server_call( req );
1711 cursor = wine_server_ptr_handle( reply->prev_handle );
1712 prev_count = reply->prev_count;
1714 SERVER_END_REQ;
1716 TRACE("%d, count=%d\n", bShow, prev_count + increment );
1718 if (!prev_count) USER_Driver->pSetCursor( bShow ? cursor : 0 );
1720 return prev_count + increment;
1723 /***********************************************************************
1724 * GetCursor (USER32.@)
1726 HCURSOR WINAPI GetCursor(void)
1728 HCURSOR ret;
1730 SERVER_START_REQ( set_cursor )
1732 req->flags = 0;
1733 wine_server_call( req );
1734 ret = wine_server_ptr_handle( reply->prev_handle );
1736 SERVER_END_REQ;
1737 return ret;
1741 /***********************************************************************
1742 * ClipCursor (USER32.@)
1744 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1746 RECT virt;
1748 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1749 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1750 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1751 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1753 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1754 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1756 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1757 CURSOR_ClipRect = virt;
1759 USER_Driver->pClipCursor( rect );
1760 return TRUE;
1764 /***********************************************************************
1765 * GetClipCursor (USER32.@)
1767 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1769 /* If this is first time - initialize the rect */
1770 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1772 return CopyRect( rect, &CURSOR_ClipRect );
1776 /***********************************************************************
1777 * SetSystemCursor (USER32.@)
1779 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1781 FIXME("(%p,%08x),stub!\n", hcur, id);
1782 return TRUE;
1786 /**********************************************************************
1787 * LookupIconIdFromDirectoryEx (USER32.@)
1789 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1790 INT width, INT height, UINT cFlag )
1792 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1793 UINT retVal = 0;
1794 if( dir && !dir->idReserved && (dir->idType & 3) )
1796 CURSORICONDIRENTRY* entry;
1798 const HDC hdc = GetDC(0);
1799 const int depth = (cFlag & LR_MONOCHROME) ?
1800 1 : GetDeviceCaps(hdc, BITSPIXEL);
1801 ReleaseDC(0, hdc);
1803 if( bIcon )
1804 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1805 else
1806 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1808 if( entry ) retVal = entry->wResId;
1810 else WARN_(cursor)("invalid resource directory\n");
1811 return retVal;
1814 /**********************************************************************
1815 * LookupIconIdFromDirectory (USER32.@)
1817 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1819 return LookupIconIdFromDirectoryEx( dir, bIcon,
1820 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1821 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1824 /***********************************************************************
1825 * LoadCursorW (USER32.@)
1827 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1829 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1831 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1832 LR_SHARED | LR_DEFAULTSIZE );
1835 /***********************************************************************
1836 * LoadCursorA (USER32.@)
1838 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1840 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1842 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1843 LR_SHARED | LR_DEFAULTSIZE );
1846 /***********************************************************************
1847 * LoadCursorFromFileW (USER32.@)
1849 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1851 TRACE("%s\n", debugstr_w(name));
1853 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1854 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1857 /***********************************************************************
1858 * LoadCursorFromFileA (USER32.@)
1860 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1862 TRACE("%s\n", debugstr_a(name));
1864 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1865 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1868 /***********************************************************************
1869 * LoadIconW (USER32.@)
1871 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1873 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1875 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1876 LR_SHARED | LR_DEFAULTSIZE );
1879 /***********************************************************************
1880 * LoadIconA (USER32.@)
1882 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1884 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1886 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1887 LR_SHARED | LR_DEFAULTSIZE );
1890 /**********************************************************************
1891 * GetIconInfo (USER32.@)
1893 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1895 CURSORICONINFO *ciconinfo;
1896 INT height;
1898 if (!(ciconinfo = get_icon_ptr( hIcon ))) return FALSE;
1900 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1901 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1903 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1904 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1906 iconinfo->fIcon = TRUE;
1907 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1908 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1910 else
1912 iconinfo->fIcon = FALSE;
1913 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1914 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1917 height = ciconinfo->nHeight;
1919 if (ciconinfo->bBitsPerPixel > 1)
1921 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1922 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1923 (char *)(ciconinfo + 1)
1924 + ciconinfo->nHeight *
1925 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1927 else
1929 iconinfo->hbmColor = 0;
1930 height *= 2;
1933 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1934 1, 1, ciconinfo + 1);
1935 release_icon_ptr( hIcon, ciconinfo );
1937 return TRUE;
1940 /**********************************************************************
1941 * CreateIconIndirect (USER32.@)
1943 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1945 DIBSECTION bmpXor;
1946 BITMAP bmpAnd;
1947 HICON hObj;
1948 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1950 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1951 iconinfo->hbmColor, iconinfo->hbmMask,
1952 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1954 if (!iconinfo->hbmMask) return 0;
1956 planes = GetDeviceCaps( screen_dc, PLANES );
1957 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
1959 if (iconinfo->hbmColor)
1961 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1962 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1963 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
1964 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
1965 /* we can use either depth 1 or screen depth for xor bitmap */
1966 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
1967 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1969 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1970 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1971 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1972 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1974 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1976 hObj = alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1977 if (hObj)
1979 CURSORICONINFO *info = get_icon_ptr( hObj );
1981 /* If we are creating an icon, the hotspot is unused */
1982 if (iconinfo->fIcon)
1984 info->ptHotSpot.x = ICON_HOTSPOT;
1985 info->ptHotSpot.y = ICON_HOTSPOT;
1987 else
1989 info->ptHotSpot.x = iconinfo->xHotspot;
1990 info->ptHotSpot.y = iconinfo->yHotspot;
1993 if (iconinfo->hbmColor)
1995 info->nWidth = bmpXor.dsBm.bmWidth;
1996 info->nHeight = bmpXor.dsBm.bmHeight;
1997 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
1998 info->bPlanes = planes;
1999 info->bBitsPerPixel = bpp;
2001 else
2003 info->nWidth = bmpAnd.bmWidth;
2004 info->nHeight = bmpAnd.bmHeight / 2;
2005 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2006 info->bPlanes = 1;
2007 info->bBitsPerPixel = 1;
2010 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2012 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2013 if (bmpAnd.bmBitsPixel == 1)
2015 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2017 else
2019 HDC hdc_mem, hdc_mem2;
2020 HBITMAP hbmp_mem_old, hbmp_mem2_old, hbmp_mono;
2022 hdc_mem = CreateCompatibleDC( 0 );
2023 hdc_mem2 = CreateCompatibleDC( 0 );
2025 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2027 hbmp_mem_old = SelectObject( hdc_mem, iconinfo->hbmMask );
2028 hbmp_mem2_old = SelectObject( hdc_mem2, hbmp_mono );
2030 BitBlt( hdc_mem2, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc_mem, 0, 0, SRCCOPY );
2032 SelectObject( hdc_mem, hbmp_mem_old );
2033 SelectObject( hdc_mem2, hbmp_mem2_old );
2035 DeleteDC( hdc_mem );
2036 DeleteDC( hdc_mem2 );
2038 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2039 DeleteObject( hbmp_mono );
2042 if (iconinfo->hbmColor)
2044 char *dst_bits = (char*)(info + 1) + sizeAnd;
2046 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2047 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2048 else
2050 BITMAPINFO bminfo;
2051 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2052 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2054 bminfo.bmiHeader.biSize = sizeof(bminfo);
2055 bminfo.bmiHeader.biWidth = info->nWidth;
2056 bminfo.bmiHeader.biHeight = info->nHeight;
2057 bminfo.bmiHeader.biPlanes = info->bPlanes;
2058 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2059 bminfo.bmiHeader.biCompression = BI_RGB;
2060 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2061 bminfo.bmiHeader.biXPelsPerMeter = 0;
2062 bminfo.bmiHeader.biYPelsPerMeter = 0;
2063 bminfo.bmiHeader.biClrUsed = 0;
2064 bminfo.bmiHeader.biClrImportant = 0;
2066 /* swap lines for dib sections */
2067 if (xor_objsize == sizeof(DIBSECTION))
2068 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2070 if (dib_width != bitmap_width) /* need to fixup alignment */
2072 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2074 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2075 src_bits, &bminfo, DIB_RGB_COLORS ))
2077 int y;
2078 for (y = 0; y < info->nHeight; y++)
2079 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2081 HeapFree( GetProcessHeap(), 0, src_bits );
2083 else
2084 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2085 dst_bits, &bminfo, DIB_RGB_COLORS );
2088 release_icon_ptr( hObj, info );
2089 USER_Driver->pCreateCursorIcon( hObj, info );
2091 return hObj;
2094 /******************************************************************************
2095 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2097 * NOTES
2098 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2100 * PARAMS
2101 * hdc [I] Handle to device context
2102 * x0 [I] X coordinate of upper left corner
2103 * y0 [I] Y coordinate of upper left corner
2104 * hIcon [I] Handle to icon to draw
2105 * cxWidth [I] Width of icon
2106 * cyWidth [I] Height of icon
2107 * istep [I] Index of frame in animated cursor
2108 * hbr [I] Handle to background brush
2109 * flags [I] Icon-drawing flags
2111 * RETURNS
2112 * Success: TRUE
2113 * Failure: FALSE
2115 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2116 INT cxWidth, INT cyWidth, UINT istep,
2117 HBRUSH hbr, UINT flags )
2119 CURSORICONINFO *ptr;
2120 HDC hDC_off = 0, hMemDC;
2121 BOOL result = FALSE, DoOffscreen;
2122 HBITMAP hB_off = 0, hOld = 0;
2123 unsigned char *xorBitmapBits;
2124 unsigned int xorLength;
2125 BOOL has_alpha = FALSE;
2127 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2128 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2130 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2131 if (!(hMemDC = CreateCompatibleDC( hdc )))
2133 release_icon_ptr( hIcon, ptr );
2134 return FALSE;
2137 if (istep)
2138 FIXME_(icon)("Ignoring istep=%d\n", istep);
2139 if (flags & DI_NOMIRROR)
2140 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2142 xorLength = ptr->nHeight * get_bitmap_width_bytes(
2143 ptr->nWidth, ptr->bBitsPerPixel);
2144 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2145 get_bitmap_width_bytes(ptr->nWidth, 1);
2147 if (flags & DI_IMAGE)
2148 has_alpha = bitmap_has_alpha_channel(
2149 ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2151 /* Calculate the size of the destination image. */
2152 if (cxWidth == 0)
2154 if (flags & DI_DEFAULTSIZE)
2155 cxWidth = GetSystemMetrics (SM_CXICON);
2156 else
2157 cxWidth = ptr->nWidth;
2159 if (cyWidth == 0)
2161 if (flags & DI_DEFAULTSIZE)
2162 cyWidth = GetSystemMetrics (SM_CYICON);
2163 else
2164 cyWidth = ptr->nHeight;
2167 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2169 if (DoOffscreen) {
2170 RECT r;
2172 r.left = 0;
2173 r.top = 0;
2174 r.right = cxWidth;
2175 r.bottom = cxWidth;
2177 hDC_off = CreateCompatibleDC(hdc);
2178 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2179 if (hDC_off && hB_off) {
2180 hOld = SelectObject(hDC_off, hB_off);
2181 FillRect(hDC_off, &r, hbr);
2185 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2187 HBITMAP hBitTemp;
2188 HBITMAP hXorBits = NULL, hAndBits = NULL;
2189 COLORREF oldFg, oldBg;
2190 INT nStretchMode;
2192 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2194 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2195 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2197 if ((flags & DI_MASK) && !has_alpha)
2199 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2200 if (hAndBits)
2202 hBitTemp = SelectObject( hMemDC, hAndBits );
2203 if (DoOffscreen)
2204 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2205 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2206 else
2207 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2208 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2209 SelectObject( hMemDC, hBitTemp );
2213 if (flags & DI_IMAGE)
2215 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
2217 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, xorBitmapBits );
2219 else
2221 unsigned char *dibBits;
2222 BITMAPINFO *bmi = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2223 FIELD_OFFSET( BITMAPINFO, bmiColors[256] ));
2224 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2225 bmi->bmiHeader.biWidth = ptr->nWidth;
2226 bmi->bmiHeader.biHeight = -ptr->nHeight;
2227 bmi->bmiHeader.biPlanes = ptr->bPlanes;
2228 bmi->bmiHeader.biBitCount = ptr->bBitsPerPixel;
2229 bmi->bmiHeader.biCompression = BI_RGB;
2230 /* FIXME: color table */
2232 hXorBits = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, (void*)&dibBits, NULL, 0);
2233 if (hXorBits)
2235 if(has_alpha)
2236 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2237 else
2238 memcpy(dibBits, xorBitmapBits, xorLength);
2242 if (hXorBits)
2244 if(has_alpha)
2246 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2248 /* Do the alpha blending render */
2249 hBitTemp = SelectObject( hMemDC, hXorBits );
2251 if (DoOffscreen)
2252 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2253 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2254 else
2255 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2256 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2258 SelectObject( hMemDC, hBitTemp );
2260 else
2262 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2263 hBitTemp = SelectObject( hMemDC, hXorBits );
2264 if (DoOffscreen)
2265 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2266 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, rop);
2267 else
2268 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2269 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, rop);
2270 SelectObject( hMemDC, hBitTemp );
2273 DeleteObject( hXorBits );
2277 result = TRUE;
2279 SetTextColor( hdc, oldFg );
2280 SetBkColor( hdc, oldBg );
2282 if (hAndBits) DeleteObject( hAndBits );
2283 SetStretchBltMode (hdc, nStretchMode);
2284 if (DoOffscreen) {
2285 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2286 SelectObject(hDC_off, hOld);
2289 if (hMemDC) DeleteDC( hMemDC );
2290 if (hDC_off) DeleteDC(hDC_off);
2291 if (hB_off) DeleteObject(hB_off);
2292 release_icon_ptr( hIcon, ptr );
2293 return result;
2296 /***********************************************************************
2297 * DIB_FixColorsToLoadflags
2299 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2300 * are in loadflags
2302 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2304 int colors;
2305 COLORREF c_W, c_S, c_F, c_L, c_C;
2306 int incr,i;
2307 RGBQUAD *ptr;
2308 int bitmap_type;
2309 LONG width;
2310 LONG height;
2311 WORD bpp;
2312 DWORD compr;
2314 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2316 WARN_(resource)("Invalid bitmap\n");
2317 return;
2320 if (bpp > 8) return;
2322 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2324 incr = 3;
2325 colors = 1 << bpp;
2327 else
2329 incr = 4;
2330 colors = bmi->bmiHeader.biClrUsed;
2331 if (colors > 256) colors = 256;
2332 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2335 c_W = GetSysColor(COLOR_WINDOW);
2336 c_S = GetSysColor(COLOR_3DSHADOW);
2337 c_F = GetSysColor(COLOR_3DFACE);
2338 c_L = GetSysColor(COLOR_3DLIGHT);
2340 if (loadflags & LR_LOADTRANSPARENT) {
2341 switch (bpp) {
2342 case 1: pix = pix >> 7; break;
2343 case 4: pix = pix >> 4; break;
2344 case 8: break;
2345 default:
2346 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2347 return;
2349 if (pix >= colors) {
2350 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2351 return;
2353 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2354 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2355 ptr->rgbBlue = GetBValue(c_W);
2356 ptr->rgbGreen = GetGValue(c_W);
2357 ptr->rgbRed = GetRValue(c_W);
2359 if (loadflags & LR_LOADMAP3DCOLORS)
2360 for (i=0; i<colors; i++) {
2361 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2362 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2363 if (c_C == RGB(128, 128, 128)) {
2364 ptr->rgbRed = GetRValue(c_S);
2365 ptr->rgbGreen = GetGValue(c_S);
2366 ptr->rgbBlue = GetBValue(c_S);
2367 } else if (c_C == RGB(192, 192, 192)) {
2368 ptr->rgbRed = GetRValue(c_F);
2369 ptr->rgbGreen = GetGValue(c_F);
2370 ptr->rgbBlue = GetBValue(c_F);
2371 } else if (c_C == RGB(223, 223, 223)) {
2372 ptr->rgbRed = GetRValue(c_L);
2373 ptr->rgbGreen = GetGValue(c_L);
2374 ptr->rgbBlue = GetBValue(c_L);
2380 /**********************************************************************
2381 * BITMAP_Load
2383 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2384 INT desiredx, INT desiredy, UINT loadflags )
2386 HBITMAP hbitmap = 0, orig_bm;
2387 HRSRC hRsrc;
2388 HGLOBAL handle;
2389 char *ptr = NULL;
2390 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2391 int size;
2392 BYTE pix;
2393 char *bits;
2394 LONG width, height, new_width, new_height;
2395 WORD bpp_dummy;
2396 DWORD compr_dummy;
2397 INT bm_type;
2398 HDC screen_mem_dc = NULL;
2400 if (!(loadflags & LR_LOADFROMFILE))
2402 if (!instance)
2404 /* OEM bitmap: try to load the resource from user32.dll */
2405 instance = user32_module;
2408 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2409 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2411 if ((info = LockResource( handle )) == NULL) return 0;
2413 else
2415 BITMAPFILEHEADER * bmfh;
2417 if (!(ptr = map_fileW( name, NULL ))) return 0;
2418 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2419 bmfh = (BITMAPFILEHEADER *)ptr;
2420 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2422 WARN("Invalid/unsupported bitmap format!\n");
2423 UnmapViewOfFile( ptr );
2424 return 0;
2428 size = bitmap_info_size(info, DIB_RGB_COLORS);
2429 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2430 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2432 if (!fix_info || !scaled_info) goto end;
2433 memcpy(fix_info, info, size);
2435 pix = *((LPBYTE)info + size);
2436 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2438 memcpy(scaled_info, fix_info, size);
2439 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2440 &bpp_dummy, &compr_dummy);
2441 if(desiredx != 0)
2442 new_width = desiredx;
2443 else
2444 new_width = width;
2446 if(desiredy != 0)
2447 new_height = height > 0 ? desiredy : -desiredy;
2448 else
2449 new_height = height;
2451 if(bm_type == 0)
2453 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2454 core->bcWidth = new_width;
2455 core->bcHeight = new_height;
2457 else
2459 scaled_info->bmiHeader.biWidth = new_width;
2460 scaled_info->bmiHeader.biHeight = new_height;
2463 if (new_height < 0) new_height = -new_height;
2465 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2466 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2468 bits = (char *)info + size;
2470 if (loadflags & LR_CREATEDIBSECTION)
2472 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2473 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2475 else
2477 if (is_dib_monochrome(fix_info))
2478 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2479 else
2480 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2483 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2484 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2485 SelectObject(screen_mem_dc, orig_bm);
2487 end:
2488 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2489 HeapFree(GetProcessHeap(), 0, scaled_info);
2490 HeapFree(GetProcessHeap(), 0, fix_info);
2491 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2493 return hbitmap;
2496 /**********************************************************************
2497 * LoadImageA (USER32.@)
2499 * See LoadImageW.
2501 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2502 INT desiredx, INT desiredy, UINT loadflags)
2504 HANDLE res;
2505 LPWSTR u_name;
2507 if (IS_INTRESOURCE(name))
2508 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2510 __TRY {
2511 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2512 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2513 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2515 __EXCEPT_PAGE_FAULT {
2516 SetLastError( ERROR_INVALID_PARAMETER );
2517 return 0;
2519 __ENDTRY
2520 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2521 HeapFree(GetProcessHeap(), 0, u_name);
2522 return res;
2526 /******************************************************************************
2527 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2529 * PARAMS
2530 * hinst [I] Handle of instance that contains image
2531 * name [I] Name of image
2532 * type [I] Type of image
2533 * desiredx [I] Desired width
2534 * desiredy [I] Desired height
2535 * loadflags [I] Load flags
2537 * RETURNS
2538 * Success: Handle to newly loaded image
2539 * Failure: NULL
2541 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2543 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2544 INT desiredx, INT desiredy, UINT loadflags )
2546 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2547 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2549 if (loadflags & LR_DEFAULTSIZE) {
2550 if (type == IMAGE_ICON) {
2551 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2552 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2553 } else if (type == IMAGE_CURSOR) {
2554 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2555 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2558 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2559 switch (type) {
2560 case IMAGE_BITMAP:
2561 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2563 case IMAGE_ICON:
2564 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2565 if (screen_dc)
2567 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2568 GetDeviceCaps(screen_dc, BITSPIXEL),
2569 FALSE, loadflags);
2571 break;
2573 case IMAGE_CURSOR:
2574 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2575 1, TRUE, loadflags);
2577 return 0;
2580 /******************************************************************************
2581 * CopyImage (USER32.@) Creates new image and copies attributes to it
2583 * PARAMS
2584 * hnd [I] Handle to image to copy
2585 * type [I] Type of image to copy
2586 * desiredx [I] Desired width of new image
2587 * desiredy [I] Desired height of new image
2588 * flags [I] Copy flags
2590 * RETURNS
2591 * Success: Handle to newly created image
2592 * Failure: NULL
2594 * BUGS
2595 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2596 * all other versions (95/2000/XP have been tested) ignore it.
2598 * NOTES
2599 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2600 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2601 * the copy will have the same depth as the screen.
2602 * The content of the image will only be copied if the bit depth of the
2603 * original image is compatible with the bit depth of the screen, or
2604 * if the source is a DIB section.
2605 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2607 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2608 INT desiredy, UINT flags )
2610 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2611 hnd, type, desiredx, desiredy, flags);
2613 switch (type)
2615 case IMAGE_BITMAP:
2617 HBITMAP res = NULL;
2618 DIBSECTION ds;
2619 int objSize;
2620 BITMAPINFO * bi;
2622 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2623 if (!objSize) return 0;
2624 if ((desiredx < 0) || (desiredy < 0)) return 0;
2626 if (flags & LR_COPYFROMRESOURCE)
2628 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2631 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2632 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2634 /* Allocate memory for a BITMAPINFOHEADER structure and a
2635 color table. The maximum number of colors in a color table
2636 is 256 which corresponds to a bitmap with depth 8.
2637 Bitmaps with higher depths don't have color tables. */
2638 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2639 if (!bi) return 0;
2641 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2642 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2643 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2644 bi->bmiHeader.biCompression = BI_RGB;
2646 if (flags & LR_CREATEDIBSECTION)
2648 /* Create a DIB section. LR_MONOCHROME is ignored */
2649 void * bits;
2650 HDC dc = CreateCompatibleDC(NULL);
2652 if (objSize == sizeof(DIBSECTION))
2654 /* The source bitmap is a DIB.
2655 Get its attributes to create an exact copy */
2656 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2659 /* Get the color table or the color masks */
2660 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2662 bi->bmiHeader.biWidth = desiredx;
2663 bi->bmiHeader.biHeight = desiredy;
2664 bi->bmiHeader.biSizeImage = 0;
2666 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2667 DeleteDC(dc);
2669 else
2671 /* Create a device-dependent bitmap */
2673 BOOL monochrome = (flags & LR_MONOCHROME);
2675 if (objSize == sizeof(DIBSECTION))
2677 /* The source bitmap is a DIB section.
2678 Get its attributes */
2679 HDC dc = CreateCompatibleDC(NULL);
2680 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2681 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2682 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2683 DeleteDC(dc);
2685 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2687 /* Look if the colors of the DIB are black and white */
2689 monochrome =
2690 (bi->bmiColors[0].rgbRed == 0xff
2691 && bi->bmiColors[0].rgbGreen == 0xff
2692 && bi->bmiColors[0].rgbBlue == 0xff
2693 && bi->bmiColors[0].rgbReserved == 0
2694 && bi->bmiColors[1].rgbRed == 0
2695 && bi->bmiColors[1].rgbGreen == 0
2696 && bi->bmiColors[1].rgbBlue == 0
2697 && bi->bmiColors[1].rgbReserved == 0)
2699 (bi->bmiColors[0].rgbRed == 0
2700 && bi->bmiColors[0].rgbGreen == 0
2701 && bi->bmiColors[0].rgbBlue == 0
2702 && bi->bmiColors[0].rgbReserved == 0
2703 && bi->bmiColors[1].rgbRed == 0xff
2704 && bi->bmiColors[1].rgbGreen == 0xff
2705 && bi->bmiColors[1].rgbBlue == 0xff
2706 && bi->bmiColors[1].rgbReserved == 0);
2709 else if (!monochrome)
2711 monochrome = ds.dsBm.bmBitsPixel == 1;
2714 if (monochrome)
2716 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2718 else
2720 HDC screenDC = GetDC(NULL);
2721 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2722 ReleaseDC(NULL, screenDC);
2726 if (res)
2728 /* Only copy the bitmap if it's a DIB section or if it's
2729 compatible to the screen */
2730 BOOL copyContents;
2732 if (objSize == sizeof(DIBSECTION))
2734 copyContents = TRUE;
2736 else
2738 HDC screenDC = GetDC(NULL);
2739 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2740 ReleaseDC(NULL, screenDC);
2742 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2745 if (copyContents)
2747 /* The source bitmap may already be selected in a device context,
2748 use GetDIBits/StretchDIBits and not StretchBlt */
2750 HDC dc;
2751 void * bits;
2753 dc = CreateCompatibleDC(NULL);
2755 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2756 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2757 bi->bmiHeader.biSizeImage = 0;
2758 bi->bmiHeader.biClrUsed = 0;
2759 bi->bmiHeader.biClrImportant = 0;
2761 /* Fill in biSizeImage */
2762 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2763 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2765 if (bits)
2767 HBITMAP oldBmp;
2769 /* Get the image bits of the source bitmap */
2770 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2772 /* Copy it to the destination bitmap */
2773 oldBmp = SelectObject(dc, res);
2774 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2775 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2776 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2777 SelectObject(dc, oldBmp);
2779 HeapFree(GetProcessHeap(), 0, bits);
2782 DeleteDC(dc);
2785 if (flags & LR_COPYDELETEORG)
2787 DeleteObject(hnd);
2790 HeapFree(GetProcessHeap(), 0, bi);
2791 return res;
2793 case IMAGE_ICON:
2794 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2795 case IMAGE_CURSOR:
2796 /* Should call CURSORICON_ExtCopy but more testing
2797 * needs to be done before we change this
2799 if (flags) FIXME("Flags are ignored\n");
2800 return CopyCursor(hnd);
2802 return 0;
2806 /******************************************************************************
2807 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2809 * RETURNS
2810 * Success: Handle to specified bitmap
2811 * Failure: NULL
2813 HBITMAP WINAPI LoadBitmapW(
2814 HINSTANCE instance, /* [in] Handle to application instance */
2815 LPCWSTR name) /* [in] Address of bitmap resource name */
2817 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2820 /**********************************************************************
2821 * LoadBitmapA (USER32.@)
2823 * See LoadBitmapW.
2825 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2827 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );