source: trunk/src/user32/winicon.cpp

Last change on this file was 21964, checked in by dmik, 14 years ago

Fix shifted icon AND masks (r21951 regression).

Turns out that ony GetDIBits needs 4-byte alignment while
GetBitmapBits (used to get the AND mask) needs 2-byte alignment.

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