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

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

EBs fixes

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