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

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

replaced os2.h includes by os2wrap.h

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