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

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

Generate injected WH_MOUSE_LL hook event in SetCursorPos (with LLMHF_INJECTED flag set)

File size: 59.8 KB
Line 
1/* $Id: winicon.cpp,v 1.34 2002-05-31 09:54:05 sandervl Exp $ */
2/*
3 * Win32 Icon Code for OS/2
4 *
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) (OS/2 Port)
7 *
8 * Based on Wine code (objects\bitmap.c, loader\resource.c, objects\cursoricon.c):
9 *
10 * Copyright 1993 Alexandre Julliard
11 * 1993 Robert J. Amstadt
12 * 1996 Martin Von Loewis
13 * 1997 Alex Korobka
14 * 1998 Turchanov Sergey
15 * 1998 Huw D M Davies
16 * Theory:
17 *
18 * http://www.microsoft.com/win32dev/ui/icons.htm
19 *
20 * Cursors and icons are stored in a global heap block, with the
21 * following layout:
22 *
23 * CURSORICONINFO info;
24 * BYTE[] ANDbits;
25 * BYTE[] XORbits;
26 *
27 * The bits structures are in the format of a device-dependent bitmap.
28 *
29 * This layout is very sub-optimal, as the bitmap bits are stored in
30 * the X client instead of in the server like other bitmaps; however,
31 * some programs (notably Paint Brush) expect to be able to manipulate
32 * the bits directly :-(
33 *
34 * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
35 *
36 **************************************************************************************************
37 *
38 * TODO: Scaling of system cursors (store them as resources in user32 instead of using PM pointers)
39 * TODO: We use the hColorBmp member of the CURSORICONINFO structure to store the PM cursor handle
40 * Might mess up PaintBrush (see above)
41 *
42 * Project Odin Software License can be found in LICENSE.TXT
43 *
44 */
45#include <os2win.h>
46#include <stdio.h>
47#include <string.h>
48#include <winicon.h>
49#include <win\cursoricon.h>
50#include <objhandle.h>
51#include "dib.h"
52#include <heapstring.h>
53#include <win\virtual.h>
54#include "initterm.h"
55#include "oslibres.h"
56#include "oslibwin.h"
57#include "dc.h"
58
59#define DBG_LOCALLOG DBG_winicon
60#include "dbglocal.h"
61
62
63/**********************************************************************
64 * ICONCACHE for cursors/icons loaded with LR_SHARED.
65 *
66 * FIXME: This should not be allocated on the system heap, but on a
67 * subsystem-global heap (i.e. one for all Win16 processes,
68 * and one for each Win32 process).
69 */
70typedef struct tagICONCACHE
71{
72 struct tagICONCACHE *next;
73
74 HMODULE hModule;
75 HRSRC hRsrc;
76 HRSRC hGroupRsrc;
77 HANDLE handle;
78 INT count;
79} ICONCACHE;
80
81static ICONCACHE *IconAnchor = NULL;
82static CRITICAL_SECTION IconCrst = CRITICAL_SECTION_INIT;
83static WORD ICON_HOTSPOT = 0x4242;
84static HCURSOR hActiveCursor = 0;
85static HCURSOR hActiveCursorPM = 0;
86
87
88static HGLOBAL CURSORICON_CreateFromResource( HINSTANCE hInstance, DWORD dwResGroupId, HGLOBAL hObj, LPBYTE bits,
89 UINT cbSize, BOOL bIcon, DWORD dwVersion, INT width, INT height, UINT loadflags );
90static HGLOBAL CURSORICON_Copy( HGLOBAL handle );
91static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
92 int height, int colors );
93static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
94 int width, int height, int color);
95BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
96 CURSORICONDIR **res, LPBYTE **ptr);
97
98static INT CURSORICON_DelSharedIcon( HANDLE handle );
99static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle );
100static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc );
101static ICONCACHE* CURSORICON_FindCache(HANDLE handle);
102
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, USEROBJ_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 = ObjGetHandleData(hIcon, USEROBJ_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 = ObjGetHandleData(hIcon, USEROBJ_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 = ObjGetHandleData(hCursor, USEROBJ_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, USEROBJ_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, USEROBJ_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 = (bmi->bmiHeader.biWidth * bmi->bmiHeader.biHeight/2)/8;
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 = BITMAP_GetWidthBytes(width, 1);
1054
1055 char *newpix = (char *)malloc(linewidth*height);
1056
1057 newpix += ((height-1)*linewidth);
1058
1059 if(cbSize - size - colorsize - bwsize == bwsize)
1060 {//this means an AND and XOR mask is present (interleaved; and/xor)
1061 for(int i=0;i<height;i++) {
1062 memcpy(newpix, xbits, linewidth);
1063 newpix -= linewidth;
1064 xbits += linewidth*2;
1065 }
1066 }
1067 else {
1068 for(int i=0;i<height;i++) {
1069 memcpy(newpix, xbits, linewidth);
1070 newpix -= linewidth;
1071 xbits += linewidth;
1072 }
1073 }
1074 newpix += linewidth;
1075 hAndBits = CreateBitmap(width, height, 1, 1, newpix);
1076
1077 free(newpix);
1078
1079#else
1080 hAndBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
1081 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
1082#endif
1083 }
1084 if( !hAndBits )
1085 DeleteObject( hXorBits );
1086 }
1087 HeapFree( GetProcessHeap(), 0, pInfo );
1088 }
1089#ifdef __WIN32OS2__
1090 DeleteDC(hdc );
1091#else
1092 ReleaseDC( 0, hdc );
1093#endif
1094 }
1095
1096 if( !hXorBits || !hAndBits )
1097 {
1098 dprintf(("\tunable to create an icon bitmap."));
1099 return 0;
1100 }
1101
1102 /* Now create the CURSORICONINFO structure */
1103 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1104 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1105 colortablesize = 0;
1106
1107 if(bmpXor.bmBitsPixel <= 8) {
1108 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
1109 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
1110 }
1111 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1112
1113 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1114
1115 if (hObj) hObj = GlobalReAlloc( hObj,
1116 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
1117 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
1118 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1119 if (hObj)
1120 {
1121 CURSORICONINFO *info;
1122
1123 info = (CURSORICONINFO *)GlobalLock( hObj );
1124 info->ptHotSpot.x = hotspot.x;
1125 info->ptHotSpot.y = hotspot.y;
1126 info->nWidth = bmpXor.bmWidth;
1127 info->nHeight = bmpXor.bmHeight;
1128 info->nWidthBytes = bmpXor.bmWidthBytes;
1129 info->bPlanes = bmpXor.bmPlanes;
1130 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1131 info->hInstance = hInstance;
1132 info->dwResGroupId = dwResGroupId;
1133 info->hColorBmp = 0;
1134
1135 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1136 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
1137
1138 if(bmpXor.bmBitsPixel > 1)
1139 {
1140 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
1141 HBITMAP oldbmp;
1142
1143 hdc = CreateCompatibleDC(0);
1144
1145 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
1146 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1147 pInfo->bmiHeader.biPlanes = info->bPlanes;
1148 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
1149
1150 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
1151 if(colortablesize) {
1152 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
1153 }
1154
1155 DeleteDC(hdc);
1156 free(pInfo);
1157 }
1158 else {
1159 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
1160 }
1161 info->hColorBmp = OSLibWinCreatePointer(info, (char*)(info + 1), (LPBITMAP_W)&bmpAnd, (char*)(info + 1) + sizeAnd, (LPBITMAP_W)&bmpXor, bIcon == FALSE);
1162
1163 dprintf(("Create cursor/icon %x with OS/2 pointer handle %x", hObj, info->hColorBmp));
1164 GlobalUnlock( hObj );
1165 }
1166
1167 DeleteObject( hAndBits );
1168 DeleteObject( hXorBits );
1169
1170#ifdef __WIN32OS2__
1171 if(hObj) {
1172 HICON hIcon;
1173 if(ObjAllocateHandle(&hIcon, (DWORD)hObj, USEROBJ_CURSORICON) == FALSE) {
1174 GlobalFree(hObj);
1175 dprintf(("ERROR: CURSORICON_Load ObjAllocateHandle failed!!"));
1176 return 0;
1177 }
1178 return hIcon;
1179 }
1180#endif
1181 return hObj;
1182}
1183
1184/**********************************************************************
1185 * CURSORICON_Destroy (USER.610)
1186 *
1187 * This routine is actually exported from Win95 USER under the name
1188 * DestroyIcon32 ... The behaviour implemented here should mimic
1189 * the Win95 one exactly, especially the return values, which
1190 * depend on the setting of various flags.
1191 */
1192WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
1193{
1194 WORD retv;
1195
1196 /* Check whether destroying active cursor */
1197
1198 if ( hActiveCursor == handle )
1199 {
1200 dprintf(("WARNING: Destroying active cursor!" ));
1201 SetCursor( 0 );
1202 }
1203
1204#ifdef __WIN32OS2__
1205 HICON hIcon = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1206 if(hIcon == -1) {
1207 dprintf(("ERROR: Invalid cursor/icon!"));
1208 return 0;
1209 }
1210#endif
1211
1212 /* Try shared cursor/icon first */
1213 if ( !(flags & CID_NONSHARED) )
1214 {
1215 INT count = CURSORICON_DelSharedIcon( handle );
1216
1217 if ( count != -1 )
1218 return (flags & CID_WIN32)? TRUE : (count == 0);
1219
1220 /* FIXME: OEM cursors/icons should be recognized */
1221 }
1222 /* Now assume non-shared cursor/icon */
1223
1224#ifdef __WIN32OS2__
1225 CURSORICONINFO *iconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
1226 if (!iconinfo) {
1227 dprintf(("ERROR: Invalid cursor!"));
1228 return 0;
1229 }
1230
1231 if(iconinfo->hColorBmp) {
1232 OSLibWinDestroyPointer(iconinfo->hColorBmp);
1233 }
1234 GlobalUnlock(hIcon);
1235 retv = GlobalFree( hIcon );
1236 ObjFreeHandle(handle);
1237
1238 return (flags & CID_RESOURCE)? retv : TRUE;
1239#else
1240 retv = GlobalFree( handle );
1241 return (flags & CID_RESOURCE)? retv : TRUE;
1242#endif
1243}
1244
1245/***********************************************************************
1246 * CURSORICON_Copy
1247 *
1248 * Make a copy of a cursor or icon.
1249 */
1250static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
1251{
1252 char *ptrOld, *ptrNew;
1253 int size;
1254 HGLOBAL hNew;
1255
1256 handle = ObjGetHandleData(handle, USEROBJ_CURSORICON);
1257 if(handle == -1) {
1258 dprintf(("ERROR: Invalid cursor/icon!"));
1259 return 0;
1260 }
1261
1262 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
1263
1264 size = GlobalSize( handle );
1265 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
1266#ifdef __WIN32OS2__
1267 if(hNew == NULL) {
1268 dprintf(("ERROR: CURSORICON_Copy GlobalAlloc failed!!"));
1269 return NULL;
1270 }
1271#endif
1272 ptrNew = (char *)GlobalLock( hNew );
1273 memcpy( ptrNew, ptrOld, size );
1274 GlobalUnlock( handle );
1275 GlobalUnlock( hNew );
1276
1277#ifdef __WIN32OS2__
1278 HICON hIcon;
1279 if(ObjAllocateHandle(&hIcon, (DWORD)hNew, USEROBJ_CURSORICON) == FALSE) {
1280 GlobalFree(hNew);
1281 dprintf(("ERROR: CURSORICON_Copy ObjAllocateHandle failed!!"));
1282 return 0;
1283 }
1284 return hIcon;
1285#else
1286 return hNew;
1287#endif
1288}
1289
1290/*************************************************************************
1291 * CURSORICON_ExtCopy
1292 *
1293 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1294 *
1295 * PARAMS
1296 * Handle [I] handle to an Image
1297 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1298 * iDesiredCX [I] The Desired width of the Image
1299 * iDesiredCY [I] The desired height of the Image
1300 * nFlags [I] The flags from CopyImage
1301 *
1302 * RETURNS
1303 * Success: The new handle of the Image
1304 *
1305 * NOTES
1306 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1307 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
1308 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1309 *
1310 *
1311 */
1312HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
1313 INT iDesiredCX, INT iDesiredCY,
1314 UINT nFlags)
1315{
1316 HGLOBAL hNew=0;
1317
1318#ifdef __WIN32OS2__
1319 HICON hIcon = ObjGetHandleData(Handle, USEROBJ_CURSORICON);
1320 if(hIcon == -1) {
1321 dprintf(("ERROR: Invalid cursor/icon!"));
1322 return 0;
1323 }
1324
1325#else
1326 if(Handle == 0)
1327 {
1328 return 0;
1329 }
1330#endif
1331 /* Best Fit or Monochrome */
1332 if( (nFlags & LR_COPYFROMRESOURCE
1333 && (iDesiredCX > 0 || iDesiredCY > 0))
1334 || nFlags & LR_MONOCHROME)
1335 {
1336#ifdef __WIN32OS2__
1337 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1338#else
1339 ICONCACHE* pIconCache = CURSORICON_FindCache(Handle);
1340#endif
1341
1342 /* Not Found in Cache, then do a straight copy
1343 */
1344 if(pIconCache == NULL)
1345 {
1346#ifdef __WIN32OS2__
1347 hNew = CURSORICON_Copy(Handle);
1348#else
1349 hNew = CURSORICON_Copy(0, Handle);
1350#endif
1351 if(nFlags & LR_COPYFROMRESOURCE)
1352 {
1353 dprintf(("WARNING: LR_COPYFROMRESOURCE: Failed to load from cache\n"));
1354 }
1355 }
1356 else
1357 {
1358 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1359 LPBYTE pBits;
1360 HANDLE hMem;
1361 HRSRC hRsrc;
1362 DWORD dwBytesInRes;
1363 WORD wResId;
1364 DWORD dwResGroupId;
1365 HINSTANCE hInstance;
1366 CURSORICONINFO *iconinfo;
1367 CURSORICONDIR *pDir;
1368 CURSORICONDIRENTRY *pDirEntry;
1369 BOOL bIsIcon = (nType == IMAGE_ICON);
1370
1371#ifdef __WIN32OS2__
1372 iconinfo = (CURSORICONINFO *)GlobalLock( hIcon );
1373#else
1374 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
1375#endif
1376 if(iconinfo == NULL) {
1377 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
1378 }
1379 hInstance = iconinfo->hInstance;
1380 dwResGroupId = iconinfo->dwResGroupId;
1381 GlobalUnlock( Handle );
1382 if(dwResGroupId == -1) {
1383 //todo: if scaling is necessary..
1384 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
1385 hNew = CURSORICON_Copy(Handle);
1386 return hNew;
1387 }
1388
1389 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1390 */
1391 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1392 || (iDesiredCX == 0 && iDesiredCY == 0))
1393 {
1394 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
1395 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
1396 }
1397
1398 /* Retreive the CURSORICONDIRENTRY
1399 */
1400 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
1401 if(!hRsrc) {
1402 goto notfound;
1403 }
1404
1405 if (!(hMem = LoadResource( hInstance, hRsrc)))
1406 {
1407 goto notfound;
1408 }
1409 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1410 {
1411 goto notfound;
1412 }
1413
1414 /* Find Best Fit
1415 */
1416 if(bIsIcon)
1417 {
1418 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1419 pDir, iDesiredCX, iDesiredCY, 256);
1420 }
1421 else
1422 {
1423 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1424 pDir, iDesiredCX, iDesiredCY, 1);
1425 }
1426
1427 wResId = pDirEntry->wResId;
1428 dwBytesInRes = pDirEntry->dwBytesInRes;
1429 FreeResource(hMem);
1430
1431 /* Get the Best Fit
1432 */
1433 if (!(hRsrc = FindResourceW(hInstance ,
1434 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
1435 {
1436 goto notfound;
1437 }
1438 if (!(hMem = LoadResource( hInstance, hRsrc )))
1439 {
1440 goto notfound;
1441 }
1442
1443 pBits = (LPBYTE)LockResource( hMem );
1444
1445 if(nFlags & LR_DEFAULTSIZE)
1446 {
1447 iTargetCY = GetSystemMetrics(SM_CYICON);
1448 iTargetCX = GetSystemMetrics(SM_CXICON);
1449 }
1450
1451 /* Create a New Icon with the proper dimension
1452 */
1453 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
1454 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1455 FreeResource(hMem);
1456 }
1457 }
1458 else
1459 {
1460 hNew = CURSORICON_Copy(Handle);
1461 }
1462 return hNew;
1463
1464notfound:
1465 dprintf(("WARNING: unable to find resource associated with icon/cursor -> copy without scaling!"));
1466 hNew = CURSORICON_Copy(Handle);
1467 return hNew;
1468}
1469
1470/**********************************************************************
1471 * CURSORICON_FindBestIcon
1472 *
1473 * Find the icon closest to the requested size and number of colors.
1474 */
1475static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
1476 int height, int colors )
1477{
1478 int i;
1479 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1480 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
1481 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
1482
1483 if (dir->idCount < 1)
1484 {
1485 dprintf(("Empty directory!\n" ));
1486 return NULL;
1487 }
1488 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1489
1490 /* Find Best Fit */
1491 iTotalDiff = 0xFFFFFFFF;
1492 iColorDiff = 0xFFFFFFFF;
1493 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1494 {
1495 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1496 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1497
1498 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1499 {
1500 iXDiff = iTempXDiff;
1501 iYDiff = iTempYDiff;
1502 iTotalDiff = iXDiff + iYDiff;
1503 }
1504 }
1505
1506 /* Find Best Colors for Best Fit */
1507 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1508 {
1509 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1510 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1511 {
1512#ifdef __WIN32OS2__
1513 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1514#else
1515 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1516#endif
1517 if(iColorDiff > iTempColorDiff)
1518 {
1519 bestEntry = entry;
1520 iColorDiff = iTempColorDiff;
1521 }
1522 }
1523 }
1524////testestest
1525 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1526 return bestEntry;
1527}
1528
1529
1530/**********************************************************************
1531 * CURSORICON_FindBestCursor
1532 *
1533 * Find the cursor closest to the requested size.
1534 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1535 * ignored too
1536 */
1537static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1538 int width, int height, int color)
1539{
1540 int i, maxwidth, maxheight;
1541 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1542
1543 if (dir->idCount < 1)
1544 {
1545 dprintf(("Empty directory!\n" ));
1546 return NULL;
1547 }
1548 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1549
1550 /* Double height to account for AND and XOR masks */
1551
1552 height *= 2;
1553
1554 /* First find the largest one smaller than or equal to the requested size*/
1555
1556 maxwidth = maxheight = 0;
1557 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1558 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1559 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1560 (entry->wBitCount == 1))
1561 {
1562 bestEntry = entry;
1563 maxwidth = entry->ResInfo.cursor.wWidth;
1564 maxheight = entry->ResInfo.cursor.wHeight;
1565 }
1566 if (bestEntry) return bestEntry;
1567
1568 /* Now find the smallest one larger than the requested size */
1569
1570 maxwidth = maxheight = 255;
1571 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1572 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1573 (entry->wBitCount == 1))
1574 {
1575 bestEntry = entry;
1576 maxwidth = entry->ResInfo.cursor.wWidth;
1577 maxheight = entry->ResInfo.cursor.wHeight;
1578 }
1579
1580 return bestEntry;
1581}
1582/**********************************************************************
1583 * LookupIconIdFromDirectoryEx (USER.364)
1584 *
1585 * FIXME: exact parameter sizes
1586 */
1587INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1588 INT width, INT height, UINT cFlag )
1589{
1590 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1591 UINT retVal = 0;
1592
1593 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1594 if( dir && !dir->idReserved && (dir->idType & 3) )
1595 {
1596 CURSORICONDIRENTRY* entry;
1597 HDC hdc;
1598 UINT palEnts;
1599 int colors;
1600 hdc = GetDC(0);
1601 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1602 if (palEnts == 0)
1603 palEnts = 256;
1604 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1605
1606 ReleaseDC(0, hdc);
1607
1608 if( bIcon )
1609 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1610 else
1611 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1612
1613 if( entry ) retVal = entry->wResId;
1614 }
1615 else dprintf(("invalid resource directory\n"));
1616 return retVal;
1617}
1618/**********************************************************************
1619 * LookupIconIdFromDirectory (USER32.379)
1620 */
1621INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1622{
1623 return LookupIconIdFromDirectoryEx( dir, bIcon,
1624 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1625 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1626}
1627//******************************************************************************
1628//******************************************************************************
1629
1630//ICON cache implementation (Wine code)
1631
1632/**********************************************************************
1633 * CURSORICON_FindSharedIcon
1634 */
1635static HANDLE CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
1636{
1637 HANDLE handle = 0;
1638 ICONCACHE *ptr;
1639
1640 EnterCriticalSection( &IconCrst );
1641
1642 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1643 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
1644 {
1645 ptr->count++;
1646 handle = ptr->handle;
1647 break;
1648 }
1649
1650 LeaveCriticalSection( &IconCrst );
1651
1652 return handle;
1653}
1654
1655/*************************************************************************
1656 * CURSORICON_FindCache
1657 *
1658 * Given a handle, find the corresponding cache element
1659 *
1660 * PARAMS
1661 * Handle [I] handle to an Image
1662 *
1663 * RETURNS
1664 * Success: The cache entry
1665 * Failure: NULL
1666 *
1667 */
1668static ICONCACHE* CURSORICON_FindCache(HANDLE handle)
1669{
1670 ICONCACHE *ptr;
1671 ICONCACHE *pRet=NULL;
1672 BOOL IsFound = FALSE;
1673 int count;
1674
1675 EnterCriticalSection( &IconCrst );
1676
1677 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
1678 {
1679 if ( handle == ptr->handle )
1680 {
1681 IsFound = TRUE;
1682 pRet = ptr;
1683 }
1684 }
1685
1686 LeaveCriticalSection( &IconCrst );
1687
1688 return pRet;
1689}
1690
1691/**********************************************************************
1692 * CURSORICON_AddSharedIcon
1693 */
1694static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HANDLE handle )
1695{
1696 ICONCACHE *ptr = (ICONCACHE *)HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
1697 if ( !ptr ) return;
1698
1699 ptr->hModule = hModule;
1700 ptr->hRsrc = hRsrc;
1701 ptr->handle = handle;
1702 ptr->hGroupRsrc = hGroupRsrc;
1703 ptr->count = 1;
1704
1705 EnterCriticalSection( &IconCrst );
1706 ptr->next = IconAnchor;
1707 IconAnchor = ptr;
1708 LeaveCriticalSection( &IconCrst );
1709}
1710
1711/**********************************************************************
1712 * CURSORICON_DelSharedIcon
1713 */
1714static INT CURSORICON_DelSharedIcon( HANDLE handle )
1715{
1716 INT count = -1;
1717 ICONCACHE *ptr;
1718
1719 EnterCriticalSection( &IconCrst );
1720
1721 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
1722 if ( ptr->handle == handle )
1723 {
1724 if ( ptr->count > 0 ) ptr->count--;
1725 count = ptr->count;
1726 break;
1727 }
1728
1729 LeaveCriticalSection( &IconCrst );
1730
1731 return count;
1732}
1733
1734/**********************************************************************
1735 * CURSORICON_FreeModuleIcons
1736 */
1737void CURSORICON_FreeModuleIcons( HMODULE hModule )
1738{
1739 ICONCACHE **ptr = &IconAnchor;
1740
1741 EnterCriticalSection( &IconCrst );
1742
1743 while ( *ptr )
1744 {
1745 if ( (*ptr)->hModule == hModule )
1746 {
1747 ICONCACHE *freePtr = *ptr;
1748 *ptr = freePtr->next;
1749
1750#ifdef __WIN32OS2__
1751 CURSORICON_Destroy(freePtr->handle, CID_NONSHARED);
1752#else
1753 GlobalFree( freePtr->handle );
1754#endif
1755 HeapFree( GetProcessHeap(), 0, freePtr );
1756 continue;
1757 }
1758 ptr = &(*ptr)->next;
1759 }
1760
1761 LeaveCriticalSection( &IconCrst );
1762}
1763
Note: See TracBrowser for help on using the repository browser.