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

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

Added new logging feature

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