source: trunk/src/user32/new/loadres.cpp@ 2401

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

get/peekmessage fixes, timer fix, (user/new) replaced wm_hittest code; added wm_ncactivate, changed system menu

File size: 18.2 KB
Line 
1/* $Id: loadres.cpp,v 1.15 2000-01-10 23:29:12 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 <winres.h>
23#include <heapstring.h>
24#include <oslibres.h>
25#include <winconst.h>
26#include <win\virtual.h>
27#include "dib.h"
28#include "initterm.h"
29#include <win\cursoricon.h>
30
31//******************************************************************************
32//******************************************************************************
33INT WIN32API LoadStringA(HINSTANCE instance, UINT resource_id,
34 LPSTR buffer, INT buflen )
35{
36 INT retval;
37 LPWSTR buffer2 = NULL;
38
39 if (buffer && buflen)
40 buffer2 = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
41
42 retval = LoadStringW(instance,resource_id,buffer2,buflen);
43
44 if (buffer2)
45 {
46 if (retval) {
47 lstrcpynWtoA( buffer, buffer2, buflen );
48 retval = lstrlenA( buffer );
49 }
50 else
51 *buffer = 0;
52 HeapFree( GetProcessHeap(), 0, buffer2 );
53 }
54 return retval;
55}
56//******************************************************************************
57//******************************************************************************
58int WIN32API LoadStringW(HINSTANCE hinst, UINT wID, LPWSTR lpBuffer, int cchBuffer)
59{
60 Win32Resource *winres;
61 WCHAR *p;
62 int string_num;
63 int i = 0;
64
65 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
66 * 20 - 31. */
67 winres = (Win32Resource *)FindResourceW(hinst, (LPWSTR)(((wID>>4)&0xffff)+1), RT_STRINGW);
68 if(winres == NULL) {
69 dprintf(("LoadStringW NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
70 *lpBuffer = 0;
71 return 0;
72 }
73
74 p = (LPWSTR)winres->lockResource();
75 if(p) {
76 string_num = wID & 0x000f;
77 for (i = 0; i < string_num; i++)
78 p += *p + 1;
79
80 if (lpBuffer == NULL) return *p;
81 i = min(cchBuffer - 1, *p);
82 if (i > 0) {
83 memcpy(lpBuffer, p + 1, i * sizeof (WCHAR));
84 lpBuffer[i] = (WCHAR) 0;
85 }
86 else {
87 if (cchBuffer > 1) {
88 lpBuffer[0] = (WCHAR) 0;
89 return 0;
90 }
91 }
92 }
93 delete winres;
94
95#ifdef DEBUG_ENABLELOG_LEVEL2
96 if(i) {
97 char *astring = (char *)HEAP_strdupWtoA(GetProcessHeap(), 0, lpBuffer);
98 dprintf(("LoadStringW from %X, id %d %s\n", hinst, wID, astring));
99 HEAP_free(astring);
100 }
101#else
102 dprintf(("LoadStringW from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
103#endif
104 return(i);
105}
106//******************************************************************************
107//******************************************************************************
108HICON WIN32API LoadIconA(HINSTANCE hinst, LPCSTR lpszIcon)
109{
110 Win32Resource *winres;
111 HICON hIcon;
112
113 if (!hinst)
114 {
115 winres = (Win32Resource*)FindResourceA(hInstanceUser32,lpszIcon,RT_ICONA);
116 if (!winres) winres = (Win32Resource*)FindResourceA(hInstanceUser32,lpszIcon,RT_GROUP_ICONA);
117 if (winres)
118 {
119 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
120 delete winres;
121 } else hIcon = OSLibWinQuerySysIcon((ULONG)lpszIcon,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON));
122 } else
123 { //not a system icon
124 winres = (Win32Resource *)FindResourceA(hinst, lpszIcon, RT_ICONA);
125 if(winres == 0) {
126 winres = (Win32Resource *)FindResourceA(hinst, lpszIcon, RT_GROUP_ICONA);
127 }
128 if(winres) {
129 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
130 delete winres;
131 } else hIcon = 0;
132 }
133 dprintf(("LoadIconA (%X) returned %x\n", hinst, hIcon));
134
135 return(hIcon);
136}
137//******************************************************************************
138//******************************************************************************
139HICON WIN32API LoadIconW(HINSTANCE hinst, LPCWSTR lpszIcon)
140{
141 Win32Resource *winres;
142 HICON hIcon;
143
144 if (!hinst)
145 {
146 winres = (Win32Resource*)FindResourceW(hInstanceUser32,lpszIcon,RT_ICONW);
147 if (!winres) winres = (Win32Resource*)FindResourceW(hInstanceUser32,lpszIcon,RT_GROUP_ICONW);
148 if (winres)
149 {
150 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
151 delete winres;
152 } else hIcon = OSLibWinQuerySysIcon((ULONG)lpszIcon,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON));
153 } else
154 {//not a system icon
155 winres = (Win32Resource *)FindResourceW(hinst, lpszIcon, RT_ICONW);
156 if(winres == 0) {
157 winres = (Win32Resource *)FindResourceW(hinst, lpszIcon, RT_GROUP_ICONW);
158 }
159 if(winres) {
160 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
161 delete winres;
162 } else hIcon = 0;
163 }
164 dprintf(("LoadIconW (%X) returned %x\n", hinst, hIcon));
165
166 return(hIcon);
167}
168//******************************************************************************
169//******************************************************************************
170HCURSOR WIN32API LoadCursorA(HINSTANCE hinst, LPCSTR lpszCursor)
171{
172 Win32Resource *winres;
173 HCURSOR hCursor;
174
175 if (!hinst)
176 {
177 winres = (Win32Resource*)FindResourceA(hInstanceUser32,lpszCursor,RT_CURSORA);
178 if (!winres) winres = (Win32Resource*)FindResourceA(hInstanceUser32,lpszCursor,RT_GROUP_CURSORA);
179 if (winres)
180 {
181 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
182 delete winres;
183 } else hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor,GetSystemMetrics(SM_CXCURSOR),GetSystemMetrics(SM_CYCURSOR));
184 } else
185 {//not a system pointer
186 winres = (Win32Resource *)FindResourceA(hinst, lpszCursor, RT_CURSORA);
187 if(winres == 0) {
188 winres = (Win32Resource *)FindResourceA(hinst, lpszCursor, RT_GROUP_CURSORA);
189 }
190 if(winres) {
191 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
192 delete winres;
193 } else hCursor = 0;
194 }
195 if(HIWORD(lpszCursor)) {
196 dprintf(("LoadCursorA %s from %x returned %x\n", lpszCursor, hinst, hCursor));
197 }
198 else dprintf(("LoadCursorA %x from %x returned %x\n", lpszCursor, hinst, hCursor));
199
200 return(hCursor);
201}
202//******************************************************************************
203//******************************************************************************
204HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
205{
206 Win32Resource *winres;
207 HCURSOR hCursor;
208
209 if (!hinst)
210 {
211 winres = (Win32Resource*)FindResourceW(hInstanceUser32,lpszCursor,RT_CURSORW);
212 if (!winres) winres = (Win32Resource*)FindResourceW(hInstanceUser32,lpszCursor,RT_GROUP_CURSORW);
213 if (winres)
214 {
215 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
216 delete winres;
217 } else hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor,GetSystemMetrics(SM_CXCURSOR),GetSystemMetrics(SM_CYCURSOR));
218 } else
219 {//not a system pointer
220 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_CURSORW);
221 if(winres == 0) {
222 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_GROUP_CURSORW);
223 }
224 if(winres) {
225 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
226 delete winres;
227 } else hCursor = 0;
228 }
229 dprintf(("LoadCursorW (%X) returned %x\n", hinst, hCursor));
230
231 return(hCursor);
232}
233//******************************************************************************
234//******************************************************************************
235BOOL IsSystemBitmap(ULONG id)
236{
237 switch(id)
238 {
239 case OBM_UPARROW:
240 case OBM_DNARROW:
241 case OBM_RGARROW:
242 case OBM_LFARROW:
243 case OBM_RESTORE:
244 case OBM_RESTORED:
245 case OBM_UPARROWD:
246 case OBM_DNARROWD:
247 case OBM_RGARROWD:
248 case OBM_LFARROWD:
249 case OBM_OLD_UPARROW:
250 case OBM_OLD_DNARROW:
251 case OBM_OLD_RGARROW:
252 case OBM_OLD_LFARROW:
253 case OBM_CHECK:
254 case OBM_CHECKBOXES:
255 case OBM_BTNCORNERS:
256 case OBM_COMBO:
257 case OBM_REDUCE:
258 case OBM_REDUCED:
259 case OBM_ZOOM:
260 case OBM_ZOOMD:
261 case OBM_SIZE:
262 case OBM_CLOSE:
263 case OBM_MNARROW:
264 case OBM_UPARROWI:
265 case OBM_DNARROWI:
266 case OBM_RGARROWI:
267 case OBM_LFARROWI:
268 case OBM_CLOSED:
269 case OBM_OLD_CLOSE:
270 case OBM_BTSIZE:
271 case OBM_OLD_REDUCE:
272 case OBM_OLD_ZOOM:
273 case OBM_OLD_RESTORE:
274 return TRUE;
275
276 default:
277 return FALSE;
278 }
279}
280//******************************************************************************
281//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)!
282//******************************************************************************
283HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
284 UINT fuLoad)
285{
286 HBITMAP hbitmap = 0;
287 HDC hdc;
288 HRSRC hRsrc;
289 HGLOBAL handle, hMapping = 0;
290 char *ptr = NULL;
291 BITMAPINFO *info, *fix_info=NULL;
292 HGLOBAL hFix;
293 int size;
294
295 if (!(fuLoad & LR_LOADFROMFILE)) {
296 if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
297 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
298
299 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
300 }
301 else
302 {
303 hMapping = VIRTUAL_MapFileA( lpszName, (LPVOID *)&ptr, TRUE);
304 if (hMapping == INVALID_HANDLE_VALUE) return 0;
305 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
306 }
307
308 //TODO: This has to be removed once pe2lx stores win32 resources!!!
309 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
310 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
311 {//assume it contains a file header first
312 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
313 }
314
315 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
316 size = sizeof(BITMAPINFOHEADER) +
317 (sizeof (RGBTRIPLE) << ((BITMAPCOREHEADER *)info)->bcBitCount);
318 } else
319 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
320
321 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
322 if (fix_info) {
323 BYTE pix;
324
325 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
326 ULONG colors;
327 ULONG *p, *q;
328
329 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
330 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
331 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
332 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
333 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
334 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
335
336 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
337 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
338 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
339 *q = *p & 0x00FFFFFFUL;
340 q++;
341 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
342 }
343 } else
344 memcpy(fix_info, info, size);
345
346 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
347 pix = *((LPBYTE)info + size);
348 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
349 if ((hdc = GetDC(0)) != 0) {
350 char *bits = (char *)info + size;
351 if (fuLoad & LR_CREATEDIBSECTION) {
352 DIBSECTION dib;
353 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
354 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
355 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
356 DIB_RGB_COLORS);
357 }
358 else {
359 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
360 bits, fix_info, DIB_RGB_COLORS );
361 }
362 ReleaseDC( 0, hdc );
363 }
364 GlobalUnlock(hFix);
365 GlobalFree(hFix);
366 }
367 if (fuLoad & LR_LOADFROMFILE) CloseHandle( hMapping );
368 return hbitmap;
369}
370//******************************************************************************
371//TODO: No support for RT_NEWBITMAP
372//******************************************************************************
373//******************************************************************************
374//TODO: No support for RT_NEWBITMAP
375//******************************************************************************
376HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
377{
378 HBITMAP hBitmap = 0;
379
380 if (!hinst)
381 {
382 if(IsSystemBitmap((ULONG)lpszBitmap))
383 {
384 hBitmap = LoadBitmapA(hInstanceUser32, lpszBitmap, 0, 0, 0);
385 }
386 }
387 if(!hBitmap)
388 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
389
390 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
391
392 return(hBitmap);
393}
394//******************************************************************************
395//TODO: No support for RT_NEWBITMAP
396//******************************************************************************
397HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
398{
399 HBITMAP hBitmap = 0;
400
401 if(HIWORD(lpszBitmap) != 0)
402 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
403
404 hBitmap = LoadBitmapA((hinst == 0) ? hInstanceUser32:hinst, (LPSTR)lpszBitmap, 0, 0, 0);
405
406 if(HIWORD(lpszBitmap) != 0)
407 FreeAsciiString((LPSTR)lpszBitmap);
408
409 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
410
411 return(hBitmap);
412}
413//******************************************************************************
414//TODO: Far from complete, but works for loading resources from exe
415//fuLoad flag ignored
416//******************************************************************************
417HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
418 int cxDesired, int cyDesired, UINT fuLoad)
419{
420 HANDLE hRet = 0;
421
422 if(HIWORD(lpszName)) {
423 dprintf(("LoadImageA NOT COMPLETE %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
424 }
425 else dprintf(("LoadImageA NOT COMPLETE %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
426
427 if (fuLoad & LR_DEFAULTSIZE) {
428 if (uType == IMAGE_ICON) {
429 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
430 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
431 }
432 else if (uType == IMAGE_CURSOR) {
433 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
434 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
435 }
436 }
437 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
438
439 switch(uType) {
440 case IMAGE_BITMAP:
441 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
442 break;
443 case IMAGE_CURSOR:
444 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
445 break;
446 case IMAGE_ICON:
447 hRet = (HANDLE)LoadIconA(hinst, lpszName);
448 break;
449 default:
450 dprintf(("LoadImageA: unsupported type %d!!", uType));
451 return 0;
452 }
453 dprintf(("LoadImageA returned %d\n", (int)hRet));
454
455 return(hRet);
456}
457//******************************************************************************
458//******************************************************************************
459HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
460 int cxDesired, int cyDesired, UINT fuLoad)
461{
462 HANDLE hRet = 0;
463
464 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", 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 case IMAGE_BITMAP:
480 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
481 break;
482 case IMAGE_CURSOR:
483 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
484 break;
485 case IMAGE_ICON:
486 hRet = (HANDLE)LoadIconW(hinst, lpszName);
487 break;
488 default:
489 dprintf(("LoadImageW: unsupported type %d!!", uType));
490 return 0;
491 }
492 dprintf(("LoadImageW returned %d\n", (int)hRet));
493
494 return(hRet);
495}
496/******************************************************************************
497 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
498 *
499 * PARAMS
500 * hnd [I] Handle to image to copy
501 * type [I] Type of image to copy
502 * desiredx [I] Desired width of new image
503 * desiredy [I] Desired height of new image
504 * flags [I] Copy flags
505 *
506 * RETURNS
507 * Success: Handle to newly created image
508 * Failure: NULL
509 *
510 * FIXME: implementation still lacks nearly all features, see LR_*
511 * defines in windows.h
512 *
513 */
514HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
515 INT desiredy, UINT flags )
516{
517 dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
518 switch (type)
519 {
520// case IMAGE_BITMAP:
521// return BITMAP_CopyBitmap(hnd);
522 case IMAGE_ICON:
523 return CopyIcon(hnd);
524 case IMAGE_CURSOR:
525 return CopyCursor(hnd);
526// return CopyCursorIcon(hnd,type, desiredx, desiredy, flags);
527 default:
528 dprintf(("CopyImage: Unsupported type"));
529 }
530 return 0;
531}
532//******************************************************************************
533//******************************************************************************
Note: See TracBrowser for help on using the repository browser.