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