Release 970305
[wine] / objects / cursoricon.c
1 /*
2  * Cursor and icon support
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
7 /*
8  * Theory:
9  *
10  * Cursors and icons are stored in a global heap block, with the
11  * following layout:
12  *
13  * CURSORICONINFO info;
14  * BYTE[]         ANDbits;
15  * BYTE[]         XORbits;
16  *
17  * The bits structures are in the format of a device-dependent bitmap.
18  *
19  * This layout is very sub-optimal, as the bitmap bits are stored in
20  * the X client instead of in the server like other bitmaps; however,
21  * some programs (notably Paint Brush) expect to be able to manipulate
22  * the bits directly :-(
23  */
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include "windows.h"
28 #include "color.h"
29 #include "bitmap.h"
30 #include "callback.h"
31 #include "cursoricon.h"
32 #include "sysmetrics.h"
33 #include "win.h"
34 #include "stddebug.h"
35 #include "debug.h"
36 #include "xmalloc.h"
37 #include "task.h"
38
39 extern UINT16 COLOR_GetSystemPaletteSize();
40
41 Cursor CURSORICON_XCursor = None;    /* Current X cursor */
42 static HCURSOR32 hActiveCursor = 0;  /* Active cursor */
43 static INT32 CURSOR_ShowCount = 0;   /* Cursor display count */
44 static RECT32 CURSOR_ClipRect;       /* Cursor clipping rect */
45
46 /**********************************************************************
47  *          CURSORICON_FindBestIcon
48  *
49  * Find the icon closest to the requested size and number of colors.
50  */
51 static ICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
52                                               int height, int colors )
53 {
54     int i, maxcolors, maxwidth, maxheight;
55     ICONDIRENTRY *entry, *bestEntry = NULL;
56
57     if (dir->idCount < 1)
58     {
59         fprintf( stderr, "Icon: empty directory!\n" );
60         return NULL;
61     }
62     if (dir->idCount == 1) return &dir->idEntries[0].icon;  /* No choice... */
63
64     /* First find the exact size with less colors */
65
66     maxcolors = 0;
67     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
68         if ((entry->bWidth == width) && (entry->bHeight == height) &&
69             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
70         {
71             bestEntry = entry;
72             maxcolors = entry->bColorCount;
73         }
74     if (bestEntry) return bestEntry;
75
76     /* First find the exact size with more colors */
77
78     maxcolors = 255;
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     /* Now find a smaller one with less colors */
89
90     maxcolors = maxwidth = maxheight = 0;
91     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
92         if ((entry->bWidth <= width) && (entry->bHeight <= height) &&
93             (entry->bWidth >= maxwidth) && (entry->bHeight >= maxheight) &&
94             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
95         {
96             bestEntry = entry;
97             maxwidth  = entry->bWidth;
98             maxheight = entry->bHeight;
99             maxcolors = entry->bColorCount;
100         }
101     if (bestEntry) return bestEntry;
102
103     /* Now find a smaller one with more colors */
104
105     maxcolors = 255;
106     maxwidth = maxheight = 0;
107     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
108         if ((entry->bWidth <= width) && (entry->bHeight <= height) &&
109             (entry->bWidth >= maxwidth) && (entry->bHeight >= maxheight) &&
110             (entry->bColorCount > colors) && (entry->bColorCount <= maxcolors))
111         {
112             bestEntry = entry;
113             maxwidth  = entry->bWidth;
114             maxheight = entry->bHeight;
115             maxcolors = entry->bColorCount;
116         }
117     if (bestEntry) return bestEntry;
118
119     /* Now find a larger one with less colors */
120
121     maxcolors = 0;
122     maxwidth = maxheight = 255;
123     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
124         if ((entry->bWidth <= maxwidth) && (entry->bHeight <= maxheight) &&
125             (entry->bColorCount <= colors) && (entry->bColorCount > maxcolors))
126         {
127             bestEntry = entry;
128             maxwidth  = entry->bWidth;
129             maxheight = entry->bHeight;
130             maxcolors = entry->bColorCount;
131         }
132     if (bestEntry) return bestEntry;
133
134     /* Now find a larger one with more colors */
135
136     maxcolors = maxwidth = maxheight = 255;
137     for (i = 0, entry = &dir->idEntries[0].icon; i < dir->idCount; i++,entry++)
138         if ((entry->bWidth <= maxwidth) && (entry->bHeight <= maxheight) &&
139             (entry->bColorCount > colors) && (entry->bColorCount <= maxcolors))
140         {
141             bestEntry = entry;
142             maxwidth  = entry->bWidth;
143             maxheight = entry->bHeight;
144             maxcolors = entry->bColorCount;
145         }
146
147     return bestEntry;
148 }
149
150
151 /**********************************************************************
152  *          CURSORICON_FindBestCursor
153  *
154  * Find the cursor closest to the requested size.
155  */
156 static CURSORDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
157                                                   int width, int height )
158 {
159     int i, maxwidth, maxheight;
160     CURSORDIRENTRY *entry, *bestEntry = NULL;
161
162     if (dir->idCount < 1)
163     {
164         fprintf( stderr, "Cursor: empty directory!\n" );
165         return NULL;
166     }
167     if (dir->idCount == 1) return &dir->idEntries[0].cursor; /* No choice... */
168
169     /* First find the largest one smaller than or equal to the requested size*/
170
171     maxwidth = maxheight = 0;
172     for(i = 0,entry = &dir->idEntries[0].cursor; i < dir->idCount; i++,entry++)
173         if ((entry->wWidth <= width) && (entry->wHeight <= height) &&
174             (entry->wWidth > maxwidth) && (entry->wHeight > maxheight))
175         {
176             bestEntry = entry;
177             maxwidth  = entry->wWidth;
178             maxheight = entry->wHeight;
179         }
180     if (bestEntry) return bestEntry;
181
182     /* Now find the smallest one larger than the requested size */
183
184     maxwidth = maxheight = 255;
185     for(i = 0,entry = &dir->idEntries[0].cursor; i < dir->idCount; i++,entry++)
186         if ((entry->wWidth < maxwidth) && (entry->wHeight < maxheight))
187         {
188             bestEntry = entry;
189             maxwidth  = entry->wWidth;
190             maxheight = entry->wHeight;
191         }
192
193     return bestEntry;
194 }
195
196
197 /**********************************************************************
198  *          CURSORICON_LoadDirEntry
199  *
200  * Load the icon/cursor directory for a given resource name and find the
201  * best matching entry.
202  */
203 static BOOL32 CURSORICON_LoadDirEntry( HINSTANCE32 hInstance, SEGPTR name,
204                                        INT32 width, INT32 height,
205                                        INT32 colors, BOOL32 fCursor,
206                                        CURSORICONDIRENTRY *dirEntry )
207 {
208     HRSRC16 hRsrc;
209     HGLOBAL16 hMem;
210     CURSORICONDIR *dir;
211     CURSORICONDIRENTRY *entry = NULL;
212
213     if (!(hRsrc = FindResource16( hInstance, name,
214                                 fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON )))
215         return FALSE;
216     if (!(hMem = LoadResource16( hInstance, hRsrc ))) return FALSE;
217     if ((dir = (CURSORICONDIR *)LockResource16( hMem )))
218     {
219         if (fCursor)
220             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
221                                                                width, height );
222         else
223             entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
224                                                        width, height, colors );
225         if (entry) *dirEntry = *entry;
226     }
227     FreeResource16( hMem );
228     return (entry != NULL);
229 }
230
231
232 /**********************************************************************
233  *          CURSORICON_LoadHandler 
234  *
235  * Create a cursor or icon from a resource.
236  */
237 HGLOBAL16 CURSORICON_LoadHandler( HGLOBAL16 handle, HINSTANCE16 hInstance,
238                                   BOOL32 fCursor )
239 {
240     HBITMAP32 hAndBits, hXorBits;
241     HDC32 hdc;
242     int size, sizeAnd, sizeXor;
243     POINT16 hotspot = { 0 ,0 };
244     BITMAPOBJ *bmpXor, *bmpAnd;
245     BITMAPINFO *bmi, *pInfo;
246     CURSORICONINFO *info;
247     char *bits;
248
249     if (fCursor)  /* If cursor, get the hotspot */
250     {
251         POINT16 *pt = (POINT16 *)LockResource16( handle );
252         hotspot = *pt;
253         bmi = (BITMAPINFO *)(pt + 1);
254     }
255     else bmi = (BITMAPINFO *)LockResource16( handle );
256
257     /* Create a copy of the bitmap header */
258
259     size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
260     /* Make sure we have room for the monochrome bitmap later on */
261     size = MAX( size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD) );
262     pInfo = (BITMAPINFO *)xmalloc( size );
263     memcpy( pInfo, bmi, size );
264
265     if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
266     {
267         if (pInfo->bmiHeader.biCompression != BI_RGB)
268         {
269             fprintf(stderr,"Unknown size for compressed icon bitmap.\n");
270             free( pInfo );
271             return 0;
272         }
273         pInfo->bmiHeader.biHeight /= 2;
274     }
275     else if (pInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
276     {
277         BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)pInfo;
278         core->bcHeight /= 2;
279     }
280     else
281     {
282         fprintf( stderr, "CURSORICON_Load: Unknown bitmap length %ld!\n",
283                  pInfo->bmiHeader.biSize );
284         free( pInfo );
285         return 0;
286     }
287
288     /* Create the XOR bitmap */
289
290     if (!(hdc = GetDC32( 0 )))
291     {
292         free( pInfo );
293         return 0;
294     }
295
296     hXorBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader, CBM_INIT,
297                                  (char*)bmi + size, pInfo, DIB_RGB_COLORS );
298     if (!hXorBits) {
299         free( pInfo );
300         ReleaseDC32( 0, hdc );
301         return 0;
302     }
303
304     /* Fix the bitmap header to load the monochrome mask */
305
306     if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
307     {
308         BITMAPINFOHEADER *bih = &pInfo->bmiHeader;
309         RGBQUAD *rgb = pInfo->bmiColors;
310         bits = (char *)bmi + size +
311             DIB_GetImageWidthBytes(bih->biWidth,bih->biBitCount)*bih->biHeight;
312         bih->biBitCount = 1;
313         bih->biClrUsed = bih->biClrImportant = 2;
314         rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
315         rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
316         rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
317     }
318     else
319     {
320         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER *)pInfo;
321         RGBTRIPLE *rgb = (RGBTRIPLE *)(bch + 1);
322         bits = (char *)bmi + size +
323             DIB_GetImageWidthBytes(bch->bcWidth,bch->bcBitCount)*bch->bcHeight;
324         bch->bcBitCount = 1;
325         rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
326         rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
327     }
328
329     /* Create the AND bitmap */
330
331     hAndBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader, CBM_INIT,
332                                  bits, pInfo, DIB_RGB_COLORS );
333     ReleaseDC32( 0, hdc );
334     if (!hAndBits) {
335         DeleteObject32( hXorBits );
336         free( pInfo );
337         return 0;
338     }
339
340     /* Now create the CURSORICONINFO structure */
341
342     bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( hXorBits, BITMAP_MAGIC );
343     bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( hAndBits, BITMAP_MAGIC );
344     sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
345     sizeAnd = bmpAnd->bitmap.bmHeight * bmpAnd->bitmap.bmWidthBytes;
346
347     if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
348                                   sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
349     {
350         DeleteObject32( hXorBits );
351         DeleteObject32( hAndBits );
352         return 0;
353     }
354
355     /* Make it owned by the module */
356     if (hInstance) FarSetOwner( handle, GetExePtr(hInstance) );
357
358     info = (CURSORICONINFO *)GlobalLock16( handle );
359     info->ptHotSpot.x   = hotspot.x;
360     info->ptHotSpot.y   = hotspot.y;
361     info->nWidth        = bmpXor->bitmap.bmWidth;
362     info->nHeight       = bmpXor->bitmap.bmHeight;
363     info->nWidthBytes   = bmpXor->bitmap.bmWidthBytes;
364     info->bPlanes       = bmpXor->bitmap.bmPlanes;
365     info->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
366
367     /* Transfer the bitmap bits to the CURSORICONINFO structure */
368
369     GetBitmapBits32( hAndBits, sizeAnd, (char *)(info + 1) );
370     GetBitmapBits32( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
371     DeleteObject32( hXorBits );
372     DeleteObject32( hAndBits );
373     GlobalUnlock16( handle );
374     return handle;
375 }
376
377 /**********************************************************************
378  *          CURSORICON_Load
379  *
380  * Load a cursor or icon.
381  */
382 static HGLOBAL16 CURSORICON_Load( HINSTANCE16 hInstance, SEGPTR name,
383                                   INT32 width, INT32 height, INT32 colors,
384                                   BOOL32 fCursor )
385 {
386     HGLOBAL16 handle, hRet;
387     HRSRC16 hRsrc;
388     CURSORICONDIRENTRY dirEntry;
389
390     if (!hInstance)  /* OEM cursor/icon */
391     {
392         if (HIWORD(name))  /* Check for '#xxx' name */
393         {
394             char *ptr = PTR_SEG_TO_LIN( name );
395             if (ptr[0] != '#') return 0;
396             if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
397         }
398         return OBM_LoadCursorIcon( LOWORD(name), fCursor );
399     }
400
401     /* Find the best entry in the directory */
402
403     if (!CURSORICON_LoadDirEntry( hInstance, name, width, height,
404                                   colors, fCursor, &dirEntry )) return 0;
405
406     /* Load the resource */
407
408     if (!(hRsrc = FindResource16( hInstance,
409                                 MAKEINTRESOURCE( dirEntry.icon.wResId ),
410                                 fCursor ? RT_CURSOR : RT_ICON ))) return 0;
411     if (!(handle = LoadResource16( hInstance, hRsrc ))) return 0;
412
413     hRet = CURSORICON_LoadHandler( handle, hInstance, fCursor );
414     FreeResource16(handle);
415     return hRet;
416 }
417
418
419 /***********************************************************************
420  *           CURSORICON_Copy
421  *
422  * Make a copy of a cursor or icon.
423  */
424 static HGLOBAL16 CURSORICON_Copy( HINSTANCE16 hInstance, HGLOBAL16 handle )
425 {
426     char *ptrOld, *ptrNew;
427     int size;
428     HGLOBAL16 hNew;
429
430     if (!(ptrOld = (char *)GlobalLock16( handle ))) return 0;
431     if (!(hInstance = GetExePtr( hInstance ))) return 0;
432     size = GlobalSize16( handle );
433     hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
434     FarSetOwner( hNew, hInstance );
435     ptrNew = (char *)GlobalLock16( hNew );
436     memcpy( ptrNew, ptrOld, size );
437     GlobalUnlock16( handle );
438     GlobalUnlock16( hNew );
439     return hNew;
440 }
441
442 /***********************************************************************
443  *           CURSORICON_IconToCursor
444  *
445  * Converts bitmap to mono and truncates if icon is too large
446  */
447 HCURSOR16 CURSORICON_IconToCursor(HICON16 hIcon, BOOL32 bSemiTransparent)
448 {
449  HCURSOR16       hRet = 0;
450  CURSORICONINFO *ptr = NULL;
451  HTASK16         hTask = GetCurrentTask();
452  TDB*            pTask = (TDB *)GlobalLock16(hTask);
453
454  if(hIcon)
455     if (!(ptr = (CURSORICONINFO*)GlobalLock16( hIcon ))) return FALSE;
456        if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
457            hRet = CURSORICON_Copy( pTask->hInstance, hIcon );
458        else
459           {
460            BYTE  pAndBits[128];
461            BYTE  pXorBits[128];
462            int   x, y, ix, iy, shift; 
463            int   bpp = (ptr->bBitsPerPixel>=24)?32:ptr->bBitsPerPixel; /* this sucks */
464            BYTE* psPtr = (BYTE *)(ptr + 1) +
465                             ptr->nHeight * BITMAP_WIDTH_BYTES(ptr->nWidth,1);
466            BYTE* pxbPtr = pXorBits;
467            unsigned *psc = NULL, val = 0;
468            unsigned val_base = 0xffffffff >> (32 - bpp);
469            BYTE* pbc = NULL;
470
471            COLORREF       col;
472            CURSORICONINFO cI;
473
474            if(!pTask) return 0;
475
476            memset(pXorBits, 0, 128);
477            cI.bBitsPerPixel = 1; cI.bPlanes = 1;
478            cI.ptHotSpot.x = cI.ptHotSpot.y = 15;
479            cI.nWidth = 32; cI.nHeight = 32;
480            cI.nWidthBytes = 4;  /* 1bpp */
481
482            x = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
483            y = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
484
485            for( iy = 0; iy < y; iy++ )
486            {
487               val = BITMAP_WIDTH_BYTES( ptr->nWidth, 1 );
488               memcpy( pAndBits + iy * 4,
489                      (BYTE *)(ptr + 1) + iy * val, (val>4) ? 4 : val);
490               shift = iy % 2;
491
492               for( ix = 0; ix < x; ix++ )
493               {
494                 if( bSemiTransparent && ((ix+shift)%2) )
495                 {
496                     pbc = pAndBits + iy * 4 + ix/8;
497                    *pbc |= 0x80 >> (ix%8);
498                 }
499                 else
500                 {
501                   psc = (unsigned*)(psPtr + (ix * bpp)/8);
502                   val = ((*psc) >> (ix * bpp)%8) & val_base;
503                   col = COLOR_ToLogical(val);
504                   if( GetRValue(col) > 0xa0 ||
505                       GetGValue(col) > 0x80 ||
506                       GetBValue(col) > 0xa0 )
507                   {
508                     pbc = pxbPtr + ix/8;
509                    *pbc |= 0x80 >> (ix%8);
510                   }
511                 }
512               }
513               psPtr += ptr->nWidthBytes;
514               pxbPtr += 4;
515            }
516            hRet = CreateCursorIconIndirect( pTask->hInstance , &cI, pAndBits, pXorBits);
517
518            if( !hRet ) /* fall back on default drag cursor */
519                 hRet = CURSORICON_Copy( pTask->hInstance ,
520                               CURSORICON_Load(0,MAKEINTRESOURCE(OCR_DRAGOBJECT),
521                                          SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE) );
522           }
523
524  return hRet;
525 }
526
527
528 /***********************************************************************
529  *           LoadCursor16    (USER.173)
530  */
531 HCURSOR16 LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
532 {
533     if (HIWORD(name))
534         dprintf_cursor( stddeb, "LoadCursor16: %04x '%s'\n",
535                         hInstance, (char *)PTR_SEG_TO_LIN( name ) );
536     else
537         dprintf_cursor( stddeb, "LoadCursor16: %04x %04x\n",
538                         hInstance, LOWORD(name) );
539
540     return CURSORICON_Load( hInstance, name,
541                             SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE);
542 }
543
544
545 /***********************************************************************
546  *           LoadIcon16    (USER.174)
547  */
548 HICON16 LoadIcon16( HINSTANCE16 hInstance, SEGPTR name )
549 {
550     if (HIWORD(name))
551         dprintf_icon( stddeb, "LoadIcon: %04x '%s'\n",
552                       hInstance, (char *)PTR_SEG_TO_LIN( name ) );
553     else
554         dprintf_icon( stddeb, "LoadIcon: %04x %04x\n",
555                       hInstance, LOWORD(name) );
556
557     return CURSORICON_Load( hInstance, name,
558                             SYSMETRICS_CXICON, SYSMETRICS_CYICON,
559                             MIN( 16, COLOR_GetSystemPaletteSize() ), FALSE );
560 }
561
562
563 /***********************************************************************
564  *           CreateCursor16    (USER.406)
565  */
566 HCURSOR16 CreateCursor16(HINSTANCE16 hInstance, INT16 xHotSpot, INT16 yHotSpot,
567                          INT16 nWidth, INT16 nHeight,
568                          LPCVOID lpANDbits, LPCVOID lpXORbits )
569 {
570     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
571
572     dprintf_cursor( stddeb, "CreateCursor: %dx%d spot=%d,%d xor=%p and=%p\n",
573                     nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
574     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
575 }
576
577
578 /***********************************************************************
579  *           CreateCursor32    (USER32.66)
580  */
581 HCURSOR32 CreateCursor32(HINSTANCE32 hInstance, INT32 xHotSpot, INT32 yHotSpot,
582                          INT32 nWidth, INT32 nHeight,
583                          LPCVOID lpANDbits, LPCVOID lpXORbits )
584 {
585     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
586
587     dprintf_cursor( stddeb, "CreateCursor: %dx%d spot=%d,%d xor=%p and=%p\n",
588                     nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
589     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
590 }
591
592
593 /***********************************************************************
594  *           CreateIcon16    (USER.407)
595  */
596 HICON16 CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth, INT16 nHeight,
597                       BYTE bPlanes, BYTE bBitsPixel,
598                       LPCVOID lpANDbits, LPCVOID lpXORbits )
599 {
600     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
601
602     dprintf_icon( stddeb, "CreateIcon: %dx%dx%d, xor=%p, and=%p\n",
603                   nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
604     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
605 }
606
607
608 /***********************************************************************
609  *           CreateIcon32    (USER32.74)
610  */
611 HICON32 CreateIcon32( HINSTANCE32 hInstance, INT32 nWidth, INT32 nHeight,
612                       BYTE bPlanes, BYTE bBitsPixel,
613                       LPCVOID lpANDbits, LPCVOID lpXORbits )
614 {
615     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
616
617     dprintf_icon( stddeb, "CreateIcon: %dx%dx%d, xor=%p, and=%p\n",
618                   nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
619     return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
620 }
621
622
623 /***********************************************************************
624  *           CreateCursorIconIndirect    (USER.408)
625  */
626 HGLOBAL16 CreateCursorIconIndirect(HINSTANCE16 hInstance, CURSORICONINFO *info,
627                                    LPCVOID lpANDbits, LPCVOID lpXORbits )
628 {
629     HGLOBAL16 handle;
630     char *ptr;
631     int sizeAnd, sizeXor;
632
633     hInstance = GetExePtr( hInstance );  /* Make it a module handle */
634     if (!hInstance || !lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
635     info->nWidthBytes = BITMAP_WIDTH_BYTES(info->nWidth,info->bBitsPerPixel);
636     sizeXor = info->nHeight * info->nWidthBytes;
637     sizeAnd = info->nHeight * BITMAP_WIDTH_BYTES( info->nWidth, 1 );
638     if (!(handle = DirectResAlloc(hInstance, 0x10,
639                                   sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
640         return 0;
641     ptr = (char *)GlobalLock16( handle );
642     memcpy( ptr, info, sizeof(*info) );
643     memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
644     memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
645     GlobalUnlock16( handle );
646     return handle;
647 }
648
649
650 /***********************************************************************
651  *           CopyIcon16    (USER.368)
652  */
653 HICON16 CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
654 {
655     dprintf_icon( stddeb, "CopyIcon16: %04x %04x\n", hInstance, hIcon );
656     return CURSORICON_Copy( hInstance, hIcon );
657 }
658
659
660 /***********************************************************************
661  *           CopyIcon32    (USER32.59)
662  */
663 HICON32 CopyIcon32( HICON32 hIcon )
664 {
665     dprintf_icon( stddeb, "CopyIcon32: %04x\n", hIcon );
666     return CURSORICON_Copy( 0, hIcon );
667 }
668
669
670 /***********************************************************************
671  *           CopyCursor16    (USER.369)
672  */
673 HCURSOR16 CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
674 {
675     dprintf_cursor( stddeb, "CopyCursor16: %04x %04x\n", hInstance, hCursor );
676     return CURSORICON_Copy( hInstance, hCursor );
677 }
678
679
680 /***********************************************************************
681  *           DestroyIcon16    (USER.457)
682  */
683 BOOL16 DestroyIcon16( HICON16 hIcon )
684 {
685     return DestroyIcon32( hIcon );
686 }
687
688
689 /***********************************************************************
690  *           DestroyIcon32    (USER32.132)
691  */
692 BOOL32 DestroyIcon32( HICON32 hIcon )
693 {
694     dprintf_icon( stddeb, "DestroyIcon: %04x\n", hIcon );
695     /* FIXME: should check for OEM icon here */
696     return (GlobalFree16( hIcon ) != 0);
697 }
698
699
700 /***********************************************************************
701  *           DestroyCursor16    (USER.458)
702  */
703 BOOL16 DestroyCursor16( HCURSOR16 hCursor )
704 {
705     return DestroyCursor32( hCursor );
706 }
707
708
709 /***********************************************************************
710  *           DestroyCursor32    (USER32.131)
711  */
712 BOOL32 DestroyCursor32( HCURSOR32 hCursor )
713 {
714     dprintf_cursor( stddeb, "DestroyCursor: %04x\n", hCursor );
715     /* FIXME: should check for OEM cursor here */
716     return (GlobalFree16( hCursor ) != 0);
717 }
718
719
720 /***********************************************************************
721  *           DrawIcon16    (USER.84)
722  */
723 BOOL16 DrawIcon16( HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon )
724 {
725     return DrawIcon32( hdc, x, y, hIcon );
726 }
727
728
729 /***********************************************************************
730  *           DrawIcon32    (USER32.158)
731  */
732 BOOL32 DrawIcon32( HDC32 hdc, INT32 x, INT32 y, HICON32 hIcon )
733 {
734     CURSORICONINFO *ptr;
735     HDC32 hMemDC;
736     HBITMAP32 hXorBits, hAndBits;
737     COLORREF oldFg, oldBg;
738
739     if (!(ptr = (CURSORICONINFO *)GlobalLock16( hIcon ))) return FALSE;
740     if (!(hMemDC = CreateCompatibleDC32( hdc ))) return FALSE;
741     hAndBits = CreateBitmap32( ptr->nWidth, ptr->nHeight, 1, 1,
742                                (char *)(ptr+1) );
743     hXorBits = CreateBitmap32( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
744                                ptr->bBitsPerPixel, (char *)(ptr + 1)
745                          + ptr->nHeight * BITMAP_WIDTH_BYTES(ptr->nWidth,1) );
746     oldFg = SetTextColor32( hdc, RGB(0,0,0) );
747     oldBg = SetBkColor32( hdc, RGB(255,255,255) );
748
749     if (hXorBits && hAndBits)
750     {
751         HBITMAP32 hBitTemp = SelectObject32( hMemDC, hAndBits );
752         BitBlt32( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
753         SelectObject32( hMemDC, hXorBits );
754         BitBlt32(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
755         SelectObject32( hMemDC, hBitTemp );
756     }
757     DeleteDC32( hMemDC );
758     if (hXorBits) DeleteObject32( hXorBits );
759     if (hAndBits) DeleteObject32( hAndBits );
760     GlobalUnlock16( hIcon );
761     SetTextColor32( hdc, oldFg );
762     SetBkColor32( hdc, oldBg );
763     return TRUE;
764 }
765
766
767 /***********************************************************************
768  *           DumpIcon    (USER.459)
769  */
770 DWORD DumpIcon( SEGPTR pInfo, WORD *lpLen,
771                 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
772 {
773     CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
774     int sizeAnd, sizeXor;
775
776     if (!info) return 0;
777     sizeXor = info->nHeight * info->nWidthBytes;
778     sizeAnd = info->nHeight * BITMAP_WIDTH_BYTES( info->nWidth, 1 );
779     if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
780     if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
781     if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
782     return MAKELONG( sizeXor, sizeXor );
783 }
784
785
786 /***********************************************************************
787  *           CURSORICON_SetCursor
788  *
789  * Change the X cursor. Helper function for SetCursor() and ShowCursor().
790  */
791 static BOOL32 CURSORICON_SetCursor( HCURSOR16 hCursor )
792 {
793     Pixmap pixmapBits, pixmapMask, pixmapAll;
794     XColor fg, bg;
795     Cursor cursor = None;
796
797     if (!hCursor)  /* Create an empty cursor */
798     {
799         static const char data[] = { 0 };
800
801         bg.red = bg.green = bg.blue = 0x0000;
802         pixmapBits = XCreateBitmapFromData( display, rootWindow, data, 1, 1 );
803         if (pixmapBits)
804         {
805             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
806                                           &bg, &bg, 0, 0 );
807             XFreePixmap( display, pixmapBits );
808         }
809     }
810     else  /* Create the X cursor from the bits */
811     {
812         CURSORICONINFO *ptr;
813         XImage *image;
814
815         if (!(ptr = (CURSORICONINFO*)GlobalLock16( hCursor ))) return FALSE;
816         if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
817         {
818             fprintf( stderr, "Cursor %04x has more than 1 bpp!\n", hCursor );
819             return FALSE;
820         }
821
822         /* Create a pixmap and transfer all the bits to it */
823
824         pixmapAll = XCreatePixmap( display, rootWindow,
825                                    ptr->nWidth, ptr->nHeight * 2, 1 );
826         image = XCreateImage( display, DefaultVisualOfScreen(screen),
827                               1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
828                               ptr->nHeight * 2, 16, ptr->nWidthBytes);
829         if (image)
830         {
831             extern void _XInitImageFuncPtrs( XImage* );
832             image->byte_order = MSBFirst;
833             image->bitmap_bit_order = MSBFirst;
834             image->bitmap_unit = 16;
835             _XInitImageFuncPtrs(image);
836             if (pixmapAll)
837                 CallTo32_LargeStack( XPutImage, 10,
838                                      display, pixmapAll, BITMAP_monoGC, image,
839                                      0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
840             image->data = NULL;
841             XDestroyImage( image );
842         }
843
844         /* Now create the 2 pixmaps for bits and mask */
845
846         pixmapBits = XCreatePixmap( display, rootWindow,
847                                     ptr->nWidth, ptr->nHeight, 1 );
848         pixmapMask = XCreatePixmap( display, rootWindow,
849                                     ptr->nWidth, ptr->nHeight, 1 );
850
851         /* Make sure everything went OK so far */
852
853         if (pixmapBits && pixmapMask && pixmapAll)
854         {
855             /* We have to do some magic here, as cursors are not fully
856              * compatible between Windows and X11. Under X11, there
857              * are only 3 possible color cursor: black, white and
858              * masked. So we map the 4th Windows color (invert the
859              * bits on the screen) to black. This require some boolean
860              * arithmetic:
861              *
862              *         Windows          |          X11
863              * Xor    And      Result   |   Bits     Mask     Result
864              *  0      0     black      |    0        1     background
865              *  0      1     no change  |    X        0     no change
866              *  1      0     white      |    1        1     foreground
867              *  1      1     inverted   |    0        1     background
868              *
869              * which gives:
870              *  Bits = 'Xor' and not 'And'
871              *  Mask = 'Xor' or not 'And'
872              *
873              * FIXME: apparently some servers do support 'inverted' color.
874              * I don't know if it's correct per the X spec, but maybe
875              * we ought to take advantage of it.  -- AJ
876              */
877             XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
878                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
879             XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
880                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
881             XSetFunction( display, BITMAP_monoGC, GXandReverse );
882             XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
883                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
884             XSetFunction( display, BITMAP_monoGC, GXorReverse );
885             XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
886                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
887             XSetFunction( display, BITMAP_monoGC, GXcopy );
888             fg.red = fg.green = fg.blue = 0xffff;
889             bg.red = bg.green = bg.blue = 0x0000;
890             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
891                                 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
892         }
893
894         /* Now free everything */
895
896         if (pixmapAll) XFreePixmap( display, pixmapAll );
897         if (pixmapBits) XFreePixmap( display, pixmapBits );
898         if (pixmapMask) XFreePixmap( display, pixmapMask );
899         GlobalUnlock16( hCursor );
900     }
901
902     if (cursor == None) return FALSE;
903     if (CURSORICON_XCursor != None) XFreeCursor( display, CURSORICON_XCursor );
904     CURSORICON_XCursor = cursor;
905
906     if (rootWindow != DefaultRootWindow(display))
907     {
908         /* Set the cursor on the desktop window */
909         XDefineCursor( display, rootWindow, cursor );
910     }
911     else
912     {
913         /* Set the same cursor for all top-level windows */
914         HWND32 hwnd = GetWindow32( GetDesktopWindow32(), GW_CHILD );
915         while(hwnd)
916         {
917             Window win = WIN_GetXWindow( hwnd );
918             if (win) XDefineCursor( display, win, cursor );
919             hwnd = GetWindow32( hwnd, GW_HWNDNEXT );
920         }
921     }
922     return TRUE;
923 }
924
925
926 /***********************************************************************
927  *           SetCursor16    (USER.69)
928  */
929 HCURSOR16 SetCursor16( HCURSOR16 hCursor )
930 {
931     return (HCURSOR16)SetCursor32( hCursor );
932 }
933
934
935 /***********************************************************************
936  *           SetCursor32    (USER32.471)
937  */
938 HCURSOR32 SetCursor32( HCURSOR32 hCursor )
939 {
940     HCURSOR32 hOldCursor;
941
942     if (hCursor == hActiveCursor) return hActiveCursor;  /* No change */
943     dprintf_cursor( stddeb, "SetCursor: %04x\n", hCursor );
944     hOldCursor = hActiveCursor;
945     hActiveCursor = hCursor;
946     /* Change the cursor shape only if it is visible */
947     if (CURSOR_ShowCount >= 0) CURSORICON_SetCursor( hActiveCursor );
948     return hOldCursor;
949 }
950
951
952 /***********************************************************************
953  *           SetCursorPos16    (USER.70)
954  */
955 void SetCursorPos16( INT16 x, INT16 y )
956 {
957     SetCursorPos32( x, y );
958 }
959
960
961 /***********************************************************************
962  *           SetCursorPos32    (USER32.473)
963  */
964 BOOL32 SetCursorPos32( INT32 x, INT32 y )
965 {
966     dprintf_cursor( stddeb, "SetCursorPos: x=%d y=%d\n", x, y );
967     XWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, x, y );
968     return TRUE;
969 }
970
971
972 /***********************************************************************
973  *           ShowCursor16    (USER.71)
974  */
975 INT16 ShowCursor16( BOOL16 bShow )
976 {
977     return ShowCursor32( bShow );
978 }
979
980
981 /***********************************************************************
982  *           ShowCursor32    (USER32.529)
983  */
984 INT32 ShowCursor32( BOOL32 bShow )
985 {
986     dprintf_cursor( stddeb, "ShowCursor: %d, count=%d\n",
987                     bShow, CURSOR_ShowCount );
988
989     if (bShow)
990     {
991         if (++CURSOR_ShowCount == 0)
992             CURSORICON_SetCursor( hActiveCursor );  /* Show it */
993     }
994     else
995     {
996         if (--CURSOR_ShowCount == -1)
997             CURSORICON_SetCursor( 0 );  /* Hide it */
998     }
999     return CURSOR_ShowCount;
1000 }
1001
1002
1003 /***********************************************************************
1004  *           GetCursor16    (USER.247)
1005  */
1006 HCURSOR16 GetCursor16(void)
1007 {
1008     return hActiveCursor;
1009 }
1010
1011
1012 /***********************************************************************
1013  *           GetCursor32    (USER32.226)
1014  */
1015 HCURSOR32 GetCursor32(void)
1016 {
1017     return hActiveCursor;
1018 }
1019
1020
1021 /***********************************************************************
1022  *           ClipCursor16    (USER.16)
1023  */
1024 BOOL16 ClipCursor16( const RECT16 *rect )
1025 {
1026     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
1027     else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1028     return TRUE;
1029 }
1030
1031
1032 /***********************************************************************
1033  *           ClipCursor32    (USER32.52)
1034  */
1035 BOOL32 ClipCursor32( const RECT32 *rect )
1036 {
1037     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
1038     else CopyRect32( &CURSOR_ClipRect, rect );
1039     return TRUE;
1040 }
1041
1042
1043 /***********************************************************************
1044  *           GetCursorPos16    (USER.17)
1045  */
1046 void GetCursorPos16( POINT16 *pt )
1047 {
1048     Window root, child;
1049     int rootX, rootY, childX, childY;
1050     unsigned int mousebut;
1051
1052     if (!pt) return;
1053     if (!XQueryPointer( display, rootWindow, &root, &child,
1054                         &rootX, &rootY, &childX, &childY, &mousebut ))
1055         pt->x = pt->y = 0;
1056     else
1057     {
1058         pt->x = childX;
1059         pt->y = childY;
1060     }
1061     dprintf_cursor(stddeb, "GetCursorPos: ret=%d,%d\n", pt->x, pt->y );
1062 }
1063
1064
1065 /***********************************************************************
1066  *           GetCursorPos32    (USER32.228)
1067  */
1068 void GetCursorPos32( POINT32 *pt )
1069 {
1070     POINT16 pt16;
1071     GetCursorPos16( &pt16 );
1072     if (pt) CONV_POINT16TO32( &pt16, pt );
1073 }
1074
1075
1076 /***********************************************************************
1077  *           GetClipCursor16    (USER.309)
1078  */
1079 void GetClipCursor16( RECT16 *rect )
1080 {
1081     if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1082 }
1083
1084
1085 /***********************************************************************
1086  *           GetClipCursor32    (USER32.220)
1087  */
1088 void GetClipCursor32( RECT32 *rect )
1089 {
1090     if (rect) CopyRect32( rect, &CURSOR_ClipRect );
1091 }
1092
1093
1094 /**********************************************************************
1095  *          GetIconID    (USER.455)
1096  */
1097 WORD GetIconID( HGLOBAL16 hResource, DWORD resType )
1098 {
1099     CURSORICONDIR *lpDir = (CURSORICONDIR *)GlobalLock16(hResource);
1100 /* LockResource16(hResource); */
1101
1102     if (!lpDir || lpDir->idReserved ||
1103         ((lpDir->idType != 1) && (lpDir->idType != 2)))
1104     {
1105         dprintf_cursor(stddeb,"GetIconID: invalid resource directory\n");
1106         return 0;
1107     }
1108
1109     dprintf_cursor( stddeb, "GetIconID: hRes=%04x, entries=%i\n",
1110                     hResource, lpDir->idCount );
1111
1112     switch(resType)
1113     {
1114     case 1:  /* cursor */
1115         {
1116             CURSORDIRENTRY *entry = CURSORICON_FindBestCursor( lpDir,
1117                                     SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR );
1118             return entry ? entry->wResId : 0;
1119         }
1120     case 3:  /* icon */
1121         {
1122             ICONDIRENTRY * entry =  CURSORICON_FindBestIcon( lpDir,
1123                                     SYSMETRICS_CXICON, SYSMETRICS_CYICON,
1124                                     MIN( 16, COLOR_GetSystemPaletteSize() ) );
1125             return entry ? entry->wResId : 0;
1126         }
1127     }
1128     fprintf( stderr, "GetIconID: invalid res type %ld\n", resType );
1129     return 0;
1130 }
1131
1132
1133 /**********************************************************************
1134  *          LoadIconHandler    (USER.456)
1135  */
1136 HICON16 LoadIconHandler( HGLOBAL16 hResource, BOOL16 bNew )
1137 {
1138     dprintf_cursor(stddeb,"LoadIconHandler: hRes=%04x\n",hResource);
1139
1140     if( !bNew )
1141       {
1142         fprintf(stdnimp,"LoadIconHandler: 2.xx resources are not supported\n");
1143         return 0;
1144       }
1145     return CURSORICON_LoadHandler( hResource, 0, FALSE);
1146 }