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

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

Workaround for StretchDIBits added for icon mask stretching

File size: 40.7 KB
Line 
1/* $Id: winicon.cpp,v 1.17 2000-11-22 21:04:32 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 //TODO: rearrange mask if and & xor mask present!!!!!
600 if(cbSize - size - colorsize - bwsize == bwsize)
601 {
602 dprintf(("TODO: rearrange mask because and & xor mask present!!!!!"));
603 }
604 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL)))
605 {
606 HBITMAP hOld;
607 HDC hMem = CreateCompatibleDC(hdc);
608 BOOL res;
609
610 if (hMem) {
611 hOld = SelectObject(hMem, hAndBits);
612//SvL: This also doesn't work as StretchDIBits doesn't handle 1bpp bitmaps correctly
613//--------->>> hack alert!
614#if 1
615 HBITMAP hBmp, hOld1;
616 HDC hMem1;
617
618 hMem1 = CreateCompatibleDC(hdc);
619
620 int linewidth = BITMAP_GetWidthBytes(pInfo->bmiHeader.biWidth, 1);
621
622 char *newpix = (char *)malloc(linewidth*pInfo->bmiHeader.biHeight);
623
624 newpix += ((pInfo->bmiHeader.biHeight-1)*linewidth);
625
626 if(cbSize - size - colorsize - bwsize == bwsize)
627 {//this means an AND and XOR mask is present (interleaved; and/xor)
628 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
629 memcpy(newpix, xbits, linewidth);
630 newpix -= linewidth;
631 xbits += linewidth*2;
632 }
633 }
634 else {
635 for(int i=0;i<pInfo->bmiHeader.biHeight;i++) {
636 memcpy(newpix, xbits, linewidth);
637 newpix -= linewidth;
638 xbits += linewidth;
639 }
640 }
641 newpix += linewidth;
642 hBmp = CreateBitmap(pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, 1, 1, newpix);
643 free(newpix);
644
645 hOld1 = SelectObject(hMem1, hBmp);
646
647 res = StretchBlt(hMem, 0, 0, width, height, hMem1, 0, 0, pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight, SRCCOPY);
648
649 SelectObject(hMem1, hOld1);
650 DeleteObject(hBmp);
651 DeleteDC(hMem1);
652
653
654#else
655 res = StretchDIBits(hMem, 0, 0, width, height, 0, 0,
656 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
657 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
658#endif
659 SelectObject(hMem, hOld);
660 DeleteDC(hMem);
661 }
662 else res = FALSE;
663 if (!res) {
664 DeleteObject(hAndBits);
665 hAndBits = 0;
666 }
667 }
668 }
669 else {
670//SvL: Must use CreateBitmap here as CreateDIBitmap converts data to 8bpp (GetObjectA info -> 8 bpp)
671#if 1
672 int linewidth = BITMAP_GetWidthBytes(width, 1);
673
674 char *newpix = (char *)malloc(linewidth*height);
675
676 newpix += ((height-1)*linewidth);
677
678 if(cbSize - size - colorsize - bwsize == bwsize)
679 {//this means an AND and XOR mask is present (interleaved; and/xor)
680 for(int i=0;i<height;i++) {
681 memcpy(newpix, xbits, linewidth);
682 newpix -= linewidth;
683 xbits += linewidth*2;
684 }
685 }
686 else {
687 for(int i=0;i<height;i++) {
688 memcpy(newpix, xbits, linewidth);
689 newpix -= linewidth;
690 xbits += linewidth;
691 }
692 }
693 newpix += linewidth;
694 hAndBits = CreateBitmap(width, height, 1, 1, newpix);
695
696 free(newpix);
697
698#else
699 hAndBits = CreateDIBitmap(hdc, &pInfo->bmiHeader,
700 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
701#endif
702 }
703 if( !hAndBits )
704 DeleteObject( hXorBits );
705 }
706 HeapFree( GetProcessHeap(), 0, pInfo );
707 }
708 ReleaseDC( 0, hdc );
709 }
710
711 if( !hXorBits || !hAndBits )
712 {
713 dprintf(("\tunable to create an icon bitmap.\n"));
714 return 0;
715 }
716
717 /* Now create the CURSORICONINFO structure */
718 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
719 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
720 colortablesize = 0;
721
722 if(bmpXor.bmBitsPixel <= 8) {
723 colortablesize = sizeof(RGBQUAD)*(1<<bmpXor.bmBitsPixel);
724 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes + colortablesize;
725 }
726 else sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
727
728 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
729
730 if (hObj) hObj = GlobalReAlloc( hObj,
731 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
732 if (!hObj) hObj = GlobalAlloc( GMEM_MOVEABLE,
733 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
734 if (hObj)
735 {
736 CURSORICONINFO *info;
737
738 info = (CURSORICONINFO *)GlobalLock( hObj );
739 info->ptHotSpot.x = hotspot.x;
740 info->ptHotSpot.y = hotspot.y;
741 info->nWidth = bmpXor.bmWidth;
742 info->nHeight = bmpXor.bmHeight;
743 info->nWidthBytes = bmpXor.bmWidthBytes;
744 info->bPlanes = bmpXor.bmPlanes;
745 info->bBitsPerPixel = bmpXor.bmBitsPixel;
746 info->hColorBmp = hXorBits;
747 info->hInstance = hInstance;
748 info->dwResGroupId = dwResGroupId;
749
750 /* Transfer the bitmap bits to the CURSORICONINFO structure */
751 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1));
752
753 if(bmpXor.bmBitsPixel > 1)
754 {
755 BITMAPINFO* pInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD)); //+ extra space for > 8bpp images
756 HBITMAP oldbmp;
757
758 hdc = CreateCompatibleDC(0);
759
760 memset(pInfo, 0, sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD));
761 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
762 pInfo->bmiHeader.biPlanes = info->bPlanes;
763 pInfo->bmiHeader.biBitCount = info->bBitsPerPixel;
764
765 GetDIBits(hdc, hXorBits, 0, bmpXor.bmHeight, (char *)(info + 1) + sizeAnd + colortablesize, pInfo, DIB_RGB_COLORS);
766 if(colortablesize) {
767 memcpy((char *)(info + 1) + sizeAnd, (char *)&pInfo->bmiHeader + pInfo->bmiHeader.biSize, colortablesize);
768 }
769
770 DeleteDC(hdc);
771 free(pInfo);
772 }
773 else {
774 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd);
775 }
776 GlobalUnlock( hObj );
777 }
778
779 DeleteObject( hAndBits );
780 DeleteObject( hXorBits );
781 return hObj;
782}
783
784/**********************************************************************
785 * CURSORICON_Destroy (USER.610)
786 *
787 * This routine is actually exported from Win95 USER under the name
788 * DestroyIcon32 ... The behaviour implemented here should mimic
789 * the Win95 one exactly, especially the return values, which
790 * depend on the setting of various flags.
791 */
792WORD WIN32API CURSORICON_Destroy( HGLOBAL handle, UINT flags )
793{
794 WORD retv;
795
796#if 0
797 /* Check whether destroying active cursor */
798
799 if ( hActiveCursor == handle )
800 {
801 ERR_(cursor)("Destroying active cursor!\n" );
802 SetCursor( 0 );
803 }
804 /* Try shared cursor/icon first */
805
806 if ( !(flags & CID_NONSHARED) )
807 {
808 INT count = CURSORICON_DelSharedIcon( handle );
809
810 if ( count != -1 )
811 return (flags & CID_WIN32)? TRUE : (count == 0);
812
813 /* FIXME: OEM cursors/icons should be recognized */
814 }
815#endif
816 /* Now assume non-shared cursor/icon */
817
818 retv = GlobalFree( handle );
819 return (flags & CID_RESOURCE)? retv : TRUE;
820}
821
822/***********************************************************************
823 * CURSORICON_Copy
824 *
825 * Make a copy of a cursor or icon.
826 */
827static HGLOBAL CURSORICON_Copy(HGLOBAL handle)
828{
829 char *ptrOld, *ptrNew;
830 int size;
831 HGLOBAL hNew;
832
833 if (!(ptrOld = (char *)GlobalLock( handle ))) return 0;
834
835 size = GlobalSize( handle );
836 hNew = GlobalAlloc( GMEM_MOVEABLE, size );
837 ptrNew = (char *)GlobalLock( hNew );
838 memcpy( ptrNew, ptrOld, size );
839 GlobalUnlock( handle );
840 GlobalUnlock( hNew );
841 return hNew;
842}
843
844/*************************************************************************
845 * CURSORICON_ExtCopy
846 *
847 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
848 *
849 * PARAMS
850 * Handle [I] handle to an Image
851 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
852 * iDesiredCX [I] The Desired width of the Image
853 * iDesiredCY [I] The desired height of the Image
854 * nFlags [I] The flags from CopyImage
855 *
856 * RETURNS
857 * Success: The new handle of the Image
858 *
859 * NOTES
860 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
861 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
862 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
863 *
864 *
865 */
866HGLOBAL CURSORICON_ExtCopy(HGLOBAL Handle, UINT nType,
867 INT iDesiredCX, INT iDesiredCY,
868 UINT nFlags)
869{
870 HGLOBAL hNew=0;
871
872 if(Handle == 0)
873 {
874 return 0;
875 }
876 /* Best Fit or Monochrome */
877 if( (nFlags & LR_COPYFROMRESOURCE
878 && (iDesiredCX > 0 || iDesiredCY > 0))
879 || nFlags & LR_MONOCHROME)
880 {
881 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
882 LPBYTE pBits;
883 HANDLE hMem;
884 HRSRC hRsrc;
885 DWORD dwBytesInRes;
886 WORD wResId;
887 DWORD dwResGroupId;
888 HINSTANCE hInstance;
889 CURSORICONINFO *iconinfo;
890 CURSORICONDIR *pDir;
891 CURSORICONDIRENTRY *pDirEntry;
892 BOOL bIsIcon = (nType == IMAGE_ICON);
893
894 iconinfo = (CURSORICONINFO *)GlobalLock( Handle );
895 if(iconinfo == NULL) {
896 dprintf(("ERROR: CURSORICON_ExtCopy invalid icon!"));
897 }
898 hInstance = iconinfo->hInstance;
899 dwResGroupId = iconinfo->dwResGroupId;
900 GlobalUnlock( Handle );
901 if(dwResGroupId == -1) {
902 dprintf(("WARNING: no resource associated with icon/cursor -> copy without scaling!"));
903 hNew = CURSORICON_Copy(Handle);
904 return hNew;
905 }
906
907 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
908 */
909 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
910 || (iDesiredCX == 0 && iDesiredCY == 0))
911 {
912 iDesiredCY = GetSystemMetrics(bIsIcon ? SM_CYICON : SM_CYCURSOR);
913 iDesiredCX = GetSystemMetrics(bIsIcon ? SM_CXICON : SM_CXCURSOR);
914 }
915
916 /* Retreive the CURSORICONDIRENTRY
917 */
918 hRsrc = FindResourceW(hInstance, (LPWSTR)dwResGroupId, bIsIcon ? RT_GROUP_ICONW : RT_GROUP_CURSORW);
919 if(!hRsrc) return 0;
920
921 if (!(hMem = LoadResource( hInstance, hRsrc)))
922 {
923 return 0;
924 }
925 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
926 {
927 return 0;
928 }
929
930 /* Find Best Fit
931 */
932 if(bIsIcon)
933 {
934 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
935 pDir, iDesiredCX, iDesiredCY, 256);
936 }
937 else
938 {
939 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
940 pDir, iDesiredCX, iDesiredCY, 1);
941 }
942
943 wResId = pDirEntry->wResId;
944 dwBytesInRes = pDirEntry->dwBytesInRes;
945 FreeResource(hMem);
946
947 /* Get the Best Fit
948 */
949 if (!(hRsrc = FindResourceW(hInstance ,
950 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
951 {
952 return 0;
953 }
954 if (!(hMem = LoadResource( hInstance, hRsrc )))
955 {
956 return 0;
957 }
958
959 pBits = (LPBYTE)LockResource( hMem );
960
961 if(nFlags & LR_DEFAULTSIZE)
962 {
963 iTargetCY = GetSystemMetrics(SM_CYICON);
964 iTargetCX = GetSystemMetrics(SM_CXICON);
965 }
966
967 /* Create a New Icon with the proper dimension
968 */
969 hNew = CURSORICON_CreateFromResource( hInstance, dwResGroupId, 0, pBits, dwBytesInRes,
970 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
971 FreeResource(hMem);
972 }
973 else
974 {
975 hNew = CURSORICON_Copy(Handle);
976 }
977 return hNew;
978}
979
980/**********************************************************************
981 * CURSORICON_FindBestIcon
982 *
983 * Find the icon closest to the requested size and number of colors.
984 */
985static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
986 int height, int colors )
987{
988 int i;
989 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
990 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
991 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
992
993 if (dir->idCount < 1)
994 {
995 dprintf(("Empty directory!\n" ));
996 return NULL;
997 }
998 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
999
1000 /* Find Best Fit */
1001 iTotalDiff = 0xFFFFFFFF;
1002 iColorDiff = 0xFFFFFFFF;
1003 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1004 {
1005 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
1006 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
1007
1008 if(iTotalDiff > (iTempXDiff + iTempYDiff))
1009 {
1010 iXDiff = iTempXDiff;
1011 iYDiff = iTempYDiff;
1012 iTotalDiff = iXDiff + iYDiff;
1013 }
1014 }
1015
1016 /* Find Best Colors for Best Fit */
1017 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1018 {
1019 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
1020 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
1021 {
1022#ifdef __WIN32OS2__
1023 iTempColorDiff = abs(colors - (1 << entry->wBitCount));
1024#else
1025 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
1026#endif
1027 if(iColorDiff > iTempColorDiff)
1028 {
1029 bestEntry = entry;
1030 iColorDiff = iTempColorDiff;
1031 }
1032 }
1033 }
1034////testestest
1035 dprintf(("CURSORICON_FindBestIcon (%d,%d) %d -> %d", width, height, colors, (bestEntry) ? bestEntry->wResId : 0));
1036 return bestEntry;
1037}
1038
1039
1040/**********************************************************************
1041 * CURSORICON_FindBestCursor
1042 *
1043 * Find the cursor closest to the requested size.
1044 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
1045 * ignored too
1046 */
1047static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
1048 int width, int height, int color)
1049{
1050 int i, maxwidth, maxheight;
1051 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
1052
1053 if (dir->idCount < 1)
1054 {
1055 dprintf(("Empty directory!\n" ));
1056 return NULL;
1057 }
1058 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
1059
1060 /* Double height to account for AND and XOR masks */
1061
1062 height *= 2;
1063
1064 /* First find the largest one smaller than or equal to the requested size*/
1065
1066 maxwidth = maxheight = 0;
1067 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1068 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
1069 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
1070 (entry->wBitCount == 1))
1071 {
1072 bestEntry = entry;
1073 maxwidth = entry->ResInfo.cursor.wWidth;
1074 maxheight = entry->ResInfo.cursor.wHeight;
1075 }
1076 if (bestEntry) return bestEntry;
1077
1078 /* Now find the smallest one larger than the requested size */
1079
1080 maxwidth = maxheight = 255;
1081 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
1082 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
1083 (entry->wBitCount == 1))
1084 {
1085 bestEntry = entry;
1086 maxwidth = entry->ResInfo.cursor.wWidth;
1087 maxheight = entry->ResInfo.cursor.wHeight;
1088 }
1089
1090 return bestEntry;
1091}
1092/**********************************************************************
1093 * LookupIconIdFromDirectoryEx (USER.364)
1094 *
1095 * FIXME: exact parameter sizes
1096 */
1097INT WIN32API LookupIconIdFromDirectoryEx(LPBYTE xdir, BOOL bIcon,
1098 INT width, INT height, UINT cFlag )
1099{
1100 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1101 UINT retVal = 0;
1102
1103 dprintf(("LookupIconIdFromDirectoryEx %x %d (%d,%d)", xdir, bIcon, width, height));
1104 if( dir && !dir->idReserved && (dir->idType & 3) )
1105 {
1106 CURSORICONDIRENTRY* entry;
1107 HDC hdc;
1108 UINT palEnts;
1109 int colors;
1110 hdc = GetDC(0);
1111 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1112 if (palEnts == 0)
1113 palEnts = 256;
1114 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1115
1116 ReleaseDC(0, hdc);
1117
1118 if( bIcon )
1119 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1120 else
1121 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1122
1123 if( entry ) retVal = entry->wResId;
1124 }
1125 else dprintf(("invalid resource directory\n"));
1126 return retVal;
1127}
1128/**********************************************************************
1129 * LookupIconIdFromDirectory (USER32.379)
1130 */
1131INT WIN32API LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1132{
1133 return LookupIconIdFromDirectoryEx( dir, bIcon,
1134 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1135 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1136}
1137//******************************************************************************
1138//******************************************************************************
Note: See TracBrowser for help on using the repository browser.