cursoricon.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  1. /*
  2. * Cursor and icon support
  3. *
  4. * Copyright 1995 Alexandre Julliard
  5. * 1996 Martin Von Loewis
  6. * 1997 Alex Korobka
  7. * 1998 Turchanov Sergey
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * Theory:
  25. *
  26. * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
  27. *
  28. * Cursors and icons are stored in a global heap block, with the
  29. * following layout:
  30. *
  31. * CURSORICONINFO info;
  32. * BYTE[] ANDbits;
  33. * BYTE[] XORbits;
  34. *
  35. * The bits structures are in the format of a device-dependent bitmap.
  36. *
  37. * This layout is very sub-optimal, as the bitmap bits are stored in
  38. * the X client instead of in the server like other bitmaps; however,
  39. * some programs (notably Paint Brush) expect to be able to manipulate
  40. * the bits directly :-(
  41. *
  42. * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
  43. */
  44. #include "config.h"
  45. #include "wine/port.h"
  46. #include <stdarg.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include "windef.h"
  50. #include "winbase.h"
  51. #include "wingdi.h"
  52. #include "wownt32.h"
  53. #include "winerror.h"
  54. #include "ntstatus.h"
  55. #include "excpt.h"
  56. #include "wine/winbase16.h"
  57. #include "wine/winuser16.h"
  58. #include "wine/exception.h"
  59. #include "cursoricon.h"
  60. #include "module.h"
  61. #include "wine/debug.h"
  62. #include "user_private.h"
  63. #include "message.h"
  64. WINE_DEFAULT_DEBUG_CHANNEL(cursor);
  65. WINE_DECLARE_DEBUG_CHANNEL(icon);
  66. WINE_DECLARE_DEBUG_CHANNEL(resource);
  67. static RECT CURSOR_ClipRect; /* Cursor clipping rect */
  68. static HDC screen_dc;
  69. static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
  70. /**********************************************************************
  71. * ICONCACHE for cursors/icons loaded with LR_SHARED.
  72. *
  73. * FIXME: This should not be allocated on the system heap, but on a
  74. * subsystem-global heap (i.e. one for all Win16 processes,
  75. * and one for each Win32 process).
  76. */
  77. typedef struct tagICONCACHE
  78. {
  79. struct tagICONCACHE *next;
  80. HMODULE hModule;
  81. HRSRC hRsrc;
  82. HRSRC hGroupRsrc;
  83. HICON hIcon;
  84. INT count;
  85. } ICONCACHE;
  86. static ICONCACHE *IconAnchor = NULL;
  87. static CRITICAL_SECTION IconCrst;
  88. static CRITICAL_SECTION_DEBUG critsect_debug =
  89. {
  90. 0, 0, &IconCrst,
  91. { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
  92. 0, 0, { 0, (DWORD)(__FILE__ ": IconCrst") }
  93. };
  94. static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
  95. static WORD ICON_HOTSPOT = 0x4242;
  96. /***********************************************************************
  97. * map_fileW
  98. *
  99. * Helper function to map a file to memory:
  100. * name - file name
  101. * [RETURN] ptr - pointer to mapped file
  102. * [RETURN] filesize - pointer size of file to be stored if not NULL
  103. */
  104. static void *map_fileW( LPCWSTR name, LPDWORD filesize )
  105. {
  106. HANDLE hFile, hMapping;
  107. LPVOID ptr = NULL;
  108. hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
  109. OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
  110. if (hFile != INVALID_HANDLE_VALUE)
  111. {
  112. hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
  113. if (hMapping)
  114. {
  115. ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
  116. CloseHandle( hMapping );
  117. if (filesize)
  118. *filesize = GetFileSize( hFile, NULL );
  119. }
  120. CloseHandle( hFile );
  121. }
  122. return ptr;
  123. }
  124. /***********************************************************************
  125. * get_bitmap_width_bytes
  126. *
  127. * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
  128. * data.
  129. */
  130. static int get_bitmap_width_bytes( int width, int bpp )
  131. {
  132. switch(bpp)
  133. {
  134. case 1:
  135. return 2 * ((width+15) / 16);
  136. case 4:
  137. return 2 * ((width+3) / 4);
  138. case 24:
  139. width *= 3;
  140. /* fall through */
  141. case 8:
  142. return width + (width & 1);
  143. case 16:
  144. case 15:
  145. return width * 2;
  146. case 32:
  147. return width * 4;
  148. default:
  149. WARN("Unknown depth %d, please report.\n", bpp );
  150. }
  151. return -1;
  152. }
  153. /***********************************************************************
  154. * get_dib_width_bytes
  155. *
  156. * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
  157. */
  158. static int get_dib_width_bytes( int width, int depth )
  159. {
  160. int words;
  161. switch(depth)
  162. {
  163. case 1: words = (width + 31) / 32; break;
  164. case 4: words = (width + 7) / 8; break;
  165. case 8: words = (width + 3) / 4; break;
  166. case 15:
  167. case 16: words = (width + 1) / 2; break;
  168. case 24: words = (width * 3 + 3)/4; break;
  169. default:
  170. WARN("(%d): Unsupported depth\n", depth );
  171. /* fall through */
  172. case 32:
  173. words = width;
  174. }
  175. return 4 * words;
  176. }
  177. /***********************************************************************
  178. * bitmap_info_size
  179. *
  180. * Return the size of the bitmap info structure including color table.
  181. */
  182. static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
  183. {
  184. int colors;
  185. if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
  186. {
  187. const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
  188. colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
  189. return sizeof(BITMAPCOREHEADER) + colors *
  190. ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
  191. }
  192. else /* assume BITMAPINFOHEADER */
  193. {
  194. colors = info->bmiHeader.biClrUsed;
  195. if (!colors && (info->bmiHeader.biBitCount <= 8))
  196. colors = 1 << info->bmiHeader.biBitCount;
  197. return sizeof(BITMAPINFOHEADER) + colors *
  198. ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
  199. }
  200. }
  201. /***********************************************************************
  202. * is_dib_monochrome
  203. *
  204. * Returns whether a DIB can be converted to a monochrome DDB.
  205. *
  206. * A DIB can be converted if its color table contains only black and
  207. * white. Black must be the first color in the color table.
  208. *
  209. * Note : If the first color in the color table is white followed by
  210. * black, we can't convert it to a monochrome DDB with
  211. * SetDIBits, because black and white would be inverted.
  212. */
  213. static BOOL is_dib_monochrome( const BITMAPINFO* info )
  214. {
  215. if (info->bmiHeader.biBitCount != 1) return FALSE;
  216. if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
  217. {
  218. RGBTRIPLE *rgb = ((BITMAPCOREINFO *) info)->bmciColors;
  219. /* Check if the first color is black */
  220. if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
  221. {
  222. rgb++;
  223. /* Check if the second color is white */
  224. return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
  225. && (rgb->rgbtBlue == 0xff));
  226. }
  227. else return FALSE;
  228. }
  229. else /* assume BITMAPINFOHEADER */
  230. {
  231. RGBQUAD *rgb = info->bmiColors;
  232. /* Check if the first color is black */
  233. if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
  234. (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
  235. {
  236. rgb++;
  237. /* Check if the second color is white */
  238. return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
  239. && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
  240. }
  241. else return FALSE;
  242. }
  243. }
  244. /***********************************************************************
  245. * DIB_GetBitmapInfo
  246. *
  247. * Get the info from a bitmap header.
  248. * Return 1 for INFOHEADER, 0 for COREHEADER,
  249. * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
  250. */
  251. static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
  252. LONG *height, WORD *bpp, DWORD *compr )
  253. {
  254. if (header->biSize == sizeof(BITMAPINFOHEADER))
  255. {
  256. *width = header->biWidth;
  257. *height = header->biHeight;
  258. *bpp = header->biBitCount;
  259. *compr = header->biCompression;
  260. return 1;
  261. }
  262. if (header->biSize == sizeof(BITMAPCOREHEADER))
  263. {
  264. BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
  265. *width = core->bcWidth;
  266. *height = core->bcHeight;
  267. *bpp = core->bcBitCount;
  268. *compr = 0;
  269. return 0;
  270. }
  271. if (header->biSize == sizeof(BITMAPV4HEADER))
  272. {
  273. BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
  274. *width = v4hdr->bV4Width;
  275. *height = v4hdr->bV4Height;
  276. *bpp = v4hdr->bV4BitCount;
  277. *compr = v4hdr->bV4V4Compression;
  278. return 4;
  279. }
  280. if (header->biSize == sizeof(BITMAPV5HEADER))
  281. {
  282. BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
  283. *width = v5hdr->bV5Width;
  284. *height = v5hdr->bV5Height;
  285. *bpp = v5hdr->bV5BitCount;
  286. *compr = v5hdr->bV5Compression;
  287. return 5;
  288. }
  289. ERR("(%ld): unknown/wrong size for header\n", header->biSize );
  290. return -1;
  291. }
  292. /**********************************************************************
  293. * CURSORICON_FindSharedIcon
  294. */
  295. static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
  296. {
  297. HICON hIcon = 0;
  298. ICONCACHE *ptr;
  299. EnterCriticalSection( &IconCrst );
  300. for ( ptr = IconAnchor; ptr; ptr = ptr->next )
  301. if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
  302. {
  303. ptr->count++;
  304. hIcon = ptr->hIcon;
  305. break;
  306. }
  307. LeaveCriticalSection( &IconCrst );
  308. return hIcon;
  309. }
  310. /*************************************************************************
  311. * CURSORICON_FindCache
  312. *
  313. * Given a handle, find the corresponding cache element
  314. *
  315. * PARAMS
  316. * Handle [I] handle to an Image
  317. *
  318. * RETURNS
  319. * Success: The cache entry
  320. * Failure: NULL
  321. *
  322. */
  323. static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
  324. {
  325. ICONCACHE *ptr;
  326. ICONCACHE *pRet=NULL;
  327. BOOL IsFound = FALSE;
  328. int count;
  329. EnterCriticalSection( &IconCrst );
  330. for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
  331. {
  332. if ( hIcon == ptr->hIcon )
  333. {
  334. IsFound = TRUE;
  335. pRet = ptr;
  336. }
  337. }
  338. LeaveCriticalSection( &IconCrst );
  339. return pRet;
  340. }
  341. /**********************************************************************
  342. * CURSORICON_AddSharedIcon
  343. */
  344. static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
  345. {
  346. ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
  347. if ( !ptr ) return;
  348. ptr->hModule = hModule;
  349. ptr->hRsrc = hRsrc;
  350. ptr->hIcon = hIcon;
  351. ptr->hGroupRsrc = hGroupRsrc;
  352. ptr->count = 1;
  353. EnterCriticalSection( &IconCrst );
  354. ptr->next = IconAnchor;
  355. IconAnchor = ptr;
  356. LeaveCriticalSection( &IconCrst );
  357. }
  358. /**********************************************************************
  359. * CURSORICON_DelSharedIcon
  360. */
  361. static INT CURSORICON_DelSharedIcon( HICON hIcon )
  362. {
  363. INT count = -1;
  364. ICONCACHE *ptr;
  365. EnterCriticalSection( &IconCrst );
  366. for ( ptr = IconAnchor; ptr; ptr = ptr->next )
  367. if ( ptr->hIcon == hIcon )
  368. {
  369. if ( ptr->count > 0 ) ptr->count--;
  370. count = ptr->count;
  371. break;
  372. }
  373. LeaveCriticalSection( &IconCrst );
  374. return count;
  375. }
  376. /**********************************************************************
  377. * CURSORICON_FreeModuleIcons
  378. */
  379. void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
  380. {
  381. ICONCACHE **ptr = &IconAnchor;
  382. HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
  383. EnterCriticalSection( &IconCrst );
  384. while ( *ptr )
  385. {
  386. if ( (*ptr)->hModule == hModule )
  387. {
  388. ICONCACHE *freePtr = *ptr;
  389. *ptr = freePtr->next;
  390. GlobalFree16(HICON_16(freePtr->hIcon));
  391. HeapFree( GetProcessHeap(), 0, freePtr );
  392. continue;
  393. }
  394. ptr = &(*ptr)->next;
  395. }
  396. LeaveCriticalSection( &IconCrst );
  397. }
  398. /**********************************************************************
  399. * CURSORICON_FindBestIcon
  400. *
  401. * Find the icon closest to the requested size and number of colors.
  402. */
  403. static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
  404. int height, int colors )
  405. {
  406. int i;
  407. CURSORICONDIRENTRY *entry, *bestEntry = NULL;
  408. UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
  409. UINT iTempXDiff, iTempYDiff, iTempColorDiff;
  410. if (dir->idCount < 1)
  411. {
  412. WARN_(icon)("Empty directory!\n" );
  413. return NULL;
  414. }
  415. if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
  416. /* Find Best Fit */
  417. iTotalDiff = 0xFFFFFFFF;
  418. iColorDiff = 0xFFFFFFFF;
  419. for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
  420. {
  421. iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
  422. iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
  423. if(iTotalDiff > (iTempXDiff + iTempYDiff))
  424. {
  425. iXDiff = iTempXDiff;
  426. iYDiff = iTempYDiff;
  427. iTotalDiff = iXDiff + iYDiff;
  428. }
  429. }
  430. /* Find Best Colors for Best Fit */
  431. for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
  432. {
  433. if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
  434. abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
  435. {
  436. iTempColorDiff = abs(colors - (1<<entry->wBitCount));
  437. if(iColorDiff > iTempColorDiff)
  438. {
  439. bestEntry = entry;
  440. iColorDiff = iTempColorDiff;
  441. }
  442. }
  443. }
  444. return bestEntry;
  445. }
  446. /**********************************************************************
  447. * CURSORICON_FindBestCursor
  448. *
  449. * Find the cursor closest to the requested size.
  450. * FIXME: parameter 'color' ignored and entries with more than 1 bpp
  451. * ignored too
  452. */
  453. static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
  454. int width, int height, int color)
  455. {
  456. int i, maxwidth, maxheight;
  457. CURSORICONDIRENTRY *entry, *bestEntry = NULL;
  458. if (dir->idCount < 1)
  459. {
  460. WARN_(cursor)("Empty directory!\n" );
  461. return NULL;
  462. }
  463. if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
  464. /* Double height to account for AND and XOR masks */
  465. height *= 2;
  466. /* First find the largest one smaller than or equal to the requested size*/
  467. maxwidth = maxheight = 0;
  468. for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
  469. if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
  470. (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
  471. (entry->wBitCount == 1))
  472. {
  473. bestEntry = entry;
  474. maxwidth = entry->ResInfo.cursor.wWidth;
  475. maxheight = entry->ResInfo.cursor.wHeight;
  476. }
  477. if (bestEntry) return bestEntry;
  478. /* Now find the smallest one larger than the requested size */
  479. maxwidth = maxheight = 255;
  480. for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
  481. if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
  482. (entry->wBitCount == 1))
  483. {
  484. bestEntry = entry;
  485. maxwidth = entry->ResInfo.cursor.wWidth;
  486. maxheight = entry->ResInfo.cursor.wHeight;
  487. }
  488. return bestEntry;
  489. }
  490. /*********************************************************************
  491. * The main purpose of this function is to create fake resource directory
  492. * and fake resource entries. There are several reasons for this:
  493. * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
  494. * fields
  495. * There are some "bad" cursor files which do not have
  496. * bColorCount initialized but instead one must read this info
  497. * directly from corresponding DIB sections
  498. * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
  499. */
  500. static BOOL CURSORICON_SimulateLoadingFromResourceW( LPCWSTR filename, BOOL fCursor,
  501. CURSORICONDIR **res, LPBYTE **ptr)
  502. {
  503. LPBYTE _free;
  504. DWORD filesize;
  505. CURSORICONFILEDIR *bits;
  506. int entries, size, i;
  507. *res = NULL;
  508. *ptr = NULL;
  509. if (!(bits = map_fileW( filename, &filesize ))) return FALSE;
  510. /* FIXME: test for inimated icons
  511. * hack to load the first icon from the *.ani file
  512. */
  513. if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
  514. { LPBYTE pos = (LPBYTE) bits;
  515. FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
  516. for (;;)
  517. { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
  518. { FIXME_(cursor)("icon entry found! %p\n", bits);
  519. pos+=4;
  520. if ( !*(LPWORD) pos==0x2fe) /* iconsize */
  521. { goto fail;
  522. }
  523. bits=(CURSORICONFILEDIR*)(pos+4);
  524. FIXME_(cursor)("icon size ok. offset=%p \n", bits);
  525. break;
  526. }
  527. pos+=2;
  528. if (pos>=(LPBYTE)bits+766) goto fail;
  529. }
  530. }
  531. if (!(entries = bits->idCount)) goto fail;
  532. if ( (sizeof(CURSORICONFILEDIR) +
  533. sizeof(CURSORICONFILEDIRENTRY) * (entries - 1)) > filesize)
  534. {
  535. FIXME("broken file %s\n", wine_dbgstr_w(filename));
  536. goto fail;
  537. }
  538. size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
  539. _free = (LPBYTE) size;
  540. for (i=0; i < entries; i++)
  541. size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
  542. if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
  543. entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
  544. if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
  545. _free = (LPBYTE)(*res) + (int)_free;
  546. memcpy((*res), bits, 6);
  547. for (i=0; i<entries; i++)
  548. {
  549. ((LPBYTE*)(*ptr))[i] = _free;
  550. if (fCursor) {
  551. (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
  552. (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
  553. ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
  554. ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
  555. _free+=sizeof(POINT16);
  556. } else {
  557. (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
  558. (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
  559. (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
  560. }
  561. (*res)->idEntries[i].wPlanes=1;
  562. (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
  563. bits->idEntries[i].dwDIBOffset))->biBitCount;
  564. (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
  565. (*res)->idEntries[i].wResId=i+1;
  566. memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
  567. (*res)->idEntries[i].dwBytesInRes);
  568. _free += (*res)->idEntries[i].dwBytesInRes;
  569. }
  570. UnmapViewOfFile( bits );
  571. return TRUE;
  572. fail:
  573. HeapFree( GetProcessHeap(), 0, *res );
  574. HeapFree( GetProcessHeap(), 0, *ptr );
  575. UnmapViewOfFile( bits );
  576. return FALSE;
  577. }
  578. /**********************************************************************
  579. * CURSORICON_CreateFromResource
  580. *
  581. * Create a cursor or icon from in-memory resource template.
  582. *
  583. * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
  584. * with cbSize parameter as well.
  585. */
  586. static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
  587. UINT cbSize, BOOL bIcon, DWORD dwVersion,
  588. INT width, INT height, UINT loadflags )
  589. {
  590. static HDC hdcMem;
  591. int sizeAnd, sizeXor;
  592. HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
  593. BITMAP bmpXor, bmpAnd;
  594. POINT16 hotspot;
  595. BITMAPINFO *bmi;
  596. BOOL DoStretch;
  597. INT size;
  598. hotspot.x = ICON_HOTSPOT;
  599. hotspot.y = ICON_HOTSPOT;
  600. TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
  601. (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
  602. bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
  603. if (dwVersion == 0x00020000)
  604. {
  605. FIXME_(cursor)("\t2.xx resources are not supported\n");
  606. return 0;
  607. }
  608. if (bIcon)
  609. bmi = (BITMAPINFO *)bits;
  610. else /* get the hotspot */
  611. {
  612. POINT16 *pt = (POINT16 *)bits;
  613. hotspot = *pt;
  614. bmi = (BITMAPINFO *)(pt + 1);
  615. }
  616. size = bitmap_info_size( bmi, DIB_RGB_COLORS );
  617. if (!width) width = bmi->bmiHeader.biWidth;
  618. if (!height) height = bmi->bmiHeader.biHeight/2;
  619. DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
  620. (bmi->bmiHeader.biWidth != width);
  621. /* Check bitmap header */
  622. if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
  623. (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
  624. bmi->bmiHeader.biCompression != BI_RGB) )
  625. {
  626. WARN_(cursor)("\tinvalid resource bitmap header.\n");
  627. return 0;
  628. }
  629. if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
  630. if (screen_dc)
  631. {
  632. BITMAPINFO* pInfo;
  633. /* Make sure we have room for the monochrome bitmap later on.
  634. * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
  635. * up to and including the biBitCount. In-memory icon resource
  636. * format is as follows:
  637. *
  638. * BITMAPINFOHEADER icHeader // DIB header
  639. * RGBQUAD icColors[] // Color table
  640. * BYTE icXOR[] // DIB bits for XOR mask
  641. * BYTE icAND[] // DIB bits for AND mask
  642. */
  643. if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
  644. max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
  645. {
  646. memcpy( pInfo, bmi, size );
  647. pInfo->bmiHeader.biHeight /= 2;
  648. /* Create the XOR bitmap */
  649. if (DoStretch) {
  650. if(bIcon)
  651. {
  652. hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
  653. }
  654. else
  655. {
  656. hXorBits = CreateBitmap(width, height, 1, 1, NULL);
  657. }
  658. if(hXorBits)
  659. {
  660. HBITMAP hOld;
  661. BOOL res = FALSE;
  662. if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
  663. if (hdcMem) {
  664. hOld = SelectObject(hdcMem, hXorBits);
  665. res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
  666. bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
  667. (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
  668. SelectObject(hdcMem, hOld);
  669. }
  670. if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
  671. }
  672. } else {
  673. if (is_dib_monochrome(bmi)) {
  674. hXorBits = CreateBitmap(width, height, 1, 1, NULL);
  675. SetDIBits(screen_dc, hXorBits, 0, height,
  676. (char*)bmi + size, pInfo, DIB_RGB_COLORS);
  677. }
  678. else
  679. hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
  680. CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
  681. }
  682. if( hXorBits )
  683. {
  684. char* xbits = (char *)bmi + size +
  685. get_dib_width_bytes( bmi->bmiHeader.biWidth,
  686. bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
  687. pInfo->bmiHeader.biBitCount = 1;
  688. if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
  689. {
  690. RGBQUAD *rgb = pInfo->bmiColors;
  691. pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
  692. rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
  693. rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
  694. rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
  695. }
  696. else
  697. {
  698. RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
  699. rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
  700. rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
  701. }
  702. /* Create the AND bitmap */
  703. if (DoStretch) {
  704. if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
  705. HBITMAP hOld;
  706. BOOL res = FALSE;
  707. if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
  708. if (hdcMem) {
  709. hOld = SelectObject(hdcMem, hAndBits);
  710. res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
  711. pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
  712. xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
  713. SelectObject(hdcMem, hOld);
  714. }
  715. if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
  716. }
  717. } else {
  718. hAndBits = CreateBitmap(width, height, 1, 1, NULL);
  719. if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
  720. xbits, pInfo, DIB_RGB_COLORS);
  721. }
  722. if( !hAndBits ) DeleteObject( hXorBits );
  723. }
  724. HeapFree( GetProcessHeap(), 0, pInfo );
  725. }
  726. }
  727. if( !hXorBits || !hAndBits )
  728. {
  729. WARN_(cursor)("\tunable to create an icon bitmap.\n");
  730. return 0;
  731. }
  732. /* Now create the CURSORICONINFO structure */
  733. GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
  734. GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
  735. sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
  736. sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
  737. if (hObj) hObj = GlobalReAlloc16( hObj,
  738. sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
  739. if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
  740. sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
  741. if (hObj)
  742. {
  743. CURSORICONINFO *info;
  744. /* Make it owned by the module */
  745. if (hModule) hModule = GetExePtr(hModule);
  746. FarSetOwner16( hObj, hModule );
  747. info = (CURSORICONINFO *)GlobalLock16( hObj );
  748. info->ptHotSpot.x = hotspot.x;
  749. info->ptHotSpot.y = hotspot.y;
  750. info->nWidth = bmpXor.bmWidth;
  751. info->nHeight = bmpXor.bmHeight;
  752. info->nWidthBytes = bmpXor.bmWidthBytes;
  753. info->bPlanes = bmpXor.bmPlanes;
  754. info->bBitsPerPixel = bmpXor.bmBitsPixel;
  755. /* Transfer the bitmap bits to the CURSORICONINFO structure */
  756. GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
  757. GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
  758. GlobalUnlock16( hObj );
  759. }
  760. DeleteObject( hAndBits );
  761. DeleteObject( hXorBits );
  762. return HICON_32((HICON16)hObj);
  763. }
  764. /**********************************************************************
  765. * CreateIconFromResource (USER32.@)
  766. */
  767. HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
  768. BOOL bIcon, DWORD dwVersion)
  769. {
  770. return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
  771. }
  772. /**********************************************************************
  773. * CreateIconFromResourceEx (USER32.@)
  774. */
  775. HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
  776. BOOL bIcon, DWORD dwVersion,
  777. INT width, INT height,
  778. UINT cFlag )
  779. {
  780. return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
  781. width, height, cFlag );
  782. }
  783. /**********************************************************************
  784. * CURSORICON_Load
  785. *
  786. * Load a cursor or icon from resource or file.
  787. */
  788. static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
  789. INT width, INT height, INT colors,
  790. BOOL fCursor, UINT loadflags)
  791. {
  792. HANDLE handle = 0;
  793. HICON hIcon = 0;
  794. HRSRC hRsrc;
  795. CURSORICONDIR *dir;
  796. CURSORICONDIRENTRY *dirEntry;
  797. LPBYTE bits;
  798. if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
  799. {
  800. LPBYTE *ptr;
  801. if (!CURSORICON_SimulateLoadingFromResourceW(name, fCursor, &dir, &ptr))
  802. return 0;
  803. if (fCursor)
  804. dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
  805. else
  806. dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
  807. bits = ptr[dirEntry->wResId-1];
  808. hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
  809. !fCursor, 0x00030000, width, height, loadflags);
  810. HeapFree( GetProcessHeap(), 0, dir );
  811. HeapFree( GetProcessHeap(), 0, ptr );
  812. }
  813. else /* Load from resource */
  814. {
  815. HRSRC hGroupRsrc;
  816. WORD wResId;
  817. DWORD dwBytesInRes;
  818. if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
  819. /* Normalize hInstance (must be uniquely represented for icon cache) */
  820. if (!HIWORD( hInstance ))
  821. hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
  822. /* Get directory resource ID */
  823. if (!(hRsrc = FindResourceW( hInstance, name,
  824. (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
  825. return 0;
  826. hGroupRsrc = hRsrc;
  827. /* Find the best entry in the directory */
  828. if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
  829. if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
  830. if (fCursor)
  831. dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
  832. width, height, 1);
  833. else
  834. dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
  835. width, height, colors );
  836. if (!dirEntry) return 0;
  837. wResId = dirEntry->wResId;
  838. dwBytesInRes = dirEntry->dwBytesInRes;
  839. FreeResource( handle );
  840. /* Load the resource */
  841. if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
  842. (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
  843. /* If shared icon, check whether it was already loaded */
  844. if ( (loadflags & LR_SHARED)
  845. && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
  846. return hIcon;
  847. if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
  848. bits = (LPBYTE)LockResource( handle );
  849. hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
  850. !fCursor, 0x00030000, width, height, loadflags);
  851. FreeResource( handle );
  852. /* If shared icon, add to icon cache */
  853. if ( hIcon && (loadflags & LR_SHARED) )
  854. CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
  855. }
  856. return hIcon;
  857. }
  858. /***********************************************************************
  859. * CURSORICON_Copy
  860. *
  861. * Make a copy of a cursor or icon.
  862. */
  863. static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
  864. {
  865. char *ptrOld, *ptrNew;
  866. int size;
  867. HICON16 hOld = HICON_16(hIcon);
  868. HICON16 hNew;
  869. if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
  870. if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
  871. size = GlobalSize16( hOld );
  872. hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
  873. FarSetOwner16( hNew, hInst16 );
  874. ptrNew = (char *)GlobalLock16( hNew );
  875. memcpy( ptrNew, ptrOld, size );
  876. GlobalUnlock16( hOld );
  877. GlobalUnlock16( hNew );
  878. return HICON_32(hNew);
  879. }
  880. /*************************************************************************
  881. * CURSORICON_ExtCopy
  882. *
  883. * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
  884. *
  885. * PARAMS
  886. * Handle [I] handle to an Image
  887. * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
  888. * iDesiredCX [I] The Desired width of the Image
  889. * iDesiredCY [I] The desired height of the Image
  890. * nFlags [I] The flags from CopyImage
  891. *
  892. * RETURNS
  893. * Success: The new handle of the Image
  894. *
  895. * NOTES
  896. * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
  897. * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
  898. * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
  899. *
  900. *
  901. */
  902. static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
  903. INT iDesiredCX, INT iDesiredCY,
  904. UINT nFlags)
  905. {
  906. HICON hNew=0;
  907. TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
  908. hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
  909. if(hIcon == 0)
  910. {
  911. return 0;
  912. }
  913. /* Best Fit or Monochrome */
  914. if( (nFlags & LR_COPYFROMRESOURCE
  915. && (iDesiredCX > 0 || iDesiredCY > 0))
  916. || nFlags & LR_MONOCHROME)
  917. {
  918. ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
  919. /* Not Found in Cache, then do a straight copy
  920. */
  921. if(pIconCache == NULL)
  922. {
  923. hNew = CURSORICON_Copy(0, hIcon);
  924. if(nFlags & LR_COPYFROMRESOURCE)
  925. {
  926. TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
  927. }
  928. }
  929. else
  930. {
  931. int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
  932. LPBYTE pBits;
  933. HANDLE hMem;
  934. HRSRC hRsrc;
  935. DWORD dwBytesInRes;
  936. WORD wResId;
  937. CURSORICONDIR *pDir;
  938. CURSORICONDIRENTRY *pDirEntry;
  939. BOOL bIsIcon = (nType == IMAGE_ICON);
  940. /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
  941. */
  942. if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
  943. || (iDesiredCX == 0 && iDesiredCY == 0))
  944. {
  945. iDesiredCY = GetSystemMetrics(bIsIcon ?
  946. SM_CYICON : SM_CYCURSOR);
  947. iDesiredCX = GetSystemMetrics(bIsIcon ?
  948. SM_CXICON : SM_CXCURSOR);
  949. }
  950. /* Retrieve the CURSORICONDIRENTRY
  951. */
  952. if (!(hMem = LoadResource( pIconCache->hModule ,
  953. pIconCache->hGroupRsrc)))
  954. {
  955. return 0;
  956. }
  957. if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
  958. {
  959. return 0;
  960. }
  961. /* Find Best Fit
  962. */
  963. if(bIsIcon)
  964. {
  965. pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
  966. pDir, iDesiredCX, iDesiredCY, 256);
  967. }
  968. else
  969. {
  970. pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
  971. pDir, iDesiredCX, iDesiredCY, 1);
  972. }
  973. wResId = pDirEntry->wResId;
  974. dwBytesInRes = pDirEntry->dwBytesInRes;
  975. FreeResource(hMem);
  976. TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
  977. wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
  978. pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
  979. /* Get the Best Fit
  980. */
  981. if (!(hRsrc = FindResourceW(pIconCache->hModule ,
  982. MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
  983. {
  984. return 0;
  985. }
  986. if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
  987. {
  988. return 0;
  989. }
  990. pBits = (LPBYTE)LockResource( hMem );
  991. if(nFlags & LR_DEFAULTSIZE)
  992. {
  993. iTargetCY = GetSystemMetrics(SM_CYICON);
  994. iTargetCX = GetSystemMetrics(SM_CXICON);
  995. }
  996. /* Create a New Icon with the proper dimension
  997. */
  998. hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
  999. bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
  1000. FreeResource(hMem);
  1001. }
  1002. }
  1003. else hNew = CURSORICON_Copy(0, hIcon);
  1004. return hNew;
  1005. }
  1006. /***********************************************************************
  1007. * CreateCursor (USER32.@)
  1008. */
  1009. HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
  1010. INT xHotSpot, INT yHotSpot,
  1011. INT nWidth, INT nHeight,
  1012. LPCVOID lpANDbits, LPCVOID lpXORbits )
  1013. {
  1014. CURSORICONINFO info;
  1015. TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
  1016. nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
  1017. info.ptHotSpot.x = xHotSpot;
  1018. info.ptHotSpot.y = yHotSpot;
  1019. info.nWidth = nWidth;
  1020. info.nHeight = nHeight;
  1021. info.nWidthBytes = 0;
  1022. info.bPlanes = 1;
  1023. info.bBitsPerPixel = 1;
  1024. return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
  1025. }
  1026. /***********************************************************************
  1027. * CreateIcon (USER.407)
  1028. */
  1029. HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
  1030. INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
  1031. LPCVOID lpANDbits, LPCVOID lpXORbits )
  1032. {
  1033. CURSORICONINFO info;
  1034. TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
  1035. nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
  1036. info.ptHotSpot.x = ICON_HOTSPOT;
  1037. info.ptHotSpot.y = ICON_HOTSPOT;
  1038. info.nWidth = nWidth;
  1039. info.nHeight = nHeight;
  1040. info.nWidthBytes = 0;
  1041. info.bPlanes = bPlanes;
  1042. info.bBitsPerPixel = bBitsPixel;
  1043. return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
  1044. }
  1045. /***********************************************************************
  1046. * CreateIcon (USER32.@)
  1047. *
  1048. * Creates an icon based on the specified bitmaps. The bitmaps must be
  1049. * provided in a device dependent format and will be resized to
  1050. * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
  1051. * depth. The provided bitmaps must be top-down bitmaps.
  1052. * Although Windows does not support 15bpp(*) this API must support it
  1053. * for Winelib applications.
  1054. *
  1055. * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
  1056. * format!
  1057. *
  1058. * BUGS
  1059. *
  1060. * - The provided bitmaps are not resized!
  1061. * - The documentation says the lpXORbits bitmap must be in a device
  1062. * dependent format. But we must still resize it and perform depth
  1063. * conversions if necessary.
  1064. * - I'm a bit unsure about the how the 'device dependent format' thing works.
  1065. * I did some tests on windows and found that if you provide a 16bpp bitmap
  1066. * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
  1067. * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
  1068. * else. I don't know if this is part of the GDI specs or if this is a
  1069. * quirk of the graphics card driver.
  1070. * - You may think that we check whether the bit depths match or not
  1071. * as an optimization. But the truth is that the conversion using
  1072. * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
  1073. * no idea why.
  1074. * - I'm pretty sure that all the things we do in CreateIcon should
  1075. * also be done in CreateIconIndirect...
  1076. */
  1077. HICON WINAPI CreateIcon(
  1078. HINSTANCE hInstance, /* [in] the application's hInstance */
  1079. INT nWidth, /* [in] the width of the provided bitmaps */
  1080. INT nHeight, /* [in] the height of the provided bitmaps */
  1081. BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
  1082. BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
  1083. LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
  1084. LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
  1085. {
  1086. HICON hIcon;
  1087. HDC hdc;
  1088. TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
  1089. nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
  1090. hdc=GetDC(0);
  1091. if (!hdc)
  1092. return 0;
  1093. if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
  1094. CURSORICONINFO info;
  1095. info.ptHotSpot.x = ICON_HOTSPOT;
  1096. info.ptHotSpot.y = ICON_HOTSPOT;
  1097. info.nWidth = nWidth;
  1098. info.nHeight = nHeight;
  1099. info.nWidthBytes = 0;
  1100. info.bPlanes = bPlanes;
  1101. info.bBitsPerPixel = bBitsPixel;
  1102. hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
  1103. } else {
  1104. ICONINFO iinfo;
  1105. BITMAPINFO bmi;
  1106. iinfo.fIcon=TRUE;
  1107. iinfo.xHotspot=ICON_HOTSPOT;
  1108. iinfo.yHotspot=ICON_HOTSPOT;
  1109. iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
  1110. bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
  1111. bmi.bmiHeader.biWidth=nWidth;
  1112. bmi.bmiHeader.biHeight=-nHeight;
  1113. bmi.bmiHeader.biPlanes=bPlanes;
  1114. bmi.bmiHeader.biBitCount=bBitsPixel;
  1115. bmi.bmiHeader.biCompression=BI_RGB;
  1116. bmi.bmiHeader.biSizeImage=0;
  1117. bmi.bmiHeader.biXPelsPerMeter=0;
  1118. bmi.bmiHeader.biYPelsPerMeter=0;
  1119. bmi.bmiHeader.biClrUsed=0;
  1120. bmi.bmiHeader.biClrImportant=0;
  1121. iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
  1122. CBM_INIT, lpXORbits,
  1123. &bmi, DIB_RGB_COLORS );
  1124. hIcon=CreateIconIndirect(&iinfo);
  1125. DeleteObject(iinfo.hbmMask);
  1126. DeleteObject(iinfo.hbmColor);
  1127. }
  1128. ReleaseDC(0,hdc);
  1129. return hIcon;
  1130. }
  1131. /***********************************************************************
  1132. * CreateCursorIconIndirect (USER.408)
  1133. */
  1134. HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
  1135. CURSORICONINFO *info,
  1136. LPCVOID lpANDbits,
  1137. LPCVOID lpXORbits )
  1138. {
  1139. HGLOBAL16 handle;
  1140. char *ptr;
  1141. int sizeAnd, sizeXor;
  1142. hInstance = GetExePtr( hInstance ); /* Make it a module handle */
  1143. if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
  1144. info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
  1145. sizeXor = info->nHeight * info->nWidthBytes;
  1146. sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
  1147. if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
  1148. sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
  1149. return 0;
  1150. FarSetOwner16( handle, hInstance );
  1151. ptr = (char *)GlobalLock16( handle );
  1152. memcpy( ptr, info, sizeof(*info) );
  1153. memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
  1154. memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
  1155. GlobalUnlock16( handle );
  1156. return handle;
  1157. }
  1158. /***********************************************************************
  1159. * CopyIcon (USER.368)
  1160. */
  1161. HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
  1162. {
  1163. TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
  1164. return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
  1165. }
  1166. /***********************************************************************
  1167. * CopyIcon (USER32.@)
  1168. */
  1169. HICON WINAPI CopyIcon( HICON hIcon )
  1170. {
  1171. TRACE_(icon)("%p\n", hIcon );
  1172. return CURSORICON_Copy( 0, hIcon );
  1173. }
  1174. /***********************************************************************
  1175. * CopyCursor (USER.369)
  1176. */
  1177. HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
  1178. {
  1179. TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
  1180. return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
  1181. }
  1182. /**********************************************************************
  1183. * DestroyIcon32 (USER.610)
  1184. *
  1185. * This routine is actually exported from Win95 USER under the name
  1186. * DestroyIcon32 ... The behaviour implemented here should mimic
  1187. * the Win95 one exactly, especially the return values, which
  1188. * depend on the setting of various flags.
  1189. */
  1190. WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
  1191. {
  1192. WORD retv;
  1193. TRACE_(icon)("(%04x, %04x)\n", handle, flags );
  1194. /* Check whether destroying active cursor */
  1195. if ( QUEUE_Current()->cursor == HICON_32(handle) )
  1196. {
  1197. WARN_(cursor)("Destroying active cursor!\n" );
  1198. SetCursor( 0 );
  1199. }
  1200. /* Try shared cursor/icon first */
  1201. if ( !(flags & CID_NONSHARED) )
  1202. {
  1203. INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
  1204. if ( count != -1 )
  1205. return (flags & CID_WIN32)? TRUE : (count == 0);
  1206. /* FIXME: OEM cursors/icons should be recognized */
  1207. }
  1208. /* Now assume non-shared cursor/icon */
  1209. retv = GlobalFree16( handle );
  1210. return (flags & CID_RESOURCE)? retv : TRUE;
  1211. }
  1212. /***********************************************************************
  1213. * DestroyIcon (USER32.@)
  1214. */
  1215. BOOL WINAPI DestroyIcon( HICON hIcon )
  1216. {
  1217. return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
  1218. }
  1219. /***********************************************************************
  1220. * DestroyCursor (USER32.@)
  1221. */
  1222. BOOL WINAPI DestroyCursor( HCURSOR hCursor )
  1223. {
  1224. return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
  1225. }
  1226. /***********************************************************************
  1227. * DrawIcon (USER32.@)
  1228. */
  1229. BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
  1230. {
  1231. CURSORICONINFO *ptr;
  1232. HDC hMemDC;
  1233. HBITMAP hXorBits, hAndBits;
  1234. COLORREF oldFg, oldBg;
  1235. if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
  1236. if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
  1237. hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
  1238. (char *)(ptr+1) );
  1239. hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
  1240. ptr->bBitsPerPixel, (char *)(ptr + 1)
  1241. + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
  1242. oldFg = SetTextColor( hdc, RGB(0,0,0) );
  1243. oldBg = SetBkColor( hdc, RGB(255,255,255) );
  1244. if (hXorBits && hAndBits)
  1245. {
  1246. HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
  1247. BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
  1248. SelectObject( hMemDC, hXorBits );
  1249. BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
  1250. SelectObject( hMemDC, hBitTemp );
  1251. }
  1252. DeleteDC( hMemDC );
  1253. if (hXorBits) DeleteObject( hXorBits );
  1254. if (hAndBits) DeleteObject( hAndBits );
  1255. GlobalUnlock16(HICON_16(hIcon));
  1256. SetTextColor( hdc, oldFg );
  1257. SetBkColor( hdc, oldBg );
  1258. return TRUE;
  1259. }
  1260. /***********************************************************************
  1261. * DumpIcon (USER.459)
  1262. */
  1263. DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
  1264. SEGPTR *lpXorBits, SEGPTR *lpAndBits )
  1265. {
  1266. CURSORICONINFO *info = MapSL( pInfo );
  1267. int sizeAnd, sizeXor;
  1268. if (!info) return 0;
  1269. sizeXor = info->nHeight * info->nWidthBytes;
  1270. sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
  1271. if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
  1272. if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
  1273. if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
  1274. return MAKELONG( sizeXor, sizeXor );
  1275. }
  1276. /***********************************************************************
  1277. * SetCursor (USER32.@)
  1278. * RETURNS:
  1279. * A handle to the previous cursor shape.
  1280. */
  1281. HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
  1282. {
  1283. MESSAGEQUEUE *queue = QUEUE_Current();
  1284. HCURSOR hOldCursor;
  1285. if (hCursor == queue->cursor) return hCursor; /* No change */
  1286. TRACE_(cursor)("%p\n", hCursor );
  1287. hOldCursor = queue->cursor;
  1288. queue->cursor = hCursor;
  1289. /* Change the cursor shape only if it is visible */
  1290. if (queue->cursor_count >= 0 && USER_Driver.pSetCursor)
  1291. {
  1292. USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
  1293. GlobalUnlock16(HCURSOR_16(hCursor));
  1294. }
  1295. return hOldCursor;
  1296. }
  1297. /***********************************************************************
  1298. * ShowCursor (USER32.@)
  1299. */
  1300. INT WINAPI ShowCursor( BOOL bShow )
  1301. {
  1302. MESSAGEQUEUE *queue = QUEUE_Current();
  1303. TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
  1304. if (bShow)
  1305. {
  1306. if (++queue->cursor_count == 0 && USER_Driver.pSetCursor) /* Show it */
  1307. {
  1308. USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
  1309. GlobalUnlock16(HCURSOR_16(queue->cursor));
  1310. }
  1311. }
  1312. else
  1313. {
  1314. if (--queue->cursor_count == -1 && USER_Driver.pSetCursor) /* Hide it */
  1315. USER_Driver.pSetCursor( NULL );
  1316. }
  1317. return queue->cursor_count;
  1318. }
  1319. /***********************************************************************
  1320. * GetCursor (USER32.@)
  1321. */
  1322. HCURSOR WINAPI GetCursor(void)
  1323. {
  1324. return QUEUE_Current()->cursor;
  1325. }
  1326. /***********************************************************************
  1327. * ClipCursor (USER32.@)
  1328. */
  1329. BOOL WINAPI ClipCursor( const RECT *rect )
  1330. {
  1331. if (!rect) SetRectEmpty( &CURSOR_ClipRect );
  1332. else CopyRect( &CURSOR_ClipRect, rect );
  1333. return TRUE;
  1334. }
  1335. /***********************************************************************
  1336. * GetClipCursor (USER32.@)
  1337. */
  1338. BOOL WINAPI GetClipCursor( RECT *rect )
  1339. {
  1340. if (rect)
  1341. {
  1342. CopyRect( rect, &CURSOR_ClipRect );
  1343. return TRUE;
  1344. }
  1345. return FALSE;
  1346. }
  1347. /**********************************************************************
  1348. * LookupIconIdFromDirectoryEx (USER.364)
  1349. *
  1350. * FIXME: exact parameter sizes
  1351. */
  1352. INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
  1353. INT16 width, INT16 height, UINT16 cFlag )
  1354. {
  1355. return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
  1356. }
  1357. /**********************************************************************
  1358. * LookupIconIdFromDirectoryEx (USER32.@)
  1359. */
  1360. INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
  1361. INT width, INT height, UINT cFlag )
  1362. {
  1363. CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
  1364. UINT retVal = 0;
  1365. if( dir && !dir->idReserved && (dir->idType & 3) )
  1366. {
  1367. CURSORICONDIRENTRY* entry;
  1368. HDC hdc;
  1369. UINT palEnts;
  1370. int colors;
  1371. hdc = GetDC(0);
  1372. palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
  1373. if (palEnts == 0)
  1374. palEnts = 256;
  1375. colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
  1376. ReleaseDC(0, hdc);
  1377. if( bIcon )
  1378. entry = CURSORICON_FindBestIcon( dir, width, height, colors );
  1379. else
  1380. entry = CURSORICON_FindBestCursor( dir, width, height, 1);
  1381. if( entry ) retVal = entry->wResId;
  1382. }
  1383. else WARN_(cursor)("invalid resource directory\n");
  1384. return retVal;
  1385. }
  1386. /**********************************************************************
  1387. * LookupIconIdFromDirectory (USER.?)
  1388. */
  1389. INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
  1390. {
  1391. return LookupIconIdFromDirectoryEx16( dir, bIcon,
  1392. bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
  1393. bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
  1394. }
  1395. /**********************************************************************
  1396. * LookupIconIdFromDirectory (USER32.@)
  1397. */
  1398. INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
  1399. {
  1400. return LookupIconIdFromDirectoryEx( dir, bIcon,
  1401. bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
  1402. bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
  1403. }
  1404. /**********************************************************************
  1405. * GetIconID (USER.455)
  1406. */
  1407. WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
  1408. {
  1409. LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
  1410. TRACE_(cursor)("hRes=%04x, entries=%i\n",
  1411. hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
  1412. switch(resType)
  1413. {
  1414. case RT_CURSOR:
  1415. return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
  1416. GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
  1417. case RT_ICON:
  1418. return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
  1419. GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
  1420. default:
  1421. WARN_(cursor)("invalid res type %ld\n", resType );
  1422. }
  1423. return 0;
  1424. }
  1425. /**********************************************************************
  1426. * LoadCursorIconHandler (USER.336)
  1427. *
  1428. * Supposed to load resources of Windows 2.x applications.
  1429. */
  1430. HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
  1431. {
  1432. FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
  1433. hResource, hModule, hRsrc);
  1434. return (HGLOBAL16)0;
  1435. }
  1436. /**********************************************************************
  1437. * LoadDIBIconHandler (USER.357)
  1438. *
  1439. * RT_ICON resource loader, installed by USER_SignalProc when module
  1440. * is initialized.
  1441. */
  1442. HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
  1443. {
  1444. /* If hResource is zero we must allocate a new memory block, if it's
  1445. * non-zero but GlobalLock() returns NULL then it was discarded and
  1446. * we have to recommit some memory, otherwise we just need to check
  1447. * the block size. See LoadProc() in 16-bit SDK for more.
  1448. */
  1449. hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
  1450. if( hMemObj )
  1451. {
  1452. LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
  1453. hMemObj = HICON_16(CURSORICON_CreateFromResource(
  1454. hModule, hMemObj, bits,
  1455. SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
  1456. GetSystemMetrics(SM_CXICON),
  1457. GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
  1458. }
  1459. return hMemObj;
  1460. }
  1461. /**********************************************************************
  1462. * LoadDIBCursorHandler (USER.356)
  1463. *
  1464. * RT_CURSOR resource loader. Same as above.
  1465. */
  1466. HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
  1467. {
  1468. hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
  1469. if( hMemObj )
  1470. {
  1471. LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
  1472. hMemObj = HICON_16(CURSORICON_CreateFromResource(
  1473. hModule, hMemObj, bits,
  1474. SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
  1475. GetSystemMetrics(SM_CXCURSOR),
  1476. GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
  1477. }
  1478. return hMemObj;
  1479. }
  1480. /**********************************************************************
  1481. * LoadIconHandler (USER.456)
  1482. */
  1483. HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
  1484. {
  1485. LPBYTE bits = (LPBYTE)LockResource16( hResource );
  1486. TRACE_(cursor)("hRes=%04x\n",hResource);
  1487. return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
  1488. bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
  1489. }
  1490. /***********************************************************************
  1491. * LoadCursorW (USER32.@)
  1492. */
  1493. HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
  1494. {
  1495. return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
  1496. LR_SHARED | LR_DEFAULTSIZE );
  1497. }
  1498. /***********************************************************************
  1499. * LoadCursorA (USER32.@)
  1500. */
  1501. HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
  1502. {
  1503. return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
  1504. LR_SHARED | LR_DEFAULTSIZE );
  1505. }
  1506. /***********************************************************************
  1507. * LoadCursorFromFileW (USER32.@)
  1508. */
  1509. HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
  1510. {
  1511. return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
  1512. LR_LOADFROMFILE | LR_DEFAULTSIZE );
  1513. }
  1514. /***********************************************************************
  1515. * LoadCursorFromFileA (USER32.@)
  1516. */
  1517. HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
  1518. {
  1519. return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
  1520. LR_LOADFROMFILE | LR_DEFAULTSIZE );
  1521. }
  1522. /***********************************************************************
  1523. * LoadIconW (USER32.@)
  1524. */
  1525. HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
  1526. {
  1527. return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
  1528. LR_SHARED | LR_DEFAULTSIZE );
  1529. }
  1530. /***********************************************************************
  1531. * LoadIconA (USER32.@)
  1532. */
  1533. HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
  1534. {
  1535. return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
  1536. LR_SHARED | LR_DEFAULTSIZE );
  1537. }
  1538. /**********************************************************************
  1539. * GetIconInfo (USER32.@)
  1540. */
  1541. BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
  1542. {
  1543. CURSORICONINFO *ciconinfo;
  1544. INT height;
  1545. ciconinfo = GlobalLock16(HICON_16(hIcon));
  1546. if (!ciconinfo)
  1547. return FALSE;
  1548. if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
  1549. (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
  1550. {
  1551. iconinfo->fIcon = TRUE;
  1552. iconinfo->xHotspot = ciconinfo->nWidth / 2;
  1553. iconinfo->yHotspot = ciconinfo->nHeight / 2;
  1554. }
  1555. else
  1556. {
  1557. iconinfo->fIcon = FALSE;
  1558. iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
  1559. iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
  1560. }
  1561. if (ciconinfo->bBitsPerPixel > 1)
  1562. {
  1563. iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
  1564. ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
  1565. (char *)(ciconinfo + 1)
  1566. + ciconinfo->nHeight *
  1567. get_bitmap_width_bytes (ciconinfo->nWidth,1) );
  1568. height = ciconinfo->nHeight;
  1569. }
  1570. else
  1571. {
  1572. iconinfo->hbmColor = 0;
  1573. height = ciconinfo->nHeight * 2;
  1574. }
  1575. iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
  1576. 1, 1, (char *)(ciconinfo + 1));
  1577. GlobalUnlock16(HICON_16(hIcon));
  1578. return TRUE;
  1579. }
  1580. /**********************************************************************
  1581. * CreateIconIndirect (USER32.@)
  1582. */
  1583. HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
  1584. {
  1585. BITMAP bmpXor,bmpAnd;
  1586. HICON16 hObj;
  1587. int sizeXor,sizeAnd;
  1588. GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
  1589. GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
  1590. sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
  1591. sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
  1592. hObj = GlobalAlloc16( GMEM_MOVEABLE,
  1593. sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
  1594. if (hObj)
  1595. {
  1596. CURSORICONINFO *info;
  1597. info = (CURSORICONINFO *)GlobalLock16( hObj );
  1598. /* If we are creating an icon, the hotspot is unused */
  1599. if (iconinfo->fIcon)
  1600. {
  1601. info->ptHotSpot.x = ICON_HOTSPOT;
  1602. info->ptHotSpot.y = ICON_HOTSPOT;
  1603. }
  1604. else
  1605. {
  1606. info->ptHotSpot.x = iconinfo->xHotspot;
  1607. info->ptHotSpot.y = iconinfo->yHotspot;
  1608. }
  1609. info->nWidth = bmpXor.bmWidth;
  1610. info->nHeight = bmpXor.bmHeight;
  1611. info->nWidthBytes = bmpXor.bmWidthBytes;
  1612. info->bPlanes = bmpXor.bmPlanes;
  1613. info->bBitsPerPixel = bmpXor.bmBitsPixel;
  1614. /* Transfer the bitmap bits to the CURSORICONINFO structure */
  1615. GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
  1616. GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
  1617. GlobalUnlock16( hObj );
  1618. }
  1619. return HICON_32(hObj);
  1620. }
  1621. /******************************************************************************
  1622. * DrawIconEx (USER32.@) Draws an icon or cursor on device context
  1623. *
  1624. * NOTES
  1625. * Why is this using SM_CXICON instead of SM_CXCURSOR?
  1626. *
  1627. * PARAMS
  1628. * hdc [I] Handle to device context
  1629. * x0 [I] X coordinate of upper left corner
  1630. * y0 [I] Y coordinate of upper left corner
  1631. * hIcon [I] Handle to icon to draw
  1632. * cxWidth [I] Width of icon
  1633. * cyWidth [I] Height of icon
  1634. * istep [I] Index of frame in animated cursor
  1635. * hbr [I] Handle to background brush
  1636. * flags [I] Icon-drawing flags
  1637. *
  1638. * RETURNS
  1639. * Success: TRUE
  1640. * Failure: FALSE
  1641. */
  1642. BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
  1643. INT cxWidth, INT cyWidth, UINT istep,
  1644. HBRUSH hbr, UINT flags )
  1645. {
  1646. CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
  1647. HDC hDC_off = 0, hMemDC;
  1648. BOOL result = FALSE, DoOffscreen;
  1649. HBITMAP hB_off = 0, hOld = 0;
  1650. if (!ptr) return FALSE;
  1651. TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
  1652. hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
  1653. hMemDC = CreateCompatibleDC (hdc);
  1654. if (istep)
  1655. FIXME_(icon)("Ignoring istep=%d\n", istep);
  1656. if (flags & DI_COMPAT)
  1657. FIXME_(icon)("Ignoring flag DI_COMPAT\n");
  1658. if (!flags) {
  1659. FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
  1660. flags = DI_NORMAL;
  1661. }
  1662. /* Calculate the size of the destination image. */
  1663. if (cxWidth == 0)
  1664. {
  1665. if (flags & DI_DEFAULTSIZE)
  1666. cxWidth = GetSystemMetrics (SM_CXICON);
  1667. else
  1668. cxWidth = ptr->nWidth;
  1669. }
  1670. if (cyWidth == 0)
  1671. {
  1672. if (flags & DI_DEFAULTSIZE)
  1673. cyWidth = GetSystemMetrics (SM_CYICON);
  1674. else
  1675. cyWidth = ptr->nHeight;
  1676. }
  1677. DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
  1678. if (DoOffscreen) {
  1679. RECT r;
  1680. r.left = 0;
  1681. r.top = 0;
  1682. r.right = cxWidth;
  1683. r.bottom = cxWidth;
  1684. hDC_off = CreateCompatibleDC(hdc);
  1685. hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
  1686. if (hDC_off && hB_off) {
  1687. hOld = SelectObject(hDC_off, hB_off);
  1688. FillRect(hDC_off, &r, hbr);
  1689. }
  1690. }
  1691. if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
  1692. {
  1693. HBITMAP hXorBits, hAndBits;
  1694. COLORREF oldFg, oldBg;
  1695. INT nStretchMode;
  1696. nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
  1697. hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
  1698. ptr->bPlanes, ptr->bBitsPerPixel,
  1699. (char *)(ptr + 1)
  1700. + ptr->nHeight *
  1701. get_bitmap_width_bytes(ptr->nWidth,1) );
  1702. hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
  1703. 1, 1, (char *)(ptr+1) );
  1704. oldFg = SetTextColor( hdc, RGB(0,0,0) );
  1705. oldBg = SetBkColor( hdc, RGB(255,255,255) );
  1706. if (hXorBits && hAndBits)
  1707. {
  1708. HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
  1709. if (flags & DI_MASK)
  1710. {
  1711. if (DoOffscreen)
  1712. StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
  1713. hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
  1714. else
  1715. StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
  1716. hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
  1717. }
  1718. SelectObject( hMemDC, hXorBits );
  1719. if (flags & DI_IMAGE)
  1720. {
  1721. if (DoOffscreen)
  1722. StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
  1723. hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
  1724. else
  1725. StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
  1726. hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
  1727. }
  1728. SelectObject( hMemDC, hBitTemp );
  1729. result = TRUE;
  1730. }
  1731. SetTextColor( hdc, oldFg );
  1732. SetBkColor( hdc, oldBg );
  1733. if (hXorBits) DeleteObject( hXorBits );
  1734. if (hAndBits) DeleteObject( hAndBits );
  1735. SetStretchBltMode (hdc, nStretchMode);
  1736. if (DoOffscreen) {
  1737. BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
  1738. SelectObject(hDC_off, hOld);
  1739. }
  1740. }
  1741. if (hMemDC) DeleteDC( hMemDC );
  1742. if (hDC_off) DeleteDC(hDC_off);
  1743. if (hB_off) DeleteObject(hB_off);
  1744. GlobalUnlock16(HICON_16(hIcon));
  1745. return result;
  1746. }
  1747. /***********************************************************************
  1748. * DIB_FixColorsToLoadflags
  1749. *
  1750. * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
  1751. * are in loadflags
  1752. */
  1753. static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
  1754. {
  1755. int colors;
  1756. COLORREF c_W, c_S, c_F, c_L, c_C;
  1757. int incr,i;
  1758. RGBQUAD *ptr;
  1759. int bitmap_type;
  1760. LONG width;
  1761. LONG height;
  1762. WORD bpp;
  1763. DWORD compr;
  1764. if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
  1765. {
  1766. WARN_(resource)("Invalid bitmap\n");
  1767. return;
  1768. }
  1769. if (bpp > 8) return;
  1770. if (bitmap_type == 0) /* BITMAPCOREHEADER */
  1771. {
  1772. incr = 3;
  1773. colors = 1 << bpp;
  1774. }
  1775. else
  1776. {
  1777. incr = 4;
  1778. colors = bmi->bmiHeader.biClrUsed;
  1779. if (!colors && (bpp <= 8)) colors = 1 << bpp;
  1780. }
  1781. c_W = GetSysColor(COLOR_WINDOW);
  1782. c_S = GetSysColor(COLOR_3DSHADOW);
  1783. c_F = GetSysColor(COLOR_3DFACE);
  1784. c_L = GetSysColor(COLOR_3DLIGHT);
  1785. if (loadflags & LR_LOADTRANSPARENT) {
  1786. switch (bpp) {
  1787. case 1: pix = pix >> 7; break;
  1788. case 4: pix = pix >> 4; break;
  1789. case 8: break;
  1790. default:
  1791. WARN_(resource)("(%d): Unsupported depth\n", bpp);
  1792. return;
  1793. }
  1794. if (pix >= colors) {
  1795. WARN_(resource)("pixel has color index greater than biClrUsed!\n");
  1796. return;
  1797. }
  1798. if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
  1799. ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
  1800. ptr->rgbBlue = GetBValue(c_W);
  1801. ptr->rgbGreen = GetGValue(c_W);
  1802. ptr->rgbRed = GetRValue(c_W);
  1803. }
  1804. if (loadflags & LR_LOADMAP3DCOLORS)
  1805. for (i=0; i<colors; i++) {
  1806. ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
  1807. c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
  1808. if (c_C == RGB(128, 128, 128)) {
  1809. ptr->rgbRed = GetRValue(c_S);
  1810. ptr->rgbGreen = GetGValue(c_S);
  1811. ptr->rgbBlue = GetBValue(c_S);
  1812. } else if (c_C == RGB(192, 192, 192)) {
  1813. ptr->rgbRed = GetRValue(c_F);
  1814. ptr->rgbGreen = GetGValue(c_F);
  1815. ptr->rgbBlue = GetBValue(c_F);
  1816. } else if (c_C == RGB(223, 223, 223)) {
  1817. ptr->rgbRed = GetRValue(c_L);
  1818. ptr->rgbGreen = GetGValue(c_L);
  1819. ptr->rgbBlue = GetBValue(c_L);
  1820. }
  1821. }
  1822. }
  1823. /**********************************************************************
  1824. * BITMAP_Load
  1825. */
  1826. static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name, UINT loadflags )
  1827. {
  1828. HBITMAP hbitmap = 0;
  1829. HRSRC hRsrc;
  1830. HGLOBAL handle;
  1831. char *ptr = NULL;
  1832. BITMAPINFO *info, *fix_info=NULL;
  1833. HGLOBAL hFix;
  1834. int size;
  1835. if (!(loadflags & LR_LOADFROMFILE))
  1836. {
  1837. if (!instance)
  1838. {
  1839. /* OEM bitmap: try to load the resource from user32.dll */
  1840. if (HIWORD(name)) return 0;
  1841. instance = user32_module;
  1842. }
  1843. if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
  1844. if (!(handle = LoadResource( instance, hRsrc ))) return 0;
  1845. if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
  1846. }
  1847. else
  1848. {
  1849. if (!(ptr = map_fileW( name, NULL ))) return 0;
  1850. info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
  1851. }
  1852. size = bitmap_info_size(info, DIB_RGB_COLORS);
  1853. if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
  1854. if (fix_info) {
  1855. BYTE pix;
  1856. memcpy(fix_info, info, size);
  1857. pix = *((LPBYTE)info + size);
  1858. DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
  1859. if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
  1860. if (screen_dc)
  1861. {
  1862. char *bits = (char *)info + size;
  1863. if (loadflags & LR_CREATEDIBSECTION) {
  1864. DIBSECTION dib;
  1865. fix_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
  1866. hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
  1867. GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
  1868. SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
  1869. DIB_RGB_COLORS);
  1870. }
  1871. else {
  1872. /* If it's possible, create a monochrome bitmap */
  1873. LONG width;
  1874. LONG height;
  1875. WORD bpp;
  1876. DWORD compr;
  1877. if (DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height, &bpp, &compr ) != -1)
  1878. {
  1879. if (width < 0)
  1880. TRACE("Bitmap has a negative width\n");
  1881. else
  1882. {
  1883. /* Top-down DIBs have a negative height */
  1884. if (height < 0) height = -height;
  1885. TRACE("width=%ld, height=%ld, bpp=%u, compr=%lu\n", width, height, bpp, compr);
  1886. if (is_dib_monochrome(fix_info))
  1887. hbitmap = CreateBitmap(width, height, 1, 1, NULL);
  1888. else
  1889. hbitmap = CreateCompatibleBitmap(screen_dc, width, height);
  1890. SetDIBits(screen_dc, hbitmap, 0, height, bits, info, DIB_RGB_COLORS);
  1891. }
  1892. }
  1893. }
  1894. }
  1895. GlobalUnlock(hFix);
  1896. GlobalFree(hFix);
  1897. }
  1898. if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
  1899. return hbitmap;
  1900. }
  1901. /**********************************************************************
  1902. * LoadImageA (USER32.@)
  1903. *
  1904. * FIXME: implementation lacks some features, see LR_ defines in winuser.h
  1905. */
  1906. /* filter for page-fault exceptions */
  1907. static WINE_EXCEPTION_FILTER(page_fault)
  1908. {
  1909. if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
  1910. return EXCEPTION_EXECUTE_HANDLER;
  1911. return EXCEPTION_CONTINUE_SEARCH;
  1912. }
  1913. /*********************************************************************/
  1914. HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
  1915. INT desiredx, INT desiredy, UINT loadflags)
  1916. {
  1917. HANDLE res;
  1918. LPWSTR u_name;
  1919. if (!HIWORD(name))
  1920. return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
  1921. __TRY {
  1922. DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
  1923. u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
  1924. MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
  1925. }
  1926. __EXCEPT(page_fault) {
  1927. SetLastError( ERROR_INVALID_PARAMETER );
  1928. return 0;
  1929. }
  1930. __ENDTRY
  1931. res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
  1932. HeapFree(GetProcessHeap(), 0, u_name);
  1933. return res;
  1934. }
  1935. /******************************************************************************
  1936. * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
  1937. *
  1938. * PARAMS
  1939. * hinst [I] Handle of instance that contains image
  1940. * name [I] Name of image
  1941. * type [I] Type of image
  1942. * desiredx [I] Desired width
  1943. * desiredy [I] Desired height
  1944. * loadflags [I] Load flags
  1945. *
  1946. * RETURNS
  1947. * Success: Handle to newly loaded image
  1948. * Failure: NULL
  1949. *
  1950. * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
  1951. */
  1952. HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
  1953. INT desiredx, INT desiredy, UINT loadflags )
  1954. {
  1955. if (HIWORD(name)) {
  1956. TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
  1957. hinst,name,type,desiredx,desiredy,loadflags);
  1958. } else {
  1959. TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
  1960. hinst,name,type,desiredx,desiredy,loadflags);
  1961. }
  1962. if (loadflags & LR_DEFAULTSIZE) {
  1963. if (type == IMAGE_ICON) {
  1964. if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
  1965. if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
  1966. } else if (type == IMAGE_CURSOR) {
  1967. if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
  1968. if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
  1969. }
  1970. }
  1971. if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
  1972. switch (type) {
  1973. case IMAGE_BITMAP:
  1974. return BITMAP_Load( hinst, name, loadflags );
  1975. case IMAGE_ICON:
  1976. if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
  1977. if (screen_dc)
  1978. {
  1979. UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
  1980. if (palEnts == 0) palEnts = 256;
  1981. return CURSORICON_Load(hinst, name, desiredx, desiredy,
  1982. palEnts, FALSE, loadflags);
  1983. }
  1984. break;
  1985. case IMAGE_CURSOR:
  1986. return CURSORICON_Load(hinst, name, desiredx, desiredy,
  1987. 1, TRUE, loadflags);
  1988. }
  1989. return 0;
  1990. }
  1991. /******************************************************************************
  1992. * CopyImage (USER32.@) Creates new image and copies attributes to it
  1993. *
  1994. * PARAMS
  1995. * hnd [I] Handle to image to copy
  1996. * type [I] Type of image to copy
  1997. * desiredx [I] Desired width of new image
  1998. * desiredy [I] Desired height of new image
  1999. * flags [I] Copy flags
  2000. *
  2001. * RETURNS
  2002. * Success: Handle to newly created image
  2003. * Failure: NULL
  2004. *
  2005. * FIXME: implementation still lacks nearly all features, see LR_*
  2006. * defines in winuser.h
  2007. */
  2008. HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
  2009. INT desiredy, UINT flags )
  2010. {
  2011. switch (type)
  2012. {
  2013. case IMAGE_BITMAP:
  2014. {
  2015. HBITMAP res;
  2016. BITMAP bm;
  2017. if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
  2018. bm.bmBits = NULL;
  2019. if ((res = CreateBitmapIndirect(&bm)))
  2020. {
  2021. char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
  2022. GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
  2023. SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
  2024. HeapFree( GetProcessHeap(), 0, buf );
  2025. }
  2026. return (HICON)res;
  2027. }
  2028. case IMAGE_ICON:
  2029. return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
  2030. case IMAGE_CURSOR:
  2031. /* Should call CURSORICON_ExtCopy but more testing
  2032. * needs to be done before we change this
  2033. */
  2034. return CopyCursor(hnd);
  2035. }
  2036. return 0;
  2037. }
  2038. /******************************************************************************
  2039. * LoadBitmapW (USER32.@) Loads bitmap from the executable file
  2040. *
  2041. * RETURNS
  2042. * Success: Handle to specified bitmap
  2043. * Failure: NULL
  2044. */
  2045. HBITMAP WINAPI LoadBitmapW(
  2046. HINSTANCE instance, /* [in] Handle to application instance */
  2047. LPCWSTR name) /* [in] Address of bitmap resource name */
  2048. {
  2049. return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
  2050. }
  2051. /**********************************************************************
  2052. * LoadBitmapA (USER32.@)
  2053. */
  2054. HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
  2055. {
  2056. return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
  2057. }