1 | /* $Id: loadres.cpp,v 1.11 1999-08-25 12:30:08 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 | //******************************************************************************
|
---|
27 | int 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 | //******************************************************************************
|
---|
54 | int 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 | //******************************************************************************
|
---|
80 | HICON 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 | //******************************************************************************
|
---|
102 | HICON 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 | //******************************************************************************
|
---|
124 | HCURSOR 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 | //******************************************************************************
|
---|
146 | HCURSOR 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 | //******************************************************************************
|
---|
168 | BOOL 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 | //NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)!
|
---|
230 | //******************************************************************************
|
---|
231 | HANDLE LoadBitmapA(HINSTANCE hinst, LPCSTR lpszName, int cxDesired, int cyDesired,
|
---|
232 | UINT fuLoad)
|
---|
233 | {
|
---|
234 | HBITMAP hbitmap = 0;
|
---|
235 | HDC hdc;
|
---|
236 | HRSRC hRsrc;
|
---|
237 | HGLOBAL handle, hMapping = 0;
|
---|
238 | char *ptr = NULL;
|
---|
239 | BITMAPINFO *info, *fix_info=NULL;
|
---|
240 | HGLOBAL hFix;
|
---|
241 | int size;
|
---|
242 |
|
---|
243 | if (!(fuLoad & LR_LOADFROMFILE)) {
|
---|
244 | if (!(hRsrc = FindResourceA( hinst, lpszName, RT_BITMAPA ))) return 0;
|
---|
245 | if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
|
---|
246 |
|
---|
247 | if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | if (!(hMapping = VIRTUAL_MapFileA( lpszName, (LPVOID *)&ptr ))) return 0;
|
---|
252 | info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
|
---|
253 | }
|
---|
254 |
|
---|
255 | //TODO: This has to be removed once pe2lx stores win32 resources!!!
|
---|
256 | if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
|
---|
257 | info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
|
---|
258 | {//assume it contains a file header first
|
---|
259 | info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
|
---|
260 | }
|
---|
261 |
|
---|
262 | size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
|
---|
263 | if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
|
---|
264 | if (fix_info) {
|
---|
265 | BYTE pix;
|
---|
266 |
|
---|
267 | memcpy(fix_info, info, size);
|
---|
268 | pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
|
---|
269 | DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
|
---|
270 | if ((hdc = GetDC(0)) != 0) {
|
---|
271 | char *bits = (char *)info + size;
|
---|
272 | if (fuLoad & LR_CREATEDIBSECTION) {
|
---|
273 | DIBSECTION dib;
|
---|
274 | hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
|
---|
275 | GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
|
---|
276 | SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
|
---|
277 | DIB_RGB_COLORS);
|
---|
278 | }
|
---|
279 | else {
|
---|
280 | hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
|
---|
281 | bits, fix_info, DIB_RGB_COLORS );
|
---|
282 | }
|
---|
283 | ReleaseDC( 0, hdc );
|
---|
284 | }
|
---|
285 | GlobalUnlock(hFix);
|
---|
286 | GlobalFree(hFix);
|
---|
287 | }
|
---|
288 | if (fuLoad & LR_LOADFROMFILE) CloseHandle( hMapping );
|
---|
289 | return hbitmap;
|
---|
290 | }
|
---|
291 | //******************************************************************************
|
---|
292 | //TODO: No support for RT_NEWBITMAP
|
---|
293 | //******************************************************************************
|
---|
294 | HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
|
---|
295 | {
|
---|
296 | HBITMAP hBitmap = 0;
|
---|
297 |
|
---|
298 | if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
|
---|
299 | hBitmap = O32_LoadBitmap(hinst, lpszBitmap);
|
---|
300 | }
|
---|
301 | else {
|
---|
302 | hBitmap = LoadBitmapA(hinst, lpszBitmap, 0, 0, 0);
|
---|
303 | }
|
---|
304 | dprintf(("LoadBitmapA returned %08xh\n", hBitmap));
|
---|
305 |
|
---|
306 | return(hBitmap);
|
---|
307 | }
|
---|
308 | //******************************************************************************
|
---|
309 | //TODO: No support for RT_NEWBITMAP
|
---|
310 | //******************************************************************************
|
---|
311 | HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
|
---|
312 | {
|
---|
313 | HBITMAP hBitmap;
|
---|
314 |
|
---|
315 | if(IsSystemBitmap((ULONG *)&lpszBitmap)) {
|
---|
316 | hBitmap = O32_LoadBitmap(hinst, (LPCSTR)lpszBitmap);
|
---|
317 | }
|
---|
318 | else {
|
---|
319 | if(HIWORD(lpszBitmap) != 0) {
|
---|
320 | lpszBitmap = (LPWSTR)UnicodeToAsciiString((LPWSTR)lpszBitmap);
|
---|
321 | }
|
---|
322 | hBitmap = LoadBitmapA(hinst, (LPSTR)lpszBitmap, 0, 0, 0);
|
---|
323 | }
|
---|
324 |
|
---|
325 | if(HIWORD(lpszBitmap) != 0)
|
---|
326 | FreeAsciiString((LPSTR)lpszBitmap);
|
---|
327 |
|
---|
328 | dprintf(("LoadBitmapW returned %08xh\n", hBitmap));
|
---|
329 |
|
---|
330 | return(hBitmap);
|
---|
331 | }
|
---|
332 | //******************************************************************************
|
---|
333 | //TODO: Far from complete, but works for loading resources from exe
|
---|
334 | //fuLoad flag ignored
|
---|
335 | //******************************************************************************
|
---|
336 | HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
|
---|
337 | int cxDesired, int cyDesired, UINT fuLoad)
|
---|
338 | {
|
---|
339 | HANDLE hRet = 0;
|
---|
340 |
|
---|
341 | dprintf(("LoadImageA NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
|
---|
342 |
|
---|
343 | if (fuLoad & LR_DEFAULTSIZE) {
|
---|
344 | if (uType == IMAGE_ICON) {
|
---|
345 | if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
|
---|
346 | if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
|
---|
347 | }
|
---|
348 | else if (uType == IMAGE_CURSOR) {
|
---|
349 | if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
|
---|
350 | if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
|
---|
354 |
|
---|
355 | switch(uType) {
|
---|
356 | case IMAGE_BITMAP:
|
---|
357 | hRet = (HANDLE)LoadBitmapA(hinst, lpszName, cxDesired, cyDesired, fuLoad);
|
---|
358 | break;
|
---|
359 | case IMAGE_CURSOR:
|
---|
360 | hRet = (HANDLE)LoadCursorA(hinst, lpszName);
|
---|
361 | break;
|
---|
362 | case IMAGE_ICON:
|
---|
363 | hRet = (HANDLE)LoadIconA(hinst, lpszName);
|
---|
364 | break;
|
---|
365 | default:
|
---|
366 | dprintf(("LoadImageA: unsupported type %d!!", uType));
|
---|
367 | return 0;
|
---|
368 | }
|
---|
369 | dprintf(("LoadImageA returned %d\n", (int)hRet));
|
---|
370 |
|
---|
371 | return(hRet);
|
---|
372 | }
|
---|
373 | //******************************************************************************
|
---|
374 | //******************************************************************************
|
---|
375 | HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
|
---|
376 | int cxDesired, int cyDesired, UINT fuLoad)
|
---|
377 | {
|
---|
378 | HANDLE hRet = 0;
|
---|
379 |
|
---|
380 | dprintf(("LoadImageW NOT COMPLETE (%d,%d)\n", cxDesired, cyDesired));
|
---|
381 |
|
---|
382 | if (fuLoad & LR_DEFAULTSIZE) {
|
---|
383 | if (uType == IMAGE_ICON) {
|
---|
384 | if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
|
---|
385 | if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
|
---|
386 | }
|
---|
387 | else if (uType == IMAGE_CURSOR) {
|
---|
388 | if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
|
---|
389 | if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
|
---|
390 | }
|
---|
391 | }
|
---|
392 | if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
|
---|
393 |
|
---|
394 | switch(uType) {
|
---|
395 | case IMAGE_BITMAP:
|
---|
396 | hRet = (HANDLE)LoadBitmapW(hinst, lpszName);
|
---|
397 | break;
|
---|
398 | case IMAGE_CURSOR:
|
---|
399 | hRet = (HANDLE)LoadCursorW(hinst, lpszName);
|
---|
400 | break;
|
---|
401 | case IMAGE_ICON:
|
---|
402 | hRet = (HANDLE)LoadIconW(hinst, lpszName);
|
---|
403 | break;
|
---|
404 | default:
|
---|
405 | dprintf(("LoadImageW: unsupported type %d!!", uType));
|
---|
406 | return 0;
|
---|
407 | }
|
---|
408 | dprintf(("LoadImageW returned %d\n", (int)hRet));
|
---|
409 |
|
---|
410 | return(hRet);
|
---|
411 | }
|
---|
412 | //******************************************************************************
|
---|
413 | //******************************************************************************
|
---|