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.
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 ******************************************************************/
33 #include <wine/debug.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
36 /* FUNCTIONS *****************************************************************/
40 ICON_CreateIconFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
42 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
43 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
46 IconInfo
.fIcon
= TRUE
;
47 IconInfo
.xHotspot
= xHotspot
;
48 IconInfo
.yHotspot
= yHotspot
;
50 /* Load the XOR bitmap */
51 IconInfo
.hbmColor
= CreateDIBitmap(hDC
, &IconImage
->icHeader
, CBM_INIT
,
52 ImageData
, (BITMAPINFO
*)IconImage
,
55 /* Make ImageData point to the start of the AND image data. */
56 ImageData
= ((PBYTE
)ImageData
) + (((IconImage
->icHeader
.biWidth
*
57 IconImage
->icHeader
.biBitCount
+ 31) & ~31) >> 3) *
58 (IconImage
->icHeader
.biHeight
);
60 /* Create a BITMAPINFO header for the monocrome part of the icon. */
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
= 0;
67 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
68 bwBIH
->bmiHeader
.biClrImportant
= 0;
69 bwBIH
->bmiHeader
.biClrUsed
= 0;
70 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
71 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
73 bwBIH
->bmiColors
[0].rgbBlue
= 0;
74 bwBIH
->bmiColors
[0].rgbGreen
= 0;
75 bwBIH
->bmiColors
[0].rgbRed
= 0;
76 bwBIH
->bmiColors
[0].rgbReserved
= 0;
78 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
79 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
80 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
81 bwBIH
->bmiColors
[1].rgbReserved
= 0;
83 /* Load the AND bitmap. */
84 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
85 ImageData
, bwBIH
, DIB_RGB_COLORS
);
87 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
88 ImageData
, bwBIH
, DIB_RGB_COLORS
);
90 /* Create the icon based on everything we have so far */
91 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
95 ICON_CreateCursorFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
97 /* FIXME - color cursors */
98 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
99 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
101 PVOID XORImageData
= ImageData
;
103 IconInfo
.fIcon
= FALSE
;
104 IconInfo
.xHotspot
= xHotspot
;
105 IconInfo
.yHotspot
= yHotspot
;
107 /* Create a BITMAPINFO header for the monocrome part of the icon */
108 bwBIH
->bmiHeader
.biBitCount
= 1;
109 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
110 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
111 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
112 bwBIH
->bmiHeader
.biPlanes
= 1;
113 bwBIH
->bmiHeader
.biSizeImage
= 0;
114 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
115 bwBIH
->bmiHeader
.biClrImportant
= 0;
116 bwBIH
->bmiHeader
.biClrUsed
= 0;
117 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
118 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
120 bwBIH
->bmiColors
[0].rgbBlue
= 0;
121 bwBIH
->bmiColors
[0].rgbGreen
= 0;
122 bwBIH
->bmiColors
[0].rgbRed
= 0;
123 bwBIH
->bmiColors
[0].rgbReserved
= 0;
125 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
126 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
127 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
128 bwBIH
->bmiColors
[1].rgbReserved
= 0;
130 /* Load the AND bitmap */
131 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
132 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
133 if (IconInfo
.hbmMask
)
135 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
136 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
139 IconInfo
.hbmColor
= (HBITMAP
)0;
141 /* Create the icon based on everything we have so far */
142 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
151 CopyIcon(HICON hIcon
)
153 HICON hRetIcon
= NULL
;
156 if(GetIconInfo(hIcon
, &IconInfo
))
158 hRetIcon
= CreateIconIndirect(&IconInfo
);
159 DeleteObject(IconInfo
.hbmColor
);
160 DeleteObject(IconInfo
.hbmMask
);
183 IconInfo
.fIcon
= TRUE
;
184 IconInfo
.xHotspot
= nWidth
/ 2;
185 IconInfo
.yHotspot
= nHeight
/ 2;
186 IconInfo
.hbmMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, ANDbits
);
187 if(!IconInfo
.hbmMask
)
191 IconInfo
.hbmColor
= CreateBitmap(nWidth
, nHeight
, cPlanes
, cBitsPixel
, XORbits
);
192 if(!IconInfo
.hbmColor
)
194 DeleteObject(IconInfo
.hbmMask
);
198 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
207 CreateIconFromResource(
213 return CreateIconFromResourceEx(presbits
, dwResSize
, fIcon
, dwVer
, 0, 0, LR_DEFAULTSIZE
|LR_SHARED
);
222 CreateIconFromResourceEx(
231 ICONIMAGE
* SafeIconImage
;
241 FIXME - does win support LR_SHARED? According to msdn it does but we don't
242 have useful information to identify the icon
243 if (uFlags & LR_SHARED)
245 DbgPrint("FIXME: need LR_SHARED support in CreateIconFromResourceEx()\n");
249 TRACE("dwVersion, cxDesired, cyDesired are all ignored in this implementation!\n");
253 wXHotspot
= *(WORD
*)pbIconBits
;
254 pbIconBits
+= sizeof(WORD
);
255 wYHotspot
= *(WORD
*)pbIconBits
;
256 pbIconBits
+= sizeof(WORD
);
257 cbIconBits
-= 2 * sizeof(WORD
);
261 wXHotspot
= cxDesired
/ 2;
262 wYHotspot
= cyDesired
/ 2;
265 /* get an safe copy of the icon data */
266 SafeIconImage
= RtlAllocateHeap(GetProcessHeap(), 0, cbIconBits
);
267 if (SafeIconImage
== NULL
)
271 memcpy(SafeIconImage
, pbIconBits
, cbIconBits
);
273 /* take into acount the original height was for both the AND and XOR images */
275 SafeIconImage
->icHeader
.biHeight
/= 2;
277 if (SafeIconImage
->icHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
279 BITMAPCOREHEADER
* Core
= (BITMAPCOREHEADER
*)SafeIconImage
;
280 ColourCount
= (Core
->bcBitCount
<= 8) ? (1 << Core
->bcBitCount
) : 0;
281 HeaderSize
= sizeof(BITMAPCOREHEADER
) + ColourCount
* sizeof(RGBTRIPLE
);
285 ColourCount
= (SafeIconImage
->icHeader
.biBitCount
<= 8) ?
286 (1 << SafeIconImage
->icHeader
.biBitCount
) : 0;
287 HeaderSize
= sizeof(BITMAPINFOHEADER
) + ColourCount
* sizeof(RGBQUAD
);
290 /* make data point to the start of the XOR image data */
291 Data
= (PBYTE
)SafeIconImage
+ HeaderSize
;
293 /* get a handle to the screen dc, the icon we create is going to be compatable with this */
294 hScreenDc
= CreateCompatibleDC(NULL
);
295 if (hScreenDc
== NULL
)
297 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
302 hIcon
= ICON_CreateIconFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
304 hIcon
= ICON_CreateCursorFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
305 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
317 CreateIconIndirect(PICONINFO IconInfo
)
324 SetLastError(ERROR_INVALID_PARAMETER
);
328 if(!GetObjectW(IconInfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
332 /* FIXME - does there really *have* to be a color bitmap? monochrome cursors don't have one */
333 if(/*IconInfo->hbmColor &&*/ !GetObjectW(IconInfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
338 /* FIXME - i doubt this is right (monochrome cursors */
339 /*if(ColorBitmap.bmWidth != MaskBitmap.bmWidth ||
340 ColorBitmap.bmHeight != MaskBitmap.bmWidth)
342 SetLastError(ERROR_INVALID_PARAMETER);
346 return (HICON
)NtUserCreateCursorIconHandle(IconInfo
, TRUE
);
358 return (BOOL
)NtUserDestroyCursor((HANDLE
)hIcon
, 0);
373 return DrawIconEx(hDC
, X
, Y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
| DI_DEFAULTSIZE
);
389 HBRUSH hbrFlickerFreeDraw
,
392 return (BOOL
)NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
393 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
407 return NtUserGetIconInfo((HANDLE
)hIcon
, IconInfo
, 0, 0, 0, 0);
420 return(LoadImageA(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
433 return(LoadImageW(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
442 LookupIconIdFromDirectory(
446 return LookupIconIdFromDirectoryEx(presbits
,
448 fIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
449 fIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
),
458 * The following macro function accounts for the irregularities of
459 * accessing cursor and icon resources in files and resource entries.
462 (*fnGetCIEntry
)(LPVOID dir
, int n
, int *width
, int *height
, int *bits
);
464 /**********************************************************************
465 * CURSORICON_FindBestIcon
467 * Find the icon closest to the requested size and number of colors.
470 CURSORICON_FindBestIcon(LPVOID dir
,
471 fnGetCIEntry get_entry
,
476 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
477 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
478 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
481 iTotalDiff
= 0xFFFFFFFF;
482 iColorDiff
= 0xFFFFFFFF;
483 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
485 iTempXDiff
= abs(Width
- cx
);
486 iTempYDiff
= abs(Height
- cy
);
488 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
492 iTotalDiff
= iXDiff
+ iYDiff
;
496 /* Find Best Colors for Best Fit */
497 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
499 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
501 iTempColorDiff
= abs(ColorBits
- Bits
);
502 if(iColorDiff
> iTempColorDiff
)
506 iColorDiff
= iTempColorDiff
;
511 TRACE("Best Icon: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
518 /**********************************************************************
519 * CURSORICON_FindBestCursor
521 * Find the cursor closest to the requested size.
522 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
526 CURSORICON_FindBestCursor(LPVOID dir
,
527 fnGetCIEntry get_entry
,
532 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
533 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
534 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
537 iTotalDiff
= 0xFFFFFFFF;
538 iColorDiff
= 0xFFFFFFFF;
539 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
541 iTempXDiff
= abs(Width
- cx
);
542 iTempYDiff
= abs(Height
- cy
);
544 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
548 iTotalDiff
= iXDiff
+ iYDiff
;
552 /* Find Best Colors for Best Fit */
553 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
555 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
557 iTempColorDiff
= abs(ColorBits
- Bits
);
558 if(iColorDiff
> iTempColorDiff
)
562 iColorDiff
= iTempColorDiff
;
567 TRACE("Best Cursor: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
574 CURSORICON_GetResIconEntry(LPVOID dir
,
580 GRPCURSORICONDIR
*ResDir
= dir
;
583 if (ResDir
->idCount
<= n
)
586 Icon
= &ResDir
->idEntries
[n
].ResInfo
.icon
;
587 *Width
= Icon
->bWidth
;
588 *Height
= Icon
->bHeight
;
589 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
594 CURSORICON_GetResCursorEntry(LPVOID dir
,
600 GRPCURSORICONDIR
*ResDir
= dir
;
601 CURSORRESDIR
*Cursor
;
603 if (ResDir
->idCount
<= n
)
606 Cursor
= &ResDir
->idEntries
[n
].ResInfo
.cursor
;
607 *Width
= Cursor
->wWidth
;
608 *Height
= Cursor
->wHeight
;
609 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
613 static GRPCURSORICONDIRENTRY
*
614 CURSORICON_FindBestIconRes(GRPCURSORICONDIR
* dir
,
620 n
= CURSORICON_FindBestIcon(dir
,
621 CURSORICON_GetResIconEntry
,
628 return &dir
->idEntries
[n
];
631 static GRPCURSORICONDIRENTRY
*
632 CURSORICON_FindBestCursorRes(GRPCURSORICONDIR
*dir
,
638 n
= CURSORICON_FindBestCursor(dir
,
639 CURSORICON_GetResCursorEntry
,
646 return &dir
->idEntries
[n
];
651 LookupIconIdFromDirectoryEx(PBYTE xdir
,
657 GRPCURSORICONDIR
*dir
= (GRPCURSORICONDIR
*)xdir
;
659 if(dir
&& !dir
->idReserved
&& (IMAGE_ICON
== dir
->idType
|| IMAGE_CURSOR
== dir
->idType
))
661 GRPCURSORICONDIRENTRY
*entry
= NULL
;
664 if (cFlag
& LR_MONOCHROME
)
670 HDC hdc
= CreateICW(NULL
, NULL
, NULL
, NULL
);
671 ColorBits
= GetDeviceCaps(hdc
, BITSPIXEL
);
676 entry
= CURSORICON_FindBestIconRes(dir
, width
, height
, ColorBits
);
678 entry
= CURSORICON_FindBestCursorRes(dir
, width
, height
, 1);
684 WARN("%s() : Invalid resource directory\n", __FUNCTION__
);