wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / user32 / cursoricon.c
blobcd7d3eda507886784a02755cab61943963cddc0e
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
40 #include "controls.h"
41 #include "win.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
48 WINE_DECLARE_DEBUG_CHANNEL(icon);
49 WINE_DECLARE_DEBUG_CHANNEL(resource);
51 #include "pshpack1.h"
53 typedef struct {
54 BYTE bWidth;
55 BYTE bHeight;
56 BYTE bColorCount;
57 BYTE bReserved;
58 WORD xHotspot;
59 WORD yHotspot;
60 DWORD dwDIBSize;
61 DWORD dwDIBOffset;
62 } CURSORICONFILEDIRENTRY;
64 typedef struct
66 WORD idReserved;
67 WORD idType;
68 WORD idCount;
69 CURSORICONFILEDIRENTRY idEntries[1];
70 } CURSORICONFILEDIR;
72 #include "poppack.h"
74 static HDC screen_dc;
76 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
78 static struct list icon_cache = LIST_INIT( icon_cache );
80 /**********************************************************************
81 * User objects management
84 struct cursoricon_frame
86 HBITMAP color; /* color bitmap */
87 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
88 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
91 struct cursoricon_object
93 struct user_object obj; /* object header */
94 struct list entry; /* entry in shared icons list */
95 ULONG_PTR param; /* opaque param used by 16-bit code */
96 HMODULE module; /* module for icons loaded from resources */
97 LPWSTR resname; /* resource name for icons loaded from resources */
98 HRSRC rsrc; /* resource for shared icons */
99 BOOL is_icon; /* whether icon or cursor */
100 UINT width;
101 UINT height;
102 POINT hotspot;
103 UINT num_frames; /* number of frames in the icon/cursor */
104 UINT ms_delay; /* delay between frames (in milliseconds) */
105 struct cursoricon_frame frames[1]; /* icon frame information */
108 static HICON alloc_icon_handle( UINT num_frames )
110 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
111 FIELD_OFFSET( struct cursoricon_object, frames[num_frames] ));
113 if (!obj) return 0;
114 obj->num_frames = num_frames;
115 return alloc_user_handle( &obj->obj, USER_ICON );
118 static struct cursoricon_object *get_icon_ptr( HICON handle )
120 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
121 if (obj == OBJ_OTHER_PROCESS)
123 WARN( "icon handle %p from other process\n", handle );
124 obj = NULL;
126 return obj;
129 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
131 release_user_handle_ptr( ptr );
134 static BOOL free_icon_handle( HICON handle )
136 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
138 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
139 else if (obj)
141 ULONG_PTR param = obj->param;
142 UINT i;
144 assert( !obj->rsrc ); /* shared icons can't be freed */
146 for (i=0; i<obj->num_frames; i++)
148 if (obj->frames[i].alpha) DeleteObject( obj->frames[i].alpha );
149 if (obj->frames[i].color) DeleteObject( obj->frames[i].color );
150 DeleteObject( obj->frames[i].mask );
152 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
153 HeapFree( GetProcessHeap(), 0, obj );
154 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
155 USER_Driver->pDestroyCursorIcon( handle );
156 return TRUE;
158 return FALSE;
161 ULONG_PTR get_icon_param( HICON handle )
163 ULONG_PTR ret = 0;
164 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
166 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
167 else if (obj)
169 ret = obj->param;
170 release_user_handle_ptr( obj );
172 return ret;
175 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
177 ULONG_PTR ret = 0;
178 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
180 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
181 else if (obj)
183 ret = obj->param;
184 obj->param = param;
185 release_user_handle_ptr( obj );
187 return ret;
191 /***********************************************************************
192 * map_fileW
194 * Helper function to map a file to memory:
195 * name - file name
196 * [RETURN] ptr - pointer to mapped file
197 * [RETURN] filesize - pointer size of file to be stored if not NULL
199 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
201 HANDLE hFile, hMapping;
202 LPVOID ptr = NULL;
204 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
205 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
206 if (hFile != INVALID_HANDLE_VALUE)
208 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
209 if (hMapping)
211 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
212 CloseHandle( hMapping );
213 if (filesize)
214 *filesize = GetFileSize( hFile, NULL );
216 CloseHandle( hFile );
218 return ptr;
222 /***********************************************************************
223 * get_dib_width_bytes
225 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
227 static int get_dib_width_bytes( int width, int depth )
229 int words;
231 switch(depth)
233 case 1: words = (width + 31) / 32; break;
234 case 4: words = (width + 7) / 8; break;
235 case 8: words = (width + 3) / 4; break;
236 case 15:
237 case 16: words = (width + 1) / 2; break;
238 case 24: words = (width * 3 + 3)/4; break;
239 default:
240 WARN("(%d): Unsupported depth\n", depth );
241 /* fall through */
242 case 32:
243 words = width;
245 return 4 * words;
249 /***********************************************************************
250 * bitmap_info_size
252 * Return the size of the bitmap info structure including color table.
254 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
256 unsigned int colors, size, masks = 0;
258 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
260 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
261 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
262 return sizeof(BITMAPCOREHEADER) + colors *
263 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
265 else /* assume BITMAPINFOHEADER */
267 colors = info->bmiHeader.biClrUsed;
268 if (colors > 256) /* buffer overflow otherwise */
269 colors = 256;
270 if (!colors && (info->bmiHeader.biBitCount <= 8))
271 colors = 1 << info->bmiHeader.biBitCount;
272 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
273 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
274 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
279 /***********************************************************************
280 * copy_bitmap
282 * Helper function to duplicate a bitmap.
284 static HBITMAP copy_bitmap( HBITMAP bitmap )
286 HDC src, dst = 0;
287 HBITMAP new_bitmap = 0;
288 BITMAP bmp;
290 if (!bitmap) return 0;
291 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
293 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
295 SelectObject( src, bitmap );
296 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
298 SelectObject( dst, new_bitmap );
299 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
302 DeleteDC( dst );
303 DeleteDC( src );
304 return new_bitmap;
308 /***********************************************************************
309 * is_dib_monochrome
311 * Returns whether a DIB can be converted to a monochrome DDB.
313 * A DIB can be converted if its color table contains only black and
314 * white. Black must be the first color in the color table.
316 * Note : If the first color in the color table is white followed by
317 * black, we can't convert it to a monochrome DDB with
318 * SetDIBits, because black and white would be inverted.
320 static BOOL is_dib_monochrome( const BITMAPINFO* info )
322 if (info->bmiHeader.biBitCount != 1) return FALSE;
324 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
326 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
328 /* Check if the first color is black */
329 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
331 rgb++;
333 /* Check if the second color is white */
334 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
335 && (rgb->rgbtBlue == 0xff));
337 else return FALSE;
339 else /* assume BITMAPINFOHEADER */
341 const RGBQUAD *rgb = info->bmiColors;
343 /* Check if the first color is black */
344 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
345 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
347 rgb++;
349 /* Check if the second color is white */
350 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
351 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
353 else return FALSE;
357 /***********************************************************************
358 * DIB_GetBitmapInfo
360 * Get the info from a bitmap header.
361 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
363 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
364 LONG *height, WORD *bpp, DWORD *compr )
366 if (header->biSize == sizeof(BITMAPCOREHEADER))
368 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
369 *width = core->bcWidth;
370 *height = core->bcHeight;
371 *bpp = core->bcBitCount;
372 *compr = 0;
373 return 0;
375 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
376 header->biSize == sizeof(BITMAPV4HEADER) ||
377 header->biSize == sizeof(BITMAPV5HEADER))
379 *width = header->biWidth;
380 *height = header->biHeight;
381 *bpp = header->biBitCount;
382 *compr = header->biCompression;
383 return 1;
385 WARN("unknown/wrong size (%u) for header\n", header->biSize);
386 return -1;
389 /**********************************************************************
390 * get_icon_size
392 BOOL get_icon_size( HICON handle, SIZE *size )
394 struct cursoricon_object *info;
396 if (!(info = get_icon_ptr( handle ))) return FALSE;
397 size->cx = info->width;
398 size->cy = info->height;
399 release_icon_ptr( handle, info );
400 return TRUE;
404 * The following macro functions account for the irregularities of
405 * accessing cursor and icon resources in files and resource entries.
407 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, int n,
408 int *width, int *height, int *bits );
410 /**********************************************************************
411 * CURSORICON_FindBestIcon
413 * Find the icon closest to the requested size and bit depth.
415 static int CURSORICON_FindBestIcon( LPCVOID dir, fnGetCIEntry get_entry,
416 int width, int height, int depth, UINT loadflags )
418 int i, cx, cy, bits, bestEntry = -1;
419 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
420 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
422 /* Find Best Fit */
423 iTotalDiff = 0xFFFFFFFF;
424 iColorDiff = 0xFFFFFFFF;
426 if (loadflags & LR_DEFAULTSIZE)
428 if (!width) width = GetSystemMetrics( SM_CXICON );
429 if (!height) height = GetSystemMetrics( SM_CYICON );
431 else if (!width && !height)
433 /* use the size of the first entry */
434 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
435 iTotalDiff = 0;
438 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
440 iTempXDiff = abs(width - cx);
441 iTempYDiff = abs(height - cy);
443 if(iTotalDiff > (iTempXDiff + iTempYDiff))
445 iXDiff = iTempXDiff;
446 iYDiff = iTempYDiff;
447 iTotalDiff = iXDiff + iYDiff;
451 /* Find Best Colors for Best Fit */
452 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
454 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
456 iTempColorDiff = abs(depth - bits);
457 if(iColorDiff > iTempColorDiff)
459 bestEntry = i;
460 iColorDiff = iTempColorDiff;
465 return bestEntry;
468 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
469 int *width, int *height, int *bits )
471 const CURSORICONDIR *resdir = dir;
472 const ICONRESDIR *icon;
474 if ( resdir->idCount <= n )
475 return FALSE;
476 icon = &resdir->idEntries[n].ResInfo.icon;
477 *width = icon->bWidth;
478 *height = icon->bHeight;
479 *bits = resdir->idEntries[n].wBitCount;
480 return TRUE;
483 /**********************************************************************
484 * CURSORICON_FindBestCursor
486 * Find the cursor closest to the requested size.
488 * FIXME: parameter 'color' ignored.
490 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
491 int width, int height, int depth, UINT loadflags )
493 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
495 if (loadflags & LR_DEFAULTSIZE)
497 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
498 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
500 else if (!width && !height)
502 /* use the first entry */
503 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
504 return 0;
507 /* Double height to account for AND and XOR masks */
509 height *= 2;
511 /* First find the largest one smaller than or equal to the requested size*/
513 maxwidth = maxheight = 0;
514 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
516 if ((cx <= width) && (cy <= height) &&
517 (cx > maxwidth) && (cy > maxheight))
519 bestEntry = i;
520 maxwidth = cx;
521 maxheight = cy;
524 if (bestEntry != -1) return bestEntry;
526 /* Now find the smallest one larger than the requested size */
528 maxwidth = maxheight = 255;
529 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
531 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
533 bestEntry = i;
534 maxwidth = cx;
535 maxheight = cy;
539 return bestEntry;
542 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
543 int *width, int *height, int *bits )
545 const CURSORICONDIR *resdir = dir;
546 const CURSORDIR *cursor;
548 if ( resdir->idCount <= n )
549 return FALSE;
550 cursor = &resdir->idEntries[n].ResInfo.cursor;
551 *width = cursor->wWidth;
552 *height = cursor->wHeight;
553 *bits = resdir->idEntries[n].wBitCount;
554 return TRUE;
557 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
558 int width, int height, int depth,
559 UINT loadflags )
561 int n;
563 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
564 width, height, depth, loadflags );
565 if ( n < 0 )
566 return NULL;
567 return &dir->idEntries[n];
570 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
571 int width, int height, int depth,
572 UINT loadflags )
574 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
575 width, height, depth, loadflags );
576 if ( n < 0 )
577 return NULL;
578 return &dir->idEntries[n];
581 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
582 int *width, int *height, int *bits )
584 const CURSORICONFILEDIR *filedir = dir;
585 const CURSORICONFILEDIRENTRY *entry;
586 const BITMAPINFOHEADER *info;
588 if ( filedir->idCount <= n )
589 return FALSE;
590 entry = &filedir->idEntries[n];
591 /* FIXME: check against file size */
592 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
593 *width = entry->bWidth;
594 *height = entry->bHeight;
595 *bits = info->biBitCount;
596 return TRUE;
599 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
600 int width, int height, int depth,
601 UINT loadflags )
603 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
604 width, height, depth, loadflags );
605 if ( n < 0 )
606 return NULL;
607 return &dir->idEntries[n];
610 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
611 int width, int height, int depth,
612 UINT loadflags )
614 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
615 width, height, depth, loadflags );
616 if ( n < 0 )
617 return NULL;
618 return &dir->idEntries[n];
621 /***********************************************************************
622 * bmi_has_alpha
624 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
626 int i;
627 BOOL has_alpha = FALSE;
628 const unsigned char *ptr = bits;
630 if (info->bmiHeader.biBitCount != 32) return FALSE;
631 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
632 if ((has_alpha = (ptr[3] != 0))) break;
633 return has_alpha;
636 /***********************************************************************
637 * create_alpha_bitmap
639 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
641 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
642 const BITMAPINFO *src_info, const void *color_bits )
644 HBITMAP alpha = 0;
645 BITMAPINFO *info = NULL;
646 BITMAP bm;
647 HDC hdc;
648 void *bits;
649 unsigned char *ptr;
650 int i;
652 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
653 if (bm.bmBitsPixel != 32) return 0;
655 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
656 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
657 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
658 info->bmiHeader.biWidth = bm.bmWidth;
659 info->bmiHeader.biHeight = -bm.bmHeight;
660 info->bmiHeader.biPlanes = 1;
661 info->bmiHeader.biBitCount = 32;
662 info->bmiHeader.biCompression = BI_RGB;
663 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
664 info->bmiHeader.biXPelsPerMeter = 0;
665 info->bmiHeader.biYPelsPerMeter = 0;
666 info->bmiHeader.biClrUsed = 0;
667 info->bmiHeader.biClrImportant = 0;
668 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
670 if (src_info)
672 SelectObject( hdc, alpha );
673 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
674 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
675 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
678 else
680 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
681 if (!bmi_has_alpha( info, bits ))
683 DeleteObject( alpha );
684 alpha = 0;
685 goto done;
689 /* pre-multiply by alpha */
690 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
692 unsigned int alpha = ptr[3];
693 ptr[0] = ptr[0] * alpha / 255;
694 ptr[1] = ptr[1] * alpha / 255;
695 ptr[2] = ptr[2] * alpha / 255;
698 done:
699 DeleteDC( hdc );
700 HeapFree( GetProcessHeap(), 0, info );
701 return alpha;
705 /***********************************************************************
706 * create_icon_bitmaps
708 * Create the color, mask and alpha bitmaps from the DIB info.
710 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
711 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
713 BOOL monochrome = is_dib_monochrome( bmi );
714 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
715 BITMAPINFO *info;
716 const void *color_bits, *mask_bits;
717 BOOL ret = FALSE;
718 HDC hdc = 0;
720 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
721 return FALSE;
722 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
724 memcpy( info, bmi, size );
725 info->bmiHeader.biHeight /= 2;
727 color_bits = (const char*)bmi + size;
728 mask_bits = (const char*)color_bits +
729 get_dib_width_bytes( bmi->bmiHeader.biWidth,
730 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
732 *alpha = 0;
733 if (monochrome)
735 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
736 *color = 0;
738 /* copy color data into second half of mask bitmap */
739 SelectObject( hdc, *mask );
740 StretchDIBits( hdc, 0, height, width, height,
741 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
742 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
744 else
746 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
747 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
748 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
750 DeleteObject( *mask );
751 goto done;
753 SelectObject( hdc, *color );
754 StretchDIBits( hdc, 0, 0, width, height,
755 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
756 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
758 if (bmi_has_alpha( info, color_bits ))
759 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
761 /* convert info to monochrome to copy the mask */
762 info->bmiHeader.biBitCount = 1;
763 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
765 RGBQUAD *rgb = info->bmiColors;
767 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
768 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
769 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
770 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
772 else
774 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
776 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
777 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
781 SelectObject( hdc, *mask );
782 StretchDIBits( hdc, 0, 0, width, height,
783 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
784 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
785 ret = TRUE;
787 done:
788 DeleteDC( hdc );
789 HeapFree( GetProcessHeap(), 0, info );
790 return ret;
793 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
794 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
796 HICON hObj;
797 HBITMAP color = 0, mask = 0, alpha = 0;
798 BOOL do_stretch;
800 /* Check bitmap header */
802 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
803 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
804 bmi->bmiHeader.biCompression != BI_RGB) )
806 WARN_(cursor)("\tinvalid resource bitmap header.\n");
807 return 0;
810 if (cFlag & LR_DEFAULTSIZE)
812 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
813 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
815 else
817 if (!width) width = bmi->bmiHeader.biWidth;
818 if (!height) height = bmi->bmiHeader.biHeight/2;
820 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
821 (bmi->bmiHeader.biWidth != width);
823 /* Scale the hotspot */
824 if (bIcon)
826 hotspot.x = width / 2;
827 hotspot.y = height / 2;
829 else if (do_stretch)
831 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
832 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
835 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
836 if (!screen_dc) return 0;
838 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
840 hObj = alloc_icon_handle(1);
841 if (hObj)
843 struct cursoricon_object *info = get_icon_ptr( hObj );
845 info->is_icon = bIcon;
846 info->module = module;
847 info->hotspot = hotspot;
848 info->width = width;
849 info->height = height;
850 info->frames[0].color = color;
851 info->frames[0].mask = mask;
852 info->frames[0].alpha = alpha;
853 if (!IS_INTRESOURCE(resname))
855 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
856 if (info->resname) strcpyW( info->resname, resname );
858 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
860 if (module && (cFlag & LR_SHARED))
862 info->rsrc = rsrc;
863 list_add_head( &icon_cache, &info->entry );
865 release_icon_ptr( hObj, info );
866 USER_Driver->pCreateCursorIcon( hObj );
868 else
870 DeleteObject( color );
871 DeleteObject( alpha );
872 DeleteObject( mask );
874 return hObj;
878 /**********************************************************************
879 * .ANI cursor support
881 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
882 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
883 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
885 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
886 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
887 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
888 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
889 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
890 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
892 #define ANI_FLAG_ICON 0x1
893 #define ANI_FLAG_SEQUENCE 0x2
895 typedef struct {
896 DWORD header_size;
897 DWORD num_frames;
898 DWORD num_steps;
899 DWORD width;
900 DWORD height;
901 DWORD bpp;
902 DWORD num_planes;
903 DWORD display_rate;
904 DWORD flags;
905 } ani_header;
907 typedef struct {
908 DWORD data_size;
909 const unsigned char *data;
910 } riff_chunk_t;
912 static void dump_ani_header( const ani_header *header )
914 TRACE(" header size: %d\n", header->header_size);
915 TRACE(" frames: %d\n", header->num_frames);
916 TRACE(" steps: %d\n", header->num_steps);
917 TRACE(" width: %d\n", header->width);
918 TRACE(" height: %d\n", header->height);
919 TRACE(" bpp: %d\n", header->bpp);
920 TRACE(" planes: %d\n", header->num_planes);
921 TRACE(" display rate: %d\n", header->display_rate);
922 TRACE(" flags: 0x%08x\n", header->flags);
927 * RIFF:
928 * DWORD "RIFF"
929 * DWORD size
930 * DWORD riff_id
931 * BYTE[] data
933 * LIST:
934 * DWORD "LIST"
935 * DWORD size
936 * DWORD list_id
937 * BYTE[] data
939 * CHUNK:
940 * DWORD chunk_id
941 * DWORD size
942 * BYTE[] data
944 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
946 const unsigned char *ptr = parent_chunk->data;
947 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
949 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
951 while (ptr < end)
953 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
954 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
956 ptr += sizeof(DWORD);
957 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
958 ptr += sizeof(DWORD);
959 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
960 chunk->data = ptr;
962 return;
965 ptr += sizeof(DWORD);
966 ptr += (*(const DWORD *)ptr + 1) & ~1;
967 ptr += sizeof(DWORD);
973 * .ANI layout:
975 * RIFF:'ACON' RIFF chunk
976 * |- CHUNK:'anih' Header
977 * |- CHUNK:'seq ' Sequence information (optional)
978 * \- LIST:'fram' Frame list
979 * |- CHUNK:icon Cursor frames
980 * |- CHUNK:icon
981 * |- ...
982 * \- CHUNK:icon
984 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
985 INT width, INT height, INT depth, UINT loadflags )
987 struct cursoricon_object *info;
988 ani_header header = {0};
989 HCURSOR cursor = 0;
990 UINT i, error = 0;
992 riff_chunk_t root_chunk = { bits_size, bits };
993 riff_chunk_t ACON_chunk = {0};
994 riff_chunk_t anih_chunk = {0};
995 riff_chunk_t fram_chunk = {0};
996 const unsigned char *icon_chunk;
997 const unsigned char *icon_data;
999 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1001 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1002 if (!ACON_chunk.data)
1004 ERR("Failed to get root chunk.\n");
1005 return 0;
1008 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1009 if (!anih_chunk.data)
1011 ERR("Failed to get 'anih' chunk.\n");
1012 return 0;
1014 memcpy( &header, anih_chunk.data, sizeof(header) );
1015 dump_ani_header( &header );
1017 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1018 if (!fram_chunk.data)
1020 ERR("Failed to get icon list.\n");
1021 return 0;
1024 cursor = alloc_icon_handle( header.num_frames );
1025 if (!cursor) return 0;
1027 info = get_icon_ptr( cursor );
1028 info->is_icon = FALSE;
1030 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1031 info->ms_delay = (100 * header.display_rate) / 6;
1033 icon_chunk = fram_chunk.data;
1034 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1035 for (i=0; i<header.num_frames; i++)
1037 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1038 struct cursoricon_frame *frame = &info->frames[i];
1039 const CURSORICONFILEDIRENTRY *entry;
1040 const BITMAPINFO *bmi;
1042 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1043 width, height, depth, loadflags );
1045 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1046 info->hotspot.x = entry->xHotspot;
1047 info->hotspot.y = entry->yHotspot;
1048 if (!header.width || !header.height)
1050 header.width = entry->bWidth;
1051 header.height = entry->bHeight;
1054 /* Grab a frame from the animation */
1055 if (!create_icon_bitmaps( bmi, header.width, header.height,
1056 &frame->color, &frame->mask, &frame->alpha ))
1058 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1059 error = TRUE;
1060 if (i == 0)
1062 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1063 info->num_frames = 0;
1064 release_icon_ptr( cursor, info );
1065 free_icon_handle( cursor );
1066 return 0;
1068 break;
1071 /* Advance to the next chunk */
1072 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1073 icon_data = icon_chunk + (2 * sizeof(DWORD));
1076 /* There was an error but we at least decoded the first frame, so just use that frame */
1077 if (error)
1079 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1080 for (i=1; i<info->num_frames; i++)
1082 if (info->frames[i].mask) DeleteObject( info->frames[i].mask );
1083 if (info->frames[i].color) DeleteObject( info->frames[i].color );
1084 if (info->frames[i].alpha) DeleteObject( info->frames[i].alpha );
1086 info->num_frames = 1;
1087 info->ms_delay = 0;
1089 info->width = header.width;
1090 info->height = header.height;
1091 release_icon_ptr( cursor, info );
1093 return cursor;
1097 /**********************************************************************
1098 * CreateIconFromResourceEx (USER32.@)
1100 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1101 * with cbSize parameter as well.
1103 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1104 BOOL bIcon, DWORD dwVersion,
1105 INT width, INT height,
1106 UINT cFlag )
1108 POINT hotspot;
1109 BITMAPINFO *bmi;
1111 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1112 bits, cbSize, dwVersion, width, height,
1113 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1115 if (!bits) return 0;
1117 if (dwVersion == 0x00020000)
1119 FIXME_(cursor)("\t2.xx resources are not supported\n");
1120 return 0;
1123 if (bIcon)
1125 hotspot.x = width / 2;
1126 hotspot.y = height / 2;
1127 bmi = (BITMAPINFO *)bits;
1129 else /* get the hotspot */
1131 SHORT *pt = (SHORT *)bits;
1132 hotspot.x = pt[0];
1133 hotspot.y = pt[1];
1134 bmi = (BITMAPINFO *)(pt + 2);
1137 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1141 /**********************************************************************
1142 * CreateIconFromResource (USER32.@)
1144 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1145 BOOL bIcon, DWORD dwVersion)
1147 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1151 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1152 INT width, INT height, INT depth,
1153 BOOL fCursor, UINT loadflags)
1155 const CURSORICONFILEDIRENTRY *entry;
1156 const CURSORICONFILEDIR *dir;
1157 DWORD filesize = 0;
1158 HICON hIcon = 0;
1159 LPBYTE bits;
1160 POINT hotspot;
1162 TRACE("loading %s\n", debugstr_w( filename ));
1164 bits = map_fileW( filename, &filesize );
1165 if (!bits)
1166 return hIcon;
1168 /* Check for .ani. */
1169 if (memcmp( bits, "RIFF", 4 ) == 0)
1171 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1172 goto end;
1175 dir = (const CURSORICONFILEDIR*) bits;
1176 if ( filesize < sizeof(*dir) )
1177 goto end;
1179 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1180 goto end;
1182 if ( fCursor )
1183 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1184 else
1185 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1187 if ( !entry )
1188 goto end;
1190 /* check that we don't run off the end of the file */
1191 if ( entry->dwDIBOffset > filesize )
1192 goto end;
1193 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1194 goto end;
1196 hotspot.x = entry->xHotspot;
1197 hotspot.y = entry->yHotspot;
1198 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1199 hotspot, !fCursor, width, height, loadflags );
1200 end:
1201 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1202 UnmapViewOfFile( bits );
1203 return hIcon;
1206 /**********************************************************************
1207 * CURSORICON_Load
1209 * Load a cursor or icon from resource or file.
1211 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1212 INT width, INT height, INT depth,
1213 BOOL fCursor, UINT loadflags)
1215 HANDLE handle = 0;
1216 HICON hIcon = 0;
1217 HRSRC hRsrc;
1218 const CURSORICONDIR *dir;
1219 const CURSORICONDIRENTRY *dirEntry;
1220 LPBYTE bits;
1221 WORD wResId;
1222 POINT hotspot;
1224 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1225 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1227 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1228 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1230 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1232 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1233 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1235 /* Get directory resource ID */
1237 if (!(hRsrc = FindResourceW( hInstance, name,
1238 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1239 return 0;
1241 /* Find the best entry in the directory */
1243 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1244 if (!(dir = LockResource( handle ))) return 0;
1245 if (fCursor)
1246 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1247 else
1248 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1249 if (!dirEntry) return 0;
1250 wResId = dirEntry->wResId;
1251 FreeResource( handle );
1253 /* Load the resource */
1255 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1256 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1258 /* If shared icon, check whether it was already loaded */
1259 if (loadflags & LR_SHARED)
1261 struct cursoricon_object *ptr;
1263 USER_Lock();
1264 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1266 if (ptr->module != hInstance) continue;
1267 if (ptr->rsrc != hRsrc) continue;
1268 hIcon = ptr->obj.handle;
1269 break;
1271 USER_Unlock();
1272 if (hIcon) return hIcon;
1275 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1276 bits = LockResource( handle );
1278 if (!fCursor)
1280 hotspot.x = width / 2;
1281 hotspot.y = height / 2;
1283 else /* get the hotspot */
1285 SHORT *pt = (SHORT *)bits;
1286 hotspot.x = pt[0];
1287 hotspot.y = pt[1];
1288 bits += 2 * sizeof(SHORT);
1290 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1291 hotspot, !fCursor, width, height, loadflags );
1292 FreeResource( handle );
1293 return hIcon;
1297 /***********************************************************************
1298 * CreateCursor (USER32.@)
1300 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1301 INT xHotSpot, INT yHotSpot,
1302 INT nWidth, INT nHeight,
1303 LPCVOID lpANDbits, LPCVOID lpXORbits )
1305 ICONINFO info;
1306 HCURSOR hCursor;
1308 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1309 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1311 info.fIcon = FALSE;
1312 info.xHotspot = xHotSpot;
1313 info.yHotspot = yHotSpot;
1314 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1315 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1316 hCursor = CreateIconIndirect( &info );
1317 DeleteObject( info.hbmMask );
1318 DeleteObject( info.hbmColor );
1319 return hCursor;
1323 /***********************************************************************
1324 * CreateIcon (USER32.@)
1326 * Creates an icon based on the specified bitmaps. The bitmaps must be
1327 * provided in a device dependent format and will be resized to
1328 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1329 * depth. The provided bitmaps must be top-down bitmaps.
1330 * Although Windows does not support 15bpp(*) this API must support it
1331 * for Winelib applications.
1333 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1334 * format!
1336 * RETURNS
1337 * Success: handle to an icon
1338 * Failure: NULL
1340 * FIXME: Do we need to resize the bitmaps?
1342 HICON WINAPI CreateIcon(
1343 HINSTANCE hInstance, /* [in] the application's hInstance */
1344 INT nWidth, /* [in] the width of the provided bitmaps */
1345 INT nHeight, /* [in] the height of the provided bitmaps */
1346 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1347 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1348 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1349 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1351 ICONINFO iinfo;
1352 HICON hIcon;
1354 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1355 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1357 iinfo.fIcon = TRUE;
1358 iinfo.xHotspot = nWidth / 2;
1359 iinfo.yHotspot = nHeight / 2;
1360 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1361 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1363 hIcon = CreateIconIndirect( &iinfo );
1365 DeleteObject( iinfo.hbmMask );
1366 DeleteObject( iinfo.hbmColor );
1368 return hIcon;
1372 /***********************************************************************
1373 * CopyIcon (USER32.@)
1375 HICON WINAPI CopyIcon( HICON hIcon )
1377 struct cursoricon_object *ptrOld, *ptrNew;
1378 HICON hNew;
1380 if (!(ptrOld = get_icon_ptr( hIcon )))
1382 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1383 return 0;
1385 if ((hNew = alloc_icon_handle(1)))
1387 ptrNew = get_icon_ptr( hNew );
1388 ptrNew->is_icon = ptrOld->is_icon;
1389 ptrNew->width = ptrOld->width;
1390 ptrNew->height = ptrOld->height;
1391 ptrNew->hotspot = ptrOld->hotspot;
1392 ptrNew->frames[0].mask = copy_bitmap( ptrOld->frames[0].mask );
1393 ptrNew->frames[0].color = copy_bitmap( ptrOld->frames[0].color );
1394 ptrNew->frames[0].alpha = copy_bitmap( ptrOld->frames[0].alpha );
1395 release_icon_ptr( hNew, ptrNew );
1397 release_icon_ptr( hIcon, ptrOld );
1398 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1399 return hNew;
1403 /***********************************************************************
1404 * DestroyIcon (USER32.@)
1406 BOOL WINAPI DestroyIcon( HICON hIcon )
1408 BOOL ret = FALSE;
1409 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1411 TRACE_(icon)("%p\n", hIcon );
1413 if (obj)
1415 BOOL shared = (obj->rsrc != NULL);
1416 release_icon_ptr( hIcon, obj );
1417 ret = (GetCursor() != hIcon);
1418 if (!shared) free_icon_handle( hIcon );
1420 return ret;
1424 /***********************************************************************
1425 * DestroyCursor (USER32.@)
1427 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1429 return DestroyIcon( hCursor );
1432 /***********************************************************************
1433 * DrawIcon (USER32.@)
1435 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1437 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1440 /***********************************************************************
1441 * SetCursor (USER32.@)
1443 * Set the cursor shape.
1445 * RETURNS
1446 * A handle to the previous cursor shape.
1448 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1450 struct cursoricon_object *obj;
1451 HCURSOR hOldCursor;
1452 int show_count;
1453 BOOL ret;
1455 TRACE("%p\n", hCursor);
1457 SERVER_START_REQ( set_cursor )
1459 req->flags = SET_CURSOR_HANDLE;
1460 req->handle = wine_server_user_handle( hCursor );
1461 if ((ret = !wine_server_call_err( req )))
1463 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1464 show_count = reply->prev_count;
1467 SERVER_END_REQ;
1469 if (!ret) return 0;
1471 /* Change the cursor shape only if it is visible */
1472 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1474 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1475 release_icon_ptr( hOldCursor, obj );
1476 return hOldCursor;
1479 /***********************************************************************
1480 * ShowCursor (USER32.@)
1482 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1484 HCURSOR cursor;
1485 int increment = bShow ? 1 : -1;
1486 int count;
1488 SERVER_START_REQ( set_cursor )
1490 req->flags = SET_CURSOR_COUNT;
1491 req->show_count = increment;
1492 wine_server_call( req );
1493 cursor = wine_server_ptr_handle( reply->prev_handle );
1494 count = reply->prev_count + increment;
1496 SERVER_END_REQ;
1498 TRACE("%d, count=%d\n", bShow, count );
1500 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1501 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1503 return count;
1506 /***********************************************************************
1507 * GetCursor (USER32.@)
1509 HCURSOR WINAPI GetCursor(void)
1511 HCURSOR ret;
1513 SERVER_START_REQ( set_cursor )
1515 req->flags = 0;
1516 wine_server_call( req );
1517 ret = wine_server_ptr_handle( reply->prev_handle );
1519 SERVER_END_REQ;
1520 return ret;
1524 /***********************************************************************
1525 * ClipCursor (USER32.@)
1527 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1529 BOOL ret;
1530 RECT new_rect;
1532 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1534 SERVER_START_REQ( set_cursor )
1536 req->flags = SET_CURSOR_CLIP;
1537 if (rect)
1539 req->clip.left = rect->left;
1540 req->clip.top = rect->top;
1541 req->clip.right = rect->right;
1542 req->clip.bottom = rect->bottom;
1544 if ((ret = !wine_server_call( req )))
1546 new_rect.left = reply->new_clip.left;
1547 new_rect.top = reply->new_clip.top;
1548 new_rect.right = reply->new_clip.right;
1549 new_rect.bottom = reply->new_clip.bottom;
1552 SERVER_END_REQ;
1553 if (ret) USER_Driver->pClipCursor( &new_rect );
1554 return ret;
1558 /***********************************************************************
1559 * GetClipCursor (USER32.@)
1561 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1563 BOOL ret;
1565 if (!rect) return FALSE;
1567 SERVER_START_REQ( set_cursor )
1569 req->flags = 0;
1570 if ((ret = !wine_server_call( req )))
1572 rect->left = reply->new_clip.left;
1573 rect->top = reply->new_clip.top;
1574 rect->right = reply->new_clip.right;
1575 rect->bottom = reply->new_clip.bottom;
1578 SERVER_END_REQ;
1579 return ret;
1583 /***********************************************************************
1584 * SetSystemCursor (USER32.@)
1586 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1588 FIXME("(%p,%08x),stub!\n", hcur, id);
1589 return TRUE;
1593 /**********************************************************************
1594 * LookupIconIdFromDirectoryEx (USER32.@)
1596 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1597 INT width, INT height, UINT cFlag )
1599 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1600 UINT retVal = 0;
1601 if( dir && !dir->idReserved && (dir->idType & 3) )
1603 const CURSORICONDIRENTRY* entry;
1605 const HDC hdc = GetDC(0);
1606 const int depth = (cFlag & LR_MONOCHROME) ?
1607 1 : GetDeviceCaps(hdc, BITSPIXEL);
1608 ReleaseDC(0, hdc);
1610 if( bIcon )
1611 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1612 else
1613 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1615 if( entry ) retVal = entry->wResId;
1617 else WARN_(cursor)("invalid resource directory\n");
1618 return retVal;
1621 /**********************************************************************
1622 * LookupIconIdFromDirectory (USER32.@)
1624 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1626 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1629 /***********************************************************************
1630 * LoadCursorW (USER32.@)
1632 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1634 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1636 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1637 LR_SHARED | LR_DEFAULTSIZE );
1640 /***********************************************************************
1641 * LoadCursorA (USER32.@)
1643 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1645 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1647 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1648 LR_SHARED | LR_DEFAULTSIZE );
1651 /***********************************************************************
1652 * LoadCursorFromFileW (USER32.@)
1654 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1656 TRACE("%s\n", debugstr_w(name));
1658 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1659 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1662 /***********************************************************************
1663 * LoadCursorFromFileA (USER32.@)
1665 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1667 TRACE("%s\n", debugstr_a(name));
1669 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1670 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1673 /***********************************************************************
1674 * LoadIconW (USER32.@)
1676 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1678 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1680 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1681 LR_SHARED | LR_DEFAULTSIZE );
1684 /***********************************************************************
1685 * LoadIconA (USER32.@)
1687 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1689 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1691 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1692 LR_SHARED | LR_DEFAULTSIZE );
1695 /**********************************************************************
1696 * GetIconInfo (USER32.@)
1698 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1700 ICONINFOEXW infoW;
1702 infoW.cbSize = sizeof(infoW);
1703 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1704 iconinfo->fIcon = infoW.fIcon;
1705 iconinfo->xHotspot = infoW.xHotspot;
1706 iconinfo->yHotspot = infoW.yHotspot;
1707 iconinfo->hbmColor = infoW.hbmColor;
1708 iconinfo->hbmMask = infoW.hbmMask;
1709 return TRUE;
1712 /**********************************************************************
1713 * GetIconInfoExA (USER32.@)
1715 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1717 ICONINFOEXW infoW;
1719 if (info->cbSize != sizeof(*info))
1721 SetLastError( ERROR_INVALID_PARAMETER );
1722 return FALSE;
1724 infoW.cbSize = sizeof(infoW);
1725 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1726 info->fIcon = infoW.fIcon;
1727 info->xHotspot = infoW.xHotspot;
1728 info->yHotspot = infoW.yHotspot;
1729 info->hbmColor = infoW.hbmColor;
1730 info->hbmMask = infoW.hbmMask;
1731 info->wResID = infoW.wResID;
1732 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1733 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1734 return TRUE;
1737 /**********************************************************************
1738 * GetIconInfoExW (USER32.@)
1740 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1742 struct cursoricon_object *ptr;
1743 HMODULE module;
1744 BOOL ret = TRUE;
1746 if (info->cbSize != sizeof(*info))
1748 SetLastError( ERROR_INVALID_PARAMETER );
1749 return FALSE;
1751 if (!(ptr = get_icon_ptr( icon )))
1753 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1754 return FALSE;
1757 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1759 info->fIcon = ptr->is_icon;
1760 info->xHotspot = ptr->hotspot.x;
1761 info->yHotspot = ptr->hotspot.y;
1762 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1763 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1764 info->wResID = 0;
1765 info->szModName[0] = 0;
1766 info->szResName[0] = 0;
1767 if (ptr->module)
1769 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1770 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1772 if (!info->hbmMask || (!info->hbmColor && ptr->frames[0].color))
1774 DeleteObject( info->hbmMask );
1775 DeleteObject( info->hbmColor );
1776 ret = FALSE;
1778 module = ptr->module;
1779 release_icon_ptr( icon, ptr );
1780 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
1781 return ret;
1784 /* copy an icon bitmap, even when it can't be selected into a DC */
1785 /* helper for CreateIconIndirect */
1786 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1787 HBITMAP src, int width, int height )
1789 HDC hdc = CreateCompatibleDC( 0 );
1791 if (!SelectObject( hdc, src )) /* do it the hard way */
1793 BITMAPINFO *info;
1794 void *bits;
1796 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1797 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1798 info->bmiHeader.biWidth = width;
1799 info->bmiHeader.biHeight = height;
1800 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1801 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1802 info->bmiHeader.biCompression = BI_RGB;
1803 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1804 info->bmiHeader.biXPelsPerMeter = 0;
1805 info->bmiHeader.biYPelsPerMeter = 0;
1806 info->bmiHeader.biClrUsed = 0;
1807 info->bmiHeader.biClrImportant = 0;
1808 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1809 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1810 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1811 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1813 HeapFree( GetProcessHeap(), 0, bits );
1814 HeapFree( GetProcessHeap(), 0, info );
1816 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1818 DeleteDC( hdc );
1821 /**********************************************************************
1822 * CreateIconIndirect (USER32.@)
1824 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1826 BITMAP bmpXor, bmpAnd;
1827 HICON hObj;
1828 HBITMAP color = 0, mask;
1829 int width, height;
1830 HDC hdc;
1832 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1833 iconinfo->hbmColor, iconinfo->hbmMask,
1834 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1836 if (!iconinfo->hbmMask) return 0;
1838 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1839 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1840 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1841 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1843 if (iconinfo->hbmColor)
1845 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1846 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1847 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1848 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1850 width = bmpXor.bmWidth;
1851 height = bmpXor.bmHeight;
1852 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1854 color = CreateCompatibleBitmap( screen_dc, width, height );
1855 mask = CreateBitmap( width, height, 1, 1, NULL );
1857 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1859 else
1861 width = bmpAnd.bmWidth;
1862 height = bmpAnd.bmHeight;
1863 mask = CreateBitmap( width, height, 1, 1, NULL );
1866 hdc = CreateCompatibleDC( 0 );
1867 SelectObject( hdc, mask );
1868 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1870 if (color)
1872 SelectObject( hdc, color );
1873 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1875 else if (iconinfo->hbmColor)
1877 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1879 else height /= 2;
1881 DeleteDC( hdc );
1883 hObj = alloc_icon_handle(1);
1884 if (hObj)
1886 struct cursoricon_object *info = get_icon_ptr( hObj );
1888 info->is_icon = iconinfo->fIcon;
1889 info->width = width;
1890 info->height = height;
1891 info->frames[0].color = color;
1892 info->frames[0].mask = mask;
1893 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1894 if (info->is_icon)
1896 info->hotspot.x = width / 2;
1897 info->hotspot.y = height / 2;
1899 else
1901 info->hotspot.x = iconinfo->xHotspot;
1902 info->hotspot.y = iconinfo->yHotspot;
1905 release_icon_ptr( hObj, info );
1906 USER_Driver->pCreateCursorIcon( hObj );
1908 return hObj;
1911 /******************************************************************************
1912 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1914 * NOTES
1915 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1917 * PARAMS
1918 * hdc [I] Handle to device context
1919 * x0 [I] X coordinate of upper left corner
1920 * y0 [I] Y coordinate of upper left corner
1921 * hIcon [I] Handle to icon to draw
1922 * cxWidth [I] Width of icon
1923 * cyWidth [I] Height of icon
1924 * istep [I] Index of frame in animated cursor
1925 * hbr [I] Handle to background brush
1926 * flags [I] Icon-drawing flags
1928 * RETURNS
1929 * Success: TRUE
1930 * Failure: FALSE
1932 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1933 INT cxWidth, INT cyWidth, UINT istep,
1934 HBRUSH hbr, UINT flags )
1936 struct cursoricon_object *ptr;
1937 HDC hdc_dest, hMemDC;
1938 BOOL result = FALSE, DoOffscreen;
1939 HBITMAP hB_off = 0;
1940 COLORREF oldFg, oldBg;
1941 INT x, y, nStretchMode;
1943 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1944 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1946 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1947 if (istep >= ptr->num_frames)
1949 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
1950 release_icon_ptr( hIcon, ptr );
1951 return FALSE;
1953 if (!(hMemDC = CreateCompatibleDC( hdc )))
1955 release_icon_ptr( hIcon, ptr );
1956 return FALSE;
1959 if (flags & DI_NOMIRROR)
1960 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
1962 /* Calculate the size of the destination image. */
1963 if (cxWidth == 0)
1965 if (flags & DI_DEFAULTSIZE)
1966 cxWidth = GetSystemMetrics (SM_CXICON);
1967 else
1968 cxWidth = ptr->width;
1970 if (cyWidth == 0)
1972 if (flags & DI_DEFAULTSIZE)
1973 cyWidth = GetSystemMetrics (SM_CYICON);
1974 else
1975 cyWidth = ptr->height;
1978 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1980 if (DoOffscreen) {
1981 RECT r;
1983 r.left = 0;
1984 r.top = 0;
1985 r.right = cxWidth;
1986 r.bottom = cxWidth;
1988 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
1989 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
1991 DeleteDC( hdc_dest );
1992 goto failed;
1994 SelectObject(hdc_dest, hB_off);
1995 FillRect(hdc_dest, &r, hbr);
1996 x = y = 0;
1998 else
2000 hdc_dest = hdc;
2001 x = x0;
2002 y = y0;
2005 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2007 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2008 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2010 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
2012 BOOL is_mono = FALSE;
2014 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2016 BITMAP bm;
2017 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2018 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
2020 if (!is_mono)
2022 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2023 SelectObject( hMemDC, ptr->frames[istep].alpha );
2024 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2025 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
2029 if (flags & DI_MASK)
2031 SelectObject( hMemDC, ptr->frames[istep].mask );
2032 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2033 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
2036 if (flags & DI_IMAGE)
2038 if (ptr->frames[istep].color)
2040 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2041 SelectObject( hMemDC, ptr->frames[istep].color );
2042 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2043 hMemDC, 0, 0, ptr->width, ptr->height, rop );
2045 else
2047 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2048 SelectObject( hMemDC, ptr->frames[istep].mask );
2049 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2050 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
2054 done:
2055 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2057 SetTextColor( hdc, oldFg );
2058 SetBkColor( hdc, oldBg );
2059 SetStretchBltMode (hdc, nStretchMode);
2060 result = TRUE;
2061 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2062 if (hB_off) DeleteObject(hB_off);
2063 failed:
2064 DeleteDC( hMemDC );
2065 release_icon_ptr( hIcon, ptr );
2066 return result;
2069 /***********************************************************************
2070 * DIB_FixColorsToLoadflags
2072 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2073 * are in loadflags
2075 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2077 int colors;
2078 COLORREF c_W, c_S, c_F, c_L, c_C;
2079 int incr,i;
2080 RGBQUAD *ptr;
2081 int bitmap_type;
2082 LONG width;
2083 LONG height;
2084 WORD bpp;
2085 DWORD compr;
2087 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2089 WARN_(resource)("Invalid bitmap\n");
2090 return;
2093 if (bpp > 8) return;
2095 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2097 incr = 3;
2098 colors = 1 << bpp;
2100 else
2102 incr = 4;
2103 colors = bmi->bmiHeader.biClrUsed;
2104 if (colors > 256) colors = 256;
2105 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2108 c_W = GetSysColor(COLOR_WINDOW);
2109 c_S = GetSysColor(COLOR_3DSHADOW);
2110 c_F = GetSysColor(COLOR_3DFACE);
2111 c_L = GetSysColor(COLOR_3DLIGHT);
2113 if (loadflags & LR_LOADTRANSPARENT) {
2114 switch (bpp) {
2115 case 1: pix = pix >> 7; break;
2116 case 4: pix = pix >> 4; break;
2117 case 8: break;
2118 default:
2119 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2120 return;
2122 if (pix >= colors) {
2123 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2124 return;
2126 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2127 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2128 ptr->rgbBlue = GetBValue(c_W);
2129 ptr->rgbGreen = GetGValue(c_W);
2130 ptr->rgbRed = GetRValue(c_W);
2132 if (loadflags & LR_LOADMAP3DCOLORS)
2133 for (i=0; i<colors; i++) {
2134 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2135 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2136 if (c_C == RGB(128, 128, 128)) {
2137 ptr->rgbRed = GetRValue(c_S);
2138 ptr->rgbGreen = GetGValue(c_S);
2139 ptr->rgbBlue = GetBValue(c_S);
2140 } else if (c_C == RGB(192, 192, 192)) {
2141 ptr->rgbRed = GetRValue(c_F);
2142 ptr->rgbGreen = GetGValue(c_F);
2143 ptr->rgbBlue = GetBValue(c_F);
2144 } else if (c_C == RGB(223, 223, 223)) {
2145 ptr->rgbRed = GetRValue(c_L);
2146 ptr->rgbGreen = GetGValue(c_L);
2147 ptr->rgbBlue = GetBValue(c_L);
2153 /**********************************************************************
2154 * BITMAP_Load
2156 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2157 INT desiredx, INT desiredy, UINT loadflags )
2159 HBITMAP hbitmap = 0, orig_bm;
2160 HRSRC hRsrc;
2161 HGLOBAL handle;
2162 char *ptr = NULL;
2163 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2164 int size;
2165 BYTE pix;
2166 char *bits;
2167 LONG width, height, new_width, new_height;
2168 WORD bpp_dummy;
2169 DWORD compr_dummy, offbits = 0;
2170 INT bm_type;
2171 HDC screen_mem_dc = NULL;
2173 if (!(loadflags & LR_LOADFROMFILE))
2175 if (!instance)
2177 /* OEM bitmap: try to load the resource from user32.dll */
2178 instance = user32_module;
2181 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2182 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2184 if ((info = LockResource( handle )) == NULL) return 0;
2186 else
2188 BITMAPFILEHEADER * bmfh;
2190 if (!(ptr = map_fileW( name, NULL ))) return 0;
2191 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2192 bmfh = (BITMAPFILEHEADER *)ptr;
2193 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2195 WARN("Invalid/unsupported bitmap format!\n");
2196 goto end;
2198 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2201 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2202 &bpp_dummy, &compr_dummy);
2203 if (bm_type == -1)
2205 WARN("Invalid bitmap format!\n");
2206 goto end;
2209 size = bitmap_info_size(info, DIB_RGB_COLORS);
2210 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2211 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2213 if (!fix_info || !scaled_info) goto end;
2214 memcpy(fix_info, info, size);
2216 pix = *((LPBYTE)info + size);
2217 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2219 memcpy(scaled_info, fix_info, size);
2221 if(desiredx != 0)
2222 new_width = desiredx;
2223 else
2224 new_width = width;
2226 if(desiredy != 0)
2227 new_height = height > 0 ? desiredy : -desiredy;
2228 else
2229 new_height = height;
2231 if(bm_type == 0)
2233 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2234 core->bcWidth = new_width;
2235 core->bcHeight = new_height;
2237 else
2239 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2240 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2241 WARN("Broken BitmapInfoHeader!\n");
2242 goto end;
2245 scaled_info->bmiHeader.biWidth = new_width;
2246 scaled_info->bmiHeader.biHeight = new_height;
2249 if (new_height < 0) new_height = -new_height;
2251 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2252 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2254 bits = (char *)info + (offbits ? offbits : size);
2256 if (loadflags & LR_CREATEDIBSECTION)
2258 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2259 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2261 else
2263 if (is_dib_monochrome(fix_info))
2264 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2265 else
2266 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2269 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2270 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2271 SelectObject(screen_mem_dc, orig_bm);
2273 end:
2274 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2275 HeapFree(GetProcessHeap(), 0, scaled_info);
2276 HeapFree(GetProcessHeap(), 0, fix_info);
2277 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2279 return hbitmap;
2282 /**********************************************************************
2283 * LoadImageA (USER32.@)
2285 * See LoadImageW.
2287 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2288 INT desiredx, INT desiredy, UINT loadflags)
2290 HANDLE res;
2291 LPWSTR u_name;
2293 if (IS_INTRESOURCE(name))
2294 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2296 __TRY {
2297 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2298 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2299 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2301 __EXCEPT_PAGE_FAULT {
2302 SetLastError( ERROR_INVALID_PARAMETER );
2303 return 0;
2305 __ENDTRY
2306 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2307 HeapFree(GetProcessHeap(), 0, u_name);
2308 return res;
2312 /******************************************************************************
2313 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2315 * PARAMS
2316 * hinst [I] Handle of instance that contains image
2317 * name [I] Name of image
2318 * type [I] Type of image
2319 * desiredx [I] Desired width
2320 * desiredy [I] Desired height
2321 * loadflags [I] Load flags
2323 * RETURNS
2324 * Success: Handle to newly loaded image
2325 * Failure: NULL
2327 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2329 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2330 INT desiredx, INT desiredy, UINT loadflags )
2332 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2333 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2335 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2336 switch (type) {
2337 case IMAGE_BITMAP:
2338 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2340 case IMAGE_ICON:
2341 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2342 if (screen_dc)
2344 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2345 GetDeviceCaps(screen_dc, BITSPIXEL),
2346 FALSE, loadflags);
2348 break;
2350 case IMAGE_CURSOR:
2351 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2352 1, TRUE, loadflags);
2354 return 0;
2357 /******************************************************************************
2358 * CopyImage (USER32.@) Creates new image and copies attributes to it
2360 * PARAMS
2361 * hnd [I] Handle to image to copy
2362 * type [I] Type of image to copy
2363 * desiredx [I] Desired width of new image
2364 * desiredy [I] Desired height of new image
2365 * flags [I] Copy flags
2367 * RETURNS
2368 * Success: Handle to newly created image
2369 * Failure: NULL
2371 * BUGS
2372 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2373 * all other versions (95/2000/XP have been tested) ignore it.
2375 * NOTES
2376 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2377 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2378 * the copy will have the same depth as the screen.
2379 * The content of the image will only be copied if the bit depth of the
2380 * original image is compatible with the bit depth of the screen, or
2381 * if the source is a DIB section.
2382 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2384 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2385 INT desiredy, UINT flags )
2387 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2388 hnd, type, desiredx, desiredy, flags);
2390 switch (type)
2392 case IMAGE_BITMAP:
2394 HBITMAP res = NULL;
2395 DIBSECTION ds;
2396 int objSize;
2397 BITMAPINFO * bi;
2399 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2400 if (!objSize) return 0;
2401 if ((desiredx < 0) || (desiredy < 0)) return 0;
2403 if (flags & LR_COPYFROMRESOURCE)
2405 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2408 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2409 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2411 /* Allocate memory for a BITMAPINFOHEADER structure and a
2412 color table. The maximum number of colors in a color table
2413 is 256 which corresponds to a bitmap with depth 8.
2414 Bitmaps with higher depths don't have color tables. */
2415 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2416 if (!bi) return 0;
2418 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2419 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2420 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2421 bi->bmiHeader.biCompression = BI_RGB;
2423 if (flags & LR_CREATEDIBSECTION)
2425 /* Create a DIB section. LR_MONOCHROME is ignored */
2426 void * bits;
2427 HDC dc = CreateCompatibleDC(NULL);
2429 if (objSize == sizeof(DIBSECTION))
2431 /* The source bitmap is a DIB.
2432 Get its attributes to create an exact copy */
2433 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2436 /* Get the color table or the color masks */
2437 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2439 bi->bmiHeader.biWidth = desiredx;
2440 bi->bmiHeader.biHeight = desiredy;
2441 bi->bmiHeader.biSizeImage = 0;
2443 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2444 DeleteDC(dc);
2446 else
2448 /* Create a device-dependent bitmap */
2450 BOOL monochrome = (flags & LR_MONOCHROME);
2452 if (objSize == sizeof(DIBSECTION))
2454 /* The source bitmap is a DIB section.
2455 Get its attributes */
2456 HDC dc = CreateCompatibleDC(NULL);
2457 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2458 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2459 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2460 DeleteDC(dc);
2462 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2464 /* Look if the colors of the DIB are black and white */
2466 monochrome =
2467 (bi->bmiColors[0].rgbRed == 0xff
2468 && bi->bmiColors[0].rgbGreen == 0xff
2469 && bi->bmiColors[0].rgbBlue == 0xff
2470 && bi->bmiColors[0].rgbReserved == 0
2471 && bi->bmiColors[1].rgbRed == 0
2472 && bi->bmiColors[1].rgbGreen == 0
2473 && bi->bmiColors[1].rgbBlue == 0
2474 && bi->bmiColors[1].rgbReserved == 0)
2476 (bi->bmiColors[0].rgbRed == 0
2477 && bi->bmiColors[0].rgbGreen == 0
2478 && bi->bmiColors[0].rgbBlue == 0
2479 && bi->bmiColors[0].rgbReserved == 0
2480 && bi->bmiColors[1].rgbRed == 0xff
2481 && bi->bmiColors[1].rgbGreen == 0xff
2482 && bi->bmiColors[1].rgbBlue == 0xff
2483 && bi->bmiColors[1].rgbReserved == 0);
2486 else if (!monochrome)
2488 monochrome = ds.dsBm.bmBitsPixel == 1;
2491 if (monochrome)
2493 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2495 else
2497 HDC screenDC = GetDC(NULL);
2498 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2499 ReleaseDC(NULL, screenDC);
2503 if (res)
2505 /* Only copy the bitmap if it's a DIB section or if it's
2506 compatible to the screen */
2507 BOOL copyContents;
2509 if (objSize == sizeof(DIBSECTION))
2511 copyContents = TRUE;
2513 else
2515 HDC screenDC = GetDC(NULL);
2516 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2517 ReleaseDC(NULL, screenDC);
2519 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2522 if (copyContents)
2524 /* The source bitmap may already be selected in a device context,
2525 use GetDIBits/StretchDIBits and not StretchBlt */
2527 HDC dc;
2528 void * bits;
2530 dc = CreateCompatibleDC(NULL);
2532 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2533 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2534 bi->bmiHeader.biSizeImage = 0;
2535 bi->bmiHeader.biClrUsed = 0;
2536 bi->bmiHeader.biClrImportant = 0;
2538 /* Fill in biSizeImage */
2539 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2540 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2542 if (bits)
2544 HBITMAP oldBmp;
2546 /* Get the image bits of the source bitmap */
2547 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2549 /* Copy it to the destination bitmap */
2550 oldBmp = SelectObject(dc, res);
2551 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2552 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2553 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2554 SelectObject(dc, oldBmp);
2556 HeapFree(GetProcessHeap(), 0, bits);
2559 DeleteDC(dc);
2562 if (flags & LR_COPYDELETEORG)
2564 DeleteObject(hnd);
2567 HeapFree(GetProcessHeap(), 0, bi);
2568 return res;
2570 case IMAGE_ICON:
2571 case IMAGE_CURSOR:
2573 struct cursoricon_object *icon;
2574 HICON res = 0;
2575 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2577 if (flags & LR_DEFAULTSIZE)
2579 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2580 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2583 if (!(icon = get_icon_ptr( hnd ))) return 0;
2585 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2586 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2587 type == IMAGE_CURSOR, flags );
2588 else
2589 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2590 release_icon_ptr( hnd, icon );
2592 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2593 return res;
2596 return 0;
2600 /******************************************************************************
2601 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2603 * RETURNS
2604 * Success: Handle to specified bitmap
2605 * Failure: NULL
2607 HBITMAP WINAPI LoadBitmapW(
2608 HINSTANCE instance, /* [in] Handle to application instance */
2609 LPCWSTR name) /* [in] Address of bitmap resource name */
2611 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2614 /**********************************************************************
2615 * LoadBitmapA (USER32.@)
2617 * See LoadBitmapW.
2619 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2621 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );