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

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

CopyImage fix for icons

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