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

Last change on this file since 5385 was 5385, checked in by sandervl, 24 years ago

cursor handling updates

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