source: trunk/src/user32/new/loadres.cpp@ 2309

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

ported all USER32 bitmaps, several bug fixes

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