source: trunk/src/user32/loadres.cpp

Last change on this file was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

File size: 19.1 KB
Line 
1/* $Id: loadres.cpp,v 1.41 2003-02-13 13:10:49 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 //(NOTE: if hBmpCloseButton present until WGSS has been updated)
171 if(fOS2Look && (hinst == hInstanceUser32 || !hinst) && hBmpCloseButton) {
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 if (!(fuLoad & LR_LOADFROMFILE))
192 {
193 handle = 0;
194 if(!hinst)
195 {
196 hRsrc = FindResourceW( hInstanceUser32, lpszName, RT_BITMAPW );
197 if(hRsrc) {
198 handle = LoadResource( hInstanceUser32, hRsrc );
199 }
200 }
201 if(handle == 0)
202 {
203 if (!(hRsrc = FindResourceW( hinst, lpszName, RT_BITMAPW ))) return 0;
204 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
205 }
206
207 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
208 }
209 else
210 {
211 hMapping = VIRTUAL_MapFileW( lpszName, (LPVOID *)&ptr, TRUE);
212 if (hMapping == INVALID_HANDLE_VALUE) {
213 //TODO: last err set to ERROR_OPEN_FAILED if file not found; correct??
214 dprintf(("LoadBitmapW: failed to load file %x (lasterr=%x)", lpszName, GetLastError()));
215 return 0;
216 }
217 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
218 }
219
220 //TODO: This has to be removed once pe2lx stores win32 resources!!!
221 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
222 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
223 {//assume it contains a file header first
224 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
225 }
226
227 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
228 {//determine size of converted header
229 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
230
231 int colors = 0;
232 if (core->bcBitCount <= 8) {
233 colors = (1 << core->bcBitCount);
234 }
235 size = sizeof(BITMAPINFOHEADER) + colors * sizeof(RGBQUAD);
236 }
237 else size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
238
239 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
240 if (fix_info)
241 {
242 BYTE pix;
243
244 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
245 {//convert old bitmap header to new format
246 ULONG colors;
247 ULONG *p, *q;
248
249 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
250 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
251 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
252 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
253 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
254 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
255
256 if(fix_info->bmiHeader.biBitCount <= 8)
257 {
258 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
259 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
260 //convert RGBTRIPLE to RGBQUAD
261 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
262 *q = *p & 0x00FFFFFFUL;
263 q++;
264 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
265 }
266 }
267 }
268 else {
269 memcpy(fix_info, info, size);
270 }
271
272 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
273
274 pix = *((LPBYTE)info + size);
275 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
276 if ((hdc = CreateCompatibleDC(0)) != 0)
277 {
278 char *bits = (char *)info + size;
279 if (fuLoad & LR_CREATEDIBSECTION) {
280 DIBSECTION dib;
281 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
282 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
283 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
284 DIB_RGB_COLORS);
285 }
286 else
287 {
288#if 0
289 if(fix_info->bmiHeader.biBitCount == 1) {
290 hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth,
291 fix_info->bmiHeader.biHeight,
292 fix_info->bmiHeader.biPlanes,
293 fix_info->bmiHeader.biBitCount,
294 (PVOID)bits);
295 }
296 else {
297#endif
298 hbitmap = CreateDIBitmap(hdc, &fix_info->bmiHeader, CBM_INIT,
299 bits, fix_info, DIB_RGB_COLORS );
300// }
301 if(hbitmap == 0) {
302 dprintf(("LoadBitmapW: CreateDIBitmap failed!!"));
303 }
304 }
305 DeleteDC(hdc);
306 }
307 GlobalUnlock(hFix);
308 GlobalFree(hFix);
309 }
310 if(fuLoad & LR_LOADFROMFILE) {
311 UnmapViewOfFile(ptr);
312 CloseHandle(hMapping);
313 }
314 return hbitmap;
315}
316//******************************************************************************
317//TODO: scale bitmap
318//******************************************************************************
319HANDLE CopyBitmap(HANDLE hBitmap, DWORD desiredx, DWORD desiredy)
320{
321 HBITMAP res = 0;
322 BITMAP bm;
323
324 if(GetObjectA(hBitmap, sizeof(BITMAP), &bm) == FALSE) {
325 dprintf(("CopyBitmap: GetObject failed!!"));
326 return 0;
327 }
328
329#ifdef __WIN32OS2__
330 BITMAPINFO* pInfo;
331 HBITMAP oldbmp;
332 HDC hdc;
333 int colortablesize, bmpsize, headersize;
334 char *pBitmapData;
335
336 colortablesize = 0;
337
338 if(bm.bmBitsPixel <= 8) {
339 colortablesize = sizeof(RGBQUAD)*(1<<bm.bmBitsPixel);
340 }
341 bmpsize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
342 headersize = sizeof(BITMAPINFO)+colortablesize+3*sizeof(DWORD); //+ extra space for > 8bpp images
343
344 pInfo = (BITMAPINFO *)malloc(headersize+bmpsize);
345 if(pInfo == NULL) {
346 DebugInt3();
347 return 0;
348 }
349 pBitmapData = (char*)((char *)pInfo + headersize);
350 memset(pInfo, 0, headersize+bmpsize);
351
352 hdc = CreateCompatibleDC(0);
353
354 pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
355 pInfo->bmiHeader.biPlanes = bm.bmPlanes;
356 pInfo->bmiHeader.biBitCount = bm.bmBitsPixel;
357 pInfo->bmiHeader.biWidth = bm.bmWidth;
358 pInfo->bmiHeader.biHeight = bm.bmHeight;
359
360 GetDIBits(hdc, hBitmap, 0, bm.bmHeight, pBitmapData, pInfo, DIB_RGB_COLORS);
361
362 res = CreateDIBitmap(hdc, &pInfo->bmiHeader, CBM_INIT, pBitmapData,
363 pInfo, DIB_RGB_COLORS );
364
365 DeleteDC(hdc);
366 free(pInfo);
367#else
368 bm.bmBits = NULL;
369 res = CreateBitmapIndirect(&bm);
370
371 if(res)
372 {
373 char *buf = (char *)HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
374 bm.bmHeight );
375 GetBitmapBits (hBitmap, bm.bmWidthBytes * bm.bmHeight, buf);
376 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
377 HeapFree( GetProcessHeap(), 0, buf );
378 }
379#endif
380 return res;
381}
382//******************************************************************************
383//TODO: No support for RT_NEWBITMAP
384//******************************************************************************
385HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
386{
387 HBITMAP hBitmap = 0;
388
389 hBitmap = LoadImageA(hinst, lpszBitmap, IMAGE_BITMAP, 0, 0, 0);
390
391 if(HIWORD(lpszBitmap)) {
392 dprintf(("LoadBitmapA %x %s returned %08xh\n", hinst, lpszBitmap, hBitmap));
393 }
394 else dprintf(("LoadBitmapA %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
395
396 return(hBitmap);
397}
398//******************************************************************************
399//TODO: No support for RT_NEWBITMAP
400//******************************************************************************
401HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
402{
403 HBITMAP hBitmap = 0;
404
405 hBitmap = LoadBitmapW((hinst == 0) ? hInstanceUser32:hinst, lpszBitmap, 0, 0, 0);
406
407 if(HIWORD(lpszBitmap)) {
408 dprintf(("LoadBitmapW %x %ls returned %08xh\n", hinst, lpszBitmap, hBitmap));
409 }
410 else dprintf(("LoadBitmapW %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
411
412 return(hBitmap);
413}
414//******************************************************************************
415//******************************************************************************
416static PFNLOADIMAGEW pfnCustomLoadImageW = NULL;
417//******************************************************************************
418//Called by custom Odin builds to hook LoadImageW
419//******************************************************************************
420BOOL WIN32API SetCustomLoadImage(PFNLOADIMAGEW pfnLoadImageW)
421{
422 pfnCustomLoadImageW = pfnLoadImageW;
423 return TRUE;
424}
425//******************************************************************************
426//******************************************************************************
427HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
428 int cxDesired, int cyDesired, UINT fuLoad)
429{
430 HANDLE res = 0;
431 LPCWSTR u_name;
432
433 if(HIWORD(lpszName)) {
434 dprintf(("LoadImageA %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
435 }
436 else dprintf(("LoadImageA %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
437
438 if (HIWORD(lpszName)) {
439 u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, lpszName);
440 }
441 else u_name=(LPWSTR)lpszName;
442
443 res = LoadImageW(hinst, u_name, uType, cxDesired, cyDesired, fuLoad);
444
445 if (HIWORD(lpszName))
446 HeapFree(GetProcessHeap(), 0, (LPVOID)u_name);
447
448 return res;
449}
450//******************************************************************************
451//******************************************************************************
452HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
453 int cxDesired, int cyDesired, UINT fuLoad)
454{
455 HANDLE hRet = 0;
456
457 if(pfnCustomLoadImageW) {
458 pfnCustomLoadImageW(&hinst, (LPWSTR *)&lpszName, &uType);
459 }
460
461 if(HIWORD(lpszName)) {
462 dprintf(("LoadImageW %x %ls %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
463 }
464 else dprintf(("LoadImageW %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
465
466 if (fuLoad & LR_DEFAULTSIZE) {
467 if (uType == IMAGE_ICON) {
468 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
469 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
470 }
471 else if (uType == IMAGE_CURSOR) {
472 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
473 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
474 }
475 }
476 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
477
478 switch (uType)
479 {
480 case IMAGE_BITMAP:
481 {
482 hRet = (HANDLE)LoadBitmapW(hinst, lpszName, cxDesired, cyDesired, fuLoad);
483 break;
484 }
485 case IMAGE_ICON:
486 {
487#ifdef __WIN32OS2__
488 ULONG palEnts = (1 << ScreenBitsPerPel);
489#else
490 HDC hdc = GetDC(0);
491 UINT palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
492 if (palEnts == 0)
493 palEnts = 256;
494 ReleaseDC(0, hdc);
495#endif
496
497 hRet = CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, palEnts, FALSE, fuLoad);
498 break;
499 }
500
501 case IMAGE_CURSOR:
502 return CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, 1, TRUE, fuLoad);
503
504 default:
505 dprintf(("LoadImageW: unsupported type %d!!", uType));
506 return 0;
507 }
508 dprintf(("LoadImageW returned %x\n", (int)hRet));
509
510 return(hRet);
511}
512/******************************************************************************
513 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
514 *
515 * PARAMS
516 * hnd [I] Handle to image to copy
517 * type [I] Type of image to copy
518 * desiredx [I] Desired width of new image
519 * desiredy [I] Desired height of new image
520 * flags [I] Copy flags
521 *
522 * RETURNS
523 * Success: Handle to newly created image
524 * Failure: NULL
525 *
526 * FIXME: implementation still lacks nearly all features, see LR_*
527 * defines in windows.h
528 *
529 */
530HICON WINAPI CopyImage(HANDLE hnd, UINT type, INT desiredx,
531 INT desiredy, UINT flags )
532{
533 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
534 switch (type)
535 {
536 case IMAGE_BITMAP:
537 return CopyBitmap(hnd, desiredx, desiredy);
538 case IMAGE_ICON:
539 return (HANDLE)CURSORICON_ExtCopy(hnd, type, desiredx, desiredy, flags);
540 case IMAGE_CURSOR:
541 /* Should call CURSORICON_ExtCopy but more testing
542 * needs to be done before we change this
543 */
544 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
545 default:
546 dprintf(("CopyImage: Unsupported type"));
547 }
548 return 0;
549}
550//******************************************************************************
551//******************************************************************************
Note: See TracBrowser for help on using the repository browser.