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 *****************************************************************/
39 ICON_CreateIconFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
41 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
42 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
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
,
54 /* Make ImageData point to the start of the AND image data. */
55 ImageData
= ((PBYTE
)ImageData
) + (((IconImage
->icHeader
.biWidth
*
56 IconImage
->icHeader
.biBitCount
+ 31) & ~31) >> 3) *
57 (IconImage
->icHeader
.biHeight
);
59 /* Create a BITMAPINFO header for the monocrome part of the icon. */
60 bwBIH
->bmiHeader
.biBitCount
= 1;
61 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
62 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
63 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
64 bwBIH
->bmiHeader
.biPlanes
= 1;
65 bwBIH
->bmiHeader
.biSizeImage
= 0;
66 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
67 bwBIH
->bmiHeader
.biClrImportant
= 0;
68 bwBIH
->bmiHeader
.biClrUsed
= 0;
69 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
70 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
72 bwBIH
->bmiColors
[0].rgbBlue
= 0;
73 bwBIH
->bmiColors
[0].rgbGreen
= 0;
74 bwBIH
->bmiColors
[0].rgbRed
= 0;
75 bwBIH
->bmiColors
[0].rgbReserved
= 0;
77 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
78 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
79 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
80 bwBIH
->bmiColors
[1].rgbReserved
= 0;
82 /* Load the AND bitmap. */
83 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
84 ImageData
, bwBIH
, DIB_RGB_COLORS
);
86 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
87 ImageData
, bwBIH
, DIB_RGB_COLORS
);
89 /* Create the icon based on everything we have so far */
90 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
94 ICON_CreateCursorFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
)
96 /* FIXME - color cursors */
97 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
98 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
100 PVOID XORImageData
= ImageData
;
102 IconInfo
.fIcon
= FALSE
;
103 IconInfo
.xHotspot
= xHotspot
;
104 IconInfo
.yHotspot
= yHotspot
;
106 /* Create a BITMAPINFO header for the monocrome part of the icon */
107 bwBIH
->bmiHeader
.biBitCount
= 1;
108 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
109 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
110 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
111 bwBIH
->bmiHeader
.biPlanes
= 1;
112 bwBIH
->bmiHeader
.biSizeImage
= 0;
113 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
114 bwBIH
->bmiHeader
.biClrImportant
= 0;
115 bwBIH
->bmiHeader
.biClrUsed
= 0;
116 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
117 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
119 bwBIH
->bmiColors
[0].rgbBlue
= 0;
120 bwBIH
->bmiColors
[0].rgbGreen
= 0;
121 bwBIH
->bmiColors
[0].rgbRed
= 0;
122 bwBIH
->bmiColors
[0].rgbReserved
= 0;
124 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
125 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
126 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
127 bwBIH
->bmiColors
[1].rgbReserved
= 0;
129 /* Load the AND bitmap */
130 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
131 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
132 if (IconInfo
.hbmMask
)
134 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
135 XORImageData
, bwBIH
, DIB_RGB_COLORS
);
138 IconInfo
.hbmColor
= (HBITMAP
)0;
140 /* Create the icon based on everything we have so far */
141 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
155 if(GetIconInfo((HANDLE
)hIcon
, &IconInfo
))
157 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
179 IconInfo
.fIcon
= TRUE
;
180 IconInfo
.xHotspot
= nWidth
/ 2;
181 IconInfo
.yHotspot
= nHeight
/ 2;
182 IconInfo
.hbmMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, ANDbits
);
183 if(!IconInfo
.hbmMask
)
187 IconInfo
.hbmColor
= CreateBitmap(nWidth
, nHeight
, cPlanes
, cBitsPixel
, XORbits
);
188 if(!IconInfo
.hbmColor
)
190 DeleteObject(IconInfo
.hbmMask
);
194 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
203 CreateIconFromResource(
209 return CreateIconFromResourceEx(presbits
, dwResSize
, fIcon
, dwVer
, 0, 0, LR_DEFAULTSIZE
|LR_SHARED
);
218 CreateIconFromResourceEx(
227 ICONIMAGE
* SafeIconImage
;
237 FIXME - does win support LR_SHARED? According to msdn it does but we don't
238 have useful information to identify the icon
239 if (uFlags & LR_SHARED)
241 DbgPrint("FIXME: need LR_SHARED support in CreateIconFromResourceEx()\n");
245 TRACE("dwVersion, cxDesired, cyDesired are all ignored in this implementation!\n");
249 wXHotspot
= *(WORD
*)pbIconBits
;
250 pbIconBits
+= sizeof(WORD
);
251 wYHotspot
= *(WORD
*)pbIconBits
;
252 pbIconBits
+= sizeof(WORD
);
253 cbIconBits
-= 2 * sizeof(WORD
);
257 wXHotspot
= cxDesired
/ 2;
258 wYHotspot
= cyDesired
/ 2;
261 /* get an safe copy of the icon data */
262 SafeIconImage
= RtlAllocateHeap(GetProcessHeap(), 0, cbIconBits
);
263 if (SafeIconImage
== NULL
)
267 memcpy(SafeIconImage
, pbIconBits
, cbIconBits
);
269 /* take into acount the original height was for both the AND and XOR images */
271 SafeIconImage
->icHeader
.biHeight
/= 2;
273 if (SafeIconImage
->icHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
275 BITMAPCOREHEADER
* Core
= (BITMAPCOREHEADER
*)SafeIconImage
;
276 ColourCount
= (Core
->bcBitCount
<= 8) ? (1 << Core
->bcBitCount
) : 0;
277 HeaderSize
= sizeof(BITMAPCOREHEADER
) + ColourCount
* sizeof(RGBTRIPLE
);
281 ColourCount
= (SafeIconImage
->icHeader
.biBitCount
<= 8) ?
282 (1 << SafeIconImage
->icHeader
.biBitCount
) : 0;
283 HeaderSize
= sizeof(BITMAPINFOHEADER
) + ColourCount
* sizeof(RGBQUAD
);
286 /* make data point to the start of the XOR image data */
287 Data
= (PBYTE
)SafeIconImage
+ HeaderSize
;
289 /* get a handle to the screen dc, the icon we create is going to be compatable with this */
290 hScreenDc
= CreateCompatibleDC(NULL
);
291 if (hScreenDc
== NULL
)
293 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
298 hIcon
= ICON_CreateIconFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
300 hIcon
= ICON_CreateCursorFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
);
301 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
313 CreateIconIndirect(PICONINFO IconInfo
)
320 SetLastError(ERROR_INVALID_PARAMETER
);
324 if(!GetObjectW(IconInfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
328 /* FIXME - does there really *have* to be a color bitmap? monochrome cursors don't have one */
329 if(/*IconInfo->hbmColor &&*/ !GetObjectW(IconInfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
334 /* FIXME - i doubt this is right (monochrome cursors */
335 /*if(ColorBitmap.bmWidth != MaskBitmap.bmWidth ||
336 ColorBitmap.bmHeight != MaskBitmap.bmWidth)
338 SetLastError(ERROR_INVALID_PARAMETER);
342 return (HICON
)NtUserCreateCursorIconHandle(IconInfo
, TRUE
);
354 return (BOOL
)NtUserDestroyCursor((HANDLE
)hIcon
, 0);
369 return DrawIconEx(hDC
, X
, Y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
| DI_DEFAULTSIZE
);
385 HBRUSH hbrFlickerFreeDraw
,
388 return (BOOL
)NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
389 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
403 return NtUserGetIconInfo((HANDLE
)hIcon
, IconInfo
, 0, 0, 0, 0);
416 return(LoadImageA(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
429 return(LoadImageW(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
438 LookupIconIdFromDirectory(
442 return LookupIconIdFromDirectoryEx(presbits
,
444 fIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
445 fIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
),
454 * The following macro function accounts for the irregularities of
455 * accessing cursor and icon resources in files and resource entries.
458 (*fnGetCIEntry
)(LPVOID dir
, int n
, int *width
, int *height
, int *bits
);
460 /**********************************************************************
461 * CURSORICON_FindBestIcon
463 * Find the icon closest to the requested size and number of colors.
466 CURSORICON_FindBestIcon(LPVOID dir
,
467 fnGetCIEntry get_entry
,
472 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
473 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
474 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
477 iTotalDiff
= 0xFFFFFFFF;
478 iColorDiff
= 0xFFFFFFFF;
479 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
481 iTempXDiff
= abs(Width
- cx
);
482 iTempYDiff
= abs(Height
- cy
);
484 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
488 iTotalDiff
= iXDiff
+ iYDiff
;
492 /* Find Best Colors for Best Fit */
493 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
495 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
497 iTempColorDiff
= abs(ColorBits
- Bits
);
498 if(iColorDiff
> iTempColorDiff
)
502 iColorDiff
= iTempColorDiff
;
507 TRACE("Best Icon: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
514 /**********************************************************************
515 * CURSORICON_FindBestCursor
517 * Find the cursor closest to the requested size.
518 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
522 CURSORICON_FindBestCursor(LPVOID dir
,
523 fnGetCIEntry get_entry
,
528 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
529 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
530 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
533 iTotalDiff
= 0xFFFFFFFF;
534 iColorDiff
= 0xFFFFFFFF;
535 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
537 iTempXDiff
= abs(Width
- cx
);
538 iTempYDiff
= abs(Height
- cy
);
540 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
544 iTotalDiff
= iXDiff
+ iYDiff
;
548 /* Find Best Colors for Best Fit */
549 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
551 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
553 iTempColorDiff
= abs(ColorBits
- Bits
);
554 if(iColorDiff
> iTempColorDiff
)
558 iColorDiff
= iTempColorDiff
;
563 TRACE("Best Cursor: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
570 CURSORICON_GetResIconEntry(LPVOID dir
,
576 GRPCURSORICONDIR
*ResDir
= dir
;
579 if (ResDir
->idCount
<= n
)
582 Icon
= &ResDir
->idEntries
[n
].ResInfo
.icon
;
583 *Width
= Icon
->bWidth
;
584 *Height
= Icon
->bHeight
;
585 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
590 CURSORICON_GetResCursorEntry(LPVOID dir
,
596 GRPCURSORICONDIR
*ResDir
= dir
;
597 CURSORRESDIR
*Cursor
;
599 if (ResDir
->idCount
<= n
)
602 Cursor
= &ResDir
->idEntries
[n
].ResInfo
.cursor
;
603 *Width
= Cursor
->wWidth
;
604 *Height
= Cursor
->wHeight
;
605 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
609 static GRPCURSORICONDIRENTRY
*
610 CURSORICON_FindBestIconRes(GRPCURSORICONDIR
* dir
,
616 n
= CURSORICON_FindBestIcon(dir
,
617 CURSORICON_GetResIconEntry
,
624 return &dir
->idEntries
[n
];
627 static GRPCURSORICONDIRENTRY
*
628 CURSORICON_FindBestCursorRes(GRPCURSORICONDIR
*dir
,
634 n
= CURSORICON_FindBestCursor(dir
,
635 CURSORICON_GetResCursorEntry
,
642 return &dir
->idEntries
[n
];
647 LookupIconIdFromDirectoryEx(PBYTE xdir
,
653 GRPCURSORICONDIR
*dir
= (GRPCURSORICONDIR
*)xdir
;
655 if(dir
&& !dir
->idReserved
&& (IMAGE_ICON
== dir
->idType
|| IMAGE_CURSOR
== dir
->idType
))
657 GRPCURSORICONDIRENTRY
*entry
= NULL
;
660 if (cFlag
& LR_MONOCHROME
)
666 HDC hdc
= CreateICW(NULL
, NULL
, NULL
, NULL
);
667 ColorBits
= GetDeviceCaps(hdc
, BITSPIXEL
);
672 entry
= CURSORICON_FindBestIconRes(dir
, width
, height
, ColorBits
);
674 entry
= CURSORICON_FindBestCursorRes(dir
, width
, height
, 1);
680 WARN("%s() : Invalid resource directory\n", __FUNCTION__
);