1 | /* $Id: winres.cpp,v 1.3 1999-06-19 10:54:44 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 |
|
---|
32 | static 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 | //******************************************************************************
|
---|
84 | Win32Resource::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 | rc = DosQueryResourceSize(module->hinstance, type, id, &ressize);
|
---|
98 | if(rc) {
|
---|
99 | dprintf(("Win32Resource ctor: DosQueryResourceSize returned %X\n", rc));
|
---|
100 | ressize = 0;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | //******************************************************************************
|
---|
104 | //******************************************************************************
|
---|
105 | Win32Resource::Win32Resource(Win32Image *module, ULONG id, ULONG type,
|
---|
106 | ULONG size, char *resdata) : hres(NULL),
|
---|
107 | os2resdata(NULL), winresdata(NULL)
|
---|
108 | {
|
---|
109 | next = module->winres;
|
---|
110 | module->winres = this;
|
---|
111 |
|
---|
112 | this->module = module;
|
---|
113 | this->id = id;
|
---|
114 | this->type = type;
|
---|
115 | this->ressize = size;
|
---|
116 | winresdata = (char *)malloc(size);
|
---|
117 | if(winresdata == NULL) {
|
---|
118 | DebugInt3();
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | memcpy(winresdata, resdata, size);
|
---|
122 | }
|
---|
123 | //******************************************************************************
|
---|
124 | //******************************************************************************
|
---|
125 | Win32Resource::~Win32Resource()
|
---|
126 | {
|
---|
127 | Win32Resource *res = module->winres;
|
---|
128 |
|
---|
129 | if(os2resdata) free(os2resdata);
|
---|
130 | if(winresdata) free(winresdata);
|
---|
131 |
|
---|
132 | if(res == this) {
|
---|
133 | module->winres = res->next;
|
---|
134 | }
|
---|
135 | else {
|
---|
136 | while(res->next != this) {
|
---|
137 | res = res->next;
|
---|
138 | }
|
---|
139 | if(res)
|
---|
140 | res->next = next;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | //******************************************************************************
|
---|
144 | //******************************************************************************
|
---|
145 | PVOID Win32Resource::lockResource()
|
---|
146 | {
|
---|
147 | int restype = 0, newid;
|
---|
148 | void *resdata = NULL;
|
---|
149 | APIRET rc;
|
---|
150 | ULONG os2type = RT_RCDATA;
|
---|
151 |
|
---|
152 | dprintf(("Win32Resource::lockResource %d\n", id));
|
---|
153 | if(winresdata)
|
---|
154 | return(winresdata);
|
---|
155 |
|
---|
156 | switch(type) {
|
---|
157 | case NTRT_BITMAP:
|
---|
158 | rc = DosGetResource((HMODULE)module->hinstance, RT_BITMAP, id, (PPVOID)&resdata);
|
---|
159 | if(rc) return(NULL);
|
---|
160 | winresdata = ConvertBitmap((BITMAPFILEHEADER2 *)resdata);
|
---|
161 | break;
|
---|
162 |
|
---|
163 | case NTRT_ACCELERATORS:
|
---|
164 | case NTRT_MENU:
|
---|
165 | case NTRT_DIALOG:
|
---|
166 | newid = module->getWin32ResourceId(id);
|
---|
167 |
|
---|
168 | rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, (int)newid, (PPVOID)&resdata);
|
---|
169 | if(rc) {
|
---|
170 | dprintf(("Can't find original resource!!!\n"));
|
---|
171 | return(NULL);
|
---|
172 | }
|
---|
173 | winresdata = (char *)malloc(ressize);
|
---|
174 | memcpy(winresdata, resdata, ressize);
|
---|
175 | break;
|
---|
176 |
|
---|
177 | //TODO:not yet implemented
|
---|
178 | case NTRT_FONTDIR:
|
---|
179 | case NTRT_FONT:
|
---|
180 | case NTRT_MESSAGETABLE:
|
---|
181 | case NTRT_NEWBITMAP:
|
---|
182 | case NTRT_NEWMENU:
|
---|
183 | case NTRT_NEWDIALOG:
|
---|
184 | os2type = RT_RCDATA;
|
---|
185 | break;
|
---|
186 |
|
---|
187 | //Can't do this right now (all group icons stored into a single one)
|
---|
188 | case NTRT_CURSOR:
|
---|
189 | case NTRT_GROUP_CURSOR:
|
---|
190 | case NTRT_GROUP_ICON:
|
---|
191 | case NTRT_ICON:
|
---|
192 | dprintf(("Can't convert this resource!!!!!\n"));
|
---|
193 | os2type = RT_POINTER;
|
---|
194 | break;
|
---|
195 |
|
---|
196 | case NTRT_STRING:
|
---|
197 | rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, id, (PPVOID)&resdata);
|
---|
198 | if(rc) {
|
---|
199 | dprintf(("Can't find original string!!!\n"));
|
---|
200 | return(NULL);
|
---|
201 | }
|
---|
202 | winresdata = (char *)malloc(ressize);
|
---|
203 | memcpy(winresdata, resdata, ressize);
|
---|
204 | DosFreeResource(resdata);
|
---|
205 | return((PVOID)((ULONG)winresdata+2)); //skip length word
|
---|
206 |
|
---|
207 | //no conversion necessary
|
---|
208 | case NTRT_RCDATA:
|
---|
209 | case NTRT_VERSION:
|
---|
210 | default:
|
---|
211 | os2type = RT_RCDATA;
|
---|
212 | break;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if(winresdata == NULL) {
|
---|
216 | rc = DosGetResource((HMODULE)module->hinstance, os2type, id, (PPVOID)&resdata);
|
---|
217 | if(rc) {
|
---|
218 | dprintf(("Can't find original string!!!\n"));
|
---|
219 | return(NULL);
|
---|
220 | }
|
---|
221 | winresdata = (char *)malloc(ressize);
|
---|
222 | memcpy(winresdata, resdata, ressize);
|
---|
223 | }
|
---|
224 | if(resdata)
|
---|
225 | DosFreeResource(resdata);
|
---|
226 | return winresdata;
|
---|
227 | }
|
---|
228 | //******************************************************************************
|
---|
229 | //******************************************************************************
|
---|
230 | PVOID Win32Resource::ConvertBitmap(void *bmpdata)
|
---|
231 | {
|
---|
232 | BITMAPFILEHEADER2 *bmphdr = (BITMAPFILEHEADER2 *)bmpdata;
|
---|
233 | WINBITMAPINFOHEADER *winbmphdr;
|
---|
234 | RGBQUAD *rgb;
|
---|
235 | RGB2 *os2rgb;
|
---|
236 | int palsize = 0;
|
---|
237 | int imgsize;
|
---|
238 |
|
---|
239 | if(bmphdr->cbSize != sizeof(BITMAPFILEHEADER2))
|
---|
240 | return(bmpdata); //don't convert OS/2 1.x bitmap
|
---|
241 |
|
---|
242 | if(bmphdr->bmp2.cBitCount < 16) {
|
---|
243 | palsize = (1 << bmphdr->bmp2.cBitCount) * sizeof(RGBQUAD);
|
---|
244 | }
|
---|
245 |
|
---|
246 | // EB: ->>> added imgsize
|
---|
247 | if(bmphdr->bmp2.cbImage == 0)
|
---|
248 | imgsize = CalcBitmapSize(bmphdr->bmp2.cBitCount,
|
---|
249 | bmphdr->bmp2.cx,
|
---|
250 | bmphdr->bmp2.cy);
|
---|
251 | else
|
---|
252 | imgsize = bmphdr->bmp2.cbImage;
|
---|
253 |
|
---|
254 | winbmphdr = (WINBITMAPINFOHEADER *)malloc(sizeof(WINBITMAPINFOHEADER) +
|
---|
255 | imgsize + palsize);
|
---|
256 | memset((char *)winbmphdr, 0, sizeof(WINBITMAPINFOHEADER) + imgsize + palsize);
|
---|
257 |
|
---|
258 | winbmphdr->biSize = sizeof(WINBITMAPINFOHEADER);
|
---|
259 | winbmphdr->biWidth = bmphdr->bmp2.cx;
|
---|
260 | winbmphdr->biHeight = bmphdr->bmp2.cy;
|
---|
261 | winbmphdr->biPlanes = bmphdr->bmp2.cPlanes;
|
---|
262 | winbmphdr->biBitCount = bmphdr->bmp2.cBitCount;
|
---|
263 | //TODO: Identical except for BI_BITFIELDS (3L) type!
|
---|
264 | winbmphdr->biCompression = bmphdr->bmp2.ulCompression;
|
---|
265 | winbmphdr->biSizeImage = bmphdr->bmp2.cbImage; //imgsize;
|
---|
266 | //TODO: Doesn't seem to be completely identical..
|
---|
267 | winbmphdr->biClrUsed = bmphdr->bmp2.cclrUsed;
|
---|
268 | winbmphdr->biClrImportant = bmphdr->bmp2.cclrImportant;
|
---|
269 | winbmphdr->biXPelsPerMeter = bmphdr->bmp2.cxResolution;
|
---|
270 | winbmphdr->biYPelsPerMeter = bmphdr->bmp2.cyResolution;
|
---|
271 |
|
---|
272 | os2rgb = (RGB2 *)(bmphdr+1);
|
---|
273 | rgb = (RGBQUAD *)(winbmphdr+1);
|
---|
274 |
|
---|
275 | if(palsize) {
|
---|
276 | memcpy((char *)rgb, (char *)os2rgb, palsize);
|
---|
277 | os2rgb = (RGB2 *)((int)os2rgb + palsize);
|
---|
278 | rgb = (RGBQUAD *)((int)rgb + palsize);
|
---|
279 | }
|
---|
280 | memcpy((char *)rgb, (char *)os2rgb, imgsize);
|
---|
281 | return((PVOID)winbmphdr);
|
---|
282 | }
|
---|
283 | //******************************************************************************
|
---|
284 | //******************************************************************************
|
---|
285 | void Win32Resource::destroyAll(Win32Image *module)
|
---|
286 | {
|
---|
287 | Win32Resource *res = module->winres, *next;
|
---|
288 |
|
---|
289 | while(res) {
|
---|
290 | next = res->next;
|
---|
291 | delete(res);
|
---|
292 | res = next;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | //******************************************************************************
|
---|
296 | //******************************************************************************
|
---|