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 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
98 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
99 BITMAPINFO
*orgBIH
= (BITMAPINFO
*)IconImage
;
101 PVOID XORImageData
= ImageData
;
103 IconInfo
.fIcon
= FALSE
;
104 IconInfo
.xHotspot
= xHotspot
;
105 IconInfo
.yHotspot
= yHotspot
;
107 /* Create a BITMAPINFO header for the monochrome 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
, orgBIH
, DIB_RGB_COLORS
);
139 if (IconImage
->icHeader
.biBitCount
== 1)
141 IconInfo
.hbmColor
= (HBITMAP
)0;
145 /* Create the color part of the icon */
146 IconInfo
.hbmColor
= CreateDIBitmap(hDC
, &IconImage
->icHeader
, 0,
147 XORImageData
, orgBIH
, DIB_RGB_COLORS
);
148 if (IconInfo
.hbmColor
)
150 SetDIBits(hDC
, IconInfo
.hbmColor
, 0, IconImage
->icHeader
.biHeight
,
151 XORImageData
, orgBIH
, DIB_RGB_COLORS
);
155 /* Create the icon based on everything we have so far */
156 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
165 CopyIcon(HICON hIcon
)
167 HICON hRetIcon
= NULL
;
170 if(GetIconInfo(hIcon
, &IconInfo
))
172 hRetIcon
= CreateIconIndirect(&IconInfo
);
173 DeleteObject(IconInfo
.hbmColor
);
174 DeleteObject(IconInfo
.hbmMask
);
197 IconInfo
.fIcon
= TRUE
;
198 IconInfo
.xHotspot
= nWidth
/ 2;
199 IconInfo
.yHotspot
= nHeight
/ 2;
200 IconInfo
.hbmMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, ANDbits
);
201 if(!IconInfo
.hbmMask
)
205 IconInfo
.hbmColor
= CreateBitmap(nWidth
, nHeight
, cPlanes
, cBitsPixel
, XORbits
);
206 if(!IconInfo
.hbmColor
)
208 DeleteObject(IconInfo
.hbmMask
);
212 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
221 CreateIconFromResource(
227 return CreateIconFromResourceEx(presbits
, dwResSize
, fIcon
, dwVer
, 0, 0, LR_DEFAULTSIZE
|LR_SHARED
);
236 CreateIconFromResourceEx(
245 ICONIMAGE
* SafeIconImage
;
255 FIXME - does win support LR_SHARED? According to msdn it does but we don't
256 have useful information to identify the icon
257 if (uFlags & LR_SHARED)
259 DbgPrint("FIXME: need LR_SHARED support in CreateIconFromResourceEx()\n");
263 TRACE("dwVersion, cxDesired, cyDesired are all ignored in this implementation!\n");
267 wXHotspot
= *(WORD
*)pbIconBits
;
268 pbIconBits
+= sizeof(WORD
);
269 wYHotspot
= *(WORD
*)pbIconBits
;
270 pbIconBits
+= sizeof(WORD
);
271 cbIconBits
-= 2 * sizeof(WORD
);
275 wXHotspot
= cxDesired
/ 2;
276 wYHotspot
= cyDesired
/ 2;
279 /* get an safe copy of the icon data */
280 SafeIconImage
= RtlAllocateHeap(GetProcessHeap(), 0, cbIconBits
);
281 if (SafeIconImage
== NULL
)
285 memcpy(SafeIconImage
, pbIconBits
, cbIconBits
);
287 /* take into acount the original height was for both the AND and XOR images */
289 SafeIconImage
->icHeader
.biHeight
/= 2;
291 if (SafeIconImage
->icHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
293 BITMAPCOREHEADER
* Core
= (BITMAPCOREHEADER
*)SafeIconImage
;
294 ColourCount
= (Core
->bcBitCount
<= 8) ? (1 << Core
->bcBitCount
) : 0;
295 HeaderSize
= sizeof(BITMAPCOREHEADER
) + ColourCount
* sizeof(RGBTRIPLE
);
299 ColourCount
= (SafeIconImage
->icHeader
.biBitCount
<= 8) ?
300 (1 << SafeIconImage
->icHeader
.biBitCount
) : 0;
301 HeaderSize
= sizeof(BITMAPINFOHEADER
) + ColourCount
* sizeof(RGBQUAD
);
304 /* make data point to the start of the XOR image data */
305 Data
= (PBYTE
)SafeIconImage
+ HeaderSize
;
307 /* get a handle to the screen dc, the icon we create is going to be compatable with this */
308 // FIXME!!! This is a victim of the Win32k Initialization BUG!!!!!
309 //hScreenDc = CreateDCW(NULL, NULL, NULL, NULL);
310 hScreenDc
= CreateCompatibleDC(NULL
);
311 if (hScreenDc
== NULL
)
313 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
318 hIcon
= ICON_CreateIconFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
320 hIcon
= ICON_CreateCursorFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
321 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
333 CreateIconIndirect(PICONINFO IconInfo
)
340 SetLastError(ERROR_INVALID_PARAMETER
);
344 if(!GetObjectW(IconInfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
348 /* FIXME - does there really *have* to be a color bitmap? monochrome cursors don't have one */
349 if(/*IconInfo->hbmColor &&*/ !GetObjectW(IconInfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
354 /* FIXME - i doubt this is right (monochrome cursors */
355 /*if(ColorBitmap.bmWidth != MaskBitmap.bmWidth ||
356 ColorBitmap.bmHeight != MaskBitmap.bmWidth)
358 SetLastError(ERROR_INVALID_PARAMETER);
362 return (HICON
)NtUserCreateCursorIconHandle(IconInfo
, TRUE
);
374 return (BOOL
)NtUserDestroyCursor((HANDLE
)hIcon
, 0);
389 return DrawIconEx(hDC
, X
, Y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
| DI_DEFAULTSIZE
);
405 HBRUSH hbrFlickerFreeDraw
,
408 return (BOOL
)NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
409 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
423 return NtUserGetIconInfo((HANDLE
)hIcon
, IconInfo
, 0, 0, 0, 0);
436 return(LoadImageA(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
449 return(LoadImageW(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
458 LookupIconIdFromDirectory(
462 return LookupIconIdFromDirectoryEx(presbits
,
464 fIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
465 fIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
),
474 * The following macro function accounts for the irregularities of
475 * accessing cursor and icon resources in files and resource entries.
478 (*fnGetCIEntry
)(LPVOID dir
, int n
, int *width
, int *height
, int *bits
);
480 /**********************************************************************
481 * CURSORICON_FindBestIcon
483 * Find the icon closest to the requested size and number of colors.
486 CURSORICON_FindBestIcon(LPVOID dir
,
487 fnGetCIEntry get_entry
,
492 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
493 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
494 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
497 iTotalDiff
= 0xFFFFFFFF;
498 iColorDiff
= 0xFFFFFFFF;
499 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
501 iTempXDiff
= abs(Width
- cx
);
502 iTempYDiff
= abs(Height
- cy
);
504 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
508 iTotalDiff
= iXDiff
+ iYDiff
;
512 /* Find Best Colors for Best Fit */
513 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
515 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
517 iTempColorDiff
= abs(ColorBits
- Bits
);
518 if(iColorDiff
> iTempColorDiff
)
522 iColorDiff
= iTempColorDiff
;
527 TRACE("Best Icon: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
534 /**********************************************************************
535 * CURSORICON_FindBestCursor
537 * Find the cursor closest to the requested size.
538 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
542 CURSORICON_FindBestCursor(LPVOID dir
,
543 fnGetCIEntry get_entry
,
548 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
549 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
550 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
553 iTotalDiff
= 0xFFFFFFFF;
554 iColorDiff
= 0xFFFFFFFF;
555 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
557 iTempXDiff
= abs(Width
- cx
);
558 iTempYDiff
= abs(Height
- cy
);
560 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
564 iTotalDiff
= iXDiff
+ iYDiff
;
568 /* Find Best Colors for Best Fit */
569 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
571 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
573 iTempColorDiff
= abs(ColorBits
- Bits
);
574 if(iColorDiff
> iTempColorDiff
)
578 iColorDiff
= iTempColorDiff
;
583 TRACE("Best Cursor: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
590 CURSORICON_GetResIconEntry(LPVOID dir
,
596 GRPCURSORICONDIR
*ResDir
= dir
;
599 if (ResDir
->idCount
<= n
)
602 Icon
= &ResDir
->idEntries
[n
].ResInfo
.icon
;
603 *Width
= Icon
->bWidth
;
604 *Height
= Icon
->bHeight
;
605 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
610 CURSORICON_GetResCursorEntry(LPVOID dir
,
616 GRPCURSORICONDIR
*ResDir
= dir
;
617 CURSORRESDIR
*Cursor
;
619 if (ResDir
->idCount
<= n
)
622 Cursor
= &ResDir
->idEntries
[n
].ResInfo
.cursor
;
623 *Width
= Cursor
->wWidth
;
624 *Height
= Cursor
->wHeight
;
625 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
629 static GRPCURSORICONDIRENTRY
*
630 CURSORICON_FindBestIconRes(GRPCURSORICONDIR
* dir
,
636 n
= CURSORICON_FindBestIcon(dir
,
637 CURSORICON_GetResIconEntry
,
644 return &dir
->idEntries
[n
];
647 static GRPCURSORICONDIRENTRY
*
648 CURSORICON_FindBestCursorRes(GRPCURSORICONDIR
*dir
,
654 n
= CURSORICON_FindBestCursor(dir
,
655 CURSORICON_GetResCursorEntry
,
662 return &dir
->idEntries
[n
];
667 LookupIconIdFromDirectoryEx(PBYTE xdir
,
673 GRPCURSORICONDIR
*dir
= (GRPCURSORICONDIR
*)xdir
;
678 if(dir
&& !dir
->idReserved
&& (IMAGE_ICON
== dir
->idType
|| IMAGE_CURSOR
== dir
->idType
))
680 GRPCURSORICONDIRENTRY
*entry
= NULL
;
683 if (cFlag
& LR_MONOCHROME
)
687 else if (cFlag
& LR_VGACOLOR
)
693 ColorBits
= g_psi
->BitsPixel
;
697 entry
= CURSORICON_FindBestIconRes(dir
, width
, height
, ColorBits
);
699 entry
= CURSORICON_FindBestCursorRes(dir
, width
, height
, 1);
705 WARN("%s() : Invalid resource directory\n", __FUNCTION__
);