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

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

moved icon conversion to user32

File size: 58.7 KB
Line 
1/* $Id: winicon.cpp,v 1.28 2001-08-08 10:07:19 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, hCursor, -1, hIcon );
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 dprintf2(("CURSORICON_CreateFromResource %x %x %x %x %d", hInstance, dwResGroupId, hObj, bits, cbSize));
839
840 if (dwVersion == 0x00020000)
841 {
842 dprintf(("CURSORICON_CreateFromResource 2.xx resources are not supported"));
843 return 0;
844 }
845
846 if (bIcon) {
847 bmi = (BITMAPINFO *)bits;
848 }
849 else /* get the hotspot */
850 {
851 POINT16 *pt = (POINT16 *)bits;
852 hotspot = *pt;
853 bmi = (BITMAPINFO *)(pt + 1);
854 }
855 size = DIB_BitmapInfoSize(bmi, DIB_RGB_COLORS);
856
857 if (!width) width = bmi->bmiHeader.biWidth;
858 if (!height) height = bmi->bmiHeader.biHeight/2;
859
860 DoStretch = (bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width);
861
862 colorsize = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2, bmi->bmiHeader.biBitCount);
863 bwsize = (bmi->bmiHeader.biWidth * bmi->bmiHeader.biHeight/2)/8;
864
865 /* Check bitmap header */
866 if((bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
867 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
868 bmi->bmiHeader.biCompression != BI_RGB) )
869 {
870 return 0;
871 }
872
873#ifdef __WIN32OS2__
874 if( (hdc = CreateCompatibleDC( 0 )) )
875#else
876 if( (hdc = GetDC( 0 )) )
877#endif
878 {
879 BITMAPINFO* pInfo;
880
881 /* Make sure we have room for the monochrome bitmap later on.
882 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
883 * up to and including the biBitCount. In-memory icon resource
884 * format is as follows:
885 *
886 * BITMAPINFOHEADER icHeader // DIB header
887 * RGBQUAD icColors[] // Color table
888 * BYTE icXOR[] // DIB bits for XOR mask
889 * BYTE icAND[] // DIB bits for AND mask
890 */
891
892 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
893 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
894 {
895 memcpy( pInfo, bmi, size );
896 pInfo->bmiHeader.biHeight /= 2;
897
898 /* Create the XOR bitmap */
899 if (DoStretch)
900 {
901 if(bIcon)
902 {
903 hXorBits = CreateCompatibleBitmap(hdc, width, height);
904 }
905 else
906 {
907 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
908 }
909 if(hXorBits)
910 {
911 HBITMAP hOld;
912 HDC hMem = CreateCompatibleDC(hdc);
913 BOOL res;
914
915 if (hMem) {
916 hOld = SelectObject(hMem, hXorBits);
917 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
918 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
919 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
920 SelectObject(hMem, hOld);
921 DeleteDC(hMem);
922 }
923 else res = FALSE;
924 if (!res) {
925 DeleteObject(hXorBits);
926 hXorBits = 0;
927 }
928 }
929 }
930 else
931 {
932 hXorBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
933 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
934 }
935 if( hXorBits )
936 {
937 char* xbits = (char *)bmi + size + DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
938 bmi->bmiHeader.biHeight,
939 bmi->bmiHeader.biBitCount) / 2;
940
941 pInfo->bmiHeader.biBitCount = 1;
942 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
943 {
944 RGBQUAD *rgb = pInfo->bmiColors;
945
946 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
947 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
948 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
949 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
950 }
951 else
952 {
953 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
954
955 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
956 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
957 }
958
959 /* Create the AND bitmap */
960 if (DoStretch)
961 {
962 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL)))
963 {
964 HBITMAP hOld;
965 HDC hMem = CreateCompatibleDC(hdc);
966 BOOL res;
967
968 if (hMem) {
969 hOld = SelectObject(hMem, hAndBits);
970//SvL: This also doesn't work as StretchDIBits doesn't handle 1bpp bitmaps correctly
971//--------->>> hack alert!
972#if 1
973 HBITMAP hBmp, hOld1;
974 HDC hMem1;
975
976 hMem1 = CreateCompatibleDC(hdc);
977
978 int linewidth = BITMAP_GetWidthBytes(pInfo->bmiHeader.biWidth, 1);
979
980 char *newpix = (char *)malloc(linewidth*pInfo->bmiHeader.biHeight);
981
982 newpix += ((pInfo->bmiHeader.biHeight-1)*linewidth);
983
984 if(cbSize - size - colorsize - bwsize == bwsize)
985 {//this means an AND and XOR mask is present (interleaved; and/xor)
986 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
987 memcpy(newpix, xbits, linewidth);
988 newpix -= linewidth;
989 xbits += linewidth*2;
990 }
991 }
992 else {
993 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
994 memcpy(newpix, xbits, linewidth);
995 newpix -= linewidth;
996 xbits += linewidth;
997 }
998 }
999 newpix += linewidth;
1000 hBmp = CreateBitmap(pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, 1, 1, newpix);
1001 free(newpix);
1002
1003 hOld1 = SelectObject(hMem1, hBmp);
1004
1005 res = StretchBlt(hMem, 0, 0, width, height, hMem1, 0, 0, pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, SRCCOPY);
1006
1007 SelectObject(hMem1, hOld1);
1008 DeleteObject(hBmp);
1009 DeleteDC(hMem1);
1010
1011
1012#else
1013 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
1014 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
1015 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
1016#endif
1017 SelectObject(hMem, hOld);
1018 DeleteDC(hMem);
1019 }
1020 else res = FALSE;
1021 if (!res) {
1022 DeleteObject(hAndBits);
1023 hAndBits = 0;
1024 }
1025 }
1026 }
1027 else {
1028//SvL: Must use CreateBitmap here as CreateDIBitmap converts data to 8bpp (GetObjectA info -> 8 bpp)
1029#if 1
1030 int linewidth = BITMAP_GetWidthBytes(width, 1);
1031
1032 char *newpix = (char *)malloc(linewidth*height);
1033
1034 newpix += ((height-1)*linewidth);
1035
1036 if(cbSize - size - colorsize - bwsize == bwsize)
1037 {//this means an AND and XOR mask is present (interleaved; and/xor)
1038 for(int i=0;i<height;i++) {
1039 memcpy(newpix, xbits, linewidth);
1040 newpix -= linewidth;
1041 xbits += linewidth*2;
1042 }
1043 }
1044 else {
1045 for(int i=0;i<height;i++) {
1046 memcpy(newpix, xbits, linewidth);
1047 newpix -= linewidth;
1048 xbits += linewidth;
1049 }
1050 }
1051 newpix += linewidth;
1052 hAndBits = CreateBitmap(width, height, 1, 1, newpix);
1053
1054 free(newpix);
1055
1056#else
1057 hAndBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
1058 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
1059#endif
1060 }
1061 if( !hAndBits )
1062 DeleteObject( hXorBits );
1063 }
1064 HeapFree( GetProcessHeap(), 0, pInfo );
1065 }
1066#ifdef __WIN32OS2__
1067 DeleteDC(hdc );
1068#else
1069 ReleaseDC( 0, hdc );
1070#endif
1071 }
1072
1073 if( !hXorBits || !hAndBits )
1074 {
1075 dprintf(("\tunable to create an icon bitmap."));
1076 return 0;
1077 }
1078
1079 /* Now create the CURSORICONINFO structure */
1080 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1081 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1082 colortablesize = 0;
1083
1084 if(bmpXor.bmBitsPixel <= 8) {
1085 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
1086 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
1087 }
1088 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1089
1090 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1091
1092 if (hObj) hObj = GlobalReAlloc( hObj,
1093 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
1094 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
1095 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1096 if (hObj)
1097 {
1098 CURSORICONINFO *info;
1099
1100 info = (CURSORICONINFO *)GlobalLock( hObj );
1101 info->ptHotSpot.x = hotspot.x;
1102 info->ptHotSpot.y = hotspot.y;
1103 info->nWidth = bmpXor.bmWidth;
1104 info->nHeight = bmpXor.bmHeight;
1105 info->nWidthBytes = bmpXor.bmWidthBytes;
1106 info->bPlanes = bmpXor.bmPlanes;
1107 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1108 info->hInstance = hInstance;
1109 info->dwResGroupId = dwResGroupId;
1110 info->hColorBmp = 0;
1111
1112 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1113 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
1114
1115 if(bmpXor.bmBitsPixel > 1)
1116 {
1117 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
1118 HBITMAP oldbmp;
1119
1120 hdc = CreateCompatibleDC(0);
1121
1122 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
1123 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1124 pInfo->bmiHeader.biPlanes = info->bPlanes;
1125 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
1126
1127 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
1128 if(colortablesize) {
1129 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
1130 }
1131
1132 DeleteDC(hdc);
1133 free(pInfo);
1134 }
1135 else {
1136 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
1137 }
1138 info->hColorBmp = OSLibWinCreatePointer(info, (char *)(info + 1), (LPBITMAP_W)&bmpAnd, (char *)(info + 1) + sizeAnd, (LPBITMAP_W)&bmpXor, bIcon == FALSE);
1139 dprintf(("Create cursor/icon %x with OS/2 pointer handle %x", hObj, info->hColorBmp));
1140 GlobalUnlock( hObj );
1141 }
1142
1143 DeleteObject( hAndBits );
1144 DeleteObject( hXorBits );
1145
1146#ifdef __WIN32OS2__
1147 if(hObj) {
1148 HICON hIcon;
1149 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, USEROBJ_CURSORICON) == FALSE) {
1150 GlobalFree(hObj);
1151 dprintf(("ERROR: CURSORICON_Load ObjAllocateHandle failed!!"));
1152 return 0;
1153 }
1154 return hIcon;
1155 }
1156#endif
1157 return hObj;
1158}
1159
1160/**********************************************************************
1161 * CURSORICON_Destroy (USER.610)
1162 *
1163 * This routine is actually exported from Win95 USER under the name
1164 * DestroyIcon32 ... The behaviour implemented here should mimic
1165 * the Win95 one exactly, especially the return values, which
1166 * depend on the setting of various flags.
1167 */
1168WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
1169{
1170 WORD retv;
1171
1172 /* Check whether destroying active cursor */
1173
1174 if ( hActiveCursor == handle )
1175 {
1176 dprintf(("WARNING: Destroying active cursor!" ));
1177 SetCursor( 0 );
1178 }
1179
1180#ifdef __WIN32OS2__
1181 HICON hIcon = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1182 if(hIcon == -1) {
1183 dprintf(("ERROR: Invalid cursor/icon!"));
1184 return 0;
1185 }
1186#endif
1187
1188 /* Try shared cursor/icon first */
1189 if ( !(flags & CID_NONSHARED) )
1190 {
1191 INT count = CURSORICON_DelSharedIcon( handle );
1192
1193 if ( count != -1 )
1194 return (flags & CID_WIN32)? TRUE : (count == 0);
1195
1196 /* FIXME: OEM cursors/icons should be recognized */
1197 }
1198 /* Now assume non-shared cursor/icon */
1199
1200#ifdef __WIN32OS2__
1201 CURSORICONINFO *iconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
1202 if (!iconinfo) {
1203 dprintf(("ERROR: Invalid cursor!"));
1204 return 0;
1205 }
1206
1207 if(iconinfo->hColorBmp) {
1208 OSLibWinDestroyPointer(iconinfo->hColorBmp);
1209 }
1210 GlobalUnlock(hIcon);
1211 retv = GlobalFree( hIcon );
1212 ObjFreeHandle(handle);
1213
1214 return (flags & CID_RESOURCE)? retv : TRUE;
1215#else
1216 retv = GlobalFree( handle );
1217 return (flags & CID_RESOURCE)? retv : TRUE;
1218#endif
1219}
1220
1221/***********************************************************************
1222 * CURSORICON_Copy
1223 *
1224 * Make a copy of a cursor or icon.
1225 */
1226static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
1227{
1228 char *ptrOld, *ptrNew;
1229 int size;
1230 HGLOBAL hNew;
1231
1232 handle = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1233 if(handle == -1) {
1234 dprintf(("ERROR: Invalid cursor/icon!"));
1235 return 0;
1236 }
1237
1238 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
1239
1240 size = GlobalSize( handle );
1241 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
1242#ifdef __WIN32OS2__
1243 if(hNew == NULL) {
1244 dprintf(("ERROR: CURSORICON_Copy GlobalAlloc failed!!"));
1245 return NULL;
1246 }
1247#endif
1248 ptrNew = (char *)GlobalLock( hNew );
1249 memcpy( ptrNew, ptrOld, size );
1250 GlobalUnlock( handle );
1251 GlobalUnlock( hNew );
1252
1253#ifdef __WIN32OS2__
1254 HICON hIcon;
1255 if(ObjAllocateHandle(&hIcon, (DWORD)hNew, USEROBJ_CURSORICON) == FALSE) {
1256 GlobalFree(hNew);
1257 dprintf(("ERROR: CURSORICON_Copy ObjAllocateHandle failed!!"));
1258 return 0;
1259 }
1260 return hIcon;
1261#else
1262 return hNew;
1263#endif
1264}
1265
1266/*************************************************************************
1267 * CURSORICON_ExtCopy
1268 *
1269 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1270 *
1271 * PARAMS
1272 * Handle [I] handle to an Image
1273 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1274 * iDesiredCX [I] The Desired width of the Image
1275 * iDesiredCY [I] The desired height of the Image
1276 * nFlags [I] The flags from CopyImage
1277 *
1278 * RETURNS
1279 * Success: The new handle of the Image
1280 *
1281 * NOTES
1282 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1283 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
1284 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1285 *
1286 *
1287 */
1288HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
1289 INT iDesiredCX, INT iDesiredCY,
1290 UINT nFlags)
1291{
1292 HGLOBAL hNew=0;
1293
1294#ifdef __WIN32OS2__
1295 HICON hIcon = ObjGetHandleData(Handle, USEROBJ_CURSORICON);
1296 if(hIcon == -1) {
1297 dprintf(("ERROR: Invalid cursor/icon!"));
1298 return 0;
1299 }
1300
1301#else
1302 if(Handle == 0)
1303 {
1304 return 0;
1305 }
1306#endif
1307 /* Best Fit or Monochrome */
1308 if( (nFlags & LR_COPYFROMRESOURCE
1309 && (iDesiredCX > 0 || iDesiredCY > 0))
1310 || nFlags & LR_MONOCHROME)
1311 {
1312#ifdef __WIN32OS2__
1313 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1314#else
1315 ICONCACHE* pIconCache = CURSORICON_FindCache(Handle);
1316#endif
1317
1318 /* Not Found in Cache, then do a straight copy
1319 */
1320 if(pIconCache == NULL)
1321 {
1322#ifdef __WIN32OS2__
1323 hNew = CURSORICON_Copy(Handle);
1324#else
1325 hNew = CURSORICON_Copy(0, Handle);
1326#endif
1327 if(nFlags & LR_COPYFROMRESOURCE)
1328 {
1329 dprintf(("WARNING: LR_COPYFROMRESOURCE: Failed to load from cache\n"));
1330 }
1331 }
1332 else
1333 {
1334 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1335 LPBYTE pBits;
1336 HANDLE hMem;
1337 HRSRC hRsrc;
1338 DWORD dwBytesInRes;
1339 WORD wResId;
1340 DWORD dwResGroupId;
1341 HINSTANCE hInstance;
1342 CURSORICONINFO *iconinfo;
1343 CURSORICONDIR *pDir;
1344 CURSORICONDIRENTRY *pDirEntry;
1345 BOOL bIsIcon = (nType == IMAGE_ICON);
1346
1347#ifdef __WIN32OS2__
1348 iconinfo = (CURSORICONINFO *)GlobalLock( hIcon );
1349#else
1350 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
1351#endif
1352 if(iconinfo == NULL) {
1353 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
1354 }
1355 hInstance = iconinfo->hInstance;
1356 dwResGroupId = iconinfo->dwResGroupId;
1357 GlobalUnlock( Handle );
1358 if(dwResGroupId == -1) {
1359 //todo: if scaling is necessary..
1360 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
1361 hNew = CURSORICON_Copy(Handle);
1362 return hNew;
1363 }
1364
1365 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1366 */
1367 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1368 || (iDesiredCX == 0 && iDesiredCY == 0))
1369 {
1370 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
1371 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
1372 }
1373
1374 /* Retreive the CURSORICONDIRENTRY
1375 */
1376 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
1377 if(!hRsrc) {
1378 goto notfound;
1379 }
1380
1381 if (!(hMem = LoadResource( hInstance, hRsrc)))
1382 {
1383 goto notfound;
1384 }
1385 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1386 {
1387 goto notfound;
1388 }
1389
1390 /* Find Best Fit
1391 */
1392 if(bIsIcon)
1393 {
1394 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1395 pDir, iDesiredCX, iDesiredCY, 256);
1396 }
1397 else
1398 {
1399 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1400 pDir, iDesiredCX, iDesiredCY, 1);
1401 }
1402
1403 wResId = pDirEntry->wResId;
1404 dwBytesInRes = pDirEntry->dwBytesInRes;
1405 FreeResource(hMem);
1406
1407 /* Get the Best Fit
1408 */
1409 if (!(hRsrc = FindResourceW(hInstance ,
1410 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
1411 {
1412 goto notfound;
1413 }
1414 if (!(hMem = LoadResource( hInstance, hRsrc )))
1415 {
1416 goto notfound;
1417 }
1418
1419 pBits = (LPBYTE)LockResource( hMem );
1420
1421 if(nFlags & LR_DEFAULTSIZE)
1422 {
1423 iTargetCY = GetSystemMetrics(SM_CYICON);
1424 iTargetCX = GetSystemMetrics(SM_CXICON);
1425 }
1426
1427 /* Create a New Icon with the proper dimension
1428 */
1429 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
1430 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1431 FreeResource(hMem);
1432 }
1433 }
1434 else
1435 {
1436 hNew = CURSORICON_Copy(Handle);
1437 }
1438 return hNew;
1439
1440notfound:
1441 dprintf(("WARNING: unable to find resource associated with icon/cursor -> copy without scaling!"));
1442 hNew = CURSORICON_Copy(Handle);
1443 return hNew;
1444}
1445
1446/**********************************************************************
1447 * CURSORICON_FindBestIcon
1448 *
1449 * Find the icon closest to the requested size and number of colors.
1450 */
1451static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
1452 int height, int colors )
1453{
1454 int i;
1455 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1456 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
1457 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
1458
1459 if (dir->idCount < 1)
1460 {
1461 dprintf(("Empty directory!\n" ));
1462 return NULL;
1463 }
1464 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1465
1466 /* Find Best Fit */
1467 iTotalDiff = 0xFFFFFFFF;
1468 iColorDiff = 0xFFFFFFFF;
1469 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1470 {
1471 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1472 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1473
1474 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1475 {
1476 iXDiff = iTempXDiff;
1477 iYDiff = iTempYDiff;
1478 iTotalDiff = iXDiff + iYDiff;
1479 }
1480 }
1481
1482 /* Find Best Colors for Best Fit */
1483 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1484 {
1485 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1486 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1487 {
1488#ifdef __WIN32OS2__
1489 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1490#else
1491 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1492#endif
1493 if(iColorDiff > iTempColorDiff)
1494 {
1495 bestEntry = entry;
1496 iColorDiff = iTempColorDiff;
1497 }
1498 }
1499 }
1500////testestest
1501 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1502 return bestEntry;
1503}
1504
1505
1506/**********************************************************************
1507 * CURSORICON_FindBestCursor
1508 *
1509 * Find the cursor closest to the requested size.
1510 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1511 * ignored too
1512 */
1513static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1514 int width, int height, int color)
1515{
1516 int i, maxwidth, maxheight;
1517 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1518
1519 if (dir->idCount < 1)
1520 {
1521 dprintf(("Empty directory!\n" ));
1522 return NULL;
1523 }
1524 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1525
1526 /* Double height to account for AND and XOR masks */
1527
1528 height *= 2;
1529
1530 /* First find the largest one smaller than or equal to the requested size*/
1531
1532 maxwidth = maxheight = 0;
1533 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1534 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1535 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1536 (entry->wBitCount == 1))
1537 {
1538 bestEntry = entry;
1539 maxwidth = entry->ResInfo.cursor.wWidth;
1540 maxheight = entry->ResInfo.cursor.wHeight;
1541 }
1542 if (bestEntry) return bestEntry;
1543
1544 /* Now find the smallest one larger than the requested size */
1545
1546 maxwidth = maxheight = 255;
1547 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1548 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1549 (entry->wBitCount == 1))
1550 {
1551 bestEntry = entry;
1552 maxwidth = entry->ResInfo.cursor.wWidth;
1553 maxheight = entry->ResInfo.cursor.wHeight;
1554 }
1555
1556 return bestEntry;
1557}
1558/**********************************************************************
1559 * LookupIconIdFromDirectoryEx (USER.364)
1560 *
1561 * FIXME: exact parameter sizes
1562 */
1563INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1564 INT width, INT height, UINT cFlag )
1565{
1566 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1567 UINT retVal = 0;
1568
1569 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1570 if( dir && !dir->idReserved && (dir->idType & 3) )
1571 {
1572 CURSORICONDIRENTRY* entry;
1573 HDC hdc;
1574 UINT palEnts;
1575 int colors;
1576 hdc = GetDC(0);
1577 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1578 if (palEnts == 0)
1579 palEnts = 256;
1580 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1581
1582 ReleaseDC(0, hdc);
1583
1584 if( bIcon )
1585 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1586 else
1587 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1588
1589 if( entry ) retVal = entry->wResId;
1590 }
1591 else dprintf(("invalid resource directory\n"));
1592 return retVal;
1593}
1594/**********************************************************************
1595 * LookupIconIdFromDirectory (USER32.379)
1596 */
1597INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1598{
1599 return LookupIconIdFromDirectoryEx( dir, bIcon,
1600 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1601 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1602}
1603//******************************************************************************
1604//******************************************************************************
1605
1606//ICON cache implementation (Wine code)
1607
1608/**********************************************************************
1609 * CURSORICON_FindSharedIcon
1610 */
1611static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
1612{
1613 HANDLE handle = 0;
1614 ICONCACHE *ptr;
1615
1616 EnterCriticalSection( &IconCrst );
1617
1618 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1619 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
1620 {
1621 ptr->count++;
1622 handle = ptr->handle;
1623 break;
1624 }
1625
1626 LeaveCriticalSection( &IconCrst );
1627
1628 return handle;
1629}
1630
1631/*************************************************************************
1632 * CURSORICON_FindCache
1633 *
1634 * Given a handle, find the corresponding cache element
1635 *
1636 * PARAMS
1637 * Handle [I] handle to an Image
1638 *
1639 * RETURNS
1640 * Success: The cache entry
1641 * Failure: NULL
1642 *
1643 */
1644static ICONCACHE* CURSORICON_FindCache(HANDLE handle)
1645{
1646 ICONCACHE *ptr;
1647 ICONCACHE *pRet=NULL;
1648 BOOL IsFound = FALSE;
1649 int count;
1650
1651 EnterCriticalSection( &IconCrst );
1652
1653 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
1654 {
1655 if ( handle == ptr->handle )
1656 {
1657 IsFound = TRUE;
1658 pRet = ptr;
1659 }
1660 }
1661
1662 LeaveCriticalSection( &IconCrst );
1663
1664 return pRet;
1665}
1666
1667/**********************************************************************
1668 * CURSORICON_AddSharedIcon
1669 */
1670static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle )
1671{
1672 ICONCACHE *ptr = (ICONCACHE *)HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
1673 if ( !ptr ) return;
1674
1675 ptr->hModule = hModule;
1676 ptr->hRsrc = hRsrc;
1677 ptr->handle = handle;
1678 ptr->hGroupRsrc = hGroupRsrc;
1679 ptr->count = 1;
1680
1681 EnterCriticalSection( &IconCrst );
1682 ptr->next = IconAnchor;
1683 IconAnchor = ptr;
1684 LeaveCriticalSection( &IconCrst );
1685}
1686
1687/**********************************************************************
1688 * CURSORICON_DelSharedIcon
1689 */
1690static INT CURSORICON_DelSharedIcon( HANDLE handle )
1691{
1692 INT count = -1;
1693 ICONCACHE *ptr;
1694
1695 EnterCriticalSection( &IconCrst );
1696
1697 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1698 if ( ptr->handle == handle )
1699 {
1700 if ( ptr->count > 0 ) ptr->count--;
1701 count = ptr->count;
1702 break;
1703 }
1704
1705 LeaveCriticalSection( &IconCrst );
1706
1707 return count;
1708}
1709
1710/**********************************************************************
1711 * CURSORICON_FreeModuleIcons
1712 */
1713void CURSORICON_FreeModuleIcons( HMODULE hModule )
1714{
1715 ICONCACHE **ptr = &IconAnchor;
1716
1717 EnterCriticalSection( &IconCrst );
1718
1719 while ( *ptr )
1720 {
1721 if ( (*ptr)->hModule == hModule )
1722 {
1723 ICONCACHE *freePtr = *ptr;
1724 *ptr = freePtr->next;
1725
1726#ifdef __WIN32OS2__
1727 CURSORICON_Destroy(freePtr->handle, CID_NONSHARED);
1728#else
1729 GlobalFree( freePtr->handle );
1730#endif
1731 HeapFree( GetProcessHeap(), 0, freePtr );
1732 continue;
1733 }
1734 ptr = &(*ptr)->next;
1735 }
1736
1737 LeaveCriticalSection( &IconCrst );
1738}
1739
Note: See TracBrowser for help on using the repository browser.