Release 951226
[wine.git] / objects / cursoricon.c
blob4098051b845155010e2bebc7092c61344caf851f
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 */
7 /*
8 * Theory:
10 * Cursors and icons are stored in a global heap block, with the
11 * following layout:
13 * CURSORICONINFO info;
14 * BYTE[] ANDbits;
15 * BYTE[] XORbits;
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 :-(
25 #include <string.h>
26 #include <stdlib.h>
27 #include "windows.h"
28 #include "bitmap.h"
29 #include "callback.h"
30 #include "cursoricon.h"
31 #include "sysmetrics.h"
32 #include "win.h"
33 #include "stddebug.h"
34 #include "debug.h"
35 #include "xmalloc.h"
36 #include "task.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 RECT 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;
55 if (dir->idCount < 1)
57 fprintf( stderr, "Icon: empty directory!\n" );
58 return NULL;
60 if (dir->idCount == 1) return &dir->idEntries[0].icon; /* No choice... */
62 /* First find the exact size with less colors */
64 maxcolors = 0;
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))
69 bestEntry = entry;
70 maxcolors = entry->bColorCount;
72 if (bestEntry) return bestEntry;
74 /* First find the exact size with more colors */
76 maxcolors = 255;
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))
81 bestEntry = entry;
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))
94 bestEntry = entry;
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 */
103 maxcolors = 255;
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))
110 bestEntry = entry;
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 */
119 maxcolors = 0;
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))
125 bestEntry = entry;
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))
139 bestEntry = entry;
140 maxwidth = entry->bWidth;
141 maxheight = entry->bHeight;
142 maxcolors = entry->bColorCount;
145 return bestEntry;
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" );
163 return NULL;
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))
174 bestEntry = entry;
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))
186 bestEntry = entry;
187 maxwidth = entry->wWidth;
188 maxheight = entry->wHeight;
191 return bestEntry;
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)
205 HRSRC hRsrc;
206 HANDLE hMem;
207 CURSORICONDIR *dir;
208 CURSORICONDIRENTRY *entry = NULL;
210 if (!(hRsrc = FindResource( hInstance, name,
211 fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON )))
212 return FALSE;
213 if (!(hMem = LoadResource( hInstance, hRsrc ))) return FALSE;
214 if ((dir = (CURSORICONDIR *)LockResource( hMem )))
216 if (fCursor)
217 entry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
218 width, height );
219 else
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_Load
232 * Load a cursor or icon.
234 static HANDLE CURSORICON_Load( HANDLE hInstance, SEGPTR name, int width,
235 int height, int colors, BOOL fCursor )
237 HANDLE handle, hAndBits, hXorBits;
238 HRSRC hRsrc;
239 HDC hdc;
240 int size, sizeAnd, sizeXor;
241 POINT hotspot = { 0 ,0 };
242 BITMAPOBJ *bmpXor, *bmpAnd;
243 BITMAPINFO *bmi, *pInfo;
244 CURSORICONINFO *info;
245 CURSORICONDIRENTRY dirEntry;
246 char *bits;
248 if (!hInstance) /* OEM cursor/icon */
250 if (HIWORD(name)) /* Check for '#xxx' name */
252 char *ptr = PTR_SEG_TO_LIN( name );
253 if (ptr[0] != '#') return 0;
254 if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
256 return OBM_LoadCursorIcon( LOWORD(name), fCursor );
259 /* Find the best entry in the directory */
261 if (!CURSORICON_LoadDirEntry( hInstance, name, width, height,
262 colors, fCursor, &dirEntry )) return 0;
264 /* Load the resource */
266 if (!(hRsrc = FindResource( hInstance,
267 MAKEINTRESOURCE( dirEntry.icon.wResId ),
268 fCursor ? RT_CURSOR : RT_ICON ))) return 0;
269 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
271 if (fCursor) /* If cursor, get the hotspot */
273 POINT *pt = (POINT *)LockResource( handle );
274 hotspot = *pt;
275 bmi = (BITMAPINFO *)(pt + 1);
277 else bmi = (BITMAPINFO *)LockResource( handle );
279 /* Create a copy of the bitmap header */
281 size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
282 /* Make sure we have room for the monochrome bitmap later on */
283 size = MAX( size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD) );
284 pInfo = (BITMAPINFO *)xmalloc( size );
285 memcpy( pInfo, bmi, size );
287 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
289 if (pInfo->bmiHeader.biCompression != BI_RGB)
291 fprintf(stderr,"Unknown size for compressed icon bitmap.\n");
292 FreeResource( handle );
293 free( pInfo );
294 return 0;
296 pInfo->bmiHeader.biHeight /= 2;
298 else if (pInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
300 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)pInfo;
301 core->bcHeight /= 2;
303 else
305 fprintf( stderr, "CURSORICON_Load: Unknown bitmap length %ld!\n",
306 pInfo->bmiHeader.biSize );
307 FreeResource( handle );
308 free( pInfo );
309 return 0;
312 /* Create the XOR bitmap */
314 if (!(hdc = GetDC( 0 )))
316 FreeResource( handle );
317 free( pInfo );
318 return 0;
321 hXorBits = CreateDIBitmap( hdc, &pInfo->bmiHeader, CBM_INIT,
322 (char*)bmi + size, pInfo, DIB_RGB_COLORS );
324 /* Fix the bitmap header to load the monochrome mask */
326 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
328 BITMAPINFOHEADER *bih = &pInfo->bmiHeader;
329 RGBQUAD *rgb = pInfo->bmiColors;
330 bits = (char *)bmi + size +
331 DIB_GetImageWidthBytes(bih->biWidth,bih->biBitCount)*bih->biHeight;
332 bih->biBitCount = 1;
333 bih->biClrUsed = bih->biClrImportant = 2;
334 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
335 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
336 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
338 else
340 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER *)pInfo;
341 RGBTRIPLE *rgb = (RGBTRIPLE *)(bch + 1);
342 bits = (char *)bmi + size +
343 DIB_GetImageWidthBytes(bch->bcWidth,bch->bcBitCount)*bch->bcHeight;
344 bch->bcBitCount = 1;
345 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
346 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
349 /* Create the AND bitmap */
351 hAndBits = CreateDIBitmap( hdc, &pInfo->bmiHeader, CBM_INIT,
352 bits, pInfo, DIB_RGB_COLORS );
353 ReleaseDC( 0, hdc );
354 FreeResource( handle );
356 /* Now create the CURSORICONINFO structure */
358 bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( hXorBits, BITMAP_MAGIC );
359 bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( hAndBits, BITMAP_MAGIC );
360 sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
361 sizeAnd = bmpAnd->bitmap.bmHeight * bmpAnd->bitmap.bmWidthBytes;
363 if (!(handle = GlobalAlloc( GMEM_MOVEABLE,
364 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
366 DeleteObject( hXorBits );
367 DeleteObject( hAndBits );
368 return 0;
370 /* Make it owned by the module */
371 FarSetOwner( handle, (WORD)(DWORD)GetExePtr( hInstance ) );
372 info = (CURSORICONINFO *)GlobalLock( handle );
373 info->ptHotSpot.x = hotspot.x;
374 info->ptHotSpot.y = hotspot.y;
375 info->nWidth = bmpXor->bitmap.bmWidth;
376 info->nHeight = bmpXor->bitmap.bmHeight;
377 info->nWidthBytes = bmpXor->bitmap.bmWidthBytes;
378 info->bPlanes = bmpXor->bitmap.bmPlanes;
379 info->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
381 /* Transfer the bitmap bits to the CURSORICONINFO structure */
383 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
384 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
385 DeleteObject( hXorBits );
386 DeleteObject( hAndBits );
387 GlobalUnlock( handle );
388 return handle;
392 /***********************************************************************
393 * CURSORICON_Copy
395 * Make a copy of a cursor or icon.
397 static HANDLE CURSORICON_Copy( HANDLE hInstance, HANDLE handle )
399 char *ptrOld, *ptrNew;
400 int size;
401 HANDLE hNew;
403 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
404 if (!(hInstance = GetExePtr( hInstance ))) return 0;
405 size = GlobalSize( handle );
406 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
407 FarSetOwner( hNew, (WORD)(DWORD)hInstance );
408 ptrNew = (char *)GlobalLock( hNew );
409 memcpy( ptrNew, ptrOld, size );
410 GlobalUnlock( handle );
411 GlobalUnlock( hNew );
412 return hNew;
415 /***********************************************************************
416 * CURSORICON_IconToCursor
418 * Should convert bitmap to mono and truncate if too large
419 * FIXME: if icon is passed returns a copy of OCR_DRAGOBJECT cursor
420 * but should actually convert icon to cursor.
422 HCURSOR CURSORICON_IconToCursor(HICON hIcon)
424 CURSORICONINFO *ptr = NULL;
426 if(hIcon)
427 if (!(ptr = (CURSORICONINFO*)GlobalLock( hIcon ))) return FALSE;
428 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
430 return hIcon; /* assuming it's a cursor */
432 else
434 /* kludge */
436 HTASK hTask = GetCurrentTask();
437 TDB* pTask = (TDB *)GlobalLock(hTask);
439 if(!pTask) return 0;
441 fprintf( stdnimp, "IconToCursor: Icons are not supported, returning default!\n");
442 return CURSORICON_Copy( pTask->hInstance ,
443 CURSORICON_Load(0,MAKEINTRESOURCE(OCR_DRAGOBJECT),
444 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE) );
447 return 0;
450 /***********************************************************************
451 * LoadCursor (USER.173)
453 HCURSOR LoadCursor( HANDLE hInstance, SEGPTR name )
455 if (HIWORD(name))
456 dprintf_cursor( stddeb, "LoadCursor: "NPFMT" '%s'\n",
457 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
458 else
459 dprintf_cursor( stddeb, "LoadCursor: "NPFMT" %04x\n",
460 hInstance, LOWORD(name) );
462 return CURSORICON_Load( hInstance, name,
463 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR, 1, TRUE);
467 /***********************************************************************
468 * LoadIcon (USER.174)
470 HICON LoadIcon( HANDLE hInstance, SEGPTR name )
472 if (HIWORD(name))
473 dprintf_icon( stddeb, "LoadIcon: "NPFMT" '%s'\n",
474 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
475 else
476 dprintf_icon( stddeb, "LoadIcon: "NPFMT" %04x\n",
477 hInstance, LOWORD(name) );
479 return CURSORICON_Load( hInstance, name,
480 SYSMETRICS_CXICON, SYSMETRICS_CYICON,
481 MIN( 16, 1 << screenDepth ), FALSE );
485 /***********************************************************************
486 * CreateCursor (USER.406)
488 HICON CreateCursor( HANDLE hInstance, INT xHotSpot, INT yHotSpot,
489 INT nWidth, INT nHeight, LPSTR lpANDbits, LPSTR lpXORbits)
491 CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
493 dprintf_cursor( stddeb, "CreateCursor: %dx%d spot=%d,%d xor=%p and=%p\n",
494 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
495 return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
499 /***********************************************************************
500 * CreateIcon (USER.407)
502 HICON CreateIcon( HANDLE hInstance, INT nWidth, INT nHeight, BYTE bPlanes,
503 BYTE bBitsPixel, LPSTR lpANDbits, LPSTR lpXORbits)
505 CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel };
507 dprintf_icon( stddeb, "CreateIcon: %dx%dx%d, xor=%p, and=%p\n",
508 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
509 return CreateCursorIconIndirect( hInstance, &info, lpANDbits, lpXORbits );
513 /***********************************************************************
514 * CreateCursorIconIndirect (USER.408)
516 HANDLE CreateCursorIconIndirect( HANDLE hInstance, CURSORICONINFO *info,
517 LPSTR lpANDbits, LPSTR lpXORbits )
519 HANDLE handle;
520 char *ptr;
521 int sizeAnd, sizeXor;
523 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
524 if (!hInstance || !lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
525 info->nWidthBytes = (info->nWidth * info->bBitsPerPixel + 15) / 16 * 2;
526 sizeXor = info->nHeight * info->nWidthBytes;
527 sizeAnd = info->nHeight * ((info->nWidth + 15) / 16 * 2);
528 if (!(handle = DirectResAlloc(hInstance, 0x10,
529 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
530 return 0;
531 ptr = (char *)GlobalLock( handle );
532 memcpy( ptr, info, sizeof(*info) );
533 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
534 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
535 GlobalUnlock( handle );
536 return handle;
540 /***********************************************************************
541 * CopyIcon (USER.368)
543 HICON CopyIcon( HANDLE hInstance, HICON hIcon )
545 dprintf_icon( stddeb, "CopyIcon: "NPFMT" "NPFMT"\n", hInstance, hIcon );
546 return CURSORICON_Copy( hInstance, hIcon );
550 /***********************************************************************
551 * CopyCursor (USER.369)
553 HCURSOR CopyCursor( HANDLE hInstance, HCURSOR hCursor )
555 dprintf_cursor( stddeb, "CopyCursor: "NPFMT" "NPFMT"\n", hInstance, hCursor );
556 return CURSORICON_Copy( hInstance, hCursor );
560 /***********************************************************************
561 * DestroyIcon (USER.457)
563 BOOL DestroyIcon( HICON hIcon )
565 dprintf_icon( stddeb, "DestroyIcon: "NPFMT"\n", hIcon );
566 /* FIXME: should check for OEM icon here */
567 return (GlobalFree( hIcon ) != 0);
571 /***********************************************************************
572 * DestroyCursor (USER.458)
574 BOOL DestroyCursor( HCURSOR hCursor )
576 dprintf_cursor( stddeb, "DestroyCursor: "NPFMT"\n", hCursor );
577 /* FIXME: should check for OEM cursor here */
578 return (GlobalFree( hCursor ) != 0);
582 /***********************************************************************
583 * DrawIcon (USER.84)
585 BOOL DrawIcon( HDC hdc, short x, short y, HICON hIcon )
587 CURSORICONINFO *ptr;
588 HDC hMemDC;
589 HBITMAP hXorBits, hAndBits;
590 COLORREF oldFg, oldBg;
592 if (!(ptr = (CURSORICONINFO *)GlobalLock( hIcon ))) return FALSE;
593 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
594 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, (char *)(ptr+1));
595 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
596 ptr->bBitsPerPixel, (char *)(ptr + 1)
597 + ptr->nHeight * ((ptr->nWidth + 15) / 16 * 2) );
598 oldFg = SetTextColor( hdc, RGB(0,0,0) );
599 oldBg = SetBkColor( hdc, RGB(255,255,255) );
601 if (hXorBits && hAndBits)
603 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
604 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
605 SelectObject( hMemDC, hXorBits );
606 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCINVERT);
607 SelectObject( hMemDC, hBitTemp );
609 DeleteDC( hMemDC );
610 if (hXorBits) DeleteObject( hXorBits );
611 if (hAndBits) DeleteObject( hAndBits );
612 GlobalUnlock( hIcon );
613 SetTextColor( hdc, oldFg );
614 SetBkColor( hdc, oldBg );
615 return TRUE;
619 /***********************************************************************
620 * DumpIcon (USER.459)
622 DWORD DumpIcon( SEGPTR pInfo, WORD *lpLen,
623 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
625 CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
626 int sizeAnd, sizeXor;
628 if (!info) return 0;
629 sizeXor = info->nHeight * info->nWidthBytes;
630 sizeAnd = info->nHeight * ((info->nWidth + 15) / 16 * 2);
631 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
632 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
633 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
634 return MAKELONG( sizeXor, sizeXor );
638 /***********************************************************************
639 * CURSORICON_SetCursor
641 * Change the X cursor. Helper function for SetCursor() and ShowCursor().
643 static BOOL CURSORICON_SetCursor( HCURSOR hCursor )
645 Pixmap pixmapBits, pixmapMask, pixmapAll;
646 XColor fg, bg;
647 Cursor cursor = None;
649 if (!hCursor) /* Create an empty cursor */
651 static const char data[] = { 0 };
653 bg.red = bg.green = bg.blue = 0x0000;
654 pixmapBits = XCreateBitmapFromData( display, rootWindow, data, 1, 1 );
655 if (pixmapBits)
657 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
658 &bg, &bg, 0, 0 );
659 XFreePixmap( display, pixmapBits );
662 else /* Create the X cursor from the bits */
664 CURSORICONINFO *ptr;
665 XImage *image;
667 if (!(ptr = (CURSORICONINFO*)GlobalLock( hCursor ))) return FALSE;
668 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
670 fprintf( stderr, "Cursor "NPFMT" has more than 1 bpp!\n", hCursor );
671 return FALSE;
674 /* Create a pixmap and transfer all the bits to it */
676 pixmapAll = XCreatePixmap( display, rootWindow,
677 ptr->nWidth, ptr->nHeight * 2, 1 );
678 image = XCreateImage( display, DefaultVisualOfScreen(screen),
679 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
680 ptr->nHeight * 2, 16, ptr->nWidthBytes);
681 if (image)
683 extern void _XInitImageFuncPtrs( XImage* );
684 image->byte_order = MSBFirst;
685 image->bitmap_bit_order = MSBFirst;
686 image->bitmap_unit = 16;
687 _XInitImageFuncPtrs(image);
688 if (pixmapAll)
689 CallTo32_LargeStack( XPutImage, 10,
690 display, pixmapAll, BITMAP_monoGC, image,
691 0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
692 image->data = NULL;
693 XDestroyImage( image );
696 /* Now create the 2 pixmaps for bits and mask */
698 pixmapBits = XCreatePixmap( display, rootWindow,
699 ptr->nWidth, ptr->nHeight, 1 );
700 pixmapMask = XCreatePixmap( display, rootWindow,
701 ptr->nWidth, ptr->nHeight, 1 );
703 /* Make sure everything went OK so far */
705 if (pixmapBits && pixmapMask && pixmapAll)
707 /* We have to do some magic here, as cursors are not fully
708 * compatible between Windows and X11. Under X11, there
709 * are only 3 possible color cursor: black, white and
710 * masked. So we map the 4th Windows color (invert the
711 * bits on the screen) to black. This require some boolean
712 * arithmetic:
714 * Windows | X11
715 * Xor And Result | Bits Mask Result
716 * 0 0 black | 0 1 background
717 * 0 1 no change | X 0 no change
718 * 1 0 white | 1 1 foreground
719 * 1 1 inverted | 0 1 background
721 * which gives:
722 * Bits = 'Xor' and not 'And'
723 * Mask = 'Xor' or not 'And'
725 * FIXME: apparently some servers do support 'inverted' color.
726 * I don't know if it's correct per the X spec, but maybe
727 * we ought to take advantage of it. -- AJ
729 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
730 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
731 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
732 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
733 XSetFunction( display, BITMAP_monoGC, GXandReverse );
734 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
735 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
736 XSetFunction( display, BITMAP_monoGC, GXorReverse );
737 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
738 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
739 XSetFunction( display, BITMAP_monoGC, GXcopy );
740 fg.red = fg.green = fg.blue = 0xffff;
741 bg.red = bg.green = bg.blue = 0x0000;
742 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
743 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
746 /* Now free everything */
748 if (pixmapAll) XFreePixmap( display, pixmapAll );
749 if (pixmapBits) XFreePixmap( display, pixmapBits );
750 if (pixmapMask) XFreePixmap( display, pixmapMask );
751 GlobalUnlock( hCursor );
754 if (cursor == None) return FALSE;
755 if (CURSORICON_XCursor != None) XFreeCursor( display, CURSORICON_XCursor );
756 CURSORICON_XCursor = cursor;
758 if (rootWindow != DefaultRootWindow(display))
760 /* Set the cursor on the desktop window */
761 XDefineCursor( display, rootWindow, cursor );
763 else
765 /* Set the same cursor for all top-level windows */
766 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
767 while(hwnd)
769 Window win = WIN_GetXWindow( hwnd );
770 if (win) XDefineCursor( display, win, cursor );
771 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
774 return TRUE;
778 /***********************************************************************
779 * SetCursor (USER.69)
781 HCURSOR SetCursor( HCURSOR hCursor )
783 HCURSOR hOldCursor;
785 if (hCursor == hActiveCursor) return hActiveCursor; /* No change */
786 dprintf_cursor( stddeb, "SetCursor: "NPFMT"\n", hCursor );
787 hOldCursor = hActiveCursor;
788 hActiveCursor = hCursor;
789 /* Change the cursor shape only if it is visible */
790 if (CURSOR_ShowCount >= 0) CURSORICON_SetCursor( hActiveCursor );
791 return hOldCursor;
795 /***********************************************************************
796 * SetCursorPos (USER.70)
798 void SetCursorPos( short x, short y )
800 dprintf_cursor( stddeb, "SetCursorPos: x=%d y=%d\n", x, y );
801 XWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, x, y );
805 /***********************************************************************
806 * ShowCursor (USER.71)
808 int ShowCursor( BOOL bShow )
810 dprintf_cursor( stddeb, "ShowCursor: %d, count=%d\n",
811 bShow, CURSOR_ShowCount );
813 if (bShow)
815 if (++CURSOR_ShowCount == 0)
816 CURSORICON_SetCursor( hActiveCursor ); /* Show it */
818 else
820 if (--CURSOR_ShowCount == -1)
821 CURSORICON_SetCursor( 0 ); /* Hide it */
823 return CURSOR_ShowCount;
827 /***********************************************************************
828 * GetCursor (USER.247)
830 HCURSOR GetCursor(void)
832 return hActiveCursor;
836 /***********************************************************************
837 * ClipCursor (USER.16)
839 void ClipCursor( RECT *rect )
841 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
842 else CopyRect( &CURSOR_ClipRect, rect );
846 /***********************************************************************
847 * GetCursorPos (USER.17)
849 void GetCursorPos( POINT *pt )
851 Window root, child;
852 int rootX, rootY, childX, childY;
853 unsigned int mousebut;
855 if (!pt) return;
856 if (!XQueryPointer( display, rootWindow, &root, &child,
857 &rootX, &rootY, &childX, &childY, &mousebut ))
858 pt->x = pt->y = 0;
859 else
861 pt->x = rootX + desktopX;
862 pt->y = rootY + desktopY;
864 dprintf_cursor(stddeb, "GetCursorPos: ret=%ld,%ld\n", (LONG)pt->x,
865 (LONG)pt->y );
869 /***********************************************************************
870 * GetClipCursor (USER.309)
872 void GetClipCursor( RECT *rect )
874 if (rect) CopyRect( rect, &CURSOR_ClipRect );
878 /**********************************************************************
879 * GetIconID (USER.455)
881 WORD GetIconID( HANDLE hResource, DWORD resType )
883 fprintf( stderr, "GetIconId("NPFMT",%ld): empty stub!\n",
884 hResource, resType );
885 return 0;
889 /**********************************************************************
890 * LoadIconHandler (USER.456)
892 HICON LoadIconHandler( HANDLE hResource, BOOL bNew )
894 fprintf( stderr, "LoadIconHandle("NPFMT",%d): empty stub!\n",
895 hResource, bNew );
896 return 0;