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

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

InsertMenu & msg translation fixes

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