1 /*
2 * Cursor and icon support
3 *
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
9 *
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.
14 *
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.
19 *
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
23 */
24
25 /*
26 * Theory:
27 *
28 * Cursors and icons are stored in a global heap block, with the
29 * following layout:
30 *
31 * CURSORICONINFO info;
32 * BYTE[] ANDbits;
33 * BYTE[] XORbits;
34 *
35 * The bits structures are in the format of a device-dependent bitmap.
36 *
37 * This layout is very sub-optimal, as the bitmap bits are stored in
38 * the X client instead of in the server like other bitmaps; however,
39 * some programs (notably Paint Brush) expect to be able to manipulate
40 * the bits directly :-(
41 */
42
43 #include "config.h"
44 #include "wine/port.h"
45
46 #include <stdarg.h>
47 #include <string.h>
48 #include <stdlib.h>
49
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "user_private.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 WINE_DECLARE_DEBUG_CHANNEL(icon);
62 WINE_DECLARE_DEBUG_CHANNEL(resource);
63
64 #include "pshpack1.h"
65
66 typedef struct {
67 BYTE bWidth;
68 BYTE bHeight;
69 BYTE bColorCount;
70 BYTE bReserved;
71 WORD xHotspot;
72 WORD yHotspot;
73 DWORD dwDIBSize;
74 DWORD dwDIBOffset;
75 } CURSORICONFILEDIRENTRY;
76
77 typedef struct
78 {
79 WORD idReserved;
80 WORD idType;
81 WORD idCount;
82 CURSORICONFILEDIRENTRY idEntries[1];
83 } CURSORICONFILEDIR;
84
85 #include "poppack.h"
86
87 #define CID_RESOURCE 0x0001
88 #define CID_WIN32 0x0004
89 #define CID_NONSHARED 0x0008
90
91 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
92
93 static HDC screen_dc;
94
95 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96
97 /**********************************************************************
98 * ICONCACHE for cursors/icons loaded with LR_SHARED.
99 *
100 * FIXME: This should not be allocated on the system heap, but on a
101 * subsystem-global heap (i.e. one for all Win16 processes,
102 * and one for each Win32 process).
103 */
104 typedef struct tagICONCACHE
105 {
106 struct tagICONCACHE *next;
107
108 HMODULE hModule;
109 HRSRC hRsrc;
110 HRSRC hGroupRsrc;
111 HICON hIcon;
112
113 INT count;
114
115 } ICONCACHE;
116
117 static ICONCACHE *IconAnchor = NULL;
118
119 static CRITICAL_SECTION IconCrst;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
121 {
122 0, 0, &IconCrst,
123 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 };
126 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127
128 static const WORD ICON_HOTSPOT = 0x4242;
129
130
131 /***********************************************************************
132 * map_fileW
133 *
134 * Helper function to map a file to memory:
135 * name - file name
136 * [RETURN] ptr - pointer to mapped file
137 * [RETURN] filesize - pointer size of file to be stored if not NULL
138 */
139 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
140 {
141 HANDLE hFile, hMapping;
142 LPVOID ptr = NULL;
143
144 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
145 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
146 if (hFile != INVALID_HANDLE_VALUE)
147 {
148 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
149 if (hMapping)
150 {
151 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
152 CloseHandle( hMapping );
153 if (filesize)
154 *filesize = GetFileSize( hFile, NULL );
155 }
156 CloseHandle( hFile );
157 }
158 return ptr;
159 }
160
161
162 /***********************************************************************
163 * get_bitmap_width_bytes
164 *
165 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
166 * data.
167 */
168 static int get_bitmap_width_bytes( int width, int bpp )
169 {
170 switch(bpp)
171 {
172 case 1:
173 return 2 * ((width+15) / 16);
174 case 4:
175 return 2 * ((width+3) / 4);
176 case 24:
177 width *= 3;
178 /* fall through */
179 case 8:
180 return width + (width & 1);
181 case 16:
182 case 15:
183 return width * 2;
184 case 32:
185 return width * 4;
186 default:
187 WARN("Unknown depth %d, please report.\n", bpp );
188 }
189 return -1;
190 }
191
192
193 /***********************************************************************
194 * get_dib_width_bytes
195 *
196 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
197 */
198 static int get_dib_width_bytes( int width, int depth )
199 {
200 int words;
201
202 switch(depth)
203 {
204 case 1: words = (width + 31) / 32; break;
205 case 4: words = (width + 7) / 8; break;
206 case 8: words = (width + 3) / 4; break;
207 case 15:
208 case 16: words = (width + 1) / 2; break;
209 case 24: words = (width * 3 + 3)/4; break;
210 default:
211 WARN("(%d): Unsupported depth\n", depth );
212 /* fall through */
213 case 32:
214 words = width;
215 }
216 return 4 * words;
217 }
218
219
220 /***********************************************************************
221 * bitmap_info_size
222 *
223 * Return the size of the bitmap info structure including color table.
224 */
225 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
226 {
227 int colors, masks = 0;
228
229 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
230 {
231 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
232 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
233 return sizeof(BITMAPCOREHEADER) + colors *
234 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
235 }
236 else /* assume BITMAPINFOHEADER */
237 {
238 colors = info->bmiHeader.biClrUsed;
239 if (colors > 256) /* buffer overflow otherwise */
240 colors = 256;
241 if (!colors && (info->bmiHeader.biBitCount <= 8))
242 colors = 1 << info->bmiHeader.biBitCount;
243 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
244 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
245 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
246 }
247 }
248
249
250 /***********************************************************************
251 * is_dib_monochrome
252 *
253 * Returns whether a DIB can be converted to a monochrome DDB.
254 *
255 * A DIB can be converted if its color table contains only black and
256 * white. Black must be the first color in the color table.
257 *
258 * Note : If the first color in the color table is white followed by
259 * black, we can't convert it to a monochrome DDB with
260 * SetDIBits, because black and white would be inverted.
261 */
262 static BOOL is_dib_monochrome( const BITMAPINFO* info )
263 {
264 if (info->bmiHeader.biBitCount != 1) return FALSE;
265
266 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
267 {
268 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
269
270 /* Check if the first color is black */
271 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
272 {
273 rgb++;
274
275 /* Check if the second color is white */
276 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
277 && (rgb->rgbtBlue == 0xff));
278 }
279 else return FALSE;
280 }
281 else /* assume BITMAPINFOHEADER */
282 {
283 const RGBQUAD *rgb = info->bmiColors;
284
285 /* Check if the first color is black */
286 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
287 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
288 {
289 rgb++;
290
291 /* Check if the second color is white */
292 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
293 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
294 }
295 else return FALSE;
296 }
297 }
298
299 /***********************************************************************
300 * DIB_GetBitmapInfo
301 *
302 * Get the info from a bitmap header.
303 * Return 1 for INFOHEADER, 0 for COREHEADER,
304 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
305 */
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307 LONG *height, WORD *bpp, DWORD *compr )
308 {
309 if (header->biSize == sizeof(BITMAPINFOHEADER))
310 {
311 *width = header->biWidth;
312 *height = header->biHeight;
313 *bpp = header->biBitCount;
314 *compr = header->biCompression;
315 return 1;
316 }
317 if (header->biSize == sizeof(BITMAPCOREHEADER))
318 {
319 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
320 *width = core->bcWidth;
321 *height = core->bcHeight;
322 *bpp = core->bcBitCount;
323 *compr = 0;
324 return 0;
325 }
326 if (header->biSize == sizeof(BITMAPV4HEADER))
327 {
328 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
329 *width = v4hdr->bV4Width;
330 *height = v4hdr->bV4Height;
331 *bpp = v4hdr->bV4BitCount;
332 *compr = v4hdr->bV4V4Compression;
333 return 4;
334 }
335 if (header->biSize == sizeof(BITMAPV5HEADER))
336 {
337 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
338 *width = v5hdr->bV5Width;
339 *height = v5hdr->bV5Height;
340 *bpp = v5hdr->bV5BitCount;
341 *compr = v5hdr->bV5Compression;
342 return 5;
343 }
344 ERR("(%d): unknown/wrong size for header\n", header->biSize );
345 return -1;
346 }
347
348 /**********************************************************************
349 * CURSORICON_FindSharedIcon
350 */
351 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
352 {
353 HICON hIcon = 0;
354 ICONCACHE *ptr;
355
356 EnterCriticalSection( &IconCrst );
357
358 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
359 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
360 {
361 ptr->count++;
362 hIcon = ptr->hIcon;
363 break;
364 }
365
366 LeaveCriticalSection( &IconCrst );
367
368 return hIcon;
369 }
370
371 /*************************************************************************
372 * CURSORICON_FindCache
373 *
374 * Given a handle, find the corresponding cache element
375 *
376 * PARAMS
377 * Handle [I] handle to an Image
378 *
379 * RETURNS
380 * Success: The cache entry
381 * Failure: NULL
382 *
383 */
384 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
385 {
386 ICONCACHE *ptr;
387 ICONCACHE *pRet=NULL;
388 BOOL IsFound = FALSE;
389
390 EnterCriticalSection( &IconCrst );
391
392 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
393 {
394 if ( hIcon == ptr->hIcon )
395 {
396 IsFound = TRUE;
397 pRet = ptr;
398 }
399 }
400
401 LeaveCriticalSection( &IconCrst );
402
403 return pRet;
404 }
405
406 /**********************************************************************
407 * CURSORICON_AddSharedIcon
408 */
409 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
410 {
411 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
412 if ( !ptr ) return;
413
414 ptr->hModule = hModule;
415 ptr->hRsrc = hRsrc;
416 ptr->hIcon = hIcon;
417 ptr->hGroupRsrc = hGroupRsrc;
418 ptr->count = 1;
419
420 EnterCriticalSection( &IconCrst );
421 ptr->next = IconAnchor;
422 IconAnchor = ptr;
423 LeaveCriticalSection( &IconCrst );
424 }
425
426 /**********************************************************************
427 * CURSORICON_DelSharedIcon
428 */
429 static INT CURSORICON_DelSharedIcon( HICON hIcon )
430 {
431 INT count = -1;
432 ICONCACHE *ptr;
433
434 EnterCriticalSection( &IconCrst );
435
436 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
437 if ( ptr->hIcon == hIcon )
438 {
439 if ( ptr->count > 0 ) ptr->count--;
440 count = ptr->count;
441 break;
442 }
443
444 LeaveCriticalSection( &IconCrst );
445
446 return count;
447 }
448
449 /**********************************************************************
450 * CURSORICON_FreeModuleIcons
451 */
452 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
453 {
454 ICONCACHE **ptr = &IconAnchor;
455 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
456
457 EnterCriticalSection( &IconCrst );
458
459 while ( *ptr )
460 {
461 if ( (*ptr)->hModule == hModule )
462 {
463 ICONCACHE *freePtr = *ptr;
464 *ptr = freePtr->next;
465
466 GlobalFree16(HICON_16(freePtr->hIcon));
467 HeapFree( GetProcessHeap(), 0, freePtr );
468 continue;
469 }
470 ptr = &(*ptr)->next;
471 }
472
473 LeaveCriticalSection( &IconCrst );
474 }
475
476 /*
477 * The following macro functions account for the irregularities of
478 * accessing cursor and icon resources in files and resource entries.
479 */
480 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
481 int *width, int *height, int *bits );
482
483 /**********************************************************************
484 * CURSORICON_FindBestIcon
485 *
486 * Find the icon closest to the requested size and number of colors.
487 */
488 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
489 int width, int height, int colors )
490 {
491 int i, cx, cy, bits, bestEntry = -1;
492 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
493 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
494
495 /* Find Best Fit */
496 iTotalDiff = 0xFFFFFFFF;
497 iColorDiff = 0xFFFFFFFF;
498 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
499 {
500 iTempXDiff = abs(width - cx);
501 iTempYDiff = abs(height - cy);
502
503 if(iTotalDiff > (iTempXDiff + iTempYDiff))
504 {
505 iXDiff = iTempXDiff;
506 iYDiff = iTempYDiff;
507 iTotalDiff = iXDiff + iYDiff;
508 }
509 }
510
511 /* Find Best Colors for Best Fit */
512 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
513 {
514 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
515 {
516 iTempColorDiff = abs(colors - (1<<bits));
517 if(iColorDiff > iTempColorDiff)
518 {
519 bestEntry = i;
520 iColorDiff = iTempColorDiff;
521 }
522 }
523 }
524
525 return bestEntry;
526 }
527
528 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
529 int *width, int *height, int *bits )
530 {
531 CURSORICONDIR *resdir = dir;
532 ICONRESDIR *icon;
533
534 if ( resdir->idCount <= n )
535 return FALSE;
536 icon = &resdir->idEntries[n].ResInfo.icon;
537 *width = icon->bWidth;
538 *height = icon->bHeight;
539 *bits = resdir->idEntries[n].wBitCount;
540 return TRUE;
541 }
542
543 /**********************************************************************
544 * CURSORICON_FindBestCursor
545 *
546 * Find the cursor closest to the requested size.
547 *
548 * FIXME: parameter 'color' ignored.
549 */
550 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
551 int width, int height, int color )
552 {
553 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
554
555 /* Double height to account for AND and XOR masks */
556
557 height *= 2;
558
559 /* First find the largest one smaller than or equal to the requested size*/
560
561 maxwidth = maxheight = 0;
562 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
563 {
564 if ((cx <= width) && (cy <= height) &&
565 (cx > maxwidth) && (cy > maxheight))
566 {
567 bestEntry = i;
568 maxwidth = cx;
569 maxheight = cy;
570 }
571 }
572 if (bestEntry != -1) return bestEntry;
573
574 /* Now find the smallest one larger than the requested size */
575
576 maxwidth = maxheight = 255;
577 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
578 {
579 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
580 {
581 bestEntry = i;
582 maxwidth = cx;
583 maxheight = cy;
584 }
585 }
586
587 return bestEntry;
588 }
589
590 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
591 int *width, int *height, int *bits )
592 {
593 CURSORICONDIR *resdir = dir;
594 CURSORDIR *cursor;
595
596 if ( resdir->idCount <= n )
597 return FALSE;
598 cursor = &resdir->idEntries[n].ResInfo.cursor;
599 *width = cursor->wWidth;
600 *height = cursor->wHeight;
601 *bits = resdir->idEntries[n].wBitCount;
602 return TRUE;
603 }
604
605 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
606 int width, int height, int colors )
607 {
608 int n;
609
610 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
611 width, height, colors );
612 if ( n < 0 )
613 return NULL;
614 return &dir->idEntries[n];
615 }
616
617 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
618 int width, int height, int color )
619 {
620 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
621 width, height, color );
622 if ( n < 0 )
623 return NULL;
624 return &dir->idEntries[n];
625 }
626
627 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
628 int *width, int *height, int *bits )
629 {
630 CURSORICONFILEDIR *filedir = dir;
631 CURSORICONFILEDIRENTRY *entry;
632
633 if ( filedir->idCount <= n )
634 return FALSE;
635 entry = &filedir->idEntries[n];
636 *width = entry->bWidth;
637 *height = entry->bHeight;
638 *bits = entry->bColorCount;
639 return TRUE;
640 }
641
642 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
643 int width, int height, int color )
644 {
645 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
646 width, height, color );
647 if ( n < 0 )
648 return NULL;
649 return &dir->idEntries[n];
650 }
651
652 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
653 int width, int height, int color )
654 {
655 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
656 width, height, color );
657 if ( n < 0 )
658 return NULL;
659 return &dir->idEntries[n];
660 }
661
662 /***********************************************************************
663 * stretch_blt_icon
664 *
665 * A helper function that stretches a bitmap buffer into an HBITMAP.
666 *
667 * PARAMS
668 * hDest [I] The handle of the destination bitmap.
669 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
670 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
671 * pSrcBits [I] A pointer to the source bitmap buffer.
672 **/
673 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
674 {
675 HBITMAP hOld;
676 BOOL res = FALSE;
677 HDC hdcMem = CreateCompatibleDC(screen_dc);
678
679 if (hdcMem)
680 {
681 hOld = SelectObject(hdcMem, hDest);
682 res = StretchDIBits(hdcMem,
683 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
684 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
685 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
686 SelectObject(hdcMem, hOld);
687 DeleteDC( hdcMem );
688 }
689
690 return res;
691 }
692
693 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
694 POINT16 hotspot, BOOL bIcon,
695 DWORD dwVersion,
696 INT width, INT height,
697 UINT cFlag )
698 {
699 HGLOBAL16 hObj;
700 int sizeAnd, sizeXor;
701 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
702 BITMAP bmpXor, bmpAnd;
703 INT size;
704 BITMAPINFO *pSrcInfo, *pDestInfo;
705
706 if (dwVersion == 0x00020000)
707 {
708 FIXME_(cursor)("\t2.xx resources are not supported\n");
709 return 0;
710 }
711
712 /* Check bitmap header */
713
714 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
715 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
716 bmi->bmiHeader.biCompression != BI_RGB) )
717 {
718 WARN_(cursor)("\tinvalid resource bitmap header.\n");
719 return 0;
720 }
721
722 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
723
724 if (!width) width = bmi->bmiHeader.biWidth;
725 if (!height) height = bmi->bmiHeader.biHeight/2;
726
727 /* Scale the hotspot */
728 if (((bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width)) &&
729 hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
730 {
731 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
732 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
733 }
734
735 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
736 if (screen_dc)
737 {
738 /* Make sure we have room for the monochrome bitmap later on.
739 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
740 * up to and including the biBitCount. In-memory icon resource
741 * format is as follows:
742 *
743 * BITMAPINFOHEADER icHeader // DIB header
744 * RGBQUAD icColors[] // Color table
745 * BYTE icXOR[] // DIB bits for XOR mask
746 * BYTE icAND[] // DIB bits for AND mask
747 */
748
749 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
750 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
751 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
752 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
753 if (pSrcInfo && pDestInfo)
754 {
755 memcpy( pSrcInfo, bmi, size );
756 pSrcInfo->bmiHeader.biHeight /= 2;
757
758 memcpy( pDestInfo, bmi, size );
759 pDestInfo->bmiHeader.biWidth = width;
760 pDestInfo->bmiHeader.biHeight = height;
761 pDestInfo->bmiHeader.biSizeImage = 0;
762
763 /* Create the XOR bitmap */
764 if(pSrcInfo->bmiHeader.biBitCount == 32)
765 {
766 void *pDIBBuffer = NULL;
767 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
768
769 if(hXorBits)
770 {
771 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
772 {
773 DeleteObject(hXorBits);
774 hXorBits = 0;
775 }
776 }
777 }
778 else
779 {
780 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
781
782 if(hXorBits)
783 {
784 if(!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
785 {
786 DeleteObject(hXorBits);
787 hXorBits = 0;
788 }
789 }
790 }
791
792 if( hXorBits )
793 {
794 char* xbits = (char *)bmi + size +
795 get_dib_width_bytes( bmi->bmiHeader.biWidth,
796 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
797
798 pSrcInfo->bmiHeader.biBitCount = 1;
799 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
800 {
801 RGBQUAD *rgb = pSrcInfo->bmiColors;
802
803 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
804 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
805 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
806 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
807 }
808 else
809 {
810 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
811
812 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
813 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
814 }
815
816 /* Create the AND bitmap */
817 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
818
819 if(!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
820 {
821 DeleteObject(hAndBits);
822 hAndBits = 0;
823 }
824
825 if( !hAndBits )
826 {
827 DeleteObject( hXorBits );
828 hXorBits = 0;
829 }
830 }
831 }
832
833 HeapFree( GetProcessHeap(), 0, pSrcInfo );
834 HeapFree( GetProcessHeap(), 0, pDestInfo );
835 }
836
837 if( !hXorBits || !hAndBits )
838 {
839 WARN_(cursor)("\tunable to create an icon bitmap.\n");
840 return 0;
841 }
842
843 /* Now create the CURSORICONINFO structure */
844 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
845 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
846 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
847 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
848
849 hObj = GlobalAlloc16( GMEM_MOVEABLE,
850 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
851 if (hObj)
852 {
853 CURSORICONINFO *info;
854
855 info = GlobalLock16( hObj );
856 info->ptHotSpot.x = hotspot.x;
857 info->ptHotSpot.y = hotspot.y;
858 info->nWidth = bmpXor.bmWidth;
859 info->nHeight = bmpXor.bmHeight;
860 info->nWidthBytes = bmpXor.bmWidthBytes;
861 info->bPlanes = bmpXor.bmPlanes;
862 info->bBitsPerPixel = bmpXor.bmBitsPixel;
863
864 /* Transfer the bitmap bits to the CURSORICONINFO structure */
865
866 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
867 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
868 GlobalUnlock16( hObj );
869 }
870
871 DeleteObject( hAndBits );
872 DeleteObject( hXorBits );
873 return HICON_32(hObj);
874 }
875
876
877 /**********************************************************************
878 * .ANI cursor support
879 */
880 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
881 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
882 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
883
884 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
885 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
886 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
887 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
888 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
889 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
890
891 #define ANI_FLAG_ICON 0x1
892 #define ANI_FLAG_SEQUENCE 0x2
893
894 typedef struct {
895 DWORD header_size;
896 DWORD num_frames;
897 DWORD num_steps;
898 DWORD width;
899 DWORD height;
900 DWORD bpp;
901 DWORD num_planes;
902 DWORD display_rate;
903 DWORD flags;
904 } ani_header;
905
906 typedef struct {
907 DWORD data_size;
908 const unsigned char *data;
909 } riff_chunk_t;
910
911 static void dump_ani_header( const ani_header *header )
912 {
913 TRACE(" header size: %d\n", header->header_size);
914 TRACE(" frames: %d\n", header->num_frames);
915 TRACE(" steps: %d\n", header->num_steps);
916 TRACE(" width: %d\n", header->width);
917 TRACE(" height: %d\n", header->height);
918 TRACE(" bpp: %d\n", header->bpp);
919 TRACE(" planes: %d\n", header->num_planes);
920 TRACE(" display rate: %d\n", header->display_rate);
921 TRACE(" flags: 0x%08x\n", header->flags);
922 }
923
924
925 /*
926 * RIFF:
927 * DWORD "RIFF"
928 * DWORD size
929 * DWORD riff_id
930 * BYTE[] data
931 *
932 * LIST:
933 * DWORD "LIST"
934 * DWORD size
935 * DWORD list_id
936 * BYTE[] data
937 *
938 * CHUNK:
939 * DWORD chunk_id
940 * DWORD size
941 * BYTE[] data
942 */
943 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
944 {
945 const unsigned char *ptr = parent_chunk->data;
946 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
947
948 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
949
950 while (ptr < end)
951 {
952 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
953 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
954 {
955 ptr += sizeof(DWORD);
956 chunk->data_size = *(DWORD *)ptr;
957 ptr += sizeof(DWORD);
958 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
959 chunk->data = ptr;
960
961 return;
962 }
963
964 ptr += sizeof(DWORD);
965 ptr += *(DWORD *)ptr;
966 ptr += sizeof(DWORD);
967 }
968 }
969
970
971 /*
972 * .ANI layout:
973 *
974 * RIFF:'ACON' RIFF chunk
975 * |- CHUNK:'anih' Header
976 * |- CHUNK:'seq ' Sequence information (optional)
977 * \- LIST:'fram' Frame list
978 * |- CHUNK:icon Cursor frames
979 * |- CHUNK:icon
980 * |- ...
981 * \- CHUNK:icon
982 */
983 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
984 INT width, INT height, INT colors )
985 {
986 HCURSOR cursor;
987 ani_header header = {0};
988 LPBYTE frame_bits = 0;
989 POINT16 hotspot;
990 CURSORICONFILEDIRENTRY *entry;
991
992 riff_chunk_t root_chunk = { bits_size, bits };
993 riff_chunk_t ACON_chunk = {0};
994 riff_chunk_t anih_chunk = {0};
995 riff_chunk_t fram_chunk = {0};
996 const unsigned char *icon_data;
997
998 TRACE("bits %p, bits_size %d\n", bits, bits_size);
999
1000 if (!bits) return 0;
1001
1002 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1003 if (!ACON_chunk.data)
1004 {
1005 ERR("Failed to get root chunk.\n");
1006 return 0;
1007 }
1008
1009 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1010 if (!anih_chunk.data)
1011 {
1012 ERR("Failed to get 'anih' chunk.\n");
1013 return 0;
1014 }
1015 memcpy( &header, anih_chunk.data, sizeof(header) );
1016 dump_ani_header( &header );
1017
1018 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1019 if (!fram_chunk.data)
1020 {
1021 ERR("Failed to get icon list.\n");
1022 return 0;
1023 }
1024
1025 /* FIXME: For now, just load the first frame. Before we can load all the
1026 * frames, we need to write the needed code in wineserver, etc. to handle
1027 * cursors. Once this code is written, we can extend it to support .ani
1028 * cursors and then update user32 and winex11.drv to load all frames.
1029 *
1030 * Hopefully this will at least make some games (C&C3, etc.) more playable
1031 * in the meantime.
1032 */
1033 FIXME("Loading all frames for .ani cursors not implemented.\n");
1034 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1035
1036 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1037 width, height, colors );
1038
1039 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1040 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1041
1042 if (!header.width || !header.height)
1043 {
1044 header.width = entry->bWidth;
1045 header.height = entry->bHeight;
1046 }
1047
1048 hotspot.x = entry->xHotspot;
1049 hotspot.y = entry->yHotspot;
1050
1051 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1052 FALSE, 0x00030000, header.width, header.height, 0 );
1053
1054 HeapFree( GetProcessHeap(), 0, frame_bits );
1055
1056 return cursor;
1057 }
1058
1059
1060 /**********************************************************************
1061 * CreateIconFromResourceEx (USER32.@)
1062 *
1063 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1064 * with cbSize parameter as well.
1065 */
1066 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1067 BOOL bIcon, DWORD dwVersion,
1068 INT width, INT height,
1069 UINT cFlag )
1070 {
1071 POINT16 hotspot;
1072 BITMAPINFO *bmi;
1073
1074 hotspot.x = ICON_HOTSPOT;
1075 hotspot.y = ICON_HOTSPOT;
1076
1077 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1078 bits, cbSize, dwVersion, width, height,
1079 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1080
1081 if (bIcon)
1082 bmi = (BITMAPINFO *)bits;
1083 else /* get the hotspot */
1084 {
1085 POINT16 *pt = (POINT16 *)bits;
1086 hotspot = *pt;
1087 bmi = (BITMAPINFO *)(pt + 1);
1088 }
1089
1090 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1091 width, height, cFlag );
1092 }
1093
1094
1095 /**********************************************************************
1096 * CreateIconFromResource (USER32.@)
1097 */
1098 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1099 BOOL bIcon, DWORD dwVersion)
1100 {
1101 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1102 }
1103
1104
1105 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1106 INT width, INT height, INT colors,
1107 BOOL fCursor, UINT loadflags)
1108 {
1109 CURSORICONFILEDIRENTRY *entry;
1110 CURSORICONFILEDIR *dir;
1111 DWORD filesize = 0;
1112 HICON hIcon = 0;
1113 LPBYTE bits;
1114 POINT16 hotspot;
1115
1116 TRACE("loading %s\n", debugstr_w( filename ));
1117
1118 bits = map_fileW( filename, &filesize );
1119 if (!bits)
1120 return hIcon;
1121
1122 /* Check for .ani. */
1123 if (memcmp( bits, "RIFF", 4 ) == 0)
1124 {
1125 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1126 colors );
1127 goto end;
1128 }
1129
1130 dir = (CURSORICONFILEDIR*) bits;
1131 if ( filesize < sizeof(*dir) )
1132 goto end;
1133
1134 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1135 goto end;
1136
1137 if ( fCursor )
1138 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1139 else
1140 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1141
1142 if ( !entry )
1143 goto end;
1144
1145 /* check that we don't run off the end of the file */
1146 if ( entry->dwDIBOffset > filesize )
1147 goto end;
1148 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1149 goto end;
1150
1151 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1152 if ( fCursor )
1153 {
1154 hotspot.x = entry->xHotspot;
1155 hotspot.y = entry->yHotspot;
1156 }
1157 else
1158 {
1159 hotspot.x = ICON_HOTSPOT;
1160 hotspot.y = ICON_HOTSPOT;
1161 }
1162 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1163 hotspot, !fCursor, 0x00030000,
1164 width, height, loadflags );
1165 end:
1166 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1167 UnmapViewOfFile( bits );
1168 return hIcon;
1169 }
1170
1171 /**********************************************************************
1172 * CURSORICON_Load
1173 *
1174 * Load a cursor or icon from resource or file.
1175 */
1176 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1177 INT width, INT height, INT colors,
1178 BOOL fCursor, UINT loadflags)
1179 {
1180 HANDLE handle = 0;
1181 HICON hIcon = 0;
1182 HRSRC hRsrc, hGroupRsrc;
1183 CURSORICONDIR *dir;
1184 CURSORICONDIRENTRY *dirEntry;
1185 LPBYTE bits;
1186 WORD wResId;
1187 DWORD dwBytesInRes;
1188
1189 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1190 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1191
1192 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1193 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1194
1195 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1196
1197 /* Normalize hInstance (must be uniquely represented for icon cache) */
1198
1199 if (!HIWORD( hInstance ))
1200 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1201
1202 /* Get directory resource ID */
1203
1204 if (!(hRsrc = FindResourceW( hInstance, name,
1205 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1206 return 0;
1207 hGroupRsrc = hRsrc;
1208
1209 /* Find the best entry in the directory */
1210
1211 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1212 if (!(dir = LockResource( handle ))) return 0;
1213 if (fCursor)
1214 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1215 else
1216 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1217 if (!dirEntry) return 0;
1218 wResId = dirEntry->wResId;
1219 dwBytesInRes = dirEntry->dwBytesInRes;
1220 FreeResource( handle );
1221
1222 /* Load the resource */
1223
1224 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1225 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1226
1227 /* If shared icon, check whether it was already loaded */
1228 if ( (loadflags & LR_SHARED)
1229 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1230 return hIcon;
1231
1232 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1233 bits = LockResource( handle );
1234 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1235 !fCursor, 0x00030000, width, height, loadflags);
1236 FreeResource( handle );
1237
1238 /* If shared icon, add to icon cache */
1239
1240 if ( hIcon && (loadflags & LR_SHARED) )
1241 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1242
1243 return hIcon;
1244 }
1245
1246 /***********************************************************************
1247 * CURSORICON_Copy
1248 *
1249 * Make a copy of a cursor or icon.
1250 */
1251 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1252 {
1253 char *ptrOld, *ptrNew;
1254 int size;
1255 HICON16 hOld = HICON_16(hIcon);
1256 HICON16 hNew;
1257
1258 if (!(ptrOld = GlobalLock16( hOld ))) return 0;
1259 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1260 size = GlobalSize16( hOld );
1261 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1262 FarSetOwner16( hNew, hInst16 );
1263 ptrNew = GlobalLock16( hNew );
1264 memcpy( ptrNew, ptrOld, size );
1265 GlobalUnlock16( hOld );
1266 GlobalUnlock16( hNew );
1267 return HICON_32(hNew);
1268 }
1269
1270 /*************************************************************************
1271 * CURSORICON_ExtCopy
1272 *
1273 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1274 *
1275 * PARAMS
1276 * Handle [I] handle to an Image
1277 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1278 * iDesiredCX [I] The Desired width of the Image
1279 * iDesiredCY [I] The desired height of the Image
1280 * nFlags [I] The flags from CopyImage
1281 *
1282 * RETURNS
1283 * Success: The new handle of the Image
1284 *
1285 * NOTES
1286 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1287 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1288 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1289 *
1290 *
1291 */
1292
1293 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1294 INT iDesiredCX, INT iDesiredCY,
1295 UINT nFlags)
1296 {
1297 HICON hNew=0;
1298
1299 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1300 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1301
1302 if(hIcon == 0)
1303 {
1304 return 0;
1305 }
1306
1307 /* Best Fit or Monochrome */
1308 if( (nFlags & LR_COPYFROMRESOURCE
1309 && (iDesiredCX > 0 || iDesiredCY > 0))
1310 || nFlags & LR_MONOCHROME)
1311 {
1312 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1313
1314 /* Not Found in Cache, then do a straight copy
1315 */
1316 if(pIconCache == NULL)
1317 {
1318 hNew = CURSORICON_Copy(0, hIcon);
1319 if(nFlags & LR_COPYFROMRESOURCE)
1320 {
1321 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1322 }
1323 }
1324 else
1325 {
1326 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1327 LPBYTE pBits;
1328 HANDLE hMem;
1329 HRSRC hRsrc;
1330 DWORD dwBytesInRes;
1331 WORD wResId;
1332 CURSORICONDIR *pDir;
1333 CURSORICONDIRENTRY *pDirEntry;
1334 BOOL bIsIcon = (nType == IMAGE_ICON);
1335
1336 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1337 */
1338 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1339 || (iDesiredCX == 0 && iDesiredCY == 0))
1340 {
1341 iDesiredCY = GetSystemMetrics(bIsIcon ?
1342 SM_CYICON : SM_CYCURSOR);
1343 iDesiredCX = GetSystemMetrics(bIsIcon ?
1344 SM_CXICON : SM_CXCURSOR);
1345 }
1346
1347 /* Retrieve the CURSORICONDIRENTRY
1348 */
1349 if (!(hMem = LoadResource( pIconCache->hModule ,
1350 pIconCache->hGroupRsrc)))
1351 {
1352 return 0;
1353 }
1354 if (!(pDir = LockResource( hMem )))
1355 {
1356 return 0;
1357 }
1358
1359 /* Find Best Fit
1360 */
1361 if(bIsIcon)
1362 {
1363 pDirEntry = CURSORICON_FindBestIconRes(
1364 pDir, iDesiredCX, iDesiredCY, 256 );
1365 }
1366 else
1367 {
1368 pDirEntry = CURSORICON_FindBestCursorRes(
1369 pDir, iDesiredCX, iDesiredCY, 1);
1370 }
1371
1372 wResId = pDirEntry->wResId;
1373 dwBytesInRes = pDirEntry->dwBytesInRes;
1374 FreeResource(hMem);
1375
1376 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1377 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1378 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1379
1380 /* Get the Best Fit
1381 */
1382 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1383 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1384 {
1385 return 0;
1386 }
1387 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1388 {
1389 return 0;
1390 }
1391
1392 pBits = LockResource( hMem );
1393
1394 if(nFlags & LR_DEFAULTSIZE)
1395 {
1396 iTargetCY = GetSystemMetrics(SM_CYICON);
1397 iTargetCX = GetSystemMetrics(SM_CXICON);
1398 }
1399
1400 /* Create a New Icon with the proper dimension
1401 */
1402 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1403 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1404 FreeResource(hMem);
1405 }
1406 }
1407 else hNew = CURSORICON_Copy(0, hIcon);
1408 return hNew;
1409 }
1410
1411
1412 /***********************************************************************
1413 * CreateCursor (USER32.@)
1414 */
1415 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1416 INT xHotSpot, INT yHotSpot,
1417 INT nWidth, INT nHeight,
1418 LPCVOID lpANDbits, LPCVOID lpXORbits )
1419 {
1420 CURSORICONINFO info;
1421
1422 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1423 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1424
1425 info.ptHotSpot.x = xHotSpot;
1426 info.ptHotSpot.y = yHotSpot;
1427 info.nWidth = nWidth;
1428 info.nHeight = nHeight;
1429 info.nWidthBytes = 0;
1430 info.bPlanes = 1;
1431 info.bBitsPerPixel = 1;
1432
1433 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1434 }
1435
1436
1437 /***********************************************************************
1438 * CreateIcon (USER.407)
1439 */
1440 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1441 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1442 LPCVOID lpANDbits, LPCVOID lpXORbits )
1443 {
1444 CURSORICONINFO info;
1445
1446 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1447 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1448
1449 info.ptHotSpot.x = ICON_HOTSPOT;
1450 info.ptHotSpot.y = ICON_HOTSPOT;
1451 info.nWidth = nWidth;
1452 info.nHeight = nHeight;
1453 info.nWidthBytes = 0;
1454 info.bPlanes = bPlanes;
1455 info.bBitsPerPixel = bBitsPixel;
1456
1457 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1458 }
1459
1460
1461 /***********************************************************************
1462 * CreateIcon (USER32.@)
1463 *
1464 * Creates an icon based on the specified bitmaps. The bitmaps must be
1465 * provided in a device dependent format and will be resized to
1466 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1467 * depth. The provided bitmaps must be top-down bitmaps.
1468 * Although Windows does not support 15bpp(*) this API must support it
1469 * for Winelib applications.
1470 *
1471 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1472 * format!
1473 *
1474 * RETURNS
1475 * Success: handle to an icon
1476 * Failure: NULL
1477 *
1478 * FIXME: Do we need to resize the bitmaps?
1479 */
1480 HICON WINAPI CreateIcon(
1481 HINSTANCE hInstance, /* [in] the application's hInstance */
1482 INT nWidth, /* [in] the width of the provided bitmaps */
1483 INT nHeight, /* [in] the height of the provided bitmaps */
1484 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1485 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1486 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1487 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1488 {
1489 ICONINFO iinfo;
1490 HICON hIcon;
1491
1492 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1493 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1494
1495 iinfo.fIcon = TRUE;
1496 iinfo.xHotspot = ICON_HOTSPOT;
1497 iinfo.yHotspot = ICON_HOTSPOT;
1498 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1499 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1500
1501 hIcon = CreateIconIndirect( &iinfo );
1502
1503 DeleteObject( iinfo.hbmMask );
1504 DeleteObject( iinfo.hbmColor );
1505
1506 return hIcon;
1507 }
1508
1509
1510 /***********************************************************************
1511 * CreateCursorIconIndirect (USER.408)
1512 */
1513 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1514 CURSORICONINFO *info,
1515 LPCVOID lpANDbits,
1516 LPCVOID lpXORbits )
1517 {
1518 HGLOBAL16 handle;
1519 char *ptr;
1520 int sizeAnd, sizeXor;
1521
1522 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1523 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1524 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1525 sizeXor = info->nHeight * info->nWidthBytes;
1526 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1527 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1528 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1529 return 0;
1530 FarSetOwner16( handle, hInstance );
1531 ptr = GlobalLock16( handle );
1532 memcpy( ptr, info, sizeof(*info) );
1533 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1534 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1535 GlobalUnlock16( handle );
1536 return handle;
1537 }
1538
1539
1540 /***********************************************************************
1541 * CopyIcon (USER.368)
1542 */
1543 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1544 {
1545 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1546 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1547 }
1548
1549
1550 /***********************************************************************
1551 * CopyIcon (USER32.@)
1552 */
1553 HICON WINAPI CopyIcon( HICON hIcon )
1554 {
1555 TRACE_(icon)("%p\n", hIcon );
1556 return CURSORICON_Copy( 0, hIcon );
1557 }
1558
1559
1560 /***********************************************************************
1561 * CopyCursor (USER.369)
1562 */
1563 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1564 {
1565 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1566 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1567 }
1568
1569 /**********************************************************************
1570 * DestroyIcon32 (USER.610)
1571 *
1572 * This routine is actually exported from Win95 USER under the name
1573 * DestroyIcon32 ... The behaviour implemented here should mimic
1574 * the Win95 one exactly, especially the return values, which
1575 * depend on the setting of various flags.
1576 */
1577 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1578 {
1579 WORD retv;
1580
1581 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1582
1583 /* Check whether destroying active cursor */
1584
1585 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1586 {
1587 WARN_(cursor)("Destroying active cursor!\n" );
1588 return FALSE;
1589 }
1590
1591 /* Try shared cursor/icon first */
1592
1593 if ( !(flags & CID_NONSHARED) )
1594 {
1595 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1596
1597 if ( count != -1 )
1598 return (flags & CID_WIN32)? TRUE : (count == 0);
1599
1600 /* FIXME: OEM cursors/icons should be recognized */
1601 }
1602
1603 /* Now assume non-shared cursor/icon */
1604
1605 retv = GlobalFree16( handle );
1606 return (flags & CID_RESOURCE)? retv : TRUE;
1607 }
1608
1609 /***********************************************************************
1610 * DestroyIcon (USER32.@)
1611 */
1612 BOOL WINAPI DestroyIcon( HICON hIcon )
1613 {
1614 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1615 }
1616
1617
1618 /***********************************************************************
1619 * DestroyCursor (USER32.@)
1620 */
1621 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1622 {
1623 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1624 }
1625
1626 /***********************************************************************
1627 * bitmap_has_alpha_channel
1628 *
1629 * Analyses bits bitmap to determine if alpha data is present.
1630 *
1631 * PARAMS
1632 * bpp [I] The bits-per-pixel of the bitmap
1633 * bitmapBits [I] A pointer to the bitmap data
1634 * bitmapLength [I] The length of the bitmap in bytes
1635 *
1636 * RETURNS
1637 * TRUE if an alpha channel is discovered, FALSE
1638 *
1639 * NOTE
1640 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1641 * least one pixel has a non-zero alpha, then the bitmap is a
1642 * treated as having an alpha channel transparentcy. Otherwise,
1643 * it's treated as being completely opaque.
1644 *
1645 */
1646 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1647 unsigned int bitmapLength )
1648 {
1649 /* Detect an alpha channel by looking for non-zero alpha pixels */
1650 if(bpp == 32)
1651 {
1652 unsigned int offset;
1653 for(offset = 3; offset < bitmapLength; offset += 4)
1654 {
1655 if(bitmapBits[offset] != 0)
1656 {
1657 return TRUE;
1658 }
1659 }
1660 }
1661 return FALSE;
1662 }
1663
1664 /***********************************************************************
1665 * premultiply_alpha_channel
1666 *
1667 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1668 * channel. This is a necessary step that must be carried out on
1669 * the image before it is passed to GdiAlphaBlend
1670 *
1671 * PARAMS
1672 * destBitmap [I] The destination bitmap buffer
1673 * srcBitmap [I] The source bitmap buffer
1674 * bitmapLength [I] The length of the bitmap in bytes
1675 *
1676 */
1677 static void premultiply_alpha_channel( unsigned char *destBitmap,
1678 unsigned char *srcBitmap,
1679 unsigned int bitmapLength )
1680 {
1681 unsigned char *destPixel = destBitmap;
1682 unsigned char *srcPixel = srcBitmap;
1683
1684 while(destPixel < destBitmap + bitmapLength)
1685 {
1686 unsigned char alpha = srcPixel[3];
1687 *(destPixel++) = *(srcPixel++) * alpha / 255;
1688 *(destPixel++) = *(srcPixel++) * alpha / 255;
1689 *(destPixel++) = *(srcPixel++) * alpha / 255;
1690 *(destPixel++) = *(srcPixel++);
1691 }
1692 }
1693
1694 /***********************************************************************
1695 * DrawIcon (USER32.@)
1696 */
1697 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1698 {
1699 CURSORICONINFO *ptr;
1700 HDC hMemDC;
1701 HBITMAP hXorBits = NULL, hAndBits = NULL, hBitTemp = NULL;
1702 COLORREF oldFg, oldBg;
1703 unsigned char *xorBitmapBits;
1704 unsigned int dibLength;
1705
1706 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1707
1708 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
1709 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1710
1711 dibLength = ptr->nHeight * get_bitmap_width_bytes(
1712 ptr->nWidth, ptr->bBitsPerPixel);
1713
1714 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
1715 get_bitmap_width_bytes(ptr->nWidth, 1);
1716
1717 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1718 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1719
1720 if(bitmap_has_alpha_channel(ptr->bBitsPerPixel, xorBitmapBits, dibLength))
1721 {
1722 BITMAPINFOHEADER bmih;
1723 unsigned char *dibBits;
1724
1725 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
1726 bmih.biSize = sizeof(BITMAPINFOHEADER);
1727 bmih.biWidth = ptr->nWidth;
1728 bmih.biHeight = -ptr->nHeight;
1729 bmih.biPlanes = ptr->bPlanes;
1730 bmih.biBitCount = 32;
1731 bmih.biCompression = BI_RGB;
1732
1733 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
1734 (void*)&dibBits, NULL, 0);
1735
1736 if (hXorBits && dibBits)
1737 {
1738 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1739
1740 /* Do the alpha blending render */
1741 premultiply_alpha_channel(dibBits, xorBitmapBits, dibLength);
1742 hBitTemp = SelectObject( hMemDC, hXorBits );
1743 /* Destination width/height has to be "System Large" size */
1744 GdiAlphaBlend(hdc, x, y, GetSystemMetrics(SM_CXICON),
1745 GetSystemMetrics(SM_CYICON), hMemDC,
1746 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
1747 SelectObject( hMemDC, hBitTemp );
1748 }
1749 }
1750 else
1751 {
1752 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
1753 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1754 ptr->bBitsPerPixel, xorBitmapBits);
1755
1756 if (hXorBits && hAndBits)
1757 {
1758 hBitTemp = SelectObject( hMemDC, hAndBits );
1759 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1760 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1761 ptr->nWidth, ptr->nHeight, SRCAND );
1762 SelectObject( hMemDC, hXorBits );
1763 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1764 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1765 ptr->nWidth, ptr->nHeight, SRCINVERT );
1766 SelectObject( hMemDC, hBitTemp );
1767 }
1768 }
1769
1770 DeleteDC( hMemDC );
1771 if (hXorBits) DeleteObject( hXorBits );
1772 if (hAndBits) DeleteObject( hAndBits );
1773 GlobalUnlock16(HICON_16(hIcon));
1774 SetTextColor( hdc, oldFg );
1775 SetBkColor( hdc, oldBg );
1776 return TRUE;
1777 }
1778
1779 /***********************************************************************
1780 * DumpIcon (USER.459)
1781 */
1782 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1783 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1784 {
1785 CURSORICONINFO *info = MapSL( pInfo );
1786 int sizeAnd, sizeXor;
1787
1788 if (!info) return 0;
1789 sizeXor = info->nHeight * info->nWidthBytes;
1790 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1791 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1792 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1793 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1794 return MAKELONG( sizeXor, sizeXor );
1795 }
1796
1797
1798 /***********************************************************************
1799 * SetCursor (USER32.@)
1800 *
1801 * Set the cursor shape.
1802 *
1803 * RETURNS
1804 * A handle to the previous cursor shape.
1805 */
1806 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1807 {
1808 struct user_thread_info *thread_info = get_user_thread_info();
1809 HCURSOR hOldCursor;
1810
1811 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1812 TRACE("%p\n", hCursor);
1813 hOldCursor = thread_info->cursor;
1814 thread_info->cursor = hCursor;
1815 /* Change the cursor shape only if it is visible */
1816 if (thread_info->cursor_count >= 0)
1817 {
1818 USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(hCursor)));
1819 GlobalUnlock16(HCURSOR_16(hCursor));
1820 }
1821 return hOldCursor;
1822 }
1823
1824 /***********************************************************************
1825 * ShowCursor (USER32.@)
1826 */
1827 INT WINAPI ShowCursor( BOOL bShow )
1828 {
1829 struct user_thread_info *thread_info = get_user_thread_info();
1830
1831 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1832
1833 if (bShow)
1834 {
1835 if (++thread_info->cursor_count == 0) /* Show it */
1836 {
1837 USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(thread_info->cursor)));
1838 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1839 }
1840 }
1841 else
1842 {
1843 if (--thread_info->cursor_count == -1) /* Hide it */
1844 USER_Driver->pSetCursor( NULL );
1845 }
1846 return thread_info->cursor_count;
1847 }
1848
1849 /***********************************************************************
1850 * GetCursor (USER32.@)
1851 */
1852 HCURSOR WINAPI GetCursor(void)
1853 {
1854 return get_user_thread_info()->cursor;
1855 }
1856
1857
1858 /***********************************************************************
1859 * ClipCursor (USER32.@)
1860 */
1861 BOOL WINAPI ClipCursor( const RECT *rect )
1862 {
1863 RECT virt;
1864
1865 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1866 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1867 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1868 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1869
1870 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1871 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1872
1873 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1874 CURSOR_ClipRect = virt;
1875
1876 USER_Driver->pClipCursor( rect );
1877 return TRUE;
1878 }
1879
1880
1881 /***********************************************************************
1882 * GetClipCursor (USER32.@)
1883 */
1884 BOOL WINAPI GetClipCursor( RECT *rect )
1885 {
1886 /* If this is first time - initialize the rect */
1887 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1888
1889 return CopyRect( rect, &CURSOR_ClipRect );
1890 }
1891
1892
1893 /***********************************************************************
1894 * SetSystemCursor (USER32.@)
1895 */
1896 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1897 {
1898 FIXME("(%p,%08x),stub!\n", hcur, id);
1899 return TRUE;
1900 }
1901
1902
1903 /**********************************************************************
1904 * LookupIconIdFromDirectoryEx (USER.364)
1905 *
1906 * FIXME: exact parameter sizes
1907 */
1908 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1909 INT16 width, INT16 height, UINT16 cFlag )
1910 {
1911 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1912 }
1913
1914 /**********************************************************************
1915 * LookupIconIdFromDirectoryEx (USER32.@)
1916 */
1917 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1918 INT width, INT height, UINT cFlag )
1919 {
1920 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1921 UINT retVal = 0;
1922 if( dir && !dir->idReserved && (dir->idType & 3) )
1923 {
1924 CURSORICONDIRENTRY* entry;
1925 HDC hdc;
1926 UINT palEnts;
1927 int colors;
1928 hdc = GetDC(0);
1929 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1930 if (palEnts == 0)
1931 palEnts = 256;
1932 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1933
1934 ReleaseDC(0, hdc);
1935
1936 if( bIcon )
1937 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1938 else
1939 entry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1940
1941 if( entry ) retVal = entry->wResId;
1942 }
1943 else WARN_(cursor)("invalid resource directory\n");
1944 return retVal;
1945 }
1946
1947 /**********************************************************************
1948 * LookupIconIdFromDirectory (USER32.@)
1949 */
1950 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1951 {
1952 return LookupIconIdFromDirectoryEx( dir, bIcon,
1953 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1954 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1955 }
1956
1957 /**********************************************************************
1958 * GetIconID (USER.455)
1959 */
1960 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1961 {
1962 LPBYTE lpDir = GlobalLock16(hResource);
1963
1964 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1965 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1966
1967 switch(resType)
1968 {
1969 case RT_CURSOR:
1970 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1971 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1972 case RT_ICON:
1973 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1974 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1975 default:
1976 WARN_(cursor)("invalid res type %d\n", resType );
1977 }
1978 return 0;
1979 }
1980
1981 /**********************************************************************
1982 * LoadCursorIconHandler (USER.336)
1983 *
1984 * Supposed to load resources of Windows 2.x applications.
1985 */
1986 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1987 {
1988 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1989 hResource, hModule, hRsrc);
1990 return 0;
1991 }
1992
1993 /**********************************************************************
1994 * LoadIconHandler (USER.456)
1995 */
1996 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1997 {
1998 LPBYTE bits = LockResource16( hResource );
1999
2000 TRACE_(cursor)("hRes=%04x\n",hResource);
2001
2002 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2003 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2004 }
2005
2006 /***********************************************************************
2007 * LoadCursorW (USER32.@)
2008 */
2009 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2010 {
2011 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2012
2013 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2014 LR_SHARED | LR_DEFAULTSIZE );
2015 }
2016
2017 /***********************************************************************
2018 * LoadCursorA (USER32.@)
2019 */
2020 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2021 {
2022 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2023
2024 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2025 LR_SHARED | LR_DEFAULTSIZE );
2026 }
2027
2028 /***********************************************************************
2029 * LoadCursorFromFileW (USER32.@)
2030 */
2031 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2032 {
2033 TRACE("%s\n", debugstr_w(name));
2034
2035 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2036 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2037 }
2038
2039 /***********************************************************************
2040 * LoadCursorFromFileA (USER32.@)
2041 */
2042 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2043 {
2044 TRACE("%s\n", debugstr_a(name));
2045
2046 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2047 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2048 }
2049
2050 /***********************************************************************
2051 * LoadIconW (USER32.@)
2052 */
2053 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2054 {
2055 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2056
2057 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2058 LR_SHARED | LR_DEFAULTSIZE );
2059 }
2060
2061 /***********************************************************************
2062 * LoadIconA (USER32.@)
2063 */
2064 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2065 {
2066 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2067
2068 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2069 LR_SHARED | LR_DEFAULTSIZE );
2070 }
2071
2072 /**********************************************************************
2073 * GetIconInfo (USER32.@)
2074 */
2075 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2076 {
2077 CURSORICONINFO *ciconinfo;
2078 INT height;
2079
2080 ciconinfo = GlobalLock16(HICON_16(hIcon));
2081 if (!ciconinfo)
2082 return FALSE;
2083
2084 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2085 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2086
2087 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2088 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2089 {
2090 iconinfo->fIcon = TRUE;
2091 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2092 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2093 }
2094 else
2095 {
2096 iconinfo->fIcon = FALSE;
2097 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2098 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2099 }
2100
2101 height = ciconinfo->nHeight;
2102
2103 if (ciconinfo->bBitsPerPixel > 1)
2104 {
2105 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2106 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2107 (char *)(ciconinfo + 1)
2108 + ciconinfo->nHeight *
2109 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2110 }
2111 else
2112 {
2113 iconinfo->hbmColor = 0;
2114 height *= 2;
2115 }
2116
2117 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2118 1, 1, ciconinfo + 1);
2119
2120 GlobalUnlock16(HICON_16(hIcon));
2121
2122 return TRUE;
2123 }
2124
2125 /**********************************************************************
2126 * CreateIconIndirect (USER32.@)
2127 */
2128 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2129 {
2130 DIBSECTION bmpXor;
2131 BITMAP bmpAnd;
2132 HICON16 hObj;
2133 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
2134
2135 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2136 iconinfo->hbmColor, iconinfo->hbmMask,
2137 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2138
2139 if (!iconinfo->hbmMask) return 0;
2140
2141 planes = GetDeviceCaps( screen_dc, PLANES );
2142 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2143
2144 if (iconinfo->hbmColor)
2145 {
2146 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2147 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2148 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2149 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2150 /* we can use either depth 1 or screen depth for xor bitmap */
2151 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2152 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2153 }
2154 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2155 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2156 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2157 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2158
2159 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2160
2161 hObj = GlobalAlloc16( GMEM_MOVEABLE,
2162 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
2163 if (hObj)
2164 {
2165 CURSORICONINFO *info;
2166
2167 info = GlobalLock16( hObj );
2168
2169 /* If we are creating an icon, the hotspot is unused */
2170 if (iconinfo->fIcon)
2171 {
2172 info->ptHotSpot.x = ICON_HOTSPOT;
2173 info->ptHotSpot.y = ICON_HOTSPOT;
2174 }
2175 else
2176 {
2177 info->ptHotSpot.x = iconinfo->xHotspot;
2178 info->ptHotSpot.y = iconinfo->yHotspot;
2179 }
2180
2181 if (iconinfo->hbmColor)
2182 {
2183 info->nWidth = bmpXor.dsBm.bmWidth;
2184 info->nHeight = bmpXor.dsBm.bmHeight;
2185 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2186 info->bPlanes = planes;
2187 info->bBitsPerPixel = bpp;
2188 }
2189 else
2190 {
2191 info->nWidth = bmpAnd.bmWidth;
2192 info->nHeight = bmpAnd.bmHeight / 2;
2193 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2194 info->bPlanes = 1;
2195 info->bBitsPerPixel = 1;
2196 }
2197
2198 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2199
2200 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2201 if (bmpAnd.bmBitsPixel == 1)
2202 {
2203 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2204 }
2205 else
2206 {
2207 HDC hdc, hdc_mem;
2208 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2209
2210 hdc = GetDC( 0 );
2211 hdc_mem = CreateCompatibleDC( hdc );
2212
2213 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2214
2215 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2216 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2217
2218 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2219
2220 SelectObject( hdc, hbmp_old );
2221 SelectObject( hdc_mem, hbmp_mem_old );
2222
2223 DeleteDC( hdc_mem );
2224 ReleaseDC( 0, hdc );
2225
2226 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2227 DeleteObject( hbmp_mono );
2228 }
2229
2230 if (iconinfo->hbmColor)
2231 {
2232 char *dst_bits = (char*)(info + 1) + sizeAnd;
2233
2234 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2235 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2236 else
2237 {
2238 BITMAPINFO bminfo;
2239 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2240 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2241
2242 bminfo.bmiHeader.biSize = sizeof(bminfo);
2243 bminfo.bmiHeader.biWidth = info->nWidth;
2244 bminfo.bmiHeader.biHeight = info->nHeight;
2245 bminfo.bmiHeader.biPlanes = info->bPlanes;
2246 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2247 bminfo.bmiHeader.biCompression = BI_RGB;
2248 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2249 bminfo.bmiHeader.biXPelsPerMeter = 0;
2250 bminfo.bmiHeader.biYPelsPerMeter = 0;
2251 bminfo.bmiHeader.biClrUsed = 0;
2252 bminfo.bmiHeader.biClrImportant = 0;
2253
2254 /* swap lines for dib sections */
2255 if (xor_objsize == sizeof(DIBSECTION))
2256 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2257
2258 if (dib_width != bitmap_width) /* need to fixup alignment */
2259 {
2260 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2261
2262 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2263 src_bits, &bminfo, DIB_RGB_COLORS ))
2264 {
2265 int y;
2266 for (y = 0; y < info->nHeight; y++)
2267 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2268 }
2269 HeapFree( GetProcessHeap(), 0, src_bits );
2270 }
2271 else
2272 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2273 dst_bits, &bminfo, DIB_RGB_COLORS );
2274 }
2275 }
2276 GlobalUnlock16( hObj );
2277 }
2278 return HICON_32(hObj);
2279 }
2280
2281 /******************************************************************************
2282 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2283 *
2284 * NOTES
2285 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2286 *
2287 * PARAMS
2288 * hdc [I] Handle to device context
2289 * x0 [I] X coordinate of upper left corner
2290 * y0 [I] Y coordinate of upper left corner
2291 * hIcon [I] Handle to icon to draw
2292 * cxWidth [I] Width of icon
2293 * cyWidth [I] Height of icon
2294 * istep [I] Index of frame in animated cursor
2295 * hbr [I] Handle to background brush
2296 * flags [I] Icon-drawing flags
2297 *
2298 * RETURNS
2299 * Success: TRUE
2300 * Failure: FALSE
2301 */
2302 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2303 INT cxWidth, INT cyWidth, UINT istep,
2304 HBRUSH hbr, UINT flags )
2305 {
2306 CURSORICONINFO *ptr;
2307 HDC hDC_off = 0, hMemDC;
2308 BOOL result = FALSE, DoOffscreen;
2309 HBITMAP hB_off = 0, hOld = 0;
2310 unsigned char *xorBitmapBits;
2311 unsigned int xorLength;
2312 BOOL has_alpha = FALSE;
2313
2314 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2315 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2316
2317 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
2318 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2319
2320 if (istep)
2321 FIXME_(icon)("Ignoring istep=%d\n", istep);
2322 if (flags & DI_NOMIRROR)
2323 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2324
2325 xorLength = ptr->nHeight * get_bitmap_width_bytes(
2326 ptr->nWidth, ptr->bBitsPerPixel);
2327 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2328 get_bitmap_width_bytes(ptr->nWidth, 1);
2329
2330 if (flags & DI_IMAGE)
2331 has_alpha = bitmap_has_alpha_channel(
2332 ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2333
2334 /* Calculate the size of the destination image. */
2335 if (cxWidth == 0)
2336 {
2337 if (flags & DI_DEFAULTSIZE)
2338 cxWidth = GetSystemMetrics (SM_CXICON);
2339 else
2340 cxWidth = ptr->nWidth;
2341 }
2342 if (cyWidth == 0)
2343 {
2344 if (flags & DI_DEFAULTSIZE)
2345 cyWidth = GetSystemMetrics (SM_CYICON);
2346 else
2347 cyWidth = ptr->nHeight;
2348 }
2349
2350 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2351
2352 if (DoOffscreen) {
2353 RECT r;
2354
2355 r.left = 0;
2356 r.top = 0;
2357 r.right = cxWidth;
2358 r.bottom = cxWidth;
2359
2360 hDC_off = CreateCompatibleDC(hdc);
2361 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2362 if (hDC_off && hB_off) {
2363 hOld = SelectObject(hDC_off, hB_off);
2364 FillRect(hDC_off, &r, hbr);
2365 }
2366 }
2367
2368 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2369 {
2370 HBITMAP hBitTemp;
2371 HBITMAP hXorBits = NULL, hAndBits = NULL;
2372 COLORREF oldFg, oldBg;
2373 INT nStretchMode;
2374
2375 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2376
2377 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2378 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2379
2380 if (((flags & DI_MASK) && !(flags & DI_IMAGE)) ||
2381 ((flags & DI_MASK) && !has_alpha))
2382 {
2383 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2384 if (hAndBits)
2385 {
2386 hBitTemp = SelectObject( hMemDC, hAndBits );
2387 if (DoOffscreen)
2388 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2389 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2390 else
2391 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2392 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2393 SelectObject( hMemDC, hBitTemp );
2394 }
2395 }
2396
2397 if (flags & DI_IMAGE)
2398 {
2399 BITMAPINFOHEADER bmih;
2400 unsigned char *dibBits;
2401
2402 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
2403 bmih.biSize = sizeof(BITMAPINFOHEADER);
2404 bmih.biWidth = ptr->nWidth;
2405 bmih.biHeight = -ptr->nHeight;
2406 bmih.biPlanes = ptr->bPlanes;
2407 bmih.biBitCount = ptr->bBitsPerPixel;
2408 bmih.biCompression = BI_RGB;
2409
2410 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
2411 (void*)&dibBits, NULL, 0);
2412
2413 if (hXorBits && dibBits)
2414 {
2415 if(has_alpha)
2416 {
2417 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2418
2419 /* Do the alpha blending render */
2420 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2421 hBitTemp = SelectObject( hMemDC, hXorBits );
2422
2423 if (DoOffscreen)
2424 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2425 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2426 else
2427 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2428 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2429
2430 SelectObject( hMemDC, hBitTemp );
2431 }
2432 else
2433 {
2434 memcpy(dibBits, xorBitmapBits, xorLength);
2435 hBitTemp = SelectObject( hMemDC, hXorBits );
2436 if (DoOffscreen)
2437 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2438 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2439 else
2440 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2441 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2442 SelectObject( hMemDC, hBitTemp );
2443 }
2444
2445 DeleteObject( hXorBits );
2446 }
2447 }
2448
2449 result = TRUE;
2450
2451 SetTextColor( hdc, oldFg );
2452 SetBkColor( hdc, oldBg );
2453
2454 if (hAndBits) DeleteObject( hAndBits );
2455 SetStretchBltMode (hdc, nStretchMode);
2456 if (DoOffscreen) {
2457 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2458 SelectObject(hDC_off, hOld);
2459 }
2460 }
2461 if (hMemDC) DeleteDC( hMemDC );
2462 if (hDC_off) DeleteDC(hDC_off);
2463 if (hB_off) DeleteObject(hB_off);
2464 GlobalUnlock16(HICON_16(hIcon));
2465 return result;
2466 }
2467
2468 /***********************************************************************
2469 * DIB_FixColorsToLoadflags
2470 *
2471 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2472 * are in loadflags
2473 */
2474 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2475 {
2476 int colors;
2477 COLORREF c_W, c_S, c_F, c_L, c_C;
2478 int incr,i;
2479 RGBQUAD *ptr;
2480 int bitmap_type;
2481 LONG width;
2482 LONG height;
2483 WORD bpp;
2484 DWORD compr;
2485
2486 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2487 {
2488 WARN_(resource)("Invalid bitmap\n");
2489 return;
2490 }
2491
2492 if (bpp > 8) return;
2493
2494 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2495 {
2496 incr = 3;
2497 colors = 1 << bpp;
2498 }
2499 else
2500 {
2501 incr = 4;
2502 colors = bmi->bmiHeader.biClrUsed;
2503 if (colors > 256) colors = 256;
2504 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2505 }
2506
2507 c_W = GetSysColor(COLOR_WINDOW);
2508 c_S = GetSysColor(COLOR_3DSHADOW);
2509 c_F = GetSysColor(COLOR_3DFACE);
2510 c_L = GetSysColor(COLOR_3DLIGHT);
2511
2512 if (loadflags & LR_LOADTRANSPARENT) {
2513 switch (bpp) {
2514 case 1: pix = pix >> 7; break;
2515 case 4: pix = pix >> 4; break;
2516 case 8: break;
2517 default:
2518 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2519 return;
2520 }
2521 if (pix >= colors) {
2522 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2523 return;
2524 }
2525 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2526 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2527 ptr->rgbBlue = GetBValue(c_W);
2528 ptr->rgbGreen = GetGValue(c_W);
2529 ptr->rgbRed = GetRValue(c_W);
2530 }
2531 if (loadflags & LR_LOADMAP3DCOLORS)
2532 for (i=0; i<colors; i++) {
2533 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2534 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2535 if (c_C == RGB(128, 128, 128)) {
2536 ptr->rgbRed = GetRValue(c_S);
2537 ptr->rgbGreen = GetGValue(c_S);
2538 ptr->rgbBlue = GetBValue(c_S);
2539 } else if (c_C == RGB(192, 192, 192)) {
2540 ptr->rgbRed = GetRValue(c_F);
2541 ptr->rgbGreen = GetGValue(c_F);
2542 ptr->rgbBlue = GetBValue(c_F);
2543 } else if (c_C == RGB(223, 223, 223)) {
2544 ptr->rgbRed = GetRValue(c_L);
2545 ptr->rgbGreen = GetGValue(c_L);
2546 ptr->rgbBlue = GetBValue(c_L);
2547 }
2548 }
2549 }
2550
2551
2552 /**********************************************************************
2553 * BITMAP_Load
2554 */
2555 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2556 INT desiredx, INT desiredy, UINT loadflags )
2557 {
2558 HBITMAP hbitmap = 0, orig_bm;
2559 HRSRC hRsrc;
2560 HGLOBAL handle;
2561 char *ptr = NULL;
2562 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2563 int size;
2564 BYTE pix;
2565 char *bits;
2566 LONG width, height, new_width, new_height;
2567 WORD bpp_dummy;
2568 DWORD compr_dummy;
2569 INT bm_type;
2570 HDC screen_mem_dc = NULL;
2571
2572 if (!(loadflags & LR_LOADFROMFILE))
2573 {
2574 if (!instance)
2575 {
2576 /* OEM bitmap: try to load the resource from user32.dll */
2577 instance = user32_module;
2578 }
2579
2580 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2581 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2582
2583 if ((info = LockResource( handle )) == NULL) return 0;
2584 }
2585 else
2586 {
2587 BITMAPFILEHEADER * bmfh;
2588
2589 if (!(ptr = map_fileW( name, NULL ))) return 0;
2590 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2591 bmfh = (BITMAPFILEHEADER *)ptr;
2592 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2593 bmfh->bfReserved1 == 0 &&
2594 bmfh->bfReserved2 == 0))
2595 {
2596 WARN("Invalid/unsupported bitmap format!\n");
2597 UnmapViewOfFile( ptr );
2598 return 0;
2599 }
2600 }
2601
2602 size = bitmap_info_size(info, DIB_RGB_COLORS);
2603 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2604 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2605
2606 if (!fix_info || !scaled_info) goto end;
2607 memcpy(fix_info, info, size);
2608
2609 pix = *((LPBYTE)info + size);
2610 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2611
2612 memcpy(scaled_info, fix_info, size);
2613 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2614 &bpp_dummy, &compr_dummy);
2615 if(desiredx != 0)
2616 new_width = desiredx;
2617 else
2618 new_width = width;
2619
2620 if(desiredy != 0)
2621 new_height = height > 0 ? desiredy : -desiredy;
2622 else
2623 new_height = height;
2624
2625 if(bm_type == 0)
2626 {
2627 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2628 core->bcWidth = new_width;
2629 core->bcHeight = new_height;
2630 }
2631 else
2632 {
2633 scaled_info->bmiHeader.biWidth = new_width;
2634 scaled_info->bmiHeader.biHeight = new_height;
2635 }
2636
2637 if (new_height < 0) new_height = -new_height;
2638
2639 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2640 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2641
2642 bits = (char *)info + size;
2643
2644 if (loadflags & LR_CREATEDIBSECTION)
2645 {
2646 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2647 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2648 }
2649 else
2650 {
2651 if (is_dib_monochrome(fix_info))
2652 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2653 else
2654 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2655 }
2656
2657 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2658 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2659 SelectObject(screen_mem_dc, orig_bm);
2660
2661 end:
2662 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2663 HeapFree(GetProcessHeap(), 0, scaled_info);
2664 HeapFree(GetProcessHeap(), 0, fix_info);
2665 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2666
2667 return hbitmap;
2668 }
2669
2670 /**********************************************************************
2671 * LoadImageA (USER32.@)
2672 *
2673 * See LoadImageW.
2674 */
2675 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2676 INT desiredx, INT desiredy, UINT loadflags)
2677 {
2678 HANDLE res;
2679 LPWSTR u_name;
2680
2681 if (!HIWORD(name))
2682 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2683
2684 __TRY {
2685 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2686 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2687 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2688 }
2689 __EXCEPT_PAGE_FAULT {
2690 SetLastError( ERROR_INVALID_PARAMETER );
2691 return 0;
2692 }
2693 __ENDTRY
2694 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2695 HeapFree(GetProcessHeap(), 0, u_name);
2696 return res;
2697 }
2698
2699
2700 /******************************************************************************
2701 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2702 *
2703 * PARAMS
2704 * hinst [I] Handle of instance that contains image
2705 * name [I] Name of image
2706 * type [I] Type of image
2707 * desiredx [I] Desired width
2708 * desiredy [I] Desired height
2709 * loadflags [I] Load flags
2710 *
2711 * RETURNS
2712 * Success: Handle to newly loaded image
2713 * Failure: NULL
2714 *
2715 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2716 */
2717 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2718 INT desiredx, INT desiredy, UINT loadflags )
2719 {
2720 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2721 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2722
2723 if (loadflags & LR_DEFAULTSIZE) {
2724 if (type == IMAGE_ICON) {
2725 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2726 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2727 } else if (type == IMAGE_CURSOR) {
2728 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2729 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2730 }
2731 }
2732 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2733 switch (type) {
2734 case IMAGE_BITMAP:
2735 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2736
2737 case IMAGE_ICON:
2738 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2739 if (screen_dc)
2740 {
2741 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2742 if (palEnts == 0) palEnts = 256;
2743 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2744 palEnts, FALSE, loadflags);
2745 }
2746 break;
2747
2748 case IMAGE_CURSOR:
2749 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2750 1, TRUE, loadflags);
2751 }
2752 return 0;
2753 }
2754
2755 /******************************************************************************
2756 * CopyImage (USER32.@) Creates new image and copies attributes to it
2757 *
2758 * PARAMS
2759 * hnd [I] Handle to image to copy
2760 * type [I] Type of image to copy
2761 * desiredx [I] Desired width of new image
2762 * desiredy [I] Desired height of new image
2763 * flags [I] Copy flags
2764 *
2765 * RETURNS
2766 * Success: Handle to newly created image
2767 * Failure: NULL
2768 *
2769 * BUGS
2770 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2771 * all other versions (95/2000/XP have been tested) ignore it.
2772 *
2773 * NOTES
2774 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2775 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2776 * the copy will have the same depth as the screen.
2777 * The content of the image will only be copied if the bit depth of the
2778 * original image is compatible with the bit depth of the screen, or
2779 * if the source is a DIB section.
2780 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2781 */
2782 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2783 INT desiredy, UINT flags )
2784 {
2785 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2786 hnd, type, desiredx, desiredy, flags);
2787
2788 switch (type)
2789 {
2790 case IMAGE_BITMAP:
2791 {
2792 HBITMAP res = NULL;
2793 DIBSECTION ds;
2794 int objSize;
2795 BITMAPINFO * bi;
2796
2797 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2798 if (!objSize) return 0;
2799 if ((desiredx < 0) || (desiredy < 0)) return 0;
2800
2801 if (flags & LR_COPYFROMRESOURCE)
2802 {
2803 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2804 }
2805
2806 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2807 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2808
2809 /* Allocate memory for a BITMAPINFOHEADER structure and a
2810 color table. The maximum number of colors in a color table
2811 is 256 which corresponds to a bitmap with depth 8.
2812 Bitmaps with higher depths don't have color tables. */
2813 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2814 if (!bi) return 0;
2815
2816 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2817 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2818 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2819 bi->bmiHeader.biCompression = BI_RGB;
2820
2821 if (flags & LR_CREATEDIBSECTION)
2822 {
2823 /* Create a DIB section. LR_MONOCHROME is ignored */
2824 void * bits;
2825 HDC dc = CreateCompatibleDC(NULL);
2826
2827 if (objSize == sizeof(DIBSECTION))
2828 {
2829 /* The source bitmap is a DIB.
2830 Get its attributes to create an exact copy */
2831 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2832 }
2833
2834 /* Get the color table or the color masks */
2835 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2836
2837 bi->bmiHeader.biWidth = desiredx;
2838 bi->bmiHeader.biHeight = desiredy;
2839 bi->bmiHeader.biSizeImage = 0;
2840
2841 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2842 DeleteDC(dc);
2843 }
2844 else
2845 {
2846 /* Create a device-dependent bitmap */
2847
2848 BOOL monochrome = (flags & LR_MONOCHROME);
2849
2850 if (objSize == sizeof(DIBSECTION))
2851 {
2852 /* The source bitmap is a DIB section.
2853 Get its attributes */
2854 HDC dc = CreateCompatibleDC(NULL);
2855 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2856 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2857 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2858 DeleteDC(dc);
2859
2860 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2861 {
2862 /* Look if the colors of the DIB are black and white */
2863
2864 monochrome =
2865 (bi->bmiColors[0].rgbRed == 0xff
2866 && bi->bmiColors[0].rgbGreen == 0xff
2867 && bi->bmiColors[0].rgbBlue == 0xff
2868 && bi->bmiColors[0].rgbReserved == 0
2869 && bi->bmiColors[1].rgbRed == 0
2870 && bi->bmiColors[1].rgbGreen == 0
2871 && bi->bmiColors[1].rgbBlue == 0
2872 && bi->bmiColors[1].rgbReserved == 0)
2873 ||
2874 (bi->bmiColors[0].rgbRed == 0
2875 && bi->bmiColors[0].rgbGreen == 0
2876 && bi->bmiColors[0].rgbBlue == 0
2877 && bi->bmiColors[0].rgbReserved == 0
2878 && bi->bmiColors[1].rgbRed == 0xff
2879 && bi->bmiColors[1].rgbGreen == 0xff
2880 && bi->bmiColors[1].rgbBlue == 0xff
2881 && bi->bmiColors[1].rgbReserved == 0);
2882 }
2883 }
2884 else if (!monochrome)
2885 {
2886 monochrome = ds.dsBm.bmBitsPixel == 1;
2887 }
2888
2889 if (monochrome)
2890 {
2891 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2892 }
2893 else
2894 {
2895 HDC screenDC = GetDC(NULL);
2896 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2897 ReleaseDC(NULL, screenDC);
2898 }
2899 }
2900
2901 if (res)
2902 {
2903 /* Only copy the bitmap if it's a DIB section or if it's
2904 compatible to the screen */
2905 BOOL copyContents;
2906
2907 if (objSize == sizeof(DIBSECTION))
2908 {
2909 copyContents = TRUE;
2910 }
2911 else
2912 {
2913 HDC screenDC = GetDC(NULL);
2914 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2915 ReleaseDC(NULL, screenDC);
2916
2917 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2918 }
2919
2920 if (copyContents)
2921 {
2922 /* The source bitmap may already be selected in a device context,
2923 use GetDIBits/StretchDIBits and not StretchBlt */
2924
2925 HDC dc;
2926 void * bits;
2927
2928 dc = CreateCompatibleDC(NULL);
2929
2930 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2931 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2932 bi->bmiHeader.biSizeImage = 0;
2933 bi->bmiHeader.biClrUsed = 0;
2934 bi->bmiHeader.biClrImportant = 0;
2935
2936 /* Fill in biSizeImage */
2937 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2938 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2939
2940 if (bits)
2941 {
2942 HBITMAP oldBmp;
2943
2944 /* Get the image bits of the source bitmap */
2945 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2946
2947 /* Copy it to the destination bitmap */
2948 oldBmp = SelectObject(dc, res);
2949 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2950 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2951 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2952 SelectObject(dc, oldBmp);
2953
2954 HeapFree(GetProcessHeap(), 0, bits);
2955 }
2956
2957 DeleteDC(dc);
2958 }
2959
2960 if (flags & LR_COPYDELETEORG)
2961 {
2962 DeleteObject(hnd);
2963 }
2964 }
2965 HeapFree(GetProcessHeap(), 0, bi);
2966 return res;
2967 }
2968 case IMAGE_ICON:
2969 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2970 case IMAGE_CURSOR:
2971 /* Should call CURSORICON_ExtCopy but more testing
2972 * needs to be done before we change this
2973 */
2974 if (flags) FIXME("Flags are ignored\n");
2975 return CopyCursor(hnd);
2976 }
2977 return 0;
2978 }
2979
2980
2981 /******************************************************************************
2982 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2983 *
2984 * RETURNS
2985 * Success: Handle to specified bitmap
2986 * Failure: NULL
2987 */
2988 HBITMAP WINAPI LoadBitmapW(
2989 HINSTANCE instance, /* [in] Handle to application instance */
2990 LPCWSTR name) /* [in] Address of bitmap resource name */
2991 {
2992 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2993 }
2994
2995 /**********************************************************************
2996 * LoadBitmapA (USER32.@)
2997 *
2998 * See LoadBitmapW.
2999 */
3000 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3001 {
3002 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3003 }
3004
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.