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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2008 by Giulio Bernardi
Group icon 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 groupiconresource;
{$MODE OBJFPC}
interface
uses
Classes, SysUtils, resource, groupresource;
type
{ TGroupIconResource }
TGroupIconResource = class(TGroupResource)
private
function WriteIcoIconHeader(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
TIconInfo = record
res : TAbstractResource;
header : TIconDir;
end;
PIconInfo = ^TIconInfo;
{ TGroupIconResource }
procedure TGroupIconResource.ReadResourceItemHeader;
var pii : PIconInfo;
res : TAbstractResource;
offsetid : longword;
begin
if OwnerList=nil then exit;
GetMem(pii,sizeof(TIconInfo));
try
//TIconDir is slightly different in resources and in .ico files
pii^.header.offsetId:=0;
RawData.ReadBuffer(pii^.header,sizeof(TIconDir)-2);
offsetid:=pii^.header.offsetId;
{$IFDEF ENDIAN_BIG}
offsetId:=SwapEndian(offsetId);
{$ENDIF}
res:=OwnerList.Find(RT_ICON,offsetID,LangID);
pii^.res:=res;
SetChildOwner(res);
fItemList.Add(pii);
except
FreeMem(pii);
raise;
end;
end;
function TGroupIconResource.WriteIcoIconHeader(aStream : TStream;
const index : integer; const start : longword) : longword;
var pii : PIconInfo;
hdr : TIconDir;
begin
pii:=PIconInfo(fItemList[index]);
hdr:=pii^.header;
hdr.offsetId:=start;
{$IFDEF ENDIAN_BIG}
hdr.offsetId:=SwapEndian(hdr.offsetId);
{$ENDIF}
aStream.WriteBuffer(hdr,sizeof(hdr));
Result:=start+pii^.res.RawData.Size;
end;
procedure TGroupIconResource.WriteHeader(aStream: TStream);
var nh : TNewHeader;
i : integer;
addrcount : longword;
begin
//write ICO file header (identical to the resource icon header)
nh.reserved:=0;
nh.restype:=RES_ICON;
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(TIconDir)*fItemList.Count;
for i:=0 to fItemList.Count-1 do
addrcount:=WriteIcoIconHeader(aStream,i,addrcount);
end;
procedure TGroupIconResource.ClearItemList;
var pii : PIconInfo;
i : integer;
begin
if fItemList=nil then exit;
for i:=0 to fItemList.Count-1 do
begin
pii:=PIconInfo(fItemList[i]);
//if we are not in a TResources, free all subitems by ourselves.
if OwnerList=nil then pii^.res.Free;
FreeMem(pii);
end;
fItemList.Clear;
end;
procedure TGroupIconResource.DeleteSubItems;
var pii : PIconInfo;
i : integer;
begin
if fItemList=nil then exit;
for i:=0 to fItemList.Count-1 do
begin
pii:=PIconInfo(fItemList[i]);
if OwnerList<>nil then
OwnerList.Remove(pii^.res);
pii^.res.Free;
FreeMem(pii);
end;
fItemList.Clear;
end;
procedure TGroupIconResource.CreateSubItem;
var res : TAbstractResource;
pii : PIconInfo;
oldpos : int64;
bytesinres : longword;
offsetid : 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(pii,sizeof(TIconInfo));
fItemList.Add(pii);
pii^.res:=res;
ItemData.ReadBuffer(pii^.header,sizeof(TIconDir));
bytesinres:=pii^.header.bytesinres;
offsetid:=pii^.header.offsetid;
{$IFDEF ENDIAN_BIG}
bytesinres:=SwapEndian(bytesinres);
offsetID:=SwapEndian(offsetID);
{$ENDIF}
oldpos:=ItemData.Position;
try
ItemData.Position:=offsetid;
res.RawData.Size:=0;
res.RawData.Position:=0;
res.RawData.CopyFrom(ItemData,bytesinres);
finally
ItemData.Position:=oldpos;
end;
pii^.header.offsetId:=index;
{$IFDEF ENDIAN_BIG}
pii^.header.offsetID:=SwapEndian(pii^.header.offsetID);
{$ENDIF}
//TIconDir is slightly different in resources and in .ico files
RawData.WriteBuffer(pii^.header,sizeof(TIconDir)-2);
end;
procedure TGroupIconResource.UpdateItemOwner(index: integer);
var pii : PIconInfo;
theid : longword;
oldpos : int64;
begin
pii:=PIconInfo(fItemList[index]);
if pii^.res.OwnerList=OwnerList then exit;
if OwnerList=nil then
begin
pii^.res.OwnerList.Remove(pii^.res);
exit;
end;
theid:=pii^.res.Name.ID;
OwnerList.AddAutoID(pii^.res);
if theid<>pii^.res.Name.ID then //id changed, update
begin
theid:=pii^.res.Name.ID;
pii^.header.offsetId:=theid; //update header id value
{$IFDEF ENDIAN_BIG}
pii^.header.offsetID:=SwapEndian(pii^.header.offsetID);
{$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(TIconDir)-2)-2;
RawData.WriteBuffer(pii^.header.offsetID,2); //update id (it's a word)
finally
RawData.Position:=oldpos;
end;
end;
end;
end;
function TGroupIconResource.GetSubStream(const index: integer; out aSize : int64): TStream;
begin
Result:=PIconInfo(fItemList[index])^.res.RawData;
Result.Position:=0;
aSize:=Result.Size;
end;
function TGroupIconResource.GetType: TResourceDesc;
begin
Result:=fType;
end;
function TGroupIconResource.GetName: TResourceDesc;
begin
Result:=fName;
end;
function TGroupIconResource.ChangeDescTypeAllowed(aDesc: TResourceDesc
): boolean;
begin
Result:=aDesc=fName;
end;
function TGroupIconResource.ChangeDescValueAllowed(aDesc: TResourceDesc
): boolean;
begin
Result:=aDesc=fName;
end;
constructor TGroupIconResource.Create;
begin
inherited Create;
fItemList:=nil;
fItemData:=nil;
fType:=TResourceDesc.Create(RT_GROUP_ICON);
fName:=TResourceDesc.Create(1);
SetDescOwner(fType);
SetDescOwner(fName);
dummyType:=TResourceDesc.Create(RT_ICON);
dummyName:=TResourceDesc.Create(1);
end;
constructor TGroupIconResource.Create(aType, aName: TResourceDesc);
begin
Create;
fName.Assign(aName);
end;
initialization
TResourceFactory.RegisterResourceClass(RT_GROUP_ICON,TGroupIconResource);
end.
|