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

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

add icon to PM tasklist

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