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

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

MoveTo, system resource handling fixes

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