source: trunk/src/user32/loadres.cpp@ 4586

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

LoadBitmapW fix for OS/2 1.3 bitmap conversion heap corruption

File size: 18.2 KB
Line 
1/* $Id: loadres.cpp,v 1.34 2000-11-12 10:58:12 sandervl Exp $ */
2
3/*
4 * Win32 resource API functions for OS/2
5 *
6 * Copyright 1998 Sander van Leeuwen
7 *
8 * Parts 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 <user32.h>
22#include <heapstring.h>
23#include <oslibres.h>
24#include <win\virtual.h>
25#include "dib.h"
26#include "initterm.h"
27#include <win\cursoricon.h>
28#include <winres.h>
29
30#define DBG_LOCALLOG DBG_loadres
31#include "dbglocal.h"
32
33//******************************************************************************
34//******************************************************************************
35INT WIN32API LoadStringA(HINSTANCE instance, UINT resource_id,
36 LPSTR buffer, INT buflen )
37{
38 INT retval;
39 LPWSTR buffer2 = NULL;
40
41 if (buffer && buflen)
42 buffer2 = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
43
44 retval = LoadStringW(instance,resource_id,buffer2,buflen);
45
46 if (buffer2)
47 {
48 if (retval) {
49 lstrcpynWtoA( buffer, buffer2, buflen );
50 retval = lstrlenA( buffer );
51 }
52 else
53 *buffer = 0;
54 HeapFree( GetProcessHeap(), 0, buffer2 );
55 }
56 return retval;
57}
58//******************************************************************************
59//******************************************************************************
60int WIN32API LoadStringW(HINSTANCE hinst, UINT wID, LPWSTR lpBuffer, int cchBuffer)
61{
62 WCHAR *p;
63 int string_num;
64 int i = 0;
65 HRSRC hRes;
66
67 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
68 * 20 - 31. */
69 hRes = FindResourceW(hinst, (LPWSTR)(((wID>>4)&0xffff)+1), RT_STRINGW);
70 if(hRes == NULL) {
71 dprintf(("LoadStringW NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
72 *lpBuffer = 0;
73 return 0;
74 }
75
76 p = (LPWSTR)LockResource(LoadResource(hinst, hRes));
77 if(p) {
78 string_num = wID & 0x000f;
79 for (i = 0; i < string_num; i++)
80 p += *p + 1;
81
82 if (lpBuffer == NULL) return *p;
83 i = min(cchBuffer - 1, *p);
84 if (i > 0) {
85 memcpy(lpBuffer, p + 1, i * sizeof (WCHAR));
86 lpBuffer[i] = (WCHAR) 0;
87 }
88 else {
89 if (cchBuffer > 1) {
90 lpBuffer[0] = (WCHAR) 0;
91 return 0;
92 }
93 }
94 }
95
96#ifdef DEBUG_ENABLELOG_LEVEL2
97 if(i) {
98 char *astring = (char *)HEAP_strdupWtoA(GetProcessHeap(), 0, lpBuffer);
99 dprintf(("LoadStringW from %X, id %d %s\n", hinst, wID, astring));
100 HEAP_free(astring);
101 }
102#else
103 dprintf(("LoadStringW from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
104#endif
105 return(i);
106}
107//******************************************************************************
108//******************************************************************************
109HICON WIN32API LoadIconA(HINSTANCE hinst, LPCSTR lpszIcon)
110{
111 if(HIWORD(lpszIcon)) {
112 dprintf(("LoadIconA %x %s", hinst, lpszIcon));
113 }
114 else dprintf(("LoadIconA %x %x", hinst, lpszIcon));
115 return LoadImageA(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
116}
117//******************************************************************************
118//******************************************************************************
119HICON WIN32API LoadIconW(HINSTANCE hinst, LPCWSTR lpszIcon)
120{
121 dprintf(("LoadIconA %x %x", hinst, lpszIcon));
122 return LoadImageW(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
123}
124//******************************************************************************
125//******************************************************************************
126HCURSOR LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor, DWORD cxDesired,
127 DWORD cyDesired, DWORD fuLoad)
128{
129 HCURSOR hCursor;
130 HANDLE hMapping = 0;
131 char *ptr = NULL;
132 HRSRC hRes;
133 LPSTR restype = RT_CURSORA;
134 LPVOID lpOS2Resdata = NULL;
135
136 if(fuLoad & LR_LOADFROMFILE)
137 {
138 hMapping = VIRTUAL_MapFileW( lpszCursor, (LPVOID *)&ptr, TRUE);
139 if(hMapping == INVALID_HANDLE_VALUE)
140 return 0;
141
142 lpOS2Resdata = ConvertCursorToOS2(ptr);
143 hCursor = OSLibWinCreatePointer(lpOS2Resdata, cxDesired, cyDesired);
144 FreeOS2Resource(lpOS2Resdata);
145
146 UnmapViewOfFile(ptr);
147 CloseHandle(hMapping);
148 }
149 else
150 {
151 if(!hinst)
152 {
153 hRes = FindResourceW(hInstanceUser32,lpszCursor,RT_CURSORW);
154 if(!hRes) {
155 hRes = FindResourceW(hInstanceUser32,lpszCursor,RT_GROUP_CURSORW);
156 restype = RT_GROUP_CURSORA;
157 }
158 if(hRes)
159 {
160 lpOS2Resdata = ConvertResourceToOS2(hInstanceUser32, restype, hRes);
161 hCursor = OSLibWinCreatePointer(lpOS2Resdata, cxDesired, cyDesired);
162 FreeOS2Resource(lpOS2Resdata);
163 }
164 else hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor, cxDesired, cyDesired);
165 }
166 else
167 { //not a system icon
168 hRes = FindResourceW(hinst,lpszCursor,RT_CURSORW);
169 if(!hRes) {
170 hRes = FindResourceW(hinst,lpszCursor,RT_GROUP_CURSORW);
171 restype = RT_GROUP_CURSORA;
172 }
173 if(hRes) {
174 lpOS2Resdata = ConvertResourceToOS2(hinst, restype, hRes);
175 hCursor = OSLibWinCreatePointer(lpOS2Resdata, cxDesired, cyDesired);
176 FreeOS2Resource(lpOS2Resdata);
177 }
178 else hCursor = 0;
179 }
180 }
181 dprintf(("LoadCursorA %x from %x returned %x\n", lpszCursor, hinst, hCursor));
182
183 return(hCursor);
184}
185//******************************************************************************
186//******************************************************************************
187HCURSOR WIN32API LoadCursorA(HINSTANCE hinst, LPCSTR lpszCursor)
188{
189 return LoadImageA(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
190 LR_SHARED | LR_DEFAULTSIZE );
191}
192//******************************************************************************
193//******************************************************************************
194HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
195{
196 return LoadImageW(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
197 LR_SHARED | LR_DEFAULTSIZE );
198}
199/***********************************************************************
200* LoadCursorFromFileW (USER32.361)
201*/
202HCURSOR WIN32API LoadCursorFromFileW (LPCWSTR name)
203{
204 return LoadImageW(0, name, IMAGE_CURSOR, 0, 0,
205 LR_LOADFROMFILE | LR_DEFAULTSIZE );
206}
207/***********************************************************************
208* LoadCursorFromFileA (USER32.360)
209*/
210HCURSOR WIN32API LoadCursorFromFileA (LPCSTR name)
211{
212 return LoadImageA(0, name, IMAGE_CURSOR, 0, 0,
213 LR_LOADFROMFILE | LR_DEFAULTSIZE );
214}
215//******************************************************************************
216//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)! (still??)
217//******************************************************************************
218HANDLE LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszName, int cxDesired, int cyDesired,
219 UINT fuLoad)
220{
221 HBITMAP hbitmap = 0;
222 HDC hdc;
223 HRSRC hRsrc;
224 HGLOBAL handle, hMapping = 0;
225 char *ptr = NULL;
226 BITMAPINFO *info, *fix_info=NULL;
227 HGLOBAL hFix;
228 int size;
229
230 if (!(fuLoad & LR_LOADFROMFILE))
231 {
232 handle = 0;
233 if(!hinst)
234 {
235 hRsrc = FindResourceW( hInstanceUser32, lpszName, RT_BITMAPW );
236 if(hRsrc) {
237 handle = LoadResource( hInstanceUser32, hRsrc );
238 }
239 }
240 if(handle == 0)
241 {
242 if (!(hRsrc = FindResourceW( hinst, lpszName, RT_BITMAPW ))) return 0;
243 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
244 }
245
246 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
247 }
248 else
249 {
250 hMapping = VIRTUAL_MapFileW( lpszName, (LPVOID *)&ptr, TRUE);
251 if (hMapping == INVALID_HANDLE_VALUE) {
252 //TODO: last err set to ERROR_OPEN_FAILED if file not found; correct??
253 dprintf(("LoadBitmapW: failed to load file %x (lasterr=%x)", lpszName, GetLastError()));
254 return 0;
255 }
256 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
257 }
258
259 //TODO: This has to be removed once pe2lx stores win32 resources!!!
260 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
261 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
262 {//assume it contains a file header first
263 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
264 }
265
266 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
267 {//determine size of converted header
268 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
269
270 int colors = 0;
271 if (core->bcBitCount <= 8) {
272 colors = (1 << core->bcBitCount);
273 }
274 size = sizeof(BITMAPINFOHEADER) + colors * sizeof(RGBQUAD);
275 }
276 else size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
277
278 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
279 if (fix_info)
280 {
281 BYTE pix;
282
283 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
284 {//convert old bitmap header to new format
285 ULONG colors;
286 ULONG *p, *q;
287
288 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
289 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
290 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
291 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
292 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
293 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
294
295 if(fix_info->bmiHeader.biBitCount <= 8)
296 {
297 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
298 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
299 //convert RGBTRIPLE to RGBQUAD
300 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
301 *q = *p & 0x00FFFFFFUL;
302 q++;
303 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
304 }
305 }
306 }
307 else {
308 memcpy(fix_info, info, size);
309 }
310
311 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
312
313 pix = *((LPBYTE)info + size);
314 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
315 if ((hdc = GetDC(0)) != 0)
316 {
317 char *bits = (char *)info + size;
318 if (fuLoad & LR_CREATEDIBSECTION) {
319 DIBSECTION dib;
320 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
321 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
322 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
323 DIB_RGB_COLORS);
324 }
325 else
326 {
327#if 0
328 if(fix_info->bmiHeader.biBitCount == 1) {
329 hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth,
330 fix_info->bmiHeader.biHeight,
331 fix_info->bmiHeader.biPlanes,
332 fix_info->bmiHeader.biBitCount,
333 (PVOID)bits);
334 }
335 else {
336#endif
337 hbitmap = CreateDIBitmap(hdc, &fix_info->bmiHeader, CBM_INIT,
338 bits, fix_info, DIB_RGB_COLORS );
339// }
340 if(hbitmap == 0) {
341 dprintf(("LoadBitmapW: CreateDIBitmap failed!!"));
342 }
343 }
344 ReleaseDC( 0, hdc );
345 }
346 GlobalUnlock(hFix);
347 GlobalFree(hFix);
348 }
349 if(fuLoad & LR_LOADFROMFILE) {
350 UnmapViewOfFile(ptr);
351 CloseHandle(hMapping);
352 }
353 return hbitmap;
354}
355//******************************************************************************
356//TODO: scale bitmap
357//******************************************************************************
358HANDLE CopyBitmap(HANDLE hBitmap, DWORD desiredx, DWORD desiredy)
359{
360 HBITMAP res = 0;
361 BITMAP bm;
362
363 if(GetObjectA(hBitmap, sizeof(BITMAP), &bm) == FALSE) {
364 dprintf(("CopyBitmap: GetObject failed!!"));
365 return 0;
366 }
367
368 bm.bmBits = NULL;
369 res = CreateBitmapIndirect(&bm);
370
371 if(res)
372 {
373 char *buf = (char *)HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
374 bm.bmHeight );
375 GetBitmapBits (hBitmap, bm.bmWidthBytes * bm.bmHeight, buf);
376 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
377 HeapFree( GetProcessHeap(), 0, buf );
378 }
379 return res;
380}
381//******************************************************************************
382//TODO: No support for RT_NEWBITMAP
383//******************************************************************************
384HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
385{
386 HBITMAP hBitmap = 0;
387
388 hBitmap = LoadImageA(hinst, lpszBitmap, IMAGE_BITMAP, 0, 0, 0);
389
390 if(HIWORD(lpszBitmap)) {
391 dprintf(("LoadBitmapA %x %s returned %08xh\n", hinst, lpszBitmap, hBitmap));
392 }
393 else dprintf(("LoadBitmapA %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
394
395 return(hBitmap);
396}
397//******************************************************************************
398//TODO: No support for RT_NEWBITMAP
399//******************************************************************************
400HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
401{
402 HBITMAP hBitmap = 0;
403
404 hBitmap = LoadBitmapW((hinst == 0) ? hInstanceUser32:hinst, lpszBitmap, 0, 0, 0);
405
406 if(HIWORD(lpszBitmap)) {
407 dprintf(("LoadBitmapW %x %s returned %08xh\n", hinst, lpszBitmap, hBitmap));
408 }
409 else dprintf(("LoadBitmapW %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
410
411 return(hBitmap);
412}
413//******************************************************************************
414//******************************************************************************
415HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
416 int cxDesired, int cyDesired, UINT fuLoad)
417{
418 HANDLE res = 0;
419 LPCWSTR u_name;
420
421 if(HIWORD(lpszName)) {
422 dprintf(("LoadImageA %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
423 }
424 else dprintf(("LoadImageA %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
425
426 if (HIWORD(lpszName)) {
427 u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, lpszName);
428 }
429 else u_name=(LPWSTR)lpszName;
430
431 res = LoadImageW(hinst, u_name, uType, cxDesired, cyDesired, fuLoad);
432
433 if (HIWORD(lpszName))
434 HeapFree(GetProcessHeap(), 0, (LPVOID)u_name);
435
436 return res;
437}
438//******************************************************************************
439//******************************************************************************
440HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
441 int cxDesired, int cyDesired, UINT fuLoad)
442{
443 HANDLE hRet = 0;
444
445 dprintf(("LoadImageW %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
446
447 if (fuLoad & LR_DEFAULTSIZE) {
448 if (uType == IMAGE_ICON) {
449 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
450 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
451 }
452 else if (uType == IMAGE_CURSOR) {
453 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
454 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
455 }
456 }
457 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
458
459 switch (uType)
460 {
461 case IMAGE_BITMAP:
462 {
463 hRet = (HANDLE)LoadBitmapW(hinst, lpszName, cxDesired, cyDesired, fuLoad);
464 break;
465 }
466 case IMAGE_ICON:
467 {
468 HDC hdc = GetDC(0);
469 UINT palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
470 if (palEnts == 0)
471 palEnts = 256;
472 ReleaseDC(0, hdc);
473
474 hRet = CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, palEnts, FALSE, fuLoad);
475 break;
476 }
477
478 case IMAGE_CURSOR:
479 hRet = (HANDLE)LoadCursorW(hinst, lpszName, cxDesired, cyDesired, fuLoad);
480 break;
481// return CURSORICON_Load(hinst, name, desiredx, desiredy, 1, TRUE, loadflags);
482
483 default:
484 dprintf(("LoadImageW: unsupported type %d!!", uType));
485 return 0;
486 }
487 dprintf(("LoadImageW returned %x\n", (int)hRet));
488
489 return(hRet);
490}
491/******************************************************************************
492 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
493 *
494 * PARAMS
495 * hnd [I] Handle to image to copy
496 * type [I] Type of image to copy
497 * desiredx [I] Desired width of new image
498 * desiredy [I] Desired height of new image
499 * flags [I] Copy flags
500 *
501 * RETURNS
502 * Success: Handle to newly created image
503 * Failure: NULL
504 *
505 * FIXME: implementation still lacks nearly all features, see LR_*
506 * defines in windows.h
507 *
508 */
509HICON WINAPI CopyImage(HANDLE hnd, UINT type, INT desiredx,
510 INT desiredy, UINT flags )
511{
512 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
513 switch (type)
514 {
515 case IMAGE_BITMAP:
516 return CopyBitmap(hnd, desiredx, desiredy);
517 case IMAGE_ICON:
518 return (HANDLE)CURSORICON_ExtCopy(hnd, type, desiredx, desiredy, flags);
519 case IMAGE_CURSOR:
520 /* Should call CURSORICON_ExtCopy but more testing
521 * needs to be done before we change this
522 */
523 return O32_CopyCursor(hnd);
524// return CopyCursorIcon(hnd,type, desiredx, desiredy, flags);
525 default:
526 dprintf(("CopyImage: Unsupported type"));
527 }
528 return 0;
529}
530//******************************************************************************
531//******************************************************************************
Note: See TracBrowser for help on using the repository browser.