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