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