source: trunk/src/kernel32/winres.cpp@ 337

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

Added method for retrieving OS/2 converted resources (winres.cpp)

File size: 9.8 KB
Line 
1/* $Id: winres.cpp,v 1.4 1999-07-19 11:50:35 sandervl Exp $ */
2
3/*
4 * Win32 resource class
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12#define INCL_BASE
13#define INCL_WIN
14#define INCL_GPIBITMAPS
15#define INCL_BITMAPFILEFORMAT
16#define INCL_DOSMODULEMGR
17#include <os2wrap.h> //Odin32 OS/2 api wrappers
18#include <stdarg.h>
19#ifdef __IBMCPP__
20#include <builtin.h>
21#endif
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#define INCL_WINRES
26#include <win32type.h>
27#include <winres.h>
28#include <misc.h>
29#include <nameid.h>
30#include <winexe.h>
31
32static ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy)
33{
34 ULONG alignment;
35 ULONG factor;
36 BOOL flag = TRUE; //true: '*' false: '/'
37
38 cy = cy < 0 ? -cy : cy;
39
40 switch(cBits)
41 {
42 case 1:
43 factor = 8;
44 flag = FALSE;
45 break;
46
47 case 4:
48 factor = 2;
49 flag = FALSE;
50 break;
51
52 case 8:
53 factor = 1;
54 break;
55
56 case 16:
57 factor = 2;
58 break;
59
60 case 24:
61 factor = 3;
62 break;
63
64 case 32:
65 return cx*cy;
66
67 default:
68 return 0;
69 }
70
71 if (flag)
72 alignment = (cx = (cx*factor)) % 4;
73 else
74 alignment = (cx = ((cx+factor-1)/factor)) % 4;
75
76 if (alignment != 0)
77 cx += 4 - alignment;
78
79 return cx*cy;
80}
81
82//******************************************************************************
83//******************************************************************************
84Win32Resource::Win32Resource(Win32Image *module, HRSRC hRes, ULONG id, ULONG type) :
85 os2resdata(NULL), winresdata(NULL)
86{
87 APIRET rc;
88
89 next = module->winres;
90 module->winres = this;
91
92 this->module = module;
93 this->id = id;
94 this->type = type;
95 this->hres = hRes;
96
97 switch(type) {
98 case NTRT_NEWBITMAP:
99 case NTRT_BITMAP:
100 orgos2type = RT_BITMAP;
101 break;
102 case NTRT_CURSOR:
103 case NTRT_GROUP_CURSOR:
104 case NTRT_GROUP_ICON:
105 case NTRT_ICON:
106 orgos2type = RT_POINTER;
107 break;
108 case NTRT_ACCELERATORS:
109 orgos2type = RT_ACCELTABLE;
110 break;
111 case NTRT_NEWMENU:
112 case NTRT_MENU:
113 orgos2type = RT_MENU;
114 break;
115 case NTRT_NEWDIALOG:
116 case NTRT_DIALOG:
117 orgos2type = RT_DIALOG;
118 break;
119 case NTRT_FONTDIR:
120 case NTRT_FONT:
121 case NTRT_MESSAGETABLE:
122 case NTRT_STRING:
123 case NTRT_RCDATA:
124 case NTRT_VERSION:
125 default:
126 orgos2type = RT_RCDATA;
127 break;
128 }
129
130 rc = DosQueryResourceSize(module->hinstance, type, id, &ressize);
131 if(rc) {
132 dprintf(("Win32Resource ctor: DosQueryResourceSize %x %d %d returned %X\n", module->hinstance, type, id, rc));
133 ressize = 0;
134 }
135}
136//******************************************************************************
137//******************************************************************************
138Win32Resource::Win32Resource(Win32Image *module, ULONG id, ULONG type,
139 ULONG size, char *resdata) : hres(NULL),
140 os2resdata(NULL), winresdata(NULL)
141{
142 next = module->winres;
143 module->winres = this;
144
145 this->module = module;
146 this->id = id;
147 this->type = type;
148 orgos2type = -1;
149 this->ressize = size;
150 winresdata = (char *)malloc(size);
151 if(winresdata == NULL) {
152 DebugInt3();
153 return;
154 }
155 memcpy(winresdata, resdata, size);
156}
157//******************************************************************************
158//******************************************************************************
159Win32Resource::~Win32Resource()
160{
161 Win32Resource *res = module->winres;
162
163#if 0
164 //returned by DosGetResource, so we don't (and mustn't) free it
165 if(os2resdata) free(os2resdata);
166#endif
167 if(winresdata) free(winresdata);
168
169 if(res == this) {
170 module->winres = res->next;
171 }
172 else {
173 while(res->next != this) {
174 res = res->next;
175 }
176 if(res)
177 res->next = next;
178 }
179}
180//******************************************************************************
181//******************************************************************************
182PVOID Win32Resource::lockResource()
183{
184 int restype = 0, newid;
185 void *resdata = NULL;
186 APIRET rc;
187 ULONG os2type = RT_RCDATA;
188
189 dprintf(("Win32Resource::lockResource %d\n", id));
190 if(winresdata)
191 return(winresdata);
192
193 switch(type) {
194 case NTRT_BITMAP:
195 rc = DosGetResource((HMODULE)module->hinstance, RT_BITMAP, id, (PPVOID)&resdata);
196 if(rc) return(NULL);
197 winresdata = ConvertBitmap((BITMAPFILEHEADER2 *)resdata);
198 break;
199
200 case NTRT_ACCELERATORS:
201 case NTRT_MENU:
202 case NTRT_DIALOG:
203 newid = module->getWin32ResourceId(id);
204
205 rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, (int)newid, (PPVOID)&resdata);
206 if(rc) {
207 dprintf(("Can't find original resource!!!\n"));
208 return(NULL);
209 }
210 winresdata = (char *)malloc(ressize);
211 memcpy(winresdata, resdata, ressize);
212 break;
213
214 //TODO:not yet implemented
215 case NTRT_FONTDIR:
216 case NTRT_FONT:
217 case NTRT_MESSAGETABLE:
218 case NTRT_NEWBITMAP:
219 case NTRT_NEWMENU:
220 case NTRT_NEWDIALOG:
221 os2type = RT_RCDATA;
222 break;
223
224 //Can't do this right now (all group icons stored into a single one)
225 case NTRT_CURSOR:
226 case NTRT_GROUP_CURSOR:
227 case NTRT_GROUP_ICON:
228 case NTRT_ICON:
229 dprintf(("Can't convert this resource!!!!!\n"));
230 os2type = RT_POINTER;
231 break;
232
233 case NTRT_STRING:
234 rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, id, (PPVOID)&resdata);
235 if(rc) {
236 dprintf(("Can't find original string!!!\n"));
237 return(NULL);
238 }
239 winresdata = (char *)malloc(ressize);
240 memcpy(winresdata, resdata, ressize);
241 DosFreeResource(resdata);
242 return((PVOID)((ULONG)winresdata+2)); //skip length word
243
244 //no conversion necessary
245 case NTRT_RCDATA:
246 case NTRT_VERSION:
247 default:
248 os2type = RT_RCDATA;
249 break;
250 }
251
252 if(winresdata == NULL) {
253 rc = DosGetResource((HMODULE)module->hinstance, os2type, id, (PPVOID)&resdata);
254 if(rc) {
255 dprintf(("Can't find original string!!!\n"));
256 return(NULL);
257 }
258 winresdata = (char *)malloc(ressize);
259 memcpy(winresdata, resdata, ressize);
260 }
261 if(resdata)
262 DosFreeResource(resdata);
263 return winresdata;
264}
265//******************************************************************************
266//******************************************************************************
267PVOID Win32Resource::lockOS2Resource()
268{
269 APIRET rc;
270 PVOID resdata;
271
272 dprintf(("Win32Resource::lockOS2Resource %d\n", id));
273 if(os2resdata == NULL) {
274 rc = DosGetResource((HMODULE)module->hinstance, orgos2type, id, (PPVOID)&resdata);
275 if(rc) return(NULL);
276 os2resdata = resdata;
277 }
278 return os2resdata;
279}
280//******************************************************************************
281//******************************************************************************
282PVOID Win32Resource::ConvertBitmap(void *bmpdata)
283{
284 BITMAPFILEHEADER2 *bmphdr = (BITMAPFILEHEADER2 *)bmpdata;
285 WINBITMAPINFOHEADER *winbmphdr;
286 RGBQUAD *rgb;
287 RGB2 *os2rgb;
288 int palsize = 0;
289 int imgsize;
290
291 if(bmphdr->cbSize != sizeof(BITMAPFILEHEADER2))
292 return(bmpdata); //don't convert OS/2 1.x bitmap
293
294 if(bmphdr->bmp2.cBitCount < 16) {
295 palsize = (1 << bmphdr->bmp2.cBitCount) * sizeof(RGBQUAD);
296 }
297
298 // EB: ->>> added imgsize
299 if(bmphdr->bmp2.cbImage == 0)
300 imgsize = CalcBitmapSize(bmphdr->bmp2.cBitCount,
301 bmphdr->bmp2.cx,
302 bmphdr->bmp2.cy);
303 else
304 imgsize = bmphdr->bmp2.cbImage;
305
306 winbmphdr = (WINBITMAPINFOHEADER *)malloc(sizeof(WINBITMAPINFOHEADER) +
307 imgsize + palsize);
308 memset((char *)winbmphdr, 0, sizeof(WINBITMAPINFOHEADER) + imgsize + palsize);
309
310 winbmphdr->biSize = sizeof(WINBITMAPINFOHEADER);
311 winbmphdr->biWidth = bmphdr->bmp2.cx;
312 winbmphdr->biHeight = bmphdr->bmp2.cy;
313 winbmphdr->biPlanes = bmphdr->bmp2.cPlanes;
314 winbmphdr->biBitCount = bmphdr->bmp2.cBitCount;
315 //TODO: Identical except for BI_BITFIELDS (3L) type!
316 winbmphdr->biCompression = bmphdr->bmp2.ulCompression;
317 winbmphdr->biSizeImage = bmphdr->bmp2.cbImage; //imgsize;
318 //TODO: Doesn't seem to be completely identical..
319 winbmphdr->biClrUsed = bmphdr->bmp2.cclrUsed;
320 winbmphdr->biClrImportant = bmphdr->bmp2.cclrImportant;
321 winbmphdr->biXPelsPerMeter = bmphdr->bmp2.cxResolution;
322 winbmphdr->biYPelsPerMeter = bmphdr->bmp2.cyResolution;
323
324 os2rgb = (RGB2 *)(bmphdr+1);
325 rgb = (RGBQUAD *)(winbmphdr+1);
326
327 if(palsize) {
328 memcpy((char *)rgb, (char *)os2rgb, palsize);
329 os2rgb = (RGB2 *)((int)os2rgb + palsize);
330 rgb = (RGBQUAD *)((int)rgb + palsize);
331 }
332 memcpy((char *)rgb, (char *)os2rgb, imgsize);
333 return((PVOID)winbmphdr);
334}
335//******************************************************************************
336//******************************************************************************
337void Win32Resource::destroyAll(Win32Image *module)
338{
339 Win32Resource *res = module->winres, *next;
340
341 while(res) {
342 next = res->next;
343 delete(res);
344 res = next;
345 }
346}
347//******************************************************************************
348//******************************************************************************
Note: See TracBrowser for help on using the repository browser.