cursoricon.c 83.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Cursor and icon support
 *
 * Copyright 1995 Alexandre Julliard
Alexandre Julliard's avatar
Alexandre Julliard committed
5 6
 *           1996 Martin Von Loewis
 *           1997 Alex Korobka
7
 *           1998 Turchanov Sergey
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 */

/*
 * Theory:
 *
 * Cursors and icons are stored in a global heap block, with the
 * following layout:
 *
 * CURSORICONINFO info;
 * BYTE[]         ANDbits;
 * BYTE[]         XORbits;
 *
 * The bits structures are in the format of a device-dependent bitmap.
 *
 * This layout is very sub-optimal, as the bitmap bits are stored in
 * the X client instead of in the server like other bitmaps; however,
 * some programs (notably Paint Brush) expect to be able to manipulate
 * the bits directly :-(
 */

Steven Edwards's avatar
Steven Edwards committed
42 43 44
#include "config.h"
#include "wine/port.h"

45
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
46 47
#include <string.h>
#include <stdlib.h>
48

49
#include "windef.h"
50
#include "winbase.h"
51
#include "wingdi.h"
52
#include "winerror.h"
53
#include "wine/winbase16.h"
54
#include "wine/winuser16.h"
55
#include "wine/exception.h"
56
#include "wine/debug.h"
57
#include "user_private.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
58

59
WINE_DEFAULT_DEBUG_CHANNEL(cursor);
60 61
WINE_DECLARE_DEBUG_CHANNEL(icon);
WINE_DECLARE_DEBUG_CHANNEL(resource);
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include "pshpack1.h"

typedef struct {
    BYTE bWidth;
    BYTE bHeight;
    BYTE bColorCount;
    BYTE bReserved;
    WORD xHotspot;
    WORD yHotspot;
    DWORD dwDIBSize;
    DWORD dwDIBOffset;
} CURSORICONFILEDIRENTRY;

typedef struct
{
    WORD                idReserved;
    WORD                idType;
    WORD                idCount;
    CURSORICONFILEDIRENTRY  idEntries[1];
} CURSORICONFILEDIR;

#include "poppack.h"

#define CID_RESOURCE  0x0001
#define CID_WIN32     0x0004
#define CID_NONSHARED 0x0008
89

90
static RECT CURSOR_ClipRect;       /* Cursor clipping rect */
Alexandre Julliard's avatar
Alexandre Julliard committed
91

92
static HDC screen_dc;
93

94 95
static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};

96 97 98 99 100
/**********************************************************************
 * ICONCACHE for cursors/icons loaded with LR_SHARED.
 *
 * FIXME: This should not be allocated on the system heap, but on a
 *        subsystem-global heap (i.e. one for all Win16 processes,
101
 *        and one for each Win32 process).
102 103 104 105 106 107 108
 */
typedef struct tagICONCACHE
{
    struct tagICONCACHE *next;

    HMODULE              hModule;
    HRSRC                hRsrc;
109
    HRSRC                hGroupRsrc;
110
    HICON                hIcon;
111 112 113 114 115 116

    INT                  count;

} ICONCACHE;

static ICONCACHE *IconAnchor = NULL;
117 118 119 120 121 122

static CRITICAL_SECTION IconCrst;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &IconCrst,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123
      0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
124 125 126
};
static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };

127
static const WORD ICON_HOTSPOT = 0x4242;
128

129 130 131 132 133

/***********************************************************************
 *             map_fileW
 *
 * Helper function to map a file to memory:
134
 *  name			-	file name
135
 *  [RETURN] ptr		-	pointer to mapped file
136
 *  [RETURN] filesize           -       pointer size of file to be stored if not NULL
137
 */
138
static void *map_fileW( LPCWSTR name, LPDWORD filesize )
139 140 141 142 143 144 145 146
{
    HANDLE hFile, hMapping;
    LPVOID ptr = NULL;

    hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
                         OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
    if (hFile != INVALID_HANDLE_VALUE)
    {
147
        hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
148 149 150 151
        if (hMapping)
        {
            ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
            CloseHandle( hMapping );
152 153
            if (filesize)
                *filesize = GetFileSize( hFile, NULL );
154
        }
155
        CloseHandle( hFile );
156 157 158 159 160
    }
    return ptr;
}


161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/***********************************************************************
 *           get_bitmap_width_bytes
 *
 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
 * data.
 */
static int get_bitmap_width_bytes( int width, int bpp )
{
    switch(bpp)
    {
    case 1:
        return 2 * ((width+15) / 16);
    case 4:
        return 2 * ((width+3) / 4);
    case 24:
        width *= 3;
        /* fall through */
    case 8:
        return width + (width & 1);
    case 16:
    case 15:
        return width * 2;
    case 32:
        return width * 4;
    default:
        WARN("Unknown depth %d, please report.\n", bpp );
    }
    return -1;
}


192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/***********************************************************************
 *          get_dib_width_bytes
 *
 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
 */
static int get_dib_width_bytes( int width, int depth )
{
    int words;

    switch(depth)
    {
    case 1:  words = (width + 31) / 32; break;
    case 4:  words = (width + 7) / 8; break;
    case 8:  words = (width + 3) / 4; break;
    case 15:
    case 16: words = (width + 1) / 2; break;
    case 24: words = (width * 3 + 3)/4; break;
    default:
        WARN("(%d): Unsupported depth\n", depth );
        /* fall through */
    case 32:
        words = width;
    }
    return 4 * words;
}


/***********************************************************************
 *           bitmap_info_size
 *
 * Return the size of the bitmap info structure including color table.
 */
static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
{
226
    int colors, masks = 0;
227 228 229

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
230
        const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
231 232 233 234 235 236 237
        colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
        return sizeof(BITMAPCOREHEADER) + colors *
             ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
    }
    else  /* assume BITMAPINFOHEADER */
    {
        colors = info->bmiHeader.biClrUsed;
238 239
        if (colors > 256) /* buffer overflow otherwise */
                colors = 256;
240 241
        if (!colors && (info->bmiHeader.biBitCount <= 8))
            colors = 1 << info->bmiHeader.biBitCount;
242 243
        if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
        return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
244 245 246 247 248
               ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
    }
}


249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/***********************************************************************
 *          is_dib_monochrome
 *
 * Returns whether a DIB can be converted to a monochrome DDB.
 *
 * A DIB can be converted if its color table contains only black and
 * white. Black must be the first color in the color table.
 *
 * Note : If the first color in the color table is white followed by
 *        black, we can't convert it to a monochrome DDB with
 *        SetDIBits, because black and white would be inverted.
 */
static BOOL is_dib_monochrome( const BITMAPINFO* info )
{
    if (info->bmiHeader.biBitCount != 1) return FALSE;

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
267
        const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
268

269 270 271 272 273 274 275 276 277 278 279 280 281
        /* Check if the first color is black */
        if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
        {
            rgb++;

            /* Check if the second color is white */
            return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
                 && (rgb->rgbtBlue == 0xff));
        }
        else return FALSE;
    }
    else  /* assume BITMAPINFOHEADER */
    {
Eric Pouech's avatar
Eric Pouech committed
282
        const RGBQUAD *rgb = info->bmiColors;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

        /* Check if the first color is black */
        if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
            (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
        {
            rgb++;

            /* Check if the second color is white */
            return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
                 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
        }
        else return FALSE;
    }
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/***********************************************************************
 *           DIB_GetBitmapInfo
 *
 * Get the info from a bitmap header.
 * Return 1 for INFOHEADER, 0 for COREHEADER,
 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
 */
static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
                              LONG *height, WORD *bpp, DWORD *compr )
{
    if (header->biSize == sizeof(BITMAPINFOHEADER))
    {
        *width  = header->biWidth;
        *height = header->biHeight;
        *bpp    = header->biBitCount;
        *compr  = header->biCompression;
        return 1;
    }
    if (header->biSize == sizeof(BITMAPCOREHEADER))
    {
318
        const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
319 320 321 322 323 324 325 326
        *width  = core->bcWidth;
        *height = core->bcHeight;
        *bpp    = core->bcBitCount;
        *compr  = 0;
        return 0;
    }
    if (header->biSize == sizeof(BITMAPV4HEADER))
    {
327
        const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
328 329 330 331 332 333 334 335
        *width  = v4hdr->bV4Width;
        *height = v4hdr->bV4Height;
        *bpp    = v4hdr->bV4BitCount;
        *compr  = v4hdr->bV4V4Compression;
        return 4;
    }
    if (header->biSize == sizeof(BITMAPV5HEADER))
    {
336
        const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
337 338 339 340 341 342
        *width  = v5hdr->bV5Width;
        *height = v5hdr->bV5Height;
        *bpp    = v5hdr->bV5BitCount;
        *compr  = v5hdr->bV5Compression;
        return 5;
    }
343
    ERR("(%d): unknown/wrong size for header\n", header->biSize );
344 345
    return -1;
}
346

347 348 349
/**********************************************************************
 *	    CURSORICON_FindSharedIcon
 */
350
static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
351
{
352
    HICON hIcon = 0;
353 354 355 356 357 358 359 360
    ICONCACHE *ptr;

    EnterCriticalSection( &IconCrst );

    for ( ptr = IconAnchor; ptr; ptr = ptr->next )
        if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
        {
            ptr->count++;
361
            hIcon = ptr->hIcon;
362 363 364 365 366
            break;
        }

    LeaveCriticalSection( &IconCrst );

367
    return hIcon;
368 369
}

370
/*************************************************************************
371
 * CURSORICON_FindCache
372
 *
Andreas Mohr's avatar
Andreas Mohr committed
373
 * Given a handle, find the corresponding cache element
374 375
 *
 * PARAMS
376
 *      Handle     [I] handle to an Image
377 378 379 380 381 382
 *
 * RETURNS
 *     Success: The cache entry
 *     Failure: NULL
 *
 */
383
static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
384 385 386 387 388 389 390
{
    ICONCACHE *ptr;
    ICONCACHE *pRet=NULL;
    BOOL IsFound = FALSE;

    EnterCriticalSection( &IconCrst );

391
    for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
392
    {
393
        if ( hIcon == ptr->hIcon )
394 395 396 397 398 399 400 401 402 403 404
        {
            IsFound = TRUE;
            pRet = ptr;
        }
    }

    LeaveCriticalSection( &IconCrst );

    return pRet;
}

405 406 407
/**********************************************************************
 *	    CURSORICON_AddSharedIcon
 */
408
static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
409
{
410
    ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
411 412 413 414
    if ( !ptr ) return;

    ptr->hModule = hModule;
    ptr->hRsrc   = hRsrc;
415
    ptr->hIcon  = hIcon;
416
    ptr->hGroupRsrc = hGroupRsrc;
417 418 419 420 421 422 423 424 425 426 427
    ptr->count   = 1;

    EnterCriticalSection( &IconCrst );
    ptr->next    = IconAnchor;
    IconAnchor   = ptr;
    LeaveCriticalSection( &IconCrst );
}

/**********************************************************************
 *	    CURSORICON_DelSharedIcon
 */
428
static INT CURSORICON_DelSharedIcon( HICON hIcon )
429 430 431 432 433 434 435
{
    INT count = -1;
    ICONCACHE *ptr;

    EnterCriticalSection( &IconCrst );

    for ( ptr = IconAnchor; ptr; ptr = ptr->next )
436
        if ( ptr->hIcon == hIcon )
437 438 439 440 441 442 443 444 445 446 447 448 449 450
        {
            if ( ptr->count > 0 ) ptr->count--;
            count = ptr->count;
            break;
        }

    LeaveCriticalSection( &IconCrst );

    return count;
}

/**********************************************************************
 *	    CURSORICON_FreeModuleIcons
 */
451
void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
452 453
{
    ICONCACHE **ptr = &IconAnchor;
454
    HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
455 456 457 458 459 460 461 462 463

    EnterCriticalSection( &IconCrst );

    while ( *ptr )
    {
        if ( (*ptr)->hModule == hModule )
        {
            ICONCACHE *freePtr = *ptr;
            *ptr = freePtr->next;
464

465
            GlobalFree16(HICON_16(freePtr->hIcon));
466
            HeapFree( GetProcessHeap(), 0, freePtr );
467 468 469 470 471 472 473 474
            continue;
        }
        ptr = &(*ptr)->next;
    }

    LeaveCriticalSection( &IconCrst );
}

475 476 477 478 479 480 481
/*
 *  The following macro functions account for the irregularities of
 *   accessing cursor and icon resources in files and resource entries.
 */
typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
                              int *width, int *height, int *bits );

Alexandre Julliard's avatar
Alexandre Julliard committed
482 483 484 485 486
/**********************************************************************
 *	    CURSORICON_FindBestIcon
 *
 * Find the icon closest to the requested size and number of colors.
 */
487 488
static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
                                    int width, int height, int colors )
Alexandre Julliard's avatar
Alexandre Julliard committed
489
{
490
    int i, cx, cy, bits, bestEntry = -1;
491 492
    UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
    UINT iTempXDiff, iTempYDiff, iTempColorDiff;
Alexandre Julliard's avatar
Alexandre Julliard committed
493

494 495 496
    /* Find Best Fit */
    iTotalDiff = 0xFFFFFFFF;
    iColorDiff = 0xFFFFFFFF;
497
    for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
498
    {
499 500
        iTempXDiff = abs(width - cx);
        iTempYDiff = abs(height - cy);
Alexandre Julliard's avatar
Alexandre Julliard committed
501

502
        if(iTotalDiff > (iTempXDiff + iTempYDiff))
Alexandre Julliard's avatar
Alexandre Julliard committed
503
        {
504 505
            iXDiff = iTempXDiff;
            iYDiff = iTempYDiff;
506
            iTotalDiff = iXDiff + iYDiff;
Alexandre Julliard's avatar
Alexandre Julliard committed
507
        }
508
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
509

510
    /* Find Best Colors for Best Fit */
511
    for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
512
    {
513
        if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
Alexandre Julliard's avatar
Alexandre Julliard committed
514
        {
515
            iTempColorDiff = abs(colors - (1<<bits));
516
            if(iColorDiff > iTempColorDiff)
517
            {
518
                bestEntry = i;
519
                iColorDiff = iTempColorDiff;
520
            }
521 522
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
523 524 525 526

    return bestEntry;
}

527 528 529 530 531 532 533 534 535 536
static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
                                        int *width, int *height, int *bits )
{
    CURSORICONDIR *resdir = dir;
    ICONRESDIR *icon;

    if ( resdir->idCount <= n )
        return FALSE;
    icon = &resdir->idEntries[n].ResInfo.icon;
    *width = icon->bWidth;
537
    *height = icon->bHeight;
538 539 540
    *bits = resdir->idEntries[n].wBitCount;
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
541 542 543 544 545

/**********************************************************************
 *	    CURSORICON_FindBestCursor
 *
 * Find the cursor closest to the requested size.
546 547
 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
 *        ignored too
Alexandre Julliard's avatar
Alexandre Julliard committed
548
 */
549 550
static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
                                      int width, int height, int color )
Alexandre Julliard's avatar
Alexandre Julliard committed
551
{
552
    int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
553

554 555 556 557
    /* Double height to account for AND and XOR masks */

    height *= 2;

Alexandre Julliard's avatar
Alexandre Julliard committed
558 559 560
    /* First find the largest one smaller than or equal to the requested size*/

    maxwidth = maxheight = 0;
561 562 563 564 565
    for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
    {
        if ((cx <= width) && (cy <= height) &&
            (cx > maxwidth) && (cy > maxheight) &&
            (bits == 1))
Alexandre Julliard's avatar
Alexandre Julliard committed
566
        {
567 568 569
            bestEntry = i;
            maxwidth  = cx;
            maxheight = cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
570
        }
571 572
    }
    if (bestEntry != -1) return bestEntry;
Alexandre Julliard's avatar
Alexandre Julliard committed
573 574 575 576

    /* Now find the smallest one larger than the requested size */

    maxwidth = maxheight = 255;
577 578
    for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
    {
579 580
        if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
            (bestEntry==-1))
Alexandre Julliard's avatar
Alexandre Julliard committed
581
        {
582 583 584
            bestEntry = i;
            maxwidth  = cx;
            maxheight = cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
585
        }
586
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
587 588 589 590

    return bestEntry;
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
                                          int *width, int *height, int *bits )
{
    CURSORICONDIR *resdir = dir;
    CURSORDIR *cursor;

    if ( resdir->idCount <= n )
        return FALSE;
    cursor = &resdir->idEntries[n].ResInfo.cursor;
    *width = cursor->wWidth;
    *height = cursor->wHeight;
    *bits = resdir->idEntries[n].wBitCount;
    return TRUE;
}

static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
                                      int width, int height, int colors )
{
    int n;

    n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
                                 width, height, colors );
    if ( n < 0 )
        return NULL;
    return &dir->idEntries[n];
}

static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
                                      int width, int height, int color )
{
    int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
                                   width, height, color );
    if ( n < 0 )
        return NULL;
    return &dir->idEntries[n];
}

628 629
static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
                                     int *width, int *height, int *bits )
630
{
631 632
    CURSORICONFILEDIR *filedir = dir;
    CURSORICONFILEDIRENTRY *entry;
633

634 635 636 637 638 639
    if ( filedir->idCount <= n )
        return FALSE;
    entry = &filedir->idEntries[n];
    *width = entry->bWidth;
    *height = entry->bHeight;
    *bits = entry->bColorCount;
640
    return TRUE;
641
}
Alexandre Julliard's avatar
Alexandre Julliard committed
642

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
                                      int width, int height, int color )
{
    int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
                                       width, height, color );
    if ( n < 0 )
        return NULL;
    return &dir->idEntries[n];
}

static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
                                      int width, int height, int color )
{
    int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
                                     width, height, color );
    if ( n < 0 )
        return NULL;
    return &dir->idEntries[n];
}
Alexandre Julliard's avatar
Alexandre Julliard committed
662

Alexandre Julliard's avatar
Alexandre Julliard committed
663
/**********************************************************************
664
 *		CreateIconFromResourceEx (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
665
 *
666
 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
Alexandre Julliard's avatar
Alexandre Julliard committed
667 668
 *        with cbSize parameter as well.
 */
669 670 671 672
HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
                                       BOOL bIcon, DWORD dwVersion,
                                       INT width, INT height,
                                       UINT cFlag )
Alexandre Julliard's avatar
Alexandre Julliard committed
673
{
674
    HGLOBAL16 hObj;
675
    static HDC hdcMem;
Alexandre Julliard's avatar
Alexandre Julliard committed
676
    int sizeAnd, sizeXor;
677
    HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
678
    BITMAP bmpXor, bmpAnd;
679
    POINT16 hotspot;
Alexandre Julliard's avatar
Alexandre Julliard committed
680
    BITMAPINFO *bmi;
681 682
    BOOL DoStretch;
    INT size;
Alexandre Julliard's avatar
Alexandre Julliard committed
683

684 685 686
    hotspot.x = ICON_HOTSPOT;
    hotspot.y = ICON_HOTSPOT;

687
    TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
688
                   bits, cbSize, dwVersion, width, height,
689
                                  bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
Alexandre Julliard's avatar
Alexandre Julliard committed
690 691
    if (dwVersion == 0x00020000)
    {
692 693
        FIXME_(cursor)("\t2.xx resources are not supported\n");
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
694 695 696
    }

    if (bIcon)
697
        bmi = (BITMAPINFO *)bits;
Alexandre Julliard's avatar
Alexandre Julliard committed
698
    else /* get the hotspot */
Alexandre Julliard's avatar
Alexandre Julliard committed
699
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
700
        POINT16 *pt = (POINT16 *)bits;
Alexandre Julliard's avatar
Alexandre Julliard committed
701 702 703 704
        hotspot = *pt;
        bmi = (BITMAPINFO *)(pt + 1);
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
705
    /* Check bitmap header */
Alexandre Julliard's avatar
Alexandre Julliard committed
706

Alexandre Julliard's avatar
Alexandre Julliard committed
707
    if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
708 709
         (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER)  ||
          bmi->bmiHeader.biCompression != BI_RGB) )
Alexandre Julliard's avatar
Alexandre Julliard committed
710
    {
711
          WARN_(cursor)("\tinvalid resource bitmap header.\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
712
          return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
713 714
    }

715 716 717 718 719 720 721
    size = bitmap_info_size( bmi, DIB_RGB_COLORS );

    if (!width) width = bmi->bmiHeader.biWidth;
    if (!height) height = bmi->bmiHeader.biHeight/2;
    DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
      (bmi->bmiHeader.biWidth != width);

722
    if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
723
    if (screen_dc)
Alexandre Julliard's avatar
Alexandre Julliard committed
724
    {
725
        BITMAPINFO* pInfo;
Alexandre Julliard's avatar
Alexandre Julliard committed
726 727

        /* Make sure we have room for the monochrome bitmap later on.
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
         * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
         * up to and including the biBitCount. In-memory icon resource
         * format is as follows:
         *
         *   BITMAPINFOHEADER   icHeader  // DIB header
         *   RGBQUAD         icColors[]   // Color table
         *   BYTE            icXOR[]      // DIB bits for XOR mask
         *   BYTE            icAND[]      // DIB bits for AND mask
         */

        if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
                                max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
        {
            memcpy( pInfo, bmi, size );
            pInfo->bmiHeader.biHeight /= 2;

            /* Create the XOR bitmap */

            if (DoStretch) {
747 748
                if(bIcon)
                {
749
                    hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
750 751 752 753 754 755 756
                }
                else
                {
                    hXorBits = CreateBitmap(width, height, 1, 1, NULL);
                }
                if(hXorBits)
                {
757
                HBITMAP hOld;
758 759 760 761 762 763 764 765 766 767
                BOOL res = FALSE;

                if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
                if (hdcMem) {
                    hOld = SelectObject(hdcMem, hXorBits);
                    res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
                                        bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
                                        (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
                    SelectObject(hdcMem, hOld);
                }
768 769 770
                if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
              }
            } else {
771 772 773 774 775
              if (is_dib_monochrome(bmi)) {
                  hXorBits = CreateBitmap(width, height, 1, 1, NULL);
                  SetDIBits(screen_dc, hXorBits, 0, height,
                     (char*)bmi + size, pInfo, DIB_RGB_COLORS);
              }
776
              else
777 778
                  hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
                     CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS); 
779
            }
780

781 782 783
            if( hXorBits )
            {
                char* xbits = (char *)bmi + size +
784 785
                    get_dib_width_bytes( bmi->bmiHeader.biWidth,
                                         bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
Alexandre Julliard's avatar
Alexandre Julliard committed
786

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
                pInfo->bmiHeader.biBitCount = 1;
                if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
                {
                    RGBQUAD *rgb = pInfo->bmiColors;

                    pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
                    rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
                    rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
                    rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
                }
                else
                {
                    RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);

                    rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
                    rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
                }

                /* Create the AND bitmap */

            if (DoStretch) {
              if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
                HBITMAP hOld;
810 811 812 813 814 815 816 817 818 819
                BOOL res = FALSE;

                if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
                if (hdcMem) {
                    hOld = SelectObject(hdcMem, hAndBits);
                    res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
                                        pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
                                        xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
                    SelectObject(hdcMem, hOld);
                }
820 821 822
                if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
              }
            } else {
823
              hAndBits = CreateBitmap(width, height, 1, 1, NULL);
824

825 826
              if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
                             xbits, pInfo, DIB_RGB_COLORS);
827

828 829 830 831 832
            }
                if( !hAndBits ) DeleteObject( hXorBits );
            }
            HeapFree( GetProcessHeap(), 0, pInfo );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
833
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
834

835
    if( !hXorBits || !hAndBits )
Alexandre Julliard's avatar
Alexandre Julliard committed
836
    {
837 838
        WARN_(cursor)("\tunable to create an icon bitmap.\n");
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
839
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
840 841

    /* Now create the CURSORICONINFO structure */
842 843 844 845
    GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
    GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
    sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
    sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
Alexandre Julliard's avatar
Alexandre Julliard committed
846

847
    hObj = GlobalAlloc16( GMEM_MOVEABLE,
848
                     sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
849
    if (hObj)
Alexandre Julliard's avatar
Alexandre Julliard committed
850
    {
851
        CURSORICONINFO *info;
Alexandre Julliard's avatar
Alexandre Julliard committed
852

853 854 855 856 857 858 859 860
        info = (CURSORICONINFO *)GlobalLock16( hObj );
        info->ptHotSpot.x   = hotspot.x;
        info->ptHotSpot.y   = hotspot.y;
        info->nWidth        = bmpXor.bmWidth;
        info->nHeight       = bmpXor.bmHeight;
        info->nWidthBytes   = bmpXor.bmWidthBytes;
        info->bPlanes       = bmpXor.bmPlanes;
        info->bBitsPerPixel = bmpXor.bmBitsPixel;
Alexandre Julliard's avatar
Alexandre Julliard committed
861

862
        /* Transfer the bitmap bits to the CURSORICONINFO structure */
Alexandre Julliard's avatar
Alexandre Julliard committed
863

864 865 866
        GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
        GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
        GlobalUnlock16( hObj );
Alexandre Julliard's avatar
Alexandre Julliard committed
867
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
868

869
    DeleteObject( hAndBits );
870
    DeleteObject( hXorBits );
871
    return HICON_32(hObj);
Alexandre Julliard's avatar
Alexandre Julliard committed
872 873 874
}


Alexandre Julliard's avatar
Alexandre Julliard committed
875
/**********************************************************************
876
 *		CreateIconFromResource (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
877
 */
878 879
HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
                                           BOOL bIcon, DWORD dwVersion)
Alexandre Julliard's avatar
Alexandre Julliard committed
880
{
881
    return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
Alexandre Julliard's avatar
Alexandre Julliard committed
882 883 884
}


885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
                             INT width, INT height, INT colors,
                             BOOL fCursor, UINT loadflags)
{
    CURSORICONFILEDIRENTRY *entry;
    CURSORICONFILEDIR *dir;
    DWORD filesize = 0;
    HICON hIcon = 0;
    LPBYTE bits;

    TRACE("loading %s\n", debugstr_w( filename ));

    bits = map_fileW( filename, &filesize );
    if (!bits)
        return hIcon;

901 902 903 904 905 906 907
    /* Check for .ani. */
    if (memcmp( bits, "RIFF", 4 ) == 0)
    {
        FIXME("No support for .ani cursors.\n");
        goto end;
    }

908 909 910 911 912 913 914 915
    dir = (CURSORICONFILEDIR*) bits;
    if ( filesize < sizeof(*dir) )
        goto end;

    if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
        goto end;

    if ( fCursor )
916
        entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    else
        entry = CURSORICON_FindBestIconFile( dir, width, height, colors );

    if ( !entry )
        goto end;

    /* check that we don't run off the end of the file */
    if ( entry->dwDIBOffset > filesize )
        goto end;
    if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
        goto end;

    hIcon = CreateIconFromResourceEx( &bits[entry->dwDIBOffset], entry->dwDIBSize,
                                      !fCursor, 0x00030000, width, height, loadflags );
end:
    TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
    UnmapViewOfFile( bits );
    return hIcon;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
937
/**********************************************************************
938
 *          CURSORICON_Load
Alexandre Julliard's avatar
Alexandre Julliard committed
939
 *
940
 * Load a cursor or icon from resource or file.
Alexandre Julliard's avatar
Alexandre Julliard committed
941
 */
942
static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
943 944
                             INT width, INT height, INT colors,
                             BOOL fCursor, UINT loadflags)
Alexandre Julliard's avatar
Alexandre Julliard committed
945
{
946 947
    HANDLE handle = 0;
    HICON hIcon = 0;
948
    HRSRC hRsrc, hGroupRsrc;
949 950 951
    CURSORICONDIR *dir;
    CURSORICONDIRENTRY *dirEntry;
    LPBYTE bits;
952 953
    WORD wResId;
    DWORD dwBytesInRes;
954

955 956 957
    TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
          hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);

958
    if ( loadflags & LR_LOADFROMFILE )    /* Load from file */
959
        return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
Alexandre Julliard's avatar
Alexandre Julliard committed
960

961
    if (!hInstance) hInstance = user32_module;  /* Load OEM cursor/icon */
962

963
    /* Normalize hInstance (must be uniquely represented for icon cache) */
964

965 966
    if (!HIWORD( hInstance ))
        hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
Alexandre Julliard's avatar
Alexandre Julliard committed
967

968
    /* Get directory resource ID */
Alexandre Julliard's avatar
Alexandre Julliard committed
969

970 971 972 973
    if (!(hRsrc = FindResourceW( hInstance, name,
                                 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
        return 0;
    hGroupRsrc = hRsrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
974

975
    /* Find the best entry in the directory */
976

977 978 979 980 981 982 983 984 985 986
    if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
    if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
    if (fCursor)
        dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
    else
        dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
    if (!dirEntry) return 0;
    wResId = dirEntry->wResId;
    dwBytesInRes = dirEntry->dwBytesInRes;
    FreeResource( handle );
987

988
    /* Load the resource */
989

990 991
    if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
                                (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
992

993 994 995 996
    /* If shared icon, check whether it was already loaded */
    if (    (loadflags & LR_SHARED)
         && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
        return hIcon;
997

998 999 1000 1001 1002
    if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
    bits = (LPBYTE)LockResource( handle );
    hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
                                      !fCursor, 0x00030000, width, height, loadflags);
    FreeResource( handle );
1003

1004
    /* If shared icon, add to icon cache */
1005

1006 1007
    if ( hIcon && (loadflags & LR_SHARED) )
        CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1008

1009
    return hIcon;
Alexandre Julliard's avatar
Alexandre Julliard committed
1010 1011
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1012 1013 1014 1015 1016
/***********************************************************************
 *           CURSORICON_Copy
 *
 * Make a copy of a cursor or icon.
 */
1017
static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1018 1019 1020
{
    char *ptrOld, *ptrNew;
    int size;
1021 1022
    HICON16 hOld = HICON_16(hIcon);
    HICON16 hNew;
Alexandre Julliard's avatar
Alexandre Julliard committed
1023

1024 1025 1026
    if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
    if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
    size = GlobalSize16( hOld );
Alexandre Julliard's avatar
Alexandre Julliard committed
1027
    hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1028
    FarSetOwner16( hNew, hInst16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1029
    ptrNew = (char *)GlobalLock16( hNew );
Alexandre Julliard's avatar
Alexandre Julliard committed
1030
    memcpy( ptrNew, ptrOld, size );
1031
    GlobalUnlock16( hOld );
Alexandre Julliard's avatar
Alexandre Julliard committed
1032
    GlobalUnlock16( hNew );
1033
    return HICON_32(hNew);
Alexandre Julliard's avatar
Alexandre Julliard committed
1034 1035
}

1036
/*************************************************************************
1037
 * CURSORICON_ExtCopy
1038 1039 1040 1041
 *
 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
 *
 * PARAMS
1042
 *      Handle     [I] handle to an Image
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
 *      nType      [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
 *      iDesiredCX [I] The Desired width of the Image
 *      iDesiredCY [I] The desired height of the Image
 *      nFlags     [I] The flags from CopyImage
 *
 * RETURNS
 *     Success: The new handle of the Image
 *
 * NOTES
 *     LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1053
 *     LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1054 1055
 *     LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
 *
1056
 *
1057 1058
 */

1059
static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1060 1061
                                INT iDesiredCX, INT iDesiredCY,
                                UINT nFlags)
1062
{
1063
    HICON hNew=0;
1064

1065 1066
    TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
                 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1067

1068
    if(hIcon == 0)
1069
    {
1070
        return 0;
1071 1072 1073 1074 1075
    }

    /* Best Fit or Monochrome */
    if( (nFlags & LR_COPYFROMRESOURCE
        && (iDesiredCX > 0 || iDesiredCY > 0))
1076
        || nFlags & LR_MONOCHROME)
1077
    {
1078
        ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1079

1080
        /* Not Found in Cache, then do a straight copy
1081 1082 1083
        */
        if(pIconCache == NULL)
        {
1084
            hNew = CURSORICON_Copy(0, hIcon);
1085 1086 1087 1088 1089 1090 1091
            if(nFlags & LR_COPYFROMRESOURCE)
            {
                TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
            }
        }
        else
        {
1092
            int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
            LPBYTE pBits;
            HANDLE hMem;
            HRSRC hRsrc;
            DWORD dwBytesInRes;
            WORD wResId;
            CURSORICONDIR *pDir;
            CURSORICONDIRENTRY *pDirEntry;
            BOOL bIsIcon = (nType == IMAGE_ICON);

            /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
            */
            if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
                || (iDesiredCX == 0 && iDesiredCY == 0))
            {
1107
                iDesiredCY = GetSystemMetrics(bIsIcon ?
1108
                    SM_CYICON : SM_CYCURSOR);
1109
                iDesiredCX = GetSystemMetrics(bIsIcon ?
1110 1111 1112
                    SM_CXICON : SM_CXCURSOR);
            }

1113
            /* Retrieve the CURSORICONDIRENTRY
1114
            */
1115 1116
            if (!(hMem = LoadResource( pIconCache->hModule ,
                            pIconCache->hGroupRsrc)))
1117 1118 1119
            {
                return 0;
            }
1120
            if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1121 1122 1123 1124
            {
                return 0;
            }

1125
            /* Find Best Fit
1126 1127 1128
            */
            if(bIsIcon)
            {
1129 1130
                pDirEntry = CURSORICON_FindBestIconRes(
                                pDir, iDesiredCX, iDesiredCY, 256 );
1131 1132 1133
            }
            else
            {
1134
                pDirEntry = CURSORICON_FindBestCursorRes(
1135 1136 1137 1138 1139 1140 1141
                                pDir, iDesiredCX, iDesiredCY, 1);
            }

            wResId = pDirEntry->wResId;
            dwBytesInRes = pDirEntry->dwBytesInRes;
            FreeResource(hMem);

1142
            TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1143
                wResId, dwBytesInRes,  pDirEntry->ResInfo.icon.bWidth,
1144 1145 1146 1147 1148
                pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);

            /* Get the Best Fit
            */
            if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1149
                MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1150 1151 1152
            {
                return 0;
            }
1153
            if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1154 1155 1156 1157 1158
            {
                return 0;
            }

            pBits = (LPBYTE)LockResource( hMem );
1159

1160 1161 1162
            if(nFlags & LR_DEFAULTSIZE)
            {
                iTargetCY = GetSystemMetrics(SM_CYICON);
1163
                iTargetCX = GetSystemMetrics(SM_CXICON);
1164
            }
1165 1166 1167

            /* Create a New Icon with the proper dimension
            */
1168
            hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1169 1170 1171 1172
                       bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
            FreeResource(hMem);
        }
    }
1173
    else hNew = CURSORICON_Copy(0, hIcon);
1174 1175 1176
    return hNew;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1177 1178

/***********************************************************************
1179
 *		CreateCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1180
 */
1181 1182 1183
HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
                                 INT xHotSpot, INT yHotSpot,
                                 INT nWidth, INT nHeight,
Alexandre Julliard's avatar
Alexandre Julliard committed
1184
                                 LPCVOID lpANDbits, LPCVOID lpXORbits )
Alexandre Julliard's avatar
Alexandre Julliard committed
1185
{
1186
    CURSORICONINFO info;
Alexandre Julliard's avatar
Alexandre Julliard committed
1187

1188
    TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1189
                    nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1190 1191 1192 1193 1194 1195 1196 1197 1198

    info.ptHotSpot.x = xHotSpot;
    info.ptHotSpot.y = yHotSpot;
    info.nWidth = nWidth;
    info.nHeight = nHeight;
    info.nWidthBytes = 0;
    info.bPlanes = 1;
    info.bBitsPerPixel = 1;

1199
    return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
Alexandre Julliard's avatar
Alexandre Julliard committed
1200 1201 1202 1203
}


/***********************************************************************
1204
 *		CreateIcon (USER.407)
Alexandre Julliard's avatar
Alexandre Julliard committed
1205
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1206 1207 1208
HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
                             INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
                             LPCVOID lpANDbits, LPCVOID lpXORbits )
Alexandre Julliard's avatar
Alexandre Julliard committed
1209
{
1210
    CURSORICONINFO info;
Alexandre Julliard's avatar
Alexandre Julliard committed
1211

1212
    TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1213
                  nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1214

1215 1216
    info.ptHotSpot.x = ICON_HOTSPOT;
    info.ptHotSpot.y = ICON_HOTSPOT;
1217 1218 1219 1220 1221 1222
    info.nWidth = nWidth;
    info.nHeight = nHeight;
    info.nWidthBytes = 0;
    info.bPlanes = bPlanes;
    info.bBitsPerPixel = bBitsPixel;

1223
    return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
Alexandre Julliard's avatar
Alexandre Julliard committed
1224 1225 1226 1227
}


/***********************************************************************
1228
 *		CreateIcon (USER32.@)
1229
 *
1230 1231 1232
 *  Creates an icon based on the specified bitmaps. The bitmaps must be
 *  provided in a device dependent format and will be resized to
 *  (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1233
 *  depth. The provided bitmaps must be top-down bitmaps.
1234
 *  Although Windows does not support 15bpp(*) this API must support it
1235 1236
 *  for Winelib applications.
 *
1237
 *  (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1238 1239
 *      format!
 *
1240 1241 1242 1243
 * RETURNS
 *  Success: handle to an icon
 *  Failure: NULL
 *
1244
 * FIXME: Do we need to resize the bitmaps?
Alexandre Julliard's avatar
Alexandre Julliard committed
1245
 */
1246
HICON WINAPI CreateIcon(
1247
    HINSTANCE hInstance,  /* [in] the application's hInstance */
1248 1249 1250 1251 1252 1253
    INT       nWidth,     /* [in] the width of the provided bitmaps */
    INT       nHeight,    /* [in] the height of the provided bitmaps */
    BYTE      bPlanes,    /* [in] the number of planes in the provided bitmaps */
    BYTE      bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
    LPCVOID   lpANDbits,  /* [in] a monochrome bitmap representing the icon's mask */
    LPCVOID   lpXORbits)  /* [in] the icon's 'color' bitmap */
Alexandre Julliard's avatar
Alexandre Julliard committed
1254
{
1255
    ICONINFO iinfo;
1256
    HICON hIcon;
Alexandre Julliard's avatar
Alexandre Julliard committed
1257

1258 1259
    TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
                 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1260

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    iinfo.fIcon = TRUE;
    iinfo.xHotspot = ICON_HOTSPOT;
    iinfo.yHotspot = ICON_HOTSPOT;
    iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
    iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );

    hIcon = CreateIconIndirect( &iinfo );

    DeleteObject( iinfo.hbmMask );
    DeleteObject( iinfo.hbmColor );
1271 1272

    return hIcon;
Alexandre Julliard's avatar
Alexandre Julliard committed
1273 1274 1275 1276
}


/***********************************************************************
1277
 *		CreateCursorIconIndirect (USER.408)
Alexandre Julliard's avatar
Alexandre Julliard committed
1278
 */
1279
HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
Alexandre Julliard's avatar
Alexandre Julliard committed
1280 1281 1282
                                           CURSORICONINFO *info,
                                           LPCVOID lpANDbits,
                                           LPCVOID lpXORbits )
Alexandre Julliard's avatar
Alexandre Julliard committed
1283
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1284
    HGLOBAL16 handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1285 1286 1287 1288
    char *ptr;
    int sizeAnd, sizeXor;

    hInstance = GetExePtr( hInstance );  /* Make it a module handle */
Alexandre Julliard's avatar
Alexandre Julliard committed
1289
    if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1290
    info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
Alexandre Julliard's avatar
Alexandre Julliard committed
1291
    sizeXor = info->nHeight * info->nWidthBytes;
1292
    sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1293
    if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
Alexandre Julliard's avatar
Alexandre Julliard committed
1294 1295
                                  sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
        return 0;
1296
    FarSetOwner16( handle, hInstance );
Alexandre Julliard's avatar
Alexandre Julliard committed
1297
    ptr = (char *)GlobalLock16( handle );
Alexandre Julliard's avatar
Alexandre Julliard committed
1298 1299 1300
    memcpy( ptr, info, sizeof(*info) );
    memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
    memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
Alexandre Julliard's avatar
Alexandre Julliard committed
1301
    GlobalUnlock16( handle );
Alexandre Julliard's avatar
Alexandre Julliard committed
1302 1303 1304 1305 1306
    return handle;
}


/***********************************************************************
1307
 *		CopyIcon (USER.368)
Alexandre Julliard's avatar
Alexandre Julliard committed
1308
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1309
HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1310
{
1311
    TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1312
    return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
Alexandre Julliard's avatar
Alexandre Julliard committed
1313 1314 1315 1316
}


/***********************************************************************
1317
 *		CopyIcon (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1318
 */
1319
HICON WINAPI CopyIcon( HICON hIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1320
{
1321
    TRACE_(icon)("%p\n", hIcon );
1322
    return CURSORICON_Copy( 0, hIcon );
Alexandre Julliard's avatar
Alexandre Julliard committed
1323
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1324 1325 1326


/***********************************************************************
1327
 *		CopyCursor (USER.369)
Alexandre Julliard's avatar
Alexandre Julliard committed
1328
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1329
HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
Alexandre Julliard's avatar
Alexandre Julliard committed
1330
{
1331
    TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1332
    return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
Alexandre Julliard's avatar
Alexandre Julliard committed
1333 1334
}

1335
/**********************************************************************
1336
 *		DestroyIcon32 (USER.610)
1337 1338
 *
 * This routine is actually exported from Win95 USER under the name
1339 1340
 * DestroyIcon32 ...  The behaviour implemented here should mimic
 * the Win95 one exactly, especially the return values, which
1341 1342
 * depend on the setting of various flags.
 */
1343
WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1344 1345 1346
{
    WORD retv;

1347
    TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1348 1349 1350

    /* Check whether destroying active cursor */

1351
    if ( get_user_thread_info()->cursor == HICON_32(handle) )
1352
    {
1353
        WARN_(cursor)("Destroying active cursor!\n" );
1354
        return FALSE;
1355 1356 1357 1358 1359 1360
    }

    /* Try shared cursor/icon first */

    if ( !(flags & CID_NONSHARED) )
    {
1361
        INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

        if ( count != -1 )
            return (flags & CID_WIN32)? TRUE : (count == 0);

        /* FIXME: OEM cursors/icons should be recognized */
    }

    /* Now assume non-shared cursor/icon */

    retv = GlobalFree16( handle );
    return (flags & CID_RESOURCE)? retv : TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1374

Alexandre Julliard's avatar
Alexandre Julliard committed
1375
/***********************************************************************
1376
 *		DestroyIcon (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1377
 */
1378
BOOL WINAPI DestroyIcon( HICON hIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1379
{
1380
    return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
Alexandre Julliard's avatar
Alexandre Julliard committed
1381 1382
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1383 1384

/***********************************************************************
1385
 *		DestroyCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1386
 */
1387
BOOL WINAPI DestroyCursor( HCURSOR hCursor )
Alexandre Julliard's avatar
Alexandre Julliard committed
1388
{
1389
    return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
Alexandre Julliard's avatar
Alexandre Julliard committed
1390 1391 1392
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1393
/***********************************************************************
1394
 *		DrawIcon (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1395
 */
1396
BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1397 1398
{
    CURSORICONINFO *ptr;
1399 1400
    HDC hMemDC;
    HBITMAP hXorBits, hAndBits;
Alexandre Julliard's avatar
Alexandre Julliard committed
1401 1402
    COLORREF oldFg, oldBg;

1403 1404
    TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);

1405
    if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1406 1407
    if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
    hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
Alexandre Julliard's avatar
Alexandre Julliard committed
1408
                               (char *)(ptr+1) );
1409
    hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
Alexandre Julliard's avatar
Alexandre Julliard committed
1410
                               ptr->bBitsPerPixel, (char *)(ptr + 1)
1411
                        + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1412 1413
    oldFg = SetTextColor( hdc, RGB(0,0,0) );
    oldBg = SetBkColor( hdc, RGB(255,255,255) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1414 1415 1416

    if (hXorBits && hAndBits)
    {
1417 1418 1419 1420 1421
        HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
        BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
        SelectObject( hMemDC, hXorBits );
        BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
        SelectObject( hMemDC, hBitTemp );
Alexandre Julliard's avatar
Alexandre Julliard committed
1422
    }
1423 1424 1425
    DeleteDC( hMemDC );
    if (hXorBits) DeleteObject( hXorBits );
    if (hAndBits) DeleteObject( hAndBits );
1426
    GlobalUnlock16(HICON_16(hIcon));
1427 1428
    SetTextColor( hdc, oldFg );
    SetBkColor( hdc, oldBg );
Alexandre Julliard's avatar
Alexandre Julliard committed
1429 1430 1431 1432
    return TRUE;
}

/***********************************************************************
1433
 *		DumpIcon (USER.459)
Alexandre Julliard's avatar
Alexandre Julliard committed
1434
 */
1435
DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
Alexandre Julliard's avatar
Alexandre Julliard committed
1436
                       SEGPTR *lpXorBits, SEGPTR *lpAndBits )
Alexandre Julliard's avatar
Alexandre Julliard committed
1437
{
1438
    CURSORICONINFO *info = MapSL( pInfo );
Alexandre Julliard's avatar
Alexandre Julliard committed
1439 1440 1441 1442
    int sizeAnd, sizeXor;

    if (!info) return 0;
    sizeXor = info->nHeight * info->nWidthBytes;
1443
    sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1444 1445
    if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
    if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
Alexandre Julliard's avatar
Alexandre Julliard committed
1446 1447 1448 1449 1450
    if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
    return MAKELONG( sizeXor, sizeXor );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1451
/***********************************************************************
1452
 *		SetCursor (USER32.@)
1453 1454 1455 1456
 *
 * Set the cursor shape.
 *
 * RETURNS
Alexandre Julliard's avatar
Alexandre Julliard committed
1457
 *	A handle to the previous cursor shape.
Alexandre Julliard's avatar
Alexandre Julliard committed
1458
 */
1459 1460
HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
{
1461
    struct user_thread_info *thread_info = get_user_thread_info();
1462
    HCURSOR hOldCursor;
Alexandre Julliard's avatar
Alexandre Julliard committed
1463

1464
    if (hCursor == thread_info->cursor) return hCursor;  /* No change */
1465
    TRACE("%p\n", hCursor);
1466 1467
    hOldCursor = thread_info->cursor;
    thread_info->cursor = hCursor;
Alexandre Julliard's avatar
Alexandre Julliard committed
1468
    /* Change the cursor shape only if it is visible */
1469
    if (thread_info->cursor_count >= 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
1470
    {
1471
        USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1472
        GlobalUnlock16(HCURSOR_16(hCursor));
Alexandre Julliard's avatar
Alexandre Julliard committed
1473
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1474 1475 1476
    return hOldCursor;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1477
/***********************************************************************
1478
 *		ShowCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1479
 */
1480
INT WINAPI ShowCursor( BOOL bShow )
Alexandre Julliard's avatar
Alexandre Julliard committed
1481
{
1482
    struct user_thread_info *thread_info = get_user_thread_info();
1483

1484
    TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
Alexandre Julliard's avatar
Alexandre Julliard committed
1485 1486 1487

    if (bShow)
    {
1488
        if (++thread_info->cursor_count == 0) /* Show it */
1489
        {
1490
            USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1491
            GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1492
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1493 1494 1495
    }
    else
    {
1496 1497
        if (--thread_info->cursor_count == -1) /* Hide it */
            USER_Driver->pSetCursor( NULL );
Alexandre Julliard's avatar
Alexandre Julliard committed
1498
    }
1499
    return thread_info->cursor_count;
Alexandre Julliard's avatar
Alexandre Julliard committed
1500 1501
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1502
/***********************************************************************
1503
 *		GetCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1504
 */
1505
HCURSOR WINAPI GetCursor(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1506
{
1507
    return get_user_thread_info()->cursor;
Alexandre Julliard's avatar
Alexandre Julliard committed
1508 1509 1510 1511
}


/***********************************************************************
1512
 *		ClipCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1513
 */
1514
BOOL WINAPI ClipCursor( const RECT *rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
1515
{
1516 1517 1518 1519 1520 1521 1522
    RECT virt;

    SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
                          GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
    OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
                       GetSystemMetrics( SM_YVIRTUALSCREEN ) );

1523 1524 1525
    TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
           wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );

1526 1527 1528
    if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
        CURSOR_ClipRect = virt;

1529
    USER_Driver->pClipCursor( rect );
Alexandre Julliard's avatar
Alexandre Julliard committed
1530 1531 1532 1533 1534
    return TRUE;
}


/***********************************************************************
1535
 *		GetClipCursor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1536
 */
1537
BOOL WINAPI GetClipCursor( RECT *rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
1538
{
1539 1540 1541
    /* If this is first time - initialize the rect */
    if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );

1542
    return CopyRect( rect, &CURSOR_ClipRect );
Alexandre Julliard's avatar
Alexandre Julliard committed
1543 1544
}

1545 1546 1547 1548 1549 1550

/***********************************************************************
 *		SetSystemCursor (USER32.@)
 */
BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
{
1551
    FIXME("(%p,%08x),stub!\n",  hcur, id);
1552 1553 1554 1555
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1556
/**********************************************************************
1557
 *		LookupIconIdFromDirectoryEx (USER.364)
Alexandre Julliard's avatar
Alexandre Julliard committed
1558 1559 1560
 *
 * FIXME: exact parameter sizes
 */
1561
INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1562
                                            INT16 width, INT16 height, UINT16 cFlag )
1563 1564 1565 1566 1567 1568 1569 1570 1571
{
    return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
}

/**********************************************************************
 *		LookupIconIdFromDirectoryEx (USER32.@)
 */
INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
             INT width, INT height, UINT cFlag )
Alexandre Julliard's avatar
Alexandre Julliard committed
1572
{
1573
    CURSORICONDIR       *dir = (CURSORICONDIR*)xdir;
1574
    UINT retVal = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1575 1576
    if( dir && !dir->idReserved && (dir->idType & 3) )
    {
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
        CURSORICONDIRENTRY* entry;
        HDC hdc;
        UINT palEnts;
        int colors;
        hdc = GetDC(0);
        palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
        if (palEnts == 0)
            palEnts = 256;
        colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;

        ReleaseDC(0, hdc);

        if( bIcon )
1590
            entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1591
        else
1592
            entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1593

1594
        if( entry ) retVal = entry->wResId;
Alexandre Julliard's avatar
Alexandre Julliard committed
1595
    }
1596
    else WARN_(cursor)("invalid resource directory\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1597 1598 1599 1600
    return retVal;
}

/**********************************************************************
1601
 *              LookupIconIdFromDirectory (USER.?)
Alexandre Julliard's avatar
Alexandre Julliard committed
1602
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1603
INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1604
{
1605
    return LookupIconIdFromDirectoryEx16( dir, bIcon,
1606 1607
           bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
           bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
Alexandre Julliard's avatar
Alexandre Julliard committed
1608
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1609

Alexandre Julliard's avatar
Alexandre Julliard committed
1610
/**********************************************************************
1611
 *              LookupIconIdFromDirectory (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1612
 */
1613
INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1614
{
1615
    return LookupIconIdFromDirectoryEx( dir, bIcon,
1616 1617
           bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
           bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
Alexandre Julliard's avatar
Alexandre Julliard committed
1618 1619
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1620
/**********************************************************************
1621
 *              GetIconID (USER.455)
Alexandre Julliard's avatar
Alexandre Julliard committed
1622
 */
1623
WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
Alexandre Julliard's avatar
Alexandre Julliard committed
1624
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1625
    LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
Alexandre Julliard's avatar
Alexandre Julliard committed
1626

1627
    TRACE_(cursor)("hRes=%04x, entries=%i\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1628
                    hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1629 1630 1631

    switch(resType)
    {
1632 1633 1634 1635 1636 1637 1638
        case RT_CURSOR:
             return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
                          GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
        case RT_ICON:
             return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
                          GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
        default:
1639
             WARN_(cursor)("invalid res type %d\n", resType );
Alexandre Julliard's avatar
Alexandre Julliard committed
1640
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1641 1642 1643
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1644
/**********************************************************************
1645
 *              LoadCursorIconHandler (USER.336)
Alexandre Julliard's avatar
Alexandre Julliard committed
1646 1647 1648
 *
 * Supposed to load resources of Windows 2.x applications.
 */
1649
HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
Alexandre Julliard's avatar
Alexandre Julliard committed
1650
{
1651
    FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1652
          hResource, hModule, hRsrc);
Alexandre Julliard's avatar
Alexandre Julliard committed
1653 1654 1655
    return (HGLOBAL16)0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1656
/**********************************************************************
1657
 *              LoadIconHandler (USER.456)
Alexandre Julliard's avatar
Alexandre Julliard committed
1658
 */
1659
HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
Alexandre Julliard's avatar
Alexandre Julliard committed
1660
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1661 1662
    LPBYTE bits = (LPBYTE)LockResource16( hResource );

1663
    TRACE_(cursor)("hRes=%04x\n",hResource);
Alexandre Julliard's avatar
Alexandre Julliard committed
1664

1665
    return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1666
                      bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
Alexandre Julliard's avatar
Alexandre Julliard committed
1667 1668 1669
}

/***********************************************************************
1670
 *              LoadCursorW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1671
 */
1672
HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
Alexandre Julliard's avatar
Alexandre Julliard committed
1673
{
1674 1675
    TRACE("%p, %s\n", hInstance, debugstr_w(name));

1676
    return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1677
                       LR_SHARED | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1678 1679 1680
}

/***********************************************************************
1681
 *		LoadCursorA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1682
 */
1683
HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
Alexandre Julliard's avatar
Alexandre Julliard committed
1684
{
1685 1686
    TRACE("%p, %s\n", hInstance, debugstr_a(name));

1687
    return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1688
                       LR_SHARED | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1689
}
1690

Alexandre Julliard's avatar
Alexandre Julliard committed
1691
/***********************************************************************
1692 1693
 *		LoadCursorFromFileW (USER32.@)
 */
1694
HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1695
{
1696 1697
    TRACE("%s\n", debugstr_w(name));

1698
    return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1699
                       LR_LOADFROMFILE | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1700
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1701

Alexandre Julliard's avatar
Alexandre Julliard committed
1702
/***********************************************************************
1703 1704
 *		LoadCursorFromFileA (USER32.@)
 */
1705
HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1706
{
1707 1708
    TRACE("%s\n", debugstr_a(name));

1709
    return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1710
                       LR_LOADFROMFILE | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1711
}
1712

Alexandre Julliard's avatar
Alexandre Julliard committed
1713
/***********************************************************************
1714
 *		LoadIconW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1715
 */
1716
HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
Alexandre Julliard's avatar
Alexandre Julliard committed
1717
{
1718 1719
    TRACE("%p, %s\n", hInstance, debugstr_w(name));

1720
    return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1721
                       LR_SHARED | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1722 1723 1724
}

/***********************************************************************
1725
 *              LoadIconA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1726
 */
1727
HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
Alexandre Julliard's avatar
Alexandre Julliard committed
1728
{
1729 1730
    TRACE("%p, %s\n", hInstance, debugstr_a(name));

1731
    return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1732
                       LR_SHARED | LR_DEFAULTSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1733
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1734

1735
/**********************************************************************
1736
 *              GetIconInfo (USER32.@)
1737
 */
1738 1739
BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
{
1740
    CURSORICONINFO *ciconinfo;
1741
    INT height;
Alexandre Julliard's avatar
Alexandre Julliard committed
1742

1743
    ciconinfo = GlobalLock16(HICON_16(hIcon));
Alexandre Julliard's avatar
Alexandre Julliard committed
1744
    if (!ciconinfo)
1745
        return FALSE;
1746

1747 1748 1749
    TRACE("%p => %dx%d, %d bpp\n", hIcon,
          ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);

1750
    if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1751
         (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1752 1753
    {
      iconinfo->fIcon    = TRUE;
1754 1755
      iconinfo->xHotspot = ciconinfo->nWidth / 2;
      iconinfo->yHotspot = ciconinfo->nHeight / 2;
1756 1757 1758 1759 1760 1761 1762
    }
    else
    {
      iconinfo->fIcon    = FALSE;
      iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
      iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1763

1764 1765
    height = ciconinfo->nHeight;

1766 1767 1768
    if (ciconinfo->bBitsPerPixel > 1)
    {
        iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
Alexandre Julliard's avatar
Alexandre Julliard committed
1769 1770 1771
                                ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
                                (char *)(ciconinfo + 1)
                                + ciconinfo->nHeight *
1772
                                get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1773 1774 1775 1776
    }
    else
    {
        iconinfo->hbmColor = 0;
1777
        height *= 2;
1778 1779 1780
    }

    iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
Alexandre Julliard's avatar
Alexandre Julliard committed
1781 1782
                                1, 1, (char *)(ciconinfo + 1));

1783
    GlobalUnlock16(HICON_16(hIcon));
Alexandre Julliard's avatar
Alexandre Julliard committed
1784

Alexandre Julliard's avatar
Alexandre Julliard committed
1785 1786 1787 1788
    return TRUE;
}

/**********************************************************************
1789
 *		CreateIconIndirect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1790
 */
1791
HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1792
{
1793 1794
    DIBSECTION bmpXor;
    BITMAP bmpAnd;
1795
    HICON16 hObj;
1796
    int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
Alexandre Julliard's avatar
Alexandre Julliard committed
1797

1798 1799 1800 1801
    TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
           iconinfo->hbmColor, iconinfo->hbmMask,
           iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);

1802 1803
    if (!iconinfo->hbmMask) return 0;

1804 1805 1806
    planes = GetDeviceCaps( screen_dc, PLANES );
    bpp = GetDeviceCaps( screen_dc, BITSPIXEL );

1807 1808
    if (iconinfo->hbmColor)
    {
1809
        xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1810
        TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1811 1812 1813 1814 1815
               bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
               bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
        /* we can use either depth 1 or screen depth for xor bitmap */
        if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
        sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1816 1817 1818 1819 1820
    }
    GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
    TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
           bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
           bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
Alexandre Julliard's avatar
Alexandre Julliard committed
1821

1822
    sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
1823

1824
    hObj = GlobalAlloc16( GMEM_MOVEABLE,
1825
                          sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1826 1827
    if (hObj)
    {
1828 1829 1830 1831 1832 1833 1834
        CURSORICONINFO *info;

        info = (CURSORICONINFO *)GlobalLock16( hObj );

        /* If we are creating an icon, the hotspot is unused */
        if (iconinfo->fIcon)
        {
1835 1836
            info->ptHotSpot.x   = ICON_HOTSPOT;
            info->ptHotSpot.y   = ICON_HOTSPOT;
1837 1838 1839
        }
        else
        {
1840 1841
            info->ptHotSpot.x   = iconinfo->xHotspot;
            info->ptHotSpot.y   = iconinfo->yHotspot;
1842 1843
        }

1844 1845
        if (iconinfo->hbmColor)
        {
1846 1847 1848 1849 1850
            info->nWidth        = bmpXor.dsBm.bmWidth;
            info->nHeight       = bmpXor.dsBm.bmHeight;
            info->nWidthBytes   = bmpXor.dsBm.bmWidthBytes;
            info->bPlanes       = planes;
            info->bBitsPerPixel = bpp;
1851 1852 1853 1854
        }
        else
        {
            info->nWidth        = bmpAnd.bmWidth;
1855
            info->nHeight       = bmpAnd.bmHeight / 2;
1856 1857 1858
            info->nWidthBytes   = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
            info->bPlanes       = 1;
            info->bBitsPerPixel = 1;
1859
        }
1860 1861 1862

        /* Transfer the bitmap bits to the CURSORICONINFO structure */

1863
        /* Some apps pass a color bitmap as a mask, convert it to b/w */
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
        if (bmpAnd.bmBitsPixel == 1)
        {
            GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
        }
        else
        {
            HDC hdc, hdc_mem;
            HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;

            hdc = GetDC( 0 );
            hdc_mem = CreateCompatibleDC( hdc );
1875

1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
            hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );

            hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
            hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );

            BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );

            SelectObject( hdc, hbmp_old );
            SelectObject( hdc_mem, hbmp_mem_old );

            DeleteDC( hdc_mem );
            ReleaseDC( 0, hdc );

            GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
            DeleteObject( hbmp_mono );
        }
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938

        if (iconinfo->hbmColor)
        {
            char *dst_bits = (char*)(info + 1) + sizeAnd;

            if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
                GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
            else
            {
                BITMAPINFO bminfo;
                int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
                int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );

                bminfo.bmiHeader.biSize = sizeof(bminfo);
                bminfo.bmiHeader.biWidth = info->nWidth;
                bminfo.bmiHeader.biHeight = info->nHeight;
                bminfo.bmiHeader.biPlanes = info->bPlanes;
                bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
                bminfo.bmiHeader.biCompression = BI_RGB;
                bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
                bminfo.bmiHeader.biXPelsPerMeter = 0;
                bminfo.bmiHeader.biYPelsPerMeter = 0;
                bminfo.bmiHeader.biClrUsed = 0;
                bminfo.bmiHeader.biClrImportant = 0;

                /* swap lines for dib sections */
                if (xor_objsize == sizeof(DIBSECTION))
                    bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;

                if (dib_width != bitmap_width)  /* need to fixup alignment */
                {
                    char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );

                    if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
                                               src_bits, &bminfo, DIB_RGB_COLORS ))
                    {
                        int y;
                        for (y = 0; y < info->nHeight; y++)
                            memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
                    }
                    HeapFree( GetProcessHeap(), 0, src_bits );
                }
                else
                    GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
                               dst_bits, &bminfo, DIB_RGB_COLORS );
            }
        }
1939
        GlobalUnlock16( hObj );
Alexandre Julliard's avatar
Alexandre Julliard committed
1940
    }
1941
    return HICON_32(hObj);
Alexandre Julliard's avatar
Alexandre Julliard committed
1942
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1943

Alexandre Julliard's avatar
Alexandre Julliard committed
1944
/******************************************************************************
1945
 *		DrawIconEx (USER32.@) Draws an icon or cursor on device context
Alexandre Julliard's avatar
Alexandre Julliard committed
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
 *
 * NOTES
 *    Why is this using SM_CXICON instead of SM_CXCURSOR?
 *
 * PARAMS
 *    hdc     [I] Handle to device context
 *    x0      [I] X coordinate of upper left corner
 *    y0      [I] Y coordinate of upper left corner
 *    hIcon   [I] Handle to icon to draw
 *    cxWidth [I] Width of icon
 *    cyWidth [I] Height of icon
 *    istep   [I] Index of frame in animated cursor
 *    hbr     [I] Handle to background brush
 *    flags   [I] Icon-drawing flags
 *
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
 */
1965
BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1966
                            INT cxWidth, INT cyWidth, UINT istep,
1967
                            HBRUSH hbr, UINT flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
1968
{
1969
    CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1970
    HDC hDC_off = 0, hMemDC;
1971
    BOOL result = FALSE, DoOffscreen;
1972
    HBITMAP hB_off = 0, hOld = 0;
1973 1974

    if (!ptr) return FALSE;
1975 1976
    TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
                 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
1977

1978
    hMemDC = CreateCompatibleDC (hdc);
Alexandre Julliard's avatar
Alexandre Julliard committed
1979
    if (istep)
1980
        FIXME_(icon)("Ignoring istep=%d\n", istep);
Alexandre Julliard's avatar
Alexandre Julliard committed
1981
    if (flags & DI_COMPAT)
1982
        FIXME_(icon)("Ignoring flag DI_COMPAT\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1983

1984
    if (!flags) {
1985 1986
        FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
        flags = DI_NORMAL;
1987 1988
    }

1989 1990
    /* Calculate the size of the destination image.  */
    if (cxWidth == 0)
1991
    {
1992 1993 1994 1995
        if (flags & DI_DEFAULTSIZE)
            cxWidth = GetSystemMetrics (SM_CXICON);
        else
            cxWidth = ptr->nWidth;
1996
    }
1997
    if (cyWidth == 0)
1998
    {
1999 2000 2001 2002
        if (flags & DI_DEFAULTSIZE)
            cyWidth = GetSystemMetrics (SM_CYICON);
        else
            cyWidth = ptr->nHeight;
2003
    }
2004

2005 2006
    DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);

2007
    if (DoOffscreen) {
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
        RECT r;

        r.left = 0;
        r.top = 0;
        r.right = cxWidth;
        r.bottom = cxWidth;

        hDC_off = CreateCompatibleDC(hdc);
        hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
        if (hDC_off && hB_off) {
            hOld = SelectObject(hDC_off, hB_off);
            FillRect(hDC_off, &r, hbr);
        }
2021
    }
2022 2023

    if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
Alexandre Julliard's avatar
Alexandre Julliard committed
2024
    {
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
        HBITMAP hXorBits, hAndBits;
        COLORREF  oldFg, oldBg;
        INT     nStretchMode;

        nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);

        hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
                                  ptr->bPlanes, ptr->bBitsPerPixel,
                                  (char *)(ptr + 1)
                                  + ptr->nHeight *
                                  get_bitmap_width_bytes(ptr->nWidth,1) );
        hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
                                  1, 1, (char *)(ptr+1) );
        oldFg = SetTextColor( hdc, RGB(0,0,0) );
        oldBg = SetBkColor( hdc, RGB(255,255,255) );

        if (hXorBits && hAndBits)
        {
            HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
            if (flags & DI_MASK)
2045
            {
2046 2047 2048 2049 2050 2051
                if (DoOffscreen)
                    StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
                                hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
                else
                    StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
                                hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2052
            }
2053 2054
            SelectObject( hMemDC, hXorBits );
            if (flags & DI_IMAGE)
2055
            {
2056 2057 2058 2059 2060 2061
                if (DoOffscreen)
                    StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
                                hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
                else
                    StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
                                hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2062
            }
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
            SelectObject( hMemDC, hBitTemp );
            result = TRUE;
        }

        SetTextColor( hdc, oldFg );
        SetBkColor( hdc, oldBg );
        if (hXorBits) DeleteObject( hXorBits );
        if (hAndBits) DeleteObject( hAndBits );
        SetStretchBltMode (hdc, nStretchMode);
        if (DoOffscreen) {
            BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
            SelectObject(hDC_off, hOld);
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2076
    }
2077 2078 2079
    if (hMemDC) DeleteDC( hMemDC );
    if (hDC_off) DeleteDC(hDC_off);
    if (hB_off) DeleteObject(hB_off);
2080
    GlobalUnlock16(HICON_16(hIcon));
Alexandre Julliard's avatar
Alexandre Julliard committed
2081 2082
    return result;
}
2083

2084 2085 2086 2087 2088 2089 2090 2091
/***********************************************************************
 *           DIB_FixColorsToLoadflags
 *
 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
 * are in loadflags
 */
static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
{
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
    int colors;
    COLORREF c_W, c_S, c_F, c_L, c_C;
    int incr,i;
    RGBQUAD *ptr;
    int bitmap_type;
    LONG width;
    LONG height;
    WORD bpp;
    DWORD compr;

    if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
    {
        WARN_(resource)("Invalid bitmap\n");
        return;
    }

    if (bpp > 8) return;

    if (bitmap_type == 0) /* BITMAPCOREHEADER */
    {
        incr = 3;
        colors = 1 << bpp;
    }
    else
    {
        incr = 4;
        colors = bmi->bmiHeader.biClrUsed;
        if (colors > 256) colors = 256;
        if (!colors && (bpp <= 8)) colors = 1 << bpp;
    }

    c_W = GetSysColor(COLOR_WINDOW);
    c_S = GetSysColor(COLOR_3DSHADOW);
    c_F = GetSysColor(COLOR_3DFACE);
    c_L = GetSysColor(COLOR_3DLIGHT);

    if (loadflags & LR_LOADTRANSPARENT) {
        switch (bpp) {
        case 1: pix = pix >> 7; break;
        case 4: pix = pix >> 4; break;
        case 8: break;
        default:
            WARN_(resource)("(%d): Unsupported depth\n", bpp);
            return;
        }
        if (pix >= colors) {
            WARN_(resource)("pixel has color index greater than biClrUsed!\n");
            return;
        }
        if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
        ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
        ptr->rgbBlue = GetBValue(c_W);
        ptr->rgbGreen = GetGValue(c_W);
        ptr->rgbRed = GetRValue(c_W);
2146
    }
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
    if (loadflags & LR_LOADMAP3DCOLORS)
        for (i=0; i<colors; i++) {
            ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
            c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
            if (c_C == RGB(128, 128, 128)) {
                ptr->rgbRed = GetRValue(c_S);
                ptr->rgbGreen = GetGValue(c_S);
                ptr->rgbBlue = GetBValue(c_S);
            } else if (c_C == RGB(192, 192, 192)) {
                ptr->rgbRed = GetRValue(c_F);
                ptr->rgbGreen = GetGValue(c_F);
                ptr->rgbBlue = GetBValue(c_F);
            } else if (c_C == RGB(223, 223, 223)) {
                ptr->rgbRed = GetRValue(c_L);
                ptr->rgbGreen = GetGValue(c_L);
                ptr->rgbBlue = GetBValue(c_L);
            }
        }
2165 2166 2167
}


2168 2169 2170
/**********************************************************************
 *       BITMAP_Load
 */
2171 2172
static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
                            INT desiredx, INT desiredy, UINT loadflags )
2173
{
2174
    HBITMAP hbitmap = 0, orig_bm;
2175 2176 2177
    HRSRC hRsrc;
    HGLOBAL handle;
    char *ptr = NULL;
2178
    BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2179
    int size;
2180 2181 2182 2183 2184 2185 2186
    BYTE pix;
    char *bits;
    LONG width, height, new_width, new_height;
    WORD bpp_dummy;
    DWORD compr_dummy;
    INT bm_type;
    HDC screen_mem_dc = NULL;
2187

2188 2189
    if (!(loadflags & LR_LOADFROMFILE))
    {
2190 2191 2192 2193 2194
        if (!instance)
        {
            /* OEM bitmap: try to load the resource from user32.dll */
            instance = user32_module;
        }
2195

2196 2197
        if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
        if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2198

2199
        if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2200 2201 2202
    }
    else
    {
2203 2204
        BITMAPFILEHEADER * bmfh;

2205
        if (!(ptr = map_fileW( name, NULL ))) return 0;
2206
        info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2207 2208 2209 2210 2211 2212 2213 2214 2215
        bmfh = (BITMAPFILEHEADER *)ptr;
        if (!(  bmfh->bfType == 0x4d42 /* 'BM' */ &&
                bmfh->bfReserved1 == 0 &&
                bmfh->bfReserved2 == 0))
        {
            WARN("Invalid/unsupported bitmap format!\n");
            UnmapViewOfFile( ptr );
            return 0;
        }
2216
    }
2217

2218
    size = bitmap_info_size(info, DIB_RGB_COLORS);
2219 2220
    fix_info = HeapAlloc(GetProcessHeap(), 0, size);
    scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2221

2222 2223
    if (!fix_info || !scaled_info) goto end;
    memcpy(fix_info, info, size);
2224

2225 2226
    pix = *((LPBYTE)info + size);
    DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2227

2228 2229 2230 2231 2232 2233 2234
    memcpy(scaled_info, fix_info, size);
    bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
                                 &bpp_dummy, &compr_dummy);
    if(desiredx != 0)
        new_width = desiredx;
    else
        new_width = width;
2235

2236 2237 2238 2239
    if(desiredy != 0)
        new_height = height > 0 ? desiredy : -desiredy;
    else
        new_height = height;
2240

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
    if(bm_type == 0)
    {
        BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
        core->bcWidth = new_width;
        core->bcHeight = new_height;
    }
    else
    {
        scaled_info->bmiHeader.biWidth = new_width;
        scaled_info->bmiHeader.biHeight = new_height;
    }

    if (new_height < 0) new_height = -new_height;

    if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
    if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;

    bits = (char *)info + size;
2259

2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
    if (loadflags & LR_CREATEDIBSECTION)
    {
        scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
        hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
    }
    else
    {
        if (is_dib_monochrome(fix_info))
            hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
        else
            hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);        
2271
    }
2272

2273 2274 2275 2276 2277 2278 2279 2280
    orig_bm = SelectObject(screen_mem_dc, hbitmap);
    StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
    SelectObject(screen_mem_dc, orig_bm);

end:
    if (screen_mem_dc) DeleteDC(screen_mem_dc);
    HeapFree(GetProcessHeap(), 0, scaled_info);
    HeapFree(GetProcessHeap(), 0, fix_info);
2281
    if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2282

2283 2284 2285 2286
    return hbitmap;
}

/**********************************************************************
2287
 *		LoadImageA (USER32.@)
2288
 *
2289
 * See LoadImageW.
2290 2291 2292 2293 2294 2295 2296
 */
HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
                              INT desiredx, INT desiredy, UINT loadflags)
{
    HANDLE res;
    LPWSTR u_name;

2297
    if (!HIWORD(name))
2298
        return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2299

2300
    __TRY {
2301 2302 2303
        DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
        u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2304
    }
2305
    __EXCEPT_PAGE_FAULT {
2306 2307
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
2308 2309
    }
    __ENDTRY
2310
    res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2311
    HeapFree(GetProcessHeap(), 0, u_name);
2312 2313 2314 2315 2316
    return res;
}


/******************************************************************************
2317
 *		LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
 *
 * PARAMS
 *    hinst     [I] Handle of instance that contains image
 *    name      [I] Name of image
 *    type      [I] Type of image
 *    desiredx  [I] Desired width
 *    desiredy  [I] Desired height
 *    loadflags [I] Load flags
 *
 * RETURNS
 *    Success: Handle to newly loaded image
 *    Failure: NULL
 *
2331
 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2332 2333 2334 2335
 */
HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
                INT desiredx, INT desiredy, UINT loadflags )
{
2336 2337 2338
    TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
                     hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);

2339 2340
    if (loadflags & LR_DEFAULTSIZE) {
        if (type == IMAGE_ICON) {
2341 2342 2343
            if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
            if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
        } else if (type == IMAGE_CURSOR) {
2344
            if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2345 2346
            if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
        }
2347 2348 2349 2350
    }
    if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
    switch (type) {
    case IMAGE_BITMAP:
2351
        return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2352 2353

    case IMAGE_ICON:
2354
        if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2355
        if (screen_dc)
2356
        {
2357 2358 2359 2360 2361 2362
            UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
            if (palEnts == 0) palEnts = 256;
            return CURSORICON_Load(hinst, name, desiredx, desiredy,
                                   palEnts, FALSE, loadflags);
        }
        break;
2363 2364 2365

    case IMAGE_CURSOR:
        return CURSORICON_Load(hinst, name, desiredx, desiredy,
2366
                               1, TRUE, loadflags);
2367 2368 2369 2370 2371
    }
    return 0;
}

/******************************************************************************
2372
 *		CopyImage (USER32.@) Creates new image and copies attributes to it
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
 *
 * PARAMS
 *    hnd      [I] Handle to image to copy
 *    type     [I] Type of image to copy
 *    desiredx [I] Desired width of new image
 *    desiredy [I] Desired height of new image
 *    flags    [I] Copy flags
 *
 * RETURNS
 *    Success: Handle to newly created image
 *    Failure: NULL
 *
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
 * BUGS
 *    Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
 *    all other versions (95/2000/XP have been tested) ignore it.
 *
 * NOTES
 *    If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
 *    a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
 *    the copy will have the same depth as the screen.
 *    The content of the image will only be copied if the bit depth of the
 *    original image is compatible with the bit depth of the screen, or
 *    if the source is a DIB section.
 *    The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2397
 */
2398
HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2399 2400
                             INT desiredy, UINT flags )
{
2401 2402 2403
    TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
          hnd, type, desiredx, desiredy, flags);

2404 2405
    switch (type)
    {
2406
        case IMAGE_BITMAP:
2407
        {
2408 2409 2410 2411
            HBITMAP res = NULL;
            DIBSECTION ds;
            int objSize;
            BITMAPINFO * bi;
2412

2413 2414 2415 2416 2417
            objSize = GetObjectW( hnd, sizeof(ds), &ds );
            if (!objSize) return 0;
            if ((desiredx < 0) || (desiredy < 0)) return 0;

            if (flags & LR_COPYFROMRESOURCE)
2418
            {
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
                FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
            }

            if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
            if (desiredy == 0) desiredy = ds.dsBm.bmHeight;

            /* Allocate memory for a BITMAPINFOHEADER structure and a
               color table. The maximum number of colors in a color table
               is 256 which corresponds to a bitmap with depth 8.
               Bitmaps with higher depths don't have color tables. */
            bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
            if (!bi) return 0;

            bi->bmiHeader.biSize        = sizeof(bi->bmiHeader);
            bi->bmiHeader.biPlanes      = ds.dsBm.bmPlanes;
            bi->bmiHeader.biBitCount    = ds.dsBm.bmBitsPixel;
            bi->bmiHeader.biCompression = BI_RGB;

            if (flags & LR_CREATEDIBSECTION)
            {
                /* Create a DIB section. LR_MONOCHROME is ignored */
                void * bits;
                HDC dc = CreateCompatibleDC(NULL);

                if (objSize == sizeof(DIBSECTION))
                {
                    /* The source bitmap is a DIB.
                       Get its attributes to create an exact copy */
                    memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
                }

                /* Get the color table or the color masks */
                GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);

                bi->bmiHeader.biWidth  = desiredx;
                bi->bmiHeader.biHeight = desiredy;
                bi->bmiHeader.biSizeImage = 0;

                res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
                DeleteDC(dc);
            }
            else
            {
                /* Create a device-dependent bitmap */

                BOOL monochrome = (flags & LR_MONOCHROME);

                if (objSize == sizeof(DIBSECTION))
                {
                    /* The source bitmap is a DIB section.
                       Get its attributes */
                    HDC dc = CreateCompatibleDC(NULL);
                    bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
                    bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
                    GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
                    DeleteDC(dc);

                    if (!monochrome && ds.dsBm.bmBitsPixel == 1)
                    {
                        /* Look if the colors of the DIB are black and white */

                        monochrome = 
                              (bi->bmiColors[0].rgbRed == 0xff
                            && bi->bmiColors[0].rgbGreen == 0xff
                            && bi->bmiColors[0].rgbBlue == 0xff
                            && bi->bmiColors[0].rgbReserved == 0
                            && bi->bmiColors[1].rgbRed == 0
                            && bi->bmiColors[1].rgbGreen == 0
                            && bi->bmiColors[1].rgbBlue == 0
                            && bi->bmiColors[1].rgbReserved == 0)
                            ||
                              (bi->bmiColors[0].rgbRed == 0
                            && bi->bmiColors[0].rgbGreen == 0
                            && bi->bmiColors[0].rgbBlue == 0
                            && bi->bmiColors[0].rgbReserved == 0
                            && bi->bmiColors[1].rgbRed == 0xff
                            && bi->bmiColors[1].rgbGreen == 0xff
                            && bi->bmiColors[1].rgbBlue == 0xff
                            && bi->bmiColors[1].rgbReserved == 0);
                    }
                }
                else if (!monochrome)
                {
                    monochrome = ds.dsBm.bmBitsPixel == 1;
                }

                if (monochrome)
                {
                    res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
                }
                else
                {
                    HDC screenDC = GetDC(NULL);
                    res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
                    ReleaseDC(NULL, screenDC);
                }
            }

            if (res)
            {
                /* Only copy the bitmap if it's a DIB section or if it's
                   compatible to the screen */
                BOOL copyContents;

                if (objSize == sizeof(DIBSECTION))
                {
                    copyContents = TRUE;
                }
                else
                {
                    HDC screenDC = GetDC(NULL);
                    int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
                    ReleaseDC(NULL, screenDC);

                    copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
                }

                if (copyContents)
                {
                    /* The source bitmap may already be selected in a device context,
                       use GetDIBits/StretchDIBits and not StretchBlt  */

                    HDC dc;
                    void * bits;

                    dc = CreateCompatibleDC(NULL);

                    bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
                    bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
                    bi->bmiHeader.biSizeImage = 0;
                    bi->bmiHeader.biClrUsed = 0;
                    bi->bmiHeader.biClrImportant = 0;

                    /* Fill in biSizeImage */
                    GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
                    bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);

                    if (bits)
                    {
                        HBITMAP oldBmp;

                        /* Get the image bits of the source bitmap */
                        GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);

                        /* Copy it to the destination bitmap */
                        oldBmp = SelectObject(dc, res);
                        StretchDIBits(dc, 0, 0, desiredx, desiredy,
                                      0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
                                      bits, bi, DIB_RGB_COLORS, SRCCOPY);
                        SelectObject(dc, oldBmp);

                        HeapFree(GetProcessHeap(), 0, bits);
                    }

                    DeleteDC(dc);
                }

                if (flags & LR_COPYDELETEORG)
                {
                    DeleteObject(hnd);
                }
2580
            }
2581
            HeapFree(GetProcessHeap(), 0, bi);
2582
            return res;
2583
        }
2584 2585 2586 2587 2588 2589
        case IMAGE_ICON:
                return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
        case IMAGE_CURSOR:
                /* Should call CURSORICON_ExtCopy but more testing
                 * needs to be done before we change this
                 */
2590
                if (flags) FIXME("Flags are ignored\n");
2591
                return CopyCursor(hnd);
2592 2593 2594 2595 2596 2597
    }
    return 0;
}


/******************************************************************************
2598
 *		LoadBitmapW (USER32.@) Loads bitmap from the executable file
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
 *
 * RETURNS
 *    Success: Handle to specified bitmap
 *    Failure: NULL
 */
HBITMAP WINAPI LoadBitmapW(
    HINSTANCE instance, /* [in] Handle to application instance */
    LPCWSTR name)         /* [in] Address of bitmap resource name */
{
    return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
}

/**********************************************************************
2612
 *		LoadBitmapA (USER32.@)
2613 2614
 *
 * See LoadBitmapW.
2615 2616 2617 2618 2619
 */
HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
{
    return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
}