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

Last change on this file since 9624 was 9624, checked in by sandervl, 23 years ago

Added workaround for handling black & white cursors

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