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

Last change on this file since 5120 was 4825, checked in by sandervl, 25 years ago

hook, mdi & focus fixes

File size: 40.7 KB
Line 
1/* $Id: winicon.cpp,v 1.19 2000-12-17 15:04:14 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 *
17 * Project Odin Software License can be found in LICENSE.TXT
18 *
19 */
20#include <os2win.h>
21#include <stdio.h>
22#include <string.h>
23#include <winicon.h>
24#include <win\cursoricon.h>
25#include "dib.h"
26#include <heapstring.h>
27#include <win\virtual.h>
28#include "initterm.h"
29
30#define DBG_LOCALLOG DBG_winicon
31#include "dbglocal.h"
32
33static WORD ICON_HOTSPOT = 0x4242;
34
35static HGLOBAL CURSORICON_CreateFromResource( HINSTANCE hInstance, DWORD dwResGroupId, HGLOBAL hObj, LPBYTE bits,
36 UINT cbSize, BOOL bIcon, DWORD dwVersion, INT width, INT height, UINT loadflags );
37static HGLOBAL CURSORICON_Copy( HGLOBAL handle );
38static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
39 int height, int colors );
40static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
41 int width, int height, int color);
42BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
43 CURSORICONDIR **res, LPBYTE **ptr);
44
45/***********************************************************************
46 * CreateIcon (USER32.75)
47 */
48HICON WIN32API CreateIcon(HINSTANCE hInstance, INT nWidth,
49 INT nHeight, BYTE bPlanes, BYTE bBitsPixel,
50 LPCVOID lpANDbits, LPCVOID lpXORbits )
51{
52 CURSORICONINFO info;
53
54 dprintf(("USER32: CreateIcon (%d,%d), %d, %x, %x", nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits));
55
56 info.ptHotSpot.x = ICON_HOTSPOT;
57 info.ptHotSpot.y = ICON_HOTSPOT;
58 info.nWidth = nWidth;
59 info.nHeight = nHeight;
60 info.nWidthBytes = 0;
61 info.bPlanes = bPlanes;
62 info.bBitsPerPixel = bBitsPixel;
63 info.hInstance = hInstance;
64 info.dwResGroupId = -1;
65 return CreateCursorIconIndirect(0, &info, lpANDbits, lpXORbits);
66}
67/**********************************************************************
68 * CreateIconFromResource (USER32.76)
69 */
70HICON WIN32API CreateIconFromResource(LPBYTE bits, UINT cbSize,
71 BOOL bIcon, DWORD dwVersion)
72{
73 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
74}
75//******************************************************************************
76//******************************************************************************
77HICON WIN32API CreateIconFromResourceEx(LPBYTE bits, UINT cbSize,
78 BOOL bIcon, DWORD dwVersion,
79 INT width, INT height,
80 UINT cFlag )
81{
82 dprintf(("USER32: CreateIconFromResourceEx %X %d %d %X %d %d %X,", bits, cbSize, bIcon, dwVersion, width, height, cFlag));
83 return CURSORICON_CreateFromResource(0, -1, 0, bits, cbSize, bIcon, dwVersion, width, height, cFlag );
84}
85/**********************************************************************
86 * CreateIconIndirect (USER32.78)
87 */
88HICON WINAPI CreateIconIndirect(ICONINFO *iconinfo)
89{
90 BITMAP bmpXor,bmpAnd;
91 HICON hObj;
92 int sizeXor,sizeAnd;
93
94 dprintf(("USER32: CreateIconIndirect %x", iconinfo));
95
96 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
97 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
98
99 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
100 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
101
102 hObj = GlobalAlloc( GMEM_MOVEABLE, sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
103 if (hObj)
104 {
105 CURSORICONINFO *info;
106
107 info = (CURSORICONINFO *)GlobalLock( hObj );
108
109 /* If we are creating an icon, the hotspot is unused */
110 if (iconinfo->fIcon)
111 {
112 info->ptHotSpot.x = ICON_HOTSPOT;
113 info->ptHotSpot.y = ICON_HOTSPOT;
114 }
115 else
116 {
117 info->ptHotSpot.x = iconinfo->xHotspot;
118 info->ptHotSpot.y = iconinfo->yHotspot;
119 }
120
121 info->nWidth = bmpXor.bmWidth;
122 info->nHeight = bmpXor.bmHeight;
123 info->nWidthBytes = bmpXor.bmWidthBytes;
124 info->bPlanes = bmpXor.bmPlanes;
125 info->bBitsPerPixel = bmpXor.bmBitsPixel;
126 info->hInstance = -1;
127 info->dwResGroupId = -1;
128 /* Transfer the bitmap bits to the CURSORICONINFO structure */
129 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
130 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
131 GlobalUnlock(hObj);
132 }
133 else {
134 dprintf(("ERROR: CreateIconIndirect GlobalAlloc failed!!"));
135 }
136 return hObj;
137}
138//******************************************************************************
139//******************************************************************************
140BOOL WIN32API DestroyIcon( HICON hIcon)
141{
142 dprintf(("USER32: DestroyIcon %x", hIcon));
143 return CURSORICON_Destroy( hIcon, 0 );
144}
145//******************************************************************************
146//******************************************************************************
147HICON WIN32API CopyIcon( HICON hIcon)
148{
149 dprintf(("USER32: CopyIcon %x", hIcon));
150 return CURSORICON_Copy( hIcon );
151}
152/**********************************************************************
153 * GetIconInfo (USER32.242)
154 */
155BOOL WINAPI GetIconInfo(HICON hIcon, ICONINFO *iconinfo)
156{
157 CURSORICONINFO *ciconinfo;
158
159 dprintf(("GetIconInfo %x %x", hIcon, iconinfo));
160
161 ciconinfo = (CURSORICONINFO *)GlobalLock((HGLOBAL)hIcon);
162 if (!ciconinfo)
163 return FALSE;
164
165 if((ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
166 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT))
167 {
168 iconinfo->fIcon = TRUE;
169 iconinfo->xHotspot = ciconinfo->nWidth / 2;
170 iconinfo->yHotspot = ciconinfo->nHeight / 2;
171 }
172 else
173 {
174 iconinfo->fIcon = FALSE;
175 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
176 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
177 }
178
179 //Create new bitmaps for the color and mask data; application is responsible
180 //for deleteing them (according to docs & verified in NT4)
181 if(ciconinfo->bBitsPerPixel > 1)
182 {
183 BITMAPINFO* pInfo;
184 int colorsize = 0;
185 int coloroff;
186
187 HDC hdc = CreateCompatibleDC(0);
188
189 if(ciconinfo->bBitsPerPixel <= 8) {
190 colorsize = (1<<ciconinfo->bBitsPerPixel)*sizeof(RGBQUAD);
191 }
192 else {
193 colorsize = 3*sizeof(DWORD); //color masks
194 }
195 pInfo = (BITMAPINFO *)malloc(ciconinfo->nHeight * ciconinfo->nWidthBytes + colorsize + sizeof(BITMAPINFO));
196 memset(pInfo, 0, sizeof(BITMAPINFO)+colorsize);
197
198 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
199 pInfo->bmiHeader.biWidth = ciconinfo->nWidth;
200 pInfo->bmiHeader.biHeight = ciconinfo->nHeight,
201 pInfo->bmiHeader.biPlanes = ciconinfo->bPlanes;
202 pInfo->bmiHeader.biBitCount = ciconinfo->bBitsPerPixel;
203 pInfo->bmiHeader.biSizeImage= ciconinfo->nHeight * ciconinfo->nWidthBytes;
204
205 //offset in cursorinfo memory
206 coloroff = ciconinfo->nHeight * BITMAP_GetWidthBytes (ciconinfo->nWidth, 1);
207
208 char *src = (char *)(ciconinfo + 1) + coloroff;
209 if(ciconinfo->bBitsPerPixel <= 8) {
210 src += colorsize; //no color masks in cursorinfo data for bpp > 8
211 }
212 if(ciconinfo->bBitsPerPixel <= 8) {
213 memcpy(&pInfo->bmiColors[0], (char *)(ciconinfo + 1) + coloroff, colorsize);
214 }
215 //else TODO: color masks (curerntly unused in CreateDIBitmap)
216
217 iconinfo->hbmColor = CreateDIBitmap(hdc, &pInfo->bmiHeader, CBM_INIT, src, pInfo, DIB_RGB_COLORS);
218
219 free(pInfo);
220 DeleteDC(hdc);
221 }
222 else {
223 iconinfo->hbmColor = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
224 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
225 (char *)(ciconinfo + 1)
226 + ciconinfo->nHeight *
227 BITMAP_GetWidthBytes (ciconinfo->nWidth, 1) );
228 }
229
230 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
231 1, 1, (char *)(ciconinfo + 1));
232
233 GlobalUnlock(hIcon);
234
235 return TRUE;
236}
237/***********************************************************************
238 * CreateCursorIconIndirect (USER.408)
239 */
240HGLOBAL WIN32API CreateCursorIconIndirect( HINSTANCE hInstance,
241 CURSORICONINFO *info,
242 LPCVOID lpANDbits,
243 LPCVOID lpXORbits )
244{
245 HGLOBAL handle;
246 char *ptr;
247 int sizeAnd, sizeXor;
248
249 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
250 info->nWidthBytes = BITMAP_GetWidthBytes(info->nWidth,info->bBitsPerPixel);
251 sizeXor = info->nHeight * info->nWidthBytes;
252 sizeAnd = info->nHeight * BITMAP_GetWidthBytes( info->nWidth, 1 );
253 if (!(handle = GlobalAlloc( GMEM_MOVEABLE,
254 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
255 return 0;
256 ptr = (char *)GlobalLock( handle );
257 memcpy( ptr, info, sizeof(*info) );
258 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
259 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
260 GlobalUnlock( handle );
261 return handle;
262}
263/**********************************************************************
264 * CURSORICON_Load
265 *
266 * Load a cursor or icon from resource or file.
267 */
268HGLOBAL CURSORICON_Load( HINSTANCE hInstance, LPCWSTR name,
269 INT width, INT height, INT colors,
270 BOOL fCursor, UINT loadflags )
271{
272 HANDLE handle = 0, h = 0;
273 HANDLE hRsrc;
274 CURSORICONDIR *dir;
275 CURSORICONDIRENTRY *dirEntry;
276 LPBYTE bits;
277
278 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
279 {
280 LPBYTE *ptr;
281 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
282 return 0;
283 if (fCursor)
284 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
285 else
286 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
287 bits = ptr[dirEntry->wResId-1];
288 h = CURSORICON_CreateFromResource( 0, -1, 0, bits, dirEntry->dwBytesInRes,
289 !fCursor, 0x00030000, width, height, loadflags);
290 HeapFree( GetProcessHeap(), 0, dir );
291 HeapFree( GetProcessHeap(), 0, ptr );
292 }
293 else /* Load from resource */
294 {
295 HANDLE hGroupRsrc;
296 WORD wResId;
297 DWORD dwBytesInRes;
298 BOOL bIsGroup = TRUE;
299
300 /* Get directory resource ID */
301 if (!hInstance)
302 {
303 hRsrc = FindResourceW(hInstanceUser32, name, fCursor ? RT_CURSORW : RT_ICONW);
304 if(!hRsrc) {
305 hRsrc = FindResourceW(hInstanceUser32, name, fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW);
306 }
307 else bIsGroup = FALSE;
308
309 if(!hRsrc) return 0;
310
311 hInstance = hInstanceUser32;
312 }
313 else {
314 hRsrc = FindResourceW(hInstance, name, fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW);
315 if(!hRsrc) return 0;
316 }
317 hGroupRsrc = hRsrc;
318
319 if(bIsGroup) {
320 /* Find the best entry in the directory */
321
322 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
323 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
324
325 if (fCursor)
326 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
327 width, height, 1);
328 else
329 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
330 width, height, colors );
331 if (!dirEntry) return 0;
332 wResId = dirEntry->wResId;
333 dwBytesInRes = dirEntry->dwBytesInRes;
334 FreeResource( handle );
335
336 /* Load the resource */
337 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
338 fCursor ? RT_CURSORW : RT_ICONW ))) return 0;
339 }
340
341 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
342 bits = (LPBYTE)LockResource( handle );
343 h = CURSORICON_CreateFromResource( hInstance, (DWORD)name, 0, bits, dwBytesInRes,
344 !fCursor, 0x00030000, width, height, loadflags);
345 FreeResource( handle );
346
347 }
348
349 return h;
350}
351
352/*********************************************************************
353 * The main purpose of this function is to create fake resource directory
354 * and fake resource entries. There are several reasons for this:
355 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
356 * fields
357 * There are some "bad" cursor files which do not have
358 * bColorCount initialized but instead one must read this info
359 * directly from corresponding DIB sections
360 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
361 */
362BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
363 CURSORICONDIR **res, LPBYTE **ptr)
364{
365 LPBYTE _free;
366 CURSORICONFILEDIR *bits;
367 int entries, size, i;
368 HANDLE hMapping = 0;
369
370 *res = NULL;
371 *ptr = NULL;
372
373 hMapping = VIRTUAL_MapFileW( filename, (LPVOID *)&bits, TRUE);
374 if(hMapping == INVALID_HANDLE_VALUE)
375 return FALSE;
376
377 /* FIXME: test for inimated icons
378 * hack to load the first icon from the *.ani file
379 */
380 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
381 {
382 LPBYTE pos = (LPBYTE) bits;
383 dprintf(("Animated icons not correctly implemented! %p \n", bits));
384
385 for (;;)
386 {
387 if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
388 {
389 dprintf(("icon entry found! %p\n", bits));
390 pos+=4;
391 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
392 {
393 goto fail;
394 }
395 bits=(CURSORICONFILEDIR*)(pos+4);
396 dprintf(("icon size ok. offset=%p \n", bits));
397 break;
398 }
399 pos+=2;
400 if (pos>=(LPBYTE)bits+766) goto fail;
401 }
402 }
403 if (!(entries = bits->idCount)) goto fail;
404 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
405 _free = (LPBYTE) size;
406
407 for (i=0; i < entries; i++)
408 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
409
410 if (!(*ptr = (LPBYTE *)HeapAlloc( GetProcessHeap(), 0,
411 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
412 if (!(*res = (CURSORICONDIR *)HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
413
414 _free = (LPBYTE)(*res) + (int)_free;
415 memcpy((*res), bits, 6);
416 for (i=0; i<entries; i++)
417 {
418 ((LPBYTE*)(*ptr))[i] = _free;
419 if (fCursor) {
420 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
421 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
422 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
423 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
424 _free+=sizeof(POINT16);
425 }
426 else {
427 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
428 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
429 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
430 }
431 (*res)->idEntries[i].wPlanes=1;
432 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
433 bits->idEntries[i].dwDIBOffset))->biBitCount;
434 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
435 (*res)->idEntries[i].wResId=i+1;
436
437 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
438 (*res)->idEntries[i].dwBytesInRes);
439 _free += (*res)->idEntries[i].dwBytesInRes;
440 }
441 UnmapViewOfFile( bits );
442 CloseHandle(hMapping);
443 return TRUE;
444
445fail:
446 if (*res) HeapFree( GetProcessHeap(), 0, *res );
447 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
448
449 UnmapViewOfFile( bits );
450 CloseHandle(hMapping);
451 return FALSE;
452}
453
454/**********************************************************************
455 * CURSORICON_CreateFromResource
456 *
457 * Create a cursor or icon from in-memory resource template.
458 *
459 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
460 * with cbSize parameter as well.
461 */
462static HGLOBAL CURSORICON_CreateFromResource( HINSTANCE hInstance, DWORD dwResGroupId, HGLOBAL hObj, LPBYTE bits,
463 UINT cbSize, BOOL bIcon, DWORD dwVersion,
464 INT width, INT height, UINT loadflags )
465{
466 int sizeAnd, sizeXor;
467 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
468 BITMAP bmpXor, bmpAnd;
469 POINT16 hotspot;
470 BITMAPINFO *bmi;
471 HDC hdc = 0;
472 BOOL DoStretch;
473 INT size, colortablesize, bwsize, colorsize;
474
475 hotspot.x = ICON_HOTSPOT;
476 hotspot.y = ICON_HOTSPOT;
477
478//testestest
479 dprintf(("CURSORICON_CreateFromResource %x %x %x %x %d", hInstance, dwResGroupId, hObj, bits, cbSize));
480
481 if (dwVersion == 0x00020000)
482 {
483 dprintf(("CURSORICON_CreateFromResource 2.xx resources are not supported"));
484 return 0;
485 }
486
487 if (bIcon) {
488 bmi = (BITMAPINFO *)bits;
489 }
490 else /* get the hotspot */
491 {
492 POINT16 *pt = (POINT16 *)bits;
493 hotspot = *pt;
494 bmi = (BITMAPINFO *)(pt + 1);
495 }
496 size = DIB_BitmapInfoSize(bmi, DIB_RGB_COLORS);
497
498 if (!width) width = bmi->bmiHeader.biWidth;
499 if (!height) height = bmi->bmiHeader.biHeight/2;
500
501 DoStretch = (bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width);
502
503 colorsize = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2, bmi->bmiHeader.biBitCount);
504 bwsize = (bmi->bmiHeader.biWidth * bmi->bmiHeader.biHeight/2)/8;
505
506 /* Check bitmap header */
507 if((bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
508 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
509 bmi->bmiHeader.biCompression != BI_RGB) )
510 {
511 return 0;
512 }
513
514 if( (hdc = GetDC( 0 )) )
515 {
516 BITMAPINFO* pInfo;
517
518 /* Make sure we have room for the monochrome bitmap later on.
519 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
520 * up to and including the biBitCount. In-memory icon resource
521 * format is as follows:
522 *
523 * BITMAPINFOHEADER icHeader // DIB header
524 * RGBQUAD icColors[] // Color table
525 * BYTE icXOR[] // DIB bits for XOR mask
526 * BYTE icAND[] // DIB bits for AND mask
527 */
528
529 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
530 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
531 {
532 memcpy( pInfo, bmi, size );
533 pInfo->bmiHeader.biHeight /= 2;
534
535 /* Create the XOR bitmap */
536 if (DoStretch)
537 {
538 if(bIcon)
539 {
540 hXorBits = CreateCompatibleBitmap(hdc, width, height);
541 }
542 else
543 {
544 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
545 }
546 if(hXorBits)
547 {
548 HBITMAP hOld;
549 HDC hMem = CreateCompatibleDC(hdc);
550 BOOL res;
551
552 if (hMem) {
553 hOld = SelectObject(hMem, hXorBits);
554 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
555 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
556 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
557 SelectObject(hMem, hOld);
558 DeleteDC(hMem);
559 }
560 else res = FALSE;
561 if (!res) {
562 DeleteObject(hXorBits);
563 hXorBits = 0;
564 }
565 }
566 }
567 else
568 {
569 hXorBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
570 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
571 }
572 if( hXorBits )
573 {
574 char* xbits = (char *)bmi + size + DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
575 bmi->bmiHeader.biHeight,
576 bmi->bmiHeader.biBitCount) / 2;
577
578 pInfo->bmiHeader.biBitCount = 1;
579 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
580 {
581 RGBQUAD *rgb = pInfo->bmiColors;
582
583 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
584 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
585 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
586 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
587 }
588 else
589 {
590 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
591
592 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
593 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
594 }
595
596 /* Create the AND bitmap */
597 if (DoStretch)
598 {
599 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL)))
600 {
601 HBITMAP hOld;
602 HDC hMem = CreateCompatibleDC(hdc);
603 BOOL res;
604
605 if (hMem) {
606 hOld = SelectObject(hMem, hAndBits);
607//SvL: This also doesn't work as StretchDIBits doesn't handle 1bpp bitmaps correctly
608//--------->>> hack alert!
609#if 1
610 HBITMAP hBmp, hOld1;
611 HDC hMem1;
612
613 hMem1 = CreateCompatibleDC(hdc);
614
615 int linewidth = BITMAP_GetWidthBytes(pInfo->bmiHeader.biWidth, 1);
616
617 char *newpix = (char *)malloc(linewidth*pInfo->bmiHeader.biHeight);
618
619 newpix += ((pInfo->bmiHeader.biHeight-1)*linewidth);
620
621 if(cbSize - size - colorsize - bwsize == bwsize)
622 {//this means an AND and XOR mask is present (interleaved; and/xor)
623 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
624 memcpy(newpix, xbits, linewidth);
625 newpix -= linewidth;
626 xbits += linewidth*2;
627 }
628 }
629 else {
630 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
631 memcpy(newpix, xbits, linewidth);
632 newpix -= linewidth;
633 xbits += linewidth;
634 }
635 }
636 newpix += linewidth;
637 hBmp = CreateBitmap(pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, 1, 1, newpix);
638 free(newpix);
639
640 hOld1 = SelectObject(hMem1, hBmp);
641
642 res = StretchBlt(hMem, 0, 0, width, height, hMem1, 0, 0, pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, SRCCOPY);
643
644 SelectObject(hMem1, hOld1);
645 DeleteObject(hBmp);
646 DeleteDC(hMem1);
647
648
649#else
650 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
651 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
652 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
653#endif
654 SelectObject(hMem, hOld);
655 DeleteDC(hMem);
656 }
657 else res = FALSE;
658 if (!res) {
659 DeleteObject(hAndBits);
660 hAndBits = 0;
661 }
662 }
663 }
664 else {
665//SvL: Must use CreateBitmap here as CreateDIBitmap converts data to 8bpp (GetObjectA info -> 8 bpp)
666#if 1
667 int linewidth = BITMAP_GetWidthBytes(width, 1);
668
669 char *newpix = (char *)malloc(linewidth*height);
670
671 newpix += ((height-1)*linewidth);
672
673 if(cbSize - size - colorsize - bwsize == bwsize)
674 {//this means an AND and XOR mask is present (interleaved; and/xor)
675 for(int i=0;i<height;i++) {
676 memcpy(newpix, xbits, linewidth);
677 newpix -= linewidth;
678 xbits += linewidth*2;
679 }
680 }
681 else {
682 for(int i=0;i<height;i++) {
683 memcpy(newpix, xbits, linewidth);
684 newpix -= linewidth;
685 xbits += linewidth;
686 }
687 }
688 newpix += linewidth;
689 hAndBits = CreateBitmap(width, height, 1, 1, newpix);
690
691 free(newpix);
692
693#else
694 hAndBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
695 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
696#endif
697 }
698 if( !hAndBits )
699 DeleteObject( hXorBits );
700 }
701 HeapFree( GetProcessHeap(), 0, pInfo );
702 }
703 ReleaseDC( 0, hdc );
704 }
705
706 if( !hXorBits || !hAndBits )
707 {
708 dprintf(("\tunable to create an icon bitmap.\n"));
709 return 0;
710 }
711
712 /* Now create the CURSORICONINFO structure */
713 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
714 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
715 colortablesize = 0;
716
717 if(bmpXor.bmBitsPixel <= 8) {
718 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
719 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
720 }
721 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
722
723 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
724
725 if (hObj) hObj = GlobalReAlloc( hObj,
726 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
727 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
728 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
729 if (hObj)
730 {
731 CURSORICONINFO *info;
732
733 info = (CURSORICONINFO *)GlobalLock( hObj );
734 info->ptHotSpot.x = hotspot.x;
735 info->ptHotSpot.y = hotspot.y;
736 info->nWidth = bmpXor.bmWidth;
737 info->nHeight = bmpXor.bmHeight;
738 info->nWidthBytes = bmpXor.bmWidthBytes;
739 info->bPlanes = bmpXor.bmPlanes;
740 info->bBitsPerPixel = bmpXor.bmBitsPixel;
741 info->hColorBmp = hXorBits;
742 info->hInstance = hInstance;
743 info->dwResGroupId = dwResGroupId;
744
745 /* Transfer the bitmap bits to the CURSORICONINFO structure */
746 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
747
748 if(bmpXor.bmBitsPixel > 1)
749 {
750 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
751 HBITMAP oldbmp;
752
753 hdc = CreateCompatibleDC(0);
754
755 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
756 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
757 pInfo->bmiHeader.biPlanes = info->bPlanes;
758 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
759
760 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
761 if(colortablesize) {
762 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
763 }
764
765 DeleteDC(hdc);
766 free(pInfo);
767 }
768 else {
769 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
770 }
771 GlobalUnlock( hObj );
772 }
773
774 DeleteObject( hAndBits );
775 DeleteObject( hXorBits );
776 return hObj;
777}
778
779/**********************************************************************
780 * CURSORICON_Destroy (USER.610)
781 *
782 * This routine is actually exported from Win95 USER under the name
783 * DestroyIcon32 ... The behaviour implemented here should mimic
784 * the Win95 one exactly, especially the return values, which
785 * depend on the setting of various flags.
786 */
787WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
788{
789 WORD retv;
790
791#if 0
792 /* Check whether destroying active cursor */
793
794 if ( hActiveCursor == handle )
795 {
796 ERR_(cursor)("Destroying active cursor!\n" );
797 SetCursor( 0 );
798 }
799 /* Try shared cursor/icon first */
800
801 if ( !(flags & CID_NONSHARED) )
802 {
803 INT count = CURSORICON_DelSharedIcon( handle );
804
805 if ( count != -1 )
806 return (flags & CID_WIN32)? TRUE : (count == 0);
807
808 /* FIXME: OEM cursors/icons should be recognized */
809 }
810#endif
811 /* Now assume non-shared cursor/icon */
812
813 retv = GlobalFree( handle );
814 return (flags & CID_RESOURCE)? retv : TRUE;
815}
816
817/***********************************************************************
818 * CURSORICON_Copy
819 *
820 * Make a copy of a cursor or icon.
821 */
822static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
823{
824 char *ptrOld, *ptrNew;
825 int size;
826 HGLOBAL hNew;
827
828 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
829
830 size = GlobalSize( handle );
831 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
832 ptrNew = (char *)GlobalLock( hNew );
833 memcpy( ptrNew, ptrOld, size );
834 GlobalUnlock( handle );
835 GlobalUnlock( hNew );
836 return hNew;
837}
838
839/*************************************************************************
840 * CURSORICON_ExtCopy
841 *
842 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
843 *
844 * PARAMS
845 * Handle [I] handle to an Image
846 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
847 * iDesiredCX [I] The Desired width of the Image
848 * iDesiredCY [I] The desired height of the Image
849 * nFlags [I] The flags from CopyImage
850 *
851 * RETURNS
852 * Success: The new handle of the Image
853 *
854 * NOTES
855 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
856 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
857 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
858 *
859 *
860 */
861HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
862 INT iDesiredCX, INT iDesiredCY,
863 UINT nFlags)
864{
865 HGLOBAL hNew=0;
866
867 if(Handle == 0)
868 {
869 return 0;
870 }
871 /* Best Fit or Monochrome */
872 if( (nFlags & LR_COPYFROMRESOURCE
873 && (iDesiredCX > 0 || iDesiredCY > 0))
874 || nFlags & LR_MONOCHROME)
875 {
876 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
877 LPBYTE pBits;
878 HANDLE hMem;
879 HRSRC hRsrc;
880 DWORD dwBytesInRes;
881 WORD wResId;
882 DWORD dwResGroupId;
883 HINSTANCE hInstance;
884 CURSORICONINFO *iconinfo;
885 CURSORICONDIR *pDir;
886 CURSORICONDIRENTRY *pDirEntry;
887 BOOL bIsIcon = (nType == IMAGE_ICON);
888
889 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
890 if(iconinfo == NULL) {
891 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
892 }
893 hInstance = iconinfo->hInstance;
894 dwResGroupId = iconinfo->dwResGroupId;
895 GlobalUnlock( Handle );
896 if(dwResGroupId == -1) {
897 //todo: if scaling is necessary..
898 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
899 hNew = CURSORICON_Copy(Handle);
900 return hNew;
901 }
902
903 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
904 */
905 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
906 || (iDesiredCX == 0 && iDesiredCY == 0))
907 {
908 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
909 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
910 }
911
912 /* Retreive the CURSORICONDIRENTRY
913 */
914 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
915 if(!hRsrc) {
916 goto notfound;
917 }
918
919 if (!(hMem = LoadResource( hInstance, hRsrc)))
920 {
921 goto notfound;
922 }
923 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
924 {
925 goto notfound;
926 }
927
928 /* Find Best Fit
929 */
930 if(bIsIcon)
931 {
932 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
933 pDir, iDesiredCX, iDesiredCY, 256);
934 }
935 else
936 {
937 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
938 pDir, iDesiredCX, iDesiredCY, 1);
939 }
940
941 wResId = pDirEntry->wResId;
942 dwBytesInRes = pDirEntry->dwBytesInRes;
943 FreeResource(hMem);
944
945 /* Get the Best Fit
946 */
947 if (!(hRsrc = FindResourceW(hInstance ,
948 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
949 {
950 goto notfound;
951 }
952 if (!(hMem = LoadResource( hInstance, hRsrc )))
953 {
954 goto notfound;
955 }
956
957 pBits = (LPBYTE)LockResource( hMem );
958
959 if(nFlags & LR_DEFAULTSIZE)
960 {
961 iTargetCY = GetSystemMetrics(SM_CYICON);
962 iTargetCX = GetSystemMetrics(SM_CXICON);
963 }
964
965 /* Create a New Icon with the proper dimension
966 */
967 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
968 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
969 FreeResource(hMem);
970 }
971 else
972 {
973 hNew = CURSORICON_Copy(Handle);
974 }
975 return hNew;
976
977notfound:
978 dprintf(("WARNING: unable to find resource associated with icon/cursor -> copy without scaling!"));
979 hNew = CURSORICON_Copy(Handle);
980 return hNew;
981}
982
983/**********************************************************************
984 * CURSORICON_FindBestIcon
985 *
986 * Find the icon closest to the requested size and number of colors.
987 */
988static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
989 int height, int colors )
990{
991 int i;
992 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
993 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
994 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
995
996 if (dir->idCount < 1)
997 {
998 dprintf(("Empty directory!\n" ));
999 return NULL;
1000 }
1001 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1002
1003 /* Find Best Fit */
1004 iTotalDiff = 0xFFFFFFFF;
1005 iColorDiff = 0xFFFFFFFF;
1006 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1007 {
1008 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1009 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1010
1011 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1012 {
1013 iXDiff = iTempXDiff;
1014 iYDiff = iTempYDiff;
1015 iTotalDiff = iXDiff + iYDiff;
1016 }
1017 }
1018
1019 /* Find Best Colors for Best Fit */
1020 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1021 {
1022 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1023 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1024 {
1025#ifdef __WIN32OS2__
1026 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1027#else
1028 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1029#endif
1030 if(iColorDiff > iTempColorDiff)
1031 {
1032 bestEntry = entry;
1033 iColorDiff = iTempColorDiff;
1034 }
1035 }
1036 }
1037////testestest
1038 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1039 return bestEntry;
1040}
1041
1042
1043/**********************************************************************
1044 * CURSORICON_FindBestCursor
1045 *
1046 * Find the cursor closest to the requested size.
1047 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1048 * ignored too
1049 */
1050static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1051 int width, int height, int color)
1052{
1053 int i, maxwidth, maxheight;
1054 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1055
1056 if (dir->idCount < 1)
1057 {
1058 dprintf(("Empty directory!\n" ));
1059 return NULL;
1060 }
1061 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1062
1063 /* Double height to account for AND and XOR masks */
1064
1065 height *= 2;
1066
1067 /* First find the largest one smaller than or equal to the requested size*/
1068
1069 maxwidth = maxheight = 0;
1070 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1071 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1072 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1073 (entry->wBitCount == 1))
1074 {
1075 bestEntry = entry;
1076 maxwidth = entry->ResInfo.cursor.wWidth;
1077 maxheight = entry->ResInfo.cursor.wHeight;
1078 }
1079 if (bestEntry) return bestEntry;
1080
1081 /* Now find the smallest one larger than the requested size */
1082
1083 maxwidth = maxheight = 255;
1084 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1085 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1086 (entry->wBitCount == 1))
1087 {
1088 bestEntry = entry;
1089 maxwidth = entry->ResInfo.cursor.wWidth;
1090 maxheight = entry->ResInfo.cursor.wHeight;
1091 }
1092
1093 return bestEntry;
1094}
1095/**********************************************************************
1096 * LookupIconIdFromDirectoryEx (USER.364)
1097 *
1098 * FIXME: exact parameter sizes
1099 */
1100INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1101 INT width, INT height, UINT cFlag )
1102{
1103 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1104 UINT retVal = 0;
1105
1106 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1107 if( dir && !dir->idReserved && (dir->idType & 3) )
1108 {
1109 CURSORICONDIRENTRY* entry;
1110 HDC hdc;
1111 UINT palEnts;
1112 int colors;
1113 hdc = GetDC(0);
1114 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1115 if (palEnts == 0)
1116 palEnts = 256;
1117 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1118
1119 ReleaseDC(0, hdc);
1120
1121 if( bIcon )
1122 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1123 else
1124 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1125
1126 if( entry ) retVal = entry->wResId;
1127 }
1128 else dprintf(("invalid resource directory\n"));
1129 return retVal;
1130}
1131/**********************************************************************
1132 * LookupIconIdFromDirectory (USER32.379)
1133 */
1134INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1135{
1136 return LookupIconIdFromDirectoryEx( dir, bIcon,
1137 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1138 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1139}
1140//******************************************************************************
1141//******************************************************************************
Note: See TracBrowser for help on using the repository browser.