- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / objects / cursoricon.c
1 /*
2  * Cursor and icon support
3  *
4  * Copyright 1995 Alexandre Julliard
5  *           1996 Martin Von Loewis
6  *           1997 Alex Korobka
7  *           1998 Turchanov Sergey
8  */
9
10 /*
11  * Theory:
12  *
13  * http://www.microsoft.com/win32dev/ui/icons.htm
14  *
15  * Cursors and icons are stored in a global heap block, with the
16  * following layout:
17  *
18  * CURSORICONINFO info;
19  * BYTE[]         ANDbits;
20  * BYTE[]         XORbits;
21  *
22  * The bits structures are in the format of a device-dependent bitmap.
23  *
24  * This layout is very sub-optimal, as the bitmap bits are stored in
25  * the X client instead of in the server like other bitmaps; however,
26  * some programs (notably Paint Brush) expect to be able to manipulate
27  * the bits directly :-(
28  *
29  * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
30  */
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include "heap.h"
35 #include "windows.h"
36 #include "peexe.h"
37 #include "color.h"
38 #include "bitmap.h"
39 #include "cursoricon.h"
40 #include "dc.h"
41 #include "gdi.h"
42 #include "sysmetrics.h"
43 #include "global.h"
44 #include "module.h"
45 #include "win.h"
46 #include "debug.h"
47 #include "task.h"
48 #include "user.h"
49 #include "input.h"
50 #include "display.h"
51 #include "message.h"
52 #include "winerror.h"
53
54 static HCURSOR32 hActiveCursor = 0;  /* Active cursor */
55 static INT32 CURSOR_ShowCount = 0;   /* Cursor display count */
56 static RECT32 CURSOR_ClipRect;       /* Cursor clipping rect */
57
58 /**********************************************************************
59  *          CURSORICON_FindBestIcon
60  *
61  * Find the icon closest to the requested size and number of colors.
62  */
63 static ICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
64                                               int height, int colors )
65 {
66     int i, maxcolors, maxwidth, maxheight;
67     ICONDIRENTRY *entry, *bestEntry = NULL;
68
69     if (dir->idCount < 1)
70     {
71         WARN(icon, "Empty directory!\n" );
72         return NULL;
73     }
74     if (dir->idCount == 1) return &dir->idEntries[0].icon;  /* No choice... */
75
76     /* First find the exact size with less colors */
77
78     maxcolors = 0;
79     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
80         if ((entry->bWidth == width) && (entry->bHeight == height) &&
81             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
82         {
83             bestEntry = entry;
84             maxcolors = entry->bColorCount;
85         }
86     if (bestEntry) return bestEntry;
87
88     /* First find the exact size with more colors */
89
90     maxcolors = 255;
91     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
92         if ((entry->bWidth == width) && (entry->bHeight == height) &&
93             (entry->bColorCount > colors) && (entry->bColorCount <= maxcolors))
94         {
95             bestEntry = entry;
96             maxcolors = entry->bColorCount;
97         }
98     if (bestEntry) return bestEntry;
99
100     /* Now find a smaller one with less colors */
101
102     maxcolors = maxwidth = maxheight = 0;
103     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
104         if ((entry->bWidth <= width) && (entry->bHeight <= height) &&
105             (entry->bWidth >= maxwidth) && (entry->bHeight >= maxheight) &&
106             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
107         {
108             bestEntry = entry;
109             maxwidth  = entry->bWidth;
110             maxheight = entry->bHeight;
111             maxcolors = entry->bColorCount;
112         }
113     if (bestEntry) return bestEntry;
114
115     /* Now find a smaller one with more colors */
116
117     maxcolors = 255;
118     maxwidth = maxheight = 0;
119     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
120         if ((entry->bWidth <= width) && (entry->bHeight <= height) &&
121             (entry->bWidth >= maxwidth) && (entry->bHeight >= maxheight) &&
122             (entry->bColorCount > colors) && (entry->bColorCount <= maxcolors))
123         {
124             bestEntry = entry;
125             maxwidth  = entry->bWidth;
126             maxheight = entry->bHeight;
127             maxcolors = entry->bColorCount;
128         }
129     if (bestEntry) return bestEntry;
130
131     /* Now find a larger one with less colors */
132
133     maxcolors = 0;
134     maxwidth = maxheight = 255;
135     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
136         if ((entry->bWidth <= maxwidth) && (entry->bHeight <= maxheight) &&
137             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
138         {
139             bestEntry = entry;
140             maxwidth  = entry->bWidth;
141             maxheight = entry->bHeight;
142             maxcolors = entry->bColorCount;
143         }
144     if (bestEntry) return bestEntry;
145
146     /* Now find a larger one with more colors */
147
148     maxcolors = maxwidth = maxheight = 255;
149     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
150         if ((entry->bWidth <= maxwidth) && (entry->bHeight <= maxheight) &&
151             (entry->bColorCount > colors) && (entry->bColorCount <= maxcolors))
152         {
153             bestEntry = entry;
154             maxwidth  = entry->bWidth;
155             maxheight = entry->bHeight;
156             maxcolors = entry->bColorCount;
157         }
158
159     return bestEntry;
160 }
161
162
163 /**********************************************************************
164  *          CURSORICON_FindBestCursor
165  *
166  * Find the cursor closest to the requested size.
167  * FIXME: parameter 'color' ignored and entries with more than 1 bpp
168  *        ignored too
169  */
170 static CURSORDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
171                                                   int width, int height, int color)
172 {
173     int i, maxwidth, maxheight;
174     CURSORDIRENTRY *entry, *bestEntry = NULL;
175
176     if (dir->idCount < 1)
177     {
178         WARN(cursor, "Empty directory!\n" );
179         return NULL;
180     }
181     if (dir->idCount == 1) return &dir->idEntries[0].cursor; /* No choice... */
182
183     /* First find the largest one smaller than or equal to the requested size*/
184
185     maxwidth = maxheight = 0;
186     for(i = 0,entry = &dir->idEntries[0].cursor; i < dir->idCount; i++,entry++)
187         if ((entry->wWidth <= width) && (entry->wHeight <= height) &&
188             (entry->wWidth > maxwidth) && (entry->wHeight > maxheight) &&
189             (entry->wBitCount == 1))
190         {
191             bestEntry = entry;
192             maxwidth  = entry->wWidth;
193             maxheight = entry->wHeight;
194         }
195     if (bestEntry) return bestEntry;
196
197     /* Now find the smallest one larger than the requested size */
198
199     maxwidth = maxheight = 255;
200     for(i = 0,entry = &dir->idEntries[0].cursor; i < dir->idCount; i++,entry++)
201         if ((entry->wWidth < maxwidth) && (entry->wHeight < maxheight) &&
202             (entry->wBitCount == 1))
203         {
204             bestEntry = entry;
205             maxwidth  = entry->wWidth;
206             maxheight = entry->wHeight;
207         }
208
209     return bestEntry;
210 }
211
212 /*********************************************************************
213  * The main purpose of this function is to create fake resource directory
214  * and fake resource entries. There are several reasons for this:
215  *      -       CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
216  *              fields
217  *      There are some "bad" cursor files which do not have
218  *              bColorCount initialized but instead one must read this info
219  *              directly from corresponding DIB sections
220  * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
221  */
222 BOOL32 CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL32 fCursor,
223                                                 CURSORICONDIR **res, LPBYTE **ptr)
224 {
225     LPBYTE   _free;
226     CURSORICONFILEDIR *bits;
227     int      entries, size, i;
228
229     *res = NULL;
230     *ptr = NULL;
231     if (!(bits = (CURSORICONFILEDIR *)VIRTUAL_MapFileW( filename ))) return FALSE;
232
233     /* FIXME: test for inimated icons
234      * hack to load the first icon from the *.ani file
235      */
236     if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
237     { LPBYTE pos = (LPBYTE) bits;
238       FIXME (cursor,"Animated icons not correctly implemented! %p \n", bits);
239         
240       for (;;)
241       { if (*(LPDWORD)pos==0x6e6f6369)          /* "icon" */
242         { FIXME (cursor,"icon entry found! %p\n", bits);
243           pos+=4;
244           if ( !*(LPWORD) pos==0x2fe)           /* iconsize */
245           { goto fail;
246           }
247           bits+=2;
248           FIXME (cursor,"icon size ok %p \n", bits);
249           break;
250         }
251         pos+=2;
252         if (pos>=(LPBYTE)bits+766) goto fail;
253       }
254     }
255     if (!(entries = bits->idCount)) goto fail;
256     (int)_free = size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * 
257                                                 (entries - 1);
258     for (i=0; i < entries; i++)
259       size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
260     
261     if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
262                             entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
263     if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
264
265     _free = (LPBYTE)(*res) + (int)_free;
266     memcpy((*res), bits, 6);
267     for (i=0; i<entries; i++)
268     {
269       ((LPBYTE*)(*ptr))[i] = _free;
270       if (fCursor) {
271         (*res)->idEntries[i].cursor.wWidth=bits->idEntries[i].bWidth;
272         (*res)->idEntries[i].cursor.wHeight=bits->idEntries[i].bHeight;
273         (*res)->idEntries[i].cursor.wPlanes=1;
274         (*res)->idEntries[i].cursor.wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
275                                                    bits->idEntries[i].dwDIBOffset))->biBitCount;
276         (*res)->idEntries[i].cursor.dwBytesInRes = bits->idEntries[i].dwDIBSize;
277         (*res)->idEntries[i].cursor.wResId=i+1;
278         ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
279         ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
280         _free+=sizeof(POINT16);
281       } else {
282         (*res)->idEntries[i].icon.bWidth=bits->idEntries[i].bWidth;
283         (*res)->idEntries[i].icon.bHeight=bits->idEntries[i].bHeight;
284         (*res)->idEntries[i].icon.bColorCount = bits->idEntries[i].bColorCount;
285         (*res)->idEntries[i].icon.wPlanes=1;
286         (*res)->idEntries[i].icon.wBitCount= ((LPBITMAPINFOHEADER)((LPBYTE)bits +
287                                              bits->idEntries[i].dwDIBOffset))->biBitCount;
288         (*res)->idEntries[i].icon.dwBytesInRes = bits->idEntries[i].dwDIBSize;
289         (*res)->idEntries[i].icon.wResId=i+1;
290       }
291       memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
292              (*res)->idEntries[i].icon.dwBytesInRes);
293       _free += (*res)->idEntries[i].icon.dwBytesInRes;
294     }
295     UnmapViewOfFile( bits );
296     return TRUE;    
297 fail:
298     if (*res) HeapFree( GetProcessHeap(), 0, *res );
299     if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
300     UnmapViewOfFile( bits );
301     return FALSE;
302 }
303
304 /**********************************************************************
305  *          CURSORICON_LoadDirEntry16
306  *
307  * Load the icon/cursor directory for a given resource name and find the
308  * best matching entry.
309  */
310 static BOOL32 CURSORICON_LoadDirEntry16( HINSTANCE32 hInstance, SEGPTR name,
311                                          INT32 width, INT32 height, INT32 colors, 
312                                          BOOL32 fCursor, CURSORICONDIRENTRY *dirEntry )
313 {
314     HRSRC16 hRsrc;
315     HGLOBAL16 hMem;
316     CURSORICONDIR *dir;
317     CURSORICONDIRENTRY *entry = NULL;
318
319     if (!(hRsrc = FindResource16( hInstance, name,
320                               fCursor ? RT_GROUP_CURSOR16 : RT_GROUP_ICON16 )))
321         return FALSE;
322     if (!(hMem = LoadResource16( hInstance, hRsrc ))) return FALSE;
323     if ((dir = (CURSORICONDIR *)LockResource16( hMem )))
324     {
325         if (fCursor)
326             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
327                                                                width, height, 1);
328         else
329             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
330                                                        width, height, colors );
331         if (entry) *dirEntry = *entry;
332     }
333     FreeResource16( hMem );
334     return (entry != NULL);
335 }
336
337
338 /**********************************************************************
339  *          CURSORICON_LoadDirEntry32
340  *
341  * Load the icon/cursor directory for a given resource name and find the
342  * best matching entry.
343  */
344 static BOOL32 CURSORICON_LoadDirEntry32( HINSTANCE32 hInstance, LPCWSTR name,
345                                          INT32 width, INT32 height, INT32 colors,
346                                          BOOL32 fCursor, CURSORICONDIRENTRY *dirEntry )
347 {
348     HANDLE32 hRsrc;
349     HANDLE32 hMem;
350     CURSORICONDIR *dir;
351     CURSORICONDIRENTRY *entry = NULL;
352
353     if (!(hRsrc = FindResource32W( hInstance, name,
354                             fCursor ? RT_GROUP_CURSOR32W : RT_GROUP_ICON32W )))
355         return FALSE;
356     if (!(hMem = LoadResource32( hInstance, hRsrc ))) return FALSE;
357     if ((dir = (CURSORICONDIR*)LockResource32( hMem )))
358     {
359         if (fCursor)
360             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
361                                                                width, height, 1);
362         else
363             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
364                                                        width, height, colors );
365         if (entry) *dirEntry = *entry;
366     }
367     FreeResource32( hMem );
368     return (entry != NULL);
369 }
370
371
372 /**********************************************************************
373  *          CURSORICON_CreateFromResource
374  *
375  * Create a cursor or icon from in-memory resource template. 
376  *
377  * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
378  *        with cbSize parameter as well.
379  */
380 static HGLOBAL16 CURSORICON_CreateFromResource( HINSTANCE16 hInstance, HGLOBAL16 hObj, LPBYTE bits,
381                                                 UINT32 cbSize, BOOL32 bIcon, DWORD dwVersion, 
382                                                 INT32 width, INT32 height, UINT32 loadflags )
383 {
384     int sizeAnd, sizeXor;
385     HBITMAP32 hAndBits = 0, hXorBits = 0; /* error condition for later */
386     BITMAPOBJ *bmpXor, *bmpAnd;
387     POINT16 hotspot = { 0 ,0 };
388     BITMAPINFO *bmi;
389     HDC32 hdc;
390     BOOL32 DoStretch;
391     INT32 size;
392
393     TRACE(cursor,"%08x (%u bytes), ver %08x, %ix%i %s %s\n",
394                         (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
395                                   bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
396     if (dwVersion == 0x00020000)
397     {
398         FIXME(cursor,"\t2.xx resources are not supported\n");
399         return 0;
400     }
401
402     if (bIcon)
403         bmi = (BITMAPINFO *)bits;
404     else /* get the hotspot */
405     {
406         POINT16 *pt = (POINT16 *)bits;
407         hotspot = *pt;
408         bmi = (BITMAPINFO *)(pt + 1);
409     }
410     size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
411
412     if (!width) width = bmi->bmiHeader.biWidth;
413     if (!height) height = bmi->bmiHeader.biHeight/2;
414     DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
415       (bmi->bmiHeader.biWidth != width);
416
417     /* Check bitmap header */
418
419     if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
420          (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER)  ||
421           bmi->bmiHeader.biCompression != BI_RGB) )
422     {
423           WARN(cursor,"\tinvalid resource bitmap header.\n");
424           return 0;
425     }
426
427     if( (hdc = GetDC32( 0 )) )
428     {
429         BITMAPINFO* pInfo;
430
431         /* Make sure we have room for the monochrome bitmap later on.
432          * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
433          * up to and including the biBitCount. In-memory icon resource 
434          * format is as follows:
435          *
436          *   BITMAPINFOHEADER   icHeader  // DIB header
437          *   RGBQUAD         icColors[]   // Color table
438          *   BYTE            icXOR[]      // DIB bits for XOR mask
439          *   BYTE            icAND[]      // DIB bits for AND mask
440          */
441
442         if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0, 
443           MAX(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
444         {       
445             memcpy( pInfo, bmi, size ); 
446             pInfo->bmiHeader.biHeight /= 2;
447
448             /* Create the XOR bitmap */
449
450             if (DoStretch) {
451               if ((hXorBits = CreateCompatibleBitmap32(hdc, width, height))) {
452                 HBITMAP32 hOld;
453                 HDC32 hMem = CreateCompatibleDC32(hdc);
454                 BOOL32 res;
455
456                 if (hMem) {
457                   hOld = SelectObject32(hMem, hXorBits);
458                   res = StretchDIBits32(hMem, 0, 0, width, height, 0, 0,
459                     bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
460                     (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
461                   SelectObject32(hMem, hOld);
462                   DeleteDC32(hMem);
463                 } else res = FALSE;
464                 if (!res) { DeleteObject32(hXorBits); hXorBits = 0; }
465               }
466             } else hXorBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader,
467                 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
468             if( hXorBits )
469             {
470                 char* bits = (char *)bmi + size + bmi->bmiHeader.biHeight *
471                                 DIB_GetDIBWidthBytes(bmi->bmiHeader.biWidth,
472                                                      bmi->bmiHeader.biBitCount) / 2;
473
474                 pInfo->bmiHeader.biBitCount = 1;
475                 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
476                 {
477                     RGBQUAD *rgb = pInfo->bmiColors;
478
479                     pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
480                     rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
481                     rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
482                     rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
483                 }
484                 else
485                 {
486                     RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
487
488                     rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
489                     rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
490                 }
491
492                 /* Create the AND bitmap */
493
494             if (DoStretch) {
495               if ((hAndBits = CreateBitmap32(width, height, 1, 1, NULL))) {
496                 HBITMAP32 hOld;
497                 HDC32 hMem = CreateCompatibleDC32(hdc);
498                 BOOL32 res;
499
500                 if (hMem) {
501                   hOld = SelectObject32(hMem, hAndBits);
502                   res = StretchDIBits32(hMem, 0, 0, width, height, 0, 0,
503                     pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
504                     bits, pInfo, DIB_RGB_COLORS, SRCCOPY);
505                   SelectObject32(hMem, hOld);
506                   DeleteDC32(hMem);
507                 } else res = FALSE;
508                 if (!res) { DeleteObject32(hAndBits); hAndBits = 0; }
509               }
510             } else hAndBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader,
511               CBM_INIT, bits, pInfo, DIB_RGB_COLORS );
512
513                 if( !hAndBits ) DeleteObject32( hXorBits );
514             }
515             HeapFree( GetProcessHeap(), 0, pInfo ); 
516         }
517         ReleaseDC32( 0, hdc );
518     }
519
520     if( !hXorBits || !hAndBits ) 
521     {
522         WARN(cursor,"\tunable to create an icon bitmap.\n");
523         return 0;
524     }
525
526     /* Now create the CURSORICONINFO structure */
527     bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( hXorBits, BITMAP_MAGIC );
528     bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( hAndBits, BITMAP_MAGIC );
529     sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
530     sizeAnd = bmpAnd->bitmap.bmHeight * bmpAnd->bitmap.bmWidthBytes;
531
532     if (hObj) hObj = GlobalReAlloc16( hObj, 
533                      sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
534     if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE, 
535                      sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
536     if (hObj)
537     {
538         CURSORICONINFO *info;
539
540         /* Make it owned by the module */
541         if (hInstance) FarSetOwner( hObj, GetExePtr(hInstance) );
542
543         info = (CURSORICONINFO *)GlobalLock16( hObj );
544         info->ptHotSpot.x   = hotspot.x;
545         info->ptHotSpot.y   = hotspot.y;
546         info->nWidth        = bmpXor->bitmap.bmWidth;
547         info->nHeight       = bmpXor->bitmap.bmHeight;
548         info->nWidthBytes   = bmpXor->bitmap.bmWidthBytes;
549         info->bPlanes       = bmpXor->bitmap.bmPlanes;
550         info->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
551
552         /* Transfer the bitmap bits to the CURSORICONINFO structure */
553
554         GetBitmapBits32( hAndBits, sizeAnd, (char *)(info + 1) );
555         GetBitmapBits32( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
556         GlobalUnlock16( hObj );
557     }
558
559     DeleteObject32( hXorBits );
560     DeleteObject32( hAndBits );
561     return hObj;
562 }
563
564
565 /**********************************************************************
566  *          CreateIconFromResourceEx16          (USER.450)
567  *
568  * FIXME: not sure about exact parameter types
569  */
570 HICON16 WINAPI CreateIconFromResourceEx16( LPBYTE bits, UINT16 cbSize, BOOL16 bIcon,
571                                     DWORD dwVersion, INT16 width, INT16 height, UINT16 cFlag )
572 {
573     return CreateIconFromResourceEx32(bits, cbSize, bIcon, dwVersion, 
574       width, height, cFlag);
575 }
576
577
578 /**********************************************************************
579  *          CreateIconFromResource          (USER32.76)
580  */
581 HICON32 WINAPI CreateIconFromResource32( LPBYTE bits, UINT32 cbSize,
582                                            BOOL32 bIcon, DWORD dwVersion)
583 {
584     return CreateIconFromResourceEx32( bits, cbSize, bIcon, dwVersion, 0,0,0);
585 }
586
587
588 /**********************************************************************
589  *          CreateIconFromResourceEx32          (USER32.77)
590  */
591 HICON32 WINAPI CreateIconFromResourceEx32( LPBYTE bits, UINT32 cbSize,
592                                            BOOL32 bIcon, DWORD dwVersion,
593                                            INT32 width, INT32 height,
594                                            UINT32 cFlag )
595 {
596     TDB* pTask = (TDB*)GlobalLock16( GetCurrentTask() );
597     if( pTask )
598         return CURSORICON_CreateFromResource( pTask->hInstance, 0, bits, cbSize, bIcon, dwVersion,
599                                               width, height, cFlag );
600     return 0;
601 }
602
603
604 /**********************************************************************
605  *          CURSORICON_Load16
606  *
607  * Load a cursor or icon from a 16-bit resource.
608  */
609 static HGLOBAL16 CURSORICON_Load16( HINSTANCE16 hInstance, SEGPTR name,
610                                     INT32 width, INT32 height, INT32 colors,
611                                     BOOL32 fCursor, UINT32 loadflags)
612 {
613     HGLOBAL16 handle = 0;
614     HRSRC16 hRsrc;
615     CURSORICONDIRENTRY dirEntry;
616
617     if (!hInstance)  /* OEM cursor/icon */
618     {
619         HDC32 hdc;
620         DC *dc;
621
622         if (HIWORD(name))  /* Check for '#xxx' name */
623         {
624             char *ptr = PTR_SEG_TO_LIN( name );
625             if (ptr[0] != '#') return 0;
626             if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
627         }
628         hdc = CreateDC32A( "DISPLAY", NULL, NULL, NULL );
629         dc = DC_GetDCPtr( hdc );
630         if(dc->funcs->pLoadOEMResource)
631             handle = dc->funcs->pLoadOEMResource( LOWORD(name), fCursor ?
632                                                   OEM_CURSOR : OEM_ICON);
633         GDI_HEAP_UNLOCK( hdc );
634         DeleteDC32( hdc );
635         return handle;
636     }
637
638     /* Find the best entry in the directory */
639
640     if ( !CURSORICON_LoadDirEntry16( hInstance, name, width, height,
641                                     colors, fCursor, &dirEntry ) )  return 0;
642     /* Load the resource */
643
644     if ( (hRsrc = FindResource16( hInstance,
645                                 MAKEINTRESOURCE16( dirEntry.icon.wResId ),
646                                 fCursor ? RT_CURSOR16 : RT_ICON16 )) )
647     {
648         /* 16-bit icon or cursor resources are processed
649          * transparently by the LoadResource16() via custom
650          * resource handlers set by SetResourceHandler().
651          */
652
653         if ( (handle = LoadResource16( hInstance, hRsrc )) )
654             return handle;
655     }
656     return 0;
657 }
658
659 /**********************************************************************
660  *          CURSORICON_Load32
661  *
662  * Load a cursor or icon from a 32-bit resource.
663  */
664 HGLOBAL32 CURSORICON_Load32( HINSTANCE32 hInstance, LPCWSTR name,
665                              int width, int height, int colors,
666                              BOOL32 fCursor, UINT32 loadflags )
667 {
668     HANDLE32 handle = 0, h = 0;
669     HANDLE32 hRsrc;
670     CURSORICONDIRENTRY dirEntry;
671     LPBYTE bits;
672
673     if (!(loadflags & LR_LOADFROMFILE))
674     {
675         if (!hInstance)  /* OEM cursor/icon */
676         {
677             WORD resid;
678             HDC32 hdc;
679             DC *dc;
680
681             if(HIWORD(name))
682             {
683                 LPSTR ansi = HEAP_strdupWtoA(GetProcessHeap(),0,name);
684                 if( ansi[0]=='#')        /*Check for '#xxx' name */
685                 {
686                     resid = atoi(ansi+1);
687                     HeapFree( GetProcessHeap(), 0, ansi );
688                 }
689                 else
690                 {
691                     HeapFree( GetProcessHeap(), 0, ansi );
692                     return 0;
693                 }
694             }
695             else resid = LOWORD(name);
696             hdc = CreateDC32A( "DISPLAY", NULL, NULL, NULL );
697             dc = DC_GetDCPtr( hdc );
698             if(dc->funcs->pLoadOEMResource)
699                 handle = dc->funcs->pLoadOEMResource( resid, fCursor ?
700                                                       OEM_CURSOR : OEM_ICON );
701             GDI_HEAP_UNLOCK( hdc );
702             DeleteDC32(  hdc );
703             return handle;
704         }
705
706         /* Find the best entry in the directory */
707  
708         if (!CURSORICON_LoadDirEntry32( hInstance, name, width, height,
709                                         colors, fCursor, &dirEntry ) )  return 0;
710         /* Load the resource */
711
712         if (!(hRsrc = FindResource32W(hInstance,MAKEINTRESOURCE32W(dirEntry.icon.wResId),
713                                       fCursor ? RT_CURSOR32W : RT_ICON32W ))) return 0;
714         if (!(handle = LoadResource32( hInstance, hRsrc ))) return 0;
715         /* Hack to keep LoadCursor/Icon32() from spawning multiple
716          * copies of the same object.
717          */
718 #define pRsrcEntry ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)
719         if( pRsrcEntry->ResourceHandle ) return pRsrcEntry->ResourceHandle;
720         bits = (LPBYTE)LockResource32( handle );
721         h = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry.icon.dwBytesInRes, 
722                                            !fCursor, 0x00030000, width, height, loadflags);
723         pRsrcEntry->ResourceHandle = h;
724     }
725     else
726     {
727         CURSORICONDIR *res;
728         LPBYTE *ptr;
729         if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &res, &ptr))
730             return 0;
731         if (fCursor)
732             dirEntry = *(CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(res, width, height, 1);
733         else
734             dirEntry = *(CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(res, width, height, colors);
735         bits = ptr[dirEntry.icon.wResId-1];
736         h = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry.icon.dwBytesInRes, 
737                                            !fCursor, 0x00030000, width, height, loadflags);
738         HeapFree( GetProcessHeap(), 0, res );
739         HeapFree( GetProcessHeap(), 0, ptr );
740     }
741     return h;
742 #undef  pRsrcEntry
743 }
744
745
746 /***********************************************************************
747  *           CURSORICON_Copy
748  *
749  * Make a copy of a cursor or icon.
750  */
751 static HGLOBAL16 CURSORICON_Copy( HINSTANCE16 hInstance, HGLOBAL16 handle )
752 {
753     char *ptrOld, *ptrNew;
754     int size;
755     HGLOBAL16 hNew;
756
757     if (!(ptrOld = (char *)GlobalLock16( handle ))) return 0;
758     if (!(hInstance = GetExePtr( hInstance ))) return 0;
759     size = GlobalSize16( handle );
760     hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
761     FarSetOwner( hNew, hInstance );
762     ptrNew = (char *)GlobalLock16( hNew );
763     memcpy( ptrNew, ptrOld, size );
764     GlobalUnlock16( handle );
765     GlobalUnlock16( hNew );
766     return hNew;
767 }
768
769 /***********************************************************************
770  *           CURSORICON_IconToCursor
771  *
772  * Converts bitmap to mono and truncates if icon is too large (should
773  * probably do StretchBlt() instead).
774  */
775 HCURSOR16 CURSORICON_IconToCursor(HICON16 hIcon, BOOL32 bSemiTransparent)
776 {
777  HCURSOR16       hRet = 0;
778  CURSORICONINFO *pIcon = NULL;
779  HTASK16         hTask = GetCurrentTask();
780  TDB*            pTask = (TDB *)GlobalLock16(hTask);
781
782  if(hIcon && pTask)
783     if (!(pIcon = (CURSORICONINFO*)GlobalLock16( hIcon ))) return FALSE;
784        if (pIcon->bPlanes * pIcon->bBitsPerPixel == 1)
785            hRet = CURSORICON_Copy( pTask->hInstance, hIcon );
786        else
787        {
788            BYTE  pAndBits[128];
789            BYTE  pXorBits[128];
790            int   maxx, maxy, ix, iy, bpp = pIcon->bBitsPerPixel;
791            BYTE* psPtr, *pxbPtr = pXorBits;
792            unsigned xor_width, and_width, val_base = 0xffffffff >> (32 - bpp);
793            BYTE* pbc = NULL;
794
795            COLORREF       col;
796            CURSORICONINFO cI;
797
798            TRACE(icon, "[%04x] %ix%i %ibpp (bogus %ibps)\n", 
799                 hIcon, pIcon->nWidth, pIcon->nHeight, pIcon->bBitsPerPixel, pIcon->nWidthBytes );
800
801            xor_width = BITMAP_GetWidthBytes( pIcon->nWidth, bpp );
802            and_width = BITMAP_GetWidthBytes( pIcon->nWidth, 1 );
803            psPtr = (BYTE *)(pIcon + 1) + pIcon->nHeight * and_width;
804
805            memset(pXorBits, 0, 128);
806            cI.bBitsPerPixel = 1; cI.bPlanes = 1;
807            cI.ptHotSpot.x = cI.ptHotSpot.y = 15;
808            cI.nWidth = 32; cI.nHeight = 32;
809            cI.nWidthBytes = 4;  /* 32x1bpp */
810
811            maxx = (pIcon->nWidth > 32) ? 32 : pIcon->nWidth;
812            maxy = (pIcon->nHeight > 32) ? 32 : pIcon->nHeight;
813
814            for( iy = 0; iy < maxy; iy++ )
815            {
816               unsigned shift = iy % 2; 
817
818               memcpy( pAndBits + iy * 4, (BYTE *)(pIcon + 1) + iy * and_width, 
819                                          (and_width > 4) ? 4 : and_width );
820               for( ix = 0; ix < maxx; ix++ )
821               {
822                 if( bSemiTransparent && ((ix+shift)%2) )
823                 {
824                     /* set AND bit, XOR bit stays 0 */
825
826                     pbc = pAndBits + iy * 4 + ix/8;
827                    *pbc |= 0x80 >> (ix%8);
828                 }
829                 else
830                 {
831                     /* keep AND bit, set XOR bit */
832
833                   unsigned *psc = (unsigned*)(psPtr + (ix * bpp)/8);
834                   unsigned  val = ((*psc) >> (ix * bpp)%8) & val_base;
835                   col = COLOR_ToLogical(val);
836                   if( (GetRValue(col) + GetGValue(col) + GetBValue(col)) > 0x180 )
837                   {
838                     pbc = pxbPtr + ix/8;
839                    *pbc |= 0x80 >> (ix%8);
840                   }
841                 }
842               }
843               psPtr += xor_width;
844               pxbPtr += 4;
845            }
846
847            hRet = CreateCursorIconIndirect( pTask->hInstance , &cI, pAndBits, pXorBits);
848
849            if( !hRet ) /* fall back on default drag cursor */
850                 hRet = CURSORICON_Copy( pTask->hInstance ,
851                               CURSORICON_Load16(0,MAKEINTRESOURCE16(OCR_DRAGOBJECT),
852                                          SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE, 0) );
853        }
854
855  return hRet;
856 }
857
858
859 /***********************************************************************
860  *           LoadCursor16    (USER.173)
861  */
862 HCURSOR16 WINAPI LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
863 {
864     if (HIWORD(name))
865         TRACE(cursor, "%04x '%s'\n",
866                         hInstance, (char *)PTR_SEG_TO_LIN( name ) );
867     else
868         TRACE(cursor, "%04x %04x\n",
869                         hInstance, LOWORD(name) );
870
871     return CURSORICON_Load16( hInstance, name,
872                               SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE, 0);
873 }
874
875
876 /***********************************************************************
877  *           LoadIcon16    (USER.174)
878  */
879 HICON16 WINAPI LoadIcon16( HINSTANCE16 hInstance, SEGPTR name )
880 {
881     HDC32 hdc = GetDC32(0);
882     UINT32 palEnts = GetSystemPaletteEntries32(hdc, 0, 0, NULL);
883     ReleaseDC32(0, hdc);
884
885     if (HIWORD(name))
886         TRACE(icon, "%04x '%s'\n",
887                       hInstance, (char *)PTR_SEG_TO_LIN( name ) );
888     else
889         TRACE(icon, "%04x %04x\n",
890                       hInstance, LOWORD(name) );
891
892     return CURSORICON_Load16( hInstance, name,
893                               SYSMETRICS_CXICON, SYSMETRICS_CYICON,
894                               MIN(16, palEnts), FALSE, 0);
895 }
896
897
898 /***********************************************************************
899  *           CreateCursor16    (USER.406)
900  */
901 HCURSOR16 WINAPI CreateCursor16( HINSTANCE16 hInstance,
902                                  INT16 xHotSpot, INT16 yHotSpot,
903                                  INT16 nWidth, INT16 nHeight,
904                                  LPCVOID lpANDbits, LPCVOID lpXORbits )
905 {
906     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
907
908     TRACE(cursor, "%dx%d spot=%d,%d xor=%p and=%p\n",
909                     nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
910     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
911 }
912
913
914 /***********************************************************************
915  *           CreateCursor32    (USER32.67)
916  */
917 HCURSOR32 WINAPI CreateCursor32( HINSTANCE32 hInstance,
918                                  INT32 xHotSpot, INT32 yHotSpot,
919                                  INT32 nWidth, INT32 nHeight,
920                                  LPCVOID lpANDbits, LPCVOID lpXORbits )
921 {
922     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
923
924     TRACE(cursor, "%dx%d spot=%d,%d xor=%p and=%p\n",
925                     nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
926     return CreateCursorIconIndirect( 0, &info, lpANDbits, lpXORbits );
927 }
928
929
930 /***********************************************************************
931  *           CreateIcon16    (USER.407)
932  */
933 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
934                              INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
935                              LPCVOID lpANDbits, LPCVOID lpXORbits )
936 {
937     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
938
939     TRACE(icon, "%dx%dx%d, xor=%p, and=%p\n",
940                   nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
941     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
942 }
943
944
945 /***********************************************************************
946  *           CreateIcon32    (USER32.75)
947  */
948 HICON32 WINAPI CreateIcon32( HINSTANCE32 hInstance, INT32 nWidth,
949                              INT32 nHeight, BYTE bPlanes, BYTE bBitsPixel,
950                              LPCVOID lpANDbits, LPCVOID lpXORbits )
951 {
952     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
953
954     TRACE(icon, "%dx%dx%d, xor=%p, and=%p\n",
955                   nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
956     return CreateCursorIconIndirect( 0, &info, lpANDbits, lpXORbits );
957 }
958
959
960 /***********************************************************************
961  *           CreateCursorIconIndirect    (USER.408)
962  */
963 HGLOBAL16 WINAPI CreateCursorIconIndirect( HINSTANCE16 hInstance,
964                                            CURSORICONINFO *info,
965                                            LPCVOID lpANDbits,
966                                            LPCVOID lpXORbits )
967 {
968     HGLOBAL16 handle;
969     char *ptr;
970     int sizeAnd, sizeXor;
971
972     hInstance = GetExePtr( hInstance );  /* Make it a module handle */
973     if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
974     info->nWidthBytes = BITMAP_GetWidthBytes(info->nWidth,info->bBitsPerPixel);
975     sizeXor = info->nHeight * info->nWidthBytes;
976     sizeAnd = info->nHeight * BITMAP_GetWidthBytes( info->nWidth, 1 );
977     if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
978                                   sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
979         return 0;
980     if (hInstance) FarSetOwner( handle, hInstance );
981     ptr = (char *)GlobalLock16( handle );
982     memcpy( ptr, info, sizeof(*info) );
983     memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
984     memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
985     GlobalUnlock16( handle );
986     return handle;
987 }
988
989
990 /***********************************************************************
991  *           CopyIcon16    (USER.368)
992  */
993 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
994 {
995     TRACE(icon, "%04x %04x\n", hInstance, hIcon );
996     return CURSORICON_Copy( hInstance, hIcon );
997 }
998
999
1000 /***********************************************************************
1001  *           CopyIcon32    (USER32.60)
1002  */
1003 HICON32 WINAPI CopyIcon32( HICON32 hIcon )
1004 {
1005   HTASK16 hTask = GetCurrentTask ();
1006   TDB* pTask = (TDB *) GlobalLock16 (hTask);
1007     TRACE(icon, "%04x\n", hIcon );
1008   return CURSORICON_Copy( pTask->hInstance, hIcon );
1009 }
1010
1011
1012 /***********************************************************************
1013  *           CopyCursor16    (USER.369)
1014  */
1015 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1016 {
1017     TRACE(cursor, "%04x %04x\n", hInstance, hCursor );
1018     return CURSORICON_Copy( hInstance, hCursor );
1019 }
1020
1021
1022 /***********************************************************************
1023  *           DestroyIcon16    (USER.457)
1024  */
1025 BOOL16 WINAPI DestroyIcon16( HICON16 hIcon )
1026 {
1027     TRACE(icon, "%04x\n", hIcon );
1028     /* FIXME: should check for OEM/global heap icon here */
1029     return (FreeResource16( hIcon ) == 0);
1030 }
1031
1032
1033 /***********************************************************************
1034  *           DestroyIcon32    (USER32.133)
1035  */
1036 BOOL32 WINAPI DestroyIcon32( HICON32 hIcon )
1037 {
1038     TRACE(icon, "%04x\n", hIcon );
1039     /* FIXME: should check for OEM/global heap icon here */
1040     /* Unlike DestroyIcon16, only icons created with CreateIcon32
1041        are valid for DestroyIcon32, so don't use FreeResource32 */
1042     return (GlobalFree16( hIcon ) == 0);
1043 }
1044
1045
1046 /***********************************************************************
1047  *           DestroyCursor16    (USER.458)
1048  */
1049 BOOL16 WINAPI DestroyCursor16( HCURSOR16 hCursor )
1050 {
1051     TRACE(cursor, "%04x\n", hCursor );
1052     if (FreeResource16( hCursor ) == 0)
1053       return TRUE;
1054     else
1055       /* I believe this very same line should be added for every function
1056          where appears the comment:
1057
1058          "FIXME: should check for OEM/global heap cursor here"
1059
1060          which are most (all?) the ones that call FreeResource, at least
1061          in this module. Maybe this should go to a wrapper to avoid
1062          repetition. Or: couldn't it go to FreeResoutce itself?
1063          
1064          I'll let this to someone savvy on the subject.
1065          */
1066       return (GlobalFree16 (hCursor) == 0);
1067 }
1068
1069
1070 /***********************************************************************
1071  *           DestroyCursor32    (USER32.132)
1072  */
1073 BOOL32 WINAPI DestroyCursor32( HCURSOR32 hCursor )
1074 {
1075     TRACE(cursor, "%04x\n", hCursor );
1076     /* FIXME: should check for OEM/global heap cursor here */
1077     /* Unlike DestroyCursor16, only cursors created with CreateCursor32
1078        are valid for DestroyCursor32, so don't use FreeResource32 */
1079     return (GlobalFree16( hCursor ) == 0);
1080 }
1081
1082
1083 /***********************************************************************
1084  *           DrawIcon16    (USER.84)
1085  */
1086 BOOL16 WINAPI DrawIcon16( HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon )
1087 {
1088     return DrawIcon32( hdc, x, y, hIcon );
1089 }
1090
1091
1092 /***********************************************************************
1093  *           DrawIcon32    (USER32.159)
1094  */
1095 BOOL32 WINAPI DrawIcon32( HDC32 hdc, INT32 x, INT32 y, HICON32 hIcon )
1096 {
1097     CURSORICONINFO *ptr;
1098     HDC32 hMemDC;
1099     HBITMAP32 hXorBits, hAndBits;
1100     COLORREF oldFg, oldBg;
1101
1102     if (!(ptr = (CURSORICONINFO *)GlobalLock16( hIcon ))) return FALSE;
1103     if (!(hMemDC = CreateCompatibleDC32( hdc ))) return FALSE;
1104     hAndBits = CreateBitmap32( ptr->nWidth, ptr->nHeight, 1, 1,
1105                                (char *)(ptr+1) );
1106     hXorBits = CreateBitmap32( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1107                                ptr->bBitsPerPixel, (char *)(ptr + 1)
1108                         + ptr->nHeight * BITMAP_GetWidthBytes(ptr->nWidth,1) );
1109     oldFg = SetTextColor32( hdc, RGB(0,0,0) );
1110     oldBg = SetBkColor32( hdc, RGB(255,255,255) );
1111
1112     if (hXorBits && hAndBits)
1113     {
1114         HBITMAP32 hBitTemp = SelectObject32( hMemDC, hAndBits );
1115         BitBlt32( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1116         SelectObject32( hMemDC, hXorBits );
1117         BitBlt32(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1118         SelectObject32( hMemDC, hBitTemp );
1119     }
1120     DeleteDC32( hMemDC );
1121     if (hXorBits) DeleteObject32( hXorBits );
1122     if (hAndBits) DeleteObject32( hAndBits );
1123     GlobalUnlock16( hIcon );
1124     SetTextColor32( hdc, oldFg );
1125     SetBkColor32( hdc, oldBg );
1126     return TRUE;
1127 }
1128
1129
1130 /***********************************************************************
1131  *           DumpIcon    (USER.459)
1132  */
1133 DWORD WINAPI DumpIcon( SEGPTR pInfo, WORD *lpLen,
1134                        SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1135 {
1136     CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
1137     int sizeAnd, sizeXor;
1138
1139     if (!info) return 0;
1140     sizeXor = info->nHeight * info->nWidthBytes;
1141     sizeAnd = info->nHeight * BITMAP_GetWidthBytes( info->nWidth, 1 );
1142     if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1143     if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1144     if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1145     return MAKELONG( sizeXor, sizeXor );
1146 }
1147
1148
1149 /***********************************************************************
1150  *           SetCursor16    (USER.69)
1151  */
1152 HCURSOR16 WINAPI SetCursor16( HCURSOR16 hCursor )
1153 {
1154     return (HCURSOR16)SetCursor32( hCursor );
1155 }
1156
1157
1158 /***********************************************************************
1159  *           SetCursor32    (USER32.472)
1160  * RETURNS:
1161  *      A handle to the previous cursor shape.
1162  */
1163 HCURSOR32 WINAPI SetCursor32(
1164                  HCURSOR32 hCursor /* Handle of cursor to show */
1165 ) {
1166     HCURSOR32 hOldCursor;
1167
1168     if (hCursor == hActiveCursor) return hActiveCursor;  /* No change */
1169     TRACE(cursor, "%04x\n", hCursor );
1170     hOldCursor = hActiveCursor;
1171     hActiveCursor = hCursor;
1172     /* Change the cursor shape only if it is visible */
1173     if (CURSOR_ShowCount >= 0)
1174     {
1175         DISPLAY_SetCursor( (CURSORICONINFO*)GlobalLock16( hActiveCursor ) );
1176         GlobalUnlock16( hActiveCursor );
1177     }
1178     return hOldCursor;
1179 }
1180
1181
1182 /***********************************************************************
1183  *           SetCursorPos16    (USER.70)
1184  */
1185 void WINAPI SetCursorPos16( INT16 x, INT16 y )
1186 {
1187     SetCursorPos32( x, y );
1188 }
1189
1190
1191 /***********************************************************************
1192  *           SetCursorPos32    (USER32.474)
1193  */
1194 BOOL32 WINAPI SetCursorPos32( INT32 x, INT32 y )
1195 {
1196     DISPLAY_MoveCursor( x, y );
1197     return TRUE;
1198 }
1199
1200
1201 /***********************************************************************
1202  *           ShowCursor16    (USER.71)
1203  */
1204 INT16 WINAPI ShowCursor16( BOOL16 bShow )
1205 {
1206     return ShowCursor32( bShow );
1207 }
1208
1209
1210 /***********************************************************************
1211  *           ShowCursor32    (USER32.530)
1212  */
1213 INT32 WINAPI ShowCursor32( BOOL32 bShow )
1214 {
1215     TRACE(cursor, "%d, count=%d\n",
1216                     bShow, CURSOR_ShowCount );
1217
1218     if (bShow)
1219     {
1220         if (++CURSOR_ShowCount == 0)  /* Show it */
1221         {
1222             DISPLAY_SetCursor((CURSORICONINFO*)GlobalLock16( hActiveCursor ));
1223             GlobalUnlock16( hActiveCursor );
1224         }
1225     }
1226     else
1227     {
1228         if (--CURSOR_ShowCount == -1)  /* Hide it */
1229             DISPLAY_SetCursor( NULL );
1230     }
1231     return CURSOR_ShowCount;
1232 }
1233
1234
1235 /***********************************************************************
1236  *           GetCursor16    (USER.247)
1237  */
1238 HCURSOR16 WINAPI GetCursor16(void)
1239 {
1240     return hActiveCursor;
1241 }
1242
1243
1244 /***********************************************************************
1245  *           GetCursor32    (USER32.227)
1246  */
1247 HCURSOR32 WINAPI GetCursor32(void)
1248 {
1249     return hActiveCursor;
1250 }
1251
1252
1253 /***********************************************************************
1254  *           ClipCursor16    (USER.16)
1255  */
1256 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
1257 {
1258     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
1259     else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1260     return TRUE;
1261 }
1262
1263
1264 /***********************************************************************
1265  *           ClipCursor32    (USER32.53)
1266  */
1267 BOOL32 WINAPI ClipCursor32( const RECT32 *rect )
1268 {
1269     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
1270     else CopyRect32( &CURSOR_ClipRect, rect );
1271     return TRUE;
1272 }
1273
1274
1275 /***********************************************************************
1276  *           GetCursorPos16    (USER.17)
1277  */
1278 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
1279 {
1280     DWORD posX, posY, state;
1281
1282     if (!pt) return 0;
1283     if (!EVENT_QueryPointer( &posX, &posY, &state ))
1284         pt->x = pt->y = 0;
1285     else
1286     {
1287         pt->x = posX;
1288         pt->y = posY;
1289         if (state & MK_LBUTTON)
1290             AsyncMouseButtonsStates[0] = MouseButtonsStates[0] = TRUE;
1291         else
1292             MouseButtonsStates[0] = FALSE;
1293         if (state & MK_MBUTTON)
1294             AsyncMouseButtonsStates[1] = MouseButtonsStates[1] = TRUE;
1295         else       
1296             MouseButtonsStates[1] = FALSE;
1297         if (state & MK_RBUTTON)
1298             AsyncMouseButtonsStates[2] = MouseButtonsStates[2] = TRUE;
1299         else
1300             MouseButtonsStates[2] = FALSE;
1301     }
1302     TRACE(cursor, "ret=%d,%d\n", pt->x, pt->y );
1303     return 1;
1304 }
1305
1306
1307 /***********************************************************************
1308  *           GetCursorPos32    (USER32.229)
1309  */
1310 BOOL32 WINAPI GetCursorPos32( POINT32 *pt )
1311 {
1312     BOOL32 ret;
1313
1314     POINT16 pt16;
1315     ret = GetCursorPos16( &pt16 );
1316     if (pt) CONV_POINT16TO32( &pt16, pt );
1317     return ((pt) ? ret : 0);
1318 }
1319
1320
1321 /***********************************************************************
1322  *           GetClipCursor16    (USER.309)
1323  */
1324 void WINAPI GetClipCursor16( RECT16 *rect )
1325 {
1326     if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1327 }
1328
1329
1330 /***********************************************************************
1331  *           GetClipCursor32    (USER32.221)
1332  */
1333 BOOL32 WINAPI GetClipCursor32( RECT32 *rect )
1334 {
1335     if (rect) 
1336     {
1337        CopyRect32( rect, &CURSOR_ClipRect );
1338        return TRUE;
1339     }
1340     return FALSE;
1341 }
1342
1343 /**********************************************************************
1344  *          LookupIconIdFromDirectoryEx16       (USER.364)
1345  *
1346  * FIXME: exact parameter sizes
1347  */
1348 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE xdir, BOOL16 bIcon,
1349              INT16 width, INT16 height, UINT16 cFlag )
1350 {
1351     CURSORICONDIR       *dir = (CURSORICONDIR*)xdir;
1352     UINT16 retVal = 0;
1353     if( dir && !dir->idReserved && (dir->idType & 3) )
1354     {
1355         HDC32 hdc = GetDC32(0);
1356         UINT32 palEnts = GetSystemPaletteEntries32(hdc, 0, 0, NULL);
1357         int colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1358         ReleaseDC32(0, hdc);
1359
1360         if( bIcon )
1361         {
1362             ICONDIRENTRY* entry;
1363             entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1364             if( entry ) retVal = entry->wResId;
1365         }
1366         else
1367         {
1368             CURSORDIRENTRY* entry;
1369             entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1370             if( entry ) retVal = entry->wResId;
1371         }
1372     }
1373     else WARN(cursor, "invalid resource directory\n");
1374     return retVal;
1375 }
1376
1377 /**********************************************************************
1378  *          LookupIconIdFromDirectoryEx32       (USER32.380)
1379  */
1380 INT32 WINAPI LookupIconIdFromDirectoryEx32( LPBYTE dir, BOOL32 bIcon,
1381              INT32 width, INT32 height, UINT32 cFlag )
1382 {
1383     return LookupIconIdFromDirectoryEx16( dir, bIcon, width, height, cFlag );
1384 }
1385
1386 /**********************************************************************
1387  *          LookupIconIdFromDirectory           (USER.???)
1388  */
1389 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1390 {
1391     return LookupIconIdFromDirectoryEx16( dir, bIcon, 
1392            bIcon ? SYSMETRICS_CXICON : SYSMETRICS_CXCURSOR,
1393            bIcon ? SYSMETRICS_CYICON : SYSMETRICS_CYCURSOR, bIcon ? 0 : LR_MONOCHROME );
1394 }
1395
1396 /**********************************************************************
1397  *          LookupIconIdFromDirectory           (USER32.379)
1398  */
1399 INT32 WINAPI LookupIconIdFromDirectory32( LPBYTE dir, BOOL32 bIcon )
1400 {
1401     return LookupIconIdFromDirectoryEx32( dir, bIcon, 
1402            bIcon ? SYSMETRICS_CXICON : SYSMETRICS_CXCURSOR,
1403            bIcon ? SYSMETRICS_CYICON : SYSMETRICS_CYCURSOR, bIcon ? 0 : LR_MONOCHROME );
1404 }
1405
1406 /**********************************************************************
1407  *          GetIconID    (USER.455)
1408  */
1409 WORD WINAPI GetIconID( HGLOBAL16 hResource, DWORD resType )
1410 {
1411     LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1412
1413     TRACE(cursor, "hRes=%04x, entries=%i\n",
1414                     hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1415
1416     switch(resType)
1417     {
1418         case RT_CURSOR16:
1419              return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE, 
1420                           SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, LR_MONOCHROME );
1421         case RT_ICON16:
1422              return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1423                           SYSMETRICS_CXICON, SYSMETRICS_CYICON, 0 );
1424         default:
1425              WARN(cursor, "invalid res type %ld\n", resType );
1426     }
1427     return 0;
1428 }
1429
1430 /**********************************************************************
1431  *          LoadCursorIconHandler    (USER.336)
1432  *
1433  * Supposed to load resources of Windows 2.x applications.
1434  */
1435 HGLOBAL16 WINAPI LoadCursorIconHandler( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1436 {
1437     FIXME(cursor,"(%04x,%04x,%04x): old 2.x resources are not supported!\n", 
1438           hResource, hModule, hRsrc);
1439     return (HGLOBAL16)0;
1440 }
1441
1442 /**********************************************************************
1443  *          LoadDIBIconHandler    (USER.357)
1444  * 
1445  * RT_ICON resource loader, installed by USER_SignalProc when module
1446  * is initialized.
1447  */
1448 HGLOBAL16 WINAPI LoadDIBIconHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1449 {
1450     /* If hResource is zero we must allocate a new memory block, if it's
1451      * non-zero but GlobalLock() returns NULL then it was discarded and
1452      * we have to recommit some memory, otherwise we just need to check 
1453      * the block size. See LoadProc() in 16-bit SDK for more.
1454      */
1455
1456      hMemObj = USER_CallDefaultRsrcHandler( hMemObj, hModule, hRsrc );
1457      if( hMemObj )
1458      {
1459          LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1460          hMemObj = CURSORICON_CreateFromResource( hModule, hMemObj, bits, 
1461                    SizeofResource16(hModule, hRsrc), TRUE, 0x00030000, 
1462                    SYSMETRICS_CXICON, SYSMETRICS_CYICON, LR_DEFAULTCOLOR );
1463      }
1464      return hMemObj;
1465 }
1466
1467 /**********************************************************************
1468  *          LoadDIBCursorHandler    (USER.356)
1469  *
1470  * RT_CURSOR resource loader. Same as above.
1471  */
1472 HGLOBAL16 WINAPI LoadDIBCursorHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1473 {
1474     hMemObj = USER_CallDefaultRsrcHandler( hMemObj, hModule, hRsrc );
1475     if( hMemObj )
1476     {
1477         LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1478         hMemObj = CURSORICON_CreateFromResource( hModule, hMemObj, bits,
1479                   SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1480                   SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, LR_MONOCHROME );
1481     }
1482     return hMemObj;
1483 }
1484
1485 /**********************************************************************
1486  *          LoadIconHandler    (USER.456)
1487  */
1488 HICON16 WINAPI LoadIconHandler( HGLOBAL16 hResource, BOOL16 bNew )
1489 {
1490     LPBYTE bits = (LPBYTE)LockResource16( hResource );
1491
1492     TRACE(cursor,"hRes=%04x\n",hResource);
1493
1494     return CURSORICON_CreateFromResource( 0, 0, bits, 0, TRUE, 
1495                       bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR );
1496 }
1497
1498 /***********************************************************************
1499  *           LoadCursorW                (USER32.362)
1500  */
1501 HCURSOR32 WINAPI LoadCursor32W(HINSTANCE32 hInstance, LPCWSTR name)
1502 {
1503     return CURSORICON_Load32( hInstance, name,
1504                               SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE, 0);
1505 }
1506
1507 /***********************************************************************
1508  *           LoadCursorA                (USER32.359)
1509  */
1510 HCURSOR32 WINAPI LoadCursor32A(HINSTANCE32 hInstance, LPCSTR name)
1511 {
1512         HCURSOR32 res=0;
1513         if(!HIWORD(name))
1514                 return LoadCursor32W(hInstance,(LPCWSTR)name);
1515         else
1516         {
1517             LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
1518             res = LoadCursor32W(hInstance, uni);
1519             HeapFree( GetProcessHeap(), 0, uni);
1520         }
1521         return res;
1522 }
1523 /***********************************************************************
1524 *            LoadCursorFromFile32W    (USER32.361)
1525 */
1526 HCURSOR32 WINAPI LoadCursorFromFile32W (LPCWSTR name)
1527 {
1528     return LoadImage32W(0, name, IMAGE_CURSOR, SYSMETRICS_CXCURSOR,
1529       SYSMETRICS_CYCURSOR, LR_LOADFROMFILE);
1530 }
1531
1532 /***********************************************************************
1533 *            LoadCursorFromFile32A    (USER32.360)
1534 */
1535 HCURSOR32 WINAPI LoadCursorFromFile32A (LPCSTR name)
1536 {
1537     HCURSOR32 hcur;
1538     LPWSTR u_name = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
1539     hcur = LoadCursorFromFile32W(u_name);
1540     HeapFree( GetProcessHeap(), 0, u_name );
1541     return hcur;
1542 }
1543   
1544 /***********************************************************************
1545  *           LoadIconW          (USER32.364)
1546  */
1547 HICON32 WINAPI LoadIcon32W(HINSTANCE32 hInstance, LPCWSTR name)
1548 {
1549     HDC32 hdc = GetDC32(0);
1550     UINT32 palEnts = GetSystemPaletteEntries32(hdc, 0, 0, NULL);
1551     ReleaseDC32(0, hdc);
1552
1553     return CURSORICON_Load32( hInstance, name,
1554                               SYSMETRICS_CXICON, SYSMETRICS_CYICON,
1555                               MIN( 16, palEnts ), FALSE, 0);
1556 }
1557
1558 /***********************************************************************
1559  *           LoadIconA          (USER32.363)
1560  */
1561 HICON32 WINAPI LoadIcon32A(HINSTANCE32 hInstance, LPCSTR name)
1562 {
1563     HICON32 res=0;
1564
1565     if( !HIWORD(name) )
1566         return LoadIcon32W(hInstance, (LPCWSTR)name);
1567     else
1568     {
1569         LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
1570         res = LoadIcon32W( hInstance, uni );
1571         HeapFree( GetProcessHeap(), 0, uni );
1572     }
1573     return res;
1574 }
1575
1576 /**********************************************************************
1577  *          GetIconInfo16       (USER.395)
1578  */
1579 BOOL16 WINAPI GetIconInfo16(HICON16 hIcon,LPICONINFO16 iconinfo)
1580 {
1581     ICONINFO32  ii32;
1582     BOOL16      ret = GetIconInfo32((HICON32)hIcon, &ii32);
1583
1584     iconinfo->fIcon = ii32.fIcon;
1585     iconinfo->xHotspot = ii32.xHotspot;
1586     iconinfo->yHotspot = ii32.yHotspot;
1587     iconinfo->hbmMask = ii32.hbmMask;
1588     iconinfo->hbmColor = ii32.hbmColor;
1589     return ret;
1590 }
1591
1592 /**********************************************************************
1593  *          GetIconInfo32               (USER32.242)
1594  */
1595 BOOL32 WINAPI GetIconInfo32(HICON32 hIcon,LPICONINFO32 iconinfo) {
1596     CURSORICONINFO      *ciconinfo;
1597
1598     ciconinfo = GlobalLock16(hIcon);
1599     if (!ciconinfo)
1600         return FALSE;
1601     iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1602     iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1603     iconinfo->fIcon    = TRUE; /* hmm */
1604
1605     iconinfo->hbmColor = CreateBitmap32 ( ciconinfo->nWidth, ciconinfo->nHeight,
1606                                 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1607                                 (char *)(ciconinfo + 1)
1608                                 + ciconinfo->nHeight *
1609                                 BITMAP_GetWidthBytes (ciconinfo->nWidth,1) );
1610     iconinfo->hbmMask = CreateBitmap32 ( ciconinfo->nWidth, ciconinfo->nHeight,
1611                                 1, 1, (char *)(ciconinfo + 1));
1612
1613     GlobalUnlock16(hIcon);
1614
1615     return TRUE;
1616 }
1617
1618 /**********************************************************************
1619  *          CreateIconIndirect          (USER32.78)
1620  */
1621 HICON32 WINAPI CreateIconIndirect(LPICONINFO32 iconinfo) {
1622     BITMAPOBJ *bmpXor,*bmpAnd;
1623     HICON32 hObj;
1624     int sizeXor,sizeAnd;
1625
1626     bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( iconinfo->hbmColor, BITMAP_MAGIC );
1627     bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( iconinfo->hbmMask, BITMAP_MAGIC );
1628
1629     sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
1630     sizeAnd = bmpAnd->bitmap.bmHeight * bmpAnd->bitmap.bmWidthBytes;
1631
1632     hObj = GlobalAlloc16( GMEM_MOVEABLE, 
1633                      sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1634     if (hObj)
1635     {
1636         CURSORICONINFO *info;
1637
1638         info = (CURSORICONINFO *)GlobalLock16( hObj );
1639         info->ptHotSpot.x   = iconinfo->xHotspot;
1640         info->ptHotSpot.y   = iconinfo->yHotspot;
1641         info->nWidth        = bmpXor->bitmap.bmWidth;
1642         info->nHeight       = bmpXor->bitmap.bmHeight;
1643         info->nWidthBytes   = bmpXor->bitmap.bmWidthBytes;
1644         info->bPlanes       = bmpXor->bitmap.bmPlanes;
1645         info->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
1646
1647         /* Transfer the bitmap bits to the CURSORICONINFO structure */
1648
1649         GetBitmapBits32( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1650         GetBitmapBits32( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1651         GlobalUnlock16( hObj );
1652     }
1653     return hObj;
1654 }
1655
1656
1657 /**********************************************************************
1658  *          
1659  DrawIconEx16           (USER.394)
1660  */
1661 BOOL16 WINAPI DrawIconEx16 (HDC16 hdc, INT16 xLeft, INT16 yTop, HICON16 hIcon,
1662                             INT16 cxWidth, INT16 cyWidth, UINT16 istep,
1663                             HBRUSH16 hbr, UINT16 flags)
1664 {
1665     return DrawIconEx32(hdc, xLeft, yTop, hIcon, cxWidth, cyWidth,
1666                         istep, hbr, flags);
1667 }
1668
1669
1670 /******************************************************************************
1671  * DrawIconEx32 [USER32.160]  Draws an icon or cursor on device context
1672  *
1673  * NOTES
1674  *    Why is this using SM_CXICON instead of SM_CXCURSOR?
1675  *
1676  * PARAMS
1677  *    hdc     [I] Handle to device context
1678  *    x0      [I] X coordinate of upper left corner
1679  *    y0      [I] Y coordinate of upper left corner
1680  *    hIcon   [I] Handle to icon to draw
1681  *    cxWidth [I] Width of icon
1682  *    cyWidth [I] Height of icon
1683  *    istep   [I] Index of frame in animated cursor
1684  *    hbr     [I] Handle to background brush
1685  *    flags   [I] Icon-drawing flags
1686  *
1687  * RETURNS
1688  *    Success: TRUE
1689  *    Failure: FALSE
1690  */
1691 BOOL32 WINAPI DrawIconEx32( HDC32 hdc, INT32 x0, INT32 y0, HICON32 hIcon,
1692                             INT32 cxWidth, INT32 cyWidth, UINT32 istep, 
1693                             HBRUSH32 hbr, UINT32 flags )
1694 {
1695     CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16 (hIcon);
1696     HDC32 hDC_off = 0, hMemDC = CreateCompatibleDC32 (hdc);
1697     BOOL32 result = FALSE, DoOffscreen = FALSE;
1698     HBITMAP32 hB_off = 0, hOld = 0;
1699
1700     if (!ptr) return FALSE;
1701
1702     if (istep)
1703         FIXME(icon, "Ignoring istep=%d\n", istep);
1704     if (flags & DI_COMPAT)
1705         FIXME(icon, "Ignoring flag DI_COMPAT\n");
1706
1707     /* Calculate the size of the destination image.  */
1708     if (cxWidth == 0)
1709     {
1710       if (flags & DI_DEFAULTSIZE)
1711         cxWidth = GetSystemMetrics32 (SM_CXICON);
1712       else
1713         cxWidth = ptr->nWidth;
1714     }
1715     if (cyWidth == 0)
1716     {
1717       if (flags & DI_DEFAULTSIZE)
1718         cyWidth = GetSystemMetrics32 (SM_CYICON);
1719       else
1720         cyWidth = ptr->nHeight;
1721     }
1722
1723     if (!(DoOffscreen = (hbr >= STOCK_WHITE_BRUSH) && (hbr <= 
1724       STOCK_HOLLOW_BRUSH)))
1725     {
1726         GDIOBJHDR *object = (GDIOBJHDR *) GDI_HEAP_LOCK(hbr);
1727         if (object)
1728         {
1729             UINT16 magic = object->wMagic;
1730             GDI_HEAP_UNLOCK(hbr);
1731             DoOffscreen = magic == BRUSH_MAGIC;
1732         }
1733     }
1734     if (DoOffscreen) {
1735       RECT32 r = {0, 0, cxWidth, cxWidth};
1736
1737       hDC_off = CreateCompatibleDC32(hdc);
1738       hB_off = CreateCompatibleBitmap32(hdc, cxWidth, cyWidth);
1739       if (hDC_off && hB_off) {
1740         hOld = SelectObject32(hDC_off, hB_off);
1741         FillRect32(hDC_off, &r, hbr);
1742       }
1743     };
1744
1745     if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1746     {
1747         HBITMAP32 hXorBits, hAndBits;
1748         COLORREF  oldFg, oldBg;
1749         INT32     nStretchMode;
1750
1751         nStretchMode = SetStretchBltMode32 (hdc, STRETCH_DELETESCANS);
1752
1753         hXorBits = CreateBitmap32 ( ptr->nWidth, ptr->nHeight,
1754                                     ptr->bPlanes, ptr->bBitsPerPixel,
1755                                     (char *)(ptr + 1)
1756                                     + ptr->nHeight *
1757                                     BITMAP_GetWidthBytes(ptr->nWidth,1) );
1758         hAndBits = CreateBitmap32 ( ptr->nWidth, ptr->nHeight,
1759                                     1, 1, (char *)(ptr+1) );
1760         oldFg = SetTextColor32( hdc, RGB(0,0,0) );
1761         oldBg = SetBkColor32( hdc, RGB(255,255,255) );
1762
1763         if (hXorBits && hAndBits)
1764         {
1765             HBITMAP32 hBitTemp = SelectObject32( hMemDC, hAndBits );
1766             if (flags & DI_MASK)
1767             {
1768               if (DoOffscreen) 
1769                 StretchBlt32 (hDC_off, 0, 0, cxWidth, cyWidth,
1770                               hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1771               else 
1772                 StretchBlt32 (hdc, x0, y0, cxWidth, cyWidth,
1773                               hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1774             }
1775             SelectObject32( hMemDC, hXorBits );
1776             if (flags & DI_IMAGE)
1777             {
1778               if (DoOffscreen) 
1779                 StretchBlt32 (hDC_off, 0, 0, cxWidth, cyWidth,
1780                           hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1781               else
1782                 StretchBlt32 (hdc, x0, y0, cxWidth, cyWidth,
1783                               hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1784             }
1785             SelectObject32( hMemDC, hBitTemp );
1786             result = TRUE;
1787         }
1788
1789         SetTextColor32( hdc, oldFg );
1790         SetBkColor32( hdc, oldBg );
1791         if (hXorBits) DeleteObject32( hXorBits );
1792         if (hAndBits) DeleteObject32( hAndBits );
1793         SetStretchBltMode32 (hdc, nStretchMode);
1794         if (DoOffscreen) {
1795           BitBlt32(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1796           SelectObject32(hDC_off, hOld);
1797         }
1798     }
1799     if (hMemDC) DeleteDC32( hMemDC );
1800     if (hDC_off) DeleteDC32(hDC_off);
1801     if (hB_off) DeleteObject32(hB_off);
1802     GlobalUnlock16( hIcon );
1803     return result;
1804 }