0c88734834c87fc037b8482365d85de6d182928d
[reactos.git] / dll / win32 / user32 / windows / cursoricon.c
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 #include <user32.h>
26
27 #include <wine/debug.h>
28
29 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
30 WINE_DECLARE_DEBUG_CHANNEL(icon);
31 WINE_DECLARE_DEBUG_CHANNEL(resource);
32
33 #include "pshpack1.h"
34
35 typedef struct {
36 BYTE bWidth;
37 BYTE bHeight;
38 BYTE bColorCount;
39 BYTE bReserved;
40 WORD xHotspot;
41 WORD yHotspot;
42 DWORD dwDIBSize;
43 DWORD dwDIBOffset;
44 } CURSORICONFILEDIRENTRY;
45
46 typedef struct
47 {
48 WORD idReserved;
49 WORD idType;
50 WORD idCount;
51 CURSORICONFILEDIRENTRY idEntries[1];
52 } CURSORICONFILEDIR;
53
54 #include "poppack.h"
55
56 static HDC screen_dc;
57
58 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
59
60
61 static CRITICAL_SECTION IconCrst;
62 static CRITICAL_SECTION_DEBUG critsect_debug =
63 {
64 0, 0, &IconCrst,
65 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
67 };
68 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
69
70 /***********************************************************************
71 * CreateCursorIconHandle
72 *
73 * Creates a handle with everything in there
74 */
75 static
76 HICON
77 CreateCursorIconHandle( PICONINFO IconInfo )
78 {
79 HICON hIcon = (HICON)NtUserCallOneParam(0,
80 ONEPARAM_ROUTINE_CREATEEMPTYCUROBJECT);
81 if(!hIcon)
82 return NULL;
83
84 NtUserSetCursorContents(hIcon, IconInfo);
85 return hIcon;
86 }
87
88
89
90 /***********************************************************************
91 * map_fileW
92 *
93 * Helper function to map a file to memory:
94 * name - file name
95 * [RETURN] ptr - pointer to mapped file
96 * [RETURN] filesize - pointer size of file to be stored if not NULL
97 */
98 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
99 {
100 HANDLE hFile, hMapping;
101 LPVOID ptr = NULL;
102
103 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
104 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
105 if (hFile != INVALID_HANDLE_VALUE)
106 {
107 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
108 if (hMapping)
109 {
110 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
111 CloseHandle( hMapping );
112 if (filesize)
113 *filesize = GetFileSize( hFile, NULL );
114 }
115 CloseHandle( hFile );
116 }
117 return ptr;
118 }
119
120
121 /***********************************************************************
122 * get_dib_width_bytes
123 *
124 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
125 */
126 static int get_dib_width_bytes( int width, int depth )
127 {
128 int words;
129
130 switch(depth)
131 {
132 case 1: words = (width + 31) / 32; break;
133 case 4: words = (width + 7) / 8; break;
134 case 8: words = (width + 3) / 4; break;
135 case 15:
136 case 16: words = (width + 1) / 2; break;
137 case 24: words = (width * 3 + 3)/4; break;
138 default:
139 WARN("(%d): Unsupported depth\n", depth );
140 /* fall through */
141 case 32:
142 words = width;
143 }
144 return 4 * words;
145 }
146
147
148 /***********************************************************************
149 * bitmap_info_size
150 *
151 * Return the size of the bitmap info structure including color table.
152 */
153 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
154 {
155 unsigned int colors, size, masks = 0;
156
157 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
158 {
159 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
160 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
161 return sizeof(BITMAPCOREHEADER) + colors *
162 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
163 }
164 else /* assume BITMAPINFOHEADER */
165 {
166 colors = info->bmiHeader.biClrUsed;
167 if (colors > 256) /* buffer overflow otherwise */
168 colors = 256;
169 if (!colors && (info->bmiHeader.biBitCount <= 8))
170 colors = 1 << info->bmiHeader.biBitCount;
171 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
172 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
173 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
174 }
175 }
176
177
178 /***********************************************************************
179 * is_dib_monochrome
180 *
181 * Returns whether a DIB can be converted to a monochrome DDB.
182 *
183 * A DIB can be converted if its color table contains only black and
184 * white. Black must be the first color in the color table.
185 *
186 * Note : If the first color in the color table is white followed by
187 * black, we can't convert it to a monochrome DDB with
188 * SetDIBits, because black and white would be inverted.
189 */
190 static BOOL is_dib_monochrome( const BITMAPINFO* info )
191 {
192 if (info->bmiHeader.biBitCount != 1) return FALSE;
193
194 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
195 {
196 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
197
198 /* Check if the first color is black */
199 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
200 {
201 rgb++;
202
203 /* Check if the second color is white */
204 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
205 && (rgb->rgbtBlue == 0xff));
206 }
207 else return FALSE;
208 }
209 else /* assume BITMAPINFOHEADER */
210 {
211 const RGBQUAD *rgb = info->bmiColors;
212
213 /* Check if the first color is black */
214 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
215 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
216 {
217 rgb++;
218
219 /* Check if the second color is white */
220 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
221 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
222 }
223 else return FALSE;
224 }
225 }
226
227 /***********************************************************************
228 * DIB_GetBitmapInfo
229 *
230 * Get the info from a bitmap header.
231 * Return 1 for INFOHEADER, 0 for COREHEADER,
232 */
233 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
234 LONG *height, WORD *bpp, DWORD *compr )
235 {
236 if (header->biSize == sizeof(BITMAPCOREHEADER))
237 {
238 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
239 *width = core->bcWidth;
240 *height = core->bcHeight;
241 *bpp = core->bcBitCount;
242 *compr = 0;
243 return 0;
244 }
245 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
246 {
247 *width = header->biWidth;
248 *height = header->biHeight;
249 *bpp = header->biBitCount;
250 *compr = header->biCompression;
251 return 1;
252 }
253 ERR("(%d): unknown/wrong size for header\n", header->biSize );
254 return -1;
255 }
256
257
258 /*
259 * The following macro functions account for the irregularities of
260 * accessing cursor and icon resources in files and resource entries.
261 */
262 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
263 int *width, int *height, int *bits );
264
265 /**********************************************************************
266 * CURSORICON_FindBestIcon
267 *
268 * Find the icon closest to the requested size and bit depth.
269 */
270 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
271 int width, int height, int depth )
272 {
273 int i, cx, cy, bits, bestEntry = -1;
274 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
275 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
276
277 /* Find Best Fit */
278 iTotalDiff = 0xFFFFFFFF;
279 iColorDiff = 0xFFFFFFFF;
280 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
281 {
282 iTempXDiff = abs(width - cx);
283 iTempYDiff = abs(height - cy);
284
285 if(iTotalDiff > (iTempXDiff + iTempYDiff))
286 {
287 iXDiff = iTempXDiff;
288 iYDiff = iTempYDiff;
289 iTotalDiff = iXDiff + iYDiff;
290 }
291 }
292
293 /* Find Best Colors for Best Fit */
294 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
295 {
296 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
297 {
298 iTempColorDiff = abs(depth - bits);
299 if(iColorDiff > iTempColorDiff)
300 {
301 bestEntry = i;
302 iColorDiff = iTempColorDiff;
303 }
304 }
305 }
306
307 return bestEntry;
308 }
309
310 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
311 int *width, int *height, int *bits )
312 {
313 CURSORICONDIR *resdir = dir;
314 ICONRESDIR *icon;
315
316 if ( resdir->idCount <= n )
317 return FALSE;
318 icon = &resdir->idEntries[n].ResInfo.icon;
319 *width = icon->bWidth;
320 *height = icon->bHeight;
321 *bits = resdir->idEntries[n].wBitCount;
322 return TRUE;
323 }
324
325 /**********************************************************************
326 * CURSORICON_FindBestCursor
327 *
328 * Find the cursor closest to the requested size.
329 *
330 * FIXME: parameter 'color' ignored.
331 */
332 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
333 int width, int height, int depth )
334 {
335 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
336
337 /* Double height to account for AND and XOR masks */
338
339 height *= 2;
340
341 /* First find the largest one smaller than or equal to the requested size*/
342
343 maxwidth = maxheight = 0;
344 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
345 {
346 if ((cx <= width) && (cy <= height) &&
347 (cx > maxwidth) && (cy > maxheight))
348 {
349 bestEntry = i;
350 maxwidth = cx;
351 maxheight = cy;
352 }
353 }
354 if (bestEntry != -1) return bestEntry;
355
356 /* Now find the smallest one larger than the requested size */
357
358 maxwidth = maxheight = 255;
359 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
360 {
361 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
362 {
363 bestEntry = i;
364 maxwidth = cx;
365 maxheight = cy;
366 }
367 }
368
369 return bestEntry;
370 }
371
372 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
373 int *width, int *height, int *bits )
374 {
375 CURSORICONDIR *resdir = dir;
376 CURSORDIR *cursor;
377
378 if ( resdir->idCount <= n )
379 return FALSE;
380 cursor = &resdir->idEntries[n].ResInfo.cursor;
381 *width = cursor->wWidth;
382 *height = cursor->wHeight;
383 *bits = resdir->idEntries[n].wBitCount;
384 return TRUE;
385 }
386
387 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
388 int width, int height, int depth )
389 {
390 int n;
391
392 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
393 width, height, depth );
394 if ( n < 0 )
395 return NULL;
396 return &dir->idEntries[n];
397 }
398
399 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
400 int width, int height, int depth )
401 {
402 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
403 width, height, depth );
404 if ( n < 0 )
405 return NULL;
406 return &dir->idEntries[n];
407 }
408
409 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
410 int *width, int *height, int *bits )
411 {
412 CURSORICONFILEDIR *filedir = dir;
413 CURSORICONFILEDIRENTRY *entry;
414 BITMAPINFOHEADER *info;
415
416 if ( filedir->idCount <= n )
417 return FALSE;
418 entry = &filedir->idEntries[n];
419 /* FIXME: check against file size */
420 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
421 *width = entry->bWidth;
422 *height = entry->bHeight;
423 *bits = info->biBitCount;
424 return TRUE;
425 }
426
427 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
428 int width, int height, int depth )
429 {
430 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
431 width, height, depth );
432 if ( n < 0 )
433 return NULL;
434 return &dir->idEntries[n];
435 }
436
437 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
438 int width, int height, int depth )
439 {
440 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
441 width, height, depth );
442 if ( n < 0 )
443 return NULL;
444 return &dir->idEntries[n];
445 }
446
447 /***********************************************************************
448 * create_icon_bitmaps
449 *
450 * Create the color, mask and alpha bitmaps from the DIB info.
451 */
452 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
453 HBITMAP *color, HBITMAP *mask )
454 {
455 BOOL monochrome = is_dib_monochrome( bmi );
456 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
457 BITMAPINFO *info;
458 void *color_bits, *mask_bits;
459 BOOL ret = FALSE;
460 HDC hdc = 0;
461
462 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
463 return FALSE;
464 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
465
466 memcpy( info, bmi, size );
467 info->bmiHeader.biHeight /= 2;
468
469 color_bits = (char *)bmi + size;
470 mask_bits = (char *)color_bits +
471 get_dib_width_bytes( bmi->bmiHeader.biWidth,
472 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
473
474 if (monochrome)
475 {
476 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
477 *color = 0;
478
479 /* copy color data into second half of mask bitmap */
480 SelectObject( hdc, *mask );
481 StretchDIBits( hdc, 0, height, width, height,
482 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
483 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
484 }
485 else
486 {
487 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
488 if (!(*color = CreateBitmap( width, height, bmi->bmiHeader.biPlanes,
489 bmi->bmiHeader.biBitCount, NULL )))
490 {
491 DeleteObject( *mask );
492 goto done;
493 }
494 SelectObject( hdc, *color );
495 StretchDIBits( hdc, 0, 0, width, height,
496 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
497 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
498
499 /* convert info to monochrome to copy the mask */
500 info->bmiHeader.biBitCount = 1;
501 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
502 {
503 RGBQUAD *rgb = info->bmiColors;
504
505 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
506 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
507 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
508 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
509 }
510 else
511 {
512 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
513
514 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
515 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
516 }
517 }
518
519 SelectObject( hdc, *mask );
520 StretchDIBits( hdc, 0, 0, width, height,
521 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
522 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
523 ret = TRUE;
524
525 done:
526 DeleteDC( hdc );
527 HeapFree( GetProcessHeap(), 0, info );
528 return ret;
529 }
530
531 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
532 POINT hotspot, BOOL bIcon,
533 DWORD dwVersion,
534 INT width, INT height,
535 UINT cFlag )
536 {
537 HBITMAP color = 0, mask = 0;
538 BOOL do_stretch;
539 ICONINFO IconInfo;
540
541 if (dwVersion == 0x00020000)
542 {
543 FIXME_(cursor)("\t2.xx resources are not supported\n");
544 return 0;
545 }
546
547 /* Check bitmap header */
548
549 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
550 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
551 bmi->bmiHeader.biCompression != BI_RGB) )
552 {
553 WARN_(cursor)("\tinvalid resource bitmap header.\n");
554 return 0;
555 }
556
557 if (!width) width = bmi->bmiHeader.biWidth;
558 if (!height) height = bmi->bmiHeader.biHeight/2;
559 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
560 (bmi->bmiHeader.biWidth != width);
561
562 /* Scale the hotspot */
563 if (bIcon)
564 {
565 hotspot.x = width / 2;
566 hotspot.y = height / 2;
567 }
568 else if (do_stretch)
569 {
570 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
571 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
572 }
573
574 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
575 if (!screen_dc) return 0;
576
577 if (!create_icon_bitmaps( bmi, width, height, &color, &mask )) return 0;
578
579 IconInfo.xHotspot = hotspot.x;
580 IconInfo.yHotspot = hotspot.y;
581 IconInfo.fIcon = bIcon;
582 IconInfo.hbmColor = color;
583 IconInfo.hbmMask = mask;
584
585 return CreateCursorIconHandle(&IconInfo);
586 }
587
588
589 /**********************************************************************
590 * .ANI cursor support
591 */
592 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
593 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
594 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
595
596 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
597 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
598 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
599 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
600 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
601 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
602
603 #define ANI_FLAG_ICON 0x1
604 #define ANI_FLAG_SEQUENCE 0x2
605
606 typedef struct {
607 DWORD header_size;
608 DWORD num_frames;
609 DWORD num_steps;
610 DWORD width;
611 DWORD height;
612 DWORD bpp;
613 DWORD num_planes;
614 DWORD display_rate;
615 DWORD flags;
616 } ani_header;
617
618 typedef struct {
619 DWORD data_size;
620 const unsigned char *data;
621 } riff_chunk_t;
622
623 static void dump_ani_header( const ani_header *header )
624 {
625 TRACE(" header size: %d\n", header->header_size);
626 TRACE(" frames: %d\n", header->num_frames);
627 TRACE(" steps: %d\n", header->num_steps);
628 TRACE(" width: %d\n", header->width);
629 TRACE(" height: %d\n", header->height);
630 TRACE(" bpp: %d\n", header->bpp);
631 TRACE(" planes: %d\n", header->num_planes);
632 TRACE(" display rate: %d\n", header->display_rate);
633 TRACE(" flags: 0x%08x\n", header->flags);
634 }
635
636
637 /*
638 * RIFF:
639 * DWORD "RIFF"
640 * DWORD size
641 * DWORD riff_id
642 * BYTE[] data
643 *
644 * LIST:
645 * DWORD "LIST"
646 * DWORD size
647 * DWORD list_id
648 * BYTE[] data
649 *
650 * CHUNK:
651 * DWORD chunk_id
652 * DWORD size
653 * BYTE[] data
654 */
655 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
656 {
657 const unsigned char *ptr = parent_chunk->data;
658 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
659
660 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
661
662 while (ptr < end)
663 {
664 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
665 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
666 {
667 ptr += sizeof(DWORD);
668 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
669 ptr += sizeof(DWORD);
670 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
671 chunk->data = ptr;
672
673 return;
674 }
675
676 ptr += sizeof(DWORD);
677 ptr += (*(const DWORD *)ptr + 1) & ~1;
678 ptr += sizeof(DWORD);
679 }
680 }
681
682
683 /*
684 * .ANI layout:
685 *
686 * RIFF:'ACON' RIFF chunk
687 * |- CHUNK:'anih' Header
688 * |- CHUNK:'seq ' Sequence information (optional)
689 * \- LIST:'fram' Frame list
690 * |- CHUNK:icon Cursor frames
691 * |- CHUNK:icon
692 * |- ...
693 * \- CHUNK:icon
694 */
695 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
696 INT width, INT height, INT depth )
697 {
698 HCURSOR cursor;
699 ani_header header = {0};
700 LPBYTE frame_bits = 0;
701 POINT hotspot;
702 CURSORICONFILEDIRENTRY *entry;
703
704 riff_chunk_t root_chunk = { bits_size, bits };
705 riff_chunk_t ACON_chunk = {0};
706 riff_chunk_t anih_chunk = {0};
707 riff_chunk_t fram_chunk = {0};
708 const unsigned char *icon_data;
709
710 TRACE("bits %p, bits_size %d\n", bits, bits_size);
711
712 if (!bits) return 0;
713
714 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
715 if (!ACON_chunk.data)
716 {
717 ERR("Failed to get root chunk.\n");
718 return 0;
719 }
720
721 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
722 if (!anih_chunk.data)
723 {
724 ERR("Failed to get 'anih' chunk.\n");
725 return 0;
726 }
727 memcpy( &header, anih_chunk.data, sizeof(header) );
728 dump_ani_header( &header );
729
730 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
731 if (!fram_chunk.data)
732 {
733 ERR("Failed to get icon list.\n");
734 return 0;
735 }
736
737 /* FIXME: For now, just load the first frame. Before we can load all the
738 * frames, we need to write the needed code in wineserver, etc. to handle
739 * cursors. Once this code is written, we can extend it to support .ani
740 * cursors and then update user32 and winex11.drv to load all frames.
741 *
742 * Hopefully this will at least make some games (C&C3, etc.) more playable
743 * in the meantime.
744 */
745 FIXME("Loading all frames for .ani cursors not implemented.\n");
746 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
747
748 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
749 width, height, depth );
750
751 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
752 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
753
754 if (!header.width || !header.height)
755 {
756 header.width = entry->bWidth;
757 header.height = entry->bHeight;
758 }
759
760 hotspot.x = entry->xHotspot;
761 hotspot.y = entry->yHotspot;
762
763 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
764 FALSE, 0x00030000, header.width, header.height, 0 );
765
766 HeapFree( GetProcessHeap(), 0, frame_bits );
767
768 return cursor;
769 }
770
771
772 /**********************************************************************
773 * CreateIconFromResourceEx (USER32.@)
774 *
775 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
776 * with cbSize parameter as well.
777 */
778 HICON WINAPI CreateIconFromResourceEx( PBYTE bits, DWORD cbSize,
779 BOOL bIcon, DWORD dwVersion,
780 int width, int height,
781 UINT cFlag )
782 {
783 POINT hotspot;
784 BITMAPINFO *bmi;
785
786 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
787 bits, cbSize, dwVersion, width, height,
788 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
789
790 if (bIcon)
791 {
792 hotspot.x = width / 2;
793 hotspot.y = height / 2;
794 bmi = (BITMAPINFO *)bits;
795 }
796 else /* get the hotspot */
797 {
798 SHORT *pt = (SHORT *)bits;
799 hotspot.x = pt[0];
800 hotspot.y = pt[1];
801 bmi = (BITMAPINFO *)(pt + 2);
802 }
803
804 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
805 width, height, cFlag );
806 }
807
808
809 /**********************************************************************
810 * CreateIconFromResource (USER32.@)
811 */
812 HICON WINAPI CreateIconFromResource( PBYTE bits, DWORD cbSize,
813 BOOL bIcon, DWORD dwVersion)
814 {
815 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
816 }
817
818
819 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
820 INT width, INT height, INT depth,
821 BOOL fCursor, UINT loadflags)
822 {
823 CURSORICONFILEDIRENTRY *entry;
824 CURSORICONFILEDIR *dir;
825 DWORD filesize = 0;
826 HICON hIcon = 0;
827 LPBYTE bits;
828 POINT hotspot;
829
830 TRACE("loading %s\n", debugstr_w( filename ));
831
832 bits = map_fileW( filename, &filesize );
833 if (!bits)
834 return hIcon;
835
836 /* Check for .ani. */
837 if (memcmp( bits, "RIFF", 4 ) == 0)
838 {
839 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
840 depth );
841 goto end;
842 }
843
844 dir = (CURSORICONFILEDIR*) bits;
845 if ( filesize < sizeof(*dir) )
846 goto end;
847
848 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
849 goto end;
850
851 if ( fCursor )
852 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
853 else
854 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
855
856 if ( !entry )
857 goto end;
858
859 /* check that we don't run off the end of the file */
860 if ( entry->dwDIBOffset > filesize )
861 goto end;
862 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
863 goto end;
864
865 hotspot.x = entry->xHotspot;
866 hotspot.y = entry->yHotspot;
867 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
868 hotspot, !fCursor, 0x00030000,
869 width, height, loadflags );
870 end:
871 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
872 UnmapViewOfFile( bits );
873 return hIcon;
874 }
875
876 /**********************************************************************
877 * CURSORICON_Load
878 *
879 * Load a cursor or icon from resource or file.
880 */
881 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
882 INT width, INT height, INT depth,
883 BOOL fCursor, UINT loadflags)
884 {
885 HANDLE handle = 0;
886 HICON hIcon = 0;
887 HRSRC hRsrc, hGroupRsrc;
888 CURSORICONDIR *dir;
889 CURSORICONDIRENTRY *dirEntry;
890 LPBYTE bits;
891 WORD wResId;
892 DWORD dwBytesInRes;
893
894 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
895 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
896
897 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
898 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
899
900 if (!hInstance) hInstance = User32Instance; /* Load OEM cursor/icon */
901
902 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
903 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
904
905 /* Get directory resource ID */
906
907 if (!(hRsrc = FindResourceW( hInstance, name,
908 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
909 return 0;
910 hGroupRsrc = hRsrc;
911
912 /* Find the best entry in the directory */
913
914 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
915 if (!(dir = LockResource( handle ))) return 0;
916 if (fCursor)
917 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
918 else
919 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
920 if (!dirEntry) return 0;
921 wResId = dirEntry->wResId;
922 dwBytesInRes = dirEntry->dwBytesInRes;
923 FreeResource( handle );
924
925 /* Load the resource */
926
927 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
928 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
929
930 /* If shared icon, check whether it was already loaded */
931 if ( (loadflags & LR_SHARED)
932 && (hIcon = NtUserFindExistingCursorIcon( hInstance, hRsrc, 0, 0 ) ) != 0 )
933 return hIcon;
934
935 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
936 bits = LockResource( handle );
937 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
938 !fCursor, 0x00030000, width, height, loadflags);
939 FreeResource( handle );
940
941 /* If shared icon, add to icon cache */
942
943 if (hIcon && 0 != (loadflags & LR_SHARED))
944 {
945 #if 1
946 NtUserSetCursorIconData((HICON)hIcon, NULL, NULL, hInstance, hRsrc,
947 (HRSRC)NULL);
948 #else
949 ICONINFO iconInfo;
950
951 if(NtUserGetIconInfo(ResIcon, &iconInfo, NULL, NULL, NULL, FALSE))
952 NtUserSetCursorIconData((HICON)hIcon, hinst, NULL, &iconInfo);
953 #endif
954 }
955
956 return hIcon;
957 }
958
959
960 /*************************************************************************
961 * CURSORICON_ExtCopy
962 *
963 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
964 *
965 * PARAMS
966 * Handle [I] handle to an Image
967 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
968 * iDesiredCX [I] The Desired width of the Image
969 * iDesiredCY [I] The desired height of the Image
970 * nFlags [I] The flags from CopyImage
971 *
972 * RETURNS
973 * Success: The new handle of the Image
974 *
975 * NOTES
976 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
977 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
978 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
979 *
980 *
981 */
982
983 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
984 INT iDesiredCX, INT iDesiredCY,
985 UINT nFlags)
986 {
987 HICON hNew=0;
988
989 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
990 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
991
992 if(hIcon == 0)
993 {
994 return 0;
995 }
996
997 /* Best Fit or Monochrome */
998 if( (nFlags & LR_COPYFROMRESOURCE
999 && (iDesiredCX > 0 || iDesiredCY > 0))
1000 || nFlags & LR_MONOCHROME)
1001 {
1002 FIXME("Copying from resource isn't implemented yet\n");
1003 hNew = CopyIcon(hIcon);
1004
1005 #if 0
1006 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1007
1008 /* Not Found in Cache, then do a straight copy
1009 */
1010 if(pIconCache == NULL)
1011 {
1012 hNew = CopyIcon( hIcon );
1013 if(nFlags & LR_COPYFROMRESOURCE)
1014 {
1015 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1016 }
1017 }
1018 else
1019 {
1020 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1021 LPBYTE pBits;
1022 HANDLE hMem;
1023 HRSRC hRsrc;
1024 DWORD dwBytesInRes;
1025 WORD wResId;
1026 CURSORICONDIR *pDir;
1027 CURSORICONDIRENTRY *pDirEntry;
1028 BOOL bIsIcon = (nType == IMAGE_ICON);
1029
1030 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1031 */
1032 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1033 || (iDesiredCX == 0 && iDesiredCY == 0))
1034 {
1035 iDesiredCY = GetSystemMetrics(bIsIcon ?
1036 SM_CYICON : SM_CYCURSOR);
1037 iDesiredCX = GetSystemMetrics(bIsIcon ?
1038 SM_CXICON : SM_CXCURSOR);
1039 }
1040
1041 /* Retrieve the CURSORICONDIRENTRY
1042 */
1043 if (!(hMem = LoadResource( pIconCache->hModule ,
1044 pIconCache->hGroupRsrc)))
1045 {
1046 return 0;
1047 }
1048 if (!(pDir = LockResource( hMem )))
1049 {
1050 return 0;
1051 }
1052
1053 /* Find Best Fit
1054 */
1055 if(bIsIcon)
1056 {
1057 pDirEntry = CURSORICON_FindBestIconRes(
1058 pDir, iDesiredCX, iDesiredCY, 256 );
1059 }
1060 else
1061 {
1062 pDirEntry = CURSORICON_FindBestCursorRes(
1063 pDir, iDesiredCX, iDesiredCY, 1);
1064 }
1065
1066 wResId = pDirEntry->wResId;
1067 dwBytesInRes = pDirEntry->dwBytesInRes;
1068 FreeResource(hMem);
1069
1070 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1071 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1072 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1073
1074 /* Get the Best Fit
1075 */
1076 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1077 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1078 {
1079 return 0;
1080 }
1081 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1082 {
1083 return 0;
1084 }
1085
1086 pBits = LockResource( hMem );
1087
1088 if(nFlags & LR_DEFAULTSIZE)
1089 {
1090 iTargetCY = GetSystemMetrics(SM_CYICON);
1091 iTargetCX = GetSystemMetrics(SM_CXICON);
1092 }
1093
1094 /* Create a New Icon with the proper dimension
1095 */
1096 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1097 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1098 FreeResource(hMem);
1099 }
1100 #endif
1101 }
1102 else hNew = CopyIcon( hIcon );
1103 return hNew;
1104 }
1105
1106
1107 /***********************************************************************
1108 * CreateCursor (USER32.@)
1109 */
1110 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1111 INT xHotSpot, INT yHotSpot,
1112 INT nWidth, INT nHeight,
1113 LPCVOID lpANDbits, LPCVOID lpXORbits )
1114 {
1115 ICONINFO info;
1116 HCURSOR hCursor;
1117
1118 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1119 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1120
1121 info.fIcon = FALSE;
1122 info.xHotspot = xHotSpot;
1123 info.yHotspot = yHotSpot;
1124 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1125 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1126 hCursor = CreateIconIndirect( &info );
1127 DeleteObject( info.hbmMask );
1128 DeleteObject( info.hbmColor );
1129 return hCursor;
1130 }
1131
1132
1133 /***********************************************************************
1134 * CreateIcon (USER32.@)
1135 *
1136 * Creates an icon based on the specified bitmaps. The bitmaps must be
1137 * provided in a device dependent format and will be resized to
1138 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1139 * depth. The provided bitmaps must be top-down bitmaps.
1140 * Although Windows does not support 15bpp(*) this API must support it
1141 * for Winelib applications.
1142 *
1143 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1144 * format!
1145 *
1146 * RETURNS
1147 * Success: handle to an icon
1148 * Failure: NULL
1149 *
1150 * FIXME: Do we need to resize the bitmaps?
1151 */
1152 HICON WINAPI CreateIcon(
1153 HINSTANCE hInstance, /* [in] the application's hInstance */
1154 int nWidth, /* [in] the width of the provided bitmaps */
1155 int nHeight, /* [in] the height of the provided bitmaps */
1156 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1157 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1158 const BYTE* lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1159 const BYTE* lpXORbits) /* [in] the icon's 'color' bitmap */
1160 {
1161 ICONINFO iinfo;
1162 HICON hIcon;
1163
1164 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1165 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1166
1167 iinfo.fIcon = TRUE;
1168 iinfo.xHotspot = nWidth / 2;
1169 iinfo.yHotspot = nHeight / 2;
1170 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1171 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1172
1173 hIcon = CreateIconIndirect( &iinfo );
1174
1175 DeleteObject( iinfo.hbmMask );
1176 DeleteObject( iinfo.hbmColor );
1177
1178 return hIcon;
1179 }
1180
1181
1182 /***********************************************************************
1183 * CopyIcon (USER32.@)
1184 */
1185 HICON WINAPI CopyIcon( HICON hIcon )
1186 {
1187 HICON hRetIcon = NULL;
1188 ICONINFO IconInfo;
1189
1190 if(GetIconInfo(hIcon, &IconInfo))
1191 {
1192 hRetIcon = CreateIconIndirect(&IconInfo);
1193 DeleteObject(IconInfo.hbmColor);
1194 DeleteObject(IconInfo.hbmMask);
1195 }
1196
1197 return hRetIcon;
1198 }
1199
1200
1201 /***********************************************************************
1202 * DestroyIcon (USER32.@)
1203 */
1204 BOOL WINAPI DestroyIcon( HICON hIcon )
1205 {
1206 TRACE_(icon)("%p\n", hIcon );
1207
1208 return NtUserDestroyCursor(hIcon, 0);
1209 }
1210
1211
1212 /***********************************************************************
1213 * DestroyCursor (USER32.@)
1214 */
1215 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1216 {
1217 if (GetCursor() == hCursor)
1218 {
1219 WARN_(cursor)("Destroying active cursor!\n" );
1220 return FALSE;
1221 }
1222 return DestroyIcon( hCursor );
1223 }
1224
1225 /***********************************************************************
1226 * DrawIcon (USER32.@)
1227 */
1228 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1229 {
1230 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1231 }
1232
1233 /***********************************************************************
1234 * SetCursor (USER32.@)
1235 *
1236 * Set the cursor shape.
1237 *
1238 * RETURNS
1239 * A handle to the previous cursor shape.
1240 */
1241 HCURSOR WINAPI /*DECLSPEC_HOTPATCH*/ SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1242 {
1243 return NtUserSetCursor(hCursor);
1244 }
1245
1246 /***********************************************************************
1247 * ShowCursor (USER32.@)
1248 */
1249 INT WINAPI /*DECLSPEC_HOTPATCH*/ ShowCursor( BOOL bShow )
1250 {
1251 return NtUserShowCursor(bShow);
1252 }
1253
1254 /***********************************************************************
1255 * GetCursor (USER32.@)
1256 */
1257 HCURSOR WINAPI GetCursor(void)
1258 {
1259 CURSORINFO ci;
1260 ci.cbSize = sizeof(CURSORINFO);
1261 if(NtUserGetCursorInfo(&ci))
1262 return ci.hCursor;
1263 else
1264 return (HCURSOR)0;
1265 }
1266
1267
1268 /***********************************************************************
1269 * ClipCursor (USER32.@)
1270 */
1271 BOOL WINAPI /*DECLSPEC_HOTPATCH*/ ClipCursor( const RECT *rect )
1272 {
1273 return NtUserClipCursor((RECT *)rect);
1274 }
1275
1276
1277 /***********************************************************************
1278 * GetClipCursor (USER32.@)
1279 */
1280 BOOL WINAPI /*DECLSPEC_HOTPATCH*/ GetClipCursor( RECT *rect )
1281 {
1282 return NtUserGetClipCursor(rect);
1283 }
1284
1285
1286 /***********************************************************************
1287 * SetSystemCursor (USER32.@)
1288 */
1289 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1290 {
1291 FIXME("(%p,%08x),stub!\n", hcur, id);
1292 return TRUE;
1293 }
1294
1295
1296 /**********************************************************************
1297 * LookupIconIdFromDirectoryEx (USER32.@)
1298 */
1299 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1300 INT width, INT height, UINT cFlag )
1301 {
1302 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1303 UINT retVal = 0;
1304 if( dir && !dir->idReserved && (dir->idType & 3) )
1305 {
1306 CURSORICONDIRENTRY* entry;
1307
1308 const HDC hdc = GetDC(0);
1309 const int depth = (cFlag & LR_MONOCHROME) ?
1310 1 : GetDeviceCaps(hdc, BITSPIXEL);
1311 ReleaseDC(0, hdc);
1312
1313 if( bIcon )
1314 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1315 else
1316 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1317
1318 if( entry ) retVal = entry->wResId;
1319 }
1320 else WARN_(cursor)("invalid resource directory\n");
1321 return retVal;
1322 }
1323
1324 /**********************************************************************
1325 * LookupIconIdFromDirectory (USER32.@)
1326 */
1327 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1328 {
1329 return LookupIconIdFromDirectoryEx( dir, bIcon,
1330 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1331 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1332 }
1333
1334 /***********************************************************************
1335 * LoadCursorW (USER32.@)
1336 */
1337 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1338 {
1339 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1340
1341 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1342 LR_SHARED | LR_DEFAULTSIZE );
1343 }
1344
1345 /***********************************************************************
1346 * LoadCursorA (USER32.@)
1347 */
1348 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1349 {
1350 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1351
1352 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1353 LR_SHARED | LR_DEFAULTSIZE );
1354 }
1355
1356 /***********************************************************************
1357 * LoadCursorFromFileW (USER32.@)
1358 */
1359 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1360 {
1361 TRACE("%s\n", debugstr_w(name));
1362
1363 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1364 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1365 }
1366
1367 /***********************************************************************
1368 * LoadCursorFromFileA (USER32.@)
1369 */
1370 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1371 {
1372 TRACE("%s\n", debugstr_a(name));
1373
1374 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1375 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1376 }
1377
1378 /***********************************************************************
1379 * LoadIconW (USER32.@)
1380 */
1381 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1382 {
1383 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1384
1385 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1386 LR_SHARED | LR_DEFAULTSIZE );
1387 }
1388
1389 /***********************************************************************
1390 * LoadIconA (USER32.@)
1391 */
1392 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1393 {
1394 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1395
1396 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1397 LR_SHARED | LR_DEFAULTSIZE );
1398 }
1399
1400 /**********************************************************************
1401 * GetIconInfo (USER32.@)
1402 */
1403 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1404 {
1405 return NtUserGetIconInfo(hIcon, iconinfo, 0, 0, 0, 0);
1406 }
1407
1408 /* copy an icon bitmap, even when it can't be selected into a DC */
1409 /* helper for CreateIconIndirect */
1410 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1411 HBITMAP src, int width, int height )
1412 {
1413 HDC hdc = CreateCompatibleDC( 0 );
1414
1415 if (!SelectObject( hdc, src )) /* do it the hard way */
1416 {
1417 BITMAPINFO *info;
1418 void *bits;
1419
1420 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1421 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1422 info->bmiHeader.biWidth = width;
1423 info->bmiHeader.biHeight = height;
1424 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1425 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1426 info->bmiHeader.biCompression = BI_RGB;
1427 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1428 info->bmiHeader.biXPelsPerMeter = 0;
1429 info->bmiHeader.biYPelsPerMeter = 0;
1430 info->bmiHeader.biClrUsed = 0;
1431 info->bmiHeader.biClrImportant = 0;
1432 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1433 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1434 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1435 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1436
1437 HeapFree( GetProcessHeap(), 0, bits );
1438 HeapFree( GetProcessHeap(), 0, info );
1439 }
1440 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1441
1442 DeleteDC( hdc );
1443 }
1444
1445 /**********************************************************************
1446 * CreateIconIndirect (USER32.@)
1447 */
1448 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1449 {
1450 BITMAP bmpXor, bmpAnd;
1451 HBITMAP color = 0, mask;
1452 int width, height;
1453 HDC hdc;
1454 ICONINFO iinfo;
1455
1456 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1457 iconinfo->hbmColor, iconinfo->hbmMask,
1458 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1459
1460 if (!iconinfo->hbmMask) return 0;
1461
1462 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1463 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1464 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1465 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1466
1467 if (iconinfo->hbmColor)
1468 {
1469 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1470 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1471 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1472 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1473
1474 width = bmpXor.bmWidth;
1475 height = bmpXor.bmHeight;
1476 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1477 {
1478 color = CreateBitmap( width, height, bmpXor.bmPlanes, bmpXor.bmBitsPixel, NULL );
1479 if(!color)
1480 {
1481 ERR("Unable to create color bitmap!\n");
1482 return NULL;
1483 }
1484 mask = CreateBitmap( width, height, 1, 1, NULL );
1485 if(!mask)
1486 {
1487 ERR("Unable to create mask bitmap!\n");
1488 DeleteObject(color);
1489 return NULL;
1490 }
1491 }
1492 else
1493 {
1494 mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1495 if(!mask)
1496 {
1497 ERR("Unable to create mask bitmap!\n");
1498 return NULL;
1499 }
1500 }
1501 }
1502 else
1503 {
1504 width = bmpAnd.bmWidth;
1505 height = bmpAnd.bmHeight;
1506 mask = CreateBitmap( width, height, 1, 1, NULL );
1507 }
1508
1509 hdc = CreateCompatibleDC( 0 );
1510 SelectObject( hdc, mask );
1511 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1512
1513 if (color)
1514 {
1515 SelectObject( hdc, color );
1516 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1517 }
1518 else if (iconinfo->hbmColor)
1519 {
1520 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1521 }
1522 else height /= 2;
1523
1524 DeleteDC( hdc );
1525
1526 iinfo.hbmColor = color ;
1527 iinfo.hbmMask = mask ;
1528 iinfo.fIcon = iconinfo->fIcon;
1529 if (iinfo.fIcon)
1530 {
1531 iinfo.xHotspot = width / 2;
1532 iinfo.yHotspot = height / 2;
1533 }
1534 else
1535 {
1536 iinfo.xHotspot = iconinfo->xHotspot;
1537 iinfo.yHotspot = iconinfo->yHotspot;
1538 }
1539
1540 return CreateCursorIconHandle(&iinfo);
1541 }
1542
1543 /******************************************************************************
1544 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1545 *
1546 * NOTES
1547 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1548 *
1549 * PARAMS
1550 * hdc [I] Handle to device context
1551 * x0 [I] X coordinate of upper left corner
1552 * y0 [I] Y coordinate of upper left corner
1553 * hIcon [I] Handle to icon to draw
1554 * cxWidth [I] Width of icon
1555 * cyWidth [I] Height of icon
1556 * istep [I] Index of frame in animated cursor
1557 * hbr [I] Handle to background brush
1558 * flags [I] Icon-drawing flags
1559 *
1560 * RETURNS
1561 * Success: TRUE
1562 * Failure: FALSE
1563 */
1564 BOOL WINAPI DrawIconEx( HDC hdc, INT xLeft, INT yTop, HICON hIcon,
1565 INT cxWidth, INT cyWidth, UINT istepIfAniCur,
1566 HBRUSH hbrFlickerFreeDraw, UINT diFlags )
1567 {
1568 return NtUserDrawIconEx(hdc, xLeft, yTop, hIcon, cxWidth, cyWidth,
1569 istepIfAniCur, hbrFlickerFreeDraw, diFlags,
1570 0, 0);
1571 }
1572
1573 /***********************************************************************
1574 * DIB_FixColorsToLoadflags
1575 *
1576 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1577 * are in loadflags
1578 */
1579 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1580 {
1581 int colors;
1582 COLORREF c_W, c_S, c_F, c_L, c_C;
1583 int incr,i;
1584 RGBQUAD *ptr;
1585 int bitmap_type;
1586 LONG width;
1587 LONG height;
1588 WORD bpp;
1589 DWORD compr;
1590
1591 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
1592 {
1593 WARN_(resource)("Invalid bitmap\n");
1594 return;
1595 }
1596
1597 if (bpp > 8) return;
1598
1599 if (bitmap_type == 0) /* BITMAPCOREHEADER */
1600 {
1601 incr = 3;
1602 colors = 1 << bpp;
1603 }
1604 else
1605 {
1606 incr = 4;
1607 colors = bmi->bmiHeader.biClrUsed;
1608 if (colors > 256) colors = 256;
1609 if (!colors && (bpp <= 8)) colors = 1 << bpp;
1610 }
1611
1612 c_W = GetSysColor(COLOR_WINDOW);
1613 c_S = GetSysColor(COLOR_3DSHADOW);
1614 c_F = GetSysColor(COLOR_3DFACE);
1615 c_L = GetSysColor(COLOR_3DLIGHT);
1616
1617 if (loadflags & LR_LOADTRANSPARENT) {
1618 switch (bpp) {
1619 case 1: pix = pix >> 7; break;
1620 case 4: pix = pix >> 4; break;
1621 case 8: break;
1622 default:
1623 WARN_(resource)("(%d): Unsupported depth\n", bpp);
1624 return;
1625 }
1626 if (pix >= colors) {
1627 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1628 return;
1629 }
1630 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1631 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1632 ptr->rgbBlue = GetBValue(c_W);
1633 ptr->rgbGreen = GetGValue(c_W);
1634 ptr->rgbRed = GetRValue(c_W);
1635 }
1636 if (loadflags & LR_LOADMAP3DCOLORS)
1637 for (i=0; i<colors; i++) {
1638 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1639 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1640 if (c_C == RGB(128, 128, 128)) {
1641 ptr->rgbRed = GetRValue(c_S);
1642 ptr->rgbGreen = GetGValue(c_S);
1643 ptr->rgbBlue = GetBValue(c_S);
1644 } else if (c_C == RGB(192, 192, 192)) {
1645 ptr->rgbRed = GetRValue(c_F);
1646 ptr->rgbGreen = GetGValue(c_F);
1647 ptr->rgbBlue = GetBValue(c_F);
1648 } else if (c_C == RGB(223, 223, 223)) {
1649 ptr->rgbRed = GetRValue(c_L);
1650 ptr->rgbGreen = GetGValue(c_L);
1651 ptr->rgbBlue = GetBValue(c_L);
1652 }
1653 }
1654 }
1655
1656
1657 /**********************************************************************
1658 * BITMAP_Load
1659 */
1660 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
1661 INT desiredx, INT desiredy, UINT loadflags )
1662 {
1663 HBITMAP hbitmap = 0, orig_bm;
1664 HRSRC hRsrc;
1665 HGLOBAL handle;
1666 char *ptr = NULL;
1667 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
1668 int size;
1669 BYTE pix;
1670 char *bits;
1671 LONG width, height, new_width, new_height;
1672 WORD bpp_dummy;
1673 DWORD compr_dummy, offbits = 0;
1674 INT bm_type;
1675 HDC screen_mem_dc = NULL;
1676
1677 if (!(loadflags & LR_LOADFROMFILE))
1678 {
1679 if (!instance)
1680 {
1681 /* OEM bitmap: try to load the resource from user32.dll */
1682 instance = User32Instance;
1683 }
1684
1685 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
1686 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
1687
1688 if ((info = LockResource( handle )) == NULL) return 0;
1689 }
1690 else
1691 {
1692 BITMAPFILEHEADER * bmfh;
1693
1694 if (!(ptr = map_fileW( name, NULL ))) return 0;
1695 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
1696 bmfh = (BITMAPFILEHEADER *)ptr;
1697 if (bmfh->bfType != 0x4d42 /* 'BM' */)
1698 {
1699 WARN("Invalid/unsupported bitmap format!\n");
1700 goto end_close;
1701 }
1702 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
1703 }
1704
1705 size = bitmap_info_size(info, DIB_RGB_COLORS);
1706 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
1707 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
1708
1709 if (!fix_info || !scaled_info) goto end;
1710 memcpy(fix_info, info, size);
1711
1712 pix = *((LPBYTE)info + size);
1713 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
1714
1715 memcpy(scaled_info, fix_info, size);
1716 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
1717 &bpp_dummy, &compr_dummy);
1718 if(desiredx != 0)
1719 new_width = desiredx;
1720 else
1721 new_width = width;
1722
1723 if(desiredy != 0)
1724 new_height = height > 0 ? desiredy : -desiredy;
1725 else
1726 new_height = height;
1727
1728 if(bm_type == 0)
1729 {
1730 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
1731 core->bcWidth = new_width;
1732 core->bcHeight = new_height;
1733 }
1734 else
1735 {
1736 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
1737 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
1738 WARN("Broken BitmapInfoHeader!\n");
1739 goto end;
1740 }
1741
1742 scaled_info->bmiHeader.biWidth = new_width;
1743 scaled_info->bmiHeader.biHeight = new_height;
1744 }
1745
1746 if (new_height < 0) new_height = -new_height;
1747
1748 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1749 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
1750
1751 bits = (char *)info + (offbits ? offbits : size);
1752
1753 if (loadflags & LR_CREATEDIBSECTION)
1754 {
1755 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
1756 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
1757 }
1758 else
1759 {
1760 if (is_dib_monochrome(fix_info))
1761 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
1762 else
1763 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
1764 }
1765
1766 orig_bm = SelectObject(screen_mem_dc, hbitmap);
1767 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
1768 SelectObject(screen_mem_dc, orig_bm);
1769
1770 end:
1771 if (screen_mem_dc) DeleteDC(screen_mem_dc);
1772 HeapFree(GetProcessHeap(), 0, scaled_info);
1773 HeapFree(GetProcessHeap(), 0, fix_info);
1774 end_close:
1775 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
1776
1777 return hbitmap;
1778 }
1779
1780 /**********************************************************************
1781 * LoadImageA (USER32.@)
1782 *
1783 * See LoadImageW.
1784 */
1785 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
1786 INT desiredx, INT desiredy, UINT loadflags)
1787 {
1788 HANDLE res;
1789 LPWSTR u_name;
1790
1791 if (IS_INTRESOURCE(name))
1792 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
1793
1794 __TRY {
1795 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1796 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1797 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
1798 }
1799 __EXCEPT_PAGE_FAULT {
1800 SetLastError( ERROR_INVALID_PARAMETER );
1801 return 0;
1802 }
1803 __ENDTRY
1804 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
1805 HeapFree(GetProcessHeap(), 0, u_name);
1806 return res;
1807 }
1808
1809
1810 /******************************************************************************
1811 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
1812 *
1813 * PARAMS
1814 * hinst [I] Handle of instance that contains image
1815 * name [I] Name of image
1816 * type [I] Type of image
1817 * desiredx [I] Desired width
1818 * desiredy [I] Desired height
1819 * loadflags [I] Load flags
1820 *
1821 * RETURNS
1822 * Success: Handle to newly loaded image
1823 * Failure: NULL
1824 *
1825 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
1826 */
1827 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
1828 INT desiredx, INT desiredy, UINT loadflags )
1829 {
1830 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
1831 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
1832
1833 if (loadflags & LR_DEFAULTSIZE) {
1834 if (type == IMAGE_ICON) {
1835 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
1836 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
1837 } else if (type == IMAGE_CURSOR) {
1838 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
1839 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
1840 }
1841 }
1842 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
1843 switch (type) {
1844 case IMAGE_BITMAP:
1845 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
1846
1847 case IMAGE_ICON:
1848 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
1849 if (screen_dc)
1850 {
1851 return CURSORICON_Load(hinst, name, desiredx, desiredy,
1852 GetDeviceCaps(screen_dc, BITSPIXEL),
1853 FALSE, loadflags);
1854 }
1855 break;
1856
1857 case IMAGE_CURSOR:
1858 return CURSORICON_Load(hinst, name, desiredx, desiredy,
1859 1, TRUE, loadflags);
1860 }
1861 return 0;
1862 }
1863
1864 /******************************************************************************
1865 * CopyImage (USER32.@) Creates new image and copies attributes to it
1866 *
1867 * PARAMS
1868 * hnd [I] Handle to image to copy
1869 * type [I] Type of image to copy
1870 * desiredx [I] Desired width of new image
1871 * desiredy [I] Desired height of new image
1872 * flags [I] Copy flags
1873 *
1874 * RETURNS
1875 * Success: Handle to newly created image
1876 * Failure: NULL
1877 *
1878 * BUGS
1879 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
1880 * all other versions (95/2000/XP have been tested) ignore it.
1881 *
1882 * NOTES
1883 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
1884 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
1885 * the copy will have the same depth as the screen.
1886 * The content of the image will only be copied if the bit depth of the
1887 * original image is compatible with the bit depth of the screen, or
1888 * if the source is a DIB section.
1889 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
1890 */
1891 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
1892 INT desiredy, UINT flags )
1893 {
1894 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
1895 hnd, type, desiredx, desiredy, flags);
1896
1897 switch (type)
1898 {
1899 case IMAGE_BITMAP:
1900 {
1901 HBITMAP res = NULL;
1902 DIBSECTION ds;
1903 int objSize;
1904 BITMAPINFO * bi;
1905
1906 objSize = GetObjectW( hnd, sizeof(ds), &ds );
1907 if (!objSize) return 0;
1908 if ((desiredx < 0) || (desiredy < 0)) return 0;
1909
1910 if (flags & LR_COPYFROMRESOURCE)
1911 {
1912 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
1913 }
1914
1915 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
1916 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
1917
1918 /* Allocate memory for a BITMAPINFOHEADER structure and a
1919 color table. The maximum number of colors in a color table
1920 is 256 which corresponds to a bitmap with depth 8.
1921 Bitmaps with higher depths don't have color tables. */
1922 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1923 if (!bi) return 0;
1924
1925 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
1926 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
1927 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
1928 bi->bmiHeader.biCompression = BI_RGB;
1929
1930 if (flags & LR_CREATEDIBSECTION)
1931 {
1932 /* Create a DIB section. LR_MONOCHROME is ignored */
1933 void * bits;
1934 HDC dc = CreateCompatibleDC(NULL);
1935
1936 if (objSize == sizeof(DIBSECTION))
1937 {
1938 /* The source bitmap is a DIB.
1939 Get its attributes to create an exact copy */
1940 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
1941 }
1942
1943 /* Get the color table or the color masks */
1944 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
1945
1946 bi->bmiHeader.biWidth = desiredx;
1947 bi->bmiHeader.biHeight = desiredy;
1948 bi->bmiHeader.biSizeImage = 0;
1949
1950 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
1951 DeleteDC(dc);
1952 }
1953 else
1954 {
1955 /* Create a device-dependent bitmap */
1956
1957 BOOL monochrome = (flags & LR_MONOCHROME);
1958
1959 if (objSize == sizeof(DIBSECTION))
1960 {
1961 /* The source bitmap is a DIB section.
1962 Get its attributes */
1963 HDC dc = CreateCompatibleDC(NULL);
1964 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
1965 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
1966 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
1967 DeleteDC(dc);
1968
1969 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
1970 {
1971 /* Look if the colors of the DIB are black and white */
1972
1973 monochrome =
1974 (bi->bmiColors[0].rgbRed == 0xff
1975 && bi->bmiColors[0].rgbGreen == 0xff
1976 && bi->bmiColors[0].rgbBlue == 0xff
1977 && bi->bmiColors[0].rgbReserved == 0
1978 && bi->bmiColors[1].rgbRed == 0
1979 && bi->bmiColors[1].rgbGreen == 0
1980 && bi->bmiColors[1].rgbBlue == 0
1981 && bi->bmiColors[1].rgbReserved == 0)
1982 ||
1983 (bi->bmiColors[0].rgbRed == 0
1984 && bi->bmiColors[0].rgbGreen == 0
1985 && bi->bmiColors[0].rgbBlue == 0
1986 && bi->bmiColors[0].rgbReserved == 0
1987 && bi->bmiColors[1].rgbRed == 0xff
1988 && bi->bmiColors[1].rgbGreen == 0xff
1989 && bi->bmiColors[1].rgbBlue == 0xff
1990 && bi->bmiColors[1].rgbReserved == 0);
1991 }
1992 }
1993 else if (!monochrome)
1994 {
1995 monochrome = ds.dsBm.bmBitsPixel == 1;
1996 }
1997
1998 if (monochrome)
1999 {
2000 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2001 }
2002 else
2003 {
2004 HDC screenDC = GetDC(NULL);
2005 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2006 ReleaseDC(NULL, screenDC);
2007 }
2008 }
2009
2010 if (res)
2011 {
2012 /* Only copy the bitmap if it's a DIB section or if it's
2013 compatible to the screen */
2014 BOOL copyContents;
2015
2016 if (objSize == sizeof(DIBSECTION))
2017 {
2018 copyContents = TRUE;
2019 }
2020 else
2021 {
2022 HDC screenDC = GetDC(NULL);
2023 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2024 ReleaseDC(NULL, screenDC);
2025
2026 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2027 }
2028
2029 if (copyContents)
2030 {
2031 /* The source bitmap may already be selected in a device context,
2032 use GetDIBits/StretchDIBits and not StretchBlt */
2033
2034 HDC dc;
2035 void * bits;
2036
2037 dc = CreateCompatibleDC(NULL);
2038
2039 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2040 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2041 bi->bmiHeader.biSizeImage = 0;
2042 bi->bmiHeader.biClrUsed = 0;
2043 bi->bmiHeader.biClrImportant = 0;
2044
2045 /* Fill in biSizeImage */
2046 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2047 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2048
2049 if (bits)
2050 {
2051 HBITMAP oldBmp;
2052
2053 /* Get the image bits of the source bitmap */
2054 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2055
2056 /* Copy it to the destination bitmap */
2057 oldBmp = SelectObject(dc, res);
2058 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2059 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2060 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2061 SelectObject(dc, oldBmp);
2062
2063 HeapFree(GetProcessHeap(), 0, bits);
2064 }
2065
2066 DeleteDC(dc);
2067 }
2068
2069 if (flags & LR_COPYDELETEORG)
2070 {
2071 DeleteObject(hnd);
2072 }
2073 }
2074 HeapFree(GetProcessHeap(), 0, bi);
2075 return res;
2076 }
2077 case IMAGE_ICON:
2078 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2079 case IMAGE_CURSOR:
2080 /* Should call CURSORICON_ExtCopy but more testing
2081 * needs to be done before we change this
2082 */
2083 if (flags) FIXME("Flags are ignored\n");
2084 return CopyCursor(hnd);
2085 }
2086 return 0;
2087 }
2088
2089
2090 /******************************************************************************
2091 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2092 *
2093 * RETURNS
2094 * Success: Handle to specified bitmap
2095 * Failure: NULL
2096 */
2097 HBITMAP WINAPI LoadBitmapW(
2098 HINSTANCE instance, /* [in] Handle to application instance */
2099 LPCWSTR name) /* [in] Address of bitmap resource name */
2100 {
2101 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2102 }
2103
2104 /**********************************************************************
2105 * LoadBitmapA (USER32.@)
2106 *
2107 * See LoadBitmapW.
2108 */
2109 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2110 {
2111 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2112 }
2113
2114 HCURSOR
2115 CursorIconToCursor(HICON hIcon,
2116 BOOL SemiTransparent)
2117 {
2118 UNIMPLEMENTED;
2119 return 0;
2120 }
2121
2122 /*
2123 * @implemented
2124 */
2125 BOOL
2126 WINAPI
2127 SetCursorPos(int X,
2128 int Y)
2129 {
2130 return NtUserSetCursorPos(X,Y);
2131 }
2132
2133 /*
2134 * @implemented
2135 */
2136 BOOL
2137 WINAPI
2138 GetCursorPos(LPPOINT lpPoint)
2139 {
2140 BOOL res;
2141 /* Windows doesn't check if lpPoint == NULL, we do */
2142 if(!lpPoint)
2143 {
2144 SetLastError(ERROR_INVALID_PARAMETER);
2145 return FALSE;
2146 }
2147
2148 res = NtUserGetCursorPos(lpPoint);
2149
2150 return res;
2151 }
2152
2153 /* INTERNAL ******************************************************************/
2154
2155 /* This callback routine is called directly after switching to gui mode */
2156 NTSTATUS
2157 WINAPI
2158 User32SetupDefaultCursors(PVOID Arguments,
2159 ULONG ArgumentLength)
2160 {
2161 BOOL *DefaultCursor = (BOOL*)Arguments;
2162 LRESULT Result = TRUE;
2163
2164 if(*DefaultCursor)
2165 {
2166 /* set default cursor */
2167 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
2168 }
2169 else
2170 {
2171 /* FIXME load system cursor scheme */
2172 SetCursor(0);
2173 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
2174 }
2175
2176 return(ZwCallbackReturn(&Result, sizeof(LRESULT), STATUS_SUCCESS));
2177 }