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

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

LoadBitmap changes

File size: 12.7 KB
Line 
1/* $Id: loadres.cpp,v 1.8 1999-08-21 19:10:59 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):
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 dprintf(("LoadCursorA (%X) returned %x\n", hinst, hCursor));
141
142 return(hCursor);
143}
144//******************************************************************************
145//******************************************************************************
146HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
147{
148 Win32Resource *winres;
149 HCURSOR hCursor;
150
151 hCursor = OSLibWinQuerySysPointer((ULONG)lpszCursor);
152 if(hCursor == 0) {//not a system pointer
153 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_CURSORW);
154 if(winres == 0) {
155 winres = (Win32Resource *)FindResourceW(hinst, lpszCursor, RT_GROUP_CURSORW);
156 }
157 if(winres) {
158 hCursor = OSLibWinCreatePointer(winres->lockOS2Resource());
159 delete winres;
160 }
161 }
162 dprintf(("LoadCursorW (%X) returned %x\n", hinst, hCursor));
163
164 return(hCursor);
165}
166//******************************************************************************
167//******************************************************************************
168BOOL IsSystemBitmap(ULONG *id)
169{
170 switch(*id)
171 {
172 case OBM_UPARROW_W:
173 case OBM_DNARROW_W:
174 case OBM_RGARROW_W:
175 case OBM_LFARROW_W:
176 case OBM_RESTORE_W:
177 case OBM_RESTORED_W:
178 case OBM_UPARROWD_W:
179 case OBM_DNARROWD_W:
180 case OBM_RGARROWD_W:
181 case OBM_LFARROWD_W:
182 case OBM_OLD_UPARROW_W:
183 case OBM_OLD_DNARROW_W:
184 case OBM_OLD_RGARROW_W:
185 case OBM_OLD_LFARROW_W:
186 case OBM_CHECK_W:
187 case OBM_CHECKBOXES_W:
188 case OBM_BTNCORNERS_W:
189 case OBM_COMBO_W:
190 case OBM_REDUCE_W:
191 case OBM_REDUCED_W:
192 case OBM_ZOOM_W:
193 case OBM_ZOOMD_W:
194 case OBM_SIZE_W:
195 case OBM_CLOSE_W:
196 case OBM_MNARROW_W:
197 case OBM_UPARROWI_W:
198 case OBM_DNARROWI_W:
199 case OBM_RGARROWI_W:
200 case OBM_LFARROWI_W:
201 return TRUE;
202
203 //TODO: Not supported by Open32. Replacement may not be accurate
204 case OBM_OLD_CLOSE_W:
205 *id = OBM_CLOSE_W;
206 return TRUE;
207
208 case OBM_BTSIZE_W:
209 *id = OBM_SIZE_W;
210 return TRUE;
211
212 case OBM_OLD_REDUCE_W:
213 *id = OBM_REDUCE_W;
214 return TRUE;
215
216 case OBM_OLD_ZOOM_W:
217 *id = OBM_ZOOM_W;
218 return TRUE;
219
220 case OBM_OLD_RESTORE_W:
221 *id = OBM_RESTORE_W;
222 return TRUE;
223
224 default:
225 return FALSE;
226 }
227}
228//******************************************************************************
229//******************************************************************************
230HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
231 UINT fuLoad)
232{
233 HBITMAP hbitmap = 0;
234 HDC hdc;
235 HRSRC hRsrc;
236 HGLOBAL handle;
237 char *ptr = NULL;
238 BITMAPINFO *info, *fix_info=NULL;
239 HGLOBAL hFix;
240 int size;
241
242 if (!(fuLoad & LR_LOADFROMFILE)) {
243 if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
244 if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
245
246 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
247 }
248 else
249 {
250 if (!(ptr = (char *)VIRTUAL_MapFileA( lpszName ))) return 0;
251 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
252 }
253
254 //TODO: This has to be removed once pe2lx stores win32 resources!!!
255 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
256 info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
257 {//assume it contains a file header first
258 info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
259 }
260
261 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
262 if ((hFix = GlobalAlloc(0, size))) fix_info = (BITMAPINFO *)GlobalLock(hFix);
263 if (fix_info) {
264 BYTE pix;
265
266 memcpy(fix_info, info, size);
267 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
268 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
269 if ((hdc = GetDC(0)) != 0) {
270 char *bits = (char *)info + size;
271 if (fuLoad & LR_CREATEDIBSECTION) {
272 DIBSECTION dib;
273 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
274 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
275 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
276 DIB_RGB_COLORS);
277 }
278 else {
279 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
280 bits, fix_info, DIB_RGB_COLORS );
281 }
282 ReleaseDC( 0, hdc );
283 }
284 GlobalUnlock(hFix);
285 GlobalFree(hFix);
286 }
287 if (fuLoad & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
288 return hbitmap;
289}
290//******************************************************************************
291//TODO: No support for RT_NEWBITMAP
292//******************************************************************************
293HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
294{
295 HBITMAP hBitmap = 0;
296
297 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
298 hBitmap = O32_LoadBitmap(hinst, lpszBitmap);
299 }
300 else {
301 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
302 }
303 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
304
305 return(hBitmap);
306}
307//******************************************************************************
308//TODO: No support for RT_NEWBITMAP
309//******************************************************************************
310HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
311{
312 HBITMAP hBitmap;
313
314 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
315 hBitmap = O32_LoadBitmap(hinst, (LPCSTR)lpszBitmap);
316 }
317 else {
318 if(HIWORD(lpszBitmap) != 0) {
319 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
320 }
321 hBitmap = LoadBitmapA(hinst, (LPSTR)lpszBitmap, 0, 0, 0);
322 }
323
324 if(HIWORD(lpszBitmap) != 0)
325 FreeAsciiString((LPSTR)lpszBitmap);
326
327 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
328
329 return(hBitmap);
330}
331//******************************************************************************
332//TODO: Far from complete, but works for loading resources from exe
333//fuLoad flag ignored
334//******************************************************************************
335HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
336 int cxDesired, int cyDesired, UINT fuLoad)
337{
338 HANDLE hRet = 0;
339
340 dprintf(("LoadImageA NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
341
342 if (fuLoad & LR_DEFAULTSIZE) {
343 if (uType == IMAGE_ICON) {
344 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
345 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
346 }
347 else if (uType == IMAGE_CURSOR) {
348 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
349 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
350 }
351 }
352 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
353
354 switch(uType) {
355 case IMAGE_BITMAP:
356 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
357 break;
358 case IMAGE_CURSOR:
359 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
360 break;
361 case IMAGE_ICON:
362 hRet = (HANDLE)LoadIconA(hinst, lpszName);
363 break;
364 default:
365 dprintf(("LoadImageA: unsupported type %d!!", uType));
366 return 0;
367 }
368 dprintf(("LoadImageA returned %d\n", (int)hRet));
369
370 return(hRet);
371}
372//******************************************************************************
373//******************************************************************************
374HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
375 int cxDesired, int cyDesired, UINT fuLoad)
376{
377 HANDLE hRet = 0;
378
379 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
380
381 if (fuLoad & LR_DEFAULTSIZE) {
382 if (uType == IMAGE_ICON) {
383 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
384 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
385 }
386 else if (uType == IMAGE_CURSOR) {
387 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
388 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
389 }
390 }
391 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
392
393 switch(uType) {
394 case IMAGE_BITMAP:
395 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
396 break;
397 case IMAGE_CURSOR:
398 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
399 break;
400 case IMAGE_ICON:
401 hRet = (HANDLE)LoadIconW(hinst, lpszName);
402 break;
403 default:
404 dprintf(("LoadImageW: unsupported type %d!!", uType));
405 return 0;
406 }
407 dprintf(("LoadImageW returned %d\n", (int)hRet));
408
409 return(hRet);
410}
411//******************************************************************************
412//******************************************************************************
Note: See TracBrowser for help on using the repository browser.