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

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

Handle icon masks with different line alignment:

File size: 60.4 KB
Line 
1/* $Id: winicon.cpp,v 1.38 2002-11-26 14:41:07 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
1114 /* Now create the CURSORICONINFO structure */
1115 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1116 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1117 colortablesize = 0;
1118
1119 if(bmpXor.bmBitsPixel <= 8) {
1120 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
1121 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
1122 }
1123 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1124
1125 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1126
1127 if (hObj) hObj = GlobalReAlloc( hObj,
1128 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
1129 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
1130 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1131 if (hObj)
1132 {
1133 CURSORICONINFO *info;
1134
1135 info = (CURSORICONINFO *)GlobalLock( hObj );
1136 info->ptHotSpot.x = hotspot.x;
1137 info->ptHotSpot.y = hotspot.y;
1138 info->nWidth = bmpXor.bmWidth;
1139 info->nHeight = bmpXor.bmHeight;
1140 info->nWidthBytes = bmpXor.bmWidthBytes;
1141 info->bPlanes = bmpXor.bmPlanes;
1142 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1143 info->hInstance = hInstance;
1144 info->dwResGroupId = dwResGroupId;
1145 info->hColorBmp = 0;
1146
1147 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1148 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
1149
1150 if(bmpXor.bmBitsPixel > 1)
1151 {
1152 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
1153 HBITMAP oldbmp;
1154
1155 hdc = CreateCompatibleDC(0);
1156
1157 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
1158 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1159 pInfo->bmiHeader.biPlanes = info->bPlanes;
1160 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
1161
1162 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
1163 if(colortablesize) {
1164 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
1165 }
1166
1167 DeleteDC(hdc);
1168 free(pInfo);
1169 }
1170 else {
1171 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
1172 }
1173 info->hColorBmp = OSLibWinCreatePointer(info, (char*)(info + 1), (LPBITMAP_W)&bmpAnd, (char*)(info + 1) + sizeAnd, (LPBITMAP_W)&bmpXor, bIcon == FALSE);
1174
1175 dprintf(("Create cursor/icon %x with OS/2 pointer handle %x", hObj, info->hColorBmp));
1176 GlobalUnlock( hObj );
1177 }
1178
1179 DeleteObject( hAndBits );
1180 DeleteObject( hXorBits );
1181
1182#ifdef __WIN32OS2__
1183 if(hObj) {
1184 HICON hIcon;
1185 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, HNDL_CURSORICON) == FALSE) {
1186 GlobalFree(hObj);
1187 dprintf(("ERROR: CURSORICON_Load ObjAllocateHandle failed!!"));
1188 return 0;
1189 }
1190 return hIcon;
1191 }
1192#endif
1193 return hObj;
1194}
1195
1196/**********************************************************************
1197 * CURSORICON_Destroy (USER.610)
1198 *
1199 * This routine is actually exported from Win95 USER under the name
1200 * DestroyIcon32 ... The behaviour implemented here should mimic
1201 * the Win95 one exactly, especially the return values, which
1202 * depend on the setting of various flags.
1203 */
1204WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
1205{
1206 WORD retv;
1207
1208 /* Check whether destroying active cursor */
1209
1210 if ( hActiveCursor == handle )
1211 {
1212 dprintf(("WARNING: Destroying active cursor!" ));
1213 SetCursor( 0 );
1214 }
1215
1216#ifdef __WIN32OS2__
1217 HICON hIcon = ObjQueryHandleData(handle, HNDL_CURSORICON);
1218 if(hIcon == -1) {
1219 dprintf(("ERROR: Invalid cursor/icon!"));
1220 return 0;
1221 }
1222#endif
1223
1224 /* Try shared cursor/icon first */
1225 if ( !(flags & CID_NONSHARED) )
1226 {
1227 INT count = CURSORICON_DelSharedIcon( handle );
1228
1229 if ( count != -1 )
1230 return (flags & CID_WIN32)? TRUE : (count == 0);
1231
1232 /* FIXME: OEM cursors/icons should be recognized */
1233 }
1234 /* Now assume non-shared cursor/icon */
1235
1236#ifdef __WIN32OS2__
1237 CURSORICONINFO *iconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
1238 if (!iconinfo) {
1239 dprintf(("ERROR: Invalid cursor!"));
1240 return 0;
1241 }
1242
1243 if(iconinfo->hColorBmp) {
1244 OSLibWinDestroyPointer(iconinfo->hColorBmp);
1245 }
1246 GlobalUnlock(hIcon);
1247 retv = GlobalFree( hIcon );
1248 ObjDeleteHandle(handle, HNDL_CURSORICON);
1249
1250 return (flags & CID_RESOURCE)? retv : TRUE;
1251#else
1252 retv = GlobalFree( handle );
1253 return (flags & CID_RESOURCE)? retv : TRUE;
1254#endif
1255}
1256
1257/***********************************************************************
1258 * CURSORICON_Copy
1259 *
1260 * Make a copy of a cursor or icon.
1261 */
1262static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
1263{
1264 char *ptrOld, *ptrNew;
1265 int size;
1266 HGLOBAL hNew;
1267
1268 handle = ObjQueryHandleData(handle, HNDL_CURSORICON);
1269 if(handle == -1) {
1270 dprintf(("ERROR: Invalid cursor/icon!"));
1271 return 0;
1272 }
1273
1274 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
1275
1276 size = GlobalSize( handle );
1277 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
1278#ifdef __WIN32OS2__
1279 if(hNew == NULL) {
1280 dprintf(("ERROR: CURSORICON_Copy GlobalAlloc failed!!"));
1281 return NULL;
1282 }
1283#endif
1284 ptrNew = (char *)GlobalLock( hNew );
1285 memcpy( ptrNew, ptrOld, size );
1286 GlobalUnlock( handle );
1287 GlobalUnlock( hNew );
1288
1289#ifdef __WIN32OS2__
1290 HICON hIcon;
1291 if(ObjAllocateHandle(&hIcon, (DWORD)hNew, HNDL_CURSORICON) == FALSE) {
1292 GlobalFree(hNew);
1293 dprintf(("ERROR: CURSORICON_Copy ObjAllocateHandle failed!!"));
1294 return 0;
1295 }
1296 return hIcon;
1297#else
1298 return hNew;
1299#endif
1300}
1301
1302/*************************************************************************
1303 * CURSORICON_ExtCopy
1304 *
1305 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1306 *
1307 * PARAMS
1308 * Handle [I] handle to an Image
1309 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1310 * iDesiredCX [I] The Desired width of the Image
1311 * iDesiredCY [I] The desired height of the Image
1312 * nFlags [I] The flags from CopyImage
1313 *
1314 * RETURNS
1315 * Success: The new handle of the Image
1316 *
1317 * NOTES
1318 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1319 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
1320 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1321 *
1322 *
1323 */
1324HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
1325 INT iDesiredCX, INT iDesiredCY,
1326 UINT nFlags)
1327{
1328 HGLOBAL hNew=0;
1329
1330#ifdef __WIN32OS2__
1331 HICON hIcon = ObjQueryHandleData(Handle, HNDL_CURSORICON);
1332 if(hIcon == -1) {
1333 dprintf(("ERROR: Invalid cursor/icon!"));
1334 return 0;
1335 }
1336
1337#else
1338 if(Handle == 0)
1339 {
1340 return 0;
1341 }
1342#endif
1343 /* Best Fit or Monochrome */
1344 if( (nFlags & LR_COPYFROMRESOURCE
1345 && (iDesiredCX > 0 || iDesiredCY > 0))
1346 || nFlags & LR_MONOCHROME)
1347 {
1348#ifdef __WIN32OS2__
1349 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1350#else
1351 ICONCACHE* pIconCache = CURSORICON_FindCache(Handle);
1352#endif
1353
1354 /* Not Found in Cache, then do a straight copy
1355 */
1356 if(pIconCache == NULL)
1357 {
1358#ifdef __WIN32OS2__
1359 hNew = CURSORICON_Copy(Handle);
1360#else
1361 hNew = CURSORICON_Copy(0, Handle);
1362#endif
1363 if(nFlags & LR_COPYFROMRESOURCE)
1364 {
1365 dprintf(("WARNING: LR_COPYFROMRESOURCE: Failed to load from cache\n"));
1366 }
1367 }
1368 else
1369 {
1370 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1371 LPBYTE pBits;
1372 HANDLE hMem;
1373 HRSRC hRsrc;
1374 DWORD dwBytesInRes;
1375 WORD wResId;
1376 DWORD dwResGroupId;
1377 HINSTANCE hInstance;
1378 CURSORICONINFO *iconinfo;
1379 CURSORICONDIR *pDir;
1380 CURSORICONDIRENTRY *pDirEntry;
1381 BOOL bIsIcon = (nType == IMAGE_ICON);
1382
1383#ifdef __WIN32OS2__
1384 iconinfo = (CURSORICONINFO *)GlobalLock( hIcon );
1385#else
1386 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
1387#endif
1388 if(iconinfo == NULL) {
1389 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
1390 }
1391 hInstance = iconinfo->hInstance;
1392 dwResGroupId = iconinfo->dwResGroupId;
1393 GlobalUnlock( Handle );
1394 if(dwResGroupId == -1) {
1395 //todo: if scaling is necessary..
1396 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
1397 hNew = CURSORICON_Copy(Handle);
1398 return hNew;
1399 }
1400
1401 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1402 */
1403 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1404 || (iDesiredCX == 0 && iDesiredCY == 0))
1405 {
1406 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
1407 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
1408 }
1409
1410 /* Retreive the CURSORICONDIRENTRY
1411 */
1412 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
1413 if(!hRsrc) {
1414 goto notfound;
1415 }
1416
1417 if (!(hMem = LoadResource( hInstance, hRsrc)))
1418 {
1419 goto notfound;
1420 }
1421 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1422 {
1423 goto notfound;
1424 }
1425
1426 /* Find Best Fit
1427 */
1428 if(bIsIcon)
1429 {
1430 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1431 pDir, iDesiredCX, iDesiredCY, 256);
1432 }
1433 else
1434 {
1435 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1436 pDir, iDesiredCX, iDesiredCY, 1);
1437 }
1438
1439 wResId = pDirEntry->wResId;
1440 dwBytesInRes = pDirEntry->dwBytesInRes;
1441 FreeResource(hMem);
1442
1443 /* Get the Best Fit
1444 */
1445 if (!(hRsrc = FindResourceW(hInstance ,
1446 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
1447 {
1448 goto notfound;
1449 }
1450 if (!(hMem = LoadResource( hInstance, hRsrc )))
1451 {
1452 goto notfound;
1453 }
1454
1455 pBits = (LPBYTE)LockResource( hMem );
1456
1457 if(nFlags & LR_DEFAULTSIZE)
1458 {
1459 iTargetCY = GetSystemMetrics(SM_CYICON);
1460 iTargetCX = GetSystemMetrics(SM_CXICON);
1461 }
1462
1463 /* Create a New Icon with the proper dimension
1464 */
1465 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
1466 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1467 FreeResource(hMem);
1468 }
1469 }
1470 else
1471 {
1472 hNew = CURSORICON_Copy(Handle);
1473 }
1474 return hNew;
1475
1476notfound:
1477 dprintf(("WARNING: unable to find resource associated with icon/cursor -> copy without scaling!"));
1478 hNew = CURSORICON_Copy(Handle);
1479 return hNew;
1480}
1481
1482/**********************************************************************
1483 * CURSORICON_FindBestIcon
1484 *
1485 * Find the icon closest to the requested size and number of colors.
1486 */
1487static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
1488 int height, int colors )
1489{
1490 int i;
1491 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1492 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
1493 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
1494
1495 if (dir->idCount < 1)
1496 {
1497 dprintf(("Empty directory!\n" ));
1498 return NULL;
1499 }
1500 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1501
1502 /* Find Best Fit */
1503 iTotalDiff = 0xFFFFFFFF;
1504 iColorDiff = 0xFFFFFFFF;
1505 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1506 {
1507 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1508 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1509
1510 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1511 {
1512 iXDiff = iTempXDiff;
1513 iYDiff = iTempYDiff;
1514 iTotalDiff = iXDiff + iYDiff;
1515 }
1516 }
1517
1518 /* Find Best Colors for Best Fit */
1519 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1520 {
1521 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1522 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1523 {
1524#ifdef __WIN32OS2__
1525 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1526#else
1527 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1528#endif
1529 if(iColorDiff > iTempColorDiff)
1530 {
1531 bestEntry = entry;
1532 iColorDiff = iTempColorDiff;
1533 }
1534 }
1535 }
1536////testestest
1537 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1538 return bestEntry;
1539}
1540
1541
1542/**********************************************************************
1543 * CURSORICON_FindBestCursor
1544 *
1545 * Find the cursor closest to the requested size.
1546 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1547 * ignored too
1548 */
1549static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1550 int width, int height, int color)
1551{
1552 int i, maxwidth, maxheight;
1553 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1554
1555 if (dir->idCount < 1)
1556 {
1557 dprintf(("Empty directory!\n" ));
1558 return NULL;
1559 }
1560 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1561
1562 /* Double height to account for AND and XOR masks */
1563
1564 height *= 2;
1565
1566 /* First find the largest one smaller than or equal to the requested size*/
1567
1568 maxwidth = maxheight = 0;
1569 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1570 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1571 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1572 (entry->wBitCount == 1))
1573 {
1574 bestEntry = entry;
1575 maxwidth = entry->ResInfo.cursor.wWidth;
1576 maxheight = entry->ResInfo.cursor.wHeight;
1577 }
1578 if (bestEntry) return bestEntry;
1579
1580 /* Now find the smallest one larger than the requested size */
1581
1582 maxwidth = maxheight = 255;
1583 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1584 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1585 (entry->wBitCount == 1))
1586 {
1587 bestEntry = entry;
1588 maxwidth = entry->ResInfo.cursor.wWidth;
1589 maxheight = entry->ResInfo.cursor.wHeight;
1590 }
1591
1592 return bestEntry;
1593}
1594/**********************************************************************
1595 * LookupIconIdFromDirectoryEx (USER.364)
1596 *
1597 * FIXME: exact parameter sizes
1598 */
1599INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1600 INT width, INT height, UINT cFlag )
1601{
1602 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1603 UINT retVal = 0;
1604
1605 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1606 if( dir && !dir->idReserved && (dir->idType & 3) )
1607 {
1608 CURSORICONDIRENTRY* entry;
1609 HDC hdc;
1610 UINT palEnts;
1611 int colors;
1612 hdc = GetDC(0);
1613 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1614 if (palEnts == 0)
1615 palEnts = 256;
1616 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1617
1618 ReleaseDC(0, hdc);
1619
1620 if( bIcon )
1621 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1622 else
1623 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1624
1625 if( entry ) retVal = entry->wResId;
1626 }
1627 else dprintf(("invalid resource directory\n"));
1628 return retVal;
1629}
1630/**********************************************************************
1631 * LookupIconIdFromDirectory (USER32.379)
1632 */
1633INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1634{
1635 return LookupIconIdFromDirectoryEx( dir, bIcon,
1636 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1637 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1638}
1639//******************************************************************************
1640//******************************************************************************
1641
1642//ICON cache implementation (Wine code)
1643
1644/**********************************************************************
1645 * CURSORICON_FindSharedIcon
1646 */
1647static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
1648{
1649 HANDLE handle = 0;
1650 ICONCACHE *ptr;
1651
1652 EnterCriticalSection( &IconCrst );
1653
1654 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1655 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
1656 {
1657 ptr->count++;
1658 handle = ptr->handle;
1659 break;
1660 }
1661
1662 LeaveCriticalSection( &IconCrst );
1663
1664 return handle;
1665}
1666
1667/*************************************************************************
1668 * CURSORICON_FindCache
1669 *
1670 * Given a handle, find the corresponding cache element
1671 *
1672 * PARAMS
1673 * Handle [I] handle to an Image
1674 *
1675 * RETURNS
1676 * Success: The cache entry
1677 * Failure: NULL
1678 *
1679 */
1680static ICONCACHE* CURSORICON_FindCache(HANDLE handle)
1681{
1682 ICONCACHE *ptr;
1683 ICONCACHE *pRet=NULL;
1684 BOOL IsFound = FALSE;
1685 int count;
1686
1687 EnterCriticalSection( &IconCrst );
1688
1689 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
1690 {
1691 if ( handle == ptr->handle )
1692 {
1693 IsFound = TRUE;
1694 pRet = ptr;
1695 }
1696 }
1697
1698 LeaveCriticalSection( &IconCrst );
1699
1700 return pRet;
1701}
1702
1703/**********************************************************************
1704 * CURSORICON_AddSharedIcon
1705 */
1706static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle )
1707{
1708 ICONCACHE *ptr = (ICONCACHE *)HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
1709 if ( !ptr ) return;
1710
1711 ptr->hModule = hModule;
1712 ptr->hRsrc = hRsrc;
1713 ptr->handle = handle;
1714 ptr->hGroupRsrc = hGroupRsrc;
1715 ptr->count = 1;
1716
1717 EnterCriticalSection( &IconCrst );
1718 ptr->next = IconAnchor;
1719 IconAnchor = ptr;
1720 LeaveCriticalSection( &IconCrst );
1721}
1722
1723/**********************************************************************
1724 * CURSORICON_DelSharedIcon
1725 */
1726static INT CURSORICON_DelSharedIcon( HANDLE handle )
1727{
1728 INT count = -1;
1729 ICONCACHE *ptr;
1730
1731 EnterCriticalSection( &IconCrst );
1732
1733 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1734 if ( ptr->handle == handle )
1735 {
1736 if ( ptr->count > 0 ) ptr->count--;
1737 count = ptr->count;
1738 break;
1739 }
1740
1741 LeaveCriticalSection( &IconCrst );
1742
1743 return count;
1744}
1745
1746/**********************************************************************
1747 * CURSORICON_FreeModuleIcons
1748 */
1749void CURSORICON_FreeModuleIcons( HMODULE hModule )
1750{
1751 ICONCACHE **ptr = &IconAnchor;
1752
1753 EnterCriticalSection( &IconCrst );
1754
1755 while ( *ptr )
1756 {
1757 if ( (*ptr)->hModule == hModule )
1758 {
1759 ICONCACHE *freePtr = *ptr;
1760 *ptr = freePtr->next;
1761
1762#ifdef __WIN32OS2__
1763 CURSORICON_Destroy(freePtr->handle, CID_NONSHARED);
1764#else
1765 GlobalFree( freePtr->handle );
1766#endif
1767 HeapFree( GetProcessHeap(), 0, freePtr );
1768 continue;
1769 }
1770 ptr = &(*ptr)->next;
1771 }
1772
1773 LeaveCriticalSection( &IconCrst );
1774}
1775
Note: See TracBrowser for help on using the repository browser.