2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
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
27 #include <wine/debug.h>
29 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
30 WINE_DECLARE_DEBUG_CHANNEL(icon
);
31 WINE_DECLARE_DEBUG_CHANNEL(resource
);
44 } CURSORICONFILEDIRENTRY
;
51 CURSORICONFILEDIRENTRY idEntries
[1];
58 static const WCHAR DISPLAYW
[] = {'D','I','S','P','L','A','Y',0};
61 static CRITICAL_SECTION IconCrst
;
62 static CRITICAL_SECTION_DEBUG critsect_debug
=
65 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
66 0, 0, { (DWORD_PTR
)(__FILE__
": IconCrst") }
68 static CRITICAL_SECTION IconCrst
= { &critsect_debug
, -1, 0, 0, 0, 0 };
71 /***********************************************************************
74 * Helper function to map a file to memory:
76 * [RETURN] ptr - pointer to mapped file
77 * [RETURN] filesize - pointer size of file to be stored if not NULL
79 static void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
81 HANDLE hFile
, hMapping
;
84 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
85 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
86 if (hFile
!= INVALID_HANDLE_VALUE
)
88 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
91 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
92 CloseHandle( hMapping
);
94 *filesize
= GetFileSize( hFile
, NULL
);
102 /***********************************************************************
103 * get_dib_width_bytes
105 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
107 static int get_dib_width_bytes( int width
, int depth
)
113 case 1: words
= (width
+ 31) / 32; break;
114 case 4: words
= (width
+ 7) / 8; break;
115 case 8: words
= (width
+ 3) / 4; break;
117 case 16: words
= (width
+ 1) / 2; break;
118 case 24: words
= (width
* 3 + 3)/4; break;
120 WARN("(%d): Unsupported depth\n", depth
);
129 /***********************************************************************
132 * Return the size of the bitmap info structure including color table.
134 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
136 unsigned int colors
, size
, masks
= 0;
138 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
140 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
141 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
142 return sizeof(BITMAPCOREHEADER
) + colors
*
143 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
145 else /* assume BITMAPINFOHEADER */
147 colors
= info
->bmiHeader
.biClrUsed
;
148 if (colors
> 256) /* buffer overflow otherwise */
150 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
151 colors
= 1 << info
->bmiHeader
.biBitCount
;
152 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
153 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
154 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
159 /***********************************************************************
162 * Returns whether a DIB can be converted to a monochrome DDB.
164 * A DIB can be converted if its color table contains only black and
165 * white. Black must be the first color in the color table.
167 * Note : If the first color in the color table is white followed by
168 * black, we can't convert it to a monochrome DDB with
169 * SetDIBits, because black and white would be inverted.
171 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
173 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
175 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
177 const RGBTRIPLE
*rgb
= ((const BITMAPCOREINFO
*)info
)->bmciColors
;
179 /* Check if the first color is black */
180 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
184 /* Check if the second color is white */
185 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
186 && (rgb
->rgbtBlue
== 0xff));
190 else /* assume BITMAPINFOHEADER */
192 const RGBQUAD
*rgb
= info
->bmiColors
;
194 /* Check if the first color is black */
195 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
196 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
200 /* Check if the second color is white */
201 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
202 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
208 /***********************************************************************
211 * Get the info from a bitmap header.
212 * Return 1 for INFOHEADER, 0 for COREHEADER,
214 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
215 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
217 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
219 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
220 *width
= core
->bcWidth
;
221 *height
= core
->bcHeight
;
222 *bpp
= core
->bcBitCount
;
226 else if (header
->biSize
>= sizeof(BITMAPINFOHEADER
))
228 *width
= header
->biWidth
;
229 *height
= header
->biHeight
;
230 *bpp
= header
->biBitCount
;
231 *compr
= header
->biCompression
;
234 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
240 * The following macro functions account for the irregularities of
241 * accessing cursor and icon resources in files and resource entries.
243 typedef BOOL (*fnGetCIEntry
)( LPVOID dir
, int n
,
244 int *width
, int *height
, int *bits
);
246 /**********************************************************************
247 * CURSORICON_FindBestIcon
249 * Find the icon closest to the requested size and bit depth.
251 static int CURSORICON_FindBestIcon( LPVOID dir
, fnGetCIEntry get_entry
,
252 int width
, int height
, int depth
)
254 int i
, cx
, cy
, bits
, bestEntry
= -1;
255 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
256 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
259 iTotalDiff
= 0xFFFFFFFF;
260 iColorDiff
= 0xFFFFFFFF;
261 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
263 iTempXDiff
= abs(width
- cx
);
264 iTempYDiff
= abs(height
- cy
);
266 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
270 iTotalDiff
= iXDiff
+ iYDiff
;
274 /* Find Best Colors for Best Fit */
275 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
277 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
279 iTempColorDiff
= abs(depth
- bits
);
280 if(iColorDiff
> iTempColorDiff
)
283 iColorDiff
= iTempColorDiff
;
291 static BOOL
CURSORICON_GetResIconEntry( LPVOID dir
, int n
,
292 int *width
, int *height
, int *bits
)
294 CURSORICONDIR
*resdir
= dir
;
297 if ( resdir
->idCount
<= n
)
299 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
300 *width
= icon
->bWidth
;
301 *height
= icon
->bHeight
;
302 *bits
= resdir
->idEntries
[n
].wBitCount
;
306 /**********************************************************************
307 * CURSORICON_FindBestCursor
309 * Find the cursor closest to the requested size.
311 * FIXME: parameter 'color' ignored.
313 static int CURSORICON_FindBestCursor( LPVOID dir
, fnGetCIEntry get_entry
,
314 int width
, int height
, int depth
)
316 int i
, maxwidth
, maxheight
, cx
, cy
, bits
, bestEntry
= -1;
318 /* Double height to account for AND and XOR masks */
322 /* First find the largest one smaller than or equal to the requested size*/
324 maxwidth
= maxheight
= 0;
325 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
327 if ((cx
<= width
) && (cy
<= height
) &&
328 (cx
> maxwidth
) && (cy
> maxheight
))
335 if (bestEntry
!= -1) return bestEntry
;
337 /* Now find the smallest one larger than the requested size */
339 maxwidth
= maxheight
= 255;
340 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
342 if (((cx
< maxwidth
) && (cy
< maxheight
)) || (bestEntry
== -1))
353 static BOOL
CURSORICON_GetResCursorEntry( LPVOID dir
, int n
,
354 int *width
, int *height
, int *bits
)
356 CURSORICONDIR
*resdir
= dir
;
359 if ( resdir
->idCount
<= n
)
361 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
362 *width
= cursor
->wWidth
;
363 *height
= cursor
->wHeight
;
364 *bits
= resdir
->idEntries
[n
].wBitCount
;
368 static CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( CURSORICONDIR
* dir
,
369 int width
, int height
, int depth
)
373 n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetResIconEntry
,
374 width
, height
, depth
);
377 return &dir
->idEntries
[n
];
380 static CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( CURSORICONDIR
*dir
,
381 int width
, int height
, int depth
)
383 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetResCursorEntry
,
384 width
, height
, depth
);
387 return &dir
->idEntries
[n
];
390 static BOOL
CURSORICON_GetFileEntry( LPVOID dir
, int n
,
391 int *width
, int *height
, int *bits
)
393 CURSORICONFILEDIR
*filedir
= dir
;
394 CURSORICONFILEDIRENTRY
*entry
;
395 BITMAPINFOHEADER
*info
;
397 if ( filedir
->idCount
<= n
)
399 entry
= &filedir
->idEntries
[n
];
400 /* FIXME: check against file size */
401 info
= (BITMAPINFOHEADER
*)((char *)dir
+ entry
->dwDIBOffset
);
402 *width
= entry
->bWidth
;
403 *height
= entry
->bHeight
;
404 *bits
= info
->biBitCount
;
408 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( CURSORICONFILEDIR
*dir
,
409 int width
, int height
, int depth
)
411 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetFileEntry
,
412 width
, height
, depth
);
415 return &dir
->idEntries
[n
];
418 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( CURSORICONFILEDIR
*dir
,
419 int width
, int height
, int depth
)
421 int n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetFileEntry
,
422 width
, height
, depth
);
425 return &dir
->idEntries
[n
];
428 /***********************************************************************
429 * create_icon_bitmaps
431 * Create the color, mask and alpha bitmaps from the DIB info.
433 static BOOL
create_icon_bitmaps( const BITMAPINFO
*bmi
, int width
, int height
,
434 HBITMAP
*color
, HBITMAP
*mask
)
436 BOOL monochrome
= is_dib_monochrome( bmi
);
437 unsigned int size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
439 void *color_bits
, *mask_bits
;
443 if (!(info
= HeapAlloc( GetProcessHeap(), 0, max( size
, FIELD_OFFSET( BITMAPINFO
, bmiColors
[2] )))))
445 if (!(hdc
= CreateCompatibleDC( 0 ))) goto done
;
447 memcpy( info
, bmi
, size
);
448 info
->bmiHeader
.biHeight
/= 2;
450 color_bits
= (char *)bmi
+ size
;
451 mask_bits
= (char *)color_bits
+
452 get_dib_width_bytes( bmi
->bmiHeader
.biWidth
,
453 bmi
->bmiHeader
.biBitCount
) * abs(info
->bmiHeader
.biHeight
);
457 if (!(*mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
))) goto done
;
460 /* copy color data into second half of mask bitmap */
461 SelectObject( hdc
, *mask
);
462 StretchDIBits( hdc
, 0, height
, width
, height
,
463 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
464 color_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
468 if (!(*mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
469 if (!(*color
= CreateBitmap( width
, height
, GetDeviceCaps( screen_dc
, PLANES
),
470 GetDeviceCaps( screen_dc
, BITSPIXEL
), NULL
)))
472 DeleteObject( *mask
);
475 SelectObject( hdc
, *color
);
476 StretchDIBits( hdc
, 0, 0, width
, height
,
477 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
478 color_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
480 /* convert info to monochrome to copy the mask */
481 info
->bmiHeader
.biBitCount
= 1;
482 if (info
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
484 RGBQUAD
*rgb
= info
->bmiColors
;
486 info
->bmiHeader
.biClrUsed
= info
->bmiHeader
.biClrImportant
= 2;
487 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
488 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
489 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
493 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)info
) + 1);
495 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
496 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
500 SelectObject( hdc
, *mask
);
501 StretchDIBits( hdc
, 0, 0, width
, height
,
502 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
503 mask_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
508 HeapFree( GetProcessHeap(), 0, info
);
512 static HICON
CURSORICON_CreateIconFromBMI( BITMAPINFO
*bmi
,
513 POINT hotspot
, BOOL bIcon
,
515 INT width
, INT height
,
518 HBITMAP color
= 0, mask
= 0;
522 if (dwVersion
== 0x00020000)
524 FIXME_(cursor
)("\t2.xx resources are not supported\n");
528 /* Check bitmap header */
530 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
531 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
532 bmi
->bmiHeader
.biCompression
!= BI_RGB
) )
534 WARN_(cursor
)("\tinvalid resource bitmap header.\n");
538 if (!width
) width
= bmi
->bmiHeader
.biWidth
;
539 if (!height
) height
= bmi
->bmiHeader
.biHeight
/2;
540 do_stretch
= (bmi
->bmiHeader
.biHeight
/2 != height
) ||
541 (bmi
->bmiHeader
.biWidth
!= width
);
543 /* Scale the hotspot */
546 hotspot
.x
= width
/ 2;
547 hotspot
.y
= height
/ 2;
551 hotspot
.x
= (hotspot
.x
* width
) / bmi
->bmiHeader
.biWidth
;
552 hotspot
.y
= (hotspot
.y
* height
) / (bmi
->bmiHeader
.biHeight
/ 2);
555 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
556 if (!screen_dc
) return 0;
558 if (!create_icon_bitmaps( bmi
, width
, height
, &color
, &mask
)) return 0;
560 IconInfo
.xHotspot
= hotspot
.x
;
561 IconInfo
.yHotspot
= hotspot
.y
;
562 IconInfo
.fIcon
= bIcon
;
563 IconInfo
.hbmColor
= color
;
564 IconInfo
.hbmMask
= mask
;
566 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
570 /**********************************************************************
571 * .ANI cursor support
573 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
574 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
575 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
577 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
578 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
579 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
580 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
581 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
582 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
584 #define ANI_FLAG_ICON 0x1
585 #define ANI_FLAG_SEQUENCE 0x2
601 const unsigned char *data
;
604 static void dump_ani_header( const ani_header
*header
)
606 TRACE(" header size: %d\n", header
->header_size
);
607 TRACE(" frames: %d\n", header
->num_frames
);
608 TRACE(" steps: %d\n", header
->num_steps
);
609 TRACE(" width: %d\n", header
->width
);
610 TRACE(" height: %d\n", header
->height
);
611 TRACE(" bpp: %d\n", header
->bpp
);
612 TRACE(" planes: %d\n", header
->num_planes
);
613 TRACE(" display rate: %d\n", header
->display_rate
);
614 TRACE(" flags: 0x%08x\n", header
->flags
);
636 static void riff_find_chunk( DWORD chunk_id
, DWORD chunk_type
, const riff_chunk_t
*parent_chunk
, riff_chunk_t
*chunk
)
638 const unsigned char *ptr
= parent_chunk
->data
;
639 const unsigned char *end
= parent_chunk
->data
+ (parent_chunk
->data_size
- (2 * sizeof(DWORD
)));
641 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) end
-= sizeof(DWORD
);
645 if ((!chunk_type
&& *(const DWORD
*)ptr
== chunk_id
)
646 || (chunk_type
&& *(const DWORD
*)ptr
== chunk_type
&& *((const DWORD
*)ptr
+ 2) == chunk_id
))
648 ptr
+= sizeof(DWORD
);
649 chunk
->data_size
= (*(const DWORD
*)ptr
+ 1) & ~1;
650 ptr
+= sizeof(DWORD
);
651 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) ptr
+= sizeof(DWORD
);
657 ptr
+= sizeof(DWORD
);
658 ptr
+= (*(const DWORD
*)ptr
+ 1) & ~1;
659 ptr
+= sizeof(DWORD
);
667 * RIFF:'ACON' RIFF chunk
668 * |- CHUNK:'anih' Header
669 * |- CHUNK:'seq ' Sequence information (optional)
670 * \- LIST:'fram' Frame list
671 * |- CHUNK:icon Cursor frames
676 static HCURSOR
CURSORICON_CreateIconFromANI( const LPBYTE bits
, DWORD bits_size
,
677 INT width
, INT height
, INT depth
)
680 ani_header header
= {0};
681 LPBYTE frame_bits
= 0;
683 CURSORICONFILEDIRENTRY
*entry
;
685 riff_chunk_t root_chunk
= { bits_size
, bits
};
686 riff_chunk_t ACON_chunk
= {0};
687 riff_chunk_t anih_chunk
= {0};
688 riff_chunk_t fram_chunk
= {0};
689 const unsigned char *icon_data
;
691 TRACE("bits %p, bits_size %d\n", bits
, bits_size
);
695 riff_find_chunk( ANI_ACON_ID
, ANI_RIFF_ID
, &root_chunk
, &ACON_chunk
);
696 if (!ACON_chunk
.data
)
698 ERR("Failed to get root chunk.\n");
702 riff_find_chunk( ANI_anih_ID
, 0, &ACON_chunk
, &anih_chunk
);
703 if (!anih_chunk
.data
)
705 ERR("Failed to get 'anih' chunk.\n");
708 memcpy( &header
, anih_chunk
.data
, sizeof(header
) );
709 dump_ani_header( &header
);
711 riff_find_chunk( ANI_fram_ID
, ANI_LIST_ID
, &ACON_chunk
, &fram_chunk
);
712 if (!fram_chunk
.data
)
714 ERR("Failed to get icon list.\n");
718 /* FIXME: For now, just load the first frame. Before we can load all the
719 * frames, we need to write the needed code in wineserver, etc. to handle
720 * cursors. Once this code is written, we can extend it to support .ani
721 * cursors and then update user32 and winex11.drv to load all frames.
723 * Hopefully this will at least make some games (C&C3, etc.) more playable
726 FIXME("Loading all frames for .ani cursors not implemented.\n");
727 icon_data
= fram_chunk
.data
+ (2 * sizeof(DWORD
));
729 entry
= CURSORICON_FindBestIconFile( (CURSORICONFILEDIR
*) icon_data
,
730 width
, height
, depth
);
732 frame_bits
= HeapAlloc( GetProcessHeap(), 0, entry
->dwDIBSize
);
733 memcpy( frame_bits
, icon_data
+ entry
->dwDIBOffset
, entry
->dwDIBSize
);
735 if (!header
.width
|| !header
.height
)
737 header
.width
= entry
->bWidth
;
738 header
.height
= entry
->bHeight
;
741 hotspot
.x
= entry
->xHotspot
;
742 hotspot
.y
= entry
->yHotspot
;
744 cursor
= CURSORICON_CreateIconFromBMI( (BITMAPINFO
*) frame_bits
, hotspot
,
745 FALSE
, 0x00030000, header
.width
, header
.height
, 0 );
747 HeapFree( GetProcessHeap(), 0, frame_bits
);
753 /**********************************************************************
754 * CreateIconFromResourceEx (USER32.@)
756 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
757 * with cbSize parameter as well.
759 HICON WINAPI
CreateIconFromResourceEx( PBYTE bits
, DWORD cbSize
,
760 BOOL bIcon
, DWORD dwVersion
,
761 int width
, int height
,
767 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
768 bits
, cbSize
, dwVersion
, width
, height
,
769 bIcon
? "icon" : "cursor", (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
773 hotspot
.x
= width
/ 2;
774 hotspot
.y
= height
/ 2;
775 bmi
= (BITMAPINFO
*)bits
;
777 else /* get the hotspot */
779 SHORT
*pt
= (SHORT
*)bits
;
782 bmi
= (BITMAPINFO
*)(pt
+ 2);
785 return CURSORICON_CreateIconFromBMI( bmi
, hotspot
, bIcon
, dwVersion
,
786 width
, height
, cFlag
);
790 /**********************************************************************
791 * CreateIconFromResource (USER32.@)
793 HICON WINAPI
CreateIconFromResource( PBYTE bits
, DWORD cbSize
,
794 BOOL bIcon
, DWORD dwVersion
)
796 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0,0,0);
800 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
801 INT width
, INT height
, INT depth
,
802 BOOL fCursor
, UINT loadflags
)
804 CURSORICONFILEDIRENTRY
*entry
;
805 CURSORICONFILEDIR
*dir
;
811 TRACE("loading %s\n", debugstr_w( filename
));
813 bits
= map_fileW( filename
, &filesize
);
817 /* Check for .ani. */
818 if (memcmp( bits
, "RIFF", 4 ) == 0)
820 hIcon
= CURSORICON_CreateIconFromANI( bits
, filesize
, width
, height
,
825 dir
= (CURSORICONFILEDIR
*) bits
;
826 if ( filesize
< sizeof(*dir
) )
829 if ( filesize
< (sizeof(*dir
) + sizeof(dir
->idEntries
[0])*(dir
->idCount
-1)) )
833 entry
= CURSORICON_FindBestCursorFile( dir
, width
, height
, depth
);
835 entry
= CURSORICON_FindBestIconFile( dir
, width
, height
, depth
);
840 /* check that we don't run off the end of the file */
841 if ( entry
->dwDIBOffset
> filesize
)
843 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
846 hotspot
.x
= entry
->xHotspot
;
847 hotspot
.y
= entry
->yHotspot
;
848 hIcon
= CURSORICON_CreateIconFromBMI( (BITMAPINFO
*)&bits
[entry
->dwDIBOffset
],
849 hotspot
, !fCursor
, 0x00030000,
850 width
, height
, loadflags
);
852 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
853 UnmapViewOfFile( bits
);
857 /**********************************************************************
860 * Load a cursor or icon from resource or file.
862 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
863 INT width
, INT height
, INT depth
,
864 BOOL fCursor
, UINT loadflags
)
868 HRSRC hRsrc
, hGroupRsrc
;
870 CURSORICONDIRENTRY
*dirEntry
;
875 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
876 hInstance
, debugstr_w(name
), width
, height
, depth
, fCursor
, loadflags
);
878 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
879 return CURSORICON_LoadFromFile( name
, width
, height
, depth
, fCursor
, loadflags
);
881 if (!hInstance
) hInstance
= User32Instance
; /* Load OEM cursor/icon */
883 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
884 if ((ULONG_PTR
)hInstance
>> 16 == 0) loadflags
&= ~LR_SHARED
;
886 /* Get directory resource ID */
888 if (!(hRsrc
= FindResourceW( hInstance
, name
,
889 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
893 /* Find the best entry in the directory */
895 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
896 if (!(dir
= LockResource( handle
))) return 0;
898 dirEntry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, depth
);
900 dirEntry
= CURSORICON_FindBestIconRes( dir
, width
, height
, depth
);
901 if (!dirEntry
) return 0;
902 wResId
= dirEntry
->wResId
;
903 dwBytesInRes
= dirEntry
->dwBytesInRes
;
904 FreeResource( handle
);
906 /* Load the resource */
908 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
909 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
911 /* If shared icon, check whether it was already loaded */
912 if ( (loadflags
& LR_SHARED
)
913 && (hIcon
= NtUserFindExistingCursorIcon( hInstance
, hRsrc
, 0, 0 ) ) != 0 )
916 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
917 bits
= LockResource( handle
);
918 hIcon
= CreateIconFromResourceEx( bits
, dwBytesInRes
,
919 !fCursor
, 0x00030000, width
, height
, loadflags
);
920 FreeResource( handle
);
922 /* If shared icon, add to icon cache */
924 if (hIcon
&& 0 != (loadflags
& LR_SHARED
))
927 NtUserSetCursorIconData((HICON
)hIcon
, NULL
, NULL
, hInstance
, hRsrc
,
932 if(NtUserGetIconInfo(ResIcon
, &iconInfo
, NULL
, NULL
, NULL
, FALSE
))
933 NtUserSetCursorIconData((HICON
)hIcon
, hinst
, NULL
, &iconInfo
);
941 /*************************************************************************
944 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
947 * Handle [I] handle to an Image
948 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
949 * iDesiredCX [I] The Desired width of the Image
950 * iDesiredCY [I] The desired height of the Image
951 * nFlags [I] The flags from CopyImage
954 * Success: The new handle of the Image
957 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
958 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
959 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
964 static HICON
CURSORICON_ExtCopy(HICON hIcon
, UINT nType
,
965 INT iDesiredCX
, INT iDesiredCY
,
970 TRACE_(icon
)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
971 hIcon
, nType
, iDesiredCX
, iDesiredCY
, nFlags
);
978 /* Best Fit or Monochrome */
979 if( (nFlags
& LR_COPYFROMRESOURCE
980 && (iDesiredCX
> 0 || iDesiredCY
> 0))
981 || nFlags
& LR_MONOCHROME
)
983 FIXME("Copying from resource isn't implemented yet\n");
984 hNew
= CopyIcon(hIcon
);
987 ICONCACHE
* pIconCache
= CURSORICON_FindCache(hIcon
);
989 /* Not Found in Cache, then do a straight copy
991 if(pIconCache
== NULL
)
993 hNew
= CopyIcon( hIcon
);
994 if(nFlags
& LR_COPYFROMRESOURCE
)
996 TRACE_(icon
)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1001 int iTargetCY
= iDesiredCY
, iTargetCX
= iDesiredCX
;
1007 CURSORICONDIR
*pDir
;
1008 CURSORICONDIRENTRY
*pDirEntry
;
1009 BOOL bIsIcon
= (nType
== IMAGE_ICON
);
1011 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1013 if(((nFlags
& LR_MONOCHROME
) && !(nFlags
& LR_COPYFROMRESOURCE
))
1014 || (iDesiredCX
== 0 && iDesiredCY
== 0))
1016 iDesiredCY
= GetSystemMetrics(bIsIcon
?
1017 SM_CYICON
: SM_CYCURSOR
);
1018 iDesiredCX
= GetSystemMetrics(bIsIcon
?
1019 SM_CXICON
: SM_CXCURSOR
);
1022 /* Retrieve the CURSORICONDIRENTRY
1024 if (!(hMem
= LoadResource( pIconCache
->hModule
,
1025 pIconCache
->hGroupRsrc
)))
1029 if (!(pDir
= LockResource( hMem
)))
1038 pDirEntry
= CURSORICON_FindBestIconRes(
1039 pDir
, iDesiredCX
, iDesiredCY
, 256 );
1043 pDirEntry
= CURSORICON_FindBestCursorRes(
1044 pDir
, iDesiredCX
, iDesiredCY
, 1);
1047 wResId
= pDirEntry
->wResId
;
1048 dwBytesInRes
= pDirEntry
->dwBytesInRes
;
1051 TRACE_(icon
)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1052 wResId
, dwBytesInRes
, pDirEntry
->ResInfo
.icon
.bWidth
,
1053 pDirEntry
->ResInfo
.icon
.bHeight
, iDesiredCX
, iDesiredCY
);
1057 if (!(hRsrc
= FindResourceW(pIconCache
->hModule
,
1058 MAKEINTRESOURCEW(wResId
), (LPWSTR
)(bIsIcon
? RT_ICON
: RT_CURSOR
))))
1062 if (!(hMem
= LoadResource( pIconCache
->hModule
, hRsrc
)))
1067 pBits
= LockResource( hMem
);
1069 if(nFlags
& LR_DEFAULTSIZE
)
1071 iTargetCY
= GetSystemMetrics(SM_CYICON
);
1072 iTargetCX
= GetSystemMetrics(SM_CXICON
);
1075 /* Create a New Icon with the proper dimension
1077 hNew
= CreateIconFromResourceEx( pBits
, dwBytesInRes
,
1078 bIsIcon
, 0x00030000, iTargetCX
, iTargetCY
, nFlags
);
1083 else hNew
= CopyIcon( hIcon
);
1088 /***********************************************************************
1089 * CreateCursor (USER32.@)
1091 HCURSOR WINAPI
CreateCursor( HINSTANCE hInstance
,
1092 INT xHotSpot
, INT yHotSpot
,
1093 INT nWidth
, INT nHeight
,
1094 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1099 TRACE_(cursor
)("%dx%d spot=%d,%d xor=%p and=%p\n",
1100 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
1103 info
.xHotspot
= xHotSpot
;
1104 info
.yHotspot
= yHotSpot
;
1105 info
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1106 info
.hbmColor
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpXORbits
);
1107 hCursor
= CreateIconIndirect( &info
);
1108 DeleteObject( info
.hbmMask
);
1109 DeleteObject( info
.hbmColor
);
1114 /***********************************************************************
1115 * CreateIcon (USER32.@)
1117 * Creates an icon based on the specified bitmaps. The bitmaps must be
1118 * provided in a device dependent format and will be resized to
1119 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1120 * depth. The provided bitmaps must be top-down bitmaps.
1121 * Although Windows does not support 15bpp(*) this API must support it
1122 * for Winelib applications.
1124 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1128 * Success: handle to an icon
1131 * FIXME: Do we need to resize the bitmaps?
1133 HICON WINAPI
CreateIcon(
1134 HINSTANCE hInstance
, /* [in] the application's hInstance */
1135 int nWidth
, /* [in] the width of the provided bitmaps */
1136 int nHeight
, /* [in] the height of the provided bitmaps */
1137 BYTE bPlanes
, /* [in] the number of planes in the provided bitmaps */
1138 BYTE bBitsPixel
, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1139 const BYTE
* lpANDbits
, /* [in] a monochrome bitmap representing the icon's mask */
1140 const BYTE
* lpXORbits
) /* [in] the icon's 'color' bitmap */
1145 TRACE_(icon
)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1146 nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
, lpANDbits
);
1149 iinfo
.xHotspot
= nWidth
/ 2;
1150 iinfo
.yHotspot
= nHeight
/ 2;
1151 iinfo
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1152 iinfo
.hbmColor
= CreateBitmap( nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
);
1154 hIcon
= CreateIconIndirect( &iinfo
);
1156 DeleteObject( iinfo
.hbmMask
);
1157 DeleteObject( iinfo
.hbmColor
);
1163 /***********************************************************************
1164 * CopyIcon (USER32.@)
1166 HICON WINAPI
CopyIcon( HICON hIcon
)
1168 HICON hRetIcon
= NULL
;
1171 if(GetIconInfo(hIcon
, &IconInfo
))
1173 hRetIcon
= CreateIconIndirect(&IconInfo
);
1174 DeleteObject(IconInfo
.hbmColor
);
1175 DeleteObject(IconInfo
.hbmMask
);
1182 /***********************************************************************
1183 * DestroyIcon (USER32.@)
1185 BOOL WINAPI
DestroyIcon( HICON hIcon
)
1187 TRACE_(icon
)("%p\n", hIcon
);
1189 return NtUserDestroyCursor(hIcon
, 0);
1193 /***********************************************************************
1194 * DestroyCursor (USER32.@)
1196 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
1198 if (GetCursor() == hCursor
)
1200 WARN_(cursor
)("Destroying active cursor!\n" );
1203 return DestroyIcon( hCursor
);
1206 /***********************************************************************
1207 * DrawIcon (USER32.@)
1209 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
1211 return DrawIconEx( hdc
, x
, y
, hIcon
, 0, 0, 0, 0, DI_NORMAL
| DI_COMPAT
| DI_DEFAULTSIZE
);
1214 /***********************************************************************
1215 * SetCursor (USER32.@)
1217 * Set the cursor shape.
1220 * A handle to the previous cursor shape.
1222 HCURSOR WINAPI
/*DECLSPEC_HOTPATCH*/ SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
1224 return NtUserSetCursor(hCursor
);
1227 /***********************************************************************
1228 * ShowCursor (USER32.@)
1230 INT WINAPI
/*DECLSPEC_HOTPATCH*/ ShowCursor( BOOL bShow
)
1232 return NtUserShowCursor(bShow
);
1235 /***********************************************************************
1236 * GetCursor (USER32.@)
1238 HCURSOR WINAPI
GetCursor(void)
1241 ci
.cbSize
= sizeof(CURSORINFO
);
1242 if(NtUserGetCursorInfo(&ci
))
1249 /***********************************************************************
1250 * ClipCursor (USER32.@)
1252 BOOL WINAPI
/*DECLSPEC_HOTPATCH*/ ClipCursor( const RECT
*rect
)
1254 return NtUserClipCursor((RECT
*)rect
);
1258 /***********************************************************************
1259 * GetClipCursor (USER32.@)
1261 BOOL WINAPI
/*DECLSPEC_HOTPATCH*/ GetClipCursor( RECT
*rect
)
1263 return NtUserGetClipCursor(rect
);
1267 /***********************************************************************
1268 * SetSystemCursor (USER32.@)
1270 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
1272 FIXME("(%p,%08x),stub!\n", hcur
, id
);
1277 /**********************************************************************
1278 * LookupIconIdFromDirectoryEx (USER32.@)
1280 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
1281 INT width
, INT height
, UINT cFlag
)
1283 CURSORICONDIR
*dir
= (CURSORICONDIR
*)xdir
;
1285 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
1287 CURSORICONDIRENTRY
* entry
;
1289 const HDC hdc
= GetDC(0);
1290 const int depth
= (cFlag
& LR_MONOCHROME
) ?
1291 1 : GetDeviceCaps(hdc
, BITSPIXEL
);
1295 entry
= CURSORICON_FindBestIconRes( dir
, width
, height
, depth
);
1297 entry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, depth
);
1299 if( entry
) retVal
= entry
->wResId
;
1301 else WARN_(cursor
)("invalid resource directory\n");
1305 /**********************************************************************
1306 * LookupIconIdFromDirectory (USER32.@)
1308 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
1310 return LookupIconIdFromDirectoryEx( dir
, bIcon
,
1311 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
1312 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
1315 /***********************************************************************
1316 * LoadCursorW (USER32.@)
1318 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
1320 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1322 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1323 LR_SHARED
| LR_DEFAULTSIZE
);
1326 /***********************************************************************
1327 * LoadCursorA (USER32.@)
1329 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
1331 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1333 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1334 LR_SHARED
| LR_DEFAULTSIZE
);
1337 /***********************************************************************
1338 * LoadCursorFromFileW (USER32.@)
1340 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
1342 TRACE("%s\n", debugstr_w(name
));
1344 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
1345 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1348 /***********************************************************************
1349 * LoadCursorFromFileA (USER32.@)
1351 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
1353 TRACE("%s\n", debugstr_a(name
));
1355 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
1356 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1359 /***********************************************************************
1360 * LoadIconW (USER32.@)
1362 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
1364 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1366 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
1367 LR_SHARED
| LR_DEFAULTSIZE
);
1370 /***********************************************************************
1371 * LoadIconA (USER32.@)
1373 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
1375 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1377 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
1378 LR_SHARED
| LR_DEFAULTSIZE
);
1381 /**********************************************************************
1382 * GetIconInfo (USER32.@)
1384 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
1386 return NtUserGetIconInfo(hIcon
, iconinfo
, 0, 0, 0, 0);
1389 /**********************************************************************
1390 * CreateIconIndirect (USER32.@)
1392 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
1399 SetLastError(ERROR_INVALID_PARAMETER
);
1403 if(!GetObjectW(iconinfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
1408 /* Try to get color bitmap */
1409 if (GetObjectW(iconinfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
1411 /* Compare size of color and mask bitmap*/
1412 if (ColorBitmap
.bmWidth
!= MaskBitmap
.bmWidth
||
1413 ColorBitmap
.bmHeight
!= MaskBitmap
.bmHeight
)
1415 ERR("Color and mask size are different!");
1416 SetLastError(ERROR_INVALID_PARAMETER
);
1420 return (HICON
)NtUserCreateCursorIconHandle(iconinfo
, TRUE
);
1423 /******************************************************************************
1424 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1427 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1430 * hdc [I] Handle to device context
1431 * x0 [I] X coordinate of upper left corner
1432 * y0 [I] Y coordinate of upper left corner
1433 * hIcon [I] Handle to icon to draw
1434 * cxWidth [I] Width of icon
1435 * cyWidth [I] Height of icon
1436 * istep [I] Index of frame in animated cursor
1437 * hbr [I] Handle to background brush
1438 * flags [I] Icon-drawing flags
1444 BOOL WINAPI
DrawIconEx( HDC hdc
, INT xLeft
, INT yTop
, HICON hIcon
,
1445 INT cxWidth
, INT cyWidth
, UINT istepIfAniCur
,
1446 HBRUSH hbrFlickerFreeDraw
, UINT diFlags
)
1448 return NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
1449 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
1453 /***********************************************************************
1454 * DIB_FixColorsToLoadflags
1456 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1459 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
1462 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
1471 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
1473 WARN_(resource
)("Invalid bitmap\n");
1477 if (bpp
> 8) return;
1479 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
1487 colors
= bmi
->bmiHeader
.biClrUsed
;
1488 if (colors
> 256) colors
= 256;
1489 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
1492 c_W
= GetSysColor(COLOR_WINDOW
);
1493 c_S
= GetSysColor(COLOR_3DSHADOW
);
1494 c_F
= GetSysColor(COLOR_3DFACE
);
1495 c_L
= GetSysColor(COLOR_3DLIGHT
);
1497 if (loadflags
& LR_LOADTRANSPARENT
) {
1499 case 1: pix
= pix
>> 7; break;
1500 case 4: pix
= pix
>> 4; break;
1503 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
1506 if (pix
>= colors
) {
1507 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
1510 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
1511 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
1512 ptr
->rgbBlue
= GetBValue(c_W
);
1513 ptr
->rgbGreen
= GetGValue(c_W
);
1514 ptr
->rgbRed
= GetRValue(c_W
);
1516 if (loadflags
& LR_LOADMAP3DCOLORS
)
1517 for (i
=0; i
<colors
; i
++) {
1518 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
1519 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
1520 if (c_C
== RGB(128, 128, 128)) {
1521 ptr
->rgbRed
= GetRValue(c_S
);
1522 ptr
->rgbGreen
= GetGValue(c_S
);
1523 ptr
->rgbBlue
= GetBValue(c_S
);
1524 } else if (c_C
== RGB(192, 192, 192)) {
1525 ptr
->rgbRed
= GetRValue(c_F
);
1526 ptr
->rgbGreen
= GetGValue(c_F
);
1527 ptr
->rgbBlue
= GetBValue(c_F
);
1528 } else if (c_C
== RGB(223, 223, 223)) {
1529 ptr
->rgbRed
= GetRValue(c_L
);
1530 ptr
->rgbGreen
= GetGValue(c_L
);
1531 ptr
->rgbBlue
= GetBValue(c_L
);
1537 /**********************************************************************
1540 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
1541 INT desiredx
, INT desiredy
, UINT loadflags
)
1543 HBITMAP hbitmap
= 0, orig_bm
;
1547 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
1551 LONG width
, height
, new_width
, new_height
;
1553 DWORD compr_dummy
, offbits
= 0;
1555 HDC screen_mem_dc
= NULL
;
1557 if (!(loadflags
& LR_LOADFROMFILE
))
1561 /* OEM bitmap: try to load the resource from user32.dll */
1562 instance
= User32Instance
;
1565 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
1566 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
1568 if ((info
= LockResource( handle
)) == NULL
) return 0;
1572 BITMAPFILEHEADER
* bmfh
;
1574 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
1575 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
1576 bmfh
= (BITMAPFILEHEADER
*)ptr
;
1577 if (bmfh
->bfType
!= 0x4d42 /* 'BM' */)
1579 WARN("Invalid/unsupported bitmap format!\n");
1582 if (bmfh
->bfOffBits
) offbits
= bmfh
->bfOffBits
- sizeof(BITMAPFILEHEADER
);
1585 size
= bitmap_info_size(info
, DIB_RGB_COLORS
);
1586 fix_info
= HeapAlloc(GetProcessHeap(), 0, size
);
1587 scaled_info
= HeapAlloc(GetProcessHeap(), 0, size
);
1589 if (!fix_info
|| !scaled_info
) goto end
;
1590 memcpy(fix_info
, info
, size
);
1592 pix
= *((LPBYTE
)info
+ size
);
1593 DIB_FixColorsToLoadflags(fix_info
, loadflags
, pix
);
1595 memcpy(scaled_info
, fix_info
, size
);
1596 bm_type
= DIB_GetBitmapInfo( &fix_info
->bmiHeader
, &width
, &height
,
1597 &bpp_dummy
, &compr_dummy
);
1599 new_width
= desiredx
;
1604 new_height
= height
> 0 ? desiredy
: -desiredy
;
1606 new_height
= height
;
1610 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
1611 core
->bcWidth
= new_width
;
1612 core
->bcHeight
= new_height
;
1616 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
1617 if (info
->bmiHeader
.biHeight
> 65535 || info
->bmiHeader
.biWidth
> 65535) {
1618 WARN("Broken BitmapInfoHeader!\n");
1622 scaled_info
->bmiHeader
.biWidth
= new_width
;
1623 scaled_info
->bmiHeader
.biHeight
= new_height
;
1626 if (new_height
< 0) new_height
= -new_height
;
1628 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
1629 if (!(screen_mem_dc
= CreateCompatibleDC( screen_dc
))) goto end
;
1631 bits
= (char *)info
+ (offbits
? offbits
: size
);
1633 if (loadflags
& LR_CREATEDIBSECTION
)
1635 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
1636 hbitmap
= CreateDIBSection(screen_dc
, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
1640 if (is_dib_monochrome(fix_info
))
1641 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
1643 hbitmap
= CreateCompatibleBitmap(screen_dc
, new_width
, new_height
);
1646 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
1647 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
1648 SelectObject(screen_mem_dc
, orig_bm
);
1651 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
1652 HeapFree(GetProcessHeap(), 0, scaled_info
);
1653 HeapFree(GetProcessHeap(), 0, fix_info
);
1655 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
1660 /**********************************************************************
1661 * LoadImageA (USER32.@)
1665 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
1666 INT desiredx
, INT desiredy
, UINT loadflags
)
1671 if (IS_INTRESOURCE(name
))
1672 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
1675 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
1676 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1677 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
1679 __EXCEPT_PAGE_FAULT
{
1680 SetLastError( ERROR_INVALID_PARAMETER
);
1684 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
1685 HeapFree(GetProcessHeap(), 0, u_name
);
1690 /******************************************************************************
1691 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
1694 * hinst [I] Handle of instance that contains image
1695 * name [I] Name of image
1696 * type [I] Type of image
1697 * desiredx [I] Desired width
1698 * desiredy [I] Desired height
1699 * loadflags [I] Load flags
1702 * Success: Handle to newly loaded image
1705 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
1707 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
1708 INT desiredx
, INT desiredy
, UINT loadflags
)
1710 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
1711 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
1713 if (loadflags
& LR_DEFAULTSIZE
) {
1714 if (type
== IMAGE_ICON
) {
1715 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXICON
);
1716 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYICON
);
1717 } else if (type
== IMAGE_CURSOR
) {
1718 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXCURSOR
);
1719 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYCURSOR
);
1722 if (loadflags
& LR_LOADFROMFILE
) loadflags
&= ~LR_SHARED
;
1725 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
1728 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
1731 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
1732 GetDeviceCaps(screen_dc
, BITSPIXEL
),
1738 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
1739 1, TRUE
, loadflags
);
1744 /******************************************************************************
1745 * CopyImage (USER32.@) Creates new image and copies attributes to it
1748 * hnd [I] Handle to image to copy
1749 * type [I] Type of image to copy
1750 * desiredx [I] Desired width of new image
1751 * desiredy [I] Desired height of new image
1752 * flags [I] Copy flags
1755 * Success: Handle to newly created image
1759 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
1760 * all other versions (95/2000/XP have been tested) ignore it.
1763 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
1764 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
1765 * the copy will have the same depth as the screen.
1766 * The content of the image will only be copied if the bit depth of the
1767 * original image is compatible with the bit depth of the screen, or
1768 * if the source is a DIB section.
1769 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
1771 HANDLE WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
1772 INT desiredy
, UINT flags
)
1774 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
1775 hnd
, type
, desiredx
, desiredy
, flags
);
1786 objSize
= GetObjectW( hnd
, sizeof(ds
), &ds
);
1787 if (!objSize
) return 0;
1788 if ((desiredx
< 0) || (desiredy
< 0)) return 0;
1790 if (flags
& LR_COPYFROMRESOURCE
)
1792 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
1795 if (desiredx
== 0) desiredx
= ds
.dsBm
.bmWidth
;
1796 if (desiredy
== 0) desiredy
= ds
.dsBm
.bmHeight
;
1798 /* Allocate memory for a BITMAPINFOHEADER structure and a
1799 color table. The maximum number of colors in a color table
1800 is 256 which corresponds to a bitmap with depth 8.
1801 Bitmaps with higher depths don't have color tables. */
1802 bi
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
1805 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
1806 bi
->bmiHeader
.biPlanes
= ds
.dsBm
.bmPlanes
;
1807 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
1808 bi
->bmiHeader
.biCompression
= BI_RGB
;
1810 if (flags
& LR_CREATEDIBSECTION
)
1812 /* Create a DIB section. LR_MONOCHROME is ignored */
1814 HDC dc
= CreateCompatibleDC(NULL
);
1816 if (objSize
== sizeof(DIBSECTION
))
1818 /* The source bitmap is a DIB.
1819 Get its attributes to create an exact copy */
1820 memcpy(bi
, &ds
.dsBmih
, sizeof(BITMAPINFOHEADER
));
1823 /* Get the color table or the color masks */
1824 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
1826 bi
->bmiHeader
.biWidth
= desiredx
;
1827 bi
->bmiHeader
.biHeight
= desiredy
;
1828 bi
->bmiHeader
.biSizeImage
= 0;
1830 res
= CreateDIBSection(dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
1835 /* Create a device-dependent bitmap */
1837 BOOL monochrome
= (flags
& LR_MONOCHROME
);
1839 if (objSize
== sizeof(DIBSECTION
))
1841 /* The source bitmap is a DIB section.
1842 Get its attributes */
1843 HDC dc
= CreateCompatibleDC(NULL
);
1844 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
1845 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
1846 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
1849 if (!monochrome
&& ds
.dsBm
.bmBitsPixel
== 1)
1851 /* Look if the colors of the DIB are black and white */
1854 (bi
->bmiColors
[0].rgbRed
== 0xff
1855 && bi
->bmiColors
[0].rgbGreen
== 0xff
1856 && bi
->bmiColors
[0].rgbBlue
== 0xff
1857 && bi
->bmiColors
[0].rgbReserved
== 0
1858 && bi
->bmiColors
[1].rgbRed
== 0
1859 && bi
->bmiColors
[1].rgbGreen
== 0
1860 && bi
->bmiColors
[1].rgbBlue
== 0
1861 && bi
->bmiColors
[1].rgbReserved
== 0)
1863 (bi
->bmiColors
[0].rgbRed
== 0
1864 && bi
->bmiColors
[0].rgbGreen
== 0
1865 && bi
->bmiColors
[0].rgbBlue
== 0
1866 && bi
->bmiColors
[0].rgbReserved
== 0
1867 && bi
->bmiColors
[1].rgbRed
== 0xff
1868 && bi
->bmiColors
[1].rgbGreen
== 0xff
1869 && bi
->bmiColors
[1].rgbBlue
== 0xff
1870 && bi
->bmiColors
[1].rgbReserved
== 0);
1873 else if (!monochrome
)
1875 monochrome
= ds
.dsBm
.bmBitsPixel
== 1;
1880 res
= CreateBitmap(desiredx
, desiredy
, 1, 1, NULL
);
1884 HDC screenDC
= GetDC(NULL
);
1885 res
= CreateCompatibleBitmap(screenDC
, desiredx
, desiredy
);
1886 ReleaseDC(NULL
, screenDC
);
1892 /* Only copy the bitmap if it's a DIB section or if it's
1893 compatible to the screen */
1896 if (objSize
== sizeof(DIBSECTION
))
1898 copyContents
= TRUE
;
1902 HDC screenDC
= GetDC(NULL
);
1903 int screen_depth
= GetDeviceCaps(screenDC
, BITSPIXEL
);
1904 ReleaseDC(NULL
, screenDC
);
1906 copyContents
= (ds
.dsBm
.bmBitsPixel
== 1 || ds
.dsBm
.bmBitsPixel
== screen_depth
);
1911 /* The source bitmap may already be selected in a device context,
1912 use GetDIBits/StretchDIBits and not StretchBlt */
1917 dc
= CreateCompatibleDC(NULL
);
1919 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
1920 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
1921 bi
->bmiHeader
.biSizeImage
= 0;
1922 bi
->bmiHeader
.biClrUsed
= 0;
1923 bi
->bmiHeader
.biClrImportant
= 0;
1925 /* Fill in biSizeImage */
1926 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
1927 bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bi
->bmiHeader
.biSizeImage
);
1933 /* Get the image bits of the source bitmap */
1934 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, bits
, bi
, DIB_RGB_COLORS
);
1936 /* Copy it to the destination bitmap */
1937 oldBmp
= SelectObject(dc
, res
);
1938 StretchDIBits(dc
, 0, 0, desiredx
, desiredy
,
1939 0, 0, ds
.dsBm
.bmWidth
, ds
.dsBm
.bmHeight
,
1940 bits
, bi
, DIB_RGB_COLORS
, SRCCOPY
);
1941 SelectObject(dc
, oldBmp
);
1943 HeapFree(GetProcessHeap(), 0, bits
);
1949 if (flags
& LR_COPYDELETEORG
)
1954 HeapFree(GetProcessHeap(), 0, bi
);
1958 return CURSORICON_ExtCopy(hnd
,type
, desiredx
, desiredy
, flags
);
1960 /* Should call CURSORICON_ExtCopy but more testing
1961 * needs to be done before we change this
1963 if (flags
) FIXME("Flags are ignored\n");
1964 return CopyCursor(hnd
);
1970 /******************************************************************************
1971 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
1974 * Success: Handle to specified bitmap
1977 HBITMAP WINAPI
LoadBitmapW(
1978 HINSTANCE instance
, /* [in] Handle to application instance */
1979 LPCWSTR name
) /* [in] Address of bitmap resource name */
1981 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
1984 /**********************************************************************
1985 * LoadBitmapA (USER32.@)
1989 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
1991 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
1995 CursorIconToCursor(HICON hIcon
,
1996 BOOL SemiTransparent
)
2010 return NtUserSetCursorPos(X
,Y
);
2018 GetCursorPos(LPPOINT lpPoint
)
2021 /* Windows doesn't check if lpPoint == NULL, we do */
2024 SetLastError(ERROR_INVALID_PARAMETER
);
2028 res
= NtUserGetCursorPos(lpPoint
);
2039 CopyCursor(HCURSOR pcur
)
2043 if(GetIconInfo((HANDLE
)pcur
, &IconInfo
))
2045 return (HCURSOR
)NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
2050 /* INTERNAL ******************************************************************/
2052 /* This callback routine is called directly after switching to gui mode */
2055 User32SetupDefaultCursors(PVOID Arguments
,
2056 ULONG ArgumentLength
)
2058 BOOL
*DefaultCursor
= (BOOL
*)Arguments
;
2059 LRESULT Result
= TRUE
;
2063 /* set default cursor */
2064 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_ARROW
));
2068 /* FIXME load system cursor scheme */
2070 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_ARROW
));
2073 return(ZwCallbackReturn(&Result
, sizeof(LRESULT
), STATUS_SUCCESS
));