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

Last change on this file since 2857 was 2857, checked in by cbratschi, 26 years ago

WS_EX_CONTEXTHELP, activated some WINE code

File size: 18.6 KB
Line 
1/* $Id: loadres.cpp,v 1.24 2000-02-22 17:07:40 cbratschi 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 case OBM_CONTEXTHELP:
279 case OBM_CONTEXTHELPD:
280 return TRUE;
281
282 default:
283 return FALSE;
284 }
285}
286//******************************************************************************
287//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)!
288//******************************************************************************
289HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
290 UINT fuLoad)
291{
292 HBITMAP hbitmap = 0;
293 HDC hdc;
294 HRSRC hRsrc;
295 HGLOBAL handle, hMapping = 0;
296 char *ptr = NULL;
297 BITMAPINFO *info, *fix_info=NULL;
298 HGLOBAL hFix;
299 int size;
300
301 if (!(fuLoad & LR_LOADFROMFILE)) {
302 if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
303 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
304
305 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
306 }
307 else
308 {
309 hMapping = VIRTUAL_MapFileA( lpszName, (LPVOID *)&ptr, TRUE);
310 if (hMapping == INVALID_HANDLE_VALUE) return 0;
311 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
312 }
313
314 //TODO: This has to be removed once pe2lx stores win32 resources!!!
315 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
316 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
317 {//assume it contains a file header first
318 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
319 }
320
321 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
322 size = sizeof(BITMAPINFOHEADER) +
323 (sizeof (RGBTRIPLE) << ((BITMAPCOREHEADER *)info)->bcBitCount);
324 } else
325 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
326
327 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
328 if (fix_info) {
329 BYTE pix;
330
331 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
332 ULONG colors;
333 ULONG *p, *q;
334
335 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
336 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
337 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
338 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
339 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
340 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
341
342 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
343 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
344 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
345 *q = *p & 0x00FFFFFFUL;
346 q++;
347 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
348 }
349 } else
350 memcpy(fix_info, info, size);
351
352 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
353 pix = *((LPBYTE)info + size);
354 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
355 if ((hdc = GetDC(0)) != 0) {
356 char *bits = (char *)info + size;
357 if (fuLoad & LR_CREATEDIBSECTION) {
358 DIBSECTION dib;
359 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
360 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
361 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
362 DIB_RGB_COLORS);
363 }
364 else {
365// if(fix_info->bmiHeader.biBitCount == 1) {
366// hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth,
367// fix_info->bmiHeader.biHeight,
368// fix_info->bmiHeader.biPlanes,
369// fix_info->bmiHeader.biBitCount,
370// (PVOID)bits);
371// }
372// else {
373 hbitmap = CreateDIBitmap(hdc, &fix_info->bmiHeader, CBM_INIT,
374 bits, fix_info, DIB_RGB_COLORS );
375// }
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 = LoadBitmapA(hInstanceUser32, lpszBitmap, 0, 0, 0);
397 }
398 }
399 if(!hBitmap)
400 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
401
402 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
403
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.