user32: Define an explicit structure for storing the cursor data.
[wine/multimedia.git] / dlls / user32 / cursoricon.c
blob66aaf4d2e877bf7e2b1fcea1338f624c449cdfd0
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 CURSORICONINFO data;
139 /* followed by cursor bits in CURSORICONINFO format */
142 static HICON alloc_icon_handle( unsigned int size )
144 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) + size );
145 if (!obj) return 0;
146 obj->param = 0;
147 return alloc_user_handle( &obj->obj, USER_ICON );
150 static struct cursoricon_object *get_icon_ptr( HICON handle )
152 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
153 if (obj == OBJ_OTHER_PROCESS)
155 WARN( "icon handle %p from other process\n", handle );
156 obj = NULL;
158 return obj;
161 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
163 release_user_handle_ptr( ptr );
166 static BOOL free_icon_handle( HICON handle )
168 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
170 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
171 else if (obj)
173 ULONG_PTR param = obj->param;
174 HeapFree( GetProcessHeap(), 0, obj );
175 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
176 USER_Driver->pDestroyCursorIcon( handle );
177 return TRUE;
179 return FALSE;
182 ULONG_PTR get_icon_param( HICON handle )
184 ULONG_PTR ret = 0;
185 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
187 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
188 else if (obj)
190 ret = obj->param;
191 release_user_handle_ptr( obj );
193 return ret;
196 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
198 ULONG_PTR ret = 0;
199 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
201 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
202 else if (obj)
204 ret = obj->param;
205 obj->param = param;
206 release_user_handle_ptr( obj );
208 return ret;
212 /***********************************************************************
213 * map_fileW
215 * Helper function to map a file to memory:
216 * name - file name
217 * [RETURN] ptr - pointer to mapped file
218 * [RETURN] filesize - pointer size of file to be stored if not NULL
220 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
222 HANDLE hFile, hMapping;
223 LPVOID ptr = NULL;
225 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
226 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
227 if (hFile != INVALID_HANDLE_VALUE)
229 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
230 if (hMapping)
232 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
233 CloseHandle( hMapping );
234 if (filesize)
235 *filesize = GetFileSize( hFile, NULL );
237 CloseHandle( hFile );
239 return ptr;
243 /***********************************************************************
244 * get_bitmap_width_bytes
246 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
247 * data.
249 static int get_bitmap_width_bytes( int width, int bpp )
251 switch(bpp)
253 case 1:
254 return 2 * ((width+15) / 16);
255 case 4:
256 return 2 * ((width+3) / 4);
257 case 24:
258 width *= 3;
259 /* fall through */
260 case 8:
261 return width + (width & 1);
262 case 16:
263 case 15:
264 return width * 2;
265 case 32:
266 return width * 4;
267 default:
268 WARN("Unknown depth %d, please report.\n", bpp );
270 return -1;
274 /***********************************************************************
275 * get_dib_width_bytes
277 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
279 static int get_dib_width_bytes( int width, int depth )
281 int words;
283 switch(depth)
285 case 1: words = (width + 31) / 32; break;
286 case 4: words = (width + 7) / 8; break;
287 case 8: words = (width + 3) / 4; break;
288 case 15:
289 case 16: words = (width + 1) / 2; break;
290 case 24: words = (width * 3 + 3)/4; break;
291 default:
292 WARN("(%d): Unsupported depth\n", depth );
293 /* fall through */
294 case 32:
295 words = width;
297 return 4 * words;
301 /***********************************************************************
302 * bitmap_info_size
304 * Return the size of the bitmap info structure including color table.
306 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
308 unsigned int colors, size, masks = 0;
310 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
312 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
313 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
314 return sizeof(BITMAPCOREHEADER) + colors *
315 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
317 else /* assume BITMAPINFOHEADER */
319 colors = info->bmiHeader.biClrUsed;
320 if (colors > 256) /* buffer overflow otherwise */
321 colors = 256;
322 if (!colors && (info->bmiHeader.biBitCount <= 8))
323 colors = 1 << info->bmiHeader.biBitCount;
324 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
325 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
326 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
331 /***********************************************************************
332 * is_dib_monochrome
334 * Returns whether a DIB can be converted to a monochrome DDB.
336 * A DIB can be converted if its color table contains only black and
337 * white. Black must be the first color in the color table.
339 * Note : If the first color in the color table is white followed by
340 * black, we can't convert it to a monochrome DDB with
341 * SetDIBits, because black and white would be inverted.
343 static BOOL is_dib_monochrome( const BITMAPINFO* info )
345 if (info->bmiHeader.biBitCount != 1) return FALSE;
347 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
349 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
351 /* Check if the first color is black */
352 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
354 rgb++;
356 /* Check if the second color is white */
357 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
358 && (rgb->rgbtBlue == 0xff));
360 else return FALSE;
362 else /* assume BITMAPINFOHEADER */
364 const RGBQUAD *rgb = info->bmiColors;
366 /* Check if the first color is black */
367 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
368 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
370 rgb++;
372 /* Check if the second color is white */
373 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
374 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
376 else return FALSE;
380 /***********************************************************************
381 * DIB_GetBitmapInfo
383 * Get the info from a bitmap header.
384 * Return 1 for INFOHEADER, 0 for COREHEADER,
386 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
387 LONG *height, WORD *bpp, DWORD *compr )
389 if (header->biSize == sizeof(BITMAPCOREHEADER))
391 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
392 *width = core->bcWidth;
393 *height = core->bcHeight;
394 *bpp = core->bcBitCount;
395 *compr = 0;
396 return 0;
398 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
400 *width = header->biWidth;
401 *height = header->biHeight;
402 *bpp = header->biBitCount;
403 *compr = header->biCompression;
404 return 1;
406 ERR("(%d): unknown/wrong size for header\n", header->biSize );
407 return -1;
410 /**********************************************************************
411 * CURSORICON_FindSharedIcon
413 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
415 HICON hIcon = 0;
416 ICONCACHE *ptr;
418 EnterCriticalSection( &IconCrst );
420 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
421 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
423 ptr->count++;
424 hIcon = ptr->hIcon;
425 break;
428 LeaveCriticalSection( &IconCrst );
430 return hIcon;
433 /*************************************************************************
434 * CURSORICON_FindCache
436 * Given a handle, find the corresponding cache element
438 * PARAMS
439 * Handle [I] handle to an Image
441 * RETURNS
442 * Success: The cache entry
443 * Failure: NULL
446 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
448 ICONCACHE *ptr;
449 ICONCACHE *pRet=NULL;
450 BOOL IsFound = FALSE;
452 EnterCriticalSection( &IconCrst );
454 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
456 if ( hIcon == ptr->hIcon )
458 IsFound = TRUE;
459 pRet = ptr;
463 LeaveCriticalSection( &IconCrst );
465 return pRet;
468 /**********************************************************************
469 * CURSORICON_AddSharedIcon
471 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
473 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
474 if ( !ptr ) return;
476 ptr->hModule = hModule;
477 ptr->hRsrc = hRsrc;
478 ptr->hIcon = hIcon;
479 ptr->hGroupRsrc = hGroupRsrc;
480 ptr->count = 1;
482 EnterCriticalSection( &IconCrst );
483 ptr->next = IconAnchor;
484 IconAnchor = ptr;
485 LeaveCriticalSection( &IconCrst );
488 /**********************************************************************
489 * CURSORICON_DelSharedIcon
491 static INT CURSORICON_DelSharedIcon( HICON hIcon )
493 INT count = -1;
494 ICONCACHE *ptr;
496 EnterCriticalSection( &IconCrst );
498 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
499 if ( ptr->hIcon == hIcon )
501 if ( ptr->count > 0 ) ptr->count--;
502 count = ptr->count;
503 break;
506 LeaveCriticalSection( &IconCrst );
508 return count;
511 /**********************************************************************
512 * get_icon_size
514 BOOL get_icon_size( HICON handle, SIZE *size )
516 struct cursoricon_object *info;
518 if (!(info = get_icon_ptr( handle ))) return FALSE;
519 size->cx = info->data.nWidth;
520 size->cy = info->data.nHeight;
521 release_icon_ptr( handle, info );
522 return TRUE;
526 * The following macro functions account for the irregularities of
527 * accessing cursor and icon resources in files and resource entries.
529 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
530 int *width, int *height, int *bits );
532 /**********************************************************************
533 * CURSORICON_FindBestIcon
535 * Find the icon closest to the requested size and bit depth.
537 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
538 int width, int height, int depth )
540 int i, cx, cy, bits, bestEntry = -1;
541 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
542 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
544 /* Find Best Fit */
545 iTotalDiff = 0xFFFFFFFF;
546 iColorDiff = 0xFFFFFFFF;
547 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
549 iTempXDiff = abs(width - cx);
550 iTempYDiff = abs(height - cy);
552 if(iTotalDiff > (iTempXDiff + iTempYDiff))
554 iXDiff = iTempXDiff;
555 iYDiff = iTempYDiff;
556 iTotalDiff = iXDiff + iYDiff;
560 /* Find Best Colors for Best Fit */
561 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
563 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
565 iTempColorDiff = abs(depth - bits);
566 if(iColorDiff > iTempColorDiff)
568 bestEntry = i;
569 iColorDiff = iTempColorDiff;
574 return bestEntry;
577 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
578 int *width, int *height, int *bits )
580 CURSORICONDIR *resdir = dir;
581 ICONRESDIR *icon;
583 if ( resdir->idCount <= n )
584 return FALSE;
585 icon = &resdir->idEntries[n].ResInfo.icon;
586 *width = icon->bWidth;
587 *height = icon->bHeight;
588 *bits = resdir->idEntries[n].wBitCount;
589 return TRUE;
592 /**********************************************************************
593 * CURSORICON_FindBestCursor
595 * Find the cursor closest to the requested size.
597 * FIXME: parameter 'color' ignored.
599 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
600 int width, int height, int depth )
602 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
604 /* Double height to account for AND and XOR masks */
606 height *= 2;
608 /* First find the largest one smaller than or equal to the requested size*/
610 maxwidth = maxheight = 0;
611 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
613 if ((cx <= width) && (cy <= height) &&
614 (cx > maxwidth) && (cy > maxheight))
616 bestEntry = i;
617 maxwidth = cx;
618 maxheight = cy;
621 if (bestEntry != -1) return bestEntry;
623 /* Now find the smallest one larger than the requested size */
625 maxwidth = maxheight = 255;
626 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
628 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
630 bestEntry = i;
631 maxwidth = cx;
632 maxheight = cy;
636 return bestEntry;
639 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
640 int *width, int *height, int *bits )
642 CURSORICONDIR *resdir = dir;
643 CURSORDIR *cursor;
645 if ( resdir->idCount <= n )
646 return FALSE;
647 cursor = &resdir->idEntries[n].ResInfo.cursor;
648 *width = cursor->wWidth;
649 *height = cursor->wHeight;
650 *bits = resdir->idEntries[n].wBitCount;
651 return TRUE;
654 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
655 int width, int height, int depth )
657 int n;
659 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
660 width, height, depth );
661 if ( n < 0 )
662 return NULL;
663 return &dir->idEntries[n];
666 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
667 int width, int height, int depth )
669 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
670 width, height, depth );
671 if ( n < 0 )
672 return NULL;
673 return &dir->idEntries[n];
676 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
677 int *width, int *height, int *bits )
679 CURSORICONFILEDIR *filedir = dir;
680 CURSORICONFILEDIRENTRY *entry;
681 BITMAPINFOHEADER *info;
683 if ( filedir->idCount <= n )
684 return FALSE;
685 entry = &filedir->idEntries[n];
686 /* FIXME: check against file size */
687 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
688 *width = entry->bWidth;
689 *height = entry->bHeight;
690 *bits = info->biBitCount;
691 return TRUE;
694 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
695 int width, int height, int depth )
697 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
698 width, height, depth );
699 if ( n < 0 )
700 return NULL;
701 return &dir->idEntries[n];
704 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
705 int width, int height, int depth )
707 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
708 width, height, depth );
709 if ( n < 0 )
710 return NULL;
711 return &dir->idEntries[n];
714 /***********************************************************************
715 * stretch_blt_icon
717 * A helper function that stretches a bitmap buffer into an HBITMAP.
719 * PARAMS
720 * hDest [I] The handle of the destination bitmap.
721 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
722 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
723 * pSrcBits [I] A pointer to the source bitmap buffer.
725 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
727 HBITMAP hOld;
728 BOOL res = FALSE;
729 HDC hdcMem = CreateCompatibleDC(screen_dc);
731 if (hdcMem)
733 hOld = SelectObject(hdcMem, hDest);
734 res = StretchDIBits(hdcMem,
735 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
736 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
737 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
738 SelectObject(hdcMem, hOld);
739 DeleteDC( hdcMem );
742 return res;
745 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
746 POINT16 hotspot, BOOL bIcon,
747 DWORD dwVersion,
748 INT width, INT height,
749 UINT cFlag )
751 HICON hObj;
752 int sizeAnd, sizeXor;
753 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
754 BITMAP bmpXor, bmpAnd;
755 BOOL do_stretch;
756 INT size;
757 BITMAPINFO *pSrcInfo, *pDestInfo;
759 if (dwVersion == 0x00020000)
761 FIXME_(cursor)("\t2.xx resources are not supported\n");
762 return 0;
765 /* Check bitmap header */
767 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
768 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
769 bmi->bmiHeader.biCompression != BI_RGB) )
771 WARN_(cursor)("\tinvalid resource bitmap header.\n");
772 return 0;
775 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
777 if (!width) width = bmi->bmiHeader.biWidth;
778 if (!height) height = bmi->bmiHeader.biHeight/2;
779 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
780 (bmi->bmiHeader.biWidth != width);
782 /* Scale the hotspot */
783 if (do_stretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
785 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
786 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
789 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
790 if (screen_dc)
792 /* Make sure we have room for the monochrome bitmap later on.
793 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
794 * up to and including the biBitCount. In-memory icon resource
795 * format is as follows:
797 * BITMAPINFOHEADER icHeader // DIB header
798 * RGBQUAD icColors[] // Color table
799 * BYTE icXOR[] // DIB bits for XOR mask
800 * BYTE icAND[] // DIB bits for AND mask
803 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
804 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
805 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
806 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
807 if (pSrcInfo && pDestInfo)
809 memcpy( pSrcInfo, bmi, size );
810 pSrcInfo->bmiHeader.biHeight /= 2;
812 memcpy( pDestInfo, bmi, size );
813 pDestInfo->bmiHeader.biWidth = width;
814 pDestInfo->bmiHeader.biHeight = height;
815 pDestInfo->bmiHeader.biSizeImage = 0;
817 /* Create the XOR bitmap */
818 if(pSrcInfo->bmiHeader.biBitCount == 32)
820 void *pDIBBuffer = NULL;
821 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
823 if(hXorBits)
825 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
827 DeleteObject(hXorBits);
828 hXorBits = 0;
832 else
834 if (do_stretch)
836 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
837 if (hXorBits)
839 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
841 DeleteObject(hXorBits);
842 hXorBits = 0;
846 else
848 if (is_dib_monochrome(bmi))
850 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
851 SetDIBits(screen_dc, hXorBits, 0, height,
852 (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
854 else
855 hXorBits = CreateDIBitmap(screen_dc, &pSrcInfo->bmiHeader,
856 CBM_INIT, (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
860 if( hXorBits )
862 char* xbits = (char *)bmi + size +
863 get_dib_width_bytes( bmi->bmiHeader.biWidth,
864 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
866 pSrcInfo->bmiHeader.biBitCount = 1;
867 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
869 RGBQUAD *rgb = pSrcInfo->bmiColors;
871 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
872 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
873 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
874 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
876 else
878 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
880 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
881 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
884 /* Create the AND bitmap */
885 if (do_stretch)
887 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
889 if (!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
891 DeleteObject(hAndBits);
892 hAndBits = 0;
895 else
897 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
898 SetDIBits(screen_dc, hAndBits, 0, height,
899 xbits, pSrcInfo, DIB_RGB_COLORS);
902 if( !hAndBits )
904 DeleteObject( hXorBits );
905 hXorBits = 0;
910 HeapFree( GetProcessHeap(), 0, pSrcInfo );
911 HeapFree( GetProcessHeap(), 0, pDestInfo );
914 if( !hXorBits || !hAndBits )
916 WARN_(cursor)("\tunable to create an icon bitmap.\n");
917 return 0;
920 /* Now create the CURSORICONINFO structure */
921 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
922 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
923 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
924 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
926 hObj = alloc_icon_handle( sizeXor + sizeAnd );
927 if (hObj)
929 struct cursoricon_object *info = get_icon_ptr( hObj );
931 info->data.ptHotSpot.x = hotspot.x;
932 info->data.ptHotSpot.y = hotspot.y;
933 info->data.nWidth = bmpXor.bmWidth;
934 info->data.nHeight = bmpXor.bmHeight;
935 info->data.nWidthBytes = bmpXor.bmWidthBytes;
936 info->data.bPlanes = bmpXor.bmPlanes;
937 info->data.bBitsPerPixel = bmpXor.bmBitsPixel;
939 /* Transfer the bitmap bits to the CURSORICONINFO structure */
941 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
942 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
943 release_icon_ptr( hObj, info );
944 USER_Driver->pCreateCursorIcon( hObj, &info->data );
947 DeleteObject( hAndBits );
948 DeleteObject( hXorBits );
949 return hObj;
953 /**********************************************************************
954 * .ANI cursor support
956 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
957 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
958 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
960 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
961 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
962 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
963 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
964 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
965 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
967 #define ANI_FLAG_ICON 0x1
968 #define ANI_FLAG_SEQUENCE 0x2
970 typedef struct {
971 DWORD header_size;
972 DWORD num_frames;
973 DWORD num_steps;
974 DWORD width;
975 DWORD height;
976 DWORD bpp;
977 DWORD num_planes;
978 DWORD display_rate;
979 DWORD flags;
980 } ani_header;
982 typedef struct {
983 DWORD data_size;
984 const unsigned char *data;
985 } riff_chunk_t;
987 static void dump_ani_header( const ani_header *header )
989 TRACE(" header size: %d\n", header->header_size);
990 TRACE(" frames: %d\n", header->num_frames);
991 TRACE(" steps: %d\n", header->num_steps);
992 TRACE(" width: %d\n", header->width);
993 TRACE(" height: %d\n", header->height);
994 TRACE(" bpp: %d\n", header->bpp);
995 TRACE(" planes: %d\n", header->num_planes);
996 TRACE(" display rate: %d\n", header->display_rate);
997 TRACE(" flags: 0x%08x\n", header->flags);
1002 * RIFF:
1003 * DWORD "RIFF"
1004 * DWORD size
1005 * DWORD riff_id
1006 * BYTE[] data
1008 * LIST:
1009 * DWORD "LIST"
1010 * DWORD size
1011 * DWORD list_id
1012 * BYTE[] data
1014 * CHUNK:
1015 * DWORD chunk_id
1016 * DWORD size
1017 * BYTE[] data
1019 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1021 const unsigned char *ptr = parent_chunk->data;
1022 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1024 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1026 while (ptr < end)
1028 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1029 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1031 ptr += sizeof(DWORD);
1032 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1033 ptr += sizeof(DWORD);
1034 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1035 chunk->data = ptr;
1037 return;
1040 ptr += sizeof(DWORD);
1041 ptr += (*(const DWORD *)ptr + 1) & ~1;
1042 ptr += sizeof(DWORD);
1048 * .ANI layout:
1050 * RIFF:'ACON' RIFF chunk
1051 * |- CHUNK:'anih' Header
1052 * |- CHUNK:'seq ' Sequence information (optional)
1053 * \- LIST:'fram' Frame list
1054 * |- CHUNK:icon Cursor frames
1055 * |- CHUNK:icon
1056 * |- ...
1057 * \- CHUNK:icon
1059 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1060 INT width, INT height, INT depth )
1062 HCURSOR cursor;
1063 ani_header header = {0};
1064 LPBYTE frame_bits = 0;
1065 POINT16 hotspot;
1066 CURSORICONFILEDIRENTRY *entry;
1068 riff_chunk_t root_chunk = { bits_size, bits };
1069 riff_chunk_t ACON_chunk = {0};
1070 riff_chunk_t anih_chunk = {0};
1071 riff_chunk_t fram_chunk = {0};
1072 const unsigned char *icon_data;
1074 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1076 if (!bits) return 0;
1078 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1079 if (!ACON_chunk.data)
1081 ERR("Failed to get root chunk.\n");
1082 return 0;
1085 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1086 if (!anih_chunk.data)
1088 ERR("Failed to get 'anih' chunk.\n");
1089 return 0;
1091 memcpy( &header, anih_chunk.data, sizeof(header) );
1092 dump_ani_header( &header );
1094 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1095 if (!fram_chunk.data)
1097 ERR("Failed to get icon list.\n");
1098 return 0;
1101 /* FIXME: For now, just load the first frame. Before we can load all the
1102 * frames, we need to write the needed code in wineserver, etc. to handle
1103 * cursors. Once this code is written, we can extend it to support .ani
1104 * cursors and then update user32 and winex11.drv to load all frames.
1106 * Hopefully this will at least make some games (C&C3, etc.) more playable
1107 * in the meantime.
1109 FIXME("Loading all frames for .ani cursors not implemented.\n");
1110 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1112 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1113 width, height, depth );
1115 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1116 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1118 if (!header.width || !header.height)
1120 header.width = entry->bWidth;
1121 header.height = entry->bHeight;
1124 hotspot.x = entry->xHotspot;
1125 hotspot.y = entry->yHotspot;
1127 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1128 FALSE, 0x00030000, header.width, header.height, 0 );
1130 HeapFree( GetProcessHeap(), 0, frame_bits );
1132 return cursor;
1136 /**********************************************************************
1137 * CreateIconFromResourceEx (USER32.@)
1139 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1140 * with cbSize parameter as well.
1142 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1143 BOOL bIcon, DWORD dwVersion,
1144 INT width, INT height,
1145 UINT cFlag )
1147 POINT16 hotspot;
1148 BITMAPINFO *bmi;
1150 hotspot.x = ICON_HOTSPOT;
1151 hotspot.y = ICON_HOTSPOT;
1153 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1154 bits, cbSize, dwVersion, width, height,
1155 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1157 if (bIcon)
1158 bmi = (BITMAPINFO *)bits;
1159 else /* get the hotspot */
1161 POINT16 *pt = (POINT16 *)bits;
1162 hotspot = *pt;
1163 bmi = (BITMAPINFO *)(pt + 1);
1166 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1167 width, height, cFlag );
1171 /**********************************************************************
1172 * CreateIconFromResource (USER32.@)
1174 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1175 BOOL bIcon, DWORD dwVersion)
1177 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1181 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1182 INT width, INT height, INT depth,
1183 BOOL fCursor, UINT loadflags)
1185 CURSORICONFILEDIRENTRY *entry;
1186 CURSORICONFILEDIR *dir;
1187 DWORD filesize = 0;
1188 HICON hIcon = 0;
1189 LPBYTE bits;
1190 POINT16 hotspot;
1192 TRACE("loading %s\n", debugstr_w( filename ));
1194 bits = map_fileW( filename, &filesize );
1195 if (!bits)
1196 return hIcon;
1198 /* Check for .ani. */
1199 if (memcmp( bits, "RIFF", 4 ) == 0)
1201 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1202 depth );
1203 goto end;
1206 dir = (CURSORICONFILEDIR*) bits;
1207 if ( filesize < sizeof(*dir) )
1208 goto end;
1210 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1211 goto end;
1213 if ( fCursor )
1214 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1215 else
1216 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1218 if ( !entry )
1219 goto end;
1221 /* check that we don't run off the end of the file */
1222 if ( entry->dwDIBOffset > filesize )
1223 goto end;
1224 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1225 goto end;
1227 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1228 if ( fCursor )
1230 hotspot.x = entry->xHotspot;
1231 hotspot.y = entry->yHotspot;
1233 else
1235 hotspot.x = ICON_HOTSPOT;
1236 hotspot.y = ICON_HOTSPOT;
1238 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1239 hotspot, !fCursor, 0x00030000,
1240 width, height, loadflags );
1241 end:
1242 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1243 UnmapViewOfFile( bits );
1244 return hIcon;
1247 /**********************************************************************
1248 * CURSORICON_Load
1250 * Load a cursor or icon from resource or file.
1252 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1253 INT width, INT height, INT depth,
1254 BOOL fCursor, UINT loadflags)
1256 HANDLE handle = 0;
1257 HICON hIcon = 0;
1258 HRSRC hRsrc, hGroupRsrc;
1259 CURSORICONDIR *dir;
1260 CURSORICONDIRENTRY *dirEntry;
1261 LPBYTE bits;
1262 WORD wResId;
1263 DWORD dwBytesInRes;
1265 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1266 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1268 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1269 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1271 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1273 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1274 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1276 /* Get directory resource ID */
1278 if (!(hRsrc = FindResourceW( hInstance, name,
1279 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1280 return 0;
1281 hGroupRsrc = hRsrc;
1283 /* Find the best entry in the directory */
1285 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1286 if (!(dir = LockResource( handle ))) return 0;
1287 if (fCursor)
1288 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1289 else
1290 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1291 if (!dirEntry) return 0;
1292 wResId = dirEntry->wResId;
1293 dwBytesInRes = dirEntry->dwBytesInRes;
1294 FreeResource( handle );
1296 /* Load the resource */
1298 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1299 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1301 /* If shared icon, check whether it was already loaded */
1302 if ( (loadflags & LR_SHARED)
1303 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1304 return hIcon;
1306 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1307 bits = LockResource( handle );
1308 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1309 !fCursor, 0x00030000, width, height, loadflags);
1310 FreeResource( handle );
1312 /* If shared icon, add to icon cache */
1314 if ( hIcon && (loadflags & LR_SHARED) )
1315 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1317 return hIcon;
1321 /*************************************************************************
1322 * CURSORICON_ExtCopy
1324 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1326 * PARAMS
1327 * Handle [I] handle to an Image
1328 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1329 * iDesiredCX [I] The Desired width of the Image
1330 * iDesiredCY [I] The desired height of the Image
1331 * nFlags [I] The flags from CopyImage
1333 * RETURNS
1334 * Success: The new handle of the Image
1336 * NOTES
1337 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1338 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1339 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1344 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1345 INT iDesiredCX, INT iDesiredCY,
1346 UINT nFlags)
1348 HICON hNew=0;
1350 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1351 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1353 if(hIcon == 0)
1355 return 0;
1358 /* Best Fit or Monochrome */
1359 if( (nFlags & LR_COPYFROMRESOURCE
1360 && (iDesiredCX > 0 || iDesiredCY > 0))
1361 || nFlags & LR_MONOCHROME)
1363 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1365 /* Not Found in Cache, then do a straight copy
1367 if(pIconCache == NULL)
1369 hNew = CopyIcon( hIcon );
1370 if(nFlags & LR_COPYFROMRESOURCE)
1372 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1375 else
1377 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1378 LPBYTE pBits;
1379 HANDLE hMem;
1380 HRSRC hRsrc;
1381 DWORD dwBytesInRes;
1382 WORD wResId;
1383 CURSORICONDIR *pDir;
1384 CURSORICONDIRENTRY *pDirEntry;
1385 BOOL bIsIcon = (nType == IMAGE_ICON);
1387 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1389 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1390 || (iDesiredCX == 0 && iDesiredCY == 0))
1392 iDesiredCY = GetSystemMetrics(bIsIcon ?
1393 SM_CYICON : SM_CYCURSOR);
1394 iDesiredCX = GetSystemMetrics(bIsIcon ?
1395 SM_CXICON : SM_CXCURSOR);
1398 /* Retrieve the CURSORICONDIRENTRY
1400 if (!(hMem = LoadResource( pIconCache->hModule ,
1401 pIconCache->hGroupRsrc)))
1403 return 0;
1405 if (!(pDir = LockResource( hMem )))
1407 return 0;
1410 /* Find Best Fit
1412 if(bIsIcon)
1414 pDirEntry = CURSORICON_FindBestIconRes(
1415 pDir, iDesiredCX, iDesiredCY, 256 );
1417 else
1419 pDirEntry = CURSORICON_FindBestCursorRes(
1420 pDir, iDesiredCX, iDesiredCY, 1);
1423 wResId = pDirEntry->wResId;
1424 dwBytesInRes = pDirEntry->dwBytesInRes;
1425 FreeResource(hMem);
1427 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1428 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1429 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1431 /* Get the Best Fit
1433 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1434 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1436 return 0;
1438 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1440 return 0;
1443 pBits = LockResource( hMem );
1445 if(nFlags & LR_DEFAULTSIZE)
1447 iTargetCY = GetSystemMetrics(SM_CYICON);
1448 iTargetCX = GetSystemMetrics(SM_CXICON);
1451 /* Create a New Icon with the proper dimension
1453 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1454 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1455 FreeResource(hMem);
1458 else hNew = CopyIcon( hIcon );
1459 return hNew;
1463 /***********************************************************************
1464 * CreateCursor (USER32.@)
1466 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1467 INT xHotSpot, INT yHotSpot,
1468 INT nWidth, INT nHeight,
1469 LPCVOID lpANDbits, LPCVOID lpXORbits )
1471 ICONINFO info;
1472 HCURSOR hCursor;
1474 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1475 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1477 info.fIcon = FALSE;
1478 info.xHotspot = xHotSpot;
1479 info.yHotspot = yHotSpot;
1480 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1481 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1482 hCursor = CreateIconIndirect( &info );
1483 DeleteObject( info.hbmMask );
1484 DeleteObject( info.hbmColor );
1485 return hCursor;
1489 /***********************************************************************
1490 * CreateIcon (USER32.@)
1492 * Creates an icon based on the specified bitmaps. The bitmaps must be
1493 * provided in a device dependent format and will be resized to
1494 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1495 * depth. The provided bitmaps must be top-down bitmaps.
1496 * Although Windows does not support 15bpp(*) this API must support it
1497 * for Winelib applications.
1499 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1500 * format!
1502 * RETURNS
1503 * Success: handle to an icon
1504 * Failure: NULL
1506 * FIXME: Do we need to resize the bitmaps?
1508 HICON WINAPI CreateIcon(
1509 HINSTANCE hInstance, /* [in] the application's hInstance */
1510 INT nWidth, /* [in] the width of the provided bitmaps */
1511 INT nHeight, /* [in] the height of the provided bitmaps */
1512 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1513 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1514 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1515 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1517 ICONINFO iinfo;
1518 HICON hIcon;
1520 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1521 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1523 iinfo.fIcon = TRUE;
1524 iinfo.xHotspot = ICON_HOTSPOT;
1525 iinfo.yHotspot = ICON_HOTSPOT;
1526 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1527 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1529 hIcon = CreateIconIndirect( &iinfo );
1531 DeleteObject( iinfo.hbmMask );
1532 DeleteObject( iinfo.hbmColor );
1534 return hIcon;
1538 /***********************************************************************
1539 * CopyIcon (USER32.@)
1541 HICON WINAPI CopyIcon( HICON hIcon )
1543 struct cursoricon_object *ptrOld, *ptrNew;
1544 int size;
1545 HICON hNew;
1547 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1548 size = ptrOld->data.nHeight * get_bitmap_width_bytes( ptrOld->data.nWidth, 1 ); /* and bitmap */
1549 size += ptrOld->data.nHeight * ptrOld->data.nWidthBytes; /* xor bitmap */
1550 hNew = alloc_icon_handle( size );
1551 ptrNew = get_icon_ptr( hNew );
1552 memcpy( &ptrNew->data, &ptrOld->data, sizeof(ptrNew->data) + size );
1553 release_icon_ptr( hIcon, ptrOld );
1554 release_icon_ptr( hNew, ptrNew );
1555 USER_Driver->pCreateCursorIcon( hNew, &ptrNew->data );
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 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 count = reply->prev_count + increment;
1714 SERVER_END_REQ;
1716 TRACE("%d, count=%d\n", bShow, count );
1718 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1719 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1721 return count;
1724 /***********************************************************************
1725 * GetCursor (USER32.@)
1727 HCURSOR WINAPI GetCursor(void)
1729 HCURSOR ret;
1731 SERVER_START_REQ( set_cursor )
1733 req->flags = 0;
1734 wine_server_call( req );
1735 ret = wine_server_ptr_handle( reply->prev_handle );
1737 SERVER_END_REQ;
1738 return ret;
1742 /***********************************************************************
1743 * ClipCursor (USER32.@)
1745 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1747 RECT virt;
1749 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1750 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1751 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1752 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1754 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1755 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1757 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1758 CURSOR_ClipRect = virt;
1760 USER_Driver->pClipCursor( rect );
1761 return TRUE;
1765 /***********************************************************************
1766 * GetClipCursor (USER32.@)
1768 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1770 /* If this is first time - initialize the rect */
1771 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1773 return CopyRect( rect, &CURSOR_ClipRect );
1777 /***********************************************************************
1778 * SetSystemCursor (USER32.@)
1780 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1782 FIXME("(%p,%08x),stub!\n", hcur, id);
1783 return TRUE;
1787 /**********************************************************************
1788 * LookupIconIdFromDirectoryEx (USER32.@)
1790 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1791 INT width, INT height, UINT cFlag )
1793 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1794 UINT retVal = 0;
1795 if( dir && !dir->idReserved && (dir->idType & 3) )
1797 CURSORICONDIRENTRY* entry;
1799 const HDC hdc = GetDC(0);
1800 const int depth = (cFlag & LR_MONOCHROME) ?
1801 1 : GetDeviceCaps(hdc, BITSPIXEL);
1802 ReleaseDC(0, hdc);
1804 if( bIcon )
1805 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1806 else
1807 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1809 if( entry ) retVal = entry->wResId;
1811 else WARN_(cursor)("invalid resource directory\n");
1812 return retVal;
1815 /**********************************************************************
1816 * LookupIconIdFromDirectory (USER32.@)
1818 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1820 return LookupIconIdFromDirectoryEx( dir, bIcon,
1821 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1822 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1825 /***********************************************************************
1826 * LoadCursorW (USER32.@)
1828 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1830 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1832 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1833 LR_SHARED | LR_DEFAULTSIZE );
1836 /***********************************************************************
1837 * LoadCursorA (USER32.@)
1839 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1841 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1843 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1844 LR_SHARED | LR_DEFAULTSIZE );
1847 /***********************************************************************
1848 * LoadCursorFromFileW (USER32.@)
1850 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1852 TRACE("%s\n", debugstr_w(name));
1854 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1855 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1858 /***********************************************************************
1859 * LoadCursorFromFileA (USER32.@)
1861 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1863 TRACE("%s\n", debugstr_a(name));
1865 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1866 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1869 /***********************************************************************
1870 * LoadIconW (USER32.@)
1872 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1874 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1876 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1877 LR_SHARED | LR_DEFAULTSIZE );
1880 /***********************************************************************
1881 * LoadIconA (USER32.@)
1883 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1885 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1887 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1888 LR_SHARED | LR_DEFAULTSIZE );
1891 /**********************************************************************
1892 * GetIconInfo (USER32.@)
1894 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1896 struct cursoricon_object *ciconinfo;
1897 INT height;
1899 if (!(ciconinfo = get_icon_ptr( hIcon ))) return FALSE;
1901 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1902 ciconinfo->data.nWidth, ciconinfo->data.nHeight, ciconinfo->data.bBitsPerPixel);
1904 if ( (ciconinfo->data.ptHotSpot.x == ICON_HOTSPOT) &&
1905 (ciconinfo->data.ptHotSpot.y == ICON_HOTSPOT) )
1907 iconinfo->fIcon = TRUE;
1908 iconinfo->xHotspot = ciconinfo->data.nWidth / 2;
1909 iconinfo->yHotspot = ciconinfo->data.nHeight / 2;
1911 else
1913 iconinfo->fIcon = FALSE;
1914 iconinfo->xHotspot = ciconinfo->data.ptHotSpot.x;
1915 iconinfo->yHotspot = ciconinfo->data.ptHotSpot.y;
1918 height = ciconinfo->data.nHeight;
1920 if (ciconinfo->data.bBitsPerPixel > 1)
1922 iconinfo->hbmColor = CreateBitmap( ciconinfo->data.nWidth, ciconinfo->data.nHeight,
1923 ciconinfo->data.bPlanes, ciconinfo->data.bBitsPerPixel,
1924 (char *)(ciconinfo + 1)
1925 + ciconinfo->data.nHeight *
1926 get_bitmap_width_bytes (ciconinfo->data.nWidth,1) );
1928 else
1930 iconinfo->hbmColor = 0;
1931 height *= 2;
1934 iconinfo->hbmMask = CreateBitmap ( ciconinfo->data.nWidth, height,
1935 1, 1, ciconinfo + 1);
1936 release_icon_ptr( hIcon, ciconinfo );
1938 return TRUE;
1941 /**********************************************************************
1942 * CreateIconIndirect (USER32.@)
1944 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1946 DIBSECTION bmpXor;
1947 BITMAP bmpAnd;
1948 HICON hObj;
1949 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1951 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1952 iconinfo->hbmColor, iconinfo->hbmMask,
1953 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1955 if (!iconinfo->hbmMask) return 0;
1957 planes = GetDeviceCaps( screen_dc, PLANES );
1958 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
1960 if (iconinfo->hbmColor)
1962 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1963 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1964 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
1965 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
1966 /* we can use either depth 1 or screen depth for xor bitmap */
1967 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
1968 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1970 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1971 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1972 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1973 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1975 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1977 hObj = alloc_icon_handle( sizeXor + sizeAnd );
1978 if (hObj)
1980 struct cursoricon_object *info = get_icon_ptr( hObj );
1982 /* If we are creating an icon, the hotspot is unused */
1983 if (iconinfo->fIcon)
1985 info->data.ptHotSpot.x = ICON_HOTSPOT;
1986 info->data.ptHotSpot.y = ICON_HOTSPOT;
1988 else
1990 info->data.ptHotSpot.x = iconinfo->xHotspot;
1991 info->data.ptHotSpot.y = iconinfo->yHotspot;
1994 if (iconinfo->hbmColor)
1996 info->data.nWidth = bmpXor.dsBm.bmWidth;
1997 info->data.nHeight = bmpXor.dsBm.bmHeight;
1998 info->data.nWidthBytes = bmpXor.dsBm.bmWidthBytes;
1999 info->data.bPlanes = planes;
2000 info->data.bBitsPerPixel = bpp;
2002 else
2004 info->data.nWidth = bmpAnd.bmWidth;
2005 info->data.nHeight = bmpAnd.bmHeight / 2;
2006 info->data.nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2007 info->data.bPlanes = 1;
2008 info->data.bBitsPerPixel = 1;
2011 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2013 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2014 if (bmpAnd.bmBitsPixel == 1)
2016 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2018 else
2020 HDC hdc_mem, hdc_mem2;
2021 HBITMAP hbmp_mem_old, hbmp_mem2_old, hbmp_mono;
2023 hdc_mem = CreateCompatibleDC( 0 );
2024 hdc_mem2 = CreateCompatibleDC( 0 );
2026 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2028 hbmp_mem_old = SelectObject( hdc_mem, iconinfo->hbmMask );
2029 hbmp_mem2_old = SelectObject( hdc_mem2, hbmp_mono );
2031 BitBlt( hdc_mem2, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc_mem, 0, 0, SRCCOPY );
2033 SelectObject( hdc_mem, hbmp_mem_old );
2034 SelectObject( hdc_mem2, hbmp_mem2_old );
2036 DeleteDC( hdc_mem );
2037 DeleteDC( hdc_mem2 );
2039 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2040 DeleteObject( hbmp_mono );
2043 if (iconinfo->hbmColor)
2045 char *dst_bits = (char*)(info + 1) + sizeAnd;
2047 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2048 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2049 else
2051 BITMAPINFO bminfo;
2052 int dib_width = get_dib_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2053 int bitmap_width = get_bitmap_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2055 bminfo.bmiHeader.biSize = sizeof(bminfo);
2056 bminfo.bmiHeader.biWidth = info->data.nWidth;
2057 bminfo.bmiHeader.biHeight = info->data.nHeight;
2058 bminfo.bmiHeader.biPlanes = info->data.bPlanes;
2059 bminfo.bmiHeader.biBitCount = info->data.bBitsPerPixel;
2060 bminfo.bmiHeader.biCompression = BI_RGB;
2061 bminfo.bmiHeader.biSizeImage = info->data.nHeight * dib_width;
2062 bminfo.bmiHeader.biXPelsPerMeter = 0;
2063 bminfo.bmiHeader.biYPelsPerMeter = 0;
2064 bminfo.bmiHeader.biClrUsed = 0;
2065 bminfo.bmiHeader.biClrImportant = 0;
2067 /* swap lines for dib sections */
2068 if (xor_objsize == sizeof(DIBSECTION))
2069 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2071 if (dib_width != bitmap_width) /* need to fixup alignment */
2073 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2075 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2076 src_bits, &bminfo, DIB_RGB_COLORS ))
2078 int y;
2079 for (y = 0; y < info->data.nHeight; y++)
2080 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2082 HeapFree( GetProcessHeap(), 0, src_bits );
2084 else
2085 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2086 dst_bits, &bminfo, DIB_RGB_COLORS );
2089 release_icon_ptr( hObj, info );
2090 USER_Driver->pCreateCursorIcon( hObj, &info->data );
2092 return hObj;
2095 /******************************************************************************
2096 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2098 * NOTES
2099 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2101 * PARAMS
2102 * hdc [I] Handle to device context
2103 * x0 [I] X coordinate of upper left corner
2104 * y0 [I] Y coordinate of upper left corner
2105 * hIcon [I] Handle to icon to draw
2106 * cxWidth [I] Width of icon
2107 * cyWidth [I] Height of icon
2108 * istep [I] Index of frame in animated cursor
2109 * hbr [I] Handle to background brush
2110 * flags [I] Icon-drawing flags
2112 * RETURNS
2113 * Success: TRUE
2114 * Failure: FALSE
2116 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2117 INT cxWidth, INT cyWidth, UINT istep,
2118 HBRUSH hbr, UINT flags )
2120 struct cursoricon_object *ptr;
2121 HDC hDC_off = 0, hMemDC;
2122 BOOL result = FALSE, DoOffscreen;
2123 HBITMAP hB_off = 0, hOld = 0;
2124 unsigned char *xorBitmapBits;
2125 unsigned int xorLength;
2126 BOOL has_alpha = FALSE;
2128 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2129 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2131 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2132 if (!(hMemDC = CreateCompatibleDC( hdc )))
2134 release_icon_ptr( hIcon, ptr );
2135 return FALSE;
2138 if (istep)
2139 FIXME_(icon)("Ignoring istep=%d\n", istep);
2140 if (flags & DI_NOMIRROR)
2141 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2143 xorLength = ptr->data.nHeight * get_bitmap_width_bytes(
2144 ptr->data.nWidth, ptr->data.bBitsPerPixel);
2145 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->data.nHeight *
2146 get_bitmap_width_bytes(ptr->data.nWidth, 1);
2148 if (flags & DI_IMAGE)
2149 has_alpha = bitmap_has_alpha_channel(
2150 ptr->data.bBitsPerPixel, xorBitmapBits, xorLength);
2152 /* Calculate the size of the destination image. */
2153 if (cxWidth == 0)
2155 if (flags & DI_DEFAULTSIZE)
2156 cxWidth = GetSystemMetrics (SM_CXICON);
2157 else
2158 cxWidth = ptr->data.nWidth;
2160 if (cyWidth == 0)
2162 if (flags & DI_DEFAULTSIZE)
2163 cyWidth = GetSystemMetrics (SM_CYICON);
2164 else
2165 cyWidth = ptr->data.nHeight;
2168 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2170 if (DoOffscreen) {
2171 RECT r;
2173 r.left = 0;
2174 r.top = 0;
2175 r.right = cxWidth;
2176 r.bottom = cxWidth;
2178 hDC_off = CreateCompatibleDC(hdc);
2179 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2180 if (hDC_off && hB_off) {
2181 hOld = SelectObject(hDC_off, hB_off);
2182 FillRect(hDC_off, &r, hbr);
2186 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2188 HBITMAP hBitTemp;
2189 HBITMAP hXorBits = NULL, hAndBits = NULL;
2190 COLORREF oldFg, oldBg;
2191 INT nStretchMode;
2193 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2195 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2196 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2198 if ((flags & DI_MASK) && !has_alpha)
2200 hAndBits = CreateBitmap ( ptr->data.nWidth, ptr->data.nHeight, 1, 1, ptr + 1 );
2201 if (hAndBits)
2203 hBitTemp = SelectObject( hMemDC, hAndBits );
2204 if (DoOffscreen)
2205 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2206 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND);
2207 else
2208 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2209 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND);
2210 SelectObject( hMemDC, hBitTemp );
2214 if (flags & DI_IMAGE)
2216 if (ptr->data.bPlanes * ptr->data.bBitsPerPixel == 1)
2218 hXorBits = CreateBitmap( ptr->data.nWidth, ptr->data.nHeight, 1, 1, xorBitmapBits );
2220 else
2222 unsigned char *dibBits;
2223 BITMAPINFO *bmi = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2224 FIELD_OFFSET( BITMAPINFO, bmiColors[256] ));
2225 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2226 bmi->bmiHeader.biWidth = ptr->data.nWidth;
2227 bmi->bmiHeader.biHeight = -ptr->data.nHeight;
2228 bmi->bmiHeader.biPlanes = ptr->data.bPlanes;
2229 bmi->bmiHeader.biBitCount = ptr->data.bBitsPerPixel;
2230 bmi->bmiHeader.biCompression = BI_RGB;
2231 /* FIXME: color table */
2233 hXorBits = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, (void*)&dibBits, NULL, 0);
2234 if (hXorBits)
2236 if(has_alpha)
2237 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2238 else
2239 memcpy(dibBits, xorBitmapBits, xorLength);
2243 if (hXorBits)
2245 if(has_alpha)
2247 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2249 /* Do the alpha blending render */
2250 hBitTemp = SelectObject( hMemDC, hXorBits );
2252 if (DoOffscreen)
2253 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2254 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend);
2255 else
2256 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2257 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend);
2259 SelectObject( hMemDC, hBitTemp );
2261 else
2263 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2264 hBitTemp = SelectObject( hMemDC, hXorBits );
2265 if (DoOffscreen)
2266 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2267 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop);
2268 else
2269 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2270 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop);
2271 SelectObject( hMemDC, hBitTemp );
2274 DeleteObject( hXorBits );
2278 result = TRUE;
2280 SetTextColor( hdc, oldFg );
2281 SetBkColor( hdc, oldBg );
2283 if (hAndBits) DeleteObject( hAndBits );
2284 SetStretchBltMode (hdc, nStretchMode);
2285 if (DoOffscreen) {
2286 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2287 SelectObject(hDC_off, hOld);
2290 if (hMemDC) DeleteDC( hMemDC );
2291 if (hDC_off) DeleteDC(hDC_off);
2292 if (hB_off) DeleteObject(hB_off);
2293 release_icon_ptr( hIcon, ptr );
2294 return result;
2297 /***********************************************************************
2298 * DIB_FixColorsToLoadflags
2300 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2301 * are in loadflags
2303 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2305 int colors;
2306 COLORREF c_W, c_S, c_F, c_L, c_C;
2307 int incr,i;
2308 RGBQUAD *ptr;
2309 int bitmap_type;
2310 LONG width;
2311 LONG height;
2312 WORD bpp;
2313 DWORD compr;
2315 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2317 WARN_(resource)("Invalid bitmap\n");
2318 return;
2321 if (bpp > 8) return;
2323 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2325 incr = 3;
2326 colors = 1 << bpp;
2328 else
2330 incr = 4;
2331 colors = bmi->bmiHeader.biClrUsed;
2332 if (colors > 256) colors = 256;
2333 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2336 c_W = GetSysColor(COLOR_WINDOW);
2337 c_S = GetSysColor(COLOR_3DSHADOW);
2338 c_F = GetSysColor(COLOR_3DFACE);
2339 c_L = GetSysColor(COLOR_3DLIGHT);
2341 if (loadflags & LR_LOADTRANSPARENT) {
2342 switch (bpp) {
2343 case 1: pix = pix >> 7; break;
2344 case 4: pix = pix >> 4; break;
2345 case 8: break;
2346 default:
2347 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2348 return;
2350 if (pix >= colors) {
2351 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2352 return;
2354 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2355 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2356 ptr->rgbBlue = GetBValue(c_W);
2357 ptr->rgbGreen = GetGValue(c_W);
2358 ptr->rgbRed = GetRValue(c_W);
2360 if (loadflags & LR_LOADMAP3DCOLORS)
2361 for (i=0; i<colors; i++) {
2362 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2363 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2364 if (c_C == RGB(128, 128, 128)) {
2365 ptr->rgbRed = GetRValue(c_S);
2366 ptr->rgbGreen = GetGValue(c_S);
2367 ptr->rgbBlue = GetBValue(c_S);
2368 } else if (c_C == RGB(192, 192, 192)) {
2369 ptr->rgbRed = GetRValue(c_F);
2370 ptr->rgbGreen = GetGValue(c_F);
2371 ptr->rgbBlue = GetBValue(c_F);
2372 } else if (c_C == RGB(223, 223, 223)) {
2373 ptr->rgbRed = GetRValue(c_L);
2374 ptr->rgbGreen = GetGValue(c_L);
2375 ptr->rgbBlue = GetBValue(c_L);
2381 /**********************************************************************
2382 * BITMAP_Load
2384 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2385 INT desiredx, INT desiredy, UINT loadflags )
2387 HBITMAP hbitmap = 0, orig_bm;
2388 HRSRC hRsrc;
2389 HGLOBAL handle;
2390 char *ptr = NULL;
2391 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2392 int size;
2393 BYTE pix;
2394 char *bits;
2395 LONG width, height, new_width, new_height;
2396 WORD bpp_dummy;
2397 DWORD compr_dummy, offbits = 0;
2398 INT bm_type;
2399 HDC screen_mem_dc = NULL;
2401 if (!(loadflags & LR_LOADFROMFILE))
2403 if (!instance)
2405 /* OEM bitmap: try to load the resource from user32.dll */
2406 instance = user32_module;
2409 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2410 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2412 if ((info = LockResource( handle )) == NULL) return 0;
2414 else
2416 BITMAPFILEHEADER * bmfh;
2418 if (!(ptr = map_fileW( name, NULL ))) return 0;
2419 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2420 bmfh = (BITMAPFILEHEADER *)ptr;
2421 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2423 WARN("Invalid/unsupported bitmap format!\n");
2424 goto end_close;
2426 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2429 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2430 WARN("Broken BitmapInfoHeader!\n");
2431 goto end_close;
2434 size = bitmap_info_size(info, DIB_RGB_COLORS);
2435 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2436 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2438 if (!fix_info || !scaled_info) goto end;
2439 memcpy(fix_info, info, size);
2441 pix = *((LPBYTE)info + size);
2442 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2444 memcpy(scaled_info, fix_info, size);
2445 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2446 &bpp_dummy, &compr_dummy);
2447 if(desiredx != 0)
2448 new_width = desiredx;
2449 else
2450 new_width = width;
2452 if(desiredy != 0)
2453 new_height = height > 0 ? desiredy : -desiredy;
2454 else
2455 new_height = height;
2457 if(bm_type == 0)
2459 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2460 core->bcWidth = new_width;
2461 core->bcHeight = new_height;
2463 else
2465 scaled_info->bmiHeader.biWidth = new_width;
2466 scaled_info->bmiHeader.biHeight = new_height;
2469 if (new_height < 0) new_height = -new_height;
2471 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2472 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2474 bits = (char *)info + (offbits ? offbits : size);
2476 if (loadflags & LR_CREATEDIBSECTION)
2478 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2479 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2481 else
2483 if (is_dib_monochrome(fix_info))
2484 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2485 else
2486 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2489 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2490 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2491 SelectObject(screen_mem_dc, orig_bm);
2493 end:
2494 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2495 HeapFree(GetProcessHeap(), 0, scaled_info);
2496 HeapFree(GetProcessHeap(), 0, fix_info);
2497 end_close:
2498 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2500 return hbitmap;
2503 /**********************************************************************
2504 * LoadImageA (USER32.@)
2506 * See LoadImageW.
2508 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2509 INT desiredx, INT desiredy, UINT loadflags)
2511 HANDLE res;
2512 LPWSTR u_name;
2514 if (IS_INTRESOURCE(name))
2515 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2517 __TRY {
2518 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2519 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2520 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2522 __EXCEPT_PAGE_FAULT {
2523 SetLastError( ERROR_INVALID_PARAMETER );
2524 return 0;
2526 __ENDTRY
2527 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2528 HeapFree(GetProcessHeap(), 0, u_name);
2529 return res;
2533 /******************************************************************************
2534 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2536 * PARAMS
2537 * hinst [I] Handle of instance that contains image
2538 * name [I] Name of image
2539 * type [I] Type of image
2540 * desiredx [I] Desired width
2541 * desiredy [I] Desired height
2542 * loadflags [I] Load flags
2544 * RETURNS
2545 * Success: Handle to newly loaded image
2546 * Failure: NULL
2548 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2550 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2551 INT desiredx, INT desiredy, UINT loadflags )
2553 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2554 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2556 if (loadflags & LR_DEFAULTSIZE) {
2557 if (type == IMAGE_ICON) {
2558 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2559 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2560 } else if (type == IMAGE_CURSOR) {
2561 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2562 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2565 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2566 switch (type) {
2567 case IMAGE_BITMAP:
2568 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2570 case IMAGE_ICON:
2571 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2572 if (screen_dc)
2574 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2575 GetDeviceCaps(screen_dc, BITSPIXEL),
2576 FALSE, loadflags);
2578 break;
2580 case IMAGE_CURSOR:
2581 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2582 1, TRUE, loadflags);
2584 return 0;
2587 /******************************************************************************
2588 * CopyImage (USER32.@) Creates new image and copies attributes to it
2590 * PARAMS
2591 * hnd [I] Handle to image to copy
2592 * type [I] Type of image to copy
2593 * desiredx [I] Desired width of new image
2594 * desiredy [I] Desired height of new image
2595 * flags [I] Copy flags
2597 * RETURNS
2598 * Success: Handle to newly created image
2599 * Failure: NULL
2601 * BUGS
2602 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2603 * all other versions (95/2000/XP have been tested) ignore it.
2605 * NOTES
2606 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2607 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2608 * the copy will have the same depth as the screen.
2609 * The content of the image will only be copied if the bit depth of the
2610 * original image is compatible with the bit depth of the screen, or
2611 * if the source is a DIB section.
2612 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2614 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2615 INT desiredy, UINT flags )
2617 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2618 hnd, type, desiredx, desiredy, flags);
2620 switch (type)
2622 case IMAGE_BITMAP:
2624 HBITMAP res = NULL;
2625 DIBSECTION ds;
2626 int objSize;
2627 BITMAPINFO * bi;
2629 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2630 if (!objSize) return 0;
2631 if ((desiredx < 0) || (desiredy < 0)) return 0;
2633 if (flags & LR_COPYFROMRESOURCE)
2635 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2638 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2639 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2641 /* Allocate memory for a BITMAPINFOHEADER structure and a
2642 color table. The maximum number of colors in a color table
2643 is 256 which corresponds to a bitmap with depth 8.
2644 Bitmaps with higher depths don't have color tables. */
2645 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2646 if (!bi) return 0;
2648 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2649 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2650 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2651 bi->bmiHeader.biCompression = BI_RGB;
2653 if (flags & LR_CREATEDIBSECTION)
2655 /* Create a DIB section. LR_MONOCHROME is ignored */
2656 void * bits;
2657 HDC dc = CreateCompatibleDC(NULL);
2659 if (objSize == sizeof(DIBSECTION))
2661 /* The source bitmap is a DIB.
2662 Get its attributes to create an exact copy */
2663 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2666 /* Get the color table or the color masks */
2667 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2669 bi->bmiHeader.biWidth = desiredx;
2670 bi->bmiHeader.biHeight = desiredy;
2671 bi->bmiHeader.biSizeImage = 0;
2673 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2674 DeleteDC(dc);
2676 else
2678 /* Create a device-dependent bitmap */
2680 BOOL monochrome = (flags & LR_MONOCHROME);
2682 if (objSize == sizeof(DIBSECTION))
2684 /* The source bitmap is a DIB section.
2685 Get its attributes */
2686 HDC dc = CreateCompatibleDC(NULL);
2687 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2688 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2689 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2690 DeleteDC(dc);
2692 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2694 /* Look if the colors of the DIB are black and white */
2696 monochrome =
2697 (bi->bmiColors[0].rgbRed == 0xff
2698 && bi->bmiColors[0].rgbGreen == 0xff
2699 && bi->bmiColors[0].rgbBlue == 0xff
2700 && bi->bmiColors[0].rgbReserved == 0
2701 && bi->bmiColors[1].rgbRed == 0
2702 && bi->bmiColors[1].rgbGreen == 0
2703 && bi->bmiColors[1].rgbBlue == 0
2704 && bi->bmiColors[1].rgbReserved == 0)
2706 (bi->bmiColors[0].rgbRed == 0
2707 && bi->bmiColors[0].rgbGreen == 0
2708 && bi->bmiColors[0].rgbBlue == 0
2709 && bi->bmiColors[0].rgbReserved == 0
2710 && bi->bmiColors[1].rgbRed == 0xff
2711 && bi->bmiColors[1].rgbGreen == 0xff
2712 && bi->bmiColors[1].rgbBlue == 0xff
2713 && bi->bmiColors[1].rgbReserved == 0);
2716 else if (!monochrome)
2718 monochrome = ds.dsBm.bmBitsPixel == 1;
2721 if (monochrome)
2723 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2725 else
2727 HDC screenDC = GetDC(NULL);
2728 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2729 ReleaseDC(NULL, screenDC);
2733 if (res)
2735 /* Only copy the bitmap if it's a DIB section or if it's
2736 compatible to the screen */
2737 BOOL copyContents;
2739 if (objSize == sizeof(DIBSECTION))
2741 copyContents = TRUE;
2743 else
2745 HDC screenDC = GetDC(NULL);
2746 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2747 ReleaseDC(NULL, screenDC);
2749 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2752 if (copyContents)
2754 /* The source bitmap may already be selected in a device context,
2755 use GetDIBits/StretchDIBits and not StretchBlt */
2757 HDC dc;
2758 void * bits;
2760 dc = CreateCompatibleDC(NULL);
2762 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2763 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2764 bi->bmiHeader.biSizeImage = 0;
2765 bi->bmiHeader.biClrUsed = 0;
2766 bi->bmiHeader.biClrImportant = 0;
2768 /* Fill in biSizeImage */
2769 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2770 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2772 if (bits)
2774 HBITMAP oldBmp;
2776 /* Get the image bits of the source bitmap */
2777 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2779 /* Copy it to the destination bitmap */
2780 oldBmp = SelectObject(dc, res);
2781 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2782 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2783 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2784 SelectObject(dc, oldBmp);
2786 HeapFree(GetProcessHeap(), 0, bits);
2789 DeleteDC(dc);
2792 if (flags & LR_COPYDELETEORG)
2794 DeleteObject(hnd);
2797 HeapFree(GetProcessHeap(), 0, bi);
2798 return res;
2800 case IMAGE_ICON:
2801 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2802 case IMAGE_CURSOR:
2803 /* Should call CURSORICON_ExtCopy but more testing
2804 * needs to be done before we change this
2806 if (flags) FIXME("Flags are ignored\n");
2807 return CopyCursor(hnd);
2809 return 0;
2813 /******************************************************************************
2814 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2816 * RETURNS
2817 * Success: Handle to specified bitmap
2818 * Failure: NULL
2820 HBITMAP WINAPI LoadBitmapW(
2821 HINSTANCE instance, /* [in] Handle to application instance */
2822 LPCWSTR name) /* [in] Address of bitmap resource name */
2824 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2827 /**********************************************************************
2828 * LoadBitmapA (USER32.@)
2830 * See LoadBitmapW.
2832 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2834 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );