Release 960717
[wine/multimedia.git] / objects / cursoricon.c
blobc750d6143b66fa821f1bc8c94299f2c46118ab4b
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(HINSTANCE32 hInstance, SEGPTR name,
202 int width, int height, int colors,
203 BOOL fCursor, CURSORICONDIRENTRY *dirEntry)
205 HRSRC16 hRsrc;
206 HGLOBAL16 hMem;
207 CURSORICONDIR *dir;
208 CURSORICONDIRENTRY *entry = NULL;
210 if (!(hRsrc = FindResource16( hInstance, name,
211 fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON )))
212 return FALSE;
213 if (!(hMem = LoadResource16( hInstance, hRsrc ))) return FALSE;
214 if ((dir = (CURSORICONDIR *)LockResource16( 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 FreeResource16( 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 *)LockResource16( handle );
249 hotspot = *pt;
250 bmi = (BITMAPINFO *)(pt + 1);
252 else bmi = (BITMAPINFO *)LockResource16( 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 = FindResource16( hInstance,
395 MAKEINTRESOURCE( dirEntry.icon.wResId ),
396 fCursor ? RT_CURSOR : RT_ICON ))) return 0;
397 if (!(handle = LoadResource16( hInstance, hRsrc ))) return 0;
399 hRet = CURSORICON_LoadHandler( handle, hInstance, fCursor );
400 FreeResource16(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 HCURSOR16 LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
468 if (HIWORD(name))
469 dprintf_cursor( stddeb, "LoadCursor16: %04x '%s'\n",
470 hInstance, (char *)PTR_SEG_TO_LIN( name ) );
471 else
472 dprintf_cursor( stddeb, "LoadCursor16: %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 HICON16 LoadIcon16(HINSTANCE16 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 * 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 /***********************************************************************
607 * DrawIcon (USER.84)
609 BOOL DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
611 CURSORICONINFO *ptr;
612 HDC hMemDC;
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 );
633 DeleteDC( hMemDC );
634 if (hXorBits) DeleteObject( hXorBits );
635 if (hAndBits) DeleteObject( hAndBits );
636 GlobalUnlock16( hIcon );
637 SetTextColor( hdc, oldFg );
638 SetBkColor( hdc, oldBg );
639 return TRUE;
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;
652 if (!info) return 0;
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;
670 XColor fg, bg;
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 );
679 if (pixmapBits)
681 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
682 &bg, &bg, 0, 0 );
683 XFreePixmap( display, pixmapBits );
686 else /* Create the X cursor from the bits */
688 CURSORICONINFO *ptr;
689 XImage *image;
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 );
695 return FALSE;
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);
705 if (image)
707 extern void _XInitImageFuncPtrs( XImage* );
708 image->byte_order = MSBFirst;
709 image->bitmap_bit_order = MSBFirst;
710 image->bitmap_unit = 16;
711 _XInitImageFuncPtrs(image);
712 if (pixmapAll)
713 CallTo32_LargeStack( XPutImage, 10,
714 display, pixmapAll, BITMAP_monoGC, image,
715 0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
716 image->data = NULL;
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
736 * arithmetic:
738 * Windows | X11
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
745 * which gives:
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 );
787 else
789 /* Set the same cursor for all top-level windows */
790 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
791 while(hwnd)
793 Window win = WIN_GetXWindow( hwnd );
794 if (win) XDefineCursor( display, win, cursor );
795 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
798 return TRUE;
802 /***********************************************************************
803 * SetCursor (USER.69)
805 HCURSOR SetCursor( HCURSOR hCursor )
807 HCURSOR hOldCursor;
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 );
815 return hOldCursor;
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 );
837 if (bShow)
839 if (++CURSOR_ShowCount == 0)
840 CURSORICON_SetCursor( hActiveCursor ); /* Show it */
842 else
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 );
867 return TRUE;
871 /***********************************************************************
872 * ClipCursor32 (USER32.52)
874 BOOL32 ClipCursor32( const RECT32 *rect )
876 if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
877 else CopyRect32( &CURSOR_ClipRect, rect );
878 return TRUE;
882 /***********************************************************************
883 * GetCursorPos16 (USER.17)
885 void GetCursorPos16( POINT16 *pt )
887 Window root, child;
888 int rootX, rootY, childX, childY;
889 unsigned int mousebut;
891 if (!pt) return;
892 if (!XQueryPointer( display, rootWindow, &root, &child,
893 &rootX, &rootY, &childX, &childY, &mousebut ))
894 pt->x = pt->y = 0;
895 else
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 )
909 POINT16 pt16;
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 = LockResource16(hResource);
940 if (!lpDir || lpDir->idReserved ||
941 ((lpDir->idType != 1) && (lpDir->idType != 2)))
943 dprintf_cursor(stddeb,"GetIconID: invalid resource directory\n");
944 return 0;
947 dprintf_cursor( stddeb, "GetIconID: hRes=%04x, entries=%i\n",
948 hResource, lpDir->idCount );
950 switch(resType)
952 case 1: /* cursor */
954 CURSORDIRENTRY *entry = CURSORICON_FindBestCursor( lpDir,
955 SYSMETRICS_CXCURSOR, SYSMETRICS_CYCURSOR );
956 return entry ? entry->wResId : 0;
958 case 3: /* icon */
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 );
967 return 0;
971 /**********************************************************************
972 * LoadIconHandler (USER.456)
974 HICON LoadIconHandler( HANDLE hResource, BOOL bNew )
976 dprintf_cursor(stddeb,"LoadIconHandler: hRes=%04x\n",hResource);
978 if( !bNew )
980 fprintf(stdnimp,"LoadIconHandler: 2.xx resources are not supported\n");
981 return 0;
983 return CURSORICON_LoadHandler( hResource, 0, FALSE);