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

Last change on this file since 2265 was 2265, checked in by sandervl, 26 years ago

Load entire bitmap into memory for LoadBitmapA (file)

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