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

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

Several updates/bug fixes

File size: 18.5 KB
Line 
1/* $Id: loadres.cpp,v 1.18 2000-01-09 14:37:09 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 return LoadBitmapA((hinst == 0) ? hInstanceUser32:hinst,lpszBitmap,0,0,0);
393#if 0
394 if (!hinst)
395 {
396 if(IsSystemBitmap((ULONG *)&lpszBitmap))
397 {
398 hBitmap = O32_LoadBitmap(hInstanceUser32,lpszBitmap);
399 if (!hBitmap) hBitmap = O32_LoadBitmap(hinst,lpszBitmap);
400 } else hBitmap = 0;
401 } else hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
402 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
403#endif
404 return(hBitmap);
405}
406//******************************************************************************
407//TODO: No support for RT_NEWBITMAP
408//******************************************************************************
409HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
410{
411 HBITMAP hBitmap = 0;
412
413 if(HIWORD(lpszBitmap) != 0)
414 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
415
416 hBitmap = LoadBitmapA((hinst == 0) ? hInstanceUser32:hinst, (LPSTR)lpszBitmap, 0, 0, 0);
417
418 if(HIWORD(lpszBitmap) != 0)
419 FreeAsciiString((LPSTR)lpszBitmap);
420
421 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
422
423 return(hBitmap);
424}
425//******************************************************************************
426//TODO: Far from complete, but works for loading resources from exe
427//fuLoad flag ignored
428//******************************************************************************
429HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
430 int cxDesired, int cyDesired, UINT fuLoad)
431{
432 HANDLE hRet = 0;
433
434 if(HIWORD(lpszName)) {
435 dprintf(("LoadImageA NOT COMPLETE %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
436 }
437 else dprintf(("LoadImageA NOT COMPLETE %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
438
439 if (fuLoad & LR_DEFAULTSIZE) {
440 if (uType == IMAGE_ICON) {
441 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
442 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
443 }
444 else if (uType == IMAGE_CURSOR) {
445 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
446 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
447 }
448 }
449 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
450
451 switch(uType) {
452 case IMAGE_BITMAP:
453 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
454 break;
455 case IMAGE_CURSOR:
456 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
457 break;
458 case IMAGE_ICON:
459 hRet = (HANDLE)LoadIconA(hinst, lpszName);
460 break;
461 default:
462 dprintf(("LoadImageA: unsupported type %d!!", uType));
463 return 0;
464 }
465 dprintf(("LoadImageA returned %d\n", (int)hRet));
466
467 return(hRet);
468}
469//******************************************************************************
470//******************************************************************************
471HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
472 int cxDesired, int cyDesired, UINT fuLoad)
473{
474 HANDLE hRet = 0;
475
476 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
477
478 if (fuLoad & LR_DEFAULTSIZE) {
479 if (uType == IMAGE_ICON) {
480 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
481 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
482 }
483 else if (uType == IMAGE_CURSOR) {
484 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
485 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
486 }
487 }
488 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
489
490 switch(uType) {
491 case IMAGE_BITMAP:
492 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
493 break;
494 case IMAGE_CURSOR:
495 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
496 break;
497 case IMAGE_ICON:
498 hRet = (HANDLE)LoadIconW(hinst, lpszName);
499 break;
500 default:
501 dprintf(("LoadImageW: unsupported type %d!!", uType));
502 return 0;
503 }
504 dprintf(("LoadImageW returned %d\n", (int)hRet));
505
506 return(hRet);
507}
508/******************************************************************************
509 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
510 *
511 * PARAMS
512 * hnd [I] Handle to image to copy
513 * type [I] Type of image to copy
514 * desiredx [I] Desired width of new image
515 * desiredy [I] Desired height of new image
516 * flags [I] Copy flags
517 *
518 * RETURNS
519 * Success: Handle to newly created image
520 * Failure: NULL
521 *
522 * FIXME: implementation still lacks nearly all features, see LR_*
523 * defines in windows.h
524 *
525 */
526HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
527 INT desiredy, UINT flags )
528{
529 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
530 switch (type)
531 {
532// case IMAGE_BITMAP:
533// return BITMAP_CopyBitmap(hnd);
534 case IMAGE_ICON:
535 return CopyIcon(hnd);
536 case IMAGE_CURSOR:
537 return CopyCursor(hnd);
538// return CopyCursorIcon(hnd,type, desiredx, desiredy, flags);
539 default:
540 dprintf(("CopyImage: Unsupported type"));
541 }
542 return 0;
543}
544//******************************************************************************
545//******************************************************************************
Note: See TracBrowser for help on using the repository browser.