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 :-(
30 #include "cursoricon.h"
31 #include "sysmetrics.h"
39 Cursor CURSORICON_XCursor
= None
; /* Current X cursor */
40 static HCURSOR hActiveCursor
= 0; /* Active cursor */
41 static int CURSOR_ShowCount
= 0; /* Cursor display count */
42 static RECT32 CURSOR_ClipRect
; /* Cursor clipping rect */
44 /**********************************************************************
45 * CURSORICON_FindBestIcon
47 * Find the icon closest to the requested size and number of colors.
49 static ICONDIRENTRY
*CURSORICON_FindBestIcon( CURSORICONDIR
*dir
, int width
,
50 int height
, int colors
)
52 int i
, maxcolors
, maxwidth
, maxheight
;
53 ICONDIRENTRY
*entry
, *bestEntry
= NULL
;
57 fprintf( stderr
, "Icon: empty directory!\n" );
60 if (dir
->idCount
== 1) return &dir
->idEntries
[0].icon
; /* No choice... */
62 /* First find the exact size with less colors */
65 for (i
= 0, entry
= &dir
->idEntries
[0].icon
; i
< dir
->idCount
; i
++,entry
++)
66 if ((entry
->bWidth
== width
) && (entry
->bHeight
== height
) &&
67 (entry
->bColorCount
<= colors
) && (entry
->bColorCount
> maxcolors
))
70 maxcolors
= entry
->bColorCount
;
72 if (bestEntry
) return bestEntry
;
74 /* First find the exact size with more colors */
77 for (i
= 0, entry
= &dir
->idEntries
[0].icon
; i
< dir
->idCount
; i
++,entry
++)
78 if ((entry
->bWidth
== width
) && (entry
->bHeight
== height
) &&
79 (entry
->bColorCount
> colors
) && (entry
->bColorCount
<= maxcolors
))
82 maxcolors
= entry
->bColorCount
;
84 if (bestEntry
) return bestEntry
;
86 /* Now find a smaller one with less colors */
88 maxcolors
= maxwidth
= maxheight
= 0;
89 for (i
= 0, entry
= &dir
->idEntries
[0].icon
; i
< dir
->idCount
; i
++,entry
++)
90 if ((entry
->bWidth
<= width
) && (entry
->bHeight
<= height
) &&
91 (entry
->bWidth
>= maxwidth
) && (entry
->bHeight
>= maxheight
) &&
92 (entry
->bColorCount
<= colors
) && (entry
->bColorCount
> maxcolors
))
95 maxwidth
= entry
->bWidth
;
96 maxheight
= entry
->bHeight
;
97 maxcolors
= entry
->bColorCount
;
99 if (bestEntry
) return bestEntry
;
101 /* Now find a smaller one with more colors */
104 maxwidth
= maxheight
= 0;
105 for (i
= 0, entry
= &dir
->idEntries
[0].icon
; i
< dir
->idCount
; i
++,entry
++)
106 if ((entry
->bWidth
<= width
) && (entry
->bHeight
<= height
) &&
107 (entry
->bWidth
>= maxwidth
) && (entry
->bHeight
>= maxheight
) &&
108 (entry
->bColorCount
> colors
) && (entry
->bColorCount
<= maxcolors
))
111 maxwidth
= entry
->bWidth
;
112 maxheight
= entry
->bHeight
;
113 maxcolors
= entry
->bColorCount
;
115 if (bestEntry
) return bestEntry
;
117 /* Now find a larger one with less colors */
120 maxwidth
= maxheight
= 255;
121 for (i
= 0, entry
= &dir
->idEntries
[0].icon
; i
< dir
->idCount
; i
++,entry
++)
122 if ((entry
->bWidth
<= maxwidth
) && (entry
->bHeight
<= maxheight
) &&
123 (entry
->bColorCount
<= colors
) && (entry
->bColorCount
> maxcolors
))
126 maxwidth
= entry
->bWidth
;
127 maxheight
= entry
->bHeight
;
128 maxcolors
= entry
->bColorCount
;
130 if (bestEntry
) return bestEntry
;
132 /* Now find a larger one with more colors */
134 maxcolors
= 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
))
140 maxwidth
= entry
->bWidth
;
141 maxheight
= entry
->bHeight
;
142 maxcolors
= entry
->bColorCount
;
149 /**********************************************************************
150 * CURSORICON_FindBestCursor
152 * Find the cursor closest to the requested size.
154 static CURSORDIRENTRY
*CURSORICON_FindBestCursor( CURSORICONDIR
*dir
,
155 int width
, int height
)
157 int i
, maxwidth
, maxheight
;
158 CURSORDIRENTRY
*entry
, *bestEntry
= NULL
;
160 if (dir
->idCount
< 1)
162 fprintf( stderr
, "Cursor: empty directory!\n" );
165 if (dir
->idCount
== 1) return &dir
->idEntries
[0].cursor
; /* No choice... */
167 /* First find the largest one smaller than or equal to the requested size*/
169 maxwidth
= maxheight
= 0;
170 for(i
= 0,entry
= &dir
->idEntries
[0].cursor
; i
< dir
->idCount
; i
++,entry
++)
171 if ((entry
->wWidth
<= width
) && (entry
->wHeight
<= height
) &&
172 (entry
->wWidth
> maxwidth
) && (entry
->wHeight
> maxheight
))
175 maxwidth
= entry
->wWidth
;
176 maxheight
= entry
->wHeight
;
178 if (bestEntry
) return bestEntry
;
180 /* Now find the smallest one larger than the requested size */
182 maxwidth
= maxheight
= 255;
183 for(i
= 0,entry
= &dir
->idEntries
[0].cursor
; i
< dir
->idCount
; i
++,entry
++)
184 if ((entry
->wWidth
< maxwidth
) && (entry
->wHeight
< maxheight
))
187 maxwidth
= entry
->wWidth
;
188 maxheight
= entry
->wHeight
;
195 /**********************************************************************
196 * CURSORICON_LoadDirEntry
198 * Load the icon/cursor directory for a given resource name and find the
199 * best matching entry.
201 static BOOL
CURSORICON_LoadDirEntry(HANDLE hInstance
, SEGPTR name
,
202 int width
, int height
, int colors
,
203 BOOL fCursor
, CURSORICONDIRENTRY
*dirEntry
)
208 CURSORICONDIRENTRY
*entry
= NULL
;
210 if (!(hRsrc
= FindResource( hInstance
, name
,
211 fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
)))
213 if (!(hMem
= LoadResource( hInstance
, hRsrc
))) return FALSE
;
214 if ((dir
= (CURSORICONDIR
*)LockResource( hMem
)))
217 entry
= (CURSORICONDIRENTRY
*)CURSORICON_FindBestCursor( dir
,
220 entry
= (CURSORICONDIRENTRY
*)CURSORICON_FindBestIcon( dir
,
221 width
, height
, colors
);
222 if (entry
) *dirEntry
= *entry
;
224 FreeResource( hMem
);
225 return (entry
!= NULL
);
229 /**********************************************************************
230 * CURSORICON_LoadHandler
232 * Create a cursor or icon from a resource.
234 HANDLE
CURSORICON_LoadHandler( HANDLE handle
, HINSTANCE hInstance
,
237 HANDLE hAndBits
, hXorBits
;
239 int size
, sizeAnd
, sizeXor
;
240 POINT16 hotspot
= { 0 ,0 };
241 BITMAPOBJ
*bmpXor
, *bmpAnd
;
242 BITMAPINFO
*bmi
, *pInfo
;
243 CURSORICONINFO
*info
;
246 if (fCursor
) /* If cursor, get the hotspot */
248 POINT16
*pt
= (POINT16
*)LockResource( handle
);
250 bmi
= (BITMAPINFO
*)(pt
+ 1);
252 else bmi
= (BITMAPINFO
*)LockResource( handle
);
254 /* Create a copy of the bitmap header */
256 size
= DIB_BitmapInfoSize( bmi
, DIB_RGB_COLORS
);
257 /* Make sure we have room for the monochrome bitmap later on */
258 size
= MAX( size
, sizeof(BITMAPINFOHEADER
) + 2*sizeof(RGBQUAD
) );
259 pInfo
= (BITMAPINFO
*)xmalloc( size
);
260 memcpy( pInfo
, bmi
, size
);
262 if (pInfo
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
264 if (pInfo
->bmiHeader
.biCompression
!= BI_RGB
)
266 fprintf(stderr
,"Unknown size for compressed icon bitmap.\n");
270 pInfo
->bmiHeader
.biHeight
/= 2;
272 else if (pInfo
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
274 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)pInfo
;
279 fprintf( stderr
, "CURSORICON_Load: Unknown bitmap length %ld!\n",
280 pInfo
->bmiHeader
.biSize
);
285 /* Create the XOR bitmap */
287 if (!(hdc
= GetDC( 0 )))
293 hXorBits
= CreateDIBitmap( hdc
, &pInfo
->bmiHeader
, CBM_INIT
,
294 (char*)bmi
+ size
, pInfo
, DIB_RGB_COLORS
);
296 /* Fix the bitmap header to load the monochrome mask */
298 if (pInfo
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
300 BITMAPINFOHEADER
*bih
= &pInfo
->bmiHeader
;
301 RGBQUAD
*rgb
= pInfo
->bmiColors
;
302 bits
= (char *)bmi
+ size
+
303 DIB_GetImageWidthBytes(bih
->biWidth
,bih
->biBitCount
)*bih
->biHeight
;
305 bih
->biClrUsed
= bih
->biClrImportant
= 2;
306 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
307 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
308 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
312 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)pInfo
;
313 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(bch
+ 1);
314 bits
= (char *)bmi
+ size
+
315 DIB_GetImageWidthBytes(bch
->bcWidth
,bch
->bcBitCount
)*bch
->bcHeight
;
317 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
318 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
321 /* Create the AND bitmap */
323 hAndBits
= CreateDIBitmap( hdc
, &pInfo
->bmiHeader
, CBM_INIT
,
324 bits
, pInfo
, DIB_RGB_COLORS
);
327 /* Now create the CURSORICONINFO structure */
329 bmpXor
= (BITMAPOBJ
*) GDI_GetObjPtr( hXorBits
, BITMAP_MAGIC
);
330 bmpAnd
= (BITMAPOBJ
*) GDI_GetObjPtr( hAndBits
, BITMAP_MAGIC
);
331 sizeXor
= bmpXor
->bitmap
.bmHeight
* bmpXor
->bitmap
.bmWidthBytes
;
332 sizeAnd
= bmpAnd
->bitmap
.bmHeight
* bmpAnd
->bitmap
.bmWidthBytes
;
334 if (!(handle
= GlobalAlloc16( GMEM_MOVEABLE
,
335 sizeof(CURSORICONINFO
) + sizeXor
+ sizeAnd
)))
337 DeleteObject( hXorBits
);
338 DeleteObject( hAndBits
);
342 /* Make it owned by the module */
343 if (hInstance
) FarSetOwner( handle
, GetExePtr(hInstance
) );
345 info
= (CURSORICONINFO
*)GlobalLock16( handle
);
346 info
->ptHotSpot
.x
= hotspot
.x
;
347 info
->ptHotSpot
.y
= hotspot
.y
;
348 info
->nWidth
= bmpXor
->bitmap
.bmWidth
;
349 info
->nHeight
= bmpXor
->bitmap
.bmHeight
;
350 info
->nWidthBytes
= bmpXor
->bitmap
.bmWidthBytes
;
351 info
->bPlanes
= bmpXor
->bitmap
.bmPlanes
;
352 info
->bBitsPerPixel
= bmpXor
->bitmap
.bmBitsPixel
;
354 /* Transfer the bitmap bits to the CURSORICONINFO structure */
356 GetBitmapBits( hAndBits
, sizeAnd
, (char *)(info
+ 1) );
357 GetBitmapBits( hXorBits
, sizeXor
, (char *)(info
+ 1) + sizeAnd
);
358 DeleteObject( hXorBits
);
359 DeleteObject( hAndBits
);
360 GlobalUnlock16( handle
);
364 /**********************************************************************
367 * Load a cursor or icon.
369 static HANDLE
CURSORICON_Load( HANDLE hInstance
, SEGPTR name
, int width
,
370 int height
, int colors
, BOOL fCursor
)
374 CURSORICONDIRENTRY dirEntry
;
376 if (!hInstance
) /* OEM cursor/icon */
378 if (HIWORD(name
)) /* Check for '#xxx' name */
380 char *ptr
= PTR_SEG_TO_LIN( name
);
381 if (ptr
[0] != '#') return 0;
382 if (!(name
= (SEGPTR
)atoi( ptr
+ 1 ))) return 0;
384 return OBM_LoadCursorIcon( LOWORD(name
), fCursor
);
387 /* Find the best entry in the directory */
389 if (!CURSORICON_LoadDirEntry( hInstance
, name
, width
, height
,
390 colors
, fCursor
, &dirEntry
)) return 0;
392 /* Load the resource */
394 if (!(hRsrc
= FindResource( hInstance
,
395 MAKEINTRESOURCE( dirEntry
.icon
.wResId
),
396 fCursor
? RT_CURSOR
: RT_ICON
))) return 0;
397 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
399 hRet
= CURSORICON_LoadHandler( handle
, hInstance
, fCursor
);
400 FreeResource(handle
);
405 /***********************************************************************
408 * Make a copy of a cursor or icon.
410 static HANDLE
CURSORICON_Copy( HANDLE hInstance
, HANDLE handle
)
412 char *ptrOld
, *ptrNew
;
416 if (!(ptrOld
= (char *)GlobalLock16( handle
))) return 0;
417 if (!(hInstance
= GetExePtr( hInstance
))) return 0;
418 size
= GlobalSize16( handle
);
419 hNew
= GlobalAlloc16( GMEM_MOVEABLE
, size
);
420 FarSetOwner( hNew
, hInstance
);
421 ptrNew
= (char *)GlobalLock16( hNew
);
422 memcpy( ptrNew
, ptrOld
, size
);
423 GlobalUnlock16( handle
);
424 GlobalUnlock16( hNew
);
428 /***********************************************************************
429 * CURSORICON_IconToCursor
431 * Should convert bitmap to mono and truncate if too large
432 * FIXME: if icon is passed returns a copy of OCR_DRAGOBJECT cursor
433 * but should actually convert icon to cursor.
435 HCURSOR
CURSORICON_IconToCursor(HICON hIcon
)
437 CURSORICONINFO
*ptr
= NULL
;
440 if (!(ptr
= (CURSORICONINFO
*)GlobalLock16( hIcon
))) return FALSE
;
441 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
== 1)
443 return hIcon
; /* assuming it's a cursor */
449 HTASK hTask
= GetCurrentTask();
450 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
454 fprintf( stdnimp
, "IconToCursor: Icons are not supported, returning default!\n");
455 return CURSORICON_Copy( pTask
->hInstance
,
456 CURSORICON_Load(0,MAKEINTRESOURCE(OCR_DRAGOBJECT
),
457 SYSMETRICS_CXCURSOR
, SYSMETRICS_CYCURSOR
, 1, TRUE
) );
463 /***********************************************************************
464 * LoadCursor (USER.173)
466 HCURSOR
LoadCursor( HANDLE hInstance
, SEGPTR name
)
469 dprintf_cursor( stddeb
, "LoadCursor: %04x '%s'\n",
470 hInstance
, (char *)PTR_SEG_TO_LIN( name
) );
472 dprintf_cursor( stddeb
, "LoadCursor: %04x %04x\n",
473 hInstance
, LOWORD(name
) );
475 return CURSORICON_Load( hInstance
, name
,
476 SYSMETRICS_CXCURSOR
, SYSMETRICS_CYCURSOR
, 1, TRUE
);
480 /***********************************************************************
481 * LoadIcon (USER.174)
483 HICON
LoadIcon( HANDLE hInstance
, SEGPTR name
)
486 dprintf_icon( stddeb
, "LoadIcon: %04x '%s'\n",
487 hInstance
, (char *)PTR_SEG_TO_LIN( name
) );
489 dprintf_icon( stddeb
, "LoadIcon: %04x %04x\n",
490 hInstance
, LOWORD(name
) );
492 return CURSORICON_Load( hInstance
, name
,
493 SYSMETRICS_CXICON
, SYSMETRICS_CYICON
,
494 MIN( 16, 1 << screenDepth
), FALSE
);
498 /***********************************************************************
499 * CreateCursor (USER.406)
501 HCURSOR
CreateCursor( HINSTANCE hInstance
, INT xHotSpot
, INT yHotSpot
,
502 INT nWidth
, INT nHeight
,
503 const BYTE
*lpANDbits
, const BYTE
*lpXORbits
)
505 CURSORICONINFO info
= { { xHotSpot
, yHotSpot
}, nWidth
, nHeight
, 0, 1, 1 };
507 dprintf_cursor( stddeb
, "CreateCursor: %dx%d spot=%d,%d xor=%p and=%p\n",
508 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
509 return CreateCursorIconIndirect( hInstance
, &info
, lpANDbits
, lpXORbits
);
513 /***********************************************************************
514 * CreateIcon (USER.407)
516 HICON
CreateIcon( HINSTANCE hInstance
, INT nWidth
, INT nHeight
, BYTE bPlanes
,
517 BYTE bBitsPixel
, const BYTE
* lpANDbits
, const BYTE
* lpXORbits
)
519 CURSORICONINFO info
= { { 0, 0 }, nWidth
, nHeight
, 0, bPlanes
, bBitsPixel
};
521 dprintf_icon( stddeb
, "CreateIcon: %dx%dx%d, xor=%p, and=%p\n",
522 nWidth
, nHeight
, bPlanes
* bBitsPixel
, lpXORbits
, lpANDbits
);
523 return CreateCursorIconIndirect( hInstance
, &info
, lpANDbits
, lpXORbits
);
527 /***********************************************************************
528 * CreateCursorIconIndirect (USER.408)
530 HANDLE
CreateCursorIconIndirect( HANDLE hInstance
, CURSORICONINFO
*info
,
531 const BYTE
*lpANDbits
, const BYTE
*lpXORbits
)
535 int sizeAnd
, sizeXor
;
537 hInstance
= GetExePtr( hInstance
); /* Make it a module handle */
538 if (!hInstance
|| !lpXORbits
|| !lpANDbits
|| info
->bPlanes
!= 1) return 0;
539 info
->nWidthBytes
= BITMAP_WIDTH_BYTES(info
->nWidth
,info
->bBitsPerPixel
);
540 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
541 sizeAnd
= info
->nHeight
* BITMAP_WIDTH_BYTES( info
->nWidth
, 1 );
542 if (!(handle
= DirectResAlloc(hInstance
, 0x10,
543 sizeof(CURSORICONINFO
) + sizeXor
+ sizeAnd
)))
545 ptr
= (char *)GlobalLock16( handle
);
546 memcpy( ptr
, info
, sizeof(*info
) );
547 memcpy( ptr
+ sizeof(CURSORICONINFO
), lpANDbits
, sizeAnd
);
548 memcpy( ptr
+ sizeof(CURSORICONINFO
) + sizeAnd
, lpXORbits
, sizeXor
);
549 GlobalUnlock16( handle
);
554 /***********************************************************************
555 * CopyIcon16 (USER.368)
557 HICON16
CopyIcon16( HINSTANCE16 hInstance
, HICON16 hIcon
)
559 dprintf_icon( stddeb
, "CopyIcon16: %04x %04x\n", hInstance
, hIcon
);
560 return CURSORICON_Copy( hInstance
, hIcon
);
564 /***********************************************************************
565 * CopyIcon32 (USER32.59)
567 HICON32
CopyIcon32( HICON32 hIcon
)
569 dprintf_icon( stddeb
, "CopyIcon32: %04x\n", hIcon
);
570 return CURSORICON_Copy( 0, hIcon
);
574 /***********************************************************************
575 * CopyCursor16 (USER.369)
577 HCURSOR16
CopyCursor16( HINSTANCE16 hInstance
, HCURSOR16 hCursor
)
579 dprintf_cursor( stddeb
, "CopyCursor16: %04x %04x\n", hInstance
, hCursor
);
580 return CURSORICON_Copy( hInstance
, hCursor
);
584 /***********************************************************************
585 * DestroyIcon (USER.457)
587 BOOL
DestroyIcon( HICON hIcon
)
589 dprintf_icon( stddeb
, "DestroyIcon: %04x\n", hIcon
);
590 /* FIXME: should check for OEM icon here */
591 return (GlobalFree16( hIcon
) != 0);
595 /***********************************************************************
596 * DestroyCursor (USER.458)
598 BOOL
DestroyCursor( HCURSOR hCursor
)
600 dprintf_cursor( stddeb
, "DestroyCursor: %04x\n", hCursor
);
601 /* FIXME: should check for OEM cursor here */
602 return (GlobalFree16( hCursor
) != 0);
606 /***********************************************************************
609 BOOL
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
613 HBITMAP hXorBits
, hAndBits
;
614 COLORREF oldFg
, oldBg
;
616 if (!(ptr
= (CURSORICONINFO
*)GlobalLock16( hIcon
))) return FALSE
;
617 if (!(hMemDC
= CreateCompatibleDC( hdc
))) return FALSE
;
618 hAndBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, 1, 1, (char *)(ptr
+1));
619 hXorBits
= CreateBitmap( ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
,
620 ptr
->bBitsPerPixel
, (char *)(ptr
+ 1)
621 + ptr
->nHeight
* BITMAP_WIDTH_BYTES(ptr
->nWidth
,1) );
622 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
623 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
625 if (hXorBits
&& hAndBits
)
627 HBITMAP hBitTemp
= SelectObject( hMemDC
, hAndBits
);
628 BitBlt( hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0, SRCAND
);
629 SelectObject( hMemDC
, hXorBits
);
630 BitBlt( hdc
, x
, y
, ptr
->nWidth
, ptr
->nHeight
, hMemDC
, 0, 0, SRCINVERT
);
631 SelectObject( hMemDC
, hBitTemp
);
634 if (hXorBits
) DeleteObject( hXorBits
);
635 if (hAndBits
) DeleteObject( hAndBits
);
636 GlobalUnlock16( hIcon
);
637 SetTextColor( hdc
, oldFg
);
638 SetBkColor( hdc
, oldBg
);
643 /***********************************************************************
644 * DumpIcon (USER.459)
646 DWORD
DumpIcon( SEGPTR pInfo
, WORD
*lpLen
,
647 SEGPTR
*lpXorBits
, SEGPTR
*lpAndBits
)
649 CURSORICONINFO
*info
= PTR_SEG_TO_LIN( pInfo
);
650 int sizeAnd
, sizeXor
;
653 sizeXor
= info
->nHeight
* info
->nWidthBytes
;
654 sizeAnd
= info
->nHeight
* BITMAP_WIDTH_BYTES( info
->nWidth
, 1 );
655 if (lpAndBits
) *lpAndBits
= pInfo
+ sizeof(CURSORICONINFO
);
656 if (lpXorBits
) *lpXorBits
= pInfo
+ sizeof(CURSORICONINFO
) + sizeAnd
;
657 if (lpLen
) *lpLen
= sizeof(CURSORICONINFO
) + sizeAnd
+ sizeXor
;
658 return MAKELONG( sizeXor
, sizeXor
);
662 /***********************************************************************
663 * CURSORICON_SetCursor
665 * Change the X cursor. Helper function for SetCursor() and ShowCursor().
667 static BOOL
CURSORICON_SetCursor( HCURSOR hCursor
)
669 Pixmap pixmapBits
, pixmapMask
, pixmapAll
;
671 Cursor cursor
= None
;
673 if (!hCursor
) /* Create an empty cursor */
675 static const char data
[] = { 0 };
677 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
678 pixmapBits
= XCreateBitmapFromData( display
, rootWindow
, data
, 1, 1 );
681 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
683 XFreePixmap( display
, pixmapBits
);
686 else /* Create the X cursor from the bits */
691 if (!(ptr
= (CURSORICONINFO
*)GlobalLock16( hCursor
))) return FALSE
;
692 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
!= 1)
694 fprintf( stderr
, "Cursor %04x has more than 1 bpp!\n", hCursor
);
698 /* Create a pixmap and transfer all the bits to it */
700 pixmapAll
= XCreatePixmap( display
, rootWindow
,
701 ptr
->nWidth
, ptr
->nHeight
* 2, 1 );
702 image
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
703 1, ZPixmap
, 0, (char *)(ptr
+ 1), ptr
->nWidth
,
704 ptr
->nHeight
* 2, 16, ptr
->nWidthBytes
);
707 extern void _XInitImageFuncPtrs( XImage
* );
708 image
->byte_order
= MSBFirst
;
709 image
->bitmap_bit_order
= MSBFirst
;
710 image
->bitmap_unit
= 16;
711 _XInitImageFuncPtrs(image
);
713 CallTo32_LargeStack( XPutImage
, 10,
714 display
, pixmapAll
, BITMAP_monoGC
, image
,
715 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
*2 );
717 XDestroyImage( image
);
720 /* Now create the 2 pixmaps for bits and mask */
722 pixmapBits
= XCreatePixmap( display
, rootWindow
,
723 ptr
->nWidth
, ptr
->nHeight
, 1 );
724 pixmapMask
= XCreatePixmap( display
, rootWindow
,
725 ptr
->nWidth
, ptr
->nHeight
, 1 );
727 /* Make sure everything went OK so far */
729 if (pixmapBits
&& pixmapMask
&& pixmapAll
)
731 /* We have to do some magic here, as cursors are not fully
732 * compatible between Windows and X11. Under X11, there
733 * are only 3 possible color cursor: black, white and
734 * masked. So we map the 4th Windows color (invert the
735 * bits on the screen) to black. This require some boolean
739 * Xor And Result | Bits Mask Result
740 * 0 0 black | 0 1 background
741 * 0 1 no change | X 0 no change
742 * 1 0 white | 1 1 foreground
743 * 1 1 inverted | 0 1 background
746 * Bits = 'Xor' and not 'And'
747 * Mask = 'Xor' or not 'And'
749 * FIXME: apparently some servers do support 'inverted' color.
750 * I don't know if it's correct per the X spec, but maybe
751 * we ought to take advantage of it. -- AJ
753 XCopyArea( display
, pixmapAll
, pixmapBits
, BITMAP_monoGC
,
754 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
755 XCopyArea( display
, pixmapAll
, pixmapMask
, BITMAP_monoGC
,
756 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
757 XSetFunction( display
, BITMAP_monoGC
, GXandReverse
);
758 XCopyArea( display
, pixmapAll
, pixmapBits
, BITMAP_monoGC
,
759 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
760 XSetFunction( display
, BITMAP_monoGC
, GXorReverse
);
761 XCopyArea( display
, pixmapAll
, pixmapMask
, BITMAP_monoGC
,
762 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
763 XSetFunction( display
, BITMAP_monoGC
, GXcopy
);
764 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
765 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
766 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
767 &fg
, &bg
, ptr
->ptHotSpot
.x
, ptr
->ptHotSpot
.y
);
770 /* Now free everything */
772 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
773 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
774 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
775 GlobalUnlock16( hCursor
);
778 if (cursor
== None
) return FALSE
;
779 if (CURSORICON_XCursor
!= None
) XFreeCursor( display
, CURSORICON_XCursor
);
780 CURSORICON_XCursor
= cursor
;
782 if (rootWindow
!= DefaultRootWindow(display
))
784 /* Set the cursor on the desktop window */
785 XDefineCursor( display
, rootWindow
, cursor
);
789 /* Set the same cursor for all top-level windows */
790 HWND hwnd
= GetWindow( GetDesktopWindow(), GW_CHILD
);
793 Window win
= WIN_GetXWindow( hwnd
);
794 if (win
) XDefineCursor( display
, win
, cursor
);
795 hwnd
= GetWindow( hwnd
, GW_HWNDNEXT
);
802 /***********************************************************************
803 * SetCursor (USER.69)
805 HCURSOR
SetCursor( HCURSOR hCursor
)
809 if (hCursor
== hActiveCursor
) return hActiveCursor
; /* No change */
810 dprintf_cursor( stddeb
, "SetCursor: %04x\n", hCursor
);
811 hOldCursor
= hActiveCursor
;
812 hActiveCursor
= hCursor
;
813 /* Change the cursor shape only if it is visible */
814 if (CURSOR_ShowCount
>= 0) CURSORICON_SetCursor( hActiveCursor
);
819 /***********************************************************************
820 * SetCursorPos (USER.70)
822 void SetCursorPos( short x
, short y
)
824 dprintf_cursor( stddeb
, "SetCursorPos: x=%d y=%d\n", x
, y
);
825 XWarpPointer( display
, rootWindow
, rootWindow
, 0, 0, 0, 0, x
, y
);
829 /***********************************************************************
830 * ShowCursor (USER.71)
832 int ShowCursor( BOOL bShow
)
834 dprintf_cursor( stddeb
, "ShowCursor: %d, count=%d\n",
835 bShow
, CURSOR_ShowCount
);
839 if (++CURSOR_ShowCount
== 0)
840 CURSORICON_SetCursor( hActiveCursor
); /* Show it */
844 if (--CURSOR_ShowCount
== -1)
845 CURSORICON_SetCursor( 0 ); /* Hide it */
847 return CURSOR_ShowCount
;
851 /***********************************************************************
852 * GetCursor (USER.247)
854 HCURSOR
GetCursor(void)
856 return hActiveCursor
;
860 /***********************************************************************
861 * ClipCursor16 (USER.16)
863 BOOL16
ClipCursor16( const RECT16
*rect
)
865 if (!rect
) SetRectEmpty32( &CURSOR_ClipRect
);
866 else CONV_RECT16TO32( rect
, &CURSOR_ClipRect
);
871 /***********************************************************************
872 * ClipCursor32 (USER32.52)
874 BOOL32
ClipCursor32( const RECT32
*rect
)
876 if (!rect
) SetRectEmpty32( &CURSOR_ClipRect
);
877 else CopyRect32( &CURSOR_ClipRect
, rect
);
882 /***********************************************************************
883 * GetCursorPos16 (USER.17)
885 void GetCursorPos16( POINT16
*pt
)
888 int rootX
, rootY
, childX
, childY
;
889 unsigned int mousebut
;
892 if (!XQueryPointer( display
, rootWindow
, &root
, &child
,
893 &rootX
, &rootY
, &childX
, &childY
, &mousebut
))
897 pt
->x
= rootX
+ desktopX
;
898 pt
->y
= rootY
+ desktopY
;
900 dprintf_cursor(stddeb
, "GetCursorPos: ret=%d,%d\n", pt
->x
, pt
->y
);
904 /***********************************************************************
905 * GetCursorPos32 (USER32.228)
907 void GetCursorPos32( POINT32
*pt
)
910 GetCursorPos16( &pt16
);
911 if (pt
) CONV_POINT16TO32( &pt16
, pt
);
915 /***********************************************************************
916 * GetClipCursor16 (USER.309)
918 void GetClipCursor16( RECT16
*rect
)
920 if (rect
) CONV_RECT32TO16( &CURSOR_ClipRect
, rect
);
924 /***********************************************************************
925 * GetClipCursor32 (USER32.220)
927 void GetClipCursor32( RECT32
*rect
)
929 if (rect
) CopyRect32( rect
, &CURSOR_ClipRect
);
933 /**********************************************************************
934 * GetIconID (USER.455)
936 WORD
GetIconID( HANDLE hResource
, DWORD resType
)
938 CURSORICONDIR
*lpDir
= LockResource(hResource
);
940 if (!lpDir
|| lpDir
->idReserved
||
941 ((lpDir
->idType
!= 1) && (lpDir
->idType
!= 2)))
943 dprintf_cursor(stddeb
,"GetIconID: invalid resource directory\n");
947 dprintf_cursor( stddeb
, "GetIconID: hRes=%04x, entries=%i\n",
948 hResource
, lpDir
->idCount
);
954 CURSORDIRENTRY
*entry
= CURSORICON_FindBestCursor( lpDir
,
955 SYSMETRICS_CXCURSOR
, SYSMETRICS_CYCURSOR
);
956 return entry
? entry
->wResId
: 0;
960 ICONDIRENTRY
*entry
= CURSORICON_FindBestIcon( lpDir
,
961 SYSMETRICS_CXICON
, SYSMETRICS_CYICON
,
962 MIN( 16, 1 << screenDepth
) );
963 return entry
? entry
->wResId
: 0;
966 fprintf( stderr
, "GetIconID: invalid res type %ld\n", resType
);
971 /**********************************************************************
972 * LoadIconHandler (USER.456)
974 HICON
LoadIconHandler( HANDLE hResource
, BOOL bNew
)
976 dprintf_cursor(stddeb
,"LoadIconHandler: hRes=%04x\n",hResource
);
980 fprintf(stdnimp
,"LoadIconHandler: 2.xx resources are not supported\n");
983 return CURSORICON_LoadHandler( hResource
, 0, FALSE
);