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

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

added LoadImageW hook

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