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 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
663 POINT16 hotspot, BOOL bIcon,
664 DWORD dwVersion,
665 INT width, INT height,
666 UINT cFlag )
667 {
668 HGLOBAL16 hObj;
669 static HDC hdcMem;
670 int sizeAnd, sizeXor;
671 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
672 BITMAP bmpXor, bmpAnd;
673 BOOL DoStretch;
674 INT size;
675
676 if (dwVersion == 0x00020000)
677 {
678 FIXME_(cursor)("\t2.xx resources are not supported\n");
679 return 0;
680 }
681
682 /* Check bitmap header */
683
684 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
685 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
686 bmi->bmiHeader.biCompression != BI_RGB) )
687 {
688 WARN_(cursor)("\tinvalid resource bitmap header.\n");
689 return 0;
690 }
691
692 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
693
694 if (!width) width = bmi->bmiHeader.biWidth;
695 if (!height) height = bmi->bmiHeader.biHeight/2;
696 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
697 (bmi->bmiHeader.biWidth != width);
698
699 /* Scale the hotspot */
700 if (DoStretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
701 {
702 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
703 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
704 }
705
706 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
707 if (screen_dc)
708 {
709 BITMAPINFO* pInfo;
710
711 /* Make sure we have room for the monochrome bitmap later on.
712 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
713 * up to and including the biBitCount. In-memory icon resource
714 * format is as follows:
715 *
716 * BITMAPINFOHEADER icHeader // DIB header
717 * RGBQUAD icColors[] // Color table
718 * BYTE icXOR[] // DIB bits for XOR mask
719 * BYTE icAND[] // DIB bits for AND mask
720 */
721
722 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
723 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
724 {
725 memcpy( pInfo, bmi, size );
726 pInfo->bmiHeader.biHeight /= 2;
727
728 /* Create the XOR bitmap */
729
730 if (DoStretch) {
731 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
732 if(hXorBits)
733 {
734 HBITMAP hOld;
735 BOOL res = FALSE;
736
737 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
738 if (hdcMem) {
739 hOld = SelectObject(hdcMem, hXorBits);
740 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
741 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
742 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
743 SelectObject(hdcMem, hOld);
744 }
745 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
746 }
747 } else {
748 if (is_dib_monochrome(bmi)) {
749 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
750 SetDIBits(screen_dc, hXorBits, 0, height,
751 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
752 }
753 else
754 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
755 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
756 }
757
758 if( hXorBits )
759 {
760 char* xbits = (char *)bmi + size +
761 get_dib_width_bytes( bmi->bmiHeader.biWidth,
762 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
763
764 pInfo->bmiHeader.biBitCount = 1;
765 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
766 {
767 RGBQUAD *rgb = pInfo->bmiColors;
768
769 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
770 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
771 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
772 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
773 }
774 else
775 {
776 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
777
778 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
779 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
780 }
781
782 /* Create the AND bitmap */
783
784 if (DoStretch) {
785 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
786 HBITMAP hOld;
787 BOOL res = FALSE;
788
789 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
790 if (hdcMem) {
791 hOld = SelectObject(hdcMem, hAndBits);
792 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
793 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
794 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
795 SelectObject(hdcMem, hOld);
796 }
797 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
798 }
799 } else {
800 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
801
802 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
803 xbits, pInfo, DIB_RGB_COLORS);
804
805 }
806 if( !hAndBits ) DeleteObject( hXorBits );
807 }
808 HeapFree( GetProcessHeap(), 0, pInfo );
809 }
810 }
811
812 if( !hXorBits || !hAndBits )
813 {
814 WARN_(cursor)("\tunable to create an icon bitmap.\n");
815 return 0;
816 }
817
818 /* Now create the CURSORICONINFO structure */
819 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
820 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
821 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
822 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
823
824 hObj = GlobalAlloc16( GMEM_MOVEABLE,
825 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
826 if (hObj)
827 {
828 CURSORICONINFO *info;
829
830 info = GlobalLock16( hObj );
831 info->ptHotSpot.x = hotspot.x;
832 info->ptHotSpot.y = hotspot.y;
833 info->nWidth = bmpXor.bmWidth;
834 info->nHeight = bmpXor.bmHeight;
835 info->nWidthBytes = bmpXor.bmWidthBytes;
836 info->bPlanes = bmpXor.bmPlanes;
837 info->bBitsPerPixel = bmpXor.bmBitsPixel;
838
839 /* Transfer the bitmap bits to the CURSORICONINFO structure */
840
841 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
842 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
843 GlobalUnlock16( hObj );
844 }
845
846 DeleteObject( hAndBits );
847 DeleteObject( hXorBits );
848 return HICON_32(hObj);
849 }
850
851
852 /**********************************************************************
853 * .ANI cursor support
854 */
855 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
856 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
857 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
858
859 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
860 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
861 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
862 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
863 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
864 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
865
866 #define ANI_FLAG_ICON 0x1
867 #define ANI_FLAG_SEQUENCE 0x2
868
869 typedef struct {
870 DWORD header_size;
871 DWORD num_frames;
872 DWORD num_steps;
873 DWORD width;
874 DWORD height;
875 DWORD bpp;
876 DWORD num_planes;
877 DWORD display_rate;
878 DWORD flags;
879 } ani_header;
880
881 typedef struct {
882 DWORD data_size;
883 const unsigned char *data;
884 } riff_chunk_t;
885
886 static void dump_ani_header( const ani_header *header )
887 {
888 TRACE(" header size: %d\n", header->header_size);
889 TRACE(" frames: %d\n", header->num_frames);
890 TRACE(" steps: %d\n", header->num_steps);
891 TRACE(" width: %d\n", header->width);
892 TRACE(" height: %d\n", header->height);
893 TRACE(" bpp: %d\n", header->bpp);
894 TRACE(" planes: %d\n", header->num_planes);
895 TRACE(" display rate: %d\n", header->display_rate);
896 TRACE(" flags: 0x%08x\n", header->flags);
897 }
898
899
900 /*
901 * RIFF:
902 * DWORD "RIFF"
903 * DWORD size
904 * DWORD riff_id
905 * BYTE[] data
906 *
907 * LIST:
908 * DWORD "LIST"
909 * DWORD size
910 * DWORD list_id
911 * BYTE[] data
912 *
913 * CHUNK:
914 * DWORD chunk_id
915 * DWORD size
916 * BYTE[] data
917 */
918 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
919 {
920 const unsigned char *ptr = parent_chunk->data;
921 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
922
923 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
924
925 while (ptr < end)
926 {
927 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
928 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
929 {
930 ptr += sizeof(DWORD);
931 chunk->data_size = *(DWORD *)ptr;
932 ptr += sizeof(DWORD);
933 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
934 chunk->data = ptr;
935
936 return;
937 }
938
939 ptr += sizeof(DWORD);
940 ptr += *(DWORD *)ptr;
941 ptr += sizeof(DWORD);
942 }
943 }
944
945
946 /*
947 * .ANI layout:
948 *
949 * RIFF:'ACON' RIFF chunk
950 * |- CHUNK:'anih' Header
951 * |- CHUNK:'seq ' Sequence information (optional)
952 * \- LIST:'fram' Frame list
953 * |- CHUNK:icon Cursor frames
954 * |- CHUNK:icon
955 * |- ...
956 * \- CHUNK:icon
957 */
958 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
959 INT width, INT height, INT colors )
960 {
961 HCURSOR cursor;
962 ani_header header = {0};
963 LPBYTE frame_bits = 0;
964 POINT16 hotspot;
965 CURSORICONFILEDIRENTRY *entry;
966
967 riff_chunk_t root_chunk = { bits_size, bits };
968 riff_chunk_t ACON_chunk = {0};
969 riff_chunk_t anih_chunk = {0};
970 riff_chunk_t fram_chunk = {0};
971 const unsigned char *icon_data;
972
973 TRACE("bits %p, bits_size %d\n", bits, bits_size);
974
975 if (!bits) return 0;
976
977 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
978 if (!ACON_chunk.data)
979 {
980 ERR("Failed to get root chunk.\n");
981 return 0;
982 }
983
984 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
985 if (!anih_chunk.data)
986 {
987 ERR("Failed to get 'anih' chunk.\n");
988 return 0;
989 }
990 memcpy( &header, anih_chunk.data, sizeof(header) );
991 dump_ani_header( &header );
992
993 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
994 if (!fram_chunk.data)
995 {
996 ERR("Failed to get icon list.\n");
997 return 0;
998 }
999
1000 /* FIXME: For now, just load the first frame. Before we can load all the
1001 * frames, we need to write the needed code in wineserver, etc. to handle
1002 * cursors. Once this code is written, we can extend it to support .ani
1003 * cursors and then update user32 and winex11.drv to load all frames.
1004 *
1005 * Hopefully this will at least make some games (C&C3, etc.) more playable
1006 * in the meantime.
1007 */
1008 FIXME("Loading all frames for .ani cursors not implemented.\n");
1009 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1010
1011 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1012 width, height, colors );
1013
1014 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1015 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1016
1017 if (!header.width || !header.height)
1018 {
1019 header.width = entry->bWidth;
1020 header.height = entry->bHeight;
1021 }
1022
1023 hotspot.x = entry->xHotspot;
1024 hotspot.y = entry->yHotspot;
1025
1026 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1027 FALSE, 0x00030000, header.width, header.height, 0 );
1028
1029 HeapFree( GetProcessHeap(), 0, frame_bits );
1030
1031 return cursor;
1032 }
1033
1034
1035 /**********************************************************************
1036 * CreateIconFromResourceEx (USER32.@)
1037 *
1038 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1039 * with cbSize parameter as well.
1040 */
1041 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1042 BOOL bIcon, DWORD dwVersion,
1043 INT width, INT height,
1044 UINT cFlag )
1045 {
1046 POINT16 hotspot;
1047 BITMAPINFO *bmi;
1048
1049 hotspot.x = ICON_HOTSPOT;
1050 hotspot.y = ICON_HOTSPOT;
1051
1052 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1053 bits, cbSize, dwVersion, width, height,
1054 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1055
1056 if (bIcon)
1057 bmi = (BITMAPINFO *)bits;
1058 else /* get the hotspot */
1059 {
1060 POINT16 *pt = (POINT16 *)bits;
1061 hotspot = *pt;
1062 bmi = (BITMAPINFO *)(pt + 1);
1063 }
1064
1065 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1066 width, height, cFlag );
1067 }
1068
1069
1070 /**********************************************************************
1071 * CreateIconFromResource (USER32.@)
1072 */
1073 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1074 BOOL bIcon, DWORD dwVersion)
1075 {
1076 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1077 }
1078
1079
1080 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1081 INT width, INT height, INT colors,
1082 BOOL fCursor, UINT loadflags)
1083 {
1084 CURSORICONFILEDIRENTRY *entry;
1085 CURSORICONFILEDIR *dir;
1086 DWORD filesize = 0;
1087 HICON hIcon = 0;
1088 LPBYTE bits;
1089 POINT16 hotspot;
1090
1091 TRACE("loading %s\n", debugstr_w( filename ));
1092
1093 bits = map_fileW( filename, &filesize );
1094 if (!bits)
1095 return hIcon;
1096
1097 /* Check for .ani. */
1098 if (memcmp( bits, "RIFF", 4 ) == 0)
1099 {
1100 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1101 colors );
1102 goto end;
1103 }
1104
1105 dir = (CURSORICONFILEDIR*) bits;
1106 if ( filesize < sizeof(*dir) )
1107 goto end;
1108
1109 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1110 goto end;
1111
1112 if ( fCursor )
1113 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1114 else
1115 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1116
1117 if ( !entry )
1118 goto end;
1119
1120 /* check that we don't run off the end of the file */
1121 if ( entry->dwDIBOffset > filesize )
1122 goto end;
1123 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1124 goto end;
1125
1126 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1127 if ( fCursor )
1128 {
1129 hotspot.x = entry->xHotspot;
1130 hotspot.y = entry->yHotspot;
1131 }
1132 else
1133 {
1134 hotspot.x = ICON_HOTSPOT;
1135 hotspot.y = ICON_HOTSPOT;
1136 }
1137 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1138 hotspot, !fCursor, 0x00030000,
1139 width, height, loadflags );
1140 end:
1141 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1142 UnmapViewOfFile( bits );
1143 return hIcon;
1144 }
1145
1146 /**********************************************************************
1147 * CURSORICON_Load
1148 *
1149 * Load a cursor or icon from resource or file.
1150 */
1151 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1152 INT width, INT height, INT colors,
1153 BOOL fCursor, UINT loadflags)
1154 {
1155 HANDLE handle = 0;
1156 HICON hIcon = 0;
1157 HRSRC hRsrc, hGroupRsrc;
1158 CURSORICONDIR *dir;
1159 CURSORICONDIRENTRY *dirEntry;
1160 LPBYTE bits;
1161 WORD wResId;
1162 DWORD dwBytesInRes;
1163
1164 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1165 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1166
1167 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1168 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1169
1170 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1171
1172 /* Normalize hInstance (must be uniquely represented for icon cache) */
1173
1174 if (!HIWORD( hInstance ))
1175 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1176
1177 /* Get directory resource ID */
1178
1179 if (!(hRsrc = FindResourceW( hInstance, name,
1180 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1181 return 0;
1182 hGroupRsrc = hRsrc;
1183
1184 /* Find the best entry in the directory */
1185
1186 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1187 if (!(dir = LockResource( handle ))) return 0;
1188 if (fCursor)
1189 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1190 else
1191 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1192 if (!dirEntry) return 0;
1193 wResId = dirEntry->wResId;
1194 dwBytesInRes = dirEntry->dwBytesInRes;
1195 FreeResource( handle );
1196
1197 /* Load the resource */
1198
1199 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1200 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1201
1202 /* If shared icon, check whether it was already loaded */
1203 if ( (loadflags & LR_SHARED)
1204 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1205 return hIcon;
1206
1207 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1208 bits = LockResource( handle );
1209 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1210 !fCursor, 0x00030000, width, height, loadflags);
1211 FreeResource( handle );
1212
1213 /* If shared icon, add to icon cache */
1214
1215 if ( hIcon && (loadflags & LR_SHARED) )
1216 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1217
1218 return hIcon;
1219 }
1220
1221 /***********************************************************************
1222 * CURSORICON_Copy
1223 *
1224 * Make a copy of a cursor or icon.
1225 */
1226 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1227 {
1228 char *ptrOld, *ptrNew;
1229 int size;
1230 HICON16 hOld = HICON_16(hIcon);
1231 HICON16 hNew;
1232
1233 if (!(ptrOld = GlobalLock16( hOld ))) return 0;
1234 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1235 size = GlobalSize16( hOld );
1236 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1237 FarSetOwner16( hNew, hInst16 );
1238 ptrNew = GlobalLock16( hNew );
1239 memcpy( ptrNew, ptrOld, size );
1240 GlobalUnlock16( hOld );
1241 GlobalUnlock16( hNew );
1242 return HICON_32(hNew);
1243 }
1244
1245 /*************************************************************************
1246 * CURSORICON_ExtCopy
1247 *
1248 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1249 *
1250 * PARAMS
1251 * Handle [I] handle to an Image
1252 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1253 * iDesiredCX [I] The Desired width of the Image
1254 * iDesiredCY [I] The desired height of the Image
1255 * nFlags [I] The flags from CopyImage
1256 *
1257 * RETURNS
1258 * Success: The new handle of the Image
1259 *
1260 * NOTES
1261 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1262 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1263 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1264 *
1265 *
1266 */
1267
1268 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1269 INT iDesiredCX, INT iDesiredCY,
1270 UINT nFlags)
1271 {
1272 HICON hNew=0;
1273
1274 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1275 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1276
1277 if(hIcon == 0)
1278 {
1279 return 0;
1280 }
1281
1282 /* Best Fit or Monochrome */
1283 if( (nFlags & LR_COPYFROMRESOURCE
1284 && (iDesiredCX > 0 || iDesiredCY > 0))
1285 || nFlags & LR_MONOCHROME)
1286 {
1287 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1288
1289 /* Not Found in Cache, then do a straight copy
1290 */
1291 if(pIconCache == NULL)
1292 {
1293 hNew = CURSORICON_Copy(0, hIcon);
1294 if(nFlags & LR_COPYFROMRESOURCE)
1295 {
1296 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1297 }
1298 }
1299 else
1300 {
1301 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1302 LPBYTE pBits;
1303 HANDLE hMem;
1304 HRSRC hRsrc;
1305 DWORD dwBytesInRes;
1306 WORD wResId;
1307 CURSORICONDIR *pDir;
1308 CURSORICONDIRENTRY *pDirEntry;
1309 BOOL bIsIcon = (nType == IMAGE_ICON);
1310
1311 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1312 */
1313 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1314 || (iDesiredCX == 0 && iDesiredCY == 0))
1315 {
1316 iDesiredCY = GetSystemMetrics(bIsIcon ?
1317 SM_CYICON : SM_CYCURSOR);
1318 iDesiredCX = GetSystemMetrics(bIsIcon ?
1319 SM_CXICON : SM_CXCURSOR);
1320 }
1321
1322 /* Retrieve the CURSORICONDIRENTRY
1323 */
1324 if (!(hMem = LoadResource( pIconCache->hModule ,
1325 pIconCache->hGroupRsrc)))
1326 {
1327 return 0;
1328 }
1329 if (!(pDir = LockResource( hMem )))
1330 {
1331 return 0;
1332 }
1333
1334 /* Find Best Fit
1335 */
1336 if(bIsIcon)
1337 {
1338 pDirEntry = CURSORICON_FindBestIconRes(
1339 pDir, iDesiredCX, iDesiredCY, 256 );
1340 }
1341 else
1342 {
1343 pDirEntry = CURSORICON_FindBestCursorRes(
1344 pDir, iDesiredCX, iDesiredCY, 1);
1345 }
1346
1347 wResId = pDirEntry->wResId;
1348 dwBytesInRes = pDirEntry->dwBytesInRes;
1349 FreeResource(hMem);
1350
1351 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1352 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1353 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1354
1355 /* Get the Best Fit
1356 */
1357 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1358 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1359 {
1360 return 0;
1361 }
1362 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1363 {
1364 return 0;
1365 }
1366
1367 pBits = LockResource( hMem );
1368
1369 if(nFlags & LR_DEFAULTSIZE)
1370 {
1371 iTargetCY = GetSystemMetrics(SM_CYICON);
1372 iTargetCX = GetSystemMetrics(SM_CXICON);
1373 }
1374
1375 /* Create a New Icon with the proper dimension
1376 */
1377 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1378 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1379 FreeResource(hMem);
1380 }
1381 }
1382 else hNew = CURSORICON_Copy(0, hIcon);
1383 return hNew;
1384 }
1385
1386
1387 /***********************************************************************
1388 * CreateCursor (USER32.@)
1389 */
1390 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1391 INT xHotSpot, INT yHotSpot,
1392 INT nWidth, INT nHeight,
1393 LPCVOID lpANDbits, LPCVOID lpXORbits )
1394 {
1395 CURSORICONINFO info;
1396
1397 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1398 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1399
1400 info.ptHotSpot.x = xHotSpot;
1401 info.ptHotSpot.y = yHotSpot;
1402 info.nWidth = nWidth;
1403 info.nHeight = nHeight;
1404 info.nWidthBytes = 0;
1405 info.bPlanes = 1;
1406 info.bBitsPerPixel = 1;
1407
1408 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1409 }
1410
1411
1412 /***********************************************************************
1413 * CreateIcon (USER.407)
1414 */
1415 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1416 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1417 LPCVOID lpANDbits, LPCVOID lpXORbits )
1418 {
1419 CURSORICONINFO info;
1420
1421 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1422 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1423
1424 info.ptHotSpot.x = ICON_HOTSPOT;
1425 info.ptHotSpot.y = ICON_HOTSPOT;
1426 info.nWidth = nWidth;
1427 info.nHeight = nHeight;
1428 info.nWidthBytes = 0;
1429 info.bPlanes = bPlanes;
1430 info.bBitsPerPixel = bBitsPixel;
1431
1432 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1433 }
1434
1435
1436 /***********************************************************************
1437 * CreateIcon (USER32.@)
1438 *
1439 * Creates an icon based on the specified bitmaps. The bitmaps must be
1440 * provided in a device dependent format and will be resized to
1441 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1442 * depth. The provided bitmaps must be top-down bitmaps.
1443 * Although Windows does not support 15bpp(*) this API must support it
1444 * for Winelib applications.
1445 *
1446 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1447 * format!
1448 *
1449 * RETURNS
1450 * Success: handle to an icon
1451 * Failure: NULL
1452 *
1453 * FIXME: Do we need to resize the bitmaps?
1454 */
1455 HICON WINAPI CreateIcon(
1456 HINSTANCE hInstance, /* [in] the application's hInstance */
1457 INT nWidth, /* [in] the width of the provided bitmaps */
1458 INT nHeight, /* [in] the height of the provided bitmaps */
1459 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1460 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1461 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1462 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1463 {
1464 ICONINFO iinfo;
1465 HICON hIcon;
1466
1467 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1468 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1469
1470 iinfo.fIcon = TRUE;
1471 iinfo.xHotspot = ICON_HOTSPOT;
1472 iinfo.yHotspot = ICON_HOTSPOT;
1473 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1474 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1475
1476 hIcon = CreateIconIndirect( &iinfo );
1477
1478 DeleteObject( iinfo.hbmMask );
1479 DeleteObject( iinfo.hbmColor );
1480
1481 return hIcon;
1482 }
1483
1484
1485 /***********************************************************************
1486 * CreateCursorIconIndirect (USER.408)
1487 */
1488 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1489 CURSORICONINFO *info,
1490 LPCVOID lpANDbits,
1491 LPCVOID lpXORbits )
1492 {
1493 HGLOBAL16 handle;
1494 char *ptr;
1495 int sizeAnd, sizeXor;
1496
1497 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1498 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1499 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1500 sizeXor = info->nHeight * info->nWidthBytes;
1501 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1502 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1503 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1504 return 0;
1505 FarSetOwner16( handle, hInstance );
1506 ptr = GlobalLock16( handle );
1507 memcpy( ptr, info, sizeof(*info) );
1508 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1509 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1510 GlobalUnlock16( handle );
1511 return handle;
1512 }
1513
1514
1515 /***********************************************************************
1516 * CopyIcon (USER.368)
1517 */
1518 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1519 {
1520 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1521 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1522 }
1523
1524
1525 /***********************************************************************
1526 * CopyIcon (USER32.@)
1527 */
1528 HICON WINAPI CopyIcon( HICON hIcon )
1529 {
1530 TRACE_(icon)("%p\n", hIcon );
1531 return CURSORICON_Copy( 0, hIcon );
1532 }
1533
1534
1535 /***********************************************************************
1536 * CopyCursor (USER.369)
1537 */
1538 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1539 {
1540 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1541 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1542 }
1543
1544 /**********************************************************************
1545 * DestroyIcon32 (USER.610)
1546 *
1547 * This routine is actually exported from Win95 USER under the name
1548 * DestroyIcon32 ... The behaviour implemented here should mimic
1549 * the Win95 one exactly, especially the return values, which
1550 * depend on the setting of various flags.
1551 */
1552 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1553 {
1554 WORD retv;
1555
1556 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1557
1558 /* Check whether destroying active cursor */
1559
1560 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1561 {
1562 WARN_(cursor)("Destroying active cursor!\n" );
1563 return FALSE;
1564 }
1565
1566 /* Try shared cursor/icon first */
1567
1568 if ( !(flags & CID_NONSHARED) )
1569 {
1570 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1571
1572 if ( count != -1 )
1573 return (flags & CID_WIN32)? TRUE : (count == 0);
1574
1575 /* FIXME: OEM cursors/icons should be recognized */
1576 }
1577
1578 /* Now assume non-shared cursor/icon */
1579
1580 retv = GlobalFree16( handle );
1581 return (flags & CID_RESOURCE)? retv : TRUE;
1582 }
1583
1584 /***********************************************************************
1585 * DestroyIcon (USER32.@)
1586 */
1587 BOOL WINAPI DestroyIcon( HICON hIcon )
1588 {
1589 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1590 }
1591
1592
1593 /***********************************************************************
1594 * DestroyCursor (USER32.@)
1595 */
1596 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1597 {
1598 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1599 }
1600
1601
1602 /***********************************************************************
1603 * DrawIcon (USER32.@)
1604 */
1605 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1606 {
1607 CURSORICONINFO *ptr;
1608 HDC hMemDC;
1609 HBITMAP hXorBits, hAndBits;
1610 COLORREF oldFg, oldBg;
1611
1612 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1613
1614 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
1615 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1616 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1617 (char *)(ptr+1) );
1618 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1619 ptr->bBitsPerPixel, (char *)(ptr + 1)
1620 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1621 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1622 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1623
1624 if (hXorBits && hAndBits)
1625 {
1626 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1627 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1628 SelectObject( hMemDC, hXorBits );
1629 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1630 SelectObject( hMemDC, hBitTemp );
1631 }
1632 DeleteDC( hMemDC );
1633 if (hXorBits) DeleteObject( hXorBits );
1634 if (hAndBits) DeleteObject( hAndBits );
1635 GlobalUnlock16(HICON_16(hIcon));
1636 SetTextColor( hdc, oldFg );
1637 SetBkColor( hdc, oldBg );
1638 return TRUE;
1639 }
1640
1641 /***********************************************************************
1642 * DumpIcon (USER.459)
1643 */
1644 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1645 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1646 {
1647 CURSORICONINFO *info = MapSL( pInfo );
1648 int sizeAnd, sizeXor;
1649
1650 if (!info) return 0;
1651 sizeXor = info->nHeight * info->nWidthBytes;
1652 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1653 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1654 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1655 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1656 return MAKELONG( sizeXor, sizeXor );
1657 }
1658
1659
1660 /***********************************************************************
1661 * SetCursor (USER32.@)
1662 *
1663 * Set the cursor shape.
1664 *
1665 * RETURNS
1666 * A handle to the previous cursor shape.
1667 */
1668 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1669 {
1670 struct user_thread_info *thread_info = get_user_thread_info();
1671 HCURSOR hOldCursor;
1672
1673 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1674 TRACE("%p\n", hCursor);
1675 hOldCursor = thread_info->cursor;
1676 thread_info->cursor = hCursor;
1677 /* Change the cursor shape only if it is visible */
1678 if (thread_info->cursor_count >= 0)
1679 {
1680 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1681 GlobalUnlock16(HCURSOR_16(hCursor));
1682 }
1683 return hOldCursor;
1684 }
1685
1686 /***********************************************************************
1687 * ShowCursor (USER32.@)
1688 */
1689 INT WINAPI ShowCursor( BOOL bShow )
1690 {
1691 struct user_thread_info *thread_info = get_user_thread_info();
1692
1693 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1694
1695 if (bShow)
1696 {
1697 if (++thread_info->cursor_count == 0) /* Show it */
1698 {
1699 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1700 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1701 }
1702 }
1703 else
1704 {
1705 if (--thread_info->cursor_count == -1) /* Hide it */
1706 USER_Driver->pSetCursor( NULL );
1707 }
1708 return thread_info->cursor_count;
1709 }
1710
1711 /***********************************************************************
1712 * GetCursor (USER32.@)
1713 */
1714 HCURSOR WINAPI GetCursor(void)
1715 {
1716 return get_user_thread_info()->cursor;
1717 }
1718
1719
1720 /***********************************************************************
1721 * ClipCursor (USER32.@)
1722 */
1723 BOOL WINAPI ClipCursor( const RECT *rect )
1724 {
1725 RECT virt;
1726
1727 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1728 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1729 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1730 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1731
1732 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1733 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1734
1735 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1736 CURSOR_ClipRect = virt;
1737
1738 USER_Driver->pClipCursor( rect );
1739 return TRUE;
1740 }
1741
1742
1743 /***********************************************************************
1744 * GetClipCursor (USER32.@)
1745 */
1746 BOOL WINAPI GetClipCursor( RECT *rect )
1747 {
1748 /* If this is first time - initialize the rect */
1749 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1750
1751 return CopyRect( rect, &CURSOR_ClipRect );
1752 }
1753
1754
1755 /***********************************************************************
1756 * SetSystemCursor (USER32.@)
1757 */
1758 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1759 {
1760 FIXME("(%p,%08x),stub!\n", hcur, id);
1761 return TRUE;
1762 }
1763
1764
1765 /**********************************************************************
1766 * LookupIconIdFromDirectoryEx (USER.364)
1767 *
1768 * FIXME: exact parameter sizes
1769 */
1770 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1771 INT16 width, INT16 height, UINT16 cFlag )
1772 {
1773 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1774 }
1775
1776 /**********************************************************************
1777 * LookupIconIdFromDirectoryEx (USER32.@)
1778 */
1779 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1780 INT width, INT height, UINT cFlag )
1781 {
1782 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1783 UINT retVal = 0;
1784 if( dir && !dir->idReserved && (dir->idType & 3) )
1785 {
1786 CURSORICONDIRENTRY* entry;
1787 HDC hdc;
1788 UINT palEnts;
1789 int colors;
1790 hdc = GetDC(0);
1791 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1792 if (palEnts == 0)
1793 palEnts = 256;
1794 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1795
1796 ReleaseDC(0, hdc);
1797
1798 if( bIcon )
1799 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1800 else
1801 entry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1802
1803 if( entry ) retVal = entry->wResId;
1804 }
1805 else WARN_(cursor)("invalid resource directory\n");
1806 return retVal;
1807 }
1808
1809 /**********************************************************************
1810 * LookupIconIdFromDirectory (USER32.@)
1811 */
1812 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1813 {
1814 return LookupIconIdFromDirectoryEx( dir, bIcon,
1815 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1816 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1817 }
1818
1819 /**********************************************************************
1820 * GetIconID (USER.455)
1821 */
1822 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1823 {
1824 LPBYTE lpDir = GlobalLock16(hResource);
1825
1826 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1827 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1828
1829 switch(resType)
1830 {
1831 case RT_CURSOR:
1832 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1833 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1834 case RT_ICON:
1835 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1836 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1837 default:
1838 WARN_(cursor)("invalid res type %d\n", resType );
1839 }
1840 return 0;
1841 }
1842
1843 /**********************************************************************
1844 * LoadCursorIconHandler (USER.336)
1845 *
1846 * Supposed to load resources of Windows 2.x applications.
1847 */
1848 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1849 {
1850 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1851 hResource, hModule, hRsrc);
1852 return 0;
1853 }
1854
1855 /**********************************************************************
1856 * LoadIconHandler (USER.456)
1857 */
1858 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1859 {
1860 LPBYTE bits = LockResource16( hResource );
1861
1862 TRACE_(cursor)("hRes=%04x\n",hResource);
1863
1864 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1865 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1866 }
1867
1868 /***********************************************************************
1869 * LoadCursorW (USER32.@)
1870 */
1871 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1872 {
1873 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1874
1875 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1876 LR_SHARED | LR_DEFAULTSIZE );
1877 }
1878
1879 /***********************************************************************
1880 * LoadCursorA (USER32.@)
1881 */
1882 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1883 {
1884 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1885
1886 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1887 LR_SHARED | LR_DEFAULTSIZE );
1888 }
1889
1890 /***********************************************************************
1891 * LoadCursorFromFileW (USER32.@)
1892 */
1893 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1894 {
1895 TRACE("%s\n", debugstr_w(name));
1896
1897 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1898 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1899 }
1900
1901 /***********************************************************************
1902 * LoadCursorFromFileA (USER32.@)
1903 */
1904 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1905 {
1906 TRACE("%s\n", debugstr_a(name));
1907
1908 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1909 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1910 }
1911
1912 /***********************************************************************
1913 * LoadIconW (USER32.@)
1914 */
1915 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1916 {
1917 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1918
1919 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1920 LR_SHARED | LR_DEFAULTSIZE );
1921 }
1922
1923 /***********************************************************************
1924 * LoadIconA (USER32.@)
1925 */
1926 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1927 {
1928 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1929
1930 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1931 LR_SHARED | LR_DEFAULTSIZE );
1932 }
1933
1934 /**********************************************************************
1935 * GetIconInfo (USER32.@)
1936 */
1937 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1938 {
1939 CURSORICONINFO *ciconinfo;
1940 INT height;
1941
1942 ciconinfo = GlobalLock16(HICON_16(hIcon));
1943 if (!ciconinfo)
1944 return FALSE;
1945
1946 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1947 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1948
1949 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1950 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1951 {
1952 iconinfo->fIcon = TRUE;
1953 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1954 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1955 }
1956 else
1957 {
1958 iconinfo->fIcon = FALSE;
1959 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1960 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1961 }
1962
1963 height = ciconinfo->nHeight;
1964
1965 if (ciconinfo->bBitsPerPixel > 1)
1966 {
1967 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1968 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1969 (char *)(ciconinfo + 1)
1970 + ciconinfo->nHeight *
1971 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1972 }
1973 else
1974 {
1975 iconinfo->hbmColor = 0;
1976 height *= 2;
1977 }
1978
1979 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1980 1, 1, (char *)(ciconinfo + 1));
1981
1982 GlobalUnlock16(HICON_16(hIcon));
1983
1984 return TRUE;
1985 }
1986
1987 /**********************************************************************
1988 * CreateIconIndirect (USER32.@)
1989 */
1990 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1991 {
1992 DIBSECTION bmpXor;
1993 BITMAP bmpAnd;
1994 HICON16 hObj;
1995 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1996
1997 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1998 iconinfo->hbmColor, iconinfo->hbmMask,
1999 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2000
2001 if (!iconinfo->hbmMask) return 0;
2002
2003 planes = GetDeviceCaps( screen_dc, PLANES );
2004 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2005
2006 if (iconinfo->hbmColor)
2007 {
2008 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2009 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2010 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2011 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2012 /* we can use either depth 1 or screen depth for xor bitmap */
2013 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2014 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2015 }
2016 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2017 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2018 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2019 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2020
2021 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2022
2023 hObj = GlobalAlloc16( GMEM_MOVEABLE,
2024 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
2025 if (hObj)
2026 {
2027 CURSORICONINFO *info;
2028
2029 info = GlobalLock16( hObj );
2030
2031 /* If we are creating an icon, the hotspot is unused */
2032 if (iconinfo->fIcon)
2033 {
2034 info->ptHotSpot.x = ICON_HOTSPOT;
2035 info->ptHotSpot.y = ICON_HOTSPOT;
2036 }
2037 else
2038 {
2039 info->ptHotSpot.x = iconinfo->xHotspot;
2040 info->ptHotSpot.y = iconinfo->yHotspot;
2041 }
2042
2043 if (iconinfo->hbmColor)
2044 {
2045 info->nWidth = bmpXor.dsBm.bmWidth;
2046 info->nHeight = bmpXor.dsBm.bmHeight;
2047 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2048 info->bPlanes = planes;
2049 info->bBitsPerPixel = bpp;
2050 }
2051 else
2052 {
2053 info->nWidth = bmpAnd.bmWidth;
2054 info->nHeight = bmpAnd.bmHeight / 2;
2055 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2056 info->bPlanes = 1;
2057 info->bBitsPerPixel = 1;
2058 }
2059
2060 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2061
2062 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2063 if (bmpAnd.bmBitsPixel == 1)
2064 {
2065 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
2066 }
2067 else
2068 {
2069 HDC hdc, hdc_mem;
2070 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2071
2072 hdc = GetDC( 0 );
2073 hdc_mem = CreateCompatibleDC( hdc );
2074
2075 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2076
2077 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2078 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2079
2080 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2081
2082 SelectObject( hdc, hbmp_old );
2083 SelectObject( hdc_mem, hbmp_mem_old );
2084
2085 DeleteDC( hdc_mem );
2086 ReleaseDC( 0, hdc );
2087
2088 GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
2089 DeleteObject( hbmp_mono );
2090 }
2091
2092 if (iconinfo->hbmColor)
2093 {
2094 char *dst_bits = (char*)(info + 1) + sizeAnd;
2095
2096 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2097 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2098 else
2099 {
2100 BITMAPINFO bminfo;
2101 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2102 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2103
2104 bminfo.bmiHeader.biSize = sizeof(bminfo);
2105 bminfo.bmiHeader.biWidth = info->nWidth;
2106 bminfo.bmiHeader.biHeight = info->nHeight;
2107 bminfo.bmiHeader.biPlanes = info->bPlanes;
2108 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2109 bminfo.bmiHeader.biCompression = BI_RGB;
2110 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2111 bminfo.bmiHeader.biXPelsPerMeter = 0;
2112 bminfo.bmiHeader.biYPelsPerMeter = 0;
2113 bminfo.bmiHeader.biClrUsed = 0;
2114 bminfo.bmiHeader.biClrImportant = 0;
2115
2116 /* swap lines for dib sections */
2117 if (xor_objsize == sizeof(DIBSECTION))
2118 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2119
2120 if (dib_width != bitmap_width) /* need to fixup alignment */
2121 {
2122 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2123
2124 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2125 src_bits, &bminfo, DIB_RGB_COLORS ))
2126 {
2127 int y;
2128 for (y = 0; y < info->nHeight; y++)
2129 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2130 }
2131 HeapFree( GetProcessHeap(), 0, src_bits );
2132 }
2133 else
2134 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2135 dst_bits, &bminfo, DIB_RGB_COLORS );
2136 }
2137 }
2138 GlobalUnlock16( hObj );
2139 }
2140 return HICON_32(hObj);
2141 }
2142
2143 /******************************************************************************
2144 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2145 *
2146 * NOTES
2147 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2148 *
2149 * PARAMS
2150 * hdc [I] Handle to device context
2151 * x0 [I] X coordinate of upper left corner
2152 * y0 [I] Y coordinate of upper left corner
2153 * hIcon [I] Handle to icon to draw
2154 * cxWidth [I] Width of icon
2155 * cyWidth [I] Height of icon
2156 * istep [I] Index of frame in animated cursor
2157 * hbr [I] Handle to background brush
2158 * flags [I] Icon-drawing flags
2159 *
2160 * RETURNS
2161 * Success: TRUE
2162 * Failure: FALSE
2163 */
2164 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2165 INT cxWidth, INT cyWidth, UINT istep,
2166 HBRUSH hbr, UINT flags )
2167 {
2168 CURSORICONINFO *ptr = GlobalLock16(HICON_16(hIcon));
2169 HDC hDC_off = 0, hMemDC;
2170 BOOL result = FALSE, DoOffscreen;
2171 HBITMAP hB_off = 0, hOld = 0;
2172
2173 if (!ptr) return FALSE;
2174 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2175 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2176
2177 hMemDC = CreateCompatibleDC (hdc);
2178 if (istep)
2179 FIXME_(icon)("Ignoring istep=%d\n", istep);
2180 if (flags & DI_NOMIRROR)
2181 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2182
2183 if (!flags) {
2184 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
2185 flags = DI_NORMAL;
2186 }
2187
2188 /* Calculate the size of the destination image. */
2189 if (cxWidth == 0)
2190 {
2191 if (flags & DI_DEFAULTSIZE)
2192 cxWidth = GetSystemMetrics (SM_CXICON);
2193 else
2194 cxWidth = ptr->nWidth;
2195 }
2196 if (cyWidth == 0)
2197 {
2198 if (flags & DI_DEFAULTSIZE)
2199 cyWidth = GetSystemMetrics (SM_CYICON);
2200 else
2201 cyWidth = ptr->nHeight;
2202 }
2203
2204 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2205
2206 if (DoOffscreen) {
2207 RECT r;
2208
2209 r.left = 0;
2210 r.top = 0;
2211 r.right = cxWidth;
2212 r.bottom = cxWidth;
2213
2214 hDC_off = CreateCompatibleDC(hdc);
2215 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2216 if (hDC_off && hB_off) {
2217 hOld = SelectObject(hDC_off, hB_off);
2218 FillRect(hDC_off, &r, hbr);
2219 }
2220 }
2221
2222 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2223 {
2224 HBITMAP hXorBits, hAndBits;
2225 COLORREF oldFg, oldBg;
2226 INT nStretchMode;
2227
2228 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2229
2230 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2231 ptr->bPlanes, ptr->bBitsPerPixel,
2232 (char *)(ptr + 1)
2233 + ptr->nHeight *
2234 get_bitmap_width_bytes(ptr->nWidth,1) );
2235 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2236 1, 1, (char *)(ptr+1) );
2237 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2238 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2239
2240 if (hXorBits && hAndBits)
2241 {
2242 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2243 if (flags & DI_MASK)
2244 {
2245 if (DoOffscreen)
2246 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2247 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2248 else
2249 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2250 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2251 }
2252 SelectObject( hMemDC, hXorBits );
2253 if (flags & DI_IMAGE)
2254 {
2255 if (DoOffscreen)
2256 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2257 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2258 else
2259 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2260 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2261 }
2262 SelectObject( hMemDC, hBitTemp );
2263 result = TRUE;
2264 }
2265
2266 SetTextColor( hdc, oldFg );
2267 SetBkColor( hdc, oldBg );
2268 if (hXorBits) DeleteObject( hXorBits );
2269 if (hAndBits) DeleteObject( hAndBits );
2270 SetStretchBltMode (hdc, nStretchMode);
2271 if (DoOffscreen) {
2272 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2273 SelectObject(hDC_off, hOld);
2274 }
2275 }
2276 if (hMemDC) DeleteDC( hMemDC );
2277 if (hDC_off) DeleteDC(hDC_off);
2278 if (hB_off) DeleteObject(hB_off);
2279 GlobalUnlock16(HICON_16(hIcon));
2280 return result;
2281 }
2282
2283 /***********************************************************************
2284 * DIB_FixColorsToLoadflags
2285 *
2286 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2287 * are in loadflags
2288 */
2289 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2290 {
2291 int colors;
2292 COLORREF c_W, c_S, c_F, c_L, c_C;
2293 int incr,i;
2294 RGBQUAD *ptr;
2295 int bitmap_type;
2296 LONG width;
2297 LONG height;
2298 WORD bpp;
2299 DWORD compr;
2300
2301 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2302 {
2303 WARN_(resource)("Invalid bitmap\n");
2304 return;
2305 }
2306
2307 if (bpp > 8) return;
2308
2309 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2310 {
2311 incr = 3;
2312 colors = 1 << bpp;
2313 }
2314 else
2315 {
2316 incr = 4;
2317 colors = bmi->bmiHeader.biClrUsed;
2318 if (colors > 256) colors = 256;
2319 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2320 }
2321
2322 c_W = GetSysColor(COLOR_WINDOW);
2323 c_S = GetSysColor(COLOR_3DSHADOW);
2324 c_F = GetSysColor(COLOR_3DFACE);
2325 c_L = GetSysColor(COLOR_3DLIGHT);
2326
2327 if (loadflags & LR_LOADTRANSPARENT) {
2328 switch (bpp) {
2329 case 1: pix = pix >> 7; break;
2330 case 4: pix = pix >> 4; break;
2331 case 8: break;
2332 default:
2333 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2334 return;
2335 }
2336 if (pix >= colors) {
2337 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2338 return;
2339 }
2340 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2341 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2342 ptr->rgbBlue = GetBValue(c_W);
2343 ptr->rgbGreen = GetGValue(c_W);
2344 ptr->rgbRed = GetRValue(c_W);
2345 }
2346 if (loadflags & LR_LOADMAP3DCOLORS)
2347 for (i=0; i<colors; i++) {
2348 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2349 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2350 if (c_C == RGB(128, 128, 128)) {
2351 ptr->rgbRed = GetRValue(c_S);
2352 ptr->rgbGreen = GetGValue(c_S);
2353 ptr->rgbBlue = GetBValue(c_S);
2354 } else if (c_C == RGB(192, 192, 192)) {
2355 ptr->rgbRed = GetRValue(c_F);
2356 ptr->rgbGreen = GetGValue(c_F);
2357 ptr->rgbBlue = GetBValue(c_F);
2358 } else if (c_C == RGB(223, 223, 223)) {
2359 ptr->rgbRed = GetRValue(c_L);
2360 ptr->rgbGreen = GetGValue(c_L);
2361 ptr->rgbBlue = GetBValue(c_L);
2362 }
2363 }
2364 }
2365
2366
2367 /**********************************************************************
2368 * BITMAP_Load
2369 */
2370 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2371 INT desiredx, INT desiredy, UINT loadflags )
2372 {
2373 HBITMAP hbitmap = 0, orig_bm;
2374 HRSRC hRsrc;
2375 HGLOBAL handle;
2376 char *ptr = NULL;
2377 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2378 int size;
2379 BYTE pix;
2380 char *bits;
2381 LONG width, height, new_width, new_height;
2382 WORD bpp_dummy;
2383 DWORD compr_dummy;
2384 INT bm_type;
2385 HDC screen_mem_dc = NULL;
2386
2387 if (!(loadflags & LR_LOADFROMFILE))
2388 {
2389 if (!instance)
2390 {
2391 /* OEM bitmap: try to load the resource from user32.dll */
2392 instance = user32_module;
2393 }
2394
2395 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2396 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2397
2398 if ((info = LockResource( handle )) == NULL) return 0;
2399 }
2400 else
2401 {
2402 BITMAPFILEHEADER * bmfh;
2403
2404 if (!(ptr = map_fileW( name, NULL ))) return 0;
2405 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2406 bmfh = (BITMAPFILEHEADER *)ptr;
2407 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2408 bmfh->bfReserved1 == 0 &&
2409 bmfh->bfReserved2 == 0))
2410 {
2411 WARN("Invalid/unsupported bitmap format!\n");
2412 UnmapViewOfFile( ptr );
2413 return 0;
2414 }
2415 }
2416
2417 size = bitmap_info_size(info, DIB_RGB_COLORS);
2418 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2419 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2420
2421 if (!fix_info || !scaled_info) goto end;
2422 memcpy(fix_info, info, size);
2423
2424 pix = *((LPBYTE)info + size);
2425 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2426
2427 memcpy(scaled_info, fix_info, size);
2428 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2429 &bpp_dummy, &compr_dummy);
2430 if(desiredx != 0)
2431 new_width = desiredx;
2432 else
2433 new_width = width;
2434
2435 if(desiredy != 0)
2436 new_height = height > 0 ? desiredy : -desiredy;
2437 else
2438 new_height = height;
2439
2440 if(bm_type == 0)
2441 {
2442 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2443 core->bcWidth = new_width;
2444 core->bcHeight = new_height;
2445 }
2446 else
2447 {
2448 scaled_info->bmiHeader.biWidth = new_width;
2449 scaled_info->bmiHeader.biHeight = new_height;
2450 }
2451
2452 if (new_height < 0) new_height = -new_height;
2453
2454 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2455 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2456
2457 bits = (char *)info + size;
2458
2459 if (loadflags & LR_CREATEDIBSECTION)
2460 {
2461 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2462 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2463 }
2464 else
2465 {
2466 if (is_dib_monochrome(fix_info))
2467 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2468 else
2469 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2470 }
2471
2472 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2473 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2474 SelectObject(screen_mem_dc, orig_bm);
2475
2476 end:
2477 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2478 HeapFree(GetProcessHeap(), 0, scaled_info);
2479 HeapFree(GetProcessHeap(), 0, fix_info);
2480 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2481
2482 return hbitmap;
2483 }
2484
2485 /**********************************************************************
2486 * LoadImageA (USER32.@)
2487 *
2488 * See LoadImageW.
2489 */
2490 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2491 INT desiredx, INT desiredy, UINT loadflags)
2492 {
2493 HANDLE res;
2494 LPWSTR u_name;
2495
2496 if (!HIWORD(name))
2497 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2498
2499 __TRY {
2500 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2501 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2502 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2503 }
2504 __EXCEPT_PAGE_FAULT {
2505 SetLastError( ERROR_INVALID_PARAMETER );
2506 return 0;
2507 }
2508 __ENDTRY
2509 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2510 HeapFree(GetProcessHeap(), 0, u_name);
2511 return res;
2512 }
2513
2514
2515 /******************************************************************************
2516 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2517 *
2518 * PARAMS
2519 * hinst [I] Handle of instance that contains image
2520 * name [I] Name of image
2521 * type [I] Type of image
2522 * desiredx [I] Desired width
2523 * desiredy [I] Desired height
2524 * loadflags [I] Load flags
2525 *
2526 * RETURNS
2527 * Success: Handle to newly loaded image
2528 * Failure: NULL
2529 *
2530 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2531 */
2532 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2533 INT desiredx, INT desiredy, UINT loadflags )
2534 {
2535 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2536 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2537
2538 if (loadflags & LR_DEFAULTSIZE) {
2539 if (type == IMAGE_ICON) {
2540 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2541 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2542 } else if (type == IMAGE_CURSOR) {
2543 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2544 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2545 }
2546 }
2547 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2548 switch (type) {
2549 case IMAGE_BITMAP:
2550 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2551
2552 case IMAGE_ICON:
2553 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2554 if (screen_dc)
2555 {
2556 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2557 if (palEnts == 0) palEnts = 256;
2558 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2559 palEnts, FALSE, loadflags);
2560 }
2561 break;
2562
2563 case IMAGE_CURSOR:
2564 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2565 1, TRUE, loadflags);
2566 }
2567 return 0;
2568 }
2569
2570 /******************************************************************************
2571 * CopyImage (USER32.@) Creates new image and copies attributes to it
2572 *
2573 * PARAMS
2574 * hnd [I] Handle to image to copy
2575 * type [I] Type of image to copy
2576 * desiredx [I] Desired width of new image
2577 * desiredy [I] Desired height of new image
2578 * flags [I] Copy flags
2579 *
2580 * RETURNS
2581 * Success: Handle to newly created image
2582 * Failure: NULL
2583 *
2584 * BUGS
2585 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2586 * all other versions (95/2000/XP have been tested) ignore it.
2587 *
2588 * NOTES
2589 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2590 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2591 * the copy will have the same depth as the screen.
2592 * The content of the image will only be copied if the bit depth of the
2593 * original image is compatible with the bit depth of the screen, or
2594 * if the source is a DIB section.
2595 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2596 */
2597 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2598 INT desiredy, UINT flags )
2599 {
2600 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2601 hnd, type, desiredx, desiredy, flags);
2602
2603 switch (type)
2604 {
2605 case IMAGE_BITMAP:
2606 {
2607 HBITMAP res = NULL;
2608 DIBSECTION ds;
2609 int objSize;
2610 BITMAPINFO * bi;
2611
2612 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2613 if (!objSize) return 0;
2614 if ((desiredx < 0) || (desiredy < 0)) return 0;
2615
2616 if (flags & LR_COPYFROMRESOURCE)
2617 {
2618 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2619 }
2620
2621 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2622 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2623
2624 /* Allocate memory for a BITMAPINFOHEADER structure and a
2625 color table. The maximum number of colors in a color table
2626 is 256 which corresponds to a bitmap with depth 8.
2627 Bitmaps with higher depths don't have color tables. */
2628 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2629 if (!bi) return 0;
2630
2631 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2632 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2633 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2634 bi->bmiHeader.biCompression = BI_RGB;
2635
2636 if (flags & LR_CREATEDIBSECTION)
2637 {
2638 /* Create a DIB section. LR_MONOCHROME is ignored */
2639 void * bits;
2640 HDC dc = CreateCompatibleDC(NULL);
2641
2642 if (objSize == sizeof(DIBSECTION))
2643 {
2644 /* The source bitmap is a DIB.
2645 Get its attributes to create an exact copy */
2646 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2647 }
2648
2649 /* Get the color table or the color masks */
2650 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2651
2652 bi->bmiHeader.biWidth = desiredx;
2653 bi->bmiHeader.biHeight = desiredy;
2654 bi->bmiHeader.biSizeImage = 0;
2655
2656 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2657 DeleteDC(dc);
2658 }
2659 else
2660 {
2661 /* Create a device-dependent bitmap */
2662
2663 BOOL monochrome = (flags & LR_MONOCHROME);
2664
2665 if (objSize == sizeof(DIBSECTION))
2666 {
2667 /* The source bitmap is a DIB section.
2668 Get its attributes */
2669 HDC dc = CreateCompatibleDC(NULL);
2670 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2671 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2672 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2673 DeleteDC(dc);
2674
2675 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2676 {
2677 /* Look if the colors of the DIB are black and white */
2678
2679 monochrome =
2680 (bi->bmiColors[0].rgbRed == 0xff
2681 && bi->bmiColors[0].rgbGreen == 0xff
2682 && bi->bmiColors[0].rgbBlue == 0xff
2683 && bi->bmiColors[0].rgbReserved == 0
2684 && bi->bmiColors[1].rgbRed == 0
2685 && bi->bmiColors[1].rgbGreen == 0
2686 && bi->bmiColors[1].rgbBlue == 0
2687 && bi->bmiColors[1].rgbReserved == 0)
2688 ||
2689 (bi->bmiColors[0].rgbRed == 0
2690 && bi->bmiColors[0].rgbGreen == 0
2691 && bi->bmiColors[0].rgbBlue == 0
2692 && bi->bmiColors[0].rgbReserved == 0
2693 && bi->bmiColors[1].rgbRed == 0xff
2694 && bi->bmiColors[1].rgbGreen == 0xff
2695 && bi->bmiColors[1].rgbBlue == 0xff
2696 && bi->bmiColors[1].rgbReserved == 0);
2697 }
2698 }
2699 else if (!monochrome)
2700 {
2701 monochrome = ds.dsBm.bmBitsPixel == 1;
2702 }
2703
2704 if (monochrome)
2705 {
2706 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2707 }
2708 else
2709 {
2710 HDC screenDC = GetDC(NULL);
2711 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2712 ReleaseDC(NULL, screenDC);
2713 }
2714 }
2715
2716 if (res)
2717 {
2718 /* Only copy the bitmap if it's a DIB section or if it's
2719 compatible to the screen */
2720 BOOL copyContents;
2721
2722 if (objSize == sizeof(DIBSECTION))
2723 {
2724 copyContents = TRUE;
2725 }
2726 else
2727 {
2728 HDC screenDC = GetDC(NULL);
2729 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2730 ReleaseDC(NULL, screenDC);
2731
2732 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2733 }
2734
2735 if (copyContents)
2736 {
2737 /* The source bitmap may already be selected in a device context,
2738 use GetDIBits/StretchDIBits and not StretchBlt */
2739
2740 HDC dc;
2741 void * bits;
2742
2743 dc = CreateCompatibleDC(NULL);
2744
2745 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2746 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2747 bi->bmiHeader.biSizeImage = 0;
2748 bi->bmiHeader.biClrUsed = 0;
2749 bi->bmiHeader.biClrImportant = 0;
2750
2751 /* Fill in biSizeImage */
2752 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2753 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2754
2755 if (bits)
2756 {
2757 HBITMAP oldBmp;
2758
2759 /* Get the image bits of the source bitmap */
2760 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2761
2762 /* Copy it to the destination bitmap */
2763 oldBmp = SelectObject(dc, res);
2764 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2765 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2766 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2767 SelectObject(dc, oldBmp);
2768
2769 HeapFree(GetProcessHeap(), 0, bits);
2770 }
2771
2772 DeleteDC(dc);
2773 }
2774
2775 if (flags & LR_COPYDELETEORG)
2776 {
2777 DeleteObject(hnd);
2778 }
2779 }
2780 HeapFree(GetProcessHeap(), 0, bi);
2781 return res;
2782 }
2783 case IMAGE_ICON:
2784 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2785 case IMAGE_CURSOR:
2786 /* Should call CURSORICON_ExtCopy but more testing
2787 * needs to be done before we change this
2788 */
2789 if (flags) FIXME("Flags are ignored\n");
2790 return CopyCursor(hnd);
2791 }
2792 return 0;
2793 }
2794
2795
2796 /******************************************************************************
2797 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2798 *
2799 * RETURNS
2800 * Success: Handle to specified bitmap
2801 * Failure: NULL
2802 */
2803 HBITMAP WINAPI LoadBitmapW(
2804 HINSTANCE instance, /* [in] Handle to application instance */
2805 LPCWSTR name) /* [in] Address of bitmap resource name */
2806 {
2807 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2808 }
2809
2810 /**********************************************************************
2811 * LoadBitmapA (USER32.@)
2812 *
2813 * See LoadBitmapW.
2814 */
2815 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2816 {
2817 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2818 }
2819
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.