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

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

icon color fix in release build

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