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

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

Cursor fixes + minor change to RedrawWindow

File size: 12.4 KB
Line 
1/* $Id: loadres.cpp,v 1.7 1999-09-25 14:15:54 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 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 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
266 if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
267 if (fix_info) {
268 BYTE pix;
269
270 memcpy(fix_info, info, size);
271 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
272 DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
273 if ((hdc = GetDC(0)) != 0) {
274 char *bits = (char *)info + size;
275 if (fuLoad & LR_CREATEDIBSECTION) {
276 DIBSECTION dib;
277 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
278 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
279 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
280 DIB_RGB_COLORS);
281 }
282 else {
283 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
284 bits, fix_info, DIB_RGB_COLORS );
285 }
286 ReleaseDC( 0, hdc );
287 }
288 GlobalUnlock(hFix);
289 GlobalFree(hFix);
290 }
291 if (fuLoad & LR_LOADFROMFILE) CloseHandle( hMapping );
292 return hbitmap;
293}
294//******************************************************************************
295//TODO: No support for RT_NEWBITMAP
296//******************************************************************************
297HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
298{
299 HBITMAP hBitmap = 0;
300
301 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
302 hBitmap = O32_LoadBitmap(hinst, lpszBitmap);
303 }
304 else {
305 hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
306 }
307 dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
308
309 return(hBitmap);
310}
311//******************************************************************************
312//TODO: No support for RT_NEWBITMAP
313//******************************************************************************
314HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
315{
316 HBITMAP hBitmap;
317
318 if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
319 hBitmap = O32_LoadBitmap(hinst, (LPCSTR)lpszBitmap);
320 }
321 else {
322 if(HIWORD(lpszBitmap) != 0) {
323 lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
324 }
325 hBitmap = LoadBitmapA(hinst, (LPSTR)lpszBitmap, 0, 0, 0);
326 }
327
328 if(HIWORD(lpszBitmap) != 0)
329 FreeAsciiString((LPSTR)lpszBitmap);
330
331 dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
332
333 return(hBitmap);
334}
335//******************************************************************************
336//TODO: Far from complete, but works for loading resources from exe
337//fuLoad flag ignored
338//******************************************************************************
339HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
340 int cxDesired, int cyDesired, UINT fuLoad)
341{
342 HANDLE hRet = 0;
343
344 dprintf(("LoadImageA NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
345
346 if (fuLoad & LR_DEFAULTSIZE) {
347 if (uType == IMAGE_ICON) {
348 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
349 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
350 }
351 else if (uType == IMAGE_CURSOR) {
352 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
353 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
354 }
355 }
356 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
357
358 switch(uType) {
359 case IMAGE_BITMAP:
360 hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
361 break;
362 case IMAGE_CURSOR:
363 hRet = (HANDLE)LoadCursorA(hinst, lpszName);
364 break;
365 case IMAGE_ICON:
366 hRet = (HANDLE)LoadIconA(hinst, lpszName);
367 break;
368 default:
369 dprintf(("LoadImageA: unsupported type %d!!", uType));
370 return 0;
371 }
372 dprintf(("LoadImageA returned %d\n", (int)hRet));
373
374 return(hRet);
375}
376//******************************************************************************
377//******************************************************************************
378HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
379 int cxDesired, int cyDesired, UINT fuLoad)
380{
381 HANDLE hRet = 0;
382
383 dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
384
385 if (fuLoad & LR_DEFAULTSIZE) {
386 if (uType == IMAGE_ICON) {
387 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
388 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
389 }
390 else if (uType == IMAGE_CURSOR) {
391 if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
392 if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
393 }
394 }
395 if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
396
397 switch(uType) {
398 case IMAGE_BITMAP:
399 hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
400 break;
401 case IMAGE_CURSOR:
402 hRet = (HANDLE)LoadCursorW(hinst, lpszName);
403 break;
404 case IMAGE_ICON:
405 hRet = (HANDLE)LoadIconW(hinst, lpszName);
406 break;
407 default:
408 dprintf(("LoadImageW: unsupported type %d!!", uType));
409 return 0;
410 }
411 dprintf(("LoadImageW returned %d\n", (int)hRet));
412
413 return(hRet);
414}
415//******************************************************************************
416//******************************************************************************
Note: See TracBrowser for help on using the repository browser.