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

Last change on this file since 9437 was 9437, checked in by sandervl, 23 years ago

CopyBitmap fix + use PM bitmaps for standard user32 frame buttons in OS/2 GUI mode

File size: 19.1 KB
Line 
1/* $Id: loadres.cpp,v 1.40 2002-11-27 13:56:26 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 <heapstring.h>
23#include <oslibres.h>
24#include <win\virtual.h>
25#include "dib.h"
26#include "initterm.h"
27#include <winres.h>
28#include <custombuild.h>
29#include "pmwindow.h"
30
31#define DBG_LOCALLOG DBG_loadres
32#include "dbglocal.h"
33
34//******************************************************************************
35//******************************************************************************
36INT WIN32API LoadStringA(HINSTANCE instance, UINT resource_id,
37 LPSTR buffer, INT buflen )
38{
39 INT retval;
40 LPWSTR buffer2 = NULL;
41
42 if (buffer && buflen)
43 buffer2 = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
44
45 retval = LoadStringW(instance,resource_id,buffer2,buflen);
46
47 if (buffer2)
48 {
49 if (retval) {
50 lstrcpynWtoA( buffer, buffer2, buflen );
51 retval = lstrlenA( buffer );
52 }
53 else
54 *buffer = 0; //NT4, SP6 clears first character
55 HeapFree( GetProcessHeap(), 0, buffer2 );
56 }
57 return retval;
58}
59//******************************************************************************
60//******************************************************************************
61int WIN32API LoadStringW(HINSTANCE hinst, UINT wID, LPWSTR lpBuffer, int cchBuffer)
62{
63 WCHAR *p;
64 int string_num;
65 int i = 0;
66 HRSRC hRes;
67
68 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
69 * 20 - 31. */
70 hRes = FindResourceW(hinst, (LPWSTR)(((wID>>4)&0xffff)+1), RT_STRINGW);
71 if(hRes == NULL) {
72 dprintf(("LoadStringW NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
73 *lpBuffer = 0; //NT4, SP6 clears first character
74 return 0;
75 }
76
77 p = (LPWSTR)LockResource(LoadResource(hinst, hRes));
78 if(p) {
79 string_num = wID & 0x000f;
80 for (i = 0; i < string_num; i++)
81 p += *p + 1;
82
83 if (lpBuffer == NULL) return *p;
84 i = min(cchBuffer - 1, *p);
85 if (i > 0) {
86 memcpy(lpBuffer, p + 1, i * sizeof (WCHAR));
87 lpBuffer[i] = (WCHAR) 0;
88 }
89 else {
90 if (cchBuffer > 1) {
91 lpBuffer[0] = (WCHAR) 0; //NT4, SP6 clears first character
92 return 0;
93 }
94 }
95 }
96
97 if(i) {
98 dprintf(("LoadStringW from %X, id %d %ls buffersize %d", hinst, wID, lpBuffer, cchBuffer));
99 }
100 else dprintf(("LoadStringW from %X, id %d buffersize %d", hinst, wID, cchBuffer));
101 return(i);
102}
103//******************************************************************************
104//******************************************************************************
105HICON WIN32API LoadIconA(HINSTANCE hinst, LPCSTR lpszIcon)
106{
107 if(HIWORD(lpszIcon)) {
108 dprintf(("LoadIconA %x %s", hinst, lpszIcon));
109 }
110 else dprintf(("LoadIconA %x %x", hinst, lpszIcon));
111 return LoadImageA(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
112}
113//******************************************************************************
114//******************************************************************************
115HICON WIN32API LoadIconW(HINSTANCE hinst, LPCWSTR lpszIcon)
116{
117 if(HIWORD(lpszIcon)) {
118 dprintf(("LoadIconW %x %ls", hinst, lpszIcon));
119 }
120 else dprintf(("LoadIconW %x %x", hinst, lpszIcon));
121 return LoadImageW(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
122}
123//******************************************************************************
124//******************************************************************************
125HCURSOR WIN32API LoadCursorA(HINSTANCE hinst, LPCSTR lpszCursor)
126{
127 return LoadImageA(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
128 LR_SHARED | LR_DEFAULTSIZE );
129}
130//******************************************************************************
131//******************************************************************************
132HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
133{
134 return LoadImageW(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
135 LR_SHARED | LR_DEFAULTSIZE );
136}
137/***********************************************************************
138* LoadCursorFromFileW (USER32.361)
139*/
140HCURSOR WIN32API LoadCursorFromFileW (LPCWSTR name)
141{
142 return LoadImageW(0, name, IMAGE_CURSOR, 0, 0,
143 LR_LOADFROMFILE | LR_DEFAULTSIZE );
144}
145/***********************************************************************
146* LoadCursorFromFileA (USER32.360)
147*/
148HCURSOR WIN32API LoadCursorFromFileA (LPCSTR name)
149{
150 return LoadImageA(0, name, IMAGE_CURSOR, 0, 0,
151 LR_LOADFROMFILE | LR_DEFAULTSIZE );
152}
153//******************************************************************************
154//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)! (still??)
155//******************************************************************************
156HANDLE LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszName, int cxDesired, int cyDesired,
157 UINT fuLoad)
158{
159 HBITMAP hbitmap = 0;
160 HDC hdc;
161 HRSRC hRsrc;
162 HGLOBAL handle, hMapping = 0;
163 char *ptr = NULL;
164 BITMAPINFO *info, *fix_info=NULL;
165 HGLOBAL hFix;
166 int size;
167
168 //if in OS/2 mode, then we must replace the standard button bitmaps
169 //(min, max, restore, close)
170#ifdef NEW_WGSS
171 if(fOS2Look && (hinst == hInstanceUser32 || !hinst)) {
172 switch((ULONG)lpszName) {
173 case OBM_CLOSE:
174 return CopyImage(hBmpCloseButton, IMAGE_BITMAP, 0, 0, 0);
175 case OBM_CLOSED:
176 return CopyImage(hBmpCloseButtonDown, IMAGE_BITMAP, 0, 0, 0);
177 case OBM_RESTORE:
178 return CopyImage(hBmpRestoreButton, IMAGE_BITMAP, 0, 0, 0);
179 case OBM_RESTORED:
180 return CopyImage(hBmpRestoreButtonDown, IMAGE_BITMAP, 0, 0, 0);
181 case OBM_REDUCE:
182 return CopyImage(hBmpMinButton, IMAGE_BITMAP, 0, 0, 0);
183 case OBM_REDUCED:
184 return CopyImage(hBmpMinButtonDown, IMAGE_BITMAP, 0, 0, 0);
185 case OBM_ZOOM:
186 return CopyImage(hBmpMaxButton, IMAGE_BITMAP, 0, 0, 0);
187 case OBM_ZOOMD:
188 return CopyImage(hBmpMaxButtonDown, IMAGE_BITMAP, 0, 0, 0);
189 }
190 }
191#endif
192 if (!(fuLoad & LR_LOADFROMFILE))
193 {
194 handle = 0;
195 if(!hinst)
196 {
197 hRsrc = FindResourceW( hInstanceUser32, lpszName, RT_BITMAPW );
198 if(hRsrc) {
199 handle = LoadResource( hInstanceUser32, hRsrc );
200 }
201 }
202 if(handle == 0)
203 {
204 if (!(hRsrc = FindResourceW( hinst, lpszName, RT_BITMAPW ))) return 0;
205 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
206 }
207
208 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
209 }
210 else
211 {
212 hMapping = VIRTUAL_MapFileW( lpszName, (LPVOID *)&ptr, TRUE);
213 if (hMapping == INVALID_HANDLE_VALUE) {
214 //TODO: last err set to ERROR_OPEN_FAILED if file not found; correct??
215 dprintf(("LoadBitmapW: failed to load file %x (lasterr=%x)", lpszName, GetLastError()));
216 return 0;
217 }
218 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
219 }
220
221 //TODO: This has to be removed once pe2lx stores win32 resources!!!
222 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
223 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
224 {//assume it contains a file header first
225 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
226 }
227
228 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
229 {//determine size of converted header
230 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
231
232 int colors = 0;
233 if (core->bcBitCount <= 8) {
234 colors = (1 << core->bcBitCount);
235 }
236 size = sizeof(BITMAPINFOHEADER) + colors * sizeof(RGBQUAD);
237 }
238 else size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
239
240 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
241 if (fix_info)
242 {
243 BYTE pix;
244
245 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
246 {//convert old bitmap header to new format
247 ULONG colors;
248 ULONG *p, *q;
249
250 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
251 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
252 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
253 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
254 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
255 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
256
257 if(fix_info->bmiHeader.biBitCount <= 8)
258 {
259 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
260 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
261 //convert RGBTRIPLE to RGBQUAD
262 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
263 *q = *p & 0x00FFFFFFUL;
264 q++;
265 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
266 }
267 }
268 }
269 else {
270 memcpy(fix_info, info, size);
271 }
272
273 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
274
275 pix = *((LPBYTE)info + size);
276 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
277 if ((hdc = CreateCompatibleDC(0)) != 0)
278 {
279 char *bits = (char *)info + size;
280 if (fuLoad & LR_CREATEDIBSECTION) {
281 DIBSECTION dib;
282 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
283 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
284 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
285 DIB_RGB_COLORS);
286 }
287 else
288 {
289#if 0
290 if(fix_info->bmiHeader.biBitCount == 1) {
291 hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth,
292 fix_info->bmiHeader.biHeight,
293 fix_info->bmiHeader.biPlanes,
294 fix_info->bmiHeader.biBitCount,
295 (PVOID)bits);
296 }
297 else {
298#endif
299 hbitmap = CreateDIBitmap(hdc, &fix_info->bmiHeader, CBM_INIT,
300 bits, fix_info, DIB_RGB_COLORS );
301// }
302 if(hbitmap == 0) {
303 dprintf(("LoadBitmapW: CreateDIBitmap failed!!"));
304 }
305 }
306 DeleteDC(hdc);
307 }
308 GlobalUnlock(hFix);
309 GlobalFree(hFix);
310 }
311 if(fuLoad & LR_LOADFROMFILE) {
312 UnmapViewOfFile(ptr);
313 CloseHandle(hMapping);
314 }
315 return hbitmap;
316}
317//******************************************************************************
318//TODO: scale bitmap
319//******************************************************************************
320HANDLE CopyBitmap(HANDLE hBitmap, DWORD desiredx, DWORD desiredy)
321{
322 HBITMAP res = 0;
323 BITMAP bm;
324
325 if(GetObjectA(hBitmap, sizeof(BITMAP), &bm) == FALSE) {
326 dprintf(("CopyBitmap: GetObject failed!!"));
327 return 0;
328 }
329
330#ifdef __WIN32OS2__
331 BITMAPINFO* pInfo;
332 HBITMAP oldbmp;
333 HDC hdc;
334 int colortablesize, bmpsize, headersize;
335 char *pBitmapData;
336
337 colortablesize = 0;
338
339 if(bm.bmBitsPixel <= 8) {
340 colortablesize = sizeof(RGBQUAD)*(1<<bm.bmBitsPixel);
341 }
342 bmpsize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
343 headersize = sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD); //+ extra space for > 8bpp images
344
345 pInfo = (BITMAPINFO *)malloc(headersize+bmpsize);
346 if(pInfo == NULL) {
347 DebugInt3();
348 return 0;
349 }
350 pBitmapData = (char*)((char *)pInfo + headersize);
351 memset(pInfo, 0, headersize+bmpsize);
352
353 hdc = CreateCompatibleDC(0);
354
355 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
356 pInfo->bmiHeader.biPlanes = bm.bmPlanes;
357 pInfo->bmiHeader.biBitCount = bm.bmBitsPixel;
358 pInfo->bmiHeader.biWidth = bm.bmWidth;
359 pInfo->bmiHeader.biHeight = bm.bmHeight;
360
361 GetDIBits(hdc, hBitmap, 0, bm.bmHeight, pBitmapData, pInfo, DIB_RGB_COLORS);
362
363 res = CreateDIBitmap(hdc, &pInfo->bmiHeader, CBM_INIT, pBitmapData,
364 pInfo, DIB_RGB_COLORS );
365
366 DeleteDC(hdc);
367 free(pInfo);
368#else
369 bm.bmBits = NULL;
370 res = CreateBitmapIndirect(&bm);
371
372 if(res)
373 {
374 char *buf = (char *)HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
375 bm.bmHeight );
376 GetBitmapBits (hBitmap, bm.bmWidthBytes * bm.bmHeight, buf);
377 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
378 HeapFree( GetProcessHeap(), 0, buf );
379 }
380#endif
381 return res;
382}
383//******************************************************************************
384//TODO: No support for RT_NEWBITMAP
385//******************************************************************************
386HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
387{
388 HBITMAP hBitmap = 0;
389
390 hBitmap = LoadImageA(hinst, lpszBitmap, IMAGE_BITMAP, 0, 0, 0);
391
392 if(HIWORD(lpszBitmap)) {
393 dprintf(("LoadBitmapA %x %s returned %08xh\n", hinst, lpszBitmap, hBitmap));
394 }
395 else dprintf(("LoadBitmapA %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
396
397 return(hBitmap);
398}
399//******************************************************************************
400//TODO: No support for RT_NEWBITMAP
401//******************************************************************************
402HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
403{
404 HBITMAP hBitmap = 0;
405
406 hBitmap = LoadBitmapW((hinst == 0) ? hInstanceUser32:hinst, lpszBitmap, 0, 0, 0);
407
408 if(HIWORD(lpszBitmap)) {
409 dprintf(("LoadBitmapW %x %ls returned %08xh\n", hinst, lpszBitmap, hBitmap));
410 }
411 else dprintf(("LoadBitmapW %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
412
413 return(hBitmap);
414}
415//******************************************************************************
416//******************************************************************************
417static PFNLOADIMAGEW pfnCustomLoadImageW = NULL;
418//******************************************************************************
419//Called by custom Odin builds to hook LoadImageW
420//******************************************************************************
421BOOL WIN32API SetCustomLoadImage(PFNLOADIMAGEW pfnLoadImageW)
422{
423 pfnCustomLoadImageW = pfnLoadImageW;
424 return TRUE;
425}
426//******************************************************************************
427//******************************************************************************
428HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
429 int cxDesired, int cyDesired, UINT fuLoad)
430{
431 HANDLE res = 0;
432 LPCWSTR u_name;
433
434 if(HIWORD(lpszName)) {
435 dprintf(("LoadImageA %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
436 }
437 else dprintf(("LoadImageA %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
438
439 if (HIWORD(lpszName)) {
440 u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, lpszName);
441 }
442 else u_name=(LPWSTR)lpszName;
443
444 res = LoadImageW(hinst, u_name, uType, cxDesired, cyDesired, fuLoad);
445
446 if (HIWORD(lpszName))
447 HeapFree(GetProcessHeap(), 0, (LPVOID)u_name);
448
449 return res;
450}
451//******************************************************************************
452//******************************************************************************
453HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
454 int cxDesired, int cyDesired, UINT fuLoad)
455{
456 HANDLE hRet = 0;
457
458 if(pfnCustomLoadImageW) {
459 pfnCustomLoadImageW(&hinst, (LPWSTR *)&lpszName, &uType);
460 }
461
462 if(HIWORD(lpszName)) {
463 dprintf(("LoadImageW %x %ls %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
464 }
465 else dprintf(("LoadImageW %x %x %d (%d,%d)\n", hinst, lpszName, uType, 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 {
481 case IMAGE_BITMAP:
482 {
483 hRet = (HANDLE)LoadBitmapW(hinst, lpszName, cxDesired, cyDesired, fuLoad);
484 break;
485 }
486 case IMAGE_ICON:
487 {
488#ifdef __WIN32OS2__
489 ULONG palEnts = (1 << ScreenBitsPerPel);
490#else
491 HDC hdc = GetDC(0);
492 UINT palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
493 if (palEnts == 0)
494 palEnts = 256;
495 ReleaseDC(0, hdc);
496#endif
497
498 hRet = CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, palEnts, FALSE, fuLoad);
499 break;
500 }
501
502 case IMAGE_CURSOR:
503 return CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, 1, TRUE, fuLoad);
504
505 default:
506 dprintf(("LoadImageW: unsupported type %d!!", uType));
507 return 0;
508 }
509 dprintf(("LoadImageW returned %x\n", (int)hRet));
510
511 return(hRet);
512}
513/******************************************************************************
514 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
515 *
516 * PARAMS
517 * hnd [I] Handle to image to copy
518 * type [I] Type of image to copy
519 * desiredx [I] Desired width of new image
520 * desiredy [I] Desired height of new image
521 * flags [I] Copy flags
522 *
523 * RETURNS
524 * Success: Handle to newly created image
525 * Failure: NULL
526 *
527 * FIXME: implementation still lacks nearly all features, see LR_*
528 * defines in windows.h
529 *
530 */
531HICON WINAPI CopyImage(HANDLE hnd, UINT type, INT desiredx,
532 INT desiredy, UINT flags )
533{
534 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
535 switch (type)
536 {
537 case IMAGE_BITMAP:
538 return CopyBitmap(hnd, desiredx, desiredy);
539 case IMAGE_ICON:
540 return (HANDLE)CURSORICON_ExtCopy(hnd, type, desiredx, desiredy, flags);
541 case IMAGE_CURSOR:
542 /* Should call CURSORICON_ExtCopy but more testing
543 * needs to be done before we change this
544 */
545 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
546 default:
547 dprintf(("CopyImage: Unsupported type"));
548 }
549 return 0;
550}
551//******************************************************************************
552//******************************************************************************
Note: See TracBrowser for help on using the repository browser.