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

Last change on this file since 1196 was 1196, checked in by dengert, 26 years ago

fix miscoloured bitmaps

File size: 14.9 KB
Line 
1/* $Id: loadres.cpp,v 1.8 1999-10-08 19:34:25 dengert 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):
9 *
10 * Copyright 1993 Alexandre Julliard
11 * 1998 Huw D M Davies
12 *
13 * Project Odin Software License can be found in LICENSE.TXT
14 *
15 */
16#include <os2win.h>
17#include <user32.h>
18#include <winres.h>
19#include <heapstring.h>
20#include <oslibres.h>
21#include <winconst.h>
22#include <win\virtual.h>
23#include "dib.h"
24
25//******************************************************************************
26//******************************************************************************
27int WIN32API LoadStringA(HINSTANCE hinst, UINT wID, LPSTR lpBuffer, int cchBuffer)
28{
29 Win32Resource *winres;
30 LPWSTR resstring;
31 int resstrlen = 0;
32
33 winres = (Win32Resource *)FindResourceA(hinst, (LPSTR)wID, RT_STRINGA);
34 if(winres == NULL) {
35 dprintf(("LoadStringA NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
36 return 0;
37 }
38
39 resstring = (LPWSTR)winres->lockResource();
40 if(resstring) {
41 resstrlen = min(lstrlenW(resstring)+1, cchBuffer);
42 lstrcpynWtoA(lpBuffer, resstring, resstrlen);
43 lpBuffer[resstrlen-1] = 0;
44 resstrlen--;
45 dprintf(("LoadStringA (%d) %s", resstrlen, lpBuffer));
46 }
47 delete winres;
48
49 dprintf(("LoadStringA from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
50 return(resstrlen);
51}
52//******************************************************************************
53//******************************************************************************
54int WIN32API LoadStringW(HINSTANCE hinst, UINT wID, LPWSTR lpBuffer, int cchBuffer)
55{
56 Win32Resource *winres;
57 LPWSTR resstring;
58 int resstrlen = 0;
59
60 winres = (Win32Resource *)FindResourceW(hinst, (LPWSTR)wID, RT_STRINGW);
61 if(winres == NULL) {
62 dprintf(("LoadStringW NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
63 return 0;
64 }
65
66 resstring = (LPWSTR)winres->lockResource();
67 if(resstring) {
68 resstrlen = min(lstrlenW(resstring)+1, cchBuffer);
69 lstrcpynW(lpBuffer, resstring, resstrlen);
70 lpBuffer[resstrlen-1] = 0;
71 resstrlen--;
72 }
73 delete winres;
74
75 dprintf(("LoadStringW from %X, id %d buffersize %d\n", hinst, wID, cchBuffer));
76 return(resstrlen);
77}
78//******************************************************************************
79//******************************************************************************
80HICON WIN32API LoadIconA(HINSTANCE hinst, LPCSTR lpszIcon)
81{
82 Win32Resource *winres;
83 HICON hIcon;
84
85 hIcon = OSLibWinQuerySysIcon((ULONG)lpszIcon);
86 if(hIcon == 0) {//not a system icon
87 winres = (Win32Resource *)FindResourceA(hinst, lpszIcon, RT_ICONA);
88 if(winres == 0) {
89 winres = (Win32Resource *)FindResourceA(hinst, lpszIcon, RT_GROUP_ICONA);
90 }
91 if(winres) {
92 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
93 delete winres;
94 }
95 }
96 dprintf(("LoadIconA (%X) returned %x\n", hinst, hIcon));
97
98 return(hIcon);
99}
100//******************************************************************************
101//******************************************************************************
102HICON WIN32API LoadIconW(HINSTANCE hinst, LPCWSTR lpszIcon)
103{
104 Win32Resource *winres;
105 HICON hIcon;
106
107 hIcon = OSLibWinQuerySysIcon((ULONG)lpszIcon);
108 if(hIcon == 0) {//not a system icon
109 winres = (Win32Resource *)FindResourceW(hinst, lpszIcon, RT_ICONW);
110 if(winres == 0) {
111 winres = (Win32Resource *)FindResourceW(hinst, lpszIcon, RT_GROUP_ICONW);
112 }
113 if(winres) {
114 hIcon = OSLibWinCreateIcon(winres->lockOS2Resource());
115 delete winres;
116 }
117 }
118 dprintf(("LoadIconW (%X) returned %x\n", hinst, hIcon));
119
120 return(hIcon);
121}
122//******************************************************************************
123//******************************************************************************
124HCURSOR WIN32API LoadCursorA(HINSTANCE hinst, LPCSTR lpszCursor)
125{
126 Win32Resource *winres;
127 HCURSOR hCursor;
128
129 hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor);
130 if(hCursor == 0) {//not a system pointer
131 winres = (Win32Resource *)FindResourceA(hinst, lpszCursor, RT_CURSORA);
132 if(winres == 0) {
133 winres = (Win32Resource *)FindResourceA(hinst, lpszCursor, RT_GROUP_CURSORA);
134 }
135 if(winres) {
136 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
137 delete winres;
138 }
139 }
140 if(HIWORD(lpszCursor)) {
141 dprintf(("LoadCursorA %s from %x returned %x\n", lpszCursor, hinst, hCursor));
142 }
143 else dprintf(("LoadCursorA %x from %x returned %x\n", lpszCursor, hinst, hCursor));
144
145 return(hCursor);
146}
147//******************************************************************************
148//******************************************************************************
149HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
150{
151 Win32Resource *winres;
152 HCURSOR hCursor;
153
154 hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor);
155 if(hCursor == 0) {//not a system pointer
156 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_CURSORW);
157 if(winres == 0) {
158 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_GROUP_CURSORW);
159 }
160 if(winres) {
161 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
162 delete winres;
163 }
164 }
165 dprintf(("LoadCursorW (%X) returned %x\n", hinst, hCursor));
166
167 return(hCursor);
168}
169//******************************************************************************
170//******************************************************************************
171BOOL IsSystemBitmap(ULONG *id)
172{
173 switch(*id)
174 {
175 case OBM_UPARROW_W:
176 case OBM_DNARROW_W:
177 case OBM_RGARROW_W:
178 case OBM_LFARROW_W:
179 case OBM_RESTORE_W:
180 case OBM_RESTORED_W:
181 case OBM_UPARROWD_W:
182 case OBM_DNARROWD_W:
183 case OBM_RGARROWD_W:
184 case OBM_LFARROWD_W:
185 case OBM_OLD_UPARROW_W:
186 case OBM_OLD_DNARROW_W:
187 case OBM_OLD_RGARROW_W:
188 case OBM_OLD_LFARROW_W:
189 case OBM_CHECK_W:
190 case OBM_CHECKBOXES_W:
191 case OBM_BTNCORNERS_W:
192 case OBM_COMBO_W:
193 case OBM_REDUCE_W:
194 case OBM_REDUCED_W:
195 case OBM_ZOOM_W:
196 case OBM_ZOOMD_W:
197 case OBM_SIZE_W:
198 case OBM_CLOSE_W:
199 case OBM_MNARROW_W:
200 case OBM_UPARROWI_W:
201 case OBM_DNARROWI_W:
202 case OBM_RGARROWI_W:
203 case OBM_LFARROWI_W:
204 return TRUE;
205
206 //TODO: Not supported by Open32. Replacement may not be accurate
207 case OBM_OLD_CLOSE_W:
208 *id = OBM_CLOSE_W;
209 return TRUE;
210
211 case OBM_BTSIZE_W:
212 *id = OBM_SIZE_W;
213 return TRUE;
214
215 case OBM_OLD_REDUCE_W:
216 *id = OBM_REDUCE_W;
217 return TRUE;
218
219 case OBM_OLD_ZOOM_W:
220 *id = OBM_ZOOM_W;
221 return TRUE;
222
223 case OBM_OLD_RESTORE_W:
224 *id = OBM_RESTORE_W;
225 return TRUE;
226
227 default:
228 return FALSE;
229 }
230}
231//******************************************************************************
232//NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)!
233//******************************************************************************
234HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
235 UINT fuLoad)
236{
237 HBITMAP hbitmap = 0;
238 HDC hdc;
239 HRSRC hRsrc;
240 HGLOBAL handle, hMapping = 0;
241 char *ptr = NULL;
242 BITMAPINFO *info, *fix_info=NULL;
243 HGLOBAL hFix;
244 int size;
245
246 if (!(fuLoad & LR_LOADFROMFILE)) {
247 if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
248 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
249
250 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
251 }
252 else
253 {
254 if (!(hMapping = VIRTUAL_MapFileA( lpszName, (LPVOID *)&ptr ))) return 0;
255 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
256 }
257
258 //TODO: This has to be removed once pe2lx stores win32 resources!!!
259 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
260 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
261 {//assume it contains a file header first
262 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
263 }
264
265 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
266 size = sizeof(BITMAPINFOHEADER) +
267 (sizeof (RGBTRIPLE) << ((BITMAPCOREHEADER *)info)->bcBitCount);
268 } else
269 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
270
271 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
272 if (fix_info) {
273 BYTE pix;
274
275 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
276 ULONG colors;
277 ULONG *p, *q;
278
279 memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
280 fix_info->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
281 fix_info->bmiHeader.biWidth = ((BITMAPCOREHEADER *)info)->bcWidth;
282 fix_info->bmiHeader.biHeight = ((BITMAPCOREHEADER *)info)->bcHeight;
283 fix_info->bmiHeader.biPlanes = ((BITMAPCOREHEADER *)info)->bcPlanes;
284 fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
285
286 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
287 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
288 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
289 *q = *p & 0x00FFFFFFUL;
290 q++;
291 p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
292 }
293 } else
294 memcpy(fix_info, info, size);
295
296 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
297 pix = *((LPBYTE)info + size);
298 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
299 if ((hdc = GetDC(0)) != 0) {
300 char *bits = (char *)info + size;
301 if (fuLoad & LR_CREATEDIBSECTION) {
302 DIBSECTION dib;
303 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
304 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
305 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
306 DIB_RGB_COLORS);
307 }
308 else {
309 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
310 bits, fix_info, DIB_RGB_COLORS );
311 }
312 ReleaseDC( 0, hdc );
313 }
314 GlobalUnlock(hFix);
315 GlobalFree(hFix);
316 }
317 if (fuLoad & LR_LOADFROMFILE) CloseHandle( hMapping );
318 return hbitmap;
319}
320//******************************************************************************
321//TODO: No support for RT_NEWBITMAP
322//******************************************************************************
323HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
324{
325 HBITMAP hBitmap = 0;
326
327 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
328 hBitmap = O32_LoadBitmap(hinst, lpszBitmap);
329 }
330 else {
331 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
332 }
333 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
334
335 return(hBitmap);
336}
337//******************************************************************************
338//TODO: No support for RT_NEWBITMAP
339//******************************************************************************
340HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
341{
342 HBITMAP hBitmap;
343
344 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
345 hBitmap = O32_LoadBitmap(hinst, (LPCSTR)lpszBitmap);
346 }
347 else {
348 if(HIWORD(lpszBitmap) != 0) {
349 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
350 }
351 hBitmap = LoadBitmapA(hinst, (LPSTR)lpszBitmap, 0, 0, 0);
352 }
353
354 if(HIWORD(lpszBitmap) != 0)
355 FreeAsciiString((LPSTR)lpszBitmap);
356
357 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
358
359 return(hBitmap);
360}
361//******************************************************************************
362//TODO: Far from complete, but works for loading resources from exe
363//fuLoad flag ignored
364//******************************************************************************
365HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
366 int cxDesired, int cyDesired, UINT fuLoad)
367{
368 HANDLE hRet = 0;
369
370 dprintf(("LoadImageA NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
371
372 if (fuLoad & LR_DEFAULTSIZE) {
373 if (uType == IMAGE_ICON) {
374 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
375 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
376 }
377 else if (uType == IMAGE_CURSOR) {
378 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
379 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
380 }
381 }
382 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
383
384 switch(uType) {
385 case IMAGE_BITMAP:
386 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
387 break;
388 case IMAGE_CURSOR:
389 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
390 break;
391 case IMAGE_ICON:
392 hRet = (HANDLE)LoadIconA(hinst, lpszName);
393 break;
394 default:
395 dprintf(("LoadImageA: unsupported type %d!!", uType));
396 return 0;
397 }
398 dprintf(("LoadImageA returned %d\n", (int)hRet));
399
400 return(hRet);
401}
402//******************************************************************************
403//******************************************************************************
404HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
405 int cxDesired, int cyDesired, UINT fuLoad)
406{
407 HANDLE hRet = 0;
408
409 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
410
411 if (fuLoad & LR_DEFAULTSIZE) {
412 if (uType == IMAGE_ICON) {
413 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
414 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
415 }
416 else if (uType == IMAGE_CURSOR) {
417 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
418 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
419 }
420 }
421 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
422
423 switch(uType) {
424 case IMAGE_BITMAP:
425 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
426 break;
427 case IMAGE_CURSOR:
428 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
429 break;
430 case IMAGE_ICON:
431 hRet = (HANDLE)LoadIconW(hinst, lpszName);
432 break;
433 default:
434 dprintf(("LoadImageW: unsupported type %d!!", uType));
435 return 0;
436 }
437 dprintf(("LoadImageW returned %d\n", (int)hRet));
438
439 return(hRet);
440}
441//******************************************************************************
442//******************************************************************************
Note: See TracBrowser for help on using the repository browser.