source: trunk/src/user32/winicon.cpp@ 6183

Last change on this file since 6183 was 6183, checked in by sandervl, 24 years ago

use 16 bits handles for icons & cursors

File size: 58.8 KB
Line 
1/* $Id: winicon.cpp,v 1.26 2001-07-06 13:47:00 sandervl Exp $ */
2/*
3 * Win32 Icon Code for OS/2
4 *
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) (OS/2 Port)
7 *
8 * Based on Wine code (objects\bitmap.c, loader\resource.c, objects\cursoricon.c):
9 *
10 * Copyright 1993 Alexandre Julliard
11 * 1993 Robert J. Amstadt
12 * 1996 Martin Von Loewis
13 * 1997 Alex Korobka
14 * 1998 Turchanov Sergey
15 * 1998 Huw D M Davies
16 * Theory:
17 *
18 * http://www.microsoft.com/win32dev/ui/icons.htm
19 *
20 * Cursors and icons are stored in a global heap block, with the
21 * following layout:
22 *
23 * CURSORICONINFO info;
24 * BYTE[] ANDbits;
25 * BYTE[] XORbits;
26 *
27 * The bits structures are in the format of a device-dependent bitmap.
28 *
29 * This layout is very sub-optimal, as the bitmap bits are stored in
30 * the X client instead of in the server like other bitmaps; however,
31 * some programs (notably Paint Brush) expect to be able to manipulate
32 * the bits directly :-(
33 *
34 * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
35 *
36 **************************************************************************************************
37 *
38 * TODO: Scaling of system cursors (store them as resources in user32 instead of using PM pointers)
39 * TODO: We use the hColorBmp member of the CURSORICONINFO structure to store the PM cursor handle
40 * Might mess up PaintBrush (see above)
41 *
42 * Project Odin Software License can be found in LICENSE.TXT
43 *
44 */
45#include <os2win.h>
46#include <stdio.h>
47#include <string.h>
48#include <winicon.h>
49#include <win\cursoricon.h>
50#include <objhandle.h>
51#include "dib.h"
52#include <heapstring.h>
53#include <win\virtual.h>
54#include "initterm.h"
55#include "oslibres.h"
56#include "oslibwin.h"
57#include "dc.h"
58
59#define DBG_LOCALLOG DBG_winicon
60#include "dbglocal.h"
61
62
63/**********************************************************************
64 * ICONCACHE for cursors/icons loaded with LR_SHARED.
65 *
66 * FIXME: This should not be allocated on the system heap, but on a
67 * subsystem-global heap (i.e. one for all Win16 processes,
68 * and one for each Win32 process).
69 */
70typedef struct tagICONCACHE
71{
72 struct tagICONCACHE *next;
73
74 HMODULE hModule;
75 HRSRC hRsrc;
76 HRSRC hGroupRsrc;
77 HANDLE handle;
78 INT count;
79} ICONCACHE;
80
81static ICONCACHE *IconAnchor = NULL;
82static CRITICAL_SECTION IconCrst = CRITICAL_SECTION_INIT;
83static WORD ICON_HOTSPOT = 0x4242;
84static HCURSOR hActiveCursor = 0;
85static HCURSOR hActiveCursorPM = 0;
86
87
88static HGLOBAL CURSORICON_CreateFromResource( HINSTANCE hInstance, DWORD dwResGroupId, HGLOBAL hObj, LPBYTE bits,
89 UINT cbSize, BOOL bIcon, DWORD dwVersion, INT width, INT height, UINT loadflags );
90static HGLOBAL CURSORICON_Copy( HGLOBAL handle );
91static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
92 int height, int colors );
93static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
94 int width, int height, int color);
95BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
96 CURSORICONDIR **res, LPBYTE **ptr);
97
98static INT CURSORICON_DelSharedIcon( HANDLE handle );
99static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle );
100static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc );
101static ICONCACHE* CURSORICON_FindCache(HANDLE handle);
102
103/***********************************************************************
104 * CreateIcon (USER32.75)
105 */
106HICON WIN32API CreateIcon(HINSTANCE hInstance, INT nWidth,
107 INT nHeight, BYTE bPlanes, BYTE bBitsPixel,
108 LPCVOID lpANDbits, LPCVOID lpXORbits )
109{
110 CURSORICONINFO info;
111
112 dprintf(("USER32: CreateIcon (%d,%d), %d, %x, %x", nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits));
113
114 info.ptHotSpot.x = ICON_HOTSPOT;
115 info.ptHotSpot.y = ICON_HOTSPOT;
116 info.nWidth = nWidth;
117 info.nHeight = nHeight;
118 info.nWidthBytes = 0;
119 info.bPlanes = bPlanes;
120 info.bBitsPerPixel = bBitsPixel;
121 info.hInstance = hInstance;
122 info.dwResGroupId = -1;
123 info.hColorBmp = 0;
124 return CreateCursorIconIndirect(0, &info, lpANDbits, lpXORbits);
125}
126/**********************************************************************
127 * CreateIconFromResource (USER32.76)
128 */
129HICON WIN32API CreateIconFromResource(LPBYTE bits, UINT cbSize,
130 BOOL bIcon, DWORD dwVersion)
131{
132 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
133}
134//******************************************************************************
135//******************************************************************************
136HICON WIN32API CreateIconFromResourceEx(LPBYTE bits, UINT cbSize,
137 BOOL bIcon, DWORD dwVersion,
138 INT width, INT height,
139 UINT cFlag )
140{
141 dprintf(("USER32: CreateIconFromResourceEx %X %d %d %X %d %d %X,", bits, cbSize, bIcon, dwVersion, width, height, cFlag));
142 return CURSORICON_CreateFromResource(0, -1, 0, bits, cbSize, bIcon, dwVersion, width, height, cFlag );
143}
144/**********************************************************************
145 * CreateIconIndirect (USER32.78)
146 */
147HICON WINAPI CreateIconIndirect(ICONINFO *iconinfo)
148{
149 BITMAP bmpXor,bmpAnd;
150 HICON hObj;
151 int sizeXor,sizeAnd;
152
153 dprintf(("USER32: CreateIconIndirect %x", iconinfo));
154
155 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
156 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
157
158 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
159 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
160
161 hObj = GlobalAlloc( GMEM_MOVEABLE, sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
162 if (hObj)
163 {
164 CURSORICONINFO *info;
165
166 info = (CURSORICONINFO *)GlobalLock( hObj );
167
168 /* If we are creating an icon, the hotspot is unused */
169 if (iconinfo->fIcon)
170 {
171 info->ptHotSpot.x = ICON_HOTSPOT;
172 info->ptHotSpot.y = ICON_HOTSPOT;
173 }
174 else
175 {
176 info->ptHotSpot.x = iconinfo->xHotspot;
177 info->ptHotSpot.y = iconinfo->yHotspot;
178 }
179
180 info->nWidth = bmpXor.bmWidth;
181 info->nHeight = bmpXor.bmHeight;
182 info->nWidthBytes = bmpXor.bmWidthBytes;
183 info->bPlanes = bmpXor.bmPlanes;
184 info->bBitsPerPixel = bmpXor.bmBitsPixel;
185 info->hInstance = -1;
186 info->dwResGroupId = -1;
187 info->hColorBmp = 0;
188
189 /* Transfer the bitmap bits to the CURSORICONINFO structure */
190 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
191 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
192 GlobalUnlock(hObj);
193
194#ifdef __WIN32OS2__
195 HICON hIcon;
196 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, USEROBJ_CURSORICON) == FALSE) {
197 GlobalFree(hObj);
198 dprintf(("ERROR: CreateIconIndirect ObjAllocateHandle failed!!"));
199 return 0;
200 }
201#endif
202 return hIcon;
203 }
204 else {
205 dprintf(("ERROR: CreateIconIndirect GlobalAlloc failed!!"));
206 return 0;
207 }
208}
209//******************************************************************************
210//******************************************************************************
211BOOL WIN32API DestroyIcon( HICON hIcon)
212{
213 dprintf(("USER32: DestroyIcon %x", hIcon));
214 return CURSORICON_Destroy( hIcon, 0 );
215}
216//******************************************************************************
217//******************************************************************************
218HICON WIN32API CopyIcon( HICON hIcon)
219{
220 dprintf(("USER32: CopyIcon %x", hIcon));
221 return CURSORICON_Copy( hIcon );
222}
223//******************************************************************************
224//******************************************************************************
225HICON WIN32API GetOS2Icon(HICON hIcon)
226{
227 CURSORICONINFO *ciconinfo;
228
229 hIcon = ObjGetHandleData(hIcon, USEROBJ_CURSORICON);
230 if(hIcon == -1) {
231 dprintf(("ERROR: Invalid cursor/icon!"));
232 return 0;
233 }
234 ciconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
235 if (!ciconinfo)
236 return 0;
237 HICON hOS2Icon = ciconinfo->hColorBmp;
238 GlobalUnlock(hIcon);
239 return hOS2Icon;
240}
241/**********************************************************************
242 * GetIconInfo (USER32.242)
243 */
244BOOL WINAPI GetIconInfo(HICON hIcon, ICONINFO *iconinfo)
245{
246 CURSORICONINFO *ciconinfo;
247
248 dprintf(("GetIconInfo %x %x", hIcon, iconinfo));
249
250#ifdef __WIN32OS2__
251 hIcon = ObjGetHandleData(hIcon, USEROBJ_CURSORICON);
252 if(hIcon == -1) {
253 dprintf(("ERROR: Invalid cursor/icon!"));
254 return 0;
255 }
256#endif
257 ciconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
258 if (!ciconinfo)
259 return FALSE;
260
261 if((ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
262 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT))
263 {
264 iconinfo->fIcon = TRUE;
265 iconinfo->xHotspot = ciconinfo->nWidth / 2;
266 iconinfo->yHotspot = ciconinfo->nHeight / 2;
267 }
268 else
269 {
270 iconinfo->fIcon = FALSE;
271 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
272 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
273 }
274
275 //Create new bitmaps for the color and mask data; application is responsible
276 //for deleteing them (according to docs & verified in NT4)
277 if(ciconinfo->bBitsPerPixel > 1)
278 {
279 BITMAPINFO* pInfo;
280 int colorsize = 0;
281 int coloroff;
282
283 HDC hdc = CreateCompatibleDC(0);
284
285 if(ciconinfo->bBitsPerPixel <= 8) {
286 colorsize = (1<<ciconinfo->bBitsPerPixel)*sizeof(RGBQUAD);
287 }
288 else {
289 colorsize = 3*sizeof(DWORD); //color masks
290 }
291 pInfo = (BITMAPINFO *)malloc(ciconinfo->nHeight * ciconinfo->nWidthBytes + colorsize + sizeof(BITMAPINFO));
292 memset(pInfo, 0, sizeof(BITMAPINFO)+colorsize);
293
294 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
295 pInfo->bmiHeader.biWidth = ciconinfo->nWidth;
296 pInfo->bmiHeader.biHeight = ciconinfo->nHeight,
297 pInfo->bmiHeader.biPlanes = ciconinfo->bPlanes;
298 pInfo->bmiHeader.biBitCount = ciconinfo->bBitsPerPixel;
299 pInfo->bmiHeader.biSizeImage= ciconinfo->nHeight * ciconinfo->nWidthBytes;
300
301 //offset in cursorinfo memory
302 coloroff = ciconinfo->nHeight * BITMAP_GetWidthBytes (ciconinfo->nWidth, 1);
303
304 char *src = (char *)(ciconinfo + 1) + coloroff;
305 if(ciconinfo->bBitsPerPixel <= 8) {
306 src += colorsize; //no color masks in cursorinfo data for bpp > 8
307 }
308 if(ciconinfo->bBitsPerPixel <= 8) {
309 memcpy(&pInfo->bmiColors[0], (char *)(ciconinfo + 1) + coloroff, colorsize);
310 }
311 //else TODO: color masks (currently unused in CreateDIBitmap)
312
313 iconinfo->hbmColor = CreateDIBitmap(hdc, &pInfo->bmiHeader, CBM_INIT, src, pInfo, DIB_RGB_COLORS);
314
315 free(pInfo);
316 DeleteDC(hdc);
317 }
318 else {
319 iconinfo->hbmColor = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
320 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
321 (char *)(ciconinfo + 1)
322 + ciconinfo->nHeight *
323 BITMAP_GetWidthBytes (ciconinfo->nWidth, 1) );
324 }
325
326 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
327 1, 1, (char *)(ciconinfo + 1));
328
329 GlobalUnlock(hIcon);
330
331 return TRUE;
332}
333//******************************************************************************
334//******************************************************************************
335HCURSOR WIN32API CreateCursor(HINSTANCE hInst, int xHotSpot, int yHotSpot, int nWidth, int nHeight,
336 const VOID *lpANDbits, const VOID *lpXORbits)
337{
338 CURSORICONINFO info;
339
340 dprintf(("CreateCursor %dx%d spot=%d,%d xor=%p and=%p\n",
341 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits));
342
343 info.ptHotSpot.x = xHotSpot;
344 info.ptHotSpot.y = yHotSpot;
345 info.nWidth = nWidth;
346 info.nHeight = nHeight;
347 info.nWidthBytes = 0;
348 info.bPlanes = 1;
349 info.bBitsPerPixel = 1;
350 info.hInstance = hInst;
351 info.dwResGroupId = -1;
352 info.hColorBmp = 0;
353
354 return CreateCursorIconIndirect( 0, &info, lpANDbits, lpXORbits );
355}
356//******************************************************************************
357//******************************************************************************
358BOOL WIN32API DestroyCursor( HCURSOR hCursor)
359{
360 dprintf(("USER32: DestroyCursor %x", hCursor));
361 return CURSORICON_Destroy( hCursor, CID_WIN32 );
362}
363//******************************************************************************
364//******************************************************************************
365HCURSOR WIN32API GetCursor(void)
366{
367 dprintf2(("USER32: GetCursor"));
368 if(hActiveCursorPM && hActiveCursorPM != OSLibWinQueryPointer()) {
369 dprintf(("Another app changed mouse cursor"));
370 hActiveCursorPM = hActiveCursor = 0;
371 }
372 return hActiveCursor;
373}
374//******************************************************************************
375//******************************************************************************
376HCURSOR WIN32API SetCursor( HCURSOR hCursor)
377{
378 HCURSOR hOldCursor;
379
380 dprintf(("USER32: SetCursor %x (prev %x)", hCursor, hActiveCursor));
381 GetCursor();
382 if (hCursor == hActiveCursor) return hActiveCursor; /* No change */
383
384 hOldCursor = hActiveCursor;
385 hActiveCursor = hCursor;
386
387#ifdef __WIN32OS2__
388 hCursor = ObjGetHandleData(hCursor, USEROBJ_CURSORICON);
389 if(hCursor == -1) {
390 dprintf(("ERROR: Invalid cursor/icon!"));
391 return 0;
392 }
393#endif
394
395 CURSORICONINFO *iconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hCursor);
396 if (!iconinfo) {
397 dprintf(("ERROR: Invalid cursor!"));
398 return 0;
399 }
400 if(!iconinfo->hColorBmp) {
401 dprintf(("SetCursor: invalid os/2 pointer handle!!"));
402 }
403
404 if(OSLibWinSetPointer(iconinfo->hColorBmp) == FALSE) {
405 dprintf(("OSLibWinSetPointer %x returned FALSE!!", iconinfo->hColorBmp));
406 }
407 hActiveCursorPM = iconinfo->hColorBmp;
408 GlobalUnlock(hCursor);
409
410 return hOldCursor;
411}
412//******************************************************************************
413//******************************************************************************
414BOOL WIN32API GetCursorPos( PPOINT lpPoint)
415{
416 dprintf2(("USER32: GetCursorPos %x", lpPoint));
417
418 if (!lpPoint) return FALSE;
419
420 if (OSLibWinQueryPointerPos(lpPoint)) //POINT == POINTL
421 {
422 mapScreenPoint((OSLIBPOINT*)lpPoint);
423 return TRUE;
424 }
425 else return FALSE;
426}
427//******************************************************************************
428//******************************************************************************
429BOOL WIN32API SetCursorPos( int X, int Y)
430{
431 dprintf(("USER32: SetCursorPos %d %d", X,Y));
432 return OSLibWinSetPointerPos(X, mapScreenY(Y));
433}
434//******************************************************************************
435//******************************************************************************
436BOOL WIN32API ClipCursor(const RECT * lpRect)
437{
438 if(lpRect) {
439 dprintf(("USER32: ClipCursor (%d,%d)(%d,%d)", lpRect->left, lpRect->top, lpRect->right, lpRect->bottom));
440 }
441 else dprintf(("USER32: ClipCursor NULL"));
442 return OSLibWinClipCursor(lpRect);
443}
444//******************************************************************************
445//******************************************************************************
446BOOL WIN32API GetClipCursor( LPRECT lpRect)
447{
448 dprintf(("USER32: GetClipCursor %x", lpRect));
449 return OSLibWinGetClipCursor(lpRect);
450}
451//******************************************************************************
452//******************************************************************************
453/*****************************************************************************
454 * Name : BOOL WIN32API SetSystemCursor
455 * Purpose : The SetSystemCursor function replaces the contents of the system
456 * cursor specified by dwCursorId with the contents of the cursor
457 * specified by hCursor, and then destroys hCursor. This function
458 * lets an application customize the system cursors.
459 * Parameters: HCURSOR hCursor set specified system cursor to this cursor's
460 * contents, then destroy this
461 * DWORD dwCursorID system cursor specified by its identifier
462 * Variables :
463 * Result : If the function succeeds, the return value is TRUE.
464 * If the function fails, the return value is FALSE. To get extended
465 * error information, call GetLastError.
466 * Remark :
467 * Status : UNTESTED STUB
468 *
469 * Author : Patrick Haller [Thu, 1998/02/26 11:55]
470 *****************************************************************************/
471BOOL WIN32API SetSystemCursor(HCURSOR hCursor, DWORD dwCursorId)
472{
473 dprintf(("USER32:SetSystemCursor (%08xh,%08x) not supported.\n",
474 hCursor,
475 dwCursorId));
476
477 return DestroyCursor(hCursor);
478}
479//******************************************************************************
480//******************************************************************************
481INT OPEN32API __ShowCursor(BOOL bShow);
482
483inline INT _ShowCursor (BOOL bShow)
484{
485 INT yyrc;
486 USHORT sel = RestoreOS2FS();
487
488 yyrc = __ShowCursor(bShow);
489 SetFS(sel);
490
491 return yyrc;
492}
493//******************************************************************************
494static int cursorshowcnt = 0;
495//******************************************************************************
496void RestoreCursor()
497{
498 dprintf2(("USER32: RestoreCursor %d", cursorshowcnt));
499 while(cursorshowcnt != 0)
500 {
501 if(cursorshowcnt > 0 )
502 {
503 ShowCursor(FALSE);
504 }
505 else
506 {
507 ShowCursor(TRUE);
508 }
509 }
510}
511//******************************************************************************
512//******************************************************************************
513int WIN32API ShowCursor(BOOL bShow)
514{
515 dprintf2(("USER32: ShowCursor %d", bShow));
516 cursorshowcnt = cursorshowcnt + ((bShow) ? 1 : -1);
517 return _ShowCursor(bShow);
518}
519/***********************************************************************
520 * CreateCursorIconIndirect
521 */
522HGLOBAL WIN32API CreateCursorIconIndirect( HINSTANCE hInstance,
523 CURSORICONINFO *info,
524 LPCVOID lpANDbits,
525 LPCVOID lpXORbits )
526{
527 HGLOBAL handle;
528 char *ptr;
529 int sizeAnd, sizeXor;
530
531 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
532 info->nWidthBytes = BITMAP_GetWidthBytes(info->nWidth,info->bBitsPerPixel);
533 sizeXor = info->nHeight * info->nWidthBytes;
534 sizeAnd = info->nHeight * BITMAP_GetWidthBytes( info->nWidth, 1 );
535 if (!(handle = GlobalAlloc( GMEM_MOVEABLE,
536 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
537 return 0;
538 ptr = (char *)GlobalLock( handle );
539 memcpy( ptr, info, sizeof(*info) );
540 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
541 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
542 GlobalUnlock( handle );
543
544#ifdef __WIN32OS2__
545 HICON hIcon;
546 if(ObjAllocateHandle(&hIcon, (DWORD)handle, USEROBJ_CURSORICON) == FALSE) {
547 GlobalFree(handle);
548 dprintf(("ERROR: CreateCursorIconIndirect ObjAllocateHandle failed!!"));
549 return 0;
550 }
551 return hIcon;
552#else
553 return handle;
554#endif
555}
556/**********************************************************************
557 * CURSORICON_Load
558 *
559 * Load a cursor or icon from resource or file.
560 */
561HGLOBAL CURSORICON_Load( HINSTANCE hInstance, LPCWSTR name,
562 INT width, INT height, INT colors,
563 BOOL fCursor, UINT loadflags )
564{
565 HANDLE handle = 0, h = 0;
566 HANDLE hRsrc;
567 CURSORICONDIR *dir;
568 CURSORICONDIRENTRY *dirEntry;
569 LPBYTE bits;
570
571#ifdef __WIN32OS2__
572 //TODO: Can system cursors be loaded by name??? (#xxx)
573 if (fCursor && hInstance == NULL && !HIWORD(name))
574 {
575 HCURSOR hCursor = OSLibWinQuerySysPointer((ULONG)name, width, height);
576 if(hCursor)
577 {
578 /* If shared icon, check whether it was already loaded */
579 if ((loadflags & LR_SHARED)
580 && (h = CURSORICON_FindSharedIcon( -1, hCursor ) ) != 0 )
581 {
582 dprintf(("Found icon/cursor in cache; returned old handle %x", h));
583 return h;
584 }
585
586 HANDLE hObj = GlobalAlloc( GMEM_MOVEABLE, sizeof(CURSORICONINFO));
587 if (!hObj)
588 {
589 DebugInt3();
590 return 0;
591 }
592 CURSORICONINFO *info;
593
594 info = (CURSORICONINFO *)GlobalLock( hObj );
595 info->ptHotSpot.x = 0;
596 info->ptHotSpot.y = 0;
597 info->nWidth = width;
598 info->nHeight = height;
599 info->nWidthBytes = width*height/8;
600 info->bPlanes = 1;
601 info->bBitsPerPixel = 1;
602 info->hColorBmp = hCursor;
603 info->hInstance = -1;
604 info->dwResGroupId = -1;
605
606 HICON hIcon;
607 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, USEROBJ_CURSORICON) == FALSE) {
608 GlobalUnlock( hObj );
609 GlobalFree(hObj);
610 dprintf(("ERROR: CURSORICON_Load ObjAllocateHandle failed!!"));
611 return 0;
612 }
613
614 if (loadflags & LR_SHARED )
615 CURSORICON_AddSharedIcon( -1, hIcon, -1, hObj );
616
617 GlobalUnlock( hObj );
618
619 return hIcon;
620 }
621 }
622#endif
623 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
624 {
625 LPBYTE *ptr;
626
627 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
628 return 0;
629 if (fCursor)
630 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
631 else
632 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
633 bits = ptr[dirEntry->wResId-1];
634 h = CURSORICON_CreateFromResource( 0, -1, 0, bits, dirEntry->dwBytesInRes,
635 !fCursor, 0x00030000, width, height, loadflags);
636
637 HeapFree( GetProcessHeap(), 0, dir );
638 HeapFree( GetProcessHeap(), 0, ptr );
639 }
640 else /* Load from resource */
641 {
642 HANDLE hGroupRsrc;
643 WORD wResId;
644 DWORD dwBytesInRes;
645 BOOL bIsGroup = TRUE;
646
647 /* Get directory resource ID */
648 if (!hInstance)
649 {
650 hRsrc = FindResourceW(hInstanceUser32, name, fCursor ? RT_CURSORW : RT_ICONW);
651 if(!hRsrc) {
652 hRsrc = FindResourceW(hInstanceUser32, name, fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW);
653 }
654 else bIsGroup = FALSE;
655
656 if(!hRsrc) return 0;
657
658 hInstance = hInstanceUser32;
659 }
660 else {
661 hRsrc = FindResourceW(hInstance, name, fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW);
662 if(!hRsrc) return 0;
663 }
664 hGroupRsrc = hRsrc;
665
666 if(bIsGroup) {
667 /* Find the best entry in the directory */
668
669 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
670 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
671
672 if (fCursor)
673 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
674 width, height, 1);
675 else
676 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
677 width, height, colors );
678 if (!dirEntry) return 0;
679 wResId = dirEntry->wResId;
680 dwBytesInRes = dirEntry->dwBytesInRes;
681 FreeResource( handle );
682
683 /* Load the resource */
684 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
685 fCursor ? RT_CURSORW : RT_ICONW ))) return 0;
686 }
687
688 /* If shared icon, check whether it was already loaded */
689 if ((loadflags & LR_SHARED)
690 && (h = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
691 {
692 dprintf(("Found icon/cursor in cache; returned old handle %x", h));
693 return h;
694 }
695
696 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
697 bits = (LPBYTE)LockResource( handle );
698 h = CURSORICON_CreateFromResource( hInstance, (DWORD)name, 0, bits, dwBytesInRes,
699 !fCursor, 0x00030000, width, height, loadflags);
700 FreeResource( handle );
701
702 /* If shared icon, add to icon cache */
703
704 if ( h && (loadflags & LR_SHARED) )
705 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, h );
706
707 }
708
709 return h;
710}
711
712/*********************************************************************
713 * The main purpose of this function is to create fake resource directory
714 * and fake resource entries. There are several reasons for this:
715 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
716 * fields
717 * There are some "bad" cursor files which do not have
718 * bColorCount initialized but instead one must read this info
719 * directly from corresponding DIB sections
720 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
721 */
722BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
723 CURSORICONDIR **res, LPBYTE **ptr)
724{
725 LPBYTE _free;
726 CURSORICONFILEDIR *bits;
727 int entries, size, i;
728 HANDLE hMapping = 0;
729
730 *res = NULL;
731 *ptr = NULL;
732
733 hMapping = VIRTUAL_MapFileW( filename, (LPVOID *)&bits, TRUE);
734 if(hMapping == INVALID_HANDLE_VALUE)
735 return FALSE;
736
737 /* FIXME: test for inimated icons
738 * hack to load the first icon from the *.ani file
739 */
740 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
741 {
742 LPBYTE pos = (LPBYTE) bits;
743 dprintf(("Animated icons not correctly implemented! %p \n", bits));
744
745 for (;;)
746 {
747 if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
748 {
749 dprintf(("icon entry found! %p\n", bits));
750 pos+=4;
751 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
752 {
753 goto fail;
754 }
755 bits=(CURSORICONFILEDIR*)(pos+4);
756 dprintf(("icon size ok. offset=%p \n", bits));
757 break;
758 }
759 pos+=2;
760 if (pos>=(LPBYTE)bits+766) goto fail;
761 }
762 }
763 if (!(entries = bits->idCount)) goto fail;
764 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
765 _free = (LPBYTE) size;
766
767 for (i=0; i < entries; i++)
768 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
769
770 if (!(*ptr = (LPBYTE *)HeapAlloc( GetProcessHeap(), 0,
771 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
772 if (!(*res = (CURSORICONDIR *)HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
773
774 _free = (LPBYTE)(*res) + (int)_free;
775 memcpy((*res), bits, 6);
776 for (i=0; i<entries; i++)
777 {
778 ((LPBYTE*)(*ptr))[i] = _free;
779 if (fCursor) {
780 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
781 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
782 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
783 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
784 _free+=sizeof(POINT16);
785 }
786 else {
787 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
788 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
789 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
790 }
791 (*res)->idEntries[i].wPlanes=1;
792 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
793 bits->idEntries[i].dwDIBOffset))->biBitCount;
794 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
795 (*res)->idEntries[i].wResId=i+1;
796
797 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
798 (*res)->idEntries[i].dwBytesInRes);
799 _free += (*res)->idEntries[i].dwBytesInRes;
800 }
801 UnmapViewOfFile( bits );
802 CloseHandle(hMapping);
803 return TRUE;
804
805fail:
806 if (*res) HeapFree( GetProcessHeap(), 0, *res );
807 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
808
809 UnmapViewOfFile( bits );
810 CloseHandle(hMapping);
811 return FALSE;
812}
813
814/**********************************************************************
815 * CURSORICON_CreateFromResource
816 *
817 * Create a cursor or icon from in-memory resource template.
818 *
819 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
820 * with cbSize parameter as well.
821 */
822static HGLOBAL CURSORICON_CreateFromResource( HINSTANCE hInstance, DWORD dwResGroupId, HGLOBAL hObj, LPBYTE bits,
823 UINT cbSize, BOOL bIcon, DWORD dwVersion,
824 INT width, INT height, UINT loadflags )
825{
826 int sizeAnd, sizeXor;
827 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
828 BITMAP bmpXor, bmpAnd;
829 POINT16 hotspot;
830 BITMAPINFO *bmi;
831 HDC hdc = 0;
832 BOOL DoStretch;
833 INT size, colortablesize, bwsize, colorsize;
834
835 hotspot.x = ICON_HOTSPOT;
836 hotspot.y = ICON_HOTSPOT;
837
838//testestest
839 dprintf(("CURSORICON_CreateFromResource %x %x %x %x %d", hInstance, dwResGroupId, hObj, bits, cbSize));
840
841 if (dwVersion == 0x00020000)
842 {
843 dprintf(("CURSORICON_CreateFromResource 2.xx resources are not supported"));
844 return 0;
845 }
846
847 if (bIcon) {
848 bmi = (BITMAPINFO *)bits;
849 }
850 else /* get the hotspot */
851 {
852 POINT16 *pt = (POINT16 *)bits;
853 hotspot = *pt;
854 bmi = (BITMAPINFO *)(pt + 1);
855 }
856 size = DIB_BitmapInfoSize(bmi, DIB_RGB_COLORS);
857
858 if (!width) width = bmi->bmiHeader.biWidth;
859 if (!height) height = bmi->bmiHeader.biHeight/2;
860
861 DoStretch = (bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width);
862
863 colorsize = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2, bmi->bmiHeader.biBitCount);
864 bwsize = (bmi->bmiHeader.biWidth * bmi->bmiHeader.biHeight/2)/8;
865
866 /* Check bitmap header */
867 if((bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
868 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
869 bmi->bmiHeader.biCompression != BI_RGB) )
870 {
871 return 0;
872 }
873
874#ifdef __WIN32OS2__
875 if( (hdc = CreateCompatibleDC( 0 )) )
876#else
877 if( (hdc = GetDC( 0 )) )
878#endif
879 {
880 BITMAPINFO* pInfo;
881
882 /* Make sure we have room for the monochrome bitmap later on.
883 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
884 * up to and including the biBitCount. In-memory icon resource
885 * format is as follows:
886 *
887 * BITMAPINFOHEADER icHeader // DIB header
888 * RGBQUAD icColors[] // Color table
889 * BYTE icXOR[] // DIB bits for XOR mask
890 * BYTE icAND[] // DIB bits for AND mask
891 */
892
893 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
894 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
895 {
896 memcpy( pInfo, bmi, size );
897 pInfo->bmiHeader.biHeight /= 2;
898
899 /* Create the XOR bitmap */
900 if (DoStretch)
901 {
902 if(bIcon)
903 {
904 hXorBits = CreateCompatibleBitmap(hdc, width, height);
905 }
906 else
907 {
908 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
909 }
910 if(hXorBits)
911 {
912 HBITMAP hOld;
913 HDC hMem = CreateCompatibleDC(hdc);
914 BOOL res;
915
916 if (hMem) {
917 hOld = SelectObject(hMem, hXorBits);
918 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
919 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
920 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
921 SelectObject(hMem, hOld);
922 DeleteDC(hMem);
923 }
924 else res = FALSE;
925 if (!res) {
926 DeleteObject(hXorBits);
927 hXorBits = 0;
928 }
929 }
930 }
931 else
932 {
933 hXorBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
934 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
935 }
936 if( hXorBits )
937 {
938 char* xbits = (char *)bmi + size + DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
939 bmi->bmiHeader.biHeight,
940 bmi->bmiHeader.biBitCount) / 2;
941
942 pInfo->bmiHeader.biBitCount = 1;
943 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
944 {
945 RGBQUAD *rgb = pInfo->bmiColors;
946
947 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
948 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
949 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
950 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
951 }
952 else
953 {
954 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
955
956 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
957 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
958 }
959
960 /* Create the AND bitmap */
961 if (DoStretch)
962 {
963 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL)))
964 {
965 HBITMAP hOld;
966 HDC hMem = CreateCompatibleDC(hdc);
967 BOOL res;
968
969 if (hMem) {
970 hOld = SelectObject(hMem, hAndBits);
971//SvL: This also doesn't work as StretchDIBits doesn't handle 1bpp bitmaps correctly
972//--------->>> hack alert!
973#if 1
974 HBITMAP hBmp, hOld1;
975 HDC hMem1;
976
977 hMem1 = CreateCompatibleDC(hdc);
978
979 int linewidth = BITMAP_GetWidthBytes(pInfo->bmiHeader.biWidth, 1);
980
981 char *newpix = (char *)malloc(linewidth*pInfo->bmiHeader.biHeight);
982
983 newpix += ((pInfo->bmiHeader.biHeight-1)*linewidth);
984
985 if(cbSize - size - colorsize - bwsize == bwsize)
986 {//this means an AND and XOR mask is present (interleaved; and/xor)
987 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
988 memcpy(newpix, xbits, linewidth);
989 newpix -= linewidth;
990 xbits += linewidth*2;
991 }
992 }
993 else {
994 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
995 memcpy(newpix, xbits, linewidth);
996 newpix -= linewidth;
997 xbits += linewidth;
998 }
999 }
1000 newpix += linewidth;
1001 hBmp = CreateBitmap(pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, 1, 1, newpix);
1002 free(newpix);
1003
1004 hOld1 = SelectObject(hMem1, hBmp);
1005
1006 res = StretchBlt(hMem, 0, 0, width, height, hMem1, 0, 0, pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, SRCCOPY);
1007
1008 SelectObject(hMem1, hOld1);
1009 DeleteObject(hBmp);
1010 DeleteDC(hMem1);
1011
1012
1013#else
1014 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
1015 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1016 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1017#endif
1018 SelectObject(hMem, hOld);
1019 DeleteDC(hMem);
1020 }
1021 else res = FALSE;
1022 if (!res) {
1023 DeleteObject(hAndBits);
1024 hAndBits = 0;
1025 }
1026 }
1027 }
1028 else {
1029//SvL: Must use CreateBitmap here as CreateDIBitmap converts data to 8bpp (GetObjectA info -> 8 bpp)
1030#if 1
1031 int linewidth = BITMAP_GetWidthBytes(width, 1);
1032
1033 char *newpix = (char *)malloc(linewidth*height);
1034
1035 newpix += ((height-1)*linewidth);
1036
1037 if(cbSize - size - colorsize - bwsize == bwsize)
1038 {//this means an AND and XOR mask is present (interleaved; and/xor)
1039 for(int i=0;i<height;i++) {
1040 memcpy(newpix, xbits, linewidth);
1041 newpix -= linewidth;
1042 xbits += linewidth*2;
1043 }
1044 }
1045 else {
1046 for(int i=0;i<height;i++) {
1047 memcpy(newpix, xbits, linewidth);
1048 newpix -= linewidth;
1049 xbits += linewidth;
1050 }
1051 }
1052 newpix += linewidth;
1053 hAndBits = CreateBitmap(width, height, 1, 1, newpix);
1054
1055 free(newpix);
1056
1057#else
1058 hAndBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
1059 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
1060#endif
1061 }
1062 if( !hAndBits )
1063 DeleteObject( hXorBits );
1064 }
1065 HeapFree( GetProcessHeap(), 0, pInfo );
1066 }
1067#ifdef __WIN32OS2__
1068 DeleteDC(hdc );
1069#else
1070 ReleaseDC( 0, hdc );
1071#endif
1072 }
1073
1074 if( !hXorBits || !hAndBits )
1075 {
1076 dprintf(("\tunable to create an icon bitmap."));
1077 return 0;
1078 }
1079
1080 /* Now create the CURSORICONINFO structure */
1081 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1082 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1083 colortablesize = 0;
1084
1085 if(bmpXor.bmBitsPixel <= 8) {
1086 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
1087 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
1088 }
1089 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1090
1091 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1092
1093 if (hObj) hObj = GlobalReAlloc( hObj,
1094 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
1095 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
1096 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1097 if (hObj)
1098 {
1099 CURSORICONINFO *info;
1100
1101 info = (CURSORICONINFO *)GlobalLock( hObj );
1102 info->ptHotSpot.x = hotspot.x;
1103 info->ptHotSpot.y = hotspot.y;
1104 info->nWidth = bmpXor.bmWidth;
1105 info->nHeight = bmpXor.bmHeight;
1106 info->nWidthBytes = bmpXor.bmWidthBytes;
1107 info->bPlanes = bmpXor.bmPlanes;
1108 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1109 info->hInstance = hInstance;
1110 info->dwResGroupId = dwResGroupId;
1111 info->hColorBmp = 0;
1112
1113 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1114 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
1115
1116 if(bmpXor.bmBitsPixel > 1)
1117 {
1118 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
1119 HBITMAP oldbmp;
1120
1121 hdc = CreateCompatibleDC(0);
1122
1123 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
1124 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1125 pInfo->bmiHeader.biPlanes = info->bPlanes;
1126 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
1127
1128 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
1129 if(colortablesize) {
1130 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
1131 }
1132
1133 DeleteDC(hdc);
1134 free(pInfo);
1135 }
1136 else {
1137 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
1138 }
1139 info->hColorBmp = OSLibWinCreatePointer(info, (char *)(info + 1), (LPBITMAP_W)&bmpAnd, (char *)(info + 1) + sizeAnd, (LPBITMAP_W)&bmpXor, bIcon == FALSE);
1140 dprintf(("Create cursor/icon %x with OS/2 pointer handle %x", hObj, info->hColorBmp));
1141 GlobalUnlock( hObj );
1142 }
1143
1144 DeleteObject( hAndBits );
1145 DeleteObject( hXorBits );
1146
1147#ifdef __WIN32OS2__
1148 if(hObj) {
1149 HICON hIcon;
1150 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, USEROBJ_CURSORICON) == FALSE) {
1151 GlobalFree(hObj);
1152 dprintf(("ERROR: CURSORICON_Load ObjAllocateHandle failed!!"));
1153 return 0;
1154 }
1155 return hIcon;
1156 }
1157#endif
1158 return hObj;
1159}
1160
1161/**********************************************************************
1162 * CURSORICON_Destroy (USER.610)
1163 *
1164 * This routine is actually exported from Win95 USER under the name
1165 * DestroyIcon32 ... The behaviour implemented here should mimic
1166 * the Win95 one exactly, especially the return values, which
1167 * depend on the setting of various flags.
1168 */
1169WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
1170{
1171 WORD retv;
1172
1173 /* Check whether destroying active cursor */
1174
1175 if ( hActiveCursor == handle )
1176 {
1177 dprintf(("WARNING: Destroying active cursor!" ));
1178 SetCursor( 0 );
1179 }
1180
1181#ifdef __WIN32OS2__
1182 HICON hIcon = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1183 if(hIcon == -1) {
1184 dprintf(("ERROR: Invalid cursor/icon!"));
1185 return 0;
1186 }
1187#endif
1188
1189 /* Try shared cursor/icon first */
1190 if ( !(flags & CID_NONSHARED) )
1191 {
1192#ifdef __WIN32OS2__
1193 INT count = CURSORICON_DelSharedIcon( hIcon );
1194#else
1195 INT count = CURSORICON_DelSharedIcon( handle );
1196#endif
1197
1198 if ( count != -1 )
1199 return (flags & CID_WIN32)? TRUE : (count == 0);
1200
1201 /* FIXME: OEM cursors/icons should be recognized */
1202 }
1203 /* Now assume non-shared cursor/icon */
1204
1205#ifdef __WIN32OS2__
1206 CURSORICONINFO *iconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
1207 if (!iconinfo) {
1208 dprintf(("ERROR: Invalid cursor!"));
1209 return 0;
1210 }
1211
1212 if(iconinfo->hColorBmp) {
1213 OSLibWinDestroyPointer(iconinfo->hColorBmp);
1214 }
1215 GlobalUnlock(hIcon);
1216 retv = GlobalFree( hIcon );
1217 ObjFreeHandle(handle);
1218
1219 return (flags & CID_RESOURCE)? retv : TRUE;
1220#else
1221 retv = GlobalFree( handle );
1222 return (flags & CID_RESOURCE)? retv : TRUE;
1223#endif
1224}
1225
1226/***********************************************************************
1227 * CURSORICON_Copy
1228 *
1229 * Make a copy of a cursor or icon.
1230 */
1231static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
1232{
1233 char *ptrOld, *ptrNew;
1234 int size;
1235 HGLOBAL hNew;
1236
1237 handle = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1238 if(handle == -1) {
1239 dprintf(("ERROR: Invalid cursor/icon!"));
1240 return 0;
1241 }
1242
1243 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
1244
1245 size = GlobalSize( handle );
1246 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
1247#ifdef __WIN32OS2__
1248 if(hNew == NULL) {
1249 dprintf(("ERROR: CURSORICON_Copy GlobalAlloc failed!!"));
1250 return NULL;
1251 }
1252#endif
1253 ptrNew = (char *)GlobalLock( hNew );
1254 memcpy( ptrNew, ptrOld, size );
1255 GlobalUnlock( handle );
1256 GlobalUnlock( hNew );
1257
1258#ifdef __WIN32OS2__
1259 HICON hIcon;
1260 if(ObjAllocateHandle(&hIcon, (DWORD)hNew, USEROBJ_CURSORICON) == FALSE) {
1261 GlobalFree(hNew);
1262 dprintf(("ERROR: CURSORICON_Copy ObjAllocateHandle failed!!"));
1263 return 0;
1264 }
1265 return hIcon;
1266#else
1267 return hNew;
1268#endif
1269}
1270
1271/*************************************************************************
1272 * CURSORICON_ExtCopy
1273 *
1274 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1275 *
1276 * PARAMS
1277 * Handle [I] handle to an Image
1278 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1279 * iDesiredCX [I] The Desired width of the Image
1280 * iDesiredCY [I] The desired height of the Image
1281 * nFlags [I] The flags from CopyImage
1282 *
1283 * RETURNS
1284 * Success: The new handle of the Image
1285 *
1286 * NOTES
1287 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1288 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
1289 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1290 *
1291 *
1292 */
1293HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
1294 INT iDesiredCX, INT iDesiredCY,
1295 UINT nFlags)
1296{
1297 HGLOBAL hNew=0;
1298
1299#ifdef __WIN32OS2__
1300 HICON hIcon = ObjGetHandleData(Handle, USEROBJ_CURSORICON);
1301 if(hIcon == -1) {
1302 dprintf(("ERROR: Invalid cursor/icon!"));
1303 return 0;
1304 }
1305
1306#else
1307 if(Handle == 0)
1308 {
1309 return 0;
1310 }
1311#endif
1312 /* Best Fit or Monochrome */
1313 if( (nFlags & LR_COPYFROMRESOURCE
1314 && (iDesiredCX > 0 || iDesiredCY > 0))
1315 || nFlags & LR_MONOCHROME)
1316 {
1317#ifdef __WIN32OS2__
1318 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1319#else
1320 ICONCACHE* pIconCache = CURSORICON_FindCache(Handle);
1321#endif
1322
1323 /* Not Found in Cache, then do a straight copy
1324 */
1325 if(pIconCache == NULL)
1326 {
1327#ifdef __WIN32OS2__
1328 hNew = CURSORICON_Copy(Handle);
1329#else
1330 hNew = CURSORICON_Copy(0, Handle);
1331#endif
1332 if(nFlags & LR_COPYFROMRESOURCE)
1333 {
1334 dprintf(("WARNING: LR_COPYFROMRESOURCE: Failed to load from cache\n"));
1335 }
1336 }
1337 else
1338 {
1339 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1340 LPBYTE pBits;
1341 HANDLE hMem;
1342 HRSRC hRsrc;
1343 DWORD dwBytesInRes;
1344 WORD wResId;
1345 DWORD dwResGroupId;
1346 HINSTANCE hInstance;
1347 CURSORICONINFO *iconinfo;
1348 CURSORICONDIR *pDir;
1349 CURSORICONDIRENTRY *pDirEntry;
1350 BOOL bIsIcon = (nType == IMAGE_ICON);
1351
1352#ifdef __WIN32OS2__
1353 iconinfo = (CURSORICONINFO *)GlobalLock( hIcon );
1354#else
1355 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
1356#endif
1357 if(iconinfo == NULL) {
1358 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
1359 }
1360 hInstance = iconinfo->hInstance;
1361 dwResGroupId = iconinfo->dwResGroupId;
1362 GlobalUnlock( Handle );
1363 if(dwResGroupId == -1) {
1364 //todo: if scaling is necessary..
1365 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
1366 hNew = CURSORICON_Copy(Handle);
1367 return hNew;
1368 }
1369
1370 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1371 */
1372 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1373 || (iDesiredCX == 0 && iDesiredCY == 0))
1374 {
1375 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
1376 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
1377 }
1378
1379 /* Retreive the CURSORICONDIRENTRY
1380 */
1381 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
1382 if(!hRsrc) {
1383 goto notfound;
1384 }
1385
1386 if (!(hMem = LoadResource( hInstance, hRsrc)))
1387 {
1388 goto notfound;
1389 }
1390 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1391 {
1392 goto notfound;
1393 }
1394
1395 /* Find Best Fit
1396 */
1397 if(bIsIcon)
1398 {
1399 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1400 pDir, iDesiredCX, iDesiredCY, 256);
1401 }
1402 else
1403 {
1404 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1405 pDir, iDesiredCX, iDesiredCY, 1);
1406 }
1407
1408 wResId = pDirEntry->wResId;
1409 dwBytesInRes = pDirEntry->dwBytesInRes;
1410 FreeResource(hMem);
1411
1412 /* Get the Best Fit
1413 */
1414 if (!(hRsrc = FindResourceW(hInstance ,
1415 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
1416 {
1417 goto notfound;
1418 }
1419 if (!(hMem = LoadResource( hInstance, hRsrc )))
1420 {
1421 goto notfound;
1422 }
1423
1424 pBits = (LPBYTE)LockResource( hMem );
1425
1426 if(nFlags & LR_DEFAULTSIZE)
1427 {
1428 iTargetCY = GetSystemMetrics(SM_CYICON);
1429 iTargetCX = GetSystemMetrics(SM_CXICON);
1430 }
1431
1432 /* Create a New Icon with the proper dimension
1433 */
1434 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
1435 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1436 FreeResource(hMem);
1437 }
1438 }
1439 else
1440 {
1441 hNew = CURSORICON_Copy(Handle);
1442 }
1443 return hNew;
1444
1445notfound:
1446 dprintf(("WARNING: unable to find resource associated with icon/cursor -> copy without scaling!"));
1447 hNew = CURSORICON_Copy(Handle);
1448 return hNew;
1449}
1450
1451/**********************************************************************
1452 * CURSORICON_FindBestIcon
1453 *
1454 * Find the icon closest to the requested size and number of colors.
1455 */
1456static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
1457 int height, int colors )
1458{
1459 int i;
1460 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1461 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
1462 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
1463
1464 if (dir->idCount < 1)
1465 {
1466 dprintf(("Empty directory!\n" ));
1467 return NULL;
1468 }
1469 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1470
1471 /* Find Best Fit */
1472 iTotalDiff = 0xFFFFFFFF;
1473 iColorDiff = 0xFFFFFFFF;
1474 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1475 {
1476 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1477 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1478
1479 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1480 {
1481 iXDiff = iTempXDiff;
1482 iYDiff = iTempYDiff;
1483 iTotalDiff = iXDiff + iYDiff;
1484 }
1485 }
1486
1487 /* Find Best Colors for Best Fit */
1488 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1489 {
1490 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1491 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1492 {
1493#ifdef __WIN32OS2__
1494 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1495#else
1496 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1497#endif
1498 if(iColorDiff > iTempColorDiff)
1499 {
1500 bestEntry = entry;
1501 iColorDiff = iTempColorDiff;
1502 }
1503 }
1504 }
1505////testestest
1506 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1507 return bestEntry;
1508}
1509
1510
1511/**********************************************************************
1512 * CURSORICON_FindBestCursor
1513 *
1514 * Find the cursor closest to the requested size.
1515 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1516 * ignored too
1517 */
1518static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1519 int width, int height, int color)
1520{
1521 int i, maxwidth, maxheight;
1522 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1523
1524 if (dir->idCount < 1)
1525 {
1526 dprintf(("Empty directory!\n" ));
1527 return NULL;
1528 }
1529 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1530
1531 /* Double height to account for AND and XOR masks */
1532
1533 height *= 2;
1534
1535 /* First find the largest one smaller than or equal to the requested size*/
1536
1537 maxwidth = maxheight = 0;
1538 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1539 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1540 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1541 (entry->wBitCount == 1))
1542 {
1543 bestEntry = entry;
1544 maxwidth = entry->ResInfo.cursor.wWidth;
1545 maxheight = entry->ResInfo.cursor.wHeight;
1546 }
1547 if (bestEntry) return bestEntry;
1548
1549 /* Now find the smallest one larger than the requested size */
1550
1551 maxwidth = maxheight = 255;
1552 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1553 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1554 (entry->wBitCount == 1))
1555 {
1556 bestEntry = entry;
1557 maxwidth = entry->ResInfo.cursor.wWidth;
1558 maxheight = entry->ResInfo.cursor.wHeight;
1559 }
1560
1561 return bestEntry;
1562}
1563/**********************************************************************
1564 * LookupIconIdFromDirectoryEx (USER.364)
1565 *
1566 * FIXME: exact parameter sizes
1567 */
1568INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1569 INT width, INT height, UINT cFlag )
1570{
1571 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1572 UINT retVal = 0;
1573
1574 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1575 if( dir && !dir->idReserved && (dir->idType & 3) )
1576 {
1577 CURSORICONDIRENTRY* entry;
1578 HDC hdc;
1579 UINT palEnts;
1580 int colors;
1581 hdc = GetDC(0);
1582 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1583 if (palEnts == 0)
1584 palEnts = 256;
1585 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1586
1587 ReleaseDC(0, hdc);
1588
1589 if( bIcon )
1590 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1591 else
1592 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1593
1594 if( entry ) retVal = entry->wResId;
1595 }
1596 else dprintf(("invalid resource directory\n"));
1597 return retVal;
1598}
1599/**********************************************************************
1600 * LookupIconIdFromDirectory (USER32.379)
1601 */
1602INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1603{
1604 return LookupIconIdFromDirectoryEx( dir, bIcon,
1605 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1606 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1607}
1608//******************************************************************************
1609//******************************************************************************
1610
1611//ICON cache implementation (Wine code)
1612
1613/**********************************************************************
1614 * CURSORICON_FindSharedIcon
1615 */
1616static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
1617{
1618 HANDLE handle = 0;
1619 ICONCACHE *ptr;
1620
1621 EnterCriticalSection( &IconCrst );
1622
1623 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1624 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
1625 {
1626 ptr->count++;
1627 handle = ptr->handle;
1628 break;
1629 }
1630
1631 LeaveCriticalSection( &IconCrst );
1632
1633 return handle;
1634}
1635
1636/*************************************************************************
1637 * CURSORICON_FindCache
1638 *
1639 * Given a handle, find the corresponding cache element
1640 *
1641 * PARAMS
1642 * Handle [I] handle to an Image
1643 *
1644 * RETURNS
1645 * Success: The cache entry
1646 * Failure: NULL
1647 *
1648 */
1649static ICONCACHE* CURSORICON_FindCache(HANDLE handle)
1650{
1651 ICONCACHE *ptr;
1652 ICONCACHE *pRet=NULL;
1653 BOOL IsFound = FALSE;
1654 int count;
1655
1656 EnterCriticalSection( &IconCrst );
1657
1658 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
1659 {
1660 if ( handle == ptr->handle )
1661 {
1662 IsFound = TRUE;
1663 pRet = ptr;
1664 }
1665 }
1666
1667 LeaveCriticalSection( &IconCrst );
1668
1669 return pRet;
1670}
1671
1672/**********************************************************************
1673 * CURSORICON_AddSharedIcon
1674 */
1675static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle )
1676{
1677 ICONCACHE *ptr = (ICONCACHE *)HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
1678 if ( !ptr ) return;
1679
1680 ptr->hModule = hModule;
1681 ptr->hRsrc = hRsrc;
1682 ptr->handle = handle;
1683 ptr->hGroupRsrc = hGroupRsrc;
1684 ptr->count = 1;
1685
1686 EnterCriticalSection( &IconCrst );
1687 ptr->next = IconAnchor;
1688 IconAnchor = ptr;
1689 LeaveCriticalSection( &IconCrst );
1690}
1691
1692/**********************************************************************
1693 * CURSORICON_DelSharedIcon
1694 */
1695static INT CURSORICON_DelSharedIcon( HANDLE handle )
1696{
1697 INT count = -1;
1698 ICONCACHE *ptr;
1699
1700 EnterCriticalSection( &IconCrst );
1701
1702 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1703 if ( ptr->handle == handle )
1704 {
1705 if ( ptr->count > 0 ) ptr->count--;
1706 count = ptr->count;
1707 break;
1708 }
1709
1710 LeaveCriticalSection( &IconCrst );
1711
1712 return count;
1713}
1714
1715/**********************************************************************
1716 * CURSORICON_FreeModuleIcons
1717 */
1718void CURSORICON_FreeModuleIcons( HMODULE hModule )
1719{
1720 ICONCACHE **ptr = &IconAnchor;
1721
1722 EnterCriticalSection( &IconCrst );
1723
1724 while ( *ptr )
1725 {
1726 if ( (*ptr)->hModule == hModule )
1727 {
1728 ICONCACHE *freePtr = *ptr;
1729 *ptr = freePtr->next;
1730
1731#ifdef __WIN32OS2__
1732 CURSORICON_Destroy(freePtr->handle, CID_NONSHARED);
1733#else
1734 GlobalFree( freePtr->handle );
1735#endif
1736 HeapFree( GetProcessHeap(), 0, freePtr );
1737 continue;
1738 }
1739 ptr = &(*ptr)->next;
1740 }
1741
1742 LeaveCriticalSection( &IconCrst );
1743}
1744
Note: See TracBrowser for help on using the repository browser.