3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: icon.c,v 1.14 2003/11/18 19:59:51 weiden Exp $
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/icon.c
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * 09-05-2001 CSH Created
29 /* INCLUDES ******************************************************************/
37 /* FUNCTIONS *****************************************************************/
40 ICON_CreateIconFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
45 IconInfo
.fIcon
= TRUE
;
46 IconInfo
.xHotspot
= xHotspot
;
47 IconInfo
.yHotspot
= yHotspot
;
49 /* Load the XOR bitmap */
50 IconInfo
.hbmColor
= CreateDIBitmap(hDC
, &IconImage
->icHeader
, CBM_INIT
,
51 ImageData
, (BITMAPINFO
*)IconImage
, DIB_RGB_COLORS
);
53 /* make ImageData point to the start of the AND image data */
54 ImageData
= ((PBYTE
)ImageData
) + (((IconImage
->icHeader
.biWidth
*
55 IconImage
->icHeader
.biBitCount
+ 31) & ~31) >> 3) *
56 (IconImage
->icHeader
.biHeight
);
58 /* create a BITMAPINFO header for the monocrome part of the icon */
59 bwBIH
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof (BITMAPINFOHEADER
)+2*sizeof(RGBQUAD
));
61 bwBIH
->bmiHeader
.biBitCount
= 1;
62 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
63 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
64 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
65 bwBIH
->bmiHeader
.biPlanes
= 1;
66 bwBIH
->bmiHeader
.biSizeImage
= (((IconImage
->icHeader
.biWidth
* 1 + 31) & ~31) >> 3) *
67 (IconImage
->icHeader
.biHeight
);
68 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
69 bwBIH
->bmiHeader
.biClrImportant
= 0;
70 bwBIH
->bmiHeader
.biClrUsed
= 0;
71 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
72 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
74 bwBIH
->bmiColors
[0].rgbBlue
= 0;
75 bwBIH
->bmiColors
[0].rgbGreen
= 0;
76 bwBIH
->bmiColors
[0].rgbRed
= 0;
77 bwBIH
->bmiColors
[0].rgbReserved
= 0;
79 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
80 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
81 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
82 bwBIH
->bmiColors
[1].rgbReserved
= 0;
84 /* load the AND bitmap */
85 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
86 ImageData
, bwBIH
, DIB_RGB_COLORS
);
88 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
89 ImageData
, bwBIH
, DIB_RGB_COLORS
);
91 RtlFreeHeap(RtlGetProcessHeap(), 0, bwBIH
);
93 /* Create the icon based on everything we have so far */
94 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
98 ICON_CreateCursorFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
100 /* FIXME - color cursors */
103 PVOID XORImageData
= ImageData
;
105 IconInfo
.fIcon
= TRUE
;
106 IconInfo
.xHotspot
= xHotspot
;
107 IconInfo
.yHotspot
= yHotspot
;
109 /* create a BITMAPINFO header for the monocrome part of the icon */
110 bwBIH
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof (BITMAPINFOHEADER
)+2*sizeof(RGBQUAD
));
112 bwBIH
->bmiHeader
.biBitCount
= 1;
113 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
114 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
115 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
116 bwBIH
->bmiHeader
.biPlanes
= 1;
117 bwBIH
->bmiHeader
.biSizeImage
= (((IconImage
->icHeader
.biWidth
* 1 + 31) & ~31) >> 3) *
118 (IconImage
->icHeader
.biHeight
);
119 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
120 bwBIH
->bmiHeader
.biClrImportant
= 0;
121 bwBIH
->bmiHeader
.biClrUsed
= 0;
122 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
123 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
125 bwBIH
->bmiColors
[0].rgbBlue
= 0;
126 bwBIH
->bmiColors
[0].rgbGreen
= 0;
127 bwBIH
->bmiColors
[0].rgbRed
= 0;
128 bwBIH
->bmiColors
[0].rgbReserved
= 0;
130 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
131 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
132 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
133 bwBIH
->bmiColors
[1].rgbReserved
= 0;
135 /* load the AND bitmap */
136 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
137 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
141 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
142 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
146 IconInfo
.hbmColor
= (HBITMAP
)0;
148 RtlFreeHeap(RtlGetProcessHeap(), 0, bwBIH
);
150 /* Create the icon based on everything we have so far */
151 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
165 if(NtUserGetIconInfo(hIcon
, &IconInfo
))
167 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
189 IconInfo
.fIcon
= TRUE
;
190 IconInfo
.xHotspot
= nWidth
/ 2;
191 IconInfo
.yHotspot
= nHeight
/ 2;
192 IconInfo
.hbmMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, ANDbits
);
193 if(!IconInfo
.hbmMask
)
197 IconInfo
.hbmColor
= CreateBitmap(nWidth
, nHeight
, cPlanes
, cBitsPixel
, XORbits
);
198 if(!IconInfo
.hbmColor
)
200 DeleteObject(IconInfo
.hbmMask
);
204 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
213 CreateIconFromResource(
219 return CreateIconFromResourceEx(presbits
, dwResSize
, fIcon
, dwVer
, 0, 0, 0);
228 CreateIconFromResourceEx(
237 ICONIMAGE
* SafeIconImage
;
247 FIXME - does win support LR_SHARED? According to msdn it does but we don't
248 have useful information to identify the icon
249 if (uFlags & LR_SHARED)
251 DbgPrint("FIXME: need LR_SHARED support in CreateIconFromResourceEx()\n");
255 DPRINT("dwVersion, cxDesired, cyDesired are all ignored in this implementation!\n");
259 wXHotspot
= (WORD
)*pbIconBits
;
261 wYHotspot
= (WORD
)*pbIconBits
;
267 wXHotspot
= cxDesired
/ 2;
268 wYHotspot
= cyDesired
/ 2;
271 /* get an safe copy of the icon data */
272 SafeIconImage
= RtlAllocateHeap(RtlGetProcessHeap(), 0, cbIconBits
);
273 memcpy(SafeIconImage
, pbIconBits
, cbIconBits
);
275 /* take into acount the origonal height was for both the AND and XOR images */
277 SafeIconImage
->icHeader
.biHeight
/= 2;
279 if (SafeIconImage
->icHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
281 BITMAPCOREHEADER
* Core
= (BITMAPCOREHEADER
*)SafeIconImage
;
282 ColourCount
= (Core
->bcBitCount
<= 8) ? (1 << Core
->bcBitCount
) : 0;
283 HeaderSize
= sizeof(BITMAPCOREHEADER
) + ColourCount
* sizeof(RGBTRIPLE
);
287 ColourCount
= (SafeIconImage
->icHeader
.biBitCount
<= 8) ?
288 (1 << SafeIconImage
->icHeader
.biBitCount
) : 0;
289 HeaderSize
= sizeof(BITMAPINFOHEADER
) + ColourCount
* sizeof(RGBQUAD
);
292 /* make data point to the start of the XOR image data */
293 Data
= (PBYTE
)SafeIconImage
+ HeaderSize
;
295 /* get a handle to the screen dc, the icon we create is going to be compatable with this */
296 hScreenDc
= CreateCompatibleDC(NULL
);
297 if (hScreenDc
== NULL
)
299 RtlFreeHeap(RtlGetProcessHeap(), 0, SafeIconImage
);
304 hIcon
= ICON_CreateIconFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
306 hIcon
= ICON_CreateCursorFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
307 RtlFreeHeap(RtlGetProcessHeap(), 0, SafeIconImage
);
318 CreateIconIndirect(PICONINFO IconInfo
)
325 SetLastError(ERROR_INVALID_PARAMETER
);
329 if(!GetObjectW(IconInfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
333 /* FIXME - does there really *have* to be a color bitmap? monochrome cursors don't have one */
334 if(IconInfo
->hbmColor
&& !GetObjectW(IconInfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
339 /* FIXME - i doubt this is right (monochrome cursors */
340 /*if(ColorBitmap.bmWidth != MaskBitmap.bmWidth ||
341 ColorBitmap.bmHeight != MaskBitmap.bmWidth)
343 SetLastError(ERROR_INVALID_PARAMETER);
347 return NtUserCreateCursorIconHandle(IconInfo
, TRUE
);
359 return (WINBOOL
)NtUserDestroyCursor(hIcon
, 0);
374 return DrawIconEx(hDC
, X
, Y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
| DI_DEFAULTSIZE
);
390 HBRUSH hbrFlickerFreeDraw
,
393 return (WINBOOL
)NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
394 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
408 /* FIXME - copy bitmaps */
409 return (WINBOOL
)NtUserGetIconInfo(hIcon
, IconInfo
);
422 return(LoadImageA(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
435 return(LoadImageW(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
444 LookupIconIdFromDirectory(
448 return LookupIconIdFromDirectoryEx( presbits
, fIcon
,
449 fIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
450 fIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), LR_DEFAULTCOLOR
);
453 /* Ported from WINE20030408 */
455 CURSORICON_FindBestCursor( CURSORICONDIR
*dir
, int width
, int height
, int colors
)
458 CURSORICONDIRENTRY
*entry
, *bestEntry
= NULL
;
459 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
460 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
462 if (dir
->idCount
< 1)
464 DPRINT("Empty directory!\n");
467 if (dir
->idCount
== 1)
468 return &dir
->idEntries
[0]; /* No choice... */
471 iTotalDiff
= 0xFFFFFFFF;
472 iColorDiff
= 0xFFFFFFFF;
473 for (i
= 0, entry
= &dir
->idEntries
[0]; i
< dir
->idCount
; i
++,entry
++)
475 iTempXDiff
= abs(width
- entry
->bWidth
);
476 iTempYDiff
= abs(height
- entry
->bHeight
);
478 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
482 iTotalDiff
= iXDiff
+ iYDiff
;
486 /* Find Best Colors for Best Fit */
487 for (i
= 0, entry
= &dir
->idEntries
[0]; i
< dir
->idCount
; i
++,entry
++)
489 if(abs(width
- entry
->bWidth
) == (int) iXDiff
&&
490 abs(height
- entry
->bHeight
) == (int) iYDiff
)
492 iTempColorDiff
= abs(colors
- entry
->bColorCount
);
494 if(iColorDiff
> iTempColorDiff
)
497 iColorDiff
= iTempColorDiff
;
505 /* Ported from WINE20030408 */
507 CURSORICON_FindBestIcon( CURSORICONDIR
*dir
, int width
, int height
, int colors
)
510 CURSORICONDIRENTRY
*entry
, *bestEntry
= NULL
;
511 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
512 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
514 if (dir
->idCount
< 1)
516 DPRINT("Empty directory!\n");
519 if (dir
->idCount
== 1)
520 return &dir
->idEntries
[0]; /* No choice... */
523 iTotalDiff
= 0xFFFFFFFF;
524 iColorDiff
= 0xFFFFFFFF;
525 for (i
= 0, entry
= &dir
->idEntries
[0]; i
< dir
->idCount
; i
++,entry
++)
527 iTempXDiff
= abs(width
- entry
->bWidth
);
529 iTempYDiff
= abs(height
- entry
->bHeight
);
531 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
535 iTotalDiff
= iXDiff
+ iYDiff
;
539 /* Find Best Colors for Best Fit */
540 for (i
= 0, entry
= &dir
->idEntries
[0]; i
< dir
->idCount
; i
++,entry
++)
542 if(abs(width
- entry
->bWidth
) == (int) iXDiff
&&
543 abs(height
- entry
->bHeight
) == (int) iYDiff
)
545 iTempColorDiff
= abs(colors
- entry
->bColorCount
);
546 if(iColorDiff
> iTempColorDiff
)
549 iColorDiff
= iTempColorDiff
;
557 /* Ported from WINE20030408 */
563 LookupIconIdFromDirectoryEx(
570 GRPCURSORICONDIR
*dir
= (GRPCURSORICONDIR
*)presbits
;
573 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
575 GRPCURSORICONDIRENTRY
* entry
;
581 palEnts
= GetSystemPaletteEntries(hdc
, 0, 0, NULL
);
585 palEnts
= 16; //use this until GetSystemPaletteEntries works
586 colors
= (Flags
& LR_MONOCHROME
) ? 2 : palEnts
;
590 entry
= (GRPCURSORICONDIRENTRY
*)CURSORICON_FindBestIcon( (CURSORICONDIR
*)dir
,
600 DbgPrint("invalid resource directory\n");