2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
10 * Cursors and icons are stored in a global heap block, with the
13 * CURSORICONINFO info;
17 * The bits structures are in the format of a device-dependent bitmap.
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 :-(
31 #include "cursoricon.h"
32 #include "sysmetrics.h"
39 extern UINT16 COLOR_GetSystemPaletteSize();
41 Cursor CURSORICON_XCursor = None; /* Current X cursor */
42 static HCURSOR16 hActiveCursor = 0; /* Active cursor */
43 static int CURSOR_ShowCount = 0; /* Cursor display count */
44 static RECT32 CURSOR_ClipRect; /* Cursor clipping rect */
46 /**********************************************************************
47 * CURSORICON_FindBestIcon
49 * Find the icon closest to the requested size and number of colors.
51 static ICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
52 int height, int colors )
54 int i, maxcolors, maxwidth, maxheight;
55 ICONDIRENTRY *entry, *bestEntry = NULL;
59 fprintf( stderr, "Icon: empty directory!\n" );
62 if (dir->idCount == 1) return &dir->idEntries[0].icon; /* No choice... */
64 /* First find the exact size with less colors */
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))
72 maxcolors = entry->bColorCount;
74 if (bestEntry) return bestEntry;
76 /* First find the exact size with more colors */
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))
84 maxcolors = entry->bColorCount;
86 if (bestEntry) return bestEntry;
88 /* Now find a smaller one with less colors */
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))
97 maxwidth = entry->bWidth;
98 maxheight = entry->bHeight;
99 maxcolors = entry->bColorCount;
101 if (bestEntry) return bestEntry;
103 /* Now find a smaller one with more colors */
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))
113 maxwidth = entry->bWidth;
114 maxheight = entry->bHeight;
115 maxcolors = entry->bColorCount;
117 if (bestEntry) return bestEntry;
119 /* Now find a larger one with less colors */
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))
128 maxwidth = entry->bWidth;
129 maxheight = entry->bHeight;
130 maxcolors = entry->bColorCount;
132 if (bestEntry) return bestEntry;
134 /* Now find a larger one with more colors */
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))
142 maxwidth = entry->bWidth;
143 maxheight = entry->bHeight;
144 maxcolors = entry->bColorCount;
151 /**********************************************************************
152 * CURSORICON_FindBestCursor
154 * Find the cursor closest to the requested size.
156 static CURSORDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
157 int width, int height )
159 int i, maxwidth, maxheight;
160 CURSORDIRENTRY *entry, *bestEntry = NULL;
162 if (dir->idCount < 1)
164 fprintf( stderr, "Cursor: empty directory!\n" );
167 if (dir->idCount == 1) return &dir->idEntries[0].cursor; /* No choice... */
169 /* First find the largest one smaller than or equal to the requested size*/
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))
177 maxwidth = entry->wWidth;
178 maxheight = entry->wHeight;
180 if (bestEntry) return bestEntry;
182 /* Now find the smallest one larger than the requested size */
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))
189 maxwidth = entry->wWidth;
190 maxheight = entry->wHeight;
197 /**********************************************************************
198 * CURSORICON_LoadDirEntry
200 * Load the icon/cursor directory for a given resource name and find the
201 * best matching entry.
203 static BOOL CURSORICON_LoadDirEntry(HINSTANCE32 hInstance, SEGPTR name,
204 int width, int height, int colors,
205 BOOL fCursor, CURSORICONDIRENTRY *dirEntry)
210 CURSORICONDIRENTRY *entry = NULL;
212 if (!(hRsrc = FindResource16( hInstance, name,
213 fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON )))
215 if (!(hMem = LoadResource16( hInstance, hRsrc ))) return FALSE;
216 if ((dir = (CURSORICONDIR *)LockResource16( hMem )))
219 entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
222 entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
223 width, height, colors );
224 if (entry) *dirEntry = *entry;
226 FreeResource16( hMem );
227 return (entry != NULL);
231 /**********************************************************************
232 * CURSORICON_LoadHandler
234 * Create a cursor or icon from a resource.
236 HGLOBAL16 CURSORICON_LoadHandler( HGLOBAL16 handle, HINSTANCE16 hInstance,
239 HBITMAP32 hAndBits, hXorBits;
241 int size, sizeAnd, sizeXor;
242 POINT16 hotspot = { 0 ,0 };
243 BITMAPOBJ *bmpXor, *bmpAnd;
244 BITMAPINFO *bmi, *pInfo;
245 CURSORICONINFO *info;
248 if (fCursor) /* If cursor, get the hotspot */
250 POINT16 *pt = (POINT16 *)LockResource16( handle );
252 bmi = (BITMAPINFO *)(pt + 1);
254 else bmi = (BITMAPINFO *)LockResource16( handle );
256 /* Create a copy of the bitmap header */
258 size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
259 /* Make sure we have room for the monochrome bitmap later on */
260 size = MAX( size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD) );
261 pInfo = (BITMAPINFO *)xmalloc( size );
262 memcpy( pInfo, bmi, size );
264 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
266 if (pInfo->bmiHeader.biCompression != BI_RGB)
268 fprintf(stderr,"Unknown size for compressed icon bitmap.\n");
272 pInfo->bmiHeader.biHeight /= 2;
274 else if (pInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
276 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)pInfo;
281 fprintf( stderr, "CURSORICON_Load: Unknown bitmap length %ld!\n",
282 pInfo->bmiHeader.biSize );
287 /* Create the XOR bitmap */
289 if (!(hdc = GetDC32( 0 )))
295 hXorBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader, CBM_INIT,
296 (char*)bmi + size, pInfo, DIB_RGB_COLORS );
298 /* Fix the bitmap header to load the monochrome mask */
300 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
302 BITMAPINFOHEADER *bih = &pInfo->bmiHeader;
303 RGBQUAD *rgb = pInfo->bmiColors;
304 bits = (char *)bmi + size +
305 DIB_GetImageWidthBytes(bih->biWidth,bih->biBitCount)*bih->biHeight;
307 bih->biClrUsed = bih->biClrImportant = 2;
308 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
309 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
310 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
314 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER *)pInfo;
315 RGBTRIPLE *rgb = (RGBTRIPLE *)(bch + 1);
316 bits = (char *)bmi + size +
317 DIB_GetImageWidthBytes(bch->bcWidth,bch->bcBitCount)*bch->bcHeight;
319 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
320 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
323 /* Create the AND bitmap */
325 hAndBits = CreateDIBitmap32( hdc, &pInfo->bmiHeader, CBM_INIT,
326 bits, pInfo, DIB_RGB_COLORS );
327 ReleaseDC32( 0, hdc );
329 /* Now create the CURSORICONINFO structure */
331 bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( hXorBits, BITMAP_MAGIC );
332 bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( hAndBits, BITMAP_MAGIC );
333 sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
334 sizeAnd = bmpAnd->bitmap.bmHeight * bmpAnd->bitmap.bmWidthBytes;
336 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
337 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
339 DeleteObject32( hXorBits );
340 DeleteObject32( hAndBits );
344 /* Make it owned by the module */
345 if (hInstance) FarSetOwner( handle, GetExePtr(hInstance) );
347 info = (CURSORICONINFO *)GlobalLock16( handle );
348 info->ptHotSpot.x = hotspot.x;
349 info->ptHotSpot.y = hotspot.y;
350 info->nWidth = bmpXor->bitmap.bmWidth;
351 info->nHeight = bmpXor->bitmap.bmHeight;
352 info->nWidthBytes = bmpXor->bitmap.bmWidthBytes;
353 info->bPlanes = bmpXor->bitmap.bmPlanes;
354 info->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
356 /* Transfer the bitmap bits to the CURSORICONINFO structure */
358 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
359 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
360 DeleteObject32( hXorBits );
361 DeleteObject32( hAndBits );
362 GlobalUnlock16( handle );
366 /**********************************************************************
369 * Load a cursor or icon.
371 static HGLOBAL16 CURSORICON_Load( HINSTANCE16 hInstance, SEGPTR name,
372 int width, int height, int colors,
375 HGLOBAL16 handle, hRet;
377 CURSORICONDIRENTRY dirEntry;
379 if (!hInstance) /* OEM cursor/icon */
381 if (HIWORD(name)) /* Check for '#xxx' name */
383 char *ptr = PTR_SEG_TO_LIN( name );
384 if (ptr[0] != '#') return 0;
385 if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
387 return OBM_LoadCursorIcon( LOWORD(name), fCursor );
390 /* Find the best entry in the directory */
392 if (!CURSORICON_LoadDirEntry( hInstance, name, width, height,
393 colors, fCursor, &dirEntry )) return 0;
395 /* Load the resource */
397 if (!(hRsrc = FindResource16( hInstance,
398 MAKEINTRESOURCE( dirEntry.icon.wResId ),
399 fCursor ? RT_CURSOR : RT_ICON ))) return 0;
400 if (!(handle = LoadResource16( hInstance, hRsrc ))) return 0;
402 hRet = CURSORICON_LoadHandler( handle, hInstance, fCursor );
403 FreeResource16(handle);
408 /***********************************************************************
411 * Make a copy of a cursor or icon.
413 static HGLOBAL16 CURSORICON_Copy( HINSTANCE16 hInstance, HGLOBAL16 handle )
415 char *ptrOld, *ptrNew;
419 if (!(ptrOld = (char *)GlobalLock16( handle ))) return 0;
420 if (!(hInstance = GetExePtr( hInstance ))) return 0;
421 size = GlobalSize16( handle );
422 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
423 FarSetOwner( hNew, hInstance );
424 ptrNew = (char *)GlobalLock16( hNew );
425 memcpy( ptrNew, ptrOld, size );
426 GlobalUnlock16( handle );
427 GlobalUnlock16( hNew );
431 /***********************************************************************
432 * CURSORICON_IconToCursor
434 * Converts bitmap to mono and truncates if icon is too large
436 HCURSOR16 CURSORICON_IconToCursor(HICON16 hIcon, BOOL32 bSemiTransparent)
439 CURSORICONINFO *ptr = NULL;
440 HTASK16 hTask = GetCurrentTask();
441 TDB* pTask = (TDB *)GlobalLock16(hTask);
444 if (!(ptr = (CURSORICONINFO*)GlobalLock16( hIcon ))) return FALSE;
445 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
446 hRet = CURSORICON_Copy( pTask->hInstance, hIcon );
451 int x, y, ix, iy, shift;
452 int bpp = (ptr->bBitsPerPixel>=24)?32:ptr->bBitsPerPixel; /* this sucks */
453 BYTE* psPtr = (BYTE *)(ptr + 1) +
454 ptr->nHeight * BITMAP_WIDTH_BYTES(ptr->nWidth,1);
455 BYTE* pxbPtr = pXorBits;
456 unsigned *psc = NULL, val = 0;
457 unsigned val_base = 0xffffffff >> (32 - bpp);
465 memset(pXorBits, 0, 128);
466 cI.bBitsPerPixel = 1; cI.bPlanes = 1;
467 cI.ptHotSpot.x = cI.ptHotSpot.y = 15;
468 cI.nWidth = 32; cI.nHeight = 32;
469 cI.nWidthBytes = 4; /* 1bpp */
471 x = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
472 y = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
474 for( iy = 0; iy < y; iy++ )
476 val = BITMAP_WIDTH_BYTES( ptr->nWidth, 1 );
477 memcpy( pAndBits + iy * 4,
478 (BYTE *)(ptr + 1) + iy * val, (val>4) ? 4 : val);
481 for( ix = 0; ix < x; ix++ )
483 if( bSemiTransparent && ((ix+shift)%2) )
485 pbc = pAndBits + iy * 4 + ix/8;
486 *pbc |= 0x80 >> (ix%8);
490 psc = (unsigned*)(psPtr + (ix * bpp)/8);
491 val = ((*psc) >> (ix * bpp)%8) & val_base;
492 col = COLOR_ToLogical(val);
493 if( GetRValue(col) > 0xa0 ||
494 GetGValue(col) > 0x80 ||
495 GetBValue(col) > 0xa0 )
498 *pbc |= 0x80 >> (ix%8);
502 psPtr += ptr->nWidthBytes;
505 hRet = CreateCursorIconIndirect( pTask->hInstance , &cI, pAndBits, pXorBits);
507 if( !hRet ) /* fall back on default drag cursor */
508 hRet = CURSORICON_Copy( pTask->hInstance ,
509 CURSORICON_Load(0,MAKEINTRESOURCE(OCR_DRAGOBJECT),
510 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE) );
516 /***********************************************************************
517 * LoadCursor (USER.173)
519 HCURSOR16 LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
522 dprintf_cursor( stddeb, "LoadCursor16: %04x '%s'\n",
523 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
525 dprintf_cursor( stddeb, "LoadCursor16: %04x %04x\n",
526 hInstance, LOWORD(name) );
528 return CURSORICON_Load( hInstance, name,
529 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE);
533 /***********************************************************************
534 * LoadIcon (USER.174)
536 HICON16 LoadIcon16(HINSTANCE16 hInstance,SEGPTR name)
539 dprintf_icon( stddeb, "LoadIcon: %04x '%s'\n",
540 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
542 dprintf_icon( stddeb, "LoadIcon: %04x %04x\n",
543 hInstance, LOWORD(name) );
545 return CURSORICON_Load( hInstance, name,
546 SYSMETRICS_CXICON, SYSMETRICS_CYICON,
547 MIN( 16, COLOR_GetSystemPaletteSize() ), FALSE );
551 /***********************************************************************
552 * CreateCursor (USER.406)
554 HCURSOR16 CreateCursor( HINSTANCE16 hInstance, INT xHotSpot, INT yHotSpot,
555 INT nWidth, INT nHeight,
556 const BYTE *lpANDbits, const BYTE *lpXORbits )
558 CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
560 dprintf_cursor( stddeb, "CreateCursor: %dx%d spot=%d,%d xor=%p and=%p\n",
561 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
562 return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
566 /***********************************************************************
567 * CreateIcon (USER.407)
569 HICON16 CreateIcon( HINSTANCE16 hInstance, INT nWidth, INT nHeight, BYTE bPlanes,
570 BYTE bBitsPixel, const BYTE* lpANDbits, const BYTE* lpXORbits)
572 CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel };
574 dprintf_icon( stddeb, "CreateIcon: %dx%dx%d, xor=%p, and=%p\n",
575 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
576 return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
580 /***********************************************************************
581 * CreateCursorIconIndirect (USER.408)
583 HGLOBAL16 CreateCursorIconIndirect( HINSTANCE16 hInstance,
584 CURSORICONINFO *info,
585 const BYTE *lpANDbits,
586 const BYTE *lpXORbits )
590 int sizeAnd, sizeXor;
592 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
593 if (!hInstance || !lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
594 info->nWidthBytes = BITMAP_WIDTH_BYTES(info->nWidth,info->bBitsPerPixel);
595 sizeXor = info->nHeight * info->nWidthBytes;
596 sizeAnd = info->nHeight * BITMAP_WIDTH_BYTES( info->nWidth, 1 );
597 if (!(handle = DirectResAlloc(hInstance, 0x10,
598 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
600 ptr = (char *)GlobalLock16( handle );
601 memcpy( ptr, info, sizeof(*info) );
602 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
603 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
604 GlobalUnlock16( handle );
609 /***********************************************************************
610 * CopyIcon16 (USER.368)
612 HICON16 CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
614 dprintf_icon( stddeb, "CopyIcon16: %04x %04x\n", hInstance, hIcon );
615 return CURSORICON_Copy( hInstance, hIcon );
619 /***********************************************************************
620 * CopyIcon32 (USER32.59)
622 HICON32 CopyIcon32( HICON32 hIcon )
624 dprintf_icon( stddeb, "CopyIcon32: %04x\n", hIcon );
625 return CURSORICON_Copy( 0, hIcon );
629 /***********************************************************************
630 * CopyCursor16 (USER.369)
632 HCURSOR16 CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
634 dprintf_cursor( stddeb, "CopyCursor16: %04x %04x\n", hInstance, hCursor );
635 return CURSORICON_Copy( hInstance, hCursor );
639 /***********************************************************************
640 * DestroyIcon (USER.457)
642 BOOL DestroyIcon( HICON16 hIcon )
644 dprintf_icon( stddeb, "DestroyIcon: %04x\n", hIcon );
645 /* FIXME: should check for OEM icon here */
646 return (GlobalFree16( hIcon ) != 0);
650 /***********************************************************************
651 * DestroyCursor (USER.458)
653 BOOL DestroyCursor( HCURSOR16 hCursor )
655 dprintf_cursor( stddeb, "DestroyCursor: %04x\n", hCursor );
656 /* FIXME: should check for OEM cursor here */
657 return (GlobalFree16( hCursor ) != 0);
661 /***********************************************************************
664 BOOL DrawIcon( HDC16 hdc, INT x, INT y, HICON16 hIcon )
668 HBITMAP16 hXorBits, hAndBits;
669 COLORREF oldFg, oldBg;
671 if (!(ptr = (CURSORICONINFO *)GlobalLock16( hIcon ))) return FALSE;
672 if (!(hMemDC = CreateCompatibleDC32( hdc ))) return FALSE;
673 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, (char *)(ptr+1));
674 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
675 ptr->bBitsPerPixel, (char *)(ptr + 1)
676 + ptr->nHeight * BITMAP_WIDTH_BYTES(ptr->nWidth,1) );
677 oldFg = SetTextColor( hdc, RGB(0,0,0) );
678 oldBg = SetBkColor( hdc, RGB(255,255,255) );
680 if (hXorBits && hAndBits)
682 HBITMAP32 hBitTemp = SelectObject32( hMemDC, hAndBits );
683 BitBlt32( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
684 SelectObject32( hMemDC, hXorBits );
685 BitBlt32(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
686 SelectObject32( hMemDC, hBitTemp );
688 DeleteDC32( hMemDC );
689 if (hXorBits) DeleteObject32( hXorBits );
690 if (hAndBits) DeleteObject32( hAndBits );
691 GlobalUnlock16( hIcon );
692 SetTextColor( hdc, oldFg );
693 SetBkColor( hdc, oldBg );
698 /***********************************************************************
699 * DumpIcon (USER.459)
701 DWORD DumpIcon( SEGPTR pInfo, WORD *lpLen,
702 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
704 CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
705 int sizeAnd, sizeXor;
708 sizeXor = info->nHeight * info->nWidthBytes;
709 sizeAnd = info->nHeight * BITMAP_WIDTH_BYTES( info->nWidth, 1 );
710 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
711 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
712 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
713 return MAKELONG( sizeXor, sizeXor );
717 /***********************************************************************
718 * CURSORICON_SetCursor
720 * Change the X cursor. Helper function for SetCursor() and ShowCursor().
722 static BOOL CURSORICON_SetCursor( HCURSOR16 hCursor )
724 Pixmap pixmapBits, pixmapMask, pixmapAll;
726 Cursor cursor = None;
728 if (!hCursor) /* Create an empty cursor */
730 static const char data[] = { 0 };
732 bg.red = bg.green = bg.blue = 0x0000;
733 pixmapBits = XCreateBitmapFromData( display, rootWindow, data, 1, 1 );
736 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
738 XFreePixmap( display, pixmapBits );
741 else /* Create the X cursor from the bits */
746 if (!(ptr = (CURSORICONINFO*)GlobalLock16( hCursor ))) return FALSE;
747 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
749 fprintf( stderr, "Cursor %04x has more than 1 bpp!\n", hCursor );
753 /* Create a pixmap and transfer all the bits to it */
755 pixmapAll = XCreatePixmap( display, rootWindow,
756 ptr->nWidth, ptr->nHeight * 2, 1 );
757 image = XCreateImage( display, DefaultVisualOfScreen(screen),
758 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
759 ptr->nHeight * 2, 16, ptr->nWidthBytes);
762 extern void _XInitImageFuncPtrs( XImage* );
763 image->byte_order = MSBFirst;
764 image->bitmap_bit_order = MSBFirst;
765 image->bitmap_unit = 16;
766 _XInitImageFuncPtrs(image);
768 CallTo32_LargeStack( XPutImage, 10,
769 display, pixmapAll, BITMAP_monoGC, image,
770 0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
772 XDestroyImage( image );
775 /* Now create the 2 pixmaps for bits and mask */
777 pixmapBits = XCreatePixmap( display, rootWindow,
778 ptr->nWidth, ptr->nHeight, 1 );
779 pixmapMask = XCreatePixmap( display, rootWindow,
780 ptr->nWidth, ptr->nHeight, 1 );
782 /* Make sure everything went OK so far */
784 if (pixmapBits && pixmapMask && pixmapAll)
786 /* We have to do some magic here, as cursors are not fully
787 * compatible between Windows and X11. Under X11, there
788 * are only 3 possible color cursor: black, white and
789 * masked. So we map the 4th Windows color (invert the
790 * bits on the screen) to black. This require some boolean
794 * Xor And Result | Bits Mask Result
795 * 0 0 black | 0 1 background
796 * 0 1 no change | X 0 no change
797 * 1 0 white | 1 1 foreground
798 * 1 1 inverted | 0 1 background
801 * Bits = 'Xor' and not 'And'
802 * Mask = 'Xor' or not 'And'
804 * FIXME: apparently some servers do support 'inverted' color.
805 * I don't know if it's correct per the X spec, but maybe
806 * we ought to take advantage of it. -- AJ
808 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
809 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
810 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
811 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
812 XSetFunction( display, BITMAP_monoGC, GXandReverse );
813 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
814 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
815 XSetFunction( display, BITMAP_monoGC, GXorReverse );
816 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
817 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
818 XSetFunction( display, BITMAP_monoGC, GXcopy );
819 fg.red = fg.green = fg.blue = 0xffff;
820 bg.red = bg.green = bg.blue = 0x0000;
821 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
822 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
825 /* Now free everything */
827 if (pixmapAll) XFreePixmap( display, pixmapAll );
828 if (pixmapBits) XFreePixmap( display, pixmapBits );
829 if (pixmapMask) XFreePixmap( display, pixmapMask );
830 GlobalUnlock16( hCursor );
833 if (cursor == None) return FALSE;
834 if (CURSORICON_XCursor != None) XFreeCursor( display, CURSORICON_XCursor );
835 CURSORICON_XCursor = cursor;
837 if (rootWindow != DefaultRootWindow(display))
839 /* Set the cursor on the desktop window */
840 XDefineCursor( display, rootWindow, cursor );
844 /* Set the same cursor for all top-level windows */
845 HWND hwnd = GetWindow( GetDesktopWindow32(), GW_CHILD );
848 Window win = WIN_GetXWindow( hwnd );
849 if (win) XDefineCursor( display, win, cursor );
850 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
857 /***********************************************************************
858 * SetCursor (USER.69)
860 HCURSOR16 SetCursor( HCURSOR16 hCursor )
862 HCURSOR16 hOldCursor;
864 if (hCursor == hActiveCursor) return hActiveCursor; /* No change */
865 dprintf_cursor( stddeb, "SetCursor: %04x\n", hCursor );
866 hOldCursor = hActiveCursor;
867 hActiveCursor = hCursor;
868 /* Change the cursor shape only if it is visible */
869 if (CURSOR_ShowCount >= 0) CURSORICON_SetCursor( hActiveCursor );
874 /***********************************************************************
875 * SetCursorPos (USER.70)
877 void SetCursorPos( short x, short y )
879 dprintf_cursor( stddeb, "SetCursorPos: x=%d y=%d\n", x, y );
880 XWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, x, y );
884 /***********************************************************************
885 * ShowCursor (USER.71)
887 int ShowCursor( BOOL bShow )
889 dprintf_cursor( stddeb, "ShowCursor: %d, count=%d\n",
890 bShow, CURSOR_ShowCount );
894 if (++CURSOR_ShowCount == 0)
895 CURSORICON_SetCursor( hActiveCursor ); /* Show it */
899 if (--CURSOR_ShowCount == -1)
900 CURSORICON_SetCursor( 0 ); /* Hide it */
902 return CURSOR_ShowCount;
906 /***********************************************************************
907 * GetCursor (USER.247)
909 HCURSOR16 GetCursor(void)
911 return hActiveCursor;
915 /***********************************************************************
916 * ClipCursor16 (USER.16)
918 BOOL16 ClipCursor16( const RECT16 *rect )
920 if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
921 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
926 /***********************************************************************
927 * ClipCursor32 (USER32.52)
929 BOOL32 ClipCursor32( const RECT32 *rect )
931 if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
932 else CopyRect32( &CURSOR_ClipRect, rect );
937 /***********************************************************************
938 * GetCursorPos16 (USER.17)
940 void GetCursorPos16( POINT16 *pt )
943 int rootX, rootY, childX, childY;
944 unsigned int mousebut;
947 if (!XQueryPointer( display, rootWindow, &root, &child,
948 &rootX, &rootY, &childX, &childY, &mousebut ))
955 dprintf_cursor(stddeb, "GetCursorPos: ret=%d,%d\n", pt->x, pt->y );
959 /***********************************************************************
960 * GetCursorPos32 (USER32.228)
962 void GetCursorPos32( POINT32 *pt )
965 GetCursorPos16( &pt16 );
966 if (pt) CONV_POINT16TO32( &pt16, pt );
970 /***********************************************************************
971 * GetClipCursor16 (USER.309)
973 void GetClipCursor16( RECT16 *rect )
975 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
979 /***********************************************************************
980 * GetClipCursor32 (USER32.220)
982 void GetClipCursor32( RECT32 *rect )
984 if (rect) CopyRect32( rect, &CURSOR_ClipRect );
988 /**********************************************************************
989 * GetIconID (USER.455)
991 WORD GetIconID( HGLOBAL16 hResource, DWORD resType )
993 CURSORICONDIR *lpDir = (CURSORICONDIR *)GlobalLock16(hResource);
994 /* LockResource16(hResource); */
996 if (!lpDir || lpDir->idReserved ||
997 ((lpDir->idType != 1) && (lpDir->idType != 2)))
999 dprintf_cursor(stddeb,"GetIconID: invalid resource directory\n");
1003 dprintf_cursor( stddeb, "GetIconID: hRes=%04x, entries=%i\n",
1004 hResource, lpDir->idCount );
1008 case 1: /* cursor */
1010 CURSORDIRENTRY *entry = CURSORICON_FindBestCursor( lpDir,
1011 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR );
1012 return entry ? entry->wResId : 0;
1016 ICONDIRENTRY * entry = CURSORICON_FindBestIcon( lpDir,
1017 SYSMETRICS_CXICON, SYSMETRICS_CYICON,
1018 MIN( 16, COLOR_GetSystemPaletteSize() ) );
1019 return entry ? entry->wResId : 0;
1022 fprintf( stderr, "GetIconID: invalid res type %ld\n", resType );
1027 /**********************************************************************
1028 * LoadIconHandler (USER.456)
1030 HICON16 LoadIconHandler( HGLOBAL16 hResource, BOOL bNew )
1032 dprintf_cursor(stddeb,"LoadIconHandler: hRes=%04x\n",hResource);
1036 fprintf(stdnimp,"LoadIconHandler: 2.xx resources are not supported\n");
1039 return CURSORICON_LoadHandler( hResource, 0, FALSE);