Release 960521
[wine/multimedia.git] / objects / cursoricon.c
blob223c0c3c0a23ffd68ca757ab8a98649668520827
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 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;
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_LoadHandler
232 * Create a cursor or icon from a resource.
234 HANDLE CURSORICON_LoadHandler( HANDLE handle, HINSTANCE hInstance,
235 BOOL fCursor )
237 HANDLE hAndBits, hXorBits;
238 HDC hdc;
239 int size, sizeAnd, sizeXor;
240 POINT16 hotspot = { 0 ,0 };
241 BITMAPOBJ *bmpXor, *bmpAnd;
242 BITMAPINFO *bmi, *pInfo;
243 CURSORICONINFO *info;
244 char *bits;
246 if (fCursor) /* If cursor, get the hotspot */
248 POINT16 *pt = (POINT16 *)LockResource( handle );
249 hotspot = *pt;
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");
267 free( pInfo );
268 return 0;
270 pInfo->bmiHeader.biHeight /= 2;
272 else if (pInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
274 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)pInfo;
275 core->bcHeight /= 2;
277 else
279 fprintf( stderr, "CURSORICON_Load: Unknown bitmap length %ld!\n",
280 pInfo->bmiHeader.biSize );
281 free( pInfo );
282 return 0;
285 /* Create the XOR bitmap */
287 if (!(hdc = GetDC( 0 )))
289 free( pInfo );
290 return 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;
304 bih->biBitCount = 1;
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;
310 else
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;
316 bch->bcBitCount = 1;
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 );
325 ReleaseDC( 0, hdc );
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 );
339 return 0;
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 );
361 return handle;
364 /**********************************************************************
365 * CURSORICON_Load
367 * Load a cursor or icon.
369 static HANDLE CURSORICON_Load( HANDLE hInstance, SEGPTR name, int width,
370 int height, int colors, BOOL fCursor )
372 HANDLE handle,hRet;
373 HRSRC hRsrc;
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);
401 return hRet;
405 /***********************************************************************
406 * CURSORICON_Copy
408 * Make a copy of a cursor or icon.
410 static HANDLE CURSORICON_Copy( HANDLE hInstance, HANDLE handle )
412 char *ptrOld, *ptrNew;
413 int size;
414 HANDLE hNew;
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 );
425 return 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;
439 if(hIcon)
440 if (!(ptr = (CURSORICONINFO*)GlobalLock16( hIcon ))) return FALSE;
441 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
443 return hIcon; /* assuming it's a cursor */
445 else
447 /* kludge */
449 HTASK hTask = GetCurrentTask();
450 TDB* pTask = (TDB *)GlobalLock16(hTask);
452 if(!pTask) return 0;
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) );
460 return 0;
463 /***********************************************************************
464 * LoadCursor (USER.173)
466 HCURSOR LoadCursor( HANDLE hInstance, SEGPTR name )
468 if (HIWORD(name))
469 dprintf_cursor( stddeb, "LoadCursor: %04x '%s'\n",
470 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
471 else
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 )
485 if (HIWORD(name))
486 dprintf_icon( stddeb, "LoadIcon: %04x '%s'\n",
487 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
488 else
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 )
533 HANDLE handle;
534 char *ptr;
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)))
544 return 0;
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 );
550 return handle;
554 /***********************************************************************
555 * CopyIcon (USER.368)
557 #ifdef WINELIB
558 HICON CopyIcon( HICON hIcon )
560 dprintf_icon( stddeb, "CopyIcon: %04x\n", hIcon );
561 return CURSORICON_Copy( 0, hIcon );
563 #else
564 HICON CopyIcon( HANDLE hInstance, HICON hIcon )
566 dprintf_icon( stddeb, "CopyIcon: %04x %04x\n", hInstance, hIcon );
567 return CURSORICON_Copy( hInstance, hIcon );
569 #endif
572 /***********************************************************************
573 * CopyCursor (USER.369)
575 #ifdef WINELIB
576 HCURSOR CopyCursor( HCURSOR hCursor )
578 dprintf_cursor( stddeb, "CopyCursor: %04x\n", hCursor );
579 return CURSORICON_Copy( 0, hCursor );
581 #else
582 HCURSOR CopyCursor( HANDLE hInstance, HCURSOR hCursor )
584 dprintf_cursor( stddeb, "CopyCursor: %04x %04x\n", hInstance, hCursor );
585 return CURSORICON_Copy( hInstance, hCursor );
587 #endif
590 /***********************************************************************
591 * DestroyIcon (USER.457)
593 BOOL DestroyIcon( HICON hIcon )
595 dprintf_icon( stddeb, "DestroyIcon: %04x\n", hIcon );
596 /* FIXME: should check for OEM icon here */
597 return (GlobalFree16( hIcon ) != 0);
601 /***********************************************************************
602 * DestroyCursor (USER.458)
604 BOOL DestroyCursor( HCURSOR hCursor )
606 dprintf_cursor( stddeb, "DestroyCursor: %04x\n", hCursor );
607 /* FIXME: should check for OEM cursor here */
608 return (GlobalFree16( hCursor ) != 0);
612 /***********************************************************************
613 * DrawIcon (USER.84)
615 BOOL DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
617 CURSORICONINFO *ptr;
618 HDC hMemDC;
619 HBITMAP hXorBits, hAndBits;
620 COLORREF oldFg, oldBg;
622 if (!(ptr = (CURSORICONINFO *)GlobalLock16( hIcon ))) return FALSE;
623 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
624 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, (char *)(ptr+1));
625 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
626 ptr->bBitsPerPixel, (char *)(ptr + 1)
627 + ptr->nHeight * BITMAP_WIDTH_BYTES(ptr->nWidth,1) );
628 oldFg = SetTextColor( hdc, RGB(0,0,0) );
629 oldBg = SetBkColor( hdc, RGB(255,255,255) );
631 if (hXorBits && hAndBits)
633 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
634 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
635 SelectObject( hMemDC, hXorBits );
636 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCINVERT);
637 SelectObject( hMemDC, hBitTemp );
639 DeleteDC( hMemDC );
640 if (hXorBits) DeleteObject( hXorBits );
641 if (hAndBits) DeleteObject( hAndBits );
642 GlobalUnlock16( hIcon );
643 SetTextColor( hdc, oldFg );
644 SetBkColor( hdc, oldBg );
645 return TRUE;
649 /***********************************************************************
650 * DumpIcon (USER.459)
652 DWORD DumpIcon( SEGPTR pInfo, WORD *lpLen,
653 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
655 CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
656 int sizeAnd, sizeXor;
658 if (!info) return 0;
659 sizeXor = info->nHeight * info->nWidthBytes;
660 sizeAnd = info->nHeight * BITMAP_WIDTH_BYTES( info->nWidth, 1 );
661 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
662 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
663 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
664 return MAKELONG( sizeXor, sizeXor );
668 /***********************************************************************
669 * CURSORICON_SetCursor
671 * Change the X cursor. Helper function for SetCursor() and ShowCursor().
673 static BOOL CURSORICON_SetCursor( HCURSOR hCursor )
675 Pixmap pixmapBits, pixmapMask, pixmapAll;
676 XColor fg, bg;
677 Cursor cursor = None;
679 if (!hCursor) /* Create an empty cursor */
681 static const char data[] = { 0 };
683 bg.red = bg.green = bg.blue = 0x0000;
684 pixmapBits = XCreateBitmapFromData( display, rootWindow, data, 1, 1 );
685 if (pixmapBits)
687 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
688 &bg, &bg, 0, 0 );
689 XFreePixmap( display, pixmapBits );
692 else /* Create the X cursor from the bits */
694 CURSORICONINFO *ptr;
695 XImage *image;
697 if (!(ptr = (CURSORICONINFO*)GlobalLock16( hCursor ))) return FALSE;
698 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
700 fprintf( stderr, "Cursor %04x has more than 1 bpp!\n", hCursor );
701 return FALSE;
704 /* Create a pixmap and transfer all the bits to it */
706 pixmapAll = XCreatePixmap( display, rootWindow,
707 ptr->nWidth, ptr->nHeight * 2, 1 );
708 image = XCreateImage( display, DefaultVisualOfScreen(screen),
709 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
710 ptr->nHeight * 2, 16, ptr->nWidthBytes);
711 if (image)
713 extern void _XInitImageFuncPtrs( XImage* );
714 image->byte_order = MSBFirst;
715 image->bitmap_bit_order = MSBFirst;
716 image->bitmap_unit = 16;
717 _XInitImageFuncPtrs(image);
718 if (pixmapAll)
719 CallTo32_LargeStack( XPutImage, 10,
720 display, pixmapAll, BITMAP_monoGC, image,
721 0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
722 image->data = NULL;
723 XDestroyImage( image );
726 /* Now create the 2 pixmaps for bits and mask */
728 pixmapBits = XCreatePixmap( display, rootWindow,
729 ptr->nWidth, ptr->nHeight, 1 );
730 pixmapMask = XCreatePixmap( display, rootWindow,
731 ptr->nWidth, ptr->nHeight, 1 );
733 /* Make sure everything went OK so far */
735 if (pixmapBits && pixmapMask && pixmapAll)
737 /* We have to do some magic here, as cursors are not fully
738 * compatible between Windows and X11. Under X11, there
739 * are only 3 possible color cursor: black, white and
740 * masked. So we map the 4th Windows color (invert the
741 * bits on the screen) to black. This require some boolean
742 * arithmetic:
744 * Windows | X11
745 * Xor And Result | Bits Mask Result
746 * 0 0 black | 0 1 background
747 * 0 1 no change | X 0 no change
748 * 1 0 white | 1 1 foreground
749 * 1 1 inverted | 0 1 background
751 * which gives:
752 * Bits = 'Xor' and not 'And'
753 * Mask = 'Xor' or not 'And'
755 * FIXME: apparently some servers do support 'inverted' color.
756 * I don't know if it's correct per the X spec, but maybe
757 * we ought to take advantage of it. -- AJ
759 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
760 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
761 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
762 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
763 XSetFunction( display, BITMAP_monoGC, GXandReverse );
764 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
765 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
766 XSetFunction( display, BITMAP_monoGC, GXorReverse );
767 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
768 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
769 XSetFunction( display, BITMAP_monoGC, GXcopy );
770 fg.red = fg.green = fg.blue = 0xffff;
771 bg.red = bg.green = bg.blue = 0x0000;
772 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
773 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
776 /* Now free everything */
778 if (pixmapAll) XFreePixmap( display, pixmapAll );
779 if (pixmapBits) XFreePixmap( display, pixmapBits );
780 if (pixmapMask) XFreePixmap( display, pixmapMask );
781 GlobalUnlock16( hCursor );
784 if (cursor == None) return FALSE;
785 if (CURSORICON_XCursor != None) XFreeCursor( display, CURSORICON_XCursor );
786 CURSORICON_XCursor = cursor;
788 if (rootWindow != DefaultRootWindow(display))
790 /* Set the cursor on the desktop window */
791 XDefineCursor( display, rootWindow, cursor );
793 else
795 /* Set the same cursor for all top-level windows */
796 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
797 while(hwnd)
799 Window win = WIN_GetXWindow( hwnd );
800 if (win) XDefineCursor( display, win, cursor );
801 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
804 return TRUE;
808 /***********************************************************************
809 * SetCursor (USER.69)
811 HCURSOR SetCursor( HCURSOR hCursor )
813 HCURSOR hOldCursor;
815 if (hCursor == hActiveCursor) return hActiveCursor; /* No change */
816 dprintf_cursor( stddeb, "SetCursor: %04x\n", hCursor );
817 hOldCursor = hActiveCursor;
818 hActiveCursor = hCursor;
819 /* Change the cursor shape only if it is visible */
820 if (CURSOR_ShowCount >= 0) CURSORICON_SetCursor( hActiveCursor );
821 return hOldCursor;
825 /***********************************************************************
826 * SetCursorPos (USER.70)
828 void SetCursorPos( short x, short y )
830 dprintf_cursor( stddeb, "SetCursorPos: x=%d y=%d\n", x, y );
831 XWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, x, y );
835 /***********************************************************************
836 * ShowCursor (USER.71)
838 int ShowCursor( BOOL bShow )
840 dprintf_cursor( stddeb, "ShowCursor: %d, count=%d\n",
841 bShow, CURSOR_ShowCount );
843 if (bShow)
845 if (++CURSOR_ShowCount == 0)
846 CURSORICON_SetCursor( hActiveCursor ); /* Show it */
848 else
850 if (--CURSOR_ShowCount == -1)
851 CURSORICON_SetCursor( 0 ); /* Hide it */
853 return CURSOR_ShowCount;
857 /***********************************************************************
858 * GetCursor (USER.247)
860 HCURSOR GetCursor(void)
862 return hActiveCursor;
866 /***********************************************************************
867 * ClipCursor16 (USER.16)
869 BOOL16 ClipCursor16( const RECT16 *rect )
871 if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
872 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
873 return TRUE;
877 /***********************************************************************
878 * ClipCursor32 (USER32.52)
880 BOOL32 ClipCursor32( const RECT32 *rect )
882 if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
883 else CopyRect32( &CURSOR_ClipRect, rect );
884 return TRUE;
888 /***********************************************************************
889 * GetCursorPos16 (USER.17)
891 void GetCursorPos16( POINT16 *pt )
893 Window root, child;
894 int rootX, rootY, childX, childY;
895 unsigned int mousebut;
897 if (!pt) return;
898 if (!XQueryPointer( display, rootWindow, &root, &child,
899 &rootX, &rootY, &childX, &childY, &mousebut ))
900 pt->x = pt->y = 0;
901 else
903 pt->x = rootX + desktopX;
904 pt->y = rootY + desktopY;
906 dprintf_cursor(stddeb, "GetCursorPos: ret=%d,%d\n", pt->x, pt->y );
910 /***********************************************************************
911 * GetCursorPos32 (USER32.228)
913 void GetCursorPos32( POINT32 *pt )
915 POINT16 pt16;
916 GetCursorPos16( &pt16 );
917 if (pt) CONV_POINT16TO32( &pt16, pt );
921 /***********************************************************************
922 * GetClipCursor16 (USER.309)
924 void GetClipCursor16( RECT16 *rect )
926 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
930 /***********************************************************************
931 * GetClipCursor32 (USER32.220)
933 void GetClipCursor32( RECT32 *rect )
935 if (rect) CopyRect32( rect, &CURSOR_ClipRect );
939 /**********************************************************************
940 * GetIconID (USER.455)
942 WORD GetIconID( HANDLE hResource, DWORD resType )
944 CURSORICONDIR *lpDir = LockResource(hResource);
946 if (!lpDir || lpDir->idReserved ||
947 ((lpDir->idType != 1) && (lpDir->idType != 2)))
949 dprintf_cursor(stddeb,"GetIconID: invalid resource directory\n");
950 return 0;
953 dprintf_cursor( stddeb, "GetIconID: hRes=%04x, entries=%i\n",
954 hResource, lpDir->idCount );
956 switch(resType)
958 case 1: /* cursor */
960 CURSORDIRENTRY *entry = CURSORICON_FindBestCursor( lpDir,
961 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR );
962 return entry ? entry->wResId : 0;
964 case 3: /* icon */
966 ICONDIRENTRY *entry = CURSORICON_FindBestIcon( lpDir,
967 SYSMETRICS_CXICON, SYSMETRICS_CYICON,
968 MIN( 16, 1 << screenDepth ) );
969 return entry ? entry->wResId : 0;
972 fprintf( stderr, "GetIconID: invalid res type %ld\n", resType );
973 return 0;
977 /**********************************************************************
978 * LoadIconHandler (USER.456)
980 HICON LoadIconHandler( HANDLE hResource, BOOL bNew )
982 dprintf_cursor(stddeb,"LoadIconHandler: hRes=%04x\n",hResource);
984 if( !bNew )
986 fprintf(stdnimp,"LoadIconHandler: 2.xx resources are not supported\n");
987 return 0;
989 return CURSORICON_LoadHandler( hResource, 0, FALSE);