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

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

cursor handling updates

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