1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2008 by Giulio Bernardi
Group cursor resource type
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit groupcursorresource;
{$MODE OBJFPC}
interface
uses
Classes, SysUtils, resource, groupresource;
type
{ TGroupCursorResource }
TGroupCursorResource = class(TGroupResource)
private
function WriteCurCursorHeader(aStream : TStream; const index : integer; const start : longword) : longword;
protected
procedure ReadResourceItemHeader; override;
procedure WriteHeader(aStream : TStream); override;
procedure CreateSubItem; override;
procedure UpdateItemOwner(index : integer); override;
procedure ClearItemList; override;
procedure DeleteSubItems; override;
function GetSubStream(const index : integer; out aSize : int64) : TStream; override;
function GetType : TResourceDesc; override;
function GetName : TResourceDesc; override;
function ChangeDescTypeAllowed(aDesc : TResourceDesc) : boolean; override;
function ChangeDescValueAllowed(aDesc : TResourceDesc) : boolean; override;
public
constructor Create; override;
constructor Create(aType,aName : TResourceDesc); override;
end;
implementation
uses
resfactory, resdatastream, icocurtypes;
type
TCurInfo = record
res : TAbstractResource;
header : TResCursorDir;
end;
PCurInfo = ^TCurInfo;
{ TGroupCursorResource }
procedure TGroupCursorResource.ReadResourceItemHeader;
var pci : PCurInfo;
res : TAbstractResource;
cursorid : word;
begin
if OwnerList=nil then exit;
GetMem(pci,sizeof(TCurInfo));
try
RawData.ReadBuffer(pci^.header,sizeof(TResCursorDir));
cursorid:=pci^.header.cursorId;
{$IFDEF ENDIAN_BIG}
cursorId:=SwapEndian(cursorId);
{$ENDIF}
res:=OwnerList.Find(RT_CURSOR,cursorid,LangID);
pci^.res:=res;
SetChildOwner(res);
fItemList.Add(pci);
except
FreeMem(pci);
raise;
end;
end;
function TGroupCursorResource.WriteCurCursorHeader(aStream: TStream;
const index: integer; const start: longword): longword;
var pci : PCurInfo;
hdr : TCurCursorDir;
tmpw,tmph : word;
begin
pci:=PCurInfo(fItemList[index]);
tmpw:=pci^.header.width;
tmph:=pci^.header.height;
{$IFDEF ENDIAN_BIG}
tmpw:=SwapEndian(tmpw);
tmph:=SwapEndian(tmph);
{$ENDIF}
tmph:=tmph div 2; //in cursor resources, height is doubled.
hdr.width:=tmpw; //it's a byte now, no need to swap
hdr.height:=tmph; //it's a byte now, no need to swap
hdr.reserved:=0;
hdr.bytesincur:=pci^.header.bytesinres;
hdr.curoffset:=start;
pci^.res.RawData.Position:=0;
pci^.res.RawData.ReadBuffer(hdr.xhotspot,2);
pci^.res.RawData.ReadBuffer(hdr.yhotspot,2);
{$IFDEF ENDIAN_BIG}
hdr.curoffset:=SwapEndian(hdr.curoffset);
hdr.bytesincur:=SwapEndian(hdr.bytesincur);
{$ENDIF}
dec(hdr.bytesincur,4); //in resources, cursor has 2 words more for hotspots
{$IFDEF ENDIAN_BIG}
hdr.bytesincur:=SwapEndian(hdr.bytesincur);
{$ENDIF}
aStream.WriteBuffer(hdr,sizeof(hdr));
Result:=start+pci^.res.RawData.Size-4;
end;
procedure TGroupCursorResource.WriteHeader(aStream: TStream);
var nh : TNewHeader;
i : integer;
addrcount : longword;
begin
//write CUR file header (identical to the resource cursor header)
nh.reserved:=0;
nh.restype:=RES_CURSOR;
nh.rescount:=fItemList.Count;
{$IFDEF ENDIAN_BIG}
nh.reserved:=SwapEndian(nh.reserved);
nh.restype:=SwapEndian(nh.restype);
nh.rescount:=SwapEndian(nh.rescount);
{$ENDIF}
aStream.Position:=0;
aStream.WriteBuffer(nh,sizeof(nh));
addrcount:=sizeof(TNewHeader)+sizeof(TCurCursorDir)*fItemList.Count;
for i:=0 to fItemList.Count-1 do
addrcount:=WriteCurCursorHeader(aStream,i,addrcount);
end;
procedure TGroupCursorResource.ClearItemList;
var pci : PCurInfo;
i : integer;
begin
if fItemList=nil then exit;
for i:=0 to fItemList.Count-1 do
begin
pci:=PCurInfo(fItemList[i]);
//if we are not in a TResources, free all subitems by ourselves.
if OwnerList=nil then pci^.res.Free;
FreeMem(pci);
end;
fItemList.Clear;
end;
procedure TGroupCursorResource.DeleteSubItems;
var pci : PCurInfo;
i : integer;
begin
if fItemList=nil then exit;
for i:=0 to fItemList.Count-1 do
begin
pci:=PCurInfo(fItemList[i]);
if OwnerList<>nil then
OwnerList.Remove(pci^.res);
pci^.res.Free;
FreeMem(pci);
end;
fItemList.Clear;
end;
procedure TGroupCursorResource.CreateSubItem;
var res : TAbstractResource;
pci : PCurInfo;
curhdr : TCurCursorDir;
oldpos : int64;
bytesinres : longword;
curoffset : longword;
index : word;
begin
index:=fItemList.Count+1;
dummyName.ID:=index;
res:=TResourceFactory.CreateResource(dummyType,dummyName);
res.LangID:=LangID;
if OwnerList<>nil then
index:=OwnerList.AddAutoID(res);
GetMem(pci,sizeof(TCurInfo));
fItemList.Add(pci);
pci^.res:=res;
ItemData.ReadBuffer(curhdr,sizeof(TCurCursorDir));
pci^.header.width:=curhdr.width; //it was a byte, no need to swap
pci^.header.height:=curhdr.height*2; //in cursor resources, height is doubled.
pci^.header.planes:=1;
pci^.header.bitcount:=1;
pci^.header.cursorId:=index;
bytesinres:=curhdr.bytesincur;
curoffset:=curhdr.curoffset;
{$IFDEF ENDIAN_BIG}
bytesinres:=SwapEndian(bytesinres);
curoffset:=SwapEndian(curoffset);
{$ENDIF}
oldpos:=ItemData.Position;
try
ItemData.Position:=curoffset;
res.RawData.Size:=0;
res.RawData.Position:=0;
res.RawData.WriteBuffer(curhdr.xhotspot,2);
res.RawData.WriteBuffer(curhdr.yhotspot,2);
res.RawData.CopyFrom(ItemData,bytesinres);
finally
ItemData.Position:=oldpos;
end;
inc(bytesinres,4); //in resources, cursor has 2 words more for hotspots
pci^.header.bytesinres:=bytesinres;
{$IFDEF ENDIAN_BIG}
pci^.header.width:=SwapEndian(pci^.header.width);
pci^.header.height:=SwapEndian(pci^.header.height);
pci^.header.planes:=SwapEndian(pci^.header.planes);
pci^.header.bitcount:=SwapEndian(pci^.header.bitcount);
pci^.header.bytesinres:=SwapEndian(pci^.header.bytesinres);
pci^.header.cursorId:=SwapEndian(pci^.header.cursorId);
{$ENDIF}
RawData.WriteBuffer(pci^.header,sizeof(TResCursorDir));
end;
procedure TGroupCursorResource.UpdateItemOwner(index: integer);
var pci : PCurInfo;
theid : word;
oldpos : int64;
begin
pci:=PCurInfo(fItemList[index]);
if pci^.res.OwnerList=OwnerList then exit;
if OwnerList=nil then
begin
pci^.res.OwnerList.Remove(pci^.res);
exit;
end;
theid:=pci^.res.Name.ID;
OwnerList.AddAutoID(pci^.res);
if theid<>pci^.res.Name.ID then //id changed, update
begin
theid:=pci^.res.Name.ID;
pci^.header.cursorId:=theid; //update header id value
{$IFDEF ENDIAN_BIG}
pci^.header.cursorId:=SwapEndian(pci^.header.cursorId);
{$ENDIF}
//update id in rawdata (ItemStream, if present, is ok)
if (fItemData=nil) or TResourceDataStream(ItemData).Cached then
begin
oldpos:=RawData.Position;
try
RawData.Position:=sizeof(TNewHeader)+(index+1)*sizeof(TResCursorDir)-2;
RawData.WriteBuffer(pci^.header.cursorId,2);
finally
RawData.Position:=oldpos;
end;
end;
end;
end;
function TGroupCursorResource.GetSubStream(const index: integer; out aSize : int64): TStream;
begin
Result:=PCurInfo(fItemList[index])^.res.RawData;
Result.Position:=4;
aSize:=Result.Size-4;
end;
function TGroupCursorResource.GetType: TResourceDesc;
begin
Result:=fType;
end;
function TGroupCursorResource.GetName: TResourceDesc;
begin
Result:=fName;
end;
function TGroupCursorResource.ChangeDescTypeAllowed(aDesc: TResourceDesc
): boolean;
begin
Result:=aDesc=fName;
end;
function TGroupCursorResource.ChangeDescValueAllowed(aDesc: TResourceDesc
): boolean;
begin
Result:=aDesc=fName;
end;
constructor TGroupCursorResource.Create;
begin
inherited Create;
fItemList:=nil;
fItemData:=nil;
fType:=TResourceDesc.Create(RT_GROUP_CURSOR);
fName:=TResourceDesc.Create(1);
SetDescOwner(fType);
SetDescOwner(fName);
dummyType:=TResourceDesc.Create(RT_CURSOR);
dummyName:=TResourceDesc.Create(1);
end;
constructor TGroupCursorResource.Create(aType, aName: TResourceDesc);
begin
Create;
fName.Assign(aName);
end;
initialization
TResourceFactory.RegisterResourceClass(RT_GROUP_CURSOR,TGroupCursorResource);
end.
|