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

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

* empty log message *

File size: 18.2 KB
Line 
1/* $Id: loadres.cpp,v 1.22 2000-01-18 20:10:37 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:
240 case OBM_DNARROW:
241 case OBM_RGARROW:
242 case OBM_LFARROW:
243 case OBM_RESTORE:
244 case OBM_RESTORED:
245 case OBM_UPARROWD:
246 case OBM_DNARROWD:
247 case OBM_RGARROWD:
248 case OBM_LFARROWD:
249 case OBM_OLD_UPARROW:
250 case OBM_OLD_DNARROW:
251 case OBM_OLD_RGARROW:
252 case OBM_OLD_LFARROW:
253 case OBM_CHECK:
254 case OBM_RADIOCHECK:
255 case OBM_CHECKBOXES:
256 case OBM_BTNCORNERS:
257 case OBM_COMBO:
258 case OBM_REDUCE:
259 case OBM_REDUCED:
260 case OBM_ZOOM:
261 case OBM_ZOOMD:
262 case OBM_SIZE:
263 case OBM_CLOSE:
264 case OBM_MNARROW:
265 case OBM_UPARROWI:
266 case OBM_DNARROWI:
267 case OBM_RGARROWI:
268 case OBM_LFARROWI:
269 case OBM_CLOSED:
270 case OBM_OLD_CLOSE:
271 case OBM_BTSIZE:
272 case OBM_OLD_REDUCE:
273 case OBM_OLD_ZOOM:
274 case OBM_OLD_RESTORE:
275 return TRUE;
276
277 default:
278 return FALSE;
279 }
280}
281//******************************************************************************
282//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)!
283//******************************************************************************
284HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
285 UINT fuLoad)
286{
287 HBITMAP hbitmap = 0;
288 HDC hdc;
289 HRSRC hRsrc;
290 HGLOBAL handle, hMapping = 0;
291 char *ptr = NULL;
292 BITMAPINFO *info, *fix_info=NULL;
293 HGLOBAL hFix;
294 int size;
295
296 if (!(fuLoad & LR_LOADFROMFILE)) {
297 if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
298 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
299
300 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
301 }
302 else
303 {
304 hMapping = VIRTUAL_MapFileA( lpszName, (LPVOID *)&ptr, TRUE);
305 if (hMapping == INVALID_HANDLE_VALUE) return 0;
306 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
307 }
308
309 //TODO: This has to be removed once pe2lx stores win32 resources!!!
310 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
311 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
312 {//assume it contains a file header first
313 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
314 }
315
316 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
317 size = sizeof(BITMAPINFOHEADER) +
318 (sizeof (RGBTRIPLE) << ((BITMAPCOREHEADER *)info)->bcBitCount);
319 } else
320 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
321
322 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
323 if (fix_info) {
324 BYTE pix;
325
326 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
327 ULONG colors;
328 ULONG *p, *q;
329
330 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
331 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
332 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
333 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
334 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
335 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
336
337 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
338 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
339 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
340 *q = *p & 0x00FFFFFFUL;
341 q++;
342 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
343 }
344 } else
345 memcpy(fix_info, info, size);
346
347 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
348 pix = *((LPBYTE)info + size);
349 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
350 if ((hdc = GetDC(0)) != 0) {
351 char *bits = (char *)info + size;
352 if (fuLoad & LR_CREATEDIBSECTION) {
353 DIBSECTION dib;
354 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
355 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
356 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
357 DIB_RGB_COLORS);
358 }
359 else {
360 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
361 bits, fix_info, DIB_RGB_COLORS );
362 }
363 ReleaseDC( 0, hdc );
364 }
365 GlobalUnlock(hFix);
366 GlobalFree(hFix);
367 }
368 if (fuLoad & LR_LOADFROMFILE) CloseHandle( hMapping );
369 return hbitmap;
370}
371//******************************************************************************
372//TODO: No support for RT_NEWBITMAP
373//******************************************************************************
374//******************************************************************************
375//TODO: No support for RT_NEWBITMAP
376//******************************************************************************
377HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
378{
379 HBITMAP hBitmap = 0;
380
381 if (!hinst)
382 {
383 if(IsSystemBitmap((ULONG)lpszBitmap))
384 {
385 hBitmap = LoadBitmapA(hInstanceUser32, lpszBitmap, 0, 0, 0);
386 }
387 }
388 if(!hBitmap)
389 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
390
391 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
392
393 return(hBitmap);
394}
395//******************************************************************************
396//TODO: No support for RT_NEWBITMAP
397//******************************************************************************
398HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
399{
400 HBITMAP hBitmap = 0;
401
402 if(HIWORD(lpszBitmap) != 0)
403 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
404
405 hBitmap = LoadBitmapA((hinst == 0) ? hInstanceUser32:hinst, (LPSTR)lpszBitmap, 0, 0, 0);
406
407 if(HIWORD(lpszBitmap) != 0)
408 FreeAsciiString((LPSTR)lpszBitmap);
409
410 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
411
412 return(hBitmap);
413}
414//******************************************************************************
415//TODO: Far from complete, but works for loading resources from exe
416//fuLoad flag ignored
417//******************************************************************************
418HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
419 int cxDesired, int cyDesired, UINT fuLoad)
420{
421 HANDLE hRet = 0;
422
423 if(HIWORD(lpszName)) {
424 dprintf(("LoadImageA NOT COMPLETE %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
425 }
426 else dprintf(("LoadImageA NOT COMPLETE %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
427
428 if (fuLoad & LR_DEFAULTSIZE) {
429 if (uType == IMAGE_ICON) {
430 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
431 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
432 }
433 else if (uType == IMAGE_CURSOR) {
434 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
435 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
436 }
437 }
438 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
439
440 switch(uType) {
441 case IMAGE_BITMAP:
442 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
443 break;
444 case IMAGE_CURSOR:
445 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
446 break;
447 case IMAGE_ICON:
448 hRet = (HANDLE)LoadIconA(hinst, lpszName);
449 break;
450 default:
451 dprintf(("LoadImageA: unsupported type %d!!", uType));
452 return 0;
453 }
454 dprintf(("LoadImageA returned %d\n", (int)hRet));
455
456 return(hRet);
457}
458//******************************************************************************
459//******************************************************************************
460HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
461 int cxDesired, int cyDesired, UINT fuLoad)
462{
463 HANDLE hRet = 0;
464
465 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
466
467 if (fuLoad & LR_DEFAULTSIZE) {
468 if (uType == IMAGE_ICON) {
469 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
470 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
471 }
472 else if (uType == IMAGE_CURSOR) {
473 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
474 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
475 }
476 }
477 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
478
479 switch(uType) {
480 case IMAGE_BITMAP:
481 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
482 break;
483 case IMAGE_CURSOR:
484 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
485 break;
486 case IMAGE_ICON:
487 hRet = (HANDLE)LoadIconW(hinst, lpszName);
488 break;
489 default:
490 dprintf(("LoadImageW: unsupported type %d!!", uType));
491 return 0;
492 }
493 dprintf(("LoadImageW returned %d\n", (int)hRet));
494
495 return(hRet);
496}
497/******************************************************************************
498 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
499 *
500 * PARAMS
501 * hnd [I] Handle to image to copy
502 * type [I] Type of image to copy
503 * desiredx [I] Desired width of new image
504 * desiredy [I] Desired height of new image
505 * flags [I] Copy flags
506 *
507 * RETURNS
508 * Success: Handle to newly created image
509 * Failure: NULL
510 *
511 * FIXME: implementation still lacks nearly all features, see LR_*
512 * defines in windows.h
513 *
514 */
515HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
516 INT desiredy, UINT flags )
517{
518 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
519 switch (type)
520 {
521// case IMAGE_BITMAP:
522// return BITMAP_CopyBitmap(hnd);
523 case IMAGE_ICON:
524 return CopyIcon(hnd);
525 case IMAGE_CURSOR:
526 return CopyCursor(hnd);
527// return CopyCursorIcon(hnd,type, desiredx, desiredy, flags);
528 default:
529 dprintf(("CopyImage: Unsupported type"));
530 }
531 return 0;
532}
533//******************************************************************************
534//******************************************************************************
Note: See TracBrowser for help on using the repository browser.