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

Last change on this file since 4 was 4, checked in by ktk, 26 years ago

Import

File size: 7.1 KB
Line 
1/* $Id: winres.cpp,v 1.1 1999-05-24 20:19:50 ktk 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//******************************************************************************
35//******************************************************************************
36Win32Resource::Win32Resource(Win32Image *module, HRSRC hRes, ULONG id, ULONG type) :
37 os2resdata(NULL), winresdata(NULL)
38{
39 APIRET rc;
40
41 next = module->winres;
42 module->winres = this;
43
44 this->module = module;
45 this->id = id;
46 this->type = type;
47 this->hres = hRes;
48
49 rc = DosQueryResourceSize(module->hinstance, type, id, &ressize);
50 if(rc) {
51 dprintf(("Win32Resource ctor: DosQueryResourceSize returned %X\n", rc));
52 ressize = 0;
53 }
54}
55//******************************************************************************
56//******************************************************************************
57Win32Resource::Win32Resource(Win32Image *module, ULONG id, ULONG type,
58 ULONG size, char *resdata) : hres(NULL),
59 os2resdata(NULL), winresdata(NULL)
60{
61 next = module->winres;
62 module->winres = this;
63
64 this->module = module;
65 this->id = id;
66 this->type = type;
67 this->ressize = size;
68 winresdata = (char *)malloc(size);
69 if(winresdata == NULL) {
70 DebugInt3();
71 return;
72 }
73 memcpy(winresdata, resdata, size);
74}
75//******************************************************************************
76//******************************************************************************
77Win32Resource::~Win32Resource()
78{
79 Win32Resource *res = module->winres;
80
81 if(os2resdata) free(os2resdata);
82 if(winresdata) free(winresdata);
83
84 if(res == this) {
85 module->winres = res->next;
86 }
87 else {
88 while(res->next != this) {
89 res = res->next;
90 }
91 if(res)
92 res->next = next;
93 }
94}
95//******************************************************************************
96//******************************************************************************
97PVOID Win32Resource::lockResource()
98{
99 int restype = 0, newid;
100 void *resdata = NULL;
101 APIRET rc;
102 ULONG os2type = RT_RCDATA;
103
104 dprintf(("Win32Resource::lockResource %d\n", id));
105 if(winresdata)
106 return(winresdata);
107
108 switch(type) {
109 case NTRT_BITMAP:
110 rc = DosGetResource((HMODULE)module->hinstance, RT_BITMAP, id, (PPVOID)&resdata);
111 if(rc) return(NULL);
112 winresdata = ConvertBitmap((BITMAPFILEHEADER2 *)resdata);
113 break;
114
115 case NTRT_ACCELERATORS:
116 case NTRT_MENU:
117 case NTRT_DIALOG:
118 newid = module->getWin32ResourceId(id);
119
120 rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, (int)newid, (PPVOID)&resdata);
121 if(rc) {
122 dprintf(("Can't find original resource!!!\n"));
123 return(NULL);
124 }
125 winresdata = (char *)malloc(ressize);
126 memcpy(winresdata, resdata, ressize);
127 break;
128
129 //TODO:not yet implemented
130 case NTRT_FONTDIR:
131 case NTRT_FONT:
132 case NTRT_MESSAGETABLE:
133 case NTRT_NEWBITMAP:
134 case NTRT_NEWMENU:
135 case NTRT_NEWDIALOG:
136 os2type = RT_RCDATA;
137 break;
138
139 //Can't do this right now (all group icons stored into a single one)
140 case NTRT_CURSOR:
141 case NTRT_GROUP_CURSOR:
142 case NTRT_GROUP_ICON:
143 case NTRT_ICON:
144 dprintf(("Can't convert this resource!!!!!\n"));
145 os2type = RT_POINTER;
146 break;
147
148 case NTRT_STRING:
149 rc = DosGetResource((HMODULE)module->hinstance, RT_RCDATA, id, (PPVOID)&resdata);
150 if(rc) {
151 dprintf(("Can't find original string!!!\n"));
152 return(NULL);
153 }
154 winresdata = (char *)malloc(ressize);
155 memcpy(winresdata, resdata, ressize);
156 DosFreeResource(resdata);
157 return((PVOID)((ULONG)winresdata+2)); //skip length word
158
159 //no conversion necessary
160 case NTRT_RCDATA:
161 case NTRT_VERSION:
162 default:
163 os2type = RT_RCDATA;
164 break;
165 }
166
167 if(winresdata == NULL) {
168 rc = DosGetResource((HMODULE)module->hinstance, os2type, id, (PPVOID)&resdata);
169 if(rc) {
170 dprintf(("Can't find original string!!!\n"));
171 return(NULL);
172 }
173 winresdata = (char *)malloc(ressize);
174 memcpy(winresdata, resdata, ressize);
175 }
176 if(resdata)
177 DosFreeResource(resdata);
178 return winresdata;
179}
180//******************************************************************************
181//******************************************************************************
182PVOID Win32Resource::ConvertBitmap(void *bmpdata)
183{
184 BITMAPFILEHEADER2 *bmphdr = (BITMAPFILEHEADER2 *)bmpdata;
185 WINBITMAPINFOHEADER *winbmphdr;
186 RGBQUAD *rgb;
187 RGB2 *os2rgb;
188 int palsize = 0;
189
190 if(bmphdr->cbSize != sizeof(BITMAPFILEHEADER2))
191 return(bmpdata); //don't convert OS/2 1.x bitmap
192
193 if(bmphdr->bmp2.cBitCount < 16) {
194 palsize = (1 << bmphdr->bmp2.cBitCount) * sizeof(RGBQUAD);
195 }
196 winbmphdr = (WINBITMAPINFOHEADER *)malloc(sizeof(WINBITMAPINFOHEADER) +
197 bmphdr->bmp2.cbImage + palsize);
198 memset((char *)winbmphdr, 0, sizeof(WINBITMAPINFOHEADER) + bmphdr->bmp2.cbImage +
199 palsize);
200
201 winbmphdr->biSize = sizeof(WINBITMAPINFOHEADER);
202 winbmphdr->biWidth = bmphdr->bmp2.cx;
203 winbmphdr->biHeight = bmphdr->bmp2.cy;
204 winbmphdr->biPlanes = bmphdr->bmp2.cPlanes;
205 winbmphdr->biBitCount = bmphdr->bmp2.cBitCount;
206 //TODO: Identical except for BI_BITFIELDS (3L) type!
207 winbmphdr->biCompression = bmphdr->bmp2.ulCompression;
208 winbmphdr->biSizeImage = bmphdr->bmp2.cbImage;
209 //TODO: Doesn't seem to be completely identical..
210 winbmphdr->biClrUsed = bmphdr->bmp2.cclrUsed;
211 winbmphdr->biClrImportant = bmphdr->bmp2.cclrImportant;
212 winbmphdr->biXPelsPerMeter = bmphdr->bmp2.cxResolution;
213 winbmphdr->biYPelsPerMeter = bmphdr->bmp2.cyResolution;
214
215 os2rgb = (RGB2 *)(bmphdr+1);
216 rgb = (RGBQUAD *)(winbmphdr+1);
217
218 if(palsize) {
219 memcpy((char *)rgb, (char *)os2rgb, palsize);
220 os2rgb = (RGB2 *)((int)os2rgb + palsize);
221 rgb = (RGBQUAD *)((int)rgb + palsize);
222 }
223 memcpy((char *)rgb, (char *)os2rgb, winbmphdr->biSizeImage);
224 return((PVOID)winbmphdr);
225}
226//******************************************************************************
227//******************************************************************************
228void Win32Resource::destroyAll(Win32Image *module)
229{
230 Win32Resource *res = module->winres, *next;
231
232 while(res) {
233 next = res->next;
234 delete(res);
235 res = next;
236 }
237}
238//******************************************************************************
239//******************************************************************************
Note: See TracBrowser for help on using the repository browser.